xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 5374c334d64f8e1dfc4aadcbcd3a1090fbe39acb)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 #include <linux/bnxt/hsi.h>
62 
63 #include "bnxt.h"
64 #include "bnxt_hwrm.h"
65 #include "bnxt_ulp.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 
78 #define BNXT_TX_TIMEOUT		(5 * HZ)
79 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
80 				 NETIF_MSG_TX_ERR)
81 
82 MODULE_IMPORT_NS("NETDEV_INTERNAL");
83 MODULE_LICENSE("GPL");
84 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
85 
86 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
87 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
88 
89 #define BNXT_TX_PUSH_THRESH 164
90 
91 /* indexed by enum board_idx */
92 static const struct {
93 	char *name;
94 } board_info[] = {
95 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
96 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
97 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
98 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
99 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
100 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
101 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
102 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
103 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
104 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
105 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
106 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
108 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
109 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
110 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
111 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
112 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
113 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
114 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
115 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
116 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
117 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
118 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
119 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
120 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
121 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
122 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
123 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
124 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
127 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
129 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
130 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
131 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
132 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
133 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
134 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
135 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
136 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
137 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
138 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
139 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
140 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
141 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
142 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
143 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
145 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
146 };
147 
148 static const struct pci_device_id bnxt_pci_tbl[] = {
149 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
150 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
157 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
183 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
198 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
199 #ifdef CONFIG_BNXT_SRIOV
200 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
218 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
219 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
220 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
221 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
223 #endif
224 	{ 0 }
225 };
226 
227 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
228 
229 static const u16 bnxt_vf_req_snif[] = {
230 	HWRM_FUNC_CFG,
231 	HWRM_FUNC_VF_CFG,
232 	HWRM_PORT_PHY_QCFG,
233 	HWRM_CFA_L2_FILTER_ALLOC,
234 };
235 
236 static const u16 bnxt_async_events_arr[] = {
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
240 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
241 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
246 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
247 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
248 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
249 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
250 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
253 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
254 };
255 
256 const u16 bnxt_bstore_to_trace[] = {
257 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
258 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
259 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
260 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
261 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
262 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
263 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
264 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
265 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
266 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
267 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
268 	[BNXT_CTX_KONG]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE,
269 	[BNXT_CTX_QPC]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE,
270 };
271 
272 static struct workqueue_struct *bnxt_pf_wq;
273 
274 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
275 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
276 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
277 
278 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
279 	.ports = {
280 		.src = 0,
281 		.dst = 0,
282 	},
283 	.addrs = {
284 		.v6addrs = {
285 			.src = BNXT_IPV6_MASK_NONE,
286 			.dst = BNXT_IPV6_MASK_NONE,
287 		},
288 	},
289 };
290 
291 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
292 	.ports = {
293 		.src = cpu_to_be16(0xffff),
294 		.dst = cpu_to_be16(0xffff),
295 	},
296 	.addrs = {
297 		.v6addrs = {
298 			.src = BNXT_IPV6_MASK_ALL,
299 			.dst = BNXT_IPV6_MASK_ALL,
300 		},
301 	},
302 };
303 
304 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
305 	.ports = {
306 		.src = cpu_to_be16(0xffff),
307 		.dst = cpu_to_be16(0xffff),
308 	},
309 	.addrs = {
310 		.v4addrs = {
311 			.src = cpu_to_be32(0xffffffff),
312 			.dst = cpu_to_be32(0xffffffff),
313 		},
314 	},
315 };
316 
317 static bool bnxt_vf_pciid(enum board_idx idx)
318 {
319 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
320 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
321 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
322 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
323 		idx == NETXTREME_E_P7_VF_HV);
324 }
325 
326 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
327 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
328 
329 #define BNXT_DB_CQ(db, idx)						\
330 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
331 
332 #define BNXT_DB_NQ_P5(db, idx)						\
333 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
334 		    (db)->doorbell)
335 
336 #define BNXT_DB_NQ_P7(db, idx)						\
337 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
338 		    DB_RING_IDX(db, idx), (db)->doorbell)
339 
340 #define BNXT_DB_CQ_ARM(db, idx)						\
341 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
342 
343 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
344 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
345 		    DB_RING_IDX(db, idx), (db)->doorbell)
346 
347 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
348 {
349 	if (bp->flags & BNXT_FLAG_CHIP_P7)
350 		BNXT_DB_NQ_P7(db, idx);
351 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
352 		BNXT_DB_NQ_P5(db, idx);
353 	else
354 		BNXT_DB_CQ(db, idx);
355 }
356 
357 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
358 {
359 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
360 		BNXT_DB_NQ_ARM_P5(db, idx);
361 	else
362 		BNXT_DB_CQ_ARM(db, idx);
363 }
364 
365 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
366 {
367 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
368 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
369 			    DB_RING_IDX(db, idx), db->doorbell);
370 	else
371 		BNXT_DB_CQ(db, idx);
372 }
373 
374 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
375 {
376 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
377 		return;
378 
379 	if (BNXT_PF(bp))
380 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
381 	else
382 		schedule_delayed_work(&bp->fw_reset_task, delay);
383 }
384 
385 static void __bnxt_queue_sp_work(struct bnxt *bp)
386 {
387 	if (BNXT_PF(bp))
388 		queue_work(bnxt_pf_wq, &bp->sp_task);
389 	else
390 		schedule_work(&bp->sp_task);
391 }
392 
393 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
394 {
395 	set_bit(event, &bp->sp_event);
396 	__bnxt_queue_sp_work(bp);
397 }
398 
399 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
400 {
401 	if (!rxr->bnapi->in_reset) {
402 		rxr->bnapi->in_reset = true;
403 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
404 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
405 		else
406 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
407 		__bnxt_queue_sp_work(bp);
408 	}
409 	rxr->rx_next_cons = 0xffff;
410 }
411 
412 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
413 			  u16 curr)
414 {
415 	struct bnxt_napi *bnapi = txr->bnapi;
416 
417 	if (bnapi->tx_fault)
418 		return;
419 
420 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
421 		   txr->txq_index, txr->tx_hw_cons,
422 		   txr->tx_cons, txr->tx_prod, curr);
423 	WARN_ON_ONCE(1);
424 	bnapi->tx_fault = 1;
425 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
426 }
427 
428 const u16 bnxt_lhint_arr[] = {
429 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
430 	TX_BD_FLAGS_LHINT_512_TO_1023,
431 	TX_BD_FLAGS_LHINT_1024_TO_2047,
432 	TX_BD_FLAGS_LHINT_1024_TO_2047,
433 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
434 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
444 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
445 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
446 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
447 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
448 };
449 
450 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
451 {
452 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
453 
454 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
455 		return 0;
456 
457 	return md_dst->u.port_info.port_id;
458 }
459 
460 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
461 			     u16 prod)
462 {
463 	/* Sync BD data before updating doorbell */
464 	wmb();
465 	bnxt_db_write(bp, &txr->tx_db, prod);
466 	txr->kick_pending = 0;
467 }
468 
469 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
470 {
471 	struct bnxt *bp = netdev_priv(dev);
472 	struct tx_bd *txbd, *txbd0;
473 	struct tx_bd_ext *txbd1;
474 	struct netdev_queue *txq;
475 	int i;
476 	dma_addr_t mapping;
477 	unsigned int length, pad = 0;
478 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
479 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
480 	struct pci_dev *pdev = bp->pdev;
481 	u16 prod, last_frag, txts_prod;
482 	struct bnxt_tx_ring_info *txr;
483 	struct bnxt_sw_tx_bd *tx_buf;
484 	__le32 lflags = 0;
485 	skb_frag_t *frag;
486 
487 	i = skb_get_queue_mapping(skb);
488 	if (unlikely(i >= bp->tx_nr_rings)) {
489 		dev_kfree_skb_any(skb);
490 		dev_core_stats_tx_dropped_inc(dev);
491 		return NETDEV_TX_OK;
492 	}
493 
494 	txq = netdev_get_tx_queue(dev, i);
495 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
496 	prod = txr->tx_prod;
497 
498 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
499 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
500 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
501 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
502 		if (skb_linearize(skb)) {
503 			dev_kfree_skb_any(skb);
504 			dev_core_stats_tx_dropped_inc(dev);
505 			return NETDEV_TX_OK;
506 		}
507 	}
508 #endif
509 	free_size = bnxt_tx_avail(bp, txr);
510 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
511 		/* We must have raced with NAPI cleanup */
512 		if (net_ratelimit() && txr->kick_pending)
513 			netif_warn(bp, tx_err, dev,
514 				   "bnxt: ring busy w/ flush pending!\n");
515 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
516 					bp->tx_wake_thresh))
517 			return NETDEV_TX_BUSY;
518 	}
519 
520 	length = skb->len;
521 	len = skb_headlen(skb);
522 	last_frag = skb_shinfo(skb)->nr_frags;
523 
524 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
525 
526 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
527 	tx_buf->skb = skb;
528 	tx_buf->nr_frags = last_frag;
529 
530 	vlan_tag_flags = 0;
531 	cfa_action = bnxt_xmit_get_cfa_action(skb);
532 	if (skb_vlan_tag_present(skb)) {
533 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
534 				 skb_vlan_tag_get(skb);
535 		/* Currently supports 8021Q, 8021AD vlan offloads
536 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
537 		 */
538 		if (skb->vlan_proto == htons(ETH_P_8021Q))
539 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
540 	}
541 
542 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
543 	    ptp->tx_tstamp_en) {
544 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
545 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
546 			tx_buf->is_ts_pkt = 1;
547 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
548 		} else if (!skb_is_gso(skb)) {
549 			u16 seq_id, hdr_off;
550 
551 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
552 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
553 				if (vlan_tag_flags)
554 					hdr_off += VLAN_HLEN;
555 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
556 				tx_buf->is_ts_pkt = 1;
557 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
558 
559 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
560 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
561 				tx_buf->txts_prod = txts_prod;
562 			}
563 		}
564 	}
565 	if (unlikely(skb->no_fcs))
566 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
567 
568 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
569 	    skb_frags_readable(skb) && !lflags) {
570 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
571 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
572 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
573 		void __iomem *db = txr->tx_db.doorbell;
574 		void *pdata = tx_push_buf->data;
575 		u64 *end;
576 		int j, push_len;
577 
578 		/* Set COAL_NOW to be ready quickly for the next push */
579 		tx_push->tx_bd_len_flags_type =
580 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
581 					TX_BD_TYPE_LONG_TX_BD |
582 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
583 					TX_BD_FLAGS_COAL_NOW |
584 					TX_BD_FLAGS_PACKET_END |
585 					TX_BD_CNT(2));
586 
587 		if (skb->ip_summed == CHECKSUM_PARTIAL)
588 			tx_push1->tx_bd_hsize_lflags =
589 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
590 		else
591 			tx_push1->tx_bd_hsize_lflags = 0;
592 
593 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
594 		tx_push1->tx_bd_cfa_action =
595 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
596 
597 		end = pdata + length;
598 		end = PTR_ALIGN(end, 8) - 1;
599 		*end = 0;
600 
601 		skb_copy_from_linear_data(skb, pdata, len);
602 		pdata += len;
603 		for (j = 0; j < last_frag; j++) {
604 			void *fptr;
605 
606 			frag = &skb_shinfo(skb)->frags[j];
607 			fptr = skb_frag_address_safe(frag);
608 			if (!fptr)
609 				goto normal_tx;
610 
611 			memcpy(pdata, fptr, skb_frag_size(frag));
612 			pdata += skb_frag_size(frag);
613 		}
614 
615 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
616 		txbd->tx_bd_haddr = txr->data_mapping;
617 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
618 		prod = NEXT_TX(prod);
619 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
620 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
621 		memcpy(txbd, tx_push1, sizeof(*txbd));
622 		prod = NEXT_TX(prod);
623 		tx_push->doorbell =
624 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
625 				    DB_RING_IDX(&txr->tx_db, prod));
626 		WRITE_ONCE(txr->tx_prod, prod);
627 
628 		tx_buf->is_push = 1;
629 		netdev_tx_sent_queue(txq, skb->len);
630 		wmb();	/* Sync is_push and byte queue before pushing data */
631 
632 		push_len = (length + sizeof(*tx_push) + 7) / 8;
633 		if (push_len > 16) {
634 			__iowrite64_copy(db, tx_push_buf, 16);
635 			__iowrite32_copy(db + 4, tx_push_buf + 1,
636 					 (push_len - 16) << 1);
637 		} else {
638 			__iowrite64_copy(db, tx_push_buf, push_len);
639 		}
640 
641 		goto tx_done;
642 	}
643 
644 normal_tx:
645 	if (length < BNXT_MIN_PKT_SIZE) {
646 		pad = BNXT_MIN_PKT_SIZE - length;
647 		if (skb_pad(skb, pad))
648 			/* SKB already freed. */
649 			goto tx_kick_pending;
650 		length = BNXT_MIN_PKT_SIZE;
651 	}
652 
653 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
654 
655 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
656 		goto tx_free;
657 
658 	dma_unmap_addr_set(tx_buf, mapping, mapping);
659 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
660 		TX_BD_CNT(last_frag + 2);
661 
662 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
663 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
664 
665 	prod = NEXT_TX(prod);
666 	txbd1 = (struct tx_bd_ext *)
667 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
668 
669 	txbd1->tx_bd_hsize_lflags = lflags;
670 	if (skb_is_gso(skb)) {
671 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
672 		u32 hdr_len;
673 
674 		if (skb->encapsulation) {
675 			if (udp_gso)
676 				hdr_len = skb_inner_transport_offset(skb) +
677 					  sizeof(struct udphdr);
678 			else
679 				hdr_len = skb_inner_tcp_all_headers(skb);
680 		} else if (udp_gso) {
681 			hdr_len = skb_transport_offset(skb) +
682 				  sizeof(struct udphdr);
683 		} else {
684 			hdr_len = skb_tcp_all_headers(skb);
685 		}
686 
687 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
688 					TX_BD_FLAGS_T_IPID |
689 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
690 		length = skb_shinfo(skb)->gso_size;
691 		txbd1->tx_bd_mss = cpu_to_le32(length);
692 		length += hdr_len;
693 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
694 		txbd1->tx_bd_hsize_lflags |=
695 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
696 		txbd1->tx_bd_mss = 0;
697 	}
698 
699 	length >>= 9;
700 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
701 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
702 				     skb->len);
703 		i = 0;
704 		goto tx_dma_error;
705 	}
706 	flags |= bnxt_lhint_arr[length];
707 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
708 
709 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
710 	txbd1->tx_bd_cfa_action =
711 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
712 	txbd0 = txbd;
713 	for (i = 0; i < last_frag; i++) {
714 		frag = &skb_shinfo(skb)->frags[i];
715 		prod = NEXT_TX(prod);
716 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
717 
718 		len = skb_frag_size(frag);
719 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
720 					   DMA_TO_DEVICE);
721 
722 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
723 			goto tx_dma_error;
724 
725 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
726 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
727 					  mapping, mapping);
728 
729 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
730 
731 		flags = len << TX_BD_LEN_SHIFT;
732 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
733 	}
734 
735 	flags &= ~TX_BD_LEN;
736 	txbd->tx_bd_len_flags_type =
737 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
738 			    TX_BD_FLAGS_PACKET_END);
739 
740 	netdev_tx_sent_queue(txq, skb->len);
741 
742 	skb_tx_timestamp(skb);
743 
744 	prod = NEXT_TX(prod);
745 	WRITE_ONCE(txr->tx_prod, prod);
746 
747 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
748 		bnxt_txr_db_kick(bp, txr, prod);
749 	} else {
750 		if (free_size >= bp->tx_wake_thresh)
751 			txbd0->tx_bd_len_flags_type |=
752 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
753 		txr->kick_pending = 1;
754 	}
755 
756 tx_done:
757 
758 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
759 		if (netdev_xmit_more() && !tx_buf->is_push) {
760 			txbd0->tx_bd_len_flags_type &=
761 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
762 			bnxt_txr_db_kick(bp, txr, prod);
763 		}
764 
765 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
766 				   bp->tx_wake_thresh);
767 	}
768 	return NETDEV_TX_OK;
769 
770 tx_dma_error:
771 	last_frag = i;
772 
773 	/* start back at beginning and unmap skb */
774 	prod = txr->tx_prod;
775 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
776 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
777 			 skb_headlen(skb), DMA_TO_DEVICE);
778 	prod = NEXT_TX(prod);
779 
780 	/* unmap remaining mapped pages */
781 	for (i = 0; i < last_frag; i++) {
782 		prod = NEXT_TX(prod);
783 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
784 		frag = &skb_shinfo(skb)->frags[i];
785 		netmem_dma_unmap_page_attrs(&pdev->dev,
786 					    dma_unmap_addr(tx_buf, mapping),
787 					    skb_frag_size(frag),
788 					    DMA_TO_DEVICE, 0);
789 	}
790 
791 tx_free:
792 	dev_kfree_skb_any(skb);
793 tx_kick_pending:
794 	if (BNXT_TX_PTP_IS_SET(lflags)) {
795 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
796 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
797 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
798 			/* set SKB to err so PTP worker will clean up */
799 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
800 	}
801 	if (txr->kick_pending)
802 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
803 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
804 	dev_core_stats_tx_dropped_inc(dev);
805 	return NETDEV_TX_OK;
806 }
807 
808 /* Returns true if some remaining TX packets not processed. */
809 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
810 			  int budget)
811 {
812 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
813 	struct pci_dev *pdev = bp->pdev;
814 	u16 hw_cons = txr->tx_hw_cons;
815 	unsigned int tx_bytes = 0;
816 	u16 cons = txr->tx_cons;
817 	skb_frag_t *frag;
818 	int tx_pkts = 0;
819 	bool rc = false;
820 
821 	while (RING_TX(bp, cons) != hw_cons) {
822 		struct bnxt_sw_tx_bd *tx_buf;
823 		struct sk_buff *skb;
824 		bool is_ts_pkt;
825 		int j, last;
826 
827 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
828 		skb = tx_buf->skb;
829 
830 		if (unlikely(!skb)) {
831 			bnxt_sched_reset_txr(bp, txr, cons);
832 			return rc;
833 		}
834 
835 		is_ts_pkt = tx_buf->is_ts_pkt;
836 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
837 			rc = true;
838 			break;
839 		}
840 
841 		cons = NEXT_TX(cons);
842 		tx_pkts++;
843 		tx_bytes += skb->len;
844 		tx_buf->skb = NULL;
845 		tx_buf->is_ts_pkt = 0;
846 
847 		if (tx_buf->is_push) {
848 			tx_buf->is_push = 0;
849 			goto next_tx_int;
850 		}
851 
852 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
853 				 skb_headlen(skb), DMA_TO_DEVICE);
854 		last = tx_buf->nr_frags;
855 
856 		for (j = 0; j < last; j++) {
857 			frag = &skb_shinfo(skb)->frags[j];
858 			cons = NEXT_TX(cons);
859 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
860 			netmem_dma_unmap_page_attrs(&pdev->dev,
861 						    dma_unmap_addr(tx_buf,
862 								   mapping),
863 						    skb_frag_size(frag),
864 						    DMA_TO_DEVICE, 0);
865 		}
866 		if (unlikely(is_ts_pkt)) {
867 			if (BNXT_CHIP_P5(bp)) {
868 				/* PTP worker takes ownership of the skb */
869 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
870 				skb = NULL;
871 			}
872 		}
873 
874 next_tx_int:
875 		cons = NEXT_TX(cons);
876 
877 		napi_consume_skb(skb, budget);
878 	}
879 
880 	WRITE_ONCE(txr->tx_cons, cons);
881 
882 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
883 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
884 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
885 
886 	return rc;
887 }
888 
889 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
890 {
891 	struct bnxt_tx_ring_info *txr;
892 	bool more = false;
893 	int i;
894 
895 	bnxt_for_each_napi_tx(i, bnapi, txr) {
896 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
897 			more |= __bnxt_tx_int(bp, txr, budget);
898 	}
899 	if (!more)
900 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
901 }
902 
903 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
904 {
905 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
906 }
907 
908 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
909 					 struct bnxt_rx_ring_info *rxr,
910 					 unsigned int *offset,
911 					 gfp_t gfp)
912 {
913 	struct page *page;
914 
915 	if (rxr->rx_page_size < PAGE_SIZE) {
916 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
917 						rxr->rx_page_size);
918 	} else {
919 		page = page_pool_dev_alloc_pages(rxr->page_pool);
920 		*offset = 0;
921 	}
922 	if (!page)
923 		return NULL;
924 
925 	*mapping = page_pool_get_dma_addr(page) + *offset;
926 	return page;
927 }
928 
929 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
930 					 struct bnxt_rx_ring_info *rxr,
931 					 unsigned int *offset,
932 					 gfp_t gfp)
933 {
934 	netmem_ref netmem;
935 
936 	if (rxr->rx_page_size < PAGE_SIZE) {
937 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
938 						     rxr->rx_page_size, gfp);
939 	} else {
940 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
941 		*offset = 0;
942 	}
943 	if (!netmem)
944 		return 0;
945 
946 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
947 	return netmem;
948 }
949 
950 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
951 				       struct bnxt_rx_ring_info *rxr,
952 				       gfp_t gfp)
953 {
954 	unsigned int offset;
955 	struct page *page;
956 
957 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
958 				    bp->rx_buf_size, gfp);
959 	if (!page)
960 		return NULL;
961 
962 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
963 	return page_address(page) + offset;
964 }
965 
966 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
967 		       u16 prod, gfp_t gfp)
968 {
969 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
970 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
971 	dma_addr_t mapping;
972 
973 	if (BNXT_RX_PAGE_MODE(bp)) {
974 		unsigned int offset;
975 		struct page *page =
976 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
977 
978 		if (!page)
979 			return -ENOMEM;
980 
981 		mapping += bp->rx_dma_offset;
982 		rx_buf->data = page;
983 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
984 	} else {
985 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
986 
987 		if (!data)
988 			return -ENOMEM;
989 
990 		rx_buf->data = data;
991 		rx_buf->data_ptr = data + bp->rx_offset;
992 	}
993 	rx_buf->mapping = mapping;
994 
995 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
996 	return 0;
997 }
998 
999 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1000 {
1001 	u16 prod = rxr->rx_prod;
1002 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1003 	struct bnxt *bp = rxr->bnapi->bp;
1004 	struct rx_bd *cons_bd, *prod_bd;
1005 
1006 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1007 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1008 
1009 	prod_rx_buf->data = data;
1010 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1011 
1012 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1013 
1014 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1015 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1016 
1017 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1018 }
1019 
1020 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1021 {
1022 	u16 next, max = rxr->rx_agg_bmap_size;
1023 
1024 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1025 	if (next >= max)
1026 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1027 	return next;
1028 }
1029 
1030 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1031 				u16 prod, gfp_t gfp)
1032 {
1033 	struct rx_bd *rxbd =
1034 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1035 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1036 	u16 sw_prod = rxr->rx_sw_agg_prod;
1037 	unsigned int offset = 0;
1038 	dma_addr_t mapping;
1039 	netmem_ref netmem;
1040 
1041 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1042 	if (!netmem)
1043 		return -ENOMEM;
1044 
1045 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1046 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1047 
1048 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1049 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1050 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1051 
1052 	rx_agg_buf->netmem = netmem;
1053 	rx_agg_buf->offset = offset;
1054 	rx_agg_buf->mapping = mapping;
1055 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1056 	rxbd->rx_bd_opaque = sw_prod;
1057 	return 0;
1058 }
1059 
1060 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1061 				       struct bnxt_cp_ring_info *cpr,
1062 				       u16 cp_cons, u16 curr)
1063 {
1064 	struct rx_agg_cmp *agg;
1065 
1066 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1067 	agg = (struct rx_agg_cmp *)
1068 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1069 	return agg;
1070 }
1071 
1072 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1073 					      struct bnxt_rx_ring_info *rxr,
1074 					      u16 agg_id, u16 curr)
1075 {
1076 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1077 
1078 	return &tpa_info->agg_arr[curr];
1079 }
1080 
1081 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1082 				   u16 start, u32 agg_bufs, bool tpa)
1083 {
1084 	struct bnxt_napi *bnapi = cpr->bnapi;
1085 	struct bnxt *bp = bnapi->bp;
1086 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1087 	u16 prod = rxr->rx_agg_prod;
1088 	u16 sw_prod = rxr->rx_sw_agg_prod;
1089 	bool p5_tpa = false;
1090 	u32 i;
1091 
1092 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1093 		p5_tpa = true;
1094 
1095 	for (i = 0; i < agg_bufs; i++) {
1096 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1097 		struct rx_agg_cmp *agg;
1098 		struct rx_bd *prod_bd;
1099 		netmem_ref netmem;
1100 		u16 cons;
1101 
1102 		if (p5_tpa)
1103 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1104 		else
1105 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1106 		cons = agg->rx_agg_cmp_opaque;
1107 		__clear_bit(cons, rxr->rx_agg_bmap);
1108 
1109 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1110 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1111 
1112 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1113 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1114 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1115 
1116 		/* It is possible for sw_prod to be equal to cons, so
1117 		 * set cons_rx_buf->netmem to 0 first.
1118 		 */
1119 		netmem = cons_rx_buf->netmem;
1120 		cons_rx_buf->netmem = 0;
1121 		prod_rx_buf->netmem = netmem;
1122 		prod_rx_buf->offset = cons_rx_buf->offset;
1123 
1124 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1125 
1126 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1127 
1128 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1129 		prod_bd->rx_bd_opaque = sw_prod;
1130 
1131 		prod = NEXT_RX_AGG(prod);
1132 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1133 	}
1134 	rxr->rx_agg_prod = prod;
1135 	rxr->rx_sw_agg_prod = sw_prod;
1136 }
1137 
1138 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1139 					      struct bnxt_rx_ring_info *rxr,
1140 					      u16 cons, void *data, u8 *data_ptr,
1141 					      dma_addr_t dma_addr,
1142 					      unsigned int offset_and_len)
1143 {
1144 	unsigned int len = offset_and_len & 0xffff;
1145 	struct page *page = data;
1146 	u16 prod = rxr->rx_prod;
1147 	struct sk_buff *skb;
1148 	int err;
1149 
1150 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1151 	if (unlikely(err)) {
1152 		bnxt_reuse_rx_data(rxr, cons, data);
1153 		return NULL;
1154 	}
1155 	dma_addr -= bp->rx_dma_offset;
1156 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1157 				bp->rx_dir);
1158 	skb = napi_build_skb(data_ptr - bp->rx_offset, rxr->rx_page_size);
1159 	if (!skb) {
1160 		page_pool_recycle_direct(rxr->page_pool, page);
1161 		return NULL;
1162 	}
1163 	skb_mark_for_recycle(skb);
1164 	skb_reserve(skb, bp->rx_offset);
1165 	__skb_put(skb, len);
1166 
1167 	return skb;
1168 }
1169 
1170 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1171 					struct bnxt_rx_ring_info *rxr,
1172 					u16 cons, void *data, u8 *data_ptr,
1173 					dma_addr_t dma_addr,
1174 					unsigned int offset_and_len)
1175 {
1176 	unsigned int payload = offset_and_len >> 16;
1177 	unsigned int len = offset_and_len & 0xffff;
1178 	skb_frag_t *frag;
1179 	struct page *page = data;
1180 	u16 prod = rxr->rx_prod;
1181 	struct sk_buff *skb;
1182 	int off, err;
1183 
1184 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1185 	if (unlikely(err)) {
1186 		bnxt_reuse_rx_data(rxr, cons, data);
1187 		return NULL;
1188 	}
1189 	dma_addr -= bp->rx_dma_offset;
1190 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1191 				bp->rx_dir);
1192 
1193 	if (unlikely(!payload))
1194 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1195 
1196 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1197 	if (!skb) {
1198 		page_pool_recycle_direct(rxr->page_pool, page);
1199 		return NULL;
1200 	}
1201 
1202 	skb_mark_for_recycle(skb);
1203 	off = (void *)data_ptr - page_address(page);
1204 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1205 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1206 	       payload + NET_IP_ALIGN);
1207 
1208 	frag = &skb_shinfo(skb)->frags[0];
1209 	skb_frag_size_sub(frag, payload);
1210 	skb_frag_off_add(frag, payload);
1211 	skb->data_len -= payload;
1212 	skb->tail += payload;
1213 
1214 	return skb;
1215 }
1216 
1217 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1218 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1219 				   void *data, u8 *data_ptr,
1220 				   dma_addr_t dma_addr,
1221 				   unsigned int offset_and_len)
1222 {
1223 	u16 prod = rxr->rx_prod;
1224 	struct sk_buff *skb;
1225 	int err;
1226 
1227 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1228 	if (unlikely(err)) {
1229 		bnxt_reuse_rx_data(rxr, cons, data);
1230 		return NULL;
1231 	}
1232 
1233 	skb = napi_build_skb(data, bp->rx_buf_size);
1234 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1235 				bp->rx_dir);
1236 	if (!skb) {
1237 		page_pool_free_va(rxr->head_pool, data, true);
1238 		return NULL;
1239 	}
1240 
1241 	skb_mark_for_recycle(skb);
1242 	skb_reserve(skb, bp->rx_offset);
1243 	skb_put(skb, offset_and_len & 0xffff);
1244 	return skb;
1245 }
1246 
1247 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1248 				 struct bnxt_cp_ring_info *cpr,
1249 				 u16 idx, u32 agg_bufs, bool tpa,
1250 				 struct sk_buff *skb,
1251 				 struct xdp_buff *xdp)
1252 {
1253 	struct bnxt_napi *bnapi = cpr->bnapi;
1254 	struct skb_shared_info *shinfo;
1255 	struct bnxt_rx_ring_info *rxr;
1256 	u32 i, total_frag_len = 0;
1257 	bool p5_tpa = false;
1258 	u16 prod;
1259 
1260 	rxr = bnapi->rx_ring;
1261 	prod = rxr->rx_agg_prod;
1262 
1263 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1264 		p5_tpa = true;
1265 
1266 	if (skb)
1267 		shinfo = skb_shinfo(skb);
1268 	else
1269 		shinfo = xdp_get_shared_info_from_buff(xdp);
1270 
1271 	for (i = 0; i < agg_bufs; i++) {
1272 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1273 		struct rx_agg_cmp *agg;
1274 		u16 cons, frag_len;
1275 		netmem_ref netmem;
1276 
1277 		if (p5_tpa)
1278 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1279 		else
1280 			agg = bnxt_get_agg(bp, cpr, idx, i);
1281 		cons = agg->rx_agg_cmp_opaque;
1282 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1283 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1284 
1285 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1286 		if (skb) {
1287 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1288 					       cons_rx_buf->offset,
1289 					       frag_len, rxr->rx_page_size);
1290 		} else {
1291 			skb_frag_t *frag = &shinfo->frags[i];
1292 
1293 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1294 						  cons_rx_buf->offset,
1295 						  frag_len);
1296 			shinfo->nr_frags = i + 1;
1297 		}
1298 		__clear_bit(cons, rxr->rx_agg_bmap);
1299 
1300 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1301 		 * a sw_prod index that equals the cons index, so we
1302 		 * need to clear the cons entry now.
1303 		 */
1304 		netmem = cons_rx_buf->netmem;
1305 		cons_rx_buf->netmem = 0;
1306 
1307 		if (xdp && netmem_is_pfmemalloc(netmem))
1308 			xdp_buff_set_frag_pfmemalloc(xdp);
1309 
1310 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1311 			if (skb) {
1312 				skb->len -= frag_len;
1313 				skb->data_len -= frag_len;
1314 				skb->truesize -= rxr->rx_page_size;
1315 			}
1316 
1317 			--shinfo->nr_frags;
1318 			cons_rx_buf->netmem = netmem;
1319 
1320 			/* Update prod since possibly some netmems have been
1321 			 * allocated already.
1322 			 */
1323 			rxr->rx_agg_prod = prod;
1324 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1325 			return 0;
1326 		}
1327 
1328 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1329 						  rxr->rx_page_size);
1330 
1331 		total_frag_len += frag_len;
1332 		prod = NEXT_RX_AGG(prod);
1333 	}
1334 	rxr->rx_agg_prod = prod;
1335 	return total_frag_len;
1336 }
1337 
1338 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1339 					       struct bnxt_cp_ring_info *cpr,
1340 					       struct sk_buff *skb, u16 idx,
1341 					       u32 agg_bufs, bool tpa)
1342 {
1343 	u32 total_frag_len = 0;
1344 
1345 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1346 					       skb, NULL);
1347 	if (!total_frag_len) {
1348 		skb_mark_for_recycle(skb);
1349 		dev_kfree_skb(skb);
1350 		return NULL;
1351 	}
1352 
1353 	return skb;
1354 }
1355 
1356 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1357 				   struct bnxt_cp_ring_info *cpr,
1358 				   struct xdp_buff *xdp, u16 idx,
1359 				   u32 agg_bufs, bool tpa)
1360 {
1361 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1362 	u32 total_frag_len = 0;
1363 
1364 	if (!xdp_buff_has_frags(xdp))
1365 		shinfo->nr_frags = 0;
1366 
1367 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1368 					       NULL, xdp);
1369 	if (total_frag_len) {
1370 		xdp_buff_set_frags_flag(xdp);
1371 		shinfo->nr_frags = agg_bufs;
1372 		shinfo->xdp_frags_size = total_frag_len;
1373 	}
1374 	return total_frag_len;
1375 }
1376 
1377 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1378 			       u8 agg_bufs, u32 *raw_cons)
1379 {
1380 	u16 last;
1381 	struct rx_agg_cmp *agg;
1382 
1383 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1384 	last = RING_CMP(*raw_cons);
1385 	agg = (struct rx_agg_cmp *)
1386 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1387 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1388 }
1389 
1390 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1391 				      unsigned int len,
1392 				      dma_addr_t mapping)
1393 {
1394 	struct bnxt *bp = bnapi->bp;
1395 	struct pci_dev *pdev = bp->pdev;
1396 	struct sk_buff *skb;
1397 
1398 	skb = napi_alloc_skb(&bnapi->napi, len);
1399 	if (!skb)
1400 		return NULL;
1401 
1402 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1403 				bp->rx_dir);
1404 
1405 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1406 	       len + NET_IP_ALIGN);
1407 
1408 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1409 				   bp->rx_dir);
1410 
1411 	skb_put(skb, len);
1412 
1413 	return skb;
1414 }
1415 
1416 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1417 				     unsigned int len,
1418 				     dma_addr_t mapping)
1419 {
1420 	return bnxt_copy_data(bnapi, data, len, mapping);
1421 }
1422 
1423 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1424 				     struct xdp_buff *xdp,
1425 				     unsigned int len,
1426 				     dma_addr_t mapping)
1427 {
1428 	unsigned int metasize = 0;
1429 	u8 *data = xdp->data;
1430 	struct sk_buff *skb;
1431 
1432 	len = xdp->data_end - xdp->data_meta;
1433 	metasize = xdp->data - xdp->data_meta;
1434 	data = xdp->data_meta;
1435 
1436 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1437 	if (!skb)
1438 		return skb;
1439 
1440 	if (metasize) {
1441 		skb_metadata_set(skb, metasize);
1442 		__skb_pull(skb, metasize);
1443 	}
1444 
1445 	return skb;
1446 }
1447 
1448 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1449 			   u32 *raw_cons, void *cmp)
1450 {
1451 	struct rx_cmp *rxcmp = cmp;
1452 	u32 tmp_raw_cons = *raw_cons;
1453 	u8 cmp_type, agg_bufs = 0;
1454 
1455 	cmp_type = RX_CMP_TYPE(rxcmp);
1456 
1457 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1458 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1459 			    RX_CMP_AGG_BUFS) >>
1460 			   RX_CMP_AGG_BUFS_SHIFT;
1461 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1462 		struct rx_tpa_end_cmp *tpa_end = cmp;
1463 
1464 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1465 			return 0;
1466 
1467 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1468 	}
1469 
1470 	if (agg_bufs) {
1471 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1472 			return -EBUSY;
1473 	}
1474 	*raw_cons = tmp_raw_cons;
1475 	return 0;
1476 }
1477 
1478 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1479 {
1480 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1481 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1482 
1483 	if (test_bit(idx, map->agg_idx_bmap)) {
1484 		idx = find_first_zero_bit(map->agg_idx_bmap, MAX_TPA_P5);
1485 		if (idx >= MAX_TPA_P5)
1486 			return INVALID_HW_RING_ID;
1487 	}
1488 	__set_bit(idx, map->agg_idx_bmap);
1489 	map->agg_id_tbl[agg_id] = idx;
1490 	return idx;
1491 }
1492 
1493 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1494 {
1495 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1496 
1497 	__clear_bit(idx, map->agg_idx_bmap);
1498 }
1499 
1500 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1501 {
1502 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1503 
1504 	return map->agg_id_tbl[agg_id];
1505 }
1506 
1507 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1508 			      struct rx_tpa_start_cmp *tpa_start,
1509 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1510 {
1511 	tpa_info->cfa_code_valid = 1;
1512 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1513 	tpa_info->vlan_valid = 0;
1514 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1515 		tpa_info->vlan_valid = 1;
1516 		tpa_info->metadata =
1517 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1518 	}
1519 }
1520 
1521 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1522 				 struct rx_tpa_start_cmp *tpa_start,
1523 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1524 {
1525 	tpa_info->vlan_valid = 0;
1526 	if (TPA_START_VLAN_VALID(tpa_start)) {
1527 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1528 		u32 vlan_proto = ETH_P_8021Q;
1529 
1530 		tpa_info->vlan_valid = 1;
1531 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1532 			vlan_proto = ETH_P_8021AD;
1533 		tpa_info->metadata = vlan_proto << 16 |
1534 				     TPA_START_METADATA0_TCI(tpa_start1);
1535 	}
1536 }
1537 
1538 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1539 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1540 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1541 {
1542 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1543 	struct bnxt_tpa_info *tpa_info;
1544 	u16 cons, prod, agg_id;
1545 	struct rx_bd *prod_bd;
1546 	dma_addr_t mapping;
1547 
1548 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1549 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1550 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1551 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1552 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1553 				    rxr->bnapi->index,
1554 				    TPA_START_AGG_ID_P5(tpa_start));
1555 			bnxt_sched_reset_rxr(bp, rxr);
1556 			return;
1557 		}
1558 	} else {
1559 		agg_id = TPA_START_AGG_ID(tpa_start);
1560 	}
1561 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1562 	prod = rxr->rx_prod;
1563 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1564 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1565 	tpa_info = &rxr->rx_tpa[agg_id];
1566 
1567 	if (unlikely(cons != rxr->rx_next_cons ||
1568 		     TPA_START_ERROR(tpa_start))) {
1569 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1570 			    cons, rxr->rx_next_cons,
1571 			    TPA_START_ERROR_CODE(tpa_start1));
1572 		bnxt_sched_reset_rxr(bp, rxr);
1573 		return;
1574 	}
1575 	prod_rx_buf->data = tpa_info->data;
1576 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1577 
1578 	mapping = tpa_info->mapping;
1579 	prod_rx_buf->mapping = mapping;
1580 
1581 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1582 
1583 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1584 
1585 	tpa_info->data = cons_rx_buf->data;
1586 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1587 	cons_rx_buf->data = NULL;
1588 	tpa_info->mapping = cons_rx_buf->mapping;
1589 
1590 	tpa_info->len =
1591 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1592 				RX_TPA_START_CMP_LEN_SHIFT;
1593 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1594 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1595 		tpa_info->gso_type = SKB_GSO_TCPV4;
1596 		if (TPA_START_IS_IPV6(tpa_start1))
1597 			tpa_info->gso_type = SKB_GSO_TCPV6;
1598 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1599 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1600 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1601 			tpa_info->gso_type = SKB_GSO_TCPV6;
1602 		tpa_info->rss_hash =
1603 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1604 	} else {
1605 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1606 		tpa_info->gso_type = 0;
1607 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1608 	}
1609 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1610 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1611 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1612 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1613 	else
1614 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1615 	tpa_info->agg_count = 0;
1616 
1617 	rxr->rx_prod = NEXT_RX(prod);
1618 	cons = RING_RX(bp, NEXT_RX(cons));
1619 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1620 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1621 
1622 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1623 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1624 	cons_rx_buf->data = NULL;
1625 }
1626 
1627 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1628 {
1629 	if (agg_bufs)
1630 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1631 }
1632 
1633 #ifdef CONFIG_INET
1634 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1635 {
1636 	struct udphdr *uh = NULL;
1637 
1638 	if (ip_proto == htons(ETH_P_IP)) {
1639 		struct iphdr *iph = (struct iphdr *)skb->data;
1640 
1641 		if (iph->protocol == IPPROTO_UDP)
1642 			uh = (struct udphdr *)(iph + 1);
1643 	} else {
1644 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1645 
1646 		if (iph->nexthdr == IPPROTO_UDP)
1647 			uh = (struct udphdr *)(iph + 1);
1648 	}
1649 	if (uh) {
1650 		if (uh->check)
1651 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1652 		else
1653 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1654 	}
1655 }
1656 #endif
1657 
1658 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1659 					   int payload_off, int tcp_ts,
1660 					   struct sk_buff *skb)
1661 {
1662 #ifdef CONFIG_INET
1663 	struct tcphdr *th;
1664 	int len, nw_off;
1665 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1666 	u32 hdr_info = tpa_info->hdr_info;
1667 	bool loopback = false;
1668 
1669 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1670 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1671 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1672 
1673 	/* If the packet is an internal loopback packet, the offsets will
1674 	 * have an extra 4 bytes.
1675 	 */
1676 	if (inner_mac_off == 4) {
1677 		loopback = true;
1678 	} else if (inner_mac_off > 4) {
1679 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1680 					    ETH_HLEN - 2));
1681 
1682 		/* We only support inner iPv4/ipv6.  If we don't see the
1683 		 * correct protocol ID, it must be a loopback packet where
1684 		 * the offsets are off by 4.
1685 		 */
1686 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1687 			loopback = true;
1688 	}
1689 	if (loopback) {
1690 		/* internal loopback packet, subtract all offsets by 4 */
1691 		inner_ip_off -= 4;
1692 		inner_mac_off -= 4;
1693 		outer_ip_off -= 4;
1694 	}
1695 
1696 	nw_off = inner_ip_off - ETH_HLEN;
1697 	skb_set_network_header(skb, nw_off);
1698 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1699 		struct ipv6hdr *iph = ipv6_hdr(skb);
1700 
1701 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1702 		len = skb->len - skb_transport_offset(skb);
1703 		th = tcp_hdr(skb);
1704 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1705 	} else {
1706 		struct iphdr *iph = ip_hdr(skb);
1707 
1708 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1709 		len = skb->len - skb_transport_offset(skb);
1710 		th = tcp_hdr(skb);
1711 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1712 	}
1713 
1714 	if (inner_mac_off) { /* tunnel */
1715 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1716 					    ETH_HLEN - 2));
1717 
1718 		bnxt_gro_tunnel(skb, proto);
1719 	}
1720 #endif
1721 	return skb;
1722 }
1723 
1724 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1725 					   int payload_off, int tcp_ts,
1726 					   struct sk_buff *skb)
1727 {
1728 #ifdef CONFIG_INET
1729 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1730 	u32 hdr_info = tpa_info->hdr_info;
1731 	int iphdr_len, nw_off;
1732 
1733 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1734 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1735 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1736 
1737 	nw_off = inner_ip_off - ETH_HLEN;
1738 	skb_set_network_header(skb, nw_off);
1739 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1740 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1741 	skb_set_transport_header(skb, nw_off + iphdr_len);
1742 
1743 	if (inner_mac_off) { /* tunnel */
1744 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1745 					    ETH_HLEN - 2));
1746 
1747 		bnxt_gro_tunnel(skb, proto);
1748 	}
1749 #endif
1750 	return skb;
1751 }
1752 
1753 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1754 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1755 
1756 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1757 					   int payload_off, int tcp_ts,
1758 					   struct sk_buff *skb)
1759 {
1760 #ifdef CONFIG_INET
1761 	struct tcphdr *th;
1762 	int len, nw_off, tcp_opt_len = 0;
1763 
1764 	if (tcp_ts)
1765 		tcp_opt_len = 12;
1766 
1767 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1768 		struct iphdr *iph;
1769 
1770 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1771 			 ETH_HLEN;
1772 		skb_set_network_header(skb, nw_off);
1773 		iph = ip_hdr(skb);
1774 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1775 		len = skb->len - skb_transport_offset(skb);
1776 		th = tcp_hdr(skb);
1777 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1778 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1779 		struct ipv6hdr *iph;
1780 
1781 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1782 			 ETH_HLEN;
1783 		skb_set_network_header(skb, nw_off);
1784 		iph = ipv6_hdr(skb);
1785 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1786 		len = skb->len - skb_transport_offset(skb);
1787 		th = tcp_hdr(skb);
1788 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1789 	} else {
1790 		dev_kfree_skb_any(skb);
1791 		return NULL;
1792 	}
1793 
1794 	if (nw_off) /* tunnel */
1795 		bnxt_gro_tunnel(skb, skb->protocol);
1796 #endif
1797 	return skb;
1798 }
1799 
1800 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1801 					   struct bnxt_tpa_info *tpa_info,
1802 					   struct rx_tpa_end_cmp *tpa_end,
1803 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1804 					   struct sk_buff *skb,
1805 					   struct bnxt_rx_sw_stats *rx_stats)
1806 {
1807 #ifdef CONFIG_INET
1808 	int payload_off;
1809 	u16 segs;
1810 
1811 	segs = TPA_END_TPA_SEGS(tpa_end);
1812 	if (segs == 1)
1813 		return skb;
1814 
1815 	rx_stats->rx_hw_gro_packets++;
1816 	rx_stats->rx_hw_gro_wire_packets += segs;
1817 
1818 	NAPI_GRO_CB(skb)->count = segs;
1819 	skb_shinfo(skb)->gso_size =
1820 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1821 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1822 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1823 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1824 	else
1825 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1826 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1827 	if (likely(skb))
1828 		tcp_gro_complete(skb);
1829 #endif
1830 	return skb;
1831 }
1832 
1833 /* Given the cfa_code of a received packet determine which
1834  * netdev (vf-rep or PF) the packet is destined to.
1835  */
1836 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1837 {
1838 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1839 
1840 	/* if vf-rep dev is NULL, it must belong to the PF */
1841 	return dev ? dev : bp->dev;
1842 }
1843 
1844 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1845 					   struct bnxt_cp_ring_info *cpr,
1846 					   u32 *raw_cons,
1847 					   struct rx_tpa_end_cmp *tpa_end,
1848 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1849 					   u8 *event)
1850 {
1851 	struct bnxt_napi *bnapi = cpr->bnapi;
1852 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1853 	struct net_device *dev = bp->dev;
1854 	u8 *data_ptr, agg_bufs;
1855 	unsigned int len;
1856 	struct bnxt_tpa_info *tpa_info;
1857 	dma_addr_t mapping;
1858 	struct sk_buff *skb;
1859 	u16 idx = 0, agg_id;
1860 	void *data;
1861 	bool gro;
1862 
1863 	if (unlikely(bnapi->in_reset)) {
1864 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1865 
1866 		if (rc < 0)
1867 			return ERR_PTR(-EBUSY);
1868 		return NULL;
1869 	}
1870 
1871 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1872 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1873 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1874 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1875 		tpa_info = &rxr->rx_tpa[agg_id];
1876 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1877 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1878 				    agg_bufs, tpa_info->agg_count);
1879 			agg_bufs = tpa_info->agg_count;
1880 		}
1881 		tpa_info->agg_count = 0;
1882 		*event |= BNXT_AGG_EVENT;
1883 		bnxt_free_agg_idx(rxr, agg_id);
1884 		idx = agg_id;
1885 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1886 	} else {
1887 		agg_id = TPA_END_AGG_ID(tpa_end);
1888 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1889 		tpa_info = &rxr->rx_tpa[agg_id];
1890 		idx = RING_CMP(*raw_cons);
1891 		if (agg_bufs) {
1892 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1893 				return ERR_PTR(-EBUSY);
1894 
1895 			*event |= BNXT_AGG_EVENT;
1896 			idx = NEXT_CMP(idx);
1897 		}
1898 		gro = !!TPA_END_GRO(tpa_end);
1899 	}
1900 	data = tpa_info->data;
1901 	data_ptr = tpa_info->data_ptr;
1902 	prefetch(data_ptr);
1903 	len = tpa_info->len;
1904 	mapping = tpa_info->mapping;
1905 
1906 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1907 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1908 		if (agg_bufs > MAX_SKB_FRAGS)
1909 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1910 				    agg_bufs, (int)MAX_SKB_FRAGS);
1911 		return NULL;
1912 	}
1913 
1914 	if (len <= bp->rx_copybreak) {
1915 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1916 		if (!skb) {
1917 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1918 			cpr->sw_stats->rx.rx_oom_discards += 1;
1919 			return NULL;
1920 		}
1921 	} else {
1922 		u8 *new_data;
1923 		dma_addr_t new_mapping;
1924 
1925 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1926 						GFP_ATOMIC);
1927 		if (!new_data) {
1928 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1929 			cpr->sw_stats->rx.rx_oom_discards += 1;
1930 			return NULL;
1931 		}
1932 
1933 		tpa_info->data = new_data;
1934 		tpa_info->data_ptr = new_data + bp->rx_offset;
1935 		tpa_info->mapping = new_mapping;
1936 
1937 		skb = napi_build_skb(data, bp->rx_buf_size);
1938 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1939 					bp->rx_buf_use_size, bp->rx_dir);
1940 
1941 		if (!skb) {
1942 			page_pool_free_va(rxr->head_pool, data, true);
1943 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1944 			cpr->sw_stats->rx.rx_oom_discards += 1;
1945 			return NULL;
1946 		}
1947 		skb_mark_for_recycle(skb);
1948 		skb_reserve(skb, bp->rx_offset);
1949 		skb_put(skb, len);
1950 	}
1951 
1952 	if (agg_bufs) {
1953 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1954 					      true);
1955 		if (!skb) {
1956 			/* Page reuse already handled by bnxt_rx_pages(). */
1957 			cpr->sw_stats->rx.rx_oom_discards += 1;
1958 			return NULL;
1959 		}
1960 	}
1961 
1962 	if (tpa_info->cfa_code_valid)
1963 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1964 	skb->protocol = eth_type_trans(skb, dev);
1965 
1966 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1967 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1968 
1969 	if (tpa_info->vlan_valid &&
1970 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1971 		__be16 vlan_proto = htons(tpa_info->metadata >>
1972 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1973 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1974 
1975 		if (eth_type_vlan(vlan_proto)) {
1976 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1977 		} else {
1978 			dev_kfree_skb(skb);
1979 			return NULL;
1980 		}
1981 	}
1982 
1983 	skb_checksum_none_assert(skb);
1984 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1985 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1986 		skb->csum_level =
1987 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1988 	}
1989 
1990 	if (gro)
1991 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
1992 				   &cpr->sw_stats->rx);
1993 
1994 	return skb;
1995 }
1996 
1997 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1998 			 struct rx_agg_cmp *rx_agg)
1999 {
2000 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2001 	struct bnxt_tpa_info *tpa_info;
2002 
2003 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2004 	tpa_info = &rxr->rx_tpa[agg_id];
2005 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2006 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2007 }
2008 
2009 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2010 			     struct sk_buff *skb)
2011 {
2012 	skb_mark_for_recycle(skb);
2013 
2014 	if (skb->dev != bp->dev) {
2015 		/* this packet belongs to a vf-rep */
2016 		bnxt_vf_rep_rx(bp, skb);
2017 		return;
2018 	}
2019 	skb_record_rx_queue(skb, bnapi->index);
2020 	napi_gro_receive(&bnapi->napi, skb);
2021 }
2022 
2023 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2024 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2025 {
2026 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2027 
2028 	if (BNXT_PTP_RX_TS_VALID(flags))
2029 		goto ts_valid;
2030 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2031 		return false;
2032 
2033 ts_valid:
2034 	*cmpl_ts = ts;
2035 	return true;
2036 }
2037 
2038 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2039 				    struct rx_cmp *rxcmp,
2040 				    struct rx_cmp_ext *rxcmp1)
2041 {
2042 	__be16 vlan_proto;
2043 	u16 vtag;
2044 
2045 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2046 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2047 		u32 meta_data;
2048 
2049 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2050 			return skb;
2051 
2052 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2053 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2054 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2055 		if (eth_type_vlan(vlan_proto))
2056 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2057 		else
2058 			goto vlan_err;
2059 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2060 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2061 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2062 
2063 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2064 				vlan_proto = htons(ETH_P_8021Q);
2065 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2066 				vlan_proto = htons(ETH_P_8021AD);
2067 			else
2068 				goto vlan_err;
2069 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2070 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2071 		}
2072 	}
2073 	return skb;
2074 vlan_err:
2075 	skb_mark_for_recycle(skb);
2076 	dev_kfree_skb(skb);
2077 	return NULL;
2078 }
2079 
2080 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2081 					   struct rx_cmp *rxcmp)
2082 {
2083 	u8 ext_op;
2084 
2085 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2086 	switch (ext_op) {
2087 	case EXT_OP_INNER_4:
2088 	case EXT_OP_OUTER_4:
2089 	case EXT_OP_INNFL_3:
2090 	case EXT_OP_OUTFL_3:
2091 		return PKT_HASH_TYPE_L4;
2092 	default:
2093 		return PKT_HASH_TYPE_L3;
2094 	}
2095 }
2096 
2097 /* returns the following:
2098  * 1       - 1 packet successfully received
2099  * 0       - successful TPA_START, packet not completed yet
2100  * -EBUSY  - completion ring does not have all the agg buffers yet
2101  * -ENOMEM - packet aborted due to out of memory
2102  * -EIO    - packet aborted due to hw error indicated in BD
2103  */
2104 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2105 		       u32 *raw_cons, u8 *event)
2106 {
2107 	struct bnxt_napi *bnapi = cpr->bnapi;
2108 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2109 	struct net_device *dev = bp->dev;
2110 	struct rx_cmp *rxcmp;
2111 	struct rx_cmp_ext *rxcmp1;
2112 	u32 tmp_raw_cons = *raw_cons;
2113 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2114 	struct skb_shared_info *sinfo;
2115 	struct bnxt_sw_rx_bd *rx_buf;
2116 	unsigned int len;
2117 	u8 *data_ptr, agg_bufs, cmp_type;
2118 	bool xdp_active = false;
2119 	dma_addr_t dma_addr;
2120 	struct sk_buff *skb;
2121 	struct xdp_buff xdp;
2122 	u32 flags, misc;
2123 	u32 cmpl_ts;
2124 	void *data;
2125 	int rc = 0;
2126 
2127 	rxcmp = (struct rx_cmp *)
2128 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2129 
2130 	cmp_type = RX_CMP_TYPE(rxcmp);
2131 
2132 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2133 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2134 		goto next_rx_no_prod_no_len;
2135 	}
2136 
2137 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2138 	cp_cons = RING_CMP(tmp_raw_cons);
2139 	rxcmp1 = (struct rx_cmp_ext *)
2140 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2141 
2142 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2143 		return -EBUSY;
2144 
2145 	/* The valid test of the entry must be done first before
2146 	 * reading any further.
2147 	 */
2148 	dma_rmb();
2149 	prod = rxr->rx_prod;
2150 
2151 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2152 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2153 		bnxt_tpa_start(bp, rxr, cmp_type,
2154 			       (struct rx_tpa_start_cmp *)rxcmp,
2155 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2156 
2157 		*event |= BNXT_RX_EVENT;
2158 		goto next_rx_no_prod_no_len;
2159 
2160 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2161 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2162 				   (struct rx_tpa_end_cmp *)rxcmp,
2163 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2164 
2165 		if (IS_ERR(skb))
2166 			return -EBUSY;
2167 
2168 		rc = -ENOMEM;
2169 		if (likely(skb)) {
2170 			bnxt_deliver_skb(bp, bnapi, skb);
2171 			rc = 1;
2172 		}
2173 		*event |= BNXT_RX_EVENT;
2174 		goto next_rx_no_prod_no_len;
2175 	}
2176 
2177 	cons = rxcmp->rx_cmp_opaque;
2178 	if (unlikely(cons != rxr->rx_next_cons)) {
2179 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2180 
2181 		/* 0xffff is forced error, don't print it */
2182 		if (rxr->rx_next_cons != 0xffff)
2183 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2184 				    cons, rxr->rx_next_cons);
2185 		bnxt_sched_reset_rxr(bp, rxr);
2186 		if (rc1)
2187 			return rc1;
2188 		goto next_rx_no_prod_no_len;
2189 	}
2190 	rx_buf = &rxr->rx_buf_ring[cons];
2191 	data = rx_buf->data;
2192 	data_ptr = rx_buf->data_ptr;
2193 	prefetch(data_ptr);
2194 
2195 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2196 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2197 
2198 	if (agg_bufs) {
2199 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2200 			return -EBUSY;
2201 
2202 		cp_cons = NEXT_CMP(cp_cons);
2203 		*event |= BNXT_AGG_EVENT;
2204 	}
2205 	*event |= BNXT_RX_EVENT;
2206 
2207 	rx_buf->data = NULL;
2208 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2209 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2210 
2211 		bnxt_reuse_rx_data(rxr, cons, data);
2212 		if (agg_bufs)
2213 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2214 					       false);
2215 
2216 		rc = -EIO;
2217 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2218 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2219 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2220 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2221 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2222 						 rx_err);
2223 				bnxt_sched_reset_rxr(bp, rxr);
2224 			}
2225 		}
2226 		goto next_rx_no_len;
2227 	}
2228 
2229 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2230 	len = flags >> RX_CMP_LEN_SHIFT;
2231 	dma_addr = rx_buf->mapping;
2232 
2233 	if (bnxt_xdp_attached(bp, rxr)) {
2234 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2235 		if (agg_bufs) {
2236 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp,
2237 							       cp_cons,
2238 							       agg_bufs,
2239 							       false);
2240 			if (!frag_len)
2241 				goto oom_next_rx;
2242 
2243 		}
2244 		xdp_active = true;
2245 	}
2246 
2247 	if (xdp_active) {
2248 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2249 			rc = 1;
2250 			goto next_rx;
2251 		}
2252 		if (xdp_buff_has_frags(&xdp)) {
2253 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2254 			agg_bufs = sinfo->nr_frags;
2255 		} else {
2256 			agg_bufs = 0;
2257 		}
2258 	}
2259 
2260 	if (len <= bp->rx_copybreak) {
2261 		if (!xdp_active)
2262 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2263 		else
2264 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2265 		bnxt_reuse_rx_data(rxr, cons, data);
2266 		if (!skb) {
2267 			if (agg_bufs) {
2268 				if (!xdp_active)
2269 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2270 							       agg_bufs, false);
2271 				else
2272 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2273 			}
2274 			goto oom_next_rx;
2275 		}
2276 	} else {
2277 		u32 payload;
2278 
2279 		if (rx_buf->data_ptr == data_ptr)
2280 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2281 		else
2282 			payload = 0;
2283 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2284 				      payload | len);
2285 		if (!skb)
2286 			goto oom_next_rx;
2287 	}
2288 
2289 	if (agg_bufs) {
2290 		if (!xdp_active) {
2291 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2292 						      agg_bufs, false);
2293 			if (!skb)
2294 				goto oom_next_rx;
2295 		} else {
2296 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr, &xdp);
2297 			if (!skb) {
2298 				/* we should be able to free the old skb here */
2299 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2300 				goto oom_next_rx;
2301 			}
2302 		}
2303 	}
2304 
2305 	if (RX_CMP_HASH_VALID(rxcmp)) {
2306 		enum pkt_hash_types type;
2307 
2308 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2309 			type = bnxt_rss_ext_op(bp, rxcmp);
2310 		} else {
2311 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2312 
2313 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2314 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2315 				type = PKT_HASH_TYPE_L4;
2316 			else
2317 				type = PKT_HASH_TYPE_L3;
2318 		}
2319 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2320 	}
2321 
2322 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2323 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2324 	skb->protocol = eth_type_trans(skb, dev);
2325 
2326 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2327 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2328 		if (!skb)
2329 			goto next_rx;
2330 	}
2331 
2332 	skb_checksum_none_assert(skb);
2333 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2334 		if (dev->features & NETIF_F_RXCSUM) {
2335 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2336 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2337 		}
2338 	} else {
2339 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2340 			if (dev->features & NETIF_F_RXCSUM)
2341 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2342 		}
2343 	}
2344 
2345 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2346 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2347 			u64 ns, ts;
2348 
2349 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2350 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2351 
2352 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2353 				memset(skb_hwtstamps(skb), 0,
2354 				       sizeof(*skb_hwtstamps(skb)));
2355 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2356 			}
2357 		}
2358 	}
2359 	bnxt_deliver_skb(bp, bnapi, skb);
2360 	rc = 1;
2361 
2362 next_rx:
2363 	cpr->rx_packets += 1;
2364 	cpr->rx_bytes += len;
2365 
2366 next_rx_no_len:
2367 	rxr->rx_prod = NEXT_RX(prod);
2368 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2369 
2370 next_rx_no_prod_no_len:
2371 	*raw_cons = tmp_raw_cons;
2372 
2373 	return rc;
2374 
2375 oom_next_rx:
2376 	cpr->sw_stats->rx.rx_oom_discards += 1;
2377 	rc = -ENOMEM;
2378 	goto next_rx;
2379 }
2380 
2381 /* In netpoll mode, if we are using a combined completion ring, we need to
2382  * discard the rx packets and recycle the buffers.
2383  */
2384 static int bnxt_force_rx_discard(struct bnxt *bp,
2385 				 struct bnxt_cp_ring_info *cpr,
2386 				 u32 *raw_cons, u8 *event)
2387 {
2388 	u32 tmp_raw_cons = *raw_cons;
2389 	struct rx_cmp_ext *rxcmp1;
2390 	struct rx_cmp *rxcmp;
2391 	u16 cp_cons;
2392 	u8 cmp_type;
2393 	int rc;
2394 
2395 	cp_cons = RING_CMP(tmp_raw_cons);
2396 	rxcmp = (struct rx_cmp *)
2397 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2398 
2399 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2400 	cp_cons = RING_CMP(tmp_raw_cons);
2401 	rxcmp1 = (struct rx_cmp_ext *)
2402 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2403 
2404 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2405 		return -EBUSY;
2406 
2407 	/* The valid test of the entry must be done first before
2408 	 * reading any further.
2409 	 */
2410 	dma_rmb();
2411 	cmp_type = RX_CMP_TYPE(rxcmp);
2412 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2413 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2414 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2415 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2416 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2417 		struct rx_tpa_end_cmp_ext *tpa_end1;
2418 
2419 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2420 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2421 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2422 	}
2423 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2424 	if (rc && rc != -EBUSY)
2425 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2426 	return rc;
2427 }
2428 
2429 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2430 {
2431 	struct bnxt_fw_health *fw_health = bp->fw_health;
2432 	u32 reg = fw_health->regs[reg_idx];
2433 	u32 reg_type, reg_off, val = 0;
2434 
2435 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2436 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2437 	switch (reg_type) {
2438 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2439 		pci_read_config_dword(bp->pdev, reg_off, &val);
2440 		break;
2441 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2442 		reg_off = fw_health->mapped_regs[reg_idx];
2443 		fallthrough;
2444 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2445 		val = readl(bp->bar0 + reg_off);
2446 		break;
2447 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2448 		val = readl(bp->bar1 + reg_off);
2449 		break;
2450 	}
2451 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2452 		val &= fw_health->fw_reset_inprog_reg_mask;
2453 	return val;
2454 }
2455 
2456 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2457 {
2458 	int i;
2459 
2460 	for (i = 0; i < bp->rx_nr_rings; i++) {
2461 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2462 		struct bnxt_ring_grp_info *grp_info;
2463 
2464 		grp_info = &bp->grp_info[grp_idx];
2465 		if (grp_info->agg_fw_ring_id == ring_id)
2466 			return grp_idx;
2467 	}
2468 	return INVALID_HW_RING_ID;
2469 }
2470 
2471 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2472 {
2473 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2474 
2475 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2476 		return link_info->force_link_speed2;
2477 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2478 		return link_info->force_pam4_link_speed;
2479 	return link_info->force_link_speed;
2480 }
2481 
2482 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2483 {
2484 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2485 
2486 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2487 		link_info->req_link_speed = link_info->force_link_speed2;
2488 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2489 		switch (link_info->req_link_speed) {
2490 		case BNXT_LINK_SPEED_50GB_PAM4:
2491 		case BNXT_LINK_SPEED_100GB_PAM4:
2492 		case BNXT_LINK_SPEED_200GB_PAM4:
2493 		case BNXT_LINK_SPEED_400GB_PAM4:
2494 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2495 			break;
2496 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2497 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2498 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2499 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2500 			break;
2501 		default:
2502 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2503 		}
2504 		return;
2505 	}
2506 	link_info->req_link_speed = link_info->force_link_speed;
2507 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2508 	if (link_info->force_pam4_link_speed) {
2509 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2510 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2511 	}
2512 }
2513 
2514 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2515 {
2516 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2517 
2518 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2519 		link_info->advertising = link_info->auto_link_speeds2;
2520 		return;
2521 	}
2522 	link_info->advertising = link_info->auto_link_speeds;
2523 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2524 }
2525 
2526 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2527 {
2528 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2529 
2530 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2531 		if (link_info->req_link_speed != link_info->force_link_speed2)
2532 			return true;
2533 		return false;
2534 	}
2535 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2536 	    link_info->req_link_speed != link_info->force_link_speed)
2537 		return true;
2538 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2539 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2540 		return true;
2541 	return false;
2542 }
2543 
2544 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2545 {
2546 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2547 
2548 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2549 		if (link_info->advertising != link_info->auto_link_speeds2)
2550 			return true;
2551 		return false;
2552 	}
2553 	if (link_info->advertising != link_info->auto_link_speeds ||
2554 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2555 		return true;
2556 	return false;
2557 }
2558 
2559 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2560 {
2561 	u32 flags = bp->ctx->ctx_arr[type].flags;
2562 
2563 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2564 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2565 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2566 }
2567 
2568 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2569 {
2570 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2571 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2572 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2573 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2574 	struct bnxt_bs_trace_info *bs_trace;
2575 	int last_pg;
2576 
2577 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2578 		return;
2579 
2580 	mem_size = ctxm->max_entries * ctxm->entry_size;
2581 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2582 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2583 
2584 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2585 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2586 
2587 	rmem = &ctx_pg[0].ring_mem;
2588 	bs_trace = &bp->bs_trace[trace_type];
2589 	bs_trace->ctx_type = ctxm->type;
2590 	bs_trace->trace_type = trace_type;
2591 	if (pages > MAX_CTX_PAGES) {
2592 		int last_pg_dir = rmem->nr_pages - 1;
2593 
2594 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2595 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2596 	} else {
2597 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2598 	}
2599 	bs_trace->magic_byte += magic_byte_offset;
2600 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2601 }
2602 
2603 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2604 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2605 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2606 
2607 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2608 	(((data2) &							\
2609 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2610 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2611 
2612 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2613 	((data2) &							\
2614 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2615 
2616 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2617 	(((data2) &							\
2618 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2619 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2620 
2621 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2622 	((data1) &							\
2623 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2624 
2625 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2626 	(((data1) &							\
2627 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2628 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2629 
2630 /* Return true if the workqueue has to be scheduled */
2631 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2632 {
2633 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2634 
2635 	switch (err_type) {
2636 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2637 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2638 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2639 		break;
2640 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2641 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2642 		break;
2643 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2644 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2645 		break;
2646 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2647 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2648 		char *threshold_type;
2649 		bool notify = false;
2650 		char *dir_str;
2651 
2652 		switch (type) {
2653 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2654 			threshold_type = "warning";
2655 			break;
2656 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2657 			threshold_type = "critical";
2658 			break;
2659 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2660 			threshold_type = "fatal";
2661 			break;
2662 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2663 			threshold_type = "shutdown";
2664 			break;
2665 		default:
2666 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2667 			return false;
2668 		}
2669 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2670 			dir_str = "above";
2671 			notify = true;
2672 		} else {
2673 			dir_str = "below";
2674 		}
2675 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2676 			    dir_str, threshold_type);
2677 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2678 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2679 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2680 		if (notify) {
2681 			bp->thermal_threshold_type = type;
2682 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2683 			return true;
2684 		}
2685 		return false;
2686 	}
2687 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2688 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2689 		break;
2690 	default:
2691 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2692 			   err_type);
2693 		break;
2694 	}
2695 	return false;
2696 }
2697 
2698 #define BNXT_GET_EVENT_PORT(data)	\
2699 	((data) &			\
2700 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2701 
2702 #define BNXT_EVENT_RING_TYPE(data2)	\
2703 	((data2) &			\
2704 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2705 
2706 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2707 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2708 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2709 
2710 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2711 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2712 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2713 
2714 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2715 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2716 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2717 
2718 #define BNXT_PHC_BITS	48
2719 
2720 static int bnxt_async_event_process(struct bnxt *bp,
2721 				    struct hwrm_async_event_cmpl *cmpl)
2722 {
2723 	u16 event_id = le16_to_cpu(cmpl->event_id);
2724 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2725 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2726 
2727 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2728 		   event_id, data1, data2);
2729 
2730 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2731 	switch (event_id) {
2732 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2733 		struct bnxt_link_info *link_info = &bp->link_info;
2734 
2735 		if (BNXT_VF(bp))
2736 			goto async_event_process_exit;
2737 
2738 		/* print unsupported speed warning in forced speed mode only */
2739 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2740 		    (data1 & 0x20000)) {
2741 			u16 fw_speed = bnxt_get_force_speed(link_info);
2742 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2743 
2744 			if (speed != SPEED_UNKNOWN)
2745 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2746 					    speed);
2747 		}
2748 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2749 	}
2750 		fallthrough;
2751 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2752 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2753 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2754 		fallthrough;
2755 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2756 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2757 		break;
2758 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2759 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2760 		break;
2761 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2762 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2763 
2764 		if (BNXT_VF(bp))
2765 			break;
2766 
2767 		if (bp->pf.port_id != port_id)
2768 			break;
2769 
2770 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2771 		break;
2772 	}
2773 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2774 		if (BNXT_PF(bp))
2775 			goto async_event_process_exit;
2776 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2777 		break;
2778 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2779 		char *type_str = "Solicited";
2780 
2781 		if (!bp->fw_health)
2782 			goto async_event_process_exit;
2783 
2784 		bp->fw_reset_timestamp = jiffies;
2785 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2786 		if (!bp->fw_reset_min_dsecs)
2787 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2788 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2789 		if (!bp->fw_reset_max_dsecs)
2790 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2791 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2792 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2793 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2794 			type_str = "Fatal";
2795 			bp->fw_health->fatalities++;
2796 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2797 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2798 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2799 			type_str = "Non-fatal";
2800 			bp->fw_health->survivals++;
2801 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2802 		}
2803 		netif_warn(bp, hw, bp->dev,
2804 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2805 			   type_str, data1, data2,
2806 			   bp->fw_reset_min_dsecs * 100,
2807 			   bp->fw_reset_max_dsecs * 100);
2808 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2809 		break;
2810 	}
2811 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2812 		struct bnxt_fw_health *fw_health = bp->fw_health;
2813 		char *status_desc = "healthy";
2814 		u32 status;
2815 
2816 		if (!fw_health)
2817 			goto async_event_process_exit;
2818 
2819 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2820 			fw_health->enabled = false;
2821 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2822 			break;
2823 		}
2824 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2825 		fw_health->tmr_multiplier =
2826 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2827 				     bp->current_interval * 10);
2828 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2829 		if (!fw_health->enabled)
2830 			fw_health->last_fw_heartbeat =
2831 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2832 		fw_health->last_fw_reset_cnt =
2833 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2834 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2835 		if (status != BNXT_FW_STATUS_HEALTHY)
2836 			status_desc = "unhealthy";
2837 		netif_info(bp, drv, bp->dev,
2838 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2839 			   fw_health->primary ? "primary" : "backup", status,
2840 			   status_desc, fw_health->last_fw_reset_cnt);
2841 		if (!fw_health->enabled) {
2842 			/* Make sure tmr_counter is set and visible to
2843 			 * bnxt_health_check() before setting enabled to true.
2844 			 */
2845 			smp_wmb();
2846 			fw_health->enabled = true;
2847 		}
2848 		goto async_event_process_exit;
2849 	}
2850 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2851 		netif_notice(bp, hw, bp->dev,
2852 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2853 			     data1, data2);
2854 		goto async_event_process_exit;
2855 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2856 		struct bnxt_rx_ring_info *rxr;
2857 		u16 grp_idx;
2858 
2859 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2860 			goto async_event_process_exit;
2861 
2862 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2863 			    BNXT_EVENT_RING_TYPE(data2), data1);
2864 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2865 			goto async_event_process_exit;
2866 
2867 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2868 		if (grp_idx == INVALID_HW_RING_ID) {
2869 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2870 				    data1);
2871 			goto async_event_process_exit;
2872 		}
2873 		rxr = bp->bnapi[grp_idx]->rx_ring;
2874 		bnxt_sched_reset_rxr(bp, rxr);
2875 		goto async_event_process_exit;
2876 	}
2877 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2878 		struct bnxt_fw_health *fw_health = bp->fw_health;
2879 
2880 		netif_notice(bp, hw, bp->dev,
2881 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2882 			     data1, data2);
2883 		if (fw_health) {
2884 			fw_health->echo_req_data1 = data1;
2885 			fw_health->echo_req_data2 = data2;
2886 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2887 			break;
2888 		}
2889 		goto async_event_process_exit;
2890 	}
2891 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2892 		bnxt_ptp_pps_event(bp, data1, data2);
2893 		goto async_event_process_exit;
2894 	}
2895 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2896 		if (bnxt_event_error_report(bp, data1, data2))
2897 			break;
2898 		goto async_event_process_exit;
2899 	}
2900 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2901 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2902 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2903 			if (BNXT_PTP_USE_RTC(bp)) {
2904 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2905 				unsigned long flags;
2906 				u64 ns;
2907 
2908 				if (!ptp)
2909 					goto async_event_process_exit;
2910 
2911 				bnxt_ptp_update_current_time(bp);
2912 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2913 				       BNXT_PHC_BITS) | ptp->current_time);
2914 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2915 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2916 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2917 			}
2918 			break;
2919 		}
2920 		goto async_event_process_exit;
2921 	}
2922 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2923 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2924 
2925 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2926 		goto async_event_process_exit;
2927 	}
2928 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2929 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2930 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2931 
2932 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2933 		goto async_event_process_exit;
2934 	}
2935 	default:
2936 		goto async_event_process_exit;
2937 	}
2938 	__bnxt_queue_sp_work(bp);
2939 async_event_process_exit:
2940 	bnxt_ulp_async_events(bp, cmpl);
2941 	return 0;
2942 }
2943 
2944 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2945 {
2946 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2947 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2948 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2949 				(struct hwrm_fwd_req_cmpl *)txcmp;
2950 
2951 	switch (cmpl_type) {
2952 	case CMPL_BASE_TYPE_HWRM_DONE:
2953 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2954 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2955 		break;
2956 
2957 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2958 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2959 
2960 		if ((vf_id < bp->pf.first_vf_id) ||
2961 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2962 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2963 				   vf_id);
2964 			return -EINVAL;
2965 		}
2966 
2967 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2968 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2969 		break;
2970 
2971 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2972 		bnxt_async_event_process(bp,
2973 					 (struct hwrm_async_event_cmpl *)txcmp);
2974 		break;
2975 
2976 	default:
2977 		break;
2978 	}
2979 
2980 	return 0;
2981 }
2982 
2983 static bool bnxt_vnic_is_active(struct bnxt *bp)
2984 {
2985 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2986 
2987 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2988 }
2989 
2990 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2991 {
2992 	struct bnxt_napi *bnapi = dev_instance;
2993 	struct bnxt *bp = bnapi->bp;
2994 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2995 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2996 
2997 	cpr->event_ctr++;
2998 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2999 	napi_schedule(&bnapi->napi);
3000 	return IRQ_HANDLED;
3001 }
3002 
3003 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
3004 {
3005 	u32 raw_cons = cpr->cp_raw_cons;
3006 	u16 cons = RING_CMP(raw_cons);
3007 	struct tx_cmp *txcmp;
3008 
3009 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3010 
3011 	return TX_CMP_VALID(txcmp, raw_cons);
3012 }
3013 
3014 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3015 			    int budget)
3016 {
3017 	struct bnxt_napi *bnapi = cpr->bnapi;
3018 	u32 raw_cons = cpr->cp_raw_cons;
3019 	bool flush_xdp = false;
3020 	u32 cons;
3021 	int rx_pkts = 0;
3022 	u8 event = 0;
3023 	struct tx_cmp *txcmp;
3024 
3025 	cpr->has_more_work = 0;
3026 	cpr->had_work_done = 1;
3027 	while (1) {
3028 		u8 cmp_type;
3029 		int rc;
3030 
3031 		cons = RING_CMP(raw_cons);
3032 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3033 
3034 		if (!TX_CMP_VALID(txcmp, raw_cons))
3035 			break;
3036 
3037 		/* The valid test of the entry must be done first before
3038 		 * reading any further.
3039 		 */
3040 		dma_rmb();
3041 		cmp_type = TX_CMP_TYPE(txcmp);
3042 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3043 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3044 			u32 opaque = txcmp->tx_cmp_opaque;
3045 			struct bnxt_tx_ring_info *txr;
3046 			u16 tx_freed;
3047 
3048 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3049 			event |= BNXT_TX_CMP_EVENT;
3050 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3051 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3052 			else
3053 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3054 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3055 				   bp->tx_ring_mask;
3056 			/* return full budget so NAPI will complete. */
3057 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3058 				rx_pkts = budget;
3059 				raw_cons = NEXT_RAW_CMP(raw_cons);
3060 				if (budget)
3061 					cpr->has_more_work = 1;
3062 				break;
3063 			}
3064 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3065 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3066 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3067 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3068 			if (likely(budget))
3069 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3070 			else
3071 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3072 							   &event);
3073 			if (event & BNXT_REDIRECT_EVENT)
3074 				flush_xdp = true;
3075 			if (likely(rc >= 0))
3076 				rx_pkts += rc;
3077 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3078 			 * the NAPI budget.  Otherwise, we may potentially loop
3079 			 * here forever if we consistently cannot allocate
3080 			 * buffers.
3081 			 */
3082 			else if (rc == -ENOMEM && budget)
3083 				rx_pkts++;
3084 			else if (rc == -EBUSY)	/* partial completion */
3085 				break;
3086 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3087 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3088 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3089 			bnxt_hwrm_handler(bp, txcmp);
3090 		}
3091 		raw_cons = NEXT_RAW_CMP(raw_cons);
3092 
3093 		if (rx_pkts && rx_pkts == budget) {
3094 			cpr->has_more_work = 1;
3095 			break;
3096 		}
3097 	}
3098 
3099 	if (flush_xdp) {
3100 		xdp_do_flush();
3101 		event &= ~BNXT_REDIRECT_EVENT;
3102 	}
3103 
3104 	if (event & BNXT_TX_EVENT) {
3105 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3106 		u16 prod = txr->tx_prod;
3107 
3108 		/* Sync BD data before updating doorbell */
3109 		wmb();
3110 
3111 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3112 		event &= ~BNXT_TX_EVENT;
3113 	}
3114 
3115 	cpr->cp_raw_cons = raw_cons;
3116 	bnapi->events |= event;
3117 	return rx_pkts;
3118 }
3119 
3120 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3121 				  int budget)
3122 {
3123 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3124 		bnapi->tx_int(bp, bnapi, budget);
3125 
3126 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3127 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3128 
3129 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3130 		bnapi->events &= ~BNXT_RX_EVENT;
3131 	}
3132 	if (bnapi->events & BNXT_AGG_EVENT) {
3133 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3134 
3135 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3136 		bnapi->events &= ~BNXT_AGG_EVENT;
3137 	}
3138 }
3139 
3140 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3141 			  int budget)
3142 {
3143 	struct bnxt_napi *bnapi = cpr->bnapi;
3144 	int rx_pkts;
3145 
3146 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3147 
3148 	/* ACK completion ring before freeing tx ring and producing new
3149 	 * buffers in rx/agg rings to prevent overflowing the completion
3150 	 * ring.
3151 	 */
3152 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3153 
3154 	__bnxt_poll_work_done(bp, bnapi, budget);
3155 	return rx_pkts;
3156 }
3157 
3158 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3159 {
3160 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3161 	struct bnxt *bp = bnapi->bp;
3162 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3163 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3164 	struct tx_cmp *txcmp;
3165 	struct rx_cmp_ext *rxcmp1;
3166 	u32 cp_cons, tmp_raw_cons;
3167 	u32 raw_cons = cpr->cp_raw_cons;
3168 	bool flush_xdp = false;
3169 	u32 rx_pkts = 0;
3170 	u8 event = 0;
3171 
3172 	while (1) {
3173 		int rc;
3174 
3175 		cp_cons = RING_CMP(raw_cons);
3176 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3177 
3178 		if (!TX_CMP_VALID(txcmp, raw_cons))
3179 			break;
3180 
3181 		/* The valid test of the entry must be done first before
3182 		 * reading any further.
3183 		 */
3184 		dma_rmb();
3185 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3186 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3187 			cp_cons = RING_CMP(tmp_raw_cons);
3188 			rxcmp1 = (struct rx_cmp_ext *)
3189 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3190 
3191 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3192 				break;
3193 
3194 			/* force an error to recycle the buffer */
3195 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3196 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3197 
3198 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3199 			if (likely(rc == -EIO) && budget)
3200 				rx_pkts++;
3201 			else if (rc == -EBUSY)	/* partial completion */
3202 				break;
3203 			if (event & BNXT_REDIRECT_EVENT)
3204 				flush_xdp = true;
3205 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3206 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3207 			bnxt_hwrm_handler(bp, txcmp);
3208 		} else {
3209 			netdev_err(bp->dev,
3210 				   "Invalid completion received on special ring\n");
3211 		}
3212 		raw_cons = NEXT_RAW_CMP(raw_cons);
3213 
3214 		if (rx_pkts == budget)
3215 			break;
3216 	}
3217 
3218 	cpr->cp_raw_cons = raw_cons;
3219 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3220 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3221 
3222 	if (event & BNXT_AGG_EVENT)
3223 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3224 	if (flush_xdp)
3225 		xdp_do_flush();
3226 
3227 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3228 		napi_complete_done(napi, rx_pkts);
3229 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3230 	}
3231 	return rx_pkts;
3232 }
3233 
3234 static int bnxt_poll(struct napi_struct *napi, int budget)
3235 {
3236 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3237 	struct bnxt *bp = bnapi->bp;
3238 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3239 	int work_done = 0;
3240 
3241 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3242 		napi_complete(napi);
3243 		return 0;
3244 	}
3245 	while (1) {
3246 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3247 
3248 		if (work_done >= budget) {
3249 			if (!budget)
3250 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3251 			break;
3252 		}
3253 
3254 		if (!bnxt_has_work(bp, cpr)) {
3255 			if (napi_complete_done(napi, work_done))
3256 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3257 			break;
3258 		}
3259 	}
3260 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3261 		struct dim_sample dim_sample = {};
3262 
3263 		dim_update_sample(cpr->event_ctr,
3264 				  cpr->rx_packets,
3265 				  cpr->rx_bytes,
3266 				  &dim_sample);
3267 		net_dim(&cpr->dim, &dim_sample);
3268 	}
3269 	return work_done;
3270 }
3271 
3272 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3273 {
3274 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3275 	int i, work_done = 0;
3276 
3277 	for (i = 0; i < cpr->cp_ring_count; i++) {
3278 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3279 
3280 		if (cpr2->had_nqe_notify) {
3281 			work_done += __bnxt_poll_work(bp, cpr2,
3282 						      budget - work_done);
3283 			cpr->has_more_work |= cpr2->has_more_work;
3284 		}
3285 	}
3286 	return work_done;
3287 }
3288 
3289 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3290 				 u64 dbr_type, int budget)
3291 {
3292 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3293 	int i;
3294 
3295 	for (i = 0; i < cpr->cp_ring_count; i++) {
3296 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3297 		struct bnxt_db_info *db;
3298 
3299 		if (cpr2->had_work_done) {
3300 			u32 tgl = 0;
3301 
3302 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3303 				cpr2->had_nqe_notify = 0;
3304 				tgl = cpr2->toggle;
3305 			}
3306 			db = &cpr2->cp_db;
3307 			bnxt_writeq(bp,
3308 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3309 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3310 				    db->doorbell);
3311 			cpr2->had_work_done = 0;
3312 		}
3313 	}
3314 	__bnxt_poll_work_done(bp, bnapi, budget);
3315 }
3316 
3317 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3318 {
3319 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3320 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3321 	struct bnxt_cp_ring_info *cpr_rx;
3322 	u32 raw_cons = cpr->cp_raw_cons;
3323 	struct bnxt *bp = bnapi->bp;
3324 	struct nqe_cn *nqcmp;
3325 	int work_done = 0;
3326 	u32 cons;
3327 
3328 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3329 		napi_complete(napi);
3330 		return 0;
3331 	}
3332 	if (cpr->has_more_work) {
3333 		cpr->has_more_work = 0;
3334 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3335 	}
3336 	while (1) {
3337 		u16 type;
3338 
3339 		cons = RING_CMP(raw_cons);
3340 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3341 
3342 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3343 			if (cpr->has_more_work)
3344 				break;
3345 
3346 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3347 					     budget);
3348 			cpr->cp_raw_cons = raw_cons;
3349 			if (napi_complete_done(napi, work_done))
3350 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3351 						  cpr->cp_raw_cons);
3352 			goto poll_done;
3353 		}
3354 
3355 		/* The valid test of the entry must be done first before
3356 		 * reading any further.
3357 		 */
3358 		dma_rmb();
3359 
3360 		type = le16_to_cpu(nqcmp->type);
3361 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3362 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3363 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3364 			struct bnxt_cp_ring_info *cpr2;
3365 
3366 			/* No more budget for RX work */
3367 			if (budget && work_done >= budget &&
3368 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3369 				break;
3370 
3371 			idx = BNXT_NQ_HDL_IDX(idx);
3372 			cpr2 = &cpr->cp_ring_arr[idx];
3373 			cpr2->had_nqe_notify = 1;
3374 			cpr2->toggle = NQE_CN_TOGGLE(type);
3375 			work_done += __bnxt_poll_work(bp, cpr2,
3376 						      budget - work_done);
3377 			cpr->has_more_work |= cpr2->has_more_work;
3378 		} else {
3379 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3380 		}
3381 		raw_cons = NEXT_RAW_CMP(raw_cons);
3382 	}
3383 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3384 	if (raw_cons != cpr->cp_raw_cons) {
3385 		cpr->cp_raw_cons = raw_cons;
3386 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3387 	}
3388 poll_done:
3389 	cpr_rx = &cpr->cp_ring_arr[0];
3390 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3391 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3392 		struct dim_sample dim_sample = {};
3393 
3394 		dim_update_sample(cpr->event_ctr,
3395 				  cpr_rx->rx_packets,
3396 				  cpr_rx->rx_bytes,
3397 				  &dim_sample);
3398 		net_dim(&cpr->dim, &dim_sample);
3399 	}
3400 	return work_done;
3401 }
3402 
3403 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3404 				       struct bnxt_tx_ring_info *txr, int idx)
3405 {
3406 	int i, max_idx;
3407 	struct pci_dev *pdev = bp->pdev;
3408 
3409 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3410 
3411 	for (i = 0; i < max_idx;) {
3412 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3413 		struct sk_buff *skb;
3414 		int j, last;
3415 
3416 		if (idx  < bp->tx_nr_rings_xdp &&
3417 		    tx_buf->action == XDP_REDIRECT) {
3418 			dma_unmap_single(&pdev->dev,
3419 					 dma_unmap_addr(tx_buf, mapping),
3420 					 dma_unmap_len(tx_buf, len),
3421 					 DMA_TO_DEVICE);
3422 			xdp_return_frame(tx_buf->xdpf);
3423 			tx_buf->action = 0;
3424 			tx_buf->xdpf = NULL;
3425 			i++;
3426 			continue;
3427 		}
3428 
3429 		skb = tx_buf->skb;
3430 		if (!skb) {
3431 			i++;
3432 			continue;
3433 		}
3434 
3435 		tx_buf->skb = NULL;
3436 
3437 		if (tx_buf->is_push) {
3438 			dev_kfree_skb(skb);
3439 			i += 2;
3440 			continue;
3441 		}
3442 
3443 		dma_unmap_single(&pdev->dev,
3444 				 dma_unmap_addr(tx_buf, mapping),
3445 				 skb_headlen(skb),
3446 				 DMA_TO_DEVICE);
3447 
3448 		last = tx_buf->nr_frags;
3449 		i += 2;
3450 		for (j = 0; j < last; j++, i++) {
3451 			int ring_idx = i & bp->tx_ring_mask;
3452 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3453 
3454 			tx_buf = &txr->tx_buf_ring[ring_idx];
3455 			netmem_dma_unmap_page_attrs(&pdev->dev,
3456 						    dma_unmap_addr(tx_buf,
3457 								   mapping),
3458 						    skb_frag_size(frag),
3459 						    DMA_TO_DEVICE, 0);
3460 		}
3461 		dev_kfree_skb(skb);
3462 	}
3463 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3464 }
3465 
3466 static void bnxt_free_tx_skbs(struct bnxt *bp)
3467 {
3468 	int i;
3469 
3470 	if (!bp->tx_ring)
3471 		return;
3472 
3473 	for (i = 0; i < bp->tx_nr_rings; i++) {
3474 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3475 
3476 		if (!txr->tx_buf_ring)
3477 			continue;
3478 
3479 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3480 	}
3481 
3482 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3483 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3484 }
3485 
3486 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3487 {
3488 	int i, max_idx;
3489 
3490 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3491 
3492 	for (i = 0; i < max_idx; i++) {
3493 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3494 		void *data = rx_buf->data;
3495 
3496 		if (!data)
3497 			continue;
3498 
3499 		rx_buf->data = NULL;
3500 		if (BNXT_RX_PAGE_MODE(bp))
3501 			page_pool_recycle_direct(rxr->page_pool, data);
3502 		else
3503 			page_pool_free_va(rxr->head_pool, data, true);
3504 	}
3505 }
3506 
3507 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3508 {
3509 	int i, max_idx;
3510 
3511 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3512 
3513 	for (i = 0; i < max_idx; i++) {
3514 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3515 		netmem_ref netmem = rx_agg_buf->netmem;
3516 
3517 		if (!netmem)
3518 			continue;
3519 
3520 		rx_agg_buf->netmem = 0;
3521 		__clear_bit(i, rxr->rx_agg_bmap);
3522 
3523 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3524 	}
3525 }
3526 
3527 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3528 					struct bnxt_rx_ring_info *rxr)
3529 {
3530 	int i;
3531 
3532 	for (i = 0; i < bp->max_tpa; i++) {
3533 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3534 		u8 *data = tpa_info->data;
3535 
3536 		if (!data)
3537 			continue;
3538 
3539 		tpa_info->data = NULL;
3540 		page_pool_free_va(rxr->head_pool, data, false);
3541 	}
3542 }
3543 
3544 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3545 				       struct bnxt_rx_ring_info *rxr)
3546 {
3547 	struct bnxt_tpa_idx_map *map;
3548 
3549 	if (!rxr->rx_tpa)
3550 		goto skip_rx_tpa_free;
3551 
3552 	bnxt_free_one_tpa_info_data(bp, rxr);
3553 
3554 skip_rx_tpa_free:
3555 	if (!rxr->rx_buf_ring)
3556 		goto skip_rx_buf_free;
3557 
3558 	bnxt_free_one_rx_ring(bp, rxr);
3559 
3560 skip_rx_buf_free:
3561 	if (!rxr->rx_agg_ring)
3562 		goto skip_rx_agg_free;
3563 
3564 	bnxt_free_one_rx_agg_ring(bp, rxr);
3565 
3566 skip_rx_agg_free:
3567 	map = rxr->rx_tpa_idx_map;
3568 	if (map)
3569 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3570 }
3571 
3572 static void bnxt_free_rx_skbs(struct bnxt *bp)
3573 {
3574 	int i;
3575 
3576 	if (!bp->rx_ring)
3577 		return;
3578 
3579 	for (i = 0; i < bp->rx_nr_rings; i++)
3580 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3581 }
3582 
3583 static void bnxt_free_skbs(struct bnxt *bp)
3584 {
3585 	bnxt_free_tx_skbs(bp);
3586 	bnxt_free_rx_skbs(bp);
3587 }
3588 
3589 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3590 {
3591 	u8 init_val = ctxm->init_value;
3592 	u16 offset = ctxm->init_offset;
3593 	u8 *p2 = p;
3594 	int i;
3595 
3596 	if (!init_val)
3597 		return;
3598 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3599 		memset(p, init_val, len);
3600 		return;
3601 	}
3602 	for (i = 0; i < len; i += ctxm->entry_size)
3603 		*(p2 + i + offset) = init_val;
3604 }
3605 
3606 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3607 			       void *buf, size_t offset, size_t head,
3608 			       size_t tail)
3609 {
3610 	int i, head_page, start_idx, source_offset;
3611 	size_t len, rem_len, total_len, max_bytes;
3612 
3613 	head_page = head / rmem->page_size;
3614 	source_offset = head % rmem->page_size;
3615 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3616 	if (!total_len)
3617 		total_len = MAX_CTX_BYTES;
3618 	start_idx = head_page % MAX_CTX_PAGES;
3619 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3620 		    source_offset;
3621 	total_len = min(total_len, max_bytes);
3622 	rem_len = total_len;
3623 
3624 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3625 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3626 		if (buf)
3627 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3628 			       len);
3629 		offset += len;
3630 		rem_len -= len;
3631 	}
3632 	return total_len;
3633 }
3634 
3635 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3636 {
3637 	struct pci_dev *pdev = bp->pdev;
3638 	int i;
3639 
3640 	if (!rmem->pg_arr)
3641 		goto skip_pages;
3642 
3643 	for (i = 0; i < rmem->nr_pages; i++) {
3644 		if (!rmem->pg_arr[i])
3645 			continue;
3646 
3647 		dma_free_coherent(&pdev->dev, rmem->page_size,
3648 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3649 
3650 		rmem->pg_arr[i] = NULL;
3651 	}
3652 skip_pages:
3653 	if (rmem->pg_tbl) {
3654 		size_t pg_tbl_size = rmem->nr_pages * 8;
3655 
3656 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3657 			pg_tbl_size = rmem->page_size;
3658 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3659 				  rmem->pg_tbl, rmem->pg_tbl_map);
3660 		rmem->pg_tbl = NULL;
3661 	}
3662 	if (rmem->vmem_size && *rmem->vmem) {
3663 		vfree(*rmem->vmem);
3664 		*rmem->vmem = NULL;
3665 	}
3666 }
3667 
3668 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3669 {
3670 	struct pci_dev *pdev = bp->pdev;
3671 	u64 valid_bit = 0;
3672 	int i;
3673 
3674 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3675 		valid_bit = PTU_PTE_VALID;
3676 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3677 		size_t pg_tbl_size = rmem->nr_pages * 8;
3678 
3679 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3680 			pg_tbl_size = rmem->page_size;
3681 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3682 						  &rmem->pg_tbl_map,
3683 						  GFP_KERNEL);
3684 		if (!rmem->pg_tbl)
3685 			return -ENOMEM;
3686 	}
3687 
3688 	for (i = 0; i < rmem->nr_pages; i++) {
3689 		u64 extra_bits = valid_bit;
3690 
3691 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3692 						     rmem->page_size,
3693 						     &rmem->dma_arr[i],
3694 						     GFP_KERNEL);
3695 		if (!rmem->pg_arr[i])
3696 			return -ENOMEM;
3697 
3698 		if (rmem->ctx_mem)
3699 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3700 					  rmem->page_size);
3701 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3702 			if (i == rmem->nr_pages - 2 &&
3703 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3704 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3705 			else if (i == rmem->nr_pages - 1 &&
3706 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3707 				extra_bits |= PTU_PTE_LAST;
3708 			rmem->pg_tbl[i] =
3709 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3710 		}
3711 	}
3712 
3713 	if (rmem->vmem_size) {
3714 		*rmem->vmem = vzalloc(rmem->vmem_size);
3715 		if (!(*rmem->vmem))
3716 			return -ENOMEM;
3717 	}
3718 	return 0;
3719 }
3720 
3721 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3722 				   struct bnxt_rx_ring_info *rxr)
3723 {
3724 	int i;
3725 
3726 	kfree(rxr->rx_tpa_idx_map);
3727 	rxr->rx_tpa_idx_map = NULL;
3728 	if (rxr->rx_tpa) {
3729 		for (i = 0; i < bp->max_tpa; i++) {
3730 			kfree(rxr->rx_tpa[i].agg_arr);
3731 			rxr->rx_tpa[i].agg_arr = NULL;
3732 		}
3733 	}
3734 	kfree(rxr->rx_tpa);
3735 	rxr->rx_tpa = NULL;
3736 }
3737 
3738 static void bnxt_free_tpa_info(struct bnxt *bp)
3739 {
3740 	int i;
3741 
3742 	for (i = 0; i < bp->rx_nr_rings; i++) {
3743 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3744 
3745 		bnxt_free_one_tpa_info(bp, rxr);
3746 	}
3747 }
3748 
3749 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3750 				   struct bnxt_rx_ring_info *rxr)
3751 {
3752 	struct rx_agg_cmp *agg;
3753 	int i;
3754 
3755 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3756 			      GFP_KERNEL);
3757 	if (!rxr->rx_tpa)
3758 		return -ENOMEM;
3759 
3760 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3761 		return 0;
3762 	for (i = 0; i < bp->max_tpa; i++) {
3763 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3764 		if (!agg)
3765 			return -ENOMEM;
3766 		rxr->rx_tpa[i].agg_arr = agg;
3767 	}
3768 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3769 				      GFP_KERNEL);
3770 	if (!rxr->rx_tpa_idx_map)
3771 		return -ENOMEM;
3772 
3773 	return 0;
3774 }
3775 
3776 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3777 {
3778 	int i, rc;
3779 
3780 	bp->max_tpa = MAX_TPA;
3781 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3782 		if (!bp->max_tpa_v2)
3783 			return 0;
3784 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3785 	}
3786 
3787 	for (i = 0; i < bp->rx_nr_rings; i++) {
3788 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3789 
3790 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3791 		if (rc)
3792 			return rc;
3793 	}
3794 	return 0;
3795 }
3796 
3797 static void bnxt_free_rx_rings(struct bnxt *bp)
3798 {
3799 	int i;
3800 
3801 	if (!bp->rx_ring)
3802 		return;
3803 
3804 	bnxt_free_tpa_info(bp);
3805 	for (i = 0; i < bp->rx_nr_rings; i++) {
3806 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3807 		struct bnxt_ring_struct *ring;
3808 
3809 		if (rxr->xdp_prog)
3810 			bpf_prog_put(rxr->xdp_prog);
3811 
3812 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3813 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3814 
3815 		page_pool_destroy(rxr->page_pool);
3816 		page_pool_destroy(rxr->head_pool);
3817 		rxr->page_pool = rxr->head_pool = NULL;
3818 
3819 		kfree(rxr->rx_agg_bmap);
3820 		rxr->rx_agg_bmap = NULL;
3821 
3822 		ring = &rxr->rx_ring_struct;
3823 		bnxt_free_ring(bp, &ring->ring_mem);
3824 
3825 		ring = &rxr->rx_agg_ring_struct;
3826 		bnxt_free_ring(bp, &ring->ring_mem);
3827 	}
3828 }
3829 
3830 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3831 				       struct bnxt_rx_ring_info *rxr)
3832 {
3833 	/* User may have chosen larger than default rx_page_size,
3834 	 * we keep the ring sizes uniform and also want uniform amount
3835 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3836 	 */
3837 	int fill_level = bp->rx_agg_ring_size;
3838 
3839 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3840 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3841 
3842 	return fill_level;
3843 }
3844 
3845 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3846 				   struct bnxt_rx_ring_info *rxr,
3847 				   int numa_node)
3848 {
3849 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3850 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3851 	struct page_pool_params pp = { 0 };
3852 	struct page_pool *pool;
3853 
3854 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3855 	if (BNXT_RX_PAGE_MODE(bp))
3856 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3857 
3858 	pp.order = get_order(rxr->rx_page_size);
3859 	pp.nid = numa_node;
3860 	pp.netdev = bp->dev;
3861 	pp.dev = &bp->pdev->dev;
3862 	pp.dma_dir = bp->rx_dir;
3863 	pp.max_len = PAGE_SIZE << pp.order;
3864 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3865 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3866 	pp.queue_idx = rxr->bnapi->index;
3867 
3868 	pool = page_pool_create(&pp);
3869 	if (IS_ERR(pool))
3870 		return PTR_ERR(pool);
3871 	rxr->page_pool = pool;
3872 
3873 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3874 	rxr->need_head_pool |= !!pp.order;
3875 	if (bnxt_separate_head_pool(rxr)) {
3876 		pp.order = 0;
3877 		pp.max_len = PAGE_SIZE;
3878 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3879 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3880 		pool = page_pool_create(&pp);
3881 		if (IS_ERR(pool))
3882 			goto err_destroy_pp;
3883 	} else {
3884 		page_pool_get(pool);
3885 	}
3886 	rxr->head_pool = pool;
3887 
3888 	return 0;
3889 
3890 err_destroy_pp:
3891 	page_pool_destroy(rxr->page_pool);
3892 	rxr->page_pool = NULL;
3893 	return PTR_ERR(pool);
3894 }
3895 
3896 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3897 {
3898 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3899 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3900 }
3901 
3902 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3903 {
3904 	u16 mem_size;
3905 
3906 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3907 	mem_size = rxr->rx_agg_bmap_size / 8;
3908 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3909 	if (!rxr->rx_agg_bmap)
3910 		return -ENOMEM;
3911 
3912 	return 0;
3913 }
3914 
3915 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3916 {
3917 	int numa_node = dev_to_node(&bp->pdev->dev);
3918 	int i, rc = 0, agg_rings = 0, cpu;
3919 
3920 	if (!bp->rx_ring)
3921 		return -ENOMEM;
3922 
3923 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3924 		agg_rings = 1;
3925 
3926 	for (i = 0; i < bp->rx_nr_rings; i++) {
3927 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3928 		struct bnxt_ring_struct *ring;
3929 		int cpu_node;
3930 
3931 		ring = &rxr->rx_ring_struct;
3932 
3933 		cpu = cpumask_local_spread(i, numa_node);
3934 		cpu_node = cpu_to_node(cpu);
3935 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3936 			   i, cpu_node);
3937 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3938 		if (rc)
3939 			return rc;
3940 		bnxt_enable_rx_page_pool(rxr);
3941 
3942 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3943 		if (rc < 0)
3944 			return rc;
3945 
3946 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3947 						MEM_TYPE_PAGE_POOL,
3948 						rxr->page_pool);
3949 		if (rc) {
3950 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3951 			return rc;
3952 		}
3953 
3954 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3955 		if (rc)
3956 			return rc;
3957 
3958 		ring->grp_idx = i;
3959 		if (agg_rings) {
3960 			ring = &rxr->rx_agg_ring_struct;
3961 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3962 			if (rc)
3963 				return rc;
3964 
3965 			ring->grp_idx = i;
3966 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3967 			if (rc)
3968 				return rc;
3969 		}
3970 	}
3971 	if (bp->flags & BNXT_FLAG_TPA)
3972 		rc = bnxt_alloc_tpa_info(bp);
3973 	return rc;
3974 }
3975 
3976 static void bnxt_free_tx_rings(struct bnxt *bp)
3977 {
3978 	int i;
3979 	struct pci_dev *pdev = bp->pdev;
3980 
3981 	if (!bp->tx_ring)
3982 		return;
3983 
3984 	for (i = 0; i < bp->tx_nr_rings; i++) {
3985 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3986 		struct bnxt_ring_struct *ring;
3987 
3988 		if (txr->tx_push) {
3989 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3990 					  txr->tx_push, txr->tx_push_mapping);
3991 			txr->tx_push = NULL;
3992 		}
3993 
3994 		ring = &txr->tx_ring_struct;
3995 
3996 		bnxt_free_ring(bp, &ring->ring_mem);
3997 	}
3998 }
3999 
4000 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
4001 	((tc) * (bp)->tx_nr_rings_per_tc)
4002 
4003 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
4004 	((tx) % (bp)->tx_nr_rings_per_tc)
4005 
4006 #define BNXT_RING_TO_TC(bp, tx)		\
4007 	((tx) / (bp)->tx_nr_rings_per_tc)
4008 
4009 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4010 {
4011 	int i, j, rc;
4012 	struct pci_dev *pdev = bp->pdev;
4013 
4014 	bp->tx_push_size = 0;
4015 	if (bp->tx_push_thresh) {
4016 		int push_size;
4017 
4018 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4019 					bp->tx_push_thresh);
4020 
4021 		if (push_size > 256) {
4022 			push_size = 0;
4023 			bp->tx_push_thresh = 0;
4024 		}
4025 
4026 		bp->tx_push_size = push_size;
4027 	}
4028 
4029 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4030 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4031 		struct bnxt_ring_struct *ring;
4032 		u8 qidx;
4033 
4034 		ring = &txr->tx_ring_struct;
4035 
4036 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4037 		if (rc)
4038 			return rc;
4039 
4040 		ring->grp_idx = txr->bnapi->index;
4041 		if (bp->tx_push_size) {
4042 			dma_addr_t mapping;
4043 
4044 			/* One pre-allocated DMA buffer to backup
4045 			 * TX push operation
4046 			 */
4047 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4048 						bp->tx_push_size,
4049 						&txr->tx_push_mapping,
4050 						GFP_KERNEL);
4051 
4052 			if (!txr->tx_push)
4053 				return -ENOMEM;
4054 
4055 			mapping = txr->tx_push_mapping +
4056 				sizeof(struct tx_push_bd);
4057 			txr->data_mapping = cpu_to_le64(mapping);
4058 		}
4059 		qidx = bp->tc_to_qidx[j];
4060 		ring->queue_id = bp->q_info[qidx].queue_id;
4061 		spin_lock_init(&txr->xdp_tx_lock);
4062 		if (i < bp->tx_nr_rings_xdp)
4063 			continue;
4064 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4065 			j++;
4066 	}
4067 	return 0;
4068 }
4069 
4070 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4071 {
4072 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4073 
4074 	kfree(cpr->cp_desc_ring);
4075 	cpr->cp_desc_ring = NULL;
4076 	ring->ring_mem.pg_arr = NULL;
4077 	kfree(cpr->cp_desc_mapping);
4078 	cpr->cp_desc_mapping = NULL;
4079 	ring->ring_mem.dma_arr = NULL;
4080 }
4081 
4082 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4083 {
4084 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
4085 	if (!cpr->cp_desc_ring)
4086 		return -ENOMEM;
4087 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
4088 				       GFP_KERNEL);
4089 	if (!cpr->cp_desc_mapping)
4090 		return -ENOMEM;
4091 	return 0;
4092 }
4093 
4094 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4095 {
4096 	int i;
4097 
4098 	if (!bp->bnapi)
4099 		return;
4100 	for (i = 0; i < bp->cp_nr_rings; i++) {
4101 		struct bnxt_napi *bnapi = bp->bnapi[i];
4102 
4103 		if (!bnapi)
4104 			continue;
4105 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4106 	}
4107 }
4108 
4109 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4110 {
4111 	int i, n = bp->cp_nr_pages;
4112 
4113 	for (i = 0; i < bp->cp_nr_rings; i++) {
4114 		struct bnxt_napi *bnapi = bp->bnapi[i];
4115 		int rc;
4116 
4117 		if (!bnapi)
4118 			continue;
4119 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4120 		if (rc)
4121 			return rc;
4122 	}
4123 	return 0;
4124 }
4125 
4126 static void bnxt_free_cp_rings(struct bnxt *bp)
4127 {
4128 	int i;
4129 
4130 	if (!bp->bnapi)
4131 		return;
4132 
4133 	for (i = 0; i < bp->cp_nr_rings; i++) {
4134 		struct bnxt_napi *bnapi = bp->bnapi[i];
4135 		struct bnxt_cp_ring_info *cpr;
4136 		struct bnxt_ring_struct *ring;
4137 		int j;
4138 
4139 		if (!bnapi)
4140 			continue;
4141 
4142 		cpr = &bnapi->cp_ring;
4143 		ring = &cpr->cp_ring_struct;
4144 
4145 		bnxt_free_ring(bp, &ring->ring_mem);
4146 
4147 		if (!cpr->cp_ring_arr)
4148 			continue;
4149 
4150 		for (j = 0; j < cpr->cp_ring_count; j++) {
4151 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4152 
4153 			ring = &cpr2->cp_ring_struct;
4154 			bnxt_free_ring(bp, &ring->ring_mem);
4155 			bnxt_free_cp_arrays(cpr2);
4156 		}
4157 		kfree(cpr->cp_ring_arr);
4158 		cpr->cp_ring_arr = NULL;
4159 		cpr->cp_ring_count = 0;
4160 	}
4161 }
4162 
4163 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4164 				  struct bnxt_cp_ring_info *cpr)
4165 {
4166 	struct bnxt_ring_mem_info *rmem;
4167 	struct bnxt_ring_struct *ring;
4168 	int rc;
4169 
4170 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4171 	if (rc) {
4172 		bnxt_free_cp_arrays(cpr);
4173 		return -ENOMEM;
4174 	}
4175 	ring = &cpr->cp_ring_struct;
4176 	rmem = &ring->ring_mem;
4177 	rmem->nr_pages = bp->cp_nr_pages;
4178 	rmem->page_size = HW_CMPD_RING_SIZE;
4179 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4180 	rmem->dma_arr = cpr->cp_desc_mapping;
4181 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4182 	rc = bnxt_alloc_ring(bp, rmem);
4183 	if (rc) {
4184 		bnxt_free_ring(bp, rmem);
4185 		bnxt_free_cp_arrays(cpr);
4186 	}
4187 	return rc;
4188 }
4189 
4190 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4191 {
4192 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4193 	int i, j, rc, ulp_msix;
4194 	int tcs = bp->num_tc;
4195 
4196 	if (!tcs)
4197 		tcs = 1;
4198 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4199 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4200 		struct bnxt_napi *bnapi = bp->bnapi[i];
4201 		struct bnxt_cp_ring_info *cpr, *cpr2;
4202 		struct bnxt_ring_struct *ring;
4203 		int cp_count = 0, k;
4204 		int rx = 0, tx = 0;
4205 
4206 		if (!bnapi)
4207 			continue;
4208 
4209 		cpr = &bnapi->cp_ring;
4210 		cpr->bnapi = bnapi;
4211 		ring = &cpr->cp_ring_struct;
4212 
4213 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4214 		if (rc)
4215 			return rc;
4216 
4217 		ring->map_idx = ulp_msix + i;
4218 
4219 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4220 			continue;
4221 
4222 		if (i < bp->rx_nr_rings) {
4223 			cp_count++;
4224 			rx = 1;
4225 		}
4226 		if (i < bp->tx_nr_rings_xdp) {
4227 			cp_count++;
4228 			tx = 1;
4229 		} else if ((sh && i < bp->tx_nr_rings) ||
4230 			 (!sh && i >= bp->rx_nr_rings)) {
4231 			cp_count += tcs;
4232 			tx = 1;
4233 		}
4234 
4235 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4236 					   GFP_KERNEL);
4237 		if (!cpr->cp_ring_arr)
4238 			return -ENOMEM;
4239 		cpr->cp_ring_count = cp_count;
4240 
4241 		for (k = 0; k < cp_count; k++) {
4242 			cpr2 = &cpr->cp_ring_arr[k];
4243 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4244 			if (rc)
4245 				return rc;
4246 			cpr2->bnapi = bnapi;
4247 			cpr2->sw_stats = cpr->sw_stats;
4248 			cpr2->cp_idx = k;
4249 			if (!k && rx) {
4250 				bp->rx_ring[i].rx_cpr = cpr2;
4251 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4252 			} else {
4253 				int n, tc = k - rx;
4254 
4255 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4256 				bp->tx_ring[n].tx_cpr = cpr2;
4257 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4258 			}
4259 		}
4260 		if (tx)
4261 			j++;
4262 	}
4263 	return 0;
4264 }
4265 
4266 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4267 				     struct bnxt_rx_ring_info *rxr)
4268 {
4269 	struct bnxt_ring_mem_info *rmem;
4270 	struct bnxt_ring_struct *ring;
4271 
4272 	ring = &rxr->rx_ring_struct;
4273 	rmem = &ring->ring_mem;
4274 	rmem->nr_pages = bp->rx_nr_pages;
4275 	rmem->page_size = HW_RXBD_RING_SIZE;
4276 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4277 	rmem->dma_arr = rxr->rx_desc_mapping;
4278 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4279 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4280 
4281 	ring = &rxr->rx_agg_ring_struct;
4282 	rmem = &ring->ring_mem;
4283 	rmem->nr_pages = bp->rx_agg_nr_pages;
4284 	rmem->page_size = HW_RXBD_RING_SIZE;
4285 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4286 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4287 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4288 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4289 }
4290 
4291 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4292 				      struct bnxt_rx_ring_info *rxr)
4293 {
4294 	struct bnxt_ring_mem_info *rmem;
4295 	struct bnxt_ring_struct *ring;
4296 	int i;
4297 
4298 	rxr->page_pool->p.napi = NULL;
4299 	rxr->page_pool = NULL;
4300 	rxr->head_pool->p.napi = NULL;
4301 	rxr->head_pool = NULL;
4302 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4303 
4304 	ring = &rxr->rx_ring_struct;
4305 	rmem = &ring->ring_mem;
4306 	rmem->pg_tbl = NULL;
4307 	rmem->pg_tbl_map = 0;
4308 	for (i = 0; i < rmem->nr_pages; i++) {
4309 		rmem->pg_arr[i] = NULL;
4310 		rmem->dma_arr[i] = 0;
4311 	}
4312 	*rmem->vmem = NULL;
4313 
4314 	ring = &rxr->rx_agg_ring_struct;
4315 	rmem = &ring->ring_mem;
4316 	rmem->pg_tbl = NULL;
4317 	rmem->pg_tbl_map = 0;
4318 	for (i = 0; i < rmem->nr_pages; i++) {
4319 		rmem->pg_arr[i] = NULL;
4320 		rmem->dma_arr[i] = 0;
4321 	}
4322 	*rmem->vmem = NULL;
4323 }
4324 
4325 static void bnxt_init_ring_struct(struct bnxt *bp)
4326 {
4327 	int i, j;
4328 
4329 	for (i = 0; i < bp->cp_nr_rings; i++) {
4330 		struct bnxt_napi *bnapi = bp->bnapi[i];
4331 		struct netdev_queue_config qcfg;
4332 		struct bnxt_ring_mem_info *rmem;
4333 		struct bnxt_cp_ring_info *cpr;
4334 		struct bnxt_rx_ring_info *rxr;
4335 		struct bnxt_tx_ring_info *txr;
4336 		struct bnxt_ring_struct *ring;
4337 
4338 		if (!bnapi)
4339 			continue;
4340 
4341 		cpr = &bnapi->cp_ring;
4342 		ring = &cpr->cp_ring_struct;
4343 		rmem = &ring->ring_mem;
4344 		rmem->nr_pages = bp->cp_nr_pages;
4345 		rmem->page_size = HW_CMPD_RING_SIZE;
4346 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4347 		rmem->dma_arr = cpr->cp_desc_mapping;
4348 		rmem->vmem_size = 0;
4349 
4350 		rxr = bnapi->rx_ring;
4351 		if (!rxr)
4352 			goto skip_rx;
4353 
4354 		netdev_queue_config(bp->dev, i, &qcfg);
4355 		rxr->rx_page_size = qcfg.rx_page_size;
4356 
4357 		ring = &rxr->rx_ring_struct;
4358 		rmem = &ring->ring_mem;
4359 		rmem->nr_pages = bp->rx_nr_pages;
4360 		rmem->page_size = HW_RXBD_RING_SIZE;
4361 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4362 		rmem->dma_arr = rxr->rx_desc_mapping;
4363 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4364 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4365 
4366 		ring = &rxr->rx_agg_ring_struct;
4367 		rmem = &ring->ring_mem;
4368 		rmem->nr_pages = bp->rx_agg_nr_pages;
4369 		rmem->page_size = HW_RXBD_RING_SIZE;
4370 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4371 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4372 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4373 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4374 
4375 skip_rx:
4376 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4377 			ring = &txr->tx_ring_struct;
4378 			rmem = &ring->ring_mem;
4379 			rmem->nr_pages = bp->tx_nr_pages;
4380 			rmem->page_size = HW_TXBD_RING_SIZE;
4381 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4382 			rmem->dma_arr = txr->tx_desc_mapping;
4383 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4384 			rmem->vmem = (void **)&txr->tx_buf_ring;
4385 		}
4386 	}
4387 }
4388 
4389 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4390 {
4391 	int i;
4392 	u32 prod;
4393 	struct rx_bd **rx_buf_ring;
4394 
4395 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4396 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4397 		int j;
4398 		struct rx_bd *rxbd;
4399 
4400 		rxbd = rx_buf_ring[i];
4401 		if (!rxbd)
4402 			continue;
4403 
4404 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4405 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4406 			rxbd->rx_bd_opaque = prod;
4407 		}
4408 	}
4409 }
4410 
4411 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4412 				       struct bnxt_rx_ring_info *rxr,
4413 				       int ring_nr)
4414 {
4415 	u32 prod;
4416 	int i;
4417 
4418 	prod = rxr->rx_prod;
4419 	for (i = 0; i < bp->rx_ring_size; i++) {
4420 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4421 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4422 				    ring_nr, i, bp->rx_ring_size);
4423 			break;
4424 		}
4425 		prod = NEXT_RX(prod);
4426 	}
4427 	rxr->rx_prod = prod;
4428 }
4429 
4430 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4431 					  struct bnxt_rx_ring_info *rxr,
4432 					  int ring_nr)
4433 {
4434 	int fill_level, i;
4435 	u32 prod;
4436 
4437 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4438 
4439 	prod = rxr->rx_agg_prod;
4440 	for (i = 0; i < fill_level; i++) {
4441 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4442 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4443 				    ring_nr, i, bp->rx_agg_ring_size);
4444 			break;
4445 		}
4446 		prod = NEXT_RX_AGG(prod);
4447 	}
4448 	rxr->rx_agg_prod = prod;
4449 }
4450 
4451 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4452 					struct bnxt_rx_ring_info *rxr)
4453 {
4454 	dma_addr_t mapping;
4455 	u8 *data;
4456 	int i;
4457 
4458 	for (i = 0; i < bp->max_tpa; i++) {
4459 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4460 					    GFP_KERNEL);
4461 		if (!data)
4462 			return -ENOMEM;
4463 
4464 		rxr->rx_tpa[i].data = data;
4465 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4466 		rxr->rx_tpa[i].mapping = mapping;
4467 	}
4468 
4469 	return 0;
4470 }
4471 
4472 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4473 {
4474 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4475 	int rc;
4476 
4477 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4478 
4479 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4480 		return 0;
4481 
4482 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4483 
4484 	if (rxr->rx_tpa) {
4485 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4486 		if (rc)
4487 			return rc;
4488 	}
4489 	return 0;
4490 }
4491 
4492 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4493 				       struct bnxt_rx_ring_info *rxr)
4494 {
4495 	struct bnxt_ring_struct *ring;
4496 	u32 type;
4497 
4498 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4499 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4500 
4501 	if (NET_IP_ALIGN == 2)
4502 		type |= RX_BD_FLAGS_SOP;
4503 
4504 	ring = &rxr->rx_ring_struct;
4505 	bnxt_init_rxbd_pages(ring, type);
4506 	ring->fw_ring_id = INVALID_HW_RING_ID;
4507 }
4508 
4509 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4510 					   struct bnxt_rx_ring_info *rxr)
4511 {
4512 	struct bnxt_ring_struct *ring;
4513 	u32 type;
4514 
4515 	ring = &rxr->rx_agg_ring_struct;
4516 	ring->fw_ring_id = INVALID_HW_RING_ID;
4517 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4518 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4519 			RX_BD_TYPE_RX_AGG_BD;
4520 
4521 		/* On P7, setting EOP will cause the chip to disable
4522 		 * Relaxed Ordering (RO) for TPA data.  Disable EOP for
4523 		 * potentially higher performance with RO.
4524 		 */
4525 		if (BNXT_CHIP_P5_AND_MINUS(bp) || !(bp->flags & BNXT_FLAG_TPA))
4526 			type |= RX_BD_FLAGS_AGG_EOP;
4527 
4528 		bnxt_init_rxbd_pages(ring, type);
4529 	}
4530 }
4531 
4532 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4533 {
4534 	struct bnxt_rx_ring_info *rxr;
4535 
4536 	rxr = &bp->rx_ring[ring_nr];
4537 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4538 
4539 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4540 			     &rxr->bnapi->napi);
4541 
4542 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4543 		bpf_prog_add(bp->xdp_prog, 1);
4544 		rxr->xdp_prog = bp->xdp_prog;
4545 	}
4546 
4547 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4548 
4549 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4550 }
4551 
4552 static void bnxt_init_cp_rings(struct bnxt *bp)
4553 {
4554 	int i, j;
4555 
4556 	for (i = 0; i < bp->cp_nr_rings; i++) {
4557 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4558 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4559 
4560 		ring->fw_ring_id = INVALID_HW_RING_ID;
4561 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4562 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4563 		if (!cpr->cp_ring_arr)
4564 			continue;
4565 		for (j = 0; j < cpr->cp_ring_count; j++) {
4566 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4567 
4568 			ring = &cpr2->cp_ring_struct;
4569 			ring->fw_ring_id = INVALID_HW_RING_ID;
4570 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4571 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4572 		}
4573 	}
4574 }
4575 
4576 static int bnxt_init_rx_rings(struct bnxt *bp)
4577 {
4578 	int i, rc = 0;
4579 
4580 	if (BNXT_RX_PAGE_MODE(bp)) {
4581 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4582 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4583 	} else {
4584 		bp->rx_offset = BNXT_RX_OFFSET;
4585 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4586 	}
4587 
4588 	for (i = 0; i < bp->rx_nr_rings; i++) {
4589 		rc = bnxt_init_one_rx_ring(bp, i);
4590 		if (rc)
4591 			break;
4592 	}
4593 
4594 	return rc;
4595 }
4596 
4597 static int bnxt_init_tx_rings(struct bnxt *bp)
4598 {
4599 	u16 i;
4600 
4601 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4602 				   BNXT_MIN_TX_DESC_CNT);
4603 
4604 	for (i = 0; i < bp->tx_nr_rings; i++) {
4605 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4606 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4607 
4608 		ring->fw_ring_id = INVALID_HW_RING_ID;
4609 
4610 		if (i >= bp->tx_nr_rings_xdp)
4611 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4612 					     NETDEV_QUEUE_TYPE_TX,
4613 					     &txr->bnapi->napi);
4614 	}
4615 
4616 	return 0;
4617 }
4618 
4619 static void bnxt_free_ring_grps(struct bnxt *bp)
4620 {
4621 	kfree(bp->grp_info);
4622 	bp->grp_info = NULL;
4623 }
4624 
4625 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4626 {
4627 	int i;
4628 
4629 	if (irq_re_init) {
4630 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4631 				       sizeof(struct bnxt_ring_grp_info),
4632 				       GFP_KERNEL);
4633 		if (!bp->grp_info)
4634 			return -ENOMEM;
4635 	}
4636 	for (i = 0; i < bp->cp_nr_rings; i++) {
4637 		if (irq_re_init)
4638 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4639 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4640 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4641 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4642 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4643 	}
4644 	return 0;
4645 }
4646 
4647 static void bnxt_free_vnics(struct bnxt *bp)
4648 {
4649 	kfree(bp->vnic_info);
4650 	bp->vnic_info = NULL;
4651 	bp->nr_vnics = 0;
4652 }
4653 
4654 static int bnxt_alloc_vnics(struct bnxt *bp)
4655 {
4656 	int num_vnics = 1;
4657 
4658 #ifdef CONFIG_RFS_ACCEL
4659 	if (bp->flags & BNXT_FLAG_RFS) {
4660 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4661 			num_vnics++;
4662 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4663 			num_vnics += bp->rx_nr_rings;
4664 	}
4665 #endif
4666 
4667 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4668 		num_vnics++;
4669 
4670 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4671 				GFP_KERNEL);
4672 	if (!bp->vnic_info)
4673 		return -ENOMEM;
4674 
4675 	bp->nr_vnics = num_vnics;
4676 	return 0;
4677 }
4678 
4679 static void bnxt_init_vnics(struct bnxt *bp)
4680 {
4681 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4682 	int i;
4683 
4684 	for (i = 0; i < bp->nr_vnics; i++) {
4685 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4686 		int j;
4687 
4688 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4689 		vnic->vnic_id = i;
4690 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4691 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4692 
4693 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4694 
4695 		if (bp->vnic_info[i].rss_hash_key) {
4696 			if (i == BNXT_VNIC_DEFAULT) {
4697 				u8 *key = (void *)vnic->rss_hash_key;
4698 				int k;
4699 
4700 				if (!bp->rss_hash_key_valid &&
4701 				    !bp->rss_hash_key_updated) {
4702 					get_random_bytes(bp->rss_hash_key,
4703 							 HW_HASH_KEY_SIZE);
4704 					bp->rss_hash_key_updated = true;
4705 				}
4706 
4707 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4708 				       HW_HASH_KEY_SIZE);
4709 
4710 				if (!bp->rss_hash_key_updated)
4711 					continue;
4712 
4713 				bp->rss_hash_key_updated = false;
4714 				bp->rss_hash_key_valid = true;
4715 
4716 				bp->toeplitz_prefix = 0;
4717 				for (k = 0; k < 8; k++) {
4718 					bp->toeplitz_prefix <<= 8;
4719 					bp->toeplitz_prefix |= key[k];
4720 				}
4721 			} else {
4722 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4723 				       HW_HASH_KEY_SIZE);
4724 			}
4725 		}
4726 	}
4727 }
4728 
4729 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4730 {
4731 	int pages;
4732 
4733 	pages = ring_size / desc_per_pg;
4734 
4735 	if (!pages)
4736 		return 1;
4737 
4738 	pages++;
4739 
4740 	while (pages & (pages - 1))
4741 		pages++;
4742 
4743 	return pages;
4744 }
4745 
4746 void bnxt_set_tpa_flags(struct bnxt *bp)
4747 {
4748 	bp->flags &= ~BNXT_FLAG_TPA;
4749 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4750 		return;
4751 	if (bp->dev->features & NETIF_F_LRO)
4752 		bp->flags |= BNXT_FLAG_LRO;
4753 	else if (bp->dev->features & NETIF_F_GRO_HW)
4754 		bp->flags |= BNXT_FLAG_GRO;
4755 }
4756 
4757 static void bnxt_init_ring_params(struct bnxt *bp)
4758 {
4759 	unsigned int rx_size;
4760 
4761 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4762 	/* Try to fit 4 chunks into a 4k page */
4763 	rx_size = SZ_1K -
4764 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4765 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4766 }
4767 
4768 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4769  * be set on entry.
4770  */
4771 void bnxt_set_ring_params(struct bnxt *bp)
4772 {
4773 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4774 	u32 agg_factor = 0, agg_ring_size = 0;
4775 
4776 	/* 8 for CRC and VLAN */
4777 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4778 
4779 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4780 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4781 
4782 	ring_size = bp->rx_ring_size;
4783 	bp->rx_agg_ring_size = 0;
4784 	bp->rx_agg_nr_pages = 0;
4785 
4786 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4787 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4788 
4789 	bp->flags &= ~BNXT_FLAG_JUMBO;
4790 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4791 		u32 jumbo_factor;
4792 
4793 		bp->flags |= BNXT_FLAG_JUMBO;
4794 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4795 		if (jumbo_factor > agg_factor)
4796 			agg_factor = jumbo_factor;
4797 	}
4798 	if (agg_factor) {
4799 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4800 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4801 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4802 				    bp->rx_ring_size, ring_size);
4803 			bp->rx_ring_size = ring_size;
4804 		}
4805 		agg_ring_size = ring_size * agg_factor;
4806 
4807 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4808 							RX_DESC_CNT);
4809 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4810 			u32 tmp = agg_ring_size;
4811 
4812 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4813 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4814 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4815 				    tmp, agg_ring_size);
4816 		}
4817 		bp->rx_agg_ring_size = agg_ring_size;
4818 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4819 
4820 		if (BNXT_RX_PAGE_MODE(bp)) {
4821 			rx_space = PAGE_SIZE;
4822 			rx_size = PAGE_SIZE -
4823 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4824 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4825 		} else {
4826 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4827 				       bp->rx_copybreak,
4828 				       bp->dev->cfg_pending->hds_thresh);
4829 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4830 			rx_space = rx_size + NET_SKB_PAD +
4831 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4832 		}
4833 	}
4834 
4835 	bp->rx_buf_use_size = rx_size;
4836 	bp->rx_buf_size = rx_space;
4837 
4838 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4839 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4840 
4841 	ring_size = bp->tx_ring_size;
4842 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4843 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4844 
4845 	max_rx_cmpl = bp->rx_ring_size;
4846 	/* MAX TPA needs to be added because TPA_START completions are
4847 	 * immediately recycled, so the TPA completions are not bound by
4848 	 * the RX ring size.
4849 	 */
4850 	if (bp->flags & BNXT_FLAG_TPA)
4851 		max_rx_cmpl += bp->max_tpa;
4852 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4853 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4854 	bp->cp_ring_size = ring_size;
4855 
4856 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4857 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4858 		bp->cp_nr_pages = MAX_CP_PAGES;
4859 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4860 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4861 			    ring_size, bp->cp_ring_size);
4862 	}
4863 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4864 	bp->cp_ring_mask = bp->cp_bit - 1;
4865 }
4866 
4867 /* Changing allocation mode of RX rings.
4868  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4869  */
4870 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4871 {
4872 	struct net_device *dev = bp->dev;
4873 
4874 	if (page_mode) {
4875 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4876 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4877 
4878 		if (bp->xdp_prog->aux->xdp_has_frags)
4879 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4880 		else
4881 			dev->max_mtu =
4882 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4883 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4884 			bp->flags |= BNXT_FLAG_JUMBO;
4885 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4886 		} else {
4887 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4888 			bp->rx_skb_func = bnxt_rx_page_skb;
4889 		}
4890 		bp->rx_dir = DMA_BIDIRECTIONAL;
4891 	} else {
4892 		dev->max_mtu = bp->max_mtu;
4893 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4894 		bp->rx_dir = DMA_FROM_DEVICE;
4895 		bp->rx_skb_func = bnxt_rx_skb;
4896 	}
4897 }
4898 
4899 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4900 {
4901 	__bnxt_set_rx_skb_mode(bp, page_mode);
4902 
4903 	if (!page_mode) {
4904 		int rx, tx;
4905 
4906 		bnxt_get_max_rings(bp, &rx, &tx, true);
4907 		if (rx > 1) {
4908 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4909 			bp->dev->hw_features |= NETIF_F_LRO;
4910 		}
4911 	}
4912 
4913 	/* Update LRO and GRO_HW availability */
4914 	netdev_update_features(bp->dev);
4915 }
4916 
4917 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4918 {
4919 	int i;
4920 	struct bnxt_vnic_info *vnic;
4921 	struct pci_dev *pdev = bp->pdev;
4922 
4923 	if (!bp->vnic_info)
4924 		return;
4925 
4926 	for (i = 0; i < bp->nr_vnics; i++) {
4927 		vnic = &bp->vnic_info[i];
4928 
4929 		kfree(vnic->fw_grp_ids);
4930 		vnic->fw_grp_ids = NULL;
4931 
4932 		kfree(vnic->uc_list);
4933 		vnic->uc_list = NULL;
4934 
4935 		if (vnic->mc_list) {
4936 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4937 					  vnic->mc_list, vnic->mc_list_mapping);
4938 			vnic->mc_list = NULL;
4939 		}
4940 
4941 		if (vnic->rss_table) {
4942 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4943 					  vnic->rss_table,
4944 					  vnic->rss_table_dma_addr);
4945 			vnic->rss_table = NULL;
4946 		}
4947 
4948 		vnic->rss_hash_key = NULL;
4949 		vnic->flags = 0;
4950 	}
4951 }
4952 
4953 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4954 {
4955 	int i, rc = 0, size;
4956 	struct bnxt_vnic_info *vnic;
4957 	struct pci_dev *pdev = bp->pdev;
4958 	int max_rings;
4959 
4960 	for (i = 0; i < bp->nr_vnics; i++) {
4961 		vnic = &bp->vnic_info[i];
4962 
4963 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4964 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4965 
4966 			if (mem_size > 0) {
4967 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4968 				if (!vnic->uc_list) {
4969 					rc = -ENOMEM;
4970 					goto out;
4971 				}
4972 			}
4973 		}
4974 
4975 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4976 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4977 			vnic->mc_list =
4978 				dma_alloc_coherent(&pdev->dev,
4979 						   vnic->mc_list_size,
4980 						   &vnic->mc_list_mapping,
4981 						   GFP_KERNEL);
4982 			if (!vnic->mc_list) {
4983 				rc = -ENOMEM;
4984 				goto out;
4985 			}
4986 		}
4987 
4988 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4989 			goto vnic_skip_grps;
4990 
4991 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4992 			max_rings = bp->rx_nr_rings;
4993 		else
4994 			max_rings = 1;
4995 
4996 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4997 		if (!vnic->fw_grp_ids) {
4998 			rc = -ENOMEM;
4999 			goto out;
5000 		}
5001 vnic_skip_grps:
5002 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
5003 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
5004 			continue;
5005 
5006 		/* Allocate rss table and hash key */
5007 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5008 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5009 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5010 
5011 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5012 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5013 						     vnic->rss_table_size,
5014 						     &vnic->rss_table_dma_addr,
5015 						     GFP_KERNEL);
5016 		if (!vnic->rss_table) {
5017 			rc = -ENOMEM;
5018 			goto out;
5019 		}
5020 
5021 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5022 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5023 	}
5024 	return 0;
5025 
5026 out:
5027 	return rc;
5028 }
5029 
5030 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5031 {
5032 	struct bnxt_hwrm_wait_token *token;
5033 
5034 	dma_pool_destroy(bp->hwrm_dma_pool);
5035 	bp->hwrm_dma_pool = NULL;
5036 
5037 	rcu_read_lock();
5038 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5039 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5040 	rcu_read_unlock();
5041 }
5042 
5043 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5044 {
5045 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5046 					    BNXT_HWRM_DMA_SIZE,
5047 					    BNXT_HWRM_DMA_ALIGN, 0);
5048 	if (!bp->hwrm_dma_pool)
5049 		return -ENOMEM;
5050 
5051 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5052 
5053 	return 0;
5054 }
5055 
5056 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5057 {
5058 	kfree(stats->hw_masks);
5059 	stats->hw_masks = NULL;
5060 	kfree(stats->sw_stats);
5061 	stats->sw_stats = NULL;
5062 	if (stats->hw_stats) {
5063 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5064 				  stats->hw_stats_map);
5065 		stats->hw_stats = NULL;
5066 	}
5067 }
5068 
5069 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5070 				bool alloc_masks)
5071 {
5072 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5073 					     &stats->hw_stats_map, GFP_KERNEL);
5074 	if (!stats->hw_stats)
5075 		return -ENOMEM;
5076 
5077 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5078 	if (!stats->sw_stats)
5079 		goto stats_mem_err;
5080 
5081 	if (alloc_masks) {
5082 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5083 		if (!stats->hw_masks)
5084 			goto stats_mem_err;
5085 	}
5086 	return 0;
5087 
5088 stats_mem_err:
5089 	bnxt_free_stats_mem(bp, stats);
5090 	return -ENOMEM;
5091 }
5092 
5093 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5094 {
5095 	int i;
5096 
5097 	for (i = 0; i < count; i++)
5098 		mask_arr[i] = mask;
5099 }
5100 
5101 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5102 {
5103 	int i;
5104 
5105 	for (i = 0; i < count; i++)
5106 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5107 }
5108 
5109 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5110 				    struct bnxt_stats_mem *stats)
5111 {
5112 	struct hwrm_func_qstats_ext_output *resp;
5113 	struct hwrm_func_qstats_ext_input *req;
5114 	__le64 *hw_masks;
5115 	int rc;
5116 
5117 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5118 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5119 		return -EOPNOTSUPP;
5120 
5121 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5122 	if (rc)
5123 		return rc;
5124 
5125 	req->fid = cpu_to_le16(0xffff);
5126 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5127 
5128 	resp = hwrm_req_hold(bp, req);
5129 	rc = hwrm_req_send(bp, req);
5130 	if (!rc) {
5131 		hw_masks = &resp->rx_ucast_pkts;
5132 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5133 	}
5134 	hwrm_req_drop(bp, req);
5135 	return rc;
5136 }
5137 
5138 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5139 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5140 
5141 static void bnxt_init_stats(struct bnxt *bp)
5142 {
5143 	struct bnxt_napi *bnapi = bp->bnapi[0];
5144 	struct bnxt_cp_ring_info *cpr;
5145 	struct bnxt_stats_mem *stats;
5146 	__le64 *rx_stats, *tx_stats;
5147 	int rc, rx_count, tx_count;
5148 	u64 *rx_masks, *tx_masks;
5149 	u64 mask;
5150 	u8 flags;
5151 
5152 	cpr = &bnapi->cp_ring;
5153 	stats = &cpr->stats;
5154 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5155 	if (rc) {
5156 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5157 			mask = (1ULL << 48) - 1;
5158 		else
5159 			mask = -1ULL;
5160 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5161 	}
5162 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5163 		stats = &bp->port_stats;
5164 		rx_stats = stats->hw_stats;
5165 		rx_masks = stats->hw_masks;
5166 		rx_count = sizeof(struct rx_port_stats) / 8;
5167 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5168 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5169 		tx_count = sizeof(struct tx_port_stats) / 8;
5170 
5171 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5172 		rc = bnxt_hwrm_port_qstats(bp, flags);
5173 		if (rc) {
5174 			mask = (1ULL << 40) - 1;
5175 
5176 			bnxt_fill_masks(rx_masks, mask, rx_count);
5177 			bnxt_fill_masks(tx_masks, mask, tx_count);
5178 		} else {
5179 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5180 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5181 			bnxt_hwrm_port_qstats(bp, 0);
5182 		}
5183 	}
5184 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5185 		stats = &bp->rx_port_stats_ext;
5186 		rx_stats = stats->hw_stats;
5187 		rx_masks = stats->hw_masks;
5188 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5189 		stats = &bp->tx_port_stats_ext;
5190 		tx_stats = stats->hw_stats;
5191 		tx_masks = stats->hw_masks;
5192 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5193 
5194 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5195 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5196 		if (rc) {
5197 			mask = (1ULL << 40) - 1;
5198 
5199 			bnxt_fill_masks(rx_masks, mask, rx_count);
5200 			if (tx_stats)
5201 				bnxt_fill_masks(tx_masks, mask, tx_count);
5202 		} else {
5203 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5204 			if (tx_stats)
5205 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5206 						   tx_count);
5207 			bnxt_hwrm_port_qstats_ext(bp, 0);
5208 		}
5209 	}
5210 }
5211 
5212 static void bnxt_free_port_stats(struct bnxt *bp)
5213 {
5214 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5215 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5216 
5217 	bnxt_free_stats_mem(bp, &bp->port_stats);
5218 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5219 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5220 }
5221 
5222 static void bnxt_free_ring_stats(struct bnxt *bp)
5223 {
5224 	int i;
5225 
5226 	if (!bp->bnapi)
5227 		return;
5228 
5229 	for (i = 0; i < bp->cp_nr_rings; i++) {
5230 		struct bnxt_napi *bnapi = bp->bnapi[i];
5231 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5232 
5233 		bnxt_free_stats_mem(bp, &cpr->stats);
5234 
5235 		kfree(cpr->sw_stats);
5236 		cpr->sw_stats = NULL;
5237 	}
5238 }
5239 
5240 static int bnxt_alloc_stats(struct bnxt *bp)
5241 {
5242 	u32 size, i;
5243 	int rc;
5244 
5245 	size = bp->hw_ring_stats_size;
5246 
5247 	for (i = 0; i < bp->cp_nr_rings; i++) {
5248 		struct bnxt_napi *bnapi = bp->bnapi[i];
5249 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5250 
5251 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5252 		if (!cpr->sw_stats)
5253 			return -ENOMEM;
5254 
5255 		cpr->stats.len = size;
5256 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5257 		if (rc)
5258 			return rc;
5259 
5260 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5261 	}
5262 
5263 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5264 		return 0;
5265 
5266 	if (bp->port_stats.hw_stats)
5267 		goto alloc_ext_stats;
5268 
5269 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5270 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5271 	if (rc)
5272 		return rc;
5273 
5274 	bp->flags |= BNXT_FLAG_PORT_STATS;
5275 
5276 alloc_ext_stats:
5277 	/* Display extended statistics only if FW supports it */
5278 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5279 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5280 			return 0;
5281 
5282 	if (bp->rx_port_stats_ext.hw_stats)
5283 		goto alloc_tx_ext_stats;
5284 
5285 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5286 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5287 	/* Extended stats are optional */
5288 	if (rc)
5289 		return 0;
5290 
5291 alloc_tx_ext_stats:
5292 	if (bp->tx_port_stats_ext.hw_stats)
5293 		return 0;
5294 
5295 	if (bp->hwrm_spec_code >= 0x10902 ||
5296 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5297 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5298 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5299 		/* Extended stats are optional */
5300 		if (rc)
5301 			return 0;
5302 	}
5303 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5304 	return 0;
5305 }
5306 
5307 static void bnxt_clear_ring_indices(struct bnxt *bp)
5308 {
5309 	int i, j;
5310 
5311 	if (!bp->bnapi)
5312 		return;
5313 
5314 	for (i = 0; i < bp->cp_nr_rings; i++) {
5315 		struct bnxt_napi *bnapi = bp->bnapi[i];
5316 		struct bnxt_cp_ring_info *cpr;
5317 		struct bnxt_rx_ring_info *rxr;
5318 		struct bnxt_tx_ring_info *txr;
5319 
5320 		if (!bnapi)
5321 			continue;
5322 
5323 		cpr = &bnapi->cp_ring;
5324 		cpr->cp_raw_cons = 0;
5325 
5326 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5327 			txr->tx_prod = 0;
5328 			txr->tx_cons = 0;
5329 			txr->tx_hw_cons = 0;
5330 		}
5331 
5332 		rxr = bnapi->rx_ring;
5333 		if (rxr) {
5334 			rxr->rx_prod = 0;
5335 			rxr->rx_agg_prod = 0;
5336 			rxr->rx_sw_agg_prod = 0;
5337 			rxr->rx_next_cons = 0;
5338 		}
5339 		bnapi->events = 0;
5340 	}
5341 }
5342 
5343 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5344 {
5345 	u8 type = fltr->type, flags = fltr->flags;
5346 
5347 	INIT_LIST_HEAD(&fltr->list);
5348 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5349 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5350 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5351 }
5352 
5353 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5354 {
5355 	if (!list_empty(&fltr->list))
5356 		list_del_init(&fltr->list);
5357 }
5358 
5359 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5360 {
5361 	struct bnxt_filter_base *usr_fltr, *tmp;
5362 
5363 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5364 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5365 			continue;
5366 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5367 	}
5368 }
5369 
5370 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5371 {
5372 	hlist_del(&fltr->hash);
5373 	bnxt_del_one_usr_fltr(bp, fltr);
5374 	if (fltr->flags) {
5375 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5376 		bp->ntp_fltr_count--;
5377 	}
5378 	kfree(fltr);
5379 }
5380 
5381 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5382 {
5383 	int i;
5384 
5385 	netdev_assert_locked_or_invisible(bp->dev);
5386 
5387 	/* Under netdev instance lock and all our NAPIs have been disabled.
5388 	 * It's safe to delete the hash table.
5389 	 */
5390 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5391 		struct hlist_head *head;
5392 		struct hlist_node *tmp;
5393 		struct bnxt_ntuple_filter *fltr;
5394 
5395 		head = &bp->ntp_fltr_hash_tbl[i];
5396 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5397 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5398 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5399 				     !list_empty(&fltr->base.list)))
5400 				continue;
5401 			bnxt_del_fltr(bp, &fltr->base);
5402 		}
5403 	}
5404 	if (!all)
5405 		return;
5406 
5407 	bitmap_free(bp->ntp_fltr_bmap);
5408 	bp->ntp_fltr_bmap = NULL;
5409 	bp->ntp_fltr_count = 0;
5410 }
5411 
5412 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5413 {
5414 	int i, rc = 0;
5415 
5416 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5417 		return 0;
5418 
5419 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5420 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5421 
5422 	bp->ntp_fltr_count = 0;
5423 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5424 
5425 	if (!bp->ntp_fltr_bmap)
5426 		rc = -ENOMEM;
5427 
5428 	return rc;
5429 }
5430 
5431 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5432 {
5433 	int i;
5434 
5435 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5436 		struct hlist_head *head;
5437 		struct hlist_node *tmp;
5438 		struct bnxt_l2_filter *fltr;
5439 
5440 		head = &bp->l2_fltr_hash_tbl[i];
5441 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5442 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5443 				     !list_empty(&fltr->base.list)))
5444 				continue;
5445 			bnxt_del_fltr(bp, &fltr->base);
5446 		}
5447 	}
5448 }
5449 
5450 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5451 {
5452 	int i;
5453 
5454 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5455 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5456 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5457 }
5458 
5459 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5460 {
5461 	bnxt_free_vnic_attributes(bp);
5462 	bnxt_free_tx_rings(bp);
5463 	bnxt_free_rx_rings(bp);
5464 	bnxt_free_cp_rings(bp);
5465 	bnxt_free_all_cp_arrays(bp);
5466 	bnxt_free_ntp_fltrs(bp, false);
5467 	bnxt_free_l2_filters(bp, false);
5468 	if (irq_re_init) {
5469 		bnxt_free_ring_stats(bp);
5470 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5471 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5472 			bnxt_free_port_stats(bp);
5473 		bnxt_free_ring_grps(bp);
5474 		bnxt_free_vnics(bp);
5475 		kfree(bp->tx_ring_map);
5476 		bp->tx_ring_map = NULL;
5477 		kfree(bp->tx_ring);
5478 		bp->tx_ring = NULL;
5479 		kfree(bp->rx_ring);
5480 		bp->rx_ring = NULL;
5481 		kfree(bp->bnapi);
5482 		bp->bnapi = NULL;
5483 	} else {
5484 		bnxt_clear_ring_indices(bp);
5485 	}
5486 }
5487 
5488 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5489 {
5490 	int i, j, rc, size, arr_size;
5491 	void *bnapi;
5492 
5493 	if (irq_re_init) {
5494 		/* Allocate bnapi mem pointer array and mem block for
5495 		 * all queues
5496 		 */
5497 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5498 				bp->cp_nr_rings);
5499 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5500 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5501 		if (!bnapi)
5502 			return -ENOMEM;
5503 
5504 		bp->bnapi = bnapi;
5505 		bnapi += arr_size;
5506 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5507 			bp->bnapi[i] = bnapi;
5508 			bp->bnapi[i]->index = i;
5509 			bp->bnapi[i]->bp = bp;
5510 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5511 				struct bnxt_cp_ring_info *cpr =
5512 					&bp->bnapi[i]->cp_ring;
5513 
5514 				cpr->cp_ring_struct.ring_mem.flags =
5515 					BNXT_RMEM_RING_PTE_FLAG;
5516 			}
5517 		}
5518 
5519 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5520 				      sizeof(struct bnxt_rx_ring_info),
5521 				      GFP_KERNEL);
5522 		if (!bp->rx_ring)
5523 			return -ENOMEM;
5524 
5525 		for (i = 0; i < bp->rx_nr_rings; i++) {
5526 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5527 
5528 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5529 				rxr->rx_ring_struct.ring_mem.flags =
5530 					BNXT_RMEM_RING_PTE_FLAG;
5531 				rxr->rx_agg_ring_struct.ring_mem.flags =
5532 					BNXT_RMEM_RING_PTE_FLAG;
5533 			} else {
5534 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5535 			}
5536 			rxr->bnapi = bp->bnapi[i];
5537 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5538 		}
5539 
5540 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5541 				      sizeof(struct bnxt_tx_ring_info),
5542 				      GFP_KERNEL);
5543 		if (!bp->tx_ring)
5544 			return -ENOMEM;
5545 
5546 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5547 					  GFP_KERNEL);
5548 
5549 		if (!bp->tx_ring_map)
5550 			return -ENOMEM;
5551 
5552 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5553 			j = 0;
5554 		else
5555 			j = bp->rx_nr_rings;
5556 
5557 		for (i = 0; i < bp->tx_nr_rings; i++) {
5558 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5559 			struct bnxt_napi *bnapi2;
5560 
5561 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5562 				txr->tx_ring_struct.ring_mem.flags =
5563 					BNXT_RMEM_RING_PTE_FLAG;
5564 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5565 			if (i >= bp->tx_nr_rings_xdp) {
5566 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5567 
5568 				bnapi2 = bp->bnapi[k];
5569 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5570 				txr->tx_napi_idx =
5571 					BNXT_RING_TO_TC(bp, txr->txq_index);
5572 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5573 				bnapi2->tx_int = bnxt_tx_int;
5574 			} else {
5575 				bnapi2 = bp->bnapi[j];
5576 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5577 				bnapi2->tx_ring[0] = txr;
5578 				bnapi2->tx_int = bnxt_tx_int_xdp;
5579 				j++;
5580 			}
5581 			txr->bnapi = bnapi2;
5582 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5583 				txr->tx_cpr = &bnapi2->cp_ring;
5584 		}
5585 
5586 		rc = bnxt_alloc_stats(bp);
5587 		if (rc)
5588 			goto alloc_mem_err;
5589 		bnxt_init_stats(bp);
5590 
5591 		rc = bnxt_alloc_ntp_fltrs(bp);
5592 		if (rc)
5593 			goto alloc_mem_err;
5594 
5595 		rc = bnxt_alloc_vnics(bp);
5596 		if (rc)
5597 			goto alloc_mem_err;
5598 	}
5599 
5600 	rc = bnxt_alloc_all_cp_arrays(bp);
5601 	if (rc)
5602 		goto alloc_mem_err;
5603 
5604 	bnxt_init_ring_struct(bp);
5605 
5606 	rc = bnxt_alloc_rx_rings(bp);
5607 	if (rc)
5608 		goto alloc_mem_err;
5609 
5610 	rc = bnxt_alloc_tx_rings(bp);
5611 	if (rc)
5612 		goto alloc_mem_err;
5613 
5614 	rc = bnxt_alloc_cp_rings(bp);
5615 	if (rc)
5616 		goto alloc_mem_err;
5617 
5618 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5619 						  BNXT_VNIC_MCAST_FLAG |
5620 						  BNXT_VNIC_UCAST_FLAG;
5621 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5622 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5623 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5624 
5625 	rc = bnxt_alloc_vnic_attributes(bp);
5626 	if (rc)
5627 		goto alloc_mem_err;
5628 	return 0;
5629 
5630 alloc_mem_err:
5631 	bnxt_free_mem(bp, true);
5632 	return rc;
5633 }
5634 
5635 static void bnxt_disable_int(struct bnxt *bp)
5636 {
5637 	int i;
5638 
5639 	if (!bp->bnapi)
5640 		return;
5641 
5642 	for (i = 0; i < bp->cp_nr_rings; i++) {
5643 		struct bnxt_napi *bnapi = bp->bnapi[i];
5644 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5645 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5646 
5647 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5648 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5649 	}
5650 }
5651 
5652 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5653 {
5654 	struct bnxt_napi *bnapi = bp->bnapi[n];
5655 	struct bnxt_cp_ring_info *cpr;
5656 
5657 	cpr = &bnapi->cp_ring;
5658 	return cpr->cp_ring_struct.map_idx;
5659 }
5660 
5661 static void bnxt_disable_int_sync(struct bnxt *bp)
5662 {
5663 	int i;
5664 
5665 	if (!bp->irq_tbl)
5666 		return;
5667 
5668 	atomic_inc(&bp->intr_sem);
5669 
5670 	bnxt_disable_int(bp);
5671 	for (i = 0; i < bp->cp_nr_rings; i++) {
5672 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5673 
5674 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5675 	}
5676 }
5677 
5678 static void bnxt_enable_int(struct bnxt *bp)
5679 {
5680 	int i;
5681 
5682 	atomic_set(&bp->intr_sem, 0);
5683 	for (i = 0; i < bp->cp_nr_rings; i++) {
5684 		struct bnxt_napi *bnapi = bp->bnapi[i];
5685 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5686 
5687 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5688 	}
5689 }
5690 
5691 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5692 			    bool async_only)
5693 {
5694 	DECLARE_BITMAP(async_events_bmap, 256);
5695 	u32 *events = (u32 *)async_events_bmap;
5696 	struct hwrm_func_drv_rgtr_output *resp;
5697 	struct hwrm_func_drv_rgtr_input *req;
5698 	u32 flags;
5699 	int rc, i;
5700 
5701 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5702 	if (rc)
5703 		return rc;
5704 
5705 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5706 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5707 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5708 
5709 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5710 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5711 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5712 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5713 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5714 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5715 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5716 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5717 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5718 	req->flags = cpu_to_le32(flags);
5719 	req->ver_maj_8b = DRV_VER_MAJ;
5720 	req->ver_min_8b = DRV_VER_MIN;
5721 	req->ver_upd_8b = DRV_VER_UPD;
5722 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5723 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5724 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5725 
5726 	if (BNXT_PF(bp)) {
5727 		u32 data[8];
5728 		int i;
5729 
5730 		memset(data, 0, sizeof(data));
5731 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5732 			u16 cmd = bnxt_vf_req_snif[i];
5733 			unsigned int bit, idx;
5734 
5735 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5736 			    cmd == HWRM_PORT_PHY_QCFG)
5737 				continue;
5738 
5739 			idx = cmd / 32;
5740 			bit = cmd % 32;
5741 			data[idx] |= 1 << bit;
5742 		}
5743 
5744 		for (i = 0; i < 8; i++)
5745 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5746 
5747 		req->enables |=
5748 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5749 	}
5750 
5751 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5752 		req->flags |= cpu_to_le32(
5753 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5754 
5755 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5756 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5757 		u16 event_id = bnxt_async_events_arr[i];
5758 
5759 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5760 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5761 			continue;
5762 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5763 		    !bp->ptp_cfg)
5764 			continue;
5765 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5766 	}
5767 	if (bmap && bmap_size) {
5768 		for (i = 0; i < bmap_size; i++) {
5769 			if (test_bit(i, bmap))
5770 				__set_bit(i, async_events_bmap);
5771 		}
5772 	}
5773 	for (i = 0; i < 8; i++)
5774 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5775 
5776 	if (async_only)
5777 		req->enables =
5778 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5779 
5780 	resp = hwrm_req_hold(bp, req);
5781 	rc = hwrm_req_send(bp, req);
5782 	if (!rc) {
5783 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5784 		if (resp->flags &
5785 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5786 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5787 	}
5788 	hwrm_req_drop(bp, req);
5789 	return rc;
5790 }
5791 
5792 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5793 {
5794 	struct hwrm_func_drv_unrgtr_input *req;
5795 	int rc;
5796 
5797 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5798 		return 0;
5799 
5800 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5801 	if (rc)
5802 		return rc;
5803 	return hwrm_req_send(bp, req);
5804 }
5805 
5806 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5807 
5808 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5809 {
5810 	struct hwrm_tunnel_dst_port_free_input *req;
5811 	int rc;
5812 
5813 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5814 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5815 		return 0;
5816 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5817 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5818 		return 0;
5819 
5820 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5821 	if (rc)
5822 		return rc;
5823 
5824 	req->tunnel_type = tunnel_type;
5825 
5826 	switch (tunnel_type) {
5827 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5828 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5829 		bp->vxlan_port = 0;
5830 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5831 		break;
5832 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5833 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5834 		bp->nge_port = 0;
5835 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5836 		break;
5837 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5838 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5839 		bp->vxlan_gpe_port = 0;
5840 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5841 		break;
5842 	default:
5843 		break;
5844 	}
5845 
5846 	rc = hwrm_req_send(bp, req);
5847 	if (rc)
5848 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5849 			   rc);
5850 	if (bp->flags & BNXT_FLAG_TPA)
5851 		bnxt_set_tpa(bp, true);
5852 	return rc;
5853 }
5854 
5855 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5856 					   u8 tunnel_type)
5857 {
5858 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5859 	struct hwrm_tunnel_dst_port_alloc_input *req;
5860 	int rc;
5861 
5862 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5863 	if (rc)
5864 		return rc;
5865 
5866 	req->tunnel_type = tunnel_type;
5867 	req->tunnel_dst_port_val = port;
5868 
5869 	resp = hwrm_req_hold(bp, req);
5870 	rc = hwrm_req_send(bp, req);
5871 	if (rc) {
5872 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5873 			   rc);
5874 		goto err_out;
5875 	}
5876 
5877 	switch (tunnel_type) {
5878 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5879 		bp->vxlan_port = port;
5880 		bp->vxlan_fw_dst_port_id =
5881 			le16_to_cpu(resp->tunnel_dst_port_id);
5882 		break;
5883 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5884 		bp->nge_port = port;
5885 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5886 		break;
5887 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5888 		bp->vxlan_gpe_port = port;
5889 		bp->vxlan_gpe_fw_dst_port_id =
5890 			le16_to_cpu(resp->tunnel_dst_port_id);
5891 		break;
5892 	default:
5893 		break;
5894 	}
5895 	if (bp->flags & BNXT_FLAG_TPA)
5896 		bnxt_set_tpa(bp, true);
5897 
5898 err_out:
5899 	hwrm_req_drop(bp, req);
5900 	return rc;
5901 }
5902 
5903 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5904 {
5905 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5906 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5907 	int rc;
5908 
5909 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5910 	if (rc)
5911 		return rc;
5912 
5913 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5914 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5915 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5916 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5917 	}
5918 	req->mask = cpu_to_le32(vnic->rx_mask);
5919 	return hwrm_req_send_silent(bp, req);
5920 }
5921 
5922 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5923 {
5924 	if (!atomic_dec_and_test(&fltr->refcnt))
5925 		return;
5926 	spin_lock_bh(&bp->ntp_fltr_lock);
5927 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5928 		spin_unlock_bh(&bp->ntp_fltr_lock);
5929 		return;
5930 	}
5931 	hlist_del_rcu(&fltr->base.hash);
5932 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5933 	if (fltr->base.flags) {
5934 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5935 		bp->ntp_fltr_count--;
5936 	}
5937 	spin_unlock_bh(&bp->ntp_fltr_lock);
5938 	kfree_rcu(fltr, base.rcu);
5939 }
5940 
5941 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5942 						      struct bnxt_l2_key *key,
5943 						      u32 idx)
5944 {
5945 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5946 	struct bnxt_l2_filter *fltr;
5947 
5948 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5949 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5950 
5951 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5952 		    l2_key->vlan == key->vlan)
5953 			return fltr;
5954 	}
5955 	return NULL;
5956 }
5957 
5958 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5959 						    struct bnxt_l2_key *key,
5960 						    u32 idx)
5961 {
5962 	struct bnxt_l2_filter *fltr = NULL;
5963 
5964 	rcu_read_lock();
5965 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5966 	if (fltr)
5967 		atomic_inc(&fltr->refcnt);
5968 	rcu_read_unlock();
5969 	return fltr;
5970 }
5971 
5972 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5973 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5974 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5975 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5976 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5977 
5978 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5979 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5980 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5981 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5982 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5983 
5984 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5985 {
5986 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5987 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5988 			return sizeof(fkeys->addrs.v4addrs) +
5989 			       sizeof(fkeys->ports);
5990 
5991 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5992 			return sizeof(fkeys->addrs.v4addrs);
5993 	}
5994 
5995 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5996 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5997 			return sizeof(fkeys->addrs.v6addrs) +
5998 			       sizeof(fkeys->ports);
5999 
6000 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6001 			return sizeof(fkeys->addrs.v6addrs);
6002 	}
6003 
6004 	return 0;
6005 }
6006 
6007 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6008 			 const unsigned char *key)
6009 {
6010 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6011 	struct bnxt_ipv4_tuple tuple4;
6012 	struct bnxt_ipv6_tuple tuple6;
6013 	int i, j, len = 0;
6014 	u8 *four_tuple;
6015 
6016 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6017 	if (!len)
6018 		return 0;
6019 
6020 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6021 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6022 		tuple4.ports = fkeys->ports;
6023 		four_tuple = (unsigned char *)&tuple4;
6024 	} else {
6025 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6026 		tuple6.ports = fkeys->ports;
6027 		four_tuple = (unsigned char *)&tuple6;
6028 	}
6029 
6030 	for (i = 0, j = 8; i < len; i++, j++) {
6031 		u8 byte = four_tuple[i];
6032 		int bit;
6033 
6034 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6035 			if (byte & 0x80)
6036 				hash ^= prefix;
6037 		}
6038 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6039 	}
6040 
6041 	/* The valid part of the hash is in the upper 32 bits. */
6042 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6043 }
6044 
6045 #ifdef CONFIG_RFS_ACCEL
6046 static struct bnxt_l2_filter *
6047 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6048 {
6049 	struct bnxt_l2_filter *fltr;
6050 	u32 idx;
6051 
6052 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6053 	      BNXT_L2_FLTR_HASH_MASK;
6054 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6055 	return fltr;
6056 }
6057 #endif
6058 
6059 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6060 			       struct bnxt_l2_key *key, u32 idx)
6061 {
6062 	struct hlist_head *head;
6063 
6064 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6065 	fltr->l2_key.vlan = key->vlan;
6066 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6067 	if (fltr->base.flags) {
6068 		int bit_id;
6069 
6070 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6071 						 bp->max_fltr, 0);
6072 		if (bit_id < 0)
6073 			return -ENOMEM;
6074 		fltr->base.sw_id = (u16)bit_id;
6075 		bp->ntp_fltr_count++;
6076 	}
6077 	head = &bp->l2_fltr_hash_tbl[idx];
6078 	hlist_add_head_rcu(&fltr->base.hash, head);
6079 	bnxt_insert_usr_fltr(bp, &fltr->base);
6080 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6081 	atomic_set(&fltr->refcnt, 1);
6082 	return 0;
6083 }
6084 
6085 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6086 						   struct bnxt_l2_key *key,
6087 						   gfp_t gfp)
6088 {
6089 	struct bnxt_l2_filter *fltr;
6090 	u32 idx;
6091 	int rc;
6092 
6093 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6094 	      BNXT_L2_FLTR_HASH_MASK;
6095 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6096 	if (fltr)
6097 		return fltr;
6098 
6099 	fltr = kzalloc(sizeof(*fltr), gfp);
6100 	if (!fltr)
6101 		return ERR_PTR(-ENOMEM);
6102 	spin_lock_bh(&bp->ntp_fltr_lock);
6103 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6104 	spin_unlock_bh(&bp->ntp_fltr_lock);
6105 	if (rc) {
6106 		bnxt_del_l2_filter(bp, fltr);
6107 		fltr = ERR_PTR(rc);
6108 	}
6109 	return fltr;
6110 }
6111 
6112 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6113 						struct bnxt_l2_key *key,
6114 						u16 flags)
6115 {
6116 	struct bnxt_l2_filter *fltr;
6117 	u32 idx;
6118 	int rc;
6119 
6120 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6121 	      BNXT_L2_FLTR_HASH_MASK;
6122 	spin_lock_bh(&bp->ntp_fltr_lock);
6123 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6124 	if (fltr) {
6125 		fltr = ERR_PTR(-EEXIST);
6126 		goto l2_filter_exit;
6127 	}
6128 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
6129 	if (!fltr) {
6130 		fltr = ERR_PTR(-ENOMEM);
6131 		goto l2_filter_exit;
6132 	}
6133 	fltr->base.flags = flags;
6134 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6135 	if (rc) {
6136 		spin_unlock_bh(&bp->ntp_fltr_lock);
6137 		bnxt_del_l2_filter(bp, fltr);
6138 		return ERR_PTR(rc);
6139 	}
6140 
6141 l2_filter_exit:
6142 	spin_unlock_bh(&bp->ntp_fltr_lock);
6143 	return fltr;
6144 }
6145 
6146 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6147 {
6148 #ifdef CONFIG_BNXT_SRIOV
6149 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6150 
6151 	return vf->fw_fid;
6152 #else
6153 	return INVALID_HW_RING_ID;
6154 #endif
6155 }
6156 
6157 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6158 {
6159 	struct hwrm_cfa_l2_filter_free_input *req;
6160 	u16 target_id = 0xffff;
6161 	int rc;
6162 
6163 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6164 		struct bnxt_pf_info *pf = &bp->pf;
6165 
6166 		if (fltr->base.vf_idx >= pf->active_vfs)
6167 			return -EINVAL;
6168 
6169 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6170 		if (target_id == INVALID_HW_RING_ID)
6171 			return -EINVAL;
6172 	}
6173 
6174 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6175 	if (rc)
6176 		return rc;
6177 
6178 	req->target_id = cpu_to_le16(target_id);
6179 	req->l2_filter_id = fltr->base.filter_id;
6180 	return hwrm_req_send(bp, req);
6181 }
6182 
6183 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6184 {
6185 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6186 	struct hwrm_cfa_l2_filter_alloc_input *req;
6187 	u16 target_id = 0xffff;
6188 	int rc;
6189 
6190 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6191 		struct bnxt_pf_info *pf = &bp->pf;
6192 
6193 		if (fltr->base.vf_idx >= pf->active_vfs)
6194 			return -EINVAL;
6195 
6196 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6197 	}
6198 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6199 	if (rc)
6200 		return rc;
6201 
6202 	req->target_id = cpu_to_le16(target_id);
6203 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6204 
6205 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6206 		req->flags |=
6207 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6208 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6209 	req->enables =
6210 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6211 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6212 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6213 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6214 	eth_broadcast_addr(req->l2_addr_mask);
6215 
6216 	if (fltr->l2_key.vlan) {
6217 		req->enables |=
6218 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6219 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6220 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6221 		req->num_vlans = 1;
6222 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6223 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6224 	}
6225 
6226 	resp = hwrm_req_hold(bp, req);
6227 	rc = hwrm_req_send(bp, req);
6228 	if (!rc) {
6229 		fltr->base.filter_id = resp->l2_filter_id;
6230 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6231 	}
6232 	hwrm_req_drop(bp, req);
6233 	return rc;
6234 }
6235 
6236 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6237 				     struct bnxt_ntuple_filter *fltr)
6238 {
6239 	struct hwrm_cfa_ntuple_filter_free_input *req;
6240 	int rc;
6241 
6242 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6243 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6244 	if (rc)
6245 		return rc;
6246 
6247 	req->ntuple_filter_id = fltr->base.filter_id;
6248 	return hwrm_req_send(bp, req);
6249 }
6250 
6251 #define BNXT_NTP_FLTR_FLAGS					\
6252 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6253 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6254 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6255 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6256 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6257 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6258 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6259 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6260 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6261 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6262 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6263 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6264 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6265 
6266 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6267 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6268 
6269 void bnxt_fill_ipv6_mask(__be32 mask[4])
6270 {
6271 	int i;
6272 
6273 	for (i = 0; i < 4; i++)
6274 		mask[i] = cpu_to_be32(~0);
6275 }
6276 
6277 static void
6278 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6279 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6280 			  struct bnxt_ntuple_filter *fltr)
6281 {
6282 	u16 rxq = fltr->base.rxq;
6283 
6284 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6285 		struct ethtool_rxfh_context *ctx;
6286 		struct bnxt_rss_ctx *rss_ctx;
6287 		struct bnxt_vnic_info *vnic;
6288 
6289 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6290 			      fltr->base.fw_vnic_id);
6291 		if (ctx) {
6292 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6293 			vnic = &rss_ctx->vnic;
6294 
6295 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6296 		}
6297 		return;
6298 	}
6299 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6300 		struct bnxt_vnic_info *vnic;
6301 		u32 enables;
6302 
6303 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6304 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6305 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6306 		req->enables |= cpu_to_le32(enables);
6307 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6308 	} else {
6309 		u32 flags;
6310 
6311 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6312 		req->flags |= cpu_to_le32(flags);
6313 		req->dst_id = cpu_to_le16(rxq);
6314 	}
6315 }
6316 
6317 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6318 				      struct bnxt_ntuple_filter *fltr)
6319 {
6320 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6321 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6322 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6323 	struct flow_keys *keys = &fltr->fkeys;
6324 	struct bnxt_l2_filter *l2_fltr;
6325 	struct bnxt_vnic_info *vnic;
6326 	int rc;
6327 
6328 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6329 	if (rc)
6330 		return rc;
6331 
6332 	l2_fltr = fltr->l2_fltr;
6333 	req->l2_filter_id = l2_fltr->base.filter_id;
6334 
6335 	if (fltr->base.flags & BNXT_ACT_DROP) {
6336 		req->flags =
6337 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6338 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6339 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6340 	} else {
6341 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6342 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6343 	}
6344 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6345 
6346 	req->ethertype = htons(ETH_P_IP);
6347 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6348 	req->ip_protocol = keys->basic.ip_proto;
6349 
6350 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6351 		req->ethertype = htons(ETH_P_IPV6);
6352 		req->ip_addr_type =
6353 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6354 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6355 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6356 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6357 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6358 	} else {
6359 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6360 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6361 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6362 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6363 	}
6364 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6365 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6366 		req->tunnel_type =
6367 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6368 	}
6369 
6370 	req->src_port = keys->ports.src;
6371 	req->src_port_mask = masks->ports.src;
6372 	req->dst_port = keys->ports.dst;
6373 	req->dst_port_mask = masks->ports.dst;
6374 
6375 	resp = hwrm_req_hold(bp, req);
6376 	rc = hwrm_req_send(bp, req);
6377 	if (!rc)
6378 		fltr->base.filter_id = resp->ntuple_filter_id;
6379 	hwrm_req_drop(bp, req);
6380 	return rc;
6381 }
6382 
6383 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6384 				     const u8 *mac_addr)
6385 {
6386 	struct bnxt_l2_filter *fltr;
6387 	struct bnxt_l2_key key;
6388 	int rc;
6389 
6390 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6391 	key.vlan = 0;
6392 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6393 	if (IS_ERR(fltr))
6394 		return PTR_ERR(fltr);
6395 
6396 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6397 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6398 	if (rc)
6399 		bnxt_del_l2_filter(bp, fltr);
6400 	else
6401 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6402 	return rc;
6403 }
6404 
6405 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6406 {
6407 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6408 
6409 	/* Any associated ntuple filters will also be cleared by firmware. */
6410 	for (i = 0; i < num_of_vnics; i++) {
6411 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6412 
6413 		for (j = 0; j < vnic->uc_filter_count; j++) {
6414 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6415 
6416 			bnxt_hwrm_l2_filter_free(bp, fltr);
6417 			bnxt_del_l2_filter(bp, fltr);
6418 		}
6419 		vnic->uc_filter_count = 0;
6420 	}
6421 }
6422 
6423 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6424 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6425 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6426 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6427 
6428 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6429 					   struct hwrm_vnic_tpa_cfg_input *req)
6430 {
6431 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6432 
6433 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6434 		return;
6435 
6436 	if (bp->vxlan_port)
6437 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6438 	if (bp->vxlan_gpe_port)
6439 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6440 	if (bp->nge_port)
6441 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6442 
6443 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6444 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6445 }
6446 
6447 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6448 			   u32 tpa_flags)
6449 {
6450 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6451 	struct hwrm_vnic_tpa_cfg_input *req;
6452 	int rc;
6453 
6454 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6455 		return 0;
6456 
6457 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6458 	if (rc)
6459 		return rc;
6460 
6461 	if (tpa_flags) {
6462 		u16 mss = bp->dev->mtu - 40;
6463 		u32 nsegs, n, segs = 0, flags;
6464 
6465 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6466 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6467 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6468 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6469 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6470 		if (tpa_flags & BNXT_FLAG_GRO)
6471 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6472 
6473 		req->flags = cpu_to_le32(flags);
6474 
6475 		req->enables =
6476 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6477 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6478 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6479 
6480 		/* Number of segs are log2 units, and first packet is not
6481 		 * included as part of this units.
6482 		 */
6483 		if (mss <= BNXT_RX_PAGE_SIZE) {
6484 			n = BNXT_RX_PAGE_SIZE / mss;
6485 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6486 		} else {
6487 			n = mss / BNXT_RX_PAGE_SIZE;
6488 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6489 				n++;
6490 			nsegs = (MAX_SKB_FRAGS - n) / n;
6491 		}
6492 
6493 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6494 			segs = MAX_TPA_SEGS_P5;
6495 			max_aggs = bp->max_tpa;
6496 		} else {
6497 			segs = ilog2(nsegs);
6498 		}
6499 		req->max_agg_segs = cpu_to_le16(segs);
6500 		req->max_aggs = cpu_to_le16(max_aggs);
6501 
6502 		req->min_agg_len = cpu_to_le32(512);
6503 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6504 	}
6505 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6506 
6507 	return hwrm_req_send(bp, req);
6508 }
6509 
6510 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6511 {
6512 	struct bnxt_ring_grp_info *grp_info;
6513 
6514 	grp_info = &bp->grp_info[ring->grp_idx];
6515 	return grp_info->cp_fw_ring_id;
6516 }
6517 
6518 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6519 {
6520 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6521 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6522 	else
6523 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6524 }
6525 
6526 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6527 {
6528 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6529 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6530 	else
6531 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6532 }
6533 
6534 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6535 {
6536 	int entries;
6537 
6538 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6539 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6540 	else
6541 		entries = HW_HASH_INDEX_SIZE;
6542 
6543 	bp->rss_indir_tbl_entries = entries;
6544 	bp->rss_indir_tbl =
6545 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6546 	if (!bp->rss_indir_tbl)
6547 		return -ENOMEM;
6548 
6549 	return 0;
6550 }
6551 
6552 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6553 				 struct ethtool_rxfh_context *rss_ctx)
6554 {
6555 	u16 max_rings, max_entries, pad, i;
6556 	u32 *rss_indir_tbl;
6557 
6558 	if (!bp->rx_nr_rings)
6559 		return;
6560 
6561 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6562 		max_rings = bp->rx_nr_rings - 1;
6563 	else
6564 		max_rings = bp->rx_nr_rings;
6565 
6566 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6567 	if (rss_ctx)
6568 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6569 	else
6570 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6571 
6572 	for (i = 0; i < max_entries; i++)
6573 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6574 
6575 	pad = bp->rss_indir_tbl_entries - max_entries;
6576 	if (pad)
6577 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6578 }
6579 
6580 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6581 {
6582 	u32 i, tbl_size, max_ring = 0;
6583 
6584 	if (!bp->rss_indir_tbl)
6585 		return 0;
6586 
6587 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6588 	for (i = 0; i < tbl_size; i++)
6589 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6590 	return max_ring;
6591 }
6592 
6593 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6594 {
6595 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6596 		if (!rx_rings)
6597 			return 0;
6598 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6599 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6600 
6601 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6602 					       BNXT_RSS_TABLE_ENTRIES_P5);
6603 	}
6604 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6605 		return 2;
6606 	return 1;
6607 }
6608 
6609 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6610 {
6611 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6612 	u16 i, j;
6613 
6614 	/* Fill the RSS indirection table with ring group ids */
6615 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6616 		if (!no_rss)
6617 			j = bp->rss_indir_tbl[i];
6618 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6619 	}
6620 }
6621 
6622 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6623 				    struct bnxt_vnic_info *vnic)
6624 {
6625 	__le16 *ring_tbl = vnic->rss_table;
6626 	struct bnxt_rx_ring_info *rxr;
6627 	u16 tbl_size, i;
6628 
6629 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6630 
6631 	for (i = 0; i < tbl_size; i++) {
6632 		u16 ring_id, j;
6633 
6634 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6635 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6636 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6637 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6638 		else
6639 			j = bp->rss_indir_tbl[i];
6640 		rxr = &bp->rx_ring[j];
6641 
6642 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6643 		*ring_tbl++ = cpu_to_le16(ring_id);
6644 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6645 		*ring_tbl++ = cpu_to_le16(ring_id);
6646 	}
6647 }
6648 
6649 static void
6650 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6651 			 struct bnxt_vnic_info *vnic)
6652 {
6653 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6654 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6655 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6656 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6657 	} else {
6658 		bnxt_fill_hw_rss_tbl(bp, vnic);
6659 	}
6660 
6661 	if (bp->rss_hash_delta) {
6662 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6663 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6664 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6665 		else
6666 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6667 	} else {
6668 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6669 	}
6670 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6671 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6672 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6673 }
6674 
6675 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6676 				  bool set_rss)
6677 {
6678 	struct hwrm_vnic_rss_cfg_input *req;
6679 	int rc;
6680 
6681 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6682 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6683 		return 0;
6684 
6685 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6686 	if (rc)
6687 		return rc;
6688 
6689 	if (set_rss)
6690 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6691 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6692 	return hwrm_req_send(bp, req);
6693 }
6694 
6695 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6696 				     struct bnxt_vnic_info *vnic, bool set_rss)
6697 {
6698 	struct hwrm_vnic_rss_cfg_input *req;
6699 	dma_addr_t ring_tbl_map;
6700 	u32 i, nr_ctxs;
6701 	int rc;
6702 
6703 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6704 	if (rc)
6705 		return rc;
6706 
6707 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6708 	if (!set_rss)
6709 		return hwrm_req_send(bp, req);
6710 
6711 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6712 	ring_tbl_map = vnic->rss_table_dma_addr;
6713 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6714 
6715 	hwrm_req_hold(bp, req);
6716 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6717 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6718 		req->ring_table_pair_index = i;
6719 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6720 		rc = hwrm_req_send(bp, req);
6721 		if (rc)
6722 			goto exit;
6723 	}
6724 
6725 exit:
6726 	hwrm_req_drop(bp, req);
6727 	return rc;
6728 }
6729 
6730 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6731 {
6732 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6733 	struct hwrm_vnic_rss_qcfg_output *resp;
6734 	struct hwrm_vnic_rss_qcfg_input *req;
6735 
6736 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6737 		return;
6738 
6739 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6740 	/* all contexts configured to same hash_type, zero always exists */
6741 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6742 	resp = hwrm_req_hold(bp, req);
6743 	if (!hwrm_req_send(bp, req)) {
6744 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6745 		bp->rss_hash_delta = 0;
6746 	}
6747 	hwrm_req_drop(bp, req);
6748 }
6749 
6750 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6751 {
6752 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6753 	struct hwrm_vnic_plcmodes_cfg_input *req;
6754 	int rc;
6755 
6756 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6757 	if (rc)
6758 		return rc;
6759 
6760 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6761 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6762 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6763 
6764 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6765 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6766 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6767 		req->enables |=
6768 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6769 		req->hds_threshold = cpu_to_le16(hds_thresh);
6770 	}
6771 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6772 	return hwrm_req_send(bp, req);
6773 }
6774 
6775 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6776 					struct bnxt_vnic_info *vnic,
6777 					u16 ctx_idx)
6778 {
6779 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6780 
6781 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6782 		return;
6783 
6784 	req->rss_cos_lb_ctx_id =
6785 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6786 
6787 	hwrm_req_send(bp, req);
6788 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6789 }
6790 
6791 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6792 {
6793 	int i, j;
6794 
6795 	for (i = 0; i < bp->nr_vnics; i++) {
6796 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6797 
6798 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6799 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6800 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6801 		}
6802 	}
6803 	bp->rsscos_nr_ctxs = 0;
6804 }
6805 
6806 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6807 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6808 {
6809 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6810 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6811 	int rc;
6812 
6813 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6814 	if (rc)
6815 		return rc;
6816 
6817 	resp = hwrm_req_hold(bp, req);
6818 	rc = hwrm_req_send(bp, req);
6819 	if (!rc)
6820 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6821 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6822 	hwrm_req_drop(bp, req);
6823 
6824 	return rc;
6825 }
6826 
6827 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6828 {
6829 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6830 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6831 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6832 }
6833 
6834 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6835 {
6836 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6837 	struct hwrm_vnic_cfg_input *req;
6838 	unsigned int ring = 0, grp_idx;
6839 	u16 def_vlan = 0;
6840 	int rc;
6841 
6842 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6843 	if (rc)
6844 		return rc;
6845 
6846 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6847 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6848 
6849 		req->default_rx_ring_id =
6850 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6851 		req->default_cmpl_ring_id =
6852 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6853 		req->enables =
6854 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6855 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6856 		goto vnic_mru;
6857 	}
6858 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6859 	/* Only RSS support for now TBD: COS & LB */
6860 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6861 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6862 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6863 					   VNIC_CFG_REQ_ENABLES_MRU);
6864 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6865 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6866 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6867 					   VNIC_CFG_REQ_ENABLES_MRU);
6868 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6869 	} else {
6870 		req->rss_rule = cpu_to_le16(0xffff);
6871 	}
6872 
6873 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6874 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6875 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6876 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6877 	} else {
6878 		req->cos_rule = cpu_to_le16(0xffff);
6879 	}
6880 
6881 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6882 		ring = 0;
6883 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6884 		ring = vnic->vnic_id - 1;
6885 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6886 		ring = bp->rx_nr_rings - 1;
6887 
6888 	grp_idx = bp->rx_ring[ring].bnapi->index;
6889 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6890 	req->lb_rule = cpu_to_le16(0xffff);
6891 vnic_mru:
6892 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
6893 	req->mru = cpu_to_le16(vnic->mru);
6894 
6895 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6896 #ifdef CONFIG_BNXT_SRIOV
6897 	if (BNXT_VF(bp))
6898 		def_vlan = bp->vf.vlan;
6899 #endif
6900 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6901 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6902 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6903 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6904 
6905 	return hwrm_req_send(bp, req);
6906 }
6907 
6908 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6909 				    struct bnxt_vnic_info *vnic)
6910 {
6911 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6912 		struct hwrm_vnic_free_input *req;
6913 
6914 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6915 			return;
6916 
6917 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6918 
6919 		hwrm_req_send(bp, req);
6920 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6921 	}
6922 }
6923 
6924 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6925 {
6926 	u16 i;
6927 
6928 	for (i = 0; i < bp->nr_vnics; i++)
6929 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6930 }
6931 
6932 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6933 			 unsigned int start_rx_ring_idx,
6934 			 unsigned int nr_rings)
6935 {
6936 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6937 	struct hwrm_vnic_alloc_output *resp;
6938 	struct hwrm_vnic_alloc_input *req;
6939 	int rc;
6940 
6941 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6942 	if (rc)
6943 		return rc;
6944 
6945 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6946 		goto vnic_no_ring_grps;
6947 
6948 	/* map ring groups to this vnic */
6949 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6950 		grp_idx = bp->rx_ring[i].bnapi->index;
6951 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6952 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6953 				   j, nr_rings);
6954 			break;
6955 		}
6956 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6957 	}
6958 
6959 vnic_no_ring_grps:
6960 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6961 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6962 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6963 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6964 
6965 	resp = hwrm_req_hold(bp, req);
6966 	rc = hwrm_req_send(bp, req);
6967 	if (!rc)
6968 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6969 	hwrm_req_drop(bp, req);
6970 	return rc;
6971 }
6972 
6973 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6974 {
6975 	struct hwrm_vnic_qcaps_output *resp;
6976 	struct hwrm_vnic_qcaps_input *req;
6977 	int rc;
6978 
6979 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6980 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6981 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6982 	if (bp->hwrm_spec_code < 0x10600)
6983 		return 0;
6984 
6985 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6986 	if (rc)
6987 		return rc;
6988 
6989 	resp = hwrm_req_hold(bp, req);
6990 	rc = hwrm_req_send(bp, req);
6991 	if (!rc) {
6992 		u32 flags = le32_to_cpu(resp->flags);
6993 
6994 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6995 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6996 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6997 		if (flags &
6998 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6999 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7000 
7001 		/* Older P5 fw before EXT_HW_STATS support did not set
7002 		 * VLAN_STRIP_CAP properly.
7003 		 */
7004 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7005 		    (BNXT_CHIP_P5(bp) &&
7006 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7007 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7008 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7009 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7010 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7011 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7012 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7013 		if (bp->max_tpa_v2) {
7014 			if (BNXT_CHIP_P5(bp))
7015 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7016 			else
7017 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7018 		}
7019 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7020 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7021 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7022 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7023 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7024 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7025 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7026 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7027 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7028 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7029 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7030 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7031 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7032 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7033 	}
7034 	hwrm_req_drop(bp, req);
7035 	return rc;
7036 }
7037 
7038 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7039 {
7040 	struct hwrm_ring_grp_alloc_output *resp;
7041 	struct hwrm_ring_grp_alloc_input *req;
7042 	int rc;
7043 	u16 i;
7044 
7045 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7046 		return 0;
7047 
7048 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7049 	if (rc)
7050 		return rc;
7051 
7052 	resp = hwrm_req_hold(bp, req);
7053 	for (i = 0; i < bp->rx_nr_rings; i++) {
7054 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7055 
7056 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7057 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7058 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7059 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7060 
7061 		rc = hwrm_req_send(bp, req);
7062 
7063 		if (rc)
7064 			break;
7065 
7066 		bp->grp_info[grp_idx].fw_grp_id =
7067 			le32_to_cpu(resp->ring_group_id);
7068 	}
7069 	hwrm_req_drop(bp, req);
7070 	return rc;
7071 }
7072 
7073 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7074 {
7075 	struct hwrm_ring_grp_free_input *req;
7076 	u16 i;
7077 
7078 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7079 		return;
7080 
7081 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7082 		return;
7083 
7084 	hwrm_req_hold(bp, req);
7085 	for (i = 0; i < bp->cp_nr_rings; i++) {
7086 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7087 			continue;
7088 		req->ring_group_id =
7089 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7090 
7091 		hwrm_req_send(bp, req);
7092 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7093 	}
7094 	hwrm_req_drop(bp, req);
7095 }
7096 
7097 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7098 				       struct hwrm_ring_alloc_input *req,
7099 				       struct bnxt_rx_ring_info *rxr,
7100 				       struct bnxt_ring_struct *ring)
7101 {
7102 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7103 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7104 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7105 
7106 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7107 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7108 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7109 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7110 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7111 	} else {
7112 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7113 		if (NET_IP_ALIGN == 2)
7114 			req->flags =
7115 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7116 	}
7117 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7118 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7119 	req->enables |= cpu_to_le32(enables);
7120 }
7121 
7122 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7123 				    struct bnxt_rx_ring_info *rxr,
7124 				    struct bnxt_ring_struct *ring,
7125 				    u32 ring_type, u32 map_index)
7126 {
7127 	struct hwrm_ring_alloc_output *resp;
7128 	struct hwrm_ring_alloc_input *req;
7129 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7130 	struct bnxt_ring_grp_info *grp_info;
7131 	int rc, err = 0;
7132 	u16 ring_id;
7133 
7134 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7135 	if (rc)
7136 		goto exit;
7137 
7138 	req->enables = 0;
7139 	if (rmem->nr_pages > 1) {
7140 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7141 		/* Page size is in log2 units */
7142 		req->page_size = BNXT_PAGE_SHIFT;
7143 		req->page_tbl_depth = 1;
7144 	} else {
7145 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7146 	}
7147 	req->fbo = 0;
7148 	/* Association of ring index with doorbell index and MSIX number */
7149 	req->logical_id = cpu_to_le16(map_index);
7150 
7151 	switch (ring_type) {
7152 	case HWRM_RING_ALLOC_TX: {
7153 		struct bnxt_tx_ring_info *txr;
7154 		u16 flags = 0;
7155 
7156 		txr = container_of(ring, struct bnxt_tx_ring_info,
7157 				   tx_ring_struct);
7158 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7159 		/* Association of transmit ring with completion ring */
7160 		grp_info = &bp->grp_info[ring->grp_idx];
7161 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7162 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7163 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7164 		req->queue_id = cpu_to_le16(ring->queue_id);
7165 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7166 			req->cmpl_coal_cnt =
7167 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7168 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7169 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7170 		req->flags = cpu_to_le16(flags);
7171 		break;
7172 	}
7173 	case HWRM_RING_ALLOC_RX:
7174 	case HWRM_RING_ALLOC_AGG:
7175 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7176 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7177 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7178 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7179 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7180 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7181 						   rxr, ring);
7182 		break;
7183 	case HWRM_RING_ALLOC_CMPL:
7184 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7185 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7186 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7187 			/* Association of cp ring with nq */
7188 			grp_info = &bp->grp_info[map_index];
7189 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7190 			req->cq_handle = cpu_to_le64(ring->handle);
7191 			req->enables |= cpu_to_le32(
7192 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7193 		} else {
7194 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7195 		}
7196 		break;
7197 	case HWRM_RING_ALLOC_NQ:
7198 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7199 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7200 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7201 		break;
7202 	default:
7203 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7204 			   ring_type);
7205 		return -EINVAL;
7206 	}
7207 
7208 	resp = hwrm_req_hold(bp, req);
7209 	rc = hwrm_req_send(bp, req);
7210 	err = le16_to_cpu(resp->error_code);
7211 	ring_id = le16_to_cpu(resp->ring_id);
7212 	hwrm_req_drop(bp, req);
7213 
7214 exit:
7215 	if (rc || err) {
7216 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7217 			   ring_type, rc, err);
7218 		return -EIO;
7219 	}
7220 	ring->fw_ring_id = ring_id;
7221 	return rc;
7222 }
7223 
7224 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7225 {
7226 	int rc;
7227 
7228 	if (BNXT_PF(bp)) {
7229 		struct hwrm_func_cfg_input *req;
7230 
7231 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7232 		if (rc)
7233 			return rc;
7234 
7235 		req->fid = cpu_to_le16(0xffff);
7236 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7237 		req->async_event_cr = cpu_to_le16(idx);
7238 		return hwrm_req_send(bp, req);
7239 	} else {
7240 		struct hwrm_func_vf_cfg_input *req;
7241 
7242 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7243 		if (rc)
7244 			return rc;
7245 
7246 		req->enables =
7247 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7248 		req->async_event_cr = cpu_to_le16(idx);
7249 		return hwrm_req_send(bp, req);
7250 	}
7251 }
7252 
7253 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7254 			     u32 ring_type)
7255 {
7256 	switch (ring_type) {
7257 	case HWRM_RING_ALLOC_TX:
7258 		db->db_ring_mask = bp->tx_ring_mask;
7259 		break;
7260 	case HWRM_RING_ALLOC_RX:
7261 		db->db_ring_mask = bp->rx_ring_mask;
7262 		break;
7263 	case HWRM_RING_ALLOC_AGG:
7264 		db->db_ring_mask = bp->rx_agg_ring_mask;
7265 		break;
7266 	case HWRM_RING_ALLOC_CMPL:
7267 	case HWRM_RING_ALLOC_NQ:
7268 		db->db_ring_mask = bp->cp_ring_mask;
7269 		break;
7270 	}
7271 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7272 		db->db_epoch_mask = db->db_ring_mask + 1;
7273 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7274 	}
7275 }
7276 
7277 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7278 			u32 map_idx, u32 xid)
7279 {
7280 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7281 		switch (ring_type) {
7282 		case HWRM_RING_ALLOC_TX:
7283 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7284 			break;
7285 		case HWRM_RING_ALLOC_RX:
7286 		case HWRM_RING_ALLOC_AGG:
7287 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7288 			break;
7289 		case HWRM_RING_ALLOC_CMPL:
7290 			db->db_key64 = DBR_PATH_L2;
7291 			break;
7292 		case HWRM_RING_ALLOC_NQ:
7293 			db->db_key64 = DBR_PATH_L2;
7294 			break;
7295 		}
7296 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7297 
7298 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7299 			db->db_key64 |= DBR_VALID;
7300 
7301 		db->doorbell = bp->bar1 + bp->db_offset;
7302 	} else {
7303 		db->doorbell = bp->bar1 + map_idx * 0x80;
7304 		switch (ring_type) {
7305 		case HWRM_RING_ALLOC_TX:
7306 			db->db_key32 = DB_KEY_TX;
7307 			break;
7308 		case HWRM_RING_ALLOC_RX:
7309 		case HWRM_RING_ALLOC_AGG:
7310 			db->db_key32 = DB_KEY_RX;
7311 			break;
7312 		case HWRM_RING_ALLOC_CMPL:
7313 			db->db_key32 = DB_KEY_CP;
7314 			break;
7315 		}
7316 	}
7317 	bnxt_set_db_mask(bp, db, ring_type);
7318 }
7319 
7320 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7321 				   struct bnxt_rx_ring_info *rxr)
7322 {
7323 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7324 	struct bnxt_napi *bnapi = rxr->bnapi;
7325 	u32 type = HWRM_RING_ALLOC_RX;
7326 	u32 map_idx = bnapi->index;
7327 	int rc;
7328 
7329 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7330 	if (rc)
7331 		return rc;
7332 
7333 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7334 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7335 
7336 	return 0;
7337 }
7338 
7339 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7340 				       struct bnxt_rx_ring_info *rxr)
7341 {
7342 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7343 	u32 type = HWRM_RING_ALLOC_AGG;
7344 	u32 grp_idx = ring->grp_idx;
7345 	u32 map_idx;
7346 	int rc;
7347 
7348 	map_idx = grp_idx + bp->rx_nr_rings;
7349 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7350 	if (rc)
7351 		return rc;
7352 
7353 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7354 		    ring->fw_ring_id);
7355 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7356 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7357 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7358 
7359 	return 0;
7360 }
7361 
7362 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7363 				      struct bnxt_cp_ring_info *cpr)
7364 {
7365 	const u32 type = HWRM_RING_ALLOC_CMPL;
7366 	struct bnxt_napi *bnapi = cpr->bnapi;
7367 	struct bnxt_ring_struct *ring;
7368 	u32 map_idx = bnapi->index;
7369 	int rc;
7370 
7371 	ring = &cpr->cp_ring_struct;
7372 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7373 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7374 	if (rc)
7375 		return rc;
7376 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7377 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7378 	return 0;
7379 }
7380 
7381 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7382 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7383 {
7384 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7385 	const u32 type = HWRM_RING_ALLOC_TX;
7386 	int rc;
7387 
7388 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7389 	if (rc)
7390 		return rc;
7391 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7392 	return 0;
7393 }
7394 
7395 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7396 {
7397 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7398 	int i, rc = 0;
7399 	u32 type;
7400 
7401 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7402 		type = HWRM_RING_ALLOC_NQ;
7403 	else
7404 		type = HWRM_RING_ALLOC_CMPL;
7405 	for (i = 0; i < bp->cp_nr_rings; i++) {
7406 		struct bnxt_napi *bnapi = bp->bnapi[i];
7407 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7408 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7409 		u32 map_idx = ring->map_idx;
7410 		unsigned int vector;
7411 
7412 		vector = bp->irq_tbl[map_idx].vector;
7413 		disable_irq_nosync(vector);
7414 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7415 		if (rc) {
7416 			enable_irq(vector);
7417 			goto err_out;
7418 		}
7419 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7420 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7421 		enable_irq(vector);
7422 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7423 
7424 		if (!i) {
7425 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7426 			if (rc)
7427 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7428 		}
7429 	}
7430 
7431 	for (i = 0; i < bp->tx_nr_rings; i++) {
7432 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7433 
7434 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7435 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7436 			if (rc)
7437 				goto err_out;
7438 		}
7439 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7440 		if (rc)
7441 			goto err_out;
7442 	}
7443 
7444 	for (i = 0; i < bp->rx_nr_rings; i++) {
7445 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7446 
7447 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7448 		if (rc)
7449 			goto err_out;
7450 		/* If we have agg rings, post agg buffers first. */
7451 		if (!agg_rings)
7452 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7453 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7454 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7455 			if (rc)
7456 				goto err_out;
7457 		}
7458 	}
7459 
7460 	if (agg_rings) {
7461 		for (i = 0; i < bp->rx_nr_rings; i++) {
7462 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7463 			if (rc)
7464 				goto err_out;
7465 		}
7466 	}
7467 err_out:
7468 	return rc;
7469 }
7470 
7471 static void bnxt_cancel_dim(struct bnxt *bp)
7472 {
7473 	int i;
7474 
7475 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7476 	 * if NAPI is enabled.
7477 	 */
7478 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7479 		return;
7480 
7481 	/* Make sure NAPI sees that the VNIC is disabled */
7482 	synchronize_net();
7483 	for (i = 0; i < bp->rx_nr_rings; i++) {
7484 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7485 		struct bnxt_napi *bnapi = rxr->bnapi;
7486 
7487 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7488 	}
7489 }
7490 
7491 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7492 				   struct bnxt_ring_struct *ring,
7493 				   u32 ring_type, int cmpl_ring_id)
7494 {
7495 	struct hwrm_ring_free_output *resp;
7496 	struct hwrm_ring_free_input *req;
7497 	u16 error_code = 0;
7498 	int rc;
7499 
7500 	if (BNXT_NO_FW_ACCESS(bp))
7501 		return 0;
7502 
7503 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7504 	if (rc)
7505 		goto exit;
7506 
7507 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7508 	req->ring_type = ring_type;
7509 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7510 
7511 	resp = hwrm_req_hold(bp, req);
7512 	rc = hwrm_req_send(bp, req);
7513 	error_code = le16_to_cpu(resp->error_code);
7514 	hwrm_req_drop(bp, req);
7515 exit:
7516 	if (rc || error_code) {
7517 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7518 			   ring_type, rc, error_code);
7519 		return -EIO;
7520 	}
7521 	return 0;
7522 }
7523 
7524 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7525 				   struct bnxt_tx_ring_info *txr,
7526 				   bool close_path)
7527 {
7528 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7529 	u32 cmpl_ring_id;
7530 
7531 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7532 		return;
7533 
7534 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7535 		       INVALID_HW_RING_ID;
7536 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7537 				cmpl_ring_id);
7538 	ring->fw_ring_id = INVALID_HW_RING_ID;
7539 }
7540 
7541 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7542 				   struct bnxt_rx_ring_info *rxr,
7543 				   bool close_path)
7544 {
7545 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7546 	u32 grp_idx = rxr->bnapi->index;
7547 	u32 cmpl_ring_id;
7548 
7549 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7550 		return;
7551 
7552 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7553 	hwrm_ring_free_send_msg(bp, ring,
7554 				RING_FREE_REQ_RING_TYPE_RX,
7555 				close_path ? cmpl_ring_id :
7556 				INVALID_HW_RING_ID);
7557 	ring->fw_ring_id = INVALID_HW_RING_ID;
7558 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7559 }
7560 
7561 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7562 				       struct bnxt_rx_ring_info *rxr,
7563 				       bool close_path)
7564 {
7565 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7566 	u32 grp_idx = rxr->bnapi->index;
7567 	u32 type, cmpl_ring_id;
7568 
7569 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7570 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7571 	else
7572 		type = RING_FREE_REQ_RING_TYPE_RX;
7573 
7574 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7575 		return;
7576 
7577 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7578 	hwrm_ring_free_send_msg(bp, ring, type,
7579 				close_path ? cmpl_ring_id :
7580 				INVALID_HW_RING_ID);
7581 	ring->fw_ring_id = INVALID_HW_RING_ID;
7582 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7583 }
7584 
7585 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7586 				   struct bnxt_cp_ring_info *cpr)
7587 {
7588 	struct bnxt_ring_struct *ring;
7589 
7590 	ring = &cpr->cp_ring_struct;
7591 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7592 		return;
7593 
7594 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7595 				INVALID_HW_RING_ID);
7596 	ring->fw_ring_id = INVALID_HW_RING_ID;
7597 }
7598 
7599 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7600 {
7601 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7602 	int i, size = ring->ring_mem.page_size;
7603 
7604 	cpr->cp_raw_cons = 0;
7605 	cpr->toggle = 0;
7606 
7607 	for (i = 0; i < bp->cp_nr_pages; i++)
7608 		if (cpr->cp_desc_ring[i])
7609 			memset(cpr->cp_desc_ring[i], 0, size);
7610 }
7611 
7612 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7613 {
7614 	u32 type;
7615 	int i;
7616 
7617 	if (!bp->bnapi)
7618 		return;
7619 
7620 	for (i = 0; i < bp->tx_nr_rings; i++)
7621 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7622 
7623 	bnxt_cancel_dim(bp);
7624 	for (i = 0; i < bp->rx_nr_rings; i++) {
7625 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7626 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7627 	}
7628 
7629 	/* The completion rings are about to be freed.  After that the
7630 	 * IRQ doorbell will not work anymore.  So we need to disable
7631 	 * IRQ here.
7632 	 */
7633 	bnxt_disable_int_sync(bp);
7634 
7635 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7636 		type = RING_FREE_REQ_RING_TYPE_NQ;
7637 	else
7638 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7639 	for (i = 0; i < bp->cp_nr_rings; i++) {
7640 		struct bnxt_napi *bnapi = bp->bnapi[i];
7641 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7642 		struct bnxt_ring_struct *ring;
7643 		int j;
7644 
7645 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7646 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7647 
7648 		ring = &cpr->cp_ring_struct;
7649 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7650 			hwrm_ring_free_send_msg(bp, ring, type,
7651 						INVALID_HW_RING_ID);
7652 			ring->fw_ring_id = INVALID_HW_RING_ID;
7653 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7654 		}
7655 	}
7656 }
7657 
7658 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7659 			     bool shared);
7660 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7661 			   bool shared);
7662 
7663 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7664 {
7665 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7666 	struct hwrm_func_qcfg_output *resp;
7667 	struct hwrm_func_qcfg_input *req;
7668 	int rc;
7669 
7670 	if (bp->hwrm_spec_code < 0x10601)
7671 		return 0;
7672 
7673 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7674 	if (rc)
7675 		return rc;
7676 
7677 	req->fid = cpu_to_le16(0xffff);
7678 	resp = hwrm_req_hold(bp, req);
7679 	rc = hwrm_req_send(bp, req);
7680 	if (rc) {
7681 		hwrm_req_drop(bp, req);
7682 		return rc;
7683 	}
7684 
7685 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7686 	if (BNXT_NEW_RM(bp)) {
7687 		u16 cp, stats;
7688 
7689 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7690 		hw_resc->resv_hw_ring_grps =
7691 			le32_to_cpu(resp->alloc_hw_ring_grps);
7692 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7693 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7694 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7695 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7696 		hw_resc->resv_irqs = cp;
7697 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7698 			int rx = hw_resc->resv_rx_rings;
7699 			int tx = hw_resc->resv_tx_rings;
7700 
7701 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7702 				rx >>= 1;
7703 			if (cp < (rx + tx)) {
7704 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7705 				if (rc)
7706 					goto get_rings_exit;
7707 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7708 					rx <<= 1;
7709 				hw_resc->resv_rx_rings = rx;
7710 				hw_resc->resv_tx_rings = tx;
7711 			}
7712 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7713 			hw_resc->resv_hw_ring_grps = rx;
7714 		}
7715 		hw_resc->resv_cp_rings = cp;
7716 		hw_resc->resv_stat_ctxs = stats;
7717 	}
7718 get_rings_exit:
7719 	hwrm_req_drop(bp, req);
7720 	return rc;
7721 }
7722 
7723 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7724 {
7725 	struct hwrm_func_qcfg_output *resp;
7726 	struct hwrm_func_qcfg_input *req;
7727 	int rc;
7728 
7729 	if (bp->hwrm_spec_code < 0x10601)
7730 		return 0;
7731 
7732 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7733 	if (rc)
7734 		return rc;
7735 
7736 	req->fid = cpu_to_le16(fid);
7737 	resp = hwrm_req_hold(bp, req);
7738 	rc = hwrm_req_send(bp, req);
7739 	if (!rc)
7740 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7741 
7742 	hwrm_req_drop(bp, req);
7743 	return rc;
7744 }
7745 
7746 static bool bnxt_rfs_supported(struct bnxt *bp);
7747 
7748 static struct hwrm_func_cfg_input *
7749 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7750 {
7751 	struct hwrm_func_cfg_input *req;
7752 	u32 enables = 0;
7753 
7754 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7755 		return NULL;
7756 
7757 	req->fid = cpu_to_le16(0xffff);
7758 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7759 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7760 	if (BNXT_NEW_RM(bp)) {
7761 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7762 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7763 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7764 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7765 			enables |= hwr->cp_p5 ?
7766 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7767 		} else {
7768 			enables |= hwr->cp ?
7769 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7770 			enables |= hwr->grp ?
7771 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7772 		}
7773 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7774 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7775 					  0;
7776 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7777 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7778 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7779 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7780 			req->num_msix = cpu_to_le16(hwr->cp);
7781 		} else {
7782 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7783 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7784 		}
7785 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7786 		req->num_vnics = cpu_to_le16(hwr->vnic);
7787 	}
7788 	req->enables = cpu_to_le32(enables);
7789 	return req;
7790 }
7791 
7792 static struct hwrm_func_vf_cfg_input *
7793 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7794 {
7795 	struct hwrm_func_vf_cfg_input *req;
7796 	u32 enables = 0;
7797 
7798 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7799 		return NULL;
7800 
7801 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7802 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7803 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7804 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7805 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7806 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7807 		enables |= hwr->cp_p5 ?
7808 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7809 	} else {
7810 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7811 		enables |= hwr->grp ?
7812 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7813 	}
7814 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7815 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7816 
7817 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7818 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7819 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7820 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7821 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7822 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7823 	} else {
7824 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7825 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7826 	}
7827 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7828 	req->num_vnics = cpu_to_le16(hwr->vnic);
7829 
7830 	req->enables = cpu_to_le32(enables);
7831 	return req;
7832 }
7833 
7834 static int
7835 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7836 {
7837 	struct hwrm_func_cfg_input *req;
7838 	int rc;
7839 
7840 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7841 	if (!req)
7842 		return -ENOMEM;
7843 
7844 	if (!req->enables) {
7845 		hwrm_req_drop(bp, req);
7846 		return 0;
7847 	}
7848 
7849 	rc = hwrm_req_send(bp, req);
7850 	if (rc)
7851 		return rc;
7852 
7853 	if (bp->hwrm_spec_code < 0x10601)
7854 		bp->hw_resc.resv_tx_rings = hwr->tx;
7855 
7856 	return bnxt_hwrm_get_rings(bp);
7857 }
7858 
7859 static int
7860 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7861 {
7862 	struct hwrm_func_vf_cfg_input *req;
7863 	int rc;
7864 
7865 	if (!BNXT_NEW_RM(bp)) {
7866 		bp->hw_resc.resv_tx_rings = hwr->tx;
7867 		return 0;
7868 	}
7869 
7870 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7871 	if (!req)
7872 		return -ENOMEM;
7873 
7874 	rc = hwrm_req_send(bp, req);
7875 	if (rc)
7876 		return rc;
7877 
7878 	return bnxt_hwrm_get_rings(bp);
7879 }
7880 
7881 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7882 {
7883 	if (BNXT_PF(bp))
7884 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7885 	else
7886 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7887 }
7888 
7889 int bnxt_nq_rings_in_use(struct bnxt *bp)
7890 {
7891 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7892 }
7893 
7894 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7895 {
7896 	int cp;
7897 
7898 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7899 		return bnxt_nq_rings_in_use(bp);
7900 
7901 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7902 	return cp;
7903 }
7904 
7905 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7906 {
7907 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7908 }
7909 
7910 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7911 {
7912 	if (!hwr->grp)
7913 		return 0;
7914 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7915 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7916 
7917 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7918 			rss_ctx *= hwr->vnic;
7919 		return rss_ctx;
7920 	}
7921 	if (BNXT_VF(bp))
7922 		return BNXT_VF_MAX_RSS_CTX;
7923 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7924 		return hwr->grp + 1;
7925 	return 1;
7926 }
7927 
7928 /* Check if a default RSS map needs to be setup.  This function is only
7929  * used on older firmware that does not require reserving RX rings.
7930  */
7931 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7932 {
7933 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7934 
7935 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7936 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7937 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7938 		if (!netif_is_rxfh_configured(bp->dev))
7939 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7940 	}
7941 }
7942 
7943 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7944 {
7945 	if (bp->flags & BNXT_FLAG_RFS) {
7946 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7947 			return 2 + bp->num_rss_ctx;
7948 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7949 			return rx_rings + 1;
7950 	}
7951 	return 1;
7952 }
7953 
7954 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7955 {
7956 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7957 	int cp = bnxt_cp_rings_in_use(bp);
7958 	int nq = bnxt_nq_rings_in_use(bp);
7959 	int rx = bp->rx_nr_rings, stat;
7960 	int vnic, grp = rx;
7961 
7962 	/* Old firmware does not need RX ring reservations but we still
7963 	 * need to setup a default RSS map when needed.  With new firmware
7964 	 * we go through RX ring reservations first and then set up the
7965 	 * RSS map for the successfully reserved RX rings when needed.
7966 	 */
7967 	if (!BNXT_NEW_RM(bp))
7968 		bnxt_check_rss_tbl_no_rmgr(bp);
7969 
7970 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7971 	    bp->hwrm_spec_code >= 0x10601)
7972 		return true;
7973 
7974 	if (!BNXT_NEW_RM(bp))
7975 		return false;
7976 
7977 	vnic = bnxt_get_total_vnics(bp, rx);
7978 
7979 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7980 		rx <<= 1;
7981 	stat = bnxt_get_func_stat_ctxs(bp);
7982 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7983 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7984 	    (hw_resc->resv_hw_ring_grps != grp &&
7985 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7986 		return true;
7987 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7988 	    hw_resc->resv_irqs != nq)
7989 		return true;
7990 	return false;
7991 }
7992 
7993 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7994 {
7995 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7996 
7997 	hwr->tx = hw_resc->resv_tx_rings;
7998 	if (BNXT_NEW_RM(bp)) {
7999 		hwr->rx = hw_resc->resv_rx_rings;
8000 		hwr->cp = hw_resc->resv_irqs;
8001 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8002 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8003 		hwr->grp = hw_resc->resv_hw_ring_grps;
8004 		hwr->vnic = hw_resc->resv_vnics;
8005 		hwr->stat = hw_resc->resv_stat_ctxs;
8006 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8007 	}
8008 }
8009 
8010 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8011 {
8012 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8013 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8014 }
8015 
8016 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8017 
8018 static int __bnxt_reserve_rings(struct bnxt *bp)
8019 {
8020 	struct bnxt_hw_rings hwr = {0};
8021 	int rx_rings, old_rx_rings, rc;
8022 	int cp = bp->cp_nr_rings;
8023 	int ulp_msix = 0;
8024 	bool sh = false;
8025 	int tx_cp;
8026 
8027 	if (!bnxt_need_reserve_rings(bp))
8028 		return 0;
8029 
8030 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
8031 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8032 		if (!ulp_msix)
8033 			bnxt_set_ulp_stat_ctxs(bp, 0);
8034 
8035 		if (ulp_msix > bp->ulp_num_msix_want)
8036 			ulp_msix = bp->ulp_num_msix_want;
8037 		hwr.cp = cp + ulp_msix;
8038 	} else {
8039 		hwr.cp = bnxt_nq_rings_in_use(bp);
8040 	}
8041 
8042 	hwr.tx = bp->tx_nr_rings;
8043 	hwr.rx = bp->rx_nr_rings;
8044 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8045 		sh = true;
8046 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8047 		hwr.cp_p5 = hwr.rx + hwr.tx;
8048 
8049 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8050 
8051 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8052 		hwr.rx <<= 1;
8053 	hwr.grp = bp->rx_nr_rings;
8054 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8055 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8056 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8057 
8058 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8059 	if (rc)
8060 		return rc;
8061 
8062 	bnxt_copy_reserved_rings(bp, &hwr);
8063 
8064 	rx_rings = hwr.rx;
8065 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8066 		if (hwr.rx >= 2) {
8067 			rx_rings = hwr.rx >> 1;
8068 		} else {
8069 			if (netif_running(bp->dev))
8070 				return -ENOMEM;
8071 
8072 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8073 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8074 			bp->dev->hw_features &= ~NETIF_F_LRO;
8075 			bp->dev->features &= ~NETIF_F_LRO;
8076 			bnxt_set_ring_params(bp);
8077 		}
8078 	}
8079 	rx_rings = min_t(int, rx_rings, hwr.grp);
8080 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8081 	if (bnxt_ulp_registered(bp->edev) &&
8082 	    hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8083 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8084 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8085 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8086 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8087 		hwr.rx = rx_rings << 1;
8088 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8089 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8090 	if (hwr.tx != bp->tx_nr_rings) {
8091 		netdev_warn(bp->dev,
8092 			    "Able to reserve only %d out of %d requested TX rings\n",
8093 			    hwr.tx, bp->tx_nr_rings);
8094 	}
8095 	bp->tx_nr_rings = hwr.tx;
8096 
8097 	/* If we cannot reserve all the RX rings, reset the RSS map only
8098 	 * if absolutely necessary
8099 	 */
8100 	if (rx_rings != bp->rx_nr_rings) {
8101 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8102 			    rx_rings, bp->rx_nr_rings);
8103 		if (netif_is_rxfh_configured(bp->dev) &&
8104 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8105 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8106 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8107 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8108 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8109 		}
8110 	}
8111 	bp->rx_nr_rings = rx_rings;
8112 	bp->cp_nr_rings = hwr.cp;
8113 
8114 	/* Fall back if we cannot reserve enough HW RSS contexts */
8115 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8116 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8117 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8118 
8119 	if (!bnxt_rings_ok(bp, &hwr))
8120 		return -ENOMEM;
8121 
8122 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8123 	    !netif_is_rxfh_configured(bp->dev))
8124 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8125 
8126 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8127 		int resv_msix, resv_ctx, ulp_ctxs;
8128 		struct bnxt_hw_resc *hw_resc;
8129 
8130 		hw_resc = &bp->hw_resc;
8131 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8132 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8133 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8134 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8135 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8136 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8137 	}
8138 
8139 	return rc;
8140 }
8141 
8142 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8143 {
8144 	struct hwrm_func_vf_cfg_input *req;
8145 	u32 flags;
8146 
8147 	if (!BNXT_NEW_RM(bp))
8148 		return 0;
8149 
8150 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8151 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8152 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8153 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8154 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8155 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8156 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8157 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8158 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8159 
8160 	req->flags = cpu_to_le32(flags);
8161 	return hwrm_req_send_silent(bp, req);
8162 }
8163 
8164 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8165 {
8166 	struct hwrm_func_cfg_input *req;
8167 	u32 flags;
8168 
8169 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8170 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8171 	if (BNXT_NEW_RM(bp)) {
8172 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8173 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8174 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8175 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8176 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8177 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8178 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8179 		else
8180 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8181 	}
8182 
8183 	req->flags = cpu_to_le32(flags);
8184 	return hwrm_req_send_silent(bp, req);
8185 }
8186 
8187 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8188 {
8189 	if (bp->hwrm_spec_code < 0x10801)
8190 		return 0;
8191 
8192 	if (BNXT_PF(bp))
8193 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8194 
8195 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8196 }
8197 
8198 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8199 {
8200 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8201 	struct hwrm_ring_aggint_qcaps_output *resp;
8202 	struct hwrm_ring_aggint_qcaps_input *req;
8203 	int rc;
8204 
8205 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8206 	coal_cap->num_cmpl_dma_aggr_max = 63;
8207 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8208 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8209 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8210 	coal_cap->int_lat_tmr_min_max = 65535;
8211 	coal_cap->int_lat_tmr_max_max = 65535;
8212 	coal_cap->num_cmpl_aggr_int_max = 65535;
8213 	coal_cap->timer_units = 80;
8214 
8215 	if (bp->hwrm_spec_code < 0x10902)
8216 		return;
8217 
8218 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8219 		return;
8220 
8221 	resp = hwrm_req_hold(bp, req);
8222 	rc = hwrm_req_send_silent(bp, req);
8223 	if (!rc) {
8224 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8225 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8226 		coal_cap->num_cmpl_dma_aggr_max =
8227 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8228 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8229 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8230 		coal_cap->cmpl_aggr_dma_tmr_max =
8231 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8232 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8233 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8234 		coal_cap->int_lat_tmr_min_max =
8235 			le16_to_cpu(resp->int_lat_tmr_min_max);
8236 		coal_cap->int_lat_tmr_max_max =
8237 			le16_to_cpu(resp->int_lat_tmr_max_max);
8238 		coal_cap->num_cmpl_aggr_int_max =
8239 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8240 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8241 	}
8242 	hwrm_req_drop(bp, req);
8243 }
8244 
8245 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8246 {
8247 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8248 
8249 	return usec * 1000 / coal_cap->timer_units;
8250 }
8251 
8252 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8253 	struct bnxt_coal *hw_coal,
8254 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8255 {
8256 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8257 	u16 val, tmr, max, flags = hw_coal->flags;
8258 	u32 cmpl_params = coal_cap->cmpl_params;
8259 
8260 	max = hw_coal->bufs_per_record * 128;
8261 	if (hw_coal->budget)
8262 		max = hw_coal->bufs_per_record * hw_coal->budget;
8263 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8264 
8265 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8266 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8267 
8268 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8269 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8270 
8271 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8272 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8273 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8274 
8275 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8276 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8277 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8278 
8279 	/* min timer set to 1/2 of interrupt timer */
8280 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8281 		val = tmr / 2;
8282 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8283 		req->int_lat_tmr_min = cpu_to_le16(val);
8284 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8285 	}
8286 
8287 	/* buf timer set to 1/4 of interrupt timer */
8288 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8289 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8290 
8291 	if (cmpl_params &
8292 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8293 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8294 		val = clamp_t(u16, tmr, 1,
8295 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8296 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8297 		req->enables |=
8298 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8299 	}
8300 
8301 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8302 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8303 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8304 	req->flags = cpu_to_le16(flags);
8305 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8306 }
8307 
8308 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8309 				   struct bnxt_coal *hw_coal)
8310 {
8311 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8312 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8313 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8314 	u32 nq_params = coal_cap->nq_params;
8315 	u16 tmr;
8316 	int rc;
8317 
8318 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8319 		return 0;
8320 
8321 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8322 	if (rc)
8323 		return rc;
8324 
8325 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8326 	req->flags =
8327 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8328 
8329 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8330 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8331 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8332 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8333 	return hwrm_req_send(bp, req);
8334 }
8335 
8336 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8337 {
8338 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8339 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8340 	struct bnxt_coal coal;
8341 	int rc;
8342 
8343 	/* Tick values in micro seconds.
8344 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8345 	 */
8346 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8347 
8348 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8349 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8350 
8351 	if (!bnapi->rx_ring)
8352 		return -ENODEV;
8353 
8354 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8355 	if (rc)
8356 		return rc;
8357 
8358 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8359 
8360 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8361 
8362 	return hwrm_req_send(bp, req_rx);
8363 }
8364 
8365 static int
8366 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8367 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8368 {
8369 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8370 
8371 	req->ring_id = cpu_to_le16(ring_id);
8372 	return hwrm_req_send(bp, req);
8373 }
8374 
8375 static int
8376 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8377 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8378 {
8379 	struct bnxt_tx_ring_info *txr;
8380 	int i, rc;
8381 
8382 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8383 		u16 ring_id;
8384 
8385 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8386 		req->ring_id = cpu_to_le16(ring_id);
8387 		rc = hwrm_req_send(bp, req);
8388 		if (rc)
8389 			return rc;
8390 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8391 			return 0;
8392 	}
8393 	return 0;
8394 }
8395 
8396 int bnxt_hwrm_set_coal(struct bnxt *bp)
8397 {
8398 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8399 	int i, rc;
8400 
8401 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8402 	if (rc)
8403 		return rc;
8404 
8405 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8406 	if (rc) {
8407 		hwrm_req_drop(bp, req_rx);
8408 		return rc;
8409 	}
8410 
8411 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8412 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8413 
8414 	hwrm_req_hold(bp, req_rx);
8415 	hwrm_req_hold(bp, req_tx);
8416 	for (i = 0; i < bp->cp_nr_rings; i++) {
8417 		struct bnxt_napi *bnapi = bp->bnapi[i];
8418 		struct bnxt_coal *hw_coal;
8419 
8420 		if (!bnapi->rx_ring)
8421 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8422 		else
8423 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8424 		if (rc)
8425 			break;
8426 
8427 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8428 			continue;
8429 
8430 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8431 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8432 			if (rc)
8433 				break;
8434 		}
8435 		if (bnapi->rx_ring)
8436 			hw_coal = &bp->rx_coal;
8437 		else
8438 			hw_coal = &bp->tx_coal;
8439 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8440 	}
8441 	hwrm_req_drop(bp, req_rx);
8442 	hwrm_req_drop(bp, req_tx);
8443 	return rc;
8444 }
8445 
8446 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8447 {
8448 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8449 	struct hwrm_stat_ctx_free_input *req;
8450 	int i;
8451 
8452 	if (!bp->bnapi)
8453 		return;
8454 
8455 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8456 		return;
8457 
8458 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8459 		return;
8460 	if (BNXT_FW_MAJ(bp) <= 20) {
8461 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8462 			hwrm_req_drop(bp, req);
8463 			return;
8464 		}
8465 		hwrm_req_hold(bp, req0);
8466 	}
8467 	hwrm_req_hold(bp, req);
8468 	for (i = 0; i < bp->cp_nr_rings; i++) {
8469 		struct bnxt_napi *bnapi = bp->bnapi[i];
8470 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8471 
8472 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8473 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8474 			if (req0) {
8475 				req0->stat_ctx_id = req->stat_ctx_id;
8476 				hwrm_req_send(bp, req0);
8477 			}
8478 			hwrm_req_send(bp, req);
8479 
8480 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8481 		}
8482 	}
8483 	hwrm_req_drop(bp, req);
8484 	if (req0)
8485 		hwrm_req_drop(bp, req0);
8486 }
8487 
8488 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8489 {
8490 	struct hwrm_stat_ctx_alloc_output *resp;
8491 	struct hwrm_stat_ctx_alloc_input *req;
8492 	int rc, i;
8493 
8494 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8495 		return 0;
8496 
8497 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8498 	if (rc)
8499 		return rc;
8500 
8501 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8502 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8503 
8504 	resp = hwrm_req_hold(bp, req);
8505 	for (i = 0; i < bp->cp_nr_rings; i++) {
8506 		struct bnxt_napi *bnapi = bp->bnapi[i];
8507 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8508 
8509 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8510 
8511 		rc = hwrm_req_send(bp, req);
8512 		if (rc)
8513 			break;
8514 
8515 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8516 
8517 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8518 	}
8519 	hwrm_req_drop(bp, req);
8520 	return rc;
8521 }
8522 
8523 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8524 {
8525 	struct hwrm_func_qcfg_output *resp;
8526 	struct hwrm_func_qcfg_input *req;
8527 	u16 flags;
8528 	int rc;
8529 
8530 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8531 	if (rc)
8532 		return rc;
8533 
8534 	req->fid = cpu_to_le16(0xffff);
8535 	resp = hwrm_req_hold(bp, req);
8536 	rc = hwrm_req_send(bp, req);
8537 	if (rc)
8538 		goto func_qcfg_exit;
8539 
8540 	flags = le16_to_cpu(resp->flags);
8541 #ifdef CONFIG_BNXT_SRIOV
8542 	if (BNXT_VF(bp)) {
8543 		struct bnxt_vf_info *vf = &bp->vf;
8544 
8545 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8546 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8547 			vf->flags |= BNXT_VF_TRUST;
8548 		else
8549 			vf->flags &= ~BNXT_VF_TRUST;
8550 	} else {
8551 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8552 	}
8553 #endif
8554 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8555 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8556 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8557 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8558 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8559 	}
8560 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8561 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8562 
8563 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8564 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8565 
8566 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8567 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8568 	if (resp->roce_bidi_opt_mode &
8569 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8570 		bp->cos0_cos1_shared = 1;
8571 	else
8572 		bp->cos0_cos1_shared = 0;
8573 
8574 	switch (resp->port_partition_type) {
8575 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8576 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8577 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8578 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8579 		bp->port_partition_type = resp->port_partition_type;
8580 		break;
8581 	}
8582 	if (bp->hwrm_spec_code < 0x10707 ||
8583 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8584 		bp->br_mode = BRIDGE_MODE_VEB;
8585 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8586 		bp->br_mode = BRIDGE_MODE_VEPA;
8587 	else
8588 		bp->br_mode = BRIDGE_MODE_UNDEF;
8589 
8590 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8591 	if (!bp->max_mtu)
8592 		bp->max_mtu = BNXT_MAX_MTU;
8593 
8594 	if (bp->db_size)
8595 		goto func_qcfg_exit;
8596 
8597 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8598 	if (BNXT_CHIP_P5(bp)) {
8599 		if (BNXT_PF(bp))
8600 			bp->db_offset = DB_PF_OFFSET_P5;
8601 		else
8602 			bp->db_offset = DB_VF_OFFSET_P5;
8603 	}
8604 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8605 				 1024);
8606 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8607 	    bp->db_size <= bp->db_offset)
8608 		bp->db_size = pci_resource_len(bp->pdev, 2);
8609 
8610 func_qcfg_exit:
8611 	hwrm_req_drop(bp, req);
8612 	return rc;
8613 }
8614 
8615 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8616 				      u8 init_val, u8 init_offset,
8617 				      bool init_mask_set)
8618 {
8619 	ctxm->init_value = init_val;
8620 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8621 	if (init_mask_set)
8622 		ctxm->init_offset = init_offset * 4;
8623 	else
8624 		ctxm->init_value = 0;
8625 }
8626 
8627 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8628 {
8629 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8630 	u16 type;
8631 
8632 	for (type = 0; type < ctx_max; type++) {
8633 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8634 		int n = 1;
8635 
8636 		if (!ctxm->max_entries || ctxm->pg_info)
8637 			continue;
8638 
8639 		if (ctxm->instance_bmap)
8640 			n = hweight32(ctxm->instance_bmap);
8641 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8642 		if (!ctxm->pg_info)
8643 			return -ENOMEM;
8644 	}
8645 	return 0;
8646 }
8647 
8648 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8649 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8650 
8651 #define BNXT_CTX_INIT_VALID(flags)	\
8652 	(!!((flags) &			\
8653 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8654 
8655 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8656 {
8657 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8658 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8659 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8660 	u16 type;
8661 	int rc;
8662 
8663 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8664 	if (rc)
8665 		return rc;
8666 
8667 	if (!ctx) {
8668 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8669 		if (!ctx)
8670 			return -ENOMEM;
8671 		bp->ctx = ctx;
8672 	}
8673 
8674 	resp = hwrm_req_hold(bp, req);
8675 
8676 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8677 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8678 		u8 init_val, init_off, i;
8679 		u32 max_entries;
8680 		u16 entry_size;
8681 		__le32 *p;
8682 		u32 flags;
8683 
8684 		req->type = cpu_to_le16(type);
8685 		rc = hwrm_req_send(bp, req);
8686 		if (rc)
8687 			goto ctx_done;
8688 		flags = le32_to_cpu(resp->flags);
8689 		type = le16_to_cpu(resp->next_valid_type);
8690 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8691 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8692 			continue;
8693 		}
8694 		entry_size = le16_to_cpu(resp->entry_size);
8695 		max_entries = le32_to_cpu(resp->max_num_entries);
8696 		if (ctxm->mem_valid) {
8697 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8698 			    ctxm->entry_size != entry_size ||
8699 			    ctxm->max_entries != max_entries)
8700 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8701 			else
8702 				continue;
8703 		}
8704 		ctxm->type = le16_to_cpu(resp->type);
8705 		ctxm->entry_size = entry_size;
8706 		ctxm->flags = flags;
8707 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8708 		ctxm->entry_multiple = resp->entry_multiple;
8709 		ctxm->max_entries = max_entries;
8710 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8711 		init_val = resp->ctx_init_value;
8712 		init_off = resp->ctx_init_offset;
8713 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8714 					  BNXT_CTX_INIT_VALID(flags));
8715 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8716 					      BNXT_MAX_SPLIT_ENTRY);
8717 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8718 		     i++, p++)
8719 			ctxm->split[i] = le32_to_cpu(*p);
8720 	}
8721 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8722 
8723 ctx_done:
8724 	hwrm_req_drop(bp, req);
8725 	return rc;
8726 }
8727 
8728 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8729 {
8730 	struct hwrm_func_backing_store_qcaps_output *resp;
8731 	struct hwrm_func_backing_store_qcaps_input *req;
8732 	int rc;
8733 
8734 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8735 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8736 		return 0;
8737 
8738 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8739 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8740 
8741 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8742 	if (rc)
8743 		return rc;
8744 
8745 	resp = hwrm_req_hold(bp, req);
8746 	rc = hwrm_req_send_silent(bp, req);
8747 	if (!rc) {
8748 		struct bnxt_ctx_mem_type *ctxm;
8749 		struct bnxt_ctx_mem_info *ctx;
8750 		u8 init_val, init_idx = 0;
8751 		u16 init_mask;
8752 
8753 		ctx = bp->ctx;
8754 		if (!ctx) {
8755 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8756 			if (!ctx) {
8757 				rc = -ENOMEM;
8758 				goto ctx_err;
8759 			}
8760 			bp->ctx = ctx;
8761 		}
8762 		init_val = resp->ctx_kind_initializer;
8763 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8764 
8765 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8766 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8767 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8768 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8769 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8770 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8771 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8772 					  (init_mask & (1 << init_idx++)) != 0);
8773 
8774 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8775 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8776 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8777 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8778 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8779 					  (init_mask & (1 << init_idx++)) != 0);
8780 
8781 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8782 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8783 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8784 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8785 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8786 					  (init_mask & (1 << init_idx++)) != 0);
8787 
8788 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8789 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8790 		ctxm->max_entries = ctxm->vnic_entries +
8791 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8792 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8793 		bnxt_init_ctx_initializer(ctxm, init_val,
8794 					  resp->vnic_init_offset,
8795 					  (init_mask & (1 << init_idx++)) != 0);
8796 
8797 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8798 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8799 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8800 		bnxt_init_ctx_initializer(ctxm, init_val,
8801 					  resp->stat_init_offset,
8802 					  (init_mask & (1 << init_idx++)) != 0);
8803 
8804 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8805 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8806 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8807 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8808 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8809 		if (!ctxm->entry_multiple)
8810 			ctxm->entry_multiple = 1;
8811 
8812 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8813 
8814 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8815 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8816 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8817 		ctxm->mrav_num_entries_units =
8818 			le16_to_cpu(resp->mrav_num_entries_units);
8819 		bnxt_init_ctx_initializer(ctxm, init_val,
8820 					  resp->mrav_init_offset,
8821 					  (init_mask & (1 << init_idx++)) != 0);
8822 
8823 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8824 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8825 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8826 
8827 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8828 		if (!ctx->tqm_fp_rings_count)
8829 			ctx->tqm_fp_rings_count = bp->max_q;
8830 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8831 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8832 
8833 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8834 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8835 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8836 
8837 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8838 	} else {
8839 		rc = 0;
8840 	}
8841 ctx_err:
8842 	hwrm_req_drop(bp, req);
8843 	return rc;
8844 }
8845 
8846 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8847 				  __le64 *pg_dir)
8848 {
8849 	if (!rmem->nr_pages)
8850 		return;
8851 
8852 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8853 	if (rmem->depth >= 1) {
8854 		if (rmem->depth == 2)
8855 			*pg_attr |= 2;
8856 		else
8857 			*pg_attr |= 1;
8858 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8859 	} else {
8860 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8861 	}
8862 }
8863 
8864 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8865 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8866 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8867 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8868 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8869 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8870 
8871 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8872 {
8873 	struct hwrm_func_backing_store_cfg_input *req;
8874 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8875 	struct bnxt_ctx_pg_info *ctx_pg;
8876 	struct bnxt_ctx_mem_type *ctxm;
8877 	void **__req = (void **)&req;
8878 	u32 req_len = sizeof(*req);
8879 	__le32 *num_entries;
8880 	__le64 *pg_dir;
8881 	u32 flags = 0;
8882 	u8 *pg_attr;
8883 	u32 ena;
8884 	int rc;
8885 	int i;
8886 
8887 	if (!ctx)
8888 		return 0;
8889 
8890 	if (req_len > bp->hwrm_max_ext_req_len)
8891 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8892 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8893 	if (rc)
8894 		return rc;
8895 
8896 	req->enables = cpu_to_le32(enables);
8897 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8898 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8899 		ctx_pg = ctxm->pg_info;
8900 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8901 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8902 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8903 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8904 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8905 				      &req->qpc_pg_size_qpc_lvl,
8906 				      &req->qpc_page_dir);
8907 
8908 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8909 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8910 	}
8911 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8912 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8913 		ctx_pg = ctxm->pg_info;
8914 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8915 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8916 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8917 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8918 				      &req->srq_pg_size_srq_lvl,
8919 				      &req->srq_page_dir);
8920 	}
8921 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8922 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8923 		ctx_pg = ctxm->pg_info;
8924 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8925 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8926 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8927 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8928 				      &req->cq_pg_size_cq_lvl,
8929 				      &req->cq_page_dir);
8930 	}
8931 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8932 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8933 		ctx_pg = ctxm->pg_info;
8934 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8935 		req->vnic_num_ring_table_entries =
8936 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8937 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8938 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8939 				      &req->vnic_pg_size_vnic_lvl,
8940 				      &req->vnic_page_dir);
8941 	}
8942 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8943 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8944 		ctx_pg = ctxm->pg_info;
8945 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8946 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8947 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8948 				      &req->stat_pg_size_stat_lvl,
8949 				      &req->stat_page_dir);
8950 	}
8951 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8952 		u32 units;
8953 
8954 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8955 		ctx_pg = ctxm->pg_info;
8956 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8957 		units = ctxm->mrav_num_entries_units;
8958 		if (units) {
8959 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8960 			u32 entries;
8961 
8962 			num_mr = ctx_pg->entries - num_ah;
8963 			entries = ((num_mr / units) << 16) | (num_ah / units);
8964 			req->mrav_num_entries = cpu_to_le32(entries);
8965 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8966 		}
8967 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8968 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8969 				      &req->mrav_pg_size_mrav_lvl,
8970 				      &req->mrav_page_dir);
8971 	}
8972 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8973 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8974 		ctx_pg = ctxm->pg_info;
8975 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8976 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8977 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8978 				      &req->tim_pg_size_tim_lvl,
8979 				      &req->tim_page_dir);
8980 	}
8981 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8982 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8983 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8984 	     pg_dir = &req->tqm_sp_page_dir,
8985 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8986 	     ctx_pg = ctxm->pg_info;
8987 	     i < BNXT_MAX_TQM_RINGS;
8988 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8989 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8990 		if (!(enables & ena))
8991 			continue;
8992 
8993 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8994 		*num_entries = cpu_to_le32(ctx_pg->entries);
8995 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8996 	}
8997 	req->flags = cpu_to_le32(flags);
8998 	return hwrm_req_send(bp, req);
8999 }
9000 
9001 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9002 				  struct bnxt_ctx_pg_info *ctx_pg)
9003 {
9004 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9005 
9006 	rmem->page_size = BNXT_PAGE_SIZE;
9007 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9008 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9009 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9010 	if (rmem->depth >= 1)
9011 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9012 	return bnxt_alloc_ring(bp, rmem);
9013 }
9014 
9015 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9016 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9017 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9018 {
9019 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9020 	int rc;
9021 
9022 	if (!mem_size)
9023 		return -EINVAL;
9024 
9025 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9026 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9027 		ctx_pg->nr_pages = 0;
9028 		return -EINVAL;
9029 	}
9030 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9031 		int nr_tbls, i;
9032 
9033 		rmem->depth = 2;
9034 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
9035 					     GFP_KERNEL);
9036 		if (!ctx_pg->ctx_pg_tbl)
9037 			return -ENOMEM;
9038 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9039 		rmem->nr_pages = nr_tbls;
9040 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9041 		if (rc)
9042 			return rc;
9043 		for (i = 0; i < nr_tbls; i++) {
9044 			struct bnxt_ctx_pg_info *pg_tbl;
9045 
9046 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
9047 			if (!pg_tbl)
9048 				return -ENOMEM;
9049 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9050 			rmem = &pg_tbl->ring_mem;
9051 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9052 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9053 			rmem->depth = 1;
9054 			rmem->nr_pages = MAX_CTX_PAGES;
9055 			rmem->ctx_mem = ctxm;
9056 			if (i == (nr_tbls - 1)) {
9057 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9058 
9059 				if (rem)
9060 					rmem->nr_pages = rem;
9061 			}
9062 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9063 			if (rc)
9064 				break;
9065 		}
9066 	} else {
9067 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9068 		if (rmem->nr_pages > 1 || depth)
9069 			rmem->depth = 1;
9070 		rmem->ctx_mem = ctxm;
9071 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9072 	}
9073 	return rc;
9074 }
9075 
9076 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9077 				    struct bnxt_ctx_pg_info *ctx_pg,
9078 				    void *buf, size_t offset, size_t head,
9079 				    size_t tail)
9080 {
9081 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9082 	size_t nr_pages = ctx_pg->nr_pages;
9083 	int page_size = rmem->page_size;
9084 	size_t len = 0, total_len = 0;
9085 	u16 depth = rmem->depth;
9086 
9087 	tail %= nr_pages * page_size;
9088 	do {
9089 		if (depth > 1) {
9090 			int i = head / (page_size * MAX_CTX_PAGES);
9091 			struct bnxt_ctx_pg_info *pg_tbl;
9092 
9093 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9094 			rmem = &pg_tbl->ring_mem;
9095 		}
9096 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9097 		head += len;
9098 		offset += len;
9099 		total_len += len;
9100 		if (head >= nr_pages * page_size)
9101 			head = 0;
9102 	} while (head != tail);
9103 	return total_len;
9104 }
9105 
9106 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9107 				  struct bnxt_ctx_pg_info *ctx_pg)
9108 {
9109 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9110 
9111 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9112 	    ctx_pg->ctx_pg_tbl) {
9113 		int i, nr_tbls = rmem->nr_pages;
9114 
9115 		for (i = 0; i < nr_tbls; i++) {
9116 			struct bnxt_ctx_pg_info *pg_tbl;
9117 			struct bnxt_ring_mem_info *rmem2;
9118 
9119 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9120 			if (!pg_tbl)
9121 				continue;
9122 			rmem2 = &pg_tbl->ring_mem;
9123 			bnxt_free_ring(bp, rmem2);
9124 			ctx_pg->ctx_pg_arr[i] = NULL;
9125 			kfree(pg_tbl);
9126 			ctx_pg->ctx_pg_tbl[i] = NULL;
9127 		}
9128 		kfree(ctx_pg->ctx_pg_tbl);
9129 		ctx_pg->ctx_pg_tbl = NULL;
9130 	}
9131 	bnxt_free_ring(bp, rmem);
9132 	ctx_pg->nr_pages = 0;
9133 }
9134 
9135 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9136 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9137 				   u8 pg_lvl)
9138 {
9139 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9140 	int i, rc = 0, n = 1;
9141 	u32 mem_size;
9142 
9143 	if (!ctxm->entry_size || !ctx_pg)
9144 		return -EINVAL;
9145 	if (ctxm->instance_bmap)
9146 		n = hweight32(ctxm->instance_bmap);
9147 	if (ctxm->entry_multiple)
9148 		entries = roundup(entries, ctxm->entry_multiple);
9149 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9150 	mem_size = entries * ctxm->entry_size;
9151 	for (i = 0; i < n && !rc; i++) {
9152 		ctx_pg[i].entries = entries;
9153 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9154 					    ctxm->init_value ? ctxm : NULL);
9155 	}
9156 	if (!rc)
9157 		ctxm->mem_valid = 1;
9158 	return rc;
9159 }
9160 
9161 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9162 					       struct bnxt_ctx_mem_type *ctxm,
9163 					       bool last)
9164 {
9165 	struct hwrm_func_backing_store_cfg_v2_input *req;
9166 	u32 instance_bmap = ctxm->instance_bmap;
9167 	int i, j, rc = 0, n = 1;
9168 	__le32 *p;
9169 
9170 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9171 		return 0;
9172 
9173 	if (instance_bmap)
9174 		n = hweight32(ctxm->instance_bmap);
9175 	else
9176 		instance_bmap = 1;
9177 
9178 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9179 	if (rc)
9180 		return rc;
9181 	hwrm_req_hold(bp, req);
9182 	req->type = cpu_to_le16(ctxm->type);
9183 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9184 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9185 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9186 		struct bnxt_bs_trace_info *bs_trace;
9187 		u32 enables;
9188 
9189 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9190 		req->enables = cpu_to_le32(enables);
9191 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9192 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9193 	}
9194 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9195 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9196 		p[i] = cpu_to_le32(ctxm->split[i]);
9197 	for (i = 0, j = 0; j < n && !rc; i++) {
9198 		struct bnxt_ctx_pg_info *ctx_pg;
9199 
9200 		if (!(instance_bmap & (1 << i)))
9201 			continue;
9202 		req->instance = cpu_to_le16(i);
9203 		ctx_pg = &ctxm->pg_info[j++];
9204 		if (!ctx_pg->entries)
9205 			continue;
9206 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9207 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9208 				      &req->page_size_pbl_level,
9209 				      &req->page_dir);
9210 		if (last && j == n)
9211 			req->flags =
9212 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9213 		rc = hwrm_req_send(bp, req);
9214 	}
9215 	hwrm_req_drop(bp, req);
9216 	return rc;
9217 }
9218 
9219 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9220 {
9221 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9222 	struct bnxt_ctx_mem_type *ctxm;
9223 	u16 last_type = BNXT_CTX_INV;
9224 	int rc = 0;
9225 	u16 type;
9226 
9227 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9228 		ctxm = &ctx->ctx_arr[type];
9229 		if (!bnxt_bs_trace_avail(bp, type))
9230 			continue;
9231 		if (!ctxm->mem_valid) {
9232 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9233 						     ctxm->max_entries, 1);
9234 			if (rc) {
9235 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9236 					    type);
9237 				continue;
9238 			}
9239 			bnxt_bs_trace_init(bp, ctxm);
9240 		}
9241 		last_type = type;
9242 	}
9243 
9244 	if (last_type == BNXT_CTX_INV) {
9245 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9246 			ctxm = &ctx->ctx_arr[type];
9247 			if (ctxm->mem_valid)
9248 				last_type = type;
9249 		}
9250 		if (last_type == BNXT_CTX_INV)
9251 			return 0;
9252 	}
9253 	ctx->ctx_arr[last_type].last = 1;
9254 
9255 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9256 		ctxm = &ctx->ctx_arr[type];
9257 
9258 		if (!ctxm->mem_valid)
9259 			continue;
9260 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9261 		if (rc)
9262 			return rc;
9263 	}
9264 	return 0;
9265 }
9266 
9267 /**
9268  * __bnxt_copy_ctx_mem - copy host context memory
9269  * @bp: The driver context
9270  * @ctxm: The pointer to the context memory type
9271  * @buf: The destination buffer or NULL to just obtain the length
9272  * @offset: The buffer offset to copy the data to
9273  * @head: The head offset of context memory to copy from
9274  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9275  *
9276  * This function is called for debugging purposes to dump the host context
9277  * used by the chip.
9278  *
9279  * Return: Length of memory copied
9280  */
9281 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9282 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9283 				  size_t offset, size_t head, size_t tail)
9284 {
9285 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9286 	size_t len = 0, total_len = 0;
9287 	int i, n = 1;
9288 
9289 	if (!ctx_pg)
9290 		return 0;
9291 
9292 	if (ctxm->instance_bmap)
9293 		n = hweight32(ctxm->instance_bmap);
9294 	for (i = 0; i < n; i++) {
9295 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9296 					    tail);
9297 		offset += len;
9298 		total_len += len;
9299 	}
9300 	return total_len;
9301 }
9302 
9303 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9304 			 void *buf, size_t offset)
9305 {
9306 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9307 
9308 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9309 }
9310 
9311 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9312 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9313 {
9314 	struct bnxt_ctx_pg_info *ctx_pg;
9315 	int i, n = 1;
9316 
9317 	ctxm->last = 0;
9318 
9319 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9320 		return;
9321 
9322 	ctx_pg = ctxm->pg_info;
9323 	if (ctx_pg) {
9324 		if (ctxm->instance_bmap)
9325 			n = hweight32(ctxm->instance_bmap);
9326 		for (i = 0; i < n; i++)
9327 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9328 
9329 		kfree(ctx_pg);
9330 		ctxm->pg_info = NULL;
9331 		ctxm->mem_valid = 0;
9332 	}
9333 	memset(ctxm, 0, sizeof(*ctxm));
9334 }
9335 
9336 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9337 {
9338 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9339 	u16 type;
9340 
9341 	if (!ctx)
9342 		return;
9343 
9344 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9345 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9346 
9347 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9348 	if (force) {
9349 		kfree(ctx);
9350 		bp->ctx = NULL;
9351 	}
9352 }
9353 
9354 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9355 {
9356 	struct bnxt_ctx_mem_type *ctxm;
9357 	struct bnxt_ctx_mem_info *ctx;
9358 	u32 l2_qps, qp1_qps, max_qps;
9359 	u32 ena, entries_sp, entries;
9360 	u32 srqs, max_srqs, min;
9361 	u32 num_mr, num_ah;
9362 	u32 extra_srqs = 0;
9363 	u32 extra_qps = 0;
9364 	u32 fast_qpmd_qps;
9365 	u8 pg_lvl = 1;
9366 	int i, rc;
9367 
9368 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9369 	if (rc) {
9370 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9371 			   rc);
9372 		return rc;
9373 	}
9374 	ctx = bp->ctx;
9375 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9376 		return 0;
9377 
9378 	ena = 0;
9379 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9380 		goto skip_legacy;
9381 
9382 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9383 	l2_qps = ctxm->qp_l2_entries;
9384 	qp1_qps = ctxm->qp_qp1_entries;
9385 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9386 	max_qps = ctxm->max_entries;
9387 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9388 	srqs = ctxm->srq_l2_entries;
9389 	max_srqs = ctxm->max_entries;
9390 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9391 		pg_lvl = 2;
9392 		if (BNXT_SW_RES_LMT(bp)) {
9393 			extra_qps = max_qps - l2_qps - qp1_qps;
9394 			extra_srqs = max_srqs - srqs;
9395 		} else {
9396 			extra_qps = min_t(u32, 65536,
9397 					  max_qps - l2_qps - qp1_qps);
9398 			/* allocate extra qps if fw supports RoCE fast qp
9399 			 * destroy feature
9400 			 */
9401 			extra_qps += fast_qpmd_qps;
9402 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9403 		}
9404 		if (fast_qpmd_qps)
9405 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9406 	}
9407 
9408 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9409 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9410 				     pg_lvl);
9411 	if (rc)
9412 		return rc;
9413 
9414 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9415 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9416 	if (rc)
9417 		return rc;
9418 
9419 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9420 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9421 				     extra_qps * 2, pg_lvl);
9422 	if (rc)
9423 		return rc;
9424 
9425 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9426 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9427 	if (rc)
9428 		return rc;
9429 
9430 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9431 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9432 	if (rc)
9433 		return rc;
9434 
9435 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9436 		goto skip_rdma;
9437 
9438 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9439 	if (BNXT_SW_RES_LMT(bp) &&
9440 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9441 		num_ah = ctxm->mrav_av_entries;
9442 		num_mr = ctxm->max_entries - num_ah;
9443 	} else {
9444 		/* 128K extra is needed to accommodate static AH context
9445 		 * allocation by f/w.
9446 		 */
9447 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9448 		num_ah = min_t(u32, num_mr, 1024 * 128);
9449 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9450 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9451 			ctxm->mrav_av_entries = num_ah;
9452 	}
9453 
9454 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9455 	if (rc)
9456 		return rc;
9457 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9458 
9459 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9460 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9461 	if (rc)
9462 		return rc;
9463 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9464 
9465 skip_rdma:
9466 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9467 	min = ctxm->min_entries;
9468 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9469 		     2 * (extra_qps + qp1_qps) + min;
9470 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9471 	if (rc)
9472 		return rc;
9473 
9474 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9475 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9476 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9477 	if (rc)
9478 		return rc;
9479 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9480 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9481 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9482 
9483 skip_legacy:
9484 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9485 		rc = bnxt_backing_store_cfg_v2(bp);
9486 	else
9487 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9488 	if (rc) {
9489 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9490 			   rc);
9491 		return rc;
9492 	}
9493 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9494 	return 0;
9495 }
9496 
9497 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9498 {
9499 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9500 	u16 page_attr;
9501 	int rc;
9502 
9503 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9504 		return 0;
9505 
9506 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9507 	if (rc)
9508 		return rc;
9509 
9510 	if (BNXT_PAGE_SIZE == 0x2000)
9511 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9512 	else if (BNXT_PAGE_SIZE == 0x10000)
9513 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9514 	else
9515 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9516 	req->pg_size_lvl = cpu_to_le16(page_attr |
9517 				       bp->fw_crash_mem->ring_mem.depth);
9518 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9519 	req->size = cpu_to_le32(bp->fw_crash_len);
9520 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9521 	return hwrm_req_send(bp, req);
9522 }
9523 
9524 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9525 {
9526 	if (bp->fw_crash_mem) {
9527 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9528 		kfree(bp->fw_crash_mem);
9529 		bp->fw_crash_mem = NULL;
9530 	}
9531 }
9532 
9533 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9534 {
9535 	u32 mem_size = 0;
9536 	int rc;
9537 
9538 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9539 		return 0;
9540 
9541 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9542 	if (rc)
9543 		return rc;
9544 
9545 	mem_size = round_up(mem_size, 4);
9546 
9547 	/* keep and use the existing pages */
9548 	if (bp->fw_crash_mem &&
9549 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9550 		goto alloc_done;
9551 
9552 	if (bp->fw_crash_mem)
9553 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9554 	else
9555 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9556 					   GFP_KERNEL);
9557 	if (!bp->fw_crash_mem)
9558 		return -ENOMEM;
9559 
9560 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9561 	if (rc) {
9562 		bnxt_free_crash_dump_mem(bp);
9563 		return rc;
9564 	}
9565 
9566 alloc_done:
9567 	bp->fw_crash_len = mem_size;
9568 	return 0;
9569 }
9570 
9571 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9572 {
9573 	struct hwrm_func_resource_qcaps_output *resp;
9574 	struct hwrm_func_resource_qcaps_input *req;
9575 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9576 	int rc;
9577 
9578 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9579 	if (rc)
9580 		return rc;
9581 
9582 	req->fid = cpu_to_le16(0xffff);
9583 	resp = hwrm_req_hold(bp, req);
9584 	rc = hwrm_req_send_silent(bp, req);
9585 	if (rc)
9586 		goto hwrm_func_resc_qcaps_exit;
9587 
9588 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9589 	if (!all)
9590 		goto hwrm_func_resc_qcaps_exit;
9591 
9592 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9593 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9594 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9595 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9596 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9597 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9598 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9599 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9600 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9601 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9602 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9603 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9604 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9605 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9606 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9607 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9608 
9609 	if (hw_resc->max_rsscos_ctxs >=
9610 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9611 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9612 
9613 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9614 		u16 max_msix = le16_to_cpu(resp->max_msix);
9615 
9616 		hw_resc->max_nqs = max_msix;
9617 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9618 	}
9619 
9620 	if (BNXT_PF(bp)) {
9621 		struct bnxt_pf_info *pf = &bp->pf;
9622 
9623 		pf->vf_resv_strategy =
9624 			le16_to_cpu(resp->vf_reservation_strategy);
9625 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9626 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9627 	}
9628 hwrm_func_resc_qcaps_exit:
9629 	hwrm_req_drop(bp, req);
9630 	return rc;
9631 }
9632 
9633 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9634 {
9635 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9636 	struct hwrm_port_mac_ptp_qcfg_input *req;
9637 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9638 	u8 flags;
9639 	int rc;
9640 
9641 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9642 		rc = -ENODEV;
9643 		goto no_ptp;
9644 	}
9645 
9646 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9647 	if (rc)
9648 		goto no_ptp;
9649 
9650 	req->port_id = cpu_to_le16(bp->pf.port_id);
9651 	resp = hwrm_req_hold(bp, req);
9652 	rc = hwrm_req_send(bp, req);
9653 	if (rc)
9654 		goto exit;
9655 
9656 	flags = resp->flags;
9657 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9658 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9659 		rc = -ENODEV;
9660 		goto exit;
9661 	}
9662 	if (!ptp) {
9663 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9664 		if (!ptp) {
9665 			rc = -ENOMEM;
9666 			goto exit;
9667 		}
9668 		ptp->bp = bp;
9669 		bp->ptp_cfg = ptp;
9670 	}
9671 
9672 	if (flags &
9673 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9674 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9675 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9676 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9677 	} else if (BNXT_CHIP_P5(bp)) {
9678 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9679 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9680 	} else {
9681 		rc = -ENODEV;
9682 		goto exit;
9683 	}
9684 	ptp->rtc_configured =
9685 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9686 	rc = bnxt_ptp_init(bp);
9687 	if (rc)
9688 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9689 exit:
9690 	hwrm_req_drop(bp, req);
9691 	if (!rc)
9692 		return 0;
9693 
9694 no_ptp:
9695 	bnxt_ptp_clear(bp);
9696 	kfree(ptp);
9697 	bp->ptp_cfg = NULL;
9698 	return rc;
9699 }
9700 
9701 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9702 {
9703 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9704 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9705 	struct hwrm_func_qcaps_output *resp;
9706 	struct hwrm_func_qcaps_input *req;
9707 	int rc;
9708 
9709 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9710 	if (rc)
9711 		return rc;
9712 
9713 	req->fid = cpu_to_le16(0xffff);
9714 	resp = hwrm_req_hold(bp, req);
9715 	rc = hwrm_req_send(bp, req);
9716 	if (rc)
9717 		goto hwrm_func_qcaps_exit;
9718 
9719 	flags = le32_to_cpu(resp->flags);
9720 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9721 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9722 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9723 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9724 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9725 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9726 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9727 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9728 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9729 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9730 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9731 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9732 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9733 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9734 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9735 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9736 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9737 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9738 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9739 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9740 
9741 	flags_ext = le32_to_cpu(resp->flags_ext);
9742 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9743 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9744 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9745 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9746 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9747 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9748 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9749 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9750 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9751 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9752 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9753 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9754 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9755 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9756 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9757 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9758 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9759 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9760 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9761 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9762 
9763 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9764 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9765 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9766 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9767 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9768 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9769 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9770 	if (flags_ext2 &
9771 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9772 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9773 	if (BNXT_PF(bp) &&
9774 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9775 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9776 
9777 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9778 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9779 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9780 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9781 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9782 
9783 	bp->tx_push_thresh = 0;
9784 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9785 	    BNXT_FW_MAJ(bp) > 217)
9786 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9787 
9788 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9789 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9790 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9791 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9792 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9793 	if (!hw_resc->max_hw_ring_grps)
9794 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9795 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9796 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9797 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9798 
9799 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9800 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9801 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9802 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9803 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9804 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9805 
9806 	if (BNXT_PF(bp)) {
9807 		struct bnxt_pf_info *pf = &bp->pf;
9808 
9809 		pf->fw_fid = le16_to_cpu(resp->fid);
9810 		pf->port_id = le16_to_cpu(resp->port_id);
9811 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9812 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9813 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9814 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9815 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9816 			bp->flags |= BNXT_FLAG_WOL_CAP;
9817 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9818 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9819 		} else {
9820 			bnxt_ptp_clear(bp);
9821 			kfree(bp->ptp_cfg);
9822 			bp->ptp_cfg = NULL;
9823 		}
9824 	} else {
9825 #ifdef CONFIG_BNXT_SRIOV
9826 		struct bnxt_vf_info *vf = &bp->vf;
9827 
9828 		vf->fw_fid = le16_to_cpu(resp->fid);
9829 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9830 #endif
9831 	}
9832 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9833 
9834 hwrm_func_qcaps_exit:
9835 	hwrm_req_drop(bp, req);
9836 	return rc;
9837 }
9838 
9839 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9840 {
9841 	struct hwrm_dbg_qcaps_output *resp;
9842 	struct hwrm_dbg_qcaps_input *req;
9843 	int rc;
9844 
9845 	bp->fw_dbg_cap = 0;
9846 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9847 		return;
9848 
9849 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9850 	if (rc)
9851 		return;
9852 
9853 	req->fid = cpu_to_le16(0xffff);
9854 	resp = hwrm_req_hold(bp, req);
9855 	rc = hwrm_req_send(bp, req);
9856 	if (rc)
9857 		goto hwrm_dbg_qcaps_exit;
9858 
9859 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9860 
9861 hwrm_dbg_qcaps_exit:
9862 	hwrm_req_drop(bp, req);
9863 }
9864 
9865 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9866 
9867 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9868 {
9869 	int rc;
9870 
9871 	rc = __bnxt_hwrm_func_qcaps(bp);
9872 	if (rc)
9873 		return rc;
9874 
9875 	bnxt_hwrm_dbg_qcaps(bp);
9876 
9877 	rc = bnxt_hwrm_queue_qportcfg(bp);
9878 	if (rc) {
9879 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9880 		return rc;
9881 	}
9882 	if (bp->hwrm_spec_code >= 0x10803) {
9883 		rc = bnxt_alloc_ctx_mem(bp);
9884 		if (rc)
9885 			return rc;
9886 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9887 		if (!rc)
9888 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9889 	}
9890 	return 0;
9891 }
9892 
9893 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9894 {
9895 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9896 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9897 	u32 flags;
9898 	int rc;
9899 
9900 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9901 		return 0;
9902 
9903 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9904 	if (rc)
9905 		return rc;
9906 
9907 	resp = hwrm_req_hold(bp, req);
9908 	rc = hwrm_req_send(bp, req);
9909 	if (rc)
9910 		goto hwrm_cfa_adv_qcaps_exit;
9911 
9912 	flags = le32_to_cpu(resp->flags);
9913 	if (flags &
9914 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9915 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9916 
9917 	if (flags &
9918 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9919 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9920 
9921 	if (flags &
9922 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9923 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9924 
9925 hwrm_cfa_adv_qcaps_exit:
9926 	hwrm_req_drop(bp, req);
9927 	return rc;
9928 }
9929 
9930 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9931 {
9932 	if (bp->fw_health)
9933 		return 0;
9934 
9935 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9936 	if (!bp->fw_health)
9937 		return -ENOMEM;
9938 
9939 	mutex_init(&bp->fw_health->lock);
9940 	return 0;
9941 }
9942 
9943 static int bnxt_alloc_fw_health(struct bnxt *bp)
9944 {
9945 	int rc;
9946 
9947 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9948 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9949 		return 0;
9950 
9951 	rc = __bnxt_alloc_fw_health(bp);
9952 	if (rc) {
9953 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9954 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9955 		return rc;
9956 	}
9957 
9958 	return 0;
9959 }
9960 
9961 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9962 {
9963 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9964 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9965 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9966 }
9967 
9968 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9969 {
9970 	struct bnxt_fw_health *fw_health = bp->fw_health;
9971 	u32 reg_type;
9972 
9973 	if (!fw_health)
9974 		return;
9975 
9976 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9977 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9978 		fw_health->status_reliable = false;
9979 
9980 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9981 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9982 		fw_health->resets_reliable = false;
9983 }
9984 
9985 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9986 {
9987 	void __iomem *hs;
9988 	u32 status_loc;
9989 	u32 reg_type;
9990 	u32 sig;
9991 
9992 	if (bp->fw_health)
9993 		bp->fw_health->status_reliable = false;
9994 
9995 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9996 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9997 
9998 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9999 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10000 		if (!bp->chip_num) {
10001 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10002 			bp->chip_num = readl(bp->bar0 +
10003 					     BNXT_FW_HEALTH_WIN_BASE +
10004 					     BNXT_GRC_REG_CHIP_NUM);
10005 		}
10006 		if (!BNXT_CHIP_P5_PLUS(bp))
10007 			return;
10008 
10009 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10010 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10011 	} else {
10012 		status_loc = readl(hs + offsetof(struct hcomm_status,
10013 						 fw_status_loc));
10014 	}
10015 
10016 	if (__bnxt_alloc_fw_health(bp)) {
10017 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10018 		return;
10019 	}
10020 
10021 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10022 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10023 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10024 		__bnxt_map_fw_health_reg(bp, status_loc);
10025 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10026 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10027 	}
10028 
10029 	bp->fw_health->status_reliable = true;
10030 }
10031 
10032 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10033 {
10034 	struct bnxt_fw_health *fw_health = bp->fw_health;
10035 	u32 reg_base = 0xffffffff;
10036 	int i;
10037 
10038 	bp->fw_health->status_reliable = false;
10039 	bp->fw_health->resets_reliable = false;
10040 	/* Only pre-map the monitoring GRC registers using window 3 */
10041 	for (i = 0; i < 4; i++) {
10042 		u32 reg = fw_health->regs[i];
10043 
10044 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10045 			continue;
10046 		if (reg_base == 0xffffffff)
10047 			reg_base = reg & BNXT_GRC_BASE_MASK;
10048 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10049 			return -ERANGE;
10050 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10051 	}
10052 	bp->fw_health->status_reliable = true;
10053 	bp->fw_health->resets_reliable = true;
10054 	if (reg_base == 0xffffffff)
10055 		return 0;
10056 
10057 	__bnxt_map_fw_health_reg(bp, reg_base);
10058 	return 0;
10059 }
10060 
10061 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10062 {
10063 	if (!bp->fw_health)
10064 		return;
10065 
10066 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10067 		bp->fw_health->status_reliable = true;
10068 		bp->fw_health->resets_reliable = true;
10069 	} else {
10070 		bnxt_try_map_fw_health_reg(bp);
10071 	}
10072 }
10073 
10074 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10075 {
10076 	struct bnxt_fw_health *fw_health = bp->fw_health;
10077 	struct hwrm_error_recovery_qcfg_output *resp;
10078 	struct hwrm_error_recovery_qcfg_input *req;
10079 	int rc, i;
10080 
10081 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10082 		return 0;
10083 
10084 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10085 	if (rc)
10086 		return rc;
10087 
10088 	resp = hwrm_req_hold(bp, req);
10089 	rc = hwrm_req_send(bp, req);
10090 	if (rc)
10091 		goto err_recovery_out;
10092 	fw_health->flags = le32_to_cpu(resp->flags);
10093 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10094 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10095 		rc = -EINVAL;
10096 		goto err_recovery_out;
10097 	}
10098 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10099 	fw_health->master_func_wait_dsecs =
10100 		le32_to_cpu(resp->master_func_wait_period);
10101 	fw_health->normal_func_wait_dsecs =
10102 		le32_to_cpu(resp->normal_func_wait_period);
10103 	fw_health->post_reset_wait_dsecs =
10104 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10105 	fw_health->post_reset_max_wait_dsecs =
10106 		le32_to_cpu(resp->max_bailout_time_after_reset);
10107 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10108 		le32_to_cpu(resp->fw_health_status_reg);
10109 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10110 		le32_to_cpu(resp->fw_heartbeat_reg);
10111 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10112 		le32_to_cpu(resp->fw_reset_cnt_reg);
10113 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10114 		le32_to_cpu(resp->reset_inprogress_reg);
10115 	fw_health->fw_reset_inprog_reg_mask =
10116 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10117 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10118 	if (fw_health->fw_reset_seq_cnt >= 16) {
10119 		rc = -EINVAL;
10120 		goto err_recovery_out;
10121 	}
10122 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10123 		fw_health->fw_reset_seq_regs[i] =
10124 			le32_to_cpu(resp->reset_reg[i]);
10125 		fw_health->fw_reset_seq_vals[i] =
10126 			le32_to_cpu(resp->reset_reg_val[i]);
10127 		fw_health->fw_reset_seq_delay_msec[i] =
10128 			resp->delay_after_reset[i];
10129 	}
10130 err_recovery_out:
10131 	hwrm_req_drop(bp, req);
10132 	if (!rc)
10133 		rc = bnxt_map_fw_health_regs(bp);
10134 	if (rc)
10135 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10136 	return rc;
10137 }
10138 
10139 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10140 {
10141 	struct hwrm_func_reset_input *req;
10142 	int rc;
10143 
10144 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10145 	if (rc)
10146 		return rc;
10147 
10148 	req->enables = 0;
10149 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10150 	return hwrm_req_send(bp, req);
10151 }
10152 
10153 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10154 {
10155 	struct hwrm_nvm_get_dev_info_output nvm_info;
10156 
10157 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10158 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10159 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10160 			 nvm_info.nvm_cfg_ver_upd);
10161 }
10162 
10163 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10164 {
10165 	struct hwrm_queue_qportcfg_output *resp;
10166 	struct hwrm_queue_qportcfg_input *req;
10167 	u8 i, j, *qptr;
10168 	bool no_rdma;
10169 	int rc = 0;
10170 
10171 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10172 	if (rc)
10173 		return rc;
10174 
10175 	resp = hwrm_req_hold(bp, req);
10176 	rc = hwrm_req_send(bp, req);
10177 	if (rc)
10178 		goto qportcfg_exit;
10179 
10180 	if (!resp->max_configurable_queues) {
10181 		rc = -EINVAL;
10182 		goto qportcfg_exit;
10183 	}
10184 	bp->max_tc = resp->max_configurable_queues;
10185 	bp->max_lltc = resp->max_configurable_lossless_queues;
10186 	if (bp->max_tc > BNXT_MAX_QUEUE)
10187 		bp->max_tc = BNXT_MAX_QUEUE;
10188 
10189 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10190 	qptr = &resp->queue_id0;
10191 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10192 		bp->q_info[j].queue_id = *qptr;
10193 		bp->q_ids[i] = *qptr++;
10194 		bp->q_info[j].queue_profile = *qptr++;
10195 		bp->tc_to_qidx[j] = j;
10196 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10197 		    (no_rdma && BNXT_PF(bp)))
10198 			j++;
10199 	}
10200 	bp->max_q = bp->max_tc;
10201 	bp->max_tc = max_t(u8, j, 1);
10202 
10203 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10204 		bp->max_tc = 1;
10205 
10206 	if (bp->max_lltc > bp->max_tc)
10207 		bp->max_lltc = bp->max_tc;
10208 
10209 qportcfg_exit:
10210 	hwrm_req_drop(bp, req);
10211 	return rc;
10212 }
10213 
10214 static int bnxt_hwrm_poll(struct bnxt *bp)
10215 {
10216 	struct hwrm_ver_get_input *req;
10217 	int rc;
10218 
10219 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10220 	if (rc)
10221 		return rc;
10222 
10223 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10224 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10225 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10226 
10227 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10228 	rc = hwrm_req_send(bp, req);
10229 	return rc;
10230 }
10231 
10232 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10233 {
10234 	struct hwrm_ver_get_output *resp;
10235 	struct hwrm_ver_get_input *req;
10236 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10237 	u32 dev_caps_cfg, hwrm_ver;
10238 	int rc, len, max_tmo_secs;
10239 
10240 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10241 	if (rc)
10242 		return rc;
10243 
10244 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10245 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10246 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10247 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10248 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10249 
10250 	resp = hwrm_req_hold(bp, req);
10251 	rc = hwrm_req_send(bp, req);
10252 	if (rc)
10253 		goto hwrm_ver_get_exit;
10254 
10255 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10256 
10257 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10258 			     resp->hwrm_intf_min_8b << 8 |
10259 			     resp->hwrm_intf_upd_8b;
10260 	if (resp->hwrm_intf_maj_8b < 1) {
10261 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10262 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10263 			    resp->hwrm_intf_upd_8b);
10264 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10265 	}
10266 
10267 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10268 			HWRM_VERSION_UPDATE;
10269 
10270 	if (bp->hwrm_spec_code > hwrm_ver)
10271 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10272 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10273 			 HWRM_VERSION_UPDATE);
10274 	else
10275 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10276 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10277 			 resp->hwrm_intf_upd_8b);
10278 
10279 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10280 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10281 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10282 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10283 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10284 		len = FW_VER_STR_LEN;
10285 	} else {
10286 		fw_maj = resp->hwrm_fw_maj_8b;
10287 		fw_min = resp->hwrm_fw_min_8b;
10288 		fw_bld = resp->hwrm_fw_bld_8b;
10289 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10290 		len = BC_HWRM_STR_LEN;
10291 	}
10292 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10293 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10294 		 fw_rsv);
10295 
10296 	if (strlen(resp->active_pkg_name)) {
10297 		int fw_ver_len = strlen(bp->fw_ver_str);
10298 
10299 		snprintf(bp->fw_ver_str + fw_ver_len,
10300 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10301 			 resp->active_pkg_name);
10302 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10303 	}
10304 
10305 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10306 	if (!bp->hwrm_cmd_timeout)
10307 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10308 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10309 	if (!bp->hwrm_cmd_max_timeout)
10310 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10311 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10312 #ifdef CONFIG_DETECT_HUNG_TASK
10313 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10314 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10315 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10316 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10317 	}
10318 #endif
10319 
10320 	if (resp->hwrm_intf_maj_8b >= 1) {
10321 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10322 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10323 	}
10324 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10325 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10326 
10327 	bp->chip_num = le16_to_cpu(resp->chip_num);
10328 	bp->chip_rev = resp->chip_rev;
10329 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10330 	    !resp->chip_metal)
10331 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10332 
10333 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10334 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10335 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10336 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10337 
10338 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10339 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10340 
10341 	if (dev_caps_cfg &
10342 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10343 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10344 
10345 	if (dev_caps_cfg &
10346 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10347 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10348 
10349 	if (dev_caps_cfg &
10350 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10351 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10352 
10353 hwrm_ver_get_exit:
10354 	hwrm_req_drop(bp, req);
10355 	return rc;
10356 }
10357 
10358 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10359 {
10360 	struct hwrm_fw_set_time_input *req;
10361 	struct tm tm;
10362 	time64_t now = ktime_get_real_seconds();
10363 	int rc;
10364 
10365 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10366 	    bp->hwrm_spec_code < 0x10400)
10367 		return -EOPNOTSUPP;
10368 
10369 	time64_to_tm(now, 0, &tm);
10370 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10371 	if (rc)
10372 		return rc;
10373 
10374 	req->year = cpu_to_le16(1900 + tm.tm_year);
10375 	req->month = 1 + tm.tm_mon;
10376 	req->day = tm.tm_mday;
10377 	req->hour = tm.tm_hour;
10378 	req->minute = tm.tm_min;
10379 	req->second = tm.tm_sec;
10380 	return hwrm_req_send(bp, req);
10381 }
10382 
10383 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10384 {
10385 	u64 sw_tmp;
10386 
10387 	hw &= mask;
10388 	sw_tmp = (*sw & ~mask) | hw;
10389 	if (hw < (*sw & mask))
10390 		sw_tmp += mask + 1;
10391 	WRITE_ONCE(*sw, sw_tmp);
10392 }
10393 
10394 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10395 				    int count, bool ignore_zero)
10396 {
10397 	int i;
10398 
10399 	for (i = 0; i < count; i++) {
10400 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10401 
10402 		if (ignore_zero && !hw)
10403 			continue;
10404 
10405 		if (masks[i] == -1ULL)
10406 			sw_stats[i] = hw;
10407 		else
10408 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10409 	}
10410 }
10411 
10412 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10413 {
10414 	if (!stats->hw_stats)
10415 		return;
10416 
10417 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10418 				stats->hw_masks, stats->len / 8, false);
10419 }
10420 
10421 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10422 {
10423 	struct bnxt_stats_mem *ring0_stats;
10424 	bool ignore_zero = false;
10425 	int i;
10426 
10427 	/* Chip bug.  Counter intermittently becomes 0. */
10428 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10429 		ignore_zero = true;
10430 
10431 	for (i = 0; i < bp->cp_nr_rings; i++) {
10432 		struct bnxt_napi *bnapi = bp->bnapi[i];
10433 		struct bnxt_cp_ring_info *cpr;
10434 		struct bnxt_stats_mem *stats;
10435 
10436 		cpr = &bnapi->cp_ring;
10437 		stats = &cpr->stats;
10438 		if (!i)
10439 			ring0_stats = stats;
10440 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10441 					ring0_stats->hw_masks,
10442 					ring0_stats->len / 8, ignore_zero);
10443 	}
10444 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10445 		struct bnxt_stats_mem *stats = &bp->port_stats;
10446 		__le64 *hw_stats = stats->hw_stats;
10447 		u64 *sw_stats = stats->sw_stats;
10448 		u64 *masks = stats->hw_masks;
10449 		int cnt;
10450 
10451 		cnt = sizeof(struct rx_port_stats) / 8;
10452 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10453 
10454 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10455 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10456 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10457 		cnt = sizeof(struct tx_port_stats) / 8;
10458 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10459 	}
10460 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10461 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10462 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10463 	}
10464 }
10465 
10466 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10467 {
10468 	struct hwrm_port_qstats_input *req;
10469 	struct bnxt_pf_info *pf = &bp->pf;
10470 	int rc;
10471 
10472 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10473 		return 0;
10474 
10475 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10476 		return -EOPNOTSUPP;
10477 
10478 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10479 	if (rc)
10480 		return rc;
10481 
10482 	req->flags = flags;
10483 	req->port_id = cpu_to_le16(pf->port_id);
10484 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10485 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10486 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10487 	return hwrm_req_send(bp, req);
10488 }
10489 
10490 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10491 {
10492 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10493 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10494 	struct hwrm_port_qstats_ext_output *resp_qs;
10495 	struct hwrm_port_qstats_ext_input *req_qs;
10496 	struct bnxt_pf_info *pf = &bp->pf;
10497 	u32 tx_stat_size;
10498 	int rc;
10499 
10500 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10501 		return 0;
10502 
10503 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10504 		return -EOPNOTSUPP;
10505 
10506 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10507 	if (rc)
10508 		return rc;
10509 
10510 	req_qs->flags = flags;
10511 	req_qs->port_id = cpu_to_le16(pf->port_id);
10512 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10513 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10514 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10515 		       sizeof(struct tx_port_stats_ext) : 0;
10516 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10517 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10518 	resp_qs = hwrm_req_hold(bp, req_qs);
10519 	rc = hwrm_req_send(bp, req_qs);
10520 	if (!rc) {
10521 		bp->fw_rx_stats_ext_size =
10522 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10523 		if (BNXT_FW_MAJ(bp) < 220 &&
10524 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10525 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10526 
10527 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10528 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10529 	} else {
10530 		bp->fw_rx_stats_ext_size = 0;
10531 		bp->fw_tx_stats_ext_size = 0;
10532 	}
10533 	hwrm_req_drop(bp, req_qs);
10534 
10535 	if (flags)
10536 		return rc;
10537 
10538 	if (bp->fw_tx_stats_ext_size <=
10539 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10540 		bp->pri2cos_valid = 0;
10541 		return rc;
10542 	}
10543 
10544 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10545 	if (rc)
10546 		return rc;
10547 
10548 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10549 
10550 	resp_qc = hwrm_req_hold(bp, req_qc);
10551 	rc = hwrm_req_send(bp, req_qc);
10552 	if (!rc) {
10553 		u8 *pri2cos;
10554 		int i, j;
10555 
10556 		pri2cos = &resp_qc->pri0_cos_queue_id;
10557 		for (i = 0; i < 8; i++) {
10558 			u8 queue_id = pri2cos[i];
10559 			u8 queue_idx;
10560 
10561 			/* Per port queue IDs start from 0, 10, 20, etc */
10562 			queue_idx = queue_id % 10;
10563 			if (queue_idx > BNXT_MAX_QUEUE) {
10564 				bp->pri2cos_valid = false;
10565 				hwrm_req_drop(bp, req_qc);
10566 				return rc;
10567 			}
10568 			for (j = 0; j < bp->max_q; j++) {
10569 				if (bp->q_ids[j] == queue_id)
10570 					bp->pri2cos_idx[i] = queue_idx;
10571 			}
10572 		}
10573 		bp->pri2cos_valid = true;
10574 	}
10575 	hwrm_req_drop(bp, req_qc);
10576 
10577 	return rc;
10578 }
10579 
10580 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10581 {
10582 	bnxt_hwrm_tunnel_dst_port_free(bp,
10583 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10584 	bnxt_hwrm_tunnel_dst_port_free(bp,
10585 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10586 }
10587 
10588 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10589 {
10590 	int rc, i;
10591 	u32 tpa_flags = 0;
10592 
10593 	if (set_tpa)
10594 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10595 	else if (BNXT_NO_FW_ACCESS(bp))
10596 		return 0;
10597 	for (i = 0; i < bp->nr_vnics; i++) {
10598 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10599 		if (rc) {
10600 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10601 				   i, rc);
10602 			return rc;
10603 		}
10604 	}
10605 	return 0;
10606 }
10607 
10608 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10609 {
10610 	int i;
10611 
10612 	for (i = 0; i < bp->nr_vnics; i++)
10613 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10614 }
10615 
10616 static void bnxt_clear_vnic(struct bnxt *bp)
10617 {
10618 	if (!bp->vnic_info)
10619 		return;
10620 
10621 	bnxt_hwrm_clear_vnic_filter(bp);
10622 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10623 		/* clear all RSS setting before free vnic ctx */
10624 		bnxt_hwrm_clear_vnic_rss(bp);
10625 		bnxt_hwrm_vnic_ctx_free(bp);
10626 	}
10627 	/* before free the vnic, undo the vnic tpa settings */
10628 	if (bp->flags & BNXT_FLAG_TPA)
10629 		bnxt_set_tpa(bp, false);
10630 	bnxt_hwrm_vnic_free(bp);
10631 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10632 		bnxt_hwrm_vnic_ctx_free(bp);
10633 }
10634 
10635 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10636 				    bool irq_re_init)
10637 {
10638 	bnxt_clear_vnic(bp);
10639 	bnxt_hwrm_ring_free(bp, close_path);
10640 	bnxt_hwrm_ring_grp_free(bp);
10641 	if (irq_re_init) {
10642 		bnxt_hwrm_stat_ctx_free(bp);
10643 		bnxt_hwrm_free_tunnel_ports(bp);
10644 	}
10645 }
10646 
10647 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10648 {
10649 	struct hwrm_func_cfg_input *req;
10650 	u8 evb_mode;
10651 	int rc;
10652 
10653 	if (br_mode == BRIDGE_MODE_VEB)
10654 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10655 	else if (br_mode == BRIDGE_MODE_VEPA)
10656 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10657 	else
10658 		return -EINVAL;
10659 
10660 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10661 	if (rc)
10662 		return rc;
10663 
10664 	req->fid = cpu_to_le16(0xffff);
10665 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10666 	req->evb_mode = evb_mode;
10667 	return hwrm_req_send(bp, req);
10668 }
10669 
10670 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10671 {
10672 	struct hwrm_func_cfg_input *req;
10673 	int rc;
10674 
10675 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10676 		return 0;
10677 
10678 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10679 	if (rc)
10680 		return rc;
10681 
10682 	req->fid = cpu_to_le16(0xffff);
10683 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10684 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10685 	if (size == 128)
10686 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10687 
10688 	return hwrm_req_send(bp, req);
10689 }
10690 
10691 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10692 {
10693 	int rc;
10694 
10695 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10696 		goto skip_rss_ctx;
10697 
10698 	/* allocate context for vnic */
10699 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10700 	if (rc) {
10701 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10702 			   vnic->vnic_id, rc);
10703 		goto vnic_setup_err;
10704 	}
10705 	bp->rsscos_nr_ctxs++;
10706 
10707 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10708 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10709 		if (rc) {
10710 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10711 				   vnic->vnic_id, rc);
10712 			goto vnic_setup_err;
10713 		}
10714 		bp->rsscos_nr_ctxs++;
10715 	}
10716 
10717 skip_rss_ctx:
10718 	/* configure default vnic, ring grp */
10719 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10720 	if (rc) {
10721 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10722 			   vnic->vnic_id, rc);
10723 		goto vnic_setup_err;
10724 	}
10725 
10726 	/* Enable RSS hashing on vnic */
10727 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10728 	if (rc) {
10729 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10730 			   vnic->vnic_id, rc);
10731 		goto vnic_setup_err;
10732 	}
10733 
10734 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10735 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10736 		if (rc) {
10737 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10738 				   vnic->vnic_id, rc);
10739 		}
10740 	}
10741 
10742 vnic_setup_err:
10743 	return rc;
10744 }
10745 
10746 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10747 			  u8 valid)
10748 {
10749 	struct hwrm_vnic_update_input *req;
10750 	int rc;
10751 
10752 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10753 	if (rc)
10754 		return rc;
10755 
10756 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10757 
10758 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10759 		req->mru = cpu_to_le16(vnic->mru);
10760 
10761 	req->enables = cpu_to_le32(valid);
10762 
10763 	return hwrm_req_send(bp, req);
10764 }
10765 
10766 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10767 {
10768 	int rc;
10769 
10770 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10771 	if (rc) {
10772 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10773 			   vnic->vnic_id, rc);
10774 		return rc;
10775 	}
10776 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10777 	if (rc)
10778 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10779 			   vnic->vnic_id, rc);
10780 	return rc;
10781 }
10782 
10783 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10784 {
10785 	int rc, i, nr_ctxs;
10786 
10787 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10788 	for (i = 0; i < nr_ctxs; i++) {
10789 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10790 		if (rc) {
10791 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10792 				   vnic->vnic_id, i, rc);
10793 			break;
10794 		}
10795 		bp->rsscos_nr_ctxs++;
10796 	}
10797 	if (i < nr_ctxs)
10798 		return -ENOMEM;
10799 
10800 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10801 	if (rc)
10802 		return rc;
10803 
10804 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10805 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10806 		if (rc) {
10807 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10808 				   vnic->vnic_id, rc);
10809 		}
10810 	}
10811 	return rc;
10812 }
10813 
10814 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10815 {
10816 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10817 		return __bnxt_setup_vnic_p5(bp, vnic);
10818 	else
10819 		return __bnxt_setup_vnic(bp, vnic);
10820 }
10821 
10822 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10823 				     struct bnxt_vnic_info *vnic,
10824 				     u16 start_rx_ring_idx, int rx_rings)
10825 {
10826 	int rc;
10827 
10828 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10829 	if (rc) {
10830 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10831 			   vnic->vnic_id, rc);
10832 		return rc;
10833 	}
10834 	return bnxt_setup_vnic(bp, vnic);
10835 }
10836 
10837 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10838 {
10839 	struct bnxt_vnic_info *vnic;
10840 	int i, rc = 0;
10841 
10842 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10843 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10844 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10845 	}
10846 
10847 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10848 		return 0;
10849 
10850 	for (i = 0; i < bp->rx_nr_rings; i++) {
10851 		u16 vnic_id = i + 1;
10852 		u16 ring_id = i;
10853 
10854 		if (vnic_id >= bp->nr_vnics)
10855 			break;
10856 
10857 		vnic = &bp->vnic_info[vnic_id];
10858 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10859 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10860 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10861 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10862 			break;
10863 	}
10864 	return rc;
10865 }
10866 
10867 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10868 			  bool all)
10869 {
10870 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10871 	struct bnxt_filter_base *usr_fltr, *tmp;
10872 	struct bnxt_ntuple_filter *ntp_fltr;
10873 	int i;
10874 
10875 	if (netif_running(bp->dev)) {
10876 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10877 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10878 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10879 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10880 		}
10881 	}
10882 	if (!all)
10883 		return;
10884 
10885 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10886 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10887 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10888 			ntp_fltr = container_of(usr_fltr,
10889 						struct bnxt_ntuple_filter,
10890 						base);
10891 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10892 			bnxt_del_ntp_filter(bp, ntp_fltr);
10893 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10894 		}
10895 	}
10896 
10897 	if (vnic->rss_table)
10898 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10899 				  vnic->rss_table,
10900 				  vnic->rss_table_dma_addr);
10901 	bp->num_rss_ctx--;
10902 }
10903 
10904 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10905 				  int rxr_id)
10906 {
10907 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10908 	int i, vnic_rx;
10909 
10910 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10911 	 * must be updated because a future filter may use it.
10912 	 */
10913 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10914 		return true;
10915 
10916 	for (i = 0; i < tbl_size; i++) {
10917 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10918 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10919 		else
10920 			vnic_rx = bp->rss_indir_tbl[i];
10921 
10922 		if (rxr_id == vnic_rx)
10923 			return true;
10924 	}
10925 
10926 	return false;
10927 }
10928 
10929 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10930 				u16 mru, int rxr_id)
10931 {
10932 	int rc;
10933 
10934 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10935 		return 0;
10936 
10937 	if (mru) {
10938 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10939 		if (rc) {
10940 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10941 				   vnic->vnic_id, rc);
10942 			return rc;
10943 		}
10944 	}
10945 	vnic->mru = mru;
10946 	bnxt_hwrm_vnic_update(bp, vnic,
10947 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10948 
10949 	return 0;
10950 }
10951 
10952 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10953 {
10954 	struct ethtool_rxfh_context *ctx;
10955 	unsigned long context;
10956 	int rc;
10957 
10958 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10959 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10960 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10961 
10962 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10963 		if (rc)
10964 			return rc;
10965 	}
10966 
10967 	return 0;
10968 }
10969 
10970 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10971 {
10972 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10973 	struct ethtool_rxfh_context *ctx;
10974 	unsigned long context;
10975 
10976 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10977 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10978 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10979 
10980 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10981 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10982 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10983 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10984 				   rss_ctx->index);
10985 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10986 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10987 		}
10988 	}
10989 }
10990 
10991 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10992 {
10993 	struct ethtool_rxfh_context *ctx;
10994 	unsigned long context;
10995 
10996 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10997 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10998 
10999 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11000 	}
11001 }
11002 
11003 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11004 static bool bnxt_promisc_ok(struct bnxt *bp)
11005 {
11006 #ifdef CONFIG_BNXT_SRIOV
11007 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11008 		return false;
11009 #endif
11010 	return true;
11011 }
11012 
11013 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11014 {
11015 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11016 	unsigned int rc = 0;
11017 
11018 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11019 	if (rc) {
11020 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11021 			   rc);
11022 		return rc;
11023 	}
11024 
11025 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11026 	if (rc) {
11027 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11028 			   rc);
11029 		return rc;
11030 	}
11031 	return rc;
11032 }
11033 
11034 static int bnxt_cfg_rx_mode(struct bnxt *);
11035 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
11036 
11037 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11038 {
11039 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11040 	int rc = 0;
11041 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11042 
11043 	if (irq_re_init) {
11044 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11045 		if (rc) {
11046 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11047 				   rc);
11048 			goto err_out;
11049 		}
11050 	}
11051 
11052 	rc = bnxt_hwrm_ring_alloc(bp);
11053 	if (rc) {
11054 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11055 		goto err_out;
11056 	}
11057 
11058 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11059 	if (rc) {
11060 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11061 		goto err_out;
11062 	}
11063 
11064 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11065 		rx_nr_rings--;
11066 
11067 	/* default vnic 0 */
11068 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11069 	if (rc) {
11070 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11071 		goto err_out;
11072 	}
11073 
11074 	if (BNXT_VF(bp))
11075 		bnxt_hwrm_func_qcfg(bp);
11076 
11077 	rc = bnxt_setup_vnic(bp, vnic);
11078 	if (rc)
11079 		goto err_out;
11080 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11081 		bnxt_hwrm_update_rss_hash_cfg(bp);
11082 
11083 	if (bp->flags & BNXT_FLAG_RFS) {
11084 		rc = bnxt_alloc_rfs_vnics(bp);
11085 		if (rc)
11086 			goto err_out;
11087 	}
11088 
11089 	if (bp->flags & BNXT_FLAG_TPA) {
11090 		rc = bnxt_set_tpa(bp, true);
11091 		if (rc)
11092 			goto err_out;
11093 	}
11094 
11095 	if (BNXT_VF(bp))
11096 		bnxt_update_vf_mac(bp);
11097 
11098 	/* Filter for default vnic 0 */
11099 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11100 	if (rc) {
11101 		if (BNXT_VF(bp) && rc == -ENODEV)
11102 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11103 		else
11104 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11105 		goto err_out;
11106 	}
11107 	vnic->uc_filter_count = 1;
11108 
11109 	vnic->rx_mask = 0;
11110 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11111 		goto skip_rx_mask;
11112 
11113 	if (bp->dev->flags & IFF_BROADCAST)
11114 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11115 
11116 	if (bp->dev->flags & IFF_PROMISC)
11117 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11118 
11119 	if (bp->dev->flags & IFF_ALLMULTI) {
11120 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11121 		vnic->mc_list_count = 0;
11122 	} else if (bp->dev->flags & IFF_MULTICAST) {
11123 		u32 mask = 0;
11124 
11125 		bnxt_mc_list_updated(bp, &mask);
11126 		vnic->rx_mask |= mask;
11127 	}
11128 
11129 	rc = bnxt_cfg_rx_mode(bp);
11130 	if (rc)
11131 		goto err_out;
11132 
11133 skip_rx_mask:
11134 	rc = bnxt_hwrm_set_coal(bp);
11135 	if (rc)
11136 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11137 				rc);
11138 
11139 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11140 		rc = bnxt_setup_nitroa0_vnic(bp);
11141 		if (rc)
11142 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11143 				   rc);
11144 	}
11145 
11146 	if (BNXT_VF(bp)) {
11147 		bnxt_hwrm_func_qcfg(bp);
11148 		netdev_update_features(bp->dev);
11149 	}
11150 
11151 	return 0;
11152 
11153 err_out:
11154 	bnxt_hwrm_resource_free(bp, 0, true);
11155 
11156 	return rc;
11157 }
11158 
11159 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11160 {
11161 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11162 	return 0;
11163 }
11164 
11165 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11166 {
11167 	bnxt_init_cp_rings(bp);
11168 	bnxt_init_rx_rings(bp);
11169 	bnxt_init_tx_rings(bp);
11170 	bnxt_init_ring_grps(bp, irq_re_init);
11171 	bnxt_init_vnics(bp);
11172 
11173 	return bnxt_init_chip(bp, irq_re_init);
11174 }
11175 
11176 static int bnxt_set_real_num_queues(struct bnxt *bp)
11177 {
11178 	int rc;
11179 	struct net_device *dev = bp->dev;
11180 
11181 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11182 					  bp->tx_nr_rings_xdp);
11183 	if (rc)
11184 		return rc;
11185 
11186 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11187 	if (rc)
11188 		return rc;
11189 
11190 #ifdef CONFIG_RFS_ACCEL
11191 	if (bp->flags & BNXT_FLAG_RFS)
11192 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11193 #endif
11194 
11195 	return rc;
11196 }
11197 
11198 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11199 			     bool shared)
11200 {
11201 	int _rx = *rx, _tx = *tx;
11202 
11203 	if (shared) {
11204 		*rx = min_t(int, _rx, max);
11205 		*tx = min_t(int, _tx, max);
11206 	} else {
11207 		if (max < 2)
11208 			return -ENOMEM;
11209 
11210 		while (_rx + _tx > max) {
11211 			if (_rx > _tx && _rx > 1)
11212 				_rx--;
11213 			else if (_tx > 1)
11214 				_tx--;
11215 		}
11216 		*rx = _rx;
11217 		*tx = _tx;
11218 	}
11219 	return 0;
11220 }
11221 
11222 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11223 {
11224 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11225 }
11226 
11227 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11228 {
11229 	int tcs = bp->num_tc;
11230 
11231 	if (!tcs)
11232 		tcs = 1;
11233 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11234 }
11235 
11236 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11237 {
11238 	int tcs = bp->num_tc;
11239 
11240 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11241 	       bp->tx_nr_rings_xdp;
11242 }
11243 
11244 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11245 			   bool sh)
11246 {
11247 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11248 
11249 	if (tx_cp != *tx) {
11250 		int tx_saved = tx_cp, rc;
11251 
11252 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11253 		if (rc)
11254 			return rc;
11255 		if (tx_cp != tx_saved)
11256 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11257 		return 0;
11258 	}
11259 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11260 }
11261 
11262 static void bnxt_setup_msix(struct bnxt *bp)
11263 {
11264 	const int len = sizeof(bp->irq_tbl[0].name);
11265 	struct net_device *dev = bp->dev;
11266 	int tcs, i;
11267 
11268 	tcs = bp->num_tc;
11269 	if (tcs) {
11270 		int i, off, count;
11271 
11272 		for (i = 0; i < tcs; i++) {
11273 			count = bp->tx_nr_rings_per_tc;
11274 			off = BNXT_TC_TO_RING_BASE(bp, i);
11275 			netdev_set_tc_queue(dev, i, count, off);
11276 		}
11277 	}
11278 
11279 	for (i = 0; i < bp->cp_nr_rings; i++) {
11280 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11281 		char *attr;
11282 
11283 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11284 			attr = "TxRx";
11285 		else if (i < bp->rx_nr_rings)
11286 			attr = "rx";
11287 		else
11288 			attr = "tx";
11289 
11290 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11291 			 attr, i);
11292 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11293 	}
11294 }
11295 
11296 static int bnxt_init_int_mode(struct bnxt *bp);
11297 
11298 static int bnxt_change_msix(struct bnxt *bp, int total)
11299 {
11300 	struct msi_map map;
11301 	int i;
11302 
11303 	/* add MSIX to the end if needed */
11304 	for (i = bp->total_irqs; i < total; i++) {
11305 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11306 		if (map.index < 0)
11307 			return bp->total_irqs;
11308 		bp->irq_tbl[i].vector = map.virq;
11309 		bp->total_irqs++;
11310 	}
11311 
11312 	/* trim MSIX from the end if needed */
11313 	for (i = bp->total_irqs; i > total; i--) {
11314 		map.index = i - 1;
11315 		map.virq = bp->irq_tbl[i - 1].vector;
11316 		pci_msix_free_irq(bp->pdev, map);
11317 		bp->total_irqs--;
11318 	}
11319 	return bp->total_irqs;
11320 }
11321 
11322 static int bnxt_setup_int_mode(struct bnxt *bp)
11323 {
11324 	int rc;
11325 
11326 	if (!bp->irq_tbl) {
11327 		rc = bnxt_init_int_mode(bp);
11328 		if (rc || !bp->irq_tbl)
11329 			return rc ?: -ENODEV;
11330 	}
11331 
11332 	bnxt_setup_msix(bp);
11333 
11334 	rc = bnxt_set_real_num_queues(bp);
11335 	return rc;
11336 }
11337 
11338 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11339 {
11340 	return bp->hw_resc.max_rsscos_ctxs;
11341 }
11342 
11343 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11344 {
11345 	return bp->hw_resc.max_vnics;
11346 }
11347 
11348 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11349 {
11350 	return bp->hw_resc.max_stat_ctxs;
11351 }
11352 
11353 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11354 {
11355 	return bp->hw_resc.max_cp_rings;
11356 }
11357 
11358 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11359 {
11360 	unsigned int cp = bp->hw_resc.max_cp_rings;
11361 
11362 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11363 		cp -= bnxt_get_ulp_msix_num(bp);
11364 
11365 	return cp;
11366 }
11367 
11368 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11369 {
11370 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11371 
11372 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11373 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11374 
11375 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11376 }
11377 
11378 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11379 {
11380 	bp->hw_resc.max_irqs = max_irqs;
11381 }
11382 
11383 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11384 {
11385 	unsigned int cp;
11386 
11387 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11388 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11389 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11390 	else
11391 		return cp - bp->cp_nr_rings;
11392 }
11393 
11394 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11395 {
11396 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11397 }
11398 
11399 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11400 {
11401 	int max_irq = bnxt_get_max_func_irqs(bp);
11402 	int total_req = bp->cp_nr_rings + num;
11403 
11404 	if (max_irq < total_req) {
11405 		num = max_irq - bp->cp_nr_rings;
11406 		if (num <= 0)
11407 			return 0;
11408 	}
11409 	return num;
11410 }
11411 
11412 static int bnxt_get_num_msix(struct bnxt *bp)
11413 {
11414 	if (!BNXT_NEW_RM(bp))
11415 		return bnxt_get_max_func_irqs(bp);
11416 
11417 	return bnxt_nq_rings_in_use(bp);
11418 }
11419 
11420 static int bnxt_init_int_mode(struct bnxt *bp)
11421 {
11422 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11423 
11424 	total_vecs = bnxt_get_num_msix(bp);
11425 	max = bnxt_get_max_func_irqs(bp);
11426 	if (total_vecs > max)
11427 		total_vecs = max;
11428 
11429 	if (!total_vecs)
11430 		return 0;
11431 
11432 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11433 		min = 2;
11434 
11435 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11436 					   PCI_IRQ_MSIX);
11437 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11438 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11439 		rc = -ENODEV;
11440 		goto msix_setup_exit;
11441 	}
11442 
11443 	tbl_size = total_vecs;
11444 	if (pci_msix_can_alloc_dyn(bp->pdev))
11445 		tbl_size = max;
11446 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11447 	if (bp->irq_tbl) {
11448 		for (i = 0; i < total_vecs; i++)
11449 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11450 
11451 		bp->total_irqs = total_vecs;
11452 		/* Trim rings based upon num of vectors allocated */
11453 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11454 				     total_vecs - ulp_msix, min == 1);
11455 		if (rc)
11456 			goto msix_setup_exit;
11457 
11458 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11459 		bp->cp_nr_rings = (min == 1) ?
11460 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11461 				  tx_cp + bp->rx_nr_rings;
11462 
11463 	} else {
11464 		rc = -ENOMEM;
11465 		goto msix_setup_exit;
11466 	}
11467 	return 0;
11468 
11469 msix_setup_exit:
11470 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11471 	kfree(bp->irq_tbl);
11472 	bp->irq_tbl = NULL;
11473 	pci_free_irq_vectors(bp->pdev);
11474 	return rc;
11475 }
11476 
11477 static void bnxt_clear_int_mode(struct bnxt *bp)
11478 {
11479 	pci_free_irq_vectors(bp->pdev);
11480 
11481 	kfree(bp->irq_tbl);
11482 	bp->irq_tbl = NULL;
11483 }
11484 
11485 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11486 {
11487 	bool irq_cleared = false;
11488 	bool irq_change = false;
11489 	int tcs = bp->num_tc;
11490 	int irqs_required;
11491 	int rc;
11492 
11493 	if (!bnxt_need_reserve_rings(bp))
11494 		return 0;
11495 
11496 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11497 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11498 
11499 		if (ulp_msix > bp->ulp_num_msix_want)
11500 			ulp_msix = bp->ulp_num_msix_want;
11501 		irqs_required = ulp_msix + bp->cp_nr_rings;
11502 	} else {
11503 		irqs_required = bnxt_get_num_msix(bp);
11504 	}
11505 
11506 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11507 		irq_change = true;
11508 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11509 			bnxt_ulp_irq_stop(bp);
11510 			bnxt_clear_int_mode(bp);
11511 			irq_cleared = true;
11512 		}
11513 	}
11514 	rc = __bnxt_reserve_rings(bp);
11515 	if (irq_cleared) {
11516 		if (!rc)
11517 			rc = bnxt_init_int_mode(bp);
11518 		bnxt_ulp_irq_restart(bp, rc);
11519 	} else if (irq_change && !rc) {
11520 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11521 			rc = -ENOSPC;
11522 	}
11523 	if (rc) {
11524 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11525 		return rc;
11526 	}
11527 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11528 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11529 		netdev_err(bp->dev, "tx ring reservation failure\n");
11530 		netdev_reset_tc(bp->dev);
11531 		bp->num_tc = 0;
11532 		if (bp->tx_nr_rings_xdp)
11533 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11534 		else
11535 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11536 		return -ENOMEM;
11537 	}
11538 	return 0;
11539 }
11540 
11541 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11542 {
11543 	struct bnxt_tx_ring_info *txr;
11544 	struct netdev_queue *txq;
11545 	struct bnxt_napi *bnapi;
11546 	int i;
11547 
11548 	bnapi = bp->bnapi[idx];
11549 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11550 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11551 		synchronize_net();
11552 
11553 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11554 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11555 			if (txq) {
11556 				__netif_tx_lock_bh(txq);
11557 				netif_tx_stop_queue(txq);
11558 				__netif_tx_unlock_bh(txq);
11559 			}
11560 		}
11561 
11562 		if (!bp->tph_mode)
11563 			continue;
11564 
11565 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11566 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11567 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11568 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11569 	}
11570 }
11571 
11572 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11573 {
11574 	struct bnxt_tx_ring_info *txr;
11575 	struct netdev_queue *txq;
11576 	struct bnxt_napi *bnapi;
11577 	int rc, i;
11578 
11579 	bnapi = bp->bnapi[idx];
11580 	/* All rings have been reserved and previously allocated.
11581 	 * Reallocating with the same parameters should never fail.
11582 	 */
11583 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11584 		if (!bp->tph_mode)
11585 			goto start_tx;
11586 
11587 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11588 		if (rc)
11589 			return rc;
11590 
11591 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11592 		if (rc)
11593 			return rc;
11594 
11595 		txr->tx_prod = 0;
11596 		txr->tx_cons = 0;
11597 		txr->tx_hw_cons = 0;
11598 start_tx:
11599 		WRITE_ONCE(txr->dev_state, 0);
11600 		synchronize_net();
11601 
11602 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11603 			continue;
11604 
11605 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11606 		if (txq)
11607 			netif_tx_start_queue(txq);
11608 	}
11609 
11610 	return 0;
11611 }
11612 
11613 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11614 				     const cpumask_t *mask)
11615 {
11616 	struct bnxt_irq *irq;
11617 	u16 tag;
11618 	int err;
11619 
11620 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11621 
11622 	if (!irq->bp->tph_mode)
11623 		return;
11624 
11625 	cpumask_copy(irq->cpu_mask, mask);
11626 
11627 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11628 		return;
11629 
11630 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11631 				cpumask_first(irq->cpu_mask), &tag))
11632 		return;
11633 
11634 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11635 		return;
11636 
11637 	netdev_lock(irq->bp->dev);
11638 	if (netif_running(irq->bp->dev)) {
11639 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11640 		if (err)
11641 			netdev_err(irq->bp->dev,
11642 				   "RX queue restart failed: err=%d\n", err);
11643 	}
11644 	netdev_unlock(irq->bp->dev);
11645 }
11646 
11647 static void bnxt_irq_affinity_release(struct kref *ref)
11648 {
11649 	struct irq_affinity_notify *notify =
11650 		container_of(ref, struct irq_affinity_notify, kref);
11651 	struct bnxt_irq *irq;
11652 
11653 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11654 
11655 	if (!irq->bp->tph_mode)
11656 		return;
11657 
11658 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11659 		netdev_err(irq->bp->dev,
11660 			   "Setting ST=0 for MSIX entry %d failed\n",
11661 			   irq->msix_nr);
11662 		return;
11663 	}
11664 }
11665 
11666 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11667 {
11668 	irq_set_affinity_notifier(irq->vector, NULL);
11669 }
11670 
11671 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11672 {
11673 	struct irq_affinity_notify *notify;
11674 
11675 	irq->bp = bp;
11676 
11677 	/* Nothing to do if TPH is not enabled */
11678 	if (!bp->tph_mode)
11679 		return;
11680 
11681 	/* Register IRQ affinity notifier */
11682 	notify = &irq->affinity_notify;
11683 	notify->irq = irq->vector;
11684 	notify->notify = bnxt_irq_affinity_notify;
11685 	notify->release = bnxt_irq_affinity_release;
11686 
11687 	irq_set_affinity_notifier(irq->vector, notify);
11688 }
11689 
11690 static void bnxt_free_irq(struct bnxt *bp)
11691 {
11692 	struct bnxt_irq *irq;
11693 	int i;
11694 
11695 #ifdef CONFIG_RFS_ACCEL
11696 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11697 	bp->dev->rx_cpu_rmap = NULL;
11698 #endif
11699 	if (!bp->irq_tbl || !bp->bnapi)
11700 		return;
11701 
11702 	for (i = 0; i < bp->cp_nr_rings; i++) {
11703 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11704 
11705 		irq = &bp->irq_tbl[map_idx];
11706 		if (irq->requested) {
11707 			if (irq->have_cpumask) {
11708 				irq_update_affinity_hint(irq->vector, NULL);
11709 				free_cpumask_var(irq->cpu_mask);
11710 				irq->have_cpumask = 0;
11711 			}
11712 
11713 			bnxt_release_irq_notifier(irq);
11714 
11715 			free_irq(irq->vector, bp->bnapi[i]);
11716 		}
11717 
11718 		irq->requested = 0;
11719 	}
11720 
11721 	/* Disable TPH support */
11722 	pcie_disable_tph(bp->pdev);
11723 	bp->tph_mode = 0;
11724 }
11725 
11726 static int bnxt_request_irq(struct bnxt *bp)
11727 {
11728 	struct cpu_rmap *rmap = NULL;
11729 	int i, j, rc = 0;
11730 	unsigned long flags = 0;
11731 
11732 	rc = bnxt_setup_int_mode(bp);
11733 	if (rc) {
11734 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11735 			   rc);
11736 		return rc;
11737 	}
11738 #ifdef CONFIG_RFS_ACCEL
11739 	rmap = bp->dev->rx_cpu_rmap;
11740 #endif
11741 
11742 	/* Enable TPH support as part of IRQ request */
11743 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11744 	if (!rc)
11745 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11746 
11747 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11748 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11749 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11750 
11751 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11752 		    rmap && bp->bnapi[i]->rx_ring) {
11753 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11754 			if (rc)
11755 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11756 					    j);
11757 			j++;
11758 		}
11759 
11760 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11761 				 bp->bnapi[i]);
11762 		if (rc)
11763 			break;
11764 
11765 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11766 		irq->requested = 1;
11767 
11768 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11769 			int numa_node = dev_to_node(&bp->pdev->dev);
11770 			u16 tag;
11771 
11772 			irq->have_cpumask = 1;
11773 			irq->msix_nr = map_idx;
11774 			irq->ring_nr = i;
11775 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11776 					irq->cpu_mask);
11777 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11778 			if (rc) {
11779 				netdev_warn(bp->dev,
11780 					    "Update affinity hint failed, IRQ = %d\n",
11781 					    irq->vector);
11782 				break;
11783 			}
11784 
11785 			bnxt_register_irq_notifier(bp, irq);
11786 
11787 			/* Init ST table entry */
11788 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11789 						cpumask_first(irq->cpu_mask),
11790 						&tag))
11791 				continue;
11792 
11793 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11794 		}
11795 	}
11796 	return rc;
11797 }
11798 
11799 static void bnxt_del_napi(struct bnxt *bp)
11800 {
11801 	int i;
11802 
11803 	if (!bp->bnapi)
11804 		return;
11805 
11806 	for (i = 0; i < bp->rx_nr_rings; i++)
11807 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11808 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11809 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11810 
11811 	for (i = 0; i < bp->cp_nr_rings; i++) {
11812 		struct bnxt_napi *bnapi = bp->bnapi[i];
11813 
11814 		__netif_napi_del_locked(&bnapi->napi);
11815 	}
11816 	/* We called __netif_napi_del_locked(), we need
11817 	 * to respect an RCU grace period before freeing napi structures.
11818 	 */
11819 	synchronize_net();
11820 }
11821 
11822 static void bnxt_init_napi(struct bnxt *bp)
11823 {
11824 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11825 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11826 	struct bnxt_napi *bnapi;
11827 	int i;
11828 
11829 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11830 		poll_fn = bnxt_poll_p5;
11831 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11832 		cp_nr_rings--;
11833 
11834 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11835 
11836 	for (i = 0; i < cp_nr_rings; i++) {
11837 		bnapi = bp->bnapi[i];
11838 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11839 					     bnapi->index);
11840 	}
11841 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11842 		bnapi = bp->bnapi[cp_nr_rings];
11843 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11844 	}
11845 }
11846 
11847 static void bnxt_disable_napi(struct bnxt *bp)
11848 {
11849 	int i;
11850 
11851 	if (!bp->bnapi ||
11852 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11853 		return;
11854 
11855 	for (i = 0; i < bp->cp_nr_rings; i++) {
11856 		struct bnxt_napi *bnapi = bp->bnapi[i];
11857 		struct bnxt_cp_ring_info *cpr;
11858 
11859 		cpr = &bnapi->cp_ring;
11860 		if (bnapi->tx_fault)
11861 			cpr->sw_stats->tx.tx_resets++;
11862 		if (bnapi->in_reset)
11863 			cpr->sw_stats->rx.rx_resets++;
11864 		napi_disable_locked(&bnapi->napi);
11865 	}
11866 }
11867 
11868 static void bnxt_enable_napi(struct bnxt *bp)
11869 {
11870 	int i;
11871 
11872 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11873 	for (i = 0; i < bp->cp_nr_rings; i++) {
11874 		struct bnxt_napi *bnapi = bp->bnapi[i];
11875 		struct bnxt_cp_ring_info *cpr;
11876 
11877 		bnapi->tx_fault = 0;
11878 
11879 		cpr = &bnapi->cp_ring;
11880 		bnapi->in_reset = false;
11881 
11882 		if (bnapi->rx_ring) {
11883 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11884 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11885 		}
11886 		napi_enable_locked(&bnapi->napi);
11887 	}
11888 }
11889 
11890 void bnxt_tx_disable(struct bnxt *bp)
11891 {
11892 	int i;
11893 	struct bnxt_tx_ring_info *txr;
11894 
11895 	if (bp->tx_ring) {
11896 		for (i = 0; i < bp->tx_nr_rings; i++) {
11897 			txr = &bp->tx_ring[i];
11898 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11899 		}
11900 	}
11901 	/* Make sure napi polls see @dev_state change */
11902 	synchronize_net();
11903 	/* Drop carrier first to prevent TX timeout */
11904 	netif_carrier_off(bp->dev);
11905 	/* Stop all TX queues */
11906 	netif_tx_disable(bp->dev);
11907 }
11908 
11909 void bnxt_tx_enable(struct bnxt *bp)
11910 {
11911 	int i;
11912 	struct bnxt_tx_ring_info *txr;
11913 
11914 	for (i = 0; i < bp->tx_nr_rings; i++) {
11915 		txr = &bp->tx_ring[i];
11916 		WRITE_ONCE(txr->dev_state, 0);
11917 	}
11918 	/* Make sure napi polls see @dev_state change */
11919 	synchronize_net();
11920 	netif_tx_wake_all_queues(bp->dev);
11921 	if (BNXT_LINK_IS_UP(bp))
11922 		netif_carrier_on(bp->dev);
11923 }
11924 
11925 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11926 {
11927 	u8 active_fec = link_info->active_fec_sig_mode &
11928 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11929 
11930 	switch (active_fec) {
11931 	default:
11932 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11933 		return "None";
11934 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11935 		return "Clause 74 BaseR";
11936 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11937 		return "Clause 91 RS(528,514)";
11938 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11939 		return "Clause 91 RS544_1XN";
11940 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11941 		return "Clause 91 RS(544,514)";
11942 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11943 		return "Clause 91 RS272_1XN";
11944 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11945 		return "Clause 91 RS(272,257)";
11946 	}
11947 }
11948 
11949 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
11950 {
11951 	u8 reason = link_info->link_down_reason;
11952 
11953 	/* Multiple bits can be set, we report 1 bit only in order of
11954 	 * priority.
11955 	 */
11956 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
11957 		return "(Remote fault)";
11958 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
11959 		return "(OTP Speed limit violation)";
11960 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
11961 		return "(Cable removed)";
11962 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
11963 		return "(Module fault)";
11964 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
11965 		return "(BMC request down)";
11966 	return "";
11967 }
11968 
11969 void bnxt_report_link(struct bnxt *bp)
11970 {
11971 	if (BNXT_LINK_IS_UP(bp)) {
11972 		const char *signal = "";
11973 		const char *flow_ctrl;
11974 		const char *duplex;
11975 		u32 speed;
11976 		u16 fec;
11977 
11978 		netif_carrier_on(bp->dev);
11979 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11980 		if (speed == SPEED_UNKNOWN) {
11981 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11982 			return;
11983 		}
11984 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11985 			duplex = "full";
11986 		else
11987 			duplex = "half";
11988 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11989 			flow_ctrl = "ON - receive & transmit";
11990 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11991 			flow_ctrl = "ON - transmit";
11992 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11993 			flow_ctrl = "ON - receive";
11994 		else
11995 			flow_ctrl = "none";
11996 		if (bp->link_info.phy_qcfg_resp.option_flags &
11997 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11998 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11999 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12000 			switch (sig_mode) {
12001 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12002 				signal = "(NRZ) ";
12003 				break;
12004 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12005 				signal = "(PAM4 56Gbps) ";
12006 				break;
12007 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12008 				signal = "(PAM4 112Gbps) ";
12009 				break;
12010 			default:
12011 				break;
12012 			}
12013 		}
12014 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12015 			    speed, signal, duplex, flow_ctrl);
12016 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12017 			netdev_info(bp->dev, "EEE is %s\n",
12018 				    bp->eee.eee_active ? "active" :
12019 							 "not active");
12020 		fec = bp->link_info.fec_cfg;
12021 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12022 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12023 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12024 				    bnxt_report_fec(&bp->link_info));
12025 	} else {
12026 		char *str = bnxt_link_down_reason(&bp->link_info);
12027 
12028 		netif_carrier_off(bp->dev);
12029 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12030 	}
12031 }
12032 
12033 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12034 {
12035 	if (!resp->supported_speeds_auto_mode &&
12036 	    !resp->supported_speeds_force_mode &&
12037 	    !resp->supported_pam4_speeds_auto_mode &&
12038 	    !resp->supported_pam4_speeds_force_mode &&
12039 	    !resp->supported_speeds2_auto_mode &&
12040 	    !resp->supported_speeds2_force_mode)
12041 		return true;
12042 	return false;
12043 }
12044 
12045 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12046 {
12047 	struct bnxt_link_info *link_info = &bp->link_info;
12048 	struct hwrm_port_phy_qcaps_output *resp;
12049 	struct hwrm_port_phy_qcaps_input *req;
12050 	int rc = 0;
12051 
12052 	if (bp->hwrm_spec_code < 0x10201)
12053 		return 0;
12054 
12055 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12056 	if (rc)
12057 		return rc;
12058 
12059 	resp = hwrm_req_hold(bp, req);
12060 	rc = hwrm_req_send(bp, req);
12061 	if (rc)
12062 		goto hwrm_phy_qcaps_exit;
12063 
12064 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12065 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12066 		struct ethtool_keee *eee = &bp->eee;
12067 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12068 
12069 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12070 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12071 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12072 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12073 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12074 	}
12075 
12076 	if (bp->hwrm_spec_code >= 0x10a01) {
12077 		if (bnxt_phy_qcaps_no_speed(resp)) {
12078 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12079 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12080 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12081 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12082 			netdev_info(bp->dev, "Ethernet link enabled\n");
12083 			/* Phy re-enabled, reprobe the speeds */
12084 			link_info->support_auto_speeds = 0;
12085 			link_info->support_pam4_auto_speeds = 0;
12086 			link_info->support_auto_speeds2 = 0;
12087 		}
12088 	}
12089 	if (resp->supported_speeds_auto_mode)
12090 		link_info->support_auto_speeds =
12091 			le16_to_cpu(resp->supported_speeds_auto_mode);
12092 	if (resp->supported_pam4_speeds_auto_mode)
12093 		link_info->support_pam4_auto_speeds =
12094 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12095 	if (resp->supported_speeds2_auto_mode)
12096 		link_info->support_auto_speeds2 =
12097 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12098 
12099 	bp->port_count = resp->port_cnt;
12100 
12101 hwrm_phy_qcaps_exit:
12102 	hwrm_req_drop(bp, req);
12103 	return rc;
12104 }
12105 
12106 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12107 {
12108 	struct hwrm_port_mac_qcaps_output *resp;
12109 	struct hwrm_port_mac_qcaps_input *req;
12110 	int rc;
12111 
12112 	if (bp->hwrm_spec_code < 0x10a03)
12113 		return;
12114 
12115 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12116 	if (rc)
12117 		return;
12118 
12119 	resp = hwrm_req_hold(bp, req);
12120 	rc = hwrm_req_send_silent(bp, req);
12121 	if (!rc)
12122 		bp->mac_flags = resp->flags;
12123 	hwrm_req_drop(bp, req);
12124 }
12125 
12126 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12127 {
12128 	u16 diff = advertising ^ supported;
12129 
12130 	return ((supported | diff) != supported);
12131 }
12132 
12133 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12134 {
12135 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12136 
12137 	/* Check if any advertised speeds are no longer supported. The caller
12138 	 * holds the link_lock mutex, so we can modify link_info settings.
12139 	 */
12140 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12141 		if (bnxt_support_dropped(link_info->advertising,
12142 					 link_info->support_auto_speeds2)) {
12143 			link_info->advertising = link_info->support_auto_speeds2;
12144 			return true;
12145 		}
12146 		return false;
12147 	}
12148 	if (bnxt_support_dropped(link_info->advertising,
12149 				 link_info->support_auto_speeds)) {
12150 		link_info->advertising = link_info->support_auto_speeds;
12151 		return true;
12152 	}
12153 	if (bnxt_support_dropped(link_info->advertising_pam4,
12154 				 link_info->support_pam4_auto_speeds)) {
12155 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12156 		return true;
12157 	}
12158 	return false;
12159 }
12160 
12161 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12162 {
12163 	struct bnxt_link_info *link_info = &bp->link_info;
12164 	struct hwrm_port_phy_qcfg_output *resp;
12165 	struct hwrm_port_phy_qcfg_input *req;
12166 	u8 link_state = link_info->link_state;
12167 	bool support_changed;
12168 	int rc;
12169 
12170 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12171 	if (rc)
12172 		return rc;
12173 
12174 	resp = hwrm_req_hold(bp, req);
12175 	rc = hwrm_req_send(bp, req);
12176 	if (rc) {
12177 		hwrm_req_drop(bp, req);
12178 		if (BNXT_VF(bp) && rc == -ENODEV) {
12179 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12180 			rc = 0;
12181 		}
12182 		return rc;
12183 	}
12184 
12185 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12186 	link_info->phy_link_status = resp->link;
12187 	link_info->duplex = resp->duplex_cfg;
12188 	if (bp->hwrm_spec_code >= 0x10800)
12189 		link_info->duplex = resp->duplex_state;
12190 	link_info->pause = resp->pause;
12191 	link_info->auto_mode = resp->auto_mode;
12192 	link_info->auto_pause_setting = resp->auto_pause;
12193 	link_info->lp_pause = resp->link_partner_adv_pause;
12194 	link_info->force_pause_setting = resp->force_pause;
12195 	link_info->duplex_setting = resp->duplex_cfg;
12196 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12197 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12198 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12199 			link_info->active_lanes = resp->active_lanes;
12200 	} else {
12201 		link_info->link_speed = 0;
12202 		link_info->active_lanes = 0;
12203 	}
12204 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12205 	link_info->force_pam4_link_speed =
12206 		le16_to_cpu(resp->force_pam4_link_speed);
12207 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12208 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12209 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12210 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12211 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12212 	link_info->auto_pam4_link_speeds =
12213 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12214 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12215 	link_info->lp_auto_link_speeds =
12216 		le16_to_cpu(resp->link_partner_adv_speeds);
12217 	link_info->lp_auto_pam4_link_speeds =
12218 		resp->link_partner_pam4_adv_speeds;
12219 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12220 	link_info->phy_ver[0] = resp->phy_maj;
12221 	link_info->phy_ver[1] = resp->phy_min;
12222 	link_info->phy_ver[2] = resp->phy_bld;
12223 	link_info->media_type = resp->media_type;
12224 	link_info->phy_type = resp->phy_type;
12225 	link_info->transceiver = resp->xcvr_pkg_type;
12226 	link_info->phy_addr = resp->eee_config_phy_addr &
12227 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12228 	link_info->module_status = resp->module_status;
12229 	link_info->link_down_reason = resp->link_down_reason;
12230 
12231 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12232 		struct ethtool_keee *eee = &bp->eee;
12233 		u16 fw_speeds;
12234 
12235 		eee->eee_active = 0;
12236 		if (resp->eee_config_phy_addr &
12237 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12238 			eee->eee_active = 1;
12239 			fw_speeds = le16_to_cpu(
12240 				resp->link_partner_adv_eee_link_speed_mask);
12241 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12242 		}
12243 
12244 		/* Pull initial EEE config */
12245 		if (!chng_link_state) {
12246 			if (resp->eee_config_phy_addr &
12247 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12248 				eee->eee_enabled = 1;
12249 
12250 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12251 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12252 
12253 			if (resp->eee_config_phy_addr &
12254 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12255 				__le32 tmr;
12256 
12257 				eee->tx_lpi_enabled = 1;
12258 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12259 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12260 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12261 			}
12262 		}
12263 	}
12264 
12265 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12266 	if (bp->hwrm_spec_code >= 0x10504) {
12267 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12268 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12269 	}
12270 	/* TODO: need to add more logic to report VF link */
12271 	if (chng_link_state) {
12272 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12273 			link_info->link_state = BNXT_LINK_STATE_UP;
12274 		else
12275 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12276 		if (link_state != link_info->link_state)
12277 			bnxt_report_link(bp);
12278 	} else {
12279 		/* always link down if not require to update link state */
12280 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12281 	}
12282 	hwrm_req_drop(bp, req);
12283 
12284 	if (!BNXT_PHY_CFG_ABLE(bp))
12285 		return 0;
12286 
12287 	support_changed = bnxt_support_speed_dropped(link_info);
12288 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12289 		bnxt_hwrm_set_link_setting(bp, true, false);
12290 	return 0;
12291 }
12292 
12293 static void bnxt_get_port_module_status(struct bnxt *bp)
12294 {
12295 	struct bnxt_link_info *link_info = &bp->link_info;
12296 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12297 	u8 module_status;
12298 
12299 	if (bnxt_update_link(bp, true))
12300 		return;
12301 
12302 	module_status = link_info->module_status;
12303 	switch (module_status) {
12304 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12305 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12306 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12307 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12308 			    bp->pf.port_id);
12309 		if (bp->hwrm_spec_code >= 0x10201) {
12310 			netdev_warn(bp->dev, "Module part number %s\n",
12311 				    resp->phy_vendor_partnumber);
12312 		}
12313 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12314 			netdev_warn(bp->dev, "TX is disabled\n");
12315 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12316 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12317 	}
12318 }
12319 
12320 static void
12321 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12322 {
12323 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12324 		if (bp->hwrm_spec_code >= 0x10201)
12325 			req->auto_pause =
12326 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12327 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12328 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12329 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12330 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12331 		req->enables |=
12332 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12333 	} else {
12334 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12335 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12336 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12337 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12338 		req->enables |=
12339 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12340 		if (bp->hwrm_spec_code >= 0x10201) {
12341 			req->auto_pause = req->force_pause;
12342 			req->enables |= cpu_to_le32(
12343 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12344 		}
12345 	}
12346 }
12347 
12348 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12349 {
12350 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12351 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12352 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12353 			req->enables |=
12354 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12355 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12356 		} else if (bp->link_info.advertising) {
12357 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12358 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12359 		}
12360 		if (bp->link_info.advertising_pam4) {
12361 			req->enables |=
12362 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12363 			req->auto_link_pam4_speed_mask =
12364 				cpu_to_le16(bp->link_info.advertising_pam4);
12365 		}
12366 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12367 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12368 	} else {
12369 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12370 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12371 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12372 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12373 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12374 				   (u32)bp->link_info.req_link_speed);
12375 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12376 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12377 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12378 		} else {
12379 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12380 		}
12381 	}
12382 
12383 	/* tell chimp that the setting takes effect immediately */
12384 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12385 }
12386 
12387 int bnxt_hwrm_set_pause(struct bnxt *bp)
12388 {
12389 	struct hwrm_port_phy_cfg_input *req;
12390 	int rc;
12391 
12392 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12393 	if (rc)
12394 		return rc;
12395 
12396 	bnxt_hwrm_set_pause_common(bp, req);
12397 
12398 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12399 	    bp->link_info.force_link_chng)
12400 		bnxt_hwrm_set_link_common(bp, req);
12401 
12402 	rc = hwrm_req_send(bp, req);
12403 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12404 		/* since changing of pause setting doesn't trigger any link
12405 		 * change event, the driver needs to update the current pause
12406 		 * result upon successfully return of the phy_cfg command
12407 		 */
12408 		bp->link_info.pause =
12409 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12410 		bp->link_info.auto_pause_setting = 0;
12411 		if (!bp->link_info.force_link_chng)
12412 			bnxt_report_link(bp);
12413 	}
12414 	bp->link_info.force_link_chng = false;
12415 	return rc;
12416 }
12417 
12418 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12419 			      struct hwrm_port_phy_cfg_input *req)
12420 {
12421 	struct ethtool_keee *eee = &bp->eee;
12422 
12423 	if (eee->eee_enabled) {
12424 		u16 eee_speeds;
12425 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12426 
12427 		if (eee->tx_lpi_enabled)
12428 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12429 		else
12430 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12431 
12432 		req->flags |= cpu_to_le32(flags);
12433 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12434 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12435 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12436 	} else {
12437 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12438 	}
12439 }
12440 
12441 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12442 {
12443 	struct hwrm_port_phy_cfg_input *req;
12444 	int rc;
12445 
12446 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12447 	if (rc)
12448 		return rc;
12449 
12450 	if (set_pause)
12451 		bnxt_hwrm_set_pause_common(bp, req);
12452 
12453 	bnxt_hwrm_set_link_common(bp, req);
12454 
12455 	if (set_eee)
12456 		bnxt_hwrm_set_eee(bp, req);
12457 	return hwrm_req_send(bp, req);
12458 }
12459 
12460 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12461 {
12462 	struct hwrm_port_phy_cfg_input *req;
12463 	int rc;
12464 
12465 	if (!BNXT_SINGLE_PF(bp))
12466 		return 0;
12467 
12468 	if (pci_num_vf(bp->pdev) &&
12469 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12470 		return 0;
12471 
12472 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12473 	if (rc)
12474 		return rc;
12475 
12476 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12477 	rc = hwrm_req_send(bp, req);
12478 	if (!rc) {
12479 		mutex_lock(&bp->link_lock);
12480 		/* Device is not obliged link down in certain scenarios, even
12481 		 * when forced. Setting the state unknown is consistent with
12482 		 * driver startup and will force link state to be reported
12483 		 * during subsequent open based on PORT_PHY_QCFG.
12484 		 */
12485 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12486 		mutex_unlock(&bp->link_lock);
12487 	}
12488 	return rc;
12489 }
12490 
12491 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12492 {
12493 #ifdef CONFIG_TEE_BNXT_FW
12494 	int rc = tee_bnxt_fw_load();
12495 
12496 	if (rc)
12497 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12498 
12499 	return rc;
12500 #else
12501 	netdev_err(bp->dev, "OP-TEE not supported\n");
12502 	return -ENODEV;
12503 #endif
12504 }
12505 
12506 static int bnxt_try_recover_fw(struct bnxt *bp)
12507 {
12508 	if (bp->fw_health && bp->fw_health->status_reliable) {
12509 		int retry = 0, rc;
12510 		u32 sts;
12511 
12512 		do {
12513 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12514 			rc = bnxt_hwrm_poll(bp);
12515 			if (!BNXT_FW_IS_BOOTING(sts) &&
12516 			    !BNXT_FW_IS_RECOVERING(sts))
12517 				break;
12518 			retry++;
12519 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12520 
12521 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12522 			netdev_err(bp->dev,
12523 				   "Firmware not responding, status: 0x%x\n",
12524 				   sts);
12525 			rc = -ENODEV;
12526 		}
12527 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12528 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12529 			return bnxt_fw_reset_via_optee(bp);
12530 		}
12531 		return rc;
12532 	}
12533 
12534 	return -ENODEV;
12535 }
12536 
12537 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12538 {
12539 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12540 
12541 	if (!BNXT_NEW_RM(bp))
12542 		return; /* no resource reservations required */
12543 
12544 	hw_resc->resv_cp_rings = 0;
12545 	hw_resc->resv_stat_ctxs = 0;
12546 	hw_resc->resv_irqs = 0;
12547 	hw_resc->resv_tx_rings = 0;
12548 	hw_resc->resv_rx_rings = 0;
12549 	hw_resc->resv_hw_ring_grps = 0;
12550 	hw_resc->resv_vnics = 0;
12551 	hw_resc->resv_rsscos_ctxs = 0;
12552 	if (!fw_reset) {
12553 		bp->tx_nr_rings = 0;
12554 		bp->rx_nr_rings = 0;
12555 	}
12556 }
12557 
12558 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12559 {
12560 	int rc;
12561 
12562 	if (!BNXT_NEW_RM(bp))
12563 		return 0; /* no resource reservations required */
12564 
12565 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12566 	if (rc)
12567 		netdev_err(bp->dev, "resc_qcaps failed\n");
12568 
12569 	bnxt_clear_reservations(bp, fw_reset);
12570 
12571 	return rc;
12572 }
12573 
12574 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12575 {
12576 	struct hwrm_func_drv_if_change_output *resp;
12577 	struct hwrm_func_drv_if_change_input *req;
12578 	bool resc_reinit = false;
12579 	bool caps_change = false;
12580 	int rc, retry = 0;
12581 	bool fw_reset;
12582 	u32 flags = 0;
12583 
12584 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12585 	bp->fw_reset_state = 0;
12586 
12587 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12588 		return 0;
12589 
12590 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12591 	if (rc)
12592 		return rc;
12593 
12594 	if (up)
12595 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12596 	resp = hwrm_req_hold(bp, req);
12597 
12598 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12599 	while (retry < BNXT_FW_IF_RETRY) {
12600 		rc = hwrm_req_send(bp, req);
12601 		if (rc != -EAGAIN)
12602 			break;
12603 
12604 		msleep(50);
12605 		retry++;
12606 	}
12607 
12608 	if (rc == -EAGAIN) {
12609 		hwrm_req_drop(bp, req);
12610 		return rc;
12611 	} else if (!rc) {
12612 		flags = le32_to_cpu(resp->flags);
12613 	} else if (up) {
12614 		rc = bnxt_try_recover_fw(bp);
12615 		fw_reset = true;
12616 	}
12617 	hwrm_req_drop(bp, req);
12618 	if (rc)
12619 		return rc;
12620 
12621 	if (!up) {
12622 		bnxt_inv_fw_health_reg(bp);
12623 		return 0;
12624 	}
12625 
12626 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12627 		resc_reinit = true;
12628 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12629 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12630 		fw_reset = true;
12631 	else
12632 		bnxt_remap_fw_health_regs(bp);
12633 
12634 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12635 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12636 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12637 		return -ENODEV;
12638 	}
12639 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12640 		caps_change = true;
12641 
12642 	if (resc_reinit || fw_reset || caps_change) {
12643 		if (fw_reset || caps_change) {
12644 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12645 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12646 				bnxt_ulp_irq_stop(bp);
12647 			bnxt_free_ctx_mem(bp, false);
12648 			bnxt_dcb_free(bp);
12649 			rc = bnxt_fw_init_one(bp);
12650 			if (rc) {
12651 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12652 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12653 				return rc;
12654 			}
12655 			/* IRQ will be initialized later in bnxt_request_irq()*/
12656 			bnxt_clear_int_mode(bp);
12657 		}
12658 		rc = bnxt_cancel_reservations(bp, fw_reset);
12659 	}
12660 	return rc;
12661 }
12662 
12663 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12664 {
12665 	struct hwrm_port_led_qcaps_output *resp;
12666 	struct hwrm_port_led_qcaps_input *req;
12667 	struct bnxt_pf_info *pf = &bp->pf;
12668 	int rc;
12669 
12670 	bp->num_leds = 0;
12671 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12672 		return 0;
12673 
12674 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12675 	if (rc)
12676 		return rc;
12677 
12678 	req->port_id = cpu_to_le16(pf->port_id);
12679 	resp = hwrm_req_hold(bp, req);
12680 	rc = hwrm_req_send(bp, req);
12681 	if (rc) {
12682 		hwrm_req_drop(bp, req);
12683 		return rc;
12684 	}
12685 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12686 		int i;
12687 
12688 		bp->num_leds = resp->num_leds;
12689 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12690 						 bp->num_leds);
12691 		for (i = 0; i < bp->num_leds; i++) {
12692 			struct bnxt_led_info *led = &bp->leds[i];
12693 			__le16 caps = led->led_state_caps;
12694 
12695 			if (!led->led_group_id ||
12696 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12697 				bp->num_leds = 0;
12698 				break;
12699 			}
12700 		}
12701 	}
12702 	hwrm_req_drop(bp, req);
12703 	return 0;
12704 }
12705 
12706 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12707 {
12708 	struct hwrm_wol_filter_alloc_output *resp;
12709 	struct hwrm_wol_filter_alloc_input *req;
12710 	int rc;
12711 
12712 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12713 	if (rc)
12714 		return rc;
12715 
12716 	req->port_id = cpu_to_le16(bp->pf.port_id);
12717 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12718 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12719 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12720 
12721 	resp = hwrm_req_hold(bp, req);
12722 	rc = hwrm_req_send(bp, req);
12723 	if (!rc)
12724 		bp->wol_filter_id = resp->wol_filter_id;
12725 	hwrm_req_drop(bp, req);
12726 	return rc;
12727 }
12728 
12729 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12730 {
12731 	struct hwrm_wol_filter_free_input *req;
12732 	int rc;
12733 
12734 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12735 	if (rc)
12736 		return rc;
12737 
12738 	req->port_id = cpu_to_le16(bp->pf.port_id);
12739 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12740 	req->wol_filter_id = bp->wol_filter_id;
12741 
12742 	return hwrm_req_send(bp, req);
12743 }
12744 
12745 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12746 {
12747 	struct hwrm_wol_filter_qcfg_output *resp;
12748 	struct hwrm_wol_filter_qcfg_input *req;
12749 	u16 next_handle = 0;
12750 	int rc;
12751 
12752 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12753 	if (rc)
12754 		return rc;
12755 
12756 	req->port_id = cpu_to_le16(bp->pf.port_id);
12757 	req->handle = cpu_to_le16(handle);
12758 	resp = hwrm_req_hold(bp, req);
12759 	rc = hwrm_req_send(bp, req);
12760 	if (!rc) {
12761 		next_handle = le16_to_cpu(resp->next_handle);
12762 		if (next_handle != 0) {
12763 			if (resp->wol_type ==
12764 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12765 				bp->wol = 1;
12766 				bp->wol_filter_id = resp->wol_filter_id;
12767 			}
12768 		}
12769 	}
12770 	hwrm_req_drop(bp, req);
12771 	return next_handle;
12772 }
12773 
12774 static void bnxt_get_wol_settings(struct bnxt *bp)
12775 {
12776 	u16 handle = 0;
12777 
12778 	bp->wol = 0;
12779 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12780 		return;
12781 
12782 	do {
12783 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12784 	} while (handle && handle != 0xffff);
12785 }
12786 
12787 static bool bnxt_eee_config_ok(struct bnxt *bp)
12788 {
12789 	struct ethtool_keee *eee = &bp->eee;
12790 	struct bnxt_link_info *link_info = &bp->link_info;
12791 
12792 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12793 		return true;
12794 
12795 	if (eee->eee_enabled) {
12796 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12797 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12798 
12799 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12800 
12801 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12802 			eee->eee_enabled = 0;
12803 			return false;
12804 		}
12805 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12806 			linkmode_and(eee->advertised, advertising,
12807 				     eee->supported);
12808 			return false;
12809 		}
12810 	}
12811 	return true;
12812 }
12813 
12814 static int bnxt_update_phy_setting(struct bnxt *bp)
12815 {
12816 	int rc;
12817 	bool update_link = false;
12818 	bool update_pause = false;
12819 	bool update_eee = false;
12820 	struct bnxt_link_info *link_info = &bp->link_info;
12821 
12822 	rc = bnxt_update_link(bp, true);
12823 	if (rc) {
12824 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12825 			   rc);
12826 		return rc;
12827 	}
12828 	if (!BNXT_SINGLE_PF(bp))
12829 		return 0;
12830 
12831 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12832 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12833 	    link_info->req_flow_ctrl)
12834 		update_pause = true;
12835 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12836 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12837 		update_pause = true;
12838 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12839 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12840 			update_link = true;
12841 		if (bnxt_force_speed_updated(link_info))
12842 			update_link = true;
12843 		if (link_info->req_duplex != link_info->duplex_setting)
12844 			update_link = true;
12845 	} else {
12846 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12847 			update_link = true;
12848 		if (bnxt_auto_speed_updated(link_info))
12849 			update_link = true;
12850 	}
12851 
12852 	/* The last close may have shutdown the link, so need to call
12853 	 * PHY_CFG to bring it back up.
12854 	 */
12855 	if (!BNXT_LINK_IS_UP(bp))
12856 		update_link = true;
12857 
12858 	if (!bnxt_eee_config_ok(bp))
12859 		update_eee = true;
12860 
12861 	if (update_link)
12862 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12863 	else if (update_pause)
12864 		rc = bnxt_hwrm_set_pause(bp);
12865 	if (rc) {
12866 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12867 			   rc);
12868 		return rc;
12869 	}
12870 
12871 	return rc;
12872 }
12873 
12874 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12875 
12876 static int bnxt_reinit_after_abort(struct bnxt *bp)
12877 {
12878 	int rc;
12879 
12880 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12881 		return -EBUSY;
12882 
12883 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12884 		return -ENODEV;
12885 
12886 	rc = bnxt_fw_init_one(bp);
12887 	if (!rc) {
12888 		bnxt_clear_int_mode(bp);
12889 		rc = bnxt_init_int_mode(bp);
12890 		if (!rc) {
12891 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12892 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12893 		}
12894 	}
12895 	return rc;
12896 }
12897 
12898 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12899 {
12900 	struct bnxt_ntuple_filter *ntp_fltr;
12901 	struct bnxt_l2_filter *l2_fltr;
12902 
12903 	if (list_empty(&fltr->list))
12904 		return;
12905 
12906 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12907 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12908 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12909 		atomic_inc(&l2_fltr->refcnt);
12910 		ntp_fltr->l2_fltr = l2_fltr;
12911 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12912 			bnxt_del_ntp_filter(bp, ntp_fltr);
12913 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12914 				   fltr->sw_id);
12915 		}
12916 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12917 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12918 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12919 			bnxt_del_l2_filter(bp, l2_fltr);
12920 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12921 				   fltr->sw_id);
12922 		}
12923 	}
12924 }
12925 
12926 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12927 {
12928 	struct bnxt_filter_base *usr_fltr, *tmp;
12929 
12930 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12931 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12932 }
12933 
12934 static int bnxt_set_xps_mapping(struct bnxt *bp)
12935 {
12936 	int numa_node = dev_to_node(&bp->pdev->dev);
12937 	unsigned int q_idx, map_idx, cpu, i;
12938 	const struct cpumask *cpu_mask_ptr;
12939 	int nr_cpus = num_online_cpus();
12940 	cpumask_t *q_map;
12941 	int rc = 0;
12942 
12943 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12944 	if (!q_map)
12945 		return -ENOMEM;
12946 
12947 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12948 	 * Each TC has the same number of TX queues. The nth TX queue for each
12949 	 * TC will have the same CPU mask.
12950 	 */
12951 	for (i = 0; i < nr_cpus; i++) {
12952 		map_idx = i % bp->tx_nr_rings_per_tc;
12953 		cpu = cpumask_local_spread(i, numa_node);
12954 		cpu_mask_ptr = get_cpu_mask(cpu);
12955 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12956 	}
12957 
12958 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12959 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12960 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12961 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12962 		if (rc) {
12963 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12964 				    q_idx);
12965 			break;
12966 		}
12967 	}
12968 
12969 	kfree(q_map);
12970 
12971 	return rc;
12972 }
12973 
12974 static int bnxt_tx_nr_rings(struct bnxt *bp)
12975 {
12976 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
12977 			    bp->tx_nr_rings_per_tc;
12978 }
12979 
12980 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
12981 {
12982 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
12983 }
12984 
12985 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12986 {
12987 	int rc = 0;
12988 
12989 	netif_carrier_off(bp->dev);
12990 	if (irq_re_init) {
12991 		/* Reserve rings now if none were reserved at driver probe. */
12992 		rc = bnxt_init_dflt_ring_mode(bp);
12993 		if (rc) {
12994 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12995 			return rc;
12996 		}
12997 	}
12998 	rc = bnxt_reserve_rings(bp, irq_re_init);
12999 	if (rc)
13000 		return rc;
13001 
13002 	/* Make adjustments if reserved TX rings are less than requested */
13003 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13004 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13005 	if (bp->tx_nr_rings_xdp) {
13006 		bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13007 		bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13008 	}
13009 	rc = bnxt_alloc_mem(bp, irq_re_init);
13010 	if (rc) {
13011 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13012 		goto open_err_free_mem;
13013 	}
13014 
13015 	if (irq_re_init) {
13016 		bnxt_init_napi(bp);
13017 		rc = bnxt_request_irq(bp);
13018 		if (rc) {
13019 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13020 			goto open_err_irq;
13021 		}
13022 	}
13023 
13024 	rc = bnxt_init_nic(bp, irq_re_init);
13025 	if (rc) {
13026 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13027 		goto open_err_irq;
13028 	}
13029 
13030 	bnxt_enable_napi(bp);
13031 	bnxt_debug_dev_init(bp);
13032 
13033 	if (link_re_init) {
13034 		mutex_lock(&bp->link_lock);
13035 		rc = bnxt_update_phy_setting(bp);
13036 		mutex_unlock(&bp->link_lock);
13037 		if (rc) {
13038 			netdev_warn(bp->dev, "failed to update phy settings\n");
13039 			if (BNXT_SINGLE_PF(bp)) {
13040 				bp->link_info.phy_retry = true;
13041 				bp->link_info.phy_retry_expires =
13042 					jiffies + 5 * HZ;
13043 			}
13044 		}
13045 	}
13046 
13047 	if (irq_re_init) {
13048 		udp_tunnel_nic_reset_ntf(bp->dev);
13049 		rc = bnxt_set_xps_mapping(bp);
13050 		if (rc)
13051 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13052 	}
13053 
13054 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13055 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13056 			static_branch_enable(&bnxt_xdp_locking_key);
13057 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13058 		static_branch_disable(&bnxt_xdp_locking_key);
13059 	}
13060 	set_bit(BNXT_STATE_OPEN, &bp->state);
13061 	bnxt_enable_int(bp);
13062 	/* Enable TX queues */
13063 	bnxt_tx_enable(bp);
13064 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13065 	/* Poll link status and check for SFP+ module status */
13066 	mutex_lock(&bp->link_lock);
13067 	bnxt_get_port_module_status(bp);
13068 	mutex_unlock(&bp->link_lock);
13069 
13070 	/* VF-reps may need to be re-opened after the PF is re-opened */
13071 	if (BNXT_PF(bp))
13072 		bnxt_vf_reps_open(bp);
13073 	bnxt_ptp_init_rtc(bp, true);
13074 	bnxt_ptp_cfg_tstamp_filters(bp);
13075 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13076 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13077 	bnxt_cfg_usr_fltrs(bp);
13078 	return 0;
13079 
13080 open_err_irq:
13081 	bnxt_del_napi(bp);
13082 
13083 open_err_free_mem:
13084 	bnxt_free_skbs(bp);
13085 	bnxt_free_irq(bp);
13086 	bnxt_free_mem(bp, true);
13087 	return rc;
13088 }
13089 
13090 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13091 {
13092 	int rc = 0;
13093 
13094 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13095 		rc = -EIO;
13096 	if (!rc)
13097 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13098 	if (rc) {
13099 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13100 		netif_close(bp->dev);
13101 	}
13102 	return rc;
13103 }
13104 
13105 /* netdev instance lock held, open the NIC half way by allocating all
13106  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13107  * for offline self tests.
13108  */
13109 int bnxt_half_open_nic(struct bnxt *bp)
13110 {
13111 	int rc = 0;
13112 
13113 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13114 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13115 		rc = -ENODEV;
13116 		goto half_open_err;
13117 	}
13118 
13119 	rc = bnxt_alloc_mem(bp, true);
13120 	if (rc) {
13121 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13122 		goto half_open_err;
13123 	}
13124 	bnxt_init_napi(bp);
13125 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13126 	rc = bnxt_init_nic(bp, true);
13127 	if (rc) {
13128 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13129 		bnxt_del_napi(bp);
13130 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13131 		goto half_open_err;
13132 	}
13133 	return 0;
13134 
13135 half_open_err:
13136 	bnxt_free_skbs(bp);
13137 	bnxt_free_mem(bp, true);
13138 	netif_close(bp->dev);
13139 	return rc;
13140 }
13141 
13142 /* netdev instance lock held, this call can only be made after a previous
13143  * successful call to bnxt_half_open_nic().
13144  */
13145 void bnxt_half_close_nic(struct bnxt *bp)
13146 {
13147 	bnxt_hwrm_resource_free(bp, false, true);
13148 	bnxt_del_napi(bp);
13149 	bnxt_free_skbs(bp);
13150 	bnxt_free_mem(bp, true);
13151 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13152 }
13153 
13154 void bnxt_reenable_sriov(struct bnxt *bp)
13155 {
13156 	if (BNXT_PF(bp)) {
13157 		struct bnxt_pf_info *pf = &bp->pf;
13158 		int n = pf->active_vfs;
13159 
13160 		if (n)
13161 			bnxt_cfg_hw_sriov(bp, &n, true);
13162 	}
13163 }
13164 
13165 static int bnxt_open(struct net_device *dev)
13166 {
13167 	struct bnxt *bp = netdev_priv(dev);
13168 	int rc;
13169 
13170 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13171 		rc = bnxt_reinit_after_abort(bp);
13172 		if (rc) {
13173 			if (rc == -EBUSY)
13174 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13175 			else
13176 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13177 			return -ENODEV;
13178 		}
13179 	}
13180 
13181 	rc = bnxt_hwrm_if_change(bp, true);
13182 	if (rc)
13183 		return rc;
13184 
13185 	rc = __bnxt_open_nic(bp, true, true);
13186 	if (rc) {
13187 		bnxt_hwrm_if_change(bp, false);
13188 	} else {
13189 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13190 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13191 				bnxt_queue_sp_work(bp,
13192 						   BNXT_RESTART_ULP_SP_EVENT);
13193 		}
13194 	}
13195 
13196 	return rc;
13197 }
13198 
13199 static bool bnxt_drv_busy(struct bnxt *bp)
13200 {
13201 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13202 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13203 }
13204 
13205 static void bnxt_get_ring_stats(struct bnxt *bp,
13206 				struct rtnl_link_stats64 *stats);
13207 
13208 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13209 			     bool link_re_init)
13210 {
13211 	/* Close the VF-reps before closing PF */
13212 	if (BNXT_PF(bp))
13213 		bnxt_vf_reps_close(bp);
13214 
13215 	/* Change device state to avoid TX queue wake up's */
13216 	bnxt_tx_disable(bp);
13217 
13218 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13219 	smp_mb__after_atomic();
13220 	while (bnxt_drv_busy(bp))
13221 		msleep(20);
13222 
13223 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13224 		bnxt_clear_rss_ctxs(bp);
13225 	/* Flush rings and disable interrupts */
13226 	bnxt_shutdown_nic(bp, irq_re_init);
13227 
13228 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13229 
13230 	bnxt_debug_dev_exit(bp);
13231 	bnxt_disable_napi(bp);
13232 	timer_delete_sync(&bp->timer);
13233 	bnxt_free_skbs(bp);
13234 
13235 	/* Save ring stats before shutdown */
13236 	if (bp->bnapi && irq_re_init) {
13237 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13238 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13239 	}
13240 	if (irq_re_init) {
13241 		bnxt_free_irq(bp);
13242 		bnxt_del_napi(bp);
13243 	}
13244 	bnxt_free_mem(bp, irq_re_init);
13245 }
13246 
13247 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13248 {
13249 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13250 		/* If we get here, it means firmware reset is in progress
13251 		 * while we are trying to close.  We can safely proceed with
13252 		 * the close because we are holding netdev instance lock.
13253 		 * Some firmware messages may fail as we proceed to close.
13254 		 * We set the ABORT_ERR flag here so that the FW reset thread
13255 		 * will later abort when it gets the netdev instance lock
13256 		 * and sees the flag.
13257 		 */
13258 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13259 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13260 	}
13261 
13262 #ifdef CONFIG_BNXT_SRIOV
13263 	if (bp->sriov_cfg) {
13264 		int rc;
13265 
13266 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13267 						      !bp->sriov_cfg,
13268 						      BNXT_SRIOV_CFG_WAIT_TMO);
13269 		if (!rc)
13270 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13271 		else if (rc < 0)
13272 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13273 	}
13274 #endif
13275 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13276 }
13277 
13278 static int bnxt_close(struct net_device *dev)
13279 {
13280 	struct bnxt *bp = netdev_priv(dev);
13281 
13282 	bnxt_close_nic(bp, true, true);
13283 	bnxt_hwrm_shutdown_link(bp);
13284 	bnxt_hwrm_if_change(bp, false);
13285 	return 0;
13286 }
13287 
13288 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13289 				   u16 *val)
13290 {
13291 	struct hwrm_port_phy_mdio_read_output *resp;
13292 	struct hwrm_port_phy_mdio_read_input *req;
13293 	int rc;
13294 
13295 	if (bp->hwrm_spec_code < 0x10a00)
13296 		return -EOPNOTSUPP;
13297 
13298 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13299 	if (rc)
13300 		return rc;
13301 
13302 	req->port_id = cpu_to_le16(bp->pf.port_id);
13303 	req->phy_addr = phy_addr;
13304 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13305 	if (mdio_phy_id_is_c45(phy_addr)) {
13306 		req->cl45_mdio = 1;
13307 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13308 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13309 		req->reg_addr = cpu_to_le16(reg);
13310 	}
13311 
13312 	resp = hwrm_req_hold(bp, req);
13313 	rc = hwrm_req_send(bp, req);
13314 	if (!rc)
13315 		*val = le16_to_cpu(resp->reg_data);
13316 	hwrm_req_drop(bp, req);
13317 	return rc;
13318 }
13319 
13320 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13321 				    u16 val)
13322 {
13323 	struct hwrm_port_phy_mdio_write_input *req;
13324 	int rc;
13325 
13326 	if (bp->hwrm_spec_code < 0x10a00)
13327 		return -EOPNOTSUPP;
13328 
13329 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13330 	if (rc)
13331 		return rc;
13332 
13333 	req->port_id = cpu_to_le16(bp->pf.port_id);
13334 	req->phy_addr = phy_addr;
13335 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13336 	if (mdio_phy_id_is_c45(phy_addr)) {
13337 		req->cl45_mdio = 1;
13338 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13339 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13340 		req->reg_addr = cpu_to_le16(reg);
13341 	}
13342 	req->reg_data = cpu_to_le16(val);
13343 
13344 	return hwrm_req_send(bp, req);
13345 }
13346 
13347 /* netdev instance lock held */
13348 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13349 {
13350 	struct mii_ioctl_data *mdio = if_mii(ifr);
13351 	struct bnxt *bp = netdev_priv(dev);
13352 	int rc;
13353 
13354 	switch (cmd) {
13355 	case SIOCGMIIPHY:
13356 		mdio->phy_id = bp->link_info.phy_addr;
13357 
13358 		fallthrough;
13359 	case SIOCGMIIREG: {
13360 		u16 mii_regval = 0;
13361 
13362 		if (!netif_running(dev))
13363 			return -EAGAIN;
13364 
13365 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13366 					     &mii_regval);
13367 		mdio->val_out = mii_regval;
13368 		return rc;
13369 	}
13370 
13371 	case SIOCSMIIREG:
13372 		if (!netif_running(dev))
13373 			return -EAGAIN;
13374 
13375 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13376 						mdio->val_in);
13377 
13378 	default:
13379 		/* do nothing */
13380 		break;
13381 	}
13382 	return -EOPNOTSUPP;
13383 }
13384 
13385 static void bnxt_get_ring_stats(struct bnxt *bp,
13386 				struct rtnl_link_stats64 *stats)
13387 {
13388 	int i;
13389 
13390 	for (i = 0; i < bp->cp_nr_rings; i++) {
13391 		struct bnxt_napi *bnapi = bp->bnapi[i];
13392 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13393 		u64 *sw = cpr->stats.sw_stats;
13394 
13395 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13396 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13397 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13398 
13399 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13400 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13401 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13402 
13403 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13404 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13405 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13406 
13407 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13408 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13409 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13410 
13411 		stats->rx_missed_errors +=
13412 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13413 
13414 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13415 
13416 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13417 
13418 		stats->rx_dropped +=
13419 			cpr->sw_stats->rx.rx_netpoll_discards +
13420 			cpr->sw_stats->rx.rx_oom_discards;
13421 	}
13422 }
13423 
13424 static void bnxt_add_prev_stats(struct bnxt *bp,
13425 				struct rtnl_link_stats64 *stats)
13426 {
13427 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13428 
13429 	stats->rx_packets += prev_stats->rx_packets;
13430 	stats->tx_packets += prev_stats->tx_packets;
13431 	stats->rx_bytes += prev_stats->rx_bytes;
13432 	stats->tx_bytes += prev_stats->tx_bytes;
13433 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13434 	stats->multicast += prev_stats->multicast;
13435 	stats->rx_dropped += prev_stats->rx_dropped;
13436 	stats->tx_dropped += prev_stats->tx_dropped;
13437 }
13438 
13439 static void
13440 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13441 {
13442 	struct bnxt *bp = netdev_priv(dev);
13443 
13444 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13445 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13446 	 * we check the BNXT_STATE_OPEN flag.
13447 	 */
13448 	smp_mb__after_atomic();
13449 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13450 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13451 		*stats = bp->net_stats_prev;
13452 		return;
13453 	}
13454 
13455 	bnxt_get_ring_stats(bp, stats);
13456 	bnxt_add_prev_stats(bp, stats);
13457 
13458 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13459 		u64 *rx = bp->port_stats.sw_stats;
13460 		u64 *tx = bp->port_stats.sw_stats +
13461 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13462 
13463 		stats->rx_crc_errors =
13464 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13465 		stats->rx_frame_errors =
13466 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13467 		stats->rx_length_errors =
13468 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13469 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13470 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13471 		stats->rx_errors =
13472 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13473 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13474 		stats->collisions =
13475 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13476 		stats->tx_fifo_errors =
13477 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13478 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13479 	}
13480 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13481 }
13482 
13483 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13484 					struct bnxt_total_ring_err_stats *stats,
13485 					struct bnxt_cp_ring_info *cpr)
13486 {
13487 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13488 	u64 *hw_stats = cpr->stats.sw_stats;
13489 
13490 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13491 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13492 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13493 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13494 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13495 	stats->rx_total_ring_discards +=
13496 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13497 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13498 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13499 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13500 	stats->tx_total_ring_discards +=
13501 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13502 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13503 }
13504 
13505 void bnxt_get_ring_err_stats(struct bnxt *bp,
13506 			     struct bnxt_total_ring_err_stats *stats)
13507 {
13508 	int i;
13509 
13510 	for (i = 0; i < bp->cp_nr_rings; i++)
13511 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13512 }
13513 
13514 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13515 {
13516 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13517 	struct net_device *dev = bp->dev;
13518 	struct netdev_hw_addr *ha;
13519 	u8 *haddr;
13520 	int mc_count = 0;
13521 	bool update = false;
13522 	int off = 0;
13523 
13524 	netdev_for_each_mc_addr(ha, dev) {
13525 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13526 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13527 			vnic->mc_list_count = 0;
13528 			return false;
13529 		}
13530 		haddr = ha->addr;
13531 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13532 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13533 			update = true;
13534 		}
13535 		off += ETH_ALEN;
13536 		mc_count++;
13537 	}
13538 	if (mc_count)
13539 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13540 
13541 	if (mc_count != vnic->mc_list_count) {
13542 		vnic->mc_list_count = mc_count;
13543 		update = true;
13544 	}
13545 	return update;
13546 }
13547 
13548 static bool bnxt_uc_list_updated(struct bnxt *bp)
13549 {
13550 	struct net_device *dev = bp->dev;
13551 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13552 	struct netdev_hw_addr *ha;
13553 	int off = 0;
13554 
13555 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13556 		return true;
13557 
13558 	netdev_for_each_uc_addr(ha, dev) {
13559 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13560 			return true;
13561 
13562 		off += ETH_ALEN;
13563 	}
13564 	return false;
13565 }
13566 
13567 static void bnxt_set_rx_mode(struct net_device *dev)
13568 {
13569 	struct bnxt *bp = netdev_priv(dev);
13570 	struct bnxt_vnic_info *vnic;
13571 	bool mc_update = false;
13572 	bool uc_update;
13573 	u32 mask;
13574 
13575 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13576 		return;
13577 
13578 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13579 	mask = vnic->rx_mask;
13580 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13581 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13582 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13583 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13584 
13585 	if (dev->flags & IFF_PROMISC)
13586 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13587 
13588 	uc_update = bnxt_uc_list_updated(bp);
13589 
13590 	if (dev->flags & IFF_BROADCAST)
13591 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13592 	if (dev->flags & IFF_ALLMULTI) {
13593 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13594 		vnic->mc_list_count = 0;
13595 	} else if (dev->flags & IFF_MULTICAST) {
13596 		mc_update = bnxt_mc_list_updated(bp, &mask);
13597 	}
13598 
13599 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13600 		vnic->rx_mask = mask;
13601 
13602 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13603 	}
13604 }
13605 
13606 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13607 {
13608 	struct net_device *dev = bp->dev;
13609 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13610 	struct netdev_hw_addr *ha;
13611 	int i, off = 0, rc;
13612 	bool uc_update;
13613 
13614 	netif_addr_lock_bh(dev);
13615 	uc_update = bnxt_uc_list_updated(bp);
13616 	netif_addr_unlock_bh(dev);
13617 
13618 	if (!uc_update)
13619 		goto skip_uc;
13620 
13621 	for (i = 1; i < vnic->uc_filter_count; i++) {
13622 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13623 
13624 		bnxt_hwrm_l2_filter_free(bp, fltr);
13625 		bnxt_del_l2_filter(bp, fltr);
13626 	}
13627 
13628 	vnic->uc_filter_count = 1;
13629 
13630 	netif_addr_lock_bh(dev);
13631 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13632 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13633 	} else {
13634 		netdev_for_each_uc_addr(ha, dev) {
13635 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13636 			off += ETH_ALEN;
13637 			vnic->uc_filter_count++;
13638 		}
13639 	}
13640 	netif_addr_unlock_bh(dev);
13641 
13642 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13643 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13644 		if (rc) {
13645 			if (BNXT_VF(bp) && rc == -ENODEV) {
13646 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13647 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13648 				else
13649 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13650 				rc = 0;
13651 			} else {
13652 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13653 			}
13654 			vnic->uc_filter_count = i;
13655 			return rc;
13656 		}
13657 	}
13658 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13659 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13660 
13661 skip_uc:
13662 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13663 	    !bnxt_promisc_ok(bp))
13664 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13665 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13666 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13667 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13668 			    rc);
13669 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13670 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13671 		vnic->mc_list_count = 0;
13672 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13673 	}
13674 	if (rc)
13675 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13676 			   rc);
13677 
13678 	return rc;
13679 }
13680 
13681 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13682 {
13683 #ifdef CONFIG_BNXT_SRIOV
13684 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13685 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13686 
13687 		/* No minimum rings were provisioned by the PF.  Don't
13688 		 * reserve rings by default when device is down.
13689 		 */
13690 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13691 			return true;
13692 
13693 		if (!netif_running(bp->dev))
13694 			return false;
13695 	}
13696 #endif
13697 	return true;
13698 }
13699 
13700 /* If the chip and firmware supports RFS */
13701 static bool bnxt_rfs_supported(struct bnxt *bp)
13702 {
13703 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13704 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13705 			return true;
13706 		return false;
13707 	}
13708 	/* 212 firmware is broken for aRFS */
13709 	if (BNXT_FW_MAJ(bp) == 212)
13710 		return false;
13711 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13712 		return true;
13713 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13714 		return true;
13715 	return false;
13716 }
13717 
13718 /* If runtime conditions support RFS */
13719 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13720 {
13721 	struct bnxt_hw_rings hwr = {0};
13722 	int max_vnics, max_rss_ctxs;
13723 
13724 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13725 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13726 		return bnxt_rfs_supported(bp);
13727 
13728 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13729 		return false;
13730 
13731 	hwr.grp = bp->rx_nr_rings;
13732 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13733 	if (new_rss_ctx)
13734 		hwr.vnic++;
13735 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13736 	max_vnics = bnxt_get_max_func_vnics(bp);
13737 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13738 
13739 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13740 		if (bp->rx_nr_rings > 1)
13741 			netdev_warn(bp->dev,
13742 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13743 				    min(max_rss_ctxs - 1, max_vnics - 1));
13744 		return false;
13745 	}
13746 
13747 	if (!BNXT_NEW_RM(bp))
13748 		return true;
13749 
13750 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13751 	 * issue that will mess up the default VNIC if we reduce the
13752 	 * reservations.
13753 	 */
13754 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13755 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13756 		return true;
13757 
13758 	bnxt_hwrm_reserve_rings(bp, &hwr);
13759 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13760 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13761 		return true;
13762 
13763 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13764 	hwr.vnic = 1;
13765 	hwr.rss_ctx = 0;
13766 	bnxt_hwrm_reserve_rings(bp, &hwr);
13767 	return false;
13768 }
13769 
13770 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13771 					   netdev_features_t features)
13772 {
13773 	struct bnxt *bp = netdev_priv(dev);
13774 	netdev_features_t vlan_features;
13775 
13776 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13777 		features &= ~NETIF_F_NTUPLE;
13778 
13779 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13780 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13781 
13782 	if (!(features & NETIF_F_GRO))
13783 		features &= ~NETIF_F_GRO_HW;
13784 
13785 	if (features & NETIF_F_GRO_HW)
13786 		features &= ~NETIF_F_LRO;
13787 
13788 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13789 	 * turned on or off together.
13790 	 */
13791 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13792 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13793 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13794 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13795 		else if (vlan_features)
13796 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13797 	}
13798 #ifdef CONFIG_BNXT_SRIOV
13799 	if (BNXT_VF(bp) && bp->vf.vlan)
13800 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13801 #endif
13802 	return features;
13803 }
13804 
13805 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13806 				bool link_re_init, u32 flags, bool update_tpa)
13807 {
13808 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13809 	bp->flags = flags;
13810 	if (update_tpa)
13811 		bnxt_set_ring_params(bp);
13812 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13813 }
13814 
13815 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13816 {
13817 	bool update_tpa = false, update_ntuple = false;
13818 	struct bnxt *bp = netdev_priv(dev);
13819 	u32 flags = bp->flags;
13820 	u32 changes;
13821 	int rc = 0;
13822 	bool re_init = false;
13823 
13824 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13825 	if (features & NETIF_F_GRO_HW)
13826 		flags |= BNXT_FLAG_GRO;
13827 	else if (features & NETIF_F_LRO)
13828 		flags |= BNXT_FLAG_LRO;
13829 
13830 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13831 		flags &= ~BNXT_FLAG_TPA;
13832 
13833 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13834 		flags |= BNXT_FLAG_STRIP_VLAN;
13835 
13836 	if (features & NETIF_F_NTUPLE)
13837 		flags |= BNXT_FLAG_RFS;
13838 	else
13839 		bnxt_clear_usr_fltrs(bp, true);
13840 
13841 	changes = flags ^ bp->flags;
13842 	if (changes & BNXT_FLAG_TPA) {
13843 		update_tpa = true;
13844 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13845 		    (flags & BNXT_FLAG_TPA) == 0 ||
13846 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13847 			re_init = true;
13848 	}
13849 
13850 	if (changes & ~BNXT_FLAG_TPA)
13851 		re_init = true;
13852 
13853 	if (changes & BNXT_FLAG_RFS)
13854 		update_ntuple = true;
13855 
13856 	if (flags != bp->flags) {
13857 		u32 old_flags = bp->flags;
13858 
13859 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13860 			bp->flags = flags;
13861 			if (update_tpa)
13862 				bnxt_set_ring_params(bp);
13863 			return rc;
13864 		}
13865 
13866 		if (update_ntuple)
13867 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13868 
13869 		if (re_init)
13870 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13871 
13872 		if (update_tpa) {
13873 			bp->flags = flags;
13874 			rc = bnxt_set_tpa(bp,
13875 					  (flags & BNXT_FLAG_TPA) ?
13876 					  true : false);
13877 			if (rc)
13878 				bp->flags = old_flags;
13879 		}
13880 	}
13881 	return rc;
13882 }
13883 
13884 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13885 			      u8 **nextp)
13886 {
13887 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13888 	int hdr_count = 0;
13889 	u8 *nexthdr;
13890 	int start;
13891 
13892 	/* Check that there are at most 2 IPv6 extension headers, no
13893 	 * fragment header, and each is <= 64 bytes.
13894 	 */
13895 	start = nw_off + sizeof(*ip6h);
13896 	nexthdr = &ip6h->nexthdr;
13897 	while (ipv6_ext_hdr(*nexthdr)) {
13898 		struct ipv6_opt_hdr *hp;
13899 		int hdrlen;
13900 
13901 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13902 		    *nexthdr == NEXTHDR_FRAGMENT)
13903 			return false;
13904 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13905 					  skb_headlen(skb), NULL);
13906 		if (!hp)
13907 			return false;
13908 		if (*nexthdr == NEXTHDR_AUTH)
13909 			hdrlen = ipv6_authlen(hp);
13910 		else
13911 			hdrlen = ipv6_optlen(hp);
13912 
13913 		if (hdrlen > 64)
13914 			return false;
13915 
13916 		hdr_count++;
13917 		nexthdr = &hp->nexthdr;
13918 		start += hdrlen;
13919 	}
13920 	if (nextp) {
13921 		/* Caller will check inner protocol */
13922 		if (skb->encapsulation) {
13923 			*nextp = nexthdr;
13924 			return true;
13925 		}
13926 		*nextp = NULL;
13927 	}
13928 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13929 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13930 }
13931 
13932 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13933 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13934 {
13935 	struct udphdr *uh = udp_hdr(skb);
13936 	__be16 udp_port = uh->dest;
13937 
13938 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13939 	    udp_port != bp->vxlan_gpe_port)
13940 		return false;
13941 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13942 		struct ethhdr *eh = inner_eth_hdr(skb);
13943 
13944 		switch (eh->h_proto) {
13945 		case htons(ETH_P_IP):
13946 			return true;
13947 		case htons(ETH_P_IPV6):
13948 			return bnxt_exthdr_check(bp, skb,
13949 						 skb_inner_network_offset(skb),
13950 						 NULL);
13951 		}
13952 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13953 		return true;
13954 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13955 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13956 					 NULL);
13957 	}
13958 	return false;
13959 }
13960 
13961 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13962 {
13963 	switch (l4_proto) {
13964 	case IPPROTO_UDP:
13965 		return bnxt_udp_tunl_check(bp, skb);
13966 	case IPPROTO_IPIP:
13967 		return true;
13968 	case IPPROTO_GRE: {
13969 		switch (skb->inner_protocol) {
13970 		default:
13971 			return false;
13972 		case htons(ETH_P_IP):
13973 			return true;
13974 		case htons(ETH_P_IPV6):
13975 			fallthrough;
13976 		}
13977 	}
13978 	case IPPROTO_IPV6:
13979 		/* Check ext headers of inner ipv6 */
13980 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13981 					 NULL);
13982 	}
13983 	return false;
13984 }
13985 
13986 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13987 					     struct net_device *dev,
13988 					     netdev_features_t features)
13989 {
13990 	struct bnxt *bp = netdev_priv(dev);
13991 	u8 *l4_proto;
13992 
13993 	features = vlan_features_check(skb, features);
13994 	switch (vlan_get_protocol(skb)) {
13995 	case htons(ETH_P_IP):
13996 		if (!skb->encapsulation)
13997 			return features;
13998 		l4_proto = &ip_hdr(skb)->protocol;
13999 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14000 			return features;
14001 		break;
14002 	case htons(ETH_P_IPV6):
14003 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14004 				       &l4_proto))
14005 			break;
14006 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14007 			return features;
14008 		break;
14009 	}
14010 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14011 }
14012 
14013 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14014 			 u32 *reg_buf)
14015 {
14016 	struct hwrm_dbg_read_direct_output *resp;
14017 	struct hwrm_dbg_read_direct_input *req;
14018 	__le32 *dbg_reg_buf;
14019 	dma_addr_t mapping;
14020 	int rc, i;
14021 
14022 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14023 	if (rc)
14024 		return rc;
14025 
14026 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14027 					 &mapping);
14028 	if (!dbg_reg_buf) {
14029 		rc = -ENOMEM;
14030 		goto dbg_rd_reg_exit;
14031 	}
14032 
14033 	req->host_dest_addr = cpu_to_le64(mapping);
14034 
14035 	resp = hwrm_req_hold(bp, req);
14036 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14037 	req->read_len32 = cpu_to_le32(num_words);
14038 
14039 	rc = hwrm_req_send(bp, req);
14040 	if (rc || resp->error_code) {
14041 		rc = -EIO;
14042 		goto dbg_rd_reg_exit;
14043 	}
14044 	for (i = 0; i < num_words; i++)
14045 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14046 
14047 dbg_rd_reg_exit:
14048 	hwrm_req_drop(bp, req);
14049 	return rc;
14050 }
14051 
14052 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14053 				       u32 ring_id, u32 *prod, u32 *cons)
14054 {
14055 	struct hwrm_dbg_ring_info_get_output *resp;
14056 	struct hwrm_dbg_ring_info_get_input *req;
14057 	int rc;
14058 
14059 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14060 	if (rc)
14061 		return rc;
14062 
14063 	req->ring_type = ring_type;
14064 	req->fw_ring_id = cpu_to_le32(ring_id);
14065 	resp = hwrm_req_hold(bp, req);
14066 	rc = hwrm_req_send(bp, req);
14067 	if (!rc) {
14068 		*prod = le32_to_cpu(resp->producer_index);
14069 		*cons = le32_to_cpu(resp->consumer_index);
14070 	}
14071 	hwrm_req_drop(bp, req);
14072 	return rc;
14073 }
14074 
14075 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14076 {
14077 	struct bnxt_tx_ring_info *txr;
14078 	int i = bnapi->index, j;
14079 
14080 	bnxt_for_each_napi_tx(j, bnapi, txr)
14081 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14082 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14083 			    txr->tx_cons);
14084 }
14085 
14086 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14087 {
14088 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14089 	int i = bnapi->index;
14090 
14091 	if (!rxr)
14092 		return;
14093 
14094 	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",
14095 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14096 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14097 		    rxr->rx_sw_agg_prod);
14098 }
14099 
14100 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14101 {
14102 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14103 	int i = bnapi->index, j;
14104 
14105 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14106 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14107 	for (j = 0; j < cpr->cp_ring_count; j++) {
14108 		cpr2 = &cpr->cp_ring_arr[j];
14109 		if (!cpr2->bnapi)
14110 			continue;
14111 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14112 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14113 			    cpr2->cp_raw_cons);
14114 	}
14115 }
14116 
14117 static void bnxt_dbg_dump_states(struct bnxt *bp)
14118 {
14119 	int i;
14120 	struct bnxt_napi *bnapi;
14121 
14122 	for (i = 0; i < bp->cp_nr_rings; i++) {
14123 		bnapi = bp->bnapi[i];
14124 		if (netif_msg_drv(bp)) {
14125 			bnxt_dump_tx_sw_state(bnapi);
14126 			bnxt_dump_rx_sw_state(bnapi);
14127 			bnxt_dump_cp_sw_state(bnapi);
14128 		}
14129 	}
14130 }
14131 
14132 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14133 {
14134 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14135 	struct hwrm_ring_reset_input *req;
14136 	struct bnxt_napi *bnapi = rxr->bnapi;
14137 	struct bnxt_cp_ring_info *cpr;
14138 	u16 cp_ring_id;
14139 	int rc;
14140 
14141 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14142 	if (rc)
14143 		return rc;
14144 
14145 	cpr = &bnapi->cp_ring;
14146 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14147 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14148 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14149 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14150 	return hwrm_req_send_silent(bp, req);
14151 }
14152 
14153 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14154 {
14155 	if (!silent)
14156 		bnxt_dbg_dump_states(bp);
14157 	if (netif_running(bp->dev)) {
14158 		bnxt_close_nic(bp, !silent, false);
14159 		bnxt_open_nic(bp, !silent, false);
14160 	}
14161 }
14162 
14163 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14164 {
14165 	struct bnxt *bp = netdev_priv(dev);
14166 
14167 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14168 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14169 }
14170 
14171 static void bnxt_fw_health_check(struct bnxt *bp)
14172 {
14173 	struct bnxt_fw_health *fw_health = bp->fw_health;
14174 	struct pci_dev *pdev = bp->pdev;
14175 	u32 val;
14176 
14177 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14178 		return;
14179 
14180 	/* Make sure it is enabled before checking the tmr_counter. */
14181 	smp_rmb();
14182 	if (fw_health->tmr_counter) {
14183 		fw_health->tmr_counter--;
14184 		return;
14185 	}
14186 
14187 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14188 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14189 		fw_health->arrests++;
14190 		goto fw_reset;
14191 	}
14192 
14193 	fw_health->last_fw_heartbeat = val;
14194 
14195 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14196 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14197 		fw_health->discoveries++;
14198 		goto fw_reset;
14199 	}
14200 
14201 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14202 	return;
14203 
14204 fw_reset:
14205 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14206 }
14207 
14208 static void bnxt_timer(struct timer_list *t)
14209 {
14210 	struct bnxt *bp = timer_container_of(bp, t, timer);
14211 	struct net_device *dev = bp->dev;
14212 
14213 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14214 		return;
14215 
14216 	if (atomic_read(&bp->intr_sem) != 0)
14217 		goto bnxt_restart_timer;
14218 
14219 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14220 		bnxt_fw_health_check(bp);
14221 
14222 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14223 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14224 
14225 	if (bnxt_tc_flower_enabled(bp))
14226 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14227 
14228 #ifdef CONFIG_RFS_ACCEL
14229 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14230 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14231 #endif /*CONFIG_RFS_ACCEL*/
14232 
14233 	if (bp->link_info.phy_retry) {
14234 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14235 			bp->link_info.phy_retry = false;
14236 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14237 		} else {
14238 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14239 		}
14240 	}
14241 
14242 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14243 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14244 
14245 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14246 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14247 
14248 bnxt_restart_timer:
14249 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14250 }
14251 
14252 static void bnxt_lock_sp(struct bnxt *bp)
14253 {
14254 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14255 	 * set.  If the device is being closed, bnxt_close() may be holding
14256 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14257 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14258 	 * instance lock.
14259 	 */
14260 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14261 	netdev_lock(bp->dev);
14262 }
14263 
14264 static void bnxt_unlock_sp(struct bnxt *bp)
14265 {
14266 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14267 	netdev_unlock(bp->dev);
14268 }
14269 
14270 /* Only called from bnxt_sp_task() */
14271 static void bnxt_reset(struct bnxt *bp, bool silent)
14272 {
14273 	bnxt_lock_sp(bp);
14274 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14275 		bnxt_reset_task(bp, silent);
14276 	bnxt_unlock_sp(bp);
14277 }
14278 
14279 /* Only called from bnxt_sp_task() */
14280 static void bnxt_rx_ring_reset(struct bnxt *bp)
14281 {
14282 	int i;
14283 
14284 	bnxt_lock_sp(bp);
14285 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14286 		bnxt_unlock_sp(bp);
14287 		return;
14288 	}
14289 	/* Disable and flush TPA before resetting the RX ring */
14290 	if (bp->flags & BNXT_FLAG_TPA)
14291 		bnxt_set_tpa(bp, false);
14292 	for (i = 0; i < bp->rx_nr_rings; i++) {
14293 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14294 		struct bnxt_cp_ring_info *cpr;
14295 		int rc;
14296 
14297 		if (!rxr->bnapi->in_reset)
14298 			continue;
14299 
14300 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14301 		if (rc) {
14302 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14303 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14304 			else
14305 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14306 					    rc);
14307 			bnxt_reset_task(bp, true);
14308 			break;
14309 		}
14310 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14311 		rxr->rx_prod = 0;
14312 		rxr->rx_agg_prod = 0;
14313 		rxr->rx_sw_agg_prod = 0;
14314 		rxr->rx_next_cons = 0;
14315 		rxr->bnapi->in_reset = false;
14316 		bnxt_alloc_one_rx_ring(bp, i);
14317 		cpr = &rxr->bnapi->cp_ring;
14318 		cpr->sw_stats->rx.rx_resets++;
14319 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14320 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14321 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14322 	}
14323 	if (bp->flags & BNXT_FLAG_TPA)
14324 		bnxt_set_tpa(bp, true);
14325 	bnxt_unlock_sp(bp);
14326 }
14327 
14328 static void bnxt_fw_fatal_close(struct bnxt *bp)
14329 {
14330 	bnxt_tx_disable(bp);
14331 	bnxt_disable_napi(bp);
14332 	bnxt_disable_int_sync(bp);
14333 	bnxt_free_irq(bp);
14334 	bnxt_clear_int_mode(bp);
14335 	pci_disable_device(bp->pdev);
14336 }
14337 
14338 static void bnxt_fw_reset_close(struct bnxt *bp)
14339 {
14340 	/* When firmware is in fatal state, quiesce device and disable
14341 	 * bus master to prevent any potential bad DMAs before freeing
14342 	 * kernel memory.
14343 	 */
14344 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14345 		u16 val = 0;
14346 
14347 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14348 		if (val == 0xffff)
14349 			bp->fw_reset_min_dsecs = 0;
14350 		bnxt_fw_fatal_close(bp);
14351 	}
14352 	__bnxt_close_nic(bp, true, false);
14353 	bnxt_vf_reps_free(bp);
14354 	bnxt_clear_int_mode(bp);
14355 	bnxt_hwrm_func_drv_unrgtr(bp);
14356 	if (pci_is_enabled(bp->pdev))
14357 		pci_disable_device(bp->pdev);
14358 	bnxt_free_ctx_mem(bp, false);
14359 }
14360 
14361 static bool is_bnxt_fw_ok(struct bnxt *bp)
14362 {
14363 	struct bnxt_fw_health *fw_health = bp->fw_health;
14364 	bool no_heartbeat = false, has_reset = false;
14365 	u32 val;
14366 
14367 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14368 	if (val == fw_health->last_fw_heartbeat)
14369 		no_heartbeat = true;
14370 
14371 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14372 	if (val != fw_health->last_fw_reset_cnt)
14373 		has_reset = true;
14374 
14375 	if (!no_heartbeat && has_reset)
14376 		return true;
14377 
14378 	return false;
14379 }
14380 
14381 /* netdev instance lock is acquired before calling this function */
14382 static void bnxt_force_fw_reset(struct bnxt *bp)
14383 {
14384 	struct bnxt_fw_health *fw_health = bp->fw_health;
14385 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14386 	u32 wait_dsecs;
14387 
14388 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14389 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14390 		return;
14391 
14392 	/* we have to serialize with bnxt_refclk_read()*/
14393 	if (ptp) {
14394 		unsigned long flags;
14395 
14396 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14397 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14398 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14399 	} else {
14400 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14401 	}
14402 	bnxt_fw_reset_close(bp);
14403 	wait_dsecs = fw_health->master_func_wait_dsecs;
14404 	if (fw_health->primary) {
14405 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14406 			wait_dsecs = 0;
14407 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14408 	} else {
14409 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14410 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14411 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14412 	}
14413 
14414 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14415 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14416 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14417 }
14418 
14419 void bnxt_fw_exception(struct bnxt *bp)
14420 {
14421 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14422 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14423 	bnxt_ulp_stop(bp);
14424 	bnxt_lock_sp(bp);
14425 	bnxt_force_fw_reset(bp);
14426 	bnxt_unlock_sp(bp);
14427 }
14428 
14429 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14430  * < 0 on error.
14431  */
14432 static int bnxt_get_registered_vfs(struct bnxt *bp)
14433 {
14434 #ifdef CONFIG_BNXT_SRIOV
14435 	int rc;
14436 
14437 	if (!BNXT_PF(bp))
14438 		return 0;
14439 
14440 	rc = bnxt_hwrm_func_qcfg(bp);
14441 	if (rc) {
14442 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14443 		return rc;
14444 	}
14445 	if (bp->pf.registered_vfs)
14446 		return bp->pf.registered_vfs;
14447 	if (bp->sriov_cfg)
14448 		return 1;
14449 #endif
14450 	return 0;
14451 }
14452 
14453 void bnxt_fw_reset(struct bnxt *bp)
14454 {
14455 	bnxt_ulp_stop(bp);
14456 	bnxt_lock_sp(bp);
14457 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14458 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14459 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14460 		int n = 0, tmo;
14461 
14462 		/* we have to serialize with bnxt_refclk_read()*/
14463 		if (ptp) {
14464 			unsigned long flags;
14465 
14466 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14467 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14468 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14469 		} else {
14470 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14471 		}
14472 		if (bp->pf.active_vfs &&
14473 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14474 			n = bnxt_get_registered_vfs(bp);
14475 		if (n < 0) {
14476 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14477 				   n);
14478 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14479 			netif_close(bp->dev);
14480 			goto fw_reset_exit;
14481 		} else if (n > 0) {
14482 			u16 vf_tmo_dsecs = n * 10;
14483 
14484 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14485 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14486 			bp->fw_reset_state =
14487 				BNXT_FW_RESET_STATE_POLL_VF;
14488 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14489 			goto fw_reset_exit;
14490 		}
14491 		bnxt_fw_reset_close(bp);
14492 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14493 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14494 			tmo = HZ / 10;
14495 		} else {
14496 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14497 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14498 		}
14499 		bnxt_queue_fw_reset_work(bp, tmo);
14500 	}
14501 fw_reset_exit:
14502 	bnxt_unlock_sp(bp);
14503 }
14504 
14505 static void bnxt_chk_missed_irq(struct bnxt *bp)
14506 {
14507 	int i;
14508 
14509 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14510 		return;
14511 
14512 	for (i = 0; i < bp->cp_nr_rings; i++) {
14513 		struct bnxt_napi *bnapi = bp->bnapi[i];
14514 		struct bnxt_cp_ring_info *cpr;
14515 		u32 fw_ring_id;
14516 		int j;
14517 
14518 		if (!bnapi)
14519 			continue;
14520 
14521 		cpr = &bnapi->cp_ring;
14522 		for (j = 0; j < cpr->cp_ring_count; j++) {
14523 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14524 			u32 val[2];
14525 
14526 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14527 				continue;
14528 
14529 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14530 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14531 				continue;
14532 			}
14533 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14534 			bnxt_dbg_hwrm_ring_info_get(bp,
14535 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14536 				fw_ring_id, &val[0], &val[1]);
14537 			cpr->sw_stats->cmn.missed_irqs++;
14538 		}
14539 	}
14540 }
14541 
14542 static void bnxt_cfg_ntp_filters(struct bnxt *);
14543 
14544 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14545 {
14546 	struct bnxt_link_info *link_info = &bp->link_info;
14547 
14548 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14549 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14550 		if (bp->hwrm_spec_code >= 0x10201) {
14551 			if (link_info->auto_pause_setting &
14552 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14553 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14554 		} else {
14555 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14556 		}
14557 		bnxt_set_auto_speed(link_info);
14558 	} else {
14559 		bnxt_set_force_speed(link_info);
14560 		link_info->req_duplex = link_info->duplex_setting;
14561 	}
14562 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14563 		link_info->req_flow_ctrl =
14564 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14565 	else
14566 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14567 }
14568 
14569 static void bnxt_fw_echo_reply(struct bnxt *bp)
14570 {
14571 	struct bnxt_fw_health *fw_health = bp->fw_health;
14572 	struct hwrm_func_echo_response_input *req;
14573 	int rc;
14574 
14575 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14576 	if (rc)
14577 		return;
14578 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14579 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14580 	hwrm_req_send(bp, req);
14581 }
14582 
14583 static void bnxt_ulp_restart(struct bnxt *bp)
14584 {
14585 	bnxt_ulp_stop(bp);
14586 	bnxt_ulp_start(bp, 0);
14587 }
14588 
14589 static void bnxt_sp_task(struct work_struct *work)
14590 {
14591 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14592 
14593 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14594 	smp_mb__after_atomic();
14595 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14596 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14597 		return;
14598 	}
14599 
14600 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14601 		bnxt_ulp_restart(bp);
14602 		bnxt_reenable_sriov(bp);
14603 	}
14604 
14605 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14606 		bnxt_cfg_rx_mode(bp);
14607 
14608 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14609 		bnxt_cfg_ntp_filters(bp);
14610 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14611 		bnxt_hwrm_exec_fwd_req(bp);
14612 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14613 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14614 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14615 		bnxt_hwrm_port_qstats(bp, 0);
14616 		bnxt_hwrm_port_qstats_ext(bp, 0);
14617 		bnxt_accumulate_all_stats(bp);
14618 	}
14619 
14620 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14621 		int rc;
14622 
14623 		mutex_lock(&bp->link_lock);
14624 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14625 				       &bp->sp_event))
14626 			bnxt_hwrm_phy_qcaps(bp);
14627 
14628 		rc = bnxt_update_link(bp, true);
14629 		if (rc)
14630 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14631 				   rc);
14632 
14633 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14634 				       &bp->sp_event))
14635 			bnxt_init_ethtool_link_settings(bp);
14636 		mutex_unlock(&bp->link_lock);
14637 	}
14638 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14639 		int rc;
14640 
14641 		mutex_lock(&bp->link_lock);
14642 		rc = bnxt_update_phy_setting(bp);
14643 		mutex_unlock(&bp->link_lock);
14644 		if (rc) {
14645 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14646 		} else {
14647 			bp->link_info.phy_retry = false;
14648 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14649 		}
14650 	}
14651 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14652 		mutex_lock(&bp->link_lock);
14653 		bnxt_get_port_module_status(bp);
14654 		mutex_unlock(&bp->link_lock);
14655 	}
14656 
14657 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14658 		bnxt_tc_flow_stats_work(bp);
14659 
14660 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14661 		bnxt_chk_missed_irq(bp);
14662 
14663 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14664 		bnxt_fw_echo_reply(bp);
14665 
14666 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14667 		bnxt_hwmon_notify_event(bp);
14668 
14669 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14670 	 * must be the last functions to be called before exiting.
14671 	 */
14672 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14673 		bnxt_reset(bp, false);
14674 
14675 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14676 		bnxt_reset(bp, true);
14677 
14678 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14679 		bnxt_rx_ring_reset(bp);
14680 
14681 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14682 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14683 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14684 			bnxt_devlink_health_fw_report(bp);
14685 		else
14686 			bnxt_fw_reset(bp);
14687 	}
14688 
14689 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14690 		if (!is_bnxt_fw_ok(bp))
14691 			bnxt_devlink_health_fw_report(bp);
14692 	}
14693 
14694 	smp_mb__before_atomic();
14695 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14696 }
14697 
14698 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14699 				int *max_cp);
14700 
14701 /* Under netdev instance lock */
14702 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14703 		     int tx_xdp)
14704 {
14705 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14706 	struct bnxt_hw_rings hwr = {0};
14707 	int rx_rings = rx;
14708 	int rc;
14709 
14710 	if (tcs)
14711 		tx_sets = tcs;
14712 
14713 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14714 
14715 	if (max_rx < rx_rings)
14716 		return -ENOMEM;
14717 
14718 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14719 		rx_rings <<= 1;
14720 
14721 	hwr.rx = rx_rings;
14722 	hwr.tx = tx * tx_sets + tx_xdp;
14723 	if (max_tx < hwr.tx)
14724 		return -ENOMEM;
14725 
14726 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14727 
14728 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14729 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14730 	if (max_cp < hwr.cp)
14731 		return -ENOMEM;
14732 	hwr.stat = hwr.cp;
14733 	if (BNXT_NEW_RM(bp)) {
14734 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14735 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14736 		hwr.grp = rx;
14737 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14738 	}
14739 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14740 		hwr.cp_p5 = hwr.tx + rx;
14741 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14742 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14743 		if (!bnxt_ulp_registered(bp->edev)) {
14744 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14745 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14746 		}
14747 		if (hwr.cp > bp->total_irqs) {
14748 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14749 
14750 			if (total_msix < hwr.cp) {
14751 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14752 					    hwr.cp, total_msix);
14753 				rc = -ENOSPC;
14754 			}
14755 		}
14756 	}
14757 	return rc;
14758 }
14759 
14760 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14761 {
14762 	if (bp->bar2) {
14763 		pci_iounmap(pdev, bp->bar2);
14764 		bp->bar2 = NULL;
14765 	}
14766 
14767 	if (bp->bar1) {
14768 		pci_iounmap(pdev, bp->bar1);
14769 		bp->bar1 = NULL;
14770 	}
14771 
14772 	if (bp->bar0) {
14773 		pci_iounmap(pdev, bp->bar0);
14774 		bp->bar0 = NULL;
14775 	}
14776 }
14777 
14778 static void bnxt_cleanup_pci(struct bnxt *bp)
14779 {
14780 	bnxt_unmap_bars(bp, bp->pdev);
14781 	pci_release_regions(bp->pdev);
14782 	if (pci_is_enabled(bp->pdev))
14783 		pci_disable_device(bp->pdev);
14784 }
14785 
14786 static void bnxt_init_dflt_coal(struct bnxt *bp)
14787 {
14788 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14789 	struct bnxt_coal *coal;
14790 	u16 flags = 0;
14791 
14792 	if (coal_cap->cmpl_params &
14793 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14794 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14795 
14796 	/* Tick values in micro seconds.
14797 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14798 	 */
14799 	coal = &bp->rx_coal;
14800 	coal->coal_ticks = 10;
14801 	coal->coal_bufs = 30;
14802 	coal->coal_ticks_irq = 1;
14803 	coal->coal_bufs_irq = 2;
14804 	coal->idle_thresh = 50;
14805 	coal->bufs_per_record = 2;
14806 	coal->budget = 64;		/* NAPI budget */
14807 	coal->flags = flags;
14808 
14809 	coal = &bp->tx_coal;
14810 	coal->coal_ticks = 28;
14811 	coal->coal_bufs = 30;
14812 	coal->coal_ticks_irq = 2;
14813 	coal->coal_bufs_irq = 2;
14814 	coal->bufs_per_record = 1;
14815 	coal->flags = flags;
14816 
14817 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14818 }
14819 
14820 /* FW that pre-reserves 1 VNIC per function */
14821 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14822 {
14823 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14824 
14825 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14826 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14827 		return true;
14828 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14829 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14830 		return true;
14831 	return false;
14832 }
14833 
14834 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
14835 {
14836 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
14837 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
14838 	int rc;
14839 
14840 	bp->max_pfcwd_tmo_ms = 0;
14841 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
14842 	if (rc)
14843 		return;
14844 	resp = hwrm_req_hold(bp, req);
14845 	rc = hwrm_req_send_silent(bp, req);
14846 	if (!rc)
14847 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
14848 	hwrm_req_drop(bp, req);
14849 }
14850 
14851 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14852 {
14853 	int rc;
14854 
14855 	bp->fw_cap = 0;
14856 	rc = bnxt_hwrm_ver_get(bp);
14857 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14858 	 * so wait before continuing with recovery.
14859 	 */
14860 	if (rc)
14861 		msleep(100);
14862 	bnxt_try_map_fw_health_reg(bp);
14863 	if (rc) {
14864 		rc = bnxt_try_recover_fw(bp);
14865 		if (rc)
14866 			return rc;
14867 		rc = bnxt_hwrm_ver_get(bp);
14868 		if (rc)
14869 			return rc;
14870 	}
14871 
14872 	bnxt_nvm_cfg_ver_get(bp);
14873 
14874 	rc = bnxt_hwrm_func_reset(bp);
14875 	if (rc)
14876 		return -ENODEV;
14877 
14878 	bnxt_hwrm_fw_set_time(bp);
14879 	return 0;
14880 }
14881 
14882 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14883 {
14884 	int rc;
14885 
14886 	/* Get the MAX capabilities for this function */
14887 	rc = bnxt_hwrm_func_qcaps(bp);
14888 	if (rc) {
14889 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14890 			   rc);
14891 		return -ENODEV;
14892 	}
14893 
14894 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14895 	if (rc)
14896 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14897 			    rc);
14898 
14899 	if (bnxt_alloc_fw_health(bp)) {
14900 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14901 	} else {
14902 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14903 		if (rc)
14904 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14905 				    rc);
14906 	}
14907 
14908 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14909 	if (rc)
14910 		return -ENODEV;
14911 
14912 	rc = bnxt_alloc_crash_dump_mem(bp);
14913 	if (rc)
14914 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14915 			    rc);
14916 	if (!rc) {
14917 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14918 		if (rc) {
14919 			bnxt_free_crash_dump_mem(bp);
14920 			netdev_warn(bp->dev,
14921 				    "hwrm crash dump mem failure rc: %d\n", rc);
14922 		}
14923 	}
14924 
14925 	if (bnxt_fw_pre_resv_vnics(bp))
14926 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14927 
14928 	bnxt_hwrm_pfcwd_qcaps(bp);
14929 	bnxt_hwrm_func_qcfg(bp);
14930 	bnxt_hwrm_vnic_qcaps(bp);
14931 	bnxt_hwrm_port_led_qcaps(bp);
14932 	bnxt_ethtool_init(bp);
14933 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14934 		__bnxt_hwrm_ptp_qcfg(bp);
14935 	bnxt_dcb_init(bp);
14936 	bnxt_hwmon_init(bp);
14937 	return 0;
14938 }
14939 
14940 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14941 {
14942 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14943 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14944 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14945 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14946 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14947 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14948 		bp->rss_hash_delta = bp->rss_hash_cfg;
14949 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14950 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14951 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14952 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14953 	}
14954 }
14955 
14956 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14957 {
14958 	struct net_device *dev = bp->dev;
14959 
14960 	dev->hw_features &= ~NETIF_F_NTUPLE;
14961 	dev->features &= ~NETIF_F_NTUPLE;
14962 	bp->flags &= ~BNXT_FLAG_RFS;
14963 	if (bnxt_rfs_supported(bp)) {
14964 		dev->hw_features |= NETIF_F_NTUPLE;
14965 		if (bnxt_rfs_capable(bp, false)) {
14966 			bp->flags |= BNXT_FLAG_RFS;
14967 			dev->features |= NETIF_F_NTUPLE;
14968 		}
14969 	}
14970 }
14971 
14972 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14973 {
14974 	struct pci_dev *pdev = bp->pdev;
14975 
14976 	bnxt_set_dflt_rss_hash_type(bp);
14977 	bnxt_set_dflt_rfs(bp);
14978 
14979 	bnxt_get_wol_settings(bp);
14980 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14981 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14982 	else
14983 		device_set_wakeup_capable(&pdev->dev, false);
14984 
14985 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14986 	bnxt_hwrm_coal_params_qcaps(bp);
14987 }
14988 
14989 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14990 
14991 int bnxt_fw_init_one(struct bnxt *bp)
14992 {
14993 	int rc;
14994 
14995 	rc = bnxt_fw_init_one_p1(bp);
14996 	if (rc) {
14997 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14998 		return rc;
14999 	}
15000 	rc = bnxt_fw_init_one_p2(bp);
15001 	if (rc) {
15002 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15003 		return rc;
15004 	}
15005 	rc = bnxt_probe_phy(bp, false);
15006 	if (rc)
15007 		return rc;
15008 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15009 	if (rc)
15010 		return rc;
15011 
15012 	bnxt_fw_init_one_p3(bp);
15013 	return 0;
15014 }
15015 
15016 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15017 {
15018 	struct bnxt_fw_health *fw_health = bp->fw_health;
15019 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15020 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15021 	u32 reg_type, reg_off, delay_msecs;
15022 
15023 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15024 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15025 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15026 	switch (reg_type) {
15027 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15028 		pci_write_config_dword(bp->pdev, reg_off, val);
15029 		break;
15030 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15031 		writel(reg_off & BNXT_GRC_BASE_MASK,
15032 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15033 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15034 		fallthrough;
15035 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15036 		writel(val, bp->bar0 + reg_off);
15037 		break;
15038 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15039 		writel(val, bp->bar1 + reg_off);
15040 		break;
15041 	}
15042 	if (delay_msecs) {
15043 		pci_read_config_dword(bp->pdev, 0, &val);
15044 		msleep(delay_msecs);
15045 	}
15046 }
15047 
15048 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15049 {
15050 	struct hwrm_func_qcfg_output *resp;
15051 	struct hwrm_func_qcfg_input *req;
15052 	bool result = true; /* firmware will enforce if unknown */
15053 
15054 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15055 		return result;
15056 
15057 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15058 		return result;
15059 
15060 	req->fid = cpu_to_le16(0xffff);
15061 	resp = hwrm_req_hold(bp, req);
15062 	if (!hwrm_req_send(bp, req))
15063 		result = !!(le16_to_cpu(resp->flags) &
15064 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15065 	hwrm_req_drop(bp, req);
15066 	return result;
15067 }
15068 
15069 static void bnxt_reset_all(struct bnxt *bp)
15070 {
15071 	struct bnxt_fw_health *fw_health = bp->fw_health;
15072 	int i, rc;
15073 
15074 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15075 		bnxt_fw_reset_via_optee(bp);
15076 		bp->fw_reset_timestamp = jiffies;
15077 		return;
15078 	}
15079 
15080 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15081 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15082 			bnxt_fw_reset_writel(bp, i);
15083 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15084 		struct hwrm_fw_reset_input *req;
15085 
15086 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15087 		if (!rc) {
15088 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15089 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15090 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15091 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15092 			rc = hwrm_req_send(bp, req);
15093 		}
15094 		if (rc != -ENODEV)
15095 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15096 	}
15097 	bp->fw_reset_timestamp = jiffies;
15098 }
15099 
15100 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15101 {
15102 	return time_after(jiffies, bp->fw_reset_timestamp +
15103 			  (bp->fw_reset_max_dsecs * HZ / 10));
15104 }
15105 
15106 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15107 {
15108 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15109 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15110 		bnxt_dl_health_fw_status_update(bp, false);
15111 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15112 	netif_close(bp->dev);
15113 }
15114 
15115 static void bnxt_fw_reset_task(struct work_struct *work)
15116 {
15117 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15118 	int rc = 0;
15119 
15120 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15121 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15122 		return;
15123 	}
15124 
15125 	switch (bp->fw_reset_state) {
15126 	case BNXT_FW_RESET_STATE_POLL_VF: {
15127 		int n = bnxt_get_registered_vfs(bp);
15128 		int tmo;
15129 
15130 		if (n < 0) {
15131 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15132 				   n, jiffies_to_msecs(jiffies -
15133 				   bp->fw_reset_timestamp));
15134 			goto fw_reset_abort;
15135 		} else if (n > 0) {
15136 			if (bnxt_fw_reset_timeout(bp)) {
15137 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15138 				bp->fw_reset_state = 0;
15139 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15140 					   n);
15141 				goto ulp_start;
15142 			}
15143 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15144 			return;
15145 		}
15146 		bp->fw_reset_timestamp = jiffies;
15147 		netdev_lock(bp->dev);
15148 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15149 			bnxt_fw_reset_abort(bp, rc);
15150 			netdev_unlock(bp->dev);
15151 			goto ulp_start;
15152 		}
15153 		bnxt_fw_reset_close(bp);
15154 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15155 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15156 			tmo = HZ / 10;
15157 		} else {
15158 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15159 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15160 		}
15161 		netdev_unlock(bp->dev);
15162 		bnxt_queue_fw_reset_work(bp, tmo);
15163 		return;
15164 	}
15165 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15166 		u32 val;
15167 
15168 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15169 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15170 		    !bnxt_fw_reset_timeout(bp)) {
15171 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15172 			return;
15173 		}
15174 
15175 		if (!bp->fw_health->primary) {
15176 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15177 
15178 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15179 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15180 			return;
15181 		}
15182 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15183 	}
15184 		fallthrough;
15185 	case BNXT_FW_RESET_STATE_RESET_FW:
15186 		bnxt_reset_all(bp);
15187 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15188 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15189 		return;
15190 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15191 		bnxt_inv_fw_health_reg(bp);
15192 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15193 		    !bp->fw_reset_min_dsecs) {
15194 			u16 val;
15195 
15196 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15197 			if (val == 0xffff) {
15198 				if (bnxt_fw_reset_timeout(bp)) {
15199 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15200 					rc = -ETIMEDOUT;
15201 					goto fw_reset_abort;
15202 				}
15203 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15204 				return;
15205 			}
15206 		}
15207 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15208 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15209 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15210 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15211 			bnxt_dl_remote_reload(bp);
15212 		if (pci_enable_device(bp->pdev)) {
15213 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15214 			rc = -ENODEV;
15215 			goto fw_reset_abort;
15216 		}
15217 		pci_set_master(bp->pdev);
15218 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15219 		fallthrough;
15220 	case BNXT_FW_RESET_STATE_POLL_FW:
15221 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15222 		rc = bnxt_hwrm_poll(bp);
15223 		if (rc) {
15224 			if (bnxt_fw_reset_timeout(bp)) {
15225 				netdev_err(bp->dev, "Firmware reset aborted\n");
15226 				goto fw_reset_abort_status;
15227 			}
15228 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15229 			return;
15230 		}
15231 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15232 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15233 		fallthrough;
15234 	case BNXT_FW_RESET_STATE_OPENING:
15235 		while (!netdev_trylock(bp->dev)) {
15236 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15237 			return;
15238 		}
15239 		rc = bnxt_open(bp->dev);
15240 		if (rc) {
15241 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15242 			bnxt_fw_reset_abort(bp, rc);
15243 			netdev_unlock(bp->dev);
15244 			goto ulp_start;
15245 		}
15246 
15247 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15248 		    bp->fw_health->enabled) {
15249 			bp->fw_health->last_fw_reset_cnt =
15250 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15251 		}
15252 		bp->fw_reset_state = 0;
15253 		/* Make sure fw_reset_state is 0 before clearing the flag */
15254 		smp_mb__before_atomic();
15255 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15256 		bnxt_ptp_reapply_pps(bp);
15257 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15258 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15259 			bnxt_dl_health_fw_recovery_done(bp);
15260 			bnxt_dl_health_fw_status_update(bp, true);
15261 		}
15262 		netdev_unlock(bp->dev);
15263 		bnxt_ulp_start(bp, 0);
15264 		bnxt_reenable_sriov(bp);
15265 		netdev_lock(bp->dev);
15266 		bnxt_vf_reps_alloc(bp);
15267 		bnxt_vf_reps_open(bp);
15268 		netdev_unlock(bp->dev);
15269 		break;
15270 	}
15271 	return;
15272 
15273 fw_reset_abort_status:
15274 	if (bp->fw_health->status_reliable ||
15275 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15276 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15277 
15278 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15279 	}
15280 fw_reset_abort:
15281 	netdev_lock(bp->dev);
15282 	bnxt_fw_reset_abort(bp, rc);
15283 	netdev_unlock(bp->dev);
15284 ulp_start:
15285 	bnxt_ulp_start(bp, rc);
15286 }
15287 
15288 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15289 {
15290 	int rc;
15291 	struct bnxt *bp = netdev_priv(dev);
15292 
15293 	SET_NETDEV_DEV(dev, &pdev->dev);
15294 
15295 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15296 	rc = pci_enable_device(pdev);
15297 	if (rc) {
15298 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15299 		goto init_err;
15300 	}
15301 
15302 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15303 		dev_err(&pdev->dev,
15304 			"Cannot find PCI device base address, aborting\n");
15305 		rc = -ENODEV;
15306 		goto init_err_disable;
15307 	}
15308 
15309 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15310 	if (rc) {
15311 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15312 		goto init_err_disable;
15313 	}
15314 
15315 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15316 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15317 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15318 		rc = -EIO;
15319 		goto init_err_release;
15320 	}
15321 
15322 	pci_set_master(pdev);
15323 
15324 	bp->dev = dev;
15325 	bp->pdev = pdev;
15326 
15327 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15328 	 * determines the BAR size.
15329 	 */
15330 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15331 	if (!bp->bar0) {
15332 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15333 		rc = -ENOMEM;
15334 		goto init_err_release;
15335 	}
15336 
15337 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15338 	if (!bp->bar2) {
15339 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15340 		rc = -ENOMEM;
15341 		goto init_err_release;
15342 	}
15343 
15344 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15345 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15346 
15347 	spin_lock_init(&bp->ntp_fltr_lock);
15348 #if BITS_PER_LONG == 32
15349 	spin_lock_init(&bp->db_lock);
15350 #endif
15351 
15352 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15353 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15354 
15355 	timer_setup(&bp->timer, bnxt_timer, 0);
15356 	bp->current_interval = BNXT_TIMER_INTERVAL;
15357 
15358 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15359 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15360 
15361 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15362 	return 0;
15363 
15364 init_err_release:
15365 	bnxt_unmap_bars(bp, pdev);
15366 	pci_release_regions(pdev);
15367 
15368 init_err_disable:
15369 	pci_disable_device(pdev);
15370 
15371 init_err:
15372 	return rc;
15373 }
15374 
15375 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15376 {
15377 	struct sockaddr *addr = p;
15378 	struct bnxt *bp = netdev_priv(dev);
15379 	int rc = 0;
15380 
15381 	netdev_assert_locked(dev);
15382 
15383 	if (!is_valid_ether_addr(addr->sa_data))
15384 		return -EADDRNOTAVAIL;
15385 
15386 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15387 		return 0;
15388 
15389 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15390 	if (rc)
15391 		return rc;
15392 
15393 	eth_hw_addr_set(dev, addr->sa_data);
15394 	bnxt_clear_usr_fltrs(bp, true);
15395 	if (netif_running(dev)) {
15396 		bnxt_close_nic(bp, false, false);
15397 		rc = bnxt_open_nic(bp, false, false);
15398 	}
15399 
15400 	return rc;
15401 }
15402 
15403 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15404 {
15405 	struct bnxt *bp = netdev_priv(dev);
15406 
15407 	netdev_assert_locked(dev);
15408 
15409 	if (netif_running(dev))
15410 		bnxt_close_nic(bp, true, false);
15411 
15412 	WRITE_ONCE(dev->mtu, new_mtu);
15413 
15414 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15415 	 * program is attached.  We need to set the AGG rings settings and
15416 	 * rx_skb_func accordingly.
15417 	 */
15418 	if (READ_ONCE(bp->xdp_prog))
15419 		bnxt_set_rx_skb_mode(bp, true);
15420 
15421 	bnxt_set_ring_params(bp);
15422 
15423 	if (netif_running(dev))
15424 		return bnxt_open_nic(bp, true, false);
15425 
15426 	return 0;
15427 }
15428 
15429 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15430 {
15431 	struct bnxt *bp = netdev_priv(dev);
15432 	bool sh = false;
15433 	int rc, tx_cp;
15434 
15435 	if (tc > bp->max_tc) {
15436 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15437 			   tc, bp->max_tc);
15438 		return -EINVAL;
15439 	}
15440 
15441 	if (bp->num_tc == tc)
15442 		return 0;
15443 
15444 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15445 		sh = true;
15446 
15447 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15448 			      sh, tc, bp->tx_nr_rings_xdp);
15449 	if (rc)
15450 		return rc;
15451 
15452 	/* Needs to close the device and do hw resource re-allocations */
15453 	if (netif_running(bp->dev))
15454 		bnxt_close_nic(bp, true, false);
15455 
15456 	if (tc) {
15457 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15458 		netdev_set_num_tc(dev, tc);
15459 		bp->num_tc = tc;
15460 	} else {
15461 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15462 		netdev_reset_tc(dev);
15463 		bp->num_tc = 0;
15464 	}
15465 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15466 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15467 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15468 			       tx_cp + bp->rx_nr_rings;
15469 
15470 	if (netif_running(bp->dev))
15471 		return bnxt_open_nic(bp, true, false);
15472 
15473 	return 0;
15474 }
15475 
15476 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15477 				  void *cb_priv)
15478 {
15479 	struct bnxt *bp = cb_priv;
15480 
15481 	if (!bnxt_tc_flower_enabled(bp) ||
15482 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15483 		return -EOPNOTSUPP;
15484 
15485 	switch (type) {
15486 	case TC_SETUP_CLSFLOWER:
15487 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15488 	default:
15489 		return -EOPNOTSUPP;
15490 	}
15491 }
15492 
15493 LIST_HEAD(bnxt_block_cb_list);
15494 
15495 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15496 			 void *type_data)
15497 {
15498 	struct bnxt *bp = netdev_priv(dev);
15499 
15500 	switch (type) {
15501 	case TC_SETUP_BLOCK:
15502 		return flow_block_cb_setup_simple(type_data,
15503 						  &bnxt_block_cb_list,
15504 						  bnxt_setup_tc_block_cb,
15505 						  bp, bp, true);
15506 	case TC_SETUP_QDISC_MQPRIO: {
15507 		struct tc_mqprio_qopt *mqprio = type_data;
15508 
15509 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15510 
15511 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15512 	}
15513 	default:
15514 		return -EOPNOTSUPP;
15515 	}
15516 }
15517 
15518 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15519 			    const struct sk_buff *skb)
15520 {
15521 	struct bnxt_vnic_info *vnic;
15522 
15523 	if (skb)
15524 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15525 
15526 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15527 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15528 }
15529 
15530 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15531 			   u32 idx)
15532 {
15533 	struct hlist_head *head;
15534 	int bit_id;
15535 
15536 	spin_lock_bh(&bp->ntp_fltr_lock);
15537 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15538 	if (bit_id < 0) {
15539 		spin_unlock_bh(&bp->ntp_fltr_lock);
15540 		return -ENOMEM;
15541 	}
15542 
15543 	fltr->base.sw_id = (u16)bit_id;
15544 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15545 	fltr->base.flags |= BNXT_ACT_RING_DST;
15546 	head = &bp->ntp_fltr_hash_tbl[idx];
15547 	hlist_add_head_rcu(&fltr->base.hash, head);
15548 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15549 	bnxt_insert_usr_fltr(bp, &fltr->base);
15550 	bp->ntp_fltr_count++;
15551 	spin_unlock_bh(&bp->ntp_fltr_lock);
15552 	return 0;
15553 }
15554 
15555 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15556 			    struct bnxt_ntuple_filter *f2)
15557 {
15558 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15559 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15560 	struct flow_keys *keys1 = &f1->fkeys;
15561 	struct flow_keys *keys2 = &f2->fkeys;
15562 
15563 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15564 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15565 		return false;
15566 
15567 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15568 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15569 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15570 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15571 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15572 			return false;
15573 	} else {
15574 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15575 				     &keys2->addrs.v6addrs.src) ||
15576 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15577 				     &masks2->addrs.v6addrs.src) ||
15578 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15579 				     &keys2->addrs.v6addrs.dst) ||
15580 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15581 				     &masks2->addrs.v6addrs.dst))
15582 			return false;
15583 	}
15584 
15585 	return keys1->ports.src == keys2->ports.src &&
15586 	       masks1->ports.src == masks2->ports.src &&
15587 	       keys1->ports.dst == keys2->ports.dst &&
15588 	       masks1->ports.dst == masks2->ports.dst &&
15589 	       keys1->control.flags == keys2->control.flags &&
15590 	       f1->l2_fltr == f2->l2_fltr;
15591 }
15592 
15593 struct bnxt_ntuple_filter *
15594 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15595 				struct bnxt_ntuple_filter *fltr, u32 idx)
15596 {
15597 	struct bnxt_ntuple_filter *f;
15598 	struct hlist_head *head;
15599 
15600 	head = &bp->ntp_fltr_hash_tbl[idx];
15601 	hlist_for_each_entry_rcu(f, head, base.hash) {
15602 		if (bnxt_fltr_match(f, fltr))
15603 			return f;
15604 	}
15605 	return NULL;
15606 }
15607 
15608 #ifdef CONFIG_RFS_ACCEL
15609 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15610 			      u16 rxq_index, u32 flow_id)
15611 {
15612 	struct bnxt *bp = netdev_priv(dev);
15613 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15614 	struct flow_keys *fkeys;
15615 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15616 	struct bnxt_l2_filter *l2_fltr;
15617 	int rc = 0, idx;
15618 	u32 flags;
15619 
15620 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15621 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15622 		atomic_inc(&l2_fltr->refcnt);
15623 	} else {
15624 		struct bnxt_l2_key key;
15625 
15626 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15627 		key.vlan = 0;
15628 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15629 		if (!l2_fltr)
15630 			return -EINVAL;
15631 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15632 			bnxt_del_l2_filter(bp, l2_fltr);
15633 			return -EINVAL;
15634 		}
15635 	}
15636 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15637 	if (!new_fltr) {
15638 		bnxt_del_l2_filter(bp, l2_fltr);
15639 		return -ENOMEM;
15640 	}
15641 
15642 	fkeys = &new_fltr->fkeys;
15643 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15644 		rc = -EPROTONOSUPPORT;
15645 		goto err_free;
15646 	}
15647 
15648 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15649 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15650 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15651 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15652 		rc = -EPROTONOSUPPORT;
15653 		goto err_free;
15654 	}
15655 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15656 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15657 		if (bp->hwrm_spec_code < 0x10601) {
15658 			rc = -EPROTONOSUPPORT;
15659 			goto err_free;
15660 		}
15661 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15662 	}
15663 	flags = fkeys->control.flags;
15664 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15665 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15666 		rc = -EPROTONOSUPPORT;
15667 		goto err_free;
15668 	}
15669 	new_fltr->l2_fltr = l2_fltr;
15670 
15671 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15672 	rcu_read_lock();
15673 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15674 	if (fltr) {
15675 		rc = fltr->base.sw_id;
15676 		rcu_read_unlock();
15677 		goto err_free;
15678 	}
15679 	rcu_read_unlock();
15680 
15681 	new_fltr->flow_id = flow_id;
15682 	new_fltr->base.rxq = rxq_index;
15683 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15684 	if (!rc) {
15685 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15686 		return new_fltr->base.sw_id;
15687 	}
15688 
15689 err_free:
15690 	bnxt_del_l2_filter(bp, l2_fltr);
15691 	kfree(new_fltr);
15692 	return rc;
15693 }
15694 #endif
15695 
15696 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15697 {
15698 	spin_lock_bh(&bp->ntp_fltr_lock);
15699 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15700 		spin_unlock_bh(&bp->ntp_fltr_lock);
15701 		return;
15702 	}
15703 	hlist_del_rcu(&fltr->base.hash);
15704 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15705 	bp->ntp_fltr_count--;
15706 	spin_unlock_bh(&bp->ntp_fltr_lock);
15707 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15708 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15709 	kfree_rcu(fltr, base.rcu);
15710 }
15711 
15712 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15713 {
15714 #ifdef CONFIG_RFS_ACCEL
15715 	int i;
15716 
15717 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15718 		struct hlist_head *head;
15719 		struct hlist_node *tmp;
15720 		struct bnxt_ntuple_filter *fltr;
15721 		int rc;
15722 
15723 		head = &bp->ntp_fltr_hash_tbl[i];
15724 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15725 			bool del = false;
15726 
15727 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15728 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15729 					continue;
15730 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15731 							fltr->flow_id,
15732 							fltr->base.sw_id)) {
15733 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15734 									 fltr);
15735 					del = true;
15736 				}
15737 			} else {
15738 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15739 								       fltr);
15740 				if (rc)
15741 					del = true;
15742 				else
15743 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15744 			}
15745 
15746 			if (del)
15747 				bnxt_del_ntp_filter(bp, fltr);
15748 		}
15749 	}
15750 #endif
15751 }
15752 
15753 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15754 				    unsigned int entry, struct udp_tunnel_info *ti)
15755 {
15756 	struct bnxt *bp = netdev_priv(netdev);
15757 	unsigned int cmd;
15758 
15759 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15760 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15761 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15762 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15763 	else
15764 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15765 
15766 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15767 }
15768 
15769 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15770 				      unsigned int entry, struct udp_tunnel_info *ti)
15771 {
15772 	struct bnxt *bp = netdev_priv(netdev);
15773 	unsigned int cmd;
15774 
15775 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15776 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15777 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15778 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15779 	else
15780 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15781 
15782 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15783 }
15784 
15785 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15786 	.set_port	= bnxt_udp_tunnel_set_port,
15787 	.unset_port	= bnxt_udp_tunnel_unset_port,
15788 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15789 	.tables		= {
15790 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15791 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15792 	},
15793 }, bnxt_udp_tunnels_p7 = {
15794 	.set_port	= bnxt_udp_tunnel_set_port,
15795 	.unset_port	= bnxt_udp_tunnel_unset_port,
15796 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15797 	.tables		= {
15798 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15799 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15800 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15801 	},
15802 };
15803 
15804 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15805 			       struct net_device *dev, u32 filter_mask,
15806 			       int nlflags)
15807 {
15808 	struct bnxt *bp = netdev_priv(dev);
15809 
15810 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15811 				       nlflags, filter_mask, NULL);
15812 }
15813 
15814 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15815 			       u16 flags, struct netlink_ext_ack *extack)
15816 {
15817 	struct bnxt *bp = netdev_priv(dev);
15818 	struct nlattr *attr, *br_spec;
15819 	int rem, rc = 0;
15820 
15821 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15822 		return -EOPNOTSUPP;
15823 
15824 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15825 	if (!br_spec)
15826 		return -EINVAL;
15827 
15828 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15829 		u16 mode;
15830 
15831 		mode = nla_get_u16(attr);
15832 		if (mode == bp->br_mode)
15833 			break;
15834 
15835 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15836 		if (!rc)
15837 			bp->br_mode = mode;
15838 		break;
15839 	}
15840 	return rc;
15841 }
15842 
15843 int bnxt_get_port_parent_id(struct net_device *dev,
15844 			    struct netdev_phys_item_id *ppid)
15845 {
15846 	struct bnxt *bp = netdev_priv(dev);
15847 
15848 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15849 		return -EOPNOTSUPP;
15850 
15851 	/* The PF and it's VF-reps only support the switchdev framework */
15852 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15853 		return -EOPNOTSUPP;
15854 
15855 	ppid->id_len = sizeof(bp->dsn);
15856 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15857 
15858 	return 0;
15859 }
15860 
15861 static const struct net_device_ops bnxt_netdev_ops = {
15862 	.ndo_open		= bnxt_open,
15863 	.ndo_start_xmit		= bnxt_start_xmit,
15864 	.ndo_stop		= bnxt_close,
15865 	.ndo_get_stats64	= bnxt_get_stats64,
15866 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15867 	.ndo_eth_ioctl		= bnxt_ioctl,
15868 	.ndo_validate_addr	= eth_validate_addr,
15869 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15870 	.ndo_change_mtu		= bnxt_change_mtu,
15871 	.ndo_fix_features	= bnxt_fix_features,
15872 	.ndo_set_features	= bnxt_set_features,
15873 	.ndo_features_check	= bnxt_features_check,
15874 	.ndo_tx_timeout		= bnxt_tx_timeout,
15875 #ifdef CONFIG_BNXT_SRIOV
15876 	.ndo_get_vf_config	= bnxt_get_vf_config,
15877 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15878 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15879 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15880 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15881 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15882 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15883 #endif
15884 	.ndo_setup_tc           = bnxt_setup_tc,
15885 #ifdef CONFIG_RFS_ACCEL
15886 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15887 #endif
15888 	.ndo_bpf		= bnxt_xdp,
15889 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15890 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15891 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15892 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
15893 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
15894 };
15895 
15896 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15897 				    struct netdev_queue_stats_rx *stats)
15898 {
15899 	struct bnxt *bp = netdev_priv(dev);
15900 	struct bnxt_cp_ring_info *cpr;
15901 	u64 *sw;
15902 
15903 	if (!bp->bnapi)
15904 		return;
15905 
15906 	cpr = &bp->bnapi[i]->cp_ring;
15907 	sw = cpr->stats.sw_stats;
15908 
15909 	stats->packets = 0;
15910 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15911 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15912 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15913 
15914 	stats->bytes = 0;
15915 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15916 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15917 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15918 
15919 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15920 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
15921 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
15922 }
15923 
15924 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15925 				    struct netdev_queue_stats_tx *stats)
15926 {
15927 	struct bnxt *bp = netdev_priv(dev);
15928 	struct bnxt_napi *bnapi;
15929 	u64 *sw;
15930 
15931 	if (!bp->tx_ring)
15932 		return;
15933 
15934 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15935 	sw = bnapi->cp_ring.stats.sw_stats;
15936 
15937 	stats->packets = 0;
15938 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15939 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15940 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15941 
15942 	stats->bytes = 0;
15943 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15944 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15945 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15946 }
15947 
15948 static void bnxt_get_base_stats(struct net_device *dev,
15949 				struct netdev_queue_stats_rx *rx,
15950 				struct netdev_queue_stats_tx *tx)
15951 {
15952 	struct bnxt *bp = netdev_priv(dev);
15953 
15954 	rx->packets = bp->net_stats_prev.rx_packets;
15955 	rx->bytes = bp->net_stats_prev.rx_bytes;
15956 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15957 	rx->hw_gro_packets = bp->ring_err_stats_prev.rx_total_hw_gro_packets;
15958 	rx->hw_gro_wire_packets = bp->ring_err_stats_prev.rx_total_hw_gro_wire_packets;
15959 
15960 	tx->packets = bp->net_stats_prev.tx_packets;
15961 	tx->bytes = bp->net_stats_prev.tx_bytes;
15962 }
15963 
15964 static const struct netdev_stat_ops bnxt_stat_ops = {
15965 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15966 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15967 	.get_base_stats		= bnxt_get_base_stats,
15968 };
15969 
15970 static void bnxt_queue_default_qcfg(struct net_device *dev,
15971 				    struct netdev_queue_config *qcfg)
15972 {
15973 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
15974 }
15975 
15976 static int bnxt_validate_qcfg(struct net_device *dev,
15977 			      struct netdev_queue_config *qcfg,
15978 			      struct netlink_ext_ack *extack)
15979 {
15980 	struct bnxt *bp = netdev_priv(dev);
15981 
15982 	/* Older chips need MSS calc so rx_page_size is not supported */
15983 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15984 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
15985 		return -EINVAL;
15986 
15987 	if (!is_power_of_2(qcfg->rx_page_size))
15988 		return -ERANGE;
15989 
15990 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
15991 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
15992 		return -ERANGE;
15993 
15994 	return 0;
15995 }
15996 
15997 static int bnxt_queue_mem_alloc(struct net_device *dev,
15998 				struct netdev_queue_config *qcfg,
15999 				void *qmem, int idx)
16000 {
16001 	struct bnxt_rx_ring_info *rxr, *clone;
16002 	struct bnxt *bp = netdev_priv(dev);
16003 	struct bnxt_ring_struct *ring;
16004 	int rc;
16005 
16006 	if (!bp->rx_ring)
16007 		return -ENETDOWN;
16008 
16009 	rxr = &bp->rx_ring[idx];
16010 	clone = qmem;
16011 	memcpy(clone, rxr, sizeof(*rxr));
16012 	bnxt_init_rx_ring_struct(bp, clone);
16013 	bnxt_reset_rx_ring_struct(bp, clone);
16014 
16015 	clone->rx_prod = 0;
16016 	clone->rx_agg_prod = 0;
16017 	clone->rx_sw_agg_prod = 0;
16018 	clone->rx_next_cons = 0;
16019 	clone->need_head_pool = false;
16020 	clone->rx_page_size = qcfg->rx_page_size;
16021 
16022 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16023 	if (rc)
16024 		return rc;
16025 
16026 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16027 	if (rc < 0)
16028 		goto err_page_pool_destroy;
16029 
16030 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16031 					MEM_TYPE_PAGE_POOL,
16032 					clone->page_pool);
16033 	if (rc)
16034 		goto err_rxq_info_unreg;
16035 
16036 	ring = &clone->rx_ring_struct;
16037 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16038 	if (rc)
16039 		goto err_free_rx_ring;
16040 
16041 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16042 		ring = &clone->rx_agg_ring_struct;
16043 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16044 		if (rc)
16045 			goto err_free_rx_agg_ring;
16046 
16047 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16048 		if (rc)
16049 			goto err_free_rx_agg_ring;
16050 	}
16051 
16052 	if (bp->flags & BNXT_FLAG_TPA) {
16053 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16054 		if (rc)
16055 			goto err_free_tpa_info;
16056 	}
16057 
16058 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16059 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16060 
16061 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16062 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16063 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16064 	if (bp->flags & BNXT_FLAG_TPA)
16065 		bnxt_alloc_one_tpa_info_data(bp, clone);
16066 
16067 	return 0;
16068 
16069 err_free_tpa_info:
16070 	bnxt_free_one_tpa_info(bp, clone);
16071 err_free_rx_agg_ring:
16072 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16073 err_free_rx_ring:
16074 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16075 err_rxq_info_unreg:
16076 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16077 err_page_pool_destroy:
16078 	page_pool_destroy(clone->page_pool);
16079 	page_pool_destroy(clone->head_pool);
16080 	clone->page_pool = NULL;
16081 	clone->head_pool = NULL;
16082 	return rc;
16083 }
16084 
16085 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16086 {
16087 	struct bnxt_rx_ring_info *rxr = qmem;
16088 	struct bnxt *bp = netdev_priv(dev);
16089 	struct bnxt_ring_struct *ring;
16090 
16091 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16092 	bnxt_free_one_tpa_info(bp, rxr);
16093 
16094 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16095 
16096 	page_pool_destroy(rxr->page_pool);
16097 	page_pool_destroy(rxr->head_pool);
16098 	rxr->page_pool = NULL;
16099 	rxr->head_pool = NULL;
16100 
16101 	ring = &rxr->rx_ring_struct;
16102 	bnxt_free_ring(bp, &ring->ring_mem);
16103 
16104 	ring = &rxr->rx_agg_ring_struct;
16105 	bnxt_free_ring(bp, &ring->ring_mem);
16106 
16107 	kfree(rxr->rx_agg_bmap);
16108 	rxr->rx_agg_bmap = NULL;
16109 }
16110 
16111 static void bnxt_copy_rx_ring(struct bnxt *bp,
16112 			      struct bnxt_rx_ring_info *dst,
16113 			      struct bnxt_rx_ring_info *src)
16114 {
16115 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16116 	struct bnxt_ring_struct *dst_ring, *src_ring;
16117 	int i;
16118 
16119 	dst_ring = &dst->rx_ring_struct;
16120 	dst_rmem = &dst_ring->ring_mem;
16121 	src_ring = &src->rx_ring_struct;
16122 	src_rmem = &src_ring->ring_mem;
16123 
16124 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16125 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16126 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16127 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16128 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16129 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16130 
16131 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16132 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16133 	*dst_rmem->vmem = *src_rmem->vmem;
16134 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16135 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16136 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16137 	}
16138 
16139 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16140 		return;
16141 
16142 	dst_ring = &dst->rx_agg_ring_struct;
16143 	dst_rmem = &dst_ring->ring_mem;
16144 	src_ring = &src->rx_agg_ring_struct;
16145 	src_rmem = &src_ring->ring_mem;
16146 
16147 	dst->rx_page_size = src->rx_page_size;
16148 
16149 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16150 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16151 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16152 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16153 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16154 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16155 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16156 
16157 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16158 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16159 	*dst_rmem->vmem = *src_rmem->vmem;
16160 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16161 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16162 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16163 	}
16164 
16165 	dst->rx_agg_bmap = src->rx_agg_bmap;
16166 }
16167 
16168 static int bnxt_queue_start(struct net_device *dev,
16169 			    struct netdev_queue_config *qcfg,
16170 			    void *qmem, int idx)
16171 {
16172 	struct bnxt *bp = netdev_priv(dev);
16173 	struct bnxt_rx_ring_info *rxr, *clone;
16174 	struct bnxt_cp_ring_info *cpr;
16175 	struct bnxt_vnic_info *vnic;
16176 	struct bnxt_napi *bnapi;
16177 	int i, rc;
16178 	u16 mru;
16179 
16180 	rxr = &bp->rx_ring[idx];
16181 	clone = qmem;
16182 
16183 	rxr->rx_prod = clone->rx_prod;
16184 	rxr->rx_agg_prod = clone->rx_agg_prod;
16185 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16186 	rxr->rx_next_cons = clone->rx_next_cons;
16187 	rxr->rx_tpa = clone->rx_tpa;
16188 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16189 	rxr->page_pool = clone->page_pool;
16190 	rxr->head_pool = clone->head_pool;
16191 	rxr->xdp_rxq = clone->xdp_rxq;
16192 	rxr->need_head_pool = clone->need_head_pool;
16193 
16194 	bnxt_copy_rx_ring(bp, rxr, clone);
16195 
16196 	bnapi = rxr->bnapi;
16197 	cpr = &bnapi->cp_ring;
16198 
16199 	/* All rings have been reserved and previously allocated.
16200 	 * Reallocating with the same parameters should never fail.
16201 	 */
16202 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16203 	if (rc)
16204 		goto err_reset;
16205 
16206 	if (bp->tph_mode) {
16207 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16208 		if (rc)
16209 			goto err_reset;
16210 	}
16211 
16212 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16213 	if (rc)
16214 		goto err_reset;
16215 
16216 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16217 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16218 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16219 
16220 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16221 		rc = bnxt_tx_queue_start(bp, idx);
16222 		if (rc)
16223 			goto err_reset;
16224 	}
16225 
16226 	bnxt_enable_rx_page_pool(rxr);
16227 	napi_enable_locked(&bnapi->napi);
16228 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16229 
16230 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16231 	for (i = 0; i < bp->nr_vnics; i++) {
16232 		vnic = &bp->vnic_info[i];
16233 
16234 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16235 		if (rc)
16236 			return rc;
16237 	}
16238 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16239 
16240 err_reset:
16241 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16242 		   rc);
16243 	napi_enable_locked(&bnapi->napi);
16244 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16245 	bnxt_reset_task(bp, true);
16246 	return rc;
16247 }
16248 
16249 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16250 {
16251 	struct bnxt *bp = netdev_priv(dev);
16252 	struct bnxt_rx_ring_info *rxr;
16253 	struct bnxt_cp_ring_info *cpr;
16254 	struct bnxt_vnic_info *vnic;
16255 	struct bnxt_napi *bnapi;
16256 	int i;
16257 
16258 	for (i = 0; i < bp->nr_vnics; i++) {
16259 		vnic = &bp->vnic_info[i];
16260 
16261 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16262 	}
16263 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16264 	/* Make sure NAPI sees that the VNIC is disabled */
16265 	synchronize_net();
16266 	rxr = &bp->rx_ring[idx];
16267 	bnapi = rxr->bnapi;
16268 	cpr = &bnapi->cp_ring;
16269 	cancel_work_sync(&cpr->dim.work);
16270 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16271 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16272 	page_pool_disable_direct_recycling(rxr->page_pool);
16273 	if (bnxt_separate_head_pool(rxr))
16274 		page_pool_disable_direct_recycling(rxr->head_pool);
16275 
16276 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16277 		bnxt_tx_queue_stop(bp, idx);
16278 
16279 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16280 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16281 	 * after seeing the completion.
16282 	 */
16283 	napi_disable_locked(&bnapi->napi);
16284 
16285 	if (bp->tph_mode) {
16286 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16287 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16288 	}
16289 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16290 
16291 	memcpy(qmem, rxr, sizeof(*rxr));
16292 	bnxt_init_rx_ring_struct(bp, qmem);
16293 
16294 	return 0;
16295 }
16296 
16297 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16298 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16299 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16300 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16301 	.ndo_queue_start	= bnxt_queue_start,
16302 	.ndo_queue_stop		= bnxt_queue_stop,
16303 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16304 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16305 	.supported_params	= QCFG_RX_PAGE_SIZE,
16306 };
16307 
16308 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16309 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16310 };
16311 
16312 static void bnxt_remove_one(struct pci_dev *pdev)
16313 {
16314 	struct net_device *dev = pci_get_drvdata(pdev);
16315 	struct bnxt *bp = netdev_priv(dev);
16316 
16317 	if (BNXT_PF(bp))
16318 		__bnxt_sriov_disable(bp);
16319 
16320 	bnxt_rdma_aux_device_del(bp);
16321 
16322 	unregister_netdev(dev);
16323 	bnxt_ptp_clear(bp);
16324 
16325 	bnxt_rdma_aux_device_uninit(bp);
16326 
16327 	bnxt_free_l2_filters(bp, true);
16328 	bnxt_free_ntp_fltrs(bp, true);
16329 	WARN_ON(bp->num_rss_ctx);
16330 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16331 	/* Flush any pending tasks */
16332 	cancel_work_sync(&bp->sp_task);
16333 	cancel_delayed_work_sync(&bp->fw_reset_task);
16334 	bp->sp_event = 0;
16335 
16336 	bnxt_dl_fw_reporters_destroy(bp);
16337 	bnxt_dl_unregister(bp);
16338 	bnxt_shutdown_tc(bp);
16339 
16340 	bnxt_clear_int_mode(bp);
16341 	bnxt_hwrm_func_drv_unrgtr(bp);
16342 	bnxt_free_hwrm_resources(bp);
16343 	bnxt_hwmon_uninit(bp);
16344 	bnxt_ethtool_free(bp);
16345 	bnxt_dcb_free(bp);
16346 	kfree(bp->ptp_cfg);
16347 	bp->ptp_cfg = NULL;
16348 	kfree(bp->fw_health);
16349 	bp->fw_health = NULL;
16350 	bnxt_cleanup_pci(bp);
16351 	bnxt_free_ctx_mem(bp, true);
16352 	bnxt_free_crash_dump_mem(bp);
16353 	kfree(bp->rss_indir_tbl);
16354 	bp->rss_indir_tbl = NULL;
16355 	bnxt_free_port_stats(bp);
16356 	free_netdev(dev);
16357 }
16358 
16359 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16360 {
16361 	int rc = 0;
16362 	struct bnxt_link_info *link_info = &bp->link_info;
16363 
16364 	bp->phy_flags = 0;
16365 	rc = bnxt_hwrm_phy_qcaps(bp);
16366 	if (rc) {
16367 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16368 			   rc);
16369 		return rc;
16370 	}
16371 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16372 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16373 	else
16374 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16375 
16376 	bp->mac_flags = 0;
16377 	bnxt_hwrm_mac_qcaps(bp);
16378 
16379 	if (!fw_dflt)
16380 		return 0;
16381 
16382 	mutex_lock(&bp->link_lock);
16383 	rc = bnxt_update_link(bp, false);
16384 	if (rc) {
16385 		mutex_unlock(&bp->link_lock);
16386 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16387 			   rc);
16388 		return rc;
16389 	}
16390 
16391 	/* Older firmware does not have supported_auto_speeds, so assume
16392 	 * that all supported speeds can be autonegotiated.
16393 	 */
16394 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16395 		link_info->support_auto_speeds = link_info->support_speeds;
16396 
16397 	bnxt_init_ethtool_link_settings(bp);
16398 	mutex_unlock(&bp->link_lock);
16399 	return 0;
16400 }
16401 
16402 static int bnxt_get_max_irq(struct pci_dev *pdev)
16403 {
16404 	u16 ctrl;
16405 
16406 	if (!pdev->msix_cap)
16407 		return 1;
16408 
16409 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16410 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16411 }
16412 
16413 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16414 				int *max_cp)
16415 {
16416 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16417 	int max_ring_grps = 0, max_irq;
16418 
16419 	*max_tx = hw_resc->max_tx_rings;
16420 	*max_rx = hw_resc->max_rx_rings;
16421 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16422 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16423 			bnxt_get_ulp_msix_num_in_use(bp),
16424 			hw_resc->max_stat_ctxs -
16425 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16426 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16427 		*max_cp = min_t(int, *max_cp, max_irq);
16428 	max_ring_grps = hw_resc->max_hw_ring_grps;
16429 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16430 		*max_cp -= 1;
16431 		*max_rx -= 2;
16432 	}
16433 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16434 		*max_rx >>= 1;
16435 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16436 		int rc;
16437 
16438 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16439 		if (rc) {
16440 			*max_rx = 0;
16441 			*max_tx = 0;
16442 		}
16443 		/* On P5 chips, max_cp output param should be available NQs */
16444 		*max_cp = max_irq;
16445 	}
16446 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16447 }
16448 
16449 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16450 {
16451 	int rx, tx, cp;
16452 
16453 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16454 	*max_rx = rx;
16455 	*max_tx = tx;
16456 	if (!rx || !tx || !cp)
16457 		return -ENOMEM;
16458 
16459 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16460 }
16461 
16462 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16463 			       bool shared)
16464 {
16465 	int rc;
16466 
16467 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16468 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16469 		/* Not enough rings, try disabling agg rings. */
16470 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16471 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16472 		if (rc) {
16473 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16474 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16475 			return rc;
16476 		}
16477 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16478 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16479 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16480 		bnxt_set_ring_params(bp);
16481 	}
16482 
16483 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16484 		int max_cp, max_stat, max_irq;
16485 
16486 		/* Reserve minimum resources for RoCE */
16487 		max_cp = bnxt_get_max_func_cp_rings(bp);
16488 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16489 		max_irq = bnxt_get_max_func_irqs(bp);
16490 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16491 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16492 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16493 			return 0;
16494 
16495 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16496 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16497 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16498 		max_cp = min_t(int, max_cp, max_irq);
16499 		max_cp = min_t(int, max_cp, max_stat);
16500 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16501 		if (rc)
16502 			rc = 0;
16503 	}
16504 	return rc;
16505 }
16506 
16507 /* In initial default shared ring setting, each shared ring must have a
16508  * RX/TX ring pair.
16509  */
16510 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16511 {
16512 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16513 	bp->rx_nr_rings = bp->cp_nr_rings;
16514 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16515 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16516 }
16517 
16518 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16519 {
16520 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16521 	int avail_msix;
16522 
16523 	if (!bnxt_can_reserve_rings(bp))
16524 		return 0;
16525 
16526 	if (sh)
16527 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16528 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16529 	/* Reduce default rings on multi-port cards so that total default
16530 	 * rings do not exceed CPU count.
16531 	 */
16532 	if (bp->port_count > 1) {
16533 		int max_rings =
16534 			max_t(int, num_online_cpus() / bp->port_count, 1);
16535 
16536 		dflt_rings = min_t(int, dflt_rings, max_rings);
16537 	}
16538 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16539 	if (rc)
16540 		return rc;
16541 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16542 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16543 	if (sh)
16544 		bnxt_trim_dflt_sh_rings(bp);
16545 	else
16546 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16547 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16548 
16549 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16550 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16551 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16552 
16553 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16554 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16555 	}
16556 
16557 	rc = __bnxt_reserve_rings(bp);
16558 	if (rc && rc != -ENODEV)
16559 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16560 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
16561 	if (sh)
16562 		bnxt_trim_dflt_sh_rings(bp);
16563 
16564 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16565 	if (bnxt_need_reserve_rings(bp)) {
16566 		rc = __bnxt_reserve_rings(bp);
16567 		if (rc && rc != -ENODEV)
16568 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16569 		bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
16570 	}
16571 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16572 		bp->rx_nr_rings++;
16573 		bp->cp_nr_rings++;
16574 	}
16575 	if (rc) {
16576 		bp->tx_nr_rings = 0;
16577 		bp->rx_nr_rings = 0;
16578 	}
16579 	return rc;
16580 }
16581 
16582 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16583 {
16584 	int rc;
16585 
16586 	if (bp->tx_nr_rings)
16587 		return 0;
16588 
16589 	bnxt_ulp_irq_stop(bp);
16590 	bnxt_clear_int_mode(bp);
16591 	rc = bnxt_set_dflt_rings(bp, true);
16592 	if (rc) {
16593 		if (BNXT_VF(bp) && rc == -ENODEV)
16594 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16595 		else
16596 			netdev_err(bp->dev, "Not enough rings available.\n");
16597 		goto init_dflt_ring_err;
16598 	}
16599 	rc = bnxt_init_int_mode(bp);
16600 	if (rc)
16601 		goto init_dflt_ring_err;
16602 
16603 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
16604 
16605 	bnxt_set_dflt_rfs(bp);
16606 
16607 init_dflt_ring_err:
16608 	bnxt_ulp_irq_restart(bp, rc);
16609 	return rc;
16610 }
16611 
16612 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16613 {
16614 	int rc;
16615 
16616 	netdev_ops_assert_locked(bp->dev);
16617 	bnxt_hwrm_func_qcaps(bp);
16618 
16619 	if (netif_running(bp->dev))
16620 		__bnxt_close_nic(bp, true, false);
16621 
16622 	bnxt_ulp_irq_stop(bp);
16623 	bnxt_clear_int_mode(bp);
16624 	rc = bnxt_init_int_mode(bp);
16625 	bnxt_ulp_irq_restart(bp, rc);
16626 
16627 	if (netif_running(bp->dev)) {
16628 		if (rc)
16629 			netif_close(bp->dev);
16630 		else
16631 			rc = bnxt_open_nic(bp, true, false);
16632 	}
16633 
16634 	return rc;
16635 }
16636 
16637 static int bnxt_init_mac_addr(struct bnxt *bp)
16638 {
16639 	int rc = 0;
16640 
16641 	if (BNXT_PF(bp)) {
16642 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16643 	} else {
16644 #ifdef CONFIG_BNXT_SRIOV
16645 		struct bnxt_vf_info *vf = &bp->vf;
16646 		bool strict_approval = true;
16647 
16648 		if (is_valid_ether_addr(vf->mac_addr)) {
16649 			/* overwrite netdev dev_addr with admin VF MAC */
16650 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16651 			/* Older PF driver or firmware may not approve this
16652 			 * correctly.
16653 			 */
16654 			strict_approval = false;
16655 		} else {
16656 			eth_hw_addr_random(bp->dev);
16657 		}
16658 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16659 #endif
16660 	}
16661 	return rc;
16662 }
16663 
16664 static void bnxt_vpd_read_info(struct bnxt *bp)
16665 {
16666 	struct pci_dev *pdev = bp->pdev;
16667 	unsigned int vpd_size, kw_len;
16668 	int pos, size;
16669 	u8 *vpd_data;
16670 
16671 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16672 	if (IS_ERR(vpd_data)) {
16673 		pci_warn(pdev, "Unable to read VPD\n");
16674 		return;
16675 	}
16676 
16677 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16678 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16679 	if (pos < 0)
16680 		goto read_sn;
16681 
16682 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16683 	memcpy(bp->board_partno, &vpd_data[pos], size);
16684 
16685 read_sn:
16686 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16687 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16688 					   &kw_len);
16689 	if (pos < 0)
16690 		goto exit;
16691 
16692 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16693 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16694 exit:
16695 	kfree(vpd_data);
16696 }
16697 
16698 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16699 {
16700 	struct pci_dev *pdev = bp->pdev;
16701 	u64 qword;
16702 
16703 	qword = pci_get_dsn(pdev);
16704 	if (!qword) {
16705 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16706 		return -EOPNOTSUPP;
16707 	}
16708 
16709 	put_unaligned_le64(qword, dsn);
16710 
16711 	bp->flags |= BNXT_FLAG_DSN_VALID;
16712 	return 0;
16713 }
16714 
16715 static int bnxt_map_db_bar(struct bnxt *bp)
16716 {
16717 	if (!bp->db_size)
16718 		return -ENODEV;
16719 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16720 	if (!bp->bar1)
16721 		return -ENOMEM;
16722 	return 0;
16723 }
16724 
16725 void bnxt_print_device_info(struct bnxt *bp)
16726 {
16727 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16728 		    board_info[bp->board_idx].name,
16729 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16730 
16731 	pcie_print_link_status(bp->pdev);
16732 }
16733 
16734 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16735 {
16736 	struct bnxt_hw_resc *hw_resc;
16737 	struct net_device *dev;
16738 	struct bnxt *bp;
16739 	int rc, max_irqs;
16740 
16741 	if (pci_is_bridge(pdev))
16742 		return -ENODEV;
16743 
16744 	if (!pdev->msix_cap) {
16745 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16746 		return -ENODEV;
16747 	}
16748 
16749 	/* Clear any pending DMA transactions from crash kernel
16750 	 * while loading driver in capture kernel.
16751 	 */
16752 	if (is_kdump_kernel()) {
16753 		pci_clear_master(pdev);
16754 		pcie_flr(pdev);
16755 	}
16756 
16757 	max_irqs = bnxt_get_max_irq(pdev);
16758 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16759 				 max_irqs);
16760 	if (!dev)
16761 		return -ENOMEM;
16762 
16763 	bp = netdev_priv(dev);
16764 	bp->board_idx = ent->driver_data;
16765 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16766 	bnxt_set_max_func_irqs(bp, max_irqs);
16767 
16768 	if (bnxt_vf_pciid(bp->board_idx))
16769 		bp->flags |= BNXT_FLAG_VF;
16770 
16771 	/* No devlink port registration in case of a VF */
16772 	if (BNXT_PF(bp))
16773 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16774 
16775 	rc = bnxt_init_board(pdev, dev);
16776 	if (rc < 0)
16777 		goto init_err_free;
16778 
16779 	dev->netdev_ops = &bnxt_netdev_ops;
16780 	dev->stat_ops = &bnxt_stat_ops;
16781 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16782 	dev->ethtool_ops = &bnxt_ethtool_ops;
16783 	pci_set_drvdata(pdev, dev);
16784 
16785 	rc = bnxt_alloc_hwrm_resources(bp);
16786 	if (rc)
16787 		goto init_err_pci_clean;
16788 
16789 	mutex_init(&bp->hwrm_cmd_lock);
16790 	mutex_init(&bp->link_lock);
16791 
16792 	rc = bnxt_fw_init_one_p1(bp);
16793 	if (rc)
16794 		goto init_err_pci_clean;
16795 
16796 	if (BNXT_PF(bp))
16797 		bnxt_vpd_read_info(bp);
16798 
16799 	if (BNXT_CHIP_P5_PLUS(bp)) {
16800 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16801 		if (BNXT_CHIP_P7(bp))
16802 			bp->flags |= BNXT_FLAG_CHIP_P7;
16803 	}
16804 
16805 	rc = bnxt_alloc_rss_indir_tbl(bp);
16806 	if (rc)
16807 		goto init_err_pci_clean;
16808 
16809 	rc = bnxt_fw_init_one_p2(bp);
16810 	if (rc)
16811 		goto init_err_pci_clean;
16812 
16813 	rc = bnxt_map_db_bar(bp);
16814 	if (rc) {
16815 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16816 			rc);
16817 		goto init_err_pci_clean;
16818 	}
16819 
16820 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16821 			   NETIF_F_TSO | NETIF_F_TSO6 |
16822 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16823 			   NETIF_F_GSO_IPXIP4 |
16824 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16825 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16826 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16827 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16828 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16829 
16830 	if (BNXT_SUPPORTS_TPA(bp))
16831 		dev->hw_features |= NETIF_F_LRO;
16832 
16833 	dev->hw_enc_features =
16834 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16835 			NETIF_F_TSO | NETIF_F_TSO6 |
16836 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16837 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16838 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16839 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16840 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16841 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16842 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16843 	else
16844 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16845 
16846 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16847 				    NETIF_F_GSO_GRE_CSUM;
16848 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16849 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16850 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16851 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16852 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16853 	if (BNXT_SUPPORTS_TPA(bp))
16854 		dev->hw_features |= NETIF_F_GRO_HW;
16855 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16856 	if (dev->features & NETIF_F_GRO_HW)
16857 		dev->features &= ~NETIF_F_LRO;
16858 	dev->priv_flags |= IFF_UNICAST_FLT;
16859 
16860 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16861 	if (bp->tso_max_segs)
16862 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16863 
16864 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16865 			    NETDEV_XDP_ACT_RX_SG;
16866 
16867 #ifdef CONFIG_BNXT_SRIOV
16868 	init_waitqueue_head(&bp->sriov_cfg_wait);
16869 #endif
16870 	if (BNXT_SUPPORTS_TPA(bp)) {
16871 		bp->gro_func = bnxt_gro_func_5730x;
16872 		if (BNXT_CHIP_P4(bp))
16873 			bp->gro_func = bnxt_gro_func_5731x;
16874 		else if (BNXT_CHIP_P5_PLUS(bp))
16875 			bp->gro_func = bnxt_gro_func_5750x;
16876 	}
16877 	if (!BNXT_CHIP_P4_PLUS(bp))
16878 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16879 
16880 	rc = bnxt_init_mac_addr(bp);
16881 	if (rc) {
16882 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16883 		rc = -EADDRNOTAVAIL;
16884 		goto init_err_pci_clean;
16885 	}
16886 
16887 	if (BNXT_PF(bp)) {
16888 		/* Read the adapter's DSN to use as the eswitch switch_id */
16889 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16890 	}
16891 
16892 	/* MTU range: 60 - FW defined max */
16893 	dev->min_mtu = ETH_ZLEN;
16894 	dev->max_mtu = bp->max_mtu;
16895 
16896 	rc = bnxt_probe_phy(bp, true);
16897 	if (rc)
16898 		goto init_err_pci_clean;
16899 
16900 	hw_resc = &bp->hw_resc;
16901 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16902 		       BNXT_L2_FLTR_MAX_FLTR;
16903 	/* Older firmware may not report these filters properly */
16904 	if (bp->max_fltr < BNXT_MAX_FLTR)
16905 		bp->max_fltr = BNXT_MAX_FLTR;
16906 	bnxt_init_l2_fltr_tbl(bp);
16907 	__bnxt_set_rx_skb_mode(bp, false);
16908 	bnxt_set_tpa_flags(bp);
16909 	bnxt_init_ring_params(bp);
16910 	bnxt_set_ring_params(bp);
16911 	bnxt_rdma_aux_device_init(bp);
16912 	rc = bnxt_set_dflt_rings(bp, true);
16913 	if (rc) {
16914 		if (BNXT_VF(bp) && rc == -ENODEV) {
16915 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16916 		} else {
16917 			netdev_err(bp->dev, "Not enough rings available.\n");
16918 			rc = -ENOMEM;
16919 		}
16920 		goto init_err_pci_clean;
16921 	}
16922 
16923 	bnxt_fw_init_one_p3(bp);
16924 
16925 	bnxt_init_dflt_coal(bp);
16926 
16927 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16928 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16929 
16930 	rc = bnxt_init_int_mode(bp);
16931 	if (rc)
16932 		goto init_err_pci_clean;
16933 
16934 	/* No TC has been set yet and rings may have been trimmed due to
16935 	 * limited MSIX, so we re-initialize the TX rings per TC.
16936 	 */
16937 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16938 
16939 	if (BNXT_PF(bp)) {
16940 		if (!bnxt_pf_wq) {
16941 			bnxt_pf_wq =
16942 				create_singlethread_workqueue("bnxt_pf_wq");
16943 			if (!bnxt_pf_wq) {
16944 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16945 				rc = -ENOMEM;
16946 				goto init_err_pci_clean;
16947 			}
16948 		}
16949 		rc = bnxt_init_tc(bp);
16950 		if (rc)
16951 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16952 				   rc);
16953 	}
16954 
16955 	bnxt_inv_fw_health_reg(bp);
16956 	rc = bnxt_dl_register(bp);
16957 	if (rc)
16958 		goto init_err_dl;
16959 
16960 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16961 
16962 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16963 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16964 
16965 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
16966 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16967 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16968 	dev->netmem_tx = true;
16969 
16970 	rc = register_netdev(dev);
16971 	if (rc)
16972 		goto init_err_cleanup;
16973 
16974 	bnxt_dl_fw_reporters_create(bp);
16975 
16976 	bnxt_rdma_aux_device_add(bp);
16977 
16978 	bnxt_print_device_info(bp);
16979 
16980 	pci_save_state(pdev);
16981 
16982 	return 0;
16983 init_err_cleanup:
16984 	bnxt_rdma_aux_device_uninit(bp);
16985 	bnxt_dl_unregister(bp);
16986 init_err_dl:
16987 	bnxt_shutdown_tc(bp);
16988 	bnxt_clear_int_mode(bp);
16989 
16990 init_err_pci_clean:
16991 	bnxt_hwrm_func_drv_unrgtr(bp);
16992 	bnxt_ptp_clear(bp);
16993 	kfree(bp->ptp_cfg);
16994 	bp->ptp_cfg = NULL;
16995 	bnxt_free_hwrm_resources(bp);
16996 	bnxt_hwmon_uninit(bp);
16997 	bnxt_ethtool_free(bp);
16998 	kfree(bp->fw_health);
16999 	bp->fw_health = NULL;
17000 	bnxt_cleanup_pci(bp);
17001 	bnxt_free_ctx_mem(bp, true);
17002 	bnxt_free_crash_dump_mem(bp);
17003 	kfree(bp->rss_indir_tbl);
17004 	bp->rss_indir_tbl = NULL;
17005 
17006 init_err_free:
17007 	free_netdev(dev);
17008 	return rc;
17009 }
17010 
17011 static void bnxt_shutdown(struct pci_dev *pdev)
17012 {
17013 	struct net_device *dev = pci_get_drvdata(pdev);
17014 	struct bnxt *bp;
17015 
17016 	if (!dev)
17017 		return;
17018 
17019 	rtnl_lock();
17020 	netdev_lock(dev);
17021 	bp = netdev_priv(dev);
17022 	if (!bp)
17023 		goto shutdown_exit;
17024 
17025 	if (netif_running(dev))
17026 		netif_close(dev);
17027 
17028 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17029 		pcie_flr(pdev);
17030 		goto shutdown_exit;
17031 	}
17032 	bnxt_ptp_clear(bp);
17033 	bnxt_clear_int_mode(bp);
17034 	pci_disable_device(pdev);
17035 
17036 	if (system_state == SYSTEM_POWER_OFF) {
17037 		pci_wake_from_d3(pdev, bp->wol);
17038 		pci_set_power_state(pdev, PCI_D3hot);
17039 	}
17040 
17041 shutdown_exit:
17042 	netdev_unlock(dev);
17043 	rtnl_unlock();
17044 }
17045 
17046 #ifdef CONFIG_PM_SLEEP
17047 static int bnxt_suspend(struct device *device)
17048 {
17049 	struct net_device *dev = dev_get_drvdata(device);
17050 	struct bnxt *bp = netdev_priv(dev);
17051 	int rc = 0;
17052 
17053 	bnxt_ulp_stop(bp);
17054 
17055 	netdev_lock(dev);
17056 	if (netif_running(dev)) {
17057 		netif_device_detach(dev);
17058 		rc = bnxt_close(dev);
17059 	}
17060 	bnxt_hwrm_func_drv_unrgtr(bp);
17061 	bnxt_ptp_clear(bp);
17062 	pci_disable_device(bp->pdev);
17063 	bnxt_free_ctx_mem(bp, false);
17064 	netdev_unlock(dev);
17065 	return rc;
17066 }
17067 
17068 static int bnxt_resume(struct device *device)
17069 {
17070 	struct net_device *dev = dev_get_drvdata(device);
17071 	struct bnxt *bp = netdev_priv(dev);
17072 	int rc = 0;
17073 
17074 	netdev_lock(dev);
17075 	rc = pci_enable_device(bp->pdev);
17076 	if (rc) {
17077 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17078 			   rc);
17079 		goto resume_exit;
17080 	}
17081 	pci_set_master(bp->pdev);
17082 	if (bnxt_hwrm_ver_get(bp)) {
17083 		rc = -ENODEV;
17084 		goto resume_exit;
17085 	}
17086 	rc = bnxt_hwrm_func_reset(bp);
17087 	if (rc) {
17088 		rc = -EBUSY;
17089 		goto resume_exit;
17090 	}
17091 
17092 	rc = bnxt_hwrm_func_qcaps(bp);
17093 	if (rc)
17094 		goto resume_exit;
17095 
17096 	bnxt_clear_reservations(bp, true);
17097 
17098 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17099 		rc = -ENODEV;
17100 		goto resume_exit;
17101 	}
17102 	if (bp->fw_crash_mem)
17103 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17104 
17105 	if (bnxt_ptp_init(bp)) {
17106 		kfree(bp->ptp_cfg);
17107 		bp->ptp_cfg = NULL;
17108 	}
17109 	bnxt_get_wol_settings(bp);
17110 	if (netif_running(dev)) {
17111 		rc = bnxt_open(dev);
17112 		if (!rc)
17113 			netif_device_attach(dev);
17114 	}
17115 
17116 resume_exit:
17117 	netdev_unlock(bp->dev);
17118 	bnxt_ulp_start(bp, rc);
17119 	if (!rc)
17120 		bnxt_reenable_sriov(bp);
17121 	return rc;
17122 }
17123 
17124 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17125 #define BNXT_PM_OPS (&bnxt_pm_ops)
17126 
17127 #else
17128 
17129 #define BNXT_PM_OPS NULL
17130 
17131 #endif /* CONFIG_PM_SLEEP */
17132 
17133 /**
17134  * bnxt_io_error_detected - called when PCI error is detected
17135  * @pdev: Pointer to PCI device
17136  * @state: The current pci connection state
17137  *
17138  * This function is called after a PCI bus error affecting
17139  * this device has been detected.
17140  */
17141 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17142 					       pci_channel_state_t state)
17143 {
17144 	struct net_device *netdev = pci_get_drvdata(pdev);
17145 	struct bnxt *bp = netdev_priv(netdev);
17146 	bool abort = false;
17147 
17148 	netdev_info(netdev, "PCI I/O error detected\n");
17149 
17150 	bnxt_ulp_stop(bp);
17151 
17152 	netdev_lock(netdev);
17153 	netif_device_detach(netdev);
17154 
17155 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17156 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17157 		abort = true;
17158 	}
17159 
17160 	if (abort || state == pci_channel_io_perm_failure) {
17161 		netdev_unlock(netdev);
17162 		return PCI_ERS_RESULT_DISCONNECT;
17163 	}
17164 
17165 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17166 	 * so we disable bus master to prevent any potential bad DMAs before
17167 	 * freeing kernel memory.
17168 	 */
17169 	if (state == pci_channel_io_frozen) {
17170 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
17171 		bnxt_fw_fatal_close(bp);
17172 	}
17173 
17174 	if (netif_running(netdev))
17175 		__bnxt_close_nic(bp, true, true);
17176 
17177 	if (pci_is_enabled(pdev))
17178 		pci_disable_device(pdev);
17179 	bnxt_free_ctx_mem(bp, false);
17180 	netdev_unlock(netdev);
17181 
17182 	/* Request a slot reset. */
17183 	return PCI_ERS_RESULT_NEED_RESET;
17184 }
17185 
17186 /**
17187  * bnxt_io_slot_reset - called after the pci bus has been reset.
17188  * @pdev: Pointer to PCI device
17189  *
17190  * Restart the card from scratch, as if from a cold-boot.
17191  * At this point, the card has experienced a hard reset,
17192  * followed by fixups by BIOS, and has its config space
17193  * set up identically to what it was at cold boot.
17194  */
17195 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17196 {
17197 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17198 	struct net_device *netdev = pci_get_drvdata(pdev);
17199 	struct bnxt *bp = netdev_priv(netdev);
17200 	int retry = 0;
17201 	int err = 0;
17202 	int off;
17203 
17204 	netdev_info(bp->dev, "PCI Slot Reset\n");
17205 
17206 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17207 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17208 		msleep(900);
17209 
17210 	netdev_lock(netdev);
17211 
17212 	if (pci_enable_device(pdev)) {
17213 		dev_err(&pdev->dev,
17214 			"Cannot re-enable PCI device after reset.\n");
17215 	} else {
17216 		pci_set_master(pdev);
17217 		/* Upon fatal error, our device internal logic that latches to
17218 		 * BAR value is getting reset and will restore only upon
17219 		 * rewriting the BARs.
17220 		 *
17221 		 * As pci_restore_state() does not re-write the BARs if the
17222 		 * value is same as saved value earlier, driver needs to
17223 		 * write the BARs to 0 to force restore, in case of fatal error.
17224 		 */
17225 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17226 				       &bp->state)) {
17227 			for (off = PCI_BASE_ADDRESS_0;
17228 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17229 				pci_write_config_dword(bp->pdev, off, 0);
17230 		}
17231 		pci_restore_state(pdev);
17232 		pci_save_state(pdev);
17233 
17234 		bnxt_inv_fw_health_reg(bp);
17235 		bnxt_try_map_fw_health_reg(bp);
17236 
17237 		/* In some PCIe AER scenarios, firmware may take up to
17238 		 * 10 seconds to become ready in the worst case.
17239 		 */
17240 		do {
17241 			err = bnxt_try_recover_fw(bp);
17242 			if (!err)
17243 				break;
17244 			retry++;
17245 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17246 
17247 		if (err) {
17248 			dev_err(&pdev->dev, "Firmware not ready\n");
17249 			goto reset_exit;
17250 		}
17251 
17252 		err = bnxt_hwrm_func_reset(bp);
17253 		if (!err)
17254 			result = PCI_ERS_RESULT_RECOVERED;
17255 
17256 		/* IRQ will be initialized later in bnxt_io_resume */
17257 		bnxt_ulp_irq_stop(bp);
17258 		bnxt_clear_int_mode(bp);
17259 	}
17260 
17261 reset_exit:
17262 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17263 	bnxt_clear_reservations(bp, true);
17264 	netdev_unlock(netdev);
17265 
17266 	return result;
17267 }
17268 
17269 /**
17270  * bnxt_io_resume - called when traffic can start flowing again.
17271  * @pdev: Pointer to PCI device
17272  *
17273  * This callback is called when the error recovery driver tells
17274  * us that its OK to resume normal operation.
17275  */
17276 static void bnxt_io_resume(struct pci_dev *pdev)
17277 {
17278 	struct net_device *netdev = pci_get_drvdata(pdev);
17279 	struct bnxt *bp = netdev_priv(netdev);
17280 	int err;
17281 
17282 	netdev_info(bp->dev, "PCI Slot Resume\n");
17283 	netdev_lock(netdev);
17284 
17285 	err = bnxt_hwrm_func_qcaps(bp);
17286 	if (!err) {
17287 		if (netif_running(netdev)) {
17288 			err = bnxt_open(netdev);
17289 		} else {
17290 			err = bnxt_reserve_rings(bp, true);
17291 			if (!err)
17292 				err = bnxt_init_int_mode(bp);
17293 		}
17294 	}
17295 
17296 	if (!err)
17297 		netif_device_attach(netdev);
17298 
17299 	netdev_unlock(netdev);
17300 	bnxt_ulp_start(bp, err);
17301 	if (!err)
17302 		bnxt_reenable_sriov(bp);
17303 }
17304 
17305 static const struct pci_error_handlers bnxt_err_handler = {
17306 	.error_detected	= bnxt_io_error_detected,
17307 	.slot_reset	= bnxt_io_slot_reset,
17308 	.resume		= bnxt_io_resume
17309 };
17310 
17311 static struct pci_driver bnxt_pci_driver = {
17312 	.name		= DRV_MODULE_NAME,
17313 	.id_table	= bnxt_pci_tbl,
17314 	.probe		= bnxt_init_one,
17315 	.remove		= bnxt_remove_one,
17316 	.shutdown	= bnxt_shutdown,
17317 	.driver.pm	= BNXT_PM_OPS,
17318 	.err_handler	= &bnxt_err_handler,
17319 #if defined(CONFIG_BNXT_SRIOV)
17320 	.sriov_configure = bnxt_sriov_configure,
17321 #endif
17322 };
17323 
17324 static int __init bnxt_init(void)
17325 {
17326 	int err;
17327 
17328 	bnxt_debug_init();
17329 	err = pci_register_driver(&bnxt_pci_driver);
17330 	if (err) {
17331 		bnxt_debug_exit();
17332 		return err;
17333 	}
17334 
17335 	return 0;
17336 }
17337 
17338 static void __exit bnxt_exit(void)
17339 {
17340 	pci_unregister_driver(&bnxt_pci_driver);
17341 	if (bnxt_pf_wq)
17342 		destroy_workqueue(bnxt_pf_wq);
17343 	bnxt_debug_exit();
17344 }
17345 
17346 module_init(bnxt_init);
17347 module_exit(bnxt_exit);
17348