xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision d755d45bc08a57a3b845b850f8760de922a499bf)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 #include <linux/bnxt/hsi.h>
62 #include <linux/bnxt/ulp.h>
63 
64 #include "bnxt.h"
65 #include "bnxt_hwrm.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 #include "bnxt_gso.h"
78 #include <net/tso.h>
79 
80 #define BNXT_TX_TIMEOUT		(5 * HZ)
81 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
82 				 NETIF_MSG_TX_ERR)
83 
84 MODULE_IMPORT_NS("NETDEV_INTERNAL");
85 MODULE_LICENSE("GPL");
86 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
87 
88 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
89 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
90 
91 #define BNXT_TX_PUSH_THRESH 164
92 
93 /* indexed by enum board_idx */
94 static const struct {
95 	char *name;
96 } board_info[] = {
97 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
100 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
101 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
102 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
103 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
104 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
108 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
109 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
110 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
112 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
113 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
114 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
115 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
116 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
117 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
118 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
119 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
120 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
121 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
122 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
123 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
124 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
125 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
126 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
127 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
128 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
129 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
130 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
131 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
132 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
133 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
134 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
135 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
136 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
137 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
138 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
139 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
141 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
142 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
143 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
145 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
146 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
147 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
148 };
149 
150 static const struct pci_device_id bnxt_pci_tbl[] = {
151 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
155 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
170 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
183 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
184 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
185 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
192 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
193 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
198 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
199 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
200 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
201 #ifdef CONFIG_BNXT_SRIOV
202 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
219 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
220 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
221 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
223 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
224 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
225 #endif
226 	{ 0 }
227 };
228 
229 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
230 
231 static const u16 bnxt_vf_req_snif[] = {
232 	HWRM_FUNC_CFG,
233 	HWRM_FUNC_VF_CFG,
234 	HWRM_PORT_PHY_QCFG,
235 	HWRM_CFA_L2_FILTER_ALLOC,
236 };
237 
238 static const u16 bnxt_async_events_arr[] = {
239 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
242 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
243 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
246 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
247 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
248 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
249 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
250 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
253 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
254 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
255 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
256 };
257 
258 const u16 bnxt_bstore_to_trace[] = {
259 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
260 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
261 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
262 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
263 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
264 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
265 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
266 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
267 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
268 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
269 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
270 	[BNXT_CTX_KONG]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE,
271 	[BNXT_CTX_QPC]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE,
272 };
273 
274 static struct workqueue_struct *bnxt_pf_wq;
275 
276 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
277 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
278 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
279 
280 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
281 	.ports = {
282 		.src = 0,
283 		.dst = 0,
284 	},
285 	.addrs = {
286 		.v6addrs = {
287 			.src = BNXT_IPV6_MASK_NONE,
288 			.dst = BNXT_IPV6_MASK_NONE,
289 		},
290 	},
291 };
292 
293 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
294 	.ports = {
295 		.src = cpu_to_be16(0xffff),
296 		.dst = cpu_to_be16(0xffff),
297 	},
298 	.addrs = {
299 		.v6addrs = {
300 			.src = BNXT_IPV6_MASK_ALL,
301 			.dst = BNXT_IPV6_MASK_ALL,
302 		},
303 	},
304 };
305 
306 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
307 	.ports = {
308 		.src = cpu_to_be16(0xffff),
309 		.dst = cpu_to_be16(0xffff),
310 	},
311 	.addrs = {
312 		.v4addrs = {
313 			.src = cpu_to_be32(0xffffffff),
314 			.dst = cpu_to_be32(0xffffffff),
315 		},
316 	},
317 };
318 
319 static bool bnxt_vf_pciid(enum board_idx idx)
320 {
321 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
322 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
323 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
324 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
325 		idx == NETXTREME_E_P7_VF_HV);
326 }
327 
328 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
329 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
330 
331 #define BNXT_DB_CQ(db, idx)						\
332 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_NQ_P5(db, idx)						\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
336 		    (db)->doorbell)
337 
338 #define BNXT_DB_NQ_P7(db, idx)						\
339 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
340 		    DB_RING_IDX(db, idx), (db)->doorbell)
341 
342 #define BNXT_DB_CQ_ARM(db, idx)						\
343 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
344 
345 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
346 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
347 		    DB_RING_IDX(db, idx), (db)->doorbell)
348 
349 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
350 {
351 	if (bp->flags & BNXT_FLAG_CHIP_P7)
352 		BNXT_DB_NQ_P7(db, idx);
353 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
354 		BNXT_DB_NQ_P5(db, idx);
355 	else
356 		BNXT_DB_CQ(db, idx);
357 }
358 
359 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
360 {
361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
362 		BNXT_DB_NQ_ARM_P5(db, idx);
363 	else
364 		BNXT_DB_CQ_ARM(db, idx);
365 }
366 
367 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
368 {
369 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
370 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
371 			    DB_RING_IDX(db, idx), db->doorbell);
372 	else
373 		BNXT_DB_CQ(db, idx);
374 }
375 
376 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
377 {
378 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
379 		return;
380 
381 	if (BNXT_PF(bp))
382 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
383 	else
384 		schedule_delayed_work(&bp->fw_reset_task, delay);
385 }
386 
387 static void __bnxt_queue_sp_work(struct bnxt *bp)
388 {
389 	if (BNXT_PF(bp))
390 		queue_work(bnxt_pf_wq, &bp->sp_task);
391 	else
392 		schedule_work(&bp->sp_task);
393 }
394 
395 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
396 {
397 	set_bit(event, &bp->sp_event);
398 	__bnxt_queue_sp_work(bp);
399 }
400 
401 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
402 {
403 	if (!rxr->bnapi->in_reset) {
404 		rxr->bnapi->in_reset = true;
405 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
406 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
407 		else
408 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
409 		__bnxt_queue_sp_work(bp);
410 	}
411 	rxr->rx_next_cons = 0xffff;
412 }
413 
414 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
415 			  u16 curr)
416 {
417 	struct bnxt_napi *bnapi = txr->bnapi;
418 
419 	if (bnapi->tx_fault)
420 		return;
421 
422 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
423 		   txr->txq_index, txr->tx_hw_cons,
424 		   txr->tx_cons, txr->tx_prod, curr);
425 	WARN_ON_ONCE(1);
426 	bnapi->tx_fault = 1;
427 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
428 }
429 
430 const u16 bnxt_lhint_arr[] = {
431 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
432 	TX_BD_FLAGS_LHINT_512_TO_1023,
433 	TX_BD_FLAGS_LHINT_1024_TO_2047,
434 	TX_BD_FLAGS_LHINT_1024_TO_2047,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
444 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
445 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
446 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
447 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
448 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
449 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
450 };
451 
452 u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
453 {
454 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
455 
456 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
457 		return 0;
458 
459 	return md_dst->u.port_info.port_id;
460 }
461 
462 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
463 			     u16 prod)
464 {
465 	/* Sync BD data before updating doorbell */
466 	wmb();
467 	bnxt_db_write(bp, &txr->tx_db, prod);
468 	txr->kick_pending = 0;
469 }
470 
471 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
472 {
473 	struct bnxt *bp = netdev_priv(dev);
474 	struct tx_bd *txbd, *txbd0;
475 	struct tx_bd_ext *txbd1;
476 	struct netdev_queue *txq;
477 	int i;
478 	dma_addr_t mapping;
479 	unsigned int length, pad = 0;
480 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
481 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
482 	struct pci_dev *pdev = bp->pdev;
483 	u16 prod, last_frag, txts_prod;
484 	struct bnxt_tx_ring_info *txr;
485 	struct bnxt_sw_tx_bd *tx_buf;
486 	__le32 lflags = 0;
487 	skb_frag_t *frag;
488 
489 	i = skb_get_queue_mapping(skb);
490 	if (unlikely(i >= bp->tx_nr_rings)) {
491 		dev_kfree_skb_any(skb);
492 		dev_core_stats_tx_dropped_inc(dev);
493 		return NETDEV_TX_OK;
494 	}
495 
496 	txq = netdev_get_tx_queue(dev, i);
497 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
498 	prod = txr->tx_prod;
499 
500 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
501 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
502 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
503 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
504 		if (skb_linearize(skb)) {
505 			dev_kfree_skb_any(skb);
506 			dev_core_stats_tx_dropped_inc(dev);
507 			return NETDEV_TX_OK;
508 		}
509 	}
510 #endif
511 	if (skb_is_gso(skb) &&
512 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
513 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP))
514 		return bnxt_sw_udp_gso_xmit(bp, txr, txq, skb);
515 
516 	free_size = bnxt_tx_avail(bp, txr);
517 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
518 		/* We must have raced with NAPI cleanup */
519 		if (net_ratelimit() && txr->kick_pending)
520 			netif_warn(bp, tx_err, dev,
521 				   "bnxt: ring busy w/ flush pending!\n");
522 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
523 					bp->tx_wake_thresh))
524 			return NETDEV_TX_BUSY;
525 	}
526 
527 	length = skb->len;
528 	len = skb_headlen(skb);
529 	last_frag = skb_shinfo(skb)->nr_frags;
530 
531 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
532 
533 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
534 	tx_buf->skb = skb;
535 	tx_buf->nr_frags = last_frag;
536 
537 	vlan_tag_flags = 0;
538 	cfa_action = bnxt_xmit_get_cfa_action(skb);
539 	if (skb_vlan_tag_present(skb)) {
540 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
541 				 skb_vlan_tag_get(skb);
542 		/* Currently supports 8021Q, 8021AD vlan offloads
543 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
544 		 */
545 		if (skb->vlan_proto == htons(ETH_P_8021Q))
546 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
547 	}
548 
549 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
550 	    ptp->tx_tstamp_en) {
551 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
552 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
553 			tx_buf->is_ts_pkt = 1;
554 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
555 		} else if (!skb_is_gso(skb)) {
556 			u16 seq_id, hdr_off;
557 
558 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
559 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
560 				if (vlan_tag_flags)
561 					hdr_off += VLAN_HLEN;
562 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
563 				tx_buf->is_ts_pkt = 1;
564 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
565 
566 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
567 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
568 				tx_buf->txts_prod = txts_prod;
569 			}
570 		}
571 	}
572 	if (unlikely(skb->no_fcs))
573 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
574 
575 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
576 	    skb_frags_readable(skb) && !lflags) {
577 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
578 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
579 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
580 		void __iomem *db = txr->tx_db.doorbell;
581 		void *pdata = tx_push_buf->data;
582 		u64 *end;
583 		int j, push_len;
584 
585 		/* Set COAL_NOW to be ready quickly for the next push */
586 		tx_push->tx_bd_len_flags_type =
587 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
588 					TX_BD_TYPE_LONG_TX_BD |
589 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
590 					TX_BD_FLAGS_COAL_NOW |
591 					TX_BD_FLAGS_PACKET_END |
592 					TX_BD_CNT(2));
593 
594 		if (skb->ip_summed == CHECKSUM_PARTIAL)
595 			tx_push1->tx_bd_hsize_lflags =
596 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
597 		else
598 			tx_push1->tx_bd_hsize_lflags = 0;
599 
600 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
601 		tx_push1->tx_bd_cfa_action =
602 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
603 
604 		end = pdata + length;
605 		end = PTR_ALIGN(end, 8) - 1;
606 		*end = 0;
607 
608 		skb_copy_from_linear_data(skb, pdata, len);
609 		pdata += len;
610 		for (j = 0; j < last_frag; j++) {
611 			void *fptr;
612 
613 			frag = &skb_shinfo(skb)->frags[j];
614 			fptr = skb_frag_address_safe(frag);
615 			if (!fptr)
616 				goto normal_tx;
617 
618 			memcpy(pdata, fptr, skb_frag_size(frag));
619 			pdata += skb_frag_size(frag);
620 		}
621 
622 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
623 		txbd->tx_bd_haddr = txr->data_mapping;
624 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
625 		prod = NEXT_TX(prod);
626 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
627 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
628 		memcpy(txbd, tx_push1, sizeof(*txbd));
629 		prod = NEXT_TX(prod);
630 		tx_push->doorbell =
631 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
632 				    DB_RING_IDX(&txr->tx_db, prod));
633 		WRITE_ONCE(txr->tx_prod, prod);
634 
635 		tx_buf->is_push = 1;
636 		netdev_tx_sent_queue(txq, skb->len);
637 		wmb();	/* Sync is_push and byte queue before pushing data */
638 
639 		push_len = (length + sizeof(*tx_push) + 7) / 8;
640 		if (push_len > 16) {
641 			__iowrite64_copy(db, tx_push_buf, 16);
642 			__iowrite32_copy(db + 4, tx_push_buf + 1,
643 					 (push_len - 16) << 1);
644 		} else {
645 			__iowrite64_copy(db, tx_push_buf, push_len);
646 		}
647 
648 		goto tx_done;
649 	}
650 
651 normal_tx:
652 	if (length < BNXT_MIN_PKT_SIZE) {
653 		pad = BNXT_MIN_PKT_SIZE - length;
654 		if (skb_pad(skb, pad))
655 			/* SKB already freed. */
656 			goto tx_kick_pending;
657 		length = BNXT_MIN_PKT_SIZE;
658 	}
659 
660 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
661 
662 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
663 		goto tx_free;
664 
665 	dma_unmap_addr_set(tx_buf, mapping, mapping);
666 	dma_unmap_len_set(tx_buf, len, len);
667 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
668 		TX_BD_CNT(last_frag + 2);
669 
670 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
671 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
672 
673 	prod = NEXT_TX(prod);
674 	txbd1 = bnxt_init_ext_bd(bp, txr, prod, lflags, vlan_tag_flags,
675 				 cfa_action);
676 
677 	if (skb_is_gso(skb)) {
678 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
679 		u32 hdr_len;
680 
681 		if (skb->encapsulation) {
682 			if (udp_gso)
683 				hdr_len = skb_inner_transport_offset(skb) +
684 					  sizeof(struct udphdr);
685 			else
686 				hdr_len = skb_inner_tcp_all_headers(skb);
687 		} else if (udp_gso) {
688 			hdr_len = skb_transport_offset(skb) +
689 				  sizeof(struct udphdr);
690 		} else {
691 			hdr_len = skb_tcp_all_headers(skb);
692 		}
693 
694 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
695 					TX_BD_FLAGS_T_IPID |
696 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
697 		length = skb_shinfo(skb)->gso_size;
698 		txbd1->tx_bd_mss = cpu_to_le32(length);
699 		length += hdr_len;
700 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
701 		txbd1->tx_bd_hsize_lflags |=
702 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
703 	}
704 
705 	length >>= 9;
706 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
707 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
708 				     skb->len);
709 		i = 0;
710 		goto tx_dma_error;
711 	}
712 	flags |= bnxt_lhint_arr[length];
713 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
714 
715 	txbd0 = txbd;
716 	for (i = 0; i < last_frag; i++) {
717 		frag = &skb_shinfo(skb)->frags[i];
718 		prod = NEXT_TX(prod);
719 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
720 
721 		len = skb_frag_size(frag);
722 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
723 					   DMA_TO_DEVICE);
724 
725 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
726 			goto tx_dma_error;
727 
728 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
729 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
730 					  mapping, mapping);
731 		dma_unmap_len_set(tx_buf, len, len);
732 
733 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
734 
735 		flags = len << TX_BD_LEN_SHIFT;
736 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
737 	}
738 
739 	flags &= ~TX_BD_LEN;
740 	txbd->tx_bd_len_flags_type =
741 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
742 			    TX_BD_FLAGS_PACKET_END);
743 
744 	netdev_tx_sent_queue(txq, skb->len);
745 
746 	skb_tx_timestamp(skb);
747 
748 	prod = NEXT_TX(prod);
749 	WRITE_ONCE(txr->tx_prod, prod);
750 
751 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
752 		bnxt_txr_db_kick(bp, txr, prod);
753 	} else {
754 		if (free_size >= bp->tx_wake_thresh)
755 			txbd0->tx_bd_len_flags_type |=
756 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
757 		txr->kick_pending = 1;
758 	}
759 
760 tx_done:
761 
762 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
763 		if (netdev_xmit_more() && !tx_buf->is_push) {
764 			txbd0->tx_bd_len_flags_type &=
765 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
766 			bnxt_txr_db_kick(bp, txr, prod);
767 		}
768 
769 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
770 				   bp->tx_wake_thresh);
771 	}
772 	return NETDEV_TX_OK;
773 
774 tx_dma_error:
775 	last_frag = i;
776 
777 	/* start back at beginning and unmap skb */
778 	prod = txr->tx_prod;
779 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
780 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
781 			 skb_headlen(skb), DMA_TO_DEVICE);
782 	prod = NEXT_TX(prod);
783 
784 	/* unmap remaining mapped pages */
785 	for (i = 0; i < last_frag; i++) {
786 		prod = NEXT_TX(prod);
787 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
788 		frag = &skb_shinfo(skb)->frags[i];
789 		netmem_dma_unmap_page_attrs(&pdev->dev,
790 					    dma_unmap_addr(tx_buf, mapping),
791 					    skb_frag_size(frag),
792 					    DMA_TO_DEVICE, 0);
793 	}
794 
795 tx_free:
796 	dev_kfree_skb_any(skb);
797 tx_kick_pending:
798 	if (BNXT_TX_PTP_IS_SET(lflags)) {
799 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
800 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
801 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
802 			/* set SKB to err so PTP worker will clean up */
803 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
804 	}
805 	if (txr->kick_pending)
806 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
807 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
808 	dev_core_stats_tx_dropped_inc(dev);
809 	return NETDEV_TX_OK;
810 }
811 
812 /* Returns true if some remaining TX packets not processed. */
813 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
814 			  int budget)
815 {
816 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
817 	struct pci_dev *pdev = bp->pdev;
818 	u16 hw_cons = txr->tx_hw_cons;
819 	unsigned int tx_bytes = 0;
820 	u16 cons = txr->tx_cons;
821 	unsigned int dma_len;
822 	dma_addr_t dma_addr;
823 	int tx_pkts = 0;
824 	bool rc = false;
825 
826 	while (RING_TX(bp, cons) != hw_cons) {
827 		struct bnxt_sw_tx_bd *tx_buf, *head_buf;
828 		struct sk_buff *skb;
829 		bool is_ts_pkt;
830 		int j, last;
831 
832 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
833 		head_buf = tx_buf;
834 		skb = tx_buf->skb;
835 
836 		if (unlikely(!skb)) {
837 			bnxt_sched_reset_txr(bp, txr, cons);
838 			return rc;
839 		}
840 
841 		is_ts_pkt = tx_buf->is_ts_pkt;
842 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
843 			rc = true;
844 			break;
845 		}
846 
847 		cons = NEXT_TX(cons);
848 		tx_pkts++;
849 		tx_bytes += skb->len;
850 		tx_buf->skb = NULL;
851 		tx_buf->is_ts_pkt = 0;
852 
853 		if (tx_buf->is_push) {
854 			tx_buf->is_push = 0;
855 			goto next_tx_int;
856 		}
857 
858 		if (dma_unmap_len(tx_buf, len)) {
859 			dma_addr = dma_unmap_addr(tx_buf, mapping);
860 			dma_len = dma_unmap_len(tx_buf, len);
861 
862 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
863 					 DMA_TO_DEVICE);
864 		}
865 
866 		last = tx_buf->nr_frags;
867 
868 		for (j = 0; j < last; j++) {
869 			cons = NEXT_TX(cons);
870 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
871 			if (dma_unmap_len(tx_buf, len)) {
872 				dma_addr = dma_unmap_addr(tx_buf, mapping);
873 				dma_len = dma_unmap_len(tx_buf, len);
874 
875 				netmem_dma_unmap_page_attrs(&pdev->dev,
876 							    dma_addr, dma_len,
877 							    DMA_TO_DEVICE, 0);
878 			}
879 		}
880 
881 		if (unlikely(head_buf->is_sw_gso)) {
882 			u16 inline_cons = txr->tx_inline_cons + 1;
883 
884 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
885 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
886 				tso_dma_map_complete(&pdev->dev,
887 						     &head_buf->sw_gso_cstate);
888 			} else {
889 				tx_pkts--;
890 				tx_bytes -= skb->len;
891 				skb = NULL;
892 			}
893 			head_buf->is_sw_gso = 0;
894 		}
895 
896 		if (unlikely(is_ts_pkt)) {
897 			if (BNXT_CHIP_P5(bp)) {
898 				/* PTP worker takes ownership of the skb */
899 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
900 				skb = NULL;
901 			}
902 		}
903 
904 next_tx_int:
905 		cons = NEXT_TX(cons);
906 
907 		napi_consume_skb(skb, budget);
908 	}
909 
910 	WRITE_ONCE(txr->tx_cons, cons);
911 
912 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
913 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
914 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
915 
916 	return rc;
917 }
918 
919 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
920 {
921 	struct bnxt_tx_ring_info *txr;
922 	bool more = false;
923 	int i;
924 
925 	bnxt_for_each_napi_tx(i, bnapi, txr) {
926 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
927 			more |= __bnxt_tx_int(bp, txr, budget);
928 	}
929 	if (!more)
930 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
931 }
932 
933 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
934 {
935 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
936 }
937 
938 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
939 					 struct bnxt_rx_ring_info *rxr,
940 					 unsigned int *offset,
941 					 gfp_t gfp)
942 {
943 	struct page *page;
944 
945 	if (rxr->rx_page_size < PAGE_SIZE) {
946 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
947 						rxr->rx_page_size);
948 	} else {
949 		page = page_pool_dev_alloc_pages(rxr->page_pool);
950 		*offset = 0;
951 	}
952 	if (!page)
953 		return NULL;
954 
955 	*mapping = page_pool_get_dma_addr(page) + *offset;
956 	return page;
957 }
958 
959 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
960 					 struct bnxt_rx_ring_info *rxr,
961 					 unsigned int *offset,
962 					 gfp_t gfp)
963 {
964 	netmem_ref netmem;
965 
966 	if (rxr->rx_page_size < PAGE_SIZE) {
967 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
968 						     rxr->rx_page_size, gfp);
969 	} else {
970 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
971 		*offset = 0;
972 	}
973 	if (!netmem)
974 		return 0;
975 
976 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
977 	return netmem;
978 }
979 
980 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
981 				       struct bnxt_rx_ring_info *rxr,
982 				       gfp_t gfp)
983 {
984 	unsigned int offset;
985 	struct page *page;
986 
987 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
988 				    bp->rx_buf_size, gfp);
989 	if (!page)
990 		return NULL;
991 
992 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
993 	return page_address(page) + offset;
994 }
995 
996 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
997 		       u16 prod, gfp_t gfp)
998 {
999 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1000 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1001 	dma_addr_t mapping;
1002 
1003 	if (BNXT_RX_PAGE_MODE(bp)) {
1004 		unsigned int offset;
1005 		struct page *page =
1006 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1007 
1008 		if (!page)
1009 			return -ENOMEM;
1010 
1011 		mapping += bp->rx_dma_offset;
1012 		rx_buf->data = page;
1013 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
1014 		rx_buf->offset = offset;
1015 	} else {
1016 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
1017 
1018 		if (!data)
1019 			return -ENOMEM;
1020 
1021 		rx_buf->data = data;
1022 		rx_buf->data_ptr = data + bp->rx_offset;
1023 		rx_buf->offset = 0;
1024 	}
1025 	rx_buf->mapping = mapping;
1026 
1027 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1028 	return 0;
1029 }
1030 
1031 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1032 {
1033 	u16 prod = rxr->rx_prod;
1034 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1035 	struct bnxt *bp = rxr->bnapi->bp;
1036 	struct rx_bd *cons_bd, *prod_bd;
1037 
1038 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1039 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1040 
1041 	prod_rx_buf->data = data;
1042 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1043 
1044 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1045 	prod_rx_buf->offset = cons_rx_buf->offset;
1046 
1047 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1048 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1049 
1050 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1051 }
1052 
1053 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1054 {
1055 	u16 next, max = rxr->rx_agg_bmap_size;
1056 
1057 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1058 	if (next >= max)
1059 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1060 	return next;
1061 }
1062 
1063 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1064 				u16 prod, gfp_t gfp)
1065 {
1066 	struct rx_bd *rxbd =
1067 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1068 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1069 	u16 sw_prod = rxr->rx_sw_agg_prod;
1070 	unsigned int offset = 0;
1071 	dma_addr_t mapping;
1072 	netmem_ref netmem;
1073 
1074 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1075 	if (!netmem)
1076 		return -ENOMEM;
1077 
1078 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1079 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1080 
1081 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1082 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1083 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1084 
1085 	rx_agg_buf->netmem = netmem;
1086 	rx_agg_buf->offset = offset;
1087 	rx_agg_buf->mapping = mapping;
1088 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1089 	rxbd->rx_bd_opaque = sw_prod;
1090 	return 0;
1091 }
1092 
1093 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1094 				       struct bnxt_cp_ring_info *cpr,
1095 				       u16 cp_cons, u16 curr)
1096 {
1097 	struct rx_agg_cmp *agg;
1098 
1099 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1100 	agg = (struct rx_agg_cmp *)
1101 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1102 	return agg;
1103 }
1104 
1105 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1106 					      struct bnxt_rx_ring_info *rxr,
1107 					      u16 agg_id, u16 curr)
1108 {
1109 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1110 
1111 	return &tpa_info->agg_arr[curr];
1112 }
1113 
1114 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1115 				   u16 start, u32 agg_bufs, bool tpa)
1116 {
1117 	struct bnxt_napi *bnapi = cpr->bnapi;
1118 	struct bnxt *bp = bnapi->bp;
1119 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1120 	u16 prod = rxr->rx_agg_prod;
1121 	u16 sw_prod = rxr->rx_sw_agg_prod;
1122 	bool p5_tpa = false;
1123 	u32 i;
1124 
1125 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1126 		p5_tpa = true;
1127 
1128 	for (i = 0; i < agg_bufs; i++) {
1129 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1130 		struct rx_agg_cmp *agg;
1131 		struct rx_bd *prod_bd;
1132 		netmem_ref netmem;
1133 		u16 cons;
1134 
1135 		if (p5_tpa)
1136 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1137 		else
1138 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1139 		cons = agg->rx_agg_cmp_opaque;
1140 		__clear_bit(cons, rxr->rx_agg_bmap);
1141 
1142 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1143 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1144 
1145 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1146 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1147 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1148 
1149 		/* It is possible for sw_prod to be equal to cons, so
1150 		 * set cons_rx_buf->netmem to 0 first.
1151 		 */
1152 		netmem = cons_rx_buf->netmem;
1153 		cons_rx_buf->netmem = 0;
1154 		prod_rx_buf->netmem = netmem;
1155 		prod_rx_buf->offset = cons_rx_buf->offset;
1156 
1157 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1158 
1159 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1160 
1161 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1162 		prod_bd->rx_bd_opaque = sw_prod;
1163 
1164 		prod = NEXT_RX_AGG(prod);
1165 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1166 	}
1167 	rxr->rx_agg_prod = prod;
1168 	rxr->rx_sw_agg_prod = sw_prod;
1169 }
1170 
1171 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1172 					      struct bnxt_rx_ring_info *rxr,
1173 					      u16 cons, void *data, u8 *data_ptr,
1174 					      dma_addr_t dma_addr,
1175 					      unsigned int offset_and_len)
1176 {
1177 	unsigned int len = offset_and_len & 0xffff;
1178 	struct page *page = data;
1179 	u16 prod = rxr->rx_prod;
1180 	struct sk_buff *skb;
1181 	void *frag_start;
1182 	int err;
1183 
1184 	frag_start = page_address(page) + rxr->rx_buf_ring[cons].offset;
1185 
1186 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1187 	if (unlikely(err)) {
1188 		bnxt_reuse_rx_data(rxr, cons, data);
1189 		return NULL;
1190 	}
1191 	dma_addr -= bp->rx_dma_offset;
1192 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1193 				bp->rx_dir);
1194 	skb = napi_build_skb(frag_start, rxr->rx_page_size);
1195 	if (!skb) {
1196 		page_pool_recycle_direct(rxr->page_pool, page);
1197 		return NULL;
1198 	}
1199 	skb_mark_for_recycle(skb);
1200 	skb_reserve(skb, data_ptr - (u8 *)frag_start);
1201 	__skb_put(skb, len);
1202 
1203 	return skb;
1204 }
1205 
1206 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1207 					struct bnxt_rx_ring_info *rxr,
1208 					u16 cons, void *data, u8 *data_ptr,
1209 					dma_addr_t dma_addr,
1210 					unsigned int offset_and_len)
1211 {
1212 	unsigned int payload = offset_and_len >> 16;
1213 	unsigned int len = offset_and_len & 0xffff;
1214 	skb_frag_t *frag;
1215 	struct page *page = data;
1216 	u16 prod = rxr->rx_prod;
1217 	struct sk_buff *skb;
1218 	int off, err;
1219 
1220 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1221 	if (unlikely(err)) {
1222 		bnxt_reuse_rx_data(rxr, cons, data);
1223 		return NULL;
1224 	}
1225 	dma_addr -= bp->rx_dma_offset;
1226 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1227 				bp->rx_dir);
1228 
1229 	if (unlikely(!payload))
1230 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1231 
1232 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1233 	if (!skb) {
1234 		page_pool_recycle_direct(rxr->page_pool, page);
1235 		return NULL;
1236 	}
1237 
1238 	skb_mark_for_recycle(skb);
1239 	off = (void *)data_ptr - page_address(page);
1240 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1241 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1242 	       payload + NET_IP_ALIGN);
1243 
1244 	frag = &skb_shinfo(skb)->frags[0];
1245 	skb_frag_size_sub(frag, payload);
1246 	skb_frag_off_add(frag, payload);
1247 	skb->data_len -= payload;
1248 	skb->tail += payload;
1249 
1250 	return skb;
1251 }
1252 
1253 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1254 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1255 				   void *data, u8 *data_ptr,
1256 				   dma_addr_t dma_addr,
1257 				   unsigned int offset_and_len)
1258 {
1259 	u16 prod = rxr->rx_prod;
1260 	struct sk_buff *skb;
1261 	int err;
1262 
1263 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1264 	if (unlikely(err)) {
1265 		bnxt_reuse_rx_data(rxr, cons, data);
1266 		return NULL;
1267 	}
1268 
1269 	skb = napi_build_skb(data, bp->rx_buf_size);
1270 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1271 				bp->rx_dir);
1272 	if (!skb) {
1273 		page_pool_free_va(rxr->head_pool, data, true);
1274 		return NULL;
1275 	}
1276 
1277 	skb_mark_for_recycle(skb);
1278 	skb_reserve(skb, bp->rx_offset);
1279 	skb_put(skb, offset_and_len & 0xffff);
1280 	return skb;
1281 }
1282 
1283 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1284 				 struct bnxt_cp_ring_info *cpr,
1285 				 u16 idx, u32 agg_bufs, bool tpa,
1286 				 struct sk_buff *skb,
1287 				 struct xdp_buff *xdp)
1288 {
1289 	struct bnxt_napi *bnapi = cpr->bnapi;
1290 	struct skb_shared_info *shinfo;
1291 	struct bnxt_rx_ring_info *rxr;
1292 	u32 i, total_frag_len = 0;
1293 	bool p5_tpa = false;
1294 	u16 prod;
1295 
1296 	rxr = bnapi->rx_ring;
1297 	prod = rxr->rx_agg_prod;
1298 
1299 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1300 		p5_tpa = true;
1301 
1302 	if (skb)
1303 		shinfo = skb_shinfo(skb);
1304 	else
1305 		shinfo = xdp_get_shared_info_from_buff(xdp);
1306 
1307 	for (i = 0; i < agg_bufs; i++) {
1308 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1309 		struct rx_agg_cmp *agg;
1310 		u16 cons, frag_len;
1311 		netmem_ref netmem;
1312 
1313 		if (p5_tpa)
1314 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1315 		else
1316 			agg = bnxt_get_agg(bp, cpr, idx, i);
1317 		cons = agg->rx_agg_cmp_opaque;
1318 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1319 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1320 
1321 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1322 		if (skb) {
1323 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1324 					       cons_rx_buf->offset,
1325 					       frag_len, rxr->rx_page_size);
1326 		} else {
1327 			skb_frag_t *frag = &shinfo->frags[i];
1328 
1329 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1330 						  cons_rx_buf->offset,
1331 						  frag_len);
1332 			shinfo->nr_frags = i + 1;
1333 		}
1334 		__clear_bit(cons, rxr->rx_agg_bmap);
1335 
1336 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1337 		 * a sw_prod index that equals the cons index, so we
1338 		 * need to clear the cons entry now.
1339 		 */
1340 		netmem = cons_rx_buf->netmem;
1341 		cons_rx_buf->netmem = 0;
1342 
1343 		if (xdp && netmem_is_pfmemalloc(netmem))
1344 			xdp_buff_set_frag_pfmemalloc(xdp);
1345 
1346 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1347 			if (skb) {
1348 				skb->len -= frag_len;
1349 				skb->data_len -= frag_len;
1350 				skb->truesize -= rxr->rx_page_size;
1351 			}
1352 
1353 			--shinfo->nr_frags;
1354 			cons_rx_buf->netmem = netmem;
1355 
1356 			/* Update prod since possibly some netmems have been
1357 			 * allocated already.
1358 			 */
1359 			rxr->rx_agg_prod = prod;
1360 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1361 			return 0;
1362 		}
1363 
1364 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1365 						  rxr->rx_page_size);
1366 
1367 		total_frag_len += frag_len;
1368 		prod = NEXT_RX_AGG(prod);
1369 	}
1370 	rxr->rx_agg_prod = prod;
1371 	return total_frag_len;
1372 }
1373 
1374 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1375 					       struct bnxt_cp_ring_info *cpr,
1376 					       struct sk_buff *skb, u16 idx,
1377 					       u32 agg_bufs, bool tpa)
1378 {
1379 	u32 total_frag_len = 0;
1380 
1381 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1382 					       skb, NULL);
1383 	if (!total_frag_len) {
1384 		skb_mark_for_recycle(skb);
1385 		dev_kfree_skb(skb);
1386 		return NULL;
1387 	}
1388 
1389 	return skb;
1390 }
1391 
1392 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1393 				   struct bnxt_cp_ring_info *cpr,
1394 				   struct xdp_buff *xdp, u16 idx,
1395 				   u32 agg_bufs, bool tpa)
1396 {
1397 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1398 	u32 total_frag_len = 0;
1399 
1400 	if (!xdp_buff_has_frags(xdp))
1401 		shinfo->nr_frags = 0;
1402 
1403 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1404 					       NULL, xdp);
1405 	if (total_frag_len) {
1406 		xdp_buff_set_frags_flag(xdp);
1407 		shinfo->nr_frags = agg_bufs;
1408 		shinfo->xdp_frags_size = total_frag_len;
1409 	}
1410 	return total_frag_len;
1411 }
1412 
1413 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1414 			       u8 agg_bufs, u32 *raw_cons)
1415 {
1416 	u16 last;
1417 	struct rx_agg_cmp *agg;
1418 
1419 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1420 	last = RING_CMP(*raw_cons);
1421 	agg = (struct rx_agg_cmp *)
1422 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1423 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1424 }
1425 
1426 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1427 				      unsigned int len,
1428 				      dma_addr_t mapping)
1429 {
1430 	struct bnxt *bp = bnapi->bp;
1431 	struct pci_dev *pdev = bp->pdev;
1432 	struct sk_buff *skb;
1433 
1434 	skb = napi_alloc_skb(&bnapi->napi, len);
1435 	if (!skb)
1436 		return NULL;
1437 
1438 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1439 				bp->rx_dir);
1440 
1441 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1442 	       len + NET_IP_ALIGN);
1443 
1444 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1445 				   bp->rx_dir);
1446 
1447 	skb_put(skb, len);
1448 
1449 	return skb;
1450 }
1451 
1452 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1453 				     unsigned int len,
1454 				     dma_addr_t mapping)
1455 {
1456 	return bnxt_copy_data(bnapi, data, len, mapping);
1457 }
1458 
1459 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1460 				     struct xdp_buff *xdp,
1461 				     unsigned int len,
1462 				     dma_addr_t mapping)
1463 {
1464 	unsigned int metasize = 0;
1465 	u8 *data = xdp->data;
1466 	struct sk_buff *skb;
1467 
1468 	len = xdp->data_end - xdp->data_meta;
1469 	metasize = xdp->data - xdp->data_meta;
1470 	data = xdp->data_meta;
1471 
1472 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1473 	if (!skb)
1474 		return skb;
1475 
1476 	if (metasize) {
1477 		skb_metadata_set(skb, metasize);
1478 		__skb_pull(skb, metasize);
1479 	}
1480 
1481 	return skb;
1482 }
1483 
1484 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1485 			   u32 *raw_cons, void *cmp)
1486 {
1487 	struct rx_cmp *rxcmp = cmp;
1488 	u32 tmp_raw_cons = *raw_cons;
1489 	u8 cmp_type, agg_bufs = 0;
1490 
1491 	cmp_type = RX_CMP_TYPE(rxcmp);
1492 
1493 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1494 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1495 			    RX_CMP_AGG_BUFS) >>
1496 			   RX_CMP_AGG_BUFS_SHIFT;
1497 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1498 		struct rx_tpa_end_cmp *tpa_end = cmp;
1499 
1500 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1501 			return 0;
1502 
1503 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1504 	}
1505 
1506 	if (agg_bufs) {
1507 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1508 			return -EBUSY;
1509 	}
1510 	*raw_cons = tmp_raw_cons;
1511 	return 0;
1512 }
1513 
1514 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1515 {
1516 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1517 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1518 
1519 	if (test_bit(idx, map->agg_idx_bmap)) {
1520 		idx = find_first_zero_bit(map->agg_idx_bmap, MAX_TPA_P5);
1521 		if (idx >= MAX_TPA_P5)
1522 			return INVALID_HW_RING_ID;
1523 	}
1524 	__set_bit(idx, map->agg_idx_bmap);
1525 	map->agg_id_tbl[agg_id] = idx;
1526 	return idx;
1527 }
1528 
1529 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1530 {
1531 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1532 
1533 	__clear_bit(idx, map->agg_idx_bmap);
1534 }
1535 
1536 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1537 {
1538 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1539 
1540 	return map->agg_id_tbl[agg_id];
1541 }
1542 
1543 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1544 			      struct rx_tpa_start_cmp *tpa_start,
1545 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1546 {
1547 	tpa_info->cfa_code_valid = 1;
1548 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1549 	tpa_info->vlan_valid = 0;
1550 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1551 		tpa_info->vlan_valid = 1;
1552 		tpa_info->metadata =
1553 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1554 	}
1555 }
1556 
1557 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1558 				 struct rx_tpa_start_cmp *tpa_start,
1559 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1560 {
1561 	tpa_info->vlan_valid = 0;
1562 	if (TPA_START_VLAN_VALID(tpa_start)) {
1563 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1564 		u32 vlan_proto = ETH_P_8021Q;
1565 
1566 		tpa_info->vlan_valid = 1;
1567 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1568 			vlan_proto = ETH_P_8021AD;
1569 		tpa_info->metadata = vlan_proto << 16 |
1570 				     TPA_START_METADATA0_TCI(tpa_start1);
1571 	}
1572 }
1573 
1574 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1575 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1576 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1577 {
1578 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1579 	struct bnxt_tpa_info *tpa_info;
1580 	u16 cons, prod, agg_id;
1581 	struct rx_bd *prod_bd;
1582 	dma_addr_t mapping;
1583 
1584 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1585 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1586 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1587 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1588 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1589 				    rxr->bnapi->index,
1590 				    TPA_START_AGG_ID_P5(tpa_start));
1591 			bnxt_sched_reset_rxr(bp, rxr);
1592 			return;
1593 		}
1594 	} else {
1595 		agg_id = TPA_START_AGG_ID(tpa_start);
1596 	}
1597 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1598 	prod = rxr->rx_prod;
1599 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1600 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1601 	tpa_info = &rxr->rx_tpa[agg_id];
1602 
1603 	if (unlikely(cons != rxr->rx_next_cons ||
1604 		     TPA_START_ERROR(tpa_start))) {
1605 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1606 			    cons, rxr->rx_next_cons,
1607 			    TPA_START_ERROR_CODE(tpa_start1));
1608 		bnxt_sched_reset_rxr(bp, rxr);
1609 		return;
1610 	}
1611 	prod_rx_buf->data = tpa_info->data;
1612 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1613 
1614 	mapping = tpa_info->mapping;
1615 	prod_rx_buf->mapping = mapping;
1616 
1617 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1618 
1619 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1620 
1621 	tpa_info->data = cons_rx_buf->data;
1622 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1623 	cons_rx_buf->data = NULL;
1624 	tpa_info->mapping = cons_rx_buf->mapping;
1625 
1626 	tpa_info->len =
1627 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1628 				RX_TPA_START_CMP_LEN_SHIFT;
1629 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1630 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1631 		tpa_info->gso_type = SKB_GSO_TCPV4;
1632 		if (TPA_START_IS_IPV6(tpa_start1))
1633 			tpa_info->gso_type = SKB_GSO_TCPV6;
1634 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1635 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1636 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1637 			tpa_info->gso_type = SKB_GSO_TCPV6;
1638 		tpa_info->rss_hash =
1639 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1640 	} else {
1641 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1642 		tpa_info->gso_type = 0;
1643 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1644 	}
1645 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1646 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1647 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1648 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1649 	else
1650 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1651 	tpa_info->agg_count = 0;
1652 
1653 	rxr->rx_prod = NEXT_RX(prod);
1654 	cons = RING_RX(bp, NEXT_RX(cons));
1655 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1656 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1657 
1658 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1659 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1660 	cons_rx_buf->data = NULL;
1661 }
1662 
1663 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1664 {
1665 	if (agg_bufs)
1666 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1667 }
1668 
1669 #ifdef CONFIG_INET
1670 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1671 {
1672 	struct udphdr *uh = NULL;
1673 
1674 	if (ip_proto == htons(ETH_P_IP)) {
1675 		struct iphdr *iph = (struct iphdr *)skb->data;
1676 
1677 		if (iph->protocol == IPPROTO_UDP)
1678 			uh = (struct udphdr *)(iph + 1);
1679 	} else {
1680 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1681 
1682 		if (iph->nexthdr == IPPROTO_UDP)
1683 			uh = (struct udphdr *)(iph + 1);
1684 	}
1685 	if (uh) {
1686 		if (uh->check)
1687 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1688 		else
1689 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1690 	}
1691 }
1692 #endif
1693 
1694 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1695 					   int payload_off, int tcp_ts,
1696 					   struct sk_buff *skb)
1697 {
1698 #ifdef CONFIG_INET
1699 	struct tcphdr *th;
1700 	int len, nw_off;
1701 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1702 	u32 hdr_info = tpa_info->hdr_info;
1703 	bool loopback = false;
1704 
1705 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1706 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1707 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1708 
1709 	/* If the packet is an internal loopback packet, the offsets will
1710 	 * have an extra 4 bytes.
1711 	 */
1712 	if (inner_mac_off == 4) {
1713 		loopback = true;
1714 	} else if (inner_mac_off > 4) {
1715 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1716 					    ETH_HLEN - 2));
1717 
1718 		/* We only support inner iPv4/ipv6.  If we don't see the
1719 		 * correct protocol ID, it must be a loopback packet where
1720 		 * the offsets are off by 4.
1721 		 */
1722 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1723 			loopback = true;
1724 	}
1725 	if (loopback) {
1726 		/* internal loopback packet, subtract all offsets by 4 */
1727 		inner_ip_off -= 4;
1728 		inner_mac_off -= 4;
1729 		outer_ip_off -= 4;
1730 	}
1731 
1732 	nw_off = inner_ip_off - ETH_HLEN;
1733 	skb_set_network_header(skb, nw_off);
1734 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1735 		struct ipv6hdr *iph = ipv6_hdr(skb);
1736 
1737 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1738 		len = skb->len - skb_transport_offset(skb);
1739 		th = tcp_hdr(skb);
1740 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1741 	} else {
1742 		struct iphdr *iph = ip_hdr(skb);
1743 
1744 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1745 		len = skb->len - skb_transport_offset(skb);
1746 		th = tcp_hdr(skb);
1747 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1748 	}
1749 
1750 	if (inner_mac_off) { /* tunnel */
1751 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1752 					    ETH_HLEN - 2));
1753 
1754 		bnxt_gro_tunnel(skb, proto);
1755 	}
1756 #endif
1757 	return skb;
1758 }
1759 
1760 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1761 					   int payload_off, int tcp_ts,
1762 					   struct sk_buff *skb)
1763 {
1764 #ifdef CONFIG_INET
1765 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1766 	u32 hdr_info = tpa_info->hdr_info;
1767 	int iphdr_len, nw_off;
1768 
1769 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1770 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1771 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1772 
1773 	nw_off = inner_ip_off - ETH_HLEN;
1774 	skb_set_network_header(skb, nw_off);
1775 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1776 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1777 	skb_set_transport_header(skb, nw_off + iphdr_len);
1778 
1779 	if (inner_mac_off) { /* tunnel */
1780 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1781 					    ETH_HLEN - 2));
1782 
1783 		bnxt_gro_tunnel(skb, proto);
1784 	}
1785 #endif
1786 	return skb;
1787 }
1788 
1789 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1790 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1791 
1792 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1793 					   int payload_off, int tcp_ts,
1794 					   struct sk_buff *skb)
1795 {
1796 #ifdef CONFIG_INET
1797 	struct tcphdr *th;
1798 	int len, nw_off, tcp_opt_len = 0;
1799 
1800 	if (tcp_ts)
1801 		tcp_opt_len = 12;
1802 
1803 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1804 		struct iphdr *iph;
1805 
1806 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1807 			 ETH_HLEN;
1808 		skb_set_network_header(skb, nw_off);
1809 		iph = ip_hdr(skb);
1810 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1811 		len = skb->len - skb_transport_offset(skb);
1812 		th = tcp_hdr(skb);
1813 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1814 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1815 		struct ipv6hdr *iph;
1816 
1817 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1818 			 ETH_HLEN;
1819 		skb_set_network_header(skb, nw_off);
1820 		iph = ipv6_hdr(skb);
1821 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1822 		len = skb->len - skb_transport_offset(skb);
1823 		th = tcp_hdr(skb);
1824 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1825 	} else {
1826 		dev_kfree_skb_any(skb);
1827 		return NULL;
1828 	}
1829 
1830 	if (nw_off) /* tunnel */
1831 		bnxt_gro_tunnel(skb, skb->protocol);
1832 #endif
1833 	return skb;
1834 }
1835 
1836 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1837 					   struct bnxt_tpa_info *tpa_info,
1838 					   struct rx_tpa_end_cmp *tpa_end,
1839 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1840 					   struct sk_buff *skb,
1841 					   struct bnxt_rx_sw_stats *rx_stats)
1842 {
1843 #ifdef CONFIG_INET
1844 	int payload_off;
1845 	u16 segs;
1846 
1847 	segs = TPA_END_TPA_SEGS(tpa_end);
1848 	if (segs == 1)
1849 		return skb;
1850 
1851 	rx_stats->rx_hw_gro_packets++;
1852 	rx_stats->rx_hw_gro_wire_packets += segs;
1853 
1854 	NAPI_GRO_CB(skb)->count = segs;
1855 	skb_shinfo(skb)->gso_size =
1856 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1857 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1858 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1859 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1860 	else
1861 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1862 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1863 	if (likely(skb))
1864 		tcp_gro_complete(skb);
1865 #endif
1866 	return skb;
1867 }
1868 
1869 /* Given the cfa_code of a received packet determine which
1870  * netdev (vf-rep or PF) the packet is destined to.
1871  */
1872 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1873 {
1874 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1875 
1876 	/* if vf-rep dev is NULL, it must belong to the PF */
1877 	return dev ? dev : bp->dev;
1878 }
1879 
1880 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1881 					   struct bnxt_cp_ring_info *cpr,
1882 					   u32 *raw_cons,
1883 					   struct rx_tpa_end_cmp *tpa_end,
1884 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1885 					   u8 *event)
1886 {
1887 	struct bnxt_napi *bnapi = cpr->bnapi;
1888 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1889 	struct net_device *dev = bp->dev;
1890 	u8 *data_ptr, agg_bufs;
1891 	unsigned int len;
1892 	struct bnxt_tpa_info *tpa_info;
1893 	dma_addr_t mapping;
1894 	struct sk_buff *skb;
1895 	u16 idx = 0, agg_id;
1896 	void *data;
1897 	bool gro;
1898 
1899 	if (unlikely(bnapi->in_reset)) {
1900 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1901 
1902 		if (rc < 0)
1903 			return ERR_PTR(-EBUSY);
1904 		return NULL;
1905 	}
1906 
1907 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1908 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1909 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1910 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1911 		tpa_info = &rxr->rx_tpa[agg_id];
1912 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1913 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1914 				    agg_bufs, tpa_info->agg_count);
1915 			agg_bufs = tpa_info->agg_count;
1916 		}
1917 		tpa_info->agg_count = 0;
1918 		*event |= BNXT_AGG_EVENT;
1919 		bnxt_free_agg_idx(rxr, agg_id);
1920 		idx = agg_id;
1921 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1922 	} else {
1923 		agg_id = TPA_END_AGG_ID(tpa_end);
1924 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1925 		tpa_info = &rxr->rx_tpa[agg_id];
1926 		idx = RING_CMP(*raw_cons);
1927 		if (agg_bufs) {
1928 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1929 				return ERR_PTR(-EBUSY);
1930 
1931 			*event |= BNXT_AGG_EVENT;
1932 			idx = NEXT_CMP(idx);
1933 		}
1934 		gro = !!TPA_END_GRO(tpa_end);
1935 	}
1936 	data = tpa_info->data;
1937 	data_ptr = tpa_info->data_ptr;
1938 	prefetch(data_ptr);
1939 	len = tpa_info->len;
1940 	mapping = tpa_info->mapping;
1941 
1942 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1943 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1944 		if (agg_bufs > MAX_SKB_FRAGS)
1945 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1946 				    agg_bufs, (int)MAX_SKB_FRAGS);
1947 		return NULL;
1948 	}
1949 
1950 	if (len <= bp->rx_copybreak) {
1951 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1952 		if (!skb) {
1953 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1954 			cpr->sw_stats->rx.rx_oom_discards += 1;
1955 			return NULL;
1956 		}
1957 	} else {
1958 		u8 *new_data;
1959 		dma_addr_t new_mapping;
1960 
1961 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1962 						GFP_ATOMIC);
1963 		if (!new_data) {
1964 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1965 			cpr->sw_stats->rx.rx_oom_discards += 1;
1966 			return NULL;
1967 		}
1968 
1969 		tpa_info->data = new_data;
1970 		tpa_info->data_ptr = new_data + bp->rx_offset;
1971 		tpa_info->mapping = new_mapping;
1972 
1973 		skb = napi_build_skb(data, bp->rx_buf_size);
1974 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1975 					bp->rx_buf_use_size, bp->rx_dir);
1976 
1977 		if (!skb) {
1978 			page_pool_free_va(rxr->head_pool, data, true);
1979 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1980 			cpr->sw_stats->rx.rx_oom_discards += 1;
1981 			return NULL;
1982 		}
1983 		skb_mark_for_recycle(skb);
1984 		skb_reserve(skb, bp->rx_offset);
1985 		skb_put(skb, len);
1986 	}
1987 
1988 	if (agg_bufs) {
1989 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1990 					      true);
1991 		if (!skb) {
1992 			/* Page reuse already handled by bnxt_rx_pages(). */
1993 			cpr->sw_stats->rx.rx_oom_discards += 1;
1994 			return NULL;
1995 		}
1996 	}
1997 
1998 	if (tpa_info->cfa_code_valid)
1999 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
2000 	skb->protocol = eth_type_trans(skb, dev);
2001 
2002 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
2003 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
2004 
2005 	if (tpa_info->vlan_valid &&
2006 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
2007 		__be16 vlan_proto = htons(tpa_info->metadata >>
2008 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
2009 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2010 
2011 		if (eth_type_vlan(vlan_proto)) {
2012 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2013 		} else {
2014 			dev_kfree_skb(skb);
2015 			return NULL;
2016 		}
2017 	}
2018 
2019 	skb_checksum_none_assert(skb);
2020 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
2021 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2022 		skb->csum_level =
2023 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
2024 	}
2025 
2026 	if (gro)
2027 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
2028 				   &cpr->sw_stats->rx);
2029 
2030 	return skb;
2031 }
2032 
2033 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
2034 			 struct rx_agg_cmp *rx_agg)
2035 {
2036 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2037 	struct bnxt_tpa_info *tpa_info;
2038 
2039 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2040 	tpa_info = &rxr->rx_tpa[agg_id];
2041 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2042 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2043 }
2044 
2045 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2046 			     struct sk_buff *skb)
2047 {
2048 	skb_mark_for_recycle(skb);
2049 
2050 	if (skb->dev != bp->dev) {
2051 		/* this packet belongs to a vf-rep */
2052 		bnxt_vf_rep_rx(bp, skb);
2053 		return;
2054 	}
2055 	skb_record_rx_queue(skb, bnapi->index);
2056 	napi_gro_receive(&bnapi->napi, skb);
2057 }
2058 
2059 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2060 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2061 {
2062 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2063 
2064 	if (BNXT_PTP_RX_TS_VALID(flags))
2065 		goto ts_valid;
2066 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2067 		return false;
2068 
2069 ts_valid:
2070 	*cmpl_ts = ts;
2071 	return true;
2072 }
2073 
2074 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2075 				    struct rx_cmp *rxcmp,
2076 				    struct rx_cmp_ext *rxcmp1)
2077 {
2078 	__be16 vlan_proto;
2079 	u16 vtag;
2080 
2081 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2082 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2083 		u32 meta_data;
2084 
2085 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2086 			return skb;
2087 
2088 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2089 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2090 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2091 		if (eth_type_vlan(vlan_proto))
2092 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2093 		else
2094 			goto vlan_err;
2095 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2096 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2097 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2098 
2099 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2100 				vlan_proto = htons(ETH_P_8021Q);
2101 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2102 				vlan_proto = htons(ETH_P_8021AD);
2103 			else
2104 				goto vlan_err;
2105 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2106 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2107 		}
2108 	}
2109 	return skb;
2110 vlan_err:
2111 	skb_mark_for_recycle(skb);
2112 	dev_kfree_skb(skb);
2113 	return NULL;
2114 }
2115 
2116 /* returns the following:
2117  * 1       - 1 packet successfully received
2118  * 0       - successful TPA_START, packet not completed yet
2119  * -EBUSY  - completion ring does not have all the agg buffers yet
2120  * -ENOMEM - packet aborted due to out of memory
2121  * -EIO    - packet aborted due to hw error indicated in BD
2122  */
2123 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2124 		       u32 *raw_cons, u8 *event)
2125 {
2126 	struct bnxt_napi *bnapi = cpr->bnapi;
2127 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2128 	struct net_device *dev = bp->dev;
2129 	struct rx_cmp *rxcmp;
2130 	struct rx_cmp_ext *rxcmp1;
2131 	u32 tmp_raw_cons = *raw_cons;
2132 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2133 	struct skb_shared_info *sinfo;
2134 	struct bnxt_xdp_buff bnxt_xdp;
2135 	struct bnxt_sw_rx_bd *rx_buf;
2136 	unsigned int len;
2137 	u8 *data_ptr, agg_bufs, cmp_type;
2138 	bool xdp_active = false;
2139 	dma_addr_t dma_addr;
2140 	struct sk_buff *skb;
2141 	u32 flags, misc;
2142 	u32 cmpl_ts;
2143 	void *data;
2144 	int rc = 0;
2145 
2146 	rxcmp = (struct rx_cmp *)
2147 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2148 
2149 	cmp_type = RX_CMP_TYPE(rxcmp);
2150 
2151 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2152 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2153 		goto next_rx_no_prod_no_len;
2154 	}
2155 
2156 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2157 	cp_cons = RING_CMP(tmp_raw_cons);
2158 	rxcmp1 = (struct rx_cmp_ext *)
2159 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2160 
2161 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2162 		return -EBUSY;
2163 
2164 	/* The valid test of the entry must be done first before
2165 	 * reading any further.
2166 	 */
2167 	dma_rmb();
2168 	prod = rxr->rx_prod;
2169 
2170 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2171 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2172 		bnxt_tpa_start(bp, rxr, cmp_type,
2173 			       (struct rx_tpa_start_cmp *)rxcmp,
2174 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2175 
2176 		*event |= BNXT_RX_EVENT;
2177 		goto next_rx_no_prod_no_len;
2178 
2179 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2180 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2181 				   (struct rx_tpa_end_cmp *)rxcmp,
2182 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2183 
2184 		if (IS_ERR(skb))
2185 			return -EBUSY;
2186 
2187 		rc = -ENOMEM;
2188 		if (likely(skb)) {
2189 			bnxt_deliver_skb(bp, bnapi, skb);
2190 			rc = 1;
2191 		}
2192 		*event |= BNXT_RX_EVENT;
2193 		goto next_rx_no_prod_no_len;
2194 	}
2195 
2196 	cons = rxcmp->rx_cmp_opaque;
2197 	if (unlikely(cons != rxr->rx_next_cons)) {
2198 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2199 
2200 		/* 0xffff is forced error, don't print it */
2201 		if (rxr->rx_next_cons != 0xffff)
2202 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2203 				    cons, rxr->rx_next_cons);
2204 		bnxt_sched_reset_rxr(bp, rxr);
2205 		if (rc1)
2206 			return rc1;
2207 		goto next_rx_no_prod_no_len;
2208 	}
2209 	rx_buf = &rxr->rx_buf_ring[cons];
2210 	data = rx_buf->data;
2211 	data_ptr = rx_buf->data_ptr;
2212 	prefetch(data_ptr);
2213 
2214 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2215 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2216 
2217 	if (agg_bufs) {
2218 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2219 			return -EBUSY;
2220 
2221 		cp_cons = NEXT_CMP(cp_cons);
2222 		*event |= BNXT_AGG_EVENT;
2223 	}
2224 	*event |= BNXT_RX_EVENT;
2225 
2226 	rx_buf->data = NULL;
2227 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2228 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2229 
2230 		bnxt_reuse_rx_data(rxr, cons, data);
2231 		if (agg_bufs)
2232 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2233 					       false);
2234 
2235 		rc = -EIO;
2236 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2237 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2238 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2239 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2240 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2241 						 rx_err);
2242 				bnxt_sched_reset_rxr(bp, rxr);
2243 			}
2244 		}
2245 		goto next_rx_no_len;
2246 	}
2247 
2248 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2249 	len = flags >> RX_CMP_LEN_SHIFT;
2250 	dma_addr = rx_buf->mapping;
2251 
2252 	if (bnxt_xdp_attached(bp, rxr)) {
2253 		bnxt_xdp.rxcmp = rxcmp;
2254 		bnxt_xdp.rxcmp1 = rxcmp1;
2255 		bnxt_xdp.cmp_type = cmp_type;
2256 
2257 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &bnxt_xdp.xdp);
2258 		if (agg_bufs) {
2259 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr,
2260 							       &bnxt_xdp.xdp,
2261 							       cp_cons,
2262 							       agg_bufs,
2263 							       false);
2264 			if (!frag_len)
2265 				goto oom_next_rx;
2266 
2267 		}
2268 		xdp_active = true;
2269 	}
2270 
2271 	if (xdp_active) {
2272 		if (bnxt_rx_xdp(bp, rxr, cons, &bnxt_xdp.xdp, data, &data_ptr,
2273 				&len, event)) {
2274 			rc = 1;
2275 			goto next_rx;
2276 		}
2277 		if (xdp_buff_has_frags(&bnxt_xdp.xdp)) {
2278 			sinfo = xdp_get_shared_info_from_buff(&bnxt_xdp.xdp);
2279 			agg_bufs = sinfo->nr_frags;
2280 		} else {
2281 			agg_bufs = 0;
2282 		}
2283 	}
2284 
2285 	if (len <= bp->rx_copybreak) {
2286 		if (!xdp_active)
2287 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2288 		else
2289 			skb = bnxt_copy_xdp(bnapi, &bnxt_xdp.xdp, len,
2290 					    dma_addr);
2291 		bnxt_reuse_rx_data(rxr, cons, data);
2292 		if (!skb) {
2293 			if (agg_bufs) {
2294 				if (!xdp_active)
2295 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2296 							       agg_bufs, false);
2297 				else
2298 					bnxt_xdp_buff_frags_free(rxr,
2299 								 &bnxt_xdp.xdp);
2300 			}
2301 			goto oom_next_rx;
2302 		}
2303 	} else {
2304 		u32 payload;
2305 
2306 		if (rx_buf->data_ptr == data_ptr)
2307 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2308 		else
2309 			payload = 0;
2310 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2311 				      payload | len);
2312 		if (!skb)
2313 			goto oom_next_rx;
2314 	}
2315 
2316 	if (agg_bufs) {
2317 		if (!xdp_active) {
2318 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2319 						      agg_bufs, false);
2320 			if (!skb)
2321 				goto oom_next_rx;
2322 		} else {
2323 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2324 						 rxr, &bnxt_xdp.xdp);
2325 			if (!skb) {
2326 				/* we should be able to free the old skb here */
2327 				bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2328 				goto oom_next_rx;
2329 			}
2330 		}
2331 	}
2332 
2333 	if (RX_CMP_HASH_VALID(rxcmp)) {
2334 		enum pkt_hash_types type;
2335 
2336 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2337 			type = bnxt_rss_ext_op(bp, rxcmp);
2338 		} else {
2339 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2340 
2341 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2342 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2343 				type = PKT_HASH_TYPE_L4;
2344 			else
2345 				type = PKT_HASH_TYPE_L3;
2346 		}
2347 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2348 	}
2349 
2350 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2351 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2352 	skb->protocol = eth_type_trans(skb, dev);
2353 
2354 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2355 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2356 		if (!skb)
2357 			goto next_rx;
2358 	}
2359 
2360 	skb_checksum_none_assert(skb);
2361 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2362 		if (dev->features & NETIF_F_RXCSUM) {
2363 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2364 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2365 		}
2366 	} else {
2367 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2368 			if (dev->features & NETIF_F_RXCSUM)
2369 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2370 		}
2371 	}
2372 
2373 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2374 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2375 			u64 ns, ts;
2376 
2377 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2378 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2379 
2380 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2381 				memset(skb_hwtstamps(skb), 0,
2382 				       sizeof(*skb_hwtstamps(skb)));
2383 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2384 			}
2385 		}
2386 	}
2387 	bnxt_deliver_skb(bp, bnapi, skb);
2388 	rc = 1;
2389 
2390 next_rx:
2391 	cpr->rx_packets += 1;
2392 	cpr->rx_bytes += len;
2393 
2394 next_rx_no_len:
2395 	rxr->rx_prod = NEXT_RX(prod);
2396 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2397 
2398 next_rx_no_prod_no_len:
2399 	*raw_cons = tmp_raw_cons;
2400 
2401 	return rc;
2402 
2403 oom_next_rx:
2404 	cpr->sw_stats->rx.rx_oom_discards += 1;
2405 	rc = -ENOMEM;
2406 	goto next_rx;
2407 }
2408 
2409 /* In netpoll mode, if we are using a combined completion ring, we need to
2410  * discard the rx packets and recycle the buffers.
2411  */
2412 static int bnxt_force_rx_discard(struct bnxt *bp,
2413 				 struct bnxt_cp_ring_info *cpr,
2414 				 u32 *raw_cons, u8 *event)
2415 {
2416 	u32 tmp_raw_cons = *raw_cons;
2417 	struct rx_cmp_ext *rxcmp1;
2418 	struct rx_cmp *rxcmp;
2419 	u16 cp_cons;
2420 	u8 cmp_type;
2421 	int rc;
2422 
2423 	cp_cons = RING_CMP(tmp_raw_cons);
2424 	rxcmp = (struct rx_cmp *)
2425 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2426 
2427 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2428 	cp_cons = RING_CMP(tmp_raw_cons);
2429 	rxcmp1 = (struct rx_cmp_ext *)
2430 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2431 
2432 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2433 		return -EBUSY;
2434 
2435 	/* The valid test of the entry must be done first before
2436 	 * reading any further.
2437 	 */
2438 	dma_rmb();
2439 	cmp_type = RX_CMP_TYPE(rxcmp);
2440 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2441 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2442 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2443 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2444 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2445 		struct rx_tpa_end_cmp_ext *tpa_end1;
2446 
2447 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2448 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2449 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2450 	}
2451 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2452 	if (rc && rc != -EBUSY)
2453 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2454 	return rc;
2455 }
2456 
2457 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2458 {
2459 	struct bnxt_fw_health *fw_health = bp->fw_health;
2460 	u32 reg = fw_health->regs[reg_idx];
2461 	u32 reg_type, reg_off, val = 0;
2462 
2463 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2464 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2465 	switch (reg_type) {
2466 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2467 		pci_read_config_dword(bp->pdev, reg_off, &val);
2468 		break;
2469 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2470 		reg_off = fw_health->mapped_regs[reg_idx];
2471 		fallthrough;
2472 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2473 		val = readl(bp->bar0 + reg_off);
2474 		break;
2475 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2476 		val = readl(bp->bar1 + reg_off);
2477 		break;
2478 	}
2479 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2480 		val &= fw_health->fw_reset_inprog_reg_mask;
2481 	return val;
2482 }
2483 
2484 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2485 {
2486 	int i;
2487 
2488 	for (i = 0; i < bp->rx_nr_rings; i++) {
2489 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2490 		struct bnxt_ring_grp_info *grp_info;
2491 
2492 		grp_info = &bp->grp_info[grp_idx];
2493 		if (grp_info->agg_fw_ring_id == ring_id)
2494 			return grp_idx;
2495 	}
2496 	return INVALID_HW_RING_ID;
2497 }
2498 
2499 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2500 {
2501 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2502 
2503 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2504 		return link_info->force_link_speed2;
2505 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2506 		return link_info->force_pam4_link_speed;
2507 	return link_info->force_link_speed;
2508 }
2509 
2510 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2511 {
2512 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2513 
2514 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2515 		link_info->req_link_speed = link_info->force_link_speed2;
2516 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2517 		switch (link_info->req_link_speed) {
2518 		case BNXT_LINK_SPEED_50GB_PAM4:
2519 		case BNXT_LINK_SPEED_100GB_PAM4:
2520 		case BNXT_LINK_SPEED_200GB_PAM4:
2521 		case BNXT_LINK_SPEED_400GB_PAM4:
2522 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2523 			break;
2524 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2525 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2526 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2527 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2528 			break;
2529 		default:
2530 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2531 		}
2532 		return;
2533 	}
2534 	link_info->req_link_speed = link_info->force_link_speed;
2535 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2536 	if (link_info->force_pam4_link_speed) {
2537 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2538 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2539 	}
2540 }
2541 
2542 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2543 {
2544 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2545 
2546 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2547 		link_info->advertising = link_info->auto_link_speeds2;
2548 		return;
2549 	}
2550 	link_info->advertising = link_info->auto_link_speeds;
2551 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2552 }
2553 
2554 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2555 {
2556 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2557 
2558 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2559 		if (link_info->req_link_speed != link_info->force_link_speed2)
2560 			return true;
2561 		return false;
2562 	}
2563 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2564 	    link_info->req_link_speed != link_info->force_link_speed)
2565 		return true;
2566 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2567 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2568 		return true;
2569 	return false;
2570 }
2571 
2572 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2573 {
2574 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2575 
2576 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2577 		if (link_info->advertising != link_info->auto_link_speeds2)
2578 			return true;
2579 		return false;
2580 	}
2581 	if (link_info->advertising != link_info->auto_link_speeds ||
2582 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2583 		return true;
2584 	return false;
2585 }
2586 
2587 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2588 {
2589 	u32 flags = bp->ctx->ctx_arr[type].flags;
2590 
2591 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2592 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2593 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2594 }
2595 
2596 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2597 {
2598 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2599 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2600 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2601 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2602 	struct bnxt_bs_trace_info *bs_trace;
2603 	int last_pg;
2604 
2605 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2606 		return;
2607 
2608 	mem_size = ctxm->max_entries * ctxm->entry_size;
2609 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2610 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2611 
2612 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2613 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2614 
2615 	rmem = &ctx_pg[0].ring_mem;
2616 	bs_trace = &bp->bs_trace[trace_type];
2617 	bs_trace->ctx_type = ctxm->type;
2618 	bs_trace->trace_type = trace_type;
2619 	if (pages > MAX_CTX_PAGES) {
2620 		int last_pg_dir = rmem->nr_pages - 1;
2621 
2622 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2623 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2624 	} else {
2625 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2626 	}
2627 	bs_trace->magic_byte += magic_byte_offset;
2628 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2629 }
2630 
2631 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2632 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2633 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2634 
2635 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2636 	(((data2) &							\
2637 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2638 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2639 
2640 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2641 	((data2) &							\
2642 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2643 
2644 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2645 	(((data2) &							\
2646 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2647 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2648 
2649 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2650 	((data1) &							\
2651 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2652 
2653 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2654 	(((data1) &							\
2655 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2656 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2657 
2658 /* Return true if the workqueue has to be scheduled */
2659 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2660 {
2661 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2662 
2663 	switch (err_type) {
2664 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2665 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2666 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2667 		break;
2668 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2669 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2670 		break;
2671 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2672 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2673 		break;
2674 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2675 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2676 		char *threshold_type;
2677 		bool notify = false;
2678 		char *dir_str;
2679 
2680 		switch (type) {
2681 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2682 			threshold_type = "warning";
2683 			break;
2684 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2685 			threshold_type = "critical";
2686 			break;
2687 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2688 			threshold_type = "fatal";
2689 			break;
2690 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2691 			threshold_type = "shutdown";
2692 			break;
2693 		default:
2694 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2695 			return false;
2696 		}
2697 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2698 			dir_str = "above";
2699 			notify = true;
2700 		} else {
2701 			dir_str = "below";
2702 		}
2703 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2704 			    dir_str, threshold_type);
2705 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2706 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2707 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2708 		if (notify) {
2709 			bp->thermal_threshold_type = type;
2710 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2711 			return true;
2712 		}
2713 		return false;
2714 	}
2715 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2716 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2717 		break;
2718 	default:
2719 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2720 			   err_type);
2721 		break;
2722 	}
2723 	return false;
2724 }
2725 
2726 #define BNXT_GET_EVENT_PORT(data)	\
2727 	((data) &			\
2728 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2729 
2730 #define BNXT_EVENT_RING_TYPE(data2)	\
2731 	((data2) &			\
2732 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2733 
2734 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2735 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2736 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2737 
2738 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2739 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2740 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2741 
2742 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2743 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2744 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2745 
2746 #define BNXT_PHC_BITS	48
2747 
2748 static int bnxt_async_event_process(struct bnxt *bp,
2749 				    struct hwrm_async_event_cmpl *cmpl)
2750 {
2751 	u16 event_id = le16_to_cpu(cmpl->event_id);
2752 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2753 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2754 
2755 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2756 		   event_id, data1, data2);
2757 
2758 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2759 	switch (event_id) {
2760 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2761 		struct bnxt_link_info *link_info = &bp->link_info;
2762 
2763 		if (BNXT_VF(bp))
2764 			goto async_event_process_exit;
2765 
2766 		/* print unsupported speed warning in forced speed mode only */
2767 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2768 		    (data1 & 0x20000)) {
2769 			u16 fw_speed = bnxt_get_force_speed(link_info);
2770 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2771 
2772 			if (speed != SPEED_UNKNOWN)
2773 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2774 					    speed);
2775 		}
2776 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2777 	}
2778 		fallthrough;
2779 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2780 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2781 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2782 		fallthrough;
2783 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2784 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2785 		break;
2786 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2787 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2788 		break;
2789 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2790 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2791 
2792 		if (BNXT_VF(bp))
2793 			break;
2794 
2795 		if (bp->pf.port_id != port_id)
2796 			break;
2797 
2798 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2799 		break;
2800 	}
2801 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2802 		if (BNXT_PF(bp))
2803 			goto async_event_process_exit;
2804 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2805 		break;
2806 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2807 		char *type_str = "Solicited";
2808 
2809 		if (!bp->fw_health)
2810 			goto async_event_process_exit;
2811 
2812 		bp->fw_reset_timestamp = jiffies;
2813 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2814 		if (!bp->fw_reset_min_dsecs)
2815 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2816 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2817 		if (!bp->fw_reset_max_dsecs)
2818 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2819 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2820 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2821 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2822 			type_str = "Fatal";
2823 			bp->fw_health->fatalities++;
2824 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2825 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2826 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2827 			type_str = "Non-fatal";
2828 			bp->fw_health->survivals++;
2829 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2830 		}
2831 		netif_warn(bp, hw, bp->dev,
2832 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2833 			   type_str, data1, data2,
2834 			   bp->fw_reset_min_dsecs * 100,
2835 			   bp->fw_reset_max_dsecs * 100);
2836 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2837 		break;
2838 	}
2839 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2840 		struct bnxt_fw_health *fw_health = bp->fw_health;
2841 		char *status_desc = "healthy";
2842 		u32 status;
2843 
2844 		if (!fw_health)
2845 			goto async_event_process_exit;
2846 
2847 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2848 			fw_health->enabled = false;
2849 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2850 			break;
2851 		}
2852 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2853 		fw_health->tmr_multiplier =
2854 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2855 				     bp->current_interval * 10);
2856 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2857 		if (!fw_health->enabled)
2858 			fw_health->last_fw_heartbeat =
2859 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2860 		fw_health->last_fw_reset_cnt =
2861 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2862 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2863 		if (status != BNXT_FW_STATUS_HEALTHY)
2864 			status_desc = "unhealthy";
2865 		netif_info(bp, drv, bp->dev,
2866 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2867 			   fw_health->primary ? "primary" : "backup", status,
2868 			   status_desc, fw_health->last_fw_reset_cnt);
2869 		if (!fw_health->enabled) {
2870 			/* Make sure tmr_counter is set and visible to
2871 			 * bnxt_health_check() before setting enabled to true.
2872 			 */
2873 			smp_wmb();
2874 			fw_health->enabled = true;
2875 		}
2876 		goto async_event_process_exit;
2877 	}
2878 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2879 		netif_notice(bp, hw, bp->dev,
2880 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2881 			     data1, data2);
2882 		goto async_event_process_exit;
2883 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2884 		struct bnxt_rx_ring_info *rxr;
2885 		u16 grp_idx;
2886 
2887 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2888 			goto async_event_process_exit;
2889 
2890 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2891 			    BNXT_EVENT_RING_TYPE(data2), data1);
2892 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2893 			goto async_event_process_exit;
2894 
2895 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2896 		if (grp_idx == INVALID_HW_RING_ID) {
2897 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2898 				    data1);
2899 			goto async_event_process_exit;
2900 		}
2901 		rxr = bp->bnapi[grp_idx]->rx_ring;
2902 		bnxt_sched_reset_rxr(bp, rxr);
2903 		goto async_event_process_exit;
2904 	}
2905 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2906 		struct bnxt_fw_health *fw_health = bp->fw_health;
2907 
2908 		netif_notice(bp, hw, bp->dev,
2909 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2910 			     data1, data2);
2911 		if (fw_health) {
2912 			fw_health->echo_req_data1 = data1;
2913 			fw_health->echo_req_data2 = data2;
2914 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2915 			break;
2916 		}
2917 		goto async_event_process_exit;
2918 	}
2919 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2920 		bnxt_ptp_pps_event(bp, data1, data2);
2921 		goto async_event_process_exit;
2922 	}
2923 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2924 		if (bnxt_event_error_report(bp, data1, data2))
2925 			break;
2926 		goto async_event_process_exit;
2927 	}
2928 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2929 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2930 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2931 			if (BNXT_PTP_USE_RTC(bp)) {
2932 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2933 				unsigned long flags;
2934 				u64 ns;
2935 
2936 				if (!ptp)
2937 					goto async_event_process_exit;
2938 
2939 				bnxt_ptp_update_current_time(bp);
2940 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2941 				       BNXT_PHC_BITS) | ptp->current_time);
2942 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2943 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2944 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2945 			}
2946 			break;
2947 		}
2948 		goto async_event_process_exit;
2949 	}
2950 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2951 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2952 
2953 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2954 		goto async_event_process_exit;
2955 	}
2956 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2957 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2958 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2959 
2960 		if (type >= ARRAY_SIZE(bp->bs_trace))
2961 			goto async_event_process_exit;
2962 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2963 		goto async_event_process_exit;
2964 	}
2965 	default:
2966 		goto async_event_process_exit;
2967 	}
2968 	__bnxt_queue_sp_work(bp);
2969 async_event_process_exit:
2970 	bnxt_ulp_async_events(bp, cmpl);
2971 	return 0;
2972 }
2973 
2974 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2975 {
2976 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2977 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2978 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2979 				(struct hwrm_fwd_req_cmpl *)txcmp;
2980 
2981 	switch (cmpl_type) {
2982 	case CMPL_BASE_TYPE_HWRM_DONE:
2983 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2984 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2985 		break;
2986 
2987 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2988 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2989 
2990 		if ((vf_id < bp->pf.first_vf_id) ||
2991 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2992 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2993 				   vf_id);
2994 			return -EINVAL;
2995 		}
2996 
2997 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2998 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2999 		break;
3000 
3001 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
3002 		bnxt_async_event_process(bp,
3003 					 (struct hwrm_async_event_cmpl *)txcmp);
3004 		break;
3005 
3006 	default:
3007 		break;
3008 	}
3009 
3010 	return 0;
3011 }
3012 
3013 static bool bnxt_vnic_is_active(struct bnxt *bp)
3014 {
3015 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
3016 
3017 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
3018 }
3019 
3020 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
3021 {
3022 	struct bnxt_napi *bnapi = dev_instance;
3023 	struct bnxt *bp = bnapi->bp;
3024 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3025 	u32 cons = RING_CMP(cpr->cp_raw_cons);
3026 
3027 	cpr->event_ctr++;
3028 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
3029 	napi_schedule(&bnapi->napi);
3030 	return IRQ_HANDLED;
3031 }
3032 
3033 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
3034 {
3035 	u32 raw_cons = cpr->cp_raw_cons;
3036 	u16 cons = RING_CMP(raw_cons);
3037 	struct tx_cmp *txcmp;
3038 
3039 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3040 
3041 	return TX_CMP_VALID(txcmp, raw_cons);
3042 }
3043 
3044 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3045 			    int budget)
3046 {
3047 	struct bnxt_napi *bnapi = cpr->bnapi;
3048 	u32 raw_cons = cpr->cp_raw_cons;
3049 	bool flush_xdp = false;
3050 	u32 cons;
3051 	int rx_pkts = 0;
3052 	u8 event = 0;
3053 	struct tx_cmp *txcmp;
3054 
3055 	cpr->has_more_work = 0;
3056 	cpr->had_work_done = 1;
3057 	while (1) {
3058 		u8 cmp_type;
3059 		int rc;
3060 
3061 		cons = RING_CMP(raw_cons);
3062 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3063 
3064 		if (!TX_CMP_VALID(txcmp, raw_cons))
3065 			break;
3066 
3067 		/* The valid test of the entry must be done first before
3068 		 * reading any further.
3069 		 */
3070 		dma_rmb();
3071 		cmp_type = TX_CMP_TYPE(txcmp);
3072 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3073 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3074 			u32 opaque = txcmp->tx_cmp_opaque;
3075 			struct bnxt_tx_ring_info *txr;
3076 			u16 tx_freed;
3077 
3078 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3079 			event |= BNXT_TX_CMP_EVENT;
3080 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3081 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3082 			else
3083 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3084 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3085 				   bp->tx_ring_mask;
3086 			/* return full budget so NAPI will complete. */
3087 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3088 				rx_pkts = budget;
3089 				raw_cons = NEXT_RAW_CMP(raw_cons);
3090 				if (budget)
3091 					cpr->has_more_work = 1;
3092 				break;
3093 			}
3094 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3095 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3096 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3097 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3098 			if (likely(budget))
3099 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3100 			else
3101 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3102 							   &event);
3103 			if (event & BNXT_REDIRECT_EVENT)
3104 				flush_xdp = true;
3105 			if (likely(rc >= 0))
3106 				rx_pkts += rc;
3107 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3108 			 * the NAPI budget.  Otherwise, we may potentially loop
3109 			 * here forever if we consistently cannot allocate
3110 			 * buffers.
3111 			 */
3112 			else if (rc == -ENOMEM && budget)
3113 				rx_pkts++;
3114 			else if (rc == -EBUSY)	/* partial completion */
3115 				break;
3116 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3117 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3118 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3119 			bnxt_hwrm_handler(bp, txcmp);
3120 		}
3121 		raw_cons = NEXT_RAW_CMP(raw_cons);
3122 
3123 		if (rx_pkts && rx_pkts == budget) {
3124 			cpr->has_more_work = 1;
3125 			break;
3126 		}
3127 	}
3128 
3129 	if (flush_xdp) {
3130 		xdp_do_flush();
3131 		event &= ~BNXT_REDIRECT_EVENT;
3132 	}
3133 
3134 	if (event & BNXT_TX_EVENT) {
3135 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3136 		u16 prod = txr->tx_prod;
3137 
3138 		/* Sync BD data before updating doorbell */
3139 		wmb();
3140 
3141 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3142 		event &= ~BNXT_TX_EVENT;
3143 	}
3144 
3145 	cpr->cp_raw_cons = raw_cons;
3146 	bnapi->events |= event;
3147 	return rx_pkts;
3148 }
3149 
3150 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3151 				  int budget)
3152 {
3153 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3154 		bnapi->tx_int(bp, bnapi, budget);
3155 
3156 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3157 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3158 
3159 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3160 		bnapi->events &= ~BNXT_RX_EVENT;
3161 	}
3162 	if (bnapi->events & BNXT_AGG_EVENT) {
3163 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3164 
3165 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3166 		bnapi->events &= ~BNXT_AGG_EVENT;
3167 	}
3168 }
3169 
3170 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3171 			  int budget)
3172 {
3173 	struct bnxt_napi *bnapi = cpr->bnapi;
3174 	int rx_pkts;
3175 
3176 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3177 
3178 	/* ACK completion ring before freeing tx ring and producing new
3179 	 * buffers in rx/agg rings to prevent overflowing the completion
3180 	 * ring.
3181 	 */
3182 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3183 
3184 	__bnxt_poll_work_done(bp, bnapi, budget);
3185 	return rx_pkts;
3186 }
3187 
3188 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3189 {
3190 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3191 	struct bnxt *bp = bnapi->bp;
3192 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3193 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3194 	struct tx_cmp *txcmp;
3195 	struct rx_cmp_ext *rxcmp1;
3196 	u32 cp_cons, tmp_raw_cons;
3197 	u32 raw_cons = cpr->cp_raw_cons;
3198 	bool flush_xdp = false;
3199 	u32 rx_pkts = 0;
3200 	u8 event = 0;
3201 
3202 	while (1) {
3203 		int rc;
3204 
3205 		cp_cons = RING_CMP(raw_cons);
3206 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3207 
3208 		if (!TX_CMP_VALID(txcmp, raw_cons))
3209 			break;
3210 
3211 		/* The valid test of the entry must be done first before
3212 		 * reading any further.
3213 		 */
3214 		dma_rmb();
3215 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3216 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3217 			cp_cons = RING_CMP(tmp_raw_cons);
3218 			rxcmp1 = (struct rx_cmp_ext *)
3219 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3220 
3221 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3222 				break;
3223 
3224 			/* force an error to recycle the buffer */
3225 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3226 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3227 
3228 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3229 			if (likely(rc == -EIO) && budget)
3230 				rx_pkts++;
3231 			else if (rc == -EBUSY)	/* partial completion */
3232 				break;
3233 			if (event & BNXT_REDIRECT_EVENT)
3234 				flush_xdp = true;
3235 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3236 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3237 			bnxt_hwrm_handler(bp, txcmp);
3238 		} else {
3239 			netdev_err(bp->dev,
3240 				   "Invalid completion received on special ring\n");
3241 		}
3242 		raw_cons = NEXT_RAW_CMP(raw_cons);
3243 
3244 		if (rx_pkts == budget)
3245 			break;
3246 	}
3247 
3248 	cpr->cp_raw_cons = raw_cons;
3249 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3250 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3251 
3252 	if (event & BNXT_AGG_EVENT)
3253 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3254 	if (flush_xdp)
3255 		xdp_do_flush();
3256 
3257 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3258 		napi_complete_done(napi, rx_pkts);
3259 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3260 	}
3261 	return rx_pkts;
3262 }
3263 
3264 static int bnxt_poll(struct napi_struct *napi, int budget)
3265 {
3266 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3267 	struct bnxt *bp = bnapi->bp;
3268 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3269 	int work_done = 0;
3270 
3271 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3272 		napi_complete(napi);
3273 		return 0;
3274 	}
3275 	while (1) {
3276 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3277 
3278 		if (work_done >= budget) {
3279 			if (!budget)
3280 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3281 			break;
3282 		}
3283 
3284 		if (!bnxt_has_work(bp, cpr)) {
3285 			if (napi_complete_done(napi, work_done))
3286 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3287 			break;
3288 		}
3289 	}
3290 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3291 		struct dim_sample dim_sample = {};
3292 
3293 		dim_update_sample(cpr->event_ctr,
3294 				  cpr->rx_packets,
3295 				  cpr->rx_bytes,
3296 				  &dim_sample);
3297 		net_dim(&cpr->dim, &dim_sample);
3298 	}
3299 	return work_done;
3300 }
3301 
3302 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3303 {
3304 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3305 	int i, work_done = 0;
3306 
3307 	for (i = 0; i < cpr->cp_ring_count; i++) {
3308 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3309 
3310 		if (cpr2->had_nqe_notify) {
3311 			work_done += __bnxt_poll_work(bp, cpr2,
3312 						      budget - work_done);
3313 			cpr->has_more_work |= cpr2->has_more_work;
3314 		}
3315 	}
3316 	return work_done;
3317 }
3318 
3319 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3320 				 u64 dbr_type, int budget)
3321 {
3322 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3323 	int i;
3324 
3325 	for (i = 0; i < cpr->cp_ring_count; i++) {
3326 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3327 		struct bnxt_db_info *db;
3328 
3329 		if (cpr2->had_work_done) {
3330 			u32 tgl = 0;
3331 
3332 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3333 				cpr2->had_nqe_notify = 0;
3334 				tgl = cpr2->toggle;
3335 			}
3336 			db = &cpr2->cp_db;
3337 			bnxt_writeq(bp,
3338 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3339 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3340 				    db->doorbell);
3341 			cpr2->had_work_done = 0;
3342 		}
3343 	}
3344 	__bnxt_poll_work_done(bp, bnapi, budget);
3345 }
3346 
3347 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3348 {
3349 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3350 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3351 	struct bnxt_cp_ring_info *cpr_rx;
3352 	u32 raw_cons = cpr->cp_raw_cons;
3353 	struct bnxt *bp = bnapi->bp;
3354 	struct nqe_cn *nqcmp;
3355 	int work_done = 0;
3356 	u32 cons;
3357 
3358 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3359 		napi_complete(napi);
3360 		return 0;
3361 	}
3362 	if (cpr->has_more_work) {
3363 		cpr->has_more_work = 0;
3364 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3365 	}
3366 	while (1) {
3367 		u16 type;
3368 
3369 		cons = RING_CMP(raw_cons);
3370 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3371 
3372 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3373 			if (cpr->has_more_work)
3374 				break;
3375 
3376 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3377 					     budget);
3378 			cpr->cp_raw_cons = raw_cons;
3379 			if (napi_complete_done(napi, work_done))
3380 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3381 						  cpr->cp_raw_cons);
3382 			goto poll_done;
3383 		}
3384 
3385 		/* The valid test of the entry must be done first before
3386 		 * reading any further.
3387 		 */
3388 		dma_rmb();
3389 
3390 		type = le16_to_cpu(nqcmp->type);
3391 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3392 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3393 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3394 			struct bnxt_cp_ring_info *cpr2;
3395 
3396 			/* No more budget for RX work */
3397 			if (budget && work_done >= budget &&
3398 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3399 				break;
3400 
3401 			idx = BNXT_NQ_HDL_IDX(idx);
3402 			cpr2 = &cpr->cp_ring_arr[idx];
3403 			cpr2->had_nqe_notify = 1;
3404 			cpr2->toggle = NQE_CN_TOGGLE(type);
3405 			work_done += __bnxt_poll_work(bp, cpr2,
3406 						      budget - work_done);
3407 			cpr->has_more_work |= cpr2->has_more_work;
3408 		} else {
3409 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3410 		}
3411 		raw_cons = NEXT_RAW_CMP(raw_cons);
3412 	}
3413 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3414 	if (raw_cons != cpr->cp_raw_cons) {
3415 		cpr->cp_raw_cons = raw_cons;
3416 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3417 	}
3418 poll_done:
3419 	cpr_rx = &cpr->cp_ring_arr[0];
3420 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3421 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3422 		struct dim_sample dim_sample = {};
3423 
3424 		dim_update_sample(cpr->event_ctr,
3425 				  cpr_rx->rx_packets,
3426 				  cpr_rx->rx_bytes,
3427 				  &dim_sample);
3428 		net_dim(&cpr->dim, &dim_sample);
3429 	}
3430 	return work_done;
3431 }
3432 
3433 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3434 				       struct bnxt_tx_ring_info *txr, int idx)
3435 {
3436 	int i, max_idx;
3437 	struct pci_dev *pdev = bp->pdev;
3438 	unsigned int dma_len;
3439 	dma_addr_t dma_addr;
3440 
3441 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3442 
3443 	for (i = 0; i < max_idx;) {
3444 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3445 		struct bnxt_sw_tx_bd *head_buf = tx_buf;
3446 		struct sk_buff *skb;
3447 		int j, last;
3448 
3449 		if (idx  < bp->tx_nr_rings_xdp &&
3450 		    tx_buf->action == XDP_REDIRECT) {
3451 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3452 			dma_len = dma_unmap_len(tx_buf, len);
3453 
3454 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3455 					 DMA_TO_DEVICE);
3456 			xdp_return_frame(tx_buf->xdpf);
3457 			tx_buf->action = 0;
3458 			tx_buf->xdpf = NULL;
3459 			i++;
3460 			continue;
3461 		}
3462 
3463 		skb = tx_buf->skb;
3464 		if (!skb) {
3465 			i++;
3466 			continue;
3467 		}
3468 
3469 		tx_buf->skb = NULL;
3470 
3471 		if (tx_buf->is_push) {
3472 			dev_kfree_skb(skb);
3473 			i += 2;
3474 			continue;
3475 		}
3476 
3477 		if (dma_unmap_len(tx_buf, len)) {
3478 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3479 			dma_len = dma_unmap_len(tx_buf, len);
3480 
3481 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3482 					 DMA_TO_DEVICE);
3483 		}
3484 
3485 		last = tx_buf->nr_frags;
3486 		i += 2;
3487 		for (j = 0; j < last; j++, i++) {
3488 			int ring_idx = i & bp->tx_ring_mask;
3489 
3490 			tx_buf = &txr->tx_buf_ring[ring_idx];
3491 			if (dma_unmap_len(tx_buf, len)) {
3492 				dma_addr = dma_unmap_addr(tx_buf, mapping);
3493 				dma_len = dma_unmap_len(tx_buf, len);
3494 
3495 				netmem_dma_unmap_page_attrs(&pdev->dev,
3496 							    dma_addr, dma_len,
3497 							    DMA_TO_DEVICE, 0);
3498 			}
3499 		}
3500 		if (head_buf->is_sw_gso) {
3501 			u16 inline_cons = txr->tx_inline_cons + 1;
3502 
3503 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
3504 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
3505 				tso_dma_map_complete(&pdev->dev,
3506 						     &head_buf->sw_gso_cstate);
3507 			} else {
3508 				skb = NULL;
3509 			}
3510 			head_buf->is_sw_gso = 0;
3511 		}
3512 		if (skb)
3513 			dev_kfree_skb(skb);
3514 	}
3515 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3516 }
3517 
3518 static void bnxt_free_tx_skbs(struct bnxt *bp)
3519 {
3520 	int i;
3521 
3522 	if (!bp->tx_ring)
3523 		return;
3524 
3525 	for (i = 0; i < bp->tx_nr_rings; i++) {
3526 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3527 
3528 		if (!txr->tx_buf_ring)
3529 			continue;
3530 
3531 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3532 	}
3533 
3534 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3535 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3536 }
3537 
3538 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3539 {
3540 	int i, max_idx;
3541 
3542 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3543 
3544 	for (i = 0; i < max_idx; i++) {
3545 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3546 		void *data = rx_buf->data;
3547 
3548 		if (!data)
3549 			continue;
3550 
3551 		rx_buf->data = NULL;
3552 		if (BNXT_RX_PAGE_MODE(bp))
3553 			page_pool_recycle_direct(rxr->page_pool, data);
3554 		else
3555 			page_pool_free_va(rxr->head_pool, data, true);
3556 	}
3557 }
3558 
3559 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3560 {
3561 	int i, max_idx;
3562 
3563 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3564 
3565 	for (i = 0; i < max_idx; i++) {
3566 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3567 		netmem_ref netmem = rx_agg_buf->netmem;
3568 
3569 		if (!netmem)
3570 			continue;
3571 
3572 		rx_agg_buf->netmem = 0;
3573 		__clear_bit(i, rxr->rx_agg_bmap);
3574 
3575 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3576 	}
3577 }
3578 
3579 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3580 					struct bnxt_rx_ring_info *rxr)
3581 {
3582 	int i;
3583 
3584 	for (i = 0; i < bp->max_tpa; i++) {
3585 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3586 		u8 *data = tpa_info->data;
3587 
3588 		if (!data)
3589 			continue;
3590 
3591 		tpa_info->data = NULL;
3592 		page_pool_free_va(rxr->head_pool, data, false);
3593 	}
3594 }
3595 
3596 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3597 				       struct bnxt_rx_ring_info *rxr)
3598 {
3599 	struct bnxt_tpa_idx_map *map;
3600 
3601 	if (!rxr->rx_tpa)
3602 		goto skip_rx_tpa_free;
3603 
3604 	bnxt_free_one_tpa_info_data(bp, rxr);
3605 
3606 skip_rx_tpa_free:
3607 	if (!rxr->rx_buf_ring)
3608 		goto skip_rx_buf_free;
3609 
3610 	bnxt_free_one_rx_ring(bp, rxr);
3611 
3612 skip_rx_buf_free:
3613 	if (!rxr->rx_agg_ring)
3614 		goto skip_rx_agg_free;
3615 
3616 	bnxt_free_one_rx_agg_ring(bp, rxr);
3617 
3618 skip_rx_agg_free:
3619 	map = rxr->rx_tpa_idx_map;
3620 	if (map)
3621 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3622 }
3623 
3624 static void bnxt_free_rx_skbs(struct bnxt *bp)
3625 {
3626 	int i;
3627 
3628 	if (!bp->rx_ring)
3629 		return;
3630 
3631 	for (i = 0; i < bp->rx_nr_rings; i++)
3632 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3633 }
3634 
3635 static void bnxt_free_skbs(struct bnxt *bp)
3636 {
3637 	bnxt_free_tx_skbs(bp);
3638 	bnxt_free_rx_skbs(bp);
3639 }
3640 
3641 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3642 {
3643 	u8 init_val = ctxm->init_value;
3644 	u16 offset = ctxm->init_offset;
3645 	u8 *p2 = p;
3646 	int i;
3647 
3648 	if (!init_val)
3649 		return;
3650 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3651 		memset(p, init_val, len);
3652 		return;
3653 	}
3654 	for (i = 0; i < len; i += ctxm->entry_size)
3655 		*(p2 + i + offset) = init_val;
3656 }
3657 
3658 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3659 			       void *buf, size_t offset, size_t head,
3660 			       size_t tail)
3661 {
3662 	int i, head_page, start_idx, source_offset;
3663 	size_t len, rem_len, total_len, max_bytes;
3664 
3665 	head_page = head / rmem->page_size;
3666 	source_offset = head % rmem->page_size;
3667 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3668 	if (!total_len)
3669 		total_len = MAX_CTX_BYTES;
3670 	start_idx = head_page % MAX_CTX_PAGES;
3671 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3672 		    source_offset;
3673 	total_len = min(total_len, max_bytes);
3674 	rem_len = total_len;
3675 
3676 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3677 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3678 		if (buf)
3679 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3680 			       len);
3681 		offset += len;
3682 		rem_len -= len;
3683 	}
3684 	return total_len;
3685 }
3686 
3687 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3688 {
3689 	struct pci_dev *pdev = bp->pdev;
3690 	int i;
3691 
3692 	if (!rmem->pg_arr)
3693 		goto skip_pages;
3694 
3695 	for (i = 0; i < rmem->nr_pages; i++) {
3696 		if (!rmem->pg_arr[i])
3697 			continue;
3698 
3699 		dma_free_coherent(&pdev->dev, rmem->page_size,
3700 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3701 
3702 		rmem->pg_arr[i] = NULL;
3703 	}
3704 skip_pages:
3705 	if (rmem->pg_tbl) {
3706 		size_t pg_tbl_size = rmem->nr_pages * 8;
3707 
3708 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3709 			pg_tbl_size = rmem->page_size;
3710 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3711 				  rmem->pg_tbl, rmem->pg_tbl_map);
3712 		rmem->pg_tbl = NULL;
3713 	}
3714 	if (rmem->vmem_size && *rmem->vmem) {
3715 		vfree(*rmem->vmem);
3716 		*rmem->vmem = NULL;
3717 	}
3718 }
3719 
3720 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3721 {
3722 	struct pci_dev *pdev = bp->pdev;
3723 	u64 valid_bit = 0;
3724 	int i;
3725 
3726 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3727 		valid_bit = PTU_PTE_VALID;
3728 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3729 		size_t pg_tbl_size = rmem->nr_pages * 8;
3730 
3731 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3732 			pg_tbl_size = rmem->page_size;
3733 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3734 						  &rmem->pg_tbl_map,
3735 						  GFP_KERNEL);
3736 		if (!rmem->pg_tbl)
3737 			return -ENOMEM;
3738 	}
3739 
3740 	for (i = 0; i < rmem->nr_pages; i++) {
3741 		u64 extra_bits = valid_bit;
3742 
3743 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3744 						     rmem->page_size,
3745 						     &rmem->dma_arr[i],
3746 						     GFP_KERNEL);
3747 		if (!rmem->pg_arr[i])
3748 			return -ENOMEM;
3749 
3750 		if (rmem->ctx_mem)
3751 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3752 					  rmem->page_size);
3753 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3754 			if (i == rmem->nr_pages - 2 &&
3755 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3756 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3757 			else if (i == rmem->nr_pages - 1 &&
3758 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3759 				extra_bits |= PTU_PTE_LAST;
3760 			rmem->pg_tbl[i] =
3761 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3762 		}
3763 	}
3764 
3765 	if (rmem->vmem_size) {
3766 		*rmem->vmem = vzalloc(rmem->vmem_size);
3767 		if (!(*rmem->vmem))
3768 			return -ENOMEM;
3769 	}
3770 	return 0;
3771 }
3772 
3773 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3774 				   struct bnxt_rx_ring_info *rxr)
3775 {
3776 	int i;
3777 
3778 	kfree(rxr->rx_tpa_idx_map);
3779 	rxr->rx_tpa_idx_map = NULL;
3780 	if (rxr->rx_tpa) {
3781 		for (i = 0; i < bp->max_tpa; i++) {
3782 			kfree(rxr->rx_tpa[i].agg_arr);
3783 			rxr->rx_tpa[i].agg_arr = NULL;
3784 		}
3785 	}
3786 	kfree(rxr->rx_tpa);
3787 	rxr->rx_tpa = NULL;
3788 }
3789 
3790 static void bnxt_free_tpa_info(struct bnxt *bp)
3791 {
3792 	int i;
3793 
3794 	for (i = 0; i < bp->rx_nr_rings; i++) {
3795 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3796 
3797 		bnxt_free_one_tpa_info(bp, rxr);
3798 	}
3799 }
3800 
3801 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3802 				   struct bnxt_rx_ring_info *rxr)
3803 {
3804 	struct rx_agg_cmp *agg;
3805 	int i;
3806 
3807 	rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info, bp->max_tpa);
3808 	if (!rxr->rx_tpa)
3809 		return -ENOMEM;
3810 
3811 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3812 		return 0;
3813 	for (i = 0; i < bp->max_tpa; i++) {
3814 		agg = kzalloc_objs(*agg, MAX_SKB_FRAGS);
3815 		if (!agg)
3816 			return -ENOMEM;
3817 		rxr->rx_tpa[i].agg_arr = agg;
3818 	}
3819 	rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map);
3820 	if (!rxr->rx_tpa_idx_map)
3821 		return -ENOMEM;
3822 
3823 	return 0;
3824 }
3825 
3826 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3827 {
3828 	int i, rc;
3829 
3830 	bp->max_tpa = MAX_TPA;
3831 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3832 		if (!bp->max_tpa_v2)
3833 			return 0;
3834 		bp->max_tpa = min_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3835 		/* Older P5 FW sets max_tpa_v2 low by mistake except NPAR */
3836 		if (bp->max_tpa <= 32 && BNXT_CHIP_P5(bp) && !BNXT_NPAR(bp))
3837 			bp->max_tpa = MAX_TPA_P5;
3838 	}
3839 
3840 	for (i = 0; i < bp->rx_nr_rings; i++) {
3841 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3842 
3843 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3844 		if (rc)
3845 			return rc;
3846 	}
3847 	return 0;
3848 }
3849 
3850 static void bnxt_free_rx_rings(struct bnxt *bp)
3851 {
3852 	int i;
3853 
3854 	if (!bp->rx_ring)
3855 		return;
3856 
3857 	bnxt_free_tpa_info(bp);
3858 	for (i = 0; i < bp->rx_nr_rings; i++) {
3859 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3860 		struct bnxt_ring_struct *ring;
3861 
3862 		if (rxr->xdp_prog)
3863 			bpf_prog_put(rxr->xdp_prog);
3864 
3865 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3866 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3867 
3868 		page_pool_destroy(rxr->page_pool);
3869 		page_pool_destroy(rxr->head_pool);
3870 		rxr->page_pool = rxr->head_pool = NULL;
3871 
3872 		kfree(rxr->rx_agg_bmap);
3873 		rxr->rx_agg_bmap = NULL;
3874 
3875 		ring = &rxr->rx_ring_struct;
3876 		bnxt_free_ring(bp, &ring->ring_mem);
3877 
3878 		ring = &rxr->rx_agg_ring_struct;
3879 		bnxt_free_ring(bp, &ring->ring_mem);
3880 	}
3881 }
3882 
3883 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3884 				       struct bnxt_rx_ring_info *rxr)
3885 {
3886 	/* User may have chosen larger than default rx_page_size,
3887 	 * we keep the ring sizes uniform and also want uniform amount
3888 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3889 	 */
3890 	int fill_level = bp->rx_agg_ring_size;
3891 
3892 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3893 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3894 
3895 	return fill_level;
3896 }
3897 
3898 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3899 				   struct bnxt_rx_ring_info *rxr,
3900 				   int numa_node)
3901 {
3902 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3903 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3904 	struct page_pool_params pp = { 0 };
3905 	struct page_pool *pool;
3906 
3907 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3908 	if (BNXT_RX_PAGE_MODE(bp))
3909 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3910 
3911 	pp.order = get_order(rxr->rx_page_size);
3912 	pp.nid = numa_node;
3913 	pp.netdev = bp->dev;
3914 	pp.dev = &bp->pdev->dev;
3915 	pp.dma_dir = bp->rx_dir;
3916 	pp.max_len = PAGE_SIZE << pp.order;
3917 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3918 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3919 	pp.queue_idx = rxr->bnapi->index;
3920 
3921 	pool = page_pool_create(&pp);
3922 	if (IS_ERR(pool))
3923 		return PTR_ERR(pool);
3924 	rxr->page_pool = pool;
3925 
3926 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3927 	rxr->need_head_pool |= !!pp.order;
3928 	if (bnxt_separate_head_pool(rxr)) {
3929 		pp.order = 0;
3930 		pp.max_len = PAGE_SIZE;
3931 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3932 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3933 		pool = page_pool_create(&pp);
3934 		if (IS_ERR(pool))
3935 			goto err_destroy_pp;
3936 	} else {
3937 		page_pool_get(pool);
3938 	}
3939 	rxr->head_pool = pool;
3940 
3941 	return 0;
3942 
3943 err_destroy_pp:
3944 	page_pool_destroy(rxr->page_pool);
3945 	rxr->page_pool = NULL;
3946 	return PTR_ERR(pool);
3947 }
3948 
3949 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3950 {
3951 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3952 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3953 }
3954 
3955 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3956 {
3957 	u16 mem_size;
3958 
3959 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3960 	mem_size = rxr->rx_agg_bmap_size / 8;
3961 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3962 	if (!rxr->rx_agg_bmap)
3963 		return -ENOMEM;
3964 
3965 	return 0;
3966 }
3967 
3968 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3969 {
3970 	int numa_node = dev_to_node(&bp->pdev->dev);
3971 	int i, rc = 0, agg_rings = 0, cpu;
3972 
3973 	if (!bp->rx_ring)
3974 		return -ENOMEM;
3975 
3976 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3977 		agg_rings = 1;
3978 
3979 	for (i = 0; i < bp->rx_nr_rings; i++) {
3980 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3981 		struct bnxt_ring_struct *ring;
3982 		int cpu_node;
3983 
3984 		ring = &rxr->rx_ring_struct;
3985 
3986 		cpu = cpumask_local_spread(i, numa_node);
3987 		cpu_node = cpu_to_node(cpu);
3988 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3989 			   i, cpu_node);
3990 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3991 		if (rc)
3992 			return rc;
3993 		bnxt_enable_rx_page_pool(rxr);
3994 
3995 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3996 		if (rc < 0)
3997 			return rc;
3998 
3999 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
4000 						MEM_TYPE_PAGE_POOL,
4001 						rxr->page_pool);
4002 		if (rc) {
4003 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
4004 			return rc;
4005 		}
4006 
4007 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4008 		if (rc)
4009 			return rc;
4010 
4011 		ring->grp_idx = i;
4012 		if (agg_rings) {
4013 			ring = &rxr->rx_agg_ring_struct;
4014 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4015 			if (rc)
4016 				return rc;
4017 
4018 			ring->grp_idx = i;
4019 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
4020 			if (rc)
4021 				return rc;
4022 		}
4023 	}
4024 	if (bp->flags & BNXT_FLAG_TPA)
4025 		rc = bnxt_alloc_tpa_info(bp);
4026 	return rc;
4027 }
4028 
4029 static void bnxt_free_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4030 				    struct pci_dev *pdev)
4031 {
4032 	if (!txr->tx_inline_buf)
4033 		return;
4034 
4035 	dma_unmap_single(&pdev->dev, txr->tx_inline_dma,
4036 			 txr->tx_inline_size, DMA_TO_DEVICE);
4037 	kfree(txr->tx_inline_buf);
4038 	txr->tx_inline_buf = NULL;
4039 	txr->tx_inline_size = 0;
4040 }
4041 
4042 static int bnxt_alloc_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4043 				    struct pci_dev *pdev,
4044 				    unsigned int size)
4045 {
4046 	txr->tx_inline_buf = kmalloc(size, GFP_KERNEL);
4047 	if (!txr->tx_inline_buf)
4048 		return -ENOMEM;
4049 
4050 	txr->tx_inline_dma = dma_map_single(&pdev->dev, txr->tx_inline_buf,
4051 					    size, DMA_TO_DEVICE);
4052 	if (dma_mapping_error(&pdev->dev, txr->tx_inline_dma)) {
4053 		kfree(txr->tx_inline_buf);
4054 		txr->tx_inline_buf = NULL;
4055 		return -ENOMEM;
4056 	}
4057 	txr->tx_inline_size = size;
4058 
4059 	return 0;
4060 }
4061 
4062 static void bnxt_free_tx_rings(struct bnxt *bp)
4063 {
4064 	int i;
4065 	struct pci_dev *pdev = bp->pdev;
4066 
4067 	if (!bp->tx_ring)
4068 		return;
4069 
4070 	for (i = 0; i < bp->tx_nr_rings; i++) {
4071 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4072 		struct bnxt_ring_struct *ring;
4073 
4074 		if (txr->tx_push) {
4075 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
4076 					  txr->tx_push, txr->tx_push_mapping);
4077 			txr->tx_push = NULL;
4078 		}
4079 
4080 		bnxt_free_tx_inline_buf(txr, pdev);
4081 
4082 		ring = &txr->tx_ring_struct;
4083 
4084 		bnxt_free_ring(bp, &ring->ring_mem);
4085 	}
4086 }
4087 
4088 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
4089 	((tc) * (bp)->tx_nr_rings_per_tc)
4090 
4091 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
4092 	((tx) % (bp)->tx_nr_rings_per_tc)
4093 
4094 #define BNXT_RING_TO_TC(bp, tx)		\
4095 	((tx) / (bp)->tx_nr_rings_per_tc)
4096 
4097 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4098 {
4099 	int i, j, rc;
4100 	struct pci_dev *pdev = bp->pdev;
4101 
4102 	bp->tx_push_size = 0;
4103 	if (bp->tx_push_thresh) {
4104 		int push_size;
4105 
4106 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4107 					bp->tx_push_thresh);
4108 
4109 		if (push_size > 256) {
4110 			push_size = 0;
4111 			bp->tx_push_thresh = 0;
4112 		}
4113 
4114 		bp->tx_push_size = push_size;
4115 	}
4116 
4117 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4118 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4119 		struct bnxt_ring_struct *ring;
4120 		u8 qidx;
4121 
4122 		ring = &txr->tx_ring_struct;
4123 
4124 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4125 		if (rc)
4126 			return rc;
4127 
4128 		ring->grp_idx = txr->bnapi->index;
4129 		if (bp->tx_push_size) {
4130 			dma_addr_t mapping;
4131 
4132 			/* One pre-allocated DMA buffer to backup
4133 			 * TX push operation
4134 			 */
4135 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4136 						bp->tx_push_size,
4137 						&txr->tx_push_mapping,
4138 						GFP_KERNEL);
4139 
4140 			if (!txr->tx_push)
4141 				return -ENOMEM;
4142 
4143 			mapping = txr->tx_push_mapping +
4144 				sizeof(struct tx_push_bd);
4145 			txr->data_mapping = cpu_to_le64(mapping);
4146 		}
4147 		if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
4148 			rc = bnxt_alloc_tx_inline_buf(txr, pdev,
4149 						      BNXT_SW_USO_MAX_SEGS *
4150 						      TSO_HEADER_SIZE);
4151 			if (rc)
4152 				return rc;
4153 		}
4154 		qidx = bp->tc_to_qidx[j];
4155 		ring->queue_id = bp->q_info[qidx].queue_id;
4156 		spin_lock_init(&txr->xdp_tx_lock);
4157 		if (i < bp->tx_nr_rings_xdp)
4158 			continue;
4159 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4160 			j++;
4161 	}
4162 	return 0;
4163 }
4164 
4165 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4166 {
4167 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4168 
4169 	kfree(cpr->cp_desc_ring);
4170 	cpr->cp_desc_ring = NULL;
4171 	ring->ring_mem.pg_arr = NULL;
4172 	kfree(cpr->cp_desc_mapping);
4173 	cpr->cp_desc_mapping = NULL;
4174 	ring->ring_mem.dma_arr = NULL;
4175 }
4176 
4177 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4178 {
4179 	cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n);
4180 	if (!cpr->cp_desc_ring)
4181 		return -ENOMEM;
4182 	cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n);
4183 	if (!cpr->cp_desc_mapping)
4184 		return -ENOMEM;
4185 	return 0;
4186 }
4187 
4188 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4189 {
4190 	int i;
4191 
4192 	if (!bp->bnapi)
4193 		return;
4194 	for (i = 0; i < bp->cp_nr_rings; i++) {
4195 		struct bnxt_napi *bnapi = bp->bnapi[i];
4196 
4197 		if (!bnapi)
4198 			continue;
4199 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4200 	}
4201 }
4202 
4203 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4204 {
4205 	int i, n = bp->cp_nr_pages;
4206 
4207 	for (i = 0; i < bp->cp_nr_rings; i++) {
4208 		struct bnxt_napi *bnapi = bp->bnapi[i];
4209 		int rc;
4210 
4211 		if (!bnapi)
4212 			continue;
4213 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4214 		if (rc)
4215 			return rc;
4216 	}
4217 	return 0;
4218 }
4219 
4220 static void bnxt_free_cp_rings(struct bnxt *bp)
4221 {
4222 	int i;
4223 
4224 	if (!bp->bnapi)
4225 		return;
4226 
4227 	for (i = 0; i < bp->cp_nr_rings; i++) {
4228 		struct bnxt_napi *bnapi = bp->bnapi[i];
4229 		struct bnxt_cp_ring_info *cpr;
4230 		struct bnxt_ring_struct *ring;
4231 		int j;
4232 
4233 		if (!bnapi)
4234 			continue;
4235 
4236 		cpr = &bnapi->cp_ring;
4237 		ring = &cpr->cp_ring_struct;
4238 
4239 		bnxt_free_ring(bp, &ring->ring_mem);
4240 
4241 		if (!cpr->cp_ring_arr)
4242 			continue;
4243 
4244 		for (j = 0; j < cpr->cp_ring_count; j++) {
4245 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4246 
4247 			ring = &cpr2->cp_ring_struct;
4248 			bnxt_free_ring(bp, &ring->ring_mem);
4249 			bnxt_free_cp_arrays(cpr2);
4250 		}
4251 		kfree(cpr->cp_ring_arr);
4252 		cpr->cp_ring_arr = NULL;
4253 		cpr->cp_ring_count = 0;
4254 	}
4255 }
4256 
4257 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4258 				  struct bnxt_cp_ring_info *cpr)
4259 {
4260 	struct bnxt_ring_mem_info *rmem;
4261 	struct bnxt_ring_struct *ring;
4262 	int rc;
4263 
4264 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4265 	if (rc) {
4266 		bnxt_free_cp_arrays(cpr);
4267 		return -ENOMEM;
4268 	}
4269 	ring = &cpr->cp_ring_struct;
4270 	rmem = &ring->ring_mem;
4271 	rmem->nr_pages = bp->cp_nr_pages;
4272 	rmem->page_size = HW_CMPD_RING_SIZE;
4273 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4274 	rmem->dma_arr = cpr->cp_desc_mapping;
4275 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4276 	rc = bnxt_alloc_ring(bp, rmem);
4277 	if (rc) {
4278 		bnxt_free_ring(bp, rmem);
4279 		bnxt_free_cp_arrays(cpr);
4280 	}
4281 	return rc;
4282 }
4283 
4284 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4285 {
4286 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4287 	int i, j, rc, ulp_msix;
4288 	int tcs = bp->num_tc;
4289 
4290 	if (!tcs)
4291 		tcs = 1;
4292 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4293 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4294 		struct bnxt_napi *bnapi = bp->bnapi[i];
4295 		struct bnxt_cp_ring_info *cpr, *cpr2;
4296 		struct bnxt_ring_struct *ring;
4297 		int cp_count = 0, k;
4298 		int rx = 0, tx = 0;
4299 
4300 		if (!bnapi)
4301 			continue;
4302 
4303 		cpr = &bnapi->cp_ring;
4304 		cpr->bnapi = bnapi;
4305 		ring = &cpr->cp_ring_struct;
4306 
4307 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4308 		if (rc)
4309 			return rc;
4310 
4311 		ring->map_idx = ulp_msix + i;
4312 
4313 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4314 			continue;
4315 
4316 		if (i < bp->rx_nr_rings) {
4317 			cp_count++;
4318 			rx = 1;
4319 		}
4320 		if (i < bp->tx_nr_rings_xdp) {
4321 			cp_count++;
4322 			tx = 1;
4323 		} else if ((sh && i < bp->tx_nr_rings) ||
4324 			 (!sh && i >= bp->rx_nr_rings)) {
4325 			cp_count += tcs;
4326 			tx = 1;
4327 		}
4328 
4329 		cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count);
4330 		if (!cpr->cp_ring_arr)
4331 			return -ENOMEM;
4332 		cpr->cp_ring_count = cp_count;
4333 
4334 		for (k = 0; k < cp_count; k++) {
4335 			cpr2 = &cpr->cp_ring_arr[k];
4336 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4337 			if (rc)
4338 				return rc;
4339 			cpr2->bnapi = bnapi;
4340 			cpr2->sw_stats = cpr->sw_stats;
4341 			cpr2->cp_idx = k;
4342 			if (!k && rx) {
4343 				bp->rx_ring[i].rx_cpr = cpr2;
4344 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4345 			} else {
4346 				int n, tc = k - rx;
4347 
4348 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4349 				bp->tx_ring[n].tx_cpr = cpr2;
4350 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4351 			}
4352 		}
4353 		if (tx)
4354 			j++;
4355 	}
4356 	return 0;
4357 }
4358 
4359 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4360 				     struct bnxt_rx_ring_info *rxr)
4361 {
4362 	struct bnxt_ring_mem_info *rmem;
4363 	struct bnxt_ring_struct *ring;
4364 
4365 	ring = &rxr->rx_ring_struct;
4366 	rmem = &ring->ring_mem;
4367 	rmem->nr_pages = bp->rx_nr_pages;
4368 	rmem->page_size = HW_RXBD_RING_SIZE;
4369 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4370 	rmem->dma_arr = rxr->rx_desc_mapping;
4371 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4372 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4373 
4374 	ring = &rxr->rx_agg_ring_struct;
4375 	rmem = &ring->ring_mem;
4376 	rmem->nr_pages = bp->rx_agg_nr_pages;
4377 	rmem->page_size = HW_RXBD_RING_SIZE;
4378 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4379 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4380 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4381 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4382 }
4383 
4384 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4385 				      struct bnxt_rx_ring_info *rxr)
4386 {
4387 	struct bnxt_ring_mem_info *rmem;
4388 	struct bnxt_ring_struct *ring;
4389 	int i;
4390 
4391 	rxr->page_pool->p.napi = NULL;
4392 	rxr->page_pool = NULL;
4393 	rxr->head_pool->p.napi = NULL;
4394 	rxr->head_pool = NULL;
4395 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4396 
4397 	ring = &rxr->rx_ring_struct;
4398 	rmem = &ring->ring_mem;
4399 	rmem->pg_tbl = NULL;
4400 	rmem->pg_tbl_map = 0;
4401 	for (i = 0; i < rmem->nr_pages; i++) {
4402 		rmem->pg_arr[i] = NULL;
4403 		rmem->dma_arr[i] = 0;
4404 	}
4405 	*rmem->vmem = NULL;
4406 
4407 	ring = &rxr->rx_agg_ring_struct;
4408 	rmem = &ring->ring_mem;
4409 	rmem->pg_tbl = NULL;
4410 	rmem->pg_tbl_map = 0;
4411 	for (i = 0; i < rmem->nr_pages; i++) {
4412 		rmem->pg_arr[i] = NULL;
4413 		rmem->dma_arr[i] = 0;
4414 	}
4415 	*rmem->vmem = NULL;
4416 }
4417 
4418 static void bnxt_init_ring_struct(struct bnxt *bp)
4419 {
4420 	int i, j;
4421 
4422 	for (i = 0; i < bp->cp_nr_rings; i++) {
4423 		struct bnxt_napi *bnapi = bp->bnapi[i];
4424 		struct netdev_queue_config qcfg;
4425 		struct bnxt_ring_mem_info *rmem;
4426 		struct bnxt_cp_ring_info *cpr;
4427 		struct bnxt_rx_ring_info *rxr;
4428 		struct bnxt_tx_ring_info *txr;
4429 		struct bnxt_ring_struct *ring;
4430 
4431 		if (!bnapi)
4432 			continue;
4433 
4434 		cpr = &bnapi->cp_ring;
4435 		ring = &cpr->cp_ring_struct;
4436 		rmem = &ring->ring_mem;
4437 		rmem->nr_pages = bp->cp_nr_pages;
4438 		rmem->page_size = HW_CMPD_RING_SIZE;
4439 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4440 		rmem->dma_arr = cpr->cp_desc_mapping;
4441 		rmem->vmem_size = 0;
4442 
4443 		rxr = bnapi->rx_ring;
4444 		if (!rxr)
4445 			goto skip_rx;
4446 
4447 		netdev_queue_config(bp->dev, i, &qcfg);
4448 		rxr->rx_page_size = qcfg.rx_page_size;
4449 
4450 		ring = &rxr->rx_ring_struct;
4451 		rmem = &ring->ring_mem;
4452 		rmem->nr_pages = bp->rx_nr_pages;
4453 		rmem->page_size = HW_RXBD_RING_SIZE;
4454 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4455 		rmem->dma_arr = rxr->rx_desc_mapping;
4456 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4457 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4458 
4459 		ring = &rxr->rx_agg_ring_struct;
4460 		rmem = &ring->ring_mem;
4461 		rmem->nr_pages = bp->rx_agg_nr_pages;
4462 		rmem->page_size = HW_RXBD_RING_SIZE;
4463 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4464 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4465 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4466 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4467 
4468 skip_rx:
4469 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4470 			ring = &txr->tx_ring_struct;
4471 			rmem = &ring->ring_mem;
4472 			rmem->nr_pages = bp->tx_nr_pages;
4473 			rmem->page_size = HW_TXBD_RING_SIZE;
4474 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4475 			rmem->dma_arr = txr->tx_desc_mapping;
4476 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4477 			rmem->vmem = (void **)&txr->tx_buf_ring;
4478 		}
4479 	}
4480 }
4481 
4482 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4483 {
4484 	int i;
4485 	u32 prod;
4486 	struct rx_bd **rx_buf_ring;
4487 
4488 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4489 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4490 		int j;
4491 		struct rx_bd *rxbd;
4492 
4493 		rxbd = rx_buf_ring[i];
4494 		if (!rxbd)
4495 			continue;
4496 
4497 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4498 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4499 			rxbd->rx_bd_opaque = prod;
4500 		}
4501 	}
4502 }
4503 
4504 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4505 				       struct bnxt_rx_ring_info *rxr,
4506 				       int ring_nr)
4507 {
4508 	u32 prod;
4509 	int i;
4510 
4511 	prod = rxr->rx_prod;
4512 	for (i = 0; i < bp->rx_ring_size; i++) {
4513 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4514 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4515 				    ring_nr, i, bp->rx_ring_size);
4516 			break;
4517 		}
4518 		prod = NEXT_RX(prod);
4519 	}
4520 	rxr->rx_prod = prod;
4521 }
4522 
4523 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4524 					  struct bnxt_rx_ring_info *rxr,
4525 					  int ring_nr)
4526 {
4527 	int fill_level, i;
4528 	u32 prod;
4529 
4530 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4531 
4532 	prod = rxr->rx_agg_prod;
4533 	for (i = 0; i < fill_level; i++) {
4534 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4535 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4536 				    ring_nr, i, bp->rx_agg_ring_size);
4537 			break;
4538 		}
4539 		prod = NEXT_RX_AGG(prod);
4540 	}
4541 	rxr->rx_agg_prod = prod;
4542 }
4543 
4544 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4545 					struct bnxt_rx_ring_info *rxr)
4546 {
4547 	dma_addr_t mapping;
4548 	u8 *data;
4549 	int i;
4550 
4551 	for (i = 0; i < bp->max_tpa; i++) {
4552 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4553 					    GFP_KERNEL);
4554 		if (!data)
4555 			return -ENOMEM;
4556 
4557 		rxr->rx_tpa[i].data = data;
4558 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4559 		rxr->rx_tpa[i].mapping = mapping;
4560 	}
4561 
4562 	return 0;
4563 }
4564 
4565 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4566 {
4567 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4568 	int rc;
4569 
4570 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4571 
4572 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4573 		return 0;
4574 
4575 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4576 
4577 	if (rxr->rx_tpa) {
4578 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4579 		if (rc)
4580 			return rc;
4581 	}
4582 	return 0;
4583 }
4584 
4585 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4586 				       struct bnxt_rx_ring_info *rxr)
4587 {
4588 	struct bnxt_ring_struct *ring;
4589 	u32 type;
4590 
4591 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4592 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4593 
4594 	if (NET_IP_ALIGN == 2)
4595 		type |= RX_BD_FLAGS_SOP;
4596 
4597 	ring = &rxr->rx_ring_struct;
4598 	bnxt_init_rxbd_pages(ring, type);
4599 	ring->fw_ring_id = INVALID_HW_RING_ID;
4600 }
4601 
4602 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4603 					   struct bnxt_rx_ring_info *rxr)
4604 {
4605 	struct bnxt_ring_struct *ring;
4606 	u32 type;
4607 
4608 	ring = &rxr->rx_agg_ring_struct;
4609 	ring->fw_ring_id = INVALID_HW_RING_ID;
4610 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4611 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4612 			RX_BD_TYPE_RX_AGG_BD;
4613 
4614 		/* On P7, setting EOP will cause the chip to disable
4615 		 * Relaxed Ordering (RO) for TPA data.  Disable EOP for
4616 		 * potentially higher performance with RO.
4617 		 */
4618 		if (BNXT_CHIP_P5_AND_MINUS(bp) || !(bp->flags & BNXT_FLAG_TPA))
4619 			type |= RX_BD_FLAGS_AGG_EOP;
4620 
4621 		bnxt_init_rxbd_pages(ring, type);
4622 	}
4623 }
4624 
4625 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4626 {
4627 	struct bnxt_rx_ring_info *rxr;
4628 
4629 	rxr = &bp->rx_ring[ring_nr];
4630 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4631 
4632 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4633 			     &rxr->bnapi->napi);
4634 
4635 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4636 		bpf_prog_add(bp->xdp_prog, 1);
4637 		rxr->xdp_prog = bp->xdp_prog;
4638 	}
4639 
4640 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4641 
4642 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4643 }
4644 
4645 static void bnxt_init_cp_rings(struct bnxt *bp)
4646 {
4647 	int i, j;
4648 
4649 	for (i = 0; i < bp->cp_nr_rings; i++) {
4650 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4651 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4652 
4653 		ring->fw_ring_id = INVALID_HW_RING_ID;
4654 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4655 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4656 		if (!cpr->cp_ring_arr)
4657 			continue;
4658 		for (j = 0; j < cpr->cp_ring_count; j++) {
4659 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4660 
4661 			ring = &cpr2->cp_ring_struct;
4662 			ring->fw_ring_id = INVALID_HW_RING_ID;
4663 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4664 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4665 		}
4666 	}
4667 }
4668 
4669 static int bnxt_init_rx_rings(struct bnxt *bp)
4670 {
4671 	int i, rc = 0;
4672 
4673 	if (BNXT_RX_PAGE_MODE(bp)) {
4674 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4675 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4676 	} else {
4677 		bp->rx_offset = BNXT_RX_OFFSET;
4678 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4679 	}
4680 
4681 	for (i = 0; i < bp->rx_nr_rings; i++) {
4682 		rc = bnxt_init_one_rx_ring(bp, i);
4683 		if (rc)
4684 			break;
4685 	}
4686 
4687 	return rc;
4688 }
4689 
4690 static int bnxt_init_tx_rings(struct bnxt *bp)
4691 {
4692 	netdev_features_t features;
4693 	u16 i;
4694 
4695 	features = bp->dev->features;
4696 
4697 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4698 				   bnxt_min_tx_desc_cnt(bp, features));
4699 
4700 	for (i = 0; i < bp->tx_nr_rings; i++) {
4701 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4702 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4703 
4704 		ring->fw_ring_id = INVALID_HW_RING_ID;
4705 
4706 		if (i >= bp->tx_nr_rings_xdp)
4707 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4708 					     NETDEV_QUEUE_TYPE_TX,
4709 					     &txr->bnapi->napi);
4710 	}
4711 
4712 	return 0;
4713 }
4714 
4715 static void bnxt_free_ring_grps(struct bnxt *bp)
4716 {
4717 	kfree(bp->grp_info);
4718 	bp->grp_info = NULL;
4719 }
4720 
4721 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4722 {
4723 	int i;
4724 
4725 	if (irq_re_init) {
4726 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4727 					    bp->cp_nr_rings);
4728 		if (!bp->grp_info)
4729 			return -ENOMEM;
4730 	}
4731 	for (i = 0; i < bp->cp_nr_rings; i++) {
4732 		if (irq_re_init)
4733 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4734 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4735 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4736 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4737 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4738 	}
4739 	return 0;
4740 }
4741 
4742 static void bnxt_free_vnics(struct bnxt *bp)
4743 {
4744 	kfree(bp->vnic_info);
4745 	bp->vnic_info = NULL;
4746 	bp->nr_vnics = 0;
4747 }
4748 
4749 static int bnxt_alloc_vnics(struct bnxt *bp)
4750 {
4751 	int num_vnics = 1;
4752 
4753 #ifdef CONFIG_RFS_ACCEL
4754 	if (bp->flags & BNXT_FLAG_RFS) {
4755 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4756 			num_vnics++;
4757 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4758 			num_vnics += bp->rx_nr_rings;
4759 	}
4760 #endif
4761 
4762 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4763 		num_vnics++;
4764 
4765 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4766 	if (!bp->vnic_info)
4767 		return -ENOMEM;
4768 
4769 	bp->nr_vnics = num_vnics;
4770 	return 0;
4771 }
4772 
4773 static void bnxt_init_vnics(struct bnxt *bp)
4774 {
4775 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4776 	int i;
4777 
4778 	for (i = 0; i < bp->nr_vnics; i++) {
4779 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4780 		int j;
4781 
4782 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4783 		vnic->vnic_id = i;
4784 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4785 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4786 
4787 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4788 
4789 		if (bp->vnic_info[i].rss_hash_key) {
4790 			if (i == BNXT_VNIC_DEFAULT) {
4791 				u8 *key = (void *)vnic->rss_hash_key;
4792 				int k;
4793 
4794 				if (!bp->rss_hash_key_valid &&
4795 				    !bp->rss_hash_key_updated) {
4796 					get_random_bytes(bp->rss_hash_key,
4797 							 HW_HASH_KEY_SIZE);
4798 					bp->rss_hash_key_updated = true;
4799 				}
4800 
4801 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4802 				       HW_HASH_KEY_SIZE);
4803 
4804 				if (!bp->rss_hash_key_updated)
4805 					continue;
4806 
4807 				bp->rss_hash_key_updated = false;
4808 				bp->rss_hash_key_valid = true;
4809 
4810 				bp->toeplitz_prefix = 0;
4811 				for (k = 0; k < 8; k++) {
4812 					bp->toeplitz_prefix <<= 8;
4813 					bp->toeplitz_prefix |= key[k];
4814 				}
4815 			} else {
4816 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4817 				       HW_HASH_KEY_SIZE);
4818 			}
4819 		}
4820 	}
4821 }
4822 
4823 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4824 {
4825 	int pages;
4826 
4827 	pages = ring_size / desc_per_pg;
4828 
4829 	if (!pages)
4830 		return 1;
4831 
4832 	pages++;
4833 
4834 	while (pages & (pages - 1))
4835 		pages++;
4836 
4837 	return pages;
4838 }
4839 
4840 void bnxt_set_tpa_flags(struct bnxt *bp)
4841 {
4842 	bp->flags &= ~BNXT_FLAG_TPA;
4843 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4844 		return;
4845 	if (bp->dev->features & NETIF_F_LRO)
4846 		bp->flags |= BNXT_FLAG_LRO;
4847 	else if (bp->dev->features & NETIF_F_GRO_HW)
4848 		bp->flags |= BNXT_FLAG_GRO;
4849 }
4850 
4851 static void bnxt_init_ring_params(struct bnxt *bp)
4852 {
4853 	unsigned int rx_size;
4854 
4855 	bp->rx_copybreak = 0; /* rx-copybreak disabled by default */
4856 	/* Try to fit 4 chunks into a 4k page */
4857 	rx_size = SZ_1K -
4858 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4859 	bp->dev->cfg->hds_thresh = max(BNXT_MIN_RX_HDR_BUF, rx_size);
4860 }
4861 
4862 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4863  * be set on entry.
4864  */
4865 void bnxt_set_ring_params(struct bnxt *bp)
4866 {
4867 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4868 	u32 agg_factor = 0, agg_ring_size = 0;
4869 
4870 	/* 8 for CRC and VLAN */
4871 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4872 
4873 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4874 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4875 
4876 	ring_size = bp->rx_ring_size;
4877 	bp->rx_agg_ring_size = 0;
4878 	bp->rx_agg_nr_pages = 0;
4879 
4880 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4881 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4882 
4883 	bp->flags &= ~BNXT_FLAG_JUMBO;
4884 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4885 		u32 jumbo_factor;
4886 
4887 		bp->flags |= BNXT_FLAG_JUMBO;
4888 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4889 		if (jumbo_factor > agg_factor)
4890 			agg_factor = jumbo_factor;
4891 	}
4892 	if (agg_factor) {
4893 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4894 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4895 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4896 				    bp->rx_ring_size, ring_size);
4897 			bp->rx_ring_size = ring_size;
4898 		}
4899 		agg_ring_size = ring_size * agg_factor;
4900 
4901 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4902 							RX_DESC_CNT);
4903 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4904 			u32 tmp = agg_ring_size;
4905 
4906 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4907 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4908 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4909 				    tmp, agg_ring_size);
4910 		}
4911 		bp->rx_agg_ring_size = agg_ring_size;
4912 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4913 
4914 		if (BNXT_RX_PAGE_MODE(bp)) {
4915 			rx_space = PAGE_SIZE;
4916 			rx_size = PAGE_SIZE -
4917 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4918 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4919 		} else {
4920 			rx_size = max3(BNXT_MIN_RX_HDR_BUF,
4921 				       bp->rx_copybreak,
4922 				       bp->dev->cfg_pending->hds_thresh);
4923 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4924 			rx_space = rx_size + NET_SKB_PAD +
4925 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4926 		}
4927 	}
4928 
4929 	bp->rx_buf_use_size = rx_size;
4930 	bp->rx_buf_size = rx_space;
4931 
4932 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4933 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4934 
4935 	ring_size = bp->tx_ring_size;
4936 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4937 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4938 
4939 	max_rx_cmpl = bp->rx_ring_size;
4940 	/* MAX TPA needs to be added because TPA_START completions are
4941 	 * immediately recycled, so the TPA completions are not bound by
4942 	 * the RX ring size.
4943 	 */
4944 	if (bp->flags & BNXT_FLAG_TPA)
4945 		max_rx_cmpl += bp->max_tpa;
4946 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4947 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4948 	bp->cp_ring_size = ring_size;
4949 
4950 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4951 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4952 		bp->cp_nr_pages = MAX_CP_PAGES;
4953 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4954 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4955 			    ring_size, bp->cp_ring_size);
4956 	}
4957 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4958 	bp->cp_ring_mask = bp->cp_bit - 1;
4959 }
4960 
4961 /* Changing allocation mode of RX rings.
4962  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4963  */
4964 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4965 {
4966 	struct net_device *dev = bp->dev;
4967 
4968 	if (page_mode) {
4969 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4970 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4971 
4972 		if (bp->xdp_prog->aux->xdp_has_frags)
4973 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4974 		else
4975 			dev->max_mtu =
4976 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4977 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4978 			bp->flags |= BNXT_FLAG_JUMBO;
4979 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4980 		} else {
4981 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4982 			bp->rx_skb_func = bnxt_rx_page_skb;
4983 		}
4984 		bp->rx_dir = DMA_BIDIRECTIONAL;
4985 	} else {
4986 		dev->max_mtu = bp->max_mtu;
4987 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4988 		bp->rx_dir = DMA_FROM_DEVICE;
4989 		bp->rx_skb_func = bnxt_rx_skb;
4990 	}
4991 }
4992 
4993 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4994 {
4995 	__bnxt_set_rx_skb_mode(bp, page_mode);
4996 
4997 	if (!page_mode) {
4998 		int rx, tx;
4999 
5000 		bnxt_get_max_rings(bp, &rx, &tx, true);
5001 		if (rx > 1) {
5002 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
5003 			bp->dev->hw_features |= NETIF_F_LRO;
5004 		}
5005 	}
5006 
5007 	/* Update LRO and GRO_HW availability */
5008 	netdev_update_features(bp->dev);
5009 }
5010 
5011 static void bnxt_free_vnic_attributes(struct bnxt *bp)
5012 {
5013 	int i;
5014 	struct bnxt_vnic_info *vnic;
5015 	struct pci_dev *pdev = bp->pdev;
5016 
5017 	if (!bp->vnic_info)
5018 		return;
5019 
5020 	for (i = 0; i < bp->nr_vnics; i++) {
5021 		vnic = &bp->vnic_info[i];
5022 
5023 		kfree(vnic->fw_grp_ids);
5024 		vnic->fw_grp_ids = NULL;
5025 
5026 		kfree(vnic->uc_list);
5027 		vnic->uc_list = NULL;
5028 
5029 		if (vnic->mc_list) {
5030 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
5031 					  vnic->mc_list, vnic->mc_list_mapping);
5032 			vnic->mc_list = NULL;
5033 		}
5034 
5035 		if (vnic->rss_table) {
5036 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
5037 					  vnic->rss_table,
5038 					  vnic->rss_table_dma_addr);
5039 			vnic->rss_table = NULL;
5040 		}
5041 
5042 		vnic->rss_hash_key = NULL;
5043 		vnic->flags = 0;
5044 	}
5045 }
5046 
5047 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
5048 {
5049 	int i, rc = 0, size;
5050 	struct bnxt_vnic_info *vnic;
5051 	struct pci_dev *pdev = bp->pdev;
5052 	int max_rings;
5053 
5054 	for (i = 0; i < bp->nr_vnics; i++) {
5055 		vnic = &bp->vnic_info[i];
5056 
5057 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
5058 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
5059 
5060 			if (mem_size > 0) {
5061 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
5062 				if (!vnic->uc_list) {
5063 					rc = -ENOMEM;
5064 					goto out;
5065 				}
5066 			}
5067 		}
5068 
5069 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
5070 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
5071 			vnic->mc_list =
5072 				dma_alloc_coherent(&pdev->dev,
5073 						   vnic->mc_list_size,
5074 						   &vnic->mc_list_mapping,
5075 						   GFP_KERNEL);
5076 			if (!vnic->mc_list) {
5077 				rc = -ENOMEM;
5078 				goto out;
5079 			}
5080 		}
5081 
5082 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5083 			goto vnic_skip_grps;
5084 
5085 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5086 			max_rings = bp->rx_nr_rings;
5087 		else
5088 			max_rings = 1;
5089 
5090 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
5091 		if (!vnic->fw_grp_ids) {
5092 			rc = -ENOMEM;
5093 			goto out;
5094 		}
5095 vnic_skip_grps:
5096 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
5097 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
5098 			continue;
5099 
5100 		/* Allocate rss table and hash key */
5101 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5102 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5103 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5104 
5105 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5106 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5107 						     vnic->rss_table_size,
5108 						     &vnic->rss_table_dma_addr,
5109 						     GFP_KERNEL);
5110 		if (!vnic->rss_table) {
5111 			rc = -ENOMEM;
5112 			goto out;
5113 		}
5114 
5115 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5116 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5117 	}
5118 	return 0;
5119 
5120 out:
5121 	return rc;
5122 }
5123 
5124 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5125 {
5126 	struct bnxt_hwrm_wait_token *token;
5127 
5128 	dma_pool_destroy(bp->hwrm_dma_pool);
5129 	bp->hwrm_dma_pool = NULL;
5130 
5131 	rcu_read_lock();
5132 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5133 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5134 	rcu_read_unlock();
5135 }
5136 
5137 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5138 {
5139 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5140 					    BNXT_HWRM_DMA_SIZE,
5141 					    BNXT_HWRM_DMA_ALIGN, 0);
5142 	if (!bp->hwrm_dma_pool)
5143 		return -ENOMEM;
5144 
5145 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5146 
5147 	return 0;
5148 }
5149 
5150 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5151 {
5152 	kfree(stats->hw_masks);
5153 	stats->hw_masks = NULL;
5154 	kfree(stats->sw_stats);
5155 	stats->sw_stats = NULL;
5156 	if (stats->hw_stats) {
5157 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5158 				  stats->hw_stats_map);
5159 		stats->hw_stats = NULL;
5160 	}
5161 }
5162 
5163 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5164 				bool alloc_masks)
5165 {
5166 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5167 					     &stats->hw_stats_map, GFP_KERNEL);
5168 	if (!stats->hw_stats)
5169 		return -ENOMEM;
5170 
5171 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5172 	if (!stats->sw_stats)
5173 		goto stats_mem_err;
5174 
5175 	if (alloc_masks) {
5176 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5177 		if (!stats->hw_masks)
5178 			goto stats_mem_err;
5179 	}
5180 	return 0;
5181 
5182 stats_mem_err:
5183 	bnxt_free_stats_mem(bp, stats);
5184 	return -ENOMEM;
5185 }
5186 
5187 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5188 {
5189 	int i;
5190 
5191 	for (i = 0; i < count; i++)
5192 		mask_arr[i] = mask;
5193 }
5194 
5195 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5196 {
5197 	int i;
5198 
5199 	for (i = 0; i < count; i++)
5200 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5201 }
5202 
5203 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5204 				    struct bnxt_stats_mem *stats)
5205 {
5206 	struct hwrm_func_qstats_ext_output *resp;
5207 	struct hwrm_func_qstats_ext_input *req;
5208 	__le64 *hw_masks;
5209 	int rc;
5210 
5211 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5212 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5213 		return -EOPNOTSUPP;
5214 
5215 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5216 	if (rc)
5217 		return rc;
5218 
5219 	req->fid = cpu_to_le16(0xffff);
5220 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5221 
5222 	resp = hwrm_req_hold(bp, req);
5223 	rc = hwrm_req_send(bp, req);
5224 	if (!rc) {
5225 		hw_masks = &resp->rx_ucast_pkts;
5226 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5227 	}
5228 	hwrm_req_drop(bp, req);
5229 	return rc;
5230 }
5231 
5232 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5233 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5234 
5235 static void bnxt_init_stats(struct bnxt *bp)
5236 {
5237 	struct bnxt_napi *bnapi = bp->bnapi[0];
5238 	struct bnxt_cp_ring_info *cpr;
5239 	struct bnxt_stats_mem *stats;
5240 	__le64 *rx_stats, *tx_stats;
5241 	int rc, rx_count, tx_count;
5242 	u64 *rx_masks, *tx_masks;
5243 	u64 mask;
5244 	u8 flags;
5245 
5246 	cpr = &bnapi->cp_ring;
5247 	stats = &cpr->stats;
5248 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5249 	if (rc) {
5250 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5251 			mask = (1ULL << 48) - 1;
5252 		else
5253 			mask = -1ULL;
5254 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5255 	}
5256 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5257 		stats = &bp->port_stats;
5258 		rx_stats = stats->hw_stats;
5259 		rx_masks = stats->hw_masks;
5260 		rx_count = sizeof(struct rx_port_stats) / 8;
5261 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5262 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5263 		tx_count = sizeof(struct tx_port_stats) / 8;
5264 
5265 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5266 		rc = bnxt_hwrm_port_qstats(bp, flags);
5267 		if (rc) {
5268 			mask = (1ULL << 40) - 1;
5269 
5270 			bnxt_fill_masks(rx_masks, mask, rx_count);
5271 			bnxt_fill_masks(tx_masks, mask, tx_count);
5272 		} else {
5273 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5274 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5275 			bnxt_hwrm_port_qstats(bp, 0);
5276 		}
5277 	}
5278 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5279 		stats = &bp->rx_port_stats_ext;
5280 		rx_stats = stats->hw_stats;
5281 		rx_masks = stats->hw_masks;
5282 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5283 		stats = &bp->tx_port_stats_ext;
5284 		tx_stats = stats->hw_stats;
5285 		tx_masks = stats->hw_masks;
5286 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5287 
5288 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5289 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5290 		if (rc) {
5291 			mask = (1ULL << 40) - 1;
5292 
5293 			bnxt_fill_masks(rx_masks, mask, rx_count);
5294 			if (tx_stats)
5295 				bnxt_fill_masks(tx_masks, mask, tx_count);
5296 		} else {
5297 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5298 			if (tx_stats)
5299 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5300 						   tx_count);
5301 			bnxt_hwrm_port_qstats_ext(bp, 0);
5302 		}
5303 	}
5304 }
5305 
5306 static void bnxt_free_port_stats(struct bnxt *bp)
5307 {
5308 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5309 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5310 
5311 	bnxt_free_stats_mem(bp, &bp->port_stats);
5312 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5313 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5314 }
5315 
5316 static void bnxt_free_ring_stats(struct bnxt *bp)
5317 {
5318 	int i;
5319 
5320 	if (!bp->bnapi)
5321 		return;
5322 
5323 	for (i = 0; i < bp->cp_nr_rings; i++) {
5324 		struct bnxt_napi *bnapi = bp->bnapi[i];
5325 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5326 
5327 		bnxt_free_stats_mem(bp, &cpr->stats);
5328 
5329 		kfree(cpr->sw_stats);
5330 		cpr->sw_stats = NULL;
5331 	}
5332 }
5333 
5334 static int bnxt_alloc_stats(struct bnxt *bp)
5335 {
5336 	u32 size, i;
5337 	int rc;
5338 
5339 	size = bp->hw_ring_stats_size;
5340 
5341 	for (i = 0; i < bp->cp_nr_rings; i++) {
5342 		struct bnxt_napi *bnapi = bp->bnapi[i];
5343 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5344 
5345 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5346 		if (!cpr->sw_stats)
5347 			return -ENOMEM;
5348 
5349 		cpr->stats.len = size;
5350 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5351 		if (rc)
5352 			return rc;
5353 
5354 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5355 	}
5356 
5357 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5358 		return 0;
5359 
5360 	if (bp->port_stats.hw_stats)
5361 		goto alloc_ext_stats;
5362 
5363 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5364 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5365 	if (rc)
5366 		return rc;
5367 
5368 	bp->flags |= BNXT_FLAG_PORT_STATS;
5369 
5370 alloc_ext_stats:
5371 	/* Display extended statistics only if FW supports it */
5372 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5373 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5374 			return 0;
5375 
5376 	if (bp->rx_port_stats_ext.hw_stats)
5377 		goto alloc_tx_ext_stats;
5378 
5379 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5380 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5381 	/* Extended stats are optional */
5382 	if (rc)
5383 		return 0;
5384 
5385 alloc_tx_ext_stats:
5386 	if (bp->tx_port_stats_ext.hw_stats)
5387 		return 0;
5388 
5389 	if (bp->hwrm_spec_code >= 0x10902 ||
5390 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5391 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5392 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5393 		/* Extended stats are optional */
5394 		if (rc)
5395 			return 0;
5396 	}
5397 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5398 	return 0;
5399 }
5400 
5401 static void bnxt_clear_ring_indices(struct bnxt *bp)
5402 {
5403 	int i, j;
5404 
5405 	if (!bp->bnapi)
5406 		return;
5407 
5408 	for (i = 0; i < bp->cp_nr_rings; i++) {
5409 		struct bnxt_napi *bnapi = bp->bnapi[i];
5410 		struct bnxt_cp_ring_info *cpr;
5411 		struct bnxt_rx_ring_info *rxr;
5412 		struct bnxt_tx_ring_info *txr;
5413 
5414 		if (!bnapi)
5415 			continue;
5416 
5417 		cpr = &bnapi->cp_ring;
5418 		cpr->cp_raw_cons = 0;
5419 
5420 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5421 			txr->tx_prod = 0;
5422 			txr->tx_cons = 0;
5423 			txr->tx_hw_cons = 0;
5424 		}
5425 
5426 		rxr = bnapi->rx_ring;
5427 		if (rxr) {
5428 			rxr->rx_prod = 0;
5429 			rxr->rx_agg_prod = 0;
5430 			rxr->rx_sw_agg_prod = 0;
5431 			rxr->rx_next_cons = 0;
5432 		}
5433 		bnapi->events = 0;
5434 	}
5435 }
5436 
5437 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5438 {
5439 	u8 type = fltr->type, flags = fltr->flags;
5440 
5441 	INIT_LIST_HEAD(&fltr->list);
5442 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5443 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5444 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5445 }
5446 
5447 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5448 {
5449 	if (!list_empty(&fltr->list))
5450 		list_del_init(&fltr->list);
5451 }
5452 
5453 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5454 {
5455 	struct bnxt_filter_base *usr_fltr, *tmp;
5456 
5457 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5458 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5459 			continue;
5460 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5461 	}
5462 }
5463 
5464 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5465 {
5466 	hlist_del(&fltr->hash);
5467 	bnxt_del_one_usr_fltr(bp, fltr);
5468 	if (fltr->flags) {
5469 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5470 		bp->ntp_fltr_count--;
5471 	}
5472 	kfree(fltr);
5473 }
5474 
5475 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5476 {
5477 	int i;
5478 
5479 	netdev_assert_locked_or_invisible(bp->dev);
5480 
5481 	/* Under netdev instance lock and all our NAPIs have been disabled.
5482 	 * It's safe to delete the hash table.
5483 	 */
5484 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5485 		struct hlist_head *head;
5486 		struct hlist_node *tmp;
5487 		struct bnxt_ntuple_filter *fltr;
5488 
5489 		head = &bp->ntp_fltr_hash_tbl[i];
5490 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5491 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5492 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5493 				     !list_empty(&fltr->base.list)))
5494 				continue;
5495 			bnxt_del_fltr(bp, &fltr->base);
5496 		}
5497 	}
5498 	if (!all)
5499 		return;
5500 
5501 	bitmap_free(bp->ntp_fltr_bmap);
5502 	bp->ntp_fltr_bmap = NULL;
5503 	bp->ntp_fltr_count = 0;
5504 }
5505 
5506 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5507 {
5508 	int i, rc = 0;
5509 
5510 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5511 		return 0;
5512 
5513 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5514 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5515 
5516 	bp->ntp_fltr_count = 0;
5517 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5518 
5519 	if (!bp->ntp_fltr_bmap)
5520 		rc = -ENOMEM;
5521 
5522 	return rc;
5523 }
5524 
5525 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5526 {
5527 	int i;
5528 
5529 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5530 		struct hlist_head *head;
5531 		struct hlist_node *tmp;
5532 		struct bnxt_l2_filter *fltr;
5533 
5534 		head = &bp->l2_fltr_hash_tbl[i];
5535 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5536 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5537 				     !list_empty(&fltr->base.list)))
5538 				continue;
5539 			bnxt_del_fltr(bp, &fltr->base);
5540 		}
5541 	}
5542 }
5543 
5544 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5545 {
5546 	int i;
5547 
5548 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5549 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5550 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5551 }
5552 
5553 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5554 {
5555 	bnxt_free_vnic_attributes(bp);
5556 	bnxt_free_tx_rings(bp);
5557 	bnxt_free_rx_rings(bp);
5558 	bnxt_free_cp_rings(bp);
5559 	bnxt_free_all_cp_arrays(bp);
5560 	bnxt_free_ntp_fltrs(bp, false);
5561 	bnxt_free_l2_filters(bp, false);
5562 	if (irq_re_init) {
5563 		bnxt_free_ring_stats(bp);
5564 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5565 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5566 			bnxt_free_port_stats(bp);
5567 		bnxt_free_ring_grps(bp);
5568 		bnxt_free_vnics(bp);
5569 		kfree(bp->tx_ring_map);
5570 		bp->tx_ring_map = NULL;
5571 		kfree(bp->tx_ring);
5572 		bp->tx_ring = NULL;
5573 		kfree(bp->rx_ring);
5574 		bp->rx_ring = NULL;
5575 		kfree(bp->bnapi);
5576 		bp->bnapi = NULL;
5577 	} else {
5578 		bnxt_clear_ring_indices(bp);
5579 	}
5580 }
5581 
5582 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5583 {
5584 	int i, j, rc, size, arr_size;
5585 	void *bnapi;
5586 
5587 	if (irq_re_init) {
5588 		/* Allocate bnapi mem pointer array and mem block for
5589 		 * all queues
5590 		 */
5591 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5592 				bp->cp_nr_rings);
5593 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5594 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5595 		if (!bnapi)
5596 			return -ENOMEM;
5597 
5598 		bp->bnapi = bnapi;
5599 		bnapi += arr_size;
5600 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5601 			bp->bnapi[i] = bnapi;
5602 			bp->bnapi[i]->index = i;
5603 			bp->bnapi[i]->bp = bp;
5604 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5605 				struct bnxt_cp_ring_info *cpr =
5606 					&bp->bnapi[i]->cp_ring;
5607 
5608 				cpr->cp_ring_struct.ring_mem.flags =
5609 					BNXT_RMEM_RING_PTE_FLAG;
5610 			}
5611 		}
5612 
5613 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5614 					   bp->rx_nr_rings);
5615 		if (!bp->rx_ring)
5616 			return -ENOMEM;
5617 
5618 		for (i = 0; i < bp->rx_nr_rings; i++) {
5619 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5620 
5621 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5622 				rxr->rx_ring_struct.ring_mem.flags =
5623 					BNXT_RMEM_RING_PTE_FLAG;
5624 				rxr->rx_agg_ring_struct.ring_mem.flags =
5625 					BNXT_RMEM_RING_PTE_FLAG;
5626 			} else {
5627 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5628 			}
5629 			rxr->bnapi = bp->bnapi[i];
5630 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5631 		}
5632 
5633 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5634 					   bp->tx_nr_rings);
5635 		if (!bp->tx_ring)
5636 			return -ENOMEM;
5637 
5638 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5639 					  GFP_KERNEL);
5640 
5641 		if (!bp->tx_ring_map)
5642 			return -ENOMEM;
5643 
5644 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5645 			j = 0;
5646 		else
5647 			j = bp->rx_nr_rings;
5648 
5649 		for (i = 0; i < bp->tx_nr_rings; i++) {
5650 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5651 			struct bnxt_napi *bnapi2;
5652 
5653 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5654 				txr->tx_ring_struct.ring_mem.flags =
5655 					BNXT_RMEM_RING_PTE_FLAG;
5656 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5657 			if (i >= bp->tx_nr_rings_xdp) {
5658 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5659 
5660 				bnapi2 = bp->bnapi[k];
5661 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5662 				txr->tx_napi_idx =
5663 					BNXT_RING_TO_TC(bp, txr->txq_index);
5664 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5665 				bnapi2->tx_int = bnxt_tx_int;
5666 			} else {
5667 				bnapi2 = bp->bnapi[j];
5668 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5669 				bnapi2->tx_ring[0] = txr;
5670 				bnapi2->tx_int = bnxt_tx_int_xdp;
5671 				j++;
5672 			}
5673 			txr->bnapi = bnapi2;
5674 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5675 				txr->tx_cpr = &bnapi2->cp_ring;
5676 		}
5677 
5678 		rc = bnxt_alloc_stats(bp);
5679 		if (rc)
5680 			goto alloc_mem_err;
5681 		bnxt_init_stats(bp);
5682 
5683 		rc = bnxt_alloc_ntp_fltrs(bp);
5684 		if (rc)
5685 			goto alloc_mem_err;
5686 
5687 		rc = bnxt_alloc_vnics(bp);
5688 		if (rc)
5689 			goto alloc_mem_err;
5690 	}
5691 
5692 	rc = bnxt_alloc_all_cp_arrays(bp);
5693 	if (rc)
5694 		goto alloc_mem_err;
5695 
5696 	bnxt_init_ring_struct(bp);
5697 
5698 	rc = bnxt_alloc_rx_rings(bp);
5699 	if (rc)
5700 		goto alloc_mem_err;
5701 
5702 	rc = bnxt_alloc_tx_rings(bp);
5703 	if (rc)
5704 		goto alloc_mem_err;
5705 
5706 	rc = bnxt_alloc_cp_rings(bp);
5707 	if (rc)
5708 		goto alloc_mem_err;
5709 
5710 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5711 						  BNXT_VNIC_MCAST_FLAG |
5712 						  BNXT_VNIC_UCAST_FLAG;
5713 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5714 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5715 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5716 
5717 	rc = bnxt_alloc_vnic_attributes(bp);
5718 	if (rc)
5719 		goto alloc_mem_err;
5720 	return 0;
5721 
5722 alloc_mem_err:
5723 	bnxt_free_mem(bp, true);
5724 	return rc;
5725 }
5726 
5727 static void bnxt_disable_int(struct bnxt *bp)
5728 {
5729 	int i;
5730 
5731 	if (!bp->bnapi)
5732 		return;
5733 
5734 	for (i = 0; i < bp->cp_nr_rings; i++) {
5735 		struct bnxt_napi *bnapi = bp->bnapi[i];
5736 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5737 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5738 
5739 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5740 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5741 	}
5742 }
5743 
5744 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5745 {
5746 	struct bnxt_napi *bnapi = bp->bnapi[n];
5747 	struct bnxt_cp_ring_info *cpr;
5748 
5749 	cpr = &bnapi->cp_ring;
5750 	return cpr->cp_ring_struct.map_idx;
5751 }
5752 
5753 static void bnxt_disable_int_sync(struct bnxt *bp)
5754 {
5755 	int i;
5756 
5757 	if (!bp->irq_tbl || !bp->bnapi)
5758 		return;
5759 
5760 	atomic_inc(&bp->intr_sem);
5761 
5762 	bnxt_disable_int(bp);
5763 	for (i = 0; i < bp->cp_nr_rings; i++) {
5764 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5765 
5766 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5767 	}
5768 }
5769 
5770 static void bnxt_enable_int(struct bnxt *bp)
5771 {
5772 	int i;
5773 
5774 	atomic_set(&bp->intr_sem, 0);
5775 	for (i = 0; i < bp->cp_nr_rings; i++) {
5776 		struct bnxt_napi *bnapi = bp->bnapi[i];
5777 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5778 
5779 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5780 	}
5781 }
5782 
5783 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5784 			    bool async_only)
5785 {
5786 	DECLARE_BITMAP(async_events_bmap, 256);
5787 	u32 *events = (u32 *)async_events_bmap;
5788 	struct hwrm_func_drv_rgtr_output *resp;
5789 	struct hwrm_func_drv_rgtr_input *req;
5790 	u32 flags;
5791 	int rc, i;
5792 
5793 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5794 	if (rc)
5795 		return rc;
5796 
5797 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5798 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5799 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5800 
5801 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5802 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5803 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5804 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5805 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5806 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5807 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5808 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5809 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5810 	req->flags = cpu_to_le32(flags);
5811 	req->ver_maj_8b = DRV_VER_MAJ;
5812 	req->ver_min_8b = DRV_VER_MIN;
5813 	req->ver_upd_8b = DRV_VER_UPD;
5814 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5815 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5816 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5817 
5818 	if (BNXT_PF(bp)) {
5819 		u32 data[8];
5820 		int i;
5821 
5822 		memset(data, 0, sizeof(data));
5823 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5824 			u16 cmd = bnxt_vf_req_snif[i];
5825 			unsigned int bit, idx;
5826 
5827 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5828 			    cmd == HWRM_PORT_PHY_QCFG)
5829 				continue;
5830 
5831 			idx = cmd / 32;
5832 			bit = cmd % 32;
5833 			data[idx] |= 1 << bit;
5834 		}
5835 
5836 		for (i = 0; i < 8; i++)
5837 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5838 
5839 		req->enables |=
5840 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5841 	}
5842 
5843 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5844 		req->flags |= cpu_to_le32(
5845 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5846 
5847 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5848 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5849 		u16 event_id = bnxt_async_events_arr[i];
5850 
5851 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5852 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5853 			continue;
5854 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5855 		    !bp->ptp_cfg)
5856 			continue;
5857 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5858 	}
5859 	if (bmap && bmap_size) {
5860 		for (i = 0; i < bmap_size; i++) {
5861 			if (test_bit(i, bmap))
5862 				__set_bit(i, async_events_bmap);
5863 		}
5864 	}
5865 	for (i = 0; i < 8; i++)
5866 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5867 
5868 	if (async_only)
5869 		req->enables =
5870 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5871 
5872 	resp = hwrm_req_hold(bp, req);
5873 	rc = hwrm_req_send(bp, req);
5874 	if (!rc) {
5875 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5876 		if (resp->flags &
5877 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5878 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5879 	}
5880 	hwrm_req_drop(bp, req);
5881 	return rc;
5882 }
5883 
5884 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5885 {
5886 	struct hwrm_func_drv_unrgtr_input *req;
5887 	int rc;
5888 
5889 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5890 		return 0;
5891 
5892 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5893 	if (rc)
5894 		return rc;
5895 	return hwrm_req_send(bp, req);
5896 }
5897 
5898 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5899 
5900 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5901 {
5902 	struct hwrm_tunnel_dst_port_free_input *req;
5903 	int rc;
5904 
5905 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5906 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5907 		return 0;
5908 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5909 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5910 		return 0;
5911 
5912 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5913 	if (rc)
5914 		return rc;
5915 
5916 	req->tunnel_type = tunnel_type;
5917 
5918 	switch (tunnel_type) {
5919 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5920 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5921 		bp->vxlan_port = 0;
5922 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5923 		break;
5924 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5925 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5926 		bp->nge_port = 0;
5927 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5928 		break;
5929 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5930 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5931 		bp->vxlan_gpe_port = 0;
5932 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5933 		break;
5934 	default:
5935 		break;
5936 	}
5937 
5938 	rc = hwrm_req_send(bp, req);
5939 	if (rc)
5940 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5941 			   rc);
5942 	if (bp->flags & BNXT_FLAG_TPA)
5943 		bnxt_set_tpa(bp, true);
5944 	return rc;
5945 }
5946 
5947 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5948 					   u8 tunnel_type)
5949 {
5950 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5951 	struct hwrm_tunnel_dst_port_alloc_input *req;
5952 	int rc;
5953 
5954 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5955 	if (rc)
5956 		return rc;
5957 
5958 	req->tunnel_type = tunnel_type;
5959 	req->tunnel_dst_port_val = port;
5960 
5961 	resp = hwrm_req_hold(bp, req);
5962 	rc = hwrm_req_send(bp, req);
5963 	if (rc) {
5964 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5965 			   rc);
5966 		goto err_out;
5967 	}
5968 
5969 	switch (tunnel_type) {
5970 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5971 		bp->vxlan_port = port;
5972 		bp->vxlan_fw_dst_port_id =
5973 			le16_to_cpu(resp->tunnel_dst_port_id);
5974 		break;
5975 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5976 		bp->nge_port = port;
5977 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5978 		break;
5979 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5980 		bp->vxlan_gpe_port = port;
5981 		bp->vxlan_gpe_fw_dst_port_id =
5982 			le16_to_cpu(resp->tunnel_dst_port_id);
5983 		break;
5984 	default:
5985 		break;
5986 	}
5987 	if (bp->flags & BNXT_FLAG_TPA)
5988 		bnxt_set_tpa(bp, true);
5989 
5990 err_out:
5991 	hwrm_req_drop(bp, req);
5992 	return rc;
5993 }
5994 
5995 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5996 {
5997 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5998 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5999 	int rc;
6000 
6001 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
6002 	if (rc)
6003 		return rc;
6004 
6005 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6006 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
6007 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
6008 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
6009 	}
6010 	req->mask = cpu_to_le32(vnic->rx_mask);
6011 	return hwrm_req_send_silent(bp, req);
6012 }
6013 
6014 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6015 {
6016 	if (!atomic_dec_and_test(&fltr->refcnt))
6017 		return;
6018 	spin_lock_bh(&bp->ntp_fltr_lock);
6019 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
6020 		spin_unlock_bh(&bp->ntp_fltr_lock);
6021 		return;
6022 	}
6023 	hlist_del_rcu(&fltr->base.hash);
6024 	bnxt_del_one_usr_fltr(bp, &fltr->base);
6025 	if (fltr->base.flags) {
6026 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
6027 		bp->ntp_fltr_count--;
6028 	}
6029 	spin_unlock_bh(&bp->ntp_fltr_lock);
6030 	kfree_rcu(fltr, base.rcu);
6031 }
6032 
6033 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
6034 						      struct bnxt_l2_key *key,
6035 						      u32 idx)
6036 {
6037 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
6038 	struct bnxt_l2_filter *fltr;
6039 
6040 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
6041 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
6042 
6043 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
6044 		    l2_key->vlan == key->vlan)
6045 			return fltr;
6046 	}
6047 	return NULL;
6048 }
6049 
6050 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
6051 						    struct bnxt_l2_key *key,
6052 						    u32 idx)
6053 {
6054 	struct bnxt_l2_filter *fltr = NULL;
6055 
6056 	rcu_read_lock();
6057 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6058 	if (fltr)
6059 		atomic_inc(&fltr->refcnt);
6060 	rcu_read_unlock();
6061 	return fltr;
6062 }
6063 
6064 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
6065 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6066 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
6067 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6068 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
6069 
6070 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
6071 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6072 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
6073 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6074 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
6075 
6076 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
6077 {
6078 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6079 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
6080 			return sizeof(fkeys->addrs.v4addrs) +
6081 			       sizeof(fkeys->ports);
6082 
6083 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
6084 			return sizeof(fkeys->addrs.v4addrs);
6085 	}
6086 
6087 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
6088 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
6089 			return sizeof(fkeys->addrs.v6addrs) +
6090 			       sizeof(fkeys->ports);
6091 
6092 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6093 			return sizeof(fkeys->addrs.v6addrs);
6094 	}
6095 
6096 	return 0;
6097 }
6098 
6099 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6100 			 const unsigned char *key)
6101 {
6102 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6103 	struct bnxt_ipv4_tuple tuple4;
6104 	struct bnxt_ipv6_tuple tuple6;
6105 	int i, j, len = 0;
6106 	u8 *four_tuple;
6107 
6108 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6109 	if (!len)
6110 		return 0;
6111 
6112 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6113 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6114 		tuple4.ports = fkeys->ports;
6115 		four_tuple = (unsigned char *)&tuple4;
6116 	} else {
6117 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6118 		tuple6.ports = fkeys->ports;
6119 		four_tuple = (unsigned char *)&tuple6;
6120 	}
6121 
6122 	for (i = 0, j = 8; i < len; i++, j++) {
6123 		u8 byte = four_tuple[i];
6124 		int bit;
6125 
6126 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6127 			if (byte & 0x80)
6128 				hash ^= prefix;
6129 		}
6130 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6131 	}
6132 
6133 	/* The valid part of the hash is in the upper 32 bits. */
6134 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6135 }
6136 
6137 #ifdef CONFIG_RFS_ACCEL
6138 static struct bnxt_l2_filter *
6139 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6140 {
6141 	struct bnxt_l2_filter *fltr;
6142 	u32 idx;
6143 
6144 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6145 	      BNXT_L2_FLTR_HASH_MASK;
6146 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6147 	return fltr;
6148 }
6149 #endif
6150 
6151 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6152 			       struct bnxt_l2_key *key, u32 idx)
6153 {
6154 	struct hlist_head *head;
6155 
6156 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6157 	fltr->l2_key.vlan = key->vlan;
6158 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6159 	if (fltr->base.flags) {
6160 		int bit_id;
6161 
6162 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6163 						 bp->max_fltr, 0);
6164 		if (bit_id < 0)
6165 			return -ENOMEM;
6166 		fltr->base.sw_id = (u16)bit_id;
6167 		bp->ntp_fltr_count++;
6168 	}
6169 	head = &bp->l2_fltr_hash_tbl[idx];
6170 	hlist_add_head_rcu(&fltr->base.hash, head);
6171 	bnxt_insert_usr_fltr(bp, &fltr->base);
6172 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6173 	atomic_set(&fltr->refcnt, 1);
6174 	return 0;
6175 }
6176 
6177 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6178 						   struct bnxt_l2_key *key,
6179 						   gfp_t gfp)
6180 {
6181 	struct bnxt_l2_filter *fltr;
6182 	u32 idx;
6183 	int rc;
6184 
6185 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6186 	      BNXT_L2_FLTR_HASH_MASK;
6187 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6188 	if (fltr)
6189 		return fltr;
6190 
6191 	fltr = kzalloc_obj(*fltr, gfp);
6192 	if (!fltr)
6193 		return ERR_PTR(-ENOMEM);
6194 	spin_lock_bh(&bp->ntp_fltr_lock);
6195 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6196 	spin_unlock_bh(&bp->ntp_fltr_lock);
6197 	if (rc) {
6198 		bnxt_del_l2_filter(bp, fltr);
6199 		fltr = ERR_PTR(rc);
6200 	}
6201 	return fltr;
6202 }
6203 
6204 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6205 						struct bnxt_l2_key *key,
6206 						u16 flags)
6207 {
6208 	struct bnxt_l2_filter *fltr;
6209 	u32 idx;
6210 	int rc;
6211 
6212 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6213 	      BNXT_L2_FLTR_HASH_MASK;
6214 	spin_lock_bh(&bp->ntp_fltr_lock);
6215 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6216 	if (fltr) {
6217 		fltr = ERR_PTR(-EEXIST);
6218 		goto l2_filter_exit;
6219 	}
6220 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6221 	if (!fltr) {
6222 		fltr = ERR_PTR(-ENOMEM);
6223 		goto l2_filter_exit;
6224 	}
6225 	fltr->base.flags = flags;
6226 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6227 	if (rc) {
6228 		spin_unlock_bh(&bp->ntp_fltr_lock);
6229 		bnxt_del_l2_filter(bp, fltr);
6230 		return ERR_PTR(rc);
6231 	}
6232 
6233 l2_filter_exit:
6234 	spin_unlock_bh(&bp->ntp_fltr_lock);
6235 	return fltr;
6236 }
6237 
6238 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6239 {
6240 #ifdef CONFIG_BNXT_SRIOV
6241 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6242 
6243 	return vf->fw_fid;
6244 #else
6245 	return INVALID_HW_RING_ID;
6246 #endif
6247 }
6248 
6249 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6250 {
6251 	struct hwrm_cfa_l2_filter_free_input *req;
6252 	u16 target_id = 0xffff;
6253 	int rc;
6254 
6255 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6256 		struct bnxt_pf_info *pf = &bp->pf;
6257 
6258 		if (fltr->base.vf_idx >= pf->active_vfs)
6259 			return -EINVAL;
6260 
6261 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6262 		if (target_id == INVALID_HW_RING_ID)
6263 			return -EINVAL;
6264 	}
6265 
6266 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6267 	if (rc)
6268 		return rc;
6269 
6270 	req->target_id = cpu_to_le16(target_id);
6271 	req->l2_filter_id = fltr->base.filter_id;
6272 	return hwrm_req_send(bp, req);
6273 }
6274 
6275 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6276 {
6277 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6278 	struct hwrm_cfa_l2_filter_alloc_input *req;
6279 	u16 target_id = 0xffff;
6280 	int rc;
6281 
6282 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6283 		struct bnxt_pf_info *pf = &bp->pf;
6284 
6285 		if (fltr->base.vf_idx >= pf->active_vfs)
6286 			return -EINVAL;
6287 
6288 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6289 	}
6290 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6291 	if (rc)
6292 		return rc;
6293 
6294 	req->target_id = cpu_to_le16(target_id);
6295 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6296 
6297 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6298 		req->flags |=
6299 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6300 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6301 	req->enables =
6302 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6303 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6304 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6305 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6306 	eth_broadcast_addr(req->l2_addr_mask);
6307 
6308 	if (fltr->l2_key.vlan) {
6309 		req->enables |=
6310 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6311 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6312 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6313 		req->num_vlans = 1;
6314 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6315 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6316 	}
6317 
6318 	resp = hwrm_req_hold(bp, req);
6319 	rc = hwrm_req_send(bp, req);
6320 	if (!rc) {
6321 		fltr->base.filter_id = resp->l2_filter_id;
6322 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6323 	}
6324 	hwrm_req_drop(bp, req);
6325 	return rc;
6326 }
6327 
6328 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6329 				     struct bnxt_ntuple_filter *fltr)
6330 {
6331 	struct hwrm_cfa_ntuple_filter_free_input *req;
6332 	int rc;
6333 
6334 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6335 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6336 		return 0;
6337 
6338 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6339 	if (rc)
6340 		return rc;
6341 
6342 	req->ntuple_filter_id = fltr->base.filter_id;
6343 	return hwrm_req_send(bp, req);
6344 }
6345 
6346 #define BNXT_NTP_FLTR_FLAGS					\
6347 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6348 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6349 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6350 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6351 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6352 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6353 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6354 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6355 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6356 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6357 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6358 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6359 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6360 
6361 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6362 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6363 
6364 void bnxt_fill_ipv6_mask(__be32 mask[4])
6365 {
6366 	int i;
6367 
6368 	for (i = 0; i < 4; i++)
6369 		mask[i] = cpu_to_be32(~0);
6370 }
6371 
6372 static void
6373 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6374 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6375 			  struct bnxt_ntuple_filter *fltr)
6376 {
6377 	u16 rxq = fltr->base.rxq;
6378 
6379 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6380 		struct ethtool_rxfh_context *ctx;
6381 		struct bnxt_rss_ctx *rss_ctx;
6382 		struct bnxt_vnic_info *vnic;
6383 
6384 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6385 			      fltr->base.fw_vnic_id);
6386 		if (ctx) {
6387 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6388 			vnic = &rss_ctx->vnic;
6389 
6390 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6391 		}
6392 		return;
6393 	}
6394 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6395 		struct bnxt_vnic_info *vnic;
6396 		u32 enables;
6397 
6398 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6399 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6400 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6401 		req->enables |= cpu_to_le32(enables);
6402 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6403 	} else {
6404 		u32 flags;
6405 
6406 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6407 		req->flags |= cpu_to_le32(flags);
6408 		req->dst_id = cpu_to_le16(rxq);
6409 	}
6410 }
6411 
6412 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6413 				      struct bnxt_ntuple_filter *fltr)
6414 {
6415 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6416 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6417 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6418 	struct flow_keys *keys = &fltr->fkeys;
6419 	struct bnxt_l2_filter *l2_fltr;
6420 	struct bnxt_vnic_info *vnic;
6421 	int rc;
6422 
6423 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6424 	if (rc)
6425 		return rc;
6426 
6427 	l2_fltr = fltr->l2_fltr;
6428 	req->l2_filter_id = l2_fltr->base.filter_id;
6429 
6430 	if (fltr->base.flags & BNXT_ACT_DROP) {
6431 		req->flags =
6432 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6433 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6434 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6435 	} else {
6436 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6437 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6438 	}
6439 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6440 
6441 	req->ethertype = htons(ETH_P_IP);
6442 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6443 	req->ip_protocol = keys->basic.ip_proto;
6444 
6445 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6446 		req->ethertype = htons(ETH_P_IPV6);
6447 		req->ip_addr_type =
6448 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6449 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6450 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6451 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6452 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6453 	} else {
6454 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6455 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6456 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6457 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6458 	}
6459 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6460 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6461 		req->tunnel_type =
6462 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6463 	}
6464 
6465 	req->src_port = keys->ports.src;
6466 	req->src_port_mask = masks->ports.src;
6467 	req->dst_port = keys->ports.dst;
6468 	req->dst_port_mask = masks->ports.dst;
6469 
6470 	resp = hwrm_req_hold(bp, req);
6471 	rc = hwrm_req_send(bp, req);
6472 	if (!rc)
6473 		fltr->base.filter_id = resp->ntuple_filter_id;
6474 	hwrm_req_drop(bp, req);
6475 	return rc;
6476 }
6477 
6478 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6479 				     const u8 *mac_addr)
6480 {
6481 	struct bnxt_l2_filter *fltr;
6482 	struct bnxt_l2_key key;
6483 	int rc;
6484 
6485 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6486 	key.vlan = 0;
6487 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6488 	if (IS_ERR(fltr))
6489 		return PTR_ERR(fltr);
6490 
6491 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6492 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6493 	if (rc)
6494 		bnxt_del_l2_filter(bp, fltr);
6495 	else
6496 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6497 	return rc;
6498 }
6499 
6500 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6501 {
6502 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6503 
6504 	/* Any associated ntuple filters will also be cleared by firmware. */
6505 	for (i = 0; i < num_of_vnics; i++) {
6506 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6507 
6508 		for (j = 0; j < vnic->uc_filter_count; j++) {
6509 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6510 
6511 			bnxt_hwrm_l2_filter_free(bp, fltr);
6512 			bnxt_del_l2_filter(bp, fltr);
6513 		}
6514 		vnic->uc_filter_count = 0;
6515 	}
6516 }
6517 
6518 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6519 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6520 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6521 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6522 
6523 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6524 					   struct hwrm_vnic_tpa_cfg_input *req)
6525 {
6526 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6527 
6528 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6529 		return;
6530 
6531 	if (bp->vxlan_port)
6532 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6533 	if (bp->vxlan_gpe_port)
6534 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6535 	if (bp->nge_port)
6536 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6537 
6538 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6539 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6540 }
6541 
6542 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6543 			   u32 tpa_flags)
6544 {
6545 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6546 	struct hwrm_vnic_tpa_cfg_input *req;
6547 	int rc;
6548 
6549 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6550 		return 0;
6551 
6552 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6553 	if (rc)
6554 		return rc;
6555 
6556 	if (tpa_flags) {
6557 		u16 mss = bp->dev->mtu - 40;
6558 		u32 nsegs, n, segs = 0, flags;
6559 
6560 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6561 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6562 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6563 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6564 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6565 		if (tpa_flags & BNXT_FLAG_GRO)
6566 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6567 
6568 		req->flags = cpu_to_le32(flags);
6569 
6570 		req->enables =
6571 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6572 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6573 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6574 
6575 		/* Number of segs are log2 units, and first packet is not
6576 		 * included as part of this units.
6577 		 */
6578 		if (mss <= BNXT_RX_PAGE_SIZE) {
6579 			n = BNXT_RX_PAGE_SIZE / mss;
6580 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6581 		} else {
6582 			n = mss / BNXT_RX_PAGE_SIZE;
6583 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6584 				n++;
6585 			nsegs = (MAX_SKB_FRAGS - n) / n;
6586 		}
6587 
6588 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6589 			segs = MAX_TPA_SEGS_P5;
6590 			max_aggs = bp->max_tpa;
6591 		} else {
6592 			segs = ilog2(nsegs);
6593 		}
6594 		req->max_agg_segs = cpu_to_le16(segs);
6595 		req->max_aggs = cpu_to_le16(max_aggs);
6596 
6597 		req->min_agg_len = cpu_to_le32(512);
6598 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6599 	}
6600 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6601 
6602 	return hwrm_req_send(bp, req);
6603 }
6604 
6605 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6606 {
6607 	struct bnxt_ring_grp_info *grp_info;
6608 
6609 	grp_info = &bp->grp_info[ring->grp_idx];
6610 	return grp_info->cp_fw_ring_id;
6611 }
6612 
6613 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6614 {
6615 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6616 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6617 	else
6618 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6619 }
6620 
6621 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6622 {
6623 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6624 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6625 	else
6626 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6627 }
6628 
6629 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6630 {
6631 	int entries;
6632 
6633 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6634 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6635 	else
6636 		entries = HW_HASH_INDEX_SIZE;
6637 
6638 	bp->rss_indir_tbl_entries = entries;
6639 	bp->rss_indir_tbl =
6640 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6641 	if (!bp->rss_indir_tbl)
6642 		return -ENOMEM;
6643 
6644 	return 0;
6645 }
6646 
6647 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6648 				 struct ethtool_rxfh_context *rss_ctx)
6649 {
6650 	u16 max_rings, max_entries, pad, i;
6651 	u32 *rss_indir_tbl;
6652 
6653 	if (!bp->rx_nr_rings)
6654 		return;
6655 
6656 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6657 		max_rings = bp->rx_nr_rings - 1;
6658 	else
6659 		max_rings = bp->rx_nr_rings;
6660 
6661 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6662 	if (rss_ctx)
6663 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6664 	else
6665 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6666 
6667 	for (i = 0; i < max_entries; i++)
6668 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6669 
6670 	pad = bp->rss_indir_tbl_entries - max_entries;
6671 	if (pad)
6672 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6673 }
6674 
6675 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6676 {
6677 	u32 i, tbl_size, max_ring = 0;
6678 
6679 	if (!bp->rss_indir_tbl)
6680 		return 0;
6681 
6682 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6683 	for (i = 0; i < tbl_size; i++)
6684 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6685 	return max_ring;
6686 }
6687 
6688 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6689 {
6690 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6691 		if (!rx_rings)
6692 			return 0;
6693 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6694 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6695 
6696 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6697 					       BNXT_RSS_TABLE_ENTRIES_P5);
6698 	}
6699 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6700 		return 2;
6701 	return 1;
6702 }
6703 
6704 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6705 {
6706 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6707 	u16 i, j;
6708 
6709 	/* Fill the RSS indirection table with ring group ids */
6710 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6711 		if (!no_rss)
6712 			j = bp->rss_indir_tbl[i];
6713 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6714 	}
6715 }
6716 
6717 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6718 				    struct bnxt_vnic_info *vnic)
6719 {
6720 	__le16 *ring_tbl = vnic->rss_table;
6721 	struct bnxt_rx_ring_info *rxr;
6722 	u16 tbl_size, i;
6723 
6724 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6725 
6726 	for (i = 0; i < tbl_size; i++) {
6727 		u16 ring_id, j;
6728 
6729 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6730 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6731 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6732 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6733 		else
6734 			j = bp->rss_indir_tbl[i];
6735 		rxr = &bp->rx_ring[j];
6736 
6737 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6738 		*ring_tbl++ = cpu_to_le16(ring_id);
6739 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6740 		*ring_tbl++ = cpu_to_le16(ring_id);
6741 	}
6742 }
6743 
6744 static void
6745 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6746 			 struct bnxt_vnic_info *vnic)
6747 {
6748 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6749 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6750 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6751 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6752 	} else {
6753 		bnxt_fill_hw_rss_tbl(bp, vnic);
6754 	}
6755 
6756 	if (bp->rss_hash_delta) {
6757 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6758 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6759 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6760 		else
6761 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6762 	} else {
6763 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6764 	}
6765 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6766 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6767 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6768 }
6769 
6770 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6771 				  bool set_rss)
6772 {
6773 	struct hwrm_vnic_rss_cfg_input *req;
6774 	int rc;
6775 
6776 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6777 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6778 		return 0;
6779 
6780 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6781 	if (rc)
6782 		return rc;
6783 
6784 	if (set_rss)
6785 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6786 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6787 	return hwrm_req_send(bp, req);
6788 }
6789 
6790 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6791 				     struct bnxt_vnic_info *vnic, bool set_rss)
6792 {
6793 	struct hwrm_vnic_rss_cfg_input *req;
6794 	dma_addr_t ring_tbl_map;
6795 	u32 i, nr_ctxs;
6796 	int rc;
6797 
6798 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6799 	if (rc)
6800 		return rc;
6801 
6802 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6803 	if (!set_rss)
6804 		return hwrm_req_send(bp, req);
6805 
6806 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6807 	ring_tbl_map = vnic->rss_table_dma_addr;
6808 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6809 
6810 	hwrm_req_hold(bp, req);
6811 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6812 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6813 		req->ring_table_pair_index = i;
6814 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6815 		rc = hwrm_req_send(bp, req);
6816 		if (rc)
6817 			goto exit;
6818 	}
6819 
6820 exit:
6821 	hwrm_req_drop(bp, req);
6822 	return rc;
6823 }
6824 
6825 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6826 {
6827 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6828 	struct hwrm_vnic_rss_qcfg_output *resp;
6829 	struct hwrm_vnic_rss_qcfg_input *req;
6830 
6831 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6832 		return;
6833 
6834 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6835 	/* all contexts configured to same hash_type, zero always exists */
6836 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6837 	resp = hwrm_req_hold(bp, req);
6838 	if (!hwrm_req_send(bp, req)) {
6839 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6840 		bp->rss_hash_delta = 0;
6841 	}
6842 	hwrm_req_drop(bp, req);
6843 }
6844 
6845 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6846 {
6847 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6848 	struct hwrm_vnic_plcmodes_cfg_input *req;
6849 	int rc;
6850 
6851 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6852 	if (rc)
6853 		return rc;
6854 
6855 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6856 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6857 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6858 
6859 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6860 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6861 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6862 		req->enables |=
6863 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6864 		req->hds_threshold = cpu_to_le16(hds_thresh);
6865 	}
6866 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6867 	return hwrm_req_send(bp, req);
6868 }
6869 
6870 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6871 					struct bnxt_vnic_info *vnic,
6872 					u16 ctx_idx)
6873 {
6874 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6875 
6876 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6877 		return;
6878 
6879 	req->rss_cos_lb_ctx_id =
6880 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6881 
6882 	hwrm_req_send(bp, req);
6883 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6884 }
6885 
6886 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6887 {
6888 	int i, j;
6889 
6890 	for (i = 0; i < bp->nr_vnics; i++) {
6891 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6892 
6893 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6894 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6895 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6896 		}
6897 	}
6898 	bp->rsscos_nr_ctxs = 0;
6899 }
6900 
6901 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6902 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6903 {
6904 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6905 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6906 	int rc;
6907 
6908 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6909 	if (rc)
6910 		return rc;
6911 
6912 	resp = hwrm_req_hold(bp, req);
6913 	rc = hwrm_req_send(bp, req);
6914 	if (!rc)
6915 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6916 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6917 	hwrm_req_drop(bp, req);
6918 
6919 	return rc;
6920 }
6921 
6922 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6923 {
6924 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6925 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6926 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6927 }
6928 
6929 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6930 {
6931 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6932 	struct hwrm_vnic_cfg_input *req;
6933 	unsigned int ring = 0, grp_idx;
6934 	u16 def_vlan = 0;
6935 	int rc;
6936 
6937 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6938 	if (rc)
6939 		return rc;
6940 
6941 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6942 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6943 
6944 		req->default_rx_ring_id =
6945 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6946 		req->default_cmpl_ring_id =
6947 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6948 		req->enables =
6949 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6950 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6951 		goto vnic_mru;
6952 	}
6953 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6954 	/* Only RSS support for now TBD: COS & LB */
6955 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6956 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6957 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6958 					   VNIC_CFG_REQ_ENABLES_MRU);
6959 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6960 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6961 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6962 					   VNIC_CFG_REQ_ENABLES_MRU);
6963 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6964 	} else {
6965 		req->rss_rule = cpu_to_le16(0xffff);
6966 	}
6967 
6968 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6969 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6970 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6971 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6972 	} else {
6973 		req->cos_rule = cpu_to_le16(0xffff);
6974 	}
6975 
6976 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6977 		ring = 0;
6978 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6979 		ring = vnic->vnic_id - 1;
6980 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6981 		ring = bp->rx_nr_rings - 1;
6982 
6983 	grp_idx = bp->rx_ring[ring].bnapi->index;
6984 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6985 	req->lb_rule = cpu_to_le16(0xffff);
6986 vnic_mru:
6987 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
6988 	req->mru = cpu_to_le16(vnic->mru);
6989 
6990 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6991 #ifdef CONFIG_BNXT_SRIOV
6992 	if (BNXT_VF(bp))
6993 		def_vlan = bp->vf.vlan;
6994 #endif
6995 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6996 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6997 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT &&
6998 	    bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA]))
6999 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
7000 
7001 	return hwrm_req_send(bp, req);
7002 }
7003 
7004 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
7005 				    struct bnxt_vnic_info *vnic)
7006 {
7007 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
7008 		struct hwrm_vnic_free_input *req;
7009 
7010 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
7011 			return;
7012 
7013 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
7014 
7015 		hwrm_req_send(bp, req);
7016 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
7017 	}
7018 }
7019 
7020 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
7021 {
7022 	u16 i;
7023 
7024 	for (i = 0; i < bp->nr_vnics; i++)
7025 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
7026 }
7027 
7028 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
7029 			 unsigned int start_rx_ring_idx,
7030 			 unsigned int nr_rings)
7031 {
7032 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
7033 	struct hwrm_vnic_alloc_output *resp;
7034 	struct hwrm_vnic_alloc_input *req;
7035 	int rc;
7036 
7037 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
7038 	if (rc)
7039 		return rc;
7040 
7041 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7042 		goto vnic_no_ring_grps;
7043 
7044 	/* map ring groups to this vnic */
7045 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
7046 		grp_idx = bp->rx_ring[i].bnapi->index;
7047 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
7048 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
7049 				   j, nr_rings);
7050 			break;
7051 		}
7052 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
7053 	}
7054 
7055 vnic_no_ring_grps:
7056 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
7057 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
7058 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
7059 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
7060 
7061 	resp = hwrm_req_hold(bp, req);
7062 	rc = hwrm_req_send(bp, req);
7063 	if (!rc)
7064 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
7065 	hwrm_req_drop(bp, req);
7066 	return rc;
7067 }
7068 
7069 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
7070 {
7071 	struct hwrm_vnic_qcaps_output *resp;
7072 	struct hwrm_vnic_qcaps_input *req;
7073 	int rc;
7074 
7075 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
7076 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
7077 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
7078 	if (bp->hwrm_spec_code < 0x10600)
7079 		return 0;
7080 
7081 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
7082 	if (rc)
7083 		return rc;
7084 
7085 	resp = hwrm_req_hold(bp, req);
7086 	rc = hwrm_req_send(bp, req);
7087 	if (!rc) {
7088 		u32 flags = le32_to_cpu(resp->flags);
7089 
7090 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
7091 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
7092 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
7093 		if (flags &
7094 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
7095 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7096 
7097 		/* Older P5 fw before EXT_HW_STATS support did not set
7098 		 * VLAN_STRIP_CAP properly.
7099 		 */
7100 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7101 		    (BNXT_CHIP_P5(bp) &&
7102 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7103 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7104 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7105 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7106 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7107 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7108 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7109 		if (bp->max_tpa_v2) {
7110 			if (BNXT_CHIP_P5(bp))
7111 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7112 			else
7113 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7114 		}
7115 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7116 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7117 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7118 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7119 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7120 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7121 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7122 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7123 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7124 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7125 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7126 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7127 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7128 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7129 	}
7130 	hwrm_req_drop(bp, req);
7131 	return rc;
7132 }
7133 
7134 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7135 {
7136 	struct hwrm_ring_grp_alloc_output *resp;
7137 	struct hwrm_ring_grp_alloc_input *req;
7138 	int rc;
7139 	u16 i;
7140 
7141 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7142 		return 0;
7143 
7144 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7145 	if (rc)
7146 		return rc;
7147 
7148 	resp = hwrm_req_hold(bp, req);
7149 	for (i = 0; i < bp->rx_nr_rings; i++) {
7150 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7151 
7152 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7153 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7154 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7155 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7156 
7157 		rc = hwrm_req_send(bp, req);
7158 
7159 		if (rc)
7160 			break;
7161 
7162 		bp->grp_info[grp_idx].fw_grp_id =
7163 			le32_to_cpu(resp->ring_group_id);
7164 	}
7165 	hwrm_req_drop(bp, req);
7166 	return rc;
7167 }
7168 
7169 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7170 {
7171 	struct hwrm_ring_grp_free_input *req;
7172 	u16 i;
7173 
7174 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7175 		return;
7176 
7177 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7178 		return;
7179 
7180 	hwrm_req_hold(bp, req);
7181 	for (i = 0; i < bp->cp_nr_rings; i++) {
7182 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7183 			continue;
7184 		req->ring_group_id =
7185 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7186 
7187 		hwrm_req_send(bp, req);
7188 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7189 	}
7190 	hwrm_req_drop(bp, req);
7191 }
7192 
7193 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7194 				       struct hwrm_ring_alloc_input *req,
7195 				       struct bnxt_rx_ring_info *rxr,
7196 				       struct bnxt_ring_struct *ring)
7197 {
7198 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7199 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7200 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7201 
7202 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7203 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7204 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7205 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7206 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7207 	} else {
7208 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7209 		if (NET_IP_ALIGN == 2)
7210 			req->flags =
7211 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7212 	}
7213 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7214 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7215 	req->enables |= cpu_to_le32(enables);
7216 }
7217 
7218 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7219 				    struct bnxt_rx_ring_info *rxr,
7220 				    struct bnxt_ring_struct *ring,
7221 				    u32 ring_type, u32 map_index)
7222 {
7223 	struct hwrm_ring_alloc_output *resp;
7224 	struct hwrm_ring_alloc_input *req;
7225 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7226 	struct bnxt_ring_grp_info *grp_info;
7227 	int rc, err = 0;
7228 	u16 ring_id;
7229 
7230 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7231 	if (rc)
7232 		goto exit;
7233 
7234 	req->enables = 0;
7235 	if (rmem->nr_pages > 1) {
7236 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7237 		/* Page size is in log2 units */
7238 		req->page_size = BNXT_PAGE_SHIFT;
7239 		req->page_tbl_depth = 1;
7240 	} else {
7241 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7242 	}
7243 	req->fbo = 0;
7244 	/* Association of ring index with doorbell index and MSIX number */
7245 	req->logical_id = cpu_to_le16(map_index);
7246 
7247 	switch (ring_type) {
7248 	case HWRM_RING_ALLOC_TX: {
7249 		struct bnxt_tx_ring_info *txr;
7250 		u16 flags = 0;
7251 
7252 		txr = container_of(ring, struct bnxt_tx_ring_info,
7253 				   tx_ring_struct);
7254 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7255 		/* Association of transmit ring with completion ring */
7256 		grp_info = &bp->grp_info[ring->grp_idx];
7257 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7258 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7259 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7260 		req->queue_id = cpu_to_le16(ring->queue_id);
7261 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7262 			req->cmpl_coal_cnt =
7263 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7264 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7265 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7266 		req->flags = cpu_to_le16(flags);
7267 		break;
7268 	}
7269 	case HWRM_RING_ALLOC_RX:
7270 	case HWRM_RING_ALLOC_AGG:
7271 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7272 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7273 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7274 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7275 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7276 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7277 						   rxr, ring);
7278 		break;
7279 	case HWRM_RING_ALLOC_CMPL:
7280 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7281 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7282 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7283 			/* Association of cp ring with nq */
7284 			grp_info = &bp->grp_info[map_index];
7285 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7286 			req->cq_handle = cpu_to_le64(ring->handle);
7287 			req->enables |= cpu_to_le32(
7288 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7289 		} else {
7290 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7291 		}
7292 		break;
7293 	case HWRM_RING_ALLOC_NQ:
7294 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7295 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7296 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7297 		break;
7298 	default:
7299 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7300 			   ring_type);
7301 		return -EINVAL;
7302 	}
7303 
7304 	resp = hwrm_req_hold(bp, req);
7305 	rc = hwrm_req_send(bp, req);
7306 	err = le16_to_cpu(resp->error_code);
7307 	ring_id = le16_to_cpu(resp->ring_id);
7308 	hwrm_req_drop(bp, req);
7309 
7310 exit:
7311 	if (rc || err) {
7312 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7313 			   ring_type, rc, err);
7314 		return -EIO;
7315 	}
7316 	ring->fw_ring_id = ring_id;
7317 	return rc;
7318 }
7319 
7320 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7321 {
7322 	int rc;
7323 
7324 	if (BNXT_PF(bp)) {
7325 		struct hwrm_func_cfg_input *req;
7326 
7327 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7328 		if (rc)
7329 			return rc;
7330 
7331 		req->fid = cpu_to_le16(0xffff);
7332 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7333 		req->async_event_cr = cpu_to_le16(idx);
7334 		return hwrm_req_send(bp, req);
7335 	} else {
7336 		struct hwrm_func_vf_cfg_input *req;
7337 
7338 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7339 		if (rc)
7340 			return rc;
7341 
7342 		req->enables =
7343 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7344 		req->async_event_cr = cpu_to_le16(idx);
7345 		return hwrm_req_send(bp, req);
7346 	}
7347 }
7348 
7349 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7350 			     u32 ring_type)
7351 {
7352 	switch (ring_type) {
7353 	case HWRM_RING_ALLOC_TX:
7354 		db->db_ring_mask = bp->tx_ring_mask;
7355 		break;
7356 	case HWRM_RING_ALLOC_RX:
7357 		db->db_ring_mask = bp->rx_ring_mask;
7358 		break;
7359 	case HWRM_RING_ALLOC_AGG:
7360 		db->db_ring_mask = bp->rx_agg_ring_mask;
7361 		break;
7362 	case HWRM_RING_ALLOC_CMPL:
7363 	case HWRM_RING_ALLOC_NQ:
7364 		db->db_ring_mask = bp->cp_ring_mask;
7365 		break;
7366 	}
7367 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7368 		db->db_epoch_mask = db->db_ring_mask + 1;
7369 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7370 	}
7371 }
7372 
7373 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7374 			u32 map_idx, u32 xid)
7375 {
7376 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7377 		switch (ring_type) {
7378 		case HWRM_RING_ALLOC_TX:
7379 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7380 			break;
7381 		case HWRM_RING_ALLOC_RX:
7382 		case HWRM_RING_ALLOC_AGG:
7383 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7384 			break;
7385 		case HWRM_RING_ALLOC_CMPL:
7386 			db->db_key64 = DBR_PATH_L2;
7387 			break;
7388 		case HWRM_RING_ALLOC_NQ:
7389 			db->db_key64 = DBR_PATH_L2;
7390 			break;
7391 		}
7392 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7393 
7394 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7395 			db->db_key64 |= DBR_VALID;
7396 
7397 		db->doorbell = bp->bar1 + bp->db_offset;
7398 	} else {
7399 		db->doorbell = bp->bar1 + map_idx * 0x80;
7400 		switch (ring_type) {
7401 		case HWRM_RING_ALLOC_TX:
7402 			db->db_key32 = DB_KEY_TX;
7403 			break;
7404 		case HWRM_RING_ALLOC_RX:
7405 		case HWRM_RING_ALLOC_AGG:
7406 			db->db_key32 = DB_KEY_RX;
7407 			break;
7408 		case HWRM_RING_ALLOC_CMPL:
7409 			db->db_key32 = DB_KEY_CP;
7410 			break;
7411 		}
7412 	}
7413 	bnxt_set_db_mask(bp, db, ring_type);
7414 }
7415 
7416 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7417 				   struct bnxt_rx_ring_info *rxr)
7418 {
7419 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7420 	struct bnxt_napi *bnapi = rxr->bnapi;
7421 	u32 type = HWRM_RING_ALLOC_RX;
7422 	u32 map_idx = bnapi->index;
7423 	int rc;
7424 
7425 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7426 	if (rc)
7427 		return rc;
7428 
7429 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7430 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7431 
7432 	return 0;
7433 }
7434 
7435 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7436 				       struct bnxt_rx_ring_info *rxr)
7437 {
7438 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7439 	u32 type = HWRM_RING_ALLOC_AGG;
7440 	u32 grp_idx = ring->grp_idx;
7441 	u32 map_idx;
7442 	int rc;
7443 
7444 	map_idx = grp_idx + bp->rx_nr_rings;
7445 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7446 	if (rc)
7447 		return rc;
7448 
7449 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7450 		    ring->fw_ring_id);
7451 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7452 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7453 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7454 
7455 	return 0;
7456 }
7457 
7458 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7459 				      struct bnxt_cp_ring_info *cpr)
7460 {
7461 	const u32 type = HWRM_RING_ALLOC_CMPL;
7462 	struct bnxt_napi *bnapi = cpr->bnapi;
7463 	struct bnxt_ring_struct *ring;
7464 	u32 map_idx = bnapi->index;
7465 	int rc;
7466 
7467 	ring = &cpr->cp_ring_struct;
7468 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7469 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7470 	if (rc)
7471 		return rc;
7472 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7473 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7474 	return 0;
7475 }
7476 
7477 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7478 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7479 {
7480 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7481 	const u32 type = HWRM_RING_ALLOC_TX;
7482 	int rc;
7483 
7484 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7485 	if (rc)
7486 		return rc;
7487 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7488 	return 0;
7489 }
7490 
7491 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7492 {
7493 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7494 	int i, rc = 0;
7495 	u32 type;
7496 
7497 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7498 		type = HWRM_RING_ALLOC_NQ;
7499 	else
7500 		type = HWRM_RING_ALLOC_CMPL;
7501 	for (i = 0; i < bp->cp_nr_rings; i++) {
7502 		struct bnxt_napi *bnapi = bp->bnapi[i];
7503 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7504 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7505 		u32 map_idx = ring->map_idx;
7506 		unsigned int vector;
7507 
7508 		vector = bp->irq_tbl[map_idx].vector;
7509 		disable_irq_nosync(vector);
7510 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7511 		if (rc) {
7512 			enable_irq(vector);
7513 			goto err_out;
7514 		}
7515 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7516 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7517 		enable_irq(vector);
7518 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7519 
7520 		if (!i) {
7521 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7522 			if (rc)
7523 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7524 		}
7525 	}
7526 
7527 	for (i = 0; i < bp->tx_nr_rings; i++) {
7528 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7529 
7530 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7531 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7532 			if (rc)
7533 				goto err_out;
7534 		}
7535 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7536 		if (rc)
7537 			goto err_out;
7538 	}
7539 
7540 	for (i = 0; i < bp->rx_nr_rings; i++) {
7541 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7542 
7543 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7544 		if (rc)
7545 			goto err_out;
7546 		/* If we have agg rings, post agg buffers first. */
7547 		if (!agg_rings)
7548 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7549 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7550 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7551 			if (rc)
7552 				goto err_out;
7553 		}
7554 	}
7555 
7556 	if (agg_rings) {
7557 		for (i = 0; i < bp->rx_nr_rings; i++) {
7558 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7559 			if (rc)
7560 				goto err_out;
7561 		}
7562 	}
7563 err_out:
7564 	return rc;
7565 }
7566 
7567 static void bnxt_cancel_dim(struct bnxt *bp)
7568 {
7569 	int i;
7570 
7571 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7572 	 * if NAPI is enabled.
7573 	 */
7574 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7575 		return;
7576 
7577 	/* Make sure NAPI sees that the VNIC is disabled */
7578 	synchronize_net();
7579 	for (i = 0; i < bp->rx_nr_rings; i++) {
7580 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7581 		struct bnxt_napi *bnapi = rxr->bnapi;
7582 
7583 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7584 	}
7585 }
7586 
7587 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7588 				   struct bnxt_ring_struct *ring,
7589 				   u32 ring_type, int cmpl_ring_id)
7590 {
7591 	struct hwrm_ring_free_output *resp;
7592 	struct hwrm_ring_free_input *req;
7593 	u16 error_code = 0;
7594 	int rc;
7595 
7596 	if (BNXT_NO_FW_ACCESS(bp))
7597 		return 0;
7598 
7599 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7600 	if (rc)
7601 		goto exit;
7602 
7603 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7604 	req->ring_type = ring_type;
7605 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7606 
7607 	resp = hwrm_req_hold(bp, req);
7608 	rc = hwrm_req_send(bp, req);
7609 	error_code = le16_to_cpu(resp->error_code);
7610 	hwrm_req_drop(bp, req);
7611 exit:
7612 	if (rc || error_code) {
7613 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7614 			   ring_type, rc, error_code);
7615 		return -EIO;
7616 	}
7617 	return 0;
7618 }
7619 
7620 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7621 				   struct bnxt_tx_ring_info *txr,
7622 				   bool close_path)
7623 {
7624 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7625 	u32 cmpl_ring_id;
7626 
7627 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7628 		return;
7629 
7630 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7631 		       INVALID_HW_RING_ID;
7632 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7633 				cmpl_ring_id);
7634 	ring->fw_ring_id = INVALID_HW_RING_ID;
7635 }
7636 
7637 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7638 				   struct bnxt_rx_ring_info *rxr,
7639 				   bool close_path)
7640 {
7641 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7642 	u32 grp_idx = rxr->bnapi->index;
7643 	u32 cmpl_ring_id;
7644 
7645 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7646 		return;
7647 
7648 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7649 	hwrm_ring_free_send_msg(bp, ring,
7650 				RING_FREE_REQ_RING_TYPE_RX,
7651 				close_path ? cmpl_ring_id :
7652 				INVALID_HW_RING_ID);
7653 	ring->fw_ring_id = INVALID_HW_RING_ID;
7654 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7655 }
7656 
7657 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7658 				       struct bnxt_rx_ring_info *rxr,
7659 				       bool close_path)
7660 {
7661 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7662 	u32 grp_idx = rxr->bnapi->index;
7663 	u32 type, cmpl_ring_id;
7664 
7665 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7666 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7667 	else
7668 		type = RING_FREE_REQ_RING_TYPE_RX;
7669 
7670 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7671 		return;
7672 
7673 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7674 	hwrm_ring_free_send_msg(bp, ring, type,
7675 				close_path ? cmpl_ring_id :
7676 				INVALID_HW_RING_ID);
7677 	ring->fw_ring_id = INVALID_HW_RING_ID;
7678 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7679 }
7680 
7681 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7682 				   struct bnxt_cp_ring_info *cpr)
7683 {
7684 	struct bnxt_ring_struct *ring;
7685 
7686 	ring = &cpr->cp_ring_struct;
7687 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7688 		return;
7689 
7690 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7691 				INVALID_HW_RING_ID);
7692 	ring->fw_ring_id = INVALID_HW_RING_ID;
7693 }
7694 
7695 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7696 {
7697 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7698 	int i, size = ring->ring_mem.page_size;
7699 
7700 	cpr->cp_raw_cons = 0;
7701 	cpr->toggle = 0;
7702 
7703 	for (i = 0; i < bp->cp_nr_pages; i++)
7704 		if (cpr->cp_desc_ring[i])
7705 			memset(cpr->cp_desc_ring[i], 0, size);
7706 }
7707 
7708 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7709 {
7710 	u32 type;
7711 	int i;
7712 
7713 	if (!bp->bnapi)
7714 		return;
7715 
7716 	for (i = 0; i < bp->tx_nr_rings; i++)
7717 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7718 
7719 	bnxt_cancel_dim(bp);
7720 	for (i = 0; i < bp->rx_nr_rings; i++) {
7721 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7722 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7723 	}
7724 
7725 	/* The completion rings are about to be freed.  After that the
7726 	 * IRQ doorbell will not work anymore.  So we need to disable
7727 	 * IRQ here.
7728 	 */
7729 	bnxt_disable_int_sync(bp);
7730 
7731 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7732 		type = RING_FREE_REQ_RING_TYPE_NQ;
7733 	else
7734 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7735 	for (i = 0; i < bp->cp_nr_rings; i++) {
7736 		struct bnxt_napi *bnapi = bp->bnapi[i];
7737 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7738 		struct bnxt_ring_struct *ring;
7739 		int j;
7740 
7741 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7742 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7743 
7744 		ring = &cpr->cp_ring_struct;
7745 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7746 			hwrm_ring_free_send_msg(bp, ring, type,
7747 						INVALID_HW_RING_ID);
7748 			ring->fw_ring_id = INVALID_HW_RING_ID;
7749 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7750 		}
7751 	}
7752 }
7753 
7754 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7755 			     bool shared);
7756 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7757 			   bool shared);
7758 
7759 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7760 {
7761 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7762 	struct hwrm_func_qcfg_output *resp;
7763 	struct hwrm_func_qcfg_input *req;
7764 	int rc;
7765 
7766 	if (bp->hwrm_spec_code < 0x10601)
7767 		return 0;
7768 
7769 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7770 	if (rc)
7771 		return rc;
7772 
7773 	req->fid = cpu_to_le16(0xffff);
7774 	resp = hwrm_req_hold(bp, req);
7775 	rc = hwrm_req_send(bp, req);
7776 	if (rc) {
7777 		hwrm_req_drop(bp, req);
7778 		return rc;
7779 	}
7780 
7781 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7782 	if (BNXT_NEW_RM(bp)) {
7783 		u16 cp, stats;
7784 
7785 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7786 		hw_resc->resv_hw_ring_grps =
7787 			le32_to_cpu(resp->alloc_hw_ring_grps);
7788 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7789 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7790 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7791 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7792 		hw_resc->resv_irqs = cp;
7793 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7794 			int rx = hw_resc->resv_rx_rings;
7795 			int tx = hw_resc->resv_tx_rings;
7796 
7797 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7798 				rx >>= 1;
7799 			if (cp < (rx + tx)) {
7800 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7801 				if (rc)
7802 					goto get_rings_exit;
7803 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7804 					rx <<= 1;
7805 				hw_resc->resv_rx_rings = rx;
7806 				hw_resc->resv_tx_rings = tx;
7807 			}
7808 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7809 			hw_resc->resv_hw_ring_grps = rx;
7810 		}
7811 		hw_resc->resv_cp_rings = cp;
7812 		hw_resc->resv_stat_ctxs = stats;
7813 	}
7814 get_rings_exit:
7815 	hwrm_req_drop(bp, req);
7816 	return rc;
7817 }
7818 
7819 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7820 {
7821 	struct hwrm_func_qcfg_output *resp;
7822 	struct hwrm_func_qcfg_input *req;
7823 	int rc;
7824 
7825 	if (bp->hwrm_spec_code < 0x10601)
7826 		return 0;
7827 
7828 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7829 	if (rc)
7830 		return rc;
7831 
7832 	req->fid = cpu_to_le16(fid);
7833 	resp = hwrm_req_hold(bp, req);
7834 	rc = hwrm_req_send(bp, req);
7835 	if (!rc)
7836 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7837 
7838 	hwrm_req_drop(bp, req);
7839 	return rc;
7840 }
7841 
7842 static bool bnxt_rfs_supported(struct bnxt *bp);
7843 
7844 static struct hwrm_func_cfg_input *
7845 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7846 {
7847 	struct hwrm_func_cfg_input *req;
7848 	u32 enables = 0;
7849 
7850 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7851 		return NULL;
7852 
7853 	req->fid = cpu_to_le16(0xffff);
7854 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7855 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7856 	if (BNXT_NEW_RM(bp)) {
7857 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7858 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7859 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7860 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7861 			enables |= hwr->cp_p5 ?
7862 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7863 		} else {
7864 			enables |= hwr->cp ?
7865 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7866 			enables |= hwr->grp ?
7867 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7868 		}
7869 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7870 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7871 					  0;
7872 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7873 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7874 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7875 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7876 			req->num_msix = cpu_to_le16(hwr->cp);
7877 		} else {
7878 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7879 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7880 		}
7881 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7882 		req->num_vnics = cpu_to_le16(hwr->vnic);
7883 	}
7884 	req->enables = cpu_to_le32(enables);
7885 	return req;
7886 }
7887 
7888 static struct hwrm_func_vf_cfg_input *
7889 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7890 {
7891 	struct hwrm_func_vf_cfg_input *req;
7892 	u32 enables = 0;
7893 
7894 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7895 		return NULL;
7896 
7897 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7898 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7899 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7900 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7901 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7902 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7903 		enables |= hwr->cp_p5 ?
7904 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7905 	} else {
7906 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7907 		enables |= hwr->grp ?
7908 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7909 	}
7910 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7911 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7912 
7913 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7914 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7915 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7916 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7917 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7918 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7919 	} else {
7920 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7921 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7922 	}
7923 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7924 	req->num_vnics = cpu_to_le16(hwr->vnic);
7925 
7926 	req->enables = cpu_to_le32(enables);
7927 	return req;
7928 }
7929 
7930 static int
7931 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7932 {
7933 	struct hwrm_func_cfg_input *req;
7934 	int rc;
7935 
7936 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7937 	if (!req)
7938 		return -ENOMEM;
7939 
7940 	if (!req->enables) {
7941 		hwrm_req_drop(bp, req);
7942 		return 0;
7943 	}
7944 
7945 	rc = hwrm_req_send(bp, req);
7946 	if (rc)
7947 		return rc;
7948 
7949 	if (bp->hwrm_spec_code < 0x10601)
7950 		bp->hw_resc.resv_tx_rings = hwr->tx;
7951 
7952 	return bnxt_hwrm_get_rings(bp);
7953 }
7954 
7955 static int
7956 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7957 {
7958 	struct hwrm_func_vf_cfg_input *req;
7959 	int rc;
7960 
7961 	if (!BNXT_NEW_RM(bp)) {
7962 		bp->hw_resc.resv_tx_rings = hwr->tx;
7963 		return 0;
7964 	}
7965 
7966 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7967 	if (!req)
7968 		return -ENOMEM;
7969 
7970 	rc = hwrm_req_send(bp, req);
7971 	if (rc)
7972 		return rc;
7973 
7974 	return bnxt_hwrm_get_rings(bp);
7975 }
7976 
7977 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7978 {
7979 	if (BNXT_PF(bp))
7980 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7981 	else
7982 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7983 }
7984 
7985 int bnxt_nq_rings_in_use(struct bnxt *bp)
7986 {
7987 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7988 }
7989 
7990 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7991 {
7992 	int cp;
7993 
7994 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7995 		return bnxt_nq_rings_in_use(bp);
7996 
7997 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7998 	return cp;
7999 }
8000 
8001 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
8002 {
8003 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
8004 }
8005 
8006 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8007 {
8008 	if (!hwr->grp)
8009 		return 0;
8010 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8011 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
8012 
8013 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8014 			rss_ctx *= hwr->vnic;
8015 		return rss_ctx;
8016 	}
8017 	if (BNXT_VF(bp))
8018 		return BNXT_VF_MAX_RSS_CTX;
8019 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
8020 		return hwr->grp + 1;
8021 	return 1;
8022 }
8023 
8024 /* Check if a default RSS map needs to be setup.  This function is only
8025  * used on older firmware that does not require reserving RX rings.
8026  */
8027 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
8028 {
8029 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8030 
8031 	/* The RSS map is valid for RX rings set to resv_rx_rings */
8032 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
8033 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
8034 		if (!netif_is_rxfh_configured(bp->dev))
8035 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8036 	}
8037 }
8038 
8039 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
8040 {
8041 	if (bp->flags & BNXT_FLAG_RFS) {
8042 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8043 			return 2 + bp->num_rss_ctx;
8044 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8045 			return rx_rings + 1;
8046 	}
8047 	return 1;
8048 }
8049 
8050 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8051 {
8052 	hwr->cp = bnxt_nq_rings_in_use(bp);
8053 	hwr->cp_p5 = 0;
8054 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8055 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
8056 	hwr->tx = bp->tx_nr_rings;
8057 	hwr->rx = bp->rx_nr_rings;
8058 	hwr->grp = hwr->rx;
8059 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
8060 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
8061 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8062 		hwr->rx <<= 1;
8063 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
8064 }
8065 
8066 static bool bnxt_need_reserve_rings(struct bnxt *bp)
8067 {
8068 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8069 	struct bnxt_hw_rings hwr;
8070 
8071 	bnxt_get_total_resources(bp, &hwr);
8072 
8073 	/* Old firmware does not need RX ring reservations but we still
8074 	 * need to setup a default RSS map when needed.  With new firmware
8075 	 * we go through RX ring reservations first and then set up the
8076 	 * RSS map for the successfully reserved RX rings when needed.
8077 	 */
8078 	if (!BNXT_NEW_RM(bp))
8079 		bnxt_check_rss_tbl_no_rmgr(bp);
8080 
8081 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
8082 		return true;
8083 
8084 	if (!BNXT_NEW_RM(bp))
8085 		return false;
8086 
8087 	if (hw_resc->resv_rx_rings != hwr.rx ||
8088 	    hw_resc->resv_vnics != hwr.vnic ||
8089 	    hw_resc->resv_stat_ctxs != hwr.stat ||
8090 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
8091 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
8092 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
8093 		return true;
8094 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8095 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
8096 			return true;
8097 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
8098 		return true;
8099 	}
8100 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
8101 	    hw_resc->resv_irqs != hwr.cp)
8102 		return true;
8103 	return false;
8104 }
8105 
8106 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8107 {
8108 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8109 
8110 	hwr->tx = hw_resc->resv_tx_rings;
8111 	if (BNXT_NEW_RM(bp)) {
8112 		hwr->rx = hw_resc->resv_rx_rings;
8113 		hwr->cp = hw_resc->resv_irqs;
8114 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8115 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8116 		hwr->grp = hw_resc->resv_hw_ring_grps;
8117 		hwr->vnic = hw_resc->resv_vnics;
8118 		hwr->stat = hw_resc->resv_stat_ctxs;
8119 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8120 	}
8121 }
8122 
8123 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8124 {
8125 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8126 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8127 }
8128 
8129 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8130 
8131 static int __bnxt_reserve_rings(struct bnxt *bp)
8132 {
8133 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
8134 	struct bnxt_hw_rings hwr = {0};
8135 	int rx_rings, old_rx_rings, rc;
8136 	int cp = bp->cp_nr_rings;
8137 	int ulp_msix = 0;
8138 	bool sh = false;
8139 	int tx_cp;
8140 
8141 	if (!bnxt_need_reserve_rings(bp))
8142 		return 0;
8143 
8144 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
8145 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8146 		if (!ulp_msix)
8147 			bnxt_set_ulp_stat_ctxs(bp, 0);
8148 		else
8149 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8150 
8151 		if (ulp_msix > bp->ulp_num_msix_want)
8152 			ulp_msix = bp->ulp_num_msix_want;
8153 		hwr.cp = cp + ulp_msix;
8154 	} else {
8155 		hwr.cp = bnxt_nq_rings_in_use(bp);
8156 	}
8157 
8158 	hwr.tx = bp->tx_nr_rings;
8159 	hwr.rx = bp->rx_nr_rings;
8160 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8161 		sh = true;
8162 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8163 		hwr.cp_p5 = hwr.rx + hwr.tx;
8164 
8165 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8166 
8167 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8168 		hwr.rx <<= 1;
8169 	hwr.grp = bp->rx_nr_rings;
8170 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8171 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8172 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8173 
8174 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8175 	if (rc)
8176 		return rc;
8177 
8178 	bnxt_copy_reserved_rings(bp, &hwr);
8179 
8180 	rx_rings = hwr.rx;
8181 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8182 		if (hwr.rx >= 2) {
8183 			rx_rings = hwr.rx >> 1;
8184 		} else {
8185 			if (netif_running(bp->dev))
8186 				return -ENOMEM;
8187 
8188 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8189 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8190 			bp->dev->hw_features &= ~NETIF_F_LRO;
8191 			bp->dev->features &= ~NETIF_F_LRO;
8192 			bnxt_set_ring_params(bp);
8193 		}
8194 	}
8195 	rx_rings = min_t(int, rx_rings, hwr.grp);
8196 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8197 	if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8198 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8199 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8200 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8201 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8202 		hwr.rx = rx_rings << 1;
8203 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8204 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8205 	if (hwr.tx != bp->tx_nr_rings) {
8206 		netdev_warn(bp->dev,
8207 			    "Able to reserve only %d out of %d requested TX rings\n",
8208 			    hwr.tx, bp->tx_nr_rings);
8209 	}
8210 	bp->tx_nr_rings = hwr.tx;
8211 
8212 	/* If we cannot reserve all the RX rings, reset the RSS map only
8213 	 * if absolutely necessary
8214 	 */
8215 	if (rx_rings != bp->rx_nr_rings) {
8216 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8217 			    rx_rings, bp->rx_nr_rings);
8218 		if (netif_is_rxfh_configured(bp->dev) &&
8219 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8220 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8221 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8222 			ethtool_rxfh_indir_lost(bp->dev);
8223 		}
8224 	}
8225 	bp->rx_nr_rings = rx_rings;
8226 	bp->cp_nr_rings = hwr.cp;
8227 
8228 	/* Fall back if we cannot reserve enough HW RSS contexts */
8229 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8230 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8231 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8232 
8233 	if (!bnxt_rings_ok(bp, &hwr))
8234 		return -ENOMEM;
8235 
8236 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8237 	    !netif_is_rxfh_configured(bp->dev))
8238 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8239 
8240 	if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) {
8241 		int resv_msix, resv_ctx, ulp_ctxs;
8242 		struct bnxt_hw_resc *hw_resc;
8243 
8244 		hw_resc = &bp->hw_resc;
8245 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8246 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8247 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8248 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8249 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8250 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8251 	}
8252 
8253 	return rc;
8254 }
8255 
8256 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8257 {
8258 	struct hwrm_func_vf_cfg_input *req;
8259 	u32 flags;
8260 
8261 	if (!BNXT_NEW_RM(bp))
8262 		return 0;
8263 
8264 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8265 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8266 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8267 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8268 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8269 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8270 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8271 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8272 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8273 
8274 	req->flags = cpu_to_le32(flags);
8275 	return hwrm_req_send_silent(bp, req);
8276 }
8277 
8278 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8279 {
8280 	struct hwrm_func_cfg_input *req;
8281 	u32 flags;
8282 
8283 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8284 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8285 	if (BNXT_NEW_RM(bp)) {
8286 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8287 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8288 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8289 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8290 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8291 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8292 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8293 		else
8294 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8295 	}
8296 
8297 	req->flags = cpu_to_le32(flags);
8298 	return hwrm_req_send_silent(bp, req);
8299 }
8300 
8301 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8302 {
8303 	if (bp->hwrm_spec_code < 0x10801)
8304 		return 0;
8305 
8306 	if (BNXT_PF(bp))
8307 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8308 
8309 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8310 }
8311 
8312 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8313 {
8314 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8315 	struct hwrm_ring_aggint_qcaps_output *resp;
8316 	struct hwrm_ring_aggint_qcaps_input *req;
8317 	int rc;
8318 
8319 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8320 	coal_cap->num_cmpl_dma_aggr_max = 63;
8321 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8322 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8323 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8324 	coal_cap->int_lat_tmr_min_max = 65535;
8325 	coal_cap->int_lat_tmr_max_max = 65535;
8326 	coal_cap->num_cmpl_aggr_int_max = 65535;
8327 	coal_cap->timer_units = 80;
8328 
8329 	if (bp->hwrm_spec_code < 0x10902)
8330 		return;
8331 
8332 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8333 		return;
8334 
8335 	resp = hwrm_req_hold(bp, req);
8336 	rc = hwrm_req_send_silent(bp, req);
8337 	if (!rc) {
8338 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8339 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8340 		coal_cap->num_cmpl_dma_aggr_max =
8341 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8342 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8343 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8344 		coal_cap->cmpl_aggr_dma_tmr_max =
8345 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8346 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8347 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8348 		coal_cap->int_lat_tmr_min_max =
8349 			le16_to_cpu(resp->int_lat_tmr_min_max);
8350 		coal_cap->int_lat_tmr_max_max =
8351 			le16_to_cpu(resp->int_lat_tmr_max_max);
8352 		coal_cap->num_cmpl_aggr_int_max =
8353 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8354 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8355 	}
8356 	hwrm_req_drop(bp, req);
8357 }
8358 
8359 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8360 {
8361 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8362 
8363 	return usec * 1000 / coal_cap->timer_units;
8364 }
8365 
8366 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8367 	struct bnxt_coal *hw_coal,
8368 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8369 {
8370 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8371 	u16 val, tmr, max, flags = hw_coal->flags;
8372 	u32 cmpl_params = coal_cap->cmpl_params;
8373 
8374 	max = hw_coal->bufs_per_record * 128;
8375 	if (hw_coal->budget)
8376 		max = hw_coal->bufs_per_record * hw_coal->budget;
8377 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8378 
8379 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8380 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8381 
8382 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8383 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8384 
8385 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8386 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8387 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8388 
8389 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8390 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8391 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8392 
8393 	/* min timer set to 1/2 of interrupt timer */
8394 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8395 		val = tmr / 2;
8396 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8397 		req->int_lat_tmr_min = cpu_to_le16(val);
8398 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8399 	}
8400 
8401 	/* buf timer set to 1/4 of interrupt timer */
8402 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8403 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8404 
8405 	if (cmpl_params &
8406 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8407 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8408 		val = clamp_t(u16, tmr, 1,
8409 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8410 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8411 		req->enables |=
8412 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8413 	}
8414 
8415 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8416 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8417 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8418 	req->flags = cpu_to_le16(flags);
8419 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8420 }
8421 
8422 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8423 				   struct bnxt_coal *hw_coal)
8424 {
8425 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8426 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8427 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8428 	u32 nq_params = coal_cap->nq_params;
8429 	u16 tmr;
8430 	int rc;
8431 
8432 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8433 		return 0;
8434 
8435 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8436 	if (rc)
8437 		return rc;
8438 
8439 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8440 	req->flags =
8441 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8442 
8443 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8444 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8445 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8446 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8447 	return hwrm_req_send(bp, req);
8448 }
8449 
8450 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8451 {
8452 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8453 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8454 	struct bnxt_coal coal;
8455 	int rc;
8456 
8457 	/* Tick values in micro seconds.
8458 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8459 	 */
8460 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8461 
8462 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8463 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8464 
8465 	if (!bnapi->rx_ring)
8466 		return -ENODEV;
8467 
8468 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8469 	if (rc)
8470 		return rc;
8471 
8472 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8473 
8474 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8475 
8476 	return hwrm_req_send(bp, req_rx);
8477 }
8478 
8479 static int
8480 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8481 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8482 {
8483 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8484 
8485 	req->ring_id = cpu_to_le16(ring_id);
8486 	return hwrm_req_send(bp, req);
8487 }
8488 
8489 static int
8490 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8491 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8492 {
8493 	struct bnxt_tx_ring_info *txr;
8494 	int i, rc;
8495 
8496 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8497 		u16 ring_id;
8498 
8499 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8500 		req->ring_id = cpu_to_le16(ring_id);
8501 		rc = hwrm_req_send(bp, req);
8502 		if (rc)
8503 			return rc;
8504 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8505 			return 0;
8506 	}
8507 	return 0;
8508 }
8509 
8510 int bnxt_hwrm_set_coal(struct bnxt *bp)
8511 {
8512 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8513 	int i, rc;
8514 
8515 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8516 	if (rc)
8517 		return rc;
8518 
8519 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8520 	if (rc) {
8521 		hwrm_req_drop(bp, req_rx);
8522 		return rc;
8523 	}
8524 
8525 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8526 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8527 
8528 	hwrm_req_hold(bp, req_rx);
8529 	hwrm_req_hold(bp, req_tx);
8530 	for (i = 0; i < bp->cp_nr_rings; i++) {
8531 		struct bnxt_napi *bnapi = bp->bnapi[i];
8532 		struct bnxt_coal *hw_coal;
8533 
8534 		if (!bnapi->rx_ring)
8535 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8536 		else
8537 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8538 		if (rc)
8539 			break;
8540 
8541 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8542 			continue;
8543 
8544 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8545 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8546 			if (rc)
8547 				break;
8548 		}
8549 		if (bnapi->rx_ring)
8550 			hw_coal = &bp->rx_coal;
8551 		else
8552 			hw_coal = &bp->tx_coal;
8553 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8554 	}
8555 	hwrm_req_drop(bp, req_rx);
8556 	hwrm_req_drop(bp, req_tx);
8557 	return rc;
8558 }
8559 
8560 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8561 {
8562 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8563 	struct hwrm_stat_ctx_free_input *req;
8564 	int i;
8565 
8566 	if (!bp->bnapi)
8567 		return;
8568 
8569 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8570 		return;
8571 
8572 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8573 		return;
8574 	if (BNXT_FW_MAJ(bp) <= 20) {
8575 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8576 			hwrm_req_drop(bp, req);
8577 			return;
8578 		}
8579 		hwrm_req_hold(bp, req0);
8580 	}
8581 	hwrm_req_hold(bp, req);
8582 	for (i = 0; i < bp->cp_nr_rings; i++) {
8583 		struct bnxt_napi *bnapi = bp->bnapi[i];
8584 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8585 
8586 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8587 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8588 			if (req0) {
8589 				req0->stat_ctx_id = req->stat_ctx_id;
8590 				hwrm_req_send(bp, req0);
8591 			}
8592 			hwrm_req_send(bp, req);
8593 
8594 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8595 		}
8596 	}
8597 	hwrm_req_drop(bp, req);
8598 	if (req0)
8599 		hwrm_req_drop(bp, req0);
8600 }
8601 
8602 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8603 {
8604 	struct hwrm_stat_ctx_alloc_output *resp;
8605 	struct hwrm_stat_ctx_alloc_input *req;
8606 	int rc, i;
8607 
8608 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8609 		return 0;
8610 
8611 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8612 	if (rc)
8613 		return rc;
8614 
8615 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8616 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8617 
8618 	resp = hwrm_req_hold(bp, req);
8619 	for (i = 0; i < bp->cp_nr_rings; i++) {
8620 		struct bnxt_napi *bnapi = bp->bnapi[i];
8621 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8622 
8623 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8624 
8625 		rc = hwrm_req_send(bp, req);
8626 		if (rc)
8627 			break;
8628 
8629 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8630 
8631 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8632 	}
8633 	hwrm_req_drop(bp, req);
8634 	return rc;
8635 }
8636 
8637 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8638 {
8639 	struct hwrm_func_qcfg_output *resp;
8640 	struct hwrm_func_qcfg_input *req;
8641 	u16 flags;
8642 	int rc;
8643 
8644 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8645 	if (rc)
8646 		return rc;
8647 
8648 	req->fid = cpu_to_le16(0xffff);
8649 	resp = hwrm_req_hold(bp, req);
8650 	rc = hwrm_req_send(bp, req);
8651 	if (rc)
8652 		goto func_qcfg_exit;
8653 
8654 	flags = le16_to_cpu(resp->flags);
8655 #ifdef CONFIG_BNXT_SRIOV
8656 	if (BNXT_VF(bp)) {
8657 		struct bnxt_vf_info *vf = &bp->vf;
8658 
8659 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8660 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8661 			vf->flags |= BNXT_VF_TRUST;
8662 		else
8663 			vf->flags &= ~BNXT_VF_TRUST;
8664 	} else {
8665 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8666 	}
8667 #endif
8668 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8669 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8670 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8671 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8672 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8673 	}
8674 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8675 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8676 
8677 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8678 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8679 
8680 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8681 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8682 	if (resp->roce_bidi_opt_mode &
8683 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8684 		bp->cos0_cos1_shared = 1;
8685 	else
8686 		bp->cos0_cos1_shared = 0;
8687 
8688 	switch (resp->port_partition_type) {
8689 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8690 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8691 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8692 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8693 		bp->port_partition_type = resp->port_partition_type;
8694 		break;
8695 	}
8696 	if (bp->hwrm_spec_code < 0x10707 ||
8697 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8698 		bp->br_mode = BRIDGE_MODE_VEB;
8699 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8700 		bp->br_mode = BRIDGE_MODE_VEPA;
8701 	else
8702 		bp->br_mode = BRIDGE_MODE_UNDEF;
8703 
8704 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8705 	if (!bp->max_mtu)
8706 		bp->max_mtu = BNXT_MAX_MTU;
8707 
8708 	if (bp->db_size)
8709 		goto func_qcfg_exit;
8710 
8711 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8712 	if (BNXT_CHIP_P5(bp)) {
8713 		if (BNXT_PF(bp))
8714 			bp->db_offset = DB_PF_OFFSET_P5;
8715 		else
8716 			bp->db_offset = DB_VF_OFFSET_P5;
8717 	}
8718 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8719 				 1024);
8720 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8721 	    bp->db_size <= bp->db_offset)
8722 		bp->db_size = pci_resource_len(bp->pdev, 2);
8723 
8724 func_qcfg_exit:
8725 	hwrm_req_drop(bp, req);
8726 	return rc;
8727 }
8728 
8729 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8730 				      u8 init_val, u8 init_offset,
8731 				      bool init_mask_set)
8732 {
8733 	ctxm->init_value = init_val;
8734 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8735 	if (init_mask_set)
8736 		ctxm->init_offset = init_offset * 4;
8737 	else
8738 		ctxm->init_value = 0;
8739 }
8740 
8741 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8742 {
8743 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8744 	u16 type;
8745 
8746 	for (type = 0; type < ctx_max; type++) {
8747 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8748 		int n = 1;
8749 
8750 		if (!ctxm->max_entries || ctxm->pg_info)
8751 			continue;
8752 
8753 		if (ctxm->instance_bmap)
8754 			n = hweight32(ctxm->instance_bmap);
8755 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8756 		if (!ctxm->pg_info)
8757 			return -ENOMEM;
8758 	}
8759 	return 0;
8760 }
8761 
8762 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8763 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8764 
8765 #define BNXT_CTX_INIT_VALID(flags)	\
8766 	(!!((flags) &			\
8767 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8768 
8769 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8770 {
8771 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8772 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8773 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8774 	u16 type, next_type = 0;
8775 	int rc;
8776 
8777 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8778 	if (rc)
8779 		return rc;
8780 
8781 	if (!ctx) {
8782 		ctx = kzalloc_obj(*ctx);
8783 		if (!ctx)
8784 			return -ENOMEM;
8785 		bp->ctx = ctx;
8786 	}
8787 
8788 	resp = hwrm_req_hold(bp, req);
8789 
8790 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8791 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8792 		u8 init_val, init_off, i;
8793 		u32 max_entries;
8794 		u16 entry_size;
8795 		__le32 *p;
8796 		u32 flags;
8797 
8798 		req->type = cpu_to_le16(type);
8799 		rc = hwrm_req_send(bp, req);
8800 		if (rc)
8801 			goto ctx_done;
8802 		flags = le32_to_cpu(resp->flags);
8803 		next_type = le16_to_cpu(resp->next_valid_type);
8804 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8805 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8806 			continue;
8807 		}
8808 		entry_size = le16_to_cpu(resp->entry_size);
8809 		max_entries = le32_to_cpu(resp->max_num_entries);
8810 		if (ctxm->mem_valid) {
8811 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8812 			    ctxm->entry_size != entry_size ||
8813 			    ctxm->max_entries != max_entries)
8814 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8815 			else
8816 				continue;
8817 		}
8818 		ctxm->type = type;
8819 		ctxm->entry_size = entry_size;
8820 		ctxm->flags = flags;
8821 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8822 		ctxm->entry_multiple = resp->entry_multiple;
8823 		ctxm->max_entries = max_entries;
8824 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8825 		init_val = resp->ctx_init_value;
8826 		init_off = resp->ctx_init_offset;
8827 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8828 					  BNXT_CTX_INIT_VALID(flags));
8829 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8830 					      BNXT_MAX_SPLIT_ENTRY);
8831 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8832 		     i++, p++)
8833 			ctxm->split[i] = le32_to_cpu(*p);
8834 	}
8835 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8836 
8837 ctx_done:
8838 	hwrm_req_drop(bp, req);
8839 	return rc;
8840 }
8841 
8842 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8843 {
8844 	struct hwrm_func_backing_store_qcaps_output *resp;
8845 	struct hwrm_func_backing_store_qcaps_input *req;
8846 	int rc;
8847 
8848 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8849 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8850 		return 0;
8851 
8852 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8853 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8854 
8855 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8856 	if (rc)
8857 		return rc;
8858 
8859 	resp = hwrm_req_hold(bp, req);
8860 	rc = hwrm_req_send_silent(bp, req);
8861 	if (!rc) {
8862 		struct bnxt_ctx_mem_type *ctxm;
8863 		struct bnxt_ctx_mem_info *ctx;
8864 		u8 init_val, init_idx = 0;
8865 		u16 init_mask;
8866 
8867 		ctx = bp->ctx;
8868 		if (!ctx) {
8869 			ctx = kzalloc_obj(*ctx);
8870 			if (!ctx) {
8871 				rc = -ENOMEM;
8872 				goto ctx_err;
8873 			}
8874 			bp->ctx = ctx;
8875 		}
8876 		init_val = resp->ctx_kind_initializer;
8877 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8878 
8879 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8880 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8881 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8882 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8883 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8884 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8885 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8886 					  (init_mask & (1 << init_idx++)) != 0);
8887 
8888 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8889 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8890 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8891 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8892 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8893 					  (init_mask & (1 << init_idx++)) != 0);
8894 
8895 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8896 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8897 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8898 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8899 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8900 					  (init_mask & (1 << init_idx++)) != 0);
8901 
8902 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8903 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8904 		ctxm->max_entries = ctxm->vnic_entries +
8905 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8906 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8907 		bnxt_init_ctx_initializer(ctxm, init_val,
8908 					  resp->vnic_init_offset,
8909 					  (init_mask & (1 << init_idx++)) != 0);
8910 
8911 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8912 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8913 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8914 		bnxt_init_ctx_initializer(ctxm, init_val,
8915 					  resp->stat_init_offset,
8916 					  (init_mask & (1 << init_idx++)) != 0);
8917 
8918 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8919 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8920 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8921 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8922 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8923 		if (!ctxm->entry_multiple)
8924 			ctxm->entry_multiple = 1;
8925 
8926 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8927 
8928 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8929 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8930 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8931 		ctxm->mrav_num_entries_units =
8932 			le16_to_cpu(resp->mrav_num_entries_units);
8933 		bnxt_init_ctx_initializer(ctxm, init_val,
8934 					  resp->mrav_init_offset,
8935 					  (init_mask & (1 << init_idx++)) != 0);
8936 
8937 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8938 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8939 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8940 
8941 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8942 		if (!ctx->tqm_fp_rings_count)
8943 			ctx->tqm_fp_rings_count = bp->max_q;
8944 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8945 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8946 
8947 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8948 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8949 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8950 
8951 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8952 	} else {
8953 		rc = 0;
8954 	}
8955 ctx_err:
8956 	hwrm_req_drop(bp, req);
8957 	return rc;
8958 }
8959 
8960 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8961 				  __le64 *pg_dir)
8962 {
8963 	if (!rmem->nr_pages)
8964 		return;
8965 
8966 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8967 	if (rmem->depth >= 1) {
8968 		if (rmem->depth == 2)
8969 			*pg_attr |= 2;
8970 		else
8971 			*pg_attr |= 1;
8972 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8973 	} else {
8974 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8975 	}
8976 }
8977 
8978 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8979 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8980 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8981 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8982 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8983 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8984 
8985 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8986 {
8987 	struct hwrm_func_backing_store_cfg_input *req;
8988 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8989 	struct bnxt_ctx_pg_info *ctx_pg;
8990 	struct bnxt_ctx_mem_type *ctxm;
8991 	void **__req = (void **)&req;
8992 	u32 req_len = sizeof(*req);
8993 	__le32 *num_entries;
8994 	__le64 *pg_dir;
8995 	u32 flags = 0;
8996 	u8 *pg_attr;
8997 	u32 ena;
8998 	int rc;
8999 	int i;
9000 
9001 	if (!ctx)
9002 		return 0;
9003 
9004 	if (req_len > bp->hwrm_max_ext_req_len)
9005 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
9006 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
9007 	if (rc)
9008 		return rc;
9009 
9010 	req->enables = cpu_to_le32(enables);
9011 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
9012 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9013 		ctx_pg = ctxm->pg_info;
9014 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
9015 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
9016 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
9017 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
9018 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9019 				      &req->qpc_pg_size_qpc_lvl,
9020 				      &req->qpc_page_dir);
9021 
9022 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
9023 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
9024 	}
9025 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
9026 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9027 		ctx_pg = ctxm->pg_info;
9028 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
9029 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
9030 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
9031 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9032 				      &req->srq_pg_size_srq_lvl,
9033 				      &req->srq_page_dir);
9034 	}
9035 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
9036 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9037 		ctx_pg = ctxm->pg_info;
9038 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
9039 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
9040 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
9041 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9042 				      &req->cq_pg_size_cq_lvl,
9043 				      &req->cq_page_dir);
9044 	}
9045 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
9046 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9047 		ctx_pg = ctxm->pg_info;
9048 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
9049 		req->vnic_num_ring_table_entries =
9050 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
9051 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
9052 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9053 				      &req->vnic_pg_size_vnic_lvl,
9054 				      &req->vnic_page_dir);
9055 	}
9056 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
9057 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9058 		ctx_pg = ctxm->pg_info;
9059 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
9060 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
9061 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9062 				      &req->stat_pg_size_stat_lvl,
9063 				      &req->stat_page_dir);
9064 	}
9065 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
9066 		u32 units;
9067 
9068 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9069 		ctx_pg = ctxm->pg_info;
9070 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
9071 		units = ctxm->mrav_num_entries_units;
9072 		if (units) {
9073 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
9074 			u32 entries;
9075 
9076 			num_mr = ctx_pg->entries - num_ah;
9077 			entries = ((num_mr / units) << 16) | (num_ah / units);
9078 			req->mrav_num_entries = cpu_to_le32(entries);
9079 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
9080 		}
9081 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
9082 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9083 				      &req->mrav_pg_size_mrav_lvl,
9084 				      &req->mrav_page_dir);
9085 	}
9086 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
9087 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9088 		ctx_pg = ctxm->pg_info;
9089 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
9090 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
9091 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9092 				      &req->tim_pg_size_tim_lvl,
9093 				      &req->tim_page_dir);
9094 	}
9095 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9096 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
9097 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
9098 	     pg_dir = &req->tqm_sp_page_dir,
9099 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
9100 	     ctx_pg = ctxm->pg_info;
9101 	     i < BNXT_MAX_TQM_RINGS;
9102 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
9103 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
9104 		if (!(enables & ena))
9105 			continue;
9106 
9107 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9108 		*num_entries = cpu_to_le32(ctx_pg->entries);
9109 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9110 	}
9111 	req->flags = cpu_to_le32(flags);
9112 	return hwrm_req_send(bp, req);
9113 }
9114 
9115 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9116 				  struct bnxt_ctx_pg_info *ctx_pg)
9117 {
9118 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9119 
9120 	rmem->page_size = BNXT_PAGE_SIZE;
9121 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9122 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9123 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9124 	if (rmem->depth >= 1)
9125 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9126 	return bnxt_alloc_ring(bp, rmem);
9127 }
9128 
9129 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9130 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9131 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9132 {
9133 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9134 	int rc;
9135 
9136 	if (!mem_size)
9137 		return -EINVAL;
9138 
9139 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9140 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9141 		ctx_pg->nr_pages = 0;
9142 		return -EINVAL;
9143 	}
9144 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9145 		int nr_tbls, i;
9146 
9147 		rmem->depth = 2;
9148 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9149 		if (!ctx_pg->ctx_pg_tbl)
9150 			return -ENOMEM;
9151 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9152 		rmem->nr_pages = nr_tbls;
9153 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9154 		if (rc)
9155 			return rc;
9156 		for (i = 0; i < nr_tbls; i++) {
9157 			struct bnxt_ctx_pg_info *pg_tbl;
9158 
9159 			pg_tbl = kzalloc_obj(*pg_tbl);
9160 			if (!pg_tbl)
9161 				return -ENOMEM;
9162 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9163 			rmem = &pg_tbl->ring_mem;
9164 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9165 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9166 			rmem->depth = 1;
9167 			rmem->nr_pages = MAX_CTX_PAGES;
9168 			rmem->ctx_mem = ctxm;
9169 			if (i == (nr_tbls - 1)) {
9170 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9171 
9172 				if (rem)
9173 					rmem->nr_pages = rem;
9174 			}
9175 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9176 			if (rc)
9177 				break;
9178 		}
9179 	} else {
9180 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9181 		if (rmem->nr_pages > 1 || depth)
9182 			rmem->depth = 1;
9183 		rmem->ctx_mem = ctxm;
9184 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9185 	}
9186 	return rc;
9187 }
9188 
9189 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9190 				    struct bnxt_ctx_pg_info *ctx_pg,
9191 				    void *buf, size_t offset, size_t head,
9192 				    size_t tail)
9193 {
9194 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9195 	size_t nr_pages = ctx_pg->nr_pages;
9196 	int page_size = rmem->page_size;
9197 	size_t len = 0, total_len = 0;
9198 	u16 depth = rmem->depth;
9199 
9200 	tail %= nr_pages * page_size;
9201 	do {
9202 		if (depth > 1) {
9203 			int i = head / (page_size * MAX_CTX_PAGES);
9204 			struct bnxt_ctx_pg_info *pg_tbl;
9205 
9206 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9207 			rmem = &pg_tbl->ring_mem;
9208 		}
9209 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9210 		head += len;
9211 		offset += len;
9212 		total_len += len;
9213 		if (head >= nr_pages * page_size)
9214 			head = 0;
9215 	} while (head != tail);
9216 	return total_len;
9217 }
9218 
9219 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9220 				  struct bnxt_ctx_pg_info *ctx_pg)
9221 {
9222 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9223 
9224 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9225 	    ctx_pg->ctx_pg_tbl) {
9226 		int i, nr_tbls = rmem->nr_pages;
9227 
9228 		for (i = 0; i < nr_tbls; i++) {
9229 			struct bnxt_ctx_pg_info *pg_tbl;
9230 			struct bnxt_ring_mem_info *rmem2;
9231 
9232 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9233 			if (!pg_tbl)
9234 				continue;
9235 			rmem2 = &pg_tbl->ring_mem;
9236 			bnxt_free_ring(bp, rmem2);
9237 			ctx_pg->ctx_pg_arr[i] = NULL;
9238 			kfree(pg_tbl);
9239 			ctx_pg->ctx_pg_tbl[i] = NULL;
9240 		}
9241 		kfree(ctx_pg->ctx_pg_tbl);
9242 		ctx_pg->ctx_pg_tbl = NULL;
9243 	}
9244 	bnxt_free_ring(bp, rmem);
9245 	ctx_pg->nr_pages = 0;
9246 }
9247 
9248 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9249 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9250 				   u8 pg_lvl)
9251 {
9252 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9253 	int i, rc = 0, n = 1;
9254 	u32 mem_size;
9255 
9256 	if (!ctxm->entry_size || !ctx_pg)
9257 		return -EINVAL;
9258 	if (ctxm->instance_bmap)
9259 		n = hweight32(ctxm->instance_bmap);
9260 	if (ctxm->entry_multiple)
9261 		entries = roundup(entries, ctxm->entry_multiple);
9262 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9263 	mem_size = entries * ctxm->entry_size;
9264 	for (i = 0; i < n && !rc; i++) {
9265 		ctx_pg[i].entries = entries;
9266 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9267 					    ctxm->init_value ? ctxm : NULL);
9268 	}
9269 	if (!rc)
9270 		ctxm->mem_valid = 1;
9271 	return rc;
9272 }
9273 
9274 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9275 					       struct bnxt_ctx_mem_type *ctxm,
9276 					       bool last)
9277 {
9278 	struct hwrm_func_backing_store_cfg_v2_input *req;
9279 	u32 instance_bmap = ctxm->instance_bmap;
9280 	int i, j, rc = 0, n = 1;
9281 	__le32 *p;
9282 
9283 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9284 		return 0;
9285 
9286 	if (instance_bmap)
9287 		n = hweight32(ctxm->instance_bmap);
9288 	else
9289 		instance_bmap = 1;
9290 
9291 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9292 	if (rc)
9293 		return rc;
9294 	hwrm_req_hold(bp, req);
9295 	req->type = cpu_to_le16(ctxm->type);
9296 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9297 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9298 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9299 		struct bnxt_bs_trace_info *bs_trace;
9300 		u32 enables;
9301 
9302 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9303 		req->enables = cpu_to_le32(enables);
9304 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9305 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9306 	}
9307 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9308 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9309 		p[i] = cpu_to_le32(ctxm->split[i]);
9310 	for (i = 0, j = 0; j < n && !rc; i++) {
9311 		struct bnxt_ctx_pg_info *ctx_pg;
9312 
9313 		if (!(instance_bmap & (1 << i)))
9314 			continue;
9315 		req->instance = cpu_to_le16(i);
9316 		ctx_pg = &ctxm->pg_info[j++];
9317 		if (!ctx_pg->entries)
9318 			continue;
9319 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9320 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9321 				      &req->page_size_pbl_level,
9322 				      &req->page_dir);
9323 		if (last && j == n)
9324 			req->flags =
9325 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9326 		rc = hwrm_req_send(bp, req);
9327 	}
9328 	hwrm_req_drop(bp, req);
9329 	return rc;
9330 }
9331 
9332 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9333 {
9334 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9335 	struct bnxt_ctx_mem_type *ctxm;
9336 	u16 last_type = BNXT_CTX_INV;
9337 	int rc = 0;
9338 	u16 type;
9339 
9340 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9341 		ctxm = &ctx->ctx_arr[type];
9342 		if (!bnxt_bs_trace_avail(bp, type))
9343 			continue;
9344 		if (!ctxm->mem_valid) {
9345 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9346 						     ctxm->max_entries, 1);
9347 			if (rc) {
9348 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9349 					    type);
9350 				continue;
9351 			}
9352 			bnxt_bs_trace_init(bp, ctxm);
9353 		}
9354 		last_type = type;
9355 	}
9356 
9357 	if (last_type == BNXT_CTX_INV) {
9358 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9359 			ctxm = &ctx->ctx_arr[type];
9360 			if (ctxm->mem_valid)
9361 				last_type = type;
9362 		}
9363 		if (last_type == BNXT_CTX_INV)
9364 			return 0;
9365 	}
9366 	ctx->ctx_arr[last_type].last = 1;
9367 
9368 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9369 		ctxm = &ctx->ctx_arr[type];
9370 
9371 		if (!ctxm->mem_valid)
9372 			continue;
9373 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9374 		if (rc)
9375 			return rc;
9376 	}
9377 	return 0;
9378 }
9379 
9380 /**
9381  * __bnxt_copy_ctx_mem - copy host context memory
9382  * @bp: The driver context
9383  * @ctxm: The pointer to the context memory type
9384  * @buf: The destination buffer or NULL to just obtain the length
9385  * @offset: The buffer offset to copy the data to
9386  * @head: The head offset of context memory to copy from
9387  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9388  *
9389  * This function is called for debugging purposes to dump the host context
9390  * used by the chip.
9391  *
9392  * Return: Length of memory copied
9393  */
9394 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9395 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9396 				  size_t offset, size_t head, size_t tail)
9397 {
9398 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9399 	size_t len = 0, total_len = 0;
9400 	int i, n = 1;
9401 
9402 	if (!ctx_pg)
9403 		return 0;
9404 
9405 	if (ctxm->instance_bmap)
9406 		n = hweight32(ctxm->instance_bmap);
9407 	for (i = 0; i < n; i++) {
9408 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9409 					    tail);
9410 		offset += len;
9411 		total_len += len;
9412 	}
9413 	return total_len;
9414 }
9415 
9416 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9417 			 void *buf, size_t offset)
9418 {
9419 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9420 
9421 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9422 }
9423 
9424 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9425 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9426 {
9427 	struct bnxt_ctx_pg_info *ctx_pg;
9428 	int i, n = 1;
9429 
9430 	ctxm->last = 0;
9431 
9432 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9433 		return;
9434 
9435 	ctx_pg = ctxm->pg_info;
9436 	if (ctx_pg) {
9437 		if (ctxm->instance_bmap)
9438 			n = hweight32(ctxm->instance_bmap);
9439 		for (i = 0; i < n; i++)
9440 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9441 
9442 		kfree(ctx_pg);
9443 		ctxm->pg_info = NULL;
9444 		ctxm->mem_valid = 0;
9445 	}
9446 	memset(ctxm, 0, sizeof(*ctxm));
9447 }
9448 
9449 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9450 {
9451 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9452 	u16 type;
9453 
9454 	if (!ctx)
9455 		return;
9456 
9457 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9458 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9459 
9460 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9461 	if (force) {
9462 		kfree(ctx);
9463 		bp->ctx = NULL;
9464 	}
9465 }
9466 
9467 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9468 {
9469 	struct bnxt_ctx_mem_type *ctxm;
9470 	struct bnxt_ctx_mem_info *ctx;
9471 	u32 l2_qps, qp1_qps, max_qps;
9472 	u32 ena, entries_sp, entries;
9473 	u32 srqs, max_srqs, min;
9474 	u32 num_mr, num_ah;
9475 	u32 extra_srqs = 0;
9476 	u32 extra_qps = 0;
9477 	u32 fast_qpmd_qps;
9478 	u8 pg_lvl = 1;
9479 	int i, rc;
9480 
9481 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9482 	if (rc) {
9483 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9484 			   rc);
9485 		return rc;
9486 	}
9487 	ctx = bp->ctx;
9488 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9489 		return 0;
9490 
9491 	ena = 0;
9492 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9493 		goto skip_legacy;
9494 
9495 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9496 	l2_qps = ctxm->qp_l2_entries;
9497 	qp1_qps = ctxm->qp_qp1_entries;
9498 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9499 	max_qps = ctxm->max_entries;
9500 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9501 	srqs = ctxm->srq_l2_entries;
9502 	max_srqs = ctxm->max_entries;
9503 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9504 		pg_lvl = 2;
9505 		if (BNXT_SW_RES_LMT(bp)) {
9506 			extra_qps = max_qps - l2_qps - qp1_qps;
9507 			extra_srqs = max_srqs - srqs;
9508 		} else {
9509 			extra_qps = min_t(u32, 65536,
9510 					  max_qps - l2_qps - qp1_qps);
9511 			/* allocate extra qps if fw supports RoCE fast qp
9512 			 * destroy feature
9513 			 */
9514 			extra_qps += fast_qpmd_qps;
9515 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9516 		}
9517 		if (fast_qpmd_qps)
9518 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9519 	}
9520 
9521 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9522 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9523 				     pg_lvl);
9524 	if (rc)
9525 		return rc;
9526 
9527 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9528 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9529 	if (rc)
9530 		return rc;
9531 
9532 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9533 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9534 				     extra_qps * 2, pg_lvl);
9535 	if (rc)
9536 		return rc;
9537 
9538 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9539 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9540 	if (rc)
9541 		return rc;
9542 
9543 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9544 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9545 	if (rc)
9546 		return rc;
9547 
9548 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9549 		goto skip_rdma;
9550 
9551 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9552 	if (BNXT_SW_RES_LMT(bp) &&
9553 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9554 		num_ah = ctxm->mrav_av_entries;
9555 		num_mr = ctxm->max_entries - num_ah;
9556 	} else {
9557 		/* 128K extra is needed to accommodate static AH context
9558 		 * allocation by f/w.
9559 		 */
9560 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9561 		num_ah = min_t(u32, num_mr, 1024 * 128);
9562 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9563 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9564 			ctxm->mrav_av_entries = num_ah;
9565 	}
9566 
9567 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9568 	if (rc)
9569 		return rc;
9570 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9571 
9572 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9573 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9574 	if (rc)
9575 		return rc;
9576 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9577 
9578 skip_rdma:
9579 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9580 	min = ctxm->min_entries;
9581 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9582 		     2 * (extra_qps + qp1_qps) + min;
9583 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9584 	if (rc)
9585 		return rc;
9586 
9587 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9588 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9589 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9590 	if (rc)
9591 		return rc;
9592 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9593 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9594 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9595 
9596 skip_legacy:
9597 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9598 		rc = bnxt_backing_store_cfg_v2(bp);
9599 	else
9600 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9601 	if (rc) {
9602 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9603 			   rc);
9604 		return rc;
9605 	}
9606 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9607 	return 0;
9608 }
9609 
9610 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9611 {
9612 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9613 	u16 page_attr;
9614 	int rc;
9615 
9616 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9617 		return 0;
9618 
9619 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9620 	if (rc)
9621 		return rc;
9622 
9623 	if (BNXT_PAGE_SIZE == 0x2000)
9624 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9625 	else if (BNXT_PAGE_SIZE == 0x10000)
9626 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9627 	else
9628 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9629 	req->pg_size_lvl = cpu_to_le16(page_attr |
9630 				       bp->fw_crash_mem->ring_mem.depth);
9631 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9632 	req->size = cpu_to_le32(bp->fw_crash_len);
9633 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9634 	return hwrm_req_send(bp, req);
9635 }
9636 
9637 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9638 {
9639 	if (bp->fw_crash_mem) {
9640 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9641 		kfree(bp->fw_crash_mem);
9642 		bp->fw_crash_mem = NULL;
9643 	}
9644 }
9645 
9646 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9647 {
9648 	u32 mem_size = 0;
9649 	int rc;
9650 
9651 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9652 		return 0;
9653 
9654 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9655 	if (rc)
9656 		return rc;
9657 
9658 	mem_size = round_up(mem_size, 4);
9659 
9660 	/* keep and use the existing pages */
9661 	if (bp->fw_crash_mem &&
9662 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9663 		goto alloc_done;
9664 
9665 	if (bp->fw_crash_mem)
9666 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9667 	else
9668 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9669 	if (!bp->fw_crash_mem)
9670 		return -ENOMEM;
9671 
9672 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9673 	if (rc) {
9674 		bnxt_free_crash_dump_mem(bp);
9675 		return rc;
9676 	}
9677 
9678 alloc_done:
9679 	bp->fw_crash_len = mem_size;
9680 	return 0;
9681 }
9682 
9683 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9684 {
9685 	struct hwrm_func_resource_qcaps_output *resp;
9686 	struct hwrm_func_resource_qcaps_input *req;
9687 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9688 	int rc;
9689 
9690 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9691 	if (rc)
9692 		return rc;
9693 
9694 	req->fid = cpu_to_le16(0xffff);
9695 	resp = hwrm_req_hold(bp, req);
9696 	rc = hwrm_req_send_silent(bp, req);
9697 	if (rc)
9698 		goto hwrm_func_resc_qcaps_exit;
9699 
9700 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9701 	if (!all)
9702 		goto hwrm_func_resc_qcaps_exit;
9703 
9704 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9705 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9706 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9707 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9708 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9709 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9710 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9711 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9712 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9713 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9714 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9715 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9716 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9717 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9718 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9719 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9720 
9721 	if (hw_resc->max_rsscos_ctxs >=
9722 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9723 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9724 
9725 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9726 		u16 max_msix = le16_to_cpu(resp->max_msix);
9727 
9728 		hw_resc->max_nqs = max_msix;
9729 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9730 	}
9731 
9732 	if (BNXT_PF(bp)) {
9733 		struct bnxt_pf_info *pf = &bp->pf;
9734 
9735 		pf->vf_resv_strategy =
9736 			le16_to_cpu(resp->vf_reservation_strategy);
9737 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9738 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9739 	}
9740 hwrm_func_resc_qcaps_exit:
9741 	hwrm_req_drop(bp, req);
9742 	return rc;
9743 }
9744 
9745 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9746 {
9747 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9748 	struct hwrm_port_mac_ptp_qcfg_input *req;
9749 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9750 	u8 flags;
9751 	int rc;
9752 
9753 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9754 		rc = -ENODEV;
9755 		goto no_ptp;
9756 	}
9757 
9758 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9759 	if (rc)
9760 		goto no_ptp;
9761 
9762 	req->port_id = cpu_to_le16(bp->pf.port_id);
9763 	resp = hwrm_req_hold(bp, req);
9764 	rc = hwrm_req_send(bp, req);
9765 	if (rc)
9766 		goto exit;
9767 
9768 	flags = resp->flags;
9769 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9770 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9771 		rc = -ENODEV;
9772 		goto exit;
9773 	}
9774 	if (!ptp) {
9775 		ptp = kzalloc_obj(*ptp);
9776 		if (!ptp) {
9777 			rc = -ENOMEM;
9778 			goto exit;
9779 		}
9780 		ptp->bp = bp;
9781 		bp->ptp_cfg = ptp;
9782 	}
9783 
9784 	if (flags &
9785 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9786 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9787 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9788 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9789 	} else if (BNXT_CHIP_P5(bp)) {
9790 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9791 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9792 	} else {
9793 		rc = -ENODEV;
9794 		goto exit;
9795 	}
9796 	ptp->rtc_configured =
9797 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9798 	rc = bnxt_ptp_init(bp);
9799 	if (rc)
9800 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9801 exit:
9802 	hwrm_req_drop(bp, req);
9803 	if (!rc)
9804 		return 0;
9805 
9806 no_ptp:
9807 	bnxt_ptp_clear(bp);
9808 	kfree(ptp);
9809 	bp->ptp_cfg = NULL;
9810 	return rc;
9811 }
9812 
9813 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9814 {
9815 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9816 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9817 	struct hwrm_func_qcaps_output *resp;
9818 	struct hwrm_func_qcaps_input *req;
9819 	int rc;
9820 
9821 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9822 	if (rc)
9823 		return rc;
9824 
9825 	req->fid = cpu_to_le16(0xffff);
9826 	resp = hwrm_req_hold(bp, req);
9827 	rc = hwrm_req_send(bp, req);
9828 	if (rc)
9829 		goto hwrm_func_qcaps_exit;
9830 
9831 	flags = le32_to_cpu(resp->flags);
9832 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9833 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9834 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9835 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9836 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9837 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9838 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9839 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9840 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9841 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9842 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9843 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9844 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9845 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9846 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9847 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9848 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9849 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9850 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9851 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9852 
9853 	flags_ext = le32_to_cpu(resp->flags_ext);
9854 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9855 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9856 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9857 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9858 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9859 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9860 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9861 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9862 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9863 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9864 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9865 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9866 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9867 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9868 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9869 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9870 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9871 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9872 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9873 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9874 
9875 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9876 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9877 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9878 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9879 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9880 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9881 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9882 	if (flags_ext2 &
9883 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9884 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9885 	if (BNXT_PF(bp) &&
9886 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9887 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9888 
9889 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9890 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9891 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9892 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9893 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9894 
9895 	bp->tx_push_thresh = 0;
9896 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9897 	    BNXT_FW_MAJ(bp) > 217)
9898 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9899 
9900 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9901 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9902 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9903 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9904 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9905 	if (!hw_resc->max_hw_ring_grps)
9906 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9907 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9908 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9909 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9910 
9911 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9912 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9913 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9914 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9915 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9916 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9917 
9918 	if (BNXT_PF(bp)) {
9919 		struct bnxt_pf_info *pf = &bp->pf;
9920 
9921 		pf->fw_fid = le16_to_cpu(resp->fid);
9922 		pf->port_id = le16_to_cpu(resp->port_id);
9923 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9924 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9925 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9926 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9927 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9928 			bp->flags |= BNXT_FLAG_WOL_CAP;
9929 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9930 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9931 		} else {
9932 			bnxt_ptp_clear(bp);
9933 			kfree(bp->ptp_cfg);
9934 			bp->ptp_cfg = NULL;
9935 		}
9936 	} else {
9937 #ifdef CONFIG_BNXT_SRIOV
9938 		struct bnxt_vf_info *vf = &bp->vf;
9939 
9940 		vf->fw_fid = le16_to_cpu(resp->fid);
9941 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9942 #endif
9943 	}
9944 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9945 
9946 hwrm_func_qcaps_exit:
9947 	hwrm_req_drop(bp, req);
9948 	return rc;
9949 }
9950 
9951 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9952 {
9953 	struct hwrm_dbg_qcaps_output *resp;
9954 	struct hwrm_dbg_qcaps_input *req;
9955 	int rc;
9956 
9957 	bp->fw_dbg_cap = 0;
9958 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9959 		return;
9960 
9961 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9962 	if (rc)
9963 		return;
9964 
9965 	req->fid = cpu_to_le16(0xffff);
9966 	resp = hwrm_req_hold(bp, req);
9967 	rc = hwrm_req_send(bp, req);
9968 	if (rc)
9969 		goto hwrm_dbg_qcaps_exit;
9970 
9971 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9972 
9973 hwrm_dbg_qcaps_exit:
9974 	hwrm_req_drop(bp, req);
9975 }
9976 
9977 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9978 
9979 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9980 {
9981 	int rc;
9982 
9983 	rc = __bnxt_hwrm_func_qcaps(bp);
9984 	if (rc)
9985 		return rc;
9986 
9987 	bnxt_hwrm_dbg_qcaps(bp);
9988 
9989 	rc = bnxt_hwrm_queue_qportcfg(bp);
9990 	if (rc) {
9991 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9992 		return rc;
9993 	}
9994 	if (bp->hwrm_spec_code >= 0x10803) {
9995 		rc = bnxt_alloc_ctx_mem(bp);
9996 		if (rc)
9997 			return rc;
9998 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9999 		if (!rc)
10000 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
10001 	}
10002 	return 0;
10003 }
10004 
10005 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
10006 {
10007 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
10008 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
10009 	u32 flags;
10010 	int rc;
10011 
10012 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
10013 		return 0;
10014 
10015 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
10016 	if (rc)
10017 		return rc;
10018 
10019 	resp = hwrm_req_hold(bp, req);
10020 	rc = hwrm_req_send(bp, req);
10021 	if (rc)
10022 		goto hwrm_cfa_adv_qcaps_exit;
10023 
10024 	flags = le32_to_cpu(resp->flags);
10025 	if (flags &
10026 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
10027 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
10028 
10029 	if (flags &
10030 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
10031 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
10032 
10033 	if (flags &
10034 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
10035 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
10036 
10037 hwrm_cfa_adv_qcaps_exit:
10038 	hwrm_req_drop(bp, req);
10039 	return rc;
10040 }
10041 
10042 static int __bnxt_alloc_fw_health(struct bnxt *bp)
10043 {
10044 	if (bp->fw_health)
10045 		return 0;
10046 
10047 	bp->fw_health = kzalloc_obj(*bp->fw_health);
10048 	if (!bp->fw_health)
10049 		return -ENOMEM;
10050 
10051 	mutex_init(&bp->fw_health->lock);
10052 	return 0;
10053 }
10054 
10055 static int bnxt_alloc_fw_health(struct bnxt *bp)
10056 {
10057 	int rc;
10058 
10059 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10060 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10061 		return 0;
10062 
10063 	rc = __bnxt_alloc_fw_health(bp);
10064 	if (rc) {
10065 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10066 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10067 		return rc;
10068 	}
10069 
10070 	return 0;
10071 }
10072 
10073 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
10074 {
10075 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
10076 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
10077 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
10078 }
10079 
10080 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
10081 {
10082 	struct bnxt_fw_health *fw_health = bp->fw_health;
10083 	u32 reg_type;
10084 
10085 	if (!fw_health)
10086 		return;
10087 
10088 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
10089 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10090 		fw_health->status_reliable = false;
10091 
10092 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
10093 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10094 		fw_health->resets_reliable = false;
10095 }
10096 
10097 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
10098 {
10099 	void __iomem *hs;
10100 	u32 status_loc;
10101 	u32 reg_type;
10102 	u32 sig;
10103 
10104 	if (bp->fw_health)
10105 		bp->fw_health->status_reliable = false;
10106 
10107 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10108 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10109 
10110 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10111 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10112 		if (!bp->chip_num) {
10113 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10114 			bp->chip_num = readl(bp->bar0 +
10115 					     BNXT_FW_HEALTH_WIN_BASE +
10116 					     BNXT_GRC_REG_CHIP_NUM);
10117 		}
10118 		if (!BNXT_CHIP_P5_PLUS(bp))
10119 			return;
10120 
10121 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10122 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10123 	} else {
10124 		status_loc = readl(hs + offsetof(struct hcomm_status,
10125 						 fw_status_loc));
10126 	}
10127 
10128 	if (__bnxt_alloc_fw_health(bp)) {
10129 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10130 		return;
10131 	}
10132 
10133 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10134 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10135 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10136 		__bnxt_map_fw_health_reg(bp, status_loc);
10137 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10138 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10139 	}
10140 
10141 	bp->fw_health->status_reliable = true;
10142 }
10143 
10144 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10145 {
10146 	struct bnxt_fw_health *fw_health = bp->fw_health;
10147 	u32 reg_base = 0xffffffff;
10148 	int i;
10149 
10150 	bp->fw_health->status_reliable = false;
10151 	bp->fw_health->resets_reliable = false;
10152 	/* Only pre-map the monitoring GRC registers using window 3 */
10153 	for (i = 0; i < 4; i++) {
10154 		u32 reg = fw_health->regs[i];
10155 
10156 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10157 			continue;
10158 		if (reg_base == 0xffffffff)
10159 			reg_base = reg & BNXT_GRC_BASE_MASK;
10160 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10161 			return -ERANGE;
10162 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10163 	}
10164 	bp->fw_health->status_reliable = true;
10165 	bp->fw_health->resets_reliable = true;
10166 	if (reg_base == 0xffffffff)
10167 		return 0;
10168 
10169 	__bnxt_map_fw_health_reg(bp, reg_base);
10170 	return 0;
10171 }
10172 
10173 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10174 {
10175 	if (!bp->fw_health)
10176 		return;
10177 
10178 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10179 		bp->fw_health->status_reliable = true;
10180 		bp->fw_health->resets_reliable = true;
10181 	} else {
10182 		bnxt_try_map_fw_health_reg(bp);
10183 	}
10184 }
10185 
10186 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10187 {
10188 	struct bnxt_fw_health *fw_health = bp->fw_health;
10189 	struct hwrm_error_recovery_qcfg_output *resp;
10190 	struct hwrm_error_recovery_qcfg_input *req;
10191 	int rc, i;
10192 
10193 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10194 		return 0;
10195 
10196 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10197 	if (rc)
10198 		return rc;
10199 
10200 	resp = hwrm_req_hold(bp, req);
10201 	rc = hwrm_req_send(bp, req);
10202 	if (rc)
10203 		goto err_recovery_out;
10204 	fw_health->flags = le32_to_cpu(resp->flags);
10205 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10206 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10207 		rc = -EINVAL;
10208 		goto err_recovery_out;
10209 	}
10210 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10211 	fw_health->master_func_wait_dsecs =
10212 		le32_to_cpu(resp->master_func_wait_period);
10213 	fw_health->normal_func_wait_dsecs =
10214 		le32_to_cpu(resp->normal_func_wait_period);
10215 	fw_health->post_reset_wait_dsecs =
10216 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10217 	fw_health->post_reset_max_wait_dsecs =
10218 		le32_to_cpu(resp->max_bailout_time_after_reset);
10219 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10220 		le32_to_cpu(resp->fw_health_status_reg);
10221 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10222 		le32_to_cpu(resp->fw_heartbeat_reg);
10223 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10224 		le32_to_cpu(resp->fw_reset_cnt_reg);
10225 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10226 		le32_to_cpu(resp->reset_inprogress_reg);
10227 	fw_health->fw_reset_inprog_reg_mask =
10228 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10229 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10230 	if (fw_health->fw_reset_seq_cnt >= 16) {
10231 		rc = -EINVAL;
10232 		goto err_recovery_out;
10233 	}
10234 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10235 		fw_health->fw_reset_seq_regs[i] =
10236 			le32_to_cpu(resp->reset_reg[i]);
10237 		fw_health->fw_reset_seq_vals[i] =
10238 			le32_to_cpu(resp->reset_reg_val[i]);
10239 		fw_health->fw_reset_seq_delay_msec[i] =
10240 			resp->delay_after_reset[i];
10241 	}
10242 err_recovery_out:
10243 	hwrm_req_drop(bp, req);
10244 	if (!rc)
10245 		rc = bnxt_map_fw_health_regs(bp);
10246 	if (rc)
10247 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10248 	return rc;
10249 }
10250 
10251 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10252 {
10253 	struct hwrm_func_reset_input *req;
10254 	int rc;
10255 
10256 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10257 	if (rc)
10258 		return rc;
10259 
10260 	req->enables = 0;
10261 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10262 	return hwrm_req_send(bp, req);
10263 }
10264 
10265 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10266 {
10267 	struct hwrm_nvm_get_dev_info_output nvm_info;
10268 
10269 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10270 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10271 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10272 			 nvm_info.nvm_cfg_ver_upd);
10273 }
10274 
10275 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10276 {
10277 	struct hwrm_queue_qportcfg_output *resp;
10278 	struct hwrm_queue_qportcfg_input *req;
10279 	u8 i, j, *qptr;
10280 	bool no_rdma;
10281 	int rc = 0;
10282 
10283 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10284 	if (rc)
10285 		return rc;
10286 
10287 	resp = hwrm_req_hold(bp, req);
10288 	rc = hwrm_req_send(bp, req);
10289 	if (rc)
10290 		goto qportcfg_exit;
10291 
10292 	if (!resp->max_configurable_queues) {
10293 		rc = -EINVAL;
10294 		goto qportcfg_exit;
10295 	}
10296 	bp->max_tc = resp->max_configurable_queues;
10297 	bp->max_lltc = resp->max_configurable_lossless_queues;
10298 	if (bp->max_tc > BNXT_MAX_QUEUE)
10299 		bp->max_tc = BNXT_MAX_QUEUE;
10300 
10301 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10302 	qptr = &resp->queue_id0;
10303 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10304 		bp->q_info[j].queue_id = *qptr;
10305 		bp->q_ids[i] = *qptr++;
10306 		bp->q_info[j].queue_profile = *qptr++;
10307 		bp->tc_to_qidx[j] = j;
10308 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10309 		    (no_rdma && BNXT_PF(bp)))
10310 			j++;
10311 	}
10312 	bp->max_q = bp->max_tc;
10313 	bp->max_tc = max_t(u8, j, 1);
10314 
10315 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10316 		bp->max_tc = 1;
10317 
10318 	if (bp->max_lltc > bp->max_tc)
10319 		bp->max_lltc = bp->max_tc;
10320 
10321 qportcfg_exit:
10322 	hwrm_req_drop(bp, req);
10323 	return rc;
10324 }
10325 
10326 static int bnxt_hwrm_poll(struct bnxt *bp)
10327 {
10328 	struct hwrm_ver_get_input *req;
10329 	int rc;
10330 
10331 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10332 	if (rc)
10333 		return rc;
10334 
10335 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10336 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10337 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10338 
10339 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10340 	rc = hwrm_req_send(bp, req);
10341 	return rc;
10342 }
10343 
10344 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10345 {
10346 	struct hwrm_ver_get_output *resp;
10347 	struct hwrm_ver_get_input *req;
10348 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10349 	u32 dev_caps_cfg, hwrm_ver;
10350 	int rc, len, max_tmo_secs;
10351 
10352 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10353 	if (rc)
10354 		return rc;
10355 
10356 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10357 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10358 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10359 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10360 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10361 
10362 	resp = hwrm_req_hold(bp, req);
10363 	rc = hwrm_req_send(bp, req);
10364 	if (rc)
10365 		goto hwrm_ver_get_exit;
10366 
10367 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10368 
10369 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10370 			     resp->hwrm_intf_min_8b << 8 |
10371 			     resp->hwrm_intf_upd_8b;
10372 	if (resp->hwrm_intf_maj_8b < 1) {
10373 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10374 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10375 			    resp->hwrm_intf_upd_8b);
10376 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10377 	}
10378 
10379 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10380 			HWRM_VERSION_UPDATE;
10381 
10382 	if (bp->hwrm_spec_code > hwrm_ver)
10383 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10384 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10385 			 HWRM_VERSION_UPDATE);
10386 	else
10387 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10388 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10389 			 resp->hwrm_intf_upd_8b);
10390 
10391 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10392 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10393 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10394 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10395 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10396 		len = FW_VER_STR_LEN;
10397 	} else {
10398 		fw_maj = resp->hwrm_fw_maj_8b;
10399 		fw_min = resp->hwrm_fw_min_8b;
10400 		fw_bld = resp->hwrm_fw_bld_8b;
10401 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10402 		len = BC_HWRM_STR_LEN;
10403 	}
10404 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10405 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10406 		 fw_rsv);
10407 
10408 	if (strlen(resp->active_pkg_name)) {
10409 		int fw_ver_len = strlen(bp->fw_ver_str);
10410 
10411 		snprintf(bp->fw_ver_str + fw_ver_len,
10412 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10413 			 resp->active_pkg_name);
10414 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10415 	}
10416 
10417 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10418 	if (!bp->hwrm_cmd_timeout)
10419 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10420 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10421 	if (!bp->hwrm_cmd_max_timeout)
10422 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10423 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10424 #ifdef CONFIG_DETECT_HUNG_TASK
10425 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10426 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10427 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10428 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10429 	}
10430 #endif
10431 
10432 	if (resp->hwrm_intf_maj_8b >= 1) {
10433 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10434 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10435 	}
10436 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10437 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10438 
10439 	bp->chip_num = le16_to_cpu(resp->chip_num);
10440 	bp->chip_rev = resp->chip_rev;
10441 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10442 	    !resp->chip_metal)
10443 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10444 
10445 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10446 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10447 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10448 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10449 
10450 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10451 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10452 
10453 	if (dev_caps_cfg &
10454 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10455 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10456 
10457 	if (dev_caps_cfg &
10458 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10459 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10460 
10461 	if (dev_caps_cfg &
10462 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10463 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10464 
10465 hwrm_ver_get_exit:
10466 	hwrm_req_drop(bp, req);
10467 	return rc;
10468 }
10469 
10470 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10471 {
10472 	struct hwrm_fw_set_time_input *req;
10473 	struct tm tm;
10474 	time64_t now = ktime_get_real_seconds();
10475 	int rc;
10476 
10477 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10478 	    bp->hwrm_spec_code < 0x10400)
10479 		return -EOPNOTSUPP;
10480 
10481 	time64_to_tm(now, 0, &tm);
10482 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10483 	if (rc)
10484 		return rc;
10485 
10486 	req->year = cpu_to_le16(1900 + tm.tm_year);
10487 	req->month = 1 + tm.tm_mon;
10488 	req->day = tm.tm_mday;
10489 	req->hour = tm.tm_hour;
10490 	req->minute = tm.tm_min;
10491 	req->second = tm.tm_sec;
10492 	return hwrm_req_send(bp, req);
10493 }
10494 
10495 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10496 {
10497 	u64 sw_tmp;
10498 
10499 	hw &= mask;
10500 	sw_tmp = (*sw & ~mask) | hw;
10501 	if (hw < (*sw & mask))
10502 		sw_tmp += mask + 1;
10503 	WRITE_ONCE(*sw, sw_tmp);
10504 }
10505 
10506 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10507 				    int count, bool ignore_zero)
10508 {
10509 	int i;
10510 
10511 	for (i = 0; i < count; i++) {
10512 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10513 
10514 		if (ignore_zero && !hw)
10515 			continue;
10516 
10517 		if (masks[i] == -1ULL)
10518 			sw_stats[i] = hw;
10519 		else
10520 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10521 	}
10522 }
10523 
10524 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10525 {
10526 	if (!stats->hw_stats)
10527 		return;
10528 
10529 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10530 				stats->hw_masks, stats->len / 8, false);
10531 }
10532 
10533 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10534 {
10535 	struct bnxt_stats_mem *ring0_stats;
10536 	bool ignore_zero = false;
10537 	int i;
10538 
10539 	/* Chip bug.  Counter intermittently becomes 0. */
10540 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10541 		ignore_zero = true;
10542 
10543 	for (i = 0; i < bp->cp_nr_rings; i++) {
10544 		struct bnxt_napi *bnapi = bp->bnapi[i];
10545 		struct bnxt_cp_ring_info *cpr;
10546 		struct bnxt_stats_mem *stats;
10547 
10548 		cpr = &bnapi->cp_ring;
10549 		stats = &cpr->stats;
10550 		if (!i)
10551 			ring0_stats = stats;
10552 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10553 					ring0_stats->hw_masks,
10554 					ring0_stats->len / 8, ignore_zero);
10555 	}
10556 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10557 		struct bnxt_stats_mem *stats = &bp->port_stats;
10558 		__le64 *hw_stats = stats->hw_stats;
10559 		u64 *sw_stats = stats->sw_stats;
10560 		u64 *masks = stats->hw_masks;
10561 		int cnt;
10562 
10563 		cnt = sizeof(struct rx_port_stats) / 8;
10564 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10565 
10566 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10567 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10568 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10569 		cnt = sizeof(struct tx_port_stats) / 8;
10570 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10571 	}
10572 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10573 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10574 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10575 	}
10576 }
10577 
10578 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10579 {
10580 	struct hwrm_port_qstats_input *req;
10581 	struct bnxt_pf_info *pf = &bp->pf;
10582 	int rc;
10583 
10584 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10585 		return 0;
10586 
10587 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10588 		return -EOPNOTSUPP;
10589 
10590 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10591 	if (rc)
10592 		return rc;
10593 
10594 	req->flags = flags;
10595 	req->port_id = cpu_to_le16(pf->port_id);
10596 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10597 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10598 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10599 	return hwrm_req_send(bp, req);
10600 }
10601 
10602 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10603 {
10604 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10605 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10606 	struct hwrm_port_qstats_ext_output *resp_qs;
10607 	struct hwrm_port_qstats_ext_input *req_qs;
10608 	struct bnxt_pf_info *pf = &bp->pf;
10609 	u32 tx_stat_size;
10610 	int rc;
10611 
10612 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10613 		return 0;
10614 
10615 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10616 		return -EOPNOTSUPP;
10617 
10618 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10619 	if (rc)
10620 		return rc;
10621 
10622 	req_qs->flags = flags;
10623 	req_qs->port_id = cpu_to_le16(pf->port_id);
10624 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10625 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10626 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10627 		       sizeof(struct tx_port_stats_ext) : 0;
10628 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10629 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10630 	resp_qs = hwrm_req_hold(bp, req_qs);
10631 	rc = hwrm_req_send(bp, req_qs);
10632 	if (!rc) {
10633 		bp->fw_rx_stats_ext_size =
10634 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10635 		if (BNXT_FW_MAJ(bp) < 220 &&
10636 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10637 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10638 
10639 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10640 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10641 	} else {
10642 		bp->fw_rx_stats_ext_size = 0;
10643 		bp->fw_tx_stats_ext_size = 0;
10644 	}
10645 	hwrm_req_drop(bp, req_qs);
10646 
10647 	if (flags)
10648 		return rc;
10649 
10650 	if (bp->fw_tx_stats_ext_size <=
10651 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10652 		bp->pri2cos_valid = 0;
10653 		return rc;
10654 	}
10655 
10656 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10657 	if (rc)
10658 		return rc;
10659 
10660 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10661 
10662 	resp_qc = hwrm_req_hold(bp, req_qc);
10663 	rc = hwrm_req_send(bp, req_qc);
10664 	if (!rc) {
10665 		u8 *pri2cos;
10666 		int i, j;
10667 
10668 		pri2cos = &resp_qc->pri0_cos_queue_id;
10669 		for (i = 0; i < 8; i++) {
10670 			u8 queue_id = pri2cos[i];
10671 			u8 queue_idx;
10672 
10673 			/* Per port queue IDs start from 0, 10, 20, etc */
10674 			queue_idx = queue_id % 10;
10675 			if (queue_idx > BNXT_MAX_QUEUE) {
10676 				bp->pri2cos_valid = false;
10677 				hwrm_req_drop(bp, req_qc);
10678 				return rc;
10679 			}
10680 			for (j = 0; j < bp->max_q; j++) {
10681 				if (bp->q_ids[j] == queue_id)
10682 					bp->pri2cos_idx[i] = queue_idx;
10683 			}
10684 		}
10685 		bp->pri2cos_valid = true;
10686 	}
10687 	hwrm_req_drop(bp, req_qc);
10688 
10689 	return rc;
10690 }
10691 
10692 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10693 {
10694 	bnxt_hwrm_tunnel_dst_port_free(bp,
10695 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10696 	bnxt_hwrm_tunnel_dst_port_free(bp,
10697 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10698 }
10699 
10700 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10701 {
10702 	int rc, i;
10703 	u32 tpa_flags = 0;
10704 
10705 	if (set_tpa)
10706 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10707 	else if (BNXT_NO_FW_ACCESS(bp))
10708 		return 0;
10709 	for (i = 0; i < bp->nr_vnics; i++) {
10710 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10711 		if (rc) {
10712 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10713 				   i, rc);
10714 			return rc;
10715 		}
10716 	}
10717 	return 0;
10718 }
10719 
10720 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10721 {
10722 	int i;
10723 
10724 	for (i = 0; i < bp->nr_vnics; i++)
10725 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10726 }
10727 
10728 static void bnxt_clear_vnic(struct bnxt *bp)
10729 {
10730 	if (!bp->vnic_info)
10731 		return;
10732 
10733 	bnxt_hwrm_clear_vnic_filter(bp);
10734 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10735 		/* clear all RSS setting before free vnic ctx */
10736 		bnxt_hwrm_clear_vnic_rss(bp);
10737 		bnxt_hwrm_vnic_ctx_free(bp);
10738 	}
10739 	/* before free the vnic, undo the vnic tpa settings */
10740 	if (bp->flags & BNXT_FLAG_TPA)
10741 		bnxt_set_tpa(bp, false);
10742 	bnxt_hwrm_vnic_free(bp);
10743 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10744 		bnxt_hwrm_vnic_ctx_free(bp);
10745 }
10746 
10747 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10748 				    bool irq_re_init)
10749 {
10750 	bnxt_clear_vnic(bp);
10751 	bnxt_hwrm_ring_free(bp, close_path);
10752 	bnxt_hwrm_ring_grp_free(bp);
10753 	if (irq_re_init) {
10754 		bnxt_hwrm_stat_ctx_free(bp);
10755 		bnxt_hwrm_free_tunnel_ports(bp);
10756 	}
10757 }
10758 
10759 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10760 {
10761 	struct hwrm_func_cfg_input *req;
10762 	u8 evb_mode;
10763 	int rc;
10764 
10765 	if (br_mode == BRIDGE_MODE_VEB)
10766 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10767 	else if (br_mode == BRIDGE_MODE_VEPA)
10768 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10769 	else
10770 		return -EINVAL;
10771 
10772 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10773 	if (rc)
10774 		return rc;
10775 
10776 	req->fid = cpu_to_le16(0xffff);
10777 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10778 	req->evb_mode = evb_mode;
10779 	return hwrm_req_send(bp, req);
10780 }
10781 
10782 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10783 {
10784 	struct hwrm_func_cfg_input *req;
10785 	int rc;
10786 
10787 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10788 		return 0;
10789 
10790 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10791 	if (rc)
10792 		return rc;
10793 
10794 	req->fid = cpu_to_le16(0xffff);
10795 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10796 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10797 	if (size == 128)
10798 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10799 
10800 	return hwrm_req_send(bp, req);
10801 }
10802 
10803 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10804 {
10805 	int rc;
10806 
10807 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10808 		goto skip_rss_ctx;
10809 
10810 	/* allocate context for vnic */
10811 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10812 	if (rc) {
10813 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10814 			   vnic->vnic_id, rc);
10815 		goto vnic_setup_err;
10816 	}
10817 	bp->rsscos_nr_ctxs++;
10818 
10819 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10820 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10821 		if (rc) {
10822 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10823 				   vnic->vnic_id, rc);
10824 			goto vnic_setup_err;
10825 		}
10826 		bp->rsscos_nr_ctxs++;
10827 	}
10828 
10829 skip_rss_ctx:
10830 	/* configure default vnic, ring grp */
10831 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10832 	if (rc) {
10833 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10834 			   vnic->vnic_id, rc);
10835 		goto vnic_setup_err;
10836 	}
10837 
10838 	/* Enable RSS hashing on vnic */
10839 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10840 	if (rc) {
10841 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10842 			   vnic->vnic_id, rc);
10843 		goto vnic_setup_err;
10844 	}
10845 
10846 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10847 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10848 		if (rc) {
10849 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10850 				   vnic->vnic_id, rc);
10851 		}
10852 	}
10853 
10854 vnic_setup_err:
10855 	return rc;
10856 }
10857 
10858 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10859 			  u8 valid)
10860 {
10861 	struct hwrm_vnic_update_input *req;
10862 	int rc;
10863 
10864 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10865 	if (rc)
10866 		return rc;
10867 
10868 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10869 
10870 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10871 		req->mru = cpu_to_le16(vnic->mru);
10872 
10873 	req->enables = cpu_to_le32(valid);
10874 
10875 	return hwrm_req_send(bp, req);
10876 }
10877 
10878 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10879 {
10880 	int rc;
10881 
10882 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10883 	if (rc) {
10884 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10885 			   vnic->vnic_id, rc);
10886 		return rc;
10887 	}
10888 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10889 	if (rc)
10890 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10891 			   vnic->vnic_id, rc);
10892 	return rc;
10893 }
10894 
10895 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10896 {
10897 	int rc, i, nr_ctxs;
10898 
10899 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10900 	for (i = 0; i < nr_ctxs; i++) {
10901 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10902 		if (rc) {
10903 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10904 				   vnic->vnic_id, i, rc);
10905 			break;
10906 		}
10907 		bp->rsscos_nr_ctxs++;
10908 	}
10909 	if (i < nr_ctxs)
10910 		return -ENOMEM;
10911 
10912 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10913 	if (rc)
10914 		return rc;
10915 
10916 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10917 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10918 		if (rc) {
10919 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10920 				   vnic->vnic_id, rc);
10921 		}
10922 	}
10923 	return rc;
10924 }
10925 
10926 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10927 {
10928 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10929 		return __bnxt_setup_vnic_p5(bp, vnic);
10930 	else
10931 		return __bnxt_setup_vnic(bp, vnic);
10932 }
10933 
10934 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10935 				     struct bnxt_vnic_info *vnic,
10936 				     u16 start_rx_ring_idx, int rx_rings)
10937 {
10938 	int rc;
10939 
10940 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10941 	if (rc) {
10942 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10943 			   vnic->vnic_id, rc);
10944 		return rc;
10945 	}
10946 	return bnxt_setup_vnic(bp, vnic);
10947 }
10948 
10949 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10950 {
10951 	struct bnxt_vnic_info *vnic;
10952 	int i, rc = 0;
10953 
10954 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10955 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10956 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10957 	}
10958 
10959 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10960 		return 0;
10961 
10962 	for (i = 0; i < bp->rx_nr_rings; i++) {
10963 		u16 vnic_id = i + 1;
10964 		u16 ring_id = i;
10965 
10966 		if (vnic_id >= bp->nr_vnics)
10967 			break;
10968 
10969 		vnic = &bp->vnic_info[vnic_id];
10970 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10971 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10972 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10973 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10974 			break;
10975 	}
10976 	return rc;
10977 }
10978 
10979 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10980 			  bool all)
10981 {
10982 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10983 	struct bnxt_filter_base *usr_fltr, *tmp;
10984 	struct bnxt_ntuple_filter *ntp_fltr;
10985 	int i;
10986 
10987 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10988 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10989 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10990 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10991 	}
10992 	if (!all)
10993 		return;
10994 
10995 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10996 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10997 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10998 			ntp_fltr = container_of(usr_fltr,
10999 						struct bnxt_ntuple_filter,
11000 						base);
11001 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
11002 			bnxt_del_ntp_filter(bp, ntp_fltr);
11003 			bnxt_del_one_usr_fltr(bp, usr_fltr);
11004 		}
11005 	}
11006 
11007 	if (vnic->rss_table)
11008 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
11009 				  vnic->rss_table,
11010 				  vnic->rss_table_dma_addr);
11011 	bp->num_rss_ctx--;
11012 }
11013 
11014 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11015 				  int rxr_id)
11016 {
11017 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
11018 	int i, vnic_rx;
11019 
11020 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
11021 	 * must be updated because a future filter may use it.
11022 	 */
11023 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
11024 		return true;
11025 
11026 	for (i = 0; i < tbl_size; i++) {
11027 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
11028 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
11029 		else
11030 			vnic_rx = bp->rss_indir_tbl[i];
11031 
11032 		if (rxr_id == vnic_rx)
11033 			return true;
11034 	}
11035 
11036 	return false;
11037 }
11038 
11039 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11040 				u16 mru, int rxr_id)
11041 {
11042 	int rc;
11043 
11044 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
11045 		return 0;
11046 
11047 	if (mru) {
11048 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11049 		if (rc) {
11050 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11051 				   vnic->vnic_id, rc);
11052 			return rc;
11053 		}
11054 	}
11055 	vnic->mru = mru;
11056 	bnxt_hwrm_vnic_update(bp, vnic,
11057 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
11058 
11059 	return 0;
11060 }
11061 
11062 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
11063 {
11064 	struct ethtool_rxfh_context *ctx;
11065 	unsigned long context;
11066 	int rc;
11067 
11068 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11069 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11070 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11071 
11072 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
11073 		if (rc)
11074 			return rc;
11075 	}
11076 
11077 	return 0;
11078 }
11079 
11080 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
11081 {
11082 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
11083 	struct ethtool_rxfh_context *ctx;
11084 	unsigned long context;
11085 
11086 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11087 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11088 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11089 
11090 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
11091 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
11092 		    __bnxt_setup_vnic_p5(bp, vnic)) {
11093 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
11094 				   rss_ctx->index);
11095 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
11096 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
11097 		}
11098 	}
11099 }
11100 
11101 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
11102 {
11103 	struct ethtool_rxfh_context *ctx;
11104 	unsigned long context;
11105 
11106 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11107 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11108 
11109 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11110 	}
11111 }
11112 
11113 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11114 static bool bnxt_promisc_ok(struct bnxt *bp)
11115 {
11116 #ifdef CONFIG_BNXT_SRIOV
11117 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11118 		return false;
11119 #endif
11120 	return true;
11121 }
11122 
11123 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11124 {
11125 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11126 	unsigned int rc = 0;
11127 
11128 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11129 	if (rc) {
11130 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11131 			   rc);
11132 		return rc;
11133 	}
11134 
11135 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11136 	if (rc) {
11137 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11138 			   rc);
11139 		return rc;
11140 	}
11141 	return rc;
11142 }
11143 
11144 static int bnxt_cfg_rx_mode(struct bnxt *, struct netdev_hw_addr_list *, bool);
11145 static bool bnxt_mc_list_updated(struct bnxt *, u32 *,
11146 				 const struct netdev_hw_addr_list *);
11147 
11148 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11149 {
11150 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11151 	int rc = 0;
11152 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11153 
11154 	if (irq_re_init) {
11155 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11156 		if (rc) {
11157 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11158 				   rc);
11159 			goto err_out;
11160 		}
11161 	}
11162 
11163 	rc = bnxt_hwrm_ring_alloc(bp);
11164 	if (rc) {
11165 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11166 		goto err_out;
11167 	}
11168 
11169 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11170 	if (rc) {
11171 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11172 		goto err_out;
11173 	}
11174 
11175 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11176 		rx_nr_rings--;
11177 
11178 	/* default vnic 0 */
11179 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11180 	if (rc) {
11181 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11182 		goto err_out;
11183 	}
11184 
11185 	if (BNXT_VF(bp))
11186 		bnxt_hwrm_func_qcfg(bp);
11187 
11188 	rc = bnxt_setup_vnic(bp, vnic);
11189 	if (rc)
11190 		goto err_out;
11191 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11192 		bnxt_hwrm_update_rss_hash_cfg(bp);
11193 
11194 	if (bp->flags & BNXT_FLAG_RFS) {
11195 		rc = bnxt_alloc_rfs_vnics(bp);
11196 		if (rc)
11197 			goto err_out;
11198 	}
11199 
11200 	if (bp->flags & BNXT_FLAG_TPA) {
11201 		rc = bnxt_set_tpa(bp, true);
11202 		if (rc)
11203 			goto err_out;
11204 	}
11205 
11206 	if (BNXT_VF(bp))
11207 		bnxt_update_vf_mac(bp);
11208 
11209 	/* Filter for default vnic 0 */
11210 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11211 	if (rc) {
11212 		if (BNXT_VF(bp) && rc == -ENODEV)
11213 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11214 		else
11215 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11216 		goto err_out;
11217 	}
11218 	vnic->uc_filter_count = 1;
11219 
11220 	vnic->rx_mask = 0;
11221 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11222 		goto skip_rx_mask;
11223 
11224 	if (bp->dev->flags & IFF_BROADCAST)
11225 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11226 
11227 	if (bp->dev->flags & IFF_PROMISC)
11228 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11229 
11230 	if (bp->dev->flags & IFF_ALLMULTI) {
11231 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11232 		vnic->mc_list_count = 0;
11233 	} else if (bp->dev->flags & IFF_MULTICAST) {
11234 		u32 mask = 0;
11235 
11236 		bnxt_mc_list_updated(bp, &mask, &bp->dev->mc);
11237 		vnic->rx_mask |= mask;
11238 	}
11239 
11240 	rc = bnxt_cfg_rx_mode(bp, &bp->dev->uc, true);
11241 	if (rc == -EAGAIN) {
11242 		netif_rx_mode_schedule_retry(bp->dev);
11243 		rc = 0;
11244 	} else if (rc) {
11245 		goto err_out;
11246 	}
11247 
11248 skip_rx_mask:
11249 	rc = bnxt_hwrm_set_coal(bp);
11250 	if (rc)
11251 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11252 				rc);
11253 
11254 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11255 		rc = bnxt_setup_nitroa0_vnic(bp);
11256 		if (rc)
11257 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11258 				   rc);
11259 	}
11260 
11261 	if (BNXT_VF(bp)) {
11262 		bnxt_hwrm_func_qcfg(bp);
11263 		netdev_update_features(bp->dev);
11264 	}
11265 
11266 	return 0;
11267 
11268 err_out:
11269 	bnxt_hwrm_resource_free(bp, 0, true);
11270 
11271 	return rc;
11272 }
11273 
11274 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11275 {
11276 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11277 	return 0;
11278 }
11279 
11280 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11281 {
11282 	bnxt_init_cp_rings(bp);
11283 	bnxt_init_rx_rings(bp);
11284 	bnxt_init_tx_rings(bp);
11285 	bnxt_init_ring_grps(bp, irq_re_init);
11286 	bnxt_init_vnics(bp);
11287 
11288 	return bnxt_init_chip(bp, irq_re_init);
11289 }
11290 
11291 static int bnxt_set_real_num_queues(struct bnxt *bp)
11292 {
11293 	int rc;
11294 	struct net_device *dev = bp->dev;
11295 
11296 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11297 					  bp->tx_nr_rings_xdp);
11298 	if (rc)
11299 		return rc;
11300 
11301 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11302 	if (rc)
11303 		return rc;
11304 
11305 #ifdef CONFIG_RFS_ACCEL
11306 	if (bp->flags & BNXT_FLAG_RFS)
11307 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11308 #endif
11309 
11310 	return rc;
11311 }
11312 
11313 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11314 			     bool shared)
11315 {
11316 	int _rx = *rx, _tx = *tx;
11317 
11318 	if (shared) {
11319 		*rx = min_t(int, _rx, max);
11320 		*tx = min_t(int, _tx, max);
11321 	} else {
11322 		if (max < 2)
11323 			return -ENOMEM;
11324 
11325 		while (_rx + _tx > max) {
11326 			if (_rx > _tx && _rx > 1)
11327 				_rx--;
11328 			else if (_tx > 1)
11329 				_tx--;
11330 		}
11331 		*rx = _rx;
11332 		*tx = _tx;
11333 	}
11334 	return 0;
11335 }
11336 
11337 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11338 {
11339 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11340 }
11341 
11342 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11343 {
11344 	int tcs = bp->num_tc;
11345 
11346 	if (!tcs)
11347 		tcs = 1;
11348 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11349 }
11350 
11351 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11352 {
11353 	int tcs = bp->num_tc;
11354 
11355 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11356 	       bp->tx_nr_rings_xdp;
11357 }
11358 
11359 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11360 			   bool sh)
11361 {
11362 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11363 
11364 	if (tx_cp != *tx) {
11365 		int tx_saved = tx_cp, rc;
11366 
11367 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11368 		if (rc)
11369 			return rc;
11370 		if (tx_cp != tx_saved)
11371 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11372 		return 0;
11373 	}
11374 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11375 }
11376 
11377 static void bnxt_setup_msix(struct bnxt *bp)
11378 {
11379 	const int len = sizeof(bp->irq_tbl[0].name);
11380 	struct net_device *dev = bp->dev;
11381 	int tcs, i;
11382 
11383 	tcs = bp->num_tc;
11384 	if (tcs) {
11385 		int i, off, count;
11386 
11387 		for (i = 0; i < tcs; i++) {
11388 			count = bp->tx_nr_rings_per_tc;
11389 			off = BNXT_TC_TO_RING_BASE(bp, i);
11390 			netdev_set_tc_queue(dev, i, count, off);
11391 		}
11392 	}
11393 
11394 	for (i = 0; i < bp->cp_nr_rings; i++) {
11395 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11396 		char *attr;
11397 
11398 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11399 			attr = "TxRx";
11400 		else if (i < bp->rx_nr_rings)
11401 			attr = "rx";
11402 		else
11403 			attr = "tx";
11404 
11405 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11406 			 attr, i);
11407 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11408 	}
11409 }
11410 
11411 static int bnxt_init_int_mode(struct bnxt *bp);
11412 
11413 static int bnxt_change_msix(struct bnxt *bp, int total)
11414 {
11415 	struct msi_map map;
11416 	int i;
11417 
11418 	/* add MSIX to the end if needed */
11419 	for (i = bp->total_irqs; i < total; i++) {
11420 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11421 		if (map.index < 0)
11422 			return bp->total_irqs;
11423 		bp->irq_tbl[i].vector = map.virq;
11424 		bp->total_irqs++;
11425 	}
11426 
11427 	/* trim MSIX from the end if needed */
11428 	for (i = bp->total_irqs; i > total; i--) {
11429 		map.index = i - 1;
11430 		map.virq = bp->irq_tbl[i - 1].vector;
11431 		pci_msix_free_irq(bp->pdev, map);
11432 		bp->total_irqs--;
11433 	}
11434 	return bp->total_irqs;
11435 }
11436 
11437 static int bnxt_setup_int_mode(struct bnxt *bp)
11438 {
11439 	int rc;
11440 
11441 	if (!bp->irq_tbl) {
11442 		rc = bnxt_init_int_mode(bp);
11443 		if (rc || !bp->irq_tbl)
11444 			return rc ?: -ENODEV;
11445 	}
11446 
11447 	bnxt_setup_msix(bp);
11448 
11449 	rc = bnxt_set_real_num_queues(bp);
11450 	return rc;
11451 }
11452 
11453 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11454 {
11455 	return bp->hw_resc.max_rsscos_ctxs;
11456 }
11457 
11458 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11459 {
11460 	return bp->hw_resc.max_vnics;
11461 }
11462 
11463 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11464 {
11465 	return bp->hw_resc.max_stat_ctxs;
11466 }
11467 
11468 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11469 {
11470 	return bp->hw_resc.max_cp_rings;
11471 }
11472 
11473 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11474 {
11475 	unsigned int cp = bp->hw_resc.max_cp_rings;
11476 
11477 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11478 		cp -= bnxt_get_ulp_msix_num(bp);
11479 
11480 	return cp;
11481 }
11482 
11483 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11484 {
11485 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11486 
11487 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11488 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11489 
11490 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11491 }
11492 
11493 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11494 {
11495 	bp->hw_resc.max_irqs = max_irqs;
11496 }
11497 
11498 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11499 {
11500 	unsigned int cp;
11501 
11502 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11503 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11504 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11505 	else
11506 		return cp - bp->cp_nr_rings;
11507 }
11508 
11509 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11510 {
11511 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11512 }
11513 
11514 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11515 {
11516 	int max_irq = bnxt_get_max_func_irqs(bp);
11517 	int total_req = bp->cp_nr_rings + num;
11518 
11519 	if (max_irq < total_req) {
11520 		num = max_irq - bp->cp_nr_rings;
11521 		if (num <= 0)
11522 			return 0;
11523 	}
11524 	return num;
11525 }
11526 
11527 static int bnxt_get_num_msix(struct bnxt *bp)
11528 {
11529 	if (!BNXT_NEW_RM(bp))
11530 		return bnxt_get_max_func_irqs(bp);
11531 
11532 	return bnxt_nq_rings_in_use(bp);
11533 }
11534 
11535 static int bnxt_init_int_mode(struct bnxt *bp)
11536 {
11537 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11538 
11539 	total_vecs = bnxt_get_num_msix(bp);
11540 	max = bnxt_get_max_func_irqs(bp);
11541 	if (total_vecs > max)
11542 		total_vecs = max;
11543 
11544 	if (!total_vecs)
11545 		return 0;
11546 
11547 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11548 		min = 2;
11549 
11550 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11551 					   PCI_IRQ_MSIX);
11552 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11553 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11554 		rc = -ENODEV;
11555 		goto msix_setup_exit;
11556 	}
11557 
11558 	tbl_size = total_vecs;
11559 	if (pci_msix_can_alloc_dyn(bp->pdev))
11560 		tbl_size = max;
11561 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11562 	if (bp->irq_tbl) {
11563 		for (i = 0; i < total_vecs; i++)
11564 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11565 
11566 		bp->total_irqs = total_vecs;
11567 		/* Trim rings based upon num of vectors allocated */
11568 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11569 				     total_vecs - ulp_msix, min == 1);
11570 		if (rc)
11571 			goto msix_setup_exit;
11572 
11573 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11574 		bp->cp_nr_rings = (min == 1) ?
11575 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11576 				  tx_cp + bp->rx_nr_rings;
11577 
11578 	} else {
11579 		rc = -ENOMEM;
11580 		goto msix_setup_exit;
11581 	}
11582 	return 0;
11583 
11584 msix_setup_exit:
11585 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11586 	kfree(bp->irq_tbl);
11587 	bp->irq_tbl = NULL;
11588 	pci_free_irq_vectors(bp->pdev);
11589 	return rc;
11590 }
11591 
11592 static void bnxt_clear_int_mode(struct bnxt *bp)
11593 {
11594 	pci_free_irq_vectors(bp->pdev);
11595 
11596 	kfree(bp->irq_tbl);
11597 	bp->irq_tbl = NULL;
11598 }
11599 
11600 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11601 {
11602 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
11603 	bool irq_cleared = false;
11604 	bool irq_change = false;
11605 	int tcs = bp->num_tc;
11606 	int irqs_required;
11607 	int rc;
11608 
11609 	if (!bnxt_need_reserve_rings(bp))
11610 		return 0;
11611 
11612 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
11613 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11614 
11615 		if (ulp_msix > bp->ulp_num_msix_want)
11616 			ulp_msix = bp->ulp_num_msix_want;
11617 		irqs_required = ulp_msix + bp->cp_nr_rings;
11618 	} else {
11619 		irqs_required = bnxt_get_num_msix(bp);
11620 	}
11621 
11622 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11623 		irq_change = true;
11624 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11625 			bnxt_ulp_irq_stop(bp);
11626 			bnxt_clear_int_mode(bp);
11627 			irq_cleared = true;
11628 		}
11629 	}
11630 	rc = __bnxt_reserve_rings(bp);
11631 	if (irq_cleared) {
11632 		if (!rc)
11633 			rc = bnxt_init_int_mode(bp);
11634 		bnxt_ulp_irq_restart(bp, rc);
11635 	} else if (irq_change && !rc) {
11636 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11637 			rc = -ENOSPC;
11638 	}
11639 	if (rc) {
11640 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11641 		return rc;
11642 	}
11643 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11644 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11645 		netdev_err(bp->dev, "tx ring reservation failure\n");
11646 		netdev_reset_tc(bp->dev);
11647 		bp->num_tc = 0;
11648 		if (bp->tx_nr_rings_xdp)
11649 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11650 		else
11651 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11652 		return -ENOMEM;
11653 	}
11654 	return 0;
11655 }
11656 
11657 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11658 {
11659 	struct bnxt_tx_ring_info *txr;
11660 	struct netdev_queue *txq;
11661 	struct bnxt_napi *bnapi;
11662 	int i;
11663 
11664 	bnapi = bp->bnapi[idx];
11665 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11666 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11667 		synchronize_net();
11668 
11669 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11670 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11671 			if (txq) {
11672 				__netif_tx_lock_bh(txq);
11673 				netif_tx_stop_queue(txq);
11674 				__netif_tx_unlock_bh(txq);
11675 			}
11676 		}
11677 
11678 		if (!bp->tph_mode)
11679 			continue;
11680 
11681 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11682 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11683 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11684 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11685 	}
11686 }
11687 
11688 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11689 {
11690 	struct bnxt_tx_ring_info *txr;
11691 	struct netdev_queue *txq;
11692 	struct bnxt_napi *bnapi;
11693 	int rc, i;
11694 
11695 	bnapi = bp->bnapi[idx];
11696 	/* All rings have been reserved and previously allocated.
11697 	 * Reallocating with the same parameters should never fail.
11698 	 */
11699 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11700 		if (!bp->tph_mode)
11701 			goto start_tx;
11702 
11703 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11704 		if (rc)
11705 			return rc;
11706 
11707 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11708 		if (rc)
11709 			return rc;
11710 
11711 		txr->tx_prod = 0;
11712 		txr->tx_cons = 0;
11713 		txr->tx_hw_cons = 0;
11714 start_tx:
11715 		WRITE_ONCE(txr->dev_state, 0);
11716 		synchronize_net();
11717 
11718 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11719 			continue;
11720 
11721 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11722 		if (txq)
11723 			netif_tx_start_queue(txq);
11724 	}
11725 
11726 	return 0;
11727 }
11728 
11729 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11730 				     const cpumask_t *mask)
11731 {
11732 	struct bnxt_irq *irq;
11733 	u16 tag;
11734 	int err;
11735 
11736 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11737 
11738 	if (!irq->bp->tph_mode)
11739 		return;
11740 
11741 	cpumask_copy(irq->cpu_mask, mask);
11742 
11743 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11744 		return;
11745 
11746 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11747 				cpumask_first(irq->cpu_mask), &tag))
11748 		return;
11749 
11750 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11751 		return;
11752 
11753 	netdev_lock(irq->bp->dev);
11754 	if (netif_running(irq->bp->dev)) {
11755 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11756 		if (err)
11757 			netdev_err(irq->bp->dev,
11758 				   "RX queue restart failed: err=%d\n", err);
11759 	}
11760 	netdev_unlock(irq->bp->dev);
11761 }
11762 
11763 static void bnxt_irq_affinity_release(struct kref *ref)
11764 {
11765 	struct irq_affinity_notify *notify =
11766 		container_of(ref, struct irq_affinity_notify, kref);
11767 	struct bnxt_irq *irq;
11768 
11769 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11770 
11771 	if (!irq->bp->tph_mode)
11772 		return;
11773 
11774 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11775 		netdev_err(irq->bp->dev,
11776 			   "Setting ST=0 for MSIX entry %d failed\n",
11777 			   irq->msix_nr);
11778 		return;
11779 	}
11780 }
11781 
11782 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11783 {
11784 	irq_set_affinity_notifier(irq->vector, NULL);
11785 }
11786 
11787 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11788 {
11789 	struct irq_affinity_notify *notify;
11790 
11791 	irq->bp = bp;
11792 
11793 	/* Nothing to do if TPH is not enabled */
11794 	if (!bp->tph_mode)
11795 		return;
11796 
11797 	/* Register IRQ affinity notifier */
11798 	notify = &irq->affinity_notify;
11799 	notify->irq = irq->vector;
11800 	notify->notify = bnxt_irq_affinity_notify;
11801 	notify->release = bnxt_irq_affinity_release;
11802 
11803 	irq_set_affinity_notifier(irq->vector, notify);
11804 }
11805 
11806 static void bnxt_free_irq(struct bnxt *bp)
11807 {
11808 	struct bnxt_irq *irq;
11809 	int i;
11810 
11811 #ifdef CONFIG_RFS_ACCEL
11812 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11813 	bp->dev->rx_cpu_rmap = NULL;
11814 #endif
11815 	if (!bp->irq_tbl || !bp->bnapi)
11816 		return;
11817 
11818 	for (i = 0; i < bp->cp_nr_rings; i++) {
11819 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11820 
11821 		irq = &bp->irq_tbl[map_idx];
11822 		if (irq->requested) {
11823 			if (irq->have_cpumask) {
11824 				irq_update_affinity_hint(irq->vector, NULL);
11825 				free_cpumask_var(irq->cpu_mask);
11826 				irq->have_cpumask = 0;
11827 			}
11828 
11829 			bnxt_release_irq_notifier(irq);
11830 
11831 			free_irq(irq->vector, bp->bnapi[i]);
11832 		}
11833 
11834 		irq->requested = 0;
11835 	}
11836 
11837 	/* Disable TPH support */
11838 	pcie_disable_tph(bp->pdev);
11839 	bp->tph_mode = 0;
11840 }
11841 
11842 static int bnxt_request_irq(struct bnxt *bp)
11843 {
11844 	struct cpu_rmap *rmap = NULL;
11845 	int i, j, rc = 0;
11846 	unsigned long flags = 0;
11847 
11848 	rc = bnxt_setup_int_mode(bp);
11849 	if (rc) {
11850 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11851 			   rc);
11852 		return rc;
11853 	}
11854 #ifdef CONFIG_RFS_ACCEL
11855 	rmap = bp->dev->rx_cpu_rmap;
11856 #endif
11857 
11858 	/* Enable TPH support as part of IRQ request */
11859 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11860 	if (!rc)
11861 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11862 
11863 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11864 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11865 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11866 
11867 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11868 		    rmap && bp->bnapi[i]->rx_ring) {
11869 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11870 			if (rc)
11871 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11872 					    j);
11873 			j++;
11874 		}
11875 
11876 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11877 				 bp->bnapi[i]);
11878 		if (rc)
11879 			break;
11880 
11881 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11882 		irq->requested = 1;
11883 
11884 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11885 			int numa_node = dev_to_node(&bp->pdev->dev);
11886 			u16 tag;
11887 
11888 			irq->have_cpumask = 1;
11889 			irq->msix_nr = map_idx;
11890 			irq->ring_nr = i;
11891 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11892 					irq->cpu_mask);
11893 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11894 			if (rc) {
11895 				netdev_warn(bp->dev,
11896 					    "Update affinity hint failed, IRQ = %d\n",
11897 					    irq->vector);
11898 				break;
11899 			}
11900 
11901 			bnxt_register_irq_notifier(bp, irq);
11902 
11903 			/* Init ST table entry */
11904 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11905 						cpumask_first(irq->cpu_mask),
11906 						&tag))
11907 				continue;
11908 
11909 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11910 		}
11911 	}
11912 	return rc;
11913 }
11914 
11915 static void bnxt_del_napi(struct bnxt *bp)
11916 {
11917 	int i;
11918 
11919 	if (!bp->bnapi)
11920 		return;
11921 
11922 	for (i = 0; i < bp->rx_nr_rings; i++)
11923 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11924 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11925 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11926 
11927 	for (i = 0; i < bp->cp_nr_rings; i++) {
11928 		struct bnxt_napi *bnapi = bp->bnapi[i];
11929 
11930 		__netif_napi_del_locked(&bnapi->napi);
11931 	}
11932 	/* We called __netif_napi_del_locked(), we need
11933 	 * to respect an RCU grace period before freeing napi structures.
11934 	 */
11935 	synchronize_net();
11936 }
11937 
11938 static void bnxt_init_napi(struct bnxt *bp)
11939 {
11940 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11941 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11942 	struct bnxt_napi *bnapi;
11943 	int i;
11944 
11945 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11946 		poll_fn = bnxt_poll_p5;
11947 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11948 		cp_nr_rings--;
11949 
11950 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11951 
11952 	for (i = 0; i < cp_nr_rings; i++) {
11953 		bnapi = bp->bnapi[i];
11954 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11955 					     bnapi->index);
11956 	}
11957 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11958 		bnapi = bp->bnapi[cp_nr_rings];
11959 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11960 	}
11961 }
11962 
11963 static void bnxt_disable_napi(struct bnxt *bp)
11964 {
11965 	int i;
11966 
11967 	if (!bp->bnapi ||
11968 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11969 		return;
11970 
11971 	for (i = 0; i < bp->cp_nr_rings; i++) {
11972 		struct bnxt_napi *bnapi = bp->bnapi[i];
11973 		struct bnxt_cp_ring_info *cpr;
11974 
11975 		cpr = &bnapi->cp_ring;
11976 		if (bnapi->tx_fault)
11977 			cpr->sw_stats->tx.tx_resets++;
11978 		if (bnapi->in_reset)
11979 			cpr->sw_stats->rx.rx_resets++;
11980 		napi_disable_locked(&bnapi->napi);
11981 	}
11982 }
11983 
11984 static void bnxt_enable_napi(struct bnxt *bp)
11985 {
11986 	int i;
11987 
11988 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11989 	for (i = 0; i < bp->cp_nr_rings; i++) {
11990 		struct bnxt_napi *bnapi = bp->bnapi[i];
11991 		struct bnxt_cp_ring_info *cpr;
11992 
11993 		bnapi->tx_fault = 0;
11994 
11995 		cpr = &bnapi->cp_ring;
11996 		bnapi->in_reset = false;
11997 
11998 		if (bnapi->rx_ring) {
11999 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
12000 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
12001 		}
12002 		napi_enable_locked(&bnapi->napi);
12003 	}
12004 }
12005 
12006 void bnxt_tx_disable(struct bnxt *bp)
12007 {
12008 	int i;
12009 	struct bnxt_tx_ring_info *txr;
12010 
12011 	if (bp->tx_ring) {
12012 		for (i = 0; i < bp->tx_nr_rings; i++) {
12013 			txr = &bp->tx_ring[i];
12014 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
12015 		}
12016 	}
12017 	/* Make sure napi polls see @dev_state change */
12018 	synchronize_net();
12019 	/* Drop carrier first to prevent TX timeout */
12020 	netif_carrier_off(bp->dev);
12021 	/* Stop all TX queues */
12022 	netif_tx_disable(bp->dev);
12023 }
12024 
12025 void bnxt_tx_enable(struct bnxt *bp)
12026 {
12027 	int i;
12028 	struct bnxt_tx_ring_info *txr;
12029 
12030 	for (i = 0; i < bp->tx_nr_rings; i++) {
12031 		txr = &bp->tx_ring[i];
12032 		WRITE_ONCE(txr->dev_state, 0);
12033 	}
12034 	/* Make sure napi polls see @dev_state change */
12035 	synchronize_net();
12036 	netif_tx_wake_all_queues(bp->dev);
12037 	if (BNXT_LINK_IS_UP(bp))
12038 		netif_carrier_on(bp->dev);
12039 }
12040 
12041 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
12042 {
12043 	u8 active_fec = link_info->active_fec_sig_mode &
12044 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
12045 
12046 	switch (active_fec) {
12047 	default:
12048 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
12049 		return "None";
12050 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
12051 		return "Clause 74 BaseR";
12052 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
12053 		return "Clause 91 RS(528,514)";
12054 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
12055 		return "Clause 91 RS544_1XN";
12056 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
12057 		return "Clause 91 RS(544,514)";
12058 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
12059 		return "Clause 91 RS272_1XN";
12060 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
12061 		return "Clause 91 RS(272,257)";
12062 	}
12063 }
12064 
12065 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
12066 {
12067 	u8 reason = link_info->link_down_reason;
12068 
12069 	/* Multiple bits can be set, we report 1 bit only in order of
12070 	 * priority.
12071 	 */
12072 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
12073 		return "(Remote fault)";
12074 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
12075 		return "(OTP Speed limit violation)";
12076 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
12077 		return "(Cable removed)";
12078 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
12079 		return "(Module fault)";
12080 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
12081 		return "(BMC request down)";
12082 	return "";
12083 }
12084 
12085 void bnxt_report_link(struct bnxt *bp)
12086 {
12087 	if (BNXT_LINK_IS_UP(bp)) {
12088 		const char *signal = "";
12089 		const char *flow_ctrl;
12090 		const char *duplex;
12091 		u32 speed;
12092 		u16 fec;
12093 
12094 		netif_carrier_on(bp->dev);
12095 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
12096 		if (speed == SPEED_UNKNOWN) {
12097 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
12098 			return;
12099 		}
12100 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
12101 			duplex = "full";
12102 		else
12103 			duplex = "half";
12104 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
12105 			flow_ctrl = "ON - receive & transmit";
12106 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
12107 			flow_ctrl = "ON - transmit";
12108 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12109 			flow_ctrl = "ON - receive";
12110 		else
12111 			flow_ctrl = "none";
12112 		if (bp->link_info.phy_qcfg_resp.option_flags &
12113 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12114 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12115 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12116 			switch (sig_mode) {
12117 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12118 				signal = "(NRZ) ";
12119 				break;
12120 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12121 				signal = "(PAM4 56Gbps) ";
12122 				break;
12123 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12124 				signal = "(PAM4 112Gbps) ";
12125 				break;
12126 			default:
12127 				break;
12128 			}
12129 		}
12130 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12131 			    speed, signal, duplex, flow_ctrl);
12132 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12133 			netdev_info(bp->dev, "EEE is %s\n",
12134 				    bp->eee.eee_active ? "active" :
12135 							 "not active");
12136 		fec = bp->link_info.fec_cfg;
12137 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12138 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12139 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12140 				    bnxt_report_fec(&bp->link_info));
12141 	} else {
12142 		char *str = bnxt_link_down_reason(&bp->link_info);
12143 
12144 		netif_carrier_off(bp->dev);
12145 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12146 	}
12147 }
12148 
12149 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12150 {
12151 	if (!resp->supported_speeds_auto_mode &&
12152 	    !resp->supported_speeds_force_mode &&
12153 	    !resp->supported_pam4_speeds_auto_mode &&
12154 	    !resp->supported_pam4_speeds_force_mode &&
12155 	    !resp->supported_speeds2_auto_mode &&
12156 	    !resp->supported_speeds2_force_mode)
12157 		return true;
12158 	return false;
12159 }
12160 
12161 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12162 {
12163 	struct bnxt_link_info *link_info = &bp->link_info;
12164 	struct hwrm_port_phy_qcaps_output *resp;
12165 	struct hwrm_port_phy_qcaps_input *req;
12166 	int rc = 0;
12167 
12168 	if (bp->hwrm_spec_code < 0x10201)
12169 		return 0;
12170 
12171 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12172 	if (rc)
12173 		return rc;
12174 
12175 	resp = hwrm_req_hold(bp, req);
12176 	rc = hwrm_req_send(bp, req);
12177 	if (rc)
12178 		goto hwrm_phy_qcaps_exit;
12179 
12180 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12181 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12182 		struct ethtool_keee *eee = &bp->eee;
12183 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12184 
12185 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12186 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12187 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12188 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12189 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12190 	}
12191 
12192 	if (bp->hwrm_spec_code >= 0x10a01) {
12193 		if (bnxt_phy_qcaps_no_speed(resp)) {
12194 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12195 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12196 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12197 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12198 			netdev_info(bp->dev, "Ethernet link enabled\n");
12199 			/* Phy re-enabled, reprobe the speeds */
12200 			link_info->support_auto_speeds = 0;
12201 			link_info->support_pam4_auto_speeds = 0;
12202 			link_info->support_auto_speeds2 = 0;
12203 		}
12204 	}
12205 	if (resp->supported_speeds_auto_mode)
12206 		link_info->support_auto_speeds =
12207 			le16_to_cpu(resp->supported_speeds_auto_mode);
12208 	if (resp->supported_pam4_speeds_auto_mode)
12209 		link_info->support_pam4_auto_speeds =
12210 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12211 	if (resp->supported_speeds2_auto_mode)
12212 		link_info->support_auto_speeds2 =
12213 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12214 
12215 	bp->port_count = resp->port_cnt;
12216 
12217 hwrm_phy_qcaps_exit:
12218 	hwrm_req_drop(bp, req);
12219 	return rc;
12220 }
12221 
12222 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12223 {
12224 	struct hwrm_port_mac_qcaps_output *resp;
12225 	struct hwrm_port_mac_qcaps_input *req;
12226 	int rc;
12227 
12228 	if (bp->hwrm_spec_code < 0x10a03)
12229 		return;
12230 
12231 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12232 	if (rc)
12233 		return;
12234 
12235 	resp = hwrm_req_hold(bp, req);
12236 	rc = hwrm_req_send_silent(bp, req);
12237 	if (!rc)
12238 		bp->mac_flags = resp->flags;
12239 	hwrm_req_drop(bp, req);
12240 }
12241 
12242 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12243 {
12244 	u16 diff = advertising ^ supported;
12245 
12246 	return ((supported | diff) != supported);
12247 }
12248 
12249 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12250 {
12251 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12252 
12253 	/* Check if any advertised speeds are no longer supported. The caller
12254 	 * holds the link_lock mutex, so we can modify link_info settings.
12255 	 */
12256 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12257 		if (bnxt_support_dropped(link_info->advertising,
12258 					 link_info->support_auto_speeds2)) {
12259 			link_info->advertising = link_info->support_auto_speeds2;
12260 			return true;
12261 		}
12262 		return false;
12263 	}
12264 	if (bnxt_support_dropped(link_info->advertising,
12265 				 link_info->support_auto_speeds)) {
12266 		link_info->advertising = link_info->support_auto_speeds;
12267 		return true;
12268 	}
12269 	if (bnxt_support_dropped(link_info->advertising_pam4,
12270 				 link_info->support_pam4_auto_speeds)) {
12271 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12272 		return true;
12273 	}
12274 	return false;
12275 }
12276 
12277 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12278 {
12279 	struct bnxt_link_info *link_info = &bp->link_info;
12280 	struct hwrm_port_phy_qcfg_output *resp;
12281 	struct hwrm_port_phy_qcfg_input *req;
12282 	u8 link_state = link_info->link_state;
12283 	bool support_changed;
12284 	int rc;
12285 
12286 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12287 	if (rc)
12288 		return rc;
12289 
12290 	resp = hwrm_req_hold(bp, req);
12291 	rc = hwrm_req_send(bp, req);
12292 	if (rc) {
12293 		hwrm_req_drop(bp, req);
12294 		if (BNXT_VF(bp) && rc == -ENODEV) {
12295 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12296 			rc = 0;
12297 		}
12298 		return rc;
12299 	}
12300 
12301 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12302 	link_info->phy_link_status = resp->link;
12303 	link_info->duplex = resp->duplex_cfg;
12304 	if (bp->hwrm_spec_code >= 0x10800)
12305 		link_info->duplex = resp->duplex_state;
12306 	link_info->pause = resp->pause;
12307 	link_info->auto_mode = resp->auto_mode;
12308 	link_info->auto_pause_setting = resp->auto_pause;
12309 	link_info->lp_pause = resp->link_partner_adv_pause;
12310 	link_info->force_pause_setting = resp->force_pause;
12311 	link_info->duplex_setting = resp->duplex_cfg;
12312 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12313 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12314 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12315 			link_info->active_lanes = resp->active_lanes;
12316 	} else {
12317 		link_info->link_speed = 0;
12318 		link_info->active_lanes = 0;
12319 	}
12320 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12321 	link_info->force_pam4_link_speed =
12322 		le16_to_cpu(resp->force_pam4_link_speed);
12323 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12324 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12325 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12326 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12327 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12328 	link_info->auto_pam4_link_speeds =
12329 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12330 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12331 	link_info->lp_auto_link_speeds =
12332 		le16_to_cpu(resp->link_partner_adv_speeds);
12333 	link_info->lp_auto_pam4_link_speeds =
12334 		resp->link_partner_pam4_adv_speeds;
12335 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12336 	link_info->phy_ver[0] = resp->phy_maj;
12337 	link_info->phy_ver[1] = resp->phy_min;
12338 	link_info->phy_ver[2] = resp->phy_bld;
12339 	link_info->media_type = resp->media_type;
12340 	link_info->phy_type = resp->phy_type;
12341 	link_info->transceiver = resp->xcvr_pkg_type;
12342 	link_info->phy_addr = resp->eee_config_phy_addr &
12343 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12344 	link_info->module_status = resp->module_status;
12345 	link_info->link_down_reason = resp->link_down_reason;
12346 
12347 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12348 		struct ethtool_keee *eee = &bp->eee;
12349 		u16 fw_speeds;
12350 
12351 		eee->eee_active = 0;
12352 		if (resp->eee_config_phy_addr &
12353 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12354 			eee->eee_active = 1;
12355 			fw_speeds = le16_to_cpu(
12356 				resp->link_partner_adv_eee_link_speed_mask);
12357 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12358 		}
12359 
12360 		/* Pull initial EEE config */
12361 		if (!chng_link_state) {
12362 			if (resp->eee_config_phy_addr &
12363 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12364 				eee->eee_enabled = 1;
12365 
12366 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12367 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12368 
12369 			if (resp->eee_config_phy_addr &
12370 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12371 				__le32 tmr;
12372 
12373 				eee->tx_lpi_enabled = 1;
12374 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12375 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12376 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12377 			}
12378 		}
12379 	}
12380 
12381 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12382 	if (bp->hwrm_spec_code >= 0x10504) {
12383 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12384 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12385 	}
12386 	/* TODO: need to add more logic to report VF link */
12387 	if (chng_link_state) {
12388 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12389 			link_info->link_state = BNXT_LINK_STATE_UP;
12390 		else
12391 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12392 		if (link_state != link_info->link_state)
12393 			bnxt_report_link(bp);
12394 	} else {
12395 		/* always link down if not require to update link state */
12396 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12397 	}
12398 	hwrm_req_drop(bp, req);
12399 
12400 	if (!BNXT_PHY_CFG_ABLE(bp))
12401 		return 0;
12402 
12403 	support_changed = bnxt_support_speed_dropped(link_info);
12404 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12405 		bnxt_hwrm_set_link_setting(bp, true, false);
12406 	return 0;
12407 }
12408 
12409 static void bnxt_get_port_module_status(struct bnxt *bp)
12410 {
12411 	struct bnxt_link_info *link_info = &bp->link_info;
12412 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12413 	u8 module_status;
12414 
12415 	if (bnxt_update_link(bp, true))
12416 		return;
12417 
12418 	module_status = link_info->module_status;
12419 	switch (module_status) {
12420 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12421 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12422 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12423 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12424 			    bp->pf.port_id);
12425 		if (bp->hwrm_spec_code >= 0x10201) {
12426 			netdev_warn(bp->dev, "Module part number %s\n",
12427 				    resp->phy_vendor_partnumber);
12428 		}
12429 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12430 			netdev_warn(bp->dev, "TX is disabled\n");
12431 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12432 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12433 	}
12434 }
12435 
12436 static void
12437 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12438 {
12439 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12440 		if (bp->hwrm_spec_code >= 0x10201)
12441 			req->auto_pause =
12442 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12443 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12444 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12445 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12446 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12447 		req->enables |=
12448 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12449 	} else {
12450 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12451 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12452 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12453 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12454 		req->enables |=
12455 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12456 		if (bp->hwrm_spec_code >= 0x10201) {
12457 			req->auto_pause = req->force_pause;
12458 			req->enables |= cpu_to_le32(
12459 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12460 		}
12461 	}
12462 }
12463 
12464 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12465 {
12466 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12467 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12468 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12469 			req->enables |=
12470 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12471 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12472 		} else if (bp->link_info.advertising) {
12473 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12474 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12475 		}
12476 		if (bp->link_info.advertising_pam4) {
12477 			req->enables |=
12478 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12479 			req->auto_link_pam4_speed_mask =
12480 				cpu_to_le16(bp->link_info.advertising_pam4);
12481 		}
12482 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12483 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12484 	} else {
12485 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12486 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12487 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12488 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12489 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12490 				   (u32)bp->link_info.req_link_speed);
12491 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12492 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12493 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12494 		} else {
12495 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12496 		}
12497 	}
12498 
12499 	/* tell chimp that the setting takes effect immediately */
12500 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12501 }
12502 
12503 int bnxt_hwrm_set_pause(struct bnxt *bp)
12504 {
12505 	struct hwrm_port_phy_cfg_input *req;
12506 	int rc;
12507 
12508 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12509 	if (rc)
12510 		return rc;
12511 
12512 	bnxt_hwrm_set_pause_common(bp, req);
12513 
12514 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12515 	    bp->link_info.force_link_chng)
12516 		bnxt_hwrm_set_link_common(bp, req);
12517 
12518 	rc = hwrm_req_send(bp, req);
12519 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12520 		/* since changing of pause setting doesn't trigger any link
12521 		 * change event, the driver needs to update the current pause
12522 		 * result upon successfully return of the phy_cfg command
12523 		 */
12524 		bp->link_info.pause =
12525 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12526 		bp->link_info.auto_pause_setting = 0;
12527 		if (!bp->link_info.force_link_chng)
12528 			bnxt_report_link(bp);
12529 	}
12530 	bp->link_info.force_link_chng = false;
12531 	return rc;
12532 }
12533 
12534 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12535 			      struct hwrm_port_phy_cfg_input *req)
12536 {
12537 	struct ethtool_keee *eee = &bp->eee;
12538 
12539 	if (eee->eee_enabled) {
12540 		u16 eee_speeds;
12541 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12542 
12543 		if (eee->tx_lpi_enabled)
12544 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12545 		else
12546 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12547 
12548 		req->flags |= cpu_to_le32(flags);
12549 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12550 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12551 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12552 	} else {
12553 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12554 	}
12555 }
12556 
12557 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12558 {
12559 	struct hwrm_port_phy_cfg_input *req;
12560 	int rc;
12561 
12562 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12563 	if (rc)
12564 		return rc;
12565 
12566 	if (set_pause)
12567 		bnxt_hwrm_set_pause_common(bp, req);
12568 
12569 	bnxt_hwrm_set_link_common(bp, req);
12570 
12571 	if (set_eee)
12572 		bnxt_hwrm_set_eee(bp, req);
12573 	return hwrm_req_send(bp, req);
12574 }
12575 
12576 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12577 {
12578 	struct hwrm_port_phy_cfg_input *req;
12579 	int rc;
12580 
12581 	if (!BNXT_SINGLE_PF(bp))
12582 		return 0;
12583 
12584 	if (pci_num_vf(bp->pdev) &&
12585 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12586 		return 0;
12587 
12588 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12589 	if (rc)
12590 		return rc;
12591 
12592 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12593 	rc = hwrm_req_send(bp, req);
12594 	if (!rc) {
12595 		mutex_lock(&bp->link_lock);
12596 		/* Device is not obliged link down in certain scenarios, even
12597 		 * when forced. Setting the state unknown is consistent with
12598 		 * driver startup and will force link state to be reported
12599 		 * during subsequent open based on PORT_PHY_QCFG.
12600 		 */
12601 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12602 		mutex_unlock(&bp->link_lock);
12603 	}
12604 	return rc;
12605 }
12606 
12607 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12608 {
12609 #ifdef CONFIG_TEE_BNXT_FW
12610 	int rc = tee_bnxt_fw_load();
12611 
12612 	if (rc)
12613 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12614 
12615 	return rc;
12616 #else
12617 	netdev_err(bp->dev, "OP-TEE not supported\n");
12618 	return -ENODEV;
12619 #endif
12620 }
12621 
12622 static int bnxt_try_recover_fw(struct bnxt *bp)
12623 {
12624 	if (bp->fw_health && bp->fw_health->status_reliable) {
12625 		int retry = 0, rc;
12626 		u32 sts;
12627 
12628 		do {
12629 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12630 			rc = bnxt_hwrm_poll(bp);
12631 			if (!BNXT_FW_IS_BOOTING(sts) &&
12632 			    !BNXT_FW_IS_RECOVERING(sts))
12633 				break;
12634 			retry++;
12635 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12636 
12637 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12638 			netdev_err(bp->dev,
12639 				   "Firmware not responding, status: 0x%x\n",
12640 				   sts);
12641 			rc = -ENODEV;
12642 		}
12643 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12644 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12645 			return bnxt_fw_reset_via_optee(bp);
12646 		}
12647 		return rc;
12648 	}
12649 
12650 	return -ENODEV;
12651 }
12652 
12653 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12654 {
12655 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12656 
12657 	if (!BNXT_NEW_RM(bp))
12658 		return; /* no resource reservations required */
12659 
12660 	hw_resc->resv_cp_rings = 0;
12661 	hw_resc->resv_stat_ctxs = 0;
12662 	hw_resc->resv_irqs = 0;
12663 	hw_resc->resv_tx_rings = 0;
12664 	hw_resc->resv_rx_rings = 0;
12665 	hw_resc->resv_hw_ring_grps = 0;
12666 	hw_resc->resv_vnics = 0;
12667 	hw_resc->resv_rsscos_ctxs = 0;
12668 	if (!fw_reset) {
12669 		bp->tx_nr_rings = 0;
12670 		bp->rx_nr_rings = 0;
12671 	}
12672 }
12673 
12674 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12675 {
12676 	int rc;
12677 
12678 	if (!BNXT_NEW_RM(bp))
12679 		return 0; /* no resource reservations required */
12680 
12681 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12682 	if (rc)
12683 		netdev_err(bp->dev, "resc_qcaps failed\n");
12684 
12685 	bnxt_clear_reservations(bp, fw_reset);
12686 
12687 	return rc;
12688 }
12689 
12690 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12691 {
12692 	struct hwrm_func_drv_if_change_output *resp;
12693 	struct hwrm_func_drv_if_change_input *req;
12694 	bool resc_reinit = false;
12695 	bool caps_change = false;
12696 	int rc, retry = 0;
12697 	bool fw_reset;
12698 	u32 flags = 0;
12699 
12700 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12701 	bp->fw_reset_state = 0;
12702 
12703 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12704 		return 0;
12705 
12706 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12707 	if (rc)
12708 		return rc;
12709 
12710 	if (up)
12711 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12712 	resp = hwrm_req_hold(bp, req);
12713 
12714 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12715 	while (retry < BNXT_FW_IF_RETRY) {
12716 		rc = hwrm_req_send(bp, req);
12717 		if (rc != -EAGAIN)
12718 			break;
12719 
12720 		msleep(50);
12721 		retry++;
12722 	}
12723 
12724 	if (rc == -EAGAIN) {
12725 		hwrm_req_drop(bp, req);
12726 		return rc;
12727 	} else if (!rc) {
12728 		flags = le32_to_cpu(resp->flags);
12729 	} else if (up) {
12730 		rc = bnxt_try_recover_fw(bp);
12731 		fw_reset = true;
12732 	}
12733 	hwrm_req_drop(bp, req);
12734 	if (rc)
12735 		return rc;
12736 
12737 	if (!up) {
12738 		bnxt_inv_fw_health_reg(bp);
12739 		return 0;
12740 	}
12741 
12742 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12743 		resc_reinit = true;
12744 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12745 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12746 		fw_reset = true;
12747 	else
12748 		bnxt_remap_fw_health_regs(bp);
12749 
12750 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12751 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12752 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12753 		return -ENODEV;
12754 	}
12755 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12756 		caps_change = true;
12757 
12758 	if (resc_reinit || fw_reset || caps_change) {
12759 		if (fw_reset || caps_change) {
12760 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12761 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12762 				bnxt_ulp_irq_stop(bp);
12763 			bnxt_free_ctx_mem(bp, false);
12764 			bnxt_dcb_free(bp);
12765 			rc = bnxt_fw_init_one(bp);
12766 			if (rc) {
12767 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12768 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12769 				return rc;
12770 			}
12771 			/* IRQ will be initialized later in bnxt_request_irq()*/
12772 			bnxt_clear_int_mode(bp);
12773 		}
12774 		rc = bnxt_cancel_reservations(bp, fw_reset);
12775 	}
12776 	return rc;
12777 }
12778 
12779 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12780 {
12781 	struct hwrm_port_led_qcaps_output *resp;
12782 	struct hwrm_port_led_qcaps_input *req;
12783 	struct bnxt_pf_info *pf = &bp->pf;
12784 	int rc;
12785 
12786 	bp->num_leds = 0;
12787 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12788 		return 0;
12789 
12790 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12791 	if (rc)
12792 		return rc;
12793 
12794 	req->port_id = cpu_to_le16(pf->port_id);
12795 	resp = hwrm_req_hold(bp, req);
12796 	rc = hwrm_req_send(bp, req);
12797 	if (rc) {
12798 		hwrm_req_drop(bp, req);
12799 		return rc;
12800 	}
12801 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12802 		int i;
12803 
12804 		bp->num_leds = resp->num_leds;
12805 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12806 						 bp->num_leds);
12807 		for (i = 0; i < bp->num_leds; i++) {
12808 			struct bnxt_led_info *led = &bp->leds[i];
12809 			__le16 caps = led->led_state_caps;
12810 
12811 			if (!led->led_group_id ||
12812 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12813 				bp->num_leds = 0;
12814 				break;
12815 			}
12816 		}
12817 	}
12818 	hwrm_req_drop(bp, req);
12819 	return 0;
12820 }
12821 
12822 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12823 {
12824 	struct hwrm_wol_filter_alloc_output *resp;
12825 	struct hwrm_wol_filter_alloc_input *req;
12826 	int rc;
12827 
12828 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12829 	if (rc)
12830 		return rc;
12831 
12832 	req->port_id = cpu_to_le16(bp->pf.port_id);
12833 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12834 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12835 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12836 
12837 	resp = hwrm_req_hold(bp, req);
12838 	rc = hwrm_req_send(bp, req);
12839 	if (!rc)
12840 		bp->wol_filter_id = resp->wol_filter_id;
12841 	hwrm_req_drop(bp, req);
12842 	return rc;
12843 }
12844 
12845 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12846 {
12847 	struct hwrm_wol_filter_free_input *req;
12848 	int rc;
12849 
12850 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12851 	if (rc)
12852 		return rc;
12853 
12854 	req->port_id = cpu_to_le16(bp->pf.port_id);
12855 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12856 	req->wol_filter_id = bp->wol_filter_id;
12857 
12858 	return hwrm_req_send(bp, req);
12859 }
12860 
12861 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12862 {
12863 	struct hwrm_wol_filter_qcfg_output *resp;
12864 	struct hwrm_wol_filter_qcfg_input *req;
12865 	u16 next_handle = 0;
12866 	int rc;
12867 
12868 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12869 	if (rc)
12870 		return rc;
12871 
12872 	req->port_id = cpu_to_le16(bp->pf.port_id);
12873 	req->handle = cpu_to_le16(handle);
12874 	resp = hwrm_req_hold(bp, req);
12875 	rc = hwrm_req_send(bp, req);
12876 	if (!rc) {
12877 		next_handle = le16_to_cpu(resp->next_handle);
12878 		if (next_handle != 0) {
12879 			if (resp->wol_type ==
12880 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12881 				bp->wol = 1;
12882 				bp->wol_filter_id = resp->wol_filter_id;
12883 			}
12884 		}
12885 	}
12886 	hwrm_req_drop(bp, req);
12887 	return next_handle;
12888 }
12889 
12890 static void bnxt_get_wol_settings(struct bnxt *bp)
12891 {
12892 	u16 handle = 0;
12893 
12894 	bp->wol = 0;
12895 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12896 		return;
12897 
12898 	do {
12899 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12900 	} while (handle && handle != 0xffff);
12901 }
12902 
12903 static bool bnxt_eee_config_ok(struct bnxt *bp)
12904 {
12905 	struct ethtool_keee *eee = &bp->eee;
12906 	struct bnxt_link_info *link_info = &bp->link_info;
12907 
12908 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12909 		return true;
12910 
12911 	if (eee->eee_enabled) {
12912 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12913 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12914 
12915 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12916 
12917 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12918 			eee->eee_enabled = 0;
12919 			return false;
12920 		}
12921 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12922 			linkmode_and(eee->advertised, advertising,
12923 				     eee->supported);
12924 			return false;
12925 		}
12926 	}
12927 	return true;
12928 }
12929 
12930 static int bnxt_update_phy_setting(struct bnxt *bp)
12931 {
12932 	int rc;
12933 	bool update_link = false;
12934 	bool update_pause = false;
12935 	bool update_eee = false;
12936 	struct bnxt_link_info *link_info = &bp->link_info;
12937 
12938 	rc = bnxt_update_link(bp, true);
12939 	if (rc) {
12940 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12941 			   rc);
12942 		return rc;
12943 	}
12944 	if (!BNXT_SINGLE_PF(bp))
12945 		return 0;
12946 
12947 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12948 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12949 	    link_info->req_flow_ctrl)
12950 		update_pause = true;
12951 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12952 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12953 		update_pause = true;
12954 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12955 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12956 			update_link = true;
12957 		if (bnxt_force_speed_updated(link_info))
12958 			update_link = true;
12959 		if (link_info->req_duplex != link_info->duplex_setting)
12960 			update_link = true;
12961 	} else {
12962 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12963 			update_link = true;
12964 		if (bnxt_auto_speed_updated(link_info))
12965 			update_link = true;
12966 	}
12967 
12968 	/* The last close may have shutdown the link, so need to call
12969 	 * PHY_CFG to bring it back up.
12970 	 */
12971 	if (!BNXT_LINK_IS_UP(bp))
12972 		update_link = true;
12973 
12974 	if (!bnxt_eee_config_ok(bp))
12975 		update_eee = true;
12976 
12977 	if (update_link)
12978 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12979 	else if (update_pause)
12980 		rc = bnxt_hwrm_set_pause(bp);
12981 	if (rc) {
12982 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12983 			   rc);
12984 		return rc;
12985 	}
12986 
12987 	return rc;
12988 }
12989 
12990 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12991 
12992 static int bnxt_reinit_after_abort(struct bnxt *bp)
12993 {
12994 	int rc;
12995 
12996 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12997 		return -EBUSY;
12998 
12999 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
13000 		return -ENODEV;
13001 
13002 	rc = bnxt_fw_init_one(bp);
13003 	if (!rc) {
13004 		bnxt_clear_int_mode(bp);
13005 		rc = bnxt_init_int_mode(bp);
13006 		if (!rc) {
13007 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13008 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
13009 		}
13010 	}
13011 	return rc;
13012 }
13013 
13014 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
13015 {
13016 	struct bnxt_ntuple_filter *ntp_fltr;
13017 	struct bnxt_l2_filter *l2_fltr;
13018 
13019 	if (list_empty(&fltr->list))
13020 		return;
13021 
13022 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
13023 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
13024 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
13025 		atomic_inc(&l2_fltr->refcnt);
13026 		ntp_fltr->l2_fltr = l2_fltr;
13027 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
13028 			bnxt_del_ntp_filter(bp, ntp_fltr);
13029 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
13030 				   fltr->sw_id);
13031 		}
13032 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
13033 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
13034 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
13035 			bnxt_del_l2_filter(bp, l2_fltr);
13036 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
13037 				   fltr->sw_id);
13038 		}
13039 	}
13040 }
13041 
13042 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
13043 {
13044 	struct bnxt_filter_base *usr_fltr, *tmp;
13045 
13046 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
13047 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
13048 }
13049 
13050 static int bnxt_set_xps_mapping(struct bnxt *bp)
13051 {
13052 	int numa_node = dev_to_node(&bp->pdev->dev);
13053 	unsigned int q_idx, map_idx, cpu, i;
13054 	const struct cpumask *cpu_mask_ptr;
13055 	int nr_cpus = num_online_cpus();
13056 	cpumask_t *q_map;
13057 	int rc = 0;
13058 
13059 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
13060 	if (!q_map)
13061 		return -ENOMEM;
13062 
13063 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
13064 	 * Each TC has the same number of TX queues. The nth TX queue for each
13065 	 * TC will have the same CPU mask.
13066 	 */
13067 	for (i = 0; i < nr_cpus; i++) {
13068 		map_idx = i % bp->tx_nr_rings_per_tc;
13069 		cpu = cpumask_local_spread(i, numa_node);
13070 		cpu_mask_ptr = get_cpu_mask(cpu);
13071 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
13072 	}
13073 
13074 	/* Register CPU mask for each TX queue except the ones marked for XDP */
13075 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
13076 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
13077 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
13078 		if (rc) {
13079 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
13080 				    q_idx);
13081 			break;
13082 		}
13083 	}
13084 
13085 	kfree(q_map);
13086 
13087 	return rc;
13088 }
13089 
13090 static int bnxt_tx_nr_rings(struct bnxt *bp)
13091 {
13092 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
13093 			    bp->tx_nr_rings_per_tc;
13094 }
13095 
13096 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
13097 {
13098 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
13099 }
13100 
13101 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
13102 {
13103 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13104 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13105 }
13106 
13107 static void bnxt_adj_tx_rings(struct bnxt *bp)
13108 {
13109 	/* Make adjustments if reserved TX rings are less than requested */
13110 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13111 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13112 	if (bp->tx_nr_rings_xdp)
13113 		bnxt_set_xdp_tx_rings(bp);
13114 }
13115 
13116 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13117 {
13118 	int rc = 0;
13119 
13120 	netif_carrier_off(bp->dev);
13121 	if (irq_re_init) {
13122 		/* Reserve rings now if none were reserved at driver probe. */
13123 		rc = bnxt_init_dflt_ring_mode(bp);
13124 		if (rc) {
13125 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13126 			return rc;
13127 		}
13128 	}
13129 	rc = bnxt_reserve_rings(bp, irq_re_init);
13130 	if (rc)
13131 		return rc;
13132 
13133 	bnxt_adj_tx_rings(bp);
13134 	rc = bnxt_alloc_mem(bp, irq_re_init);
13135 	if (rc) {
13136 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13137 		goto open_err_free_mem;
13138 	}
13139 
13140 	if (irq_re_init) {
13141 		bnxt_init_napi(bp);
13142 		rc = bnxt_request_irq(bp);
13143 		if (rc) {
13144 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13145 			goto open_err_irq;
13146 		}
13147 	}
13148 
13149 	rc = bnxt_init_nic(bp, irq_re_init);
13150 	if (rc) {
13151 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13152 		goto open_err_irq;
13153 	}
13154 
13155 	bnxt_enable_napi(bp);
13156 	bnxt_debug_dev_init(bp);
13157 
13158 	if (link_re_init) {
13159 		mutex_lock(&bp->link_lock);
13160 		rc = bnxt_update_phy_setting(bp);
13161 		mutex_unlock(&bp->link_lock);
13162 		if (rc) {
13163 			netdev_warn(bp->dev, "failed to update phy settings\n");
13164 			if (BNXT_SINGLE_PF(bp)) {
13165 				bp->link_info.phy_retry = true;
13166 				bp->link_info.phy_retry_expires =
13167 					jiffies + 5 * HZ;
13168 			}
13169 		}
13170 	}
13171 
13172 	if (irq_re_init) {
13173 		udp_tunnel_nic_reset_ntf(bp->dev);
13174 		rc = bnxt_set_xps_mapping(bp);
13175 		if (rc)
13176 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13177 	}
13178 
13179 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13180 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13181 			static_branch_enable(&bnxt_xdp_locking_key);
13182 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13183 		static_branch_disable(&bnxt_xdp_locking_key);
13184 	}
13185 	set_bit(BNXT_STATE_OPEN, &bp->state);
13186 	bnxt_enable_int(bp);
13187 	/* Enable TX queues */
13188 	bnxt_tx_enable(bp);
13189 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13190 	/* Poll link status and check for SFP+ module status */
13191 	mutex_lock(&bp->link_lock);
13192 	bnxt_get_port_module_status(bp);
13193 	mutex_unlock(&bp->link_lock);
13194 
13195 	/* VF-reps may need to be re-opened after the PF is re-opened */
13196 	if (BNXT_PF(bp))
13197 		bnxt_vf_reps_open(bp);
13198 	bnxt_ptp_init_rtc(bp, true);
13199 	bnxt_ptp_cfg_tstamp_filters(bp);
13200 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13201 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13202 	bnxt_cfg_usr_fltrs(bp);
13203 	return 0;
13204 
13205 open_err_irq:
13206 	bnxt_del_napi(bp);
13207 
13208 open_err_free_mem:
13209 	bnxt_free_skbs(bp);
13210 	bnxt_free_irq(bp);
13211 	bnxt_free_mem(bp, true);
13212 	return rc;
13213 }
13214 
13215 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13216 {
13217 	int rc = 0;
13218 
13219 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13220 		rc = -EIO;
13221 	if (!rc)
13222 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13223 	if (rc) {
13224 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13225 		netif_close(bp->dev);
13226 	}
13227 	return rc;
13228 }
13229 
13230 /* netdev instance lock held, open the NIC half way by allocating all
13231  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13232  * for offline self tests.
13233  */
13234 int bnxt_half_open_nic(struct bnxt *bp)
13235 {
13236 	int rc = 0;
13237 
13238 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13239 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13240 		rc = -ENODEV;
13241 		goto half_open_err;
13242 	}
13243 
13244 	rc = bnxt_alloc_mem(bp, true);
13245 	if (rc) {
13246 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13247 		goto half_open_err;
13248 	}
13249 	bnxt_init_napi(bp);
13250 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13251 	rc = bnxt_init_nic(bp, true);
13252 	if (rc) {
13253 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13254 		bnxt_del_napi(bp);
13255 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13256 		goto half_open_err;
13257 	}
13258 	return 0;
13259 
13260 half_open_err:
13261 	bnxt_free_skbs(bp);
13262 	bnxt_free_mem(bp, true);
13263 	netif_close(bp->dev);
13264 	return rc;
13265 }
13266 
13267 /* netdev instance lock held, this call can only be made after a previous
13268  * successful call to bnxt_half_open_nic().
13269  */
13270 void bnxt_half_close_nic(struct bnxt *bp)
13271 {
13272 	bnxt_hwrm_resource_free(bp, false, true);
13273 	bnxt_del_napi(bp);
13274 	bnxt_free_skbs(bp);
13275 	bnxt_free_mem(bp, true);
13276 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13277 }
13278 
13279 void bnxt_reenable_sriov(struct bnxt *bp)
13280 {
13281 	if (BNXT_PF(bp)) {
13282 		struct bnxt_pf_info *pf = &bp->pf;
13283 		int n = pf->active_vfs;
13284 
13285 		if (n)
13286 			bnxt_cfg_hw_sriov(bp, &n, true);
13287 	}
13288 }
13289 
13290 static int bnxt_open(struct net_device *dev)
13291 {
13292 	struct bnxt *bp = netdev_priv(dev);
13293 	int rc;
13294 
13295 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13296 		rc = bnxt_reinit_after_abort(bp);
13297 		if (rc) {
13298 			if (rc == -EBUSY)
13299 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13300 			else
13301 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13302 			return -ENODEV;
13303 		}
13304 	}
13305 
13306 	rc = bnxt_hwrm_if_change(bp, true);
13307 	if (rc)
13308 		return rc;
13309 
13310 	rc = __bnxt_open_nic(bp, true, true);
13311 	if (rc) {
13312 		bnxt_hwrm_if_change(bp, false);
13313 	} else {
13314 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13315 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13316 				bnxt_queue_sp_work(bp,
13317 						   BNXT_RESTART_ULP_SP_EVENT);
13318 		}
13319 	}
13320 
13321 	return rc;
13322 }
13323 
13324 static bool bnxt_drv_busy(struct bnxt *bp)
13325 {
13326 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13327 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13328 }
13329 
13330 static void bnxt_get_ring_stats(struct bnxt *bp,
13331 				struct rtnl_link_stats64 *stats);
13332 
13333 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13334 			     bool link_re_init)
13335 {
13336 	/* Close the VF-reps before closing PF */
13337 	if (BNXT_PF(bp))
13338 		bnxt_vf_reps_close(bp);
13339 
13340 	/* Change device state to avoid TX queue wake up's */
13341 	bnxt_tx_disable(bp);
13342 
13343 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13344 	smp_mb__after_atomic();
13345 	while (bnxt_drv_busy(bp))
13346 		msleep(20);
13347 
13348 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13349 		bnxt_clear_rss_ctxs(bp);
13350 	/* Flush rings and disable interrupts */
13351 	bnxt_shutdown_nic(bp, irq_re_init);
13352 
13353 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13354 
13355 	bnxt_debug_dev_exit(bp);
13356 	bnxt_disable_napi(bp);
13357 	timer_delete_sync(&bp->timer);
13358 	bnxt_free_skbs(bp);
13359 
13360 	/* Save ring stats before shutdown */
13361 	if (bp->bnapi && irq_re_init) {
13362 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13363 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13364 	}
13365 	if (irq_re_init) {
13366 		bnxt_free_irq(bp);
13367 		bnxt_del_napi(bp);
13368 	}
13369 	bnxt_free_mem(bp, irq_re_init);
13370 }
13371 
13372 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13373 {
13374 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13375 		/* If we get here, it means firmware reset is in progress
13376 		 * while we are trying to close.  We can safely proceed with
13377 		 * the close because we are holding netdev instance lock.
13378 		 * Some firmware messages may fail as we proceed to close.
13379 		 * We set the ABORT_ERR flag here so that the FW reset thread
13380 		 * will later abort when it gets the netdev instance lock
13381 		 * and sees the flag.
13382 		 */
13383 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13384 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13385 	}
13386 
13387 #ifdef CONFIG_BNXT_SRIOV
13388 	if (bp->sriov_cfg) {
13389 		int rc;
13390 
13391 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13392 						      !bp->sriov_cfg,
13393 						      BNXT_SRIOV_CFG_WAIT_TMO);
13394 		if (!rc)
13395 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13396 		else if (rc < 0)
13397 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13398 	}
13399 #endif
13400 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13401 }
13402 
13403 static int bnxt_close(struct net_device *dev)
13404 {
13405 	struct bnxt *bp = netdev_priv(dev);
13406 
13407 	bnxt_close_nic(bp, true, true);
13408 	bnxt_hwrm_shutdown_link(bp);
13409 	bnxt_hwrm_if_change(bp, false);
13410 	return 0;
13411 }
13412 
13413 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13414 				   u16 *val)
13415 {
13416 	struct hwrm_port_phy_mdio_read_output *resp;
13417 	struct hwrm_port_phy_mdio_read_input *req;
13418 	int rc;
13419 
13420 	if (bp->hwrm_spec_code < 0x10a00)
13421 		return -EOPNOTSUPP;
13422 
13423 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13424 	if (rc)
13425 		return rc;
13426 
13427 	req->port_id = cpu_to_le16(bp->pf.port_id);
13428 	req->phy_addr = phy_addr;
13429 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13430 	if (mdio_phy_id_is_c45(phy_addr)) {
13431 		req->cl45_mdio = 1;
13432 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13433 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13434 		req->reg_addr = cpu_to_le16(reg);
13435 	}
13436 
13437 	resp = hwrm_req_hold(bp, req);
13438 	rc = hwrm_req_send(bp, req);
13439 	if (!rc)
13440 		*val = le16_to_cpu(resp->reg_data);
13441 	hwrm_req_drop(bp, req);
13442 	return rc;
13443 }
13444 
13445 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13446 				    u16 val)
13447 {
13448 	struct hwrm_port_phy_mdio_write_input *req;
13449 	int rc;
13450 
13451 	if (bp->hwrm_spec_code < 0x10a00)
13452 		return -EOPNOTSUPP;
13453 
13454 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13455 	if (rc)
13456 		return rc;
13457 
13458 	req->port_id = cpu_to_le16(bp->pf.port_id);
13459 	req->phy_addr = phy_addr;
13460 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13461 	if (mdio_phy_id_is_c45(phy_addr)) {
13462 		req->cl45_mdio = 1;
13463 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13464 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13465 		req->reg_addr = cpu_to_le16(reg);
13466 	}
13467 	req->reg_data = cpu_to_le16(val);
13468 
13469 	return hwrm_req_send(bp, req);
13470 }
13471 
13472 /* netdev instance lock held */
13473 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13474 {
13475 	struct mii_ioctl_data *mdio = if_mii(ifr);
13476 	struct bnxt *bp = netdev_priv(dev);
13477 	int rc;
13478 
13479 	switch (cmd) {
13480 	case SIOCGMIIPHY:
13481 		mdio->phy_id = bp->link_info.phy_addr;
13482 
13483 		fallthrough;
13484 	case SIOCGMIIREG: {
13485 		u16 mii_regval = 0;
13486 
13487 		if (!netif_running(dev))
13488 			return -EAGAIN;
13489 
13490 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13491 					     &mii_regval);
13492 		mdio->val_out = mii_regval;
13493 		return rc;
13494 	}
13495 
13496 	case SIOCSMIIREG:
13497 		if (!netif_running(dev))
13498 			return -EAGAIN;
13499 
13500 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13501 						mdio->val_in);
13502 
13503 	default:
13504 		/* do nothing */
13505 		break;
13506 	}
13507 	return -EOPNOTSUPP;
13508 }
13509 
13510 static void bnxt_get_ring_stats(struct bnxt *bp,
13511 				struct rtnl_link_stats64 *stats)
13512 {
13513 	int i;
13514 
13515 	for (i = 0; i < bp->cp_nr_rings; i++) {
13516 		struct bnxt_napi *bnapi = bp->bnapi[i];
13517 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13518 		u64 *sw = cpr->stats.sw_stats;
13519 
13520 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13521 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13522 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13523 
13524 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13525 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13526 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13527 
13528 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13529 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13530 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13531 
13532 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13533 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13534 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13535 
13536 		stats->rx_missed_errors +=
13537 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13538 
13539 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13540 
13541 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13542 
13543 		stats->rx_dropped +=
13544 			cpr->sw_stats->rx.rx_netpoll_discards +
13545 			cpr->sw_stats->rx.rx_oom_discards;
13546 	}
13547 }
13548 
13549 static void bnxt_add_prev_stats(struct bnxt *bp,
13550 				struct rtnl_link_stats64 *stats)
13551 {
13552 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13553 
13554 	stats->rx_packets += prev_stats->rx_packets;
13555 	stats->tx_packets += prev_stats->tx_packets;
13556 	stats->rx_bytes += prev_stats->rx_bytes;
13557 	stats->tx_bytes += prev_stats->tx_bytes;
13558 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13559 	stats->multicast += prev_stats->multicast;
13560 	stats->rx_dropped += prev_stats->rx_dropped;
13561 	stats->tx_dropped += prev_stats->tx_dropped;
13562 }
13563 
13564 static void
13565 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13566 {
13567 	struct bnxt *bp = netdev_priv(dev);
13568 
13569 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13570 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13571 	 * we check the BNXT_STATE_OPEN flag.
13572 	 */
13573 	smp_mb__after_atomic();
13574 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13575 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13576 		*stats = bp->net_stats_prev;
13577 		return;
13578 	}
13579 
13580 	bnxt_get_ring_stats(bp, stats);
13581 	bnxt_add_prev_stats(bp, stats);
13582 
13583 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13584 		u64 *rx = bp->port_stats.sw_stats;
13585 		u64 *tx = bp->port_stats.sw_stats +
13586 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13587 
13588 		stats->rx_crc_errors =
13589 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13590 		stats->rx_frame_errors =
13591 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13592 		stats->rx_length_errors =
13593 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13594 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13595 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13596 		stats->rx_errors =
13597 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13598 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13599 		stats->collisions =
13600 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13601 		stats->tx_fifo_errors =
13602 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13603 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13604 	}
13605 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13606 }
13607 
13608 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13609 					struct bnxt_total_ring_drv_stats *stats,
13610 					struct bnxt_cp_ring_info *cpr)
13611 {
13612 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13613 	u64 *hw_stats = cpr->stats.sw_stats;
13614 
13615 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13616 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13617 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13618 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13619 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13620 	stats->rx_total_ring_discards +=
13621 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13622 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13623 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13624 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13625 	stats->tx_total_ring_discards +=
13626 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13627 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13628 }
13629 
13630 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13631 			     struct bnxt_total_ring_drv_stats *stats)
13632 {
13633 	int i;
13634 
13635 	for (i = 0; i < bp->cp_nr_rings; i++)
13636 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13637 }
13638 
13639 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask,
13640 				 const struct netdev_hw_addr_list *mc)
13641 {
13642 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13643 	struct netdev_hw_addr *ha;
13644 	u8 *haddr;
13645 	int mc_count = 0;
13646 	bool update = false;
13647 	int off = 0;
13648 
13649 	netdev_hw_addr_list_for_each(ha, mc) {
13650 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13651 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13652 			vnic->mc_list_count = 0;
13653 			return false;
13654 		}
13655 		haddr = ha->addr;
13656 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13657 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13658 			update = true;
13659 		}
13660 		off += ETH_ALEN;
13661 		mc_count++;
13662 	}
13663 	if (mc_count)
13664 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13665 
13666 	if (mc_count != vnic->mc_list_count) {
13667 		vnic->mc_list_count = mc_count;
13668 		update = true;
13669 	}
13670 	return update;
13671 }
13672 
13673 static bool bnxt_uc_list_updated(struct bnxt *bp,
13674 				 const struct netdev_hw_addr_list *uc)
13675 {
13676 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13677 	struct netdev_hw_addr *ha;
13678 	int off = 0;
13679 
13680 	if (netdev_hw_addr_list_count(uc) != (vnic->uc_filter_count - 1))
13681 		return true;
13682 
13683 	netdev_hw_addr_list_for_each(ha, uc) {
13684 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13685 			return true;
13686 
13687 		off += ETH_ALEN;
13688 	}
13689 	return false;
13690 }
13691 
13692 static int bnxt_set_rx_mode(struct net_device *dev,
13693 			    struct netdev_hw_addr_list *uc,
13694 			    struct netdev_hw_addr_list *mc)
13695 {
13696 	struct bnxt *bp = netdev_priv(dev);
13697 	struct bnxt_vnic_info *vnic;
13698 	bool mc_update = false;
13699 	bool uc_update;
13700 	u32 mask;
13701 
13702 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13703 		return 0;
13704 
13705 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13706 	mask = vnic->rx_mask;
13707 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13708 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13709 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13710 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13711 
13712 	if (dev->flags & IFF_PROMISC)
13713 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13714 
13715 	uc_update = bnxt_uc_list_updated(bp, uc);
13716 
13717 	if (dev->flags & IFF_BROADCAST)
13718 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13719 	if (dev->flags & IFF_ALLMULTI) {
13720 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13721 		vnic->mc_list_count = 0;
13722 	} else if (dev->flags & IFF_MULTICAST) {
13723 		mc_update = bnxt_mc_list_updated(bp, &mask, mc);
13724 	}
13725 
13726 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13727 		vnic->rx_mask = mask;
13728 
13729 		return bnxt_cfg_rx_mode(bp, uc, uc_update);
13730 	}
13731 
13732 	return 0;
13733 }
13734 
13735 static int bnxt_cfg_rx_mode(struct bnxt *bp, struct netdev_hw_addr_list *uc,
13736 			    bool uc_update)
13737 {
13738 	struct net_device *dev = bp->dev;
13739 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13740 	struct netdev_hw_addr *ha;
13741 	int i, off = 0, rc;
13742 
13743 	if (!uc_update)
13744 		goto skip_uc;
13745 
13746 	for (i = 1; i < vnic->uc_filter_count; i++) {
13747 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13748 
13749 		bnxt_hwrm_l2_filter_free(bp, fltr);
13750 		bnxt_del_l2_filter(bp, fltr);
13751 	}
13752 
13753 	vnic->uc_filter_count = 1;
13754 
13755 	netif_addr_lock_bh(dev);
13756 	if (netdev_hw_addr_list_count(uc) > (BNXT_MAX_UC_ADDRS - 1)) {
13757 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13758 	} else {
13759 		netdev_hw_addr_list_for_each(ha, uc) {
13760 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13761 			off += ETH_ALEN;
13762 			vnic->uc_filter_count++;
13763 		}
13764 	}
13765 	netif_addr_unlock_bh(dev);
13766 
13767 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13768 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13769 		if (rc) {
13770 			if (BNXT_VF(bp) && rc == -ENODEV) {
13771 				netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13772 				rc = -EAGAIN;
13773 			} else if (rc == -EAGAIN) {
13774 				netdev_warn(bp->dev, "FW busy while setting vnic filter, will retry\n");
13775 			} else {
13776 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13777 			}
13778 			vnic->uc_filter_count = i;
13779 			return rc;
13780 		}
13781 	}
13782 
13783 skip_uc:
13784 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13785 	    !bnxt_promisc_ok(bp))
13786 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13787 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13788 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13789 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13790 			    rc);
13791 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13792 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13793 		vnic->mc_list_count = 0;
13794 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13795 	}
13796 	if (rc)
13797 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13798 			   rc);
13799 
13800 	return rc;
13801 }
13802 
13803 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13804 {
13805 #ifdef CONFIG_BNXT_SRIOV
13806 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13807 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13808 
13809 		/* No minimum rings were provisioned by the PF.  Don't
13810 		 * reserve rings by default when device is down.
13811 		 */
13812 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13813 			return true;
13814 
13815 		if (!netif_running(bp->dev))
13816 			return false;
13817 	}
13818 #endif
13819 	return true;
13820 }
13821 
13822 /* If the chip and firmware supports RFS */
13823 static bool bnxt_rfs_supported(struct bnxt *bp)
13824 {
13825 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13826 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13827 			return true;
13828 		return false;
13829 	}
13830 	/* 212 firmware is broken for aRFS */
13831 	if (BNXT_FW_MAJ(bp) == 212)
13832 		return false;
13833 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13834 		return true;
13835 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13836 		return true;
13837 	return false;
13838 }
13839 
13840 /* If runtime conditions support RFS */
13841 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13842 {
13843 	struct bnxt_hw_rings hwr = {0};
13844 	int max_vnics, max_rss_ctxs;
13845 
13846 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13847 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13848 		return bnxt_rfs_supported(bp);
13849 
13850 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13851 		return false;
13852 
13853 	hwr.grp = bp->rx_nr_rings;
13854 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13855 	if (new_rss_ctx)
13856 		hwr.vnic++;
13857 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13858 	max_vnics = bnxt_get_max_func_vnics(bp);
13859 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13860 
13861 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13862 		if (bp->rx_nr_rings > 1)
13863 			netdev_warn(bp->dev,
13864 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13865 				    min(max_rss_ctxs - 1, max_vnics - 1));
13866 		return false;
13867 	}
13868 
13869 	if (!BNXT_NEW_RM(bp))
13870 		return true;
13871 
13872 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13873 	 * issue that will mess up the default VNIC if we reduce the
13874 	 * reservations.
13875 	 */
13876 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13877 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13878 		return true;
13879 
13880 	bnxt_hwrm_reserve_rings(bp, &hwr);
13881 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13882 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13883 		return true;
13884 
13885 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13886 	hwr.vnic = 1;
13887 	hwr.rss_ctx = 0;
13888 	bnxt_hwrm_reserve_rings(bp, &hwr);
13889 	return false;
13890 }
13891 
13892 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13893 					   netdev_features_t features)
13894 {
13895 	struct bnxt *bp = netdev_priv(dev);
13896 	netdev_features_t vlan_features;
13897 
13898 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13899 		features &= ~NETIF_F_NTUPLE;
13900 
13901 	if ((features & NETIF_F_GSO_UDP_L4) &&
13902 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
13903 	    bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS)
13904 		features &= ~NETIF_F_GSO_UDP_L4;
13905 
13906 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13907 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13908 
13909 	if (!(features & NETIF_F_GRO))
13910 		features &= ~NETIF_F_GRO_HW;
13911 
13912 	if (features & NETIF_F_GRO_HW)
13913 		features &= ~NETIF_F_LRO;
13914 
13915 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13916 	 * turned on or off together.
13917 	 */
13918 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13919 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13920 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13921 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13922 		else if (vlan_features)
13923 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13924 	}
13925 #ifdef CONFIG_BNXT_SRIOV
13926 	if (BNXT_VF(bp) && bp->vf.vlan)
13927 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13928 #endif
13929 	return features;
13930 }
13931 
13932 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13933 				bool link_re_init, u32 flags, bool update_tpa)
13934 {
13935 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13936 	bp->flags = flags;
13937 	if (update_tpa)
13938 		bnxt_set_ring_params(bp);
13939 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13940 }
13941 
13942 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13943 {
13944 	bool update_tpa = false, update_ntuple = false;
13945 	struct bnxt *bp = netdev_priv(dev);
13946 	u32 flags = bp->flags;
13947 	u32 changes;
13948 	int rc = 0;
13949 	bool re_init = false;
13950 
13951 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
13952 				   bnxt_min_tx_desc_cnt(bp, features));
13953 
13954 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13955 	if (features & NETIF_F_GRO_HW)
13956 		flags |= BNXT_FLAG_GRO;
13957 	else if (features & NETIF_F_LRO)
13958 		flags |= BNXT_FLAG_LRO;
13959 
13960 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13961 		flags &= ~BNXT_FLAG_TPA;
13962 
13963 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13964 		flags |= BNXT_FLAG_STRIP_VLAN;
13965 
13966 	if (features & NETIF_F_NTUPLE)
13967 		flags |= BNXT_FLAG_RFS;
13968 	else
13969 		bnxt_clear_usr_fltrs(bp, true);
13970 
13971 	changes = flags ^ bp->flags;
13972 	if (changes & BNXT_FLAG_TPA) {
13973 		update_tpa = true;
13974 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13975 		    (flags & BNXT_FLAG_TPA) == 0 ||
13976 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13977 			re_init = true;
13978 	}
13979 
13980 	if (changes & ~BNXT_FLAG_TPA)
13981 		re_init = true;
13982 
13983 	if (changes & BNXT_FLAG_RFS)
13984 		update_ntuple = true;
13985 
13986 	if (flags != bp->flags) {
13987 		u32 old_flags = bp->flags;
13988 
13989 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13990 			bp->flags = flags;
13991 			if (update_tpa)
13992 				bnxt_set_ring_params(bp);
13993 			return rc;
13994 		}
13995 
13996 		if (update_ntuple)
13997 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13998 
13999 		if (re_init)
14000 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
14001 
14002 		if (update_tpa) {
14003 			bp->flags = flags;
14004 			rc = bnxt_set_tpa(bp,
14005 					  (flags & BNXT_FLAG_TPA) ?
14006 					  true : false);
14007 			if (rc)
14008 				bp->flags = old_flags;
14009 		}
14010 	}
14011 	return rc;
14012 }
14013 
14014 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
14015 			      u8 **nextp)
14016 {
14017 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
14018 	int hdr_count = 0;
14019 	u8 *nexthdr;
14020 	int start;
14021 
14022 	/* Check that there are at most 2 IPv6 extension headers, no
14023 	 * fragment header, and each is <= 64 bytes.
14024 	 */
14025 	start = nw_off + sizeof(*ip6h);
14026 	nexthdr = &ip6h->nexthdr;
14027 	while (ipv6_ext_hdr(*nexthdr)) {
14028 		struct ipv6_opt_hdr *hp;
14029 		int hdrlen;
14030 
14031 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
14032 		    *nexthdr == NEXTHDR_FRAGMENT)
14033 			return false;
14034 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
14035 					  skb_headlen(skb), NULL);
14036 		if (!hp)
14037 			return false;
14038 		if (*nexthdr == NEXTHDR_AUTH)
14039 			hdrlen = ipv6_authlen(hp);
14040 		else
14041 			hdrlen = ipv6_optlen(hp);
14042 
14043 		if (hdrlen > 64)
14044 			return false;
14045 
14046 		hdr_count++;
14047 		nexthdr = &hp->nexthdr;
14048 		start += hdrlen;
14049 	}
14050 	if (nextp) {
14051 		/* Caller will check inner protocol */
14052 		if (skb->encapsulation) {
14053 			*nextp = nexthdr;
14054 			return true;
14055 		}
14056 		*nextp = NULL;
14057 	}
14058 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
14059 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
14060 }
14061 
14062 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
14063 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
14064 {
14065 	struct udphdr *uh = udp_hdr(skb);
14066 	__be16 udp_port = uh->dest;
14067 
14068 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
14069 	    udp_port != bp->vxlan_gpe_port)
14070 		return false;
14071 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
14072 		struct ethhdr *eh = inner_eth_hdr(skb);
14073 
14074 		switch (eh->h_proto) {
14075 		case htons(ETH_P_IP):
14076 			return true;
14077 		case htons(ETH_P_IPV6):
14078 			return bnxt_exthdr_check(bp, skb,
14079 						 skb_inner_network_offset(skb),
14080 						 NULL);
14081 		}
14082 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
14083 		return true;
14084 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
14085 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14086 					 NULL);
14087 	}
14088 	return false;
14089 }
14090 
14091 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
14092 {
14093 	switch (l4_proto) {
14094 	case IPPROTO_UDP:
14095 		return bnxt_udp_tunl_check(bp, skb);
14096 	case IPPROTO_IPIP:
14097 		return true;
14098 	case IPPROTO_GRE: {
14099 		switch (skb->inner_protocol) {
14100 		default:
14101 			return false;
14102 		case htons(ETH_P_IP):
14103 			return true;
14104 		case htons(ETH_P_IPV6):
14105 			fallthrough;
14106 		}
14107 	}
14108 	case IPPROTO_IPV6:
14109 		/* Check ext headers of inner ipv6 */
14110 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14111 					 NULL);
14112 	}
14113 	return false;
14114 }
14115 
14116 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
14117 					     struct net_device *dev,
14118 					     netdev_features_t features)
14119 {
14120 	struct bnxt *bp = netdev_priv(dev);
14121 	u8 *l4_proto;
14122 
14123 	features = vlan_features_check(skb, features);
14124 	switch (vlan_get_protocol(skb)) {
14125 	case htons(ETH_P_IP):
14126 		if (!skb->encapsulation)
14127 			return features;
14128 		l4_proto = &ip_hdr(skb)->protocol;
14129 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14130 			return features;
14131 		break;
14132 	case htons(ETH_P_IPV6):
14133 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14134 				       &l4_proto))
14135 			break;
14136 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14137 			return features;
14138 		break;
14139 	}
14140 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14141 }
14142 
14143 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14144 			 u32 *reg_buf)
14145 {
14146 	struct hwrm_dbg_read_direct_output *resp;
14147 	struct hwrm_dbg_read_direct_input *req;
14148 	__le32 *dbg_reg_buf;
14149 	dma_addr_t mapping;
14150 	int rc, i;
14151 
14152 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14153 	if (rc)
14154 		return rc;
14155 
14156 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14157 					 &mapping);
14158 	if (!dbg_reg_buf) {
14159 		rc = -ENOMEM;
14160 		goto dbg_rd_reg_exit;
14161 	}
14162 
14163 	req->host_dest_addr = cpu_to_le64(mapping);
14164 
14165 	resp = hwrm_req_hold(bp, req);
14166 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14167 	req->read_len32 = cpu_to_le32(num_words);
14168 
14169 	rc = hwrm_req_send(bp, req);
14170 	if (rc || resp->error_code) {
14171 		rc = -EIO;
14172 		goto dbg_rd_reg_exit;
14173 	}
14174 	for (i = 0; i < num_words; i++)
14175 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14176 
14177 dbg_rd_reg_exit:
14178 	hwrm_req_drop(bp, req);
14179 	return rc;
14180 }
14181 
14182 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14183 				       u32 ring_id, u32 *prod, u32 *cons)
14184 {
14185 	struct hwrm_dbg_ring_info_get_output *resp;
14186 	struct hwrm_dbg_ring_info_get_input *req;
14187 	int rc;
14188 
14189 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14190 	if (rc)
14191 		return rc;
14192 
14193 	req->ring_type = ring_type;
14194 	req->fw_ring_id = cpu_to_le32(ring_id);
14195 	resp = hwrm_req_hold(bp, req);
14196 	rc = hwrm_req_send(bp, req);
14197 	if (!rc) {
14198 		*prod = le32_to_cpu(resp->producer_index);
14199 		*cons = le32_to_cpu(resp->consumer_index);
14200 	}
14201 	hwrm_req_drop(bp, req);
14202 	return rc;
14203 }
14204 
14205 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14206 {
14207 	struct bnxt_tx_ring_info *txr;
14208 	int i = bnapi->index, j;
14209 
14210 	bnxt_for_each_napi_tx(j, bnapi, txr)
14211 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14212 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14213 			    txr->tx_cons);
14214 }
14215 
14216 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14217 {
14218 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14219 	int i = bnapi->index;
14220 
14221 	if (!rxr)
14222 		return;
14223 
14224 	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",
14225 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14226 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14227 		    rxr->rx_sw_agg_prod);
14228 }
14229 
14230 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14231 {
14232 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14233 	int i = bnapi->index, j;
14234 
14235 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14236 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14237 	for (j = 0; j < cpr->cp_ring_count; j++) {
14238 		cpr2 = &cpr->cp_ring_arr[j];
14239 		if (!cpr2->bnapi)
14240 			continue;
14241 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14242 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14243 			    cpr2->cp_raw_cons);
14244 	}
14245 }
14246 
14247 static void bnxt_dbg_dump_states(struct bnxt *bp)
14248 {
14249 	int i;
14250 	struct bnxt_napi *bnapi;
14251 
14252 	for (i = 0; i < bp->cp_nr_rings; i++) {
14253 		bnapi = bp->bnapi[i];
14254 		if (netif_msg_drv(bp)) {
14255 			bnxt_dump_tx_sw_state(bnapi);
14256 			bnxt_dump_rx_sw_state(bnapi);
14257 			bnxt_dump_cp_sw_state(bnapi);
14258 		}
14259 	}
14260 }
14261 
14262 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14263 {
14264 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14265 	struct hwrm_ring_reset_input *req;
14266 	struct bnxt_napi *bnapi = rxr->bnapi;
14267 	struct bnxt_cp_ring_info *cpr;
14268 	u16 cp_ring_id;
14269 	int rc;
14270 
14271 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14272 	if (rc)
14273 		return rc;
14274 
14275 	cpr = &bnapi->cp_ring;
14276 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14277 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14278 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14279 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14280 	return hwrm_req_send_silent(bp, req);
14281 }
14282 
14283 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14284 {
14285 	if (!silent)
14286 		bnxt_dbg_dump_states(bp);
14287 	if (netif_running(bp->dev)) {
14288 		bnxt_close_nic(bp, !silent, false);
14289 		bnxt_open_nic(bp, !silent, false);
14290 	}
14291 }
14292 
14293 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14294 {
14295 	struct bnxt *bp = netdev_priv(dev);
14296 
14297 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14298 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14299 }
14300 
14301 static void bnxt_fw_health_check(struct bnxt *bp)
14302 {
14303 	struct bnxt_fw_health *fw_health = bp->fw_health;
14304 	struct pci_dev *pdev = bp->pdev;
14305 	u32 val;
14306 
14307 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14308 		return;
14309 
14310 	/* Make sure it is enabled before checking the tmr_counter. */
14311 	smp_rmb();
14312 	if (fw_health->tmr_counter) {
14313 		fw_health->tmr_counter--;
14314 		return;
14315 	}
14316 
14317 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14318 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14319 		fw_health->arrests++;
14320 		goto fw_reset;
14321 	}
14322 
14323 	fw_health->last_fw_heartbeat = val;
14324 
14325 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14326 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14327 		fw_health->discoveries++;
14328 		goto fw_reset;
14329 	}
14330 
14331 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14332 	return;
14333 
14334 fw_reset:
14335 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14336 }
14337 
14338 static void bnxt_timer(struct timer_list *t)
14339 {
14340 	struct bnxt *bp = timer_container_of(bp, t, timer);
14341 	struct net_device *dev = bp->dev;
14342 
14343 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14344 		return;
14345 
14346 	if (atomic_read(&bp->intr_sem) != 0)
14347 		goto bnxt_restart_timer;
14348 
14349 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14350 		bnxt_fw_health_check(bp);
14351 
14352 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14353 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14354 
14355 	if (bnxt_tc_flower_enabled(bp))
14356 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14357 
14358 #ifdef CONFIG_RFS_ACCEL
14359 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14360 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14361 #endif /*CONFIG_RFS_ACCEL*/
14362 
14363 	if (bp->link_info.phy_retry) {
14364 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14365 			bp->link_info.phy_retry = false;
14366 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14367 		} else {
14368 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14369 		}
14370 	}
14371 
14372 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14373 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14374 
14375 bnxt_restart_timer:
14376 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14377 }
14378 
14379 static void bnxt_lock_sp(struct bnxt *bp)
14380 {
14381 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14382 	 * set.  If the device is being closed, bnxt_close() may be holding
14383 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14384 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14385 	 * instance lock.
14386 	 */
14387 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14388 	netdev_lock(bp->dev);
14389 }
14390 
14391 static void bnxt_unlock_sp(struct bnxt *bp)
14392 {
14393 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14394 	netdev_unlock(bp->dev);
14395 }
14396 
14397 /* Same as bnxt_lock_sp() with additional rtnl_lock */
14398 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
14399 {
14400 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14401 	rtnl_lock();
14402 	netdev_lock(bp->dev);
14403 }
14404 
14405 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
14406 {
14407 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14408 	netdev_unlock(bp->dev);
14409 	rtnl_unlock();
14410 }
14411 
14412 /* Only called from bnxt_sp_task() */
14413 static void bnxt_reset(struct bnxt *bp, bool silent)
14414 {
14415 	bnxt_rtnl_lock_sp(bp);
14416 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14417 		bnxt_reset_task(bp, silent);
14418 	bnxt_rtnl_unlock_sp(bp);
14419 }
14420 
14421 /* Only called from bnxt_sp_task() */
14422 static void bnxt_rx_ring_reset(struct bnxt *bp)
14423 {
14424 	int i;
14425 
14426 	bnxt_rtnl_lock_sp(bp);
14427 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14428 		bnxt_rtnl_unlock_sp(bp);
14429 		return;
14430 	}
14431 	/* Disable and flush TPA before resetting the RX ring */
14432 	if (bp->flags & BNXT_FLAG_TPA)
14433 		bnxt_set_tpa(bp, false);
14434 	for (i = 0; i < bp->rx_nr_rings; i++) {
14435 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14436 		struct bnxt_cp_ring_info *cpr;
14437 		int rc;
14438 
14439 		if (!rxr->bnapi->in_reset)
14440 			continue;
14441 
14442 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14443 		if (rc) {
14444 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14445 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14446 			else
14447 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14448 					    rc);
14449 			bnxt_reset_task(bp, true);
14450 			break;
14451 		}
14452 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14453 		rxr->rx_prod = 0;
14454 		rxr->rx_agg_prod = 0;
14455 		rxr->rx_sw_agg_prod = 0;
14456 		rxr->rx_next_cons = 0;
14457 		rxr->bnapi->in_reset = false;
14458 		bnxt_alloc_one_rx_ring(bp, i);
14459 		cpr = &rxr->bnapi->cp_ring;
14460 		cpr->sw_stats->rx.rx_resets++;
14461 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14462 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14463 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14464 	}
14465 	if (bp->flags & BNXT_FLAG_TPA)
14466 		bnxt_set_tpa(bp, true);
14467 	bnxt_rtnl_unlock_sp(bp);
14468 }
14469 
14470 static void bnxt_fw_fatal_close(struct bnxt *bp)
14471 {
14472 	bnxt_tx_disable(bp);
14473 	bnxt_disable_napi(bp);
14474 	bnxt_disable_int_sync(bp);
14475 	bnxt_free_irq(bp);
14476 	bnxt_clear_int_mode(bp);
14477 	pci_disable_device(bp->pdev);
14478 }
14479 
14480 static void bnxt_fw_reset_close(struct bnxt *bp)
14481 {
14482 	/* When firmware is in fatal state, quiesce device and disable
14483 	 * bus master to prevent any potential bad DMAs before freeing
14484 	 * kernel memory.
14485 	 */
14486 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14487 		u16 val = 0;
14488 
14489 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14490 		if (val == 0xffff)
14491 			bp->fw_reset_min_dsecs = 0;
14492 		bnxt_fw_fatal_close(bp);
14493 	}
14494 	__bnxt_close_nic(bp, true, false);
14495 	bnxt_vf_reps_free(bp);
14496 	bnxt_clear_int_mode(bp);
14497 	bnxt_hwrm_func_drv_unrgtr(bp);
14498 	if (pci_is_enabled(bp->pdev))
14499 		pci_disable_device(bp->pdev);
14500 	bnxt_free_ctx_mem(bp, false);
14501 }
14502 
14503 static bool is_bnxt_fw_ok(struct bnxt *bp)
14504 {
14505 	struct bnxt_fw_health *fw_health = bp->fw_health;
14506 	bool no_heartbeat = false, has_reset = false;
14507 	u32 val;
14508 
14509 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14510 	if (val == fw_health->last_fw_heartbeat)
14511 		no_heartbeat = true;
14512 
14513 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14514 	if (val != fw_health->last_fw_reset_cnt)
14515 		has_reset = true;
14516 
14517 	if (!no_heartbeat && has_reset)
14518 		return true;
14519 
14520 	return false;
14521 }
14522 
14523 /* netdev instance lock is acquired before calling this function */
14524 static void bnxt_force_fw_reset(struct bnxt *bp)
14525 {
14526 	struct bnxt_fw_health *fw_health = bp->fw_health;
14527 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14528 	u32 wait_dsecs;
14529 
14530 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14531 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14532 		return;
14533 
14534 	/* we have to serialize with bnxt_refclk_read()*/
14535 	if (ptp) {
14536 		unsigned long flags;
14537 
14538 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14539 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14540 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14541 	} else {
14542 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14543 	}
14544 	bnxt_fw_reset_close(bp);
14545 	wait_dsecs = fw_health->master_func_wait_dsecs;
14546 	if (fw_health->primary) {
14547 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14548 			wait_dsecs = 0;
14549 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14550 	} else {
14551 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14552 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14553 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14554 	}
14555 
14556 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14557 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14558 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14559 }
14560 
14561 void bnxt_fw_exception(struct bnxt *bp)
14562 {
14563 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14564 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14565 	bnxt_ulp_stop(bp);
14566 	bnxt_lock_sp(bp);
14567 	bnxt_force_fw_reset(bp);
14568 	bnxt_unlock_sp(bp);
14569 }
14570 
14571 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14572  * < 0 on error.
14573  */
14574 static int bnxt_get_registered_vfs(struct bnxt *bp)
14575 {
14576 #ifdef CONFIG_BNXT_SRIOV
14577 	int rc;
14578 
14579 	if (!BNXT_PF(bp))
14580 		return 0;
14581 
14582 	rc = bnxt_hwrm_func_qcfg(bp);
14583 	if (rc) {
14584 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14585 		return rc;
14586 	}
14587 	if (bp->pf.registered_vfs)
14588 		return bp->pf.registered_vfs;
14589 	if (bp->sriov_cfg)
14590 		return 1;
14591 #endif
14592 	return 0;
14593 }
14594 
14595 void bnxt_fw_reset(struct bnxt *bp)
14596 {
14597 	bnxt_ulp_stop(bp);
14598 	bnxt_lock_sp(bp);
14599 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14600 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14601 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14602 		int n = 0, tmo;
14603 
14604 		/* we have to serialize with bnxt_refclk_read()*/
14605 		if (ptp) {
14606 			unsigned long flags;
14607 
14608 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14609 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14610 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14611 		} else {
14612 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14613 		}
14614 		if (bp->pf.active_vfs &&
14615 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14616 			n = bnxt_get_registered_vfs(bp);
14617 		if (n < 0) {
14618 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14619 				   n);
14620 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14621 			netif_close(bp->dev);
14622 			goto fw_reset_exit;
14623 		} else if (n > 0) {
14624 			u16 vf_tmo_dsecs = n * 10;
14625 
14626 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14627 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14628 			bp->fw_reset_state =
14629 				BNXT_FW_RESET_STATE_POLL_VF;
14630 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14631 			goto fw_reset_exit;
14632 		}
14633 		bnxt_fw_reset_close(bp);
14634 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14635 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14636 			tmo = HZ / 10;
14637 		} else {
14638 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14639 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14640 		}
14641 		bnxt_queue_fw_reset_work(bp, tmo);
14642 	}
14643 fw_reset_exit:
14644 	bnxt_unlock_sp(bp);
14645 }
14646 
14647 static void bnxt_chk_missed_irq(struct bnxt *bp)
14648 {
14649 	int i;
14650 
14651 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14652 		return;
14653 
14654 	for (i = 0; i < bp->cp_nr_rings; i++) {
14655 		struct bnxt_napi *bnapi = bp->bnapi[i];
14656 		struct bnxt_cp_ring_info *cpr;
14657 		u32 fw_ring_id;
14658 		int j;
14659 
14660 		if (!bnapi)
14661 			continue;
14662 
14663 		cpr = &bnapi->cp_ring;
14664 		for (j = 0; j < cpr->cp_ring_count; j++) {
14665 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14666 			u32 val[2];
14667 
14668 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14669 				continue;
14670 
14671 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14672 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14673 				continue;
14674 			}
14675 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14676 			bnxt_dbg_hwrm_ring_info_get(bp,
14677 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14678 				fw_ring_id, &val[0], &val[1]);
14679 			cpr->sw_stats->cmn.missed_irqs++;
14680 		}
14681 	}
14682 }
14683 
14684 static void bnxt_cfg_ntp_filters(struct bnxt *);
14685 
14686 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14687 {
14688 	struct bnxt_link_info *link_info = &bp->link_info;
14689 
14690 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14691 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14692 		if (bp->hwrm_spec_code >= 0x10201) {
14693 			if (link_info->auto_pause_setting &
14694 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14695 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14696 		} else {
14697 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14698 		}
14699 		bnxt_set_auto_speed(link_info);
14700 	} else {
14701 		bnxt_set_force_speed(link_info);
14702 		link_info->req_duplex = link_info->duplex_setting;
14703 	}
14704 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14705 		link_info->req_flow_ctrl =
14706 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14707 	else
14708 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14709 }
14710 
14711 static void bnxt_fw_echo_reply(struct bnxt *bp)
14712 {
14713 	struct bnxt_fw_health *fw_health = bp->fw_health;
14714 	struct hwrm_func_echo_response_input *req;
14715 	int rc;
14716 
14717 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14718 	if (rc)
14719 		return;
14720 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14721 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14722 	hwrm_req_send(bp, req);
14723 }
14724 
14725 static void bnxt_ulp_restart(struct bnxt *bp)
14726 {
14727 	bnxt_ulp_stop(bp);
14728 	bnxt_ulp_start(bp);
14729 }
14730 
14731 static void bnxt_sp_task(struct work_struct *work)
14732 {
14733 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14734 
14735 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14736 	smp_mb__after_atomic();
14737 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14738 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14739 		return;
14740 	}
14741 
14742 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14743 		bnxt_ulp_restart(bp);
14744 		bnxt_reenable_sriov(bp);
14745 	}
14746 
14747 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14748 		bnxt_cfg_ntp_filters(bp);
14749 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14750 		bnxt_hwrm_exec_fwd_req(bp);
14751 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14752 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14753 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14754 		bnxt_hwrm_port_qstats(bp, 0);
14755 		bnxt_hwrm_port_qstats_ext(bp, 0);
14756 		bnxt_accumulate_all_stats(bp);
14757 	}
14758 
14759 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14760 		int rc;
14761 
14762 		mutex_lock(&bp->link_lock);
14763 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14764 				       &bp->sp_event))
14765 			bnxt_hwrm_phy_qcaps(bp);
14766 
14767 		rc = bnxt_update_link(bp, true);
14768 		if (rc)
14769 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14770 				   rc);
14771 
14772 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14773 				       &bp->sp_event))
14774 			bnxt_init_ethtool_link_settings(bp);
14775 		mutex_unlock(&bp->link_lock);
14776 	}
14777 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14778 		int rc;
14779 
14780 		mutex_lock(&bp->link_lock);
14781 		rc = bnxt_update_phy_setting(bp);
14782 		mutex_unlock(&bp->link_lock);
14783 		if (rc) {
14784 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14785 		} else {
14786 			bp->link_info.phy_retry = false;
14787 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14788 		}
14789 	}
14790 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14791 		mutex_lock(&bp->link_lock);
14792 		bnxt_get_port_module_status(bp);
14793 		mutex_unlock(&bp->link_lock);
14794 	}
14795 
14796 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14797 		bnxt_tc_flow_stats_work(bp);
14798 
14799 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14800 		bnxt_chk_missed_irq(bp);
14801 
14802 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14803 		bnxt_fw_echo_reply(bp);
14804 
14805 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14806 		bnxt_hwmon_notify_event(bp);
14807 
14808 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14809 	 * must be the last functions to be called before exiting.
14810 	 */
14811 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14812 		bnxt_reset(bp, false);
14813 
14814 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14815 		bnxt_reset(bp, true);
14816 
14817 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14818 		bnxt_rx_ring_reset(bp);
14819 
14820 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14821 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14822 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14823 			bnxt_devlink_health_fw_report(bp);
14824 		else
14825 			bnxt_fw_reset(bp);
14826 	}
14827 
14828 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14829 		if (!is_bnxt_fw_ok(bp))
14830 			bnxt_devlink_health_fw_report(bp);
14831 	}
14832 
14833 	smp_mb__before_atomic();
14834 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14835 }
14836 
14837 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14838 				int *max_cp);
14839 
14840 /* Under netdev instance lock */
14841 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14842 		     int tx_xdp)
14843 {
14844 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14845 	struct bnxt_hw_rings hwr = {0};
14846 	int rx_rings = rx;
14847 	int rc;
14848 
14849 	if (tcs)
14850 		tx_sets = tcs;
14851 
14852 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14853 
14854 	if (max_rx < rx_rings)
14855 		return -ENOMEM;
14856 
14857 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14858 		rx_rings <<= 1;
14859 
14860 	hwr.rx = rx_rings;
14861 	hwr.tx = tx * tx_sets + tx_xdp;
14862 	if (max_tx < hwr.tx)
14863 		return -ENOMEM;
14864 
14865 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14866 
14867 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14868 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14869 	if (max_cp < hwr.cp)
14870 		return -ENOMEM;
14871 	hwr.stat = hwr.cp;
14872 	if (BNXT_NEW_RM(bp)) {
14873 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14874 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14875 		hwr.grp = rx;
14876 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14877 	}
14878 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14879 		hwr.cp_p5 = hwr.tx + rx;
14880 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14881 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14882 		if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) {
14883 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14884 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14885 		}
14886 		if (hwr.cp > bp->total_irqs) {
14887 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14888 
14889 			if (total_msix < hwr.cp) {
14890 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14891 					    hwr.cp, total_msix);
14892 				rc = -ENOSPC;
14893 			}
14894 		}
14895 	}
14896 	return rc;
14897 }
14898 
14899 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14900 {
14901 	if (bp->bar2) {
14902 		pci_iounmap(pdev, bp->bar2);
14903 		bp->bar2 = NULL;
14904 	}
14905 
14906 	if (bp->bar1) {
14907 		pci_iounmap(pdev, bp->bar1);
14908 		bp->bar1 = NULL;
14909 	}
14910 
14911 	if (bp->bar0) {
14912 		pci_iounmap(pdev, bp->bar0);
14913 		bp->bar0 = NULL;
14914 	}
14915 }
14916 
14917 static void bnxt_cleanup_pci(struct bnxt *bp)
14918 {
14919 	bnxt_unmap_bars(bp, bp->pdev);
14920 	pci_release_regions(bp->pdev);
14921 	if (pci_is_enabled(bp->pdev))
14922 		pci_disable_device(bp->pdev);
14923 }
14924 
14925 static void bnxt_init_dflt_coal(struct bnxt *bp)
14926 {
14927 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14928 	struct bnxt_coal *coal;
14929 	u16 flags = 0;
14930 
14931 	if (coal_cap->cmpl_params &
14932 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14933 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14934 
14935 	/* Tick values in micro seconds.
14936 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14937 	 */
14938 	coal = &bp->rx_coal;
14939 	coal->coal_ticks = 10;
14940 	coal->coal_bufs = 30;
14941 	coal->coal_ticks_irq = 1;
14942 	coal->coal_bufs_irq = 2;
14943 	coal->idle_thresh = 50;
14944 	coal->bufs_per_record = 2;
14945 	coal->budget = 64;		/* NAPI budget */
14946 	coal->flags = flags;
14947 
14948 	coal = &bp->tx_coal;
14949 	coal->coal_ticks = 28;
14950 	coal->coal_bufs = 30;
14951 	coal->coal_ticks_irq = 2;
14952 	coal->coal_bufs_irq = 2;
14953 	coal->bufs_per_record = 1;
14954 	coal->flags = flags;
14955 
14956 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14957 }
14958 
14959 /* FW that pre-reserves 1 VNIC per function */
14960 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14961 {
14962 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14963 
14964 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14965 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14966 		return true;
14967 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14968 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14969 		return true;
14970 	return false;
14971 }
14972 
14973 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
14974 {
14975 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
14976 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
14977 	int rc;
14978 
14979 	bp->max_pfcwd_tmo_ms = 0;
14980 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
14981 	if (rc)
14982 		return;
14983 	resp = hwrm_req_hold(bp, req);
14984 	rc = hwrm_req_send_silent(bp, req);
14985 	if (!rc)
14986 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
14987 	hwrm_req_drop(bp, req);
14988 }
14989 
14990 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14991 {
14992 	int rc;
14993 
14994 	bp->fw_cap = 0;
14995 	rc = bnxt_hwrm_ver_get(bp);
14996 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14997 	 * so wait before continuing with recovery.
14998 	 */
14999 	if (rc)
15000 		msleep(100);
15001 	bnxt_try_map_fw_health_reg(bp);
15002 	if (rc) {
15003 		rc = bnxt_try_recover_fw(bp);
15004 		if (rc)
15005 			return rc;
15006 		rc = bnxt_hwrm_ver_get(bp);
15007 		if (rc)
15008 			return rc;
15009 	}
15010 
15011 	bnxt_nvm_cfg_ver_get(bp);
15012 
15013 	rc = bnxt_hwrm_func_reset(bp);
15014 	if (rc)
15015 		return -ENODEV;
15016 
15017 	bnxt_hwrm_fw_set_time(bp);
15018 	return 0;
15019 }
15020 
15021 static int bnxt_fw_init_one_p2(struct bnxt *bp)
15022 {
15023 	int rc;
15024 
15025 	/* Get the MAX capabilities for this function */
15026 	rc = bnxt_hwrm_func_qcaps(bp);
15027 	if (rc) {
15028 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
15029 			   rc);
15030 		return -ENODEV;
15031 	}
15032 
15033 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
15034 	if (rc)
15035 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
15036 			    rc);
15037 
15038 	if (bnxt_alloc_fw_health(bp)) {
15039 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
15040 	} else {
15041 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
15042 		if (rc)
15043 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
15044 				    rc);
15045 	}
15046 
15047 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
15048 	if (rc)
15049 		return -ENODEV;
15050 
15051 	rc = bnxt_alloc_crash_dump_mem(bp);
15052 	if (rc)
15053 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
15054 			    rc);
15055 	if (!rc) {
15056 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
15057 		if (rc) {
15058 			bnxt_free_crash_dump_mem(bp);
15059 			netdev_warn(bp->dev,
15060 				    "hwrm crash dump mem failure rc: %d\n", rc);
15061 		}
15062 	}
15063 
15064 	if (bnxt_fw_pre_resv_vnics(bp))
15065 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
15066 
15067 	bnxt_hwrm_pfcwd_qcaps(bp);
15068 	bnxt_hwrm_func_qcfg(bp);
15069 	bnxt_hwrm_vnic_qcaps(bp);
15070 	bnxt_hwrm_port_led_qcaps(bp);
15071 	bnxt_ethtool_init(bp);
15072 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
15073 		__bnxt_hwrm_ptp_qcfg(bp);
15074 	bnxt_dcb_init(bp);
15075 	bnxt_hwmon_init(bp);
15076 	return 0;
15077 }
15078 
15079 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
15080 {
15081 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
15082 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
15083 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
15084 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
15085 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
15086 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
15087 		bp->rss_hash_delta = bp->rss_hash_cfg;
15088 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
15089 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
15090 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
15091 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
15092 	}
15093 }
15094 
15095 static void bnxt_set_dflt_rfs(struct bnxt *bp)
15096 {
15097 	struct net_device *dev = bp->dev;
15098 
15099 	dev->hw_features &= ~NETIF_F_NTUPLE;
15100 	dev->features &= ~NETIF_F_NTUPLE;
15101 	bp->flags &= ~BNXT_FLAG_RFS;
15102 	if (bnxt_rfs_supported(bp)) {
15103 		dev->hw_features |= NETIF_F_NTUPLE;
15104 		if (bnxt_rfs_capable(bp, false)) {
15105 			bp->flags |= BNXT_FLAG_RFS;
15106 			dev->features |= NETIF_F_NTUPLE;
15107 		}
15108 	}
15109 }
15110 
15111 static void bnxt_fw_init_one_p3(struct bnxt *bp)
15112 {
15113 	struct pci_dev *pdev = bp->pdev;
15114 
15115 	bnxt_set_dflt_rss_hash_type(bp);
15116 	bnxt_set_dflt_rfs(bp);
15117 
15118 	bnxt_get_wol_settings(bp);
15119 	if (bp->flags & BNXT_FLAG_WOL_CAP)
15120 		device_set_wakeup_enable(&pdev->dev, bp->wol);
15121 	else
15122 		device_set_wakeup_capable(&pdev->dev, false);
15123 
15124 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
15125 	bnxt_hwrm_coal_params_qcaps(bp);
15126 }
15127 
15128 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15129 
15130 int bnxt_fw_init_one(struct bnxt *bp)
15131 {
15132 	int rc;
15133 
15134 	rc = bnxt_fw_init_one_p1(bp);
15135 	if (rc) {
15136 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15137 		return rc;
15138 	}
15139 	rc = bnxt_fw_init_one_p2(bp);
15140 	if (rc) {
15141 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15142 		return rc;
15143 	}
15144 	rc = bnxt_probe_phy(bp, false);
15145 	if (rc)
15146 		return rc;
15147 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15148 	if (rc)
15149 		return rc;
15150 
15151 	bnxt_fw_init_one_p3(bp);
15152 	return 0;
15153 }
15154 
15155 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15156 {
15157 	struct bnxt_fw_health *fw_health = bp->fw_health;
15158 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15159 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15160 	u32 reg_type, reg_off, delay_msecs;
15161 
15162 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15163 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15164 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15165 	switch (reg_type) {
15166 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15167 		pci_write_config_dword(bp->pdev, reg_off, val);
15168 		break;
15169 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15170 		writel(reg_off & BNXT_GRC_BASE_MASK,
15171 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15172 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15173 		fallthrough;
15174 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15175 		writel(val, bp->bar0 + reg_off);
15176 		break;
15177 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15178 		writel(val, bp->bar1 + reg_off);
15179 		break;
15180 	}
15181 	if (delay_msecs) {
15182 		pci_read_config_dword(bp->pdev, 0, &val);
15183 		msleep(delay_msecs);
15184 	}
15185 }
15186 
15187 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15188 {
15189 	struct hwrm_func_qcfg_output *resp;
15190 	struct hwrm_func_qcfg_input *req;
15191 	bool result = true; /* firmware will enforce if unknown */
15192 
15193 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15194 		return result;
15195 
15196 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15197 		return result;
15198 
15199 	req->fid = cpu_to_le16(0xffff);
15200 	resp = hwrm_req_hold(bp, req);
15201 	if (!hwrm_req_send(bp, req))
15202 		result = !!(le16_to_cpu(resp->flags) &
15203 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15204 	hwrm_req_drop(bp, req);
15205 	return result;
15206 }
15207 
15208 static void bnxt_reset_all(struct bnxt *bp)
15209 {
15210 	struct bnxt_fw_health *fw_health = bp->fw_health;
15211 	int i, rc;
15212 
15213 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15214 		bnxt_fw_reset_via_optee(bp);
15215 		bp->fw_reset_timestamp = jiffies;
15216 		return;
15217 	}
15218 
15219 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15220 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15221 			bnxt_fw_reset_writel(bp, i);
15222 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15223 		struct hwrm_fw_reset_input *req;
15224 
15225 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15226 		if (!rc) {
15227 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15228 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15229 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15230 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15231 			rc = hwrm_req_send(bp, req);
15232 		}
15233 		if (rc != -ENODEV)
15234 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15235 	}
15236 	bp->fw_reset_timestamp = jiffies;
15237 }
15238 
15239 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15240 {
15241 	return time_after(jiffies, bp->fw_reset_timestamp +
15242 			  (bp->fw_reset_max_dsecs * HZ / 10));
15243 }
15244 
15245 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15246 {
15247 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15248 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15249 		bnxt_dl_health_fw_status_update(bp, false);
15250 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15251 	netif_close(bp->dev);
15252 }
15253 
15254 static void bnxt_fw_reset_task(struct work_struct *work)
15255 {
15256 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15257 	int rc = 0;
15258 
15259 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15260 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15261 		return;
15262 	}
15263 
15264 	switch (bp->fw_reset_state) {
15265 	case BNXT_FW_RESET_STATE_POLL_VF: {
15266 		int n = bnxt_get_registered_vfs(bp);
15267 		int tmo;
15268 
15269 		if (n < 0) {
15270 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15271 				   n, jiffies_to_msecs(jiffies -
15272 				   bp->fw_reset_timestamp));
15273 			goto fw_reset_abort;
15274 		} else if (n > 0) {
15275 			if (bnxt_fw_reset_timeout(bp)) {
15276 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15277 				bp->fw_reset_state = 0;
15278 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15279 					   n);
15280 				goto ulp_start;
15281 			}
15282 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15283 			return;
15284 		}
15285 		bp->fw_reset_timestamp = jiffies;
15286 		netdev_lock(bp->dev);
15287 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15288 			bnxt_fw_reset_abort(bp, rc);
15289 			netdev_unlock(bp->dev);
15290 			goto ulp_start;
15291 		}
15292 		bnxt_fw_reset_close(bp);
15293 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15294 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15295 			tmo = HZ / 10;
15296 		} else {
15297 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15298 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15299 		}
15300 		netdev_unlock(bp->dev);
15301 		bnxt_queue_fw_reset_work(bp, tmo);
15302 		return;
15303 	}
15304 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15305 		u32 val;
15306 
15307 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15308 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15309 		    !bnxt_fw_reset_timeout(bp)) {
15310 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15311 			return;
15312 		}
15313 
15314 		if (!bp->fw_health->primary) {
15315 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15316 
15317 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15318 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15319 			return;
15320 		}
15321 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15322 	}
15323 		fallthrough;
15324 	case BNXT_FW_RESET_STATE_RESET_FW:
15325 		bnxt_reset_all(bp);
15326 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15327 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15328 		return;
15329 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15330 		bnxt_inv_fw_health_reg(bp);
15331 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15332 		    !bp->fw_reset_min_dsecs) {
15333 			u16 val;
15334 
15335 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15336 			if (val == 0xffff) {
15337 				if (bnxt_fw_reset_timeout(bp)) {
15338 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15339 					rc = -ETIMEDOUT;
15340 					goto fw_reset_abort;
15341 				}
15342 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15343 				return;
15344 			}
15345 		}
15346 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15347 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15348 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15349 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15350 			bnxt_dl_remote_reload(bp);
15351 		if (pci_enable_device(bp->pdev)) {
15352 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15353 			rc = -ENODEV;
15354 			goto fw_reset_abort;
15355 		}
15356 		pci_set_master(bp->pdev);
15357 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15358 		fallthrough;
15359 	case BNXT_FW_RESET_STATE_POLL_FW:
15360 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15361 		rc = bnxt_hwrm_poll(bp);
15362 		if (rc) {
15363 			if (bnxt_fw_reset_timeout(bp)) {
15364 				netdev_err(bp->dev, "Firmware reset aborted\n");
15365 				goto fw_reset_abort_status;
15366 			}
15367 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15368 			return;
15369 		}
15370 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15371 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15372 		fallthrough;
15373 	case BNXT_FW_RESET_STATE_OPENING:
15374 		while (!rtnl_trylock()) {
15375 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15376 			return;
15377 		}
15378 		netdev_lock(bp->dev);
15379 		rc = bnxt_open(bp->dev);
15380 		if (rc) {
15381 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15382 			bnxt_fw_reset_abort(bp, rc);
15383 			netdev_unlock(bp->dev);
15384 			rtnl_unlock();
15385 			goto ulp_start;
15386 		}
15387 
15388 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15389 		    bp->fw_health->enabled) {
15390 			bp->fw_health->last_fw_reset_cnt =
15391 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15392 		}
15393 		bp->fw_reset_state = 0;
15394 		/* Make sure fw_reset_state is 0 before clearing the flag */
15395 		smp_mb__before_atomic();
15396 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15397 		bnxt_ptp_reapply_pps(bp);
15398 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15399 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15400 			bnxt_dl_health_fw_recovery_done(bp);
15401 			bnxt_dl_health_fw_status_update(bp, true);
15402 		}
15403 		netdev_unlock(bp->dev);
15404 		rtnl_unlock();
15405 		bnxt_ulp_start(bp);
15406 		bnxt_reenable_sriov(bp);
15407 		netdev_lock(bp->dev);
15408 		bnxt_vf_reps_alloc(bp);
15409 		bnxt_vf_reps_open(bp);
15410 		netdev_unlock(bp->dev);
15411 		break;
15412 	}
15413 	return;
15414 
15415 fw_reset_abort_status:
15416 	if (bp->fw_health->status_reliable ||
15417 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15418 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15419 
15420 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15421 	}
15422 fw_reset_abort:
15423 	netdev_lock(bp->dev);
15424 	bnxt_fw_reset_abort(bp, rc);
15425 	netdev_unlock(bp->dev);
15426 ulp_start:
15427 	if (!rc)
15428 		bnxt_ulp_start(bp);
15429 }
15430 
15431 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15432 {
15433 	int rc;
15434 	struct bnxt *bp = netdev_priv(dev);
15435 
15436 	SET_NETDEV_DEV(dev, &pdev->dev);
15437 
15438 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15439 	rc = pci_enable_device(pdev);
15440 	if (rc) {
15441 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15442 		goto init_err;
15443 	}
15444 
15445 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15446 		dev_err(&pdev->dev,
15447 			"Cannot find PCI device base address, aborting\n");
15448 		rc = -ENODEV;
15449 		goto init_err_disable;
15450 	}
15451 
15452 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15453 	if (rc) {
15454 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15455 		goto init_err_disable;
15456 	}
15457 
15458 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15459 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15460 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15461 		rc = -EIO;
15462 		goto init_err_release;
15463 	}
15464 
15465 	pci_set_master(pdev);
15466 
15467 	bp->dev = dev;
15468 	bp->pdev = pdev;
15469 
15470 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15471 	 * determines the BAR size.
15472 	 */
15473 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15474 	if (!bp->bar0) {
15475 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15476 		rc = -ENOMEM;
15477 		goto init_err_release;
15478 	}
15479 
15480 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15481 	if (!bp->bar2) {
15482 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15483 		rc = -ENOMEM;
15484 		goto init_err_release;
15485 	}
15486 
15487 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15488 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15489 
15490 	spin_lock_init(&bp->ntp_fltr_lock);
15491 #if BITS_PER_LONG == 32
15492 	spin_lock_init(&bp->db_lock);
15493 #endif
15494 
15495 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15496 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15497 
15498 	timer_setup(&bp->timer, bnxt_timer, 0);
15499 	bp->current_interval = BNXT_TIMER_INTERVAL;
15500 
15501 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15502 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15503 
15504 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15505 	return 0;
15506 
15507 init_err_release:
15508 	bnxt_unmap_bars(bp, pdev);
15509 	pci_release_regions(pdev);
15510 
15511 init_err_disable:
15512 	pci_disable_device(pdev);
15513 
15514 init_err:
15515 	return rc;
15516 }
15517 
15518 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15519 {
15520 	struct sockaddr *addr = p;
15521 	struct bnxt *bp = netdev_priv(dev);
15522 	int rc = 0;
15523 
15524 	netdev_assert_locked(dev);
15525 
15526 	if (!is_valid_ether_addr(addr->sa_data))
15527 		return -EADDRNOTAVAIL;
15528 
15529 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15530 		return 0;
15531 
15532 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15533 	if (rc)
15534 		return rc;
15535 
15536 	eth_hw_addr_set(dev, addr->sa_data);
15537 	bnxt_clear_usr_fltrs(bp, true);
15538 	if (netif_running(dev)) {
15539 		bnxt_close_nic(bp, false, false);
15540 		rc = bnxt_open_nic(bp, false, false);
15541 	}
15542 
15543 	return rc;
15544 }
15545 
15546 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15547 {
15548 	struct bnxt *bp = netdev_priv(dev);
15549 
15550 	netdev_assert_locked(dev);
15551 
15552 	if (netif_running(dev))
15553 		bnxt_close_nic(bp, true, false);
15554 
15555 	WRITE_ONCE(dev->mtu, new_mtu);
15556 
15557 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15558 	 * program is attached.  We need to set the AGG rings settings and
15559 	 * rx_skb_func accordingly.
15560 	 */
15561 	if (READ_ONCE(bp->xdp_prog))
15562 		bnxt_set_rx_skb_mode(bp, true);
15563 
15564 	bnxt_set_ring_params(bp);
15565 
15566 	if (netif_running(dev))
15567 		return bnxt_open_nic(bp, true, false);
15568 
15569 	return 0;
15570 }
15571 
15572 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15573 {
15574 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15575 
15576 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15577 			       tx_cp + bp->rx_nr_rings;
15578 }
15579 
15580 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15581 {
15582 	struct bnxt *bp = netdev_priv(dev);
15583 	bool sh = false;
15584 	int rc;
15585 
15586 	if (tc > bp->max_tc) {
15587 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15588 			   tc, bp->max_tc);
15589 		return -EINVAL;
15590 	}
15591 
15592 	if (bp->num_tc == tc)
15593 		return 0;
15594 
15595 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15596 		sh = true;
15597 
15598 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15599 			      sh, tc, bp->tx_nr_rings_xdp);
15600 	if (rc)
15601 		return rc;
15602 
15603 	/* Needs to close the device and do hw resource re-allocations */
15604 	if (netif_running(bp->dev))
15605 		bnxt_close_nic(bp, true, false);
15606 
15607 	if (tc) {
15608 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15609 		netdev_set_num_tc(dev, tc);
15610 		bp->num_tc = tc;
15611 	} else {
15612 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15613 		netdev_reset_tc(dev);
15614 		bp->num_tc = 0;
15615 	}
15616 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15617 	bnxt_set_cp_rings(bp, sh);
15618 
15619 	if (netif_running(bp->dev))
15620 		return bnxt_open_nic(bp, true, false);
15621 
15622 	return 0;
15623 }
15624 
15625 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15626 				  void *cb_priv)
15627 {
15628 	struct bnxt *bp = cb_priv;
15629 
15630 	if (!bnxt_tc_flower_enabled(bp) ||
15631 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15632 		return -EOPNOTSUPP;
15633 
15634 	switch (type) {
15635 	case TC_SETUP_CLSFLOWER:
15636 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15637 	default:
15638 		return -EOPNOTSUPP;
15639 	}
15640 }
15641 
15642 LIST_HEAD(bnxt_block_cb_list);
15643 
15644 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15645 			 void *type_data)
15646 {
15647 	struct bnxt *bp = netdev_priv(dev);
15648 
15649 	switch (type) {
15650 	case TC_SETUP_BLOCK:
15651 		return flow_block_cb_setup_simple(type_data,
15652 						  &bnxt_block_cb_list,
15653 						  bnxt_setup_tc_block_cb,
15654 						  bp, bp, true);
15655 	case TC_SETUP_QDISC_MQPRIO: {
15656 		struct tc_mqprio_qopt *mqprio = type_data;
15657 
15658 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15659 
15660 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15661 	}
15662 	default:
15663 		return -EOPNOTSUPP;
15664 	}
15665 }
15666 
15667 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15668 			    const struct sk_buff *skb)
15669 {
15670 	struct bnxt_vnic_info *vnic;
15671 
15672 	if (skb)
15673 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15674 
15675 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15676 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15677 }
15678 
15679 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15680 			   u32 idx)
15681 {
15682 	struct hlist_head *head;
15683 	int bit_id;
15684 
15685 	spin_lock_bh(&bp->ntp_fltr_lock);
15686 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15687 	if (bit_id < 0) {
15688 		spin_unlock_bh(&bp->ntp_fltr_lock);
15689 		return -ENOMEM;
15690 	}
15691 
15692 	fltr->base.sw_id = (u16)bit_id;
15693 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15694 	fltr->base.flags |= BNXT_ACT_RING_DST;
15695 	head = &bp->ntp_fltr_hash_tbl[idx];
15696 	hlist_add_head_rcu(&fltr->base.hash, head);
15697 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15698 	bnxt_insert_usr_fltr(bp, &fltr->base);
15699 	bp->ntp_fltr_count++;
15700 	spin_unlock_bh(&bp->ntp_fltr_lock);
15701 	return 0;
15702 }
15703 
15704 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15705 			    struct bnxt_ntuple_filter *f2)
15706 {
15707 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15708 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15709 	struct flow_keys *keys1 = &f1->fkeys;
15710 	struct flow_keys *keys2 = &f2->fkeys;
15711 
15712 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15713 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15714 		return false;
15715 
15716 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15717 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15718 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15719 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15720 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15721 			return false;
15722 	} else {
15723 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15724 				     &keys2->addrs.v6addrs.src) ||
15725 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15726 				     &masks2->addrs.v6addrs.src) ||
15727 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15728 				     &keys2->addrs.v6addrs.dst) ||
15729 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15730 				     &masks2->addrs.v6addrs.dst))
15731 			return false;
15732 	}
15733 
15734 	return keys1->ports.src == keys2->ports.src &&
15735 	       masks1->ports.src == masks2->ports.src &&
15736 	       keys1->ports.dst == keys2->ports.dst &&
15737 	       masks1->ports.dst == masks2->ports.dst &&
15738 	       keys1->control.flags == keys2->control.flags &&
15739 	       f1->l2_fltr == f2->l2_fltr;
15740 }
15741 
15742 struct bnxt_ntuple_filter *
15743 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15744 				struct bnxt_ntuple_filter *fltr, u32 idx)
15745 {
15746 	struct bnxt_ntuple_filter *f;
15747 	struct hlist_head *head;
15748 
15749 	head = &bp->ntp_fltr_hash_tbl[idx];
15750 	hlist_for_each_entry_rcu(f, head, base.hash) {
15751 		if (bnxt_fltr_match(f, fltr))
15752 			return f;
15753 	}
15754 	return NULL;
15755 }
15756 
15757 #ifdef CONFIG_RFS_ACCEL
15758 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15759 			      u16 rxq_index, u32 flow_id)
15760 {
15761 	struct bnxt *bp = netdev_priv(dev);
15762 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15763 	struct flow_keys *fkeys;
15764 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15765 	struct bnxt_l2_filter *l2_fltr;
15766 	int rc = 0, idx;
15767 	u32 flags;
15768 
15769 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15770 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15771 		atomic_inc(&l2_fltr->refcnt);
15772 	} else {
15773 		struct bnxt_l2_key key;
15774 
15775 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15776 		key.vlan = 0;
15777 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15778 		if (!l2_fltr)
15779 			return -EINVAL;
15780 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15781 			bnxt_del_l2_filter(bp, l2_fltr);
15782 			return -EINVAL;
15783 		}
15784 	}
15785 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
15786 	if (!new_fltr) {
15787 		bnxt_del_l2_filter(bp, l2_fltr);
15788 		return -ENOMEM;
15789 	}
15790 
15791 	fkeys = &new_fltr->fkeys;
15792 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15793 		rc = -EPROTONOSUPPORT;
15794 		goto err_free;
15795 	}
15796 
15797 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15798 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15799 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15800 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15801 		rc = -EPROTONOSUPPORT;
15802 		goto err_free;
15803 	}
15804 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15805 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15806 		if (bp->hwrm_spec_code < 0x10601) {
15807 			rc = -EPROTONOSUPPORT;
15808 			goto err_free;
15809 		}
15810 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15811 	}
15812 	flags = fkeys->control.flags;
15813 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15814 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15815 		rc = -EPROTONOSUPPORT;
15816 		goto err_free;
15817 	}
15818 	new_fltr->l2_fltr = l2_fltr;
15819 
15820 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15821 	rcu_read_lock();
15822 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15823 	if (fltr) {
15824 		rc = fltr->base.sw_id;
15825 		rcu_read_unlock();
15826 		goto err_free;
15827 	}
15828 	rcu_read_unlock();
15829 
15830 	new_fltr->flow_id = flow_id;
15831 	new_fltr->base.rxq = rxq_index;
15832 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15833 	if (!rc) {
15834 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15835 		return new_fltr->base.sw_id;
15836 	}
15837 
15838 err_free:
15839 	bnxt_del_l2_filter(bp, l2_fltr);
15840 	kfree(new_fltr);
15841 	return rc;
15842 }
15843 #endif
15844 
15845 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15846 {
15847 	spin_lock_bh(&bp->ntp_fltr_lock);
15848 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15849 		spin_unlock_bh(&bp->ntp_fltr_lock);
15850 		return;
15851 	}
15852 	hlist_del_rcu(&fltr->base.hash);
15853 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15854 	bp->ntp_fltr_count--;
15855 	spin_unlock_bh(&bp->ntp_fltr_lock);
15856 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15857 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15858 	kfree_rcu(fltr, base.rcu);
15859 }
15860 
15861 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15862 {
15863 #ifdef CONFIG_RFS_ACCEL
15864 	int i;
15865 
15866 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15867 		struct hlist_head *head;
15868 		struct hlist_node *tmp;
15869 		struct bnxt_ntuple_filter *fltr;
15870 		int rc;
15871 
15872 		head = &bp->ntp_fltr_hash_tbl[i];
15873 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15874 			bool del = false;
15875 
15876 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15877 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15878 					continue;
15879 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15880 							fltr->flow_id,
15881 							fltr->base.sw_id)) {
15882 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15883 									 fltr);
15884 					del = true;
15885 				}
15886 			} else {
15887 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15888 								       fltr);
15889 				if (rc)
15890 					del = true;
15891 				else
15892 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15893 			}
15894 
15895 			if (del)
15896 				bnxt_del_ntp_filter(bp, fltr);
15897 		}
15898 	}
15899 #endif
15900 }
15901 
15902 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15903 				    unsigned int entry, struct udp_tunnel_info *ti)
15904 {
15905 	struct bnxt *bp = netdev_priv(netdev);
15906 	unsigned int cmd;
15907 
15908 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15909 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15910 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15911 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15912 	else
15913 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15914 
15915 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15916 }
15917 
15918 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15919 				      unsigned int entry, struct udp_tunnel_info *ti)
15920 {
15921 	struct bnxt *bp = netdev_priv(netdev);
15922 	unsigned int cmd;
15923 
15924 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15925 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15926 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15927 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15928 	else
15929 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15930 
15931 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15932 }
15933 
15934 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15935 	.set_port	= bnxt_udp_tunnel_set_port,
15936 	.unset_port	= bnxt_udp_tunnel_unset_port,
15937 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15938 	.tables		= {
15939 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15940 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15941 	},
15942 }, bnxt_udp_tunnels_p7 = {
15943 	.set_port	= bnxt_udp_tunnel_set_port,
15944 	.unset_port	= bnxt_udp_tunnel_unset_port,
15945 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15946 	.tables		= {
15947 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15948 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15949 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15950 	},
15951 };
15952 
15953 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15954 			       struct net_device *dev, u32 filter_mask,
15955 			       int nlflags)
15956 {
15957 	struct bnxt *bp = netdev_priv(dev);
15958 
15959 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15960 				       nlflags, filter_mask, NULL);
15961 }
15962 
15963 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15964 			       u16 flags, struct netlink_ext_ack *extack)
15965 {
15966 	struct bnxt *bp = netdev_priv(dev);
15967 	struct nlattr *attr, *br_spec;
15968 	int rem, rc = 0;
15969 
15970 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15971 		return -EOPNOTSUPP;
15972 
15973 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15974 	if (!br_spec)
15975 		return -EINVAL;
15976 
15977 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15978 		u16 mode;
15979 
15980 		mode = nla_get_u16(attr);
15981 		if (mode == bp->br_mode)
15982 			break;
15983 
15984 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15985 		if (!rc)
15986 			bp->br_mode = mode;
15987 		break;
15988 	}
15989 	return rc;
15990 }
15991 
15992 int bnxt_get_port_parent_id(struct net_device *dev,
15993 			    struct netdev_phys_item_id *ppid)
15994 {
15995 	struct bnxt *bp = netdev_priv(dev);
15996 
15997 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15998 		return -EOPNOTSUPP;
15999 
16000 	/* The PF and it's VF-reps only support the switchdev framework */
16001 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
16002 		return -EOPNOTSUPP;
16003 
16004 	ppid->id_len = sizeof(bp->dsn);
16005 	memcpy(ppid->id, bp->dsn, ppid->id_len);
16006 
16007 	return 0;
16008 }
16009 
16010 static const struct net_device_ops bnxt_netdev_ops = {
16011 	.ndo_open		= bnxt_open,
16012 	.ndo_start_xmit		= bnxt_start_xmit,
16013 	.ndo_stop		= bnxt_close,
16014 	.ndo_get_stats64	= bnxt_get_stats64,
16015 	.ndo_set_rx_mode_async	= bnxt_set_rx_mode,
16016 	.ndo_eth_ioctl		= bnxt_ioctl,
16017 	.ndo_validate_addr	= eth_validate_addr,
16018 	.ndo_set_mac_address	= bnxt_change_mac_addr,
16019 	.ndo_change_mtu		= bnxt_change_mtu,
16020 	.ndo_fix_features	= bnxt_fix_features,
16021 	.ndo_set_features	= bnxt_set_features,
16022 	.ndo_features_check	= bnxt_features_check,
16023 	.ndo_tx_timeout		= bnxt_tx_timeout,
16024 #ifdef CONFIG_BNXT_SRIOV
16025 	.ndo_get_vf_config	= bnxt_get_vf_config,
16026 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
16027 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
16028 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
16029 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
16030 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
16031 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
16032 #endif
16033 	.ndo_setup_tc           = bnxt_setup_tc,
16034 #ifdef CONFIG_RFS_ACCEL
16035 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
16036 #endif
16037 	.ndo_bpf		= bnxt_xdp,
16038 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
16039 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
16040 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
16041 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
16042 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
16043 };
16044 
16045 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
16046 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
16047 };
16048 
16049 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
16050 				    struct netdev_queue_stats_rx *stats)
16051 {
16052 	struct bnxt *bp = netdev_priv(dev);
16053 	struct bnxt_cp_ring_info *cpr;
16054 	u64 *sw;
16055 
16056 	if (!bp->bnapi)
16057 		return;
16058 
16059 	cpr = &bp->bnapi[i]->cp_ring;
16060 	sw = cpr->stats.sw_stats;
16061 
16062 	stats->packets = 0;
16063 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
16064 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
16065 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
16066 
16067 	stats->bytes = 0;
16068 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
16069 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
16070 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
16071 
16072 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
16073 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
16074 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
16075 }
16076 
16077 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
16078 				    struct netdev_queue_stats_tx *stats)
16079 {
16080 	struct bnxt *bp = netdev_priv(dev);
16081 	struct bnxt_napi *bnapi;
16082 	u64 *sw;
16083 
16084 	if (!bp->tx_ring)
16085 		return;
16086 
16087 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
16088 	sw = bnapi->cp_ring.stats.sw_stats;
16089 
16090 	stats->packets = 0;
16091 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
16092 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
16093 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
16094 
16095 	stats->bytes = 0;
16096 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
16097 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
16098 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
16099 }
16100 
16101 static void bnxt_get_base_stats(struct net_device *dev,
16102 				struct netdev_queue_stats_rx *rx,
16103 				struct netdev_queue_stats_tx *tx)
16104 {
16105 	struct bnxt *bp = netdev_priv(dev);
16106 
16107 	rx->packets = bp->net_stats_prev.rx_packets;
16108 	rx->bytes = bp->net_stats_prev.rx_bytes;
16109 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
16110 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
16111 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
16112 
16113 	tx->packets = bp->net_stats_prev.tx_packets;
16114 	tx->bytes = bp->net_stats_prev.tx_bytes;
16115 }
16116 
16117 static const struct netdev_stat_ops bnxt_stat_ops = {
16118 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
16119 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
16120 	.get_base_stats		= bnxt_get_base_stats,
16121 };
16122 
16123 static void bnxt_queue_default_qcfg(struct net_device *dev,
16124 				    struct netdev_queue_config *qcfg)
16125 {
16126 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
16127 }
16128 
16129 static int bnxt_validate_qcfg(struct net_device *dev,
16130 			      struct netdev_queue_config *qcfg,
16131 			      struct netlink_ext_ack *extack)
16132 {
16133 	struct bnxt *bp = netdev_priv(dev);
16134 
16135 	/* Older chips need MSS calc so rx_page_size is not supported */
16136 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16137 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16138 		return -EINVAL;
16139 
16140 	if (!is_power_of_2(qcfg->rx_page_size))
16141 		return -ERANGE;
16142 
16143 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16144 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16145 		return -ERANGE;
16146 
16147 	return 0;
16148 }
16149 
16150 static int bnxt_queue_mem_alloc(struct net_device *dev,
16151 				struct netdev_queue_config *qcfg,
16152 				void *qmem, int idx)
16153 {
16154 	struct bnxt_rx_ring_info *rxr, *clone;
16155 	struct bnxt *bp = netdev_priv(dev);
16156 	struct bnxt_ring_struct *ring;
16157 	int rc;
16158 
16159 	if (!bp->rx_ring)
16160 		return -ENETDOWN;
16161 
16162 	rxr = &bp->rx_ring[idx];
16163 	clone = qmem;
16164 	memcpy(clone, rxr, sizeof(*rxr));
16165 	bnxt_init_rx_ring_struct(bp, clone);
16166 	bnxt_reset_rx_ring_struct(bp, clone);
16167 
16168 	clone->rx_prod = 0;
16169 	clone->rx_agg_prod = 0;
16170 	clone->rx_sw_agg_prod = 0;
16171 	clone->rx_next_cons = 0;
16172 	clone->need_head_pool = false;
16173 	clone->rx_page_size = qcfg->rx_page_size;
16174 
16175 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16176 	if (rc)
16177 		return rc;
16178 
16179 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16180 	if (rc < 0)
16181 		goto err_page_pool_destroy;
16182 
16183 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16184 					MEM_TYPE_PAGE_POOL,
16185 					clone->page_pool);
16186 	if (rc)
16187 		goto err_rxq_info_unreg;
16188 
16189 	ring = &clone->rx_ring_struct;
16190 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16191 	if (rc)
16192 		goto err_free_rx_ring;
16193 
16194 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16195 		ring = &clone->rx_agg_ring_struct;
16196 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16197 		if (rc)
16198 			goto err_free_rx_agg_ring;
16199 
16200 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16201 		if (rc)
16202 			goto err_free_rx_agg_ring;
16203 	}
16204 
16205 	if (bp->flags & BNXT_FLAG_TPA) {
16206 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16207 		if (rc)
16208 			goto err_free_tpa_info;
16209 	}
16210 
16211 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16212 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16213 
16214 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16215 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16216 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16217 	if (bp->flags & BNXT_FLAG_TPA)
16218 		bnxt_alloc_one_tpa_info_data(bp, clone);
16219 
16220 	return 0;
16221 
16222 err_free_tpa_info:
16223 	bnxt_free_one_tpa_info(bp, clone);
16224 err_free_rx_agg_ring:
16225 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16226 err_free_rx_ring:
16227 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16228 err_rxq_info_unreg:
16229 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16230 err_page_pool_destroy:
16231 	page_pool_destroy(clone->page_pool);
16232 	page_pool_destroy(clone->head_pool);
16233 	clone->page_pool = NULL;
16234 	clone->head_pool = NULL;
16235 	return rc;
16236 }
16237 
16238 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16239 {
16240 	struct bnxt_rx_ring_info *rxr = qmem;
16241 	struct bnxt *bp = netdev_priv(dev);
16242 	struct bnxt_ring_struct *ring;
16243 
16244 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16245 	bnxt_free_one_tpa_info(bp, rxr);
16246 
16247 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16248 
16249 	page_pool_destroy(rxr->page_pool);
16250 	page_pool_destroy(rxr->head_pool);
16251 	rxr->page_pool = NULL;
16252 	rxr->head_pool = NULL;
16253 
16254 	ring = &rxr->rx_ring_struct;
16255 	bnxt_free_ring(bp, &ring->ring_mem);
16256 
16257 	ring = &rxr->rx_agg_ring_struct;
16258 	bnxt_free_ring(bp, &ring->ring_mem);
16259 
16260 	kfree(rxr->rx_agg_bmap);
16261 	rxr->rx_agg_bmap = NULL;
16262 }
16263 
16264 static void bnxt_copy_rx_ring(struct bnxt *bp,
16265 			      struct bnxt_rx_ring_info *dst,
16266 			      struct bnxt_rx_ring_info *src)
16267 {
16268 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16269 	struct bnxt_ring_struct *dst_ring, *src_ring;
16270 	int i;
16271 
16272 	dst_ring = &dst->rx_ring_struct;
16273 	dst_rmem = &dst_ring->ring_mem;
16274 	src_ring = &src->rx_ring_struct;
16275 	src_rmem = &src_ring->ring_mem;
16276 
16277 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16278 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16279 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16280 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16281 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16282 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16283 
16284 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16285 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16286 	*dst_rmem->vmem = *src_rmem->vmem;
16287 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16288 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16289 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16290 	}
16291 
16292 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16293 		return;
16294 
16295 	dst_ring = &dst->rx_agg_ring_struct;
16296 	dst_rmem = &dst_ring->ring_mem;
16297 	src_ring = &src->rx_agg_ring_struct;
16298 	src_rmem = &src_ring->ring_mem;
16299 
16300 	dst->rx_page_size = src->rx_page_size;
16301 
16302 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16303 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16304 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16305 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16306 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16307 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16308 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16309 
16310 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16311 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16312 	*dst_rmem->vmem = *src_rmem->vmem;
16313 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16314 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16315 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16316 	}
16317 
16318 	dst->rx_agg_bmap = src->rx_agg_bmap;
16319 }
16320 
16321 static int bnxt_queue_start(struct net_device *dev,
16322 			    struct netdev_queue_config *qcfg,
16323 			    void *qmem, int idx)
16324 {
16325 	struct bnxt *bp = netdev_priv(dev);
16326 	struct bnxt_rx_ring_info *rxr, *clone;
16327 	struct bnxt_cp_ring_info *cpr;
16328 	struct bnxt_vnic_info *vnic;
16329 	struct bnxt_napi *bnapi;
16330 	int i, rc;
16331 	u16 mru;
16332 
16333 	rxr = &bp->rx_ring[idx];
16334 	clone = qmem;
16335 
16336 	rxr->rx_prod = clone->rx_prod;
16337 	rxr->rx_agg_prod = clone->rx_agg_prod;
16338 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16339 	rxr->rx_next_cons = clone->rx_next_cons;
16340 	rxr->rx_tpa = clone->rx_tpa;
16341 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16342 	rxr->page_pool = clone->page_pool;
16343 	rxr->head_pool = clone->head_pool;
16344 	rxr->xdp_rxq = clone->xdp_rxq;
16345 	rxr->need_head_pool = clone->need_head_pool;
16346 
16347 	bnxt_copy_rx_ring(bp, rxr, clone);
16348 
16349 	bnapi = rxr->bnapi;
16350 	cpr = &bnapi->cp_ring;
16351 
16352 	/* All rings have been reserved and previously allocated.
16353 	 * Reallocating with the same parameters should never fail.
16354 	 */
16355 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16356 	if (rc)
16357 		goto err_reset;
16358 
16359 	if (bp->tph_mode) {
16360 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16361 		if (rc)
16362 			goto err_reset;
16363 	}
16364 
16365 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16366 	if (rc)
16367 		goto err_reset;
16368 
16369 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16370 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16371 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16372 
16373 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16374 		rc = bnxt_tx_queue_start(bp, idx);
16375 		if (rc)
16376 			goto err_reset;
16377 	}
16378 
16379 	bnxt_enable_rx_page_pool(rxr);
16380 	napi_enable_locked(&bnapi->napi);
16381 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16382 
16383 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16384 	for (i = 0; i < bp->nr_vnics; i++) {
16385 		vnic = &bp->vnic_info[i];
16386 
16387 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16388 		if (rc)
16389 			return rc;
16390 	}
16391 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16392 
16393 err_reset:
16394 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16395 		   rc);
16396 	napi_enable_locked(&bnapi->napi);
16397 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16398 	netif_close(dev);
16399 	return rc;
16400 }
16401 
16402 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16403 {
16404 	struct bnxt *bp = netdev_priv(dev);
16405 	struct bnxt_rx_ring_info *rxr;
16406 	struct bnxt_cp_ring_info *cpr;
16407 	struct bnxt_vnic_info *vnic;
16408 	struct bnxt_napi *bnapi;
16409 	int i;
16410 
16411 	for (i = 0; i < bp->nr_vnics; i++) {
16412 		vnic = &bp->vnic_info[i];
16413 
16414 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16415 	}
16416 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16417 	/* Make sure NAPI sees that the VNIC is disabled */
16418 	synchronize_net();
16419 	rxr = &bp->rx_ring[idx];
16420 	bnapi = rxr->bnapi;
16421 	cpr = &bnapi->cp_ring;
16422 	cancel_work_sync(&cpr->dim.work);
16423 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16424 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16425 	page_pool_disable_direct_recycling(rxr->page_pool);
16426 	if (bnxt_separate_head_pool(rxr))
16427 		page_pool_disable_direct_recycling(rxr->head_pool);
16428 
16429 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16430 		bnxt_tx_queue_stop(bp, idx);
16431 
16432 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16433 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16434 	 * after seeing the completion.
16435 	 */
16436 	napi_disable_locked(&bnapi->napi);
16437 
16438 	if (bp->tph_mode) {
16439 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16440 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16441 	}
16442 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16443 
16444 	memcpy(qmem, rxr, sizeof(*rxr));
16445 	bnxt_init_rx_ring_struct(bp, qmem);
16446 
16447 	return 0;
16448 }
16449 
16450 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16451 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16452 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16453 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16454 	.ndo_queue_start	= bnxt_queue_start,
16455 	.ndo_queue_stop		= bnxt_queue_stop,
16456 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16457 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16458 	.supported_params	= QCFG_RX_PAGE_SIZE,
16459 };
16460 
16461 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16462 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16463 };
16464 
16465 static void bnxt_remove_one(struct pci_dev *pdev)
16466 {
16467 	struct net_device *dev = pci_get_drvdata(pdev);
16468 	struct bnxt *bp = netdev_priv(dev);
16469 
16470 	if (BNXT_PF(bp))
16471 		__bnxt_sriov_disable(bp);
16472 
16473 	bnxt_aux_devices_del(bp);
16474 
16475 	unregister_netdev(dev);
16476 	bnxt_ptp_clear(bp);
16477 
16478 	bnxt_aux_devices_uninit(bp);
16479 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
16480 
16481 	bnxt_free_l2_filters(bp, true);
16482 	bnxt_free_ntp_fltrs(bp, true);
16483 	WARN_ON(bp->num_rss_ctx);
16484 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16485 	/* Flush any pending tasks */
16486 	cancel_work_sync(&bp->sp_task);
16487 	cancel_delayed_work_sync(&bp->fw_reset_task);
16488 	bp->sp_event = 0;
16489 
16490 	bnxt_dl_fw_reporters_destroy(bp);
16491 	bnxt_dl_unregister(bp);
16492 	bnxt_shutdown_tc(bp);
16493 
16494 	bnxt_clear_int_mode(bp);
16495 	bnxt_hwrm_func_drv_unrgtr(bp);
16496 	bnxt_free_hwrm_resources(bp);
16497 	bnxt_hwmon_uninit(bp);
16498 	bnxt_ethtool_free(bp);
16499 	bnxt_dcb_free(bp);
16500 	kfree(bp->ptp_cfg);
16501 	bp->ptp_cfg = NULL;
16502 	kfree(bp->fw_health);
16503 	bp->fw_health = NULL;
16504 	bnxt_cleanup_pci(bp);
16505 	bnxt_free_ctx_mem(bp, true);
16506 	bnxt_free_crash_dump_mem(bp);
16507 	kfree(bp->rss_indir_tbl);
16508 	bp->rss_indir_tbl = NULL;
16509 	bnxt_free_port_stats(bp);
16510 	free_netdev(dev);
16511 }
16512 
16513 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16514 {
16515 	int rc = 0;
16516 	struct bnxt_link_info *link_info = &bp->link_info;
16517 
16518 	bp->phy_flags = 0;
16519 	rc = bnxt_hwrm_phy_qcaps(bp);
16520 	if (rc) {
16521 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16522 			   rc);
16523 		return rc;
16524 	}
16525 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16526 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16527 	else
16528 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16529 
16530 	bp->mac_flags = 0;
16531 	bnxt_hwrm_mac_qcaps(bp);
16532 
16533 	if (!fw_dflt)
16534 		return 0;
16535 
16536 	mutex_lock(&bp->link_lock);
16537 	rc = bnxt_update_link(bp, false);
16538 	if (rc) {
16539 		mutex_unlock(&bp->link_lock);
16540 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16541 			   rc);
16542 		return rc;
16543 	}
16544 
16545 	/* Older firmware does not have supported_auto_speeds, so assume
16546 	 * that all supported speeds can be autonegotiated.
16547 	 */
16548 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16549 		link_info->support_auto_speeds = link_info->support_speeds;
16550 
16551 	bnxt_init_ethtool_link_settings(bp);
16552 	mutex_unlock(&bp->link_lock);
16553 	return 0;
16554 }
16555 
16556 static int bnxt_get_max_irq(struct pci_dev *pdev)
16557 {
16558 	u16 ctrl;
16559 
16560 	if (!pdev->msix_cap)
16561 		return 1;
16562 
16563 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16564 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16565 }
16566 
16567 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16568 				int *max_cp)
16569 {
16570 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16571 	int max_ring_grps = 0, max_irq;
16572 
16573 	*max_tx = hw_resc->max_tx_rings;
16574 	*max_rx = hw_resc->max_rx_rings;
16575 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16576 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16577 			bnxt_get_ulp_msix_num_in_use(bp),
16578 			hw_resc->max_stat_ctxs -
16579 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16580 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16581 		*max_cp = min_t(int, *max_cp, max_irq);
16582 	max_ring_grps = hw_resc->max_hw_ring_grps;
16583 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16584 		*max_cp -= 1;
16585 		*max_rx -= 2;
16586 	}
16587 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16588 		*max_rx >>= 1;
16589 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16590 		int rc;
16591 
16592 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16593 		if (rc) {
16594 			*max_rx = 0;
16595 			*max_tx = 0;
16596 		}
16597 		/* On P5 chips, max_cp output param should be available NQs */
16598 		*max_cp = max_irq;
16599 	}
16600 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16601 }
16602 
16603 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16604 {
16605 	int rx, tx, cp;
16606 
16607 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16608 	*max_rx = rx;
16609 	*max_tx = tx;
16610 	if (!rx || !tx || !cp)
16611 		return -ENOMEM;
16612 
16613 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16614 }
16615 
16616 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16617 			       bool shared)
16618 {
16619 	int rc;
16620 
16621 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16622 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16623 		/* Not enough rings, try disabling agg rings. */
16624 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16625 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16626 		if (rc) {
16627 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16628 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16629 			return rc;
16630 		}
16631 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16632 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16633 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16634 		bnxt_set_ring_params(bp);
16635 	}
16636 
16637 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16638 		int max_cp, max_stat, max_irq;
16639 
16640 		/* Reserve minimum resources for RoCE */
16641 		max_cp = bnxt_get_max_func_cp_rings(bp);
16642 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16643 		max_irq = bnxt_get_max_func_irqs(bp);
16644 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16645 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16646 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16647 			return 0;
16648 
16649 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16650 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16651 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16652 		max_cp = min_t(int, max_cp, max_irq);
16653 		max_cp = min_t(int, max_cp, max_stat);
16654 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16655 		if (rc)
16656 			rc = 0;
16657 	}
16658 	return rc;
16659 }
16660 
16661 /* In initial default shared ring setting, each shared ring must have a
16662  * RX/TX ring pair.
16663  */
16664 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16665 {
16666 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16667 	bp->rx_nr_rings = bp->cp_nr_rings;
16668 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16669 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16670 }
16671 
16672 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
16673 {
16674 	if (sh)
16675 		bnxt_trim_dflt_sh_rings(bp);
16676 	else
16677 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16678 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16679 	if (sh && READ_ONCE(bp->xdp_prog)) {
16680 		bnxt_set_xdp_tx_rings(bp);
16681 		bnxt_set_cp_rings(bp, true);
16682 	}
16683 }
16684 
16685 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16686 {
16687 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16688 	int avail_msix;
16689 
16690 	if (!bnxt_can_reserve_rings(bp))
16691 		return 0;
16692 
16693 	if (sh)
16694 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16695 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16696 	/* Reduce default rings on multi-port cards so that total default
16697 	 * rings do not exceed CPU count.
16698 	 */
16699 	if (bp->port_count > 1) {
16700 		int max_rings =
16701 			max_t(int, num_online_cpus() / bp->port_count, 1);
16702 
16703 		dflt_rings = min_t(int, dflt_rings, max_rings);
16704 	}
16705 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16706 	if (rc)
16707 		return rc;
16708 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16709 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16710 
16711 	bnxt_adj_dflt_rings(bp, sh);
16712 
16713 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16714 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16715 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16716 
16717 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16718 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16719 	}
16720 
16721 	rc = __bnxt_reserve_rings(bp);
16722 	if (rc && rc != -ENODEV)
16723 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16724 
16725 	bnxt_adj_tx_rings(bp);
16726 	if (sh)
16727 		bnxt_adj_dflt_rings(bp, true);
16728 
16729 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16730 	if (bnxt_need_reserve_rings(bp)) {
16731 		rc = __bnxt_reserve_rings(bp);
16732 		if (rc && rc != -ENODEV)
16733 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16734 		bnxt_adj_tx_rings(bp);
16735 	}
16736 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16737 		bp->rx_nr_rings++;
16738 		bp->cp_nr_rings++;
16739 	}
16740 	if (rc) {
16741 		bp->tx_nr_rings = 0;
16742 		bp->rx_nr_rings = 0;
16743 	}
16744 	return rc;
16745 }
16746 
16747 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16748 {
16749 	int rc;
16750 
16751 	if (bp->tx_nr_rings)
16752 		return 0;
16753 
16754 	bnxt_ulp_irq_stop(bp);
16755 	bnxt_clear_int_mode(bp);
16756 	rc = bnxt_set_dflt_rings(bp, true);
16757 	if (rc) {
16758 		if (BNXT_VF(bp) && rc == -ENODEV)
16759 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16760 		else
16761 			netdev_err(bp->dev, "Not enough rings available.\n");
16762 		goto init_dflt_ring_err;
16763 	}
16764 	rc = bnxt_init_int_mode(bp);
16765 	if (rc)
16766 		goto init_dflt_ring_err;
16767 
16768 	bnxt_adj_tx_rings(bp);
16769 
16770 	bnxt_set_dflt_rfs(bp);
16771 
16772 init_dflt_ring_err:
16773 	bnxt_ulp_irq_restart(bp, rc);
16774 	return rc;
16775 }
16776 
16777 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16778 {
16779 	int rc;
16780 
16781 	netdev_assert_locked_ops_compat(bp->dev);
16782 	bnxt_hwrm_func_qcaps(bp);
16783 
16784 	if (netif_running(bp->dev))
16785 		__bnxt_close_nic(bp, true, false);
16786 
16787 	bnxt_ulp_irq_stop(bp);
16788 	bnxt_clear_int_mode(bp);
16789 	rc = bnxt_init_int_mode(bp);
16790 	bnxt_ulp_irq_restart(bp, rc);
16791 
16792 	if (netif_running(bp->dev)) {
16793 		if (rc)
16794 			netif_close(bp->dev);
16795 		else
16796 			rc = bnxt_open_nic(bp, true, false);
16797 	}
16798 
16799 	return rc;
16800 }
16801 
16802 static int bnxt_init_mac_addr(struct bnxt *bp)
16803 {
16804 	int rc = 0;
16805 
16806 	if (BNXT_PF(bp)) {
16807 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16808 	} else {
16809 #ifdef CONFIG_BNXT_SRIOV
16810 		struct bnxt_vf_info *vf = &bp->vf;
16811 		bool strict_approval = true;
16812 
16813 		if (is_valid_ether_addr(vf->mac_addr)) {
16814 			/* overwrite netdev dev_addr with admin VF MAC */
16815 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16816 			/* Older PF driver or firmware may not approve this
16817 			 * correctly.
16818 			 */
16819 			strict_approval = false;
16820 		} else {
16821 			eth_hw_addr_random(bp->dev);
16822 		}
16823 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16824 #endif
16825 	}
16826 	return rc;
16827 }
16828 
16829 static void bnxt_vpd_read_info(struct bnxt *bp)
16830 {
16831 	struct pci_dev *pdev = bp->pdev;
16832 	unsigned int vpd_size, kw_len;
16833 	int pos, size;
16834 	u8 *vpd_data;
16835 
16836 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16837 	if (IS_ERR(vpd_data)) {
16838 		pci_warn(pdev, "Unable to read VPD\n");
16839 		return;
16840 	}
16841 
16842 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16843 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16844 	if (pos < 0)
16845 		goto read_sn;
16846 
16847 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16848 	memcpy(bp->board_partno, &vpd_data[pos], size);
16849 
16850 read_sn:
16851 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16852 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16853 					   &kw_len);
16854 	if (pos < 0)
16855 		goto exit;
16856 
16857 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16858 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16859 exit:
16860 	kfree(vpd_data);
16861 }
16862 
16863 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16864 {
16865 	struct pci_dev *pdev = bp->pdev;
16866 	u64 qword;
16867 
16868 	qword = pci_get_dsn(pdev);
16869 	if (!qword) {
16870 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16871 		return -EOPNOTSUPP;
16872 	}
16873 
16874 	put_unaligned_le64(qword, dsn);
16875 
16876 	bp->flags |= BNXT_FLAG_DSN_VALID;
16877 	return 0;
16878 }
16879 
16880 static int bnxt_map_db_bar(struct bnxt *bp)
16881 {
16882 	if (!bp->db_size)
16883 		return -ENODEV;
16884 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16885 	if (!bp->bar1)
16886 		return -ENOMEM;
16887 	return 0;
16888 }
16889 
16890 void bnxt_print_device_info(struct bnxt *bp)
16891 {
16892 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16893 		    board_info[bp->board_idx].name,
16894 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16895 
16896 	pcie_print_link_status(bp->pdev);
16897 }
16898 
16899 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16900 {
16901 	struct bnxt_hw_resc *hw_resc;
16902 	struct net_device *dev;
16903 	struct bnxt *bp;
16904 	int rc, max_irqs;
16905 
16906 	if (pci_is_bridge(pdev))
16907 		return -ENODEV;
16908 
16909 	if (!pdev->msix_cap) {
16910 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16911 		return -ENODEV;
16912 	}
16913 
16914 	/* Clear any pending DMA transactions from crash kernel
16915 	 * while loading driver in capture kernel.
16916 	 */
16917 	if (is_kdump_kernel()) {
16918 		pci_clear_master(pdev);
16919 		pcie_flr(pdev);
16920 	}
16921 
16922 	max_irqs = bnxt_get_max_irq(pdev);
16923 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16924 				 max_irqs);
16925 	if (!dev)
16926 		return -ENOMEM;
16927 
16928 	bp = netdev_priv(dev);
16929 	bp->board_idx = ent->driver_data;
16930 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16931 	bnxt_set_max_func_irqs(bp, max_irqs);
16932 
16933 	if (bnxt_vf_pciid(bp->board_idx))
16934 		bp->flags |= BNXT_FLAG_VF;
16935 
16936 	/* No devlink port registration in case of a VF */
16937 	if (BNXT_PF(bp))
16938 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16939 
16940 	rc = bnxt_init_board(pdev, dev);
16941 	if (rc < 0)
16942 		goto init_err_free;
16943 
16944 	dev->netdev_ops = &bnxt_netdev_ops;
16945 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
16946 	dev->stat_ops = &bnxt_stat_ops;
16947 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16948 	dev->ethtool_ops = &bnxt_ethtool_ops;
16949 	pci_set_drvdata(pdev, dev);
16950 
16951 	rc = bnxt_alloc_hwrm_resources(bp);
16952 	if (rc)
16953 		goto init_err_pci_clean;
16954 
16955 	mutex_init(&bp->hwrm_cmd_lock);
16956 	mutex_init(&bp->link_lock);
16957 
16958 	rc = bnxt_fw_init_one_p1(bp);
16959 	if (rc)
16960 		goto init_err_pci_clean;
16961 
16962 	if (BNXT_PF(bp))
16963 		bnxt_vpd_read_info(bp);
16964 
16965 	if (BNXT_CHIP_P5_PLUS(bp)) {
16966 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16967 		if (BNXT_CHIP_P7(bp))
16968 			bp->flags |= BNXT_FLAG_CHIP_P7;
16969 	}
16970 
16971 	rc = bnxt_alloc_rss_indir_tbl(bp);
16972 	if (rc)
16973 		goto init_err_pci_clean;
16974 
16975 	rc = bnxt_fw_init_one_p2(bp);
16976 	if (rc)
16977 		goto init_err_pci_clean;
16978 
16979 	rc = bnxt_map_db_bar(bp);
16980 	if (rc) {
16981 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16982 			rc);
16983 		goto init_err_pci_clean;
16984 	}
16985 
16986 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16987 			   NETIF_F_TSO | NETIF_F_TSO6 |
16988 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16989 			   NETIF_F_GSO_IPXIP4 |
16990 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16991 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16992 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16993 	dev->hw_features |= NETIF_F_GSO_UDP_L4;
16994 
16995 	if (BNXT_SUPPORTS_TPA(bp))
16996 		dev->hw_features |= NETIF_F_LRO;
16997 
16998 	dev->hw_enc_features =
16999 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17000 			NETIF_F_TSO | NETIF_F_TSO6 |
17001 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17002 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17003 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
17004 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
17005 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
17006 	if (bp->flags & BNXT_FLAG_CHIP_P7)
17007 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
17008 	else
17009 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
17010 
17011 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
17012 				    NETIF_F_GSO_GRE_CSUM;
17013 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
17014 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
17015 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
17016 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
17017 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
17018 	if (BNXT_SUPPORTS_TPA(bp))
17019 		dev->hw_features |= NETIF_F_GRO_HW;
17020 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
17021 	if (dev->features & NETIF_F_GRO_HW)
17022 		dev->features &= ~NETIF_F_LRO;
17023 	dev->priv_flags |= IFF_UNICAST_FLT;
17024 
17025 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
17026 	if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
17027 		u16 max_segs = BNXT_SW_USO_MAX_SEGS;
17028 
17029 		if (bp->tso_max_segs)
17030 			max_segs = min_t(u16, max_segs, bp->tso_max_segs);
17031 		netif_set_tso_max_segs(dev, max_segs);
17032 	} else if (bp->tso_max_segs) {
17033 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
17034 	}
17035 
17036 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
17037 			    NETDEV_XDP_ACT_RX_SG;
17038 
17039 #ifdef CONFIG_BNXT_SRIOV
17040 	init_waitqueue_head(&bp->sriov_cfg_wait);
17041 #endif
17042 	if (BNXT_SUPPORTS_TPA(bp)) {
17043 		bp->gro_func = bnxt_gro_func_5730x;
17044 		if (BNXT_CHIP_P4(bp))
17045 			bp->gro_func = bnxt_gro_func_5731x;
17046 		else if (BNXT_CHIP_P5_PLUS(bp))
17047 			bp->gro_func = bnxt_gro_func_5750x;
17048 	}
17049 	if (!BNXT_CHIP_P4_PLUS(bp))
17050 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
17051 
17052 	rc = bnxt_init_mac_addr(bp);
17053 	if (rc) {
17054 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
17055 		rc = -EADDRNOTAVAIL;
17056 		goto init_err_pci_clean;
17057 	}
17058 
17059 	if (BNXT_PF(bp)) {
17060 		/* Read the adapter's DSN to use as the eswitch switch_id */
17061 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
17062 	}
17063 
17064 	/* MTU range: 60 - FW defined max */
17065 	dev->min_mtu = ETH_ZLEN;
17066 	dev->max_mtu = bp->max_mtu;
17067 
17068 	rc = bnxt_probe_phy(bp, true);
17069 	if (rc)
17070 		goto init_err_pci_clean;
17071 
17072 	hw_resc = &bp->hw_resc;
17073 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
17074 		       BNXT_L2_FLTR_MAX_FLTR;
17075 	/* Older firmware may not report these filters properly */
17076 	if (bp->max_fltr < BNXT_MAX_FLTR)
17077 		bp->max_fltr = BNXT_MAX_FLTR;
17078 	bnxt_init_l2_fltr_tbl(bp);
17079 	__bnxt_set_rx_skb_mode(bp, false);
17080 	bnxt_set_tpa_flags(bp);
17081 	bnxt_init_ring_params(bp);
17082 	bnxt_set_ring_params(bp);
17083 	mutex_init(&bp->auxdev_lock);
17084 	if (!bnxt_auxdev_id_alloc(bp))
17085 		bnxt_aux_devices_init(bp);
17086 	rc = bnxt_set_dflt_rings(bp, true);
17087 	if (rc) {
17088 		if (BNXT_VF(bp) && rc == -ENODEV) {
17089 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17090 		} else {
17091 			netdev_err(bp->dev, "Not enough rings available.\n");
17092 			rc = -ENOMEM;
17093 		}
17094 		goto init_err_pci_clean;
17095 	}
17096 
17097 	bnxt_fw_init_one_p3(bp);
17098 
17099 	bnxt_init_dflt_coal(bp);
17100 
17101 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
17102 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
17103 
17104 	rc = bnxt_init_int_mode(bp);
17105 	if (rc)
17106 		goto init_err_pci_clean;
17107 
17108 	/* No TC has been set yet and rings may have been trimmed due to
17109 	 * limited MSIX, so we re-initialize the TX rings per TC.
17110 	 */
17111 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
17112 
17113 	if (BNXT_PF(bp)) {
17114 		if (!bnxt_pf_wq) {
17115 			bnxt_pf_wq =
17116 				create_singlethread_workqueue("bnxt_pf_wq");
17117 			if (!bnxt_pf_wq) {
17118 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
17119 				rc = -ENOMEM;
17120 				goto init_err_pci_clean;
17121 			}
17122 		}
17123 		rc = bnxt_init_tc(bp);
17124 		if (rc)
17125 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
17126 				   rc);
17127 	}
17128 
17129 	bnxt_inv_fw_health_reg(bp);
17130 	rc = bnxt_dl_register(bp);
17131 	if (rc)
17132 		goto init_err_dl;
17133 
17134 	INIT_LIST_HEAD(&bp->usr_fltr_list);
17135 
17136 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
17137 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
17138 
17139 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17140 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17141 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17142 	dev->netmem_tx = NETMEM_TX_DMA;
17143 
17144 	rc = register_netdev(dev);
17145 	if (rc)
17146 		goto init_err_cleanup;
17147 
17148 	bnxt_dl_fw_reporters_create(bp);
17149 
17150 	bnxt_aux_devices_add(bp);
17151 
17152 	bnxt_print_device_info(bp);
17153 
17154 	pci_save_state(pdev);
17155 
17156 	return 0;
17157 init_err_cleanup:
17158 	bnxt_aux_devices_uninit(bp);
17159 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
17160 	bnxt_dl_unregister(bp);
17161 init_err_dl:
17162 	bnxt_shutdown_tc(bp);
17163 	bnxt_clear_int_mode(bp);
17164 
17165 init_err_pci_clean:
17166 	bnxt_hwrm_func_drv_unrgtr(bp);
17167 	bnxt_ptp_clear(bp);
17168 	kfree(bp->ptp_cfg);
17169 	bp->ptp_cfg = NULL;
17170 	bnxt_free_hwrm_resources(bp);
17171 	bnxt_hwmon_uninit(bp);
17172 	bnxt_ethtool_free(bp);
17173 	kfree(bp->fw_health);
17174 	bp->fw_health = NULL;
17175 	bnxt_cleanup_pci(bp);
17176 	bnxt_free_ctx_mem(bp, true);
17177 	bnxt_free_crash_dump_mem(bp);
17178 	kfree(bp->rss_indir_tbl);
17179 	bp->rss_indir_tbl = NULL;
17180 
17181 init_err_free:
17182 	free_netdev(dev);
17183 	return rc;
17184 }
17185 
17186 static void bnxt_shutdown(struct pci_dev *pdev)
17187 {
17188 	struct net_device *dev = pci_get_drvdata(pdev);
17189 	struct bnxt *bp;
17190 
17191 	if (!dev)
17192 		return;
17193 
17194 	rtnl_lock();
17195 	netdev_lock(dev);
17196 	bp = netdev_priv(dev);
17197 	if (!bp)
17198 		goto shutdown_exit;
17199 
17200 	if (netif_running(dev))
17201 		netif_close(dev);
17202 
17203 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17204 		pcie_flr(pdev);
17205 		goto shutdown_exit;
17206 	}
17207 	bnxt_ptp_clear(bp);
17208 	bnxt_clear_int_mode(bp);
17209 	pci_disable_device(pdev);
17210 
17211 	if (system_state == SYSTEM_POWER_OFF) {
17212 		pci_wake_from_d3(pdev, bp->wol);
17213 		pci_set_power_state(pdev, PCI_D3hot);
17214 	}
17215 
17216 shutdown_exit:
17217 	netdev_unlock(dev);
17218 	rtnl_unlock();
17219 }
17220 
17221 #ifdef CONFIG_PM_SLEEP
17222 static int bnxt_suspend(struct device *device)
17223 {
17224 	struct net_device *dev = dev_get_drvdata(device);
17225 	struct bnxt *bp = netdev_priv(dev);
17226 	int rc = 0;
17227 
17228 	bnxt_ulp_stop(bp);
17229 
17230 	netdev_lock(dev);
17231 	if (netif_running(dev)) {
17232 		netif_device_detach(dev);
17233 		rc = bnxt_close(dev);
17234 	}
17235 	bnxt_hwrm_func_drv_unrgtr(bp);
17236 	bnxt_ptp_clear(bp);
17237 	pci_disable_device(bp->pdev);
17238 	bnxt_free_ctx_mem(bp, false);
17239 	netdev_unlock(dev);
17240 	return rc;
17241 }
17242 
17243 static int bnxt_resume(struct device *device)
17244 {
17245 	struct net_device *dev = dev_get_drvdata(device);
17246 	struct bnxt *bp = netdev_priv(dev);
17247 	int rc = 0;
17248 
17249 	rtnl_lock();
17250 	netdev_lock(dev);
17251 	rc = pci_enable_device(bp->pdev);
17252 	if (rc) {
17253 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17254 			   rc);
17255 		goto resume_exit;
17256 	}
17257 	pci_set_master(bp->pdev);
17258 	if (bnxt_hwrm_ver_get(bp)) {
17259 		rc = -ENODEV;
17260 		goto resume_exit;
17261 	}
17262 	rc = bnxt_hwrm_func_reset(bp);
17263 	if (rc) {
17264 		rc = -EBUSY;
17265 		goto resume_exit;
17266 	}
17267 
17268 	rc = bnxt_hwrm_func_qcaps(bp);
17269 	if (rc)
17270 		goto resume_exit;
17271 
17272 	bnxt_clear_reservations(bp, true);
17273 
17274 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17275 		rc = -ENODEV;
17276 		goto resume_exit;
17277 	}
17278 	if (bp->fw_crash_mem)
17279 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17280 
17281 	if (bnxt_ptp_init(bp)) {
17282 		kfree(bp->ptp_cfg);
17283 		bp->ptp_cfg = NULL;
17284 	}
17285 	bnxt_get_wol_settings(bp);
17286 	if (netif_running(dev)) {
17287 		rc = bnxt_open(dev);
17288 		if (!rc)
17289 			netif_device_attach(dev);
17290 	}
17291 
17292 resume_exit:
17293 	netdev_unlock(bp->dev);
17294 	rtnl_unlock();
17295 	if (!rc) {
17296 		bnxt_ulp_start(bp);
17297 		bnxt_reenable_sriov(bp);
17298 	}
17299 	return rc;
17300 }
17301 
17302 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17303 #define BNXT_PM_OPS (&bnxt_pm_ops)
17304 
17305 #else
17306 
17307 #define BNXT_PM_OPS NULL
17308 
17309 #endif /* CONFIG_PM_SLEEP */
17310 
17311 /**
17312  * bnxt_io_error_detected - called when PCI error is detected
17313  * @pdev: Pointer to PCI device
17314  * @state: The current pci connection state
17315  *
17316  * This function is called after a PCI bus error affecting
17317  * this device has been detected.
17318  */
17319 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17320 					       pci_channel_state_t state)
17321 {
17322 	struct net_device *netdev = pci_get_drvdata(pdev);
17323 	struct bnxt *bp = netdev_priv(netdev);
17324 	bool abort = false;
17325 
17326 	netdev_info(netdev, "PCI I/O error detected\n");
17327 
17328 	bnxt_ulp_stop(bp);
17329 
17330 	netdev_lock(netdev);
17331 	netif_device_detach(netdev);
17332 
17333 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17334 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17335 		abort = true;
17336 	}
17337 
17338 	if (abort || state == pci_channel_io_perm_failure) {
17339 		netdev_unlock(netdev);
17340 		return PCI_ERS_RESULT_DISCONNECT;
17341 	}
17342 
17343 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17344 	 * so we disable bus master to prevent any potential bad DMAs before
17345 	 * freeing kernel memory.
17346 	 */
17347 	if (state == pci_channel_io_frozen) {
17348 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
17349 		bnxt_fw_fatal_close(bp);
17350 	}
17351 
17352 	if (netif_running(netdev))
17353 		__bnxt_close_nic(bp, true, true);
17354 
17355 	if (pci_is_enabled(pdev))
17356 		pci_disable_device(pdev);
17357 	bnxt_free_ctx_mem(bp, false);
17358 	netdev_unlock(netdev);
17359 
17360 	/* Request a slot reset. */
17361 	return PCI_ERS_RESULT_NEED_RESET;
17362 }
17363 
17364 /**
17365  * bnxt_io_slot_reset - called after the pci bus has been reset.
17366  * @pdev: Pointer to PCI device
17367  *
17368  * Restart the card from scratch, as if from a cold-boot.
17369  * At this point, the card has experienced a hard reset,
17370  * followed by fixups by BIOS, and has its config space
17371  * set up identically to what it was at cold boot.
17372  */
17373 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17374 {
17375 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17376 	struct net_device *netdev = pci_get_drvdata(pdev);
17377 	struct bnxt *bp = netdev_priv(netdev);
17378 	int retry = 0;
17379 	int err = 0;
17380 	int off;
17381 
17382 	netdev_info(bp->dev, "PCI Slot Reset\n");
17383 
17384 	if (test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) {
17385 		/* After DPC, the chip should return CRS when the vendor ID
17386 		 * config register is read until it is ready.  On all chips,
17387 		 * this is not happening reliably so add a 5-second delay as a
17388 		 * workaround.
17389 		 */
17390 		msleep(5000);
17391 	}
17392 
17393 	netdev_lock(netdev);
17394 
17395 	if (pci_enable_device(pdev)) {
17396 		dev_err(&pdev->dev,
17397 			"Cannot re-enable PCI device after reset.\n");
17398 	} else {
17399 		pci_set_master(pdev);
17400 		/* Upon fatal error, our device internal logic that latches to
17401 		 * BAR value is getting reset and will restore only upon
17402 		 * rewriting the BARs.
17403 		 *
17404 		 * As pci_restore_state() does not re-write the BARs if the
17405 		 * value is same as saved value earlier, driver needs to
17406 		 * write the BARs to 0 to force restore, in case of fatal error.
17407 		 */
17408 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17409 				       &bp->state)) {
17410 			for (off = PCI_BASE_ADDRESS_0;
17411 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17412 				pci_write_config_dword(bp->pdev, off, 0);
17413 		}
17414 		pci_restore_state(pdev);
17415 
17416 		bnxt_inv_fw_health_reg(bp);
17417 		bnxt_try_map_fw_health_reg(bp);
17418 
17419 		/* In some PCIe AER scenarios, firmware may take up to
17420 		 * 10 seconds to become ready in the worst case.
17421 		 */
17422 		do {
17423 			err = bnxt_try_recover_fw(bp);
17424 			if (!err)
17425 				break;
17426 			retry++;
17427 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17428 
17429 		if (err) {
17430 			dev_err(&pdev->dev, "Firmware not ready\n");
17431 			goto reset_exit;
17432 		}
17433 
17434 		err = bnxt_hwrm_func_reset(bp);
17435 		if (!err)
17436 			result = PCI_ERS_RESULT_RECOVERED;
17437 
17438 		/* IRQ will be initialized later in bnxt_io_resume */
17439 		bnxt_ulp_irq_stop(bp);
17440 		bnxt_clear_int_mode(bp);
17441 	}
17442 
17443 reset_exit:
17444 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17445 	bnxt_clear_reservations(bp, true);
17446 	netdev_unlock(netdev);
17447 
17448 	return result;
17449 }
17450 
17451 /**
17452  * bnxt_io_resume - called when traffic can start flowing again.
17453  * @pdev: Pointer to PCI device
17454  *
17455  * This callback is called when the error recovery driver tells
17456  * us that its OK to resume normal operation.
17457  */
17458 static void bnxt_io_resume(struct pci_dev *pdev)
17459 {
17460 	struct net_device *netdev = pci_get_drvdata(pdev);
17461 	struct bnxt *bp = netdev_priv(netdev);
17462 	int err;
17463 
17464 	netdev_info(bp->dev, "PCI Slot Resume\n");
17465 	rtnl_lock();
17466 	netdev_lock(netdev);
17467 
17468 	err = bnxt_hwrm_func_qcaps(bp);
17469 	if (!err) {
17470 		if (netif_running(netdev)) {
17471 			err = bnxt_open(netdev);
17472 		} else {
17473 			err = bnxt_reserve_rings(bp, true);
17474 			if (!err)
17475 				err = bnxt_init_int_mode(bp);
17476 		}
17477 	}
17478 
17479 	if (!err)
17480 		netif_device_attach(netdev);
17481 
17482 	netdev_unlock(netdev);
17483 	rtnl_unlock();
17484 	if (!err) {
17485 		bnxt_ulp_start(bp);
17486 		bnxt_reenable_sriov(bp);
17487 	}
17488 }
17489 
17490 static const struct pci_error_handlers bnxt_err_handler = {
17491 	.error_detected	= bnxt_io_error_detected,
17492 	.slot_reset	= bnxt_io_slot_reset,
17493 	.resume		= bnxt_io_resume
17494 };
17495 
17496 static struct pci_driver bnxt_pci_driver = {
17497 	.name		= DRV_MODULE_NAME,
17498 	.id_table	= bnxt_pci_tbl,
17499 	.probe		= bnxt_init_one,
17500 	.remove		= bnxt_remove_one,
17501 	.shutdown	= bnxt_shutdown,
17502 	.driver.pm	= BNXT_PM_OPS,
17503 	.err_handler	= &bnxt_err_handler,
17504 #if defined(CONFIG_BNXT_SRIOV)
17505 	.sriov_configure = bnxt_sriov_configure,
17506 #endif
17507 };
17508 
17509 static int __init bnxt_init(void)
17510 {
17511 	int err;
17512 
17513 	bnxt_debug_init();
17514 	err = pci_register_driver(&bnxt_pci_driver);
17515 	if (err) {
17516 		bnxt_debug_exit();
17517 		return err;
17518 	}
17519 
17520 	return 0;
17521 }
17522 
17523 static void __exit bnxt_exit(void)
17524 {
17525 	pci_unregister_driver(&bnxt_pci_driver);
17526 	if (bnxt_pf_wq)
17527 		destroy_workqueue(bnxt_pf_wq);
17528 	bnxt_debug_exit();
17529 }
17530 
17531 module_init(bnxt_init);
17532 module_exit(bnxt_exit);
17533