xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 87fe97a184c000a3941e2b53671742993abb1ddc)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 #include <linux/bnxt/hsi.h>
62 #include <linux/bnxt/ulp.h>
63 
64 #include "bnxt.h"
65 #include "bnxt_hwrm.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 #include "bnxt_gso.h"
78 #include <net/tso.h>
79 
80 #define BNXT_TX_TIMEOUT		(5 * HZ)
81 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
82 				 NETIF_MSG_TX_ERR)
83 
84 MODULE_IMPORT_NS("NETDEV_INTERNAL");
85 MODULE_LICENSE("GPL");
86 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
87 
88 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
89 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
90 
91 #define BNXT_TX_PUSH_THRESH 164
92 
93 /* indexed by enum board_idx */
94 static const struct {
95 	char *name;
96 } board_info[] = {
97 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
100 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
101 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
102 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
103 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
104 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
108 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
109 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
110 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
112 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
113 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
114 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
115 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
116 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
117 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
118 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
119 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
120 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
121 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
122 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
123 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
124 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
125 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
126 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
127 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
128 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
129 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
130 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
131 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
132 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
133 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
134 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
135 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
136 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
137 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
138 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
139 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
141 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
142 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
143 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
145 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
146 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
147 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
148 };
149 
150 static const struct pci_device_id bnxt_pci_tbl[] = {
151 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
155 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
170 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
183 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
184 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
185 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
192 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
193 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
198 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
199 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
200 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
201 #ifdef CONFIG_BNXT_SRIOV
202 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
219 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
220 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
221 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
223 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
224 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
225 #endif
226 	{ 0 }
227 };
228 
229 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
230 
231 static const u16 bnxt_vf_req_snif[] = {
232 	HWRM_FUNC_CFG,
233 	HWRM_FUNC_VF_CFG,
234 	HWRM_PORT_PHY_QCFG,
235 	HWRM_CFA_L2_FILTER_ALLOC,
236 };
237 
238 static const u16 bnxt_async_events_arr[] = {
239 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
242 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
243 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
246 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
247 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
248 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
249 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
250 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
253 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
254 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
255 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
256 };
257 
258 const u16 bnxt_bstore_to_trace[] = {
259 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
260 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
261 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
262 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
263 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
264 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
265 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
266 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
267 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
268 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
269 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
270 	[BNXT_CTX_KONG]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE,
271 	[BNXT_CTX_QPC]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE,
272 };
273 
274 static struct workqueue_struct *bnxt_pf_wq;
275 
276 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
277 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
278 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
279 
280 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
281 	.ports = {
282 		.src = 0,
283 		.dst = 0,
284 	},
285 	.addrs = {
286 		.v6addrs = {
287 			.src = BNXT_IPV6_MASK_NONE,
288 			.dst = BNXT_IPV6_MASK_NONE,
289 		},
290 	},
291 };
292 
293 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
294 	.ports = {
295 		.src = cpu_to_be16(0xffff),
296 		.dst = cpu_to_be16(0xffff),
297 	},
298 	.addrs = {
299 		.v6addrs = {
300 			.src = BNXT_IPV6_MASK_ALL,
301 			.dst = BNXT_IPV6_MASK_ALL,
302 		},
303 	},
304 };
305 
306 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
307 	.ports = {
308 		.src = cpu_to_be16(0xffff),
309 		.dst = cpu_to_be16(0xffff),
310 	},
311 	.addrs = {
312 		.v4addrs = {
313 			.src = cpu_to_be32(0xffffffff),
314 			.dst = cpu_to_be32(0xffffffff),
315 		},
316 	},
317 };
318 
319 static bool bnxt_vf_pciid(enum board_idx idx)
320 {
321 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
322 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
323 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
324 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
325 		idx == NETXTREME_E_P7_VF_HV);
326 }
327 
328 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
329 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
330 
331 #define BNXT_DB_CQ(db, idx)						\
332 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_NQ_P5(db, idx)						\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
336 		    (db)->doorbell)
337 
338 #define BNXT_DB_NQ_P7(db, idx)						\
339 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
340 		    DB_RING_IDX(db, idx), (db)->doorbell)
341 
342 #define BNXT_DB_CQ_ARM(db, idx)						\
343 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
344 
345 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
346 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
347 		    DB_RING_IDX(db, idx), (db)->doorbell)
348 
349 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
350 {
351 	if (bp->flags & BNXT_FLAG_CHIP_P7)
352 		BNXT_DB_NQ_P7(db, idx);
353 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
354 		BNXT_DB_NQ_P5(db, idx);
355 	else
356 		BNXT_DB_CQ(db, idx);
357 }
358 
359 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
360 {
361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
362 		BNXT_DB_NQ_ARM_P5(db, idx);
363 	else
364 		BNXT_DB_CQ_ARM(db, idx);
365 }
366 
367 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
368 {
369 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
370 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
371 			    DB_RING_IDX(db, idx), db->doorbell);
372 	else
373 		BNXT_DB_CQ(db, idx);
374 }
375 
376 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
377 {
378 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
379 		return;
380 
381 	if (BNXT_PF(bp))
382 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
383 	else
384 		schedule_delayed_work(&bp->fw_reset_task, delay);
385 }
386 
387 static void __bnxt_queue_sp_work(struct bnxt *bp)
388 {
389 	if (BNXT_PF(bp))
390 		queue_work(bnxt_pf_wq, &bp->sp_task);
391 	else
392 		schedule_work(&bp->sp_task);
393 }
394 
395 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
396 {
397 	set_bit(event, &bp->sp_event);
398 	__bnxt_queue_sp_work(bp);
399 }
400 
401 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
402 {
403 	if (!rxr->bnapi->in_reset) {
404 		rxr->bnapi->in_reset = true;
405 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
406 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
407 		else
408 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
409 		__bnxt_queue_sp_work(bp);
410 	}
411 	rxr->rx_next_cons = 0xffff;
412 }
413 
414 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
415 			  u16 curr)
416 {
417 	struct bnxt_napi *bnapi = txr->bnapi;
418 
419 	if (bnapi->tx_fault)
420 		return;
421 
422 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
423 		   txr->txq_index, txr->tx_hw_cons,
424 		   txr->tx_cons, txr->tx_prod, curr);
425 	WARN_ON_ONCE(1);
426 	bnapi->tx_fault = 1;
427 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
428 }
429 
430 const u16 bnxt_lhint_arr[] = {
431 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
432 	TX_BD_FLAGS_LHINT_512_TO_1023,
433 	TX_BD_FLAGS_LHINT_1024_TO_2047,
434 	TX_BD_FLAGS_LHINT_1024_TO_2047,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
444 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
445 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
446 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
447 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
448 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
449 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
450 };
451 
452 u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
453 {
454 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
455 
456 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
457 		return 0;
458 
459 	return md_dst->u.port_info.port_id;
460 }
461 
462 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
463 			     u16 prod)
464 {
465 	/* Sync BD data before updating doorbell */
466 	wmb();
467 	bnxt_db_write(bp, &txr->tx_db, prod);
468 	txr->kick_pending = 0;
469 }
470 
471 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
472 {
473 	struct bnxt *bp = netdev_priv(dev);
474 	struct tx_bd *txbd, *txbd0;
475 	struct tx_bd_ext *txbd1;
476 	struct netdev_queue *txq;
477 	int i;
478 	dma_addr_t mapping;
479 	unsigned int length, pad = 0;
480 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
481 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
482 	struct pci_dev *pdev = bp->pdev;
483 	u16 prod, last_frag, txts_prod;
484 	struct bnxt_tx_ring_info *txr;
485 	struct bnxt_sw_tx_bd *tx_buf;
486 	__le32 lflags = 0;
487 	skb_frag_t *frag;
488 
489 	i = skb_get_queue_mapping(skb);
490 	if (unlikely(i >= bp->tx_nr_rings)) {
491 		dev_kfree_skb_any(skb);
492 		dev_core_stats_tx_dropped_inc(dev);
493 		return NETDEV_TX_OK;
494 	}
495 
496 	txq = netdev_get_tx_queue(dev, i);
497 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
498 	prod = txr->tx_prod;
499 
500 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
501 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
502 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
503 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
504 		if (skb_linearize(skb)) {
505 			dev_kfree_skb_any(skb);
506 			dev_core_stats_tx_dropped_inc(dev);
507 			return NETDEV_TX_OK;
508 		}
509 	}
510 #endif
511 	if (skb_is_gso(skb) &&
512 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
513 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP))
514 		return bnxt_sw_udp_gso_xmit(bp, txr, txq, skb);
515 
516 	free_size = bnxt_tx_avail(bp, txr);
517 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
518 		/* We must have raced with NAPI cleanup */
519 		if (net_ratelimit() && txr->kick_pending)
520 			netif_warn(bp, tx_err, dev,
521 				   "bnxt: ring busy w/ flush pending!\n");
522 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
523 					bp->tx_wake_thresh))
524 			return NETDEV_TX_BUSY;
525 	}
526 
527 	length = skb->len;
528 	len = skb_headlen(skb);
529 	last_frag = skb_shinfo(skb)->nr_frags;
530 
531 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
532 
533 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
534 	tx_buf->skb = skb;
535 	tx_buf->nr_frags = last_frag;
536 
537 	vlan_tag_flags = 0;
538 	cfa_action = bnxt_xmit_get_cfa_action(skb);
539 	if (skb_vlan_tag_present(skb)) {
540 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
541 				 skb_vlan_tag_get(skb);
542 		/* Currently supports 8021Q, 8021AD vlan offloads
543 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
544 		 */
545 		if (skb->vlan_proto == htons(ETH_P_8021Q))
546 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
547 	}
548 
549 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
550 	    ptp->tx_tstamp_en) {
551 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
552 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
553 			tx_buf->is_ts_pkt = 1;
554 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
555 		} else if (!skb_is_gso(skb)) {
556 			u16 seq_id, hdr_off;
557 
558 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
559 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
560 				if (vlan_tag_flags)
561 					hdr_off += VLAN_HLEN;
562 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
563 				tx_buf->is_ts_pkt = 1;
564 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
565 
566 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
567 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
568 				tx_buf->txts_prod = txts_prod;
569 			}
570 		}
571 	}
572 	if (unlikely(skb->no_fcs))
573 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
574 
575 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
576 	    skb_frags_readable(skb) && !lflags) {
577 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
578 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
579 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
580 		void __iomem *db = txr->tx_db.doorbell;
581 		void *pdata = tx_push_buf->data;
582 		u64 *end;
583 		int j, push_len;
584 
585 		/* Set COAL_NOW to be ready quickly for the next push */
586 		tx_push->tx_bd_len_flags_type =
587 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
588 					TX_BD_TYPE_LONG_TX_BD |
589 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
590 					TX_BD_FLAGS_COAL_NOW |
591 					TX_BD_FLAGS_PACKET_END |
592 					TX_BD_CNT(2));
593 
594 		if (skb->ip_summed == CHECKSUM_PARTIAL)
595 			tx_push1->tx_bd_hsize_lflags =
596 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
597 		else
598 			tx_push1->tx_bd_hsize_lflags = 0;
599 
600 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
601 		tx_push1->tx_bd_cfa_action =
602 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
603 
604 		end = pdata + length;
605 		end = PTR_ALIGN(end, 8) - 1;
606 		*end = 0;
607 
608 		skb_copy_from_linear_data(skb, pdata, len);
609 		pdata += len;
610 		for (j = 0; j < last_frag; j++) {
611 			void *fptr;
612 
613 			frag = &skb_shinfo(skb)->frags[j];
614 			fptr = skb_frag_address_safe(frag);
615 			if (!fptr)
616 				goto normal_tx;
617 
618 			memcpy(pdata, fptr, skb_frag_size(frag));
619 			pdata += skb_frag_size(frag);
620 		}
621 
622 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
623 		txbd->tx_bd_haddr = txr->data_mapping;
624 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
625 		prod = NEXT_TX(prod);
626 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
627 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
628 		memcpy(txbd, tx_push1, sizeof(*txbd));
629 		prod = NEXT_TX(prod);
630 		tx_push->doorbell =
631 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
632 				    DB_RING_IDX(&txr->tx_db, prod));
633 		WRITE_ONCE(txr->tx_prod, prod);
634 
635 		tx_buf->is_push = 1;
636 		netdev_tx_sent_queue(txq, skb->len);
637 		wmb();	/* Sync is_push and byte queue before pushing data */
638 
639 		push_len = (length + sizeof(*tx_push) + 7) / 8;
640 		if (push_len > 16) {
641 			__iowrite64_copy(db, tx_push_buf, 16);
642 			__iowrite32_copy(db + 4, tx_push_buf + 1,
643 					 (push_len - 16) << 1);
644 		} else {
645 			__iowrite64_copy(db, tx_push_buf, push_len);
646 		}
647 
648 		goto tx_done;
649 	}
650 
651 normal_tx:
652 	if (length < BNXT_MIN_PKT_SIZE) {
653 		pad = BNXT_MIN_PKT_SIZE - length;
654 		if (skb_pad(skb, pad))
655 			/* SKB already freed. */
656 			goto tx_kick_pending;
657 		length = BNXT_MIN_PKT_SIZE;
658 	}
659 
660 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
661 
662 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
663 		goto tx_free;
664 
665 	dma_unmap_addr_set(tx_buf, mapping, mapping);
666 	dma_unmap_len_set(tx_buf, len, len);
667 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
668 		TX_BD_CNT(last_frag + 2);
669 
670 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
671 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
672 
673 	prod = NEXT_TX(prod);
674 	txbd1 = bnxt_init_ext_bd(bp, txr, prod, lflags, vlan_tag_flags,
675 				 cfa_action);
676 
677 	if (skb_is_gso(skb)) {
678 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
679 		u32 hdr_len;
680 
681 		if (skb->encapsulation) {
682 			if (udp_gso)
683 				hdr_len = skb_inner_transport_offset(skb) +
684 					  sizeof(struct udphdr);
685 			else
686 				hdr_len = skb_inner_tcp_all_headers(skb);
687 		} else if (udp_gso) {
688 			hdr_len = skb_transport_offset(skb) +
689 				  sizeof(struct udphdr);
690 		} else {
691 			hdr_len = skb_tcp_all_headers(skb);
692 		}
693 
694 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
695 					TX_BD_FLAGS_T_IPID |
696 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
697 		length = skb_shinfo(skb)->gso_size;
698 		txbd1->tx_bd_mss = cpu_to_le32(length);
699 		length += hdr_len;
700 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
701 		txbd1->tx_bd_hsize_lflags |=
702 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
703 	}
704 
705 	length >>= 9;
706 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
707 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
708 				     skb->len);
709 		i = 0;
710 		goto tx_dma_error;
711 	}
712 	flags |= bnxt_lhint_arr[length];
713 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
714 
715 	txbd0 = txbd;
716 	for (i = 0; i < last_frag; i++) {
717 		frag = &skb_shinfo(skb)->frags[i];
718 		prod = NEXT_TX(prod);
719 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
720 
721 		len = skb_frag_size(frag);
722 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
723 					   DMA_TO_DEVICE);
724 
725 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
726 			goto tx_dma_error;
727 
728 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
729 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
730 					  mapping, mapping);
731 		dma_unmap_len_set(tx_buf, len, len);
732 
733 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
734 
735 		flags = len << TX_BD_LEN_SHIFT;
736 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
737 	}
738 
739 	flags &= ~TX_BD_LEN;
740 	txbd->tx_bd_len_flags_type =
741 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
742 			    TX_BD_FLAGS_PACKET_END);
743 
744 	netdev_tx_sent_queue(txq, skb->len);
745 
746 	skb_tx_timestamp(skb);
747 
748 	prod = NEXT_TX(prod);
749 	WRITE_ONCE(txr->tx_prod, prod);
750 
751 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
752 		bnxt_txr_db_kick(bp, txr, prod);
753 	} else {
754 		if (free_size >= bp->tx_wake_thresh)
755 			txbd0->tx_bd_len_flags_type |=
756 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
757 		txr->kick_pending = 1;
758 	}
759 
760 tx_done:
761 
762 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
763 		if (netdev_xmit_more() && !tx_buf->is_push) {
764 			txbd0->tx_bd_len_flags_type &=
765 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
766 			bnxt_txr_db_kick(bp, txr, prod);
767 		}
768 
769 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
770 				   bp->tx_wake_thresh);
771 	}
772 	return NETDEV_TX_OK;
773 
774 tx_dma_error:
775 	last_frag = i;
776 
777 	/* start back at beginning and unmap skb */
778 	prod = txr->tx_prod;
779 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
780 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
781 			 skb_headlen(skb), DMA_TO_DEVICE);
782 	prod = NEXT_TX(prod);
783 
784 	/* unmap remaining mapped pages */
785 	for (i = 0; i < last_frag; i++) {
786 		prod = NEXT_TX(prod);
787 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
788 		frag = &skb_shinfo(skb)->frags[i];
789 		netmem_dma_unmap_page_attrs(&pdev->dev,
790 					    dma_unmap_addr(tx_buf, mapping),
791 					    skb_frag_size(frag),
792 					    DMA_TO_DEVICE, 0);
793 	}
794 
795 tx_free:
796 	dev_kfree_skb_any(skb);
797 tx_kick_pending:
798 	if (BNXT_TX_PTP_IS_SET(lflags)) {
799 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
800 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
801 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
802 			/* set SKB to err so PTP worker will clean up */
803 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
804 	}
805 	if (txr->kick_pending)
806 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
807 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
808 	dev_core_stats_tx_dropped_inc(dev);
809 	return NETDEV_TX_OK;
810 }
811 
812 /* Returns true if some remaining TX packets not processed. */
813 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
814 			  int budget)
815 {
816 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
817 	struct pci_dev *pdev = bp->pdev;
818 	u16 hw_cons = txr->tx_hw_cons;
819 	unsigned int tx_bytes = 0;
820 	u16 cons = txr->tx_cons;
821 	unsigned int dma_len;
822 	dma_addr_t dma_addr;
823 	int tx_pkts = 0;
824 	bool rc = false;
825 
826 	while (RING_TX(bp, cons) != hw_cons) {
827 		struct bnxt_sw_tx_bd *tx_buf, *head_buf;
828 		struct sk_buff *skb;
829 		bool is_ts_pkt;
830 		int j, last;
831 
832 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
833 		head_buf = tx_buf;
834 		skb = tx_buf->skb;
835 
836 		if (unlikely(!skb)) {
837 			bnxt_sched_reset_txr(bp, txr, cons);
838 			return rc;
839 		}
840 
841 		is_ts_pkt = tx_buf->is_ts_pkt;
842 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
843 			rc = true;
844 			break;
845 		}
846 
847 		cons = NEXT_TX(cons);
848 		tx_pkts++;
849 		tx_bytes += skb->len;
850 		tx_buf->skb = NULL;
851 		tx_buf->is_ts_pkt = 0;
852 
853 		if (tx_buf->is_push) {
854 			tx_buf->is_push = 0;
855 			goto next_tx_int;
856 		}
857 
858 		if (dma_unmap_len(tx_buf, len)) {
859 			dma_addr = dma_unmap_addr(tx_buf, mapping);
860 			dma_len = dma_unmap_len(tx_buf, len);
861 
862 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
863 					 DMA_TO_DEVICE);
864 		}
865 
866 		last = tx_buf->nr_frags;
867 
868 		for (j = 0; j < last; j++) {
869 			cons = NEXT_TX(cons);
870 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
871 			if (dma_unmap_len(tx_buf, len)) {
872 				dma_addr = dma_unmap_addr(tx_buf, mapping);
873 				dma_len = dma_unmap_len(tx_buf, len);
874 
875 				netmem_dma_unmap_page_attrs(&pdev->dev,
876 							    dma_addr, dma_len,
877 							    DMA_TO_DEVICE, 0);
878 			}
879 		}
880 
881 		if (unlikely(head_buf->is_sw_gso)) {
882 			u16 inline_cons = txr->tx_inline_cons + 1;
883 
884 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
885 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
886 				tso_dma_map_complete(&pdev->dev,
887 						     &head_buf->sw_gso_cstate);
888 			} else {
889 				tx_pkts--;
890 				tx_bytes -= skb->len;
891 				skb = NULL;
892 			}
893 			head_buf->is_sw_gso = 0;
894 		}
895 
896 		if (unlikely(is_ts_pkt)) {
897 			if (BNXT_CHIP_P5(bp)) {
898 				/* PTP worker takes ownership of the skb */
899 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
900 				skb = NULL;
901 			}
902 		}
903 
904 next_tx_int:
905 		cons = NEXT_TX(cons);
906 
907 		napi_consume_skb(skb, budget);
908 	}
909 
910 	WRITE_ONCE(txr->tx_cons, cons);
911 
912 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
913 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
914 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
915 
916 	return rc;
917 }
918 
919 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
920 {
921 	struct bnxt_tx_ring_info *txr;
922 	bool more = false;
923 	int i;
924 
925 	bnxt_for_each_napi_tx(i, bnapi, txr) {
926 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
927 			more |= __bnxt_tx_int(bp, txr, budget);
928 	}
929 	if (!more)
930 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
931 }
932 
933 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
934 {
935 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
936 }
937 
938 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
939 					 struct bnxt_rx_ring_info *rxr,
940 					 unsigned int *offset,
941 					 gfp_t gfp)
942 {
943 	struct page *page;
944 
945 	if (rxr->rx_page_size < PAGE_SIZE) {
946 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
947 						rxr->rx_page_size);
948 	} else {
949 		page = page_pool_dev_alloc_pages(rxr->page_pool);
950 		*offset = 0;
951 	}
952 	if (!page)
953 		return NULL;
954 
955 	*mapping = page_pool_get_dma_addr(page) + *offset;
956 	return page;
957 }
958 
959 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
960 					 struct bnxt_rx_ring_info *rxr,
961 					 unsigned int *offset,
962 					 gfp_t gfp)
963 {
964 	netmem_ref netmem;
965 
966 	if (rxr->rx_page_size < PAGE_SIZE) {
967 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
968 						     rxr->rx_page_size, gfp);
969 	} else {
970 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
971 		*offset = 0;
972 	}
973 	if (!netmem)
974 		return 0;
975 
976 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
977 	return netmem;
978 }
979 
980 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
981 				       struct bnxt_rx_ring_info *rxr,
982 				       gfp_t gfp)
983 {
984 	unsigned int offset;
985 	struct page *page;
986 
987 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
988 				    bp->rx_buf_size, gfp);
989 	if (!page)
990 		return NULL;
991 
992 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
993 	return page_address(page) + offset;
994 }
995 
996 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
997 		       u16 prod, gfp_t gfp)
998 {
999 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1000 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1001 	dma_addr_t mapping;
1002 
1003 	if (BNXT_RX_PAGE_MODE(bp)) {
1004 		unsigned int offset;
1005 		struct page *page =
1006 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1007 
1008 		if (!page)
1009 			return -ENOMEM;
1010 
1011 		mapping += bp->rx_dma_offset;
1012 		rx_buf->data = page;
1013 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
1014 	} else {
1015 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
1016 
1017 		if (!data)
1018 			return -ENOMEM;
1019 
1020 		rx_buf->data = data;
1021 		rx_buf->data_ptr = data + bp->rx_offset;
1022 	}
1023 	rx_buf->mapping = mapping;
1024 
1025 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1026 	return 0;
1027 }
1028 
1029 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1030 {
1031 	u16 prod = rxr->rx_prod;
1032 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1033 	struct bnxt *bp = rxr->bnapi->bp;
1034 	struct rx_bd *cons_bd, *prod_bd;
1035 
1036 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1037 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1038 
1039 	prod_rx_buf->data = data;
1040 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1041 
1042 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1043 
1044 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1045 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1046 
1047 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1048 }
1049 
1050 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1051 {
1052 	u16 next, max = rxr->rx_agg_bmap_size;
1053 
1054 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1055 	if (next >= max)
1056 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1057 	return next;
1058 }
1059 
1060 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1061 				u16 prod, gfp_t gfp)
1062 {
1063 	struct rx_bd *rxbd =
1064 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1065 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1066 	u16 sw_prod = rxr->rx_sw_agg_prod;
1067 	unsigned int offset = 0;
1068 	dma_addr_t mapping;
1069 	netmem_ref netmem;
1070 
1071 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1072 	if (!netmem)
1073 		return -ENOMEM;
1074 
1075 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1076 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1077 
1078 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1079 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1080 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1081 
1082 	rx_agg_buf->netmem = netmem;
1083 	rx_agg_buf->offset = offset;
1084 	rx_agg_buf->mapping = mapping;
1085 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1086 	rxbd->rx_bd_opaque = sw_prod;
1087 	return 0;
1088 }
1089 
1090 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1091 				       struct bnxt_cp_ring_info *cpr,
1092 				       u16 cp_cons, u16 curr)
1093 {
1094 	struct rx_agg_cmp *agg;
1095 
1096 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1097 	agg = (struct rx_agg_cmp *)
1098 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1099 	return agg;
1100 }
1101 
1102 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1103 					      struct bnxt_rx_ring_info *rxr,
1104 					      u16 agg_id, u16 curr)
1105 {
1106 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1107 
1108 	return &tpa_info->agg_arr[curr];
1109 }
1110 
1111 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1112 				   u16 start, u32 agg_bufs, bool tpa)
1113 {
1114 	struct bnxt_napi *bnapi = cpr->bnapi;
1115 	struct bnxt *bp = bnapi->bp;
1116 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1117 	u16 prod = rxr->rx_agg_prod;
1118 	u16 sw_prod = rxr->rx_sw_agg_prod;
1119 	bool p5_tpa = false;
1120 	u32 i;
1121 
1122 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1123 		p5_tpa = true;
1124 
1125 	for (i = 0; i < agg_bufs; i++) {
1126 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1127 		struct rx_agg_cmp *agg;
1128 		struct rx_bd *prod_bd;
1129 		netmem_ref netmem;
1130 		u16 cons;
1131 
1132 		if (p5_tpa)
1133 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1134 		else
1135 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1136 		cons = agg->rx_agg_cmp_opaque;
1137 		__clear_bit(cons, rxr->rx_agg_bmap);
1138 
1139 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1140 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1141 
1142 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1143 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1144 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1145 
1146 		/* It is possible for sw_prod to be equal to cons, so
1147 		 * set cons_rx_buf->netmem to 0 first.
1148 		 */
1149 		netmem = cons_rx_buf->netmem;
1150 		cons_rx_buf->netmem = 0;
1151 		prod_rx_buf->netmem = netmem;
1152 		prod_rx_buf->offset = cons_rx_buf->offset;
1153 
1154 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1155 
1156 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1157 
1158 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1159 		prod_bd->rx_bd_opaque = sw_prod;
1160 
1161 		prod = NEXT_RX_AGG(prod);
1162 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1163 	}
1164 	rxr->rx_agg_prod = prod;
1165 	rxr->rx_sw_agg_prod = sw_prod;
1166 }
1167 
1168 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1169 					      struct bnxt_rx_ring_info *rxr,
1170 					      u16 cons, void *data, u8 *data_ptr,
1171 					      dma_addr_t dma_addr,
1172 					      unsigned int offset_and_len)
1173 {
1174 	unsigned int len = offset_and_len & 0xffff;
1175 	struct page *page = data;
1176 	u16 prod = rxr->rx_prod;
1177 	struct sk_buff *skb;
1178 	int err;
1179 
1180 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1181 	if (unlikely(err)) {
1182 		bnxt_reuse_rx_data(rxr, cons, data);
1183 		return NULL;
1184 	}
1185 	dma_addr -= bp->rx_dma_offset;
1186 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1187 				bp->rx_dir);
1188 	skb = napi_build_skb(data_ptr - bp->rx_offset, rxr->rx_page_size);
1189 	if (!skb) {
1190 		page_pool_recycle_direct(rxr->page_pool, page);
1191 		return NULL;
1192 	}
1193 	skb_mark_for_recycle(skb);
1194 	skb_reserve(skb, bp->rx_offset);
1195 	__skb_put(skb, len);
1196 
1197 	return skb;
1198 }
1199 
1200 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1201 					struct bnxt_rx_ring_info *rxr,
1202 					u16 cons, void *data, u8 *data_ptr,
1203 					dma_addr_t dma_addr,
1204 					unsigned int offset_and_len)
1205 {
1206 	unsigned int payload = offset_and_len >> 16;
1207 	unsigned int len = offset_and_len & 0xffff;
1208 	skb_frag_t *frag;
1209 	struct page *page = data;
1210 	u16 prod = rxr->rx_prod;
1211 	struct sk_buff *skb;
1212 	int off, err;
1213 
1214 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1215 	if (unlikely(err)) {
1216 		bnxt_reuse_rx_data(rxr, cons, data);
1217 		return NULL;
1218 	}
1219 	dma_addr -= bp->rx_dma_offset;
1220 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1221 				bp->rx_dir);
1222 
1223 	if (unlikely(!payload))
1224 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1225 
1226 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1227 	if (!skb) {
1228 		page_pool_recycle_direct(rxr->page_pool, page);
1229 		return NULL;
1230 	}
1231 
1232 	skb_mark_for_recycle(skb);
1233 	off = (void *)data_ptr - page_address(page);
1234 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1235 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1236 	       payload + NET_IP_ALIGN);
1237 
1238 	frag = &skb_shinfo(skb)->frags[0];
1239 	skb_frag_size_sub(frag, payload);
1240 	skb_frag_off_add(frag, payload);
1241 	skb->data_len -= payload;
1242 	skb->tail += payload;
1243 
1244 	return skb;
1245 }
1246 
1247 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1248 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1249 				   void *data, u8 *data_ptr,
1250 				   dma_addr_t dma_addr,
1251 				   unsigned int offset_and_len)
1252 {
1253 	u16 prod = rxr->rx_prod;
1254 	struct sk_buff *skb;
1255 	int err;
1256 
1257 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1258 	if (unlikely(err)) {
1259 		bnxt_reuse_rx_data(rxr, cons, data);
1260 		return NULL;
1261 	}
1262 
1263 	skb = napi_build_skb(data, bp->rx_buf_size);
1264 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1265 				bp->rx_dir);
1266 	if (!skb) {
1267 		page_pool_free_va(rxr->head_pool, data, true);
1268 		return NULL;
1269 	}
1270 
1271 	skb_mark_for_recycle(skb);
1272 	skb_reserve(skb, bp->rx_offset);
1273 	skb_put(skb, offset_and_len & 0xffff);
1274 	return skb;
1275 }
1276 
1277 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1278 				 struct bnxt_cp_ring_info *cpr,
1279 				 u16 idx, u32 agg_bufs, bool tpa,
1280 				 struct sk_buff *skb,
1281 				 struct xdp_buff *xdp)
1282 {
1283 	struct bnxt_napi *bnapi = cpr->bnapi;
1284 	struct skb_shared_info *shinfo;
1285 	struct bnxt_rx_ring_info *rxr;
1286 	u32 i, total_frag_len = 0;
1287 	bool p5_tpa = false;
1288 	u16 prod;
1289 
1290 	rxr = bnapi->rx_ring;
1291 	prod = rxr->rx_agg_prod;
1292 
1293 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1294 		p5_tpa = true;
1295 
1296 	if (skb)
1297 		shinfo = skb_shinfo(skb);
1298 	else
1299 		shinfo = xdp_get_shared_info_from_buff(xdp);
1300 
1301 	for (i = 0; i < agg_bufs; i++) {
1302 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1303 		struct rx_agg_cmp *agg;
1304 		u16 cons, frag_len;
1305 		netmem_ref netmem;
1306 
1307 		if (p5_tpa)
1308 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1309 		else
1310 			agg = bnxt_get_agg(bp, cpr, idx, i);
1311 		cons = agg->rx_agg_cmp_opaque;
1312 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1313 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1314 
1315 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1316 		if (skb) {
1317 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1318 					       cons_rx_buf->offset,
1319 					       frag_len, rxr->rx_page_size);
1320 		} else {
1321 			skb_frag_t *frag = &shinfo->frags[i];
1322 
1323 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1324 						  cons_rx_buf->offset,
1325 						  frag_len);
1326 			shinfo->nr_frags = i + 1;
1327 		}
1328 		__clear_bit(cons, rxr->rx_agg_bmap);
1329 
1330 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1331 		 * a sw_prod index that equals the cons index, so we
1332 		 * need to clear the cons entry now.
1333 		 */
1334 		netmem = cons_rx_buf->netmem;
1335 		cons_rx_buf->netmem = 0;
1336 
1337 		if (xdp && netmem_is_pfmemalloc(netmem))
1338 			xdp_buff_set_frag_pfmemalloc(xdp);
1339 
1340 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1341 			if (skb) {
1342 				skb->len -= frag_len;
1343 				skb->data_len -= frag_len;
1344 				skb->truesize -= rxr->rx_page_size;
1345 			}
1346 
1347 			--shinfo->nr_frags;
1348 			cons_rx_buf->netmem = netmem;
1349 
1350 			/* Update prod since possibly some netmems have been
1351 			 * allocated already.
1352 			 */
1353 			rxr->rx_agg_prod = prod;
1354 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1355 			return 0;
1356 		}
1357 
1358 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1359 						  rxr->rx_page_size);
1360 
1361 		total_frag_len += frag_len;
1362 		prod = NEXT_RX_AGG(prod);
1363 	}
1364 	rxr->rx_agg_prod = prod;
1365 	return total_frag_len;
1366 }
1367 
1368 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1369 					       struct bnxt_cp_ring_info *cpr,
1370 					       struct sk_buff *skb, u16 idx,
1371 					       u32 agg_bufs, bool tpa)
1372 {
1373 	u32 total_frag_len = 0;
1374 
1375 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1376 					       skb, NULL);
1377 	if (!total_frag_len) {
1378 		skb_mark_for_recycle(skb);
1379 		dev_kfree_skb(skb);
1380 		return NULL;
1381 	}
1382 
1383 	return skb;
1384 }
1385 
1386 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1387 				   struct bnxt_cp_ring_info *cpr,
1388 				   struct xdp_buff *xdp, u16 idx,
1389 				   u32 agg_bufs, bool tpa)
1390 {
1391 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1392 	u32 total_frag_len = 0;
1393 
1394 	if (!xdp_buff_has_frags(xdp))
1395 		shinfo->nr_frags = 0;
1396 
1397 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1398 					       NULL, xdp);
1399 	if (total_frag_len) {
1400 		xdp_buff_set_frags_flag(xdp);
1401 		shinfo->nr_frags = agg_bufs;
1402 		shinfo->xdp_frags_size = total_frag_len;
1403 	}
1404 	return total_frag_len;
1405 }
1406 
1407 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1408 			       u8 agg_bufs, u32 *raw_cons)
1409 {
1410 	u16 last;
1411 	struct rx_agg_cmp *agg;
1412 
1413 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1414 	last = RING_CMP(*raw_cons);
1415 	agg = (struct rx_agg_cmp *)
1416 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1417 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1418 }
1419 
1420 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1421 				      unsigned int len,
1422 				      dma_addr_t mapping)
1423 {
1424 	struct bnxt *bp = bnapi->bp;
1425 	struct pci_dev *pdev = bp->pdev;
1426 	struct sk_buff *skb;
1427 
1428 	skb = napi_alloc_skb(&bnapi->napi, len);
1429 	if (!skb)
1430 		return NULL;
1431 
1432 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1433 				bp->rx_dir);
1434 
1435 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1436 	       len + NET_IP_ALIGN);
1437 
1438 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1439 				   bp->rx_dir);
1440 
1441 	skb_put(skb, len);
1442 
1443 	return skb;
1444 }
1445 
1446 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1447 				     unsigned int len,
1448 				     dma_addr_t mapping)
1449 {
1450 	return bnxt_copy_data(bnapi, data, len, mapping);
1451 }
1452 
1453 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1454 				     struct xdp_buff *xdp,
1455 				     unsigned int len,
1456 				     dma_addr_t mapping)
1457 {
1458 	unsigned int metasize = 0;
1459 	u8 *data = xdp->data;
1460 	struct sk_buff *skb;
1461 
1462 	len = xdp->data_end - xdp->data_meta;
1463 	metasize = xdp->data - xdp->data_meta;
1464 	data = xdp->data_meta;
1465 
1466 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1467 	if (!skb)
1468 		return skb;
1469 
1470 	if (metasize) {
1471 		skb_metadata_set(skb, metasize);
1472 		__skb_pull(skb, metasize);
1473 	}
1474 
1475 	return skb;
1476 }
1477 
1478 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1479 			   u32 *raw_cons, void *cmp)
1480 {
1481 	struct rx_cmp *rxcmp = cmp;
1482 	u32 tmp_raw_cons = *raw_cons;
1483 	u8 cmp_type, agg_bufs = 0;
1484 
1485 	cmp_type = RX_CMP_TYPE(rxcmp);
1486 
1487 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1488 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1489 			    RX_CMP_AGG_BUFS) >>
1490 			   RX_CMP_AGG_BUFS_SHIFT;
1491 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1492 		struct rx_tpa_end_cmp *tpa_end = cmp;
1493 
1494 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1495 			return 0;
1496 
1497 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1498 	}
1499 
1500 	if (agg_bufs) {
1501 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1502 			return -EBUSY;
1503 	}
1504 	*raw_cons = tmp_raw_cons;
1505 	return 0;
1506 }
1507 
1508 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1509 {
1510 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1511 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1512 
1513 	if (test_bit(idx, map->agg_idx_bmap)) {
1514 		idx = find_first_zero_bit(map->agg_idx_bmap, MAX_TPA_P5);
1515 		if (idx >= MAX_TPA_P5)
1516 			return INVALID_HW_RING_ID;
1517 	}
1518 	__set_bit(idx, map->agg_idx_bmap);
1519 	map->agg_id_tbl[agg_id] = idx;
1520 	return idx;
1521 }
1522 
1523 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1524 {
1525 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1526 
1527 	__clear_bit(idx, map->agg_idx_bmap);
1528 }
1529 
1530 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1531 {
1532 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1533 
1534 	return map->agg_id_tbl[agg_id];
1535 }
1536 
1537 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1538 			      struct rx_tpa_start_cmp *tpa_start,
1539 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1540 {
1541 	tpa_info->cfa_code_valid = 1;
1542 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1543 	tpa_info->vlan_valid = 0;
1544 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1545 		tpa_info->vlan_valid = 1;
1546 		tpa_info->metadata =
1547 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1548 	}
1549 }
1550 
1551 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1552 				 struct rx_tpa_start_cmp *tpa_start,
1553 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1554 {
1555 	tpa_info->vlan_valid = 0;
1556 	if (TPA_START_VLAN_VALID(tpa_start)) {
1557 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1558 		u32 vlan_proto = ETH_P_8021Q;
1559 
1560 		tpa_info->vlan_valid = 1;
1561 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1562 			vlan_proto = ETH_P_8021AD;
1563 		tpa_info->metadata = vlan_proto << 16 |
1564 				     TPA_START_METADATA0_TCI(tpa_start1);
1565 	}
1566 }
1567 
1568 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1569 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1570 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1571 {
1572 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1573 	struct bnxt_tpa_info *tpa_info;
1574 	u16 cons, prod, agg_id;
1575 	struct rx_bd *prod_bd;
1576 	dma_addr_t mapping;
1577 
1578 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1579 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1580 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1581 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1582 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1583 				    rxr->bnapi->index,
1584 				    TPA_START_AGG_ID_P5(tpa_start));
1585 			bnxt_sched_reset_rxr(bp, rxr);
1586 			return;
1587 		}
1588 	} else {
1589 		agg_id = TPA_START_AGG_ID(tpa_start);
1590 	}
1591 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1592 	prod = rxr->rx_prod;
1593 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1594 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1595 	tpa_info = &rxr->rx_tpa[agg_id];
1596 
1597 	if (unlikely(cons != rxr->rx_next_cons ||
1598 		     TPA_START_ERROR(tpa_start))) {
1599 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1600 			    cons, rxr->rx_next_cons,
1601 			    TPA_START_ERROR_CODE(tpa_start1));
1602 		bnxt_sched_reset_rxr(bp, rxr);
1603 		return;
1604 	}
1605 	prod_rx_buf->data = tpa_info->data;
1606 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1607 
1608 	mapping = tpa_info->mapping;
1609 	prod_rx_buf->mapping = mapping;
1610 
1611 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1612 
1613 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1614 
1615 	tpa_info->data = cons_rx_buf->data;
1616 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1617 	cons_rx_buf->data = NULL;
1618 	tpa_info->mapping = cons_rx_buf->mapping;
1619 
1620 	tpa_info->len =
1621 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1622 				RX_TPA_START_CMP_LEN_SHIFT;
1623 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1624 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1625 		tpa_info->gso_type = SKB_GSO_TCPV4;
1626 		if (TPA_START_IS_IPV6(tpa_start1))
1627 			tpa_info->gso_type = SKB_GSO_TCPV6;
1628 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1629 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1630 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1631 			tpa_info->gso_type = SKB_GSO_TCPV6;
1632 		tpa_info->rss_hash =
1633 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1634 	} else {
1635 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1636 		tpa_info->gso_type = 0;
1637 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1638 	}
1639 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1640 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1641 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1642 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1643 	else
1644 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1645 	tpa_info->agg_count = 0;
1646 
1647 	rxr->rx_prod = NEXT_RX(prod);
1648 	cons = RING_RX(bp, NEXT_RX(cons));
1649 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1650 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1651 
1652 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1653 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1654 	cons_rx_buf->data = NULL;
1655 }
1656 
1657 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1658 {
1659 	if (agg_bufs)
1660 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1661 }
1662 
1663 #ifdef CONFIG_INET
1664 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1665 {
1666 	struct udphdr *uh = NULL;
1667 
1668 	if (ip_proto == htons(ETH_P_IP)) {
1669 		struct iphdr *iph = (struct iphdr *)skb->data;
1670 
1671 		if (iph->protocol == IPPROTO_UDP)
1672 			uh = (struct udphdr *)(iph + 1);
1673 	} else {
1674 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1675 
1676 		if (iph->nexthdr == IPPROTO_UDP)
1677 			uh = (struct udphdr *)(iph + 1);
1678 	}
1679 	if (uh) {
1680 		if (uh->check)
1681 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1682 		else
1683 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1684 	}
1685 }
1686 #endif
1687 
1688 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1689 					   int payload_off, int tcp_ts,
1690 					   struct sk_buff *skb)
1691 {
1692 #ifdef CONFIG_INET
1693 	struct tcphdr *th;
1694 	int len, nw_off;
1695 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1696 	u32 hdr_info = tpa_info->hdr_info;
1697 	bool loopback = false;
1698 
1699 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1700 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1701 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1702 
1703 	/* If the packet is an internal loopback packet, the offsets will
1704 	 * have an extra 4 bytes.
1705 	 */
1706 	if (inner_mac_off == 4) {
1707 		loopback = true;
1708 	} else if (inner_mac_off > 4) {
1709 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1710 					    ETH_HLEN - 2));
1711 
1712 		/* We only support inner iPv4/ipv6.  If we don't see the
1713 		 * correct protocol ID, it must be a loopback packet where
1714 		 * the offsets are off by 4.
1715 		 */
1716 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1717 			loopback = true;
1718 	}
1719 	if (loopback) {
1720 		/* internal loopback packet, subtract all offsets by 4 */
1721 		inner_ip_off -= 4;
1722 		inner_mac_off -= 4;
1723 		outer_ip_off -= 4;
1724 	}
1725 
1726 	nw_off = inner_ip_off - ETH_HLEN;
1727 	skb_set_network_header(skb, nw_off);
1728 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1729 		struct ipv6hdr *iph = ipv6_hdr(skb);
1730 
1731 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1732 		len = skb->len - skb_transport_offset(skb);
1733 		th = tcp_hdr(skb);
1734 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1735 	} else {
1736 		struct iphdr *iph = ip_hdr(skb);
1737 
1738 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1739 		len = skb->len - skb_transport_offset(skb);
1740 		th = tcp_hdr(skb);
1741 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1742 	}
1743 
1744 	if (inner_mac_off) { /* tunnel */
1745 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1746 					    ETH_HLEN - 2));
1747 
1748 		bnxt_gro_tunnel(skb, proto);
1749 	}
1750 #endif
1751 	return skb;
1752 }
1753 
1754 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1755 					   int payload_off, int tcp_ts,
1756 					   struct sk_buff *skb)
1757 {
1758 #ifdef CONFIG_INET
1759 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1760 	u32 hdr_info = tpa_info->hdr_info;
1761 	int iphdr_len, nw_off;
1762 
1763 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1764 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1765 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1766 
1767 	nw_off = inner_ip_off - ETH_HLEN;
1768 	skb_set_network_header(skb, nw_off);
1769 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1770 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1771 	skb_set_transport_header(skb, nw_off + iphdr_len);
1772 
1773 	if (inner_mac_off) { /* tunnel */
1774 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1775 					    ETH_HLEN - 2));
1776 
1777 		bnxt_gro_tunnel(skb, proto);
1778 	}
1779 #endif
1780 	return skb;
1781 }
1782 
1783 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1784 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1785 
1786 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1787 					   int payload_off, int tcp_ts,
1788 					   struct sk_buff *skb)
1789 {
1790 #ifdef CONFIG_INET
1791 	struct tcphdr *th;
1792 	int len, nw_off, tcp_opt_len = 0;
1793 
1794 	if (tcp_ts)
1795 		tcp_opt_len = 12;
1796 
1797 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1798 		struct iphdr *iph;
1799 
1800 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1801 			 ETH_HLEN;
1802 		skb_set_network_header(skb, nw_off);
1803 		iph = ip_hdr(skb);
1804 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1805 		len = skb->len - skb_transport_offset(skb);
1806 		th = tcp_hdr(skb);
1807 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1808 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1809 		struct ipv6hdr *iph;
1810 
1811 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1812 			 ETH_HLEN;
1813 		skb_set_network_header(skb, nw_off);
1814 		iph = ipv6_hdr(skb);
1815 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1816 		len = skb->len - skb_transport_offset(skb);
1817 		th = tcp_hdr(skb);
1818 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1819 	} else {
1820 		dev_kfree_skb_any(skb);
1821 		return NULL;
1822 	}
1823 
1824 	if (nw_off) /* tunnel */
1825 		bnxt_gro_tunnel(skb, skb->protocol);
1826 #endif
1827 	return skb;
1828 }
1829 
1830 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1831 					   struct bnxt_tpa_info *tpa_info,
1832 					   struct rx_tpa_end_cmp *tpa_end,
1833 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1834 					   struct sk_buff *skb,
1835 					   struct bnxt_rx_sw_stats *rx_stats)
1836 {
1837 #ifdef CONFIG_INET
1838 	int payload_off;
1839 	u16 segs;
1840 
1841 	segs = TPA_END_TPA_SEGS(tpa_end);
1842 	if (segs == 1)
1843 		return skb;
1844 
1845 	rx_stats->rx_hw_gro_packets++;
1846 	rx_stats->rx_hw_gro_wire_packets += segs;
1847 
1848 	NAPI_GRO_CB(skb)->count = segs;
1849 	skb_shinfo(skb)->gso_size =
1850 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1851 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1852 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1853 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1854 	else
1855 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1856 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1857 	if (likely(skb))
1858 		tcp_gro_complete(skb);
1859 #endif
1860 	return skb;
1861 }
1862 
1863 /* Given the cfa_code of a received packet determine which
1864  * netdev (vf-rep or PF) the packet is destined to.
1865  */
1866 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1867 {
1868 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1869 
1870 	/* if vf-rep dev is NULL, it must belong to the PF */
1871 	return dev ? dev : bp->dev;
1872 }
1873 
1874 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1875 					   struct bnxt_cp_ring_info *cpr,
1876 					   u32 *raw_cons,
1877 					   struct rx_tpa_end_cmp *tpa_end,
1878 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1879 					   u8 *event)
1880 {
1881 	struct bnxt_napi *bnapi = cpr->bnapi;
1882 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1883 	struct net_device *dev = bp->dev;
1884 	u8 *data_ptr, agg_bufs;
1885 	unsigned int len;
1886 	struct bnxt_tpa_info *tpa_info;
1887 	dma_addr_t mapping;
1888 	struct sk_buff *skb;
1889 	u16 idx = 0, agg_id;
1890 	void *data;
1891 	bool gro;
1892 
1893 	if (unlikely(bnapi->in_reset)) {
1894 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1895 
1896 		if (rc < 0)
1897 			return ERR_PTR(-EBUSY);
1898 		return NULL;
1899 	}
1900 
1901 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1902 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1903 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1904 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1905 		tpa_info = &rxr->rx_tpa[agg_id];
1906 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1907 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1908 				    agg_bufs, tpa_info->agg_count);
1909 			agg_bufs = tpa_info->agg_count;
1910 		}
1911 		tpa_info->agg_count = 0;
1912 		*event |= BNXT_AGG_EVENT;
1913 		bnxt_free_agg_idx(rxr, agg_id);
1914 		idx = agg_id;
1915 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1916 	} else {
1917 		agg_id = TPA_END_AGG_ID(tpa_end);
1918 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1919 		tpa_info = &rxr->rx_tpa[agg_id];
1920 		idx = RING_CMP(*raw_cons);
1921 		if (agg_bufs) {
1922 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1923 				return ERR_PTR(-EBUSY);
1924 
1925 			*event |= BNXT_AGG_EVENT;
1926 			idx = NEXT_CMP(idx);
1927 		}
1928 		gro = !!TPA_END_GRO(tpa_end);
1929 	}
1930 	data = tpa_info->data;
1931 	data_ptr = tpa_info->data_ptr;
1932 	prefetch(data_ptr);
1933 	len = tpa_info->len;
1934 	mapping = tpa_info->mapping;
1935 
1936 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1937 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1938 		if (agg_bufs > MAX_SKB_FRAGS)
1939 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1940 				    agg_bufs, (int)MAX_SKB_FRAGS);
1941 		return NULL;
1942 	}
1943 
1944 	if (len <= bp->rx_copybreak) {
1945 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1946 		if (!skb) {
1947 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1948 			cpr->sw_stats->rx.rx_oom_discards += 1;
1949 			return NULL;
1950 		}
1951 	} else {
1952 		u8 *new_data;
1953 		dma_addr_t new_mapping;
1954 
1955 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1956 						GFP_ATOMIC);
1957 		if (!new_data) {
1958 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1959 			cpr->sw_stats->rx.rx_oom_discards += 1;
1960 			return NULL;
1961 		}
1962 
1963 		tpa_info->data = new_data;
1964 		tpa_info->data_ptr = new_data + bp->rx_offset;
1965 		tpa_info->mapping = new_mapping;
1966 
1967 		skb = napi_build_skb(data, bp->rx_buf_size);
1968 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1969 					bp->rx_buf_use_size, bp->rx_dir);
1970 
1971 		if (!skb) {
1972 			page_pool_free_va(rxr->head_pool, data, true);
1973 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1974 			cpr->sw_stats->rx.rx_oom_discards += 1;
1975 			return NULL;
1976 		}
1977 		skb_mark_for_recycle(skb);
1978 		skb_reserve(skb, bp->rx_offset);
1979 		skb_put(skb, len);
1980 	}
1981 
1982 	if (agg_bufs) {
1983 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1984 					      true);
1985 		if (!skb) {
1986 			/* Page reuse already handled by bnxt_rx_pages(). */
1987 			cpr->sw_stats->rx.rx_oom_discards += 1;
1988 			return NULL;
1989 		}
1990 	}
1991 
1992 	if (tpa_info->cfa_code_valid)
1993 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1994 	skb->protocol = eth_type_trans(skb, dev);
1995 
1996 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1997 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1998 
1999 	if (tpa_info->vlan_valid &&
2000 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
2001 		__be16 vlan_proto = htons(tpa_info->metadata >>
2002 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
2003 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2004 
2005 		if (eth_type_vlan(vlan_proto)) {
2006 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2007 		} else {
2008 			dev_kfree_skb(skb);
2009 			return NULL;
2010 		}
2011 	}
2012 
2013 	skb_checksum_none_assert(skb);
2014 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
2015 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2016 		skb->csum_level =
2017 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
2018 	}
2019 
2020 	if (gro)
2021 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
2022 				   &cpr->sw_stats->rx);
2023 
2024 	return skb;
2025 }
2026 
2027 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
2028 			 struct rx_agg_cmp *rx_agg)
2029 {
2030 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2031 	struct bnxt_tpa_info *tpa_info;
2032 
2033 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2034 	tpa_info = &rxr->rx_tpa[agg_id];
2035 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2036 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2037 }
2038 
2039 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2040 			     struct sk_buff *skb)
2041 {
2042 	skb_mark_for_recycle(skb);
2043 
2044 	if (skb->dev != bp->dev) {
2045 		/* this packet belongs to a vf-rep */
2046 		bnxt_vf_rep_rx(bp, skb);
2047 		return;
2048 	}
2049 	skb_record_rx_queue(skb, bnapi->index);
2050 	napi_gro_receive(&bnapi->napi, skb);
2051 }
2052 
2053 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2054 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2055 {
2056 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2057 
2058 	if (BNXT_PTP_RX_TS_VALID(flags))
2059 		goto ts_valid;
2060 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2061 		return false;
2062 
2063 ts_valid:
2064 	*cmpl_ts = ts;
2065 	return true;
2066 }
2067 
2068 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2069 				    struct rx_cmp *rxcmp,
2070 				    struct rx_cmp_ext *rxcmp1)
2071 {
2072 	__be16 vlan_proto;
2073 	u16 vtag;
2074 
2075 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2076 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2077 		u32 meta_data;
2078 
2079 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2080 			return skb;
2081 
2082 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2083 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2084 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2085 		if (eth_type_vlan(vlan_proto))
2086 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2087 		else
2088 			goto vlan_err;
2089 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2090 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2091 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2092 
2093 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2094 				vlan_proto = htons(ETH_P_8021Q);
2095 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2096 				vlan_proto = htons(ETH_P_8021AD);
2097 			else
2098 				goto vlan_err;
2099 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2100 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2101 		}
2102 	}
2103 	return skb;
2104 vlan_err:
2105 	skb_mark_for_recycle(skb);
2106 	dev_kfree_skb(skb);
2107 	return NULL;
2108 }
2109 
2110 /* returns the following:
2111  * 1       - 1 packet successfully received
2112  * 0       - successful TPA_START, packet not completed yet
2113  * -EBUSY  - completion ring does not have all the agg buffers yet
2114  * -ENOMEM - packet aborted due to out of memory
2115  * -EIO    - packet aborted due to hw error indicated in BD
2116  */
2117 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2118 		       u32 *raw_cons, u8 *event)
2119 {
2120 	struct bnxt_napi *bnapi = cpr->bnapi;
2121 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2122 	struct net_device *dev = bp->dev;
2123 	struct rx_cmp *rxcmp;
2124 	struct rx_cmp_ext *rxcmp1;
2125 	u32 tmp_raw_cons = *raw_cons;
2126 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2127 	struct skb_shared_info *sinfo;
2128 	struct bnxt_xdp_buff bnxt_xdp;
2129 	struct bnxt_sw_rx_bd *rx_buf;
2130 	unsigned int len;
2131 	u8 *data_ptr, agg_bufs, cmp_type;
2132 	bool xdp_active = false;
2133 	dma_addr_t dma_addr;
2134 	struct sk_buff *skb;
2135 	u32 flags, misc;
2136 	u32 cmpl_ts;
2137 	void *data;
2138 	int rc = 0;
2139 
2140 	rxcmp = (struct rx_cmp *)
2141 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2142 
2143 	cmp_type = RX_CMP_TYPE(rxcmp);
2144 
2145 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2146 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2147 		goto next_rx_no_prod_no_len;
2148 	}
2149 
2150 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2151 	cp_cons = RING_CMP(tmp_raw_cons);
2152 	rxcmp1 = (struct rx_cmp_ext *)
2153 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2154 
2155 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2156 		return -EBUSY;
2157 
2158 	/* The valid test of the entry must be done first before
2159 	 * reading any further.
2160 	 */
2161 	dma_rmb();
2162 	prod = rxr->rx_prod;
2163 
2164 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2165 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2166 		bnxt_tpa_start(bp, rxr, cmp_type,
2167 			       (struct rx_tpa_start_cmp *)rxcmp,
2168 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2169 
2170 		*event |= BNXT_RX_EVENT;
2171 		goto next_rx_no_prod_no_len;
2172 
2173 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2174 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2175 				   (struct rx_tpa_end_cmp *)rxcmp,
2176 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2177 
2178 		if (IS_ERR(skb))
2179 			return -EBUSY;
2180 
2181 		rc = -ENOMEM;
2182 		if (likely(skb)) {
2183 			bnxt_deliver_skb(bp, bnapi, skb);
2184 			rc = 1;
2185 		}
2186 		*event |= BNXT_RX_EVENT;
2187 		goto next_rx_no_prod_no_len;
2188 	}
2189 
2190 	cons = rxcmp->rx_cmp_opaque;
2191 	if (unlikely(cons != rxr->rx_next_cons)) {
2192 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2193 
2194 		/* 0xffff is forced error, don't print it */
2195 		if (rxr->rx_next_cons != 0xffff)
2196 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2197 				    cons, rxr->rx_next_cons);
2198 		bnxt_sched_reset_rxr(bp, rxr);
2199 		if (rc1)
2200 			return rc1;
2201 		goto next_rx_no_prod_no_len;
2202 	}
2203 	rx_buf = &rxr->rx_buf_ring[cons];
2204 	data = rx_buf->data;
2205 	data_ptr = rx_buf->data_ptr;
2206 	prefetch(data_ptr);
2207 
2208 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2209 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2210 
2211 	if (agg_bufs) {
2212 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2213 			return -EBUSY;
2214 
2215 		cp_cons = NEXT_CMP(cp_cons);
2216 		*event |= BNXT_AGG_EVENT;
2217 	}
2218 	*event |= BNXT_RX_EVENT;
2219 
2220 	rx_buf->data = NULL;
2221 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2222 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2223 
2224 		bnxt_reuse_rx_data(rxr, cons, data);
2225 		if (agg_bufs)
2226 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2227 					       false);
2228 
2229 		rc = -EIO;
2230 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2231 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2232 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2233 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2234 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2235 						 rx_err);
2236 				bnxt_sched_reset_rxr(bp, rxr);
2237 			}
2238 		}
2239 		goto next_rx_no_len;
2240 	}
2241 
2242 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2243 	len = flags >> RX_CMP_LEN_SHIFT;
2244 	dma_addr = rx_buf->mapping;
2245 
2246 	if (bnxt_xdp_attached(bp, rxr)) {
2247 		bnxt_xdp.rxcmp = rxcmp;
2248 		bnxt_xdp.rxcmp1 = rxcmp1;
2249 		bnxt_xdp.cmp_type = cmp_type;
2250 
2251 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &bnxt_xdp.xdp);
2252 		if (agg_bufs) {
2253 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr,
2254 							       &bnxt_xdp.xdp,
2255 							       cp_cons,
2256 							       agg_bufs,
2257 							       false);
2258 			if (!frag_len)
2259 				goto oom_next_rx;
2260 
2261 		}
2262 		xdp_active = true;
2263 	}
2264 
2265 	if (xdp_active) {
2266 		if (bnxt_rx_xdp(bp, rxr, cons, &bnxt_xdp.xdp, data, &data_ptr,
2267 				&len, event)) {
2268 			rc = 1;
2269 			goto next_rx;
2270 		}
2271 		if (xdp_buff_has_frags(&bnxt_xdp.xdp)) {
2272 			sinfo = xdp_get_shared_info_from_buff(&bnxt_xdp.xdp);
2273 			agg_bufs = sinfo->nr_frags;
2274 		} else {
2275 			agg_bufs = 0;
2276 		}
2277 	}
2278 
2279 	if (len <= bp->rx_copybreak) {
2280 		if (!xdp_active)
2281 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2282 		else
2283 			skb = bnxt_copy_xdp(bnapi, &bnxt_xdp.xdp, len,
2284 					    dma_addr);
2285 		bnxt_reuse_rx_data(rxr, cons, data);
2286 		if (!skb) {
2287 			if (agg_bufs) {
2288 				if (!xdp_active)
2289 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2290 							       agg_bufs, false);
2291 				else
2292 					bnxt_xdp_buff_frags_free(rxr,
2293 								 &bnxt_xdp.xdp);
2294 			}
2295 			goto oom_next_rx;
2296 		}
2297 	} else {
2298 		u32 payload;
2299 
2300 		if (rx_buf->data_ptr == data_ptr)
2301 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2302 		else
2303 			payload = 0;
2304 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2305 				      payload | len);
2306 		if (!skb)
2307 			goto oom_next_rx;
2308 	}
2309 
2310 	if (agg_bufs) {
2311 		if (!xdp_active) {
2312 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2313 						      agg_bufs, false);
2314 			if (!skb)
2315 				goto oom_next_rx;
2316 		} else {
2317 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2318 						 rxr, &bnxt_xdp.xdp);
2319 			if (!skb) {
2320 				/* we should be able to free the old skb here */
2321 				bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2322 				goto oom_next_rx;
2323 			}
2324 		}
2325 	}
2326 
2327 	if (RX_CMP_HASH_VALID(rxcmp)) {
2328 		enum pkt_hash_types type;
2329 
2330 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2331 			type = bnxt_rss_ext_op(bp, rxcmp);
2332 		} else {
2333 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2334 
2335 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2336 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2337 				type = PKT_HASH_TYPE_L4;
2338 			else
2339 				type = PKT_HASH_TYPE_L3;
2340 		}
2341 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2342 	}
2343 
2344 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2345 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2346 	skb->protocol = eth_type_trans(skb, dev);
2347 
2348 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2349 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2350 		if (!skb)
2351 			goto next_rx;
2352 	}
2353 
2354 	skb_checksum_none_assert(skb);
2355 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2356 		if (dev->features & NETIF_F_RXCSUM) {
2357 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2358 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2359 		}
2360 	} else {
2361 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2362 			if (dev->features & NETIF_F_RXCSUM)
2363 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2364 		}
2365 	}
2366 
2367 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2368 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2369 			u64 ns, ts;
2370 
2371 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2372 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2373 
2374 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2375 				memset(skb_hwtstamps(skb), 0,
2376 				       sizeof(*skb_hwtstamps(skb)));
2377 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2378 			}
2379 		}
2380 	}
2381 	bnxt_deliver_skb(bp, bnapi, skb);
2382 	rc = 1;
2383 
2384 next_rx:
2385 	cpr->rx_packets += 1;
2386 	cpr->rx_bytes += len;
2387 
2388 next_rx_no_len:
2389 	rxr->rx_prod = NEXT_RX(prod);
2390 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2391 
2392 next_rx_no_prod_no_len:
2393 	*raw_cons = tmp_raw_cons;
2394 
2395 	return rc;
2396 
2397 oom_next_rx:
2398 	cpr->sw_stats->rx.rx_oom_discards += 1;
2399 	rc = -ENOMEM;
2400 	goto next_rx;
2401 }
2402 
2403 /* In netpoll mode, if we are using a combined completion ring, we need to
2404  * discard the rx packets and recycle the buffers.
2405  */
2406 static int bnxt_force_rx_discard(struct bnxt *bp,
2407 				 struct bnxt_cp_ring_info *cpr,
2408 				 u32 *raw_cons, u8 *event)
2409 {
2410 	u32 tmp_raw_cons = *raw_cons;
2411 	struct rx_cmp_ext *rxcmp1;
2412 	struct rx_cmp *rxcmp;
2413 	u16 cp_cons;
2414 	u8 cmp_type;
2415 	int rc;
2416 
2417 	cp_cons = RING_CMP(tmp_raw_cons);
2418 	rxcmp = (struct rx_cmp *)
2419 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2420 
2421 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2422 	cp_cons = RING_CMP(tmp_raw_cons);
2423 	rxcmp1 = (struct rx_cmp_ext *)
2424 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2425 
2426 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2427 		return -EBUSY;
2428 
2429 	/* The valid test of the entry must be done first before
2430 	 * reading any further.
2431 	 */
2432 	dma_rmb();
2433 	cmp_type = RX_CMP_TYPE(rxcmp);
2434 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2435 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2436 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2437 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2438 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2439 		struct rx_tpa_end_cmp_ext *tpa_end1;
2440 
2441 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2442 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2443 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2444 	}
2445 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2446 	if (rc && rc != -EBUSY)
2447 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2448 	return rc;
2449 }
2450 
2451 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2452 {
2453 	struct bnxt_fw_health *fw_health = bp->fw_health;
2454 	u32 reg = fw_health->regs[reg_idx];
2455 	u32 reg_type, reg_off, val = 0;
2456 
2457 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2458 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2459 	switch (reg_type) {
2460 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2461 		pci_read_config_dword(bp->pdev, reg_off, &val);
2462 		break;
2463 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2464 		reg_off = fw_health->mapped_regs[reg_idx];
2465 		fallthrough;
2466 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2467 		val = readl(bp->bar0 + reg_off);
2468 		break;
2469 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2470 		val = readl(bp->bar1 + reg_off);
2471 		break;
2472 	}
2473 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2474 		val &= fw_health->fw_reset_inprog_reg_mask;
2475 	return val;
2476 }
2477 
2478 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2479 {
2480 	int i;
2481 
2482 	for (i = 0; i < bp->rx_nr_rings; i++) {
2483 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2484 		struct bnxt_ring_grp_info *grp_info;
2485 
2486 		grp_info = &bp->grp_info[grp_idx];
2487 		if (grp_info->agg_fw_ring_id == ring_id)
2488 			return grp_idx;
2489 	}
2490 	return INVALID_HW_RING_ID;
2491 }
2492 
2493 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2494 {
2495 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2496 
2497 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2498 		return link_info->force_link_speed2;
2499 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2500 		return link_info->force_pam4_link_speed;
2501 	return link_info->force_link_speed;
2502 }
2503 
2504 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2505 {
2506 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2507 
2508 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2509 		link_info->req_link_speed = link_info->force_link_speed2;
2510 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2511 		switch (link_info->req_link_speed) {
2512 		case BNXT_LINK_SPEED_50GB_PAM4:
2513 		case BNXT_LINK_SPEED_100GB_PAM4:
2514 		case BNXT_LINK_SPEED_200GB_PAM4:
2515 		case BNXT_LINK_SPEED_400GB_PAM4:
2516 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2517 			break;
2518 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2519 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2520 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2521 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2522 			break;
2523 		default:
2524 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2525 		}
2526 		return;
2527 	}
2528 	link_info->req_link_speed = link_info->force_link_speed;
2529 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2530 	if (link_info->force_pam4_link_speed) {
2531 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2532 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2533 	}
2534 }
2535 
2536 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2537 {
2538 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2539 
2540 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2541 		link_info->advertising = link_info->auto_link_speeds2;
2542 		return;
2543 	}
2544 	link_info->advertising = link_info->auto_link_speeds;
2545 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2546 }
2547 
2548 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2549 {
2550 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2551 
2552 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2553 		if (link_info->req_link_speed != link_info->force_link_speed2)
2554 			return true;
2555 		return false;
2556 	}
2557 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2558 	    link_info->req_link_speed != link_info->force_link_speed)
2559 		return true;
2560 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2561 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2562 		return true;
2563 	return false;
2564 }
2565 
2566 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2567 {
2568 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2569 
2570 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2571 		if (link_info->advertising != link_info->auto_link_speeds2)
2572 			return true;
2573 		return false;
2574 	}
2575 	if (link_info->advertising != link_info->auto_link_speeds ||
2576 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2577 		return true;
2578 	return false;
2579 }
2580 
2581 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2582 {
2583 	u32 flags = bp->ctx->ctx_arr[type].flags;
2584 
2585 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2586 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2587 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2588 }
2589 
2590 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2591 {
2592 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2593 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2594 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2595 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2596 	struct bnxt_bs_trace_info *bs_trace;
2597 	int last_pg;
2598 
2599 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2600 		return;
2601 
2602 	mem_size = ctxm->max_entries * ctxm->entry_size;
2603 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2604 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2605 
2606 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2607 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2608 
2609 	rmem = &ctx_pg[0].ring_mem;
2610 	bs_trace = &bp->bs_trace[trace_type];
2611 	bs_trace->ctx_type = ctxm->type;
2612 	bs_trace->trace_type = trace_type;
2613 	if (pages > MAX_CTX_PAGES) {
2614 		int last_pg_dir = rmem->nr_pages - 1;
2615 
2616 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2617 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2618 	} else {
2619 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2620 	}
2621 	bs_trace->magic_byte += magic_byte_offset;
2622 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2623 }
2624 
2625 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2626 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2627 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2628 
2629 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2630 	(((data2) &							\
2631 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2632 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2633 
2634 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2635 	((data2) &							\
2636 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2637 
2638 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2639 	(((data2) &							\
2640 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2641 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2642 
2643 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2644 	((data1) &							\
2645 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2646 
2647 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2648 	(((data1) &							\
2649 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2650 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2651 
2652 /* Return true if the workqueue has to be scheduled */
2653 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2654 {
2655 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2656 
2657 	switch (err_type) {
2658 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2659 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2660 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2661 		break;
2662 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2663 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2664 		break;
2665 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2666 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2667 		break;
2668 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2669 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2670 		char *threshold_type;
2671 		bool notify = false;
2672 		char *dir_str;
2673 
2674 		switch (type) {
2675 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2676 			threshold_type = "warning";
2677 			break;
2678 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2679 			threshold_type = "critical";
2680 			break;
2681 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2682 			threshold_type = "fatal";
2683 			break;
2684 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2685 			threshold_type = "shutdown";
2686 			break;
2687 		default:
2688 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2689 			return false;
2690 		}
2691 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2692 			dir_str = "above";
2693 			notify = true;
2694 		} else {
2695 			dir_str = "below";
2696 		}
2697 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2698 			    dir_str, threshold_type);
2699 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2700 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2701 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2702 		if (notify) {
2703 			bp->thermal_threshold_type = type;
2704 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2705 			return true;
2706 		}
2707 		return false;
2708 	}
2709 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2710 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2711 		break;
2712 	default:
2713 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2714 			   err_type);
2715 		break;
2716 	}
2717 	return false;
2718 }
2719 
2720 #define BNXT_GET_EVENT_PORT(data)	\
2721 	((data) &			\
2722 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2723 
2724 #define BNXT_EVENT_RING_TYPE(data2)	\
2725 	((data2) &			\
2726 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2727 
2728 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2729 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2730 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2731 
2732 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2733 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2734 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2735 
2736 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2737 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2738 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2739 
2740 #define BNXT_PHC_BITS	48
2741 
2742 static int bnxt_async_event_process(struct bnxt *bp,
2743 				    struct hwrm_async_event_cmpl *cmpl)
2744 {
2745 	u16 event_id = le16_to_cpu(cmpl->event_id);
2746 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2747 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2748 
2749 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2750 		   event_id, data1, data2);
2751 
2752 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2753 	switch (event_id) {
2754 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2755 		struct bnxt_link_info *link_info = &bp->link_info;
2756 
2757 		if (BNXT_VF(bp))
2758 			goto async_event_process_exit;
2759 
2760 		/* print unsupported speed warning in forced speed mode only */
2761 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2762 		    (data1 & 0x20000)) {
2763 			u16 fw_speed = bnxt_get_force_speed(link_info);
2764 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2765 
2766 			if (speed != SPEED_UNKNOWN)
2767 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2768 					    speed);
2769 		}
2770 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2771 	}
2772 		fallthrough;
2773 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2774 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2775 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2776 		fallthrough;
2777 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2778 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2779 		break;
2780 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2781 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2782 		break;
2783 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2784 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2785 
2786 		if (BNXT_VF(bp))
2787 			break;
2788 
2789 		if (bp->pf.port_id != port_id)
2790 			break;
2791 
2792 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2793 		break;
2794 	}
2795 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2796 		if (BNXT_PF(bp))
2797 			goto async_event_process_exit;
2798 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2799 		break;
2800 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2801 		char *type_str = "Solicited";
2802 
2803 		if (!bp->fw_health)
2804 			goto async_event_process_exit;
2805 
2806 		bp->fw_reset_timestamp = jiffies;
2807 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2808 		if (!bp->fw_reset_min_dsecs)
2809 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2810 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2811 		if (!bp->fw_reset_max_dsecs)
2812 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2813 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2814 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2815 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2816 			type_str = "Fatal";
2817 			bp->fw_health->fatalities++;
2818 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2819 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2820 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2821 			type_str = "Non-fatal";
2822 			bp->fw_health->survivals++;
2823 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2824 		}
2825 		netif_warn(bp, hw, bp->dev,
2826 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2827 			   type_str, data1, data2,
2828 			   bp->fw_reset_min_dsecs * 100,
2829 			   bp->fw_reset_max_dsecs * 100);
2830 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2831 		break;
2832 	}
2833 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2834 		struct bnxt_fw_health *fw_health = bp->fw_health;
2835 		char *status_desc = "healthy";
2836 		u32 status;
2837 
2838 		if (!fw_health)
2839 			goto async_event_process_exit;
2840 
2841 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2842 			fw_health->enabled = false;
2843 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2844 			break;
2845 		}
2846 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2847 		fw_health->tmr_multiplier =
2848 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2849 				     bp->current_interval * 10);
2850 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2851 		if (!fw_health->enabled)
2852 			fw_health->last_fw_heartbeat =
2853 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2854 		fw_health->last_fw_reset_cnt =
2855 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2856 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2857 		if (status != BNXT_FW_STATUS_HEALTHY)
2858 			status_desc = "unhealthy";
2859 		netif_info(bp, drv, bp->dev,
2860 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2861 			   fw_health->primary ? "primary" : "backup", status,
2862 			   status_desc, fw_health->last_fw_reset_cnt);
2863 		if (!fw_health->enabled) {
2864 			/* Make sure tmr_counter is set and visible to
2865 			 * bnxt_health_check() before setting enabled to true.
2866 			 */
2867 			smp_wmb();
2868 			fw_health->enabled = true;
2869 		}
2870 		goto async_event_process_exit;
2871 	}
2872 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2873 		netif_notice(bp, hw, bp->dev,
2874 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2875 			     data1, data2);
2876 		goto async_event_process_exit;
2877 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2878 		struct bnxt_rx_ring_info *rxr;
2879 		u16 grp_idx;
2880 
2881 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2882 			goto async_event_process_exit;
2883 
2884 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2885 			    BNXT_EVENT_RING_TYPE(data2), data1);
2886 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2887 			goto async_event_process_exit;
2888 
2889 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2890 		if (grp_idx == INVALID_HW_RING_ID) {
2891 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2892 				    data1);
2893 			goto async_event_process_exit;
2894 		}
2895 		rxr = bp->bnapi[grp_idx]->rx_ring;
2896 		bnxt_sched_reset_rxr(bp, rxr);
2897 		goto async_event_process_exit;
2898 	}
2899 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2900 		struct bnxt_fw_health *fw_health = bp->fw_health;
2901 
2902 		netif_notice(bp, hw, bp->dev,
2903 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2904 			     data1, data2);
2905 		if (fw_health) {
2906 			fw_health->echo_req_data1 = data1;
2907 			fw_health->echo_req_data2 = data2;
2908 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2909 			break;
2910 		}
2911 		goto async_event_process_exit;
2912 	}
2913 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2914 		bnxt_ptp_pps_event(bp, data1, data2);
2915 		goto async_event_process_exit;
2916 	}
2917 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2918 		if (bnxt_event_error_report(bp, data1, data2))
2919 			break;
2920 		goto async_event_process_exit;
2921 	}
2922 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2923 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2924 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2925 			if (BNXT_PTP_USE_RTC(bp)) {
2926 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2927 				unsigned long flags;
2928 				u64 ns;
2929 
2930 				if (!ptp)
2931 					goto async_event_process_exit;
2932 
2933 				bnxt_ptp_update_current_time(bp);
2934 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2935 				       BNXT_PHC_BITS) | ptp->current_time);
2936 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2937 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2938 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2939 			}
2940 			break;
2941 		}
2942 		goto async_event_process_exit;
2943 	}
2944 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2945 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2946 
2947 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2948 		goto async_event_process_exit;
2949 	}
2950 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2951 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2952 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2953 
2954 		if (type >= ARRAY_SIZE(bp->bs_trace))
2955 			goto async_event_process_exit;
2956 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2957 		goto async_event_process_exit;
2958 	}
2959 	default:
2960 		goto async_event_process_exit;
2961 	}
2962 	__bnxt_queue_sp_work(bp);
2963 async_event_process_exit:
2964 	bnxt_ulp_async_events(bp, cmpl);
2965 	return 0;
2966 }
2967 
2968 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2969 {
2970 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2971 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2972 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2973 				(struct hwrm_fwd_req_cmpl *)txcmp;
2974 
2975 	switch (cmpl_type) {
2976 	case CMPL_BASE_TYPE_HWRM_DONE:
2977 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2978 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2979 		break;
2980 
2981 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2982 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2983 
2984 		if ((vf_id < bp->pf.first_vf_id) ||
2985 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2986 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2987 				   vf_id);
2988 			return -EINVAL;
2989 		}
2990 
2991 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2992 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2993 		break;
2994 
2995 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2996 		bnxt_async_event_process(bp,
2997 					 (struct hwrm_async_event_cmpl *)txcmp);
2998 		break;
2999 
3000 	default:
3001 		break;
3002 	}
3003 
3004 	return 0;
3005 }
3006 
3007 static bool bnxt_vnic_is_active(struct bnxt *bp)
3008 {
3009 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
3010 
3011 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
3012 }
3013 
3014 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
3015 {
3016 	struct bnxt_napi *bnapi = dev_instance;
3017 	struct bnxt *bp = bnapi->bp;
3018 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3019 	u32 cons = RING_CMP(cpr->cp_raw_cons);
3020 
3021 	cpr->event_ctr++;
3022 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
3023 	napi_schedule(&bnapi->napi);
3024 	return IRQ_HANDLED;
3025 }
3026 
3027 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
3028 {
3029 	u32 raw_cons = cpr->cp_raw_cons;
3030 	u16 cons = RING_CMP(raw_cons);
3031 	struct tx_cmp *txcmp;
3032 
3033 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3034 
3035 	return TX_CMP_VALID(txcmp, raw_cons);
3036 }
3037 
3038 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3039 			    int budget)
3040 {
3041 	struct bnxt_napi *bnapi = cpr->bnapi;
3042 	u32 raw_cons = cpr->cp_raw_cons;
3043 	bool flush_xdp = false;
3044 	u32 cons;
3045 	int rx_pkts = 0;
3046 	u8 event = 0;
3047 	struct tx_cmp *txcmp;
3048 
3049 	cpr->has_more_work = 0;
3050 	cpr->had_work_done = 1;
3051 	while (1) {
3052 		u8 cmp_type;
3053 		int rc;
3054 
3055 		cons = RING_CMP(raw_cons);
3056 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3057 
3058 		if (!TX_CMP_VALID(txcmp, raw_cons))
3059 			break;
3060 
3061 		/* The valid test of the entry must be done first before
3062 		 * reading any further.
3063 		 */
3064 		dma_rmb();
3065 		cmp_type = TX_CMP_TYPE(txcmp);
3066 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3067 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3068 			u32 opaque = txcmp->tx_cmp_opaque;
3069 			struct bnxt_tx_ring_info *txr;
3070 			u16 tx_freed;
3071 
3072 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3073 			event |= BNXT_TX_CMP_EVENT;
3074 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3075 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3076 			else
3077 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3078 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3079 				   bp->tx_ring_mask;
3080 			/* return full budget so NAPI will complete. */
3081 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3082 				rx_pkts = budget;
3083 				raw_cons = NEXT_RAW_CMP(raw_cons);
3084 				if (budget)
3085 					cpr->has_more_work = 1;
3086 				break;
3087 			}
3088 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3089 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3090 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3091 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3092 			if (likely(budget))
3093 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3094 			else
3095 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3096 							   &event);
3097 			if (event & BNXT_REDIRECT_EVENT)
3098 				flush_xdp = true;
3099 			if (likely(rc >= 0))
3100 				rx_pkts += rc;
3101 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3102 			 * the NAPI budget.  Otherwise, we may potentially loop
3103 			 * here forever if we consistently cannot allocate
3104 			 * buffers.
3105 			 */
3106 			else if (rc == -ENOMEM && budget)
3107 				rx_pkts++;
3108 			else if (rc == -EBUSY)	/* partial completion */
3109 				break;
3110 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3111 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3112 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3113 			bnxt_hwrm_handler(bp, txcmp);
3114 		}
3115 		raw_cons = NEXT_RAW_CMP(raw_cons);
3116 
3117 		if (rx_pkts && rx_pkts == budget) {
3118 			cpr->has_more_work = 1;
3119 			break;
3120 		}
3121 	}
3122 
3123 	if (flush_xdp) {
3124 		xdp_do_flush();
3125 		event &= ~BNXT_REDIRECT_EVENT;
3126 	}
3127 
3128 	if (event & BNXT_TX_EVENT) {
3129 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3130 		u16 prod = txr->tx_prod;
3131 
3132 		/* Sync BD data before updating doorbell */
3133 		wmb();
3134 
3135 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3136 		event &= ~BNXT_TX_EVENT;
3137 	}
3138 
3139 	cpr->cp_raw_cons = raw_cons;
3140 	bnapi->events |= event;
3141 	return rx_pkts;
3142 }
3143 
3144 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3145 				  int budget)
3146 {
3147 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3148 		bnapi->tx_int(bp, bnapi, budget);
3149 
3150 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3151 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3152 
3153 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3154 		bnapi->events &= ~BNXT_RX_EVENT;
3155 	}
3156 	if (bnapi->events & BNXT_AGG_EVENT) {
3157 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3158 
3159 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3160 		bnapi->events &= ~BNXT_AGG_EVENT;
3161 	}
3162 }
3163 
3164 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3165 			  int budget)
3166 {
3167 	struct bnxt_napi *bnapi = cpr->bnapi;
3168 	int rx_pkts;
3169 
3170 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3171 
3172 	/* ACK completion ring before freeing tx ring and producing new
3173 	 * buffers in rx/agg rings to prevent overflowing the completion
3174 	 * ring.
3175 	 */
3176 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3177 
3178 	__bnxt_poll_work_done(bp, bnapi, budget);
3179 	return rx_pkts;
3180 }
3181 
3182 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3183 {
3184 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3185 	struct bnxt *bp = bnapi->bp;
3186 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3187 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3188 	struct tx_cmp *txcmp;
3189 	struct rx_cmp_ext *rxcmp1;
3190 	u32 cp_cons, tmp_raw_cons;
3191 	u32 raw_cons = cpr->cp_raw_cons;
3192 	bool flush_xdp = false;
3193 	u32 rx_pkts = 0;
3194 	u8 event = 0;
3195 
3196 	while (1) {
3197 		int rc;
3198 
3199 		cp_cons = RING_CMP(raw_cons);
3200 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3201 
3202 		if (!TX_CMP_VALID(txcmp, raw_cons))
3203 			break;
3204 
3205 		/* The valid test of the entry must be done first before
3206 		 * reading any further.
3207 		 */
3208 		dma_rmb();
3209 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3210 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3211 			cp_cons = RING_CMP(tmp_raw_cons);
3212 			rxcmp1 = (struct rx_cmp_ext *)
3213 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3214 
3215 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3216 				break;
3217 
3218 			/* force an error to recycle the buffer */
3219 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3220 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3221 
3222 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3223 			if (likely(rc == -EIO) && budget)
3224 				rx_pkts++;
3225 			else if (rc == -EBUSY)	/* partial completion */
3226 				break;
3227 			if (event & BNXT_REDIRECT_EVENT)
3228 				flush_xdp = true;
3229 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3230 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3231 			bnxt_hwrm_handler(bp, txcmp);
3232 		} else {
3233 			netdev_err(bp->dev,
3234 				   "Invalid completion received on special ring\n");
3235 		}
3236 		raw_cons = NEXT_RAW_CMP(raw_cons);
3237 
3238 		if (rx_pkts == budget)
3239 			break;
3240 	}
3241 
3242 	cpr->cp_raw_cons = raw_cons;
3243 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3244 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3245 
3246 	if (event & BNXT_AGG_EVENT)
3247 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3248 	if (flush_xdp)
3249 		xdp_do_flush();
3250 
3251 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3252 		napi_complete_done(napi, rx_pkts);
3253 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3254 	}
3255 	return rx_pkts;
3256 }
3257 
3258 static int bnxt_poll(struct napi_struct *napi, int budget)
3259 {
3260 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3261 	struct bnxt *bp = bnapi->bp;
3262 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3263 	int work_done = 0;
3264 
3265 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3266 		napi_complete(napi);
3267 		return 0;
3268 	}
3269 	while (1) {
3270 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3271 
3272 		if (work_done >= budget) {
3273 			if (!budget)
3274 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3275 			break;
3276 		}
3277 
3278 		if (!bnxt_has_work(bp, cpr)) {
3279 			if (napi_complete_done(napi, work_done))
3280 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3281 			break;
3282 		}
3283 	}
3284 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3285 		struct dim_sample dim_sample = {};
3286 
3287 		dim_update_sample(cpr->event_ctr,
3288 				  cpr->rx_packets,
3289 				  cpr->rx_bytes,
3290 				  &dim_sample);
3291 		net_dim(&cpr->dim, &dim_sample);
3292 	}
3293 	return work_done;
3294 }
3295 
3296 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3297 {
3298 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3299 	int i, work_done = 0;
3300 
3301 	for (i = 0; i < cpr->cp_ring_count; i++) {
3302 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3303 
3304 		if (cpr2->had_nqe_notify) {
3305 			work_done += __bnxt_poll_work(bp, cpr2,
3306 						      budget - work_done);
3307 			cpr->has_more_work |= cpr2->has_more_work;
3308 		}
3309 	}
3310 	return work_done;
3311 }
3312 
3313 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3314 				 u64 dbr_type, int budget)
3315 {
3316 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3317 	int i;
3318 
3319 	for (i = 0; i < cpr->cp_ring_count; i++) {
3320 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3321 		struct bnxt_db_info *db;
3322 
3323 		if (cpr2->had_work_done) {
3324 			u32 tgl = 0;
3325 
3326 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3327 				cpr2->had_nqe_notify = 0;
3328 				tgl = cpr2->toggle;
3329 			}
3330 			db = &cpr2->cp_db;
3331 			bnxt_writeq(bp,
3332 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3333 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3334 				    db->doorbell);
3335 			cpr2->had_work_done = 0;
3336 		}
3337 	}
3338 	__bnxt_poll_work_done(bp, bnapi, budget);
3339 }
3340 
3341 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3342 {
3343 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3344 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3345 	struct bnxt_cp_ring_info *cpr_rx;
3346 	u32 raw_cons = cpr->cp_raw_cons;
3347 	struct bnxt *bp = bnapi->bp;
3348 	struct nqe_cn *nqcmp;
3349 	int work_done = 0;
3350 	u32 cons;
3351 
3352 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3353 		napi_complete(napi);
3354 		return 0;
3355 	}
3356 	if (cpr->has_more_work) {
3357 		cpr->has_more_work = 0;
3358 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3359 	}
3360 	while (1) {
3361 		u16 type;
3362 
3363 		cons = RING_CMP(raw_cons);
3364 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3365 
3366 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3367 			if (cpr->has_more_work)
3368 				break;
3369 
3370 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3371 					     budget);
3372 			cpr->cp_raw_cons = raw_cons;
3373 			if (napi_complete_done(napi, work_done))
3374 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3375 						  cpr->cp_raw_cons);
3376 			goto poll_done;
3377 		}
3378 
3379 		/* The valid test of the entry must be done first before
3380 		 * reading any further.
3381 		 */
3382 		dma_rmb();
3383 
3384 		type = le16_to_cpu(nqcmp->type);
3385 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3386 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3387 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3388 			struct bnxt_cp_ring_info *cpr2;
3389 
3390 			/* No more budget for RX work */
3391 			if (budget && work_done >= budget &&
3392 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3393 				break;
3394 
3395 			idx = BNXT_NQ_HDL_IDX(idx);
3396 			cpr2 = &cpr->cp_ring_arr[idx];
3397 			cpr2->had_nqe_notify = 1;
3398 			cpr2->toggle = NQE_CN_TOGGLE(type);
3399 			work_done += __bnxt_poll_work(bp, cpr2,
3400 						      budget - work_done);
3401 			cpr->has_more_work |= cpr2->has_more_work;
3402 		} else {
3403 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3404 		}
3405 		raw_cons = NEXT_RAW_CMP(raw_cons);
3406 	}
3407 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3408 	if (raw_cons != cpr->cp_raw_cons) {
3409 		cpr->cp_raw_cons = raw_cons;
3410 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3411 	}
3412 poll_done:
3413 	cpr_rx = &cpr->cp_ring_arr[0];
3414 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3415 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3416 		struct dim_sample dim_sample = {};
3417 
3418 		dim_update_sample(cpr->event_ctr,
3419 				  cpr_rx->rx_packets,
3420 				  cpr_rx->rx_bytes,
3421 				  &dim_sample);
3422 		net_dim(&cpr->dim, &dim_sample);
3423 	}
3424 	return work_done;
3425 }
3426 
3427 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3428 				       struct bnxt_tx_ring_info *txr, int idx)
3429 {
3430 	int i, max_idx;
3431 	struct pci_dev *pdev = bp->pdev;
3432 	unsigned int dma_len;
3433 	dma_addr_t dma_addr;
3434 
3435 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3436 
3437 	for (i = 0; i < max_idx;) {
3438 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3439 		struct bnxt_sw_tx_bd *head_buf = tx_buf;
3440 		struct sk_buff *skb;
3441 		int j, last;
3442 
3443 		if (idx  < bp->tx_nr_rings_xdp &&
3444 		    tx_buf->action == XDP_REDIRECT) {
3445 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3446 			dma_len = dma_unmap_len(tx_buf, len);
3447 
3448 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3449 					 DMA_TO_DEVICE);
3450 			xdp_return_frame(tx_buf->xdpf);
3451 			tx_buf->action = 0;
3452 			tx_buf->xdpf = NULL;
3453 			i++;
3454 			continue;
3455 		}
3456 
3457 		skb = tx_buf->skb;
3458 		if (!skb) {
3459 			i++;
3460 			continue;
3461 		}
3462 
3463 		tx_buf->skb = NULL;
3464 
3465 		if (tx_buf->is_push) {
3466 			dev_kfree_skb(skb);
3467 			i += 2;
3468 			continue;
3469 		}
3470 
3471 		if (dma_unmap_len(tx_buf, len)) {
3472 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3473 			dma_len = dma_unmap_len(tx_buf, len);
3474 
3475 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3476 					 DMA_TO_DEVICE);
3477 		}
3478 
3479 		last = tx_buf->nr_frags;
3480 		i += 2;
3481 		for (j = 0; j < last; j++, i++) {
3482 			int ring_idx = i & bp->tx_ring_mask;
3483 
3484 			tx_buf = &txr->tx_buf_ring[ring_idx];
3485 			if (dma_unmap_len(tx_buf, len)) {
3486 				dma_addr = dma_unmap_addr(tx_buf, mapping);
3487 				dma_len = dma_unmap_len(tx_buf, len);
3488 
3489 				netmem_dma_unmap_page_attrs(&pdev->dev,
3490 							    dma_addr, dma_len,
3491 							    DMA_TO_DEVICE, 0);
3492 			}
3493 		}
3494 		if (head_buf->is_sw_gso) {
3495 			u16 inline_cons = txr->tx_inline_cons + 1;
3496 
3497 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
3498 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
3499 				tso_dma_map_complete(&pdev->dev,
3500 						     &head_buf->sw_gso_cstate);
3501 			} else {
3502 				skb = NULL;
3503 			}
3504 			head_buf->is_sw_gso = 0;
3505 		}
3506 		if (skb)
3507 			dev_kfree_skb(skb);
3508 	}
3509 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3510 }
3511 
3512 static void bnxt_free_tx_skbs(struct bnxt *bp)
3513 {
3514 	int i;
3515 
3516 	if (!bp->tx_ring)
3517 		return;
3518 
3519 	for (i = 0; i < bp->tx_nr_rings; i++) {
3520 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3521 
3522 		if (!txr->tx_buf_ring)
3523 			continue;
3524 
3525 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3526 	}
3527 
3528 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3529 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3530 }
3531 
3532 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3533 {
3534 	int i, max_idx;
3535 
3536 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3537 
3538 	for (i = 0; i < max_idx; i++) {
3539 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3540 		void *data = rx_buf->data;
3541 
3542 		if (!data)
3543 			continue;
3544 
3545 		rx_buf->data = NULL;
3546 		if (BNXT_RX_PAGE_MODE(bp))
3547 			page_pool_recycle_direct(rxr->page_pool, data);
3548 		else
3549 			page_pool_free_va(rxr->head_pool, data, true);
3550 	}
3551 }
3552 
3553 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3554 {
3555 	int i, max_idx;
3556 
3557 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3558 
3559 	for (i = 0; i < max_idx; i++) {
3560 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3561 		netmem_ref netmem = rx_agg_buf->netmem;
3562 
3563 		if (!netmem)
3564 			continue;
3565 
3566 		rx_agg_buf->netmem = 0;
3567 		__clear_bit(i, rxr->rx_agg_bmap);
3568 
3569 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3570 	}
3571 }
3572 
3573 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3574 					struct bnxt_rx_ring_info *rxr)
3575 {
3576 	int i;
3577 
3578 	for (i = 0; i < bp->max_tpa; i++) {
3579 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3580 		u8 *data = tpa_info->data;
3581 
3582 		if (!data)
3583 			continue;
3584 
3585 		tpa_info->data = NULL;
3586 		page_pool_free_va(rxr->head_pool, data, false);
3587 	}
3588 }
3589 
3590 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3591 				       struct bnxt_rx_ring_info *rxr)
3592 {
3593 	struct bnxt_tpa_idx_map *map;
3594 
3595 	if (!rxr->rx_tpa)
3596 		goto skip_rx_tpa_free;
3597 
3598 	bnxt_free_one_tpa_info_data(bp, rxr);
3599 
3600 skip_rx_tpa_free:
3601 	if (!rxr->rx_buf_ring)
3602 		goto skip_rx_buf_free;
3603 
3604 	bnxt_free_one_rx_ring(bp, rxr);
3605 
3606 skip_rx_buf_free:
3607 	if (!rxr->rx_agg_ring)
3608 		goto skip_rx_agg_free;
3609 
3610 	bnxt_free_one_rx_agg_ring(bp, rxr);
3611 
3612 skip_rx_agg_free:
3613 	map = rxr->rx_tpa_idx_map;
3614 	if (map)
3615 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3616 }
3617 
3618 static void bnxt_free_rx_skbs(struct bnxt *bp)
3619 {
3620 	int i;
3621 
3622 	if (!bp->rx_ring)
3623 		return;
3624 
3625 	for (i = 0; i < bp->rx_nr_rings; i++)
3626 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3627 }
3628 
3629 static void bnxt_free_skbs(struct bnxt *bp)
3630 {
3631 	bnxt_free_tx_skbs(bp);
3632 	bnxt_free_rx_skbs(bp);
3633 }
3634 
3635 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3636 {
3637 	u8 init_val = ctxm->init_value;
3638 	u16 offset = ctxm->init_offset;
3639 	u8 *p2 = p;
3640 	int i;
3641 
3642 	if (!init_val)
3643 		return;
3644 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3645 		memset(p, init_val, len);
3646 		return;
3647 	}
3648 	for (i = 0; i < len; i += ctxm->entry_size)
3649 		*(p2 + i + offset) = init_val;
3650 }
3651 
3652 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3653 			       void *buf, size_t offset, size_t head,
3654 			       size_t tail)
3655 {
3656 	int i, head_page, start_idx, source_offset;
3657 	size_t len, rem_len, total_len, max_bytes;
3658 
3659 	head_page = head / rmem->page_size;
3660 	source_offset = head % rmem->page_size;
3661 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3662 	if (!total_len)
3663 		total_len = MAX_CTX_BYTES;
3664 	start_idx = head_page % MAX_CTX_PAGES;
3665 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3666 		    source_offset;
3667 	total_len = min(total_len, max_bytes);
3668 	rem_len = total_len;
3669 
3670 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3671 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3672 		if (buf)
3673 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3674 			       len);
3675 		offset += len;
3676 		rem_len -= len;
3677 	}
3678 	return total_len;
3679 }
3680 
3681 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3682 {
3683 	struct pci_dev *pdev = bp->pdev;
3684 	int i;
3685 
3686 	if (!rmem->pg_arr)
3687 		goto skip_pages;
3688 
3689 	for (i = 0; i < rmem->nr_pages; i++) {
3690 		if (!rmem->pg_arr[i])
3691 			continue;
3692 
3693 		dma_free_coherent(&pdev->dev, rmem->page_size,
3694 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3695 
3696 		rmem->pg_arr[i] = NULL;
3697 	}
3698 skip_pages:
3699 	if (rmem->pg_tbl) {
3700 		size_t pg_tbl_size = rmem->nr_pages * 8;
3701 
3702 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3703 			pg_tbl_size = rmem->page_size;
3704 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3705 				  rmem->pg_tbl, rmem->pg_tbl_map);
3706 		rmem->pg_tbl = NULL;
3707 	}
3708 	if (rmem->vmem_size && *rmem->vmem) {
3709 		vfree(*rmem->vmem);
3710 		*rmem->vmem = NULL;
3711 	}
3712 }
3713 
3714 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3715 {
3716 	struct pci_dev *pdev = bp->pdev;
3717 	u64 valid_bit = 0;
3718 	int i;
3719 
3720 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3721 		valid_bit = PTU_PTE_VALID;
3722 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3723 		size_t pg_tbl_size = rmem->nr_pages * 8;
3724 
3725 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3726 			pg_tbl_size = rmem->page_size;
3727 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3728 						  &rmem->pg_tbl_map,
3729 						  GFP_KERNEL);
3730 		if (!rmem->pg_tbl)
3731 			return -ENOMEM;
3732 	}
3733 
3734 	for (i = 0; i < rmem->nr_pages; i++) {
3735 		u64 extra_bits = valid_bit;
3736 
3737 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3738 						     rmem->page_size,
3739 						     &rmem->dma_arr[i],
3740 						     GFP_KERNEL);
3741 		if (!rmem->pg_arr[i])
3742 			return -ENOMEM;
3743 
3744 		if (rmem->ctx_mem)
3745 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3746 					  rmem->page_size);
3747 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3748 			if (i == rmem->nr_pages - 2 &&
3749 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3750 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3751 			else if (i == rmem->nr_pages - 1 &&
3752 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3753 				extra_bits |= PTU_PTE_LAST;
3754 			rmem->pg_tbl[i] =
3755 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3756 		}
3757 	}
3758 
3759 	if (rmem->vmem_size) {
3760 		*rmem->vmem = vzalloc(rmem->vmem_size);
3761 		if (!(*rmem->vmem))
3762 			return -ENOMEM;
3763 	}
3764 	return 0;
3765 }
3766 
3767 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3768 				   struct bnxt_rx_ring_info *rxr)
3769 {
3770 	int i;
3771 
3772 	kfree(rxr->rx_tpa_idx_map);
3773 	rxr->rx_tpa_idx_map = NULL;
3774 	if (rxr->rx_tpa) {
3775 		for (i = 0; i < bp->max_tpa; i++) {
3776 			kfree(rxr->rx_tpa[i].agg_arr);
3777 			rxr->rx_tpa[i].agg_arr = NULL;
3778 		}
3779 	}
3780 	kfree(rxr->rx_tpa);
3781 	rxr->rx_tpa = NULL;
3782 }
3783 
3784 static void bnxt_free_tpa_info(struct bnxt *bp)
3785 {
3786 	int i;
3787 
3788 	for (i = 0; i < bp->rx_nr_rings; i++) {
3789 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3790 
3791 		bnxt_free_one_tpa_info(bp, rxr);
3792 	}
3793 }
3794 
3795 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3796 				   struct bnxt_rx_ring_info *rxr)
3797 {
3798 	struct rx_agg_cmp *agg;
3799 	int i;
3800 
3801 	rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info, bp->max_tpa);
3802 	if (!rxr->rx_tpa)
3803 		return -ENOMEM;
3804 
3805 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3806 		return 0;
3807 	for (i = 0; i < bp->max_tpa; i++) {
3808 		agg = kzalloc_objs(*agg, MAX_SKB_FRAGS);
3809 		if (!agg)
3810 			return -ENOMEM;
3811 		rxr->rx_tpa[i].agg_arr = agg;
3812 	}
3813 	rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map);
3814 	if (!rxr->rx_tpa_idx_map)
3815 		return -ENOMEM;
3816 
3817 	return 0;
3818 }
3819 
3820 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3821 {
3822 	int i, rc;
3823 
3824 	bp->max_tpa = MAX_TPA;
3825 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3826 		if (!bp->max_tpa_v2)
3827 			return 0;
3828 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3829 	}
3830 
3831 	for (i = 0; i < bp->rx_nr_rings; i++) {
3832 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3833 
3834 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3835 		if (rc)
3836 			return rc;
3837 	}
3838 	return 0;
3839 }
3840 
3841 static void bnxt_free_rx_rings(struct bnxt *bp)
3842 {
3843 	int i;
3844 
3845 	if (!bp->rx_ring)
3846 		return;
3847 
3848 	bnxt_free_tpa_info(bp);
3849 	for (i = 0; i < bp->rx_nr_rings; i++) {
3850 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3851 		struct bnxt_ring_struct *ring;
3852 
3853 		if (rxr->xdp_prog)
3854 			bpf_prog_put(rxr->xdp_prog);
3855 
3856 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3857 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3858 
3859 		page_pool_destroy(rxr->page_pool);
3860 		page_pool_destroy(rxr->head_pool);
3861 		rxr->page_pool = rxr->head_pool = NULL;
3862 
3863 		kfree(rxr->rx_agg_bmap);
3864 		rxr->rx_agg_bmap = NULL;
3865 
3866 		ring = &rxr->rx_ring_struct;
3867 		bnxt_free_ring(bp, &ring->ring_mem);
3868 
3869 		ring = &rxr->rx_agg_ring_struct;
3870 		bnxt_free_ring(bp, &ring->ring_mem);
3871 	}
3872 }
3873 
3874 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3875 				       struct bnxt_rx_ring_info *rxr)
3876 {
3877 	/* User may have chosen larger than default rx_page_size,
3878 	 * we keep the ring sizes uniform and also want uniform amount
3879 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3880 	 */
3881 	int fill_level = bp->rx_agg_ring_size;
3882 
3883 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3884 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3885 
3886 	return fill_level;
3887 }
3888 
3889 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3890 				   struct bnxt_rx_ring_info *rxr,
3891 				   int numa_node)
3892 {
3893 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3894 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3895 	struct page_pool_params pp = { 0 };
3896 	struct page_pool *pool;
3897 
3898 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3899 	if (BNXT_RX_PAGE_MODE(bp))
3900 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3901 
3902 	pp.order = get_order(rxr->rx_page_size);
3903 	pp.nid = numa_node;
3904 	pp.netdev = bp->dev;
3905 	pp.dev = &bp->pdev->dev;
3906 	pp.dma_dir = bp->rx_dir;
3907 	pp.max_len = PAGE_SIZE << pp.order;
3908 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3909 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3910 	pp.queue_idx = rxr->bnapi->index;
3911 
3912 	pool = page_pool_create(&pp);
3913 	if (IS_ERR(pool))
3914 		return PTR_ERR(pool);
3915 	rxr->page_pool = pool;
3916 
3917 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3918 	rxr->need_head_pool |= !!pp.order;
3919 	if (bnxt_separate_head_pool(rxr)) {
3920 		pp.order = 0;
3921 		pp.max_len = PAGE_SIZE;
3922 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3923 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3924 		pool = page_pool_create(&pp);
3925 		if (IS_ERR(pool))
3926 			goto err_destroy_pp;
3927 	} else {
3928 		page_pool_get(pool);
3929 	}
3930 	rxr->head_pool = pool;
3931 
3932 	return 0;
3933 
3934 err_destroy_pp:
3935 	page_pool_destroy(rxr->page_pool);
3936 	rxr->page_pool = NULL;
3937 	return PTR_ERR(pool);
3938 }
3939 
3940 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3941 {
3942 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3943 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3944 }
3945 
3946 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3947 {
3948 	u16 mem_size;
3949 
3950 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3951 	mem_size = rxr->rx_agg_bmap_size / 8;
3952 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3953 	if (!rxr->rx_agg_bmap)
3954 		return -ENOMEM;
3955 
3956 	return 0;
3957 }
3958 
3959 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3960 {
3961 	int numa_node = dev_to_node(&bp->pdev->dev);
3962 	int i, rc = 0, agg_rings = 0, cpu;
3963 
3964 	if (!bp->rx_ring)
3965 		return -ENOMEM;
3966 
3967 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3968 		agg_rings = 1;
3969 
3970 	for (i = 0; i < bp->rx_nr_rings; i++) {
3971 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3972 		struct bnxt_ring_struct *ring;
3973 		int cpu_node;
3974 
3975 		ring = &rxr->rx_ring_struct;
3976 
3977 		cpu = cpumask_local_spread(i, numa_node);
3978 		cpu_node = cpu_to_node(cpu);
3979 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3980 			   i, cpu_node);
3981 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3982 		if (rc)
3983 			return rc;
3984 		bnxt_enable_rx_page_pool(rxr);
3985 
3986 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3987 		if (rc < 0)
3988 			return rc;
3989 
3990 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3991 						MEM_TYPE_PAGE_POOL,
3992 						rxr->page_pool);
3993 		if (rc) {
3994 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3995 			return rc;
3996 		}
3997 
3998 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3999 		if (rc)
4000 			return rc;
4001 
4002 		ring->grp_idx = i;
4003 		if (agg_rings) {
4004 			ring = &rxr->rx_agg_ring_struct;
4005 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4006 			if (rc)
4007 				return rc;
4008 
4009 			ring->grp_idx = i;
4010 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
4011 			if (rc)
4012 				return rc;
4013 		}
4014 	}
4015 	if (bp->flags & BNXT_FLAG_TPA)
4016 		rc = bnxt_alloc_tpa_info(bp);
4017 	return rc;
4018 }
4019 
4020 static void bnxt_free_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4021 				    struct pci_dev *pdev)
4022 {
4023 	if (!txr->tx_inline_buf)
4024 		return;
4025 
4026 	dma_unmap_single(&pdev->dev, txr->tx_inline_dma,
4027 			 txr->tx_inline_size, DMA_TO_DEVICE);
4028 	kfree(txr->tx_inline_buf);
4029 	txr->tx_inline_buf = NULL;
4030 	txr->tx_inline_size = 0;
4031 }
4032 
4033 static int bnxt_alloc_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4034 				    struct pci_dev *pdev,
4035 				    unsigned int size)
4036 {
4037 	txr->tx_inline_buf = kmalloc(size, GFP_KERNEL);
4038 	if (!txr->tx_inline_buf)
4039 		return -ENOMEM;
4040 
4041 	txr->tx_inline_dma = dma_map_single(&pdev->dev, txr->tx_inline_buf,
4042 					    size, DMA_TO_DEVICE);
4043 	if (dma_mapping_error(&pdev->dev, txr->tx_inline_dma)) {
4044 		kfree(txr->tx_inline_buf);
4045 		txr->tx_inline_buf = NULL;
4046 		return -ENOMEM;
4047 	}
4048 	txr->tx_inline_size = size;
4049 
4050 	return 0;
4051 }
4052 
4053 static void bnxt_free_tx_rings(struct bnxt *bp)
4054 {
4055 	int i;
4056 	struct pci_dev *pdev = bp->pdev;
4057 
4058 	if (!bp->tx_ring)
4059 		return;
4060 
4061 	for (i = 0; i < bp->tx_nr_rings; i++) {
4062 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4063 		struct bnxt_ring_struct *ring;
4064 
4065 		if (txr->tx_push) {
4066 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
4067 					  txr->tx_push, txr->tx_push_mapping);
4068 			txr->tx_push = NULL;
4069 		}
4070 
4071 		bnxt_free_tx_inline_buf(txr, pdev);
4072 
4073 		ring = &txr->tx_ring_struct;
4074 
4075 		bnxt_free_ring(bp, &ring->ring_mem);
4076 	}
4077 }
4078 
4079 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
4080 	((tc) * (bp)->tx_nr_rings_per_tc)
4081 
4082 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
4083 	((tx) % (bp)->tx_nr_rings_per_tc)
4084 
4085 #define BNXT_RING_TO_TC(bp, tx)		\
4086 	((tx) / (bp)->tx_nr_rings_per_tc)
4087 
4088 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4089 {
4090 	int i, j, rc;
4091 	struct pci_dev *pdev = bp->pdev;
4092 
4093 	bp->tx_push_size = 0;
4094 	if (bp->tx_push_thresh) {
4095 		int push_size;
4096 
4097 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4098 					bp->tx_push_thresh);
4099 
4100 		if (push_size > 256) {
4101 			push_size = 0;
4102 			bp->tx_push_thresh = 0;
4103 		}
4104 
4105 		bp->tx_push_size = push_size;
4106 	}
4107 
4108 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4109 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4110 		struct bnxt_ring_struct *ring;
4111 		u8 qidx;
4112 
4113 		ring = &txr->tx_ring_struct;
4114 
4115 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4116 		if (rc)
4117 			return rc;
4118 
4119 		ring->grp_idx = txr->bnapi->index;
4120 		if (bp->tx_push_size) {
4121 			dma_addr_t mapping;
4122 
4123 			/* One pre-allocated DMA buffer to backup
4124 			 * TX push operation
4125 			 */
4126 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4127 						bp->tx_push_size,
4128 						&txr->tx_push_mapping,
4129 						GFP_KERNEL);
4130 
4131 			if (!txr->tx_push)
4132 				return -ENOMEM;
4133 
4134 			mapping = txr->tx_push_mapping +
4135 				sizeof(struct tx_push_bd);
4136 			txr->data_mapping = cpu_to_le64(mapping);
4137 		}
4138 		if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
4139 			rc = bnxt_alloc_tx_inline_buf(txr, pdev,
4140 						      BNXT_SW_USO_MAX_SEGS *
4141 						      TSO_HEADER_SIZE);
4142 			if (rc)
4143 				return rc;
4144 		}
4145 		qidx = bp->tc_to_qidx[j];
4146 		ring->queue_id = bp->q_info[qidx].queue_id;
4147 		spin_lock_init(&txr->xdp_tx_lock);
4148 		if (i < bp->tx_nr_rings_xdp)
4149 			continue;
4150 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4151 			j++;
4152 	}
4153 	return 0;
4154 }
4155 
4156 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4157 {
4158 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4159 
4160 	kfree(cpr->cp_desc_ring);
4161 	cpr->cp_desc_ring = NULL;
4162 	ring->ring_mem.pg_arr = NULL;
4163 	kfree(cpr->cp_desc_mapping);
4164 	cpr->cp_desc_mapping = NULL;
4165 	ring->ring_mem.dma_arr = NULL;
4166 }
4167 
4168 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4169 {
4170 	cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n);
4171 	if (!cpr->cp_desc_ring)
4172 		return -ENOMEM;
4173 	cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n);
4174 	if (!cpr->cp_desc_mapping)
4175 		return -ENOMEM;
4176 	return 0;
4177 }
4178 
4179 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4180 {
4181 	int i;
4182 
4183 	if (!bp->bnapi)
4184 		return;
4185 	for (i = 0; i < bp->cp_nr_rings; i++) {
4186 		struct bnxt_napi *bnapi = bp->bnapi[i];
4187 
4188 		if (!bnapi)
4189 			continue;
4190 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4191 	}
4192 }
4193 
4194 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4195 {
4196 	int i, n = bp->cp_nr_pages;
4197 
4198 	for (i = 0; i < bp->cp_nr_rings; i++) {
4199 		struct bnxt_napi *bnapi = bp->bnapi[i];
4200 		int rc;
4201 
4202 		if (!bnapi)
4203 			continue;
4204 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4205 		if (rc)
4206 			return rc;
4207 	}
4208 	return 0;
4209 }
4210 
4211 static void bnxt_free_cp_rings(struct bnxt *bp)
4212 {
4213 	int i;
4214 
4215 	if (!bp->bnapi)
4216 		return;
4217 
4218 	for (i = 0; i < bp->cp_nr_rings; i++) {
4219 		struct bnxt_napi *bnapi = bp->bnapi[i];
4220 		struct bnxt_cp_ring_info *cpr;
4221 		struct bnxt_ring_struct *ring;
4222 		int j;
4223 
4224 		if (!bnapi)
4225 			continue;
4226 
4227 		cpr = &bnapi->cp_ring;
4228 		ring = &cpr->cp_ring_struct;
4229 
4230 		bnxt_free_ring(bp, &ring->ring_mem);
4231 
4232 		if (!cpr->cp_ring_arr)
4233 			continue;
4234 
4235 		for (j = 0; j < cpr->cp_ring_count; j++) {
4236 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4237 
4238 			ring = &cpr2->cp_ring_struct;
4239 			bnxt_free_ring(bp, &ring->ring_mem);
4240 			bnxt_free_cp_arrays(cpr2);
4241 		}
4242 		kfree(cpr->cp_ring_arr);
4243 		cpr->cp_ring_arr = NULL;
4244 		cpr->cp_ring_count = 0;
4245 	}
4246 }
4247 
4248 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4249 				  struct bnxt_cp_ring_info *cpr)
4250 {
4251 	struct bnxt_ring_mem_info *rmem;
4252 	struct bnxt_ring_struct *ring;
4253 	int rc;
4254 
4255 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4256 	if (rc) {
4257 		bnxt_free_cp_arrays(cpr);
4258 		return -ENOMEM;
4259 	}
4260 	ring = &cpr->cp_ring_struct;
4261 	rmem = &ring->ring_mem;
4262 	rmem->nr_pages = bp->cp_nr_pages;
4263 	rmem->page_size = HW_CMPD_RING_SIZE;
4264 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4265 	rmem->dma_arr = cpr->cp_desc_mapping;
4266 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4267 	rc = bnxt_alloc_ring(bp, rmem);
4268 	if (rc) {
4269 		bnxt_free_ring(bp, rmem);
4270 		bnxt_free_cp_arrays(cpr);
4271 	}
4272 	return rc;
4273 }
4274 
4275 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4276 {
4277 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4278 	int i, j, rc, ulp_msix;
4279 	int tcs = bp->num_tc;
4280 
4281 	if (!tcs)
4282 		tcs = 1;
4283 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4284 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4285 		struct bnxt_napi *bnapi = bp->bnapi[i];
4286 		struct bnxt_cp_ring_info *cpr, *cpr2;
4287 		struct bnxt_ring_struct *ring;
4288 		int cp_count = 0, k;
4289 		int rx = 0, tx = 0;
4290 
4291 		if (!bnapi)
4292 			continue;
4293 
4294 		cpr = &bnapi->cp_ring;
4295 		cpr->bnapi = bnapi;
4296 		ring = &cpr->cp_ring_struct;
4297 
4298 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4299 		if (rc)
4300 			return rc;
4301 
4302 		ring->map_idx = ulp_msix + i;
4303 
4304 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4305 			continue;
4306 
4307 		if (i < bp->rx_nr_rings) {
4308 			cp_count++;
4309 			rx = 1;
4310 		}
4311 		if (i < bp->tx_nr_rings_xdp) {
4312 			cp_count++;
4313 			tx = 1;
4314 		} else if ((sh && i < bp->tx_nr_rings) ||
4315 			 (!sh && i >= bp->rx_nr_rings)) {
4316 			cp_count += tcs;
4317 			tx = 1;
4318 		}
4319 
4320 		cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count);
4321 		if (!cpr->cp_ring_arr)
4322 			return -ENOMEM;
4323 		cpr->cp_ring_count = cp_count;
4324 
4325 		for (k = 0; k < cp_count; k++) {
4326 			cpr2 = &cpr->cp_ring_arr[k];
4327 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4328 			if (rc)
4329 				return rc;
4330 			cpr2->bnapi = bnapi;
4331 			cpr2->sw_stats = cpr->sw_stats;
4332 			cpr2->cp_idx = k;
4333 			if (!k && rx) {
4334 				bp->rx_ring[i].rx_cpr = cpr2;
4335 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4336 			} else {
4337 				int n, tc = k - rx;
4338 
4339 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4340 				bp->tx_ring[n].tx_cpr = cpr2;
4341 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4342 			}
4343 		}
4344 		if (tx)
4345 			j++;
4346 	}
4347 	return 0;
4348 }
4349 
4350 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4351 				     struct bnxt_rx_ring_info *rxr)
4352 {
4353 	struct bnxt_ring_mem_info *rmem;
4354 	struct bnxt_ring_struct *ring;
4355 
4356 	ring = &rxr->rx_ring_struct;
4357 	rmem = &ring->ring_mem;
4358 	rmem->nr_pages = bp->rx_nr_pages;
4359 	rmem->page_size = HW_RXBD_RING_SIZE;
4360 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4361 	rmem->dma_arr = rxr->rx_desc_mapping;
4362 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4363 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4364 
4365 	ring = &rxr->rx_agg_ring_struct;
4366 	rmem = &ring->ring_mem;
4367 	rmem->nr_pages = bp->rx_agg_nr_pages;
4368 	rmem->page_size = HW_RXBD_RING_SIZE;
4369 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4370 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4371 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4372 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4373 }
4374 
4375 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4376 				      struct bnxt_rx_ring_info *rxr)
4377 {
4378 	struct bnxt_ring_mem_info *rmem;
4379 	struct bnxt_ring_struct *ring;
4380 	int i;
4381 
4382 	rxr->page_pool->p.napi = NULL;
4383 	rxr->page_pool = NULL;
4384 	rxr->head_pool->p.napi = NULL;
4385 	rxr->head_pool = NULL;
4386 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4387 
4388 	ring = &rxr->rx_ring_struct;
4389 	rmem = &ring->ring_mem;
4390 	rmem->pg_tbl = NULL;
4391 	rmem->pg_tbl_map = 0;
4392 	for (i = 0; i < rmem->nr_pages; i++) {
4393 		rmem->pg_arr[i] = NULL;
4394 		rmem->dma_arr[i] = 0;
4395 	}
4396 	*rmem->vmem = NULL;
4397 
4398 	ring = &rxr->rx_agg_ring_struct;
4399 	rmem = &ring->ring_mem;
4400 	rmem->pg_tbl = NULL;
4401 	rmem->pg_tbl_map = 0;
4402 	for (i = 0; i < rmem->nr_pages; i++) {
4403 		rmem->pg_arr[i] = NULL;
4404 		rmem->dma_arr[i] = 0;
4405 	}
4406 	*rmem->vmem = NULL;
4407 }
4408 
4409 static void bnxt_init_ring_struct(struct bnxt *bp)
4410 {
4411 	int i, j;
4412 
4413 	for (i = 0; i < bp->cp_nr_rings; i++) {
4414 		struct bnxt_napi *bnapi = bp->bnapi[i];
4415 		struct netdev_queue_config qcfg;
4416 		struct bnxt_ring_mem_info *rmem;
4417 		struct bnxt_cp_ring_info *cpr;
4418 		struct bnxt_rx_ring_info *rxr;
4419 		struct bnxt_tx_ring_info *txr;
4420 		struct bnxt_ring_struct *ring;
4421 
4422 		if (!bnapi)
4423 			continue;
4424 
4425 		cpr = &bnapi->cp_ring;
4426 		ring = &cpr->cp_ring_struct;
4427 		rmem = &ring->ring_mem;
4428 		rmem->nr_pages = bp->cp_nr_pages;
4429 		rmem->page_size = HW_CMPD_RING_SIZE;
4430 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4431 		rmem->dma_arr = cpr->cp_desc_mapping;
4432 		rmem->vmem_size = 0;
4433 
4434 		rxr = bnapi->rx_ring;
4435 		if (!rxr)
4436 			goto skip_rx;
4437 
4438 		netdev_queue_config(bp->dev, i, &qcfg);
4439 		rxr->rx_page_size = qcfg.rx_page_size;
4440 
4441 		ring = &rxr->rx_ring_struct;
4442 		rmem = &ring->ring_mem;
4443 		rmem->nr_pages = bp->rx_nr_pages;
4444 		rmem->page_size = HW_RXBD_RING_SIZE;
4445 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4446 		rmem->dma_arr = rxr->rx_desc_mapping;
4447 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4448 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4449 
4450 		ring = &rxr->rx_agg_ring_struct;
4451 		rmem = &ring->ring_mem;
4452 		rmem->nr_pages = bp->rx_agg_nr_pages;
4453 		rmem->page_size = HW_RXBD_RING_SIZE;
4454 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4455 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4456 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4457 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4458 
4459 skip_rx:
4460 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4461 			ring = &txr->tx_ring_struct;
4462 			rmem = &ring->ring_mem;
4463 			rmem->nr_pages = bp->tx_nr_pages;
4464 			rmem->page_size = HW_TXBD_RING_SIZE;
4465 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4466 			rmem->dma_arr = txr->tx_desc_mapping;
4467 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4468 			rmem->vmem = (void **)&txr->tx_buf_ring;
4469 		}
4470 	}
4471 }
4472 
4473 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4474 {
4475 	int i;
4476 	u32 prod;
4477 	struct rx_bd **rx_buf_ring;
4478 
4479 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4480 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4481 		int j;
4482 		struct rx_bd *rxbd;
4483 
4484 		rxbd = rx_buf_ring[i];
4485 		if (!rxbd)
4486 			continue;
4487 
4488 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4489 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4490 			rxbd->rx_bd_opaque = prod;
4491 		}
4492 	}
4493 }
4494 
4495 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4496 				       struct bnxt_rx_ring_info *rxr,
4497 				       int ring_nr)
4498 {
4499 	u32 prod;
4500 	int i;
4501 
4502 	prod = rxr->rx_prod;
4503 	for (i = 0; i < bp->rx_ring_size; i++) {
4504 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4505 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4506 				    ring_nr, i, bp->rx_ring_size);
4507 			break;
4508 		}
4509 		prod = NEXT_RX(prod);
4510 	}
4511 	rxr->rx_prod = prod;
4512 }
4513 
4514 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4515 					  struct bnxt_rx_ring_info *rxr,
4516 					  int ring_nr)
4517 {
4518 	int fill_level, i;
4519 	u32 prod;
4520 
4521 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4522 
4523 	prod = rxr->rx_agg_prod;
4524 	for (i = 0; i < fill_level; i++) {
4525 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4526 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4527 				    ring_nr, i, bp->rx_agg_ring_size);
4528 			break;
4529 		}
4530 		prod = NEXT_RX_AGG(prod);
4531 	}
4532 	rxr->rx_agg_prod = prod;
4533 }
4534 
4535 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4536 					struct bnxt_rx_ring_info *rxr)
4537 {
4538 	dma_addr_t mapping;
4539 	u8 *data;
4540 	int i;
4541 
4542 	for (i = 0; i < bp->max_tpa; i++) {
4543 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4544 					    GFP_KERNEL);
4545 		if (!data)
4546 			return -ENOMEM;
4547 
4548 		rxr->rx_tpa[i].data = data;
4549 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4550 		rxr->rx_tpa[i].mapping = mapping;
4551 	}
4552 
4553 	return 0;
4554 }
4555 
4556 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4557 {
4558 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4559 	int rc;
4560 
4561 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4562 
4563 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4564 		return 0;
4565 
4566 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4567 
4568 	if (rxr->rx_tpa) {
4569 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4570 		if (rc)
4571 			return rc;
4572 	}
4573 	return 0;
4574 }
4575 
4576 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4577 				       struct bnxt_rx_ring_info *rxr)
4578 {
4579 	struct bnxt_ring_struct *ring;
4580 	u32 type;
4581 
4582 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4583 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4584 
4585 	if (NET_IP_ALIGN == 2)
4586 		type |= RX_BD_FLAGS_SOP;
4587 
4588 	ring = &rxr->rx_ring_struct;
4589 	bnxt_init_rxbd_pages(ring, type);
4590 	ring->fw_ring_id = INVALID_HW_RING_ID;
4591 }
4592 
4593 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4594 					   struct bnxt_rx_ring_info *rxr)
4595 {
4596 	struct bnxt_ring_struct *ring;
4597 	u32 type;
4598 
4599 	ring = &rxr->rx_agg_ring_struct;
4600 	ring->fw_ring_id = INVALID_HW_RING_ID;
4601 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4602 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4603 			RX_BD_TYPE_RX_AGG_BD;
4604 
4605 		/* On P7, setting EOP will cause the chip to disable
4606 		 * Relaxed Ordering (RO) for TPA data.  Disable EOP for
4607 		 * potentially higher performance with RO.
4608 		 */
4609 		if (BNXT_CHIP_P5_AND_MINUS(bp) || !(bp->flags & BNXT_FLAG_TPA))
4610 			type |= RX_BD_FLAGS_AGG_EOP;
4611 
4612 		bnxt_init_rxbd_pages(ring, type);
4613 	}
4614 }
4615 
4616 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4617 {
4618 	struct bnxt_rx_ring_info *rxr;
4619 
4620 	rxr = &bp->rx_ring[ring_nr];
4621 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4622 
4623 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4624 			     &rxr->bnapi->napi);
4625 
4626 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4627 		bpf_prog_add(bp->xdp_prog, 1);
4628 		rxr->xdp_prog = bp->xdp_prog;
4629 	}
4630 
4631 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4632 
4633 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4634 }
4635 
4636 static void bnxt_init_cp_rings(struct bnxt *bp)
4637 {
4638 	int i, j;
4639 
4640 	for (i = 0; i < bp->cp_nr_rings; i++) {
4641 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4642 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4643 
4644 		ring->fw_ring_id = INVALID_HW_RING_ID;
4645 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4646 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4647 		if (!cpr->cp_ring_arr)
4648 			continue;
4649 		for (j = 0; j < cpr->cp_ring_count; j++) {
4650 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4651 
4652 			ring = &cpr2->cp_ring_struct;
4653 			ring->fw_ring_id = INVALID_HW_RING_ID;
4654 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4655 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4656 		}
4657 	}
4658 }
4659 
4660 static int bnxt_init_rx_rings(struct bnxt *bp)
4661 {
4662 	int i, rc = 0;
4663 
4664 	if (BNXT_RX_PAGE_MODE(bp)) {
4665 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4666 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4667 	} else {
4668 		bp->rx_offset = BNXT_RX_OFFSET;
4669 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4670 	}
4671 
4672 	for (i = 0; i < bp->rx_nr_rings; i++) {
4673 		rc = bnxt_init_one_rx_ring(bp, i);
4674 		if (rc)
4675 			break;
4676 	}
4677 
4678 	return rc;
4679 }
4680 
4681 static int bnxt_init_tx_rings(struct bnxt *bp)
4682 {
4683 	netdev_features_t features;
4684 	u16 i;
4685 
4686 	features = bp->dev->features;
4687 
4688 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4689 				   bnxt_min_tx_desc_cnt(bp, features));
4690 
4691 	for (i = 0; i < bp->tx_nr_rings; i++) {
4692 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4693 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4694 
4695 		ring->fw_ring_id = INVALID_HW_RING_ID;
4696 
4697 		if (i >= bp->tx_nr_rings_xdp)
4698 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4699 					     NETDEV_QUEUE_TYPE_TX,
4700 					     &txr->bnapi->napi);
4701 	}
4702 
4703 	return 0;
4704 }
4705 
4706 static void bnxt_free_ring_grps(struct bnxt *bp)
4707 {
4708 	kfree(bp->grp_info);
4709 	bp->grp_info = NULL;
4710 }
4711 
4712 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4713 {
4714 	int i;
4715 
4716 	if (irq_re_init) {
4717 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4718 					    bp->cp_nr_rings);
4719 		if (!bp->grp_info)
4720 			return -ENOMEM;
4721 	}
4722 	for (i = 0; i < bp->cp_nr_rings; i++) {
4723 		if (irq_re_init)
4724 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4725 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4726 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4727 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4728 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4729 	}
4730 	return 0;
4731 }
4732 
4733 static void bnxt_free_vnics(struct bnxt *bp)
4734 {
4735 	kfree(bp->vnic_info);
4736 	bp->vnic_info = NULL;
4737 	bp->nr_vnics = 0;
4738 }
4739 
4740 static int bnxt_alloc_vnics(struct bnxt *bp)
4741 {
4742 	int num_vnics = 1;
4743 
4744 #ifdef CONFIG_RFS_ACCEL
4745 	if (bp->flags & BNXT_FLAG_RFS) {
4746 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4747 			num_vnics++;
4748 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4749 			num_vnics += bp->rx_nr_rings;
4750 	}
4751 #endif
4752 
4753 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4754 		num_vnics++;
4755 
4756 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4757 	if (!bp->vnic_info)
4758 		return -ENOMEM;
4759 
4760 	bp->nr_vnics = num_vnics;
4761 	return 0;
4762 }
4763 
4764 static void bnxt_init_vnics(struct bnxt *bp)
4765 {
4766 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4767 	int i;
4768 
4769 	for (i = 0; i < bp->nr_vnics; i++) {
4770 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4771 		int j;
4772 
4773 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4774 		vnic->vnic_id = i;
4775 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4776 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4777 
4778 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4779 
4780 		if (bp->vnic_info[i].rss_hash_key) {
4781 			if (i == BNXT_VNIC_DEFAULT) {
4782 				u8 *key = (void *)vnic->rss_hash_key;
4783 				int k;
4784 
4785 				if (!bp->rss_hash_key_valid &&
4786 				    !bp->rss_hash_key_updated) {
4787 					get_random_bytes(bp->rss_hash_key,
4788 							 HW_HASH_KEY_SIZE);
4789 					bp->rss_hash_key_updated = true;
4790 				}
4791 
4792 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4793 				       HW_HASH_KEY_SIZE);
4794 
4795 				if (!bp->rss_hash_key_updated)
4796 					continue;
4797 
4798 				bp->rss_hash_key_updated = false;
4799 				bp->rss_hash_key_valid = true;
4800 
4801 				bp->toeplitz_prefix = 0;
4802 				for (k = 0; k < 8; k++) {
4803 					bp->toeplitz_prefix <<= 8;
4804 					bp->toeplitz_prefix |= key[k];
4805 				}
4806 			} else {
4807 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4808 				       HW_HASH_KEY_SIZE);
4809 			}
4810 		}
4811 	}
4812 }
4813 
4814 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4815 {
4816 	int pages;
4817 
4818 	pages = ring_size / desc_per_pg;
4819 
4820 	if (!pages)
4821 		return 1;
4822 
4823 	pages++;
4824 
4825 	while (pages & (pages - 1))
4826 		pages++;
4827 
4828 	return pages;
4829 }
4830 
4831 void bnxt_set_tpa_flags(struct bnxt *bp)
4832 {
4833 	bp->flags &= ~BNXT_FLAG_TPA;
4834 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4835 		return;
4836 	if (bp->dev->features & NETIF_F_LRO)
4837 		bp->flags |= BNXT_FLAG_LRO;
4838 	else if (bp->dev->features & NETIF_F_GRO_HW)
4839 		bp->flags |= BNXT_FLAG_GRO;
4840 }
4841 
4842 static void bnxt_init_ring_params(struct bnxt *bp)
4843 {
4844 	unsigned int rx_size;
4845 
4846 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4847 	/* Try to fit 4 chunks into a 4k page */
4848 	rx_size = SZ_1K -
4849 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4850 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4851 }
4852 
4853 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4854  * be set on entry.
4855  */
4856 void bnxt_set_ring_params(struct bnxt *bp)
4857 {
4858 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4859 	u32 agg_factor = 0, agg_ring_size = 0;
4860 
4861 	/* 8 for CRC and VLAN */
4862 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4863 
4864 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4865 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4866 
4867 	ring_size = bp->rx_ring_size;
4868 	bp->rx_agg_ring_size = 0;
4869 	bp->rx_agg_nr_pages = 0;
4870 
4871 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4872 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4873 
4874 	bp->flags &= ~BNXT_FLAG_JUMBO;
4875 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4876 		u32 jumbo_factor;
4877 
4878 		bp->flags |= BNXT_FLAG_JUMBO;
4879 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4880 		if (jumbo_factor > agg_factor)
4881 			agg_factor = jumbo_factor;
4882 	}
4883 	if (agg_factor) {
4884 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4885 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4886 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4887 				    bp->rx_ring_size, ring_size);
4888 			bp->rx_ring_size = ring_size;
4889 		}
4890 		agg_ring_size = ring_size * agg_factor;
4891 
4892 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4893 							RX_DESC_CNT);
4894 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4895 			u32 tmp = agg_ring_size;
4896 
4897 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4898 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4899 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4900 				    tmp, agg_ring_size);
4901 		}
4902 		bp->rx_agg_ring_size = agg_ring_size;
4903 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4904 
4905 		if (BNXT_RX_PAGE_MODE(bp)) {
4906 			rx_space = PAGE_SIZE;
4907 			rx_size = PAGE_SIZE -
4908 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4909 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4910 		} else {
4911 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4912 				       bp->rx_copybreak,
4913 				       bp->dev->cfg_pending->hds_thresh);
4914 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4915 			rx_space = rx_size + NET_SKB_PAD +
4916 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4917 		}
4918 	}
4919 
4920 	bp->rx_buf_use_size = rx_size;
4921 	bp->rx_buf_size = rx_space;
4922 
4923 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4924 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4925 
4926 	ring_size = bp->tx_ring_size;
4927 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4928 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4929 
4930 	max_rx_cmpl = bp->rx_ring_size;
4931 	/* MAX TPA needs to be added because TPA_START completions are
4932 	 * immediately recycled, so the TPA completions are not bound by
4933 	 * the RX ring size.
4934 	 */
4935 	if (bp->flags & BNXT_FLAG_TPA)
4936 		max_rx_cmpl += bp->max_tpa;
4937 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4938 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4939 	bp->cp_ring_size = ring_size;
4940 
4941 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4942 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4943 		bp->cp_nr_pages = MAX_CP_PAGES;
4944 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4945 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4946 			    ring_size, bp->cp_ring_size);
4947 	}
4948 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4949 	bp->cp_ring_mask = bp->cp_bit - 1;
4950 }
4951 
4952 /* Changing allocation mode of RX rings.
4953  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4954  */
4955 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4956 {
4957 	struct net_device *dev = bp->dev;
4958 
4959 	if (page_mode) {
4960 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4961 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4962 
4963 		if (bp->xdp_prog->aux->xdp_has_frags)
4964 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4965 		else
4966 			dev->max_mtu =
4967 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4968 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4969 			bp->flags |= BNXT_FLAG_JUMBO;
4970 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4971 		} else {
4972 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4973 			bp->rx_skb_func = bnxt_rx_page_skb;
4974 		}
4975 		bp->rx_dir = DMA_BIDIRECTIONAL;
4976 	} else {
4977 		dev->max_mtu = bp->max_mtu;
4978 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4979 		bp->rx_dir = DMA_FROM_DEVICE;
4980 		bp->rx_skb_func = bnxt_rx_skb;
4981 	}
4982 }
4983 
4984 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4985 {
4986 	__bnxt_set_rx_skb_mode(bp, page_mode);
4987 
4988 	if (!page_mode) {
4989 		int rx, tx;
4990 
4991 		bnxt_get_max_rings(bp, &rx, &tx, true);
4992 		if (rx > 1) {
4993 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4994 			bp->dev->hw_features |= NETIF_F_LRO;
4995 		}
4996 	}
4997 
4998 	/* Update LRO and GRO_HW availability */
4999 	netdev_update_features(bp->dev);
5000 }
5001 
5002 static void bnxt_free_vnic_attributes(struct bnxt *bp)
5003 {
5004 	int i;
5005 	struct bnxt_vnic_info *vnic;
5006 	struct pci_dev *pdev = bp->pdev;
5007 
5008 	if (!bp->vnic_info)
5009 		return;
5010 
5011 	for (i = 0; i < bp->nr_vnics; i++) {
5012 		vnic = &bp->vnic_info[i];
5013 
5014 		kfree(vnic->fw_grp_ids);
5015 		vnic->fw_grp_ids = NULL;
5016 
5017 		kfree(vnic->uc_list);
5018 		vnic->uc_list = NULL;
5019 
5020 		if (vnic->mc_list) {
5021 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
5022 					  vnic->mc_list, vnic->mc_list_mapping);
5023 			vnic->mc_list = NULL;
5024 		}
5025 
5026 		if (vnic->rss_table) {
5027 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
5028 					  vnic->rss_table,
5029 					  vnic->rss_table_dma_addr);
5030 			vnic->rss_table = NULL;
5031 		}
5032 
5033 		vnic->rss_hash_key = NULL;
5034 		vnic->flags = 0;
5035 	}
5036 }
5037 
5038 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
5039 {
5040 	int i, rc = 0, size;
5041 	struct bnxt_vnic_info *vnic;
5042 	struct pci_dev *pdev = bp->pdev;
5043 	int max_rings;
5044 
5045 	for (i = 0; i < bp->nr_vnics; i++) {
5046 		vnic = &bp->vnic_info[i];
5047 
5048 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
5049 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
5050 
5051 			if (mem_size > 0) {
5052 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
5053 				if (!vnic->uc_list) {
5054 					rc = -ENOMEM;
5055 					goto out;
5056 				}
5057 			}
5058 		}
5059 
5060 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
5061 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
5062 			vnic->mc_list =
5063 				dma_alloc_coherent(&pdev->dev,
5064 						   vnic->mc_list_size,
5065 						   &vnic->mc_list_mapping,
5066 						   GFP_KERNEL);
5067 			if (!vnic->mc_list) {
5068 				rc = -ENOMEM;
5069 				goto out;
5070 			}
5071 		}
5072 
5073 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5074 			goto vnic_skip_grps;
5075 
5076 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5077 			max_rings = bp->rx_nr_rings;
5078 		else
5079 			max_rings = 1;
5080 
5081 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
5082 		if (!vnic->fw_grp_ids) {
5083 			rc = -ENOMEM;
5084 			goto out;
5085 		}
5086 vnic_skip_grps:
5087 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
5088 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
5089 			continue;
5090 
5091 		/* Allocate rss table and hash key */
5092 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5093 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5094 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5095 
5096 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5097 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5098 						     vnic->rss_table_size,
5099 						     &vnic->rss_table_dma_addr,
5100 						     GFP_KERNEL);
5101 		if (!vnic->rss_table) {
5102 			rc = -ENOMEM;
5103 			goto out;
5104 		}
5105 
5106 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5107 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5108 	}
5109 	return 0;
5110 
5111 out:
5112 	return rc;
5113 }
5114 
5115 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5116 {
5117 	struct bnxt_hwrm_wait_token *token;
5118 
5119 	dma_pool_destroy(bp->hwrm_dma_pool);
5120 	bp->hwrm_dma_pool = NULL;
5121 
5122 	rcu_read_lock();
5123 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5124 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5125 	rcu_read_unlock();
5126 }
5127 
5128 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5129 {
5130 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5131 					    BNXT_HWRM_DMA_SIZE,
5132 					    BNXT_HWRM_DMA_ALIGN, 0);
5133 	if (!bp->hwrm_dma_pool)
5134 		return -ENOMEM;
5135 
5136 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5137 
5138 	return 0;
5139 }
5140 
5141 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5142 {
5143 	kfree(stats->hw_masks);
5144 	stats->hw_masks = NULL;
5145 	kfree(stats->sw_stats);
5146 	stats->sw_stats = NULL;
5147 	if (stats->hw_stats) {
5148 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5149 				  stats->hw_stats_map);
5150 		stats->hw_stats = NULL;
5151 	}
5152 }
5153 
5154 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5155 				bool alloc_masks)
5156 {
5157 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5158 					     &stats->hw_stats_map, GFP_KERNEL);
5159 	if (!stats->hw_stats)
5160 		return -ENOMEM;
5161 
5162 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5163 	if (!stats->sw_stats)
5164 		goto stats_mem_err;
5165 
5166 	if (alloc_masks) {
5167 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5168 		if (!stats->hw_masks)
5169 			goto stats_mem_err;
5170 	}
5171 	return 0;
5172 
5173 stats_mem_err:
5174 	bnxt_free_stats_mem(bp, stats);
5175 	return -ENOMEM;
5176 }
5177 
5178 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5179 {
5180 	int i;
5181 
5182 	for (i = 0; i < count; i++)
5183 		mask_arr[i] = mask;
5184 }
5185 
5186 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5187 {
5188 	int i;
5189 
5190 	for (i = 0; i < count; i++)
5191 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5192 }
5193 
5194 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5195 				    struct bnxt_stats_mem *stats)
5196 {
5197 	struct hwrm_func_qstats_ext_output *resp;
5198 	struct hwrm_func_qstats_ext_input *req;
5199 	__le64 *hw_masks;
5200 	int rc;
5201 
5202 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5203 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5204 		return -EOPNOTSUPP;
5205 
5206 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5207 	if (rc)
5208 		return rc;
5209 
5210 	req->fid = cpu_to_le16(0xffff);
5211 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5212 
5213 	resp = hwrm_req_hold(bp, req);
5214 	rc = hwrm_req_send(bp, req);
5215 	if (!rc) {
5216 		hw_masks = &resp->rx_ucast_pkts;
5217 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5218 	}
5219 	hwrm_req_drop(bp, req);
5220 	return rc;
5221 }
5222 
5223 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5224 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5225 
5226 static void bnxt_init_stats(struct bnxt *bp)
5227 {
5228 	struct bnxt_napi *bnapi = bp->bnapi[0];
5229 	struct bnxt_cp_ring_info *cpr;
5230 	struct bnxt_stats_mem *stats;
5231 	__le64 *rx_stats, *tx_stats;
5232 	int rc, rx_count, tx_count;
5233 	u64 *rx_masks, *tx_masks;
5234 	u64 mask;
5235 	u8 flags;
5236 
5237 	cpr = &bnapi->cp_ring;
5238 	stats = &cpr->stats;
5239 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5240 	if (rc) {
5241 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5242 			mask = (1ULL << 48) - 1;
5243 		else
5244 			mask = -1ULL;
5245 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5246 	}
5247 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5248 		stats = &bp->port_stats;
5249 		rx_stats = stats->hw_stats;
5250 		rx_masks = stats->hw_masks;
5251 		rx_count = sizeof(struct rx_port_stats) / 8;
5252 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5253 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5254 		tx_count = sizeof(struct tx_port_stats) / 8;
5255 
5256 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5257 		rc = bnxt_hwrm_port_qstats(bp, flags);
5258 		if (rc) {
5259 			mask = (1ULL << 40) - 1;
5260 
5261 			bnxt_fill_masks(rx_masks, mask, rx_count);
5262 			bnxt_fill_masks(tx_masks, mask, tx_count);
5263 		} else {
5264 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5265 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5266 			bnxt_hwrm_port_qstats(bp, 0);
5267 		}
5268 	}
5269 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5270 		stats = &bp->rx_port_stats_ext;
5271 		rx_stats = stats->hw_stats;
5272 		rx_masks = stats->hw_masks;
5273 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5274 		stats = &bp->tx_port_stats_ext;
5275 		tx_stats = stats->hw_stats;
5276 		tx_masks = stats->hw_masks;
5277 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5278 
5279 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5280 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5281 		if (rc) {
5282 			mask = (1ULL << 40) - 1;
5283 
5284 			bnxt_fill_masks(rx_masks, mask, rx_count);
5285 			if (tx_stats)
5286 				bnxt_fill_masks(tx_masks, mask, tx_count);
5287 		} else {
5288 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5289 			if (tx_stats)
5290 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5291 						   tx_count);
5292 			bnxt_hwrm_port_qstats_ext(bp, 0);
5293 		}
5294 	}
5295 }
5296 
5297 static void bnxt_free_port_stats(struct bnxt *bp)
5298 {
5299 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5300 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5301 
5302 	bnxt_free_stats_mem(bp, &bp->port_stats);
5303 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5304 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5305 }
5306 
5307 static void bnxt_free_ring_stats(struct bnxt *bp)
5308 {
5309 	int i;
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 = &bnapi->cp_ring;
5317 
5318 		bnxt_free_stats_mem(bp, &cpr->stats);
5319 
5320 		kfree(cpr->sw_stats);
5321 		cpr->sw_stats = NULL;
5322 	}
5323 }
5324 
5325 static int bnxt_alloc_stats(struct bnxt *bp)
5326 {
5327 	u32 size, i;
5328 	int rc;
5329 
5330 	size = bp->hw_ring_stats_size;
5331 
5332 	for (i = 0; i < bp->cp_nr_rings; i++) {
5333 		struct bnxt_napi *bnapi = bp->bnapi[i];
5334 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5335 
5336 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5337 		if (!cpr->sw_stats)
5338 			return -ENOMEM;
5339 
5340 		cpr->stats.len = size;
5341 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5342 		if (rc)
5343 			return rc;
5344 
5345 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5346 	}
5347 
5348 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5349 		return 0;
5350 
5351 	if (bp->port_stats.hw_stats)
5352 		goto alloc_ext_stats;
5353 
5354 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5355 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5356 	if (rc)
5357 		return rc;
5358 
5359 	bp->flags |= BNXT_FLAG_PORT_STATS;
5360 
5361 alloc_ext_stats:
5362 	/* Display extended statistics only if FW supports it */
5363 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5364 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5365 			return 0;
5366 
5367 	if (bp->rx_port_stats_ext.hw_stats)
5368 		goto alloc_tx_ext_stats;
5369 
5370 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5371 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5372 	/* Extended stats are optional */
5373 	if (rc)
5374 		return 0;
5375 
5376 alloc_tx_ext_stats:
5377 	if (bp->tx_port_stats_ext.hw_stats)
5378 		return 0;
5379 
5380 	if (bp->hwrm_spec_code >= 0x10902 ||
5381 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5382 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5383 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5384 		/* Extended stats are optional */
5385 		if (rc)
5386 			return 0;
5387 	}
5388 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5389 	return 0;
5390 }
5391 
5392 static void bnxt_clear_ring_indices(struct bnxt *bp)
5393 {
5394 	int i, j;
5395 
5396 	if (!bp->bnapi)
5397 		return;
5398 
5399 	for (i = 0; i < bp->cp_nr_rings; i++) {
5400 		struct bnxt_napi *bnapi = bp->bnapi[i];
5401 		struct bnxt_cp_ring_info *cpr;
5402 		struct bnxt_rx_ring_info *rxr;
5403 		struct bnxt_tx_ring_info *txr;
5404 
5405 		if (!bnapi)
5406 			continue;
5407 
5408 		cpr = &bnapi->cp_ring;
5409 		cpr->cp_raw_cons = 0;
5410 
5411 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5412 			txr->tx_prod = 0;
5413 			txr->tx_cons = 0;
5414 			txr->tx_hw_cons = 0;
5415 		}
5416 
5417 		rxr = bnapi->rx_ring;
5418 		if (rxr) {
5419 			rxr->rx_prod = 0;
5420 			rxr->rx_agg_prod = 0;
5421 			rxr->rx_sw_agg_prod = 0;
5422 			rxr->rx_next_cons = 0;
5423 		}
5424 		bnapi->events = 0;
5425 	}
5426 }
5427 
5428 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5429 {
5430 	u8 type = fltr->type, flags = fltr->flags;
5431 
5432 	INIT_LIST_HEAD(&fltr->list);
5433 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5434 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5435 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5436 }
5437 
5438 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5439 {
5440 	if (!list_empty(&fltr->list))
5441 		list_del_init(&fltr->list);
5442 }
5443 
5444 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5445 {
5446 	struct bnxt_filter_base *usr_fltr, *tmp;
5447 
5448 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5449 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5450 			continue;
5451 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5452 	}
5453 }
5454 
5455 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5456 {
5457 	hlist_del(&fltr->hash);
5458 	bnxt_del_one_usr_fltr(bp, fltr);
5459 	if (fltr->flags) {
5460 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5461 		bp->ntp_fltr_count--;
5462 	}
5463 	kfree(fltr);
5464 }
5465 
5466 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5467 {
5468 	int i;
5469 
5470 	netdev_assert_locked_or_invisible(bp->dev);
5471 
5472 	/* Under netdev instance lock and all our NAPIs have been disabled.
5473 	 * It's safe to delete the hash table.
5474 	 */
5475 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5476 		struct hlist_head *head;
5477 		struct hlist_node *tmp;
5478 		struct bnxt_ntuple_filter *fltr;
5479 
5480 		head = &bp->ntp_fltr_hash_tbl[i];
5481 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5482 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5483 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5484 				     !list_empty(&fltr->base.list)))
5485 				continue;
5486 			bnxt_del_fltr(bp, &fltr->base);
5487 		}
5488 	}
5489 	if (!all)
5490 		return;
5491 
5492 	bitmap_free(bp->ntp_fltr_bmap);
5493 	bp->ntp_fltr_bmap = NULL;
5494 	bp->ntp_fltr_count = 0;
5495 }
5496 
5497 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5498 {
5499 	int i, rc = 0;
5500 
5501 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5502 		return 0;
5503 
5504 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5505 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5506 
5507 	bp->ntp_fltr_count = 0;
5508 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5509 
5510 	if (!bp->ntp_fltr_bmap)
5511 		rc = -ENOMEM;
5512 
5513 	return rc;
5514 }
5515 
5516 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5517 {
5518 	int i;
5519 
5520 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5521 		struct hlist_head *head;
5522 		struct hlist_node *tmp;
5523 		struct bnxt_l2_filter *fltr;
5524 
5525 		head = &bp->l2_fltr_hash_tbl[i];
5526 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5527 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5528 				     !list_empty(&fltr->base.list)))
5529 				continue;
5530 			bnxt_del_fltr(bp, &fltr->base);
5531 		}
5532 	}
5533 }
5534 
5535 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5536 {
5537 	int i;
5538 
5539 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5540 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5541 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5542 }
5543 
5544 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5545 {
5546 	bnxt_free_vnic_attributes(bp);
5547 	bnxt_free_tx_rings(bp);
5548 	bnxt_free_rx_rings(bp);
5549 	bnxt_free_cp_rings(bp);
5550 	bnxt_free_all_cp_arrays(bp);
5551 	bnxt_free_ntp_fltrs(bp, false);
5552 	bnxt_free_l2_filters(bp, false);
5553 	if (irq_re_init) {
5554 		bnxt_free_ring_stats(bp);
5555 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5556 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5557 			bnxt_free_port_stats(bp);
5558 		bnxt_free_ring_grps(bp);
5559 		bnxt_free_vnics(bp);
5560 		kfree(bp->tx_ring_map);
5561 		bp->tx_ring_map = NULL;
5562 		kfree(bp->tx_ring);
5563 		bp->tx_ring = NULL;
5564 		kfree(bp->rx_ring);
5565 		bp->rx_ring = NULL;
5566 		kfree(bp->bnapi);
5567 		bp->bnapi = NULL;
5568 	} else {
5569 		bnxt_clear_ring_indices(bp);
5570 	}
5571 }
5572 
5573 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5574 {
5575 	int i, j, rc, size, arr_size;
5576 	void *bnapi;
5577 
5578 	if (irq_re_init) {
5579 		/* Allocate bnapi mem pointer array and mem block for
5580 		 * all queues
5581 		 */
5582 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5583 				bp->cp_nr_rings);
5584 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5585 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5586 		if (!bnapi)
5587 			return -ENOMEM;
5588 
5589 		bp->bnapi = bnapi;
5590 		bnapi += arr_size;
5591 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5592 			bp->bnapi[i] = bnapi;
5593 			bp->bnapi[i]->index = i;
5594 			bp->bnapi[i]->bp = bp;
5595 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5596 				struct bnxt_cp_ring_info *cpr =
5597 					&bp->bnapi[i]->cp_ring;
5598 
5599 				cpr->cp_ring_struct.ring_mem.flags =
5600 					BNXT_RMEM_RING_PTE_FLAG;
5601 			}
5602 		}
5603 
5604 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5605 					   bp->rx_nr_rings);
5606 		if (!bp->rx_ring)
5607 			return -ENOMEM;
5608 
5609 		for (i = 0; i < bp->rx_nr_rings; i++) {
5610 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5611 
5612 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5613 				rxr->rx_ring_struct.ring_mem.flags =
5614 					BNXT_RMEM_RING_PTE_FLAG;
5615 				rxr->rx_agg_ring_struct.ring_mem.flags =
5616 					BNXT_RMEM_RING_PTE_FLAG;
5617 			} else {
5618 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5619 			}
5620 			rxr->bnapi = bp->bnapi[i];
5621 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5622 		}
5623 
5624 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5625 					   bp->tx_nr_rings);
5626 		if (!bp->tx_ring)
5627 			return -ENOMEM;
5628 
5629 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5630 					  GFP_KERNEL);
5631 
5632 		if (!bp->tx_ring_map)
5633 			return -ENOMEM;
5634 
5635 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5636 			j = 0;
5637 		else
5638 			j = bp->rx_nr_rings;
5639 
5640 		for (i = 0; i < bp->tx_nr_rings; i++) {
5641 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5642 			struct bnxt_napi *bnapi2;
5643 
5644 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5645 				txr->tx_ring_struct.ring_mem.flags =
5646 					BNXT_RMEM_RING_PTE_FLAG;
5647 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5648 			if (i >= bp->tx_nr_rings_xdp) {
5649 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5650 
5651 				bnapi2 = bp->bnapi[k];
5652 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5653 				txr->tx_napi_idx =
5654 					BNXT_RING_TO_TC(bp, txr->txq_index);
5655 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5656 				bnapi2->tx_int = bnxt_tx_int;
5657 			} else {
5658 				bnapi2 = bp->bnapi[j];
5659 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5660 				bnapi2->tx_ring[0] = txr;
5661 				bnapi2->tx_int = bnxt_tx_int_xdp;
5662 				j++;
5663 			}
5664 			txr->bnapi = bnapi2;
5665 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5666 				txr->tx_cpr = &bnapi2->cp_ring;
5667 		}
5668 
5669 		rc = bnxt_alloc_stats(bp);
5670 		if (rc)
5671 			goto alloc_mem_err;
5672 		bnxt_init_stats(bp);
5673 
5674 		rc = bnxt_alloc_ntp_fltrs(bp);
5675 		if (rc)
5676 			goto alloc_mem_err;
5677 
5678 		rc = bnxt_alloc_vnics(bp);
5679 		if (rc)
5680 			goto alloc_mem_err;
5681 	}
5682 
5683 	rc = bnxt_alloc_all_cp_arrays(bp);
5684 	if (rc)
5685 		goto alloc_mem_err;
5686 
5687 	bnxt_init_ring_struct(bp);
5688 
5689 	rc = bnxt_alloc_rx_rings(bp);
5690 	if (rc)
5691 		goto alloc_mem_err;
5692 
5693 	rc = bnxt_alloc_tx_rings(bp);
5694 	if (rc)
5695 		goto alloc_mem_err;
5696 
5697 	rc = bnxt_alloc_cp_rings(bp);
5698 	if (rc)
5699 		goto alloc_mem_err;
5700 
5701 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5702 						  BNXT_VNIC_MCAST_FLAG |
5703 						  BNXT_VNIC_UCAST_FLAG;
5704 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5705 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5706 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5707 
5708 	rc = bnxt_alloc_vnic_attributes(bp);
5709 	if (rc)
5710 		goto alloc_mem_err;
5711 	return 0;
5712 
5713 alloc_mem_err:
5714 	bnxt_free_mem(bp, true);
5715 	return rc;
5716 }
5717 
5718 static void bnxt_disable_int(struct bnxt *bp)
5719 {
5720 	int i;
5721 
5722 	if (!bp->bnapi)
5723 		return;
5724 
5725 	for (i = 0; i < bp->cp_nr_rings; i++) {
5726 		struct bnxt_napi *bnapi = bp->bnapi[i];
5727 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5728 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5729 
5730 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5731 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5732 	}
5733 }
5734 
5735 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5736 {
5737 	struct bnxt_napi *bnapi = bp->bnapi[n];
5738 	struct bnxt_cp_ring_info *cpr;
5739 
5740 	cpr = &bnapi->cp_ring;
5741 	return cpr->cp_ring_struct.map_idx;
5742 }
5743 
5744 static void bnxt_disable_int_sync(struct bnxt *bp)
5745 {
5746 	int i;
5747 
5748 	if (!bp->irq_tbl)
5749 		return;
5750 
5751 	atomic_inc(&bp->intr_sem);
5752 
5753 	bnxt_disable_int(bp);
5754 	for (i = 0; i < bp->cp_nr_rings; i++) {
5755 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5756 
5757 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5758 	}
5759 }
5760 
5761 static void bnxt_enable_int(struct bnxt *bp)
5762 {
5763 	int i;
5764 
5765 	atomic_set(&bp->intr_sem, 0);
5766 	for (i = 0; i < bp->cp_nr_rings; i++) {
5767 		struct bnxt_napi *bnapi = bp->bnapi[i];
5768 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5769 
5770 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5771 	}
5772 }
5773 
5774 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5775 			    bool async_only)
5776 {
5777 	DECLARE_BITMAP(async_events_bmap, 256);
5778 	u32 *events = (u32 *)async_events_bmap;
5779 	struct hwrm_func_drv_rgtr_output *resp;
5780 	struct hwrm_func_drv_rgtr_input *req;
5781 	u32 flags;
5782 	int rc, i;
5783 
5784 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5785 	if (rc)
5786 		return rc;
5787 
5788 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5789 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5790 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5791 
5792 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5793 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5794 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5795 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5796 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5797 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5798 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5799 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5800 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5801 	req->flags = cpu_to_le32(flags);
5802 	req->ver_maj_8b = DRV_VER_MAJ;
5803 	req->ver_min_8b = DRV_VER_MIN;
5804 	req->ver_upd_8b = DRV_VER_UPD;
5805 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5806 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5807 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5808 
5809 	if (BNXT_PF(bp)) {
5810 		u32 data[8];
5811 		int i;
5812 
5813 		memset(data, 0, sizeof(data));
5814 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5815 			u16 cmd = bnxt_vf_req_snif[i];
5816 			unsigned int bit, idx;
5817 
5818 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5819 			    cmd == HWRM_PORT_PHY_QCFG)
5820 				continue;
5821 
5822 			idx = cmd / 32;
5823 			bit = cmd % 32;
5824 			data[idx] |= 1 << bit;
5825 		}
5826 
5827 		for (i = 0; i < 8; i++)
5828 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5829 
5830 		req->enables |=
5831 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5832 	}
5833 
5834 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5835 		req->flags |= cpu_to_le32(
5836 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5837 
5838 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5839 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5840 		u16 event_id = bnxt_async_events_arr[i];
5841 
5842 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5843 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5844 			continue;
5845 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5846 		    !bp->ptp_cfg)
5847 			continue;
5848 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5849 	}
5850 	if (bmap && bmap_size) {
5851 		for (i = 0; i < bmap_size; i++) {
5852 			if (test_bit(i, bmap))
5853 				__set_bit(i, async_events_bmap);
5854 		}
5855 	}
5856 	for (i = 0; i < 8; i++)
5857 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5858 
5859 	if (async_only)
5860 		req->enables =
5861 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5862 
5863 	resp = hwrm_req_hold(bp, req);
5864 	rc = hwrm_req_send(bp, req);
5865 	if (!rc) {
5866 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5867 		if (resp->flags &
5868 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5869 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5870 	}
5871 	hwrm_req_drop(bp, req);
5872 	return rc;
5873 }
5874 
5875 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5876 {
5877 	struct hwrm_func_drv_unrgtr_input *req;
5878 	int rc;
5879 
5880 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5881 		return 0;
5882 
5883 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5884 	if (rc)
5885 		return rc;
5886 	return hwrm_req_send(bp, req);
5887 }
5888 
5889 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5890 
5891 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5892 {
5893 	struct hwrm_tunnel_dst_port_free_input *req;
5894 	int rc;
5895 
5896 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5897 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5898 		return 0;
5899 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5900 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5901 		return 0;
5902 
5903 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5904 	if (rc)
5905 		return rc;
5906 
5907 	req->tunnel_type = tunnel_type;
5908 
5909 	switch (tunnel_type) {
5910 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5911 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5912 		bp->vxlan_port = 0;
5913 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5914 		break;
5915 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5916 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5917 		bp->nge_port = 0;
5918 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5919 		break;
5920 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5921 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5922 		bp->vxlan_gpe_port = 0;
5923 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5924 		break;
5925 	default:
5926 		break;
5927 	}
5928 
5929 	rc = hwrm_req_send(bp, req);
5930 	if (rc)
5931 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5932 			   rc);
5933 	if (bp->flags & BNXT_FLAG_TPA)
5934 		bnxt_set_tpa(bp, true);
5935 	return rc;
5936 }
5937 
5938 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5939 					   u8 tunnel_type)
5940 {
5941 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5942 	struct hwrm_tunnel_dst_port_alloc_input *req;
5943 	int rc;
5944 
5945 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5946 	if (rc)
5947 		return rc;
5948 
5949 	req->tunnel_type = tunnel_type;
5950 	req->tunnel_dst_port_val = port;
5951 
5952 	resp = hwrm_req_hold(bp, req);
5953 	rc = hwrm_req_send(bp, req);
5954 	if (rc) {
5955 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5956 			   rc);
5957 		goto err_out;
5958 	}
5959 
5960 	switch (tunnel_type) {
5961 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5962 		bp->vxlan_port = port;
5963 		bp->vxlan_fw_dst_port_id =
5964 			le16_to_cpu(resp->tunnel_dst_port_id);
5965 		break;
5966 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5967 		bp->nge_port = port;
5968 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5969 		break;
5970 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5971 		bp->vxlan_gpe_port = port;
5972 		bp->vxlan_gpe_fw_dst_port_id =
5973 			le16_to_cpu(resp->tunnel_dst_port_id);
5974 		break;
5975 	default:
5976 		break;
5977 	}
5978 	if (bp->flags & BNXT_FLAG_TPA)
5979 		bnxt_set_tpa(bp, true);
5980 
5981 err_out:
5982 	hwrm_req_drop(bp, req);
5983 	return rc;
5984 }
5985 
5986 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5987 {
5988 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5989 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5990 	int rc;
5991 
5992 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5993 	if (rc)
5994 		return rc;
5995 
5996 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5997 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5998 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5999 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
6000 	}
6001 	req->mask = cpu_to_le32(vnic->rx_mask);
6002 	return hwrm_req_send_silent(bp, req);
6003 }
6004 
6005 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6006 {
6007 	if (!atomic_dec_and_test(&fltr->refcnt))
6008 		return;
6009 	spin_lock_bh(&bp->ntp_fltr_lock);
6010 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
6011 		spin_unlock_bh(&bp->ntp_fltr_lock);
6012 		return;
6013 	}
6014 	hlist_del_rcu(&fltr->base.hash);
6015 	bnxt_del_one_usr_fltr(bp, &fltr->base);
6016 	if (fltr->base.flags) {
6017 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
6018 		bp->ntp_fltr_count--;
6019 	}
6020 	spin_unlock_bh(&bp->ntp_fltr_lock);
6021 	kfree_rcu(fltr, base.rcu);
6022 }
6023 
6024 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
6025 						      struct bnxt_l2_key *key,
6026 						      u32 idx)
6027 {
6028 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
6029 	struct bnxt_l2_filter *fltr;
6030 
6031 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
6032 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
6033 
6034 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
6035 		    l2_key->vlan == key->vlan)
6036 			return fltr;
6037 	}
6038 	return NULL;
6039 }
6040 
6041 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
6042 						    struct bnxt_l2_key *key,
6043 						    u32 idx)
6044 {
6045 	struct bnxt_l2_filter *fltr = NULL;
6046 
6047 	rcu_read_lock();
6048 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6049 	if (fltr)
6050 		atomic_inc(&fltr->refcnt);
6051 	rcu_read_unlock();
6052 	return fltr;
6053 }
6054 
6055 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
6056 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6057 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
6058 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6059 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
6060 
6061 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
6062 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6063 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
6064 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6065 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
6066 
6067 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
6068 {
6069 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6070 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
6071 			return sizeof(fkeys->addrs.v4addrs) +
6072 			       sizeof(fkeys->ports);
6073 
6074 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
6075 			return sizeof(fkeys->addrs.v4addrs);
6076 	}
6077 
6078 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
6079 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
6080 			return sizeof(fkeys->addrs.v6addrs) +
6081 			       sizeof(fkeys->ports);
6082 
6083 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6084 			return sizeof(fkeys->addrs.v6addrs);
6085 	}
6086 
6087 	return 0;
6088 }
6089 
6090 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6091 			 const unsigned char *key)
6092 {
6093 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6094 	struct bnxt_ipv4_tuple tuple4;
6095 	struct bnxt_ipv6_tuple tuple6;
6096 	int i, j, len = 0;
6097 	u8 *four_tuple;
6098 
6099 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6100 	if (!len)
6101 		return 0;
6102 
6103 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6104 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6105 		tuple4.ports = fkeys->ports;
6106 		four_tuple = (unsigned char *)&tuple4;
6107 	} else {
6108 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6109 		tuple6.ports = fkeys->ports;
6110 		four_tuple = (unsigned char *)&tuple6;
6111 	}
6112 
6113 	for (i = 0, j = 8; i < len; i++, j++) {
6114 		u8 byte = four_tuple[i];
6115 		int bit;
6116 
6117 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6118 			if (byte & 0x80)
6119 				hash ^= prefix;
6120 		}
6121 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6122 	}
6123 
6124 	/* The valid part of the hash is in the upper 32 bits. */
6125 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6126 }
6127 
6128 #ifdef CONFIG_RFS_ACCEL
6129 static struct bnxt_l2_filter *
6130 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6131 {
6132 	struct bnxt_l2_filter *fltr;
6133 	u32 idx;
6134 
6135 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6136 	      BNXT_L2_FLTR_HASH_MASK;
6137 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6138 	return fltr;
6139 }
6140 #endif
6141 
6142 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6143 			       struct bnxt_l2_key *key, u32 idx)
6144 {
6145 	struct hlist_head *head;
6146 
6147 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6148 	fltr->l2_key.vlan = key->vlan;
6149 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6150 	if (fltr->base.flags) {
6151 		int bit_id;
6152 
6153 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6154 						 bp->max_fltr, 0);
6155 		if (bit_id < 0)
6156 			return -ENOMEM;
6157 		fltr->base.sw_id = (u16)bit_id;
6158 		bp->ntp_fltr_count++;
6159 	}
6160 	head = &bp->l2_fltr_hash_tbl[idx];
6161 	hlist_add_head_rcu(&fltr->base.hash, head);
6162 	bnxt_insert_usr_fltr(bp, &fltr->base);
6163 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6164 	atomic_set(&fltr->refcnt, 1);
6165 	return 0;
6166 }
6167 
6168 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6169 						   struct bnxt_l2_key *key,
6170 						   gfp_t gfp)
6171 {
6172 	struct bnxt_l2_filter *fltr;
6173 	u32 idx;
6174 	int rc;
6175 
6176 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6177 	      BNXT_L2_FLTR_HASH_MASK;
6178 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6179 	if (fltr)
6180 		return fltr;
6181 
6182 	fltr = kzalloc_obj(*fltr, gfp);
6183 	if (!fltr)
6184 		return ERR_PTR(-ENOMEM);
6185 	spin_lock_bh(&bp->ntp_fltr_lock);
6186 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6187 	spin_unlock_bh(&bp->ntp_fltr_lock);
6188 	if (rc) {
6189 		bnxt_del_l2_filter(bp, fltr);
6190 		fltr = ERR_PTR(rc);
6191 	}
6192 	return fltr;
6193 }
6194 
6195 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6196 						struct bnxt_l2_key *key,
6197 						u16 flags)
6198 {
6199 	struct bnxt_l2_filter *fltr;
6200 	u32 idx;
6201 	int rc;
6202 
6203 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6204 	      BNXT_L2_FLTR_HASH_MASK;
6205 	spin_lock_bh(&bp->ntp_fltr_lock);
6206 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6207 	if (fltr) {
6208 		fltr = ERR_PTR(-EEXIST);
6209 		goto l2_filter_exit;
6210 	}
6211 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6212 	if (!fltr) {
6213 		fltr = ERR_PTR(-ENOMEM);
6214 		goto l2_filter_exit;
6215 	}
6216 	fltr->base.flags = flags;
6217 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6218 	if (rc) {
6219 		spin_unlock_bh(&bp->ntp_fltr_lock);
6220 		bnxt_del_l2_filter(bp, fltr);
6221 		return ERR_PTR(rc);
6222 	}
6223 
6224 l2_filter_exit:
6225 	spin_unlock_bh(&bp->ntp_fltr_lock);
6226 	return fltr;
6227 }
6228 
6229 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6230 {
6231 #ifdef CONFIG_BNXT_SRIOV
6232 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6233 
6234 	return vf->fw_fid;
6235 #else
6236 	return INVALID_HW_RING_ID;
6237 #endif
6238 }
6239 
6240 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6241 {
6242 	struct hwrm_cfa_l2_filter_free_input *req;
6243 	u16 target_id = 0xffff;
6244 	int rc;
6245 
6246 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6247 		struct bnxt_pf_info *pf = &bp->pf;
6248 
6249 		if (fltr->base.vf_idx >= pf->active_vfs)
6250 			return -EINVAL;
6251 
6252 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6253 		if (target_id == INVALID_HW_RING_ID)
6254 			return -EINVAL;
6255 	}
6256 
6257 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6258 	if (rc)
6259 		return rc;
6260 
6261 	req->target_id = cpu_to_le16(target_id);
6262 	req->l2_filter_id = fltr->base.filter_id;
6263 	return hwrm_req_send(bp, req);
6264 }
6265 
6266 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6267 {
6268 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6269 	struct hwrm_cfa_l2_filter_alloc_input *req;
6270 	u16 target_id = 0xffff;
6271 	int rc;
6272 
6273 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6274 		struct bnxt_pf_info *pf = &bp->pf;
6275 
6276 		if (fltr->base.vf_idx >= pf->active_vfs)
6277 			return -EINVAL;
6278 
6279 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6280 	}
6281 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6282 	if (rc)
6283 		return rc;
6284 
6285 	req->target_id = cpu_to_le16(target_id);
6286 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6287 
6288 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6289 		req->flags |=
6290 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6291 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6292 	req->enables =
6293 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6294 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6295 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6296 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6297 	eth_broadcast_addr(req->l2_addr_mask);
6298 
6299 	if (fltr->l2_key.vlan) {
6300 		req->enables |=
6301 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6302 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6303 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6304 		req->num_vlans = 1;
6305 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6306 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6307 	}
6308 
6309 	resp = hwrm_req_hold(bp, req);
6310 	rc = hwrm_req_send(bp, req);
6311 	if (!rc) {
6312 		fltr->base.filter_id = resp->l2_filter_id;
6313 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6314 	}
6315 	hwrm_req_drop(bp, req);
6316 	return rc;
6317 }
6318 
6319 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6320 				     struct bnxt_ntuple_filter *fltr)
6321 {
6322 	struct hwrm_cfa_ntuple_filter_free_input *req;
6323 	int rc;
6324 
6325 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6326 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6327 		return 0;
6328 
6329 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6330 	if (rc)
6331 		return rc;
6332 
6333 	req->ntuple_filter_id = fltr->base.filter_id;
6334 	return hwrm_req_send(bp, req);
6335 }
6336 
6337 #define BNXT_NTP_FLTR_FLAGS					\
6338 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6339 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6340 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6341 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6342 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6343 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6344 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6345 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6346 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6347 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6348 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6349 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6350 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6351 
6352 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6353 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6354 
6355 void bnxt_fill_ipv6_mask(__be32 mask[4])
6356 {
6357 	int i;
6358 
6359 	for (i = 0; i < 4; i++)
6360 		mask[i] = cpu_to_be32(~0);
6361 }
6362 
6363 static void
6364 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6365 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6366 			  struct bnxt_ntuple_filter *fltr)
6367 {
6368 	u16 rxq = fltr->base.rxq;
6369 
6370 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6371 		struct ethtool_rxfh_context *ctx;
6372 		struct bnxt_rss_ctx *rss_ctx;
6373 		struct bnxt_vnic_info *vnic;
6374 
6375 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6376 			      fltr->base.fw_vnic_id);
6377 		if (ctx) {
6378 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6379 			vnic = &rss_ctx->vnic;
6380 
6381 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6382 		}
6383 		return;
6384 	}
6385 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6386 		struct bnxt_vnic_info *vnic;
6387 		u32 enables;
6388 
6389 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6390 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6391 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6392 		req->enables |= cpu_to_le32(enables);
6393 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6394 	} else {
6395 		u32 flags;
6396 
6397 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6398 		req->flags |= cpu_to_le32(flags);
6399 		req->dst_id = cpu_to_le16(rxq);
6400 	}
6401 }
6402 
6403 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6404 				      struct bnxt_ntuple_filter *fltr)
6405 {
6406 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6407 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6408 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6409 	struct flow_keys *keys = &fltr->fkeys;
6410 	struct bnxt_l2_filter *l2_fltr;
6411 	struct bnxt_vnic_info *vnic;
6412 	int rc;
6413 
6414 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6415 	if (rc)
6416 		return rc;
6417 
6418 	l2_fltr = fltr->l2_fltr;
6419 	req->l2_filter_id = l2_fltr->base.filter_id;
6420 
6421 	if (fltr->base.flags & BNXT_ACT_DROP) {
6422 		req->flags =
6423 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6424 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6425 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6426 	} else {
6427 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6428 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6429 	}
6430 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6431 
6432 	req->ethertype = htons(ETH_P_IP);
6433 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6434 	req->ip_protocol = keys->basic.ip_proto;
6435 
6436 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6437 		req->ethertype = htons(ETH_P_IPV6);
6438 		req->ip_addr_type =
6439 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6440 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6441 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6442 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6443 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6444 	} else {
6445 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6446 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6447 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6448 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6449 	}
6450 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6451 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6452 		req->tunnel_type =
6453 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6454 	}
6455 
6456 	req->src_port = keys->ports.src;
6457 	req->src_port_mask = masks->ports.src;
6458 	req->dst_port = keys->ports.dst;
6459 	req->dst_port_mask = masks->ports.dst;
6460 
6461 	resp = hwrm_req_hold(bp, req);
6462 	rc = hwrm_req_send(bp, req);
6463 	if (!rc)
6464 		fltr->base.filter_id = resp->ntuple_filter_id;
6465 	hwrm_req_drop(bp, req);
6466 	return rc;
6467 }
6468 
6469 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6470 				     const u8 *mac_addr)
6471 {
6472 	struct bnxt_l2_filter *fltr;
6473 	struct bnxt_l2_key key;
6474 	int rc;
6475 
6476 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6477 	key.vlan = 0;
6478 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6479 	if (IS_ERR(fltr))
6480 		return PTR_ERR(fltr);
6481 
6482 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6483 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6484 	if (rc)
6485 		bnxt_del_l2_filter(bp, fltr);
6486 	else
6487 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6488 	return rc;
6489 }
6490 
6491 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6492 {
6493 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6494 
6495 	/* Any associated ntuple filters will also be cleared by firmware. */
6496 	for (i = 0; i < num_of_vnics; i++) {
6497 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6498 
6499 		for (j = 0; j < vnic->uc_filter_count; j++) {
6500 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6501 
6502 			bnxt_hwrm_l2_filter_free(bp, fltr);
6503 			bnxt_del_l2_filter(bp, fltr);
6504 		}
6505 		vnic->uc_filter_count = 0;
6506 	}
6507 }
6508 
6509 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6510 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6511 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6512 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6513 
6514 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6515 					   struct hwrm_vnic_tpa_cfg_input *req)
6516 {
6517 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6518 
6519 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6520 		return;
6521 
6522 	if (bp->vxlan_port)
6523 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6524 	if (bp->vxlan_gpe_port)
6525 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6526 	if (bp->nge_port)
6527 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6528 
6529 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6530 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6531 }
6532 
6533 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6534 			   u32 tpa_flags)
6535 {
6536 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6537 	struct hwrm_vnic_tpa_cfg_input *req;
6538 	int rc;
6539 
6540 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6541 		return 0;
6542 
6543 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6544 	if (rc)
6545 		return rc;
6546 
6547 	if (tpa_flags) {
6548 		u16 mss = bp->dev->mtu - 40;
6549 		u32 nsegs, n, segs = 0, flags;
6550 
6551 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6552 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6553 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6554 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6555 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6556 		if (tpa_flags & BNXT_FLAG_GRO)
6557 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6558 
6559 		req->flags = cpu_to_le32(flags);
6560 
6561 		req->enables =
6562 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6563 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6564 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6565 
6566 		/* Number of segs are log2 units, and first packet is not
6567 		 * included as part of this units.
6568 		 */
6569 		if (mss <= BNXT_RX_PAGE_SIZE) {
6570 			n = BNXT_RX_PAGE_SIZE / mss;
6571 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6572 		} else {
6573 			n = mss / BNXT_RX_PAGE_SIZE;
6574 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6575 				n++;
6576 			nsegs = (MAX_SKB_FRAGS - n) / n;
6577 		}
6578 
6579 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6580 			segs = MAX_TPA_SEGS_P5;
6581 			max_aggs = bp->max_tpa;
6582 		} else {
6583 			segs = ilog2(nsegs);
6584 		}
6585 		req->max_agg_segs = cpu_to_le16(segs);
6586 		req->max_aggs = cpu_to_le16(max_aggs);
6587 
6588 		req->min_agg_len = cpu_to_le32(512);
6589 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6590 	}
6591 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6592 
6593 	return hwrm_req_send(bp, req);
6594 }
6595 
6596 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6597 {
6598 	struct bnxt_ring_grp_info *grp_info;
6599 
6600 	grp_info = &bp->grp_info[ring->grp_idx];
6601 	return grp_info->cp_fw_ring_id;
6602 }
6603 
6604 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6605 {
6606 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6607 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6608 	else
6609 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6610 }
6611 
6612 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6613 {
6614 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6615 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6616 	else
6617 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6618 }
6619 
6620 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6621 {
6622 	int entries;
6623 
6624 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6625 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6626 	else
6627 		entries = HW_HASH_INDEX_SIZE;
6628 
6629 	bp->rss_indir_tbl_entries = entries;
6630 	bp->rss_indir_tbl =
6631 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6632 	if (!bp->rss_indir_tbl)
6633 		return -ENOMEM;
6634 
6635 	return 0;
6636 }
6637 
6638 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6639 				 struct ethtool_rxfh_context *rss_ctx)
6640 {
6641 	u16 max_rings, max_entries, pad, i;
6642 	u32 *rss_indir_tbl;
6643 
6644 	if (!bp->rx_nr_rings)
6645 		return;
6646 
6647 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6648 		max_rings = bp->rx_nr_rings - 1;
6649 	else
6650 		max_rings = bp->rx_nr_rings;
6651 
6652 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6653 	if (rss_ctx)
6654 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6655 	else
6656 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6657 
6658 	for (i = 0; i < max_entries; i++)
6659 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6660 
6661 	pad = bp->rss_indir_tbl_entries - max_entries;
6662 	if (pad)
6663 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6664 }
6665 
6666 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6667 {
6668 	u32 i, tbl_size, max_ring = 0;
6669 
6670 	if (!bp->rss_indir_tbl)
6671 		return 0;
6672 
6673 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6674 	for (i = 0; i < tbl_size; i++)
6675 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6676 	return max_ring;
6677 }
6678 
6679 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6680 {
6681 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6682 		if (!rx_rings)
6683 			return 0;
6684 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6685 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6686 
6687 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6688 					       BNXT_RSS_TABLE_ENTRIES_P5);
6689 	}
6690 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6691 		return 2;
6692 	return 1;
6693 }
6694 
6695 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6696 {
6697 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6698 	u16 i, j;
6699 
6700 	/* Fill the RSS indirection table with ring group ids */
6701 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6702 		if (!no_rss)
6703 			j = bp->rss_indir_tbl[i];
6704 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6705 	}
6706 }
6707 
6708 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6709 				    struct bnxt_vnic_info *vnic)
6710 {
6711 	__le16 *ring_tbl = vnic->rss_table;
6712 	struct bnxt_rx_ring_info *rxr;
6713 	u16 tbl_size, i;
6714 
6715 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6716 
6717 	for (i = 0; i < tbl_size; i++) {
6718 		u16 ring_id, j;
6719 
6720 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6721 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6722 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6723 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6724 		else
6725 			j = bp->rss_indir_tbl[i];
6726 		rxr = &bp->rx_ring[j];
6727 
6728 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6729 		*ring_tbl++ = cpu_to_le16(ring_id);
6730 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6731 		*ring_tbl++ = cpu_to_le16(ring_id);
6732 	}
6733 }
6734 
6735 static void
6736 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6737 			 struct bnxt_vnic_info *vnic)
6738 {
6739 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6740 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6741 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6742 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6743 	} else {
6744 		bnxt_fill_hw_rss_tbl(bp, vnic);
6745 	}
6746 
6747 	if (bp->rss_hash_delta) {
6748 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6749 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6750 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6751 		else
6752 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6753 	} else {
6754 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6755 	}
6756 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6757 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6758 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6759 }
6760 
6761 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6762 				  bool set_rss)
6763 {
6764 	struct hwrm_vnic_rss_cfg_input *req;
6765 	int rc;
6766 
6767 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6768 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6769 		return 0;
6770 
6771 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6772 	if (rc)
6773 		return rc;
6774 
6775 	if (set_rss)
6776 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6777 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6778 	return hwrm_req_send(bp, req);
6779 }
6780 
6781 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6782 				     struct bnxt_vnic_info *vnic, bool set_rss)
6783 {
6784 	struct hwrm_vnic_rss_cfg_input *req;
6785 	dma_addr_t ring_tbl_map;
6786 	u32 i, nr_ctxs;
6787 	int rc;
6788 
6789 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6790 	if (rc)
6791 		return rc;
6792 
6793 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6794 	if (!set_rss)
6795 		return hwrm_req_send(bp, req);
6796 
6797 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6798 	ring_tbl_map = vnic->rss_table_dma_addr;
6799 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6800 
6801 	hwrm_req_hold(bp, req);
6802 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6803 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6804 		req->ring_table_pair_index = i;
6805 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6806 		rc = hwrm_req_send(bp, req);
6807 		if (rc)
6808 			goto exit;
6809 	}
6810 
6811 exit:
6812 	hwrm_req_drop(bp, req);
6813 	return rc;
6814 }
6815 
6816 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6817 {
6818 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6819 	struct hwrm_vnic_rss_qcfg_output *resp;
6820 	struct hwrm_vnic_rss_qcfg_input *req;
6821 
6822 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6823 		return;
6824 
6825 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6826 	/* all contexts configured to same hash_type, zero always exists */
6827 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6828 	resp = hwrm_req_hold(bp, req);
6829 	if (!hwrm_req_send(bp, req)) {
6830 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6831 		bp->rss_hash_delta = 0;
6832 	}
6833 	hwrm_req_drop(bp, req);
6834 }
6835 
6836 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6837 {
6838 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6839 	struct hwrm_vnic_plcmodes_cfg_input *req;
6840 	int rc;
6841 
6842 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6843 	if (rc)
6844 		return rc;
6845 
6846 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6847 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6848 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6849 
6850 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6851 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6852 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6853 		req->enables |=
6854 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6855 		req->hds_threshold = cpu_to_le16(hds_thresh);
6856 	}
6857 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6858 	return hwrm_req_send(bp, req);
6859 }
6860 
6861 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6862 					struct bnxt_vnic_info *vnic,
6863 					u16 ctx_idx)
6864 {
6865 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6866 
6867 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6868 		return;
6869 
6870 	req->rss_cos_lb_ctx_id =
6871 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6872 
6873 	hwrm_req_send(bp, req);
6874 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6875 }
6876 
6877 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6878 {
6879 	int i, j;
6880 
6881 	for (i = 0; i < bp->nr_vnics; i++) {
6882 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6883 
6884 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6885 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6886 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6887 		}
6888 	}
6889 	bp->rsscos_nr_ctxs = 0;
6890 }
6891 
6892 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6893 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6894 {
6895 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6896 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6897 	int rc;
6898 
6899 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6900 	if (rc)
6901 		return rc;
6902 
6903 	resp = hwrm_req_hold(bp, req);
6904 	rc = hwrm_req_send(bp, req);
6905 	if (!rc)
6906 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6907 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6908 	hwrm_req_drop(bp, req);
6909 
6910 	return rc;
6911 }
6912 
6913 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6914 {
6915 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6916 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6917 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6918 }
6919 
6920 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6921 {
6922 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6923 	struct hwrm_vnic_cfg_input *req;
6924 	unsigned int ring = 0, grp_idx;
6925 	u16 def_vlan = 0;
6926 	int rc;
6927 
6928 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6929 	if (rc)
6930 		return rc;
6931 
6932 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6933 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6934 
6935 		req->default_rx_ring_id =
6936 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6937 		req->default_cmpl_ring_id =
6938 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6939 		req->enables =
6940 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6941 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6942 		goto vnic_mru;
6943 	}
6944 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6945 	/* Only RSS support for now TBD: COS & LB */
6946 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6947 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6948 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6949 					   VNIC_CFG_REQ_ENABLES_MRU);
6950 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6951 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6952 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6953 					   VNIC_CFG_REQ_ENABLES_MRU);
6954 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6955 	} else {
6956 		req->rss_rule = cpu_to_le16(0xffff);
6957 	}
6958 
6959 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6960 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6961 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6962 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6963 	} else {
6964 		req->cos_rule = cpu_to_le16(0xffff);
6965 	}
6966 
6967 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6968 		ring = 0;
6969 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6970 		ring = vnic->vnic_id - 1;
6971 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6972 		ring = bp->rx_nr_rings - 1;
6973 
6974 	grp_idx = bp->rx_ring[ring].bnapi->index;
6975 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6976 	req->lb_rule = cpu_to_le16(0xffff);
6977 vnic_mru:
6978 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
6979 	req->mru = cpu_to_le16(vnic->mru);
6980 
6981 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6982 #ifdef CONFIG_BNXT_SRIOV
6983 	if (BNXT_VF(bp))
6984 		def_vlan = bp->vf.vlan;
6985 #endif
6986 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6987 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6988 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT &&
6989 	    bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA]))
6990 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6991 
6992 	return hwrm_req_send(bp, req);
6993 }
6994 
6995 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6996 				    struct bnxt_vnic_info *vnic)
6997 {
6998 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6999 		struct hwrm_vnic_free_input *req;
7000 
7001 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
7002 			return;
7003 
7004 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
7005 
7006 		hwrm_req_send(bp, req);
7007 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
7008 	}
7009 }
7010 
7011 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
7012 {
7013 	u16 i;
7014 
7015 	for (i = 0; i < bp->nr_vnics; i++)
7016 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
7017 }
7018 
7019 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
7020 			 unsigned int start_rx_ring_idx,
7021 			 unsigned int nr_rings)
7022 {
7023 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
7024 	struct hwrm_vnic_alloc_output *resp;
7025 	struct hwrm_vnic_alloc_input *req;
7026 	int rc;
7027 
7028 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
7029 	if (rc)
7030 		return rc;
7031 
7032 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7033 		goto vnic_no_ring_grps;
7034 
7035 	/* map ring groups to this vnic */
7036 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
7037 		grp_idx = bp->rx_ring[i].bnapi->index;
7038 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
7039 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
7040 				   j, nr_rings);
7041 			break;
7042 		}
7043 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
7044 	}
7045 
7046 vnic_no_ring_grps:
7047 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
7048 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
7049 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
7050 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
7051 
7052 	resp = hwrm_req_hold(bp, req);
7053 	rc = hwrm_req_send(bp, req);
7054 	if (!rc)
7055 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
7056 	hwrm_req_drop(bp, req);
7057 	return rc;
7058 }
7059 
7060 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
7061 {
7062 	struct hwrm_vnic_qcaps_output *resp;
7063 	struct hwrm_vnic_qcaps_input *req;
7064 	int rc;
7065 
7066 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
7067 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
7068 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
7069 	if (bp->hwrm_spec_code < 0x10600)
7070 		return 0;
7071 
7072 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
7073 	if (rc)
7074 		return rc;
7075 
7076 	resp = hwrm_req_hold(bp, req);
7077 	rc = hwrm_req_send(bp, req);
7078 	if (!rc) {
7079 		u32 flags = le32_to_cpu(resp->flags);
7080 
7081 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
7082 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
7083 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
7084 		if (flags &
7085 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
7086 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7087 
7088 		/* Older P5 fw before EXT_HW_STATS support did not set
7089 		 * VLAN_STRIP_CAP properly.
7090 		 */
7091 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7092 		    (BNXT_CHIP_P5(bp) &&
7093 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7094 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7095 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7096 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7097 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7098 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7099 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7100 		if (bp->max_tpa_v2) {
7101 			if (BNXT_CHIP_P5(bp))
7102 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7103 			else
7104 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7105 		}
7106 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7107 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7108 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7109 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7110 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7111 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7112 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7113 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7114 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7115 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7116 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7117 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7118 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7119 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7120 	}
7121 	hwrm_req_drop(bp, req);
7122 	return rc;
7123 }
7124 
7125 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7126 {
7127 	struct hwrm_ring_grp_alloc_output *resp;
7128 	struct hwrm_ring_grp_alloc_input *req;
7129 	int rc;
7130 	u16 i;
7131 
7132 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7133 		return 0;
7134 
7135 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7136 	if (rc)
7137 		return rc;
7138 
7139 	resp = hwrm_req_hold(bp, req);
7140 	for (i = 0; i < bp->rx_nr_rings; i++) {
7141 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7142 
7143 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7144 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7145 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7146 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7147 
7148 		rc = hwrm_req_send(bp, req);
7149 
7150 		if (rc)
7151 			break;
7152 
7153 		bp->grp_info[grp_idx].fw_grp_id =
7154 			le32_to_cpu(resp->ring_group_id);
7155 	}
7156 	hwrm_req_drop(bp, req);
7157 	return rc;
7158 }
7159 
7160 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7161 {
7162 	struct hwrm_ring_grp_free_input *req;
7163 	u16 i;
7164 
7165 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7166 		return;
7167 
7168 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7169 		return;
7170 
7171 	hwrm_req_hold(bp, req);
7172 	for (i = 0; i < bp->cp_nr_rings; i++) {
7173 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7174 			continue;
7175 		req->ring_group_id =
7176 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7177 
7178 		hwrm_req_send(bp, req);
7179 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7180 	}
7181 	hwrm_req_drop(bp, req);
7182 }
7183 
7184 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7185 				       struct hwrm_ring_alloc_input *req,
7186 				       struct bnxt_rx_ring_info *rxr,
7187 				       struct bnxt_ring_struct *ring)
7188 {
7189 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7190 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7191 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7192 
7193 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7194 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7195 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7196 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7197 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7198 	} else {
7199 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7200 		if (NET_IP_ALIGN == 2)
7201 			req->flags =
7202 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7203 	}
7204 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7205 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7206 	req->enables |= cpu_to_le32(enables);
7207 }
7208 
7209 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7210 				    struct bnxt_rx_ring_info *rxr,
7211 				    struct bnxt_ring_struct *ring,
7212 				    u32 ring_type, u32 map_index)
7213 {
7214 	struct hwrm_ring_alloc_output *resp;
7215 	struct hwrm_ring_alloc_input *req;
7216 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7217 	struct bnxt_ring_grp_info *grp_info;
7218 	int rc, err = 0;
7219 	u16 ring_id;
7220 
7221 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7222 	if (rc)
7223 		goto exit;
7224 
7225 	req->enables = 0;
7226 	if (rmem->nr_pages > 1) {
7227 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7228 		/* Page size is in log2 units */
7229 		req->page_size = BNXT_PAGE_SHIFT;
7230 		req->page_tbl_depth = 1;
7231 	} else {
7232 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7233 	}
7234 	req->fbo = 0;
7235 	/* Association of ring index with doorbell index and MSIX number */
7236 	req->logical_id = cpu_to_le16(map_index);
7237 
7238 	switch (ring_type) {
7239 	case HWRM_RING_ALLOC_TX: {
7240 		struct bnxt_tx_ring_info *txr;
7241 		u16 flags = 0;
7242 
7243 		txr = container_of(ring, struct bnxt_tx_ring_info,
7244 				   tx_ring_struct);
7245 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7246 		/* Association of transmit ring with completion ring */
7247 		grp_info = &bp->grp_info[ring->grp_idx];
7248 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7249 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7250 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7251 		req->queue_id = cpu_to_le16(ring->queue_id);
7252 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7253 			req->cmpl_coal_cnt =
7254 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7255 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7256 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7257 		req->flags = cpu_to_le16(flags);
7258 		break;
7259 	}
7260 	case HWRM_RING_ALLOC_RX:
7261 	case HWRM_RING_ALLOC_AGG:
7262 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7263 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7264 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7265 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7266 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7267 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7268 						   rxr, ring);
7269 		break;
7270 	case HWRM_RING_ALLOC_CMPL:
7271 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7272 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7273 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7274 			/* Association of cp ring with nq */
7275 			grp_info = &bp->grp_info[map_index];
7276 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7277 			req->cq_handle = cpu_to_le64(ring->handle);
7278 			req->enables |= cpu_to_le32(
7279 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7280 		} else {
7281 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7282 		}
7283 		break;
7284 	case HWRM_RING_ALLOC_NQ:
7285 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7286 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7287 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7288 		break;
7289 	default:
7290 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7291 			   ring_type);
7292 		return -EINVAL;
7293 	}
7294 
7295 	resp = hwrm_req_hold(bp, req);
7296 	rc = hwrm_req_send(bp, req);
7297 	err = le16_to_cpu(resp->error_code);
7298 	ring_id = le16_to_cpu(resp->ring_id);
7299 	hwrm_req_drop(bp, req);
7300 
7301 exit:
7302 	if (rc || err) {
7303 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7304 			   ring_type, rc, err);
7305 		return -EIO;
7306 	}
7307 	ring->fw_ring_id = ring_id;
7308 	return rc;
7309 }
7310 
7311 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7312 {
7313 	int rc;
7314 
7315 	if (BNXT_PF(bp)) {
7316 		struct hwrm_func_cfg_input *req;
7317 
7318 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7319 		if (rc)
7320 			return rc;
7321 
7322 		req->fid = cpu_to_le16(0xffff);
7323 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7324 		req->async_event_cr = cpu_to_le16(idx);
7325 		return hwrm_req_send(bp, req);
7326 	} else {
7327 		struct hwrm_func_vf_cfg_input *req;
7328 
7329 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7330 		if (rc)
7331 			return rc;
7332 
7333 		req->enables =
7334 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7335 		req->async_event_cr = cpu_to_le16(idx);
7336 		return hwrm_req_send(bp, req);
7337 	}
7338 }
7339 
7340 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7341 			     u32 ring_type)
7342 {
7343 	switch (ring_type) {
7344 	case HWRM_RING_ALLOC_TX:
7345 		db->db_ring_mask = bp->tx_ring_mask;
7346 		break;
7347 	case HWRM_RING_ALLOC_RX:
7348 		db->db_ring_mask = bp->rx_ring_mask;
7349 		break;
7350 	case HWRM_RING_ALLOC_AGG:
7351 		db->db_ring_mask = bp->rx_agg_ring_mask;
7352 		break;
7353 	case HWRM_RING_ALLOC_CMPL:
7354 	case HWRM_RING_ALLOC_NQ:
7355 		db->db_ring_mask = bp->cp_ring_mask;
7356 		break;
7357 	}
7358 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7359 		db->db_epoch_mask = db->db_ring_mask + 1;
7360 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7361 	}
7362 }
7363 
7364 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7365 			u32 map_idx, u32 xid)
7366 {
7367 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7368 		switch (ring_type) {
7369 		case HWRM_RING_ALLOC_TX:
7370 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7371 			break;
7372 		case HWRM_RING_ALLOC_RX:
7373 		case HWRM_RING_ALLOC_AGG:
7374 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7375 			break;
7376 		case HWRM_RING_ALLOC_CMPL:
7377 			db->db_key64 = DBR_PATH_L2;
7378 			break;
7379 		case HWRM_RING_ALLOC_NQ:
7380 			db->db_key64 = DBR_PATH_L2;
7381 			break;
7382 		}
7383 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7384 
7385 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7386 			db->db_key64 |= DBR_VALID;
7387 
7388 		db->doorbell = bp->bar1 + bp->db_offset;
7389 	} else {
7390 		db->doorbell = bp->bar1 + map_idx * 0x80;
7391 		switch (ring_type) {
7392 		case HWRM_RING_ALLOC_TX:
7393 			db->db_key32 = DB_KEY_TX;
7394 			break;
7395 		case HWRM_RING_ALLOC_RX:
7396 		case HWRM_RING_ALLOC_AGG:
7397 			db->db_key32 = DB_KEY_RX;
7398 			break;
7399 		case HWRM_RING_ALLOC_CMPL:
7400 			db->db_key32 = DB_KEY_CP;
7401 			break;
7402 		}
7403 	}
7404 	bnxt_set_db_mask(bp, db, ring_type);
7405 }
7406 
7407 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7408 				   struct bnxt_rx_ring_info *rxr)
7409 {
7410 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7411 	struct bnxt_napi *bnapi = rxr->bnapi;
7412 	u32 type = HWRM_RING_ALLOC_RX;
7413 	u32 map_idx = bnapi->index;
7414 	int rc;
7415 
7416 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7417 	if (rc)
7418 		return rc;
7419 
7420 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7421 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7422 
7423 	return 0;
7424 }
7425 
7426 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7427 				       struct bnxt_rx_ring_info *rxr)
7428 {
7429 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7430 	u32 type = HWRM_RING_ALLOC_AGG;
7431 	u32 grp_idx = ring->grp_idx;
7432 	u32 map_idx;
7433 	int rc;
7434 
7435 	map_idx = grp_idx + bp->rx_nr_rings;
7436 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7437 	if (rc)
7438 		return rc;
7439 
7440 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7441 		    ring->fw_ring_id);
7442 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7443 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7444 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7445 
7446 	return 0;
7447 }
7448 
7449 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7450 				      struct bnxt_cp_ring_info *cpr)
7451 {
7452 	const u32 type = HWRM_RING_ALLOC_CMPL;
7453 	struct bnxt_napi *bnapi = cpr->bnapi;
7454 	struct bnxt_ring_struct *ring;
7455 	u32 map_idx = bnapi->index;
7456 	int rc;
7457 
7458 	ring = &cpr->cp_ring_struct;
7459 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7460 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7461 	if (rc)
7462 		return rc;
7463 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7464 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7465 	return 0;
7466 }
7467 
7468 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7469 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7470 {
7471 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7472 	const u32 type = HWRM_RING_ALLOC_TX;
7473 	int rc;
7474 
7475 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7476 	if (rc)
7477 		return rc;
7478 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7479 	return 0;
7480 }
7481 
7482 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7483 {
7484 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7485 	int i, rc = 0;
7486 	u32 type;
7487 
7488 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7489 		type = HWRM_RING_ALLOC_NQ;
7490 	else
7491 		type = HWRM_RING_ALLOC_CMPL;
7492 	for (i = 0; i < bp->cp_nr_rings; i++) {
7493 		struct bnxt_napi *bnapi = bp->bnapi[i];
7494 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7495 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7496 		u32 map_idx = ring->map_idx;
7497 		unsigned int vector;
7498 
7499 		vector = bp->irq_tbl[map_idx].vector;
7500 		disable_irq_nosync(vector);
7501 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7502 		if (rc) {
7503 			enable_irq(vector);
7504 			goto err_out;
7505 		}
7506 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7507 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7508 		enable_irq(vector);
7509 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7510 
7511 		if (!i) {
7512 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7513 			if (rc)
7514 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7515 		}
7516 	}
7517 
7518 	for (i = 0; i < bp->tx_nr_rings; i++) {
7519 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7520 
7521 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7522 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7523 			if (rc)
7524 				goto err_out;
7525 		}
7526 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7527 		if (rc)
7528 			goto err_out;
7529 	}
7530 
7531 	for (i = 0; i < bp->rx_nr_rings; i++) {
7532 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7533 
7534 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7535 		if (rc)
7536 			goto err_out;
7537 		/* If we have agg rings, post agg buffers first. */
7538 		if (!agg_rings)
7539 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7540 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7541 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7542 			if (rc)
7543 				goto err_out;
7544 		}
7545 	}
7546 
7547 	if (agg_rings) {
7548 		for (i = 0; i < bp->rx_nr_rings; i++) {
7549 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7550 			if (rc)
7551 				goto err_out;
7552 		}
7553 	}
7554 err_out:
7555 	return rc;
7556 }
7557 
7558 static void bnxt_cancel_dim(struct bnxt *bp)
7559 {
7560 	int i;
7561 
7562 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7563 	 * if NAPI is enabled.
7564 	 */
7565 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7566 		return;
7567 
7568 	/* Make sure NAPI sees that the VNIC is disabled */
7569 	synchronize_net();
7570 	for (i = 0; i < bp->rx_nr_rings; i++) {
7571 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7572 		struct bnxt_napi *bnapi = rxr->bnapi;
7573 
7574 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7575 	}
7576 }
7577 
7578 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7579 				   struct bnxt_ring_struct *ring,
7580 				   u32 ring_type, int cmpl_ring_id)
7581 {
7582 	struct hwrm_ring_free_output *resp;
7583 	struct hwrm_ring_free_input *req;
7584 	u16 error_code = 0;
7585 	int rc;
7586 
7587 	if (BNXT_NO_FW_ACCESS(bp))
7588 		return 0;
7589 
7590 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7591 	if (rc)
7592 		goto exit;
7593 
7594 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7595 	req->ring_type = ring_type;
7596 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7597 
7598 	resp = hwrm_req_hold(bp, req);
7599 	rc = hwrm_req_send(bp, req);
7600 	error_code = le16_to_cpu(resp->error_code);
7601 	hwrm_req_drop(bp, req);
7602 exit:
7603 	if (rc || error_code) {
7604 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7605 			   ring_type, rc, error_code);
7606 		return -EIO;
7607 	}
7608 	return 0;
7609 }
7610 
7611 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7612 				   struct bnxt_tx_ring_info *txr,
7613 				   bool close_path)
7614 {
7615 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7616 	u32 cmpl_ring_id;
7617 
7618 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7619 		return;
7620 
7621 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7622 		       INVALID_HW_RING_ID;
7623 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7624 				cmpl_ring_id);
7625 	ring->fw_ring_id = INVALID_HW_RING_ID;
7626 }
7627 
7628 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7629 				   struct bnxt_rx_ring_info *rxr,
7630 				   bool close_path)
7631 {
7632 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7633 	u32 grp_idx = rxr->bnapi->index;
7634 	u32 cmpl_ring_id;
7635 
7636 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7637 		return;
7638 
7639 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7640 	hwrm_ring_free_send_msg(bp, ring,
7641 				RING_FREE_REQ_RING_TYPE_RX,
7642 				close_path ? cmpl_ring_id :
7643 				INVALID_HW_RING_ID);
7644 	ring->fw_ring_id = INVALID_HW_RING_ID;
7645 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7646 }
7647 
7648 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7649 				       struct bnxt_rx_ring_info *rxr,
7650 				       bool close_path)
7651 {
7652 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7653 	u32 grp_idx = rxr->bnapi->index;
7654 	u32 type, cmpl_ring_id;
7655 
7656 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7657 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7658 	else
7659 		type = RING_FREE_REQ_RING_TYPE_RX;
7660 
7661 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7662 		return;
7663 
7664 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7665 	hwrm_ring_free_send_msg(bp, ring, type,
7666 				close_path ? cmpl_ring_id :
7667 				INVALID_HW_RING_ID);
7668 	ring->fw_ring_id = INVALID_HW_RING_ID;
7669 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7670 }
7671 
7672 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7673 				   struct bnxt_cp_ring_info *cpr)
7674 {
7675 	struct bnxt_ring_struct *ring;
7676 
7677 	ring = &cpr->cp_ring_struct;
7678 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7679 		return;
7680 
7681 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7682 				INVALID_HW_RING_ID);
7683 	ring->fw_ring_id = INVALID_HW_RING_ID;
7684 }
7685 
7686 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7687 {
7688 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7689 	int i, size = ring->ring_mem.page_size;
7690 
7691 	cpr->cp_raw_cons = 0;
7692 	cpr->toggle = 0;
7693 
7694 	for (i = 0; i < bp->cp_nr_pages; i++)
7695 		if (cpr->cp_desc_ring[i])
7696 			memset(cpr->cp_desc_ring[i], 0, size);
7697 }
7698 
7699 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7700 {
7701 	u32 type;
7702 	int i;
7703 
7704 	if (!bp->bnapi)
7705 		return;
7706 
7707 	for (i = 0; i < bp->tx_nr_rings; i++)
7708 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7709 
7710 	bnxt_cancel_dim(bp);
7711 	for (i = 0; i < bp->rx_nr_rings; i++) {
7712 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7713 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7714 	}
7715 
7716 	/* The completion rings are about to be freed.  After that the
7717 	 * IRQ doorbell will not work anymore.  So we need to disable
7718 	 * IRQ here.
7719 	 */
7720 	bnxt_disable_int_sync(bp);
7721 
7722 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7723 		type = RING_FREE_REQ_RING_TYPE_NQ;
7724 	else
7725 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7726 	for (i = 0; i < bp->cp_nr_rings; i++) {
7727 		struct bnxt_napi *bnapi = bp->bnapi[i];
7728 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7729 		struct bnxt_ring_struct *ring;
7730 		int j;
7731 
7732 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7733 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7734 
7735 		ring = &cpr->cp_ring_struct;
7736 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7737 			hwrm_ring_free_send_msg(bp, ring, type,
7738 						INVALID_HW_RING_ID);
7739 			ring->fw_ring_id = INVALID_HW_RING_ID;
7740 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7741 		}
7742 	}
7743 }
7744 
7745 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7746 			     bool shared);
7747 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7748 			   bool shared);
7749 
7750 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7751 {
7752 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7753 	struct hwrm_func_qcfg_output *resp;
7754 	struct hwrm_func_qcfg_input *req;
7755 	int rc;
7756 
7757 	if (bp->hwrm_spec_code < 0x10601)
7758 		return 0;
7759 
7760 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7761 	if (rc)
7762 		return rc;
7763 
7764 	req->fid = cpu_to_le16(0xffff);
7765 	resp = hwrm_req_hold(bp, req);
7766 	rc = hwrm_req_send(bp, req);
7767 	if (rc) {
7768 		hwrm_req_drop(bp, req);
7769 		return rc;
7770 	}
7771 
7772 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7773 	if (BNXT_NEW_RM(bp)) {
7774 		u16 cp, stats;
7775 
7776 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7777 		hw_resc->resv_hw_ring_grps =
7778 			le32_to_cpu(resp->alloc_hw_ring_grps);
7779 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7780 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7781 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7782 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7783 		hw_resc->resv_irqs = cp;
7784 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7785 			int rx = hw_resc->resv_rx_rings;
7786 			int tx = hw_resc->resv_tx_rings;
7787 
7788 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7789 				rx >>= 1;
7790 			if (cp < (rx + tx)) {
7791 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7792 				if (rc)
7793 					goto get_rings_exit;
7794 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7795 					rx <<= 1;
7796 				hw_resc->resv_rx_rings = rx;
7797 				hw_resc->resv_tx_rings = tx;
7798 			}
7799 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7800 			hw_resc->resv_hw_ring_grps = rx;
7801 		}
7802 		hw_resc->resv_cp_rings = cp;
7803 		hw_resc->resv_stat_ctxs = stats;
7804 	}
7805 get_rings_exit:
7806 	hwrm_req_drop(bp, req);
7807 	return rc;
7808 }
7809 
7810 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7811 {
7812 	struct hwrm_func_qcfg_output *resp;
7813 	struct hwrm_func_qcfg_input *req;
7814 	int rc;
7815 
7816 	if (bp->hwrm_spec_code < 0x10601)
7817 		return 0;
7818 
7819 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7820 	if (rc)
7821 		return rc;
7822 
7823 	req->fid = cpu_to_le16(fid);
7824 	resp = hwrm_req_hold(bp, req);
7825 	rc = hwrm_req_send(bp, req);
7826 	if (!rc)
7827 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7828 
7829 	hwrm_req_drop(bp, req);
7830 	return rc;
7831 }
7832 
7833 static bool bnxt_rfs_supported(struct bnxt *bp);
7834 
7835 static struct hwrm_func_cfg_input *
7836 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7837 {
7838 	struct hwrm_func_cfg_input *req;
7839 	u32 enables = 0;
7840 
7841 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7842 		return NULL;
7843 
7844 	req->fid = cpu_to_le16(0xffff);
7845 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7846 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7847 	if (BNXT_NEW_RM(bp)) {
7848 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7849 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7850 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7851 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7852 			enables |= hwr->cp_p5 ?
7853 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7854 		} else {
7855 			enables |= hwr->cp ?
7856 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7857 			enables |= hwr->grp ?
7858 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7859 		}
7860 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7861 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7862 					  0;
7863 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7864 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7865 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7866 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7867 			req->num_msix = cpu_to_le16(hwr->cp);
7868 		} else {
7869 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7870 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7871 		}
7872 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7873 		req->num_vnics = cpu_to_le16(hwr->vnic);
7874 	}
7875 	req->enables = cpu_to_le32(enables);
7876 	return req;
7877 }
7878 
7879 static struct hwrm_func_vf_cfg_input *
7880 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7881 {
7882 	struct hwrm_func_vf_cfg_input *req;
7883 	u32 enables = 0;
7884 
7885 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7886 		return NULL;
7887 
7888 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7889 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7890 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7891 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7892 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7893 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7894 		enables |= hwr->cp_p5 ?
7895 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7896 	} else {
7897 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7898 		enables |= hwr->grp ?
7899 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7900 	}
7901 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7902 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7903 
7904 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7905 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7906 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7907 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7908 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7909 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7910 	} else {
7911 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7912 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7913 	}
7914 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7915 	req->num_vnics = cpu_to_le16(hwr->vnic);
7916 
7917 	req->enables = cpu_to_le32(enables);
7918 	return req;
7919 }
7920 
7921 static int
7922 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7923 {
7924 	struct hwrm_func_cfg_input *req;
7925 	int rc;
7926 
7927 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7928 	if (!req)
7929 		return -ENOMEM;
7930 
7931 	if (!req->enables) {
7932 		hwrm_req_drop(bp, req);
7933 		return 0;
7934 	}
7935 
7936 	rc = hwrm_req_send(bp, req);
7937 	if (rc)
7938 		return rc;
7939 
7940 	if (bp->hwrm_spec_code < 0x10601)
7941 		bp->hw_resc.resv_tx_rings = hwr->tx;
7942 
7943 	return bnxt_hwrm_get_rings(bp);
7944 }
7945 
7946 static int
7947 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7948 {
7949 	struct hwrm_func_vf_cfg_input *req;
7950 	int rc;
7951 
7952 	if (!BNXT_NEW_RM(bp)) {
7953 		bp->hw_resc.resv_tx_rings = hwr->tx;
7954 		return 0;
7955 	}
7956 
7957 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7958 	if (!req)
7959 		return -ENOMEM;
7960 
7961 	rc = hwrm_req_send(bp, req);
7962 	if (rc)
7963 		return rc;
7964 
7965 	return bnxt_hwrm_get_rings(bp);
7966 }
7967 
7968 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7969 {
7970 	if (BNXT_PF(bp))
7971 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7972 	else
7973 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7974 }
7975 
7976 int bnxt_nq_rings_in_use(struct bnxt *bp)
7977 {
7978 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7979 }
7980 
7981 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7982 {
7983 	int cp;
7984 
7985 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7986 		return bnxt_nq_rings_in_use(bp);
7987 
7988 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7989 	return cp;
7990 }
7991 
7992 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7993 {
7994 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7995 }
7996 
7997 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7998 {
7999 	if (!hwr->grp)
8000 		return 0;
8001 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8002 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
8003 
8004 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8005 			rss_ctx *= hwr->vnic;
8006 		return rss_ctx;
8007 	}
8008 	if (BNXT_VF(bp))
8009 		return BNXT_VF_MAX_RSS_CTX;
8010 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
8011 		return hwr->grp + 1;
8012 	return 1;
8013 }
8014 
8015 /* Check if a default RSS map needs to be setup.  This function is only
8016  * used on older firmware that does not require reserving RX rings.
8017  */
8018 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
8019 {
8020 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8021 
8022 	/* The RSS map is valid for RX rings set to resv_rx_rings */
8023 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
8024 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
8025 		if (!netif_is_rxfh_configured(bp->dev))
8026 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8027 	}
8028 }
8029 
8030 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
8031 {
8032 	if (bp->flags & BNXT_FLAG_RFS) {
8033 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8034 			return 2 + bp->num_rss_ctx;
8035 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8036 			return rx_rings + 1;
8037 	}
8038 	return 1;
8039 }
8040 
8041 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8042 {
8043 	hwr->cp = bnxt_nq_rings_in_use(bp);
8044 	hwr->cp_p5 = 0;
8045 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8046 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
8047 	hwr->tx = bp->tx_nr_rings;
8048 	hwr->rx = bp->rx_nr_rings;
8049 	hwr->grp = hwr->rx;
8050 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
8051 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
8052 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8053 		hwr->rx <<= 1;
8054 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
8055 }
8056 
8057 static bool bnxt_need_reserve_rings(struct bnxt *bp)
8058 {
8059 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8060 	struct bnxt_hw_rings hwr;
8061 
8062 	bnxt_get_total_resources(bp, &hwr);
8063 
8064 	/* Old firmware does not need RX ring reservations but we still
8065 	 * need to setup a default RSS map when needed.  With new firmware
8066 	 * we go through RX ring reservations first and then set up the
8067 	 * RSS map for the successfully reserved RX rings when needed.
8068 	 */
8069 	if (!BNXT_NEW_RM(bp))
8070 		bnxt_check_rss_tbl_no_rmgr(bp);
8071 
8072 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
8073 		return true;
8074 
8075 	if (!BNXT_NEW_RM(bp))
8076 		return false;
8077 
8078 	if (hw_resc->resv_rx_rings != hwr.rx ||
8079 	    hw_resc->resv_vnics != hwr.vnic ||
8080 	    hw_resc->resv_stat_ctxs != hwr.stat ||
8081 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
8082 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
8083 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
8084 		return true;
8085 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8086 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
8087 			return true;
8088 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
8089 		return true;
8090 	}
8091 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
8092 	    hw_resc->resv_irqs != hwr.cp)
8093 		return true;
8094 	return false;
8095 }
8096 
8097 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8098 {
8099 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8100 
8101 	hwr->tx = hw_resc->resv_tx_rings;
8102 	if (BNXT_NEW_RM(bp)) {
8103 		hwr->rx = hw_resc->resv_rx_rings;
8104 		hwr->cp = hw_resc->resv_irqs;
8105 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8106 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8107 		hwr->grp = hw_resc->resv_hw_ring_grps;
8108 		hwr->vnic = hw_resc->resv_vnics;
8109 		hwr->stat = hw_resc->resv_stat_ctxs;
8110 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8111 	}
8112 }
8113 
8114 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8115 {
8116 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8117 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8118 }
8119 
8120 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8121 
8122 static int __bnxt_reserve_rings(struct bnxt *bp)
8123 {
8124 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
8125 	struct bnxt_hw_rings hwr = {0};
8126 	int rx_rings, old_rx_rings, rc;
8127 	int cp = bp->cp_nr_rings;
8128 	int ulp_msix = 0;
8129 	bool sh = false;
8130 	int tx_cp;
8131 
8132 	if (!bnxt_need_reserve_rings(bp))
8133 		return 0;
8134 
8135 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
8136 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8137 		if (!ulp_msix)
8138 			bnxt_set_ulp_stat_ctxs(bp, 0);
8139 		else
8140 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8141 
8142 		if (ulp_msix > bp->ulp_num_msix_want)
8143 			ulp_msix = bp->ulp_num_msix_want;
8144 		hwr.cp = cp + ulp_msix;
8145 	} else {
8146 		hwr.cp = bnxt_nq_rings_in_use(bp);
8147 	}
8148 
8149 	hwr.tx = bp->tx_nr_rings;
8150 	hwr.rx = bp->rx_nr_rings;
8151 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8152 		sh = true;
8153 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8154 		hwr.cp_p5 = hwr.rx + hwr.tx;
8155 
8156 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8157 
8158 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8159 		hwr.rx <<= 1;
8160 	hwr.grp = bp->rx_nr_rings;
8161 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8162 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8163 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8164 
8165 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8166 	if (rc)
8167 		return rc;
8168 
8169 	bnxt_copy_reserved_rings(bp, &hwr);
8170 
8171 	rx_rings = hwr.rx;
8172 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8173 		if (hwr.rx >= 2) {
8174 			rx_rings = hwr.rx >> 1;
8175 		} else {
8176 			if (netif_running(bp->dev))
8177 				return -ENOMEM;
8178 
8179 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8180 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8181 			bp->dev->hw_features &= ~NETIF_F_LRO;
8182 			bp->dev->features &= ~NETIF_F_LRO;
8183 			bnxt_set_ring_params(bp);
8184 		}
8185 	}
8186 	rx_rings = min_t(int, rx_rings, hwr.grp);
8187 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8188 	if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8189 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8190 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8191 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8192 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8193 		hwr.rx = rx_rings << 1;
8194 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8195 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8196 	if (hwr.tx != bp->tx_nr_rings) {
8197 		netdev_warn(bp->dev,
8198 			    "Able to reserve only %d out of %d requested TX rings\n",
8199 			    hwr.tx, bp->tx_nr_rings);
8200 	}
8201 	bp->tx_nr_rings = hwr.tx;
8202 
8203 	/* If we cannot reserve all the RX rings, reset the RSS map only
8204 	 * if absolutely necessary
8205 	 */
8206 	if (rx_rings != bp->rx_nr_rings) {
8207 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8208 			    rx_rings, bp->rx_nr_rings);
8209 		if (netif_is_rxfh_configured(bp->dev) &&
8210 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8211 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8212 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8213 			ethtool_rxfh_indir_lost(bp->dev);
8214 		}
8215 	}
8216 	bp->rx_nr_rings = rx_rings;
8217 	bp->cp_nr_rings = hwr.cp;
8218 
8219 	/* Fall back if we cannot reserve enough HW RSS contexts */
8220 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8221 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8222 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8223 
8224 	if (!bnxt_rings_ok(bp, &hwr))
8225 		return -ENOMEM;
8226 
8227 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8228 	    !netif_is_rxfh_configured(bp->dev))
8229 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8230 
8231 	if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) {
8232 		int resv_msix, resv_ctx, ulp_ctxs;
8233 		struct bnxt_hw_resc *hw_resc;
8234 
8235 		hw_resc = &bp->hw_resc;
8236 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8237 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8238 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8239 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8240 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8241 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8242 	}
8243 
8244 	return rc;
8245 }
8246 
8247 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8248 {
8249 	struct hwrm_func_vf_cfg_input *req;
8250 	u32 flags;
8251 
8252 	if (!BNXT_NEW_RM(bp))
8253 		return 0;
8254 
8255 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8256 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8257 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8258 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8259 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8260 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8261 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8262 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8263 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8264 
8265 	req->flags = cpu_to_le32(flags);
8266 	return hwrm_req_send_silent(bp, req);
8267 }
8268 
8269 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8270 {
8271 	struct hwrm_func_cfg_input *req;
8272 	u32 flags;
8273 
8274 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8275 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8276 	if (BNXT_NEW_RM(bp)) {
8277 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8278 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8279 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8280 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8281 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8282 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8283 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8284 		else
8285 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8286 	}
8287 
8288 	req->flags = cpu_to_le32(flags);
8289 	return hwrm_req_send_silent(bp, req);
8290 }
8291 
8292 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8293 {
8294 	if (bp->hwrm_spec_code < 0x10801)
8295 		return 0;
8296 
8297 	if (BNXT_PF(bp))
8298 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8299 
8300 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8301 }
8302 
8303 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8304 {
8305 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8306 	struct hwrm_ring_aggint_qcaps_output *resp;
8307 	struct hwrm_ring_aggint_qcaps_input *req;
8308 	int rc;
8309 
8310 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8311 	coal_cap->num_cmpl_dma_aggr_max = 63;
8312 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8313 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8314 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8315 	coal_cap->int_lat_tmr_min_max = 65535;
8316 	coal_cap->int_lat_tmr_max_max = 65535;
8317 	coal_cap->num_cmpl_aggr_int_max = 65535;
8318 	coal_cap->timer_units = 80;
8319 
8320 	if (bp->hwrm_spec_code < 0x10902)
8321 		return;
8322 
8323 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8324 		return;
8325 
8326 	resp = hwrm_req_hold(bp, req);
8327 	rc = hwrm_req_send_silent(bp, req);
8328 	if (!rc) {
8329 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8330 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8331 		coal_cap->num_cmpl_dma_aggr_max =
8332 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8333 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8334 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8335 		coal_cap->cmpl_aggr_dma_tmr_max =
8336 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8337 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8338 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8339 		coal_cap->int_lat_tmr_min_max =
8340 			le16_to_cpu(resp->int_lat_tmr_min_max);
8341 		coal_cap->int_lat_tmr_max_max =
8342 			le16_to_cpu(resp->int_lat_tmr_max_max);
8343 		coal_cap->num_cmpl_aggr_int_max =
8344 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8345 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8346 	}
8347 	hwrm_req_drop(bp, req);
8348 }
8349 
8350 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8351 {
8352 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8353 
8354 	return usec * 1000 / coal_cap->timer_units;
8355 }
8356 
8357 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8358 	struct bnxt_coal *hw_coal,
8359 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8360 {
8361 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8362 	u16 val, tmr, max, flags = hw_coal->flags;
8363 	u32 cmpl_params = coal_cap->cmpl_params;
8364 
8365 	max = hw_coal->bufs_per_record * 128;
8366 	if (hw_coal->budget)
8367 		max = hw_coal->bufs_per_record * hw_coal->budget;
8368 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8369 
8370 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8371 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8372 
8373 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8374 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8375 
8376 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8377 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8378 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8379 
8380 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8381 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8382 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8383 
8384 	/* min timer set to 1/2 of interrupt timer */
8385 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8386 		val = tmr / 2;
8387 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8388 		req->int_lat_tmr_min = cpu_to_le16(val);
8389 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8390 	}
8391 
8392 	/* buf timer set to 1/4 of interrupt timer */
8393 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8394 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8395 
8396 	if (cmpl_params &
8397 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8398 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8399 		val = clamp_t(u16, tmr, 1,
8400 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8401 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8402 		req->enables |=
8403 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8404 	}
8405 
8406 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8407 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8408 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8409 	req->flags = cpu_to_le16(flags);
8410 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8411 }
8412 
8413 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8414 				   struct bnxt_coal *hw_coal)
8415 {
8416 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8417 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8418 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8419 	u32 nq_params = coal_cap->nq_params;
8420 	u16 tmr;
8421 	int rc;
8422 
8423 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8424 		return 0;
8425 
8426 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8427 	if (rc)
8428 		return rc;
8429 
8430 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8431 	req->flags =
8432 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8433 
8434 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8435 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8436 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8437 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8438 	return hwrm_req_send(bp, req);
8439 }
8440 
8441 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8442 {
8443 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8444 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8445 	struct bnxt_coal coal;
8446 	int rc;
8447 
8448 	/* Tick values in micro seconds.
8449 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8450 	 */
8451 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8452 
8453 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8454 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8455 
8456 	if (!bnapi->rx_ring)
8457 		return -ENODEV;
8458 
8459 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8460 	if (rc)
8461 		return rc;
8462 
8463 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8464 
8465 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8466 
8467 	return hwrm_req_send(bp, req_rx);
8468 }
8469 
8470 static int
8471 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8472 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8473 {
8474 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8475 
8476 	req->ring_id = cpu_to_le16(ring_id);
8477 	return hwrm_req_send(bp, req);
8478 }
8479 
8480 static int
8481 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8482 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8483 {
8484 	struct bnxt_tx_ring_info *txr;
8485 	int i, rc;
8486 
8487 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8488 		u16 ring_id;
8489 
8490 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8491 		req->ring_id = cpu_to_le16(ring_id);
8492 		rc = hwrm_req_send(bp, req);
8493 		if (rc)
8494 			return rc;
8495 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8496 			return 0;
8497 	}
8498 	return 0;
8499 }
8500 
8501 int bnxt_hwrm_set_coal(struct bnxt *bp)
8502 {
8503 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8504 	int i, rc;
8505 
8506 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8507 	if (rc)
8508 		return rc;
8509 
8510 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8511 	if (rc) {
8512 		hwrm_req_drop(bp, req_rx);
8513 		return rc;
8514 	}
8515 
8516 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8517 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8518 
8519 	hwrm_req_hold(bp, req_rx);
8520 	hwrm_req_hold(bp, req_tx);
8521 	for (i = 0; i < bp->cp_nr_rings; i++) {
8522 		struct bnxt_napi *bnapi = bp->bnapi[i];
8523 		struct bnxt_coal *hw_coal;
8524 
8525 		if (!bnapi->rx_ring)
8526 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8527 		else
8528 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8529 		if (rc)
8530 			break;
8531 
8532 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8533 			continue;
8534 
8535 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8536 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8537 			if (rc)
8538 				break;
8539 		}
8540 		if (bnapi->rx_ring)
8541 			hw_coal = &bp->rx_coal;
8542 		else
8543 			hw_coal = &bp->tx_coal;
8544 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8545 	}
8546 	hwrm_req_drop(bp, req_rx);
8547 	hwrm_req_drop(bp, req_tx);
8548 	return rc;
8549 }
8550 
8551 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8552 {
8553 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8554 	struct hwrm_stat_ctx_free_input *req;
8555 	int i;
8556 
8557 	if (!bp->bnapi)
8558 		return;
8559 
8560 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8561 		return;
8562 
8563 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8564 		return;
8565 	if (BNXT_FW_MAJ(bp) <= 20) {
8566 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8567 			hwrm_req_drop(bp, req);
8568 			return;
8569 		}
8570 		hwrm_req_hold(bp, req0);
8571 	}
8572 	hwrm_req_hold(bp, req);
8573 	for (i = 0; i < bp->cp_nr_rings; i++) {
8574 		struct bnxt_napi *bnapi = bp->bnapi[i];
8575 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8576 
8577 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8578 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8579 			if (req0) {
8580 				req0->stat_ctx_id = req->stat_ctx_id;
8581 				hwrm_req_send(bp, req0);
8582 			}
8583 			hwrm_req_send(bp, req);
8584 
8585 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8586 		}
8587 	}
8588 	hwrm_req_drop(bp, req);
8589 	if (req0)
8590 		hwrm_req_drop(bp, req0);
8591 }
8592 
8593 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8594 {
8595 	struct hwrm_stat_ctx_alloc_output *resp;
8596 	struct hwrm_stat_ctx_alloc_input *req;
8597 	int rc, i;
8598 
8599 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8600 		return 0;
8601 
8602 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8603 	if (rc)
8604 		return rc;
8605 
8606 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8607 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8608 
8609 	resp = hwrm_req_hold(bp, req);
8610 	for (i = 0; i < bp->cp_nr_rings; i++) {
8611 		struct bnxt_napi *bnapi = bp->bnapi[i];
8612 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8613 
8614 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8615 
8616 		rc = hwrm_req_send(bp, req);
8617 		if (rc)
8618 			break;
8619 
8620 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8621 
8622 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8623 	}
8624 	hwrm_req_drop(bp, req);
8625 	return rc;
8626 }
8627 
8628 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8629 {
8630 	struct hwrm_func_qcfg_output *resp;
8631 	struct hwrm_func_qcfg_input *req;
8632 	u16 flags;
8633 	int rc;
8634 
8635 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8636 	if (rc)
8637 		return rc;
8638 
8639 	req->fid = cpu_to_le16(0xffff);
8640 	resp = hwrm_req_hold(bp, req);
8641 	rc = hwrm_req_send(bp, req);
8642 	if (rc)
8643 		goto func_qcfg_exit;
8644 
8645 	flags = le16_to_cpu(resp->flags);
8646 #ifdef CONFIG_BNXT_SRIOV
8647 	if (BNXT_VF(bp)) {
8648 		struct bnxt_vf_info *vf = &bp->vf;
8649 
8650 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8651 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8652 			vf->flags |= BNXT_VF_TRUST;
8653 		else
8654 			vf->flags &= ~BNXT_VF_TRUST;
8655 	} else {
8656 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8657 	}
8658 #endif
8659 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8660 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8661 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8662 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8663 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8664 	}
8665 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8666 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8667 
8668 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8669 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8670 
8671 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8672 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8673 	if (resp->roce_bidi_opt_mode &
8674 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8675 		bp->cos0_cos1_shared = 1;
8676 	else
8677 		bp->cos0_cos1_shared = 0;
8678 
8679 	switch (resp->port_partition_type) {
8680 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8681 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8682 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8683 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8684 		bp->port_partition_type = resp->port_partition_type;
8685 		break;
8686 	}
8687 	if (bp->hwrm_spec_code < 0x10707 ||
8688 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8689 		bp->br_mode = BRIDGE_MODE_VEB;
8690 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8691 		bp->br_mode = BRIDGE_MODE_VEPA;
8692 	else
8693 		bp->br_mode = BRIDGE_MODE_UNDEF;
8694 
8695 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8696 	if (!bp->max_mtu)
8697 		bp->max_mtu = BNXT_MAX_MTU;
8698 
8699 	if (bp->db_size)
8700 		goto func_qcfg_exit;
8701 
8702 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8703 	if (BNXT_CHIP_P5(bp)) {
8704 		if (BNXT_PF(bp))
8705 			bp->db_offset = DB_PF_OFFSET_P5;
8706 		else
8707 			bp->db_offset = DB_VF_OFFSET_P5;
8708 	}
8709 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8710 				 1024);
8711 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8712 	    bp->db_size <= bp->db_offset)
8713 		bp->db_size = pci_resource_len(bp->pdev, 2);
8714 
8715 func_qcfg_exit:
8716 	hwrm_req_drop(bp, req);
8717 	return rc;
8718 }
8719 
8720 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8721 				      u8 init_val, u8 init_offset,
8722 				      bool init_mask_set)
8723 {
8724 	ctxm->init_value = init_val;
8725 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8726 	if (init_mask_set)
8727 		ctxm->init_offset = init_offset * 4;
8728 	else
8729 		ctxm->init_value = 0;
8730 }
8731 
8732 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8733 {
8734 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8735 	u16 type;
8736 
8737 	for (type = 0; type < ctx_max; type++) {
8738 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8739 		int n = 1;
8740 
8741 		if (!ctxm->max_entries || ctxm->pg_info)
8742 			continue;
8743 
8744 		if (ctxm->instance_bmap)
8745 			n = hweight32(ctxm->instance_bmap);
8746 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8747 		if (!ctxm->pg_info)
8748 			return -ENOMEM;
8749 	}
8750 	return 0;
8751 }
8752 
8753 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8754 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8755 
8756 #define BNXT_CTX_INIT_VALID(flags)	\
8757 	(!!((flags) &			\
8758 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8759 
8760 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8761 {
8762 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8763 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8764 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8765 	u16 type, next_type = 0;
8766 	int rc;
8767 
8768 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8769 	if (rc)
8770 		return rc;
8771 
8772 	if (!ctx) {
8773 		ctx = kzalloc_obj(*ctx);
8774 		if (!ctx)
8775 			return -ENOMEM;
8776 		bp->ctx = ctx;
8777 	}
8778 
8779 	resp = hwrm_req_hold(bp, req);
8780 
8781 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8782 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8783 		u8 init_val, init_off, i;
8784 		u32 max_entries;
8785 		u16 entry_size;
8786 		__le32 *p;
8787 		u32 flags;
8788 
8789 		req->type = cpu_to_le16(type);
8790 		rc = hwrm_req_send(bp, req);
8791 		if (rc)
8792 			goto ctx_done;
8793 		flags = le32_to_cpu(resp->flags);
8794 		next_type = le16_to_cpu(resp->next_valid_type);
8795 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8796 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8797 			continue;
8798 		}
8799 		entry_size = le16_to_cpu(resp->entry_size);
8800 		max_entries = le32_to_cpu(resp->max_num_entries);
8801 		if (ctxm->mem_valid) {
8802 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8803 			    ctxm->entry_size != entry_size ||
8804 			    ctxm->max_entries != max_entries)
8805 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8806 			else
8807 				continue;
8808 		}
8809 		ctxm->type = type;
8810 		ctxm->entry_size = entry_size;
8811 		ctxm->flags = flags;
8812 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8813 		ctxm->entry_multiple = resp->entry_multiple;
8814 		ctxm->max_entries = max_entries;
8815 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8816 		init_val = resp->ctx_init_value;
8817 		init_off = resp->ctx_init_offset;
8818 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8819 					  BNXT_CTX_INIT_VALID(flags));
8820 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8821 					      BNXT_MAX_SPLIT_ENTRY);
8822 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8823 		     i++, p++)
8824 			ctxm->split[i] = le32_to_cpu(*p);
8825 	}
8826 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8827 
8828 ctx_done:
8829 	hwrm_req_drop(bp, req);
8830 	return rc;
8831 }
8832 
8833 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8834 {
8835 	struct hwrm_func_backing_store_qcaps_output *resp;
8836 	struct hwrm_func_backing_store_qcaps_input *req;
8837 	int rc;
8838 
8839 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8840 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8841 		return 0;
8842 
8843 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8844 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8845 
8846 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8847 	if (rc)
8848 		return rc;
8849 
8850 	resp = hwrm_req_hold(bp, req);
8851 	rc = hwrm_req_send_silent(bp, req);
8852 	if (!rc) {
8853 		struct bnxt_ctx_mem_type *ctxm;
8854 		struct bnxt_ctx_mem_info *ctx;
8855 		u8 init_val, init_idx = 0;
8856 		u16 init_mask;
8857 
8858 		ctx = bp->ctx;
8859 		if (!ctx) {
8860 			ctx = kzalloc_obj(*ctx);
8861 			if (!ctx) {
8862 				rc = -ENOMEM;
8863 				goto ctx_err;
8864 			}
8865 			bp->ctx = ctx;
8866 		}
8867 		init_val = resp->ctx_kind_initializer;
8868 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8869 
8870 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8871 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8872 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8873 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8874 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8875 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8876 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8877 					  (init_mask & (1 << init_idx++)) != 0);
8878 
8879 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8880 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8881 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8882 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8883 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8884 					  (init_mask & (1 << init_idx++)) != 0);
8885 
8886 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8887 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8888 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8889 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8890 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8891 					  (init_mask & (1 << init_idx++)) != 0);
8892 
8893 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8894 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8895 		ctxm->max_entries = ctxm->vnic_entries +
8896 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8897 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8898 		bnxt_init_ctx_initializer(ctxm, init_val,
8899 					  resp->vnic_init_offset,
8900 					  (init_mask & (1 << init_idx++)) != 0);
8901 
8902 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8903 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8904 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8905 		bnxt_init_ctx_initializer(ctxm, init_val,
8906 					  resp->stat_init_offset,
8907 					  (init_mask & (1 << init_idx++)) != 0);
8908 
8909 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8910 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8911 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8912 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8913 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8914 		if (!ctxm->entry_multiple)
8915 			ctxm->entry_multiple = 1;
8916 
8917 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8918 
8919 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8920 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8921 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8922 		ctxm->mrav_num_entries_units =
8923 			le16_to_cpu(resp->mrav_num_entries_units);
8924 		bnxt_init_ctx_initializer(ctxm, init_val,
8925 					  resp->mrav_init_offset,
8926 					  (init_mask & (1 << init_idx++)) != 0);
8927 
8928 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8929 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8930 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8931 
8932 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8933 		if (!ctx->tqm_fp_rings_count)
8934 			ctx->tqm_fp_rings_count = bp->max_q;
8935 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8936 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8937 
8938 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8939 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8940 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8941 
8942 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8943 	} else {
8944 		rc = 0;
8945 	}
8946 ctx_err:
8947 	hwrm_req_drop(bp, req);
8948 	return rc;
8949 }
8950 
8951 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8952 				  __le64 *pg_dir)
8953 {
8954 	if (!rmem->nr_pages)
8955 		return;
8956 
8957 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8958 	if (rmem->depth >= 1) {
8959 		if (rmem->depth == 2)
8960 			*pg_attr |= 2;
8961 		else
8962 			*pg_attr |= 1;
8963 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8964 	} else {
8965 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8966 	}
8967 }
8968 
8969 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8970 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8971 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8972 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8973 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8974 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8975 
8976 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8977 {
8978 	struct hwrm_func_backing_store_cfg_input *req;
8979 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8980 	struct bnxt_ctx_pg_info *ctx_pg;
8981 	struct bnxt_ctx_mem_type *ctxm;
8982 	void **__req = (void **)&req;
8983 	u32 req_len = sizeof(*req);
8984 	__le32 *num_entries;
8985 	__le64 *pg_dir;
8986 	u32 flags = 0;
8987 	u8 *pg_attr;
8988 	u32 ena;
8989 	int rc;
8990 	int i;
8991 
8992 	if (!ctx)
8993 		return 0;
8994 
8995 	if (req_len > bp->hwrm_max_ext_req_len)
8996 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8997 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8998 	if (rc)
8999 		return rc;
9000 
9001 	req->enables = cpu_to_le32(enables);
9002 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
9003 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9004 		ctx_pg = ctxm->pg_info;
9005 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
9006 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
9007 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
9008 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
9009 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9010 				      &req->qpc_pg_size_qpc_lvl,
9011 				      &req->qpc_page_dir);
9012 
9013 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
9014 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
9015 	}
9016 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
9017 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9018 		ctx_pg = ctxm->pg_info;
9019 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
9020 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
9021 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
9022 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9023 				      &req->srq_pg_size_srq_lvl,
9024 				      &req->srq_page_dir);
9025 	}
9026 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
9027 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9028 		ctx_pg = ctxm->pg_info;
9029 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
9030 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
9031 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
9032 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9033 				      &req->cq_pg_size_cq_lvl,
9034 				      &req->cq_page_dir);
9035 	}
9036 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
9037 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9038 		ctx_pg = ctxm->pg_info;
9039 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
9040 		req->vnic_num_ring_table_entries =
9041 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
9042 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
9043 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9044 				      &req->vnic_pg_size_vnic_lvl,
9045 				      &req->vnic_page_dir);
9046 	}
9047 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
9048 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9049 		ctx_pg = ctxm->pg_info;
9050 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
9051 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
9052 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9053 				      &req->stat_pg_size_stat_lvl,
9054 				      &req->stat_page_dir);
9055 	}
9056 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
9057 		u32 units;
9058 
9059 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9060 		ctx_pg = ctxm->pg_info;
9061 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
9062 		units = ctxm->mrav_num_entries_units;
9063 		if (units) {
9064 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
9065 			u32 entries;
9066 
9067 			num_mr = ctx_pg->entries - num_ah;
9068 			entries = ((num_mr / units) << 16) | (num_ah / units);
9069 			req->mrav_num_entries = cpu_to_le32(entries);
9070 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
9071 		}
9072 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
9073 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9074 				      &req->mrav_pg_size_mrav_lvl,
9075 				      &req->mrav_page_dir);
9076 	}
9077 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
9078 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9079 		ctx_pg = ctxm->pg_info;
9080 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
9081 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
9082 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9083 				      &req->tim_pg_size_tim_lvl,
9084 				      &req->tim_page_dir);
9085 	}
9086 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9087 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
9088 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
9089 	     pg_dir = &req->tqm_sp_page_dir,
9090 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
9091 	     ctx_pg = ctxm->pg_info;
9092 	     i < BNXT_MAX_TQM_RINGS;
9093 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
9094 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
9095 		if (!(enables & ena))
9096 			continue;
9097 
9098 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9099 		*num_entries = cpu_to_le32(ctx_pg->entries);
9100 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9101 	}
9102 	req->flags = cpu_to_le32(flags);
9103 	return hwrm_req_send(bp, req);
9104 }
9105 
9106 static int bnxt_alloc_ctx_mem_blk(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 	rmem->page_size = BNXT_PAGE_SIZE;
9112 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9113 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9114 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9115 	if (rmem->depth >= 1)
9116 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9117 	return bnxt_alloc_ring(bp, rmem);
9118 }
9119 
9120 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9121 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9122 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9123 {
9124 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9125 	int rc;
9126 
9127 	if (!mem_size)
9128 		return -EINVAL;
9129 
9130 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9131 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9132 		ctx_pg->nr_pages = 0;
9133 		return -EINVAL;
9134 	}
9135 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9136 		int nr_tbls, i;
9137 
9138 		rmem->depth = 2;
9139 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9140 		if (!ctx_pg->ctx_pg_tbl)
9141 			return -ENOMEM;
9142 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9143 		rmem->nr_pages = nr_tbls;
9144 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9145 		if (rc)
9146 			return rc;
9147 		for (i = 0; i < nr_tbls; i++) {
9148 			struct bnxt_ctx_pg_info *pg_tbl;
9149 
9150 			pg_tbl = kzalloc_obj(*pg_tbl);
9151 			if (!pg_tbl)
9152 				return -ENOMEM;
9153 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9154 			rmem = &pg_tbl->ring_mem;
9155 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9156 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9157 			rmem->depth = 1;
9158 			rmem->nr_pages = MAX_CTX_PAGES;
9159 			rmem->ctx_mem = ctxm;
9160 			if (i == (nr_tbls - 1)) {
9161 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9162 
9163 				if (rem)
9164 					rmem->nr_pages = rem;
9165 			}
9166 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9167 			if (rc)
9168 				break;
9169 		}
9170 	} else {
9171 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9172 		if (rmem->nr_pages > 1 || depth)
9173 			rmem->depth = 1;
9174 		rmem->ctx_mem = ctxm;
9175 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9176 	}
9177 	return rc;
9178 }
9179 
9180 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9181 				    struct bnxt_ctx_pg_info *ctx_pg,
9182 				    void *buf, size_t offset, size_t head,
9183 				    size_t tail)
9184 {
9185 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9186 	size_t nr_pages = ctx_pg->nr_pages;
9187 	int page_size = rmem->page_size;
9188 	size_t len = 0, total_len = 0;
9189 	u16 depth = rmem->depth;
9190 
9191 	tail %= nr_pages * page_size;
9192 	do {
9193 		if (depth > 1) {
9194 			int i = head / (page_size * MAX_CTX_PAGES);
9195 			struct bnxt_ctx_pg_info *pg_tbl;
9196 
9197 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9198 			rmem = &pg_tbl->ring_mem;
9199 		}
9200 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9201 		head += len;
9202 		offset += len;
9203 		total_len += len;
9204 		if (head >= nr_pages * page_size)
9205 			head = 0;
9206 	} while (head != tail);
9207 	return total_len;
9208 }
9209 
9210 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9211 				  struct bnxt_ctx_pg_info *ctx_pg)
9212 {
9213 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9214 
9215 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9216 	    ctx_pg->ctx_pg_tbl) {
9217 		int i, nr_tbls = rmem->nr_pages;
9218 
9219 		for (i = 0; i < nr_tbls; i++) {
9220 			struct bnxt_ctx_pg_info *pg_tbl;
9221 			struct bnxt_ring_mem_info *rmem2;
9222 
9223 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9224 			if (!pg_tbl)
9225 				continue;
9226 			rmem2 = &pg_tbl->ring_mem;
9227 			bnxt_free_ring(bp, rmem2);
9228 			ctx_pg->ctx_pg_arr[i] = NULL;
9229 			kfree(pg_tbl);
9230 			ctx_pg->ctx_pg_tbl[i] = NULL;
9231 		}
9232 		kfree(ctx_pg->ctx_pg_tbl);
9233 		ctx_pg->ctx_pg_tbl = NULL;
9234 	}
9235 	bnxt_free_ring(bp, rmem);
9236 	ctx_pg->nr_pages = 0;
9237 }
9238 
9239 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9240 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9241 				   u8 pg_lvl)
9242 {
9243 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9244 	int i, rc = 0, n = 1;
9245 	u32 mem_size;
9246 
9247 	if (!ctxm->entry_size || !ctx_pg)
9248 		return -EINVAL;
9249 	if (ctxm->instance_bmap)
9250 		n = hweight32(ctxm->instance_bmap);
9251 	if (ctxm->entry_multiple)
9252 		entries = roundup(entries, ctxm->entry_multiple);
9253 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9254 	mem_size = entries * ctxm->entry_size;
9255 	for (i = 0; i < n && !rc; i++) {
9256 		ctx_pg[i].entries = entries;
9257 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9258 					    ctxm->init_value ? ctxm : NULL);
9259 	}
9260 	if (!rc)
9261 		ctxm->mem_valid = 1;
9262 	return rc;
9263 }
9264 
9265 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9266 					       struct bnxt_ctx_mem_type *ctxm,
9267 					       bool last)
9268 {
9269 	struct hwrm_func_backing_store_cfg_v2_input *req;
9270 	u32 instance_bmap = ctxm->instance_bmap;
9271 	int i, j, rc = 0, n = 1;
9272 	__le32 *p;
9273 
9274 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9275 		return 0;
9276 
9277 	if (instance_bmap)
9278 		n = hweight32(ctxm->instance_bmap);
9279 	else
9280 		instance_bmap = 1;
9281 
9282 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9283 	if (rc)
9284 		return rc;
9285 	hwrm_req_hold(bp, req);
9286 	req->type = cpu_to_le16(ctxm->type);
9287 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9288 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9289 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9290 		struct bnxt_bs_trace_info *bs_trace;
9291 		u32 enables;
9292 
9293 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9294 		req->enables = cpu_to_le32(enables);
9295 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9296 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9297 	}
9298 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9299 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9300 		p[i] = cpu_to_le32(ctxm->split[i]);
9301 	for (i = 0, j = 0; j < n && !rc; i++) {
9302 		struct bnxt_ctx_pg_info *ctx_pg;
9303 
9304 		if (!(instance_bmap & (1 << i)))
9305 			continue;
9306 		req->instance = cpu_to_le16(i);
9307 		ctx_pg = &ctxm->pg_info[j++];
9308 		if (!ctx_pg->entries)
9309 			continue;
9310 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9311 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9312 				      &req->page_size_pbl_level,
9313 				      &req->page_dir);
9314 		if (last && j == n)
9315 			req->flags =
9316 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9317 		rc = hwrm_req_send(bp, req);
9318 	}
9319 	hwrm_req_drop(bp, req);
9320 	return rc;
9321 }
9322 
9323 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9324 {
9325 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9326 	struct bnxt_ctx_mem_type *ctxm;
9327 	u16 last_type = BNXT_CTX_INV;
9328 	int rc = 0;
9329 	u16 type;
9330 
9331 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9332 		ctxm = &ctx->ctx_arr[type];
9333 		if (!bnxt_bs_trace_avail(bp, type))
9334 			continue;
9335 		if (!ctxm->mem_valid) {
9336 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9337 						     ctxm->max_entries, 1);
9338 			if (rc) {
9339 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9340 					    type);
9341 				continue;
9342 			}
9343 			bnxt_bs_trace_init(bp, ctxm);
9344 		}
9345 		last_type = type;
9346 	}
9347 
9348 	if (last_type == BNXT_CTX_INV) {
9349 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9350 			ctxm = &ctx->ctx_arr[type];
9351 			if (ctxm->mem_valid)
9352 				last_type = type;
9353 		}
9354 		if (last_type == BNXT_CTX_INV)
9355 			return 0;
9356 	}
9357 	ctx->ctx_arr[last_type].last = 1;
9358 
9359 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9360 		ctxm = &ctx->ctx_arr[type];
9361 
9362 		if (!ctxm->mem_valid)
9363 			continue;
9364 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9365 		if (rc)
9366 			return rc;
9367 	}
9368 	return 0;
9369 }
9370 
9371 /**
9372  * __bnxt_copy_ctx_mem - copy host context memory
9373  * @bp: The driver context
9374  * @ctxm: The pointer to the context memory type
9375  * @buf: The destination buffer or NULL to just obtain the length
9376  * @offset: The buffer offset to copy the data to
9377  * @head: The head offset of context memory to copy from
9378  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9379  *
9380  * This function is called for debugging purposes to dump the host context
9381  * used by the chip.
9382  *
9383  * Return: Length of memory copied
9384  */
9385 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9386 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9387 				  size_t offset, size_t head, size_t tail)
9388 {
9389 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9390 	size_t len = 0, total_len = 0;
9391 	int i, n = 1;
9392 
9393 	if (!ctx_pg)
9394 		return 0;
9395 
9396 	if (ctxm->instance_bmap)
9397 		n = hweight32(ctxm->instance_bmap);
9398 	for (i = 0; i < n; i++) {
9399 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9400 					    tail);
9401 		offset += len;
9402 		total_len += len;
9403 	}
9404 	return total_len;
9405 }
9406 
9407 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9408 			 void *buf, size_t offset)
9409 {
9410 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9411 
9412 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9413 }
9414 
9415 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9416 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9417 {
9418 	struct bnxt_ctx_pg_info *ctx_pg;
9419 	int i, n = 1;
9420 
9421 	ctxm->last = 0;
9422 
9423 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9424 		return;
9425 
9426 	ctx_pg = ctxm->pg_info;
9427 	if (ctx_pg) {
9428 		if (ctxm->instance_bmap)
9429 			n = hweight32(ctxm->instance_bmap);
9430 		for (i = 0; i < n; i++)
9431 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9432 
9433 		kfree(ctx_pg);
9434 		ctxm->pg_info = NULL;
9435 		ctxm->mem_valid = 0;
9436 	}
9437 	memset(ctxm, 0, sizeof(*ctxm));
9438 }
9439 
9440 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9441 {
9442 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9443 	u16 type;
9444 
9445 	if (!ctx)
9446 		return;
9447 
9448 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9449 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9450 
9451 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9452 	if (force) {
9453 		kfree(ctx);
9454 		bp->ctx = NULL;
9455 	}
9456 }
9457 
9458 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9459 {
9460 	struct bnxt_ctx_mem_type *ctxm;
9461 	struct bnxt_ctx_mem_info *ctx;
9462 	u32 l2_qps, qp1_qps, max_qps;
9463 	u32 ena, entries_sp, entries;
9464 	u32 srqs, max_srqs, min;
9465 	u32 num_mr, num_ah;
9466 	u32 extra_srqs = 0;
9467 	u32 extra_qps = 0;
9468 	u32 fast_qpmd_qps;
9469 	u8 pg_lvl = 1;
9470 	int i, rc;
9471 
9472 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9473 	if (rc) {
9474 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9475 			   rc);
9476 		return rc;
9477 	}
9478 	ctx = bp->ctx;
9479 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9480 		return 0;
9481 
9482 	ena = 0;
9483 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9484 		goto skip_legacy;
9485 
9486 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9487 	l2_qps = ctxm->qp_l2_entries;
9488 	qp1_qps = ctxm->qp_qp1_entries;
9489 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9490 	max_qps = ctxm->max_entries;
9491 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9492 	srqs = ctxm->srq_l2_entries;
9493 	max_srqs = ctxm->max_entries;
9494 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9495 		pg_lvl = 2;
9496 		if (BNXT_SW_RES_LMT(bp)) {
9497 			extra_qps = max_qps - l2_qps - qp1_qps;
9498 			extra_srqs = max_srqs - srqs;
9499 		} else {
9500 			extra_qps = min_t(u32, 65536,
9501 					  max_qps - l2_qps - qp1_qps);
9502 			/* allocate extra qps if fw supports RoCE fast qp
9503 			 * destroy feature
9504 			 */
9505 			extra_qps += fast_qpmd_qps;
9506 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9507 		}
9508 		if (fast_qpmd_qps)
9509 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9510 	}
9511 
9512 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9513 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9514 				     pg_lvl);
9515 	if (rc)
9516 		return rc;
9517 
9518 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9519 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9520 	if (rc)
9521 		return rc;
9522 
9523 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9524 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9525 				     extra_qps * 2, pg_lvl);
9526 	if (rc)
9527 		return rc;
9528 
9529 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9530 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9531 	if (rc)
9532 		return rc;
9533 
9534 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9535 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9536 	if (rc)
9537 		return rc;
9538 
9539 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9540 		goto skip_rdma;
9541 
9542 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9543 	if (BNXT_SW_RES_LMT(bp) &&
9544 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9545 		num_ah = ctxm->mrav_av_entries;
9546 		num_mr = ctxm->max_entries - num_ah;
9547 	} else {
9548 		/* 128K extra is needed to accommodate static AH context
9549 		 * allocation by f/w.
9550 		 */
9551 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9552 		num_ah = min_t(u32, num_mr, 1024 * 128);
9553 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9554 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9555 			ctxm->mrav_av_entries = num_ah;
9556 	}
9557 
9558 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9559 	if (rc)
9560 		return rc;
9561 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9562 
9563 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9564 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9565 	if (rc)
9566 		return rc;
9567 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9568 
9569 skip_rdma:
9570 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9571 	min = ctxm->min_entries;
9572 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9573 		     2 * (extra_qps + qp1_qps) + min;
9574 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9575 	if (rc)
9576 		return rc;
9577 
9578 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9579 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9580 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9581 	if (rc)
9582 		return rc;
9583 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9584 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9585 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9586 
9587 skip_legacy:
9588 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9589 		rc = bnxt_backing_store_cfg_v2(bp);
9590 	else
9591 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9592 	if (rc) {
9593 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9594 			   rc);
9595 		return rc;
9596 	}
9597 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9598 	return 0;
9599 }
9600 
9601 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9602 {
9603 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9604 	u16 page_attr;
9605 	int rc;
9606 
9607 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9608 		return 0;
9609 
9610 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9611 	if (rc)
9612 		return rc;
9613 
9614 	if (BNXT_PAGE_SIZE == 0x2000)
9615 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9616 	else if (BNXT_PAGE_SIZE == 0x10000)
9617 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9618 	else
9619 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9620 	req->pg_size_lvl = cpu_to_le16(page_attr |
9621 				       bp->fw_crash_mem->ring_mem.depth);
9622 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9623 	req->size = cpu_to_le32(bp->fw_crash_len);
9624 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9625 	return hwrm_req_send(bp, req);
9626 }
9627 
9628 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9629 {
9630 	if (bp->fw_crash_mem) {
9631 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9632 		kfree(bp->fw_crash_mem);
9633 		bp->fw_crash_mem = NULL;
9634 	}
9635 }
9636 
9637 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9638 {
9639 	u32 mem_size = 0;
9640 	int rc;
9641 
9642 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9643 		return 0;
9644 
9645 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9646 	if (rc)
9647 		return rc;
9648 
9649 	mem_size = round_up(mem_size, 4);
9650 
9651 	/* keep and use the existing pages */
9652 	if (bp->fw_crash_mem &&
9653 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9654 		goto alloc_done;
9655 
9656 	if (bp->fw_crash_mem)
9657 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9658 	else
9659 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9660 	if (!bp->fw_crash_mem)
9661 		return -ENOMEM;
9662 
9663 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9664 	if (rc) {
9665 		bnxt_free_crash_dump_mem(bp);
9666 		return rc;
9667 	}
9668 
9669 alloc_done:
9670 	bp->fw_crash_len = mem_size;
9671 	return 0;
9672 }
9673 
9674 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9675 {
9676 	struct hwrm_func_resource_qcaps_output *resp;
9677 	struct hwrm_func_resource_qcaps_input *req;
9678 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9679 	int rc;
9680 
9681 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9682 	if (rc)
9683 		return rc;
9684 
9685 	req->fid = cpu_to_le16(0xffff);
9686 	resp = hwrm_req_hold(bp, req);
9687 	rc = hwrm_req_send_silent(bp, req);
9688 	if (rc)
9689 		goto hwrm_func_resc_qcaps_exit;
9690 
9691 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9692 	if (!all)
9693 		goto hwrm_func_resc_qcaps_exit;
9694 
9695 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9696 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9697 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9698 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9699 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9700 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9701 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9702 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9703 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9704 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9705 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9706 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9707 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9708 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9709 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9710 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9711 
9712 	if (hw_resc->max_rsscos_ctxs >=
9713 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9714 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9715 
9716 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9717 		u16 max_msix = le16_to_cpu(resp->max_msix);
9718 
9719 		hw_resc->max_nqs = max_msix;
9720 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9721 	}
9722 
9723 	if (BNXT_PF(bp)) {
9724 		struct bnxt_pf_info *pf = &bp->pf;
9725 
9726 		pf->vf_resv_strategy =
9727 			le16_to_cpu(resp->vf_reservation_strategy);
9728 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9729 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9730 	}
9731 hwrm_func_resc_qcaps_exit:
9732 	hwrm_req_drop(bp, req);
9733 	return rc;
9734 }
9735 
9736 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9737 {
9738 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9739 	struct hwrm_port_mac_ptp_qcfg_input *req;
9740 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9741 	u8 flags;
9742 	int rc;
9743 
9744 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9745 		rc = -ENODEV;
9746 		goto no_ptp;
9747 	}
9748 
9749 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9750 	if (rc)
9751 		goto no_ptp;
9752 
9753 	req->port_id = cpu_to_le16(bp->pf.port_id);
9754 	resp = hwrm_req_hold(bp, req);
9755 	rc = hwrm_req_send(bp, req);
9756 	if (rc)
9757 		goto exit;
9758 
9759 	flags = resp->flags;
9760 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9761 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9762 		rc = -ENODEV;
9763 		goto exit;
9764 	}
9765 	if (!ptp) {
9766 		ptp = kzalloc_obj(*ptp);
9767 		if (!ptp) {
9768 			rc = -ENOMEM;
9769 			goto exit;
9770 		}
9771 		ptp->bp = bp;
9772 		bp->ptp_cfg = ptp;
9773 	}
9774 
9775 	if (flags &
9776 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9777 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9778 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9779 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9780 	} else if (BNXT_CHIP_P5(bp)) {
9781 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9782 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9783 	} else {
9784 		rc = -ENODEV;
9785 		goto exit;
9786 	}
9787 	ptp->rtc_configured =
9788 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9789 	rc = bnxt_ptp_init(bp);
9790 	if (rc)
9791 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9792 exit:
9793 	hwrm_req_drop(bp, req);
9794 	if (!rc)
9795 		return 0;
9796 
9797 no_ptp:
9798 	bnxt_ptp_clear(bp);
9799 	kfree(ptp);
9800 	bp->ptp_cfg = NULL;
9801 	return rc;
9802 }
9803 
9804 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9805 {
9806 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9807 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9808 	struct hwrm_func_qcaps_output *resp;
9809 	struct hwrm_func_qcaps_input *req;
9810 	int rc;
9811 
9812 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9813 	if (rc)
9814 		return rc;
9815 
9816 	req->fid = cpu_to_le16(0xffff);
9817 	resp = hwrm_req_hold(bp, req);
9818 	rc = hwrm_req_send(bp, req);
9819 	if (rc)
9820 		goto hwrm_func_qcaps_exit;
9821 
9822 	flags = le32_to_cpu(resp->flags);
9823 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9824 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9825 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9826 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9827 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9828 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9829 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9830 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9831 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9832 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9833 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9834 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9835 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9836 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9837 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9838 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9839 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9840 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9841 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9842 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9843 
9844 	flags_ext = le32_to_cpu(resp->flags_ext);
9845 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9846 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9847 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9848 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9849 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9850 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9851 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9852 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9853 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9854 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9855 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9856 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9857 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9858 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9859 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9860 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9861 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9862 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9863 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9864 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9865 
9866 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9867 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9868 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9869 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9870 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9871 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9872 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9873 	if (flags_ext2 &
9874 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9875 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9876 	if (BNXT_PF(bp) &&
9877 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9878 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9879 
9880 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9881 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9882 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9883 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9884 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9885 
9886 	bp->tx_push_thresh = 0;
9887 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9888 	    BNXT_FW_MAJ(bp) > 217)
9889 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9890 
9891 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9892 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9893 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9894 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9895 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9896 	if (!hw_resc->max_hw_ring_grps)
9897 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9898 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9899 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9900 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9901 
9902 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9903 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9904 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9905 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9906 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9907 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9908 
9909 	if (BNXT_PF(bp)) {
9910 		struct bnxt_pf_info *pf = &bp->pf;
9911 
9912 		pf->fw_fid = le16_to_cpu(resp->fid);
9913 		pf->port_id = le16_to_cpu(resp->port_id);
9914 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9915 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9916 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9917 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9918 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9919 			bp->flags |= BNXT_FLAG_WOL_CAP;
9920 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9921 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9922 		} else {
9923 			bnxt_ptp_clear(bp);
9924 			kfree(bp->ptp_cfg);
9925 			bp->ptp_cfg = NULL;
9926 		}
9927 	} else {
9928 #ifdef CONFIG_BNXT_SRIOV
9929 		struct bnxt_vf_info *vf = &bp->vf;
9930 
9931 		vf->fw_fid = le16_to_cpu(resp->fid);
9932 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9933 #endif
9934 	}
9935 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9936 
9937 hwrm_func_qcaps_exit:
9938 	hwrm_req_drop(bp, req);
9939 	return rc;
9940 }
9941 
9942 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9943 {
9944 	struct hwrm_dbg_qcaps_output *resp;
9945 	struct hwrm_dbg_qcaps_input *req;
9946 	int rc;
9947 
9948 	bp->fw_dbg_cap = 0;
9949 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9950 		return;
9951 
9952 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9953 	if (rc)
9954 		return;
9955 
9956 	req->fid = cpu_to_le16(0xffff);
9957 	resp = hwrm_req_hold(bp, req);
9958 	rc = hwrm_req_send(bp, req);
9959 	if (rc)
9960 		goto hwrm_dbg_qcaps_exit;
9961 
9962 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9963 
9964 hwrm_dbg_qcaps_exit:
9965 	hwrm_req_drop(bp, req);
9966 }
9967 
9968 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9969 
9970 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9971 {
9972 	int rc;
9973 
9974 	rc = __bnxt_hwrm_func_qcaps(bp);
9975 	if (rc)
9976 		return rc;
9977 
9978 	bnxt_hwrm_dbg_qcaps(bp);
9979 
9980 	rc = bnxt_hwrm_queue_qportcfg(bp);
9981 	if (rc) {
9982 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9983 		return rc;
9984 	}
9985 	if (bp->hwrm_spec_code >= 0x10803) {
9986 		rc = bnxt_alloc_ctx_mem(bp);
9987 		if (rc)
9988 			return rc;
9989 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9990 		if (!rc)
9991 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9992 	}
9993 	return 0;
9994 }
9995 
9996 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9997 {
9998 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9999 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
10000 	u32 flags;
10001 	int rc;
10002 
10003 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
10004 		return 0;
10005 
10006 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
10007 	if (rc)
10008 		return rc;
10009 
10010 	resp = hwrm_req_hold(bp, req);
10011 	rc = hwrm_req_send(bp, req);
10012 	if (rc)
10013 		goto hwrm_cfa_adv_qcaps_exit;
10014 
10015 	flags = le32_to_cpu(resp->flags);
10016 	if (flags &
10017 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
10018 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
10019 
10020 	if (flags &
10021 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
10022 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
10023 
10024 	if (flags &
10025 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
10026 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
10027 
10028 hwrm_cfa_adv_qcaps_exit:
10029 	hwrm_req_drop(bp, req);
10030 	return rc;
10031 }
10032 
10033 static int __bnxt_alloc_fw_health(struct bnxt *bp)
10034 {
10035 	if (bp->fw_health)
10036 		return 0;
10037 
10038 	bp->fw_health = kzalloc_obj(*bp->fw_health);
10039 	if (!bp->fw_health)
10040 		return -ENOMEM;
10041 
10042 	mutex_init(&bp->fw_health->lock);
10043 	return 0;
10044 }
10045 
10046 static int bnxt_alloc_fw_health(struct bnxt *bp)
10047 {
10048 	int rc;
10049 
10050 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10051 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10052 		return 0;
10053 
10054 	rc = __bnxt_alloc_fw_health(bp);
10055 	if (rc) {
10056 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10057 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10058 		return rc;
10059 	}
10060 
10061 	return 0;
10062 }
10063 
10064 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
10065 {
10066 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
10067 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
10068 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
10069 }
10070 
10071 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
10072 {
10073 	struct bnxt_fw_health *fw_health = bp->fw_health;
10074 	u32 reg_type;
10075 
10076 	if (!fw_health)
10077 		return;
10078 
10079 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
10080 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10081 		fw_health->status_reliable = false;
10082 
10083 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
10084 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10085 		fw_health->resets_reliable = false;
10086 }
10087 
10088 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
10089 {
10090 	void __iomem *hs;
10091 	u32 status_loc;
10092 	u32 reg_type;
10093 	u32 sig;
10094 
10095 	if (bp->fw_health)
10096 		bp->fw_health->status_reliable = false;
10097 
10098 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10099 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10100 
10101 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10102 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10103 		if (!bp->chip_num) {
10104 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10105 			bp->chip_num = readl(bp->bar0 +
10106 					     BNXT_FW_HEALTH_WIN_BASE +
10107 					     BNXT_GRC_REG_CHIP_NUM);
10108 		}
10109 		if (!BNXT_CHIP_P5_PLUS(bp))
10110 			return;
10111 
10112 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10113 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10114 	} else {
10115 		status_loc = readl(hs + offsetof(struct hcomm_status,
10116 						 fw_status_loc));
10117 	}
10118 
10119 	if (__bnxt_alloc_fw_health(bp)) {
10120 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10121 		return;
10122 	}
10123 
10124 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10125 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10126 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10127 		__bnxt_map_fw_health_reg(bp, status_loc);
10128 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10129 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10130 	}
10131 
10132 	bp->fw_health->status_reliable = true;
10133 }
10134 
10135 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10136 {
10137 	struct bnxt_fw_health *fw_health = bp->fw_health;
10138 	u32 reg_base = 0xffffffff;
10139 	int i;
10140 
10141 	bp->fw_health->status_reliable = false;
10142 	bp->fw_health->resets_reliable = false;
10143 	/* Only pre-map the monitoring GRC registers using window 3 */
10144 	for (i = 0; i < 4; i++) {
10145 		u32 reg = fw_health->regs[i];
10146 
10147 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10148 			continue;
10149 		if (reg_base == 0xffffffff)
10150 			reg_base = reg & BNXT_GRC_BASE_MASK;
10151 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10152 			return -ERANGE;
10153 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10154 	}
10155 	bp->fw_health->status_reliable = true;
10156 	bp->fw_health->resets_reliable = true;
10157 	if (reg_base == 0xffffffff)
10158 		return 0;
10159 
10160 	__bnxt_map_fw_health_reg(bp, reg_base);
10161 	return 0;
10162 }
10163 
10164 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10165 {
10166 	if (!bp->fw_health)
10167 		return;
10168 
10169 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10170 		bp->fw_health->status_reliable = true;
10171 		bp->fw_health->resets_reliable = true;
10172 	} else {
10173 		bnxt_try_map_fw_health_reg(bp);
10174 	}
10175 }
10176 
10177 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10178 {
10179 	struct bnxt_fw_health *fw_health = bp->fw_health;
10180 	struct hwrm_error_recovery_qcfg_output *resp;
10181 	struct hwrm_error_recovery_qcfg_input *req;
10182 	int rc, i;
10183 
10184 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10185 		return 0;
10186 
10187 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10188 	if (rc)
10189 		return rc;
10190 
10191 	resp = hwrm_req_hold(bp, req);
10192 	rc = hwrm_req_send(bp, req);
10193 	if (rc)
10194 		goto err_recovery_out;
10195 	fw_health->flags = le32_to_cpu(resp->flags);
10196 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10197 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10198 		rc = -EINVAL;
10199 		goto err_recovery_out;
10200 	}
10201 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10202 	fw_health->master_func_wait_dsecs =
10203 		le32_to_cpu(resp->master_func_wait_period);
10204 	fw_health->normal_func_wait_dsecs =
10205 		le32_to_cpu(resp->normal_func_wait_period);
10206 	fw_health->post_reset_wait_dsecs =
10207 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10208 	fw_health->post_reset_max_wait_dsecs =
10209 		le32_to_cpu(resp->max_bailout_time_after_reset);
10210 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10211 		le32_to_cpu(resp->fw_health_status_reg);
10212 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10213 		le32_to_cpu(resp->fw_heartbeat_reg);
10214 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10215 		le32_to_cpu(resp->fw_reset_cnt_reg);
10216 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10217 		le32_to_cpu(resp->reset_inprogress_reg);
10218 	fw_health->fw_reset_inprog_reg_mask =
10219 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10220 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10221 	if (fw_health->fw_reset_seq_cnt >= 16) {
10222 		rc = -EINVAL;
10223 		goto err_recovery_out;
10224 	}
10225 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10226 		fw_health->fw_reset_seq_regs[i] =
10227 			le32_to_cpu(resp->reset_reg[i]);
10228 		fw_health->fw_reset_seq_vals[i] =
10229 			le32_to_cpu(resp->reset_reg_val[i]);
10230 		fw_health->fw_reset_seq_delay_msec[i] =
10231 			resp->delay_after_reset[i];
10232 	}
10233 err_recovery_out:
10234 	hwrm_req_drop(bp, req);
10235 	if (!rc)
10236 		rc = bnxt_map_fw_health_regs(bp);
10237 	if (rc)
10238 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10239 	return rc;
10240 }
10241 
10242 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10243 {
10244 	struct hwrm_func_reset_input *req;
10245 	int rc;
10246 
10247 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10248 	if (rc)
10249 		return rc;
10250 
10251 	req->enables = 0;
10252 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10253 	return hwrm_req_send(bp, req);
10254 }
10255 
10256 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10257 {
10258 	struct hwrm_nvm_get_dev_info_output nvm_info;
10259 
10260 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10261 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10262 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10263 			 nvm_info.nvm_cfg_ver_upd);
10264 }
10265 
10266 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10267 {
10268 	struct hwrm_queue_qportcfg_output *resp;
10269 	struct hwrm_queue_qportcfg_input *req;
10270 	u8 i, j, *qptr;
10271 	bool no_rdma;
10272 	int rc = 0;
10273 
10274 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10275 	if (rc)
10276 		return rc;
10277 
10278 	resp = hwrm_req_hold(bp, req);
10279 	rc = hwrm_req_send(bp, req);
10280 	if (rc)
10281 		goto qportcfg_exit;
10282 
10283 	if (!resp->max_configurable_queues) {
10284 		rc = -EINVAL;
10285 		goto qportcfg_exit;
10286 	}
10287 	bp->max_tc = resp->max_configurable_queues;
10288 	bp->max_lltc = resp->max_configurable_lossless_queues;
10289 	if (bp->max_tc > BNXT_MAX_QUEUE)
10290 		bp->max_tc = BNXT_MAX_QUEUE;
10291 
10292 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10293 	qptr = &resp->queue_id0;
10294 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10295 		bp->q_info[j].queue_id = *qptr;
10296 		bp->q_ids[i] = *qptr++;
10297 		bp->q_info[j].queue_profile = *qptr++;
10298 		bp->tc_to_qidx[j] = j;
10299 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10300 		    (no_rdma && BNXT_PF(bp)))
10301 			j++;
10302 	}
10303 	bp->max_q = bp->max_tc;
10304 	bp->max_tc = max_t(u8, j, 1);
10305 
10306 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10307 		bp->max_tc = 1;
10308 
10309 	if (bp->max_lltc > bp->max_tc)
10310 		bp->max_lltc = bp->max_tc;
10311 
10312 qportcfg_exit:
10313 	hwrm_req_drop(bp, req);
10314 	return rc;
10315 }
10316 
10317 static int bnxt_hwrm_poll(struct bnxt *bp)
10318 {
10319 	struct hwrm_ver_get_input *req;
10320 	int rc;
10321 
10322 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10323 	if (rc)
10324 		return rc;
10325 
10326 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10327 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10328 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10329 
10330 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10331 	rc = hwrm_req_send(bp, req);
10332 	return rc;
10333 }
10334 
10335 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10336 {
10337 	struct hwrm_ver_get_output *resp;
10338 	struct hwrm_ver_get_input *req;
10339 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10340 	u32 dev_caps_cfg, hwrm_ver;
10341 	int rc, len, max_tmo_secs;
10342 
10343 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10344 	if (rc)
10345 		return rc;
10346 
10347 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10348 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10349 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10350 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10351 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10352 
10353 	resp = hwrm_req_hold(bp, req);
10354 	rc = hwrm_req_send(bp, req);
10355 	if (rc)
10356 		goto hwrm_ver_get_exit;
10357 
10358 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10359 
10360 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10361 			     resp->hwrm_intf_min_8b << 8 |
10362 			     resp->hwrm_intf_upd_8b;
10363 	if (resp->hwrm_intf_maj_8b < 1) {
10364 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10365 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10366 			    resp->hwrm_intf_upd_8b);
10367 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10368 	}
10369 
10370 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10371 			HWRM_VERSION_UPDATE;
10372 
10373 	if (bp->hwrm_spec_code > hwrm_ver)
10374 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10375 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10376 			 HWRM_VERSION_UPDATE);
10377 	else
10378 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10379 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10380 			 resp->hwrm_intf_upd_8b);
10381 
10382 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10383 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10384 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10385 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10386 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10387 		len = FW_VER_STR_LEN;
10388 	} else {
10389 		fw_maj = resp->hwrm_fw_maj_8b;
10390 		fw_min = resp->hwrm_fw_min_8b;
10391 		fw_bld = resp->hwrm_fw_bld_8b;
10392 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10393 		len = BC_HWRM_STR_LEN;
10394 	}
10395 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10396 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10397 		 fw_rsv);
10398 
10399 	if (strlen(resp->active_pkg_name)) {
10400 		int fw_ver_len = strlen(bp->fw_ver_str);
10401 
10402 		snprintf(bp->fw_ver_str + fw_ver_len,
10403 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10404 			 resp->active_pkg_name);
10405 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10406 	}
10407 
10408 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10409 	if (!bp->hwrm_cmd_timeout)
10410 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10411 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10412 	if (!bp->hwrm_cmd_max_timeout)
10413 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10414 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10415 #ifdef CONFIG_DETECT_HUNG_TASK
10416 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10417 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10418 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10419 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10420 	}
10421 #endif
10422 
10423 	if (resp->hwrm_intf_maj_8b >= 1) {
10424 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10425 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10426 	}
10427 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10428 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10429 
10430 	bp->chip_num = le16_to_cpu(resp->chip_num);
10431 	bp->chip_rev = resp->chip_rev;
10432 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10433 	    !resp->chip_metal)
10434 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10435 
10436 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10437 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10438 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10439 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10440 
10441 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10442 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10443 
10444 	if (dev_caps_cfg &
10445 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10446 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10447 
10448 	if (dev_caps_cfg &
10449 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10450 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10451 
10452 	if (dev_caps_cfg &
10453 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10454 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10455 
10456 hwrm_ver_get_exit:
10457 	hwrm_req_drop(bp, req);
10458 	return rc;
10459 }
10460 
10461 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10462 {
10463 	struct hwrm_fw_set_time_input *req;
10464 	struct tm tm;
10465 	time64_t now = ktime_get_real_seconds();
10466 	int rc;
10467 
10468 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10469 	    bp->hwrm_spec_code < 0x10400)
10470 		return -EOPNOTSUPP;
10471 
10472 	time64_to_tm(now, 0, &tm);
10473 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10474 	if (rc)
10475 		return rc;
10476 
10477 	req->year = cpu_to_le16(1900 + tm.tm_year);
10478 	req->month = 1 + tm.tm_mon;
10479 	req->day = tm.tm_mday;
10480 	req->hour = tm.tm_hour;
10481 	req->minute = tm.tm_min;
10482 	req->second = tm.tm_sec;
10483 	return hwrm_req_send(bp, req);
10484 }
10485 
10486 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10487 {
10488 	u64 sw_tmp;
10489 
10490 	hw &= mask;
10491 	sw_tmp = (*sw & ~mask) | hw;
10492 	if (hw < (*sw & mask))
10493 		sw_tmp += mask + 1;
10494 	WRITE_ONCE(*sw, sw_tmp);
10495 }
10496 
10497 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10498 				    int count, bool ignore_zero)
10499 {
10500 	int i;
10501 
10502 	for (i = 0; i < count; i++) {
10503 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10504 
10505 		if (ignore_zero && !hw)
10506 			continue;
10507 
10508 		if (masks[i] == -1ULL)
10509 			sw_stats[i] = hw;
10510 		else
10511 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10512 	}
10513 }
10514 
10515 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10516 {
10517 	if (!stats->hw_stats)
10518 		return;
10519 
10520 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10521 				stats->hw_masks, stats->len / 8, false);
10522 }
10523 
10524 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10525 {
10526 	struct bnxt_stats_mem *ring0_stats;
10527 	bool ignore_zero = false;
10528 	int i;
10529 
10530 	/* Chip bug.  Counter intermittently becomes 0. */
10531 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10532 		ignore_zero = true;
10533 
10534 	for (i = 0; i < bp->cp_nr_rings; i++) {
10535 		struct bnxt_napi *bnapi = bp->bnapi[i];
10536 		struct bnxt_cp_ring_info *cpr;
10537 		struct bnxt_stats_mem *stats;
10538 
10539 		cpr = &bnapi->cp_ring;
10540 		stats = &cpr->stats;
10541 		if (!i)
10542 			ring0_stats = stats;
10543 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10544 					ring0_stats->hw_masks,
10545 					ring0_stats->len / 8, ignore_zero);
10546 	}
10547 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10548 		struct bnxt_stats_mem *stats = &bp->port_stats;
10549 		__le64 *hw_stats = stats->hw_stats;
10550 		u64 *sw_stats = stats->sw_stats;
10551 		u64 *masks = stats->hw_masks;
10552 		int cnt;
10553 
10554 		cnt = sizeof(struct rx_port_stats) / 8;
10555 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10556 
10557 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10558 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10559 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10560 		cnt = sizeof(struct tx_port_stats) / 8;
10561 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10562 	}
10563 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10564 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10565 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10566 	}
10567 }
10568 
10569 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10570 {
10571 	struct hwrm_port_qstats_input *req;
10572 	struct bnxt_pf_info *pf = &bp->pf;
10573 	int rc;
10574 
10575 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10576 		return 0;
10577 
10578 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10579 		return -EOPNOTSUPP;
10580 
10581 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10582 	if (rc)
10583 		return rc;
10584 
10585 	req->flags = flags;
10586 	req->port_id = cpu_to_le16(pf->port_id);
10587 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10588 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10589 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10590 	return hwrm_req_send(bp, req);
10591 }
10592 
10593 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10594 {
10595 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10596 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10597 	struct hwrm_port_qstats_ext_output *resp_qs;
10598 	struct hwrm_port_qstats_ext_input *req_qs;
10599 	struct bnxt_pf_info *pf = &bp->pf;
10600 	u32 tx_stat_size;
10601 	int rc;
10602 
10603 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10604 		return 0;
10605 
10606 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10607 		return -EOPNOTSUPP;
10608 
10609 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10610 	if (rc)
10611 		return rc;
10612 
10613 	req_qs->flags = flags;
10614 	req_qs->port_id = cpu_to_le16(pf->port_id);
10615 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10616 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10617 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10618 		       sizeof(struct tx_port_stats_ext) : 0;
10619 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10620 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10621 	resp_qs = hwrm_req_hold(bp, req_qs);
10622 	rc = hwrm_req_send(bp, req_qs);
10623 	if (!rc) {
10624 		bp->fw_rx_stats_ext_size =
10625 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10626 		if (BNXT_FW_MAJ(bp) < 220 &&
10627 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10628 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10629 
10630 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10631 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10632 	} else {
10633 		bp->fw_rx_stats_ext_size = 0;
10634 		bp->fw_tx_stats_ext_size = 0;
10635 	}
10636 	hwrm_req_drop(bp, req_qs);
10637 
10638 	if (flags)
10639 		return rc;
10640 
10641 	if (bp->fw_tx_stats_ext_size <=
10642 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10643 		bp->pri2cos_valid = 0;
10644 		return rc;
10645 	}
10646 
10647 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10648 	if (rc)
10649 		return rc;
10650 
10651 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10652 
10653 	resp_qc = hwrm_req_hold(bp, req_qc);
10654 	rc = hwrm_req_send(bp, req_qc);
10655 	if (!rc) {
10656 		u8 *pri2cos;
10657 		int i, j;
10658 
10659 		pri2cos = &resp_qc->pri0_cos_queue_id;
10660 		for (i = 0; i < 8; i++) {
10661 			u8 queue_id = pri2cos[i];
10662 			u8 queue_idx;
10663 
10664 			/* Per port queue IDs start from 0, 10, 20, etc */
10665 			queue_idx = queue_id % 10;
10666 			if (queue_idx > BNXT_MAX_QUEUE) {
10667 				bp->pri2cos_valid = false;
10668 				hwrm_req_drop(bp, req_qc);
10669 				return rc;
10670 			}
10671 			for (j = 0; j < bp->max_q; j++) {
10672 				if (bp->q_ids[j] == queue_id)
10673 					bp->pri2cos_idx[i] = queue_idx;
10674 			}
10675 		}
10676 		bp->pri2cos_valid = true;
10677 	}
10678 	hwrm_req_drop(bp, req_qc);
10679 
10680 	return rc;
10681 }
10682 
10683 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10684 {
10685 	bnxt_hwrm_tunnel_dst_port_free(bp,
10686 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10687 	bnxt_hwrm_tunnel_dst_port_free(bp,
10688 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10689 }
10690 
10691 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10692 {
10693 	int rc, i;
10694 	u32 tpa_flags = 0;
10695 
10696 	if (set_tpa)
10697 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10698 	else if (BNXT_NO_FW_ACCESS(bp))
10699 		return 0;
10700 	for (i = 0; i < bp->nr_vnics; i++) {
10701 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10702 		if (rc) {
10703 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10704 				   i, rc);
10705 			return rc;
10706 		}
10707 	}
10708 	return 0;
10709 }
10710 
10711 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10712 {
10713 	int i;
10714 
10715 	for (i = 0; i < bp->nr_vnics; i++)
10716 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10717 }
10718 
10719 static void bnxt_clear_vnic(struct bnxt *bp)
10720 {
10721 	if (!bp->vnic_info)
10722 		return;
10723 
10724 	bnxt_hwrm_clear_vnic_filter(bp);
10725 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10726 		/* clear all RSS setting before free vnic ctx */
10727 		bnxt_hwrm_clear_vnic_rss(bp);
10728 		bnxt_hwrm_vnic_ctx_free(bp);
10729 	}
10730 	/* before free the vnic, undo the vnic tpa settings */
10731 	if (bp->flags & BNXT_FLAG_TPA)
10732 		bnxt_set_tpa(bp, false);
10733 	bnxt_hwrm_vnic_free(bp);
10734 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10735 		bnxt_hwrm_vnic_ctx_free(bp);
10736 }
10737 
10738 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10739 				    bool irq_re_init)
10740 {
10741 	bnxt_clear_vnic(bp);
10742 	bnxt_hwrm_ring_free(bp, close_path);
10743 	bnxt_hwrm_ring_grp_free(bp);
10744 	if (irq_re_init) {
10745 		bnxt_hwrm_stat_ctx_free(bp);
10746 		bnxt_hwrm_free_tunnel_ports(bp);
10747 	}
10748 }
10749 
10750 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10751 {
10752 	struct hwrm_func_cfg_input *req;
10753 	u8 evb_mode;
10754 	int rc;
10755 
10756 	if (br_mode == BRIDGE_MODE_VEB)
10757 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10758 	else if (br_mode == BRIDGE_MODE_VEPA)
10759 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10760 	else
10761 		return -EINVAL;
10762 
10763 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10764 	if (rc)
10765 		return rc;
10766 
10767 	req->fid = cpu_to_le16(0xffff);
10768 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10769 	req->evb_mode = evb_mode;
10770 	return hwrm_req_send(bp, req);
10771 }
10772 
10773 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10774 {
10775 	struct hwrm_func_cfg_input *req;
10776 	int rc;
10777 
10778 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10779 		return 0;
10780 
10781 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10782 	if (rc)
10783 		return rc;
10784 
10785 	req->fid = cpu_to_le16(0xffff);
10786 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10787 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10788 	if (size == 128)
10789 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10790 
10791 	return hwrm_req_send(bp, req);
10792 }
10793 
10794 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10795 {
10796 	int rc;
10797 
10798 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10799 		goto skip_rss_ctx;
10800 
10801 	/* allocate context for vnic */
10802 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10803 	if (rc) {
10804 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10805 			   vnic->vnic_id, rc);
10806 		goto vnic_setup_err;
10807 	}
10808 	bp->rsscos_nr_ctxs++;
10809 
10810 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10811 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10812 		if (rc) {
10813 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10814 				   vnic->vnic_id, rc);
10815 			goto vnic_setup_err;
10816 		}
10817 		bp->rsscos_nr_ctxs++;
10818 	}
10819 
10820 skip_rss_ctx:
10821 	/* configure default vnic, ring grp */
10822 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10823 	if (rc) {
10824 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10825 			   vnic->vnic_id, rc);
10826 		goto vnic_setup_err;
10827 	}
10828 
10829 	/* Enable RSS hashing on vnic */
10830 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10831 	if (rc) {
10832 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10833 			   vnic->vnic_id, rc);
10834 		goto vnic_setup_err;
10835 	}
10836 
10837 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10838 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10839 		if (rc) {
10840 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10841 				   vnic->vnic_id, rc);
10842 		}
10843 	}
10844 
10845 vnic_setup_err:
10846 	return rc;
10847 }
10848 
10849 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10850 			  u8 valid)
10851 {
10852 	struct hwrm_vnic_update_input *req;
10853 	int rc;
10854 
10855 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10856 	if (rc)
10857 		return rc;
10858 
10859 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10860 
10861 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10862 		req->mru = cpu_to_le16(vnic->mru);
10863 
10864 	req->enables = cpu_to_le32(valid);
10865 
10866 	return hwrm_req_send(bp, req);
10867 }
10868 
10869 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10870 {
10871 	int rc;
10872 
10873 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10874 	if (rc) {
10875 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10876 			   vnic->vnic_id, rc);
10877 		return rc;
10878 	}
10879 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10880 	if (rc)
10881 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10882 			   vnic->vnic_id, rc);
10883 	return rc;
10884 }
10885 
10886 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10887 {
10888 	int rc, i, nr_ctxs;
10889 
10890 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10891 	for (i = 0; i < nr_ctxs; i++) {
10892 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10893 		if (rc) {
10894 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10895 				   vnic->vnic_id, i, rc);
10896 			break;
10897 		}
10898 		bp->rsscos_nr_ctxs++;
10899 	}
10900 	if (i < nr_ctxs)
10901 		return -ENOMEM;
10902 
10903 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10904 	if (rc)
10905 		return rc;
10906 
10907 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10908 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10909 		if (rc) {
10910 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10911 				   vnic->vnic_id, rc);
10912 		}
10913 	}
10914 	return rc;
10915 }
10916 
10917 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10918 {
10919 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10920 		return __bnxt_setup_vnic_p5(bp, vnic);
10921 	else
10922 		return __bnxt_setup_vnic(bp, vnic);
10923 }
10924 
10925 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10926 				     struct bnxt_vnic_info *vnic,
10927 				     u16 start_rx_ring_idx, int rx_rings)
10928 {
10929 	int rc;
10930 
10931 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10932 	if (rc) {
10933 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10934 			   vnic->vnic_id, rc);
10935 		return rc;
10936 	}
10937 	return bnxt_setup_vnic(bp, vnic);
10938 }
10939 
10940 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10941 {
10942 	struct bnxt_vnic_info *vnic;
10943 	int i, rc = 0;
10944 
10945 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10946 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10947 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10948 	}
10949 
10950 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10951 		return 0;
10952 
10953 	for (i = 0; i < bp->rx_nr_rings; i++) {
10954 		u16 vnic_id = i + 1;
10955 		u16 ring_id = i;
10956 
10957 		if (vnic_id >= bp->nr_vnics)
10958 			break;
10959 
10960 		vnic = &bp->vnic_info[vnic_id];
10961 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10962 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10963 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10964 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10965 			break;
10966 	}
10967 	return rc;
10968 }
10969 
10970 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10971 			  bool all)
10972 {
10973 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10974 	struct bnxt_filter_base *usr_fltr, *tmp;
10975 	struct bnxt_ntuple_filter *ntp_fltr;
10976 	int i;
10977 
10978 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10979 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10980 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10981 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10982 	}
10983 	if (!all)
10984 		return;
10985 
10986 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10987 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10988 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10989 			ntp_fltr = container_of(usr_fltr,
10990 						struct bnxt_ntuple_filter,
10991 						base);
10992 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10993 			bnxt_del_ntp_filter(bp, ntp_fltr);
10994 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10995 		}
10996 	}
10997 
10998 	if (vnic->rss_table)
10999 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
11000 				  vnic->rss_table,
11001 				  vnic->rss_table_dma_addr);
11002 	bp->num_rss_ctx--;
11003 }
11004 
11005 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11006 				  int rxr_id)
11007 {
11008 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
11009 	int i, vnic_rx;
11010 
11011 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
11012 	 * must be updated because a future filter may use it.
11013 	 */
11014 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
11015 		return true;
11016 
11017 	for (i = 0; i < tbl_size; i++) {
11018 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
11019 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
11020 		else
11021 			vnic_rx = bp->rss_indir_tbl[i];
11022 
11023 		if (rxr_id == vnic_rx)
11024 			return true;
11025 	}
11026 
11027 	return false;
11028 }
11029 
11030 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11031 				u16 mru, int rxr_id)
11032 {
11033 	int rc;
11034 
11035 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
11036 		return 0;
11037 
11038 	if (mru) {
11039 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11040 		if (rc) {
11041 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11042 				   vnic->vnic_id, rc);
11043 			return rc;
11044 		}
11045 	}
11046 	vnic->mru = mru;
11047 	bnxt_hwrm_vnic_update(bp, vnic,
11048 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
11049 
11050 	return 0;
11051 }
11052 
11053 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
11054 {
11055 	struct ethtool_rxfh_context *ctx;
11056 	unsigned long context;
11057 	int rc;
11058 
11059 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11060 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11061 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11062 
11063 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
11064 		if (rc)
11065 			return rc;
11066 	}
11067 
11068 	return 0;
11069 }
11070 
11071 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
11072 {
11073 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
11074 	struct ethtool_rxfh_context *ctx;
11075 	unsigned long context;
11076 
11077 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11078 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11079 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11080 
11081 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
11082 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
11083 		    __bnxt_setup_vnic_p5(bp, vnic)) {
11084 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
11085 				   rss_ctx->index);
11086 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
11087 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
11088 		}
11089 	}
11090 }
11091 
11092 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
11093 {
11094 	struct ethtool_rxfh_context *ctx;
11095 	unsigned long context;
11096 
11097 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11098 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11099 
11100 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11101 	}
11102 }
11103 
11104 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11105 static bool bnxt_promisc_ok(struct bnxt *bp)
11106 {
11107 #ifdef CONFIG_BNXT_SRIOV
11108 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11109 		return false;
11110 #endif
11111 	return true;
11112 }
11113 
11114 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11115 {
11116 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11117 	unsigned int rc = 0;
11118 
11119 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11120 	if (rc) {
11121 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11122 			   rc);
11123 		return rc;
11124 	}
11125 
11126 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11127 	if (rc) {
11128 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11129 			   rc);
11130 		return rc;
11131 	}
11132 	return rc;
11133 }
11134 
11135 static int bnxt_cfg_rx_mode(struct bnxt *);
11136 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
11137 
11138 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11139 {
11140 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11141 	int rc = 0;
11142 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11143 
11144 	if (irq_re_init) {
11145 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11146 		if (rc) {
11147 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11148 				   rc);
11149 			goto err_out;
11150 		}
11151 	}
11152 
11153 	rc = bnxt_hwrm_ring_alloc(bp);
11154 	if (rc) {
11155 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11156 		goto err_out;
11157 	}
11158 
11159 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11160 	if (rc) {
11161 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11162 		goto err_out;
11163 	}
11164 
11165 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11166 		rx_nr_rings--;
11167 
11168 	/* default vnic 0 */
11169 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11170 	if (rc) {
11171 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11172 		goto err_out;
11173 	}
11174 
11175 	if (BNXT_VF(bp))
11176 		bnxt_hwrm_func_qcfg(bp);
11177 
11178 	rc = bnxt_setup_vnic(bp, vnic);
11179 	if (rc)
11180 		goto err_out;
11181 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11182 		bnxt_hwrm_update_rss_hash_cfg(bp);
11183 
11184 	if (bp->flags & BNXT_FLAG_RFS) {
11185 		rc = bnxt_alloc_rfs_vnics(bp);
11186 		if (rc)
11187 			goto err_out;
11188 	}
11189 
11190 	if (bp->flags & BNXT_FLAG_TPA) {
11191 		rc = bnxt_set_tpa(bp, true);
11192 		if (rc)
11193 			goto err_out;
11194 	}
11195 
11196 	if (BNXT_VF(bp))
11197 		bnxt_update_vf_mac(bp);
11198 
11199 	/* Filter for default vnic 0 */
11200 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11201 	if (rc) {
11202 		if (BNXT_VF(bp) && rc == -ENODEV)
11203 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11204 		else
11205 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11206 		goto err_out;
11207 	}
11208 	vnic->uc_filter_count = 1;
11209 
11210 	vnic->rx_mask = 0;
11211 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11212 		goto skip_rx_mask;
11213 
11214 	if (bp->dev->flags & IFF_BROADCAST)
11215 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11216 
11217 	if (bp->dev->flags & IFF_PROMISC)
11218 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11219 
11220 	if (bp->dev->flags & IFF_ALLMULTI) {
11221 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11222 		vnic->mc_list_count = 0;
11223 	} else if (bp->dev->flags & IFF_MULTICAST) {
11224 		u32 mask = 0;
11225 
11226 		bnxt_mc_list_updated(bp, &mask);
11227 		vnic->rx_mask |= mask;
11228 	}
11229 
11230 	rc = bnxt_cfg_rx_mode(bp);
11231 	if (rc)
11232 		goto err_out;
11233 
11234 skip_rx_mask:
11235 	rc = bnxt_hwrm_set_coal(bp);
11236 	if (rc)
11237 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11238 				rc);
11239 
11240 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11241 		rc = bnxt_setup_nitroa0_vnic(bp);
11242 		if (rc)
11243 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11244 				   rc);
11245 	}
11246 
11247 	if (BNXT_VF(bp)) {
11248 		bnxt_hwrm_func_qcfg(bp);
11249 		netdev_update_features(bp->dev);
11250 	}
11251 
11252 	return 0;
11253 
11254 err_out:
11255 	bnxt_hwrm_resource_free(bp, 0, true);
11256 
11257 	return rc;
11258 }
11259 
11260 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11261 {
11262 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11263 	return 0;
11264 }
11265 
11266 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11267 {
11268 	bnxt_init_cp_rings(bp);
11269 	bnxt_init_rx_rings(bp);
11270 	bnxt_init_tx_rings(bp);
11271 	bnxt_init_ring_grps(bp, irq_re_init);
11272 	bnxt_init_vnics(bp);
11273 
11274 	return bnxt_init_chip(bp, irq_re_init);
11275 }
11276 
11277 static int bnxt_set_real_num_queues(struct bnxt *bp)
11278 {
11279 	int rc;
11280 	struct net_device *dev = bp->dev;
11281 
11282 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11283 					  bp->tx_nr_rings_xdp);
11284 	if (rc)
11285 		return rc;
11286 
11287 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11288 	if (rc)
11289 		return rc;
11290 
11291 #ifdef CONFIG_RFS_ACCEL
11292 	if (bp->flags & BNXT_FLAG_RFS)
11293 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11294 #endif
11295 
11296 	return rc;
11297 }
11298 
11299 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11300 			     bool shared)
11301 {
11302 	int _rx = *rx, _tx = *tx;
11303 
11304 	if (shared) {
11305 		*rx = min_t(int, _rx, max);
11306 		*tx = min_t(int, _tx, max);
11307 	} else {
11308 		if (max < 2)
11309 			return -ENOMEM;
11310 
11311 		while (_rx + _tx > max) {
11312 			if (_rx > _tx && _rx > 1)
11313 				_rx--;
11314 			else if (_tx > 1)
11315 				_tx--;
11316 		}
11317 		*rx = _rx;
11318 		*tx = _tx;
11319 	}
11320 	return 0;
11321 }
11322 
11323 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11324 {
11325 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11326 }
11327 
11328 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11329 {
11330 	int tcs = bp->num_tc;
11331 
11332 	if (!tcs)
11333 		tcs = 1;
11334 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11335 }
11336 
11337 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11338 {
11339 	int tcs = bp->num_tc;
11340 
11341 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11342 	       bp->tx_nr_rings_xdp;
11343 }
11344 
11345 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11346 			   bool sh)
11347 {
11348 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11349 
11350 	if (tx_cp != *tx) {
11351 		int tx_saved = tx_cp, rc;
11352 
11353 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11354 		if (rc)
11355 			return rc;
11356 		if (tx_cp != tx_saved)
11357 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11358 		return 0;
11359 	}
11360 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11361 }
11362 
11363 static void bnxt_setup_msix(struct bnxt *bp)
11364 {
11365 	const int len = sizeof(bp->irq_tbl[0].name);
11366 	struct net_device *dev = bp->dev;
11367 	int tcs, i;
11368 
11369 	tcs = bp->num_tc;
11370 	if (tcs) {
11371 		int i, off, count;
11372 
11373 		for (i = 0; i < tcs; i++) {
11374 			count = bp->tx_nr_rings_per_tc;
11375 			off = BNXT_TC_TO_RING_BASE(bp, i);
11376 			netdev_set_tc_queue(dev, i, count, off);
11377 		}
11378 	}
11379 
11380 	for (i = 0; i < bp->cp_nr_rings; i++) {
11381 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11382 		char *attr;
11383 
11384 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11385 			attr = "TxRx";
11386 		else if (i < bp->rx_nr_rings)
11387 			attr = "rx";
11388 		else
11389 			attr = "tx";
11390 
11391 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11392 			 attr, i);
11393 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11394 	}
11395 }
11396 
11397 static int bnxt_init_int_mode(struct bnxt *bp);
11398 
11399 static int bnxt_change_msix(struct bnxt *bp, int total)
11400 {
11401 	struct msi_map map;
11402 	int i;
11403 
11404 	/* add MSIX to the end if needed */
11405 	for (i = bp->total_irqs; i < total; i++) {
11406 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11407 		if (map.index < 0)
11408 			return bp->total_irqs;
11409 		bp->irq_tbl[i].vector = map.virq;
11410 		bp->total_irqs++;
11411 	}
11412 
11413 	/* trim MSIX from the end if needed */
11414 	for (i = bp->total_irqs; i > total; i--) {
11415 		map.index = i - 1;
11416 		map.virq = bp->irq_tbl[i - 1].vector;
11417 		pci_msix_free_irq(bp->pdev, map);
11418 		bp->total_irqs--;
11419 	}
11420 	return bp->total_irqs;
11421 }
11422 
11423 static int bnxt_setup_int_mode(struct bnxt *bp)
11424 {
11425 	int rc;
11426 
11427 	if (!bp->irq_tbl) {
11428 		rc = bnxt_init_int_mode(bp);
11429 		if (rc || !bp->irq_tbl)
11430 			return rc ?: -ENODEV;
11431 	}
11432 
11433 	bnxt_setup_msix(bp);
11434 
11435 	rc = bnxt_set_real_num_queues(bp);
11436 	return rc;
11437 }
11438 
11439 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11440 {
11441 	return bp->hw_resc.max_rsscos_ctxs;
11442 }
11443 
11444 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11445 {
11446 	return bp->hw_resc.max_vnics;
11447 }
11448 
11449 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11450 {
11451 	return bp->hw_resc.max_stat_ctxs;
11452 }
11453 
11454 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11455 {
11456 	return bp->hw_resc.max_cp_rings;
11457 }
11458 
11459 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11460 {
11461 	unsigned int cp = bp->hw_resc.max_cp_rings;
11462 
11463 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11464 		cp -= bnxt_get_ulp_msix_num(bp);
11465 
11466 	return cp;
11467 }
11468 
11469 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11470 {
11471 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11472 
11473 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11474 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11475 
11476 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11477 }
11478 
11479 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11480 {
11481 	bp->hw_resc.max_irqs = max_irqs;
11482 }
11483 
11484 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11485 {
11486 	unsigned int cp;
11487 
11488 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11489 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11490 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11491 	else
11492 		return cp - bp->cp_nr_rings;
11493 }
11494 
11495 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11496 {
11497 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11498 }
11499 
11500 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11501 {
11502 	int max_irq = bnxt_get_max_func_irqs(bp);
11503 	int total_req = bp->cp_nr_rings + num;
11504 
11505 	if (max_irq < total_req) {
11506 		num = max_irq - bp->cp_nr_rings;
11507 		if (num <= 0)
11508 			return 0;
11509 	}
11510 	return num;
11511 }
11512 
11513 static int bnxt_get_num_msix(struct bnxt *bp)
11514 {
11515 	if (!BNXT_NEW_RM(bp))
11516 		return bnxt_get_max_func_irqs(bp);
11517 
11518 	return bnxt_nq_rings_in_use(bp);
11519 }
11520 
11521 static int bnxt_init_int_mode(struct bnxt *bp)
11522 {
11523 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11524 
11525 	total_vecs = bnxt_get_num_msix(bp);
11526 	max = bnxt_get_max_func_irqs(bp);
11527 	if (total_vecs > max)
11528 		total_vecs = max;
11529 
11530 	if (!total_vecs)
11531 		return 0;
11532 
11533 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11534 		min = 2;
11535 
11536 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11537 					   PCI_IRQ_MSIX);
11538 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11539 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11540 		rc = -ENODEV;
11541 		goto msix_setup_exit;
11542 	}
11543 
11544 	tbl_size = total_vecs;
11545 	if (pci_msix_can_alloc_dyn(bp->pdev))
11546 		tbl_size = max;
11547 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11548 	if (bp->irq_tbl) {
11549 		for (i = 0; i < total_vecs; i++)
11550 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11551 
11552 		bp->total_irqs = total_vecs;
11553 		/* Trim rings based upon num of vectors allocated */
11554 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11555 				     total_vecs - ulp_msix, min == 1);
11556 		if (rc)
11557 			goto msix_setup_exit;
11558 
11559 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11560 		bp->cp_nr_rings = (min == 1) ?
11561 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11562 				  tx_cp + bp->rx_nr_rings;
11563 
11564 	} else {
11565 		rc = -ENOMEM;
11566 		goto msix_setup_exit;
11567 	}
11568 	return 0;
11569 
11570 msix_setup_exit:
11571 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11572 	kfree(bp->irq_tbl);
11573 	bp->irq_tbl = NULL;
11574 	pci_free_irq_vectors(bp->pdev);
11575 	return rc;
11576 }
11577 
11578 static void bnxt_clear_int_mode(struct bnxt *bp)
11579 {
11580 	pci_free_irq_vectors(bp->pdev);
11581 
11582 	kfree(bp->irq_tbl);
11583 	bp->irq_tbl = NULL;
11584 }
11585 
11586 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11587 {
11588 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
11589 	bool irq_cleared = false;
11590 	bool irq_change = false;
11591 	int tcs = bp->num_tc;
11592 	int irqs_required;
11593 	int rc;
11594 
11595 	if (!bnxt_need_reserve_rings(bp))
11596 		return 0;
11597 
11598 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
11599 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11600 
11601 		if (ulp_msix > bp->ulp_num_msix_want)
11602 			ulp_msix = bp->ulp_num_msix_want;
11603 		irqs_required = ulp_msix + bp->cp_nr_rings;
11604 	} else {
11605 		irqs_required = bnxt_get_num_msix(bp);
11606 	}
11607 
11608 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11609 		irq_change = true;
11610 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11611 			bnxt_ulp_irq_stop(bp);
11612 			bnxt_clear_int_mode(bp);
11613 			irq_cleared = true;
11614 		}
11615 	}
11616 	rc = __bnxt_reserve_rings(bp);
11617 	if (irq_cleared) {
11618 		if (!rc)
11619 			rc = bnxt_init_int_mode(bp);
11620 		bnxt_ulp_irq_restart(bp, rc);
11621 	} else if (irq_change && !rc) {
11622 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11623 			rc = -ENOSPC;
11624 	}
11625 	if (rc) {
11626 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11627 		return rc;
11628 	}
11629 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11630 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11631 		netdev_err(bp->dev, "tx ring reservation failure\n");
11632 		netdev_reset_tc(bp->dev);
11633 		bp->num_tc = 0;
11634 		if (bp->tx_nr_rings_xdp)
11635 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11636 		else
11637 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11638 		return -ENOMEM;
11639 	}
11640 	return 0;
11641 }
11642 
11643 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11644 {
11645 	struct bnxt_tx_ring_info *txr;
11646 	struct netdev_queue *txq;
11647 	struct bnxt_napi *bnapi;
11648 	int i;
11649 
11650 	bnapi = bp->bnapi[idx];
11651 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11652 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11653 		synchronize_net();
11654 
11655 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11656 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11657 			if (txq) {
11658 				__netif_tx_lock_bh(txq);
11659 				netif_tx_stop_queue(txq);
11660 				__netif_tx_unlock_bh(txq);
11661 			}
11662 		}
11663 
11664 		if (!bp->tph_mode)
11665 			continue;
11666 
11667 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11668 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11669 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11670 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11671 	}
11672 }
11673 
11674 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11675 {
11676 	struct bnxt_tx_ring_info *txr;
11677 	struct netdev_queue *txq;
11678 	struct bnxt_napi *bnapi;
11679 	int rc, i;
11680 
11681 	bnapi = bp->bnapi[idx];
11682 	/* All rings have been reserved and previously allocated.
11683 	 * Reallocating with the same parameters should never fail.
11684 	 */
11685 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11686 		if (!bp->tph_mode)
11687 			goto start_tx;
11688 
11689 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11690 		if (rc)
11691 			return rc;
11692 
11693 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11694 		if (rc)
11695 			return rc;
11696 
11697 		txr->tx_prod = 0;
11698 		txr->tx_cons = 0;
11699 		txr->tx_hw_cons = 0;
11700 start_tx:
11701 		WRITE_ONCE(txr->dev_state, 0);
11702 		synchronize_net();
11703 
11704 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11705 			continue;
11706 
11707 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11708 		if (txq)
11709 			netif_tx_start_queue(txq);
11710 	}
11711 
11712 	return 0;
11713 }
11714 
11715 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11716 				     const cpumask_t *mask)
11717 {
11718 	struct bnxt_irq *irq;
11719 	u16 tag;
11720 	int err;
11721 
11722 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11723 
11724 	if (!irq->bp->tph_mode)
11725 		return;
11726 
11727 	cpumask_copy(irq->cpu_mask, mask);
11728 
11729 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11730 		return;
11731 
11732 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11733 				cpumask_first(irq->cpu_mask), &tag))
11734 		return;
11735 
11736 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11737 		return;
11738 
11739 	netdev_lock(irq->bp->dev);
11740 	if (netif_running(irq->bp->dev)) {
11741 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11742 		if (err)
11743 			netdev_err(irq->bp->dev,
11744 				   "RX queue restart failed: err=%d\n", err);
11745 	}
11746 	netdev_unlock(irq->bp->dev);
11747 }
11748 
11749 static void bnxt_irq_affinity_release(struct kref *ref)
11750 {
11751 	struct irq_affinity_notify *notify =
11752 		container_of(ref, struct irq_affinity_notify, kref);
11753 	struct bnxt_irq *irq;
11754 
11755 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11756 
11757 	if (!irq->bp->tph_mode)
11758 		return;
11759 
11760 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11761 		netdev_err(irq->bp->dev,
11762 			   "Setting ST=0 for MSIX entry %d failed\n",
11763 			   irq->msix_nr);
11764 		return;
11765 	}
11766 }
11767 
11768 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11769 {
11770 	irq_set_affinity_notifier(irq->vector, NULL);
11771 }
11772 
11773 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11774 {
11775 	struct irq_affinity_notify *notify;
11776 
11777 	irq->bp = bp;
11778 
11779 	/* Nothing to do if TPH is not enabled */
11780 	if (!bp->tph_mode)
11781 		return;
11782 
11783 	/* Register IRQ affinity notifier */
11784 	notify = &irq->affinity_notify;
11785 	notify->irq = irq->vector;
11786 	notify->notify = bnxt_irq_affinity_notify;
11787 	notify->release = bnxt_irq_affinity_release;
11788 
11789 	irq_set_affinity_notifier(irq->vector, notify);
11790 }
11791 
11792 static void bnxt_free_irq(struct bnxt *bp)
11793 {
11794 	struct bnxt_irq *irq;
11795 	int i;
11796 
11797 #ifdef CONFIG_RFS_ACCEL
11798 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11799 	bp->dev->rx_cpu_rmap = NULL;
11800 #endif
11801 	if (!bp->irq_tbl || !bp->bnapi)
11802 		return;
11803 
11804 	for (i = 0; i < bp->cp_nr_rings; i++) {
11805 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11806 
11807 		irq = &bp->irq_tbl[map_idx];
11808 		if (irq->requested) {
11809 			if (irq->have_cpumask) {
11810 				irq_update_affinity_hint(irq->vector, NULL);
11811 				free_cpumask_var(irq->cpu_mask);
11812 				irq->have_cpumask = 0;
11813 			}
11814 
11815 			bnxt_release_irq_notifier(irq);
11816 
11817 			free_irq(irq->vector, bp->bnapi[i]);
11818 		}
11819 
11820 		irq->requested = 0;
11821 	}
11822 
11823 	/* Disable TPH support */
11824 	pcie_disable_tph(bp->pdev);
11825 	bp->tph_mode = 0;
11826 }
11827 
11828 static int bnxt_request_irq(struct bnxt *bp)
11829 {
11830 	struct cpu_rmap *rmap = NULL;
11831 	int i, j, rc = 0;
11832 	unsigned long flags = 0;
11833 
11834 	rc = bnxt_setup_int_mode(bp);
11835 	if (rc) {
11836 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11837 			   rc);
11838 		return rc;
11839 	}
11840 #ifdef CONFIG_RFS_ACCEL
11841 	rmap = bp->dev->rx_cpu_rmap;
11842 #endif
11843 
11844 	/* Enable TPH support as part of IRQ request */
11845 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11846 	if (!rc)
11847 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11848 
11849 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11850 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11851 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11852 
11853 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11854 		    rmap && bp->bnapi[i]->rx_ring) {
11855 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11856 			if (rc)
11857 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11858 					    j);
11859 			j++;
11860 		}
11861 
11862 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11863 				 bp->bnapi[i]);
11864 		if (rc)
11865 			break;
11866 
11867 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11868 		irq->requested = 1;
11869 
11870 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11871 			int numa_node = dev_to_node(&bp->pdev->dev);
11872 			u16 tag;
11873 
11874 			irq->have_cpumask = 1;
11875 			irq->msix_nr = map_idx;
11876 			irq->ring_nr = i;
11877 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11878 					irq->cpu_mask);
11879 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11880 			if (rc) {
11881 				netdev_warn(bp->dev,
11882 					    "Update affinity hint failed, IRQ = %d\n",
11883 					    irq->vector);
11884 				break;
11885 			}
11886 
11887 			bnxt_register_irq_notifier(bp, irq);
11888 
11889 			/* Init ST table entry */
11890 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11891 						cpumask_first(irq->cpu_mask),
11892 						&tag))
11893 				continue;
11894 
11895 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11896 		}
11897 	}
11898 	return rc;
11899 }
11900 
11901 static void bnxt_del_napi(struct bnxt *bp)
11902 {
11903 	int i;
11904 
11905 	if (!bp->bnapi)
11906 		return;
11907 
11908 	for (i = 0; i < bp->rx_nr_rings; i++)
11909 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11910 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11911 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11912 
11913 	for (i = 0; i < bp->cp_nr_rings; i++) {
11914 		struct bnxt_napi *bnapi = bp->bnapi[i];
11915 
11916 		__netif_napi_del_locked(&bnapi->napi);
11917 	}
11918 	/* We called __netif_napi_del_locked(), we need
11919 	 * to respect an RCU grace period before freeing napi structures.
11920 	 */
11921 	synchronize_net();
11922 }
11923 
11924 static void bnxt_init_napi(struct bnxt *bp)
11925 {
11926 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11927 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11928 	struct bnxt_napi *bnapi;
11929 	int i;
11930 
11931 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11932 		poll_fn = bnxt_poll_p5;
11933 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11934 		cp_nr_rings--;
11935 
11936 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11937 
11938 	for (i = 0; i < cp_nr_rings; i++) {
11939 		bnapi = bp->bnapi[i];
11940 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11941 					     bnapi->index);
11942 	}
11943 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11944 		bnapi = bp->bnapi[cp_nr_rings];
11945 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11946 	}
11947 }
11948 
11949 static void bnxt_disable_napi(struct bnxt *bp)
11950 {
11951 	int i;
11952 
11953 	if (!bp->bnapi ||
11954 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11955 		return;
11956 
11957 	for (i = 0; i < bp->cp_nr_rings; i++) {
11958 		struct bnxt_napi *bnapi = bp->bnapi[i];
11959 		struct bnxt_cp_ring_info *cpr;
11960 
11961 		cpr = &bnapi->cp_ring;
11962 		if (bnapi->tx_fault)
11963 			cpr->sw_stats->tx.tx_resets++;
11964 		if (bnapi->in_reset)
11965 			cpr->sw_stats->rx.rx_resets++;
11966 		napi_disable_locked(&bnapi->napi);
11967 	}
11968 }
11969 
11970 static void bnxt_enable_napi(struct bnxt *bp)
11971 {
11972 	int i;
11973 
11974 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11975 	for (i = 0; i < bp->cp_nr_rings; i++) {
11976 		struct bnxt_napi *bnapi = bp->bnapi[i];
11977 		struct bnxt_cp_ring_info *cpr;
11978 
11979 		bnapi->tx_fault = 0;
11980 
11981 		cpr = &bnapi->cp_ring;
11982 		bnapi->in_reset = false;
11983 
11984 		if (bnapi->rx_ring) {
11985 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11986 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11987 		}
11988 		napi_enable_locked(&bnapi->napi);
11989 	}
11990 }
11991 
11992 void bnxt_tx_disable(struct bnxt *bp)
11993 {
11994 	int i;
11995 	struct bnxt_tx_ring_info *txr;
11996 
11997 	if (bp->tx_ring) {
11998 		for (i = 0; i < bp->tx_nr_rings; i++) {
11999 			txr = &bp->tx_ring[i];
12000 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
12001 		}
12002 	}
12003 	/* Make sure napi polls see @dev_state change */
12004 	synchronize_net();
12005 	/* Drop carrier first to prevent TX timeout */
12006 	netif_carrier_off(bp->dev);
12007 	/* Stop all TX queues */
12008 	netif_tx_disable(bp->dev);
12009 }
12010 
12011 void bnxt_tx_enable(struct bnxt *bp)
12012 {
12013 	int i;
12014 	struct bnxt_tx_ring_info *txr;
12015 
12016 	for (i = 0; i < bp->tx_nr_rings; i++) {
12017 		txr = &bp->tx_ring[i];
12018 		WRITE_ONCE(txr->dev_state, 0);
12019 	}
12020 	/* Make sure napi polls see @dev_state change */
12021 	synchronize_net();
12022 	netif_tx_wake_all_queues(bp->dev);
12023 	if (BNXT_LINK_IS_UP(bp))
12024 		netif_carrier_on(bp->dev);
12025 }
12026 
12027 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
12028 {
12029 	u8 active_fec = link_info->active_fec_sig_mode &
12030 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
12031 
12032 	switch (active_fec) {
12033 	default:
12034 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
12035 		return "None";
12036 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
12037 		return "Clause 74 BaseR";
12038 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
12039 		return "Clause 91 RS(528,514)";
12040 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
12041 		return "Clause 91 RS544_1XN";
12042 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
12043 		return "Clause 91 RS(544,514)";
12044 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
12045 		return "Clause 91 RS272_1XN";
12046 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
12047 		return "Clause 91 RS(272,257)";
12048 	}
12049 }
12050 
12051 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
12052 {
12053 	u8 reason = link_info->link_down_reason;
12054 
12055 	/* Multiple bits can be set, we report 1 bit only in order of
12056 	 * priority.
12057 	 */
12058 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
12059 		return "(Remote fault)";
12060 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
12061 		return "(OTP Speed limit violation)";
12062 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
12063 		return "(Cable removed)";
12064 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
12065 		return "(Module fault)";
12066 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
12067 		return "(BMC request down)";
12068 	return "";
12069 }
12070 
12071 void bnxt_report_link(struct bnxt *bp)
12072 {
12073 	if (BNXT_LINK_IS_UP(bp)) {
12074 		const char *signal = "";
12075 		const char *flow_ctrl;
12076 		const char *duplex;
12077 		u32 speed;
12078 		u16 fec;
12079 
12080 		netif_carrier_on(bp->dev);
12081 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
12082 		if (speed == SPEED_UNKNOWN) {
12083 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
12084 			return;
12085 		}
12086 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
12087 			duplex = "full";
12088 		else
12089 			duplex = "half";
12090 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
12091 			flow_ctrl = "ON - receive & transmit";
12092 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
12093 			flow_ctrl = "ON - transmit";
12094 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12095 			flow_ctrl = "ON - receive";
12096 		else
12097 			flow_ctrl = "none";
12098 		if (bp->link_info.phy_qcfg_resp.option_flags &
12099 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12100 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12101 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12102 			switch (sig_mode) {
12103 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12104 				signal = "(NRZ) ";
12105 				break;
12106 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12107 				signal = "(PAM4 56Gbps) ";
12108 				break;
12109 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12110 				signal = "(PAM4 112Gbps) ";
12111 				break;
12112 			default:
12113 				break;
12114 			}
12115 		}
12116 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12117 			    speed, signal, duplex, flow_ctrl);
12118 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12119 			netdev_info(bp->dev, "EEE is %s\n",
12120 				    bp->eee.eee_active ? "active" :
12121 							 "not active");
12122 		fec = bp->link_info.fec_cfg;
12123 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12124 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12125 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12126 				    bnxt_report_fec(&bp->link_info));
12127 	} else {
12128 		char *str = bnxt_link_down_reason(&bp->link_info);
12129 
12130 		netif_carrier_off(bp->dev);
12131 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12132 	}
12133 }
12134 
12135 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12136 {
12137 	if (!resp->supported_speeds_auto_mode &&
12138 	    !resp->supported_speeds_force_mode &&
12139 	    !resp->supported_pam4_speeds_auto_mode &&
12140 	    !resp->supported_pam4_speeds_force_mode &&
12141 	    !resp->supported_speeds2_auto_mode &&
12142 	    !resp->supported_speeds2_force_mode)
12143 		return true;
12144 	return false;
12145 }
12146 
12147 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12148 {
12149 	struct bnxt_link_info *link_info = &bp->link_info;
12150 	struct hwrm_port_phy_qcaps_output *resp;
12151 	struct hwrm_port_phy_qcaps_input *req;
12152 	int rc = 0;
12153 
12154 	if (bp->hwrm_spec_code < 0x10201)
12155 		return 0;
12156 
12157 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12158 	if (rc)
12159 		return rc;
12160 
12161 	resp = hwrm_req_hold(bp, req);
12162 	rc = hwrm_req_send(bp, req);
12163 	if (rc)
12164 		goto hwrm_phy_qcaps_exit;
12165 
12166 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12167 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12168 		struct ethtool_keee *eee = &bp->eee;
12169 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12170 
12171 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12172 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12173 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12174 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12175 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12176 	}
12177 
12178 	if (bp->hwrm_spec_code >= 0x10a01) {
12179 		if (bnxt_phy_qcaps_no_speed(resp)) {
12180 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12181 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12182 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12183 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12184 			netdev_info(bp->dev, "Ethernet link enabled\n");
12185 			/* Phy re-enabled, reprobe the speeds */
12186 			link_info->support_auto_speeds = 0;
12187 			link_info->support_pam4_auto_speeds = 0;
12188 			link_info->support_auto_speeds2 = 0;
12189 		}
12190 	}
12191 	if (resp->supported_speeds_auto_mode)
12192 		link_info->support_auto_speeds =
12193 			le16_to_cpu(resp->supported_speeds_auto_mode);
12194 	if (resp->supported_pam4_speeds_auto_mode)
12195 		link_info->support_pam4_auto_speeds =
12196 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12197 	if (resp->supported_speeds2_auto_mode)
12198 		link_info->support_auto_speeds2 =
12199 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12200 
12201 	bp->port_count = resp->port_cnt;
12202 
12203 hwrm_phy_qcaps_exit:
12204 	hwrm_req_drop(bp, req);
12205 	return rc;
12206 }
12207 
12208 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12209 {
12210 	struct hwrm_port_mac_qcaps_output *resp;
12211 	struct hwrm_port_mac_qcaps_input *req;
12212 	int rc;
12213 
12214 	if (bp->hwrm_spec_code < 0x10a03)
12215 		return;
12216 
12217 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12218 	if (rc)
12219 		return;
12220 
12221 	resp = hwrm_req_hold(bp, req);
12222 	rc = hwrm_req_send_silent(bp, req);
12223 	if (!rc)
12224 		bp->mac_flags = resp->flags;
12225 	hwrm_req_drop(bp, req);
12226 }
12227 
12228 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12229 {
12230 	u16 diff = advertising ^ supported;
12231 
12232 	return ((supported | diff) != supported);
12233 }
12234 
12235 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12236 {
12237 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12238 
12239 	/* Check if any advertised speeds are no longer supported. The caller
12240 	 * holds the link_lock mutex, so we can modify link_info settings.
12241 	 */
12242 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12243 		if (bnxt_support_dropped(link_info->advertising,
12244 					 link_info->support_auto_speeds2)) {
12245 			link_info->advertising = link_info->support_auto_speeds2;
12246 			return true;
12247 		}
12248 		return false;
12249 	}
12250 	if (bnxt_support_dropped(link_info->advertising,
12251 				 link_info->support_auto_speeds)) {
12252 		link_info->advertising = link_info->support_auto_speeds;
12253 		return true;
12254 	}
12255 	if (bnxt_support_dropped(link_info->advertising_pam4,
12256 				 link_info->support_pam4_auto_speeds)) {
12257 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12258 		return true;
12259 	}
12260 	return false;
12261 }
12262 
12263 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12264 {
12265 	struct bnxt_link_info *link_info = &bp->link_info;
12266 	struct hwrm_port_phy_qcfg_output *resp;
12267 	struct hwrm_port_phy_qcfg_input *req;
12268 	u8 link_state = link_info->link_state;
12269 	bool support_changed;
12270 	int rc;
12271 
12272 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12273 	if (rc)
12274 		return rc;
12275 
12276 	resp = hwrm_req_hold(bp, req);
12277 	rc = hwrm_req_send(bp, req);
12278 	if (rc) {
12279 		hwrm_req_drop(bp, req);
12280 		if (BNXT_VF(bp) && rc == -ENODEV) {
12281 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12282 			rc = 0;
12283 		}
12284 		return rc;
12285 	}
12286 
12287 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12288 	link_info->phy_link_status = resp->link;
12289 	link_info->duplex = resp->duplex_cfg;
12290 	if (bp->hwrm_spec_code >= 0x10800)
12291 		link_info->duplex = resp->duplex_state;
12292 	link_info->pause = resp->pause;
12293 	link_info->auto_mode = resp->auto_mode;
12294 	link_info->auto_pause_setting = resp->auto_pause;
12295 	link_info->lp_pause = resp->link_partner_adv_pause;
12296 	link_info->force_pause_setting = resp->force_pause;
12297 	link_info->duplex_setting = resp->duplex_cfg;
12298 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12299 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12300 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12301 			link_info->active_lanes = resp->active_lanes;
12302 	} else {
12303 		link_info->link_speed = 0;
12304 		link_info->active_lanes = 0;
12305 	}
12306 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12307 	link_info->force_pam4_link_speed =
12308 		le16_to_cpu(resp->force_pam4_link_speed);
12309 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12310 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12311 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12312 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12313 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12314 	link_info->auto_pam4_link_speeds =
12315 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12316 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12317 	link_info->lp_auto_link_speeds =
12318 		le16_to_cpu(resp->link_partner_adv_speeds);
12319 	link_info->lp_auto_pam4_link_speeds =
12320 		resp->link_partner_pam4_adv_speeds;
12321 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12322 	link_info->phy_ver[0] = resp->phy_maj;
12323 	link_info->phy_ver[1] = resp->phy_min;
12324 	link_info->phy_ver[2] = resp->phy_bld;
12325 	link_info->media_type = resp->media_type;
12326 	link_info->phy_type = resp->phy_type;
12327 	link_info->transceiver = resp->xcvr_pkg_type;
12328 	link_info->phy_addr = resp->eee_config_phy_addr &
12329 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12330 	link_info->module_status = resp->module_status;
12331 	link_info->link_down_reason = resp->link_down_reason;
12332 
12333 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12334 		struct ethtool_keee *eee = &bp->eee;
12335 		u16 fw_speeds;
12336 
12337 		eee->eee_active = 0;
12338 		if (resp->eee_config_phy_addr &
12339 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12340 			eee->eee_active = 1;
12341 			fw_speeds = le16_to_cpu(
12342 				resp->link_partner_adv_eee_link_speed_mask);
12343 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12344 		}
12345 
12346 		/* Pull initial EEE config */
12347 		if (!chng_link_state) {
12348 			if (resp->eee_config_phy_addr &
12349 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12350 				eee->eee_enabled = 1;
12351 
12352 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12353 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12354 
12355 			if (resp->eee_config_phy_addr &
12356 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12357 				__le32 tmr;
12358 
12359 				eee->tx_lpi_enabled = 1;
12360 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12361 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12362 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12363 			}
12364 		}
12365 	}
12366 
12367 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12368 	if (bp->hwrm_spec_code >= 0x10504) {
12369 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12370 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12371 	}
12372 	/* TODO: need to add more logic to report VF link */
12373 	if (chng_link_state) {
12374 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12375 			link_info->link_state = BNXT_LINK_STATE_UP;
12376 		else
12377 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12378 		if (link_state != link_info->link_state)
12379 			bnxt_report_link(bp);
12380 	} else {
12381 		/* always link down if not require to update link state */
12382 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12383 	}
12384 	hwrm_req_drop(bp, req);
12385 
12386 	if (!BNXT_PHY_CFG_ABLE(bp))
12387 		return 0;
12388 
12389 	support_changed = bnxt_support_speed_dropped(link_info);
12390 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12391 		bnxt_hwrm_set_link_setting(bp, true, false);
12392 	return 0;
12393 }
12394 
12395 static void bnxt_get_port_module_status(struct bnxt *bp)
12396 {
12397 	struct bnxt_link_info *link_info = &bp->link_info;
12398 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12399 	u8 module_status;
12400 
12401 	if (bnxt_update_link(bp, true))
12402 		return;
12403 
12404 	module_status = link_info->module_status;
12405 	switch (module_status) {
12406 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12407 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12408 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12409 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12410 			    bp->pf.port_id);
12411 		if (bp->hwrm_spec_code >= 0x10201) {
12412 			netdev_warn(bp->dev, "Module part number %s\n",
12413 				    resp->phy_vendor_partnumber);
12414 		}
12415 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12416 			netdev_warn(bp->dev, "TX is disabled\n");
12417 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12418 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12419 	}
12420 }
12421 
12422 static void
12423 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12424 {
12425 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12426 		if (bp->hwrm_spec_code >= 0x10201)
12427 			req->auto_pause =
12428 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12429 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12430 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12431 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12432 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12433 		req->enables |=
12434 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12435 	} else {
12436 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12437 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12438 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12439 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12440 		req->enables |=
12441 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12442 		if (bp->hwrm_spec_code >= 0x10201) {
12443 			req->auto_pause = req->force_pause;
12444 			req->enables |= cpu_to_le32(
12445 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12446 		}
12447 	}
12448 }
12449 
12450 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12451 {
12452 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12453 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12454 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12455 			req->enables |=
12456 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12457 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12458 		} else if (bp->link_info.advertising) {
12459 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12460 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12461 		}
12462 		if (bp->link_info.advertising_pam4) {
12463 			req->enables |=
12464 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12465 			req->auto_link_pam4_speed_mask =
12466 				cpu_to_le16(bp->link_info.advertising_pam4);
12467 		}
12468 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12469 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12470 	} else {
12471 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12472 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12473 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12474 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12475 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12476 				   (u32)bp->link_info.req_link_speed);
12477 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12478 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12479 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12480 		} else {
12481 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12482 		}
12483 	}
12484 
12485 	/* tell chimp that the setting takes effect immediately */
12486 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12487 }
12488 
12489 int bnxt_hwrm_set_pause(struct bnxt *bp)
12490 {
12491 	struct hwrm_port_phy_cfg_input *req;
12492 	int rc;
12493 
12494 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12495 	if (rc)
12496 		return rc;
12497 
12498 	bnxt_hwrm_set_pause_common(bp, req);
12499 
12500 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12501 	    bp->link_info.force_link_chng)
12502 		bnxt_hwrm_set_link_common(bp, req);
12503 
12504 	rc = hwrm_req_send(bp, req);
12505 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12506 		/* since changing of pause setting doesn't trigger any link
12507 		 * change event, the driver needs to update the current pause
12508 		 * result upon successfully return of the phy_cfg command
12509 		 */
12510 		bp->link_info.pause =
12511 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12512 		bp->link_info.auto_pause_setting = 0;
12513 		if (!bp->link_info.force_link_chng)
12514 			bnxt_report_link(bp);
12515 	}
12516 	bp->link_info.force_link_chng = false;
12517 	return rc;
12518 }
12519 
12520 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12521 			      struct hwrm_port_phy_cfg_input *req)
12522 {
12523 	struct ethtool_keee *eee = &bp->eee;
12524 
12525 	if (eee->eee_enabled) {
12526 		u16 eee_speeds;
12527 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12528 
12529 		if (eee->tx_lpi_enabled)
12530 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12531 		else
12532 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12533 
12534 		req->flags |= cpu_to_le32(flags);
12535 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12536 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12537 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12538 	} else {
12539 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12540 	}
12541 }
12542 
12543 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12544 {
12545 	struct hwrm_port_phy_cfg_input *req;
12546 	int rc;
12547 
12548 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12549 	if (rc)
12550 		return rc;
12551 
12552 	if (set_pause)
12553 		bnxt_hwrm_set_pause_common(bp, req);
12554 
12555 	bnxt_hwrm_set_link_common(bp, req);
12556 
12557 	if (set_eee)
12558 		bnxt_hwrm_set_eee(bp, req);
12559 	return hwrm_req_send(bp, req);
12560 }
12561 
12562 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12563 {
12564 	struct hwrm_port_phy_cfg_input *req;
12565 	int rc;
12566 
12567 	if (!BNXT_SINGLE_PF(bp))
12568 		return 0;
12569 
12570 	if (pci_num_vf(bp->pdev) &&
12571 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12572 		return 0;
12573 
12574 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12575 	if (rc)
12576 		return rc;
12577 
12578 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12579 	rc = hwrm_req_send(bp, req);
12580 	if (!rc) {
12581 		mutex_lock(&bp->link_lock);
12582 		/* Device is not obliged link down in certain scenarios, even
12583 		 * when forced. Setting the state unknown is consistent with
12584 		 * driver startup and will force link state to be reported
12585 		 * during subsequent open based on PORT_PHY_QCFG.
12586 		 */
12587 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12588 		mutex_unlock(&bp->link_lock);
12589 	}
12590 	return rc;
12591 }
12592 
12593 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12594 {
12595 #ifdef CONFIG_TEE_BNXT_FW
12596 	int rc = tee_bnxt_fw_load();
12597 
12598 	if (rc)
12599 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12600 
12601 	return rc;
12602 #else
12603 	netdev_err(bp->dev, "OP-TEE not supported\n");
12604 	return -ENODEV;
12605 #endif
12606 }
12607 
12608 static int bnxt_try_recover_fw(struct bnxt *bp)
12609 {
12610 	if (bp->fw_health && bp->fw_health->status_reliable) {
12611 		int retry = 0, rc;
12612 		u32 sts;
12613 
12614 		do {
12615 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12616 			rc = bnxt_hwrm_poll(bp);
12617 			if (!BNXT_FW_IS_BOOTING(sts) &&
12618 			    !BNXT_FW_IS_RECOVERING(sts))
12619 				break;
12620 			retry++;
12621 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12622 
12623 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12624 			netdev_err(bp->dev,
12625 				   "Firmware not responding, status: 0x%x\n",
12626 				   sts);
12627 			rc = -ENODEV;
12628 		}
12629 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12630 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12631 			return bnxt_fw_reset_via_optee(bp);
12632 		}
12633 		return rc;
12634 	}
12635 
12636 	return -ENODEV;
12637 }
12638 
12639 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12640 {
12641 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12642 
12643 	if (!BNXT_NEW_RM(bp))
12644 		return; /* no resource reservations required */
12645 
12646 	hw_resc->resv_cp_rings = 0;
12647 	hw_resc->resv_stat_ctxs = 0;
12648 	hw_resc->resv_irqs = 0;
12649 	hw_resc->resv_tx_rings = 0;
12650 	hw_resc->resv_rx_rings = 0;
12651 	hw_resc->resv_hw_ring_grps = 0;
12652 	hw_resc->resv_vnics = 0;
12653 	hw_resc->resv_rsscos_ctxs = 0;
12654 	if (!fw_reset) {
12655 		bp->tx_nr_rings = 0;
12656 		bp->rx_nr_rings = 0;
12657 	}
12658 }
12659 
12660 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12661 {
12662 	int rc;
12663 
12664 	if (!BNXT_NEW_RM(bp))
12665 		return 0; /* no resource reservations required */
12666 
12667 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12668 	if (rc)
12669 		netdev_err(bp->dev, "resc_qcaps failed\n");
12670 
12671 	bnxt_clear_reservations(bp, fw_reset);
12672 
12673 	return rc;
12674 }
12675 
12676 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12677 {
12678 	struct hwrm_func_drv_if_change_output *resp;
12679 	struct hwrm_func_drv_if_change_input *req;
12680 	bool resc_reinit = false;
12681 	bool caps_change = false;
12682 	int rc, retry = 0;
12683 	bool fw_reset;
12684 	u32 flags = 0;
12685 
12686 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12687 	bp->fw_reset_state = 0;
12688 
12689 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12690 		return 0;
12691 
12692 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12693 	if (rc)
12694 		return rc;
12695 
12696 	if (up)
12697 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12698 	resp = hwrm_req_hold(bp, req);
12699 
12700 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12701 	while (retry < BNXT_FW_IF_RETRY) {
12702 		rc = hwrm_req_send(bp, req);
12703 		if (rc != -EAGAIN)
12704 			break;
12705 
12706 		msleep(50);
12707 		retry++;
12708 	}
12709 
12710 	if (rc == -EAGAIN) {
12711 		hwrm_req_drop(bp, req);
12712 		return rc;
12713 	} else if (!rc) {
12714 		flags = le32_to_cpu(resp->flags);
12715 	} else if (up) {
12716 		rc = bnxt_try_recover_fw(bp);
12717 		fw_reset = true;
12718 	}
12719 	hwrm_req_drop(bp, req);
12720 	if (rc)
12721 		return rc;
12722 
12723 	if (!up) {
12724 		bnxt_inv_fw_health_reg(bp);
12725 		return 0;
12726 	}
12727 
12728 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12729 		resc_reinit = true;
12730 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12731 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12732 		fw_reset = true;
12733 	else
12734 		bnxt_remap_fw_health_regs(bp);
12735 
12736 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12737 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12738 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12739 		return -ENODEV;
12740 	}
12741 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12742 		caps_change = true;
12743 
12744 	if (resc_reinit || fw_reset || caps_change) {
12745 		if (fw_reset || caps_change) {
12746 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12747 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12748 				bnxt_ulp_irq_stop(bp);
12749 			bnxt_free_ctx_mem(bp, false);
12750 			bnxt_dcb_free(bp);
12751 			rc = bnxt_fw_init_one(bp);
12752 			if (rc) {
12753 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12754 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12755 				return rc;
12756 			}
12757 			/* IRQ will be initialized later in bnxt_request_irq()*/
12758 			bnxt_clear_int_mode(bp);
12759 		}
12760 		rc = bnxt_cancel_reservations(bp, fw_reset);
12761 	}
12762 	return rc;
12763 }
12764 
12765 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12766 {
12767 	struct hwrm_port_led_qcaps_output *resp;
12768 	struct hwrm_port_led_qcaps_input *req;
12769 	struct bnxt_pf_info *pf = &bp->pf;
12770 	int rc;
12771 
12772 	bp->num_leds = 0;
12773 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12774 		return 0;
12775 
12776 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12777 	if (rc)
12778 		return rc;
12779 
12780 	req->port_id = cpu_to_le16(pf->port_id);
12781 	resp = hwrm_req_hold(bp, req);
12782 	rc = hwrm_req_send(bp, req);
12783 	if (rc) {
12784 		hwrm_req_drop(bp, req);
12785 		return rc;
12786 	}
12787 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12788 		int i;
12789 
12790 		bp->num_leds = resp->num_leds;
12791 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12792 						 bp->num_leds);
12793 		for (i = 0; i < bp->num_leds; i++) {
12794 			struct bnxt_led_info *led = &bp->leds[i];
12795 			__le16 caps = led->led_state_caps;
12796 
12797 			if (!led->led_group_id ||
12798 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12799 				bp->num_leds = 0;
12800 				break;
12801 			}
12802 		}
12803 	}
12804 	hwrm_req_drop(bp, req);
12805 	return 0;
12806 }
12807 
12808 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12809 {
12810 	struct hwrm_wol_filter_alloc_output *resp;
12811 	struct hwrm_wol_filter_alloc_input *req;
12812 	int rc;
12813 
12814 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12815 	if (rc)
12816 		return rc;
12817 
12818 	req->port_id = cpu_to_le16(bp->pf.port_id);
12819 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12820 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12821 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12822 
12823 	resp = hwrm_req_hold(bp, req);
12824 	rc = hwrm_req_send(bp, req);
12825 	if (!rc)
12826 		bp->wol_filter_id = resp->wol_filter_id;
12827 	hwrm_req_drop(bp, req);
12828 	return rc;
12829 }
12830 
12831 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12832 {
12833 	struct hwrm_wol_filter_free_input *req;
12834 	int rc;
12835 
12836 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12837 	if (rc)
12838 		return rc;
12839 
12840 	req->port_id = cpu_to_le16(bp->pf.port_id);
12841 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12842 	req->wol_filter_id = bp->wol_filter_id;
12843 
12844 	return hwrm_req_send(bp, req);
12845 }
12846 
12847 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12848 {
12849 	struct hwrm_wol_filter_qcfg_output *resp;
12850 	struct hwrm_wol_filter_qcfg_input *req;
12851 	u16 next_handle = 0;
12852 	int rc;
12853 
12854 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12855 	if (rc)
12856 		return rc;
12857 
12858 	req->port_id = cpu_to_le16(bp->pf.port_id);
12859 	req->handle = cpu_to_le16(handle);
12860 	resp = hwrm_req_hold(bp, req);
12861 	rc = hwrm_req_send(bp, req);
12862 	if (!rc) {
12863 		next_handle = le16_to_cpu(resp->next_handle);
12864 		if (next_handle != 0) {
12865 			if (resp->wol_type ==
12866 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12867 				bp->wol = 1;
12868 				bp->wol_filter_id = resp->wol_filter_id;
12869 			}
12870 		}
12871 	}
12872 	hwrm_req_drop(bp, req);
12873 	return next_handle;
12874 }
12875 
12876 static void bnxt_get_wol_settings(struct bnxt *bp)
12877 {
12878 	u16 handle = 0;
12879 
12880 	bp->wol = 0;
12881 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12882 		return;
12883 
12884 	do {
12885 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12886 	} while (handle && handle != 0xffff);
12887 }
12888 
12889 static bool bnxt_eee_config_ok(struct bnxt *bp)
12890 {
12891 	struct ethtool_keee *eee = &bp->eee;
12892 	struct bnxt_link_info *link_info = &bp->link_info;
12893 
12894 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12895 		return true;
12896 
12897 	if (eee->eee_enabled) {
12898 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12899 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12900 
12901 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12902 
12903 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12904 			eee->eee_enabled = 0;
12905 			return false;
12906 		}
12907 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12908 			linkmode_and(eee->advertised, advertising,
12909 				     eee->supported);
12910 			return false;
12911 		}
12912 	}
12913 	return true;
12914 }
12915 
12916 static int bnxt_update_phy_setting(struct bnxt *bp)
12917 {
12918 	int rc;
12919 	bool update_link = false;
12920 	bool update_pause = false;
12921 	bool update_eee = false;
12922 	struct bnxt_link_info *link_info = &bp->link_info;
12923 
12924 	rc = bnxt_update_link(bp, true);
12925 	if (rc) {
12926 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12927 			   rc);
12928 		return rc;
12929 	}
12930 	if (!BNXT_SINGLE_PF(bp))
12931 		return 0;
12932 
12933 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12934 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12935 	    link_info->req_flow_ctrl)
12936 		update_pause = true;
12937 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12938 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12939 		update_pause = true;
12940 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12941 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12942 			update_link = true;
12943 		if (bnxt_force_speed_updated(link_info))
12944 			update_link = true;
12945 		if (link_info->req_duplex != link_info->duplex_setting)
12946 			update_link = true;
12947 	} else {
12948 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12949 			update_link = true;
12950 		if (bnxt_auto_speed_updated(link_info))
12951 			update_link = true;
12952 	}
12953 
12954 	/* The last close may have shutdown the link, so need to call
12955 	 * PHY_CFG to bring it back up.
12956 	 */
12957 	if (!BNXT_LINK_IS_UP(bp))
12958 		update_link = true;
12959 
12960 	if (!bnxt_eee_config_ok(bp))
12961 		update_eee = true;
12962 
12963 	if (update_link)
12964 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12965 	else if (update_pause)
12966 		rc = bnxt_hwrm_set_pause(bp);
12967 	if (rc) {
12968 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12969 			   rc);
12970 		return rc;
12971 	}
12972 
12973 	return rc;
12974 }
12975 
12976 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12977 
12978 static int bnxt_reinit_after_abort(struct bnxt *bp)
12979 {
12980 	int rc;
12981 
12982 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12983 		return -EBUSY;
12984 
12985 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12986 		return -ENODEV;
12987 
12988 	rc = bnxt_fw_init_one(bp);
12989 	if (!rc) {
12990 		bnxt_clear_int_mode(bp);
12991 		rc = bnxt_init_int_mode(bp);
12992 		if (!rc) {
12993 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12994 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12995 		}
12996 	}
12997 	return rc;
12998 }
12999 
13000 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
13001 {
13002 	struct bnxt_ntuple_filter *ntp_fltr;
13003 	struct bnxt_l2_filter *l2_fltr;
13004 
13005 	if (list_empty(&fltr->list))
13006 		return;
13007 
13008 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
13009 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
13010 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
13011 		atomic_inc(&l2_fltr->refcnt);
13012 		ntp_fltr->l2_fltr = l2_fltr;
13013 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
13014 			bnxt_del_ntp_filter(bp, ntp_fltr);
13015 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
13016 				   fltr->sw_id);
13017 		}
13018 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
13019 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
13020 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
13021 			bnxt_del_l2_filter(bp, l2_fltr);
13022 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
13023 				   fltr->sw_id);
13024 		}
13025 	}
13026 }
13027 
13028 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
13029 {
13030 	struct bnxt_filter_base *usr_fltr, *tmp;
13031 
13032 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
13033 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
13034 }
13035 
13036 static int bnxt_set_xps_mapping(struct bnxt *bp)
13037 {
13038 	int numa_node = dev_to_node(&bp->pdev->dev);
13039 	unsigned int q_idx, map_idx, cpu, i;
13040 	const struct cpumask *cpu_mask_ptr;
13041 	int nr_cpus = num_online_cpus();
13042 	cpumask_t *q_map;
13043 	int rc = 0;
13044 
13045 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
13046 	if (!q_map)
13047 		return -ENOMEM;
13048 
13049 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
13050 	 * Each TC has the same number of TX queues. The nth TX queue for each
13051 	 * TC will have the same CPU mask.
13052 	 */
13053 	for (i = 0; i < nr_cpus; i++) {
13054 		map_idx = i % bp->tx_nr_rings_per_tc;
13055 		cpu = cpumask_local_spread(i, numa_node);
13056 		cpu_mask_ptr = get_cpu_mask(cpu);
13057 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
13058 	}
13059 
13060 	/* Register CPU mask for each TX queue except the ones marked for XDP */
13061 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
13062 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
13063 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
13064 		if (rc) {
13065 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
13066 				    q_idx);
13067 			break;
13068 		}
13069 	}
13070 
13071 	kfree(q_map);
13072 
13073 	return rc;
13074 }
13075 
13076 static int bnxt_tx_nr_rings(struct bnxt *bp)
13077 {
13078 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
13079 			    bp->tx_nr_rings_per_tc;
13080 }
13081 
13082 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
13083 {
13084 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
13085 }
13086 
13087 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
13088 {
13089 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13090 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13091 }
13092 
13093 static void bnxt_adj_tx_rings(struct bnxt *bp)
13094 {
13095 	/* Make adjustments if reserved TX rings are less than requested */
13096 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13097 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13098 	if (bp->tx_nr_rings_xdp)
13099 		bnxt_set_xdp_tx_rings(bp);
13100 }
13101 
13102 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13103 {
13104 	int rc = 0;
13105 
13106 	netif_carrier_off(bp->dev);
13107 	if (irq_re_init) {
13108 		/* Reserve rings now if none were reserved at driver probe. */
13109 		rc = bnxt_init_dflt_ring_mode(bp);
13110 		if (rc) {
13111 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13112 			return rc;
13113 		}
13114 	}
13115 	rc = bnxt_reserve_rings(bp, irq_re_init);
13116 	if (rc)
13117 		return rc;
13118 
13119 	bnxt_adj_tx_rings(bp);
13120 	rc = bnxt_alloc_mem(bp, irq_re_init);
13121 	if (rc) {
13122 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13123 		goto open_err_free_mem;
13124 	}
13125 
13126 	if (irq_re_init) {
13127 		bnxt_init_napi(bp);
13128 		rc = bnxt_request_irq(bp);
13129 		if (rc) {
13130 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13131 			goto open_err_irq;
13132 		}
13133 	}
13134 
13135 	rc = bnxt_init_nic(bp, irq_re_init);
13136 	if (rc) {
13137 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13138 		goto open_err_irq;
13139 	}
13140 
13141 	bnxt_enable_napi(bp);
13142 	bnxt_debug_dev_init(bp);
13143 
13144 	if (link_re_init) {
13145 		mutex_lock(&bp->link_lock);
13146 		rc = bnxt_update_phy_setting(bp);
13147 		mutex_unlock(&bp->link_lock);
13148 		if (rc) {
13149 			netdev_warn(bp->dev, "failed to update phy settings\n");
13150 			if (BNXT_SINGLE_PF(bp)) {
13151 				bp->link_info.phy_retry = true;
13152 				bp->link_info.phy_retry_expires =
13153 					jiffies + 5 * HZ;
13154 			}
13155 		}
13156 	}
13157 
13158 	if (irq_re_init) {
13159 		udp_tunnel_nic_reset_ntf(bp->dev);
13160 		rc = bnxt_set_xps_mapping(bp);
13161 		if (rc)
13162 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13163 	}
13164 
13165 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13166 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13167 			static_branch_enable(&bnxt_xdp_locking_key);
13168 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13169 		static_branch_disable(&bnxt_xdp_locking_key);
13170 	}
13171 	set_bit(BNXT_STATE_OPEN, &bp->state);
13172 	bnxt_enable_int(bp);
13173 	/* Enable TX queues */
13174 	bnxt_tx_enable(bp);
13175 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13176 	/* Poll link status and check for SFP+ module status */
13177 	mutex_lock(&bp->link_lock);
13178 	bnxt_get_port_module_status(bp);
13179 	mutex_unlock(&bp->link_lock);
13180 
13181 	/* VF-reps may need to be re-opened after the PF is re-opened */
13182 	if (BNXT_PF(bp))
13183 		bnxt_vf_reps_open(bp);
13184 	bnxt_ptp_init_rtc(bp, true);
13185 	bnxt_ptp_cfg_tstamp_filters(bp);
13186 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13187 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13188 	bnxt_cfg_usr_fltrs(bp);
13189 	return 0;
13190 
13191 open_err_irq:
13192 	bnxt_del_napi(bp);
13193 
13194 open_err_free_mem:
13195 	bnxt_free_skbs(bp);
13196 	bnxt_free_irq(bp);
13197 	bnxt_free_mem(bp, true);
13198 	return rc;
13199 }
13200 
13201 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13202 {
13203 	int rc = 0;
13204 
13205 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13206 		rc = -EIO;
13207 	if (!rc)
13208 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13209 	if (rc) {
13210 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13211 		netif_close(bp->dev);
13212 	}
13213 	return rc;
13214 }
13215 
13216 /* netdev instance lock held, open the NIC half way by allocating all
13217  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13218  * for offline self tests.
13219  */
13220 int bnxt_half_open_nic(struct bnxt *bp)
13221 {
13222 	int rc = 0;
13223 
13224 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13225 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13226 		rc = -ENODEV;
13227 		goto half_open_err;
13228 	}
13229 
13230 	rc = bnxt_alloc_mem(bp, true);
13231 	if (rc) {
13232 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13233 		goto half_open_err;
13234 	}
13235 	bnxt_init_napi(bp);
13236 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13237 	rc = bnxt_init_nic(bp, true);
13238 	if (rc) {
13239 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13240 		bnxt_del_napi(bp);
13241 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13242 		goto half_open_err;
13243 	}
13244 	return 0;
13245 
13246 half_open_err:
13247 	bnxt_free_skbs(bp);
13248 	bnxt_free_mem(bp, true);
13249 	netif_close(bp->dev);
13250 	return rc;
13251 }
13252 
13253 /* netdev instance lock held, this call can only be made after a previous
13254  * successful call to bnxt_half_open_nic().
13255  */
13256 void bnxt_half_close_nic(struct bnxt *bp)
13257 {
13258 	bnxt_hwrm_resource_free(bp, false, true);
13259 	bnxt_del_napi(bp);
13260 	bnxt_free_skbs(bp);
13261 	bnxt_free_mem(bp, true);
13262 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13263 }
13264 
13265 void bnxt_reenable_sriov(struct bnxt *bp)
13266 {
13267 	if (BNXT_PF(bp)) {
13268 		struct bnxt_pf_info *pf = &bp->pf;
13269 		int n = pf->active_vfs;
13270 
13271 		if (n)
13272 			bnxt_cfg_hw_sriov(bp, &n, true);
13273 	}
13274 }
13275 
13276 static int bnxt_open(struct net_device *dev)
13277 {
13278 	struct bnxt *bp = netdev_priv(dev);
13279 	int rc;
13280 
13281 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13282 		rc = bnxt_reinit_after_abort(bp);
13283 		if (rc) {
13284 			if (rc == -EBUSY)
13285 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13286 			else
13287 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13288 			return -ENODEV;
13289 		}
13290 	}
13291 
13292 	rc = bnxt_hwrm_if_change(bp, true);
13293 	if (rc)
13294 		return rc;
13295 
13296 	rc = __bnxt_open_nic(bp, true, true);
13297 	if (rc) {
13298 		bnxt_hwrm_if_change(bp, false);
13299 	} else {
13300 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13301 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13302 				bnxt_queue_sp_work(bp,
13303 						   BNXT_RESTART_ULP_SP_EVENT);
13304 		}
13305 	}
13306 
13307 	return rc;
13308 }
13309 
13310 static bool bnxt_drv_busy(struct bnxt *bp)
13311 {
13312 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13313 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13314 }
13315 
13316 static void bnxt_get_ring_stats(struct bnxt *bp,
13317 				struct rtnl_link_stats64 *stats);
13318 
13319 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13320 			     bool link_re_init)
13321 {
13322 	/* Close the VF-reps before closing PF */
13323 	if (BNXT_PF(bp))
13324 		bnxt_vf_reps_close(bp);
13325 
13326 	/* Change device state to avoid TX queue wake up's */
13327 	bnxt_tx_disable(bp);
13328 
13329 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13330 	smp_mb__after_atomic();
13331 	while (bnxt_drv_busy(bp))
13332 		msleep(20);
13333 
13334 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13335 		bnxt_clear_rss_ctxs(bp);
13336 	/* Flush rings and disable interrupts */
13337 	bnxt_shutdown_nic(bp, irq_re_init);
13338 
13339 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13340 
13341 	bnxt_debug_dev_exit(bp);
13342 	bnxt_disable_napi(bp);
13343 	timer_delete_sync(&bp->timer);
13344 	bnxt_free_skbs(bp);
13345 
13346 	/* Save ring stats before shutdown */
13347 	if (bp->bnapi && irq_re_init) {
13348 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13349 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13350 	}
13351 	if (irq_re_init) {
13352 		bnxt_free_irq(bp);
13353 		bnxt_del_napi(bp);
13354 	}
13355 	bnxt_free_mem(bp, irq_re_init);
13356 }
13357 
13358 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13359 {
13360 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13361 		/* If we get here, it means firmware reset is in progress
13362 		 * while we are trying to close.  We can safely proceed with
13363 		 * the close because we are holding netdev instance lock.
13364 		 * Some firmware messages may fail as we proceed to close.
13365 		 * We set the ABORT_ERR flag here so that the FW reset thread
13366 		 * will later abort when it gets the netdev instance lock
13367 		 * and sees the flag.
13368 		 */
13369 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13370 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13371 	}
13372 
13373 #ifdef CONFIG_BNXT_SRIOV
13374 	if (bp->sriov_cfg) {
13375 		int rc;
13376 
13377 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13378 						      !bp->sriov_cfg,
13379 						      BNXT_SRIOV_CFG_WAIT_TMO);
13380 		if (!rc)
13381 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13382 		else if (rc < 0)
13383 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13384 	}
13385 #endif
13386 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13387 }
13388 
13389 static int bnxt_close(struct net_device *dev)
13390 {
13391 	struct bnxt *bp = netdev_priv(dev);
13392 
13393 	bnxt_close_nic(bp, true, true);
13394 	bnxt_hwrm_shutdown_link(bp);
13395 	bnxt_hwrm_if_change(bp, false);
13396 	return 0;
13397 }
13398 
13399 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13400 				   u16 *val)
13401 {
13402 	struct hwrm_port_phy_mdio_read_output *resp;
13403 	struct hwrm_port_phy_mdio_read_input *req;
13404 	int rc;
13405 
13406 	if (bp->hwrm_spec_code < 0x10a00)
13407 		return -EOPNOTSUPP;
13408 
13409 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13410 	if (rc)
13411 		return rc;
13412 
13413 	req->port_id = cpu_to_le16(bp->pf.port_id);
13414 	req->phy_addr = phy_addr;
13415 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13416 	if (mdio_phy_id_is_c45(phy_addr)) {
13417 		req->cl45_mdio = 1;
13418 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13419 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13420 		req->reg_addr = cpu_to_le16(reg);
13421 	}
13422 
13423 	resp = hwrm_req_hold(bp, req);
13424 	rc = hwrm_req_send(bp, req);
13425 	if (!rc)
13426 		*val = le16_to_cpu(resp->reg_data);
13427 	hwrm_req_drop(bp, req);
13428 	return rc;
13429 }
13430 
13431 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13432 				    u16 val)
13433 {
13434 	struct hwrm_port_phy_mdio_write_input *req;
13435 	int rc;
13436 
13437 	if (bp->hwrm_spec_code < 0x10a00)
13438 		return -EOPNOTSUPP;
13439 
13440 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13441 	if (rc)
13442 		return rc;
13443 
13444 	req->port_id = cpu_to_le16(bp->pf.port_id);
13445 	req->phy_addr = phy_addr;
13446 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13447 	if (mdio_phy_id_is_c45(phy_addr)) {
13448 		req->cl45_mdio = 1;
13449 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13450 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13451 		req->reg_addr = cpu_to_le16(reg);
13452 	}
13453 	req->reg_data = cpu_to_le16(val);
13454 
13455 	return hwrm_req_send(bp, req);
13456 }
13457 
13458 /* netdev instance lock held */
13459 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13460 {
13461 	struct mii_ioctl_data *mdio = if_mii(ifr);
13462 	struct bnxt *bp = netdev_priv(dev);
13463 	int rc;
13464 
13465 	switch (cmd) {
13466 	case SIOCGMIIPHY:
13467 		mdio->phy_id = bp->link_info.phy_addr;
13468 
13469 		fallthrough;
13470 	case SIOCGMIIREG: {
13471 		u16 mii_regval = 0;
13472 
13473 		if (!netif_running(dev))
13474 			return -EAGAIN;
13475 
13476 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13477 					     &mii_regval);
13478 		mdio->val_out = mii_regval;
13479 		return rc;
13480 	}
13481 
13482 	case SIOCSMIIREG:
13483 		if (!netif_running(dev))
13484 			return -EAGAIN;
13485 
13486 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13487 						mdio->val_in);
13488 
13489 	default:
13490 		/* do nothing */
13491 		break;
13492 	}
13493 	return -EOPNOTSUPP;
13494 }
13495 
13496 static void bnxt_get_ring_stats(struct bnxt *bp,
13497 				struct rtnl_link_stats64 *stats)
13498 {
13499 	int i;
13500 
13501 	for (i = 0; i < bp->cp_nr_rings; i++) {
13502 		struct bnxt_napi *bnapi = bp->bnapi[i];
13503 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13504 		u64 *sw = cpr->stats.sw_stats;
13505 
13506 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13507 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13508 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13509 
13510 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13511 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13512 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13513 
13514 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13515 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13516 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13517 
13518 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13519 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13520 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13521 
13522 		stats->rx_missed_errors +=
13523 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13524 
13525 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13526 
13527 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13528 
13529 		stats->rx_dropped +=
13530 			cpr->sw_stats->rx.rx_netpoll_discards +
13531 			cpr->sw_stats->rx.rx_oom_discards;
13532 	}
13533 }
13534 
13535 static void bnxt_add_prev_stats(struct bnxt *bp,
13536 				struct rtnl_link_stats64 *stats)
13537 {
13538 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13539 
13540 	stats->rx_packets += prev_stats->rx_packets;
13541 	stats->tx_packets += prev_stats->tx_packets;
13542 	stats->rx_bytes += prev_stats->rx_bytes;
13543 	stats->tx_bytes += prev_stats->tx_bytes;
13544 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13545 	stats->multicast += prev_stats->multicast;
13546 	stats->rx_dropped += prev_stats->rx_dropped;
13547 	stats->tx_dropped += prev_stats->tx_dropped;
13548 }
13549 
13550 static void
13551 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13552 {
13553 	struct bnxt *bp = netdev_priv(dev);
13554 
13555 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13556 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13557 	 * we check the BNXT_STATE_OPEN flag.
13558 	 */
13559 	smp_mb__after_atomic();
13560 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13561 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13562 		*stats = bp->net_stats_prev;
13563 		return;
13564 	}
13565 
13566 	bnxt_get_ring_stats(bp, stats);
13567 	bnxt_add_prev_stats(bp, stats);
13568 
13569 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13570 		u64 *rx = bp->port_stats.sw_stats;
13571 		u64 *tx = bp->port_stats.sw_stats +
13572 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13573 
13574 		stats->rx_crc_errors =
13575 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13576 		stats->rx_frame_errors =
13577 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13578 		stats->rx_length_errors =
13579 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13580 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13581 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13582 		stats->rx_errors =
13583 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13584 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13585 		stats->collisions =
13586 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13587 		stats->tx_fifo_errors =
13588 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13589 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13590 	}
13591 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13592 }
13593 
13594 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13595 					struct bnxt_total_ring_drv_stats *stats,
13596 					struct bnxt_cp_ring_info *cpr)
13597 {
13598 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13599 	u64 *hw_stats = cpr->stats.sw_stats;
13600 
13601 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13602 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13603 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13604 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13605 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13606 	stats->rx_total_ring_discards +=
13607 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13608 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13609 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13610 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13611 	stats->tx_total_ring_discards +=
13612 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13613 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13614 }
13615 
13616 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13617 			     struct bnxt_total_ring_drv_stats *stats)
13618 {
13619 	int i;
13620 
13621 	for (i = 0; i < bp->cp_nr_rings; i++)
13622 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13623 }
13624 
13625 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13626 {
13627 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13628 	struct net_device *dev = bp->dev;
13629 	struct netdev_hw_addr *ha;
13630 	u8 *haddr;
13631 	int mc_count = 0;
13632 	bool update = false;
13633 	int off = 0;
13634 
13635 	netdev_for_each_mc_addr(ha, dev) {
13636 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13637 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13638 			vnic->mc_list_count = 0;
13639 			return false;
13640 		}
13641 		haddr = ha->addr;
13642 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13643 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13644 			update = true;
13645 		}
13646 		off += ETH_ALEN;
13647 		mc_count++;
13648 	}
13649 	if (mc_count)
13650 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13651 
13652 	if (mc_count != vnic->mc_list_count) {
13653 		vnic->mc_list_count = mc_count;
13654 		update = true;
13655 	}
13656 	return update;
13657 }
13658 
13659 static bool bnxt_uc_list_updated(struct bnxt *bp)
13660 {
13661 	struct net_device *dev = bp->dev;
13662 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13663 	struct netdev_hw_addr *ha;
13664 	int off = 0;
13665 
13666 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13667 		return true;
13668 
13669 	netdev_for_each_uc_addr(ha, dev) {
13670 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13671 			return true;
13672 
13673 		off += ETH_ALEN;
13674 	}
13675 	return false;
13676 }
13677 
13678 static void bnxt_set_rx_mode(struct net_device *dev)
13679 {
13680 	struct bnxt *bp = netdev_priv(dev);
13681 	struct bnxt_vnic_info *vnic;
13682 	bool mc_update = false;
13683 	bool uc_update;
13684 	u32 mask;
13685 
13686 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13687 		return;
13688 
13689 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13690 	mask = vnic->rx_mask;
13691 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13692 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13693 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13694 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13695 
13696 	if (dev->flags & IFF_PROMISC)
13697 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13698 
13699 	uc_update = bnxt_uc_list_updated(bp);
13700 
13701 	if (dev->flags & IFF_BROADCAST)
13702 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13703 	if (dev->flags & IFF_ALLMULTI) {
13704 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13705 		vnic->mc_list_count = 0;
13706 	} else if (dev->flags & IFF_MULTICAST) {
13707 		mc_update = bnxt_mc_list_updated(bp, &mask);
13708 	}
13709 
13710 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13711 		vnic->rx_mask = mask;
13712 
13713 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13714 	}
13715 }
13716 
13717 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13718 {
13719 	struct net_device *dev = bp->dev;
13720 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13721 	struct netdev_hw_addr *ha;
13722 	int i, off = 0, rc;
13723 	bool uc_update;
13724 
13725 	netif_addr_lock_bh(dev);
13726 	uc_update = bnxt_uc_list_updated(bp);
13727 	netif_addr_unlock_bh(dev);
13728 
13729 	if (!uc_update)
13730 		goto skip_uc;
13731 
13732 	for (i = 1; i < vnic->uc_filter_count; i++) {
13733 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13734 
13735 		bnxt_hwrm_l2_filter_free(bp, fltr);
13736 		bnxt_del_l2_filter(bp, fltr);
13737 	}
13738 
13739 	vnic->uc_filter_count = 1;
13740 
13741 	netif_addr_lock_bh(dev);
13742 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13743 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13744 	} else {
13745 		netdev_for_each_uc_addr(ha, dev) {
13746 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13747 			off += ETH_ALEN;
13748 			vnic->uc_filter_count++;
13749 		}
13750 	}
13751 	netif_addr_unlock_bh(dev);
13752 
13753 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13754 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13755 		if (rc) {
13756 			if (BNXT_VF(bp) && rc == -ENODEV) {
13757 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13758 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13759 				else
13760 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13761 				rc = 0;
13762 			} else {
13763 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13764 			}
13765 			vnic->uc_filter_count = i;
13766 			return rc;
13767 		}
13768 	}
13769 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13770 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13771 
13772 skip_uc:
13773 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13774 	    !bnxt_promisc_ok(bp))
13775 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13776 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13777 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13778 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13779 			    rc);
13780 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13781 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13782 		vnic->mc_list_count = 0;
13783 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13784 	}
13785 	if (rc)
13786 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13787 			   rc);
13788 
13789 	return rc;
13790 }
13791 
13792 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13793 {
13794 #ifdef CONFIG_BNXT_SRIOV
13795 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13796 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13797 
13798 		/* No minimum rings were provisioned by the PF.  Don't
13799 		 * reserve rings by default when device is down.
13800 		 */
13801 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13802 			return true;
13803 
13804 		if (!netif_running(bp->dev))
13805 			return false;
13806 	}
13807 #endif
13808 	return true;
13809 }
13810 
13811 /* If the chip and firmware supports RFS */
13812 static bool bnxt_rfs_supported(struct bnxt *bp)
13813 {
13814 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13815 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13816 			return true;
13817 		return false;
13818 	}
13819 	/* 212 firmware is broken for aRFS */
13820 	if (BNXT_FW_MAJ(bp) == 212)
13821 		return false;
13822 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13823 		return true;
13824 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13825 		return true;
13826 	return false;
13827 }
13828 
13829 /* If runtime conditions support RFS */
13830 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13831 {
13832 	struct bnxt_hw_rings hwr = {0};
13833 	int max_vnics, max_rss_ctxs;
13834 
13835 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13836 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13837 		return bnxt_rfs_supported(bp);
13838 
13839 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13840 		return false;
13841 
13842 	hwr.grp = bp->rx_nr_rings;
13843 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13844 	if (new_rss_ctx)
13845 		hwr.vnic++;
13846 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13847 	max_vnics = bnxt_get_max_func_vnics(bp);
13848 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13849 
13850 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13851 		if (bp->rx_nr_rings > 1)
13852 			netdev_warn(bp->dev,
13853 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13854 				    min(max_rss_ctxs - 1, max_vnics - 1));
13855 		return false;
13856 	}
13857 
13858 	if (!BNXT_NEW_RM(bp))
13859 		return true;
13860 
13861 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13862 	 * issue that will mess up the default VNIC if we reduce the
13863 	 * reservations.
13864 	 */
13865 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13866 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13867 		return true;
13868 
13869 	bnxt_hwrm_reserve_rings(bp, &hwr);
13870 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13871 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13872 		return true;
13873 
13874 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13875 	hwr.vnic = 1;
13876 	hwr.rss_ctx = 0;
13877 	bnxt_hwrm_reserve_rings(bp, &hwr);
13878 	return false;
13879 }
13880 
13881 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13882 					   netdev_features_t features)
13883 {
13884 	struct bnxt *bp = netdev_priv(dev);
13885 	netdev_features_t vlan_features;
13886 
13887 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13888 		features &= ~NETIF_F_NTUPLE;
13889 
13890 	if ((features & NETIF_F_GSO_UDP_L4) &&
13891 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
13892 	    bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS)
13893 		features &= ~NETIF_F_GSO_UDP_L4;
13894 
13895 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13896 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13897 
13898 	if (!(features & NETIF_F_GRO))
13899 		features &= ~NETIF_F_GRO_HW;
13900 
13901 	if (features & NETIF_F_GRO_HW)
13902 		features &= ~NETIF_F_LRO;
13903 
13904 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13905 	 * turned on or off together.
13906 	 */
13907 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13908 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13909 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13910 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13911 		else if (vlan_features)
13912 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13913 	}
13914 #ifdef CONFIG_BNXT_SRIOV
13915 	if (BNXT_VF(bp) && bp->vf.vlan)
13916 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13917 #endif
13918 	return features;
13919 }
13920 
13921 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13922 				bool link_re_init, u32 flags, bool update_tpa)
13923 {
13924 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13925 	bp->flags = flags;
13926 	if (update_tpa)
13927 		bnxt_set_ring_params(bp);
13928 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13929 }
13930 
13931 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13932 {
13933 	bool update_tpa = false, update_ntuple = false;
13934 	struct bnxt *bp = netdev_priv(dev);
13935 	u32 flags = bp->flags;
13936 	u32 changes;
13937 	int rc = 0;
13938 	bool re_init = false;
13939 
13940 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
13941 				   bnxt_min_tx_desc_cnt(bp, features));
13942 
13943 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13944 	if (features & NETIF_F_GRO_HW)
13945 		flags |= BNXT_FLAG_GRO;
13946 	else if (features & NETIF_F_LRO)
13947 		flags |= BNXT_FLAG_LRO;
13948 
13949 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13950 		flags &= ~BNXT_FLAG_TPA;
13951 
13952 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13953 		flags |= BNXT_FLAG_STRIP_VLAN;
13954 
13955 	if (features & NETIF_F_NTUPLE)
13956 		flags |= BNXT_FLAG_RFS;
13957 	else
13958 		bnxt_clear_usr_fltrs(bp, true);
13959 
13960 	changes = flags ^ bp->flags;
13961 	if (changes & BNXT_FLAG_TPA) {
13962 		update_tpa = true;
13963 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13964 		    (flags & BNXT_FLAG_TPA) == 0 ||
13965 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13966 			re_init = true;
13967 	}
13968 
13969 	if (changes & ~BNXT_FLAG_TPA)
13970 		re_init = true;
13971 
13972 	if (changes & BNXT_FLAG_RFS)
13973 		update_ntuple = true;
13974 
13975 	if (flags != bp->flags) {
13976 		u32 old_flags = bp->flags;
13977 
13978 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13979 			bp->flags = flags;
13980 			if (update_tpa)
13981 				bnxt_set_ring_params(bp);
13982 			return rc;
13983 		}
13984 
13985 		if (update_ntuple)
13986 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13987 
13988 		if (re_init)
13989 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13990 
13991 		if (update_tpa) {
13992 			bp->flags = flags;
13993 			rc = bnxt_set_tpa(bp,
13994 					  (flags & BNXT_FLAG_TPA) ?
13995 					  true : false);
13996 			if (rc)
13997 				bp->flags = old_flags;
13998 		}
13999 	}
14000 	return rc;
14001 }
14002 
14003 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
14004 			      u8 **nextp)
14005 {
14006 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
14007 	int hdr_count = 0;
14008 	u8 *nexthdr;
14009 	int start;
14010 
14011 	/* Check that there are at most 2 IPv6 extension headers, no
14012 	 * fragment header, and each is <= 64 bytes.
14013 	 */
14014 	start = nw_off + sizeof(*ip6h);
14015 	nexthdr = &ip6h->nexthdr;
14016 	while (ipv6_ext_hdr(*nexthdr)) {
14017 		struct ipv6_opt_hdr *hp;
14018 		int hdrlen;
14019 
14020 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
14021 		    *nexthdr == NEXTHDR_FRAGMENT)
14022 			return false;
14023 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
14024 					  skb_headlen(skb), NULL);
14025 		if (!hp)
14026 			return false;
14027 		if (*nexthdr == NEXTHDR_AUTH)
14028 			hdrlen = ipv6_authlen(hp);
14029 		else
14030 			hdrlen = ipv6_optlen(hp);
14031 
14032 		if (hdrlen > 64)
14033 			return false;
14034 
14035 		hdr_count++;
14036 		nexthdr = &hp->nexthdr;
14037 		start += hdrlen;
14038 	}
14039 	if (nextp) {
14040 		/* Caller will check inner protocol */
14041 		if (skb->encapsulation) {
14042 			*nextp = nexthdr;
14043 			return true;
14044 		}
14045 		*nextp = NULL;
14046 	}
14047 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
14048 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
14049 }
14050 
14051 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
14052 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
14053 {
14054 	struct udphdr *uh = udp_hdr(skb);
14055 	__be16 udp_port = uh->dest;
14056 
14057 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
14058 	    udp_port != bp->vxlan_gpe_port)
14059 		return false;
14060 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
14061 		struct ethhdr *eh = inner_eth_hdr(skb);
14062 
14063 		switch (eh->h_proto) {
14064 		case htons(ETH_P_IP):
14065 			return true;
14066 		case htons(ETH_P_IPV6):
14067 			return bnxt_exthdr_check(bp, skb,
14068 						 skb_inner_network_offset(skb),
14069 						 NULL);
14070 		}
14071 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
14072 		return true;
14073 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
14074 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14075 					 NULL);
14076 	}
14077 	return false;
14078 }
14079 
14080 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
14081 {
14082 	switch (l4_proto) {
14083 	case IPPROTO_UDP:
14084 		return bnxt_udp_tunl_check(bp, skb);
14085 	case IPPROTO_IPIP:
14086 		return true;
14087 	case IPPROTO_GRE: {
14088 		switch (skb->inner_protocol) {
14089 		default:
14090 			return false;
14091 		case htons(ETH_P_IP):
14092 			return true;
14093 		case htons(ETH_P_IPV6):
14094 			fallthrough;
14095 		}
14096 	}
14097 	case IPPROTO_IPV6:
14098 		/* Check ext headers of inner ipv6 */
14099 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14100 					 NULL);
14101 	}
14102 	return false;
14103 }
14104 
14105 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
14106 					     struct net_device *dev,
14107 					     netdev_features_t features)
14108 {
14109 	struct bnxt *bp = netdev_priv(dev);
14110 	u8 *l4_proto;
14111 
14112 	features = vlan_features_check(skb, features);
14113 	switch (vlan_get_protocol(skb)) {
14114 	case htons(ETH_P_IP):
14115 		if (!skb->encapsulation)
14116 			return features;
14117 		l4_proto = &ip_hdr(skb)->protocol;
14118 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14119 			return features;
14120 		break;
14121 	case htons(ETH_P_IPV6):
14122 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14123 				       &l4_proto))
14124 			break;
14125 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14126 			return features;
14127 		break;
14128 	}
14129 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14130 }
14131 
14132 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14133 			 u32 *reg_buf)
14134 {
14135 	struct hwrm_dbg_read_direct_output *resp;
14136 	struct hwrm_dbg_read_direct_input *req;
14137 	__le32 *dbg_reg_buf;
14138 	dma_addr_t mapping;
14139 	int rc, i;
14140 
14141 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14142 	if (rc)
14143 		return rc;
14144 
14145 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14146 					 &mapping);
14147 	if (!dbg_reg_buf) {
14148 		rc = -ENOMEM;
14149 		goto dbg_rd_reg_exit;
14150 	}
14151 
14152 	req->host_dest_addr = cpu_to_le64(mapping);
14153 
14154 	resp = hwrm_req_hold(bp, req);
14155 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14156 	req->read_len32 = cpu_to_le32(num_words);
14157 
14158 	rc = hwrm_req_send(bp, req);
14159 	if (rc || resp->error_code) {
14160 		rc = -EIO;
14161 		goto dbg_rd_reg_exit;
14162 	}
14163 	for (i = 0; i < num_words; i++)
14164 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14165 
14166 dbg_rd_reg_exit:
14167 	hwrm_req_drop(bp, req);
14168 	return rc;
14169 }
14170 
14171 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14172 				       u32 ring_id, u32 *prod, u32 *cons)
14173 {
14174 	struct hwrm_dbg_ring_info_get_output *resp;
14175 	struct hwrm_dbg_ring_info_get_input *req;
14176 	int rc;
14177 
14178 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14179 	if (rc)
14180 		return rc;
14181 
14182 	req->ring_type = ring_type;
14183 	req->fw_ring_id = cpu_to_le32(ring_id);
14184 	resp = hwrm_req_hold(bp, req);
14185 	rc = hwrm_req_send(bp, req);
14186 	if (!rc) {
14187 		*prod = le32_to_cpu(resp->producer_index);
14188 		*cons = le32_to_cpu(resp->consumer_index);
14189 	}
14190 	hwrm_req_drop(bp, req);
14191 	return rc;
14192 }
14193 
14194 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14195 {
14196 	struct bnxt_tx_ring_info *txr;
14197 	int i = bnapi->index, j;
14198 
14199 	bnxt_for_each_napi_tx(j, bnapi, txr)
14200 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14201 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14202 			    txr->tx_cons);
14203 }
14204 
14205 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14206 {
14207 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14208 	int i = bnapi->index;
14209 
14210 	if (!rxr)
14211 		return;
14212 
14213 	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",
14214 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14215 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14216 		    rxr->rx_sw_agg_prod);
14217 }
14218 
14219 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14220 {
14221 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14222 	int i = bnapi->index, j;
14223 
14224 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14225 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14226 	for (j = 0; j < cpr->cp_ring_count; j++) {
14227 		cpr2 = &cpr->cp_ring_arr[j];
14228 		if (!cpr2->bnapi)
14229 			continue;
14230 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14231 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14232 			    cpr2->cp_raw_cons);
14233 	}
14234 }
14235 
14236 static void bnxt_dbg_dump_states(struct bnxt *bp)
14237 {
14238 	int i;
14239 	struct bnxt_napi *bnapi;
14240 
14241 	for (i = 0; i < bp->cp_nr_rings; i++) {
14242 		bnapi = bp->bnapi[i];
14243 		if (netif_msg_drv(bp)) {
14244 			bnxt_dump_tx_sw_state(bnapi);
14245 			bnxt_dump_rx_sw_state(bnapi);
14246 			bnxt_dump_cp_sw_state(bnapi);
14247 		}
14248 	}
14249 }
14250 
14251 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14252 {
14253 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14254 	struct hwrm_ring_reset_input *req;
14255 	struct bnxt_napi *bnapi = rxr->bnapi;
14256 	struct bnxt_cp_ring_info *cpr;
14257 	u16 cp_ring_id;
14258 	int rc;
14259 
14260 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14261 	if (rc)
14262 		return rc;
14263 
14264 	cpr = &bnapi->cp_ring;
14265 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14266 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14267 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14268 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14269 	return hwrm_req_send_silent(bp, req);
14270 }
14271 
14272 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14273 {
14274 	if (!silent)
14275 		bnxt_dbg_dump_states(bp);
14276 	if (netif_running(bp->dev)) {
14277 		bnxt_close_nic(bp, !silent, false);
14278 		bnxt_open_nic(bp, !silent, false);
14279 	}
14280 }
14281 
14282 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14283 {
14284 	struct bnxt *bp = netdev_priv(dev);
14285 
14286 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14287 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14288 }
14289 
14290 static void bnxt_fw_health_check(struct bnxt *bp)
14291 {
14292 	struct bnxt_fw_health *fw_health = bp->fw_health;
14293 	struct pci_dev *pdev = bp->pdev;
14294 	u32 val;
14295 
14296 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14297 		return;
14298 
14299 	/* Make sure it is enabled before checking the tmr_counter. */
14300 	smp_rmb();
14301 	if (fw_health->tmr_counter) {
14302 		fw_health->tmr_counter--;
14303 		return;
14304 	}
14305 
14306 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14307 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14308 		fw_health->arrests++;
14309 		goto fw_reset;
14310 	}
14311 
14312 	fw_health->last_fw_heartbeat = val;
14313 
14314 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14315 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14316 		fw_health->discoveries++;
14317 		goto fw_reset;
14318 	}
14319 
14320 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14321 	return;
14322 
14323 fw_reset:
14324 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14325 }
14326 
14327 static void bnxt_timer(struct timer_list *t)
14328 {
14329 	struct bnxt *bp = timer_container_of(bp, t, timer);
14330 	struct net_device *dev = bp->dev;
14331 
14332 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14333 		return;
14334 
14335 	if (atomic_read(&bp->intr_sem) != 0)
14336 		goto bnxt_restart_timer;
14337 
14338 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14339 		bnxt_fw_health_check(bp);
14340 
14341 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14342 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14343 
14344 	if (bnxt_tc_flower_enabled(bp))
14345 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14346 
14347 #ifdef CONFIG_RFS_ACCEL
14348 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14349 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14350 #endif /*CONFIG_RFS_ACCEL*/
14351 
14352 	if (bp->link_info.phy_retry) {
14353 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14354 			bp->link_info.phy_retry = false;
14355 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14356 		} else {
14357 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14358 		}
14359 	}
14360 
14361 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14362 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14363 
14364 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14365 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14366 
14367 bnxt_restart_timer:
14368 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14369 }
14370 
14371 static void bnxt_lock_sp(struct bnxt *bp)
14372 {
14373 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14374 	 * set.  If the device is being closed, bnxt_close() may be holding
14375 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14376 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14377 	 * instance lock.
14378 	 */
14379 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14380 	netdev_lock(bp->dev);
14381 }
14382 
14383 static void bnxt_unlock_sp(struct bnxt *bp)
14384 {
14385 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14386 	netdev_unlock(bp->dev);
14387 }
14388 
14389 /* Only called from bnxt_sp_task() */
14390 static void bnxt_reset(struct bnxt *bp, bool silent)
14391 {
14392 	bnxt_lock_sp(bp);
14393 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14394 		bnxt_reset_task(bp, silent);
14395 	bnxt_unlock_sp(bp);
14396 }
14397 
14398 /* Only called from bnxt_sp_task() */
14399 static void bnxt_rx_ring_reset(struct bnxt *bp)
14400 {
14401 	int i;
14402 
14403 	bnxt_lock_sp(bp);
14404 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14405 		bnxt_unlock_sp(bp);
14406 		return;
14407 	}
14408 	/* Disable and flush TPA before resetting the RX ring */
14409 	if (bp->flags & BNXT_FLAG_TPA)
14410 		bnxt_set_tpa(bp, false);
14411 	for (i = 0; i < bp->rx_nr_rings; i++) {
14412 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14413 		struct bnxt_cp_ring_info *cpr;
14414 		int rc;
14415 
14416 		if (!rxr->bnapi->in_reset)
14417 			continue;
14418 
14419 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14420 		if (rc) {
14421 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14422 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14423 			else
14424 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14425 					    rc);
14426 			bnxt_reset_task(bp, true);
14427 			break;
14428 		}
14429 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14430 		rxr->rx_prod = 0;
14431 		rxr->rx_agg_prod = 0;
14432 		rxr->rx_sw_agg_prod = 0;
14433 		rxr->rx_next_cons = 0;
14434 		rxr->bnapi->in_reset = false;
14435 		bnxt_alloc_one_rx_ring(bp, i);
14436 		cpr = &rxr->bnapi->cp_ring;
14437 		cpr->sw_stats->rx.rx_resets++;
14438 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14439 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14440 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14441 	}
14442 	if (bp->flags & BNXT_FLAG_TPA)
14443 		bnxt_set_tpa(bp, true);
14444 	bnxt_unlock_sp(bp);
14445 }
14446 
14447 static void bnxt_fw_fatal_close(struct bnxt *bp)
14448 {
14449 	bnxt_tx_disable(bp);
14450 	bnxt_disable_napi(bp);
14451 	bnxt_disable_int_sync(bp);
14452 	bnxt_free_irq(bp);
14453 	bnxt_clear_int_mode(bp);
14454 	pci_disable_device(bp->pdev);
14455 }
14456 
14457 static void bnxt_fw_reset_close(struct bnxt *bp)
14458 {
14459 	/* When firmware is in fatal state, quiesce device and disable
14460 	 * bus master to prevent any potential bad DMAs before freeing
14461 	 * kernel memory.
14462 	 */
14463 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14464 		u16 val = 0;
14465 
14466 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14467 		if (val == 0xffff)
14468 			bp->fw_reset_min_dsecs = 0;
14469 		bnxt_fw_fatal_close(bp);
14470 	}
14471 	__bnxt_close_nic(bp, true, false);
14472 	bnxt_vf_reps_free(bp);
14473 	bnxt_clear_int_mode(bp);
14474 	bnxt_hwrm_func_drv_unrgtr(bp);
14475 	if (pci_is_enabled(bp->pdev))
14476 		pci_disable_device(bp->pdev);
14477 	bnxt_free_ctx_mem(bp, false);
14478 }
14479 
14480 static bool is_bnxt_fw_ok(struct bnxt *bp)
14481 {
14482 	struct bnxt_fw_health *fw_health = bp->fw_health;
14483 	bool no_heartbeat = false, has_reset = false;
14484 	u32 val;
14485 
14486 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14487 	if (val == fw_health->last_fw_heartbeat)
14488 		no_heartbeat = true;
14489 
14490 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14491 	if (val != fw_health->last_fw_reset_cnt)
14492 		has_reset = true;
14493 
14494 	if (!no_heartbeat && has_reset)
14495 		return true;
14496 
14497 	return false;
14498 }
14499 
14500 /* netdev instance lock is acquired before calling this function */
14501 static void bnxt_force_fw_reset(struct bnxt *bp)
14502 {
14503 	struct bnxt_fw_health *fw_health = bp->fw_health;
14504 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14505 	u32 wait_dsecs;
14506 
14507 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14508 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14509 		return;
14510 
14511 	/* we have to serialize with bnxt_refclk_read()*/
14512 	if (ptp) {
14513 		unsigned long flags;
14514 
14515 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14516 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14517 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14518 	} else {
14519 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14520 	}
14521 	bnxt_fw_reset_close(bp);
14522 	wait_dsecs = fw_health->master_func_wait_dsecs;
14523 	if (fw_health->primary) {
14524 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14525 			wait_dsecs = 0;
14526 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14527 	} else {
14528 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14529 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14530 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14531 	}
14532 
14533 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14534 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14535 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14536 }
14537 
14538 void bnxt_fw_exception(struct bnxt *bp)
14539 {
14540 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14541 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14542 	bnxt_ulp_stop(bp);
14543 	bnxt_lock_sp(bp);
14544 	bnxt_force_fw_reset(bp);
14545 	bnxt_unlock_sp(bp);
14546 }
14547 
14548 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14549  * < 0 on error.
14550  */
14551 static int bnxt_get_registered_vfs(struct bnxt *bp)
14552 {
14553 #ifdef CONFIG_BNXT_SRIOV
14554 	int rc;
14555 
14556 	if (!BNXT_PF(bp))
14557 		return 0;
14558 
14559 	rc = bnxt_hwrm_func_qcfg(bp);
14560 	if (rc) {
14561 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14562 		return rc;
14563 	}
14564 	if (bp->pf.registered_vfs)
14565 		return bp->pf.registered_vfs;
14566 	if (bp->sriov_cfg)
14567 		return 1;
14568 #endif
14569 	return 0;
14570 }
14571 
14572 void bnxt_fw_reset(struct bnxt *bp)
14573 {
14574 	bnxt_ulp_stop(bp);
14575 	bnxt_lock_sp(bp);
14576 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14577 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14578 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14579 		int n = 0, tmo;
14580 
14581 		/* we have to serialize with bnxt_refclk_read()*/
14582 		if (ptp) {
14583 			unsigned long flags;
14584 
14585 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14586 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14587 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14588 		} else {
14589 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14590 		}
14591 		if (bp->pf.active_vfs &&
14592 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14593 			n = bnxt_get_registered_vfs(bp);
14594 		if (n < 0) {
14595 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14596 				   n);
14597 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14598 			netif_close(bp->dev);
14599 			goto fw_reset_exit;
14600 		} else if (n > 0) {
14601 			u16 vf_tmo_dsecs = n * 10;
14602 
14603 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14604 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14605 			bp->fw_reset_state =
14606 				BNXT_FW_RESET_STATE_POLL_VF;
14607 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14608 			goto fw_reset_exit;
14609 		}
14610 		bnxt_fw_reset_close(bp);
14611 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14612 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14613 			tmo = HZ / 10;
14614 		} else {
14615 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14616 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14617 		}
14618 		bnxt_queue_fw_reset_work(bp, tmo);
14619 	}
14620 fw_reset_exit:
14621 	bnxt_unlock_sp(bp);
14622 }
14623 
14624 static void bnxt_chk_missed_irq(struct bnxt *bp)
14625 {
14626 	int i;
14627 
14628 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14629 		return;
14630 
14631 	for (i = 0; i < bp->cp_nr_rings; i++) {
14632 		struct bnxt_napi *bnapi = bp->bnapi[i];
14633 		struct bnxt_cp_ring_info *cpr;
14634 		u32 fw_ring_id;
14635 		int j;
14636 
14637 		if (!bnapi)
14638 			continue;
14639 
14640 		cpr = &bnapi->cp_ring;
14641 		for (j = 0; j < cpr->cp_ring_count; j++) {
14642 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14643 			u32 val[2];
14644 
14645 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14646 				continue;
14647 
14648 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14649 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14650 				continue;
14651 			}
14652 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14653 			bnxt_dbg_hwrm_ring_info_get(bp,
14654 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14655 				fw_ring_id, &val[0], &val[1]);
14656 			cpr->sw_stats->cmn.missed_irqs++;
14657 		}
14658 	}
14659 }
14660 
14661 static void bnxt_cfg_ntp_filters(struct bnxt *);
14662 
14663 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14664 {
14665 	struct bnxt_link_info *link_info = &bp->link_info;
14666 
14667 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14668 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14669 		if (bp->hwrm_spec_code >= 0x10201) {
14670 			if (link_info->auto_pause_setting &
14671 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14672 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14673 		} else {
14674 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14675 		}
14676 		bnxt_set_auto_speed(link_info);
14677 	} else {
14678 		bnxt_set_force_speed(link_info);
14679 		link_info->req_duplex = link_info->duplex_setting;
14680 	}
14681 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14682 		link_info->req_flow_ctrl =
14683 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14684 	else
14685 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14686 }
14687 
14688 static void bnxt_fw_echo_reply(struct bnxt *bp)
14689 {
14690 	struct bnxt_fw_health *fw_health = bp->fw_health;
14691 	struct hwrm_func_echo_response_input *req;
14692 	int rc;
14693 
14694 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14695 	if (rc)
14696 		return;
14697 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14698 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14699 	hwrm_req_send(bp, req);
14700 }
14701 
14702 static void bnxt_ulp_restart(struct bnxt *bp)
14703 {
14704 	bnxt_ulp_stop(bp);
14705 	bnxt_ulp_start(bp);
14706 }
14707 
14708 static void bnxt_sp_task(struct work_struct *work)
14709 {
14710 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14711 
14712 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14713 	smp_mb__after_atomic();
14714 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14715 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14716 		return;
14717 	}
14718 
14719 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14720 		bnxt_ulp_restart(bp);
14721 		bnxt_reenable_sriov(bp);
14722 	}
14723 
14724 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14725 		bnxt_cfg_rx_mode(bp);
14726 
14727 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14728 		bnxt_cfg_ntp_filters(bp);
14729 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14730 		bnxt_hwrm_exec_fwd_req(bp);
14731 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14732 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14733 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14734 		bnxt_hwrm_port_qstats(bp, 0);
14735 		bnxt_hwrm_port_qstats_ext(bp, 0);
14736 		bnxt_accumulate_all_stats(bp);
14737 	}
14738 
14739 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14740 		int rc;
14741 
14742 		mutex_lock(&bp->link_lock);
14743 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14744 				       &bp->sp_event))
14745 			bnxt_hwrm_phy_qcaps(bp);
14746 
14747 		rc = bnxt_update_link(bp, true);
14748 		if (rc)
14749 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14750 				   rc);
14751 
14752 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14753 				       &bp->sp_event))
14754 			bnxt_init_ethtool_link_settings(bp);
14755 		mutex_unlock(&bp->link_lock);
14756 	}
14757 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14758 		int rc;
14759 
14760 		mutex_lock(&bp->link_lock);
14761 		rc = bnxt_update_phy_setting(bp);
14762 		mutex_unlock(&bp->link_lock);
14763 		if (rc) {
14764 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14765 		} else {
14766 			bp->link_info.phy_retry = false;
14767 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14768 		}
14769 	}
14770 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14771 		mutex_lock(&bp->link_lock);
14772 		bnxt_get_port_module_status(bp);
14773 		mutex_unlock(&bp->link_lock);
14774 	}
14775 
14776 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14777 		bnxt_tc_flow_stats_work(bp);
14778 
14779 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14780 		bnxt_chk_missed_irq(bp);
14781 
14782 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14783 		bnxt_fw_echo_reply(bp);
14784 
14785 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14786 		bnxt_hwmon_notify_event(bp);
14787 
14788 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14789 	 * must be the last functions to be called before exiting.
14790 	 */
14791 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14792 		bnxt_reset(bp, false);
14793 
14794 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14795 		bnxt_reset(bp, true);
14796 
14797 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14798 		bnxt_rx_ring_reset(bp);
14799 
14800 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14801 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14802 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14803 			bnxt_devlink_health_fw_report(bp);
14804 		else
14805 			bnxt_fw_reset(bp);
14806 	}
14807 
14808 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14809 		if (!is_bnxt_fw_ok(bp))
14810 			bnxt_devlink_health_fw_report(bp);
14811 	}
14812 
14813 	smp_mb__before_atomic();
14814 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14815 }
14816 
14817 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14818 				int *max_cp);
14819 
14820 /* Under netdev instance lock */
14821 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14822 		     int tx_xdp)
14823 {
14824 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14825 	struct bnxt_hw_rings hwr = {0};
14826 	int rx_rings = rx;
14827 	int rc;
14828 
14829 	if (tcs)
14830 		tx_sets = tcs;
14831 
14832 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14833 
14834 	if (max_rx < rx_rings)
14835 		return -ENOMEM;
14836 
14837 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14838 		rx_rings <<= 1;
14839 
14840 	hwr.rx = rx_rings;
14841 	hwr.tx = tx * tx_sets + tx_xdp;
14842 	if (max_tx < hwr.tx)
14843 		return -ENOMEM;
14844 
14845 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14846 
14847 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14848 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14849 	if (max_cp < hwr.cp)
14850 		return -ENOMEM;
14851 	hwr.stat = hwr.cp;
14852 	if (BNXT_NEW_RM(bp)) {
14853 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14854 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14855 		hwr.grp = rx;
14856 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14857 	}
14858 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14859 		hwr.cp_p5 = hwr.tx + rx;
14860 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14861 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14862 		if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) {
14863 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14864 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14865 		}
14866 		if (hwr.cp > bp->total_irqs) {
14867 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14868 
14869 			if (total_msix < hwr.cp) {
14870 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14871 					    hwr.cp, total_msix);
14872 				rc = -ENOSPC;
14873 			}
14874 		}
14875 	}
14876 	return rc;
14877 }
14878 
14879 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14880 {
14881 	if (bp->bar2) {
14882 		pci_iounmap(pdev, bp->bar2);
14883 		bp->bar2 = NULL;
14884 	}
14885 
14886 	if (bp->bar1) {
14887 		pci_iounmap(pdev, bp->bar1);
14888 		bp->bar1 = NULL;
14889 	}
14890 
14891 	if (bp->bar0) {
14892 		pci_iounmap(pdev, bp->bar0);
14893 		bp->bar0 = NULL;
14894 	}
14895 }
14896 
14897 static void bnxt_cleanup_pci(struct bnxt *bp)
14898 {
14899 	bnxt_unmap_bars(bp, bp->pdev);
14900 	pci_release_regions(bp->pdev);
14901 	if (pci_is_enabled(bp->pdev))
14902 		pci_disable_device(bp->pdev);
14903 }
14904 
14905 static void bnxt_init_dflt_coal(struct bnxt *bp)
14906 {
14907 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14908 	struct bnxt_coal *coal;
14909 	u16 flags = 0;
14910 
14911 	if (coal_cap->cmpl_params &
14912 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14913 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14914 
14915 	/* Tick values in micro seconds.
14916 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14917 	 */
14918 	coal = &bp->rx_coal;
14919 	coal->coal_ticks = 10;
14920 	coal->coal_bufs = 30;
14921 	coal->coal_ticks_irq = 1;
14922 	coal->coal_bufs_irq = 2;
14923 	coal->idle_thresh = 50;
14924 	coal->bufs_per_record = 2;
14925 	coal->budget = 64;		/* NAPI budget */
14926 	coal->flags = flags;
14927 
14928 	coal = &bp->tx_coal;
14929 	coal->coal_ticks = 28;
14930 	coal->coal_bufs = 30;
14931 	coal->coal_ticks_irq = 2;
14932 	coal->coal_bufs_irq = 2;
14933 	coal->bufs_per_record = 1;
14934 	coal->flags = flags;
14935 
14936 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14937 }
14938 
14939 /* FW that pre-reserves 1 VNIC per function */
14940 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14941 {
14942 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14943 
14944 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14945 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14946 		return true;
14947 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14948 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14949 		return true;
14950 	return false;
14951 }
14952 
14953 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
14954 {
14955 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
14956 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
14957 	int rc;
14958 
14959 	bp->max_pfcwd_tmo_ms = 0;
14960 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
14961 	if (rc)
14962 		return;
14963 	resp = hwrm_req_hold(bp, req);
14964 	rc = hwrm_req_send_silent(bp, req);
14965 	if (!rc)
14966 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
14967 	hwrm_req_drop(bp, req);
14968 }
14969 
14970 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14971 {
14972 	int rc;
14973 
14974 	bp->fw_cap = 0;
14975 	rc = bnxt_hwrm_ver_get(bp);
14976 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14977 	 * so wait before continuing with recovery.
14978 	 */
14979 	if (rc)
14980 		msleep(100);
14981 	bnxt_try_map_fw_health_reg(bp);
14982 	if (rc) {
14983 		rc = bnxt_try_recover_fw(bp);
14984 		if (rc)
14985 			return rc;
14986 		rc = bnxt_hwrm_ver_get(bp);
14987 		if (rc)
14988 			return rc;
14989 	}
14990 
14991 	bnxt_nvm_cfg_ver_get(bp);
14992 
14993 	rc = bnxt_hwrm_func_reset(bp);
14994 	if (rc)
14995 		return -ENODEV;
14996 
14997 	bnxt_hwrm_fw_set_time(bp);
14998 	return 0;
14999 }
15000 
15001 static int bnxt_fw_init_one_p2(struct bnxt *bp)
15002 {
15003 	int rc;
15004 
15005 	/* Get the MAX capabilities for this function */
15006 	rc = bnxt_hwrm_func_qcaps(bp);
15007 	if (rc) {
15008 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
15009 			   rc);
15010 		return -ENODEV;
15011 	}
15012 
15013 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
15014 	if (rc)
15015 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
15016 			    rc);
15017 
15018 	if (bnxt_alloc_fw_health(bp)) {
15019 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
15020 	} else {
15021 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
15022 		if (rc)
15023 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
15024 				    rc);
15025 	}
15026 
15027 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
15028 	if (rc)
15029 		return -ENODEV;
15030 
15031 	rc = bnxt_alloc_crash_dump_mem(bp);
15032 	if (rc)
15033 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
15034 			    rc);
15035 	if (!rc) {
15036 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
15037 		if (rc) {
15038 			bnxt_free_crash_dump_mem(bp);
15039 			netdev_warn(bp->dev,
15040 				    "hwrm crash dump mem failure rc: %d\n", rc);
15041 		}
15042 	}
15043 
15044 	if (bnxt_fw_pre_resv_vnics(bp))
15045 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
15046 
15047 	bnxt_hwrm_pfcwd_qcaps(bp);
15048 	bnxt_hwrm_func_qcfg(bp);
15049 	bnxt_hwrm_vnic_qcaps(bp);
15050 	bnxt_hwrm_port_led_qcaps(bp);
15051 	bnxt_ethtool_init(bp);
15052 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
15053 		__bnxt_hwrm_ptp_qcfg(bp);
15054 	bnxt_dcb_init(bp);
15055 	bnxt_hwmon_init(bp);
15056 	return 0;
15057 }
15058 
15059 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
15060 {
15061 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
15062 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
15063 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
15064 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
15065 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
15066 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
15067 		bp->rss_hash_delta = bp->rss_hash_cfg;
15068 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
15069 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
15070 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
15071 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
15072 	}
15073 }
15074 
15075 static void bnxt_set_dflt_rfs(struct bnxt *bp)
15076 {
15077 	struct net_device *dev = bp->dev;
15078 
15079 	dev->hw_features &= ~NETIF_F_NTUPLE;
15080 	dev->features &= ~NETIF_F_NTUPLE;
15081 	bp->flags &= ~BNXT_FLAG_RFS;
15082 	if (bnxt_rfs_supported(bp)) {
15083 		dev->hw_features |= NETIF_F_NTUPLE;
15084 		if (bnxt_rfs_capable(bp, false)) {
15085 			bp->flags |= BNXT_FLAG_RFS;
15086 			dev->features |= NETIF_F_NTUPLE;
15087 		}
15088 	}
15089 }
15090 
15091 static void bnxt_fw_init_one_p3(struct bnxt *bp)
15092 {
15093 	struct pci_dev *pdev = bp->pdev;
15094 
15095 	bnxt_set_dflt_rss_hash_type(bp);
15096 	bnxt_set_dflt_rfs(bp);
15097 
15098 	bnxt_get_wol_settings(bp);
15099 	if (bp->flags & BNXT_FLAG_WOL_CAP)
15100 		device_set_wakeup_enable(&pdev->dev, bp->wol);
15101 	else
15102 		device_set_wakeup_capable(&pdev->dev, false);
15103 
15104 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
15105 	bnxt_hwrm_coal_params_qcaps(bp);
15106 }
15107 
15108 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15109 
15110 int bnxt_fw_init_one(struct bnxt *bp)
15111 {
15112 	int rc;
15113 
15114 	rc = bnxt_fw_init_one_p1(bp);
15115 	if (rc) {
15116 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15117 		return rc;
15118 	}
15119 	rc = bnxt_fw_init_one_p2(bp);
15120 	if (rc) {
15121 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15122 		return rc;
15123 	}
15124 	rc = bnxt_probe_phy(bp, false);
15125 	if (rc)
15126 		return rc;
15127 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15128 	if (rc)
15129 		return rc;
15130 
15131 	bnxt_fw_init_one_p3(bp);
15132 	return 0;
15133 }
15134 
15135 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15136 {
15137 	struct bnxt_fw_health *fw_health = bp->fw_health;
15138 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15139 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15140 	u32 reg_type, reg_off, delay_msecs;
15141 
15142 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15143 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15144 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15145 	switch (reg_type) {
15146 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15147 		pci_write_config_dword(bp->pdev, reg_off, val);
15148 		break;
15149 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15150 		writel(reg_off & BNXT_GRC_BASE_MASK,
15151 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15152 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15153 		fallthrough;
15154 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15155 		writel(val, bp->bar0 + reg_off);
15156 		break;
15157 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15158 		writel(val, bp->bar1 + reg_off);
15159 		break;
15160 	}
15161 	if (delay_msecs) {
15162 		pci_read_config_dword(bp->pdev, 0, &val);
15163 		msleep(delay_msecs);
15164 	}
15165 }
15166 
15167 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15168 {
15169 	struct hwrm_func_qcfg_output *resp;
15170 	struct hwrm_func_qcfg_input *req;
15171 	bool result = true; /* firmware will enforce if unknown */
15172 
15173 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15174 		return result;
15175 
15176 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15177 		return result;
15178 
15179 	req->fid = cpu_to_le16(0xffff);
15180 	resp = hwrm_req_hold(bp, req);
15181 	if (!hwrm_req_send(bp, req))
15182 		result = !!(le16_to_cpu(resp->flags) &
15183 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15184 	hwrm_req_drop(bp, req);
15185 	return result;
15186 }
15187 
15188 static void bnxt_reset_all(struct bnxt *bp)
15189 {
15190 	struct bnxt_fw_health *fw_health = bp->fw_health;
15191 	int i, rc;
15192 
15193 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15194 		bnxt_fw_reset_via_optee(bp);
15195 		bp->fw_reset_timestamp = jiffies;
15196 		return;
15197 	}
15198 
15199 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15200 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15201 			bnxt_fw_reset_writel(bp, i);
15202 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15203 		struct hwrm_fw_reset_input *req;
15204 
15205 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15206 		if (!rc) {
15207 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15208 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15209 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15210 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15211 			rc = hwrm_req_send(bp, req);
15212 		}
15213 		if (rc != -ENODEV)
15214 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15215 	}
15216 	bp->fw_reset_timestamp = jiffies;
15217 }
15218 
15219 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15220 {
15221 	return time_after(jiffies, bp->fw_reset_timestamp +
15222 			  (bp->fw_reset_max_dsecs * HZ / 10));
15223 }
15224 
15225 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15226 {
15227 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15228 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15229 		bnxt_dl_health_fw_status_update(bp, false);
15230 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15231 	netif_close(bp->dev);
15232 }
15233 
15234 static void bnxt_fw_reset_task(struct work_struct *work)
15235 {
15236 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15237 	int rc = 0;
15238 
15239 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15240 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15241 		return;
15242 	}
15243 
15244 	switch (bp->fw_reset_state) {
15245 	case BNXT_FW_RESET_STATE_POLL_VF: {
15246 		int n = bnxt_get_registered_vfs(bp);
15247 		int tmo;
15248 
15249 		if (n < 0) {
15250 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15251 				   n, jiffies_to_msecs(jiffies -
15252 				   bp->fw_reset_timestamp));
15253 			goto fw_reset_abort;
15254 		} else if (n > 0) {
15255 			if (bnxt_fw_reset_timeout(bp)) {
15256 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15257 				bp->fw_reset_state = 0;
15258 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15259 					   n);
15260 				goto ulp_start;
15261 			}
15262 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15263 			return;
15264 		}
15265 		bp->fw_reset_timestamp = jiffies;
15266 		netdev_lock(bp->dev);
15267 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15268 			bnxt_fw_reset_abort(bp, rc);
15269 			netdev_unlock(bp->dev);
15270 			goto ulp_start;
15271 		}
15272 		bnxt_fw_reset_close(bp);
15273 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15274 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15275 			tmo = HZ / 10;
15276 		} else {
15277 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15278 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15279 		}
15280 		netdev_unlock(bp->dev);
15281 		bnxt_queue_fw_reset_work(bp, tmo);
15282 		return;
15283 	}
15284 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15285 		u32 val;
15286 
15287 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15288 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15289 		    !bnxt_fw_reset_timeout(bp)) {
15290 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15291 			return;
15292 		}
15293 
15294 		if (!bp->fw_health->primary) {
15295 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15296 
15297 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15298 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15299 			return;
15300 		}
15301 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15302 	}
15303 		fallthrough;
15304 	case BNXT_FW_RESET_STATE_RESET_FW:
15305 		bnxt_reset_all(bp);
15306 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15307 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15308 		return;
15309 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15310 		bnxt_inv_fw_health_reg(bp);
15311 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15312 		    !bp->fw_reset_min_dsecs) {
15313 			u16 val;
15314 
15315 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15316 			if (val == 0xffff) {
15317 				if (bnxt_fw_reset_timeout(bp)) {
15318 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15319 					rc = -ETIMEDOUT;
15320 					goto fw_reset_abort;
15321 				}
15322 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15323 				return;
15324 			}
15325 		}
15326 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15327 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15328 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15329 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15330 			bnxt_dl_remote_reload(bp);
15331 		if (pci_enable_device(bp->pdev)) {
15332 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15333 			rc = -ENODEV;
15334 			goto fw_reset_abort;
15335 		}
15336 		pci_set_master(bp->pdev);
15337 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15338 		fallthrough;
15339 	case BNXT_FW_RESET_STATE_POLL_FW:
15340 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15341 		rc = bnxt_hwrm_poll(bp);
15342 		if (rc) {
15343 			if (bnxt_fw_reset_timeout(bp)) {
15344 				netdev_err(bp->dev, "Firmware reset aborted\n");
15345 				goto fw_reset_abort_status;
15346 			}
15347 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15348 			return;
15349 		}
15350 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15351 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15352 		fallthrough;
15353 	case BNXT_FW_RESET_STATE_OPENING:
15354 		while (!netdev_trylock(bp->dev)) {
15355 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15356 			return;
15357 		}
15358 		rc = bnxt_open(bp->dev);
15359 		if (rc) {
15360 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15361 			bnxt_fw_reset_abort(bp, rc);
15362 			netdev_unlock(bp->dev);
15363 			goto ulp_start;
15364 		}
15365 
15366 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15367 		    bp->fw_health->enabled) {
15368 			bp->fw_health->last_fw_reset_cnt =
15369 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15370 		}
15371 		bp->fw_reset_state = 0;
15372 		/* Make sure fw_reset_state is 0 before clearing the flag */
15373 		smp_mb__before_atomic();
15374 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15375 		bnxt_ptp_reapply_pps(bp);
15376 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15377 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15378 			bnxt_dl_health_fw_recovery_done(bp);
15379 			bnxt_dl_health_fw_status_update(bp, true);
15380 		}
15381 		netdev_unlock(bp->dev);
15382 		bnxt_ulp_start(bp);
15383 		bnxt_reenable_sriov(bp);
15384 		netdev_lock(bp->dev);
15385 		bnxt_vf_reps_alloc(bp);
15386 		bnxt_vf_reps_open(bp);
15387 		netdev_unlock(bp->dev);
15388 		break;
15389 	}
15390 	return;
15391 
15392 fw_reset_abort_status:
15393 	if (bp->fw_health->status_reliable ||
15394 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15395 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15396 
15397 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15398 	}
15399 fw_reset_abort:
15400 	netdev_lock(bp->dev);
15401 	bnxt_fw_reset_abort(bp, rc);
15402 	netdev_unlock(bp->dev);
15403 ulp_start:
15404 	if (!rc)
15405 		bnxt_ulp_start(bp);
15406 }
15407 
15408 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15409 {
15410 	int rc;
15411 	struct bnxt *bp = netdev_priv(dev);
15412 
15413 	SET_NETDEV_DEV(dev, &pdev->dev);
15414 
15415 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15416 	rc = pci_enable_device(pdev);
15417 	if (rc) {
15418 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15419 		goto init_err;
15420 	}
15421 
15422 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15423 		dev_err(&pdev->dev,
15424 			"Cannot find PCI device base address, aborting\n");
15425 		rc = -ENODEV;
15426 		goto init_err_disable;
15427 	}
15428 
15429 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15430 	if (rc) {
15431 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15432 		goto init_err_disable;
15433 	}
15434 
15435 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15436 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15437 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15438 		rc = -EIO;
15439 		goto init_err_release;
15440 	}
15441 
15442 	pci_set_master(pdev);
15443 
15444 	bp->dev = dev;
15445 	bp->pdev = pdev;
15446 
15447 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15448 	 * determines the BAR size.
15449 	 */
15450 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15451 	if (!bp->bar0) {
15452 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15453 		rc = -ENOMEM;
15454 		goto init_err_release;
15455 	}
15456 
15457 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15458 	if (!bp->bar2) {
15459 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15460 		rc = -ENOMEM;
15461 		goto init_err_release;
15462 	}
15463 
15464 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15465 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15466 
15467 	spin_lock_init(&bp->ntp_fltr_lock);
15468 #if BITS_PER_LONG == 32
15469 	spin_lock_init(&bp->db_lock);
15470 #endif
15471 
15472 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15473 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15474 
15475 	timer_setup(&bp->timer, bnxt_timer, 0);
15476 	bp->current_interval = BNXT_TIMER_INTERVAL;
15477 
15478 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15479 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15480 
15481 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15482 	return 0;
15483 
15484 init_err_release:
15485 	bnxt_unmap_bars(bp, pdev);
15486 	pci_release_regions(pdev);
15487 
15488 init_err_disable:
15489 	pci_disable_device(pdev);
15490 
15491 init_err:
15492 	return rc;
15493 }
15494 
15495 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15496 {
15497 	struct sockaddr *addr = p;
15498 	struct bnxt *bp = netdev_priv(dev);
15499 	int rc = 0;
15500 
15501 	netdev_assert_locked(dev);
15502 
15503 	if (!is_valid_ether_addr(addr->sa_data))
15504 		return -EADDRNOTAVAIL;
15505 
15506 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15507 		return 0;
15508 
15509 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15510 	if (rc)
15511 		return rc;
15512 
15513 	eth_hw_addr_set(dev, addr->sa_data);
15514 	bnxt_clear_usr_fltrs(bp, true);
15515 	if (netif_running(dev)) {
15516 		bnxt_close_nic(bp, false, false);
15517 		rc = bnxt_open_nic(bp, false, false);
15518 	}
15519 
15520 	return rc;
15521 }
15522 
15523 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15524 {
15525 	struct bnxt *bp = netdev_priv(dev);
15526 
15527 	netdev_assert_locked(dev);
15528 
15529 	if (netif_running(dev))
15530 		bnxt_close_nic(bp, true, false);
15531 
15532 	WRITE_ONCE(dev->mtu, new_mtu);
15533 
15534 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15535 	 * program is attached.  We need to set the AGG rings settings and
15536 	 * rx_skb_func accordingly.
15537 	 */
15538 	if (READ_ONCE(bp->xdp_prog))
15539 		bnxt_set_rx_skb_mode(bp, true);
15540 
15541 	bnxt_set_ring_params(bp);
15542 
15543 	if (netif_running(dev))
15544 		return bnxt_open_nic(bp, true, false);
15545 
15546 	return 0;
15547 }
15548 
15549 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15550 {
15551 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15552 
15553 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15554 			       tx_cp + bp->rx_nr_rings;
15555 }
15556 
15557 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15558 {
15559 	struct bnxt *bp = netdev_priv(dev);
15560 	bool sh = false;
15561 	int rc;
15562 
15563 	if (tc > bp->max_tc) {
15564 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15565 			   tc, bp->max_tc);
15566 		return -EINVAL;
15567 	}
15568 
15569 	if (bp->num_tc == tc)
15570 		return 0;
15571 
15572 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15573 		sh = true;
15574 
15575 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15576 			      sh, tc, bp->tx_nr_rings_xdp);
15577 	if (rc)
15578 		return rc;
15579 
15580 	/* Needs to close the device and do hw resource re-allocations */
15581 	if (netif_running(bp->dev))
15582 		bnxt_close_nic(bp, true, false);
15583 
15584 	if (tc) {
15585 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15586 		netdev_set_num_tc(dev, tc);
15587 		bp->num_tc = tc;
15588 	} else {
15589 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15590 		netdev_reset_tc(dev);
15591 		bp->num_tc = 0;
15592 	}
15593 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15594 	bnxt_set_cp_rings(bp, sh);
15595 
15596 	if (netif_running(bp->dev))
15597 		return bnxt_open_nic(bp, true, false);
15598 
15599 	return 0;
15600 }
15601 
15602 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15603 				  void *cb_priv)
15604 {
15605 	struct bnxt *bp = cb_priv;
15606 
15607 	if (!bnxt_tc_flower_enabled(bp) ||
15608 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15609 		return -EOPNOTSUPP;
15610 
15611 	switch (type) {
15612 	case TC_SETUP_CLSFLOWER:
15613 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15614 	default:
15615 		return -EOPNOTSUPP;
15616 	}
15617 }
15618 
15619 LIST_HEAD(bnxt_block_cb_list);
15620 
15621 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15622 			 void *type_data)
15623 {
15624 	struct bnxt *bp = netdev_priv(dev);
15625 
15626 	switch (type) {
15627 	case TC_SETUP_BLOCK:
15628 		return flow_block_cb_setup_simple(type_data,
15629 						  &bnxt_block_cb_list,
15630 						  bnxt_setup_tc_block_cb,
15631 						  bp, bp, true);
15632 	case TC_SETUP_QDISC_MQPRIO: {
15633 		struct tc_mqprio_qopt *mqprio = type_data;
15634 
15635 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15636 
15637 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15638 	}
15639 	default:
15640 		return -EOPNOTSUPP;
15641 	}
15642 }
15643 
15644 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15645 			    const struct sk_buff *skb)
15646 {
15647 	struct bnxt_vnic_info *vnic;
15648 
15649 	if (skb)
15650 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15651 
15652 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15653 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15654 }
15655 
15656 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15657 			   u32 idx)
15658 {
15659 	struct hlist_head *head;
15660 	int bit_id;
15661 
15662 	spin_lock_bh(&bp->ntp_fltr_lock);
15663 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15664 	if (bit_id < 0) {
15665 		spin_unlock_bh(&bp->ntp_fltr_lock);
15666 		return -ENOMEM;
15667 	}
15668 
15669 	fltr->base.sw_id = (u16)bit_id;
15670 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15671 	fltr->base.flags |= BNXT_ACT_RING_DST;
15672 	head = &bp->ntp_fltr_hash_tbl[idx];
15673 	hlist_add_head_rcu(&fltr->base.hash, head);
15674 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15675 	bnxt_insert_usr_fltr(bp, &fltr->base);
15676 	bp->ntp_fltr_count++;
15677 	spin_unlock_bh(&bp->ntp_fltr_lock);
15678 	return 0;
15679 }
15680 
15681 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15682 			    struct bnxt_ntuple_filter *f2)
15683 {
15684 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15685 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15686 	struct flow_keys *keys1 = &f1->fkeys;
15687 	struct flow_keys *keys2 = &f2->fkeys;
15688 
15689 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15690 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15691 		return false;
15692 
15693 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15694 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15695 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15696 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15697 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15698 			return false;
15699 	} else {
15700 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15701 				     &keys2->addrs.v6addrs.src) ||
15702 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15703 				     &masks2->addrs.v6addrs.src) ||
15704 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15705 				     &keys2->addrs.v6addrs.dst) ||
15706 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15707 				     &masks2->addrs.v6addrs.dst))
15708 			return false;
15709 	}
15710 
15711 	return keys1->ports.src == keys2->ports.src &&
15712 	       masks1->ports.src == masks2->ports.src &&
15713 	       keys1->ports.dst == keys2->ports.dst &&
15714 	       masks1->ports.dst == masks2->ports.dst &&
15715 	       keys1->control.flags == keys2->control.flags &&
15716 	       f1->l2_fltr == f2->l2_fltr;
15717 }
15718 
15719 struct bnxt_ntuple_filter *
15720 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15721 				struct bnxt_ntuple_filter *fltr, u32 idx)
15722 {
15723 	struct bnxt_ntuple_filter *f;
15724 	struct hlist_head *head;
15725 
15726 	head = &bp->ntp_fltr_hash_tbl[idx];
15727 	hlist_for_each_entry_rcu(f, head, base.hash) {
15728 		if (bnxt_fltr_match(f, fltr))
15729 			return f;
15730 	}
15731 	return NULL;
15732 }
15733 
15734 #ifdef CONFIG_RFS_ACCEL
15735 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15736 			      u16 rxq_index, u32 flow_id)
15737 {
15738 	struct bnxt *bp = netdev_priv(dev);
15739 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15740 	struct flow_keys *fkeys;
15741 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15742 	struct bnxt_l2_filter *l2_fltr;
15743 	int rc = 0, idx;
15744 	u32 flags;
15745 
15746 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15747 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15748 		atomic_inc(&l2_fltr->refcnt);
15749 	} else {
15750 		struct bnxt_l2_key key;
15751 
15752 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15753 		key.vlan = 0;
15754 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15755 		if (!l2_fltr)
15756 			return -EINVAL;
15757 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15758 			bnxt_del_l2_filter(bp, l2_fltr);
15759 			return -EINVAL;
15760 		}
15761 	}
15762 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
15763 	if (!new_fltr) {
15764 		bnxt_del_l2_filter(bp, l2_fltr);
15765 		return -ENOMEM;
15766 	}
15767 
15768 	fkeys = &new_fltr->fkeys;
15769 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15770 		rc = -EPROTONOSUPPORT;
15771 		goto err_free;
15772 	}
15773 
15774 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15775 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15776 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15777 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15778 		rc = -EPROTONOSUPPORT;
15779 		goto err_free;
15780 	}
15781 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15782 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15783 		if (bp->hwrm_spec_code < 0x10601) {
15784 			rc = -EPROTONOSUPPORT;
15785 			goto err_free;
15786 		}
15787 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15788 	}
15789 	flags = fkeys->control.flags;
15790 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15791 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15792 		rc = -EPROTONOSUPPORT;
15793 		goto err_free;
15794 	}
15795 	new_fltr->l2_fltr = l2_fltr;
15796 
15797 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15798 	rcu_read_lock();
15799 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15800 	if (fltr) {
15801 		rc = fltr->base.sw_id;
15802 		rcu_read_unlock();
15803 		goto err_free;
15804 	}
15805 	rcu_read_unlock();
15806 
15807 	new_fltr->flow_id = flow_id;
15808 	new_fltr->base.rxq = rxq_index;
15809 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15810 	if (!rc) {
15811 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15812 		return new_fltr->base.sw_id;
15813 	}
15814 
15815 err_free:
15816 	bnxt_del_l2_filter(bp, l2_fltr);
15817 	kfree(new_fltr);
15818 	return rc;
15819 }
15820 #endif
15821 
15822 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15823 {
15824 	spin_lock_bh(&bp->ntp_fltr_lock);
15825 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15826 		spin_unlock_bh(&bp->ntp_fltr_lock);
15827 		return;
15828 	}
15829 	hlist_del_rcu(&fltr->base.hash);
15830 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15831 	bp->ntp_fltr_count--;
15832 	spin_unlock_bh(&bp->ntp_fltr_lock);
15833 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15834 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15835 	kfree_rcu(fltr, base.rcu);
15836 }
15837 
15838 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15839 {
15840 #ifdef CONFIG_RFS_ACCEL
15841 	int i;
15842 
15843 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15844 		struct hlist_head *head;
15845 		struct hlist_node *tmp;
15846 		struct bnxt_ntuple_filter *fltr;
15847 		int rc;
15848 
15849 		head = &bp->ntp_fltr_hash_tbl[i];
15850 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15851 			bool del = false;
15852 
15853 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15854 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15855 					continue;
15856 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15857 							fltr->flow_id,
15858 							fltr->base.sw_id)) {
15859 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15860 									 fltr);
15861 					del = true;
15862 				}
15863 			} else {
15864 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15865 								       fltr);
15866 				if (rc)
15867 					del = true;
15868 				else
15869 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15870 			}
15871 
15872 			if (del)
15873 				bnxt_del_ntp_filter(bp, fltr);
15874 		}
15875 	}
15876 #endif
15877 }
15878 
15879 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15880 				    unsigned int entry, struct udp_tunnel_info *ti)
15881 {
15882 	struct bnxt *bp = netdev_priv(netdev);
15883 	unsigned int cmd;
15884 
15885 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15886 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15887 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15888 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15889 	else
15890 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15891 
15892 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15893 }
15894 
15895 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15896 				      unsigned int entry, struct udp_tunnel_info *ti)
15897 {
15898 	struct bnxt *bp = netdev_priv(netdev);
15899 	unsigned int cmd;
15900 
15901 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15902 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15903 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15904 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15905 	else
15906 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15907 
15908 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15909 }
15910 
15911 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15912 	.set_port	= bnxt_udp_tunnel_set_port,
15913 	.unset_port	= bnxt_udp_tunnel_unset_port,
15914 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15915 	.tables		= {
15916 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15917 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15918 	},
15919 }, bnxt_udp_tunnels_p7 = {
15920 	.set_port	= bnxt_udp_tunnel_set_port,
15921 	.unset_port	= bnxt_udp_tunnel_unset_port,
15922 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15923 	.tables		= {
15924 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15925 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15926 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15927 	},
15928 };
15929 
15930 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15931 			       struct net_device *dev, u32 filter_mask,
15932 			       int nlflags)
15933 {
15934 	struct bnxt *bp = netdev_priv(dev);
15935 
15936 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15937 				       nlflags, filter_mask, NULL);
15938 }
15939 
15940 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15941 			       u16 flags, struct netlink_ext_ack *extack)
15942 {
15943 	struct bnxt *bp = netdev_priv(dev);
15944 	struct nlattr *attr, *br_spec;
15945 	int rem, rc = 0;
15946 
15947 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15948 		return -EOPNOTSUPP;
15949 
15950 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15951 	if (!br_spec)
15952 		return -EINVAL;
15953 
15954 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15955 		u16 mode;
15956 
15957 		mode = nla_get_u16(attr);
15958 		if (mode == bp->br_mode)
15959 			break;
15960 
15961 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15962 		if (!rc)
15963 			bp->br_mode = mode;
15964 		break;
15965 	}
15966 	return rc;
15967 }
15968 
15969 int bnxt_get_port_parent_id(struct net_device *dev,
15970 			    struct netdev_phys_item_id *ppid)
15971 {
15972 	struct bnxt *bp = netdev_priv(dev);
15973 
15974 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15975 		return -EOPNOTSUPP;
15976 
15977 	/* The PF and it's VF-reps only support the switchdev framework */
15978 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15979 		return -EOPNOTSUPP;
15980 
15981 	ppid->id_len = sizeof(bp->dsn);
15982 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15983 
15984 	return 0;
15985 }
15986 
15987 static const struct net_device_ops bnxt_netdev_ops = {
15988 	.ndo_open		= bnxt_open,
15989 	.ndo_start_xmit		= bnxt_start_xmit,
15990 	.ndo_stop		= bnxt_close,
15991 	.ndo_get_stats64	= bnxt_get_stats64,
15992 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15993 	.ndo_eth_ioctl		= bnxt_ioctl,
15994 	.ndo_validate_addr	= eth_validate_addr,
15995 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15996 	.ndo_change_mtu		= bnxt_change_mtu,
15997 	.ndo_fix_features	= bnxt_fix_features,
15998 	.ndo_set_features	= bnxt_set_features,
15999 	.ndo_features_check	= bnxt_features_check,
16000 	.ndo_tx_timeout		= bnxt_tx_timeout,
16001 #ifdef CONFIG_BNXT_SRIOV
16002 	.ndo_get_vf_config	= bnxt_get_vf_config,
16003 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
16004 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
16005 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
16006 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
16007 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
16008 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
16009 #endif
16010 	.ndo_setup_tc           = bnxt_setup_tc,
16011 #ifdef CONFIG_RFS_ACCEL
16012 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
16013 #endif
16014 	.ndo_bpf		= bnxt_xdp,
16015 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
16016 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
16017 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
16018 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
16019 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
16020 };
16021 
16022 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
16023 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
16024 };
16025 
16026 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
16027 				    struct netdev_queue_stats_rx *stats)
16028 {
16029 	struct bnxt *bp = netdev_priv(dev);
16030 	struct bnxt_cp_ring_info *cpr;
16031 	u64 *sw;
16032 
16033 	if (!bp->bnapi)
16034 		return;
16035 
16036 	cpr = &bp->bnapi[i]->cp_ring;
16037 	sw = cpr->stats.sw_stats;
16038 
16039 	stats->packets = 0;
16040 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
16041 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
16042 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
16043 
16044 	stats->bytes = 0;
16045 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
16046 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
16047 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
16048 
16049 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
16050 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
16051 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
16052 }
16053 
16054 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
16055 				    struct netdev_queue_stats_tx *stats)
16056 {
16057 	struct bnxt *bp = netdev_priv(dev);
16058 	struct bnxt_napi *bnapi;
16059 	u64 *sw;
16060 
16061 	if (!bp->tx_ring)
16062 		return;
16063 
16064 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
16065 	sw = bnapi->cp_ring.stats.sw_stats;
16066 
16067 	stats->packets = 0;
16068 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
16069 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
16070 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
16071 
16072 	stats->bytes = 0;
16073 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
16074 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
16075 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
16076 }
16077 
16078 static void bnxt_get_base_stats(struct net_device *dev,
16079 				struct netdev_queue_stats_rx *rx,
16080 				struct netdev_queue_stats_tx *tx)
16081 {
16082 	struct bnxt *bp = netdev_priv(dev);
16083 
16084 	rx->packets = bp->net_stats_prev.rx_packets;
16085 	rx->bytes = bp->net_stats_prev.rx_bytes;
16086 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
16087 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
16088 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
16089 
16090 	tx->packets = bp->net_stats_prev.tx_packets;
16091 	tx->bytes = bp->net_stats_prev.tx_bytes;
16092 }
16093 
16094 static const struct netdev_stat_ops bnxt_stat_ops = {
16095 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
16096 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
16097 	.get_base_stats		= bnxt_get_base_stats,
16098 };
16099 
16100 static void bnxt_queue_default_qcfg(struct net_device *dev,
16101 				    struct netdev_queue_config *qcfg)
16102 {
16103 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
16104 }
16105 
16106 static int bnxt_validate_qcfg(struct net_device *dev,
16107 			      struct netdev_queue_config *qcfg,
16108 			      struct netlink_ext_ack *extack)
16109 {
16110 	struct bnxt *bp = netdev_priv(dev);
16111 
16112 	/* Older chips need MSS calc so rx_page_size is not supported */
16113 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16114 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16115 		return -EINVAL;
16116 
16117 	if (!is_power_of_2(qcfg->rx_page_size))
16118 		return -ERANGE;
16119 
16120 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16121 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16122 		return -ERANGE;
16123 
16124 	return 0;
16125 }
16126 
16127 static int bnxt_queue_mem_alloc(struct net_device *dev,
16128 				struct netdev_queue_config *qcfg,
16129 				void *qmem, int idx)
16130 {
16131 	struct bnxt_rx_ring_info *rxr, *clone;
16132 	struct bnxt *bp = netdev_priv(dev);
16133 	struct bnxt_ring_struct *ring;
16134 	int rc;
16135 
16136 	if (!bp->rx_ring)
16137 		return -ENETDOWN;
16138 
16139 	rxr = &bp->rx_ring[idx];
16140 	clone = qmem;
16141 	memcpy(clone, rxr, sizeof(*rxr));
16142 	bnxt_init_rx_ring_struct(bp, clone);
16143 	bnxt_reset_rx_ring_struct(bp, clone);
16144 
16145 	clone->rx_prod = 0;
16146 	clone->rx_agg_prod = 0;
16147 	clone->rx_sw_agg_prod = 0;
16148 	clone->rx_next_cons = 0;
16149 	clone->need_head_pool = false;
16150 	clone->rx_page_size = qcfg->rx_page_size;
16151 
16152 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16153 	if (rc)
16154 		return rc;
16155 
16156 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16157 	if (rc < 0)
16158 		goto err_page_pool_destroy;
16159 
16160 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16161 					MEM_TYPE_PAGE_POOL,
16162 					clone->page_pool);
16163 	if (rc)
16164 		goto err_rxq_info_unreg;
16165 
16166 	ring = &clone->rx_ring_struct;
16167 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16168 	if (rc)
16169 		goto err_free_rx_ring;
16170 
16171 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16172 		ring = &clone->rx_agg_ring_struct;
16173 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16174 		if (rc)
16175 			goto err_free_rx_agg_ring;
16176 
16177 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16178 		if (rc)
16179 			goto err_free_rx_agg_ring;
16180 	}
16181 
16182 	if (bp->flags & BNXT_FLAG_TPA) {
16183 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16184 		if (rc)
16185 			goto err_free_tpa_info;
16186 	}
16187 
16188 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16189 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16190 
16191 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16192 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16193 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16194 	if (bp->flags & BNXT_FLAG_TPA)
16195 		bnxt_alloc_one_tpa_info_data(bp, clone);
16196 
16197 	return 0;
16198 
16199 err_free_tpa_info:
16200 	bnxt_free_one_tpa_info(bp, clone);
16201 err_free_rx_agg_ring:
16202 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16203 err_free_rx_ring:
16204 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16205 err_rxq_info_unreg:
16206 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16207 err_page_pool_destroy:
16208 	page_pool_destroy(clone->page_pool);
16209 	page_pool_destroy(clone->head_pool);
16210 	clone->page_pool = NULL;
16211 	clone->head_pool = NULL;
16212 	return rc;
16213 }
16214 
16215 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16216 {
16217 	struct bnxt_rx_ring_info *rxr = qmem;
16218 	struct bnxt *bp = netdev_priv(dev);
16219 	struct bnxt_ring_struct *ring;
16220 
16221 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16222 	bnxt_free_one_tpa_info(bp, rxr);
16223 
16224 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16225 
16226 	page_pool_destroy(rxr->page_pool);
16227 	page_pool_destroy(rxr->head_pool);
16228 	rxr->page_pool = NULL;
16229 	rxr->head_pool = NULL;
16230 
16231 	ring = &rxr->rx_ring_struct;
16232 	bnxt_free_ring(bp, &ring->ring_mem);
16233 
16234 	ring = &rxr->rx_agg_ring_struct;
16235 	bnxt_free_ring(bp, &ring->ring_mem);
16236 
16237 	kfree(rxr->rx_agg_bmap);
16238 	rxr->rx_agg_bmap = NULL;
16239 }
16240 
16241 static void bnxt_copy_rx_ring(struct bnxt *bp,
16242 			      struct bnxt_rx_ring_info *dst,
16243 			      struct bnxt_rx_ring_info *src)
16244 {
16245 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16246 	struct bnxt_ring_struct *dst_ring, *src_ring;
16247 	int i;
16248 
16249 	dst_ring = &dst->rx_ring_struct;
16250 	dst_rmem = &dst_ring->ring_mem;
16251 	src_ring = &src->rx_ring_struct;
16252 	src_rmem = &src_ring->ring_mem;
16253 
16254 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16255 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16256 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16257 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16258 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16259 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16260 
16261 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16262 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16263 	*dst_rmem->vmem = *src_rmem->vmem;
16264 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16265 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16266 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16267 	}
16268 
16269 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16270 		return;
16271 
16272 	dst_ring = &dst->rx_agg_ring_struct;
16273 	dst_rmem = &dst_ring->ring_mem;
16274 	src_ring = &src->rx_agg_ring_struct;
16275 	src_rmem = &src_ring->ring_mem;
16276 
16277 	dst->rx_page_size = src->rx_page_size;
16278 
16279 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16280 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16281 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16282 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16283 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16284 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16285 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16286 
16287 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16288 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16289 	*dst_rmem->vmem = *src_rmem->vmem;
16290 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16291 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16292 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16293 	}
16294 
16295 	dst->rx_agg_bmap = src->rx_agg_bmap;
16296 }
16297 
16298 static int bnxt_queue_start(struct net_device *dev,
16299 			    struct netdev_queue_config *qcfg,
16300 			    void *qmem, int idx)
16301 {
16302 	struct bnxt *bp = netdev_priv(dev);
16303 	struct bnxt_rx_ring_info *rxr, *clone;
16304 	struct bnxt_cp_ring_info *cpr;
16305 	struct bnxt_vnic_info *vnic;
16306 	struct bnxt_napi *bnapi;
16307 	int i, rc;
16308 	u16 mru;
16309 
16310 	rxr = &bp->rx_ring[idx];
16311 	clone = qmem;
16312 
16313 	rxr->rx_prod = clone->rx_prod;
16314 	rxr->rx_agg_prod = clone->rx_agg_prod;
16315 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16316 	rxr->rx_next_cons = clone->rx_next_cons;
16317 	rxr->rx_tpa = clone->rx_tpa;
16318 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16319 	rxr->page_pool = clone->page_pool;
16320 	rxr->head_pool = clone->head_pool;
16321 	rxr->xdp_rxq = clone->xdp_rxq;
16322 	rxr->need_head_pool = clone->need_head_pool;
16323 
16324 	bnxt_copy_rx_ring(bp, rxr, clone);
16325 
16326 	bnapi = rxr->bnapi;
16327 	cpr = &bnapi->cp_ring;
16328 
16329 	/* All rings have been reserved and previously allocated.
16330 	 * Reallocating with the same parameters should never fail.
16331 	 */
16332 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16333 	if (rc)
16334 		goto err_reset;
16335 
16336 	if (bp->tph_mode) {
16337 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16338 		if (rc)
16339 			goto err_reset;
16340 	}
16341 
16342 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16343 	if (rc)
16344 		goto err_reset;
16345 
16346 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16347 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16348 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16349 
16350 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16351 		rc = bnxt_tx_queue_start(bp, idx);
16352 		if (rc)
16353 			goto err_reset;
16354 	}
16355 
16356 	bnxt_enable_rx_page_pool(rxr);
16357 	napi_enable_locked(&bnapi->napi);
16358 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16359 
16360 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16361 	for (i = 0; i < bp->nr_vnics; i++) {
16362 		vnic = &bp->vnic_info[i];
16363 
16364 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16365 		if (rc)
16366 			return rc;
16367 	}
16368 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16369 
16370 err_reset:
16371 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16372 		   rc);
16373 	napi_enable_locked(&bnapi->napi);
16374 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16375 	bnxt_reset_task(bp, true);
16376 	return rc;
16377 }
16378 
16379 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16380 {
16381 	struct bnxt *bp = netdev_priv(dev);
16382 	struct bnxt_rx_ring_info *rxr;
16383 	struct bnxt_cp_ring_info *cpr;
16384 	struct bnxt_vnic_info *vnic;
16385 	struct bnxt_napi *bnapi;
16386 	int i;
16387 
16388 	for (i = 0; i < bp->nr_vnics; i++) {
16389 		vnic = &bp->vnic_info[i];
16390 
16391 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16392 	}
16393 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16394 	/* Make sure NAPI sees that the VNIC is disabled */
16395 	synchronize_net();
16396 	rxr = &bp->rx_ring[idx];
16397 	bnapi = rxr->bnapi;
16398 	cpr = &bnapi->cp_ring;
16399 	cancel_work_sync(&cpr->dim.work);
16400 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16401 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16402 	page_pool_disable_direct_recycling(rxr->page_pool);
16403 	if (bnxt_separate_head_pool(rxr))
16404 		page_pool_disable_direct_recycling(rxr->head_pool);
16405 
16406 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16407 		bnxt_tx_queue_stop(bp, idx);
16408 
16409 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16410 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16411 	 * after seeing the completion.
16412 	 */
16413 	napi_disable_locked(&bnapi->napi);
16414 
16415 	if (bp->tph_mode) {
16416 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16417 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16418 	}
16419 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16420 
16421 	memcpy(qmem, rxr, sizeof(*rxr));
16422 	bnxt_init_rx_ring_struct(bp, qmem);
16423 
16424 	return 0;
16425 }
16426 
16427 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16428 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16429 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16430 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16431 	.ndo_queue_start	= bnxt_queue_start,
16432 	.ndo_queue_stop		= bnxt_queue_stop,
16433 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16434 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16435 	.supported_params	= QCFG_RX_PAGE_SIZE,
16436 };
16437 
16438 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16439 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16440 };
16441 
16442 static void bnxt_remove_one(struct pci_dev *pdev)
16443 {
16444 	struct net_device *dev = pci_get_drvdata(pdev);
16445 	struct bnxt *bp = netdev_priv(dev);
16446 
16447 	if (BNXT_PF(bp))
16448 		__bnxt_sriov_disable(bp);
16449 
16450 	bnxt_aux_devices_del(bp);
16451 
16452 	unregister_netdev(dev);
16453 	bnxt_ptp_clear(bp);
16454 
16455 	bnxt_aux_devices_uninit(bp);
16456 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
16457 
16458 	bnxt_free_l2_filters(bp, true);
16459 	bnxt_free_ntp_fltrs(bp, true);
16460 	WARN_ON(bp->num_rss_ctx);
16461 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16462 	/* Flush any pending tasks */
16463 	cancel_work_sync(&bp->sp_task);
16464 	cancel_delayed_work_sync(&bp->fw_reset_task);
16465 	bp->sp_event = 0;
16466 
16467 	bnxt_dl_fw_reporters_destroy(bp);
16468 	bnxt_dl_unregister(bp);
16469 	bnxt_shutdown_tc(bp);
16470 
16471 	bnxt_clear_int_mode(bp);
16472 	bnxt_hwrm_func_drv_unrgtr(bp);
16473 	bnxt_free_hwrm_resources(bp);
16474 	bnxt_hwmon_uninit(bp);
16475 	bnxt_ethtool_free(bp);
16476 	bnxt_dcb_free(bp);
16477 	kfree(bp->ptp_cfg);
16478 	bp->ptp_cfg = NULL;
16479 	kfree(bp->fw_health);
16480 	bp->fw_health = NULL;
16481 	bnxt_cleanup_pci(bp);
16482 	bnxt_free_ctx_mem(bp, true);
16483 	bnxt_free_crash_dump_mem(bp);
16484 	kfree(bp->rss_indir_tbl);
16485 	bp->rss_indir_tbl = NULL;
16486 	bnxt_free_port_stats(bp);
16487 	free_netdev(dev);
16488 }
16489 
16490 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16491 {
16492 	int rc = 0;
16493 	struct bnxt_link_info *link_info = &bp->link_info;
16494 
16495 	bp->phy_flags = 0;
16496 	rc = bnxt_hwrm_phy_qcaps(bp);
16497 	if (rc) {
16498 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16499 			   rc);
16500 		return rc;
16501 	}
16502 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16503 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16504 	else
16505 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16506 
16507 	bp->mac_flags = 0;
16508 	bnxt_hwrm_mac_qcaps(bp);
16509 
16510 	if (!fw_dflt)
16511 		return 0;
16512 
16513 	mutex_lock(&bp->link_lock);
16514 	rc = bnxt_update_link(bp, false);
16515 	if (rc) {
16516 		mutex_unlock(&bp->link_lock);
16517 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16518 			   rc);
16519 		return rc;
16520 	}
16521 
16522 	/* Older firmware does not have supported_auto_speeds, so assume
16523 	 * that all supported speeds can be autonegotiated.
16524 	 */
16525 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16526 		link_info->support_auto_speeds = link_info->support_speeds;
16527 
16528 	bnxt_init_ethtool_link_settings(bp);
16529 	mutex_unlock(&bp->link_lock);
16530 	return 0;
16531 }
16532 
16533 static int bnxt_get_max_irq(struct pci_dev *pdev)
16534 {
16535 	u16 ctrl;
16536 
16537 	if (!pdev->msix_cap)
16538 		return 1;
16539 
16540 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16541 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16542 }
16543 
16544 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16545 				int *max_cp)
16546 {
16547 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16548 	int max_ring_grps = 0, max_irq;
16549 
16550 	*max_tx = hw_resc->max_tx_rings;
16551 	*max_rx = hw_resc->max_rx_rings;
16552 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16553 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16554 			bnxt_get_ulp_msix_num_in_use(bp),
16555 			hw_resc->max_stat_ctxs -
16556 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16557 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16558 		*max_cp = min_t(int, *max_cp, max_irq);
16559 	max_ring_grps = hw_resc->max_hw_ring_grps;
16560 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16561 		*max_cp -= 1;
16562 		*max_rx -= 2;
16563 	}
16564 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16565 		*max_rx >>= 1;
16566 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16567 		int rc;
16568 
16569 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16570 		if (rc) {
16571 			*max_rx = 0;
16572 			*max_tx = 0;
16573 		}
16574 		/* On P5 chips, max_cp output param should be available NQs */
16575 		*max_cp = max_irq;
16576 	}
16577 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16578 }
16579 
16580 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16581 {
16582 	int rx, tx, cp;
16583 
16584 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16585 	*max_rx = rx;
16586 	*max_tx = tx;
16587 	if (!rx || !tx || !cp)
16588 		return -ENOMEM;
16589 
16590 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16591 }
16592 
16593 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16594 			       bool shared)
16595 {
16596 	int rc;
16597 
16598 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16599 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16600 		/* Not enough rings, try disabling agg rings. */
16601 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16602 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16603 		if (rc) {
16604 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16605 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16606 			return rc;
16607 		}
16608 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16609 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16610 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16611 		bnxt_set_ring_params(bp);
16612 	}
16613 
16614 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16615 		int max_cp, max_stat, max_irq;
16616 
16617 		/* Reserve minimum resources for RoCE */
16618 		max_cp = bnxt_get_max_func_cp_rings(bp);
16619 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16620 		max_irq = bnxt_get_max_func_irqs(bp);
16621 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16622 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16623 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16624 			return 0;
16625 
16626 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16627 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16628 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16629 		max_cp = min_t(int, max_cp, max_irq);
16630 		max_cp = min_t(int, max_cp, max_stat);
16631 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16632 		if (rc)
16633 			rc = 0;
16634 	}
16635 	return rc;
16636 }
16637 
16638 /* In initial default shared ring setting, each shared ring must have a
16639  * RX/TX ring pair.
16640  */
16641 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16642 {
16643 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16644 	bp->rx_nr_rings = bp->cp_nr_rings;
16645 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16646 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16647 }
16648 
16649 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
16650 {
16651 	if (sh)
16652 		bnxt_trim_dflt_sh_rings(bp);
16653 	else
16654 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16655 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16656 	if (sh && READ_ONCE(bp->xdp_prog)) {
16657 		bnxt_set_xdp_tx_rings(bp);
16658 		bnxt_set_cp_rings(bp, true);
16659 	}
16660 }
16661 
16662 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16663 {
16664 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16665 	int avail_msix;
16666 
16667 	if (!bnxt_can_reserve_rings(bp))
16668 		return 0;
16669 
16670 	if (sh)
16671 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16672 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16673 	/* Reduce default rings on multi-port cards so that total default
16674 	 * rings do not exceed CPU count.
16675 	 */
16676 	if (bp->port_count > 1) {
16677 		int max_rings =
16678 			max_t(int, num_online_cpus() / bp->port_count, 1);
16679 
16680 		dflt_rings = min_t(int, dflt_rings, max_rings);
16681 	}
16682 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16683 	if (rc)
16684 		return rc;
16685 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16686 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16687 
16688 	bnxt_adj_dflt_rings(bp, sh);
16689 
16690 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16691 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16692 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16693 
16694 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16695 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16696 	}
16697 
16698 	rc = __bnxt_reserve_rings(bp);
16699 	if (rc && rc != -ENODEV)
16700 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16701 
16702 	bnxt_adj_tx_rings(bp);
16703 	if (sh)
16704 		bnxt_adj_dflt_rings(bp, true);
16705 
16706 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16707 	if (bnxt_need_reserve_rings(bp)) {
16708 		rc = __bnxt_reserve_rings(bp);
16709 		if (rc && rc != -ENODEV)
16710 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16711 		bnxt_adj_tx_rings(bp);
16712 	}
16713 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16714 		bp->rx_nr_rings++;
16715 		bp->cp_nr_rings++;
16716 	}
16717 	if (rc) {
16718 		bp->tx_nr_rings = 0;
16719 		bp->rx_nr_rings = 0;
16720 	}
16721 	return rc;
16722 }
16723 
16724 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16725 {
16726 	int rc;
16727 
16728 	if (bp->tx_nr_rings)
16729 		return 0;
16730 
16731 	bnxt_ulp_irq_stop(bp);
16732 	bnxt_clear_int_mode(bp);
16733 	rc = bnxt_set_dflt_rings(bp, true);
16734 	if (rc) {
16735 		if (BNXT_VF(bp) && rc == -ENODEV)
16736 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16737 		else
16738 			netdev_err(bp->dev, "Not enough rings available.\n");
16739 		goto init_dflt_ring_err;
16740 	}
16741 	rc = bnxt_init_int_mode(bp);
16742 	if (rc)
16743 		goto init_dflt_ring_err;
16744 
16745 	bnxt_adj_tx_rings(bp);
16746 
16747 	bnxt_set_dflt_rfs(bp);
16748 
16749 init_dflt_ring_err:
16750 	bnxt_ulp_irq_restart(bp, rc);
16751 	return rc;
16752 }
16753 
16754 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16755 {
16756 	int rc;
16757 
16758 	netdev_ops_assert_locked(bp->dev);
16759 	bnxt_hwrm_func_qcaps(bp);
16760 
16761 	if (netif_running(bp->dev))
16762 		__bnxt_close_nic(bp, true, false);
16763 
16764 	bnxt_ulp_irq_stop(bp);
16765 	bnxt_clear_int_mode(bp);
16766 	rc = bnxt_init_int_mode(bp);
16767 	bnxt_ulp_irq_restart(bp, rc);
16768 
16769 	if (netif_running(bp->dev)) {
16770 		if (rc)
16771 			netif_close(bp->dev);
16772 		else
16773 			rc = bnxt_open_nic(bp, true, false);
16774 	}
16775 
16776 	return rc;
16777 }
16778 
16779 static int bnxt_init_mac_addr(struct bnxt *bp)
16780 {
16781 	int rc = 0;
16782 
16783 	if (BNXT_PF(bp)) {
16784 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16785 	} else {
16786 #ifdef CONFIG_BNXT_SRIOV
16787 		struct bnxt_vf_info *vf = &bp->vf;
16788 		bool strict_approval = true;
16789 
16790 		if (is_valid_ether_addr(vf->mac_addr)) {
16791 			/* overwrite netdev dev_addr with admin VF MAC */
16792 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16793 			/* Older PF driver or firmware may not approve this
16794 			 * correctly.
16795 			 */
16796 			strict_approval = false;
16797 		} else {
16798 			eth_hw_addr_random(bp->dev);
16799 		}
16800 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16801 #endif
16802 	}
16803 	return rc;
16804 }
16805 
16806 static void bnxt_vpd_read_info(struct bnxt *bp)
16807 {
16808 	struct pci_dev *pdev = bp->pdev;
16809 	unsigned int vpd_size, kw_len;
16810 	int pos, size;
16811 	u8 *vpd_data;
16812 
16813 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16814 	if (IS_ERR(vpd_data)) {
16815 		pci_warn(pdev, "Unable to read VPD\n");
16816 		return;
16817 	}
16818 
16819 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16820 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16821 	if (pos < 0)
16822 		goto read_sn;
16823 
16824 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16825 	memcpy(bp->board_partno, &vpd_data[pos], size);
16826 
16827 read_sn:
16828 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16829 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16830 					   &kw_len);
16831 	if (pos < 0)
16832 		goto exit;
16833 
16834 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16835 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16836 exit:
16837 	kfree(vpd_data);
16838 }
16839 
16840 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16841 {
16842 	struct pci_dev *pdev = bp->pdev;
16843 	u64 qword;
16844 
16845 	qword = pci_get_dsn(pdev);
16846 	if (!qword) {
16847 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16848 		return -EOPNOTSUPP;
16849 	}
16850 
16851 	put_unaligned_le64(qword, dsn);
16852 
16853 	bp->flags |= BNXT_FLAG_DSN_VALID;
16854 	return 0;
16855 }
16856 
16857 static int bnxt_map_db_bar(struct bnxt *bp)
16858 {
16859 	if (!bp->db_size)
16860 		return -ENODEV;
16861 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16862 	if (!bp->bar1)
16863 		return -ENOMEM;
16864 	return 0;
16865 }
16866 
16867 void bnxt_print_device_info(struct bnxt *bp)
16868 {
16869 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16870 		    board_info[bp->board_idx].name,
16871 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16872 
16873 	pcie_print_link_status(bp->pdev);
16874 }
16875 
16876 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16877 {
16878 	struct bnxt_hw_resc *hw_resc;
16879 	struct net_device *dev;
16880 	struct bnxt *bp;
16881 	int rc, max_irqs;
16882 
16883 	if (pci_is_bridge(pdev))
16884 		return -ENODEV;
16885 
16886 	if (!pdev->msix_cap) {
16887 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16888 		return -ENODEV;
16889 	}
16890 
16891 	/* Clear any pending DMA transactions from crash kernel
16892 	 * while loading driver in capture kernel.
16893 	 */
16894 	if (is_kdump_kernel()) {
16895 		pci_clear_master(pdev);
16896 		pcie_flr(pdev);
16897 	}
16898 
16899 	max_irqs = bnxt_get_max_irq(pdev);
16900 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16901 				 max_irqs);
16902 	if (!dev)
16903 		return -ENOMEM;
16904 
16905 	bp = netdev_priv(dev);
16906 	bp->board_idx = ent->driver_data;
16907 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16908 	bnxt_set_max_func_irqs(bp, max_irqs);
16909 
16910 	if (bnxt_vf_pciid(bp->board_idx))
16911 		bp->flags |= BNXT_FLAG_VF;
16912 
16913 	/* No devlink port registration in case of a VF */
16914 	if (BNXT_PF(bp))
16915 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16916 
16917 	rc = bnxt_init_board(pdev, dev);
16918 	if (rc < 0)
16919 		goto init_err_free;
16920 
16921 	dev->netdev_ops = &bnxt_netdev_ops;
16922 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
16923 	dev->stat_ops = &bnxt_stat_ops;
16924 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16925 	dev->ethtool_ops = &bnxt_ethtool_ops;
16926 	pci_set_drvdata(pdev, dev);
16927 
16928 	rc = bnxt_alloc_hwrm_resources(bp);
16929 	if (rc)
16930 		goto init_err_pci_clean;
16931 
16932 	mutex_init(&bp->hwrm_cmd_lock);
16933 	mutex_init(&bp->link_lock);
16934 
16935 	rc = bnxt_fw_init_one_p1(bp);
16936 	if (rc)
16937 		goto init_err_pci_clean;
16938 
16939 	if (BNXT_PF(bp))
16940 		bnxt_vpd_read_info(bp);
16941 
16942 	if (BNXT_CHIP_P5_PLUS(bp)) {
16943 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16944 		if (BNXT_CHIP_P7(bp))
16945 			bp->flags |= BNXT_FLAG_CHIP_P7;
16946 	}
16947 
16948 	rc = bnxt_alloc_rss_indir_tbl(bp);
16949 	if (rc)
16950 		goto init_err_pci_clean;
16951 
16952 	rc = bnxt_fw_init_one_p2(bp);
16953 	if (rc)
16954 		goto init_err_pci_clean;
16955 
16956 	rc = bnxt_map_db_bar(bp);
16957 	if (rc) {
16958 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16959 			rc);
16960 		goto init_err_pci_clean;
16961 	}
16962 
16963 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16964 			   NETIF_F_TSO | NETIF_F_TSO6 |
16965 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16966 			   NETIF_F_GSO_IPXIP4 |
16967 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16968 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16969 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16970 	dev->hw_features |= NETIF_F_GSO_UDP_L4;
16971 
16972 	if (BNXT_SUPPORTS_TPA(bp))
16973 		dev->hw_features |= NETIF_F_LRO;
16974 
16975 	dev->hw_enc_features =
16976 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16977 			NETIF_F_TSO | NETIF_F_TSO6 |
16978 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16979 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16980 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16981 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16982 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16983 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16984 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16985 	else
16986 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16987 
16988 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16989 				    NETIF_F_GSO_GRE_CSUM;
16990 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16991 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16992 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16993 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16994 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16995 	if (BNXT_SUPPORTS_TPA(bp))
16996 		dev->hw_features |= NETIF_F_GRO_HW;
16997 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16998 	if (dev->features & NETIF_F_GRO_HW)
16999 		dev->features &= ~NETIF_F_LRO;
17000 	dev->priv_flags |= IFF_UNICAST_FLT;
17001 
17002 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
17003 	if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
17004 		u16 max_segs = BNXT_SW_USO_MAX_SEGS;
17005 
17006 		if (bp->tso_max_segs)
17007 			max_segs = min_t(u16, max_segs, bp->tso_max_segs);
17008 		netif_set_tso_max_segs(dev, max_segs);
17009 	} else if (bp->tso_max_segs) {
17010 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
17011 	}
17012 
17013 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
17014 			    NETDEV_XDP_ACT_RX_SG;
17015 
17016 #ifdef CONFIG_BNXT_SRIOV
17017 	init_waitqueue_head(&bp->sriov_cfg_wait);
17018 #endif
17019 	if (BNXT_SUPPORTS_TPA(bp)) {
17020 		bp->gro_func = bnxt_gro_func_5730x;
17021 		if (BNXT_CHIP_P4(bp))
17022 			bp->gro_func = bnxt_gro_func_5731x;
17023 		else if (BNXT_CHIP_P5_PLUS(bp))
17024 			bp->gro_func = bnxt_gro_func_5750x;
17025 	}
17026 	if (!BNXT_CHIP_P4_PLUS(bp))
17027 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
17028 
17029 	rc = bnxt_init_mac_addr(bp);
17030 	if (rc) {
17031 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
17032 		rc = -EADDRNOTAVAIL;
17033 		goto init_err_pci_clean;
17034 	}
17035 
17036 	if (BNXT_PF(bp)) {
17037 		/* Read the adapter's DSN to use as the eswitch switch_id */
17038 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
17039 	}
17040 
17041 	/* MTU range: 60 - FW defined max */
17042 	dev->min_mtu = ETH_ZLEN;
17043 	dev->max_mtu = bp->max_mtu;
17044 
17045 	rc = bnxt_probe_phy(bp, true);
17046 	if (rc)
17047 		goto init_err_pci_clean;
17048 
17049 	hw_resc = &bp->hw_resc;
17050 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
17051 		       BNXT_L2_FLTR_MAX_FLTR;
17052 	/* Older firmware may not report these filters properly */
17053 	if (bp->max_fltr < BNXT_MAX_FLTR)
17054 		bp->max_fltr = BNXT_MAX_FLTR;
17055 	bnxt_init_l2_fltr_tbl(bp);
17056 	__bnxt_set_rx_skb_mode(bp, false);
17057 	bnxt_set_tpa_flags(bp);
17058 	bnxt_init_ring_params(bp);
17059 	bnxt_set_ring_params(bp);
17060 	mutex_init(&bp->auxdev_lock);
17061 	if (!bnxt_auxdev_id_alloc(bp))
17062 		bnxt_aux_devices_init(bp);
17063 	rc = bnxt_set_dflt_rings(bp, true);
17064 	if (rc) {
17065 		if (BNXT_VF(bp) && rc == -ENODEV) {
17066 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17067 		} else {
17068 			netdev_err(bp->dev, "Not enough rings available.\n");
17069 			rc = -ENOMEM;
17070 		}
17071 		goto init_err_pci_clean;
17072 	}
17073 
17074 	bnxt_fw_init_one_p3(bp);
17075 
17076 	bnxt_init_dflt_coal(bp);
17077 
17078 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
17079 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
17080 
17081 	rc = bnxt_init_int_mode(bp);
17082 	if (rc)
17083 		goto init_err_pci_clean;
17084 
17085 	/* No TC has been set yet and rings may have been trimmed due to
17086 	 * limited MSIX, so we re-initialize the TX rings per TC.
17087 	 */
17088 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
17089 
17090 	if (BNXT_PF(bp)) {
17091 		if (!bnxt_pf_wq) {
17092 			bnxt_pf_wq =
17093 				create_singlethread_workqueue("bnxt_pf_wq");
17094 			if (!bnxt_pf_wq) {
17095 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
17096 				rc = -ENOMEM;
17097 				goto init_err_pci_clean;
17098 			}
17099 		}
17100 		rc = bnxt_init_tc(bp);
17101 		if (rc)
17102 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
17103 				   rc);
17104 	}
17105 
17106 	bnxt_inv_fw_health_reg(bp);
17107 	rc = bnxt_dl_register(bp);
17108 	if (rc)
17109 		goto init_err_dl;
17110 
17111 	INIT_LIST_HEAD(&bp->usr_fltr_list);
17112 
17113 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
17114 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
17115 
17116 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17117 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17118 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17119 	dev->netmem_tx = true;
17120 
17121 	rc = register_netdev(dev);
17122 	if (rc)
17123 		goto init_err_cleanup;
17124 
17125 	bnxt_dl_fw_reporters_create(bp);
17126 
17127 	bnxt_aux_devices_add(bp);
17128 
17129 	bnxt_print_device_info(bp);
17130 
17131 	pci_save_state(pdev);
17132 
17133 	return 0;
17134 init_err_cleanup:
17135 	bnxt_aux_devices_uninit(bp);
17136 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
17137 	bnxt_dl_unregister(bp);
17138 init_err_dl:
17139 	bnxt_shutdown_tc(bp);
17140 	bnxt_clear_int_mode(bp);
17141 
17142 init_err_pci_clean:
17143 	bnxt_hwrm_func_drv_unrgtr(bp);
17144 	bnxt_ptp_clear(bp);
17145 	kfree(bp->ptp_cfg);
17146 	bp->ptp_cfg = NULL;
17147 	bnxt_free_hwrm_resources(bp);
17148 	bnxt_hwmon_uninit(bp);
17149 	bnxt_ethtool_free(bp);
17150 	kfree(bp->fw_health);
17151 	bp->fw_health = NULL;
17152 	bnxt_cleanup_pci(bp);
17153 	bnxt_free_ctx_mem(bp, true);
17154 	bnxt_free_crash_dump_mem(bp);
17155 	kfree(bp->rss_indir_tbl);
17156 	bp->rss_indir_tbl = NULL;
17157 
17158 init_err_free:
17159 	free_netdev(dev);
17160 	return rc;
17161 }
17162 
17163 static void bnxt_shutdown(struct pci_dev *pdev)
17164 {
17165 	struct net_device *dev = pci_get_drvdata(pdev);
17166 	struct bnxt *bp;
17167 
17168 	if (!dev)
17169 		return;
17170 
17171 	rtnl_lock();
17172 	netdev_lock(dev);
17173 	bp = netdev_priv(dev);
17174 	if (!bp)
17175 		goto shutdown_exit;
17176 
17177 	if (netif_running(dev))
17178 		netif_close(dev);
17179 
17180 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17181 		pcie_flr(pdev);
17182 		goto shutdown_exit;
17183 	}
17184 	bnxt_ptp_clear(bp);
17185 	bnxt_clear_int_mode(bp);
17186 	pci_disable_device(pdev);
17187 
17188 	if (system_state == SYSTEM_POWER_OFF) {
17189 		pci_wake_from_d3(pdev, bp->wol);
17190 		pci_set_power_state(pdev, PCI_D3hot);
17191 	}
17192 
17193 shutdown_exit:
17194 	netdev_unlock(dev);
17195 	rtnl_unlock();
17196 }
17197 
17198 #ifdef CONFIG_PM_SLEEP
17199 static int bnxt_suspend(struct device *device)
17200 {
17201 	struct net_device *dev = dev_get_drvdata(device);
17202 	struct bnxt *bp = netdev_priv(dev);
17203 	int rc = 0;
17204 
17205 	bnxt_ulp_stop(bp);
17206 
17207 	netdev_lock(dev);
17208 	if (netif_running(dev)) {
17209 		netif_device_detach(dev);
17210 		rc = bnxt_close(dev);
17211 	}
17212 	bnxt_hwrm_func_drv_unrgtr(bp);
17213 	bnxt_ptp_clear(bp);
17214 	pci_disable_device(bp->pdev);
17215 	bnxt_free_ctx_mem(bp, false);
17216 	netdev_unlock(dev);
17217 	return rc;
17218 }
17219 
17220 static int bnxt_resume(struct device *device)
17221 {
17222 	struct net_device *dev = dev_get_drvdata(device);
17223 	struct bnxt *bp = netdev_priv(dev);
17224 	int rc = 0;
17225 
17226 	netdev_lock(dev);
17227 	rc = pci_enable_device(bp->pdev);
17228 	if (rc) {
17229 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17230 			   rc);
17231 		goto resume_exit;
17232 	}
17233 	pci_set_master(bp->pdev);
17234 	if (bnxt_hwrm_ver_get(bp)) {
17235 		rc = -ENODEV;
17236 		goto resume_exit;
17237 	}
17238 	rc = bnxt_hwrm_func_reset(bp);
17239 	if (rc) {
17240 		rc = -EBUSY;
17241 		goto resume_exit;
17242 	}
17243 
17244 	rc = bnxt_hwrm_func_qcaps(bp);
17245 	if (rc)
17246 		goto resume_exit;
17247 
17248 	bnxt_clear_reservations(bp, true);
17249 
17250 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17251 		rc = -ENODEV;
17252 		goto resume_exit;
17253 	}
17254 	if (bp->fw_crash_mem)
17255 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17256 
17257 	if (bnxt_ptp_init(bp)) {
17258 		kfree(bp->ptp_cfg);
17259 		bp->ptp_cfg = NULL;
17260 	}
17261 	bnxt_get_wol_settings(bp);
17262 	if (netif_running(dev)) {
17263 		rc = bnxt_open(dev);
17264 		if (!rc)
17265 			netif_device_attach(dev);
17266 	}
17267 
17268 resume_exit:
17269 	netdev_unlock(bp->dev);
17270 	if (!rc) {
17271 		bnxt_ulp_start(bp);
17272 		bnxt_reenable_sriov(bp);
17273 	}
17274 	return rc;
17275 }
17276 
17277 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17278 #define BNXT_PM_OPS (&bnxt_pm_ops)
17279 
17280 #else
17281 
17282 #define BNXT_PM_OPS NULL
17283 
17284 #endif /* CONFIG_PM_SLEEP */
17285 
17286 /**
17287  * bnxt_io_error_detected - called when PCI error is detected
17288  * @pdev: Pointer to PCI device
17289  * @state: The current pci connection state
17290  *
17291  * This function is called after a PCI bus error affecting
17292  * this device has been detected.
17293  */
17294 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17295 					       pci_channel_state_t state)
17296 {
17297 	struct net_device *netdev = pci_get_drvdata(pdev);
17298 	struct bnxt *bp = netdev_priv(netdev);
17299 	bool abort = false;
17300 
17301 	netdev_info(netdev, "PCI I/O error detected\n");
17302 
17303 	bnxt_ulp_stop(bp);
17304 
17305 	netdev_lock(netdev);
17306 	netif_device_detach(netdev);
17307 
17308 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17309 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17310 		abort = true;
17311 	}
17312 
17313 	if (abort || state == pci_channel_io_perm_failure) {
17314 		netdev_unlock(netdev);
17315 		return PCI_ERS_RESULT_DISCONNECT;
17316 	}
17317 
17318 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17319 	 * so we disable bus master to prevent any potential bad DMAs before
17320 	 * freeing kernel memory.
17321 	 */
17322 	if (state == pci_channel_io_frozen) {
17323 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
17324 		bnxt_fw_fatal_close(bp);
17325 	}
17326 
17327 	if (netif_running(netdev))
17328 		__bnxt_close_nic(bp, true, true);
17329 
17330 	if (pci_is_enabled(pdev))
17331 		pci_disable_device(pdev);
17332 	bnxt_free_ctx_mem(bp, false);
17333 	netdev_unlock(netdev);
17334 
17335 	/* Request a slot reset. */
17336 	return PCI_ERS_RESULT_NEED_RESET;
17337 }
17338 
17339 /**
17340  * bnxt_io_slot_reset - called after the pci bus has been reset.
17341  * @pdev: Pointer to PCI device
17342  *
17343  * Restart the card from scratch, as if from a cold-boot.
17344  * At this point, the card has experienced a hard reset,
17345  * followed by fixups by BIOS, and has its config space
17346  * set up identically to what it was at cold boot.
17347  */
17348 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17349 {
17350 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17351 	struct net_device *netdev = pci_get_drvdata(pdev);
17352 	struct bnxt *bp = netdev_priv(netdev);
17353 	int retry = 0;
17354 	int err = 0;
17355 	int off;
17356 
17357 	netdev_info(bp->dev, "PCI Slot Reset\n");
17358 
17359 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17360 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17361 		msleep(900);
17362 
17363 	netdev_lock(netdev);
17364 
17365 	if (pci_enable_device(pdev)) {
17366 		dev_err(&pdev->dev,
17367 			"Cannot re-enable PCI device after reset.\n");
17368 	} else {
17369 		pci_set_master(pdev);
17370 		/* Upon fatal error, our device internal logic that latches to
17371 		 * BAR value is getting reset and will restore only upon
17372 		 * rewriting the BARs.
17373 		 *
17374 		 * As pci_restore_state() does not re-write the BARs if the
17375 		 * value is same as saved value earlier, driver needs to
17376 		 * write the BARs to 0 to force restore, in case of fatal error.
17377 		 */
17378 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17379 				       &bp->state)) {
17380 			for (off = PCI_BASE_ADDRESS_0;
17381 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17382 				pci_write_config_dword(bp->pdev, off, 0);
17383 		}
17384 		pci_restore_state(pdev);
17385 		pci_save_state(pdev);
17386 
17387 		bnxt_inv_fw_health_reg(bp);
17388 		bnxt_try_map_fw_health_reg(bp);
17389 
17390 		/* In some PCIe AER scenarios, firmware may take up to
17391 		 * 10 seconds to become ready in the worst case.
17392 		 */
17393 		do {
17394 			err = bnxt_try_recover_fw(bp);
17395 			if (!err)
17396 				break;
17397 			retry++;
17398 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17399 
17400 		if (err) {
17401 			dev_err(&pdev->dev, "Firmware not ready\n");
17402 			goto reset_exit;
17403 		}
17404 
17405 		err = bnxt_hwrm_func_reset(bp);
17406 		if (!err)
17407 			result = PCI_ERS_RESULT_RECOVERED;
17408 
17409 		/* IRQ will be initialized later in bnxt_io_resume */
17410 		bnxt_ulp_irq_stop(bp);
17411 		bnxt_clear_int_mode(bp);
17412 	}
17413 
17414 reset_exit:
17415 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17416 	bnxt_clear_reservations(bp, true);
17417 	netdev_unlock(netdev);
17418 
17419 	return result;
17420 }
17421 
17422 /**
17423  * bnxt_io_resume - called when traffic can start flowing again.
17424  * @pdev: Pointer to PCI device
17425  *
17426  * This callback is called when the error recovery driver tells
17427  * us that its OK to resume normal operation.
17428  */
17429 static void bnxt_io_resume(struct pci_dev *pdev)
17430 {
17431 	struct net_device *netdev = pci_get_drvdata(pdev);
17432 	struct bnxt *bp = netdev_priv(netdev);
17433 	int err;
17434 
17435 	netdev_info(bp->dev, "PCI Slot Resume\n");
17436 	netdev_lock(netdev);
17437 
17438 	err = bnxt_hwrm_func_qcaps(bp);
17439 	if (!err) {
17440 		if (netif_running(netdev)) {
17441 			err = bnxt_open(netdev);
17442 		} else {
17443 			err = bnxt_reserve_rings(bp, true);
17444 			if (!err)
17445 				err = bnxt_init_int_mode(bp);
17446 		}
17447 	}
17448 
17449 	if (!err)
17450 		netif_device_attach(netdev);
17451 
17452 	netdev_unlock(netdev);
17453 	if (!err) {
17454 		bnxt_ulp_start(bp);
17455 		bnxt_reenable_sriov(bp);
17456 	}
17457 }
17458 
17459 static const struct pci_error_handlers bnxt_err_handler = {
17460 	.error_detected	= bnxt_io_error_detected,
17461 	.slot_reset	= bnxt_io_slot_reset,
17462 	.resume		= bnxt_io_resume
17463 };
17464 
17465 static struct pci_driver bnxt_pci_driver = {
17466 	.name		= DRV_MODULE_NAME,
17467 	.id_table	= bnxt_pci_tbl,
17468 	.probe		= bnxt_init_one,
17469 	.remove		= bnxt_remove_one,
17470 	.shutdown	= bnxt_shutdown,
17471 	.driver.pm	= BNXT_PM_OPS,
17472 	.err_handler	= &bnxt_err_handler,
17473 #if defined(CONFIG_BNXT_SRIOV)
17474 	.sriov_configure = bnxt_sriov_configure,
17475 #endif
17476 };
17477 
17478 static int __init bnxt_init(void)
17479 {
17480 	int err;
17481 
17482 	bnxt_debug_init();
17483 	err = pci_register_driver(&bnxt_pci_driver);
17484 	if (err) {
17485 		bnxt_debug_exit();
17486 		return err;
17487 	}
17488 
17489 	return 0;
17490 }
17491 
17492 static void __exit bnxt_exit(void)
17493 {
17494 	pci_unregister_driver(&bnxt_pci_driver);
17495 	if (bnxt_pf_wq)
17496 		destroy_workqueue(bnxt_pf_wq);
17497 	bnxt_debug_exit();
17498 }
17499 
17500 module_init(bnxt_init);
17501 module_exit(bnxt_exit);
17502