xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 686a7587bd0be9407f5ea748edf3d8bb00e5bc72)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 #include <linux/bnxt/hsi.h>
62 
63 #include "bnxt.h"
64 #include "bnxt_hwrm.h"
65 #include "bnxt_ulp.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 
78 #define BNXT_TX_TIMEOUT		(5 * HZ)
79 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
80 				 NETIF_MSG_TX_ERR)
81 
82 MODULE_IMPORT_NS("NETDEV_INTERNAL");
83 MODULE_LICENSE("GPL");
84 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
85 
86 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
87 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
88 
89 #define BNXT_TX_PUSH_THRESH 164
90 
91 /* indexed by enum board_idx */
92 static const struct {
93 	char *name;
94 } board_info[] = {
95 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
96 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
97 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
98 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
99 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
100 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
101 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
102 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
103 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
104 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
105 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
106 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
108 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
109 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
110 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
111 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
112 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
113 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
114 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
115 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
116 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
117 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
118 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
119 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
120 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
121 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
122 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
123 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
124 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
127 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
129 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
130 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
131 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
132 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
133 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
134 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
135 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
136 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
137 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
138 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
139 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
140 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
141 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
142 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
143 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
145 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
146 };
147 
148 static const struct pci_device_id bnxt_pci_tbl[] = {
149 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
150 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
157 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
183 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
198 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
199 #ifdef CONFIG_BNXT_SRIOV
200 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
218 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
219 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
220 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
221 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
223 #endif
224 	{ 0 }
225 };
226 
227 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
228 
229 static const u16 bnxt_vf_req_snif[] = {
230 	HWRM_FUNC_CFG,
231 	HWRM_FUNC_VF_CFG,
232 	HWRM_PORT_PHY_QCFG,
233 	HWRM_CFA_L2_FILTER_ALLOC,
234 };
235 
236 static const u16 bnxt_async_events_arr[] = {
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
240 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
241 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
246 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
247 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
248 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
249 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
250 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
253 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
254 };
255 
256 const u16 bnxt_bstore_to_trace[] = {
257 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
258 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
259 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
260 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
261 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
262 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
263 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
264 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
265 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
266 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
267 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
268 	[BNXT_CTX_KONG]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE,
269 	[BNXT_CTX_QPC]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE,
270 };
271 
272 static struct workqueue_struct *bnxt_pf_wq;
273 
274 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
275 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
276 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
277 
278 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
279 	.ports = {
280 		.src = 0,
281 		.dst = 0,
282 	},
283 	.addrs = {
284 		.v6addrs = {
285 			.src = BNXT_IPV6_MASK_NONE,
286 			.dst = BNXT_IPV6_MASK_NONE,
287 		},
288 	},
289 };
290 
291 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
292 	.ports = {
293 		.src = cpu_to_be16(0xffff),
294 		.dst = cpu_to_be16(0xffff),
295 	},
296 	.addrs = {
297 		.v6addrs = {
298 			.src = BNXT_IPV6_MASK_ALL,
299 			.dst = BNXT_IPV6_MASK_ALL,
300 		},
301 	},
302 };
303 
304 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
305 	.ports = {
306 		.src = cpu_to_be16(0xffff),
307 		.dst = cpu_to_be16(0xffff),
308 	},
309 	.addrs = {
310 		.v4addrs = {
311 			.src = cpu_to_be32(0xffffffff),
312 			.dst = cpu_to_be32(0xffffffff),
313 		},
314 	},
315 };
316 
317 static bool bnxt_vf_pciid(enum board_idx idx)
318 {
319 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
320 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
321 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
322 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
323 		idx == NETXTREME_E_P7_VF_HV);
324 }
325 
326 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
327 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
328 
329 #define BNXT_DB_CQ(db, idx)						\
330 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
331 
332 #define BNXT_DB_NQ_P5(db, idx)						\
333 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
334 		    (db)->doorbell)
335 
336 #define BNXT_DB_NQ_P7(db, idx)						\
337 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
338 		    DB_RING_IDX(db, idx), (db)->doorbell)
339 
340 #define BNXT_DB_CQ_ARM(db, idx)						\
341 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
342 
343 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
344 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
345 		    DB_RING_IDX(db, idx), (db)->doorbell)
346 
347 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
348 {
349 	if (bp->flags & BNXT_FLAG_CHIP_P7)
350 		BNXT_DB_NQ_P7(db, idx);
351 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
352 		BNXT_DB_NQ_P5(db, idx);
353 	else
354 		BNXT_DB_CQ(db, idx);
355 }
356 
357 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
358 {
359 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
360 		BNXT_DB_NQ_ARM_P5(db, idx);
361 	else
362 		BNXT_DB_CQ_ARM(db, idx);
363 }
364 
365 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
366 {
367 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
368 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
369 			    DB_RING_IDX(db, idx), db->doorbell);
370 	else
371 		BNXT_DB_CQ(db, idx);
372 }
373 
374 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
375 {
376 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
377 		return;
378 
379 	if (BNXT_PF(bp))
380 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
381 	else
382 		schedule_delayed_work(&bp->fw_reset_task, delay);
383 }
384 
385 static void __bnxt_queue_sp_work(struct bnxt *bp)
386 {
387 	if (BNXT_PF(bp))
388 		queue_work(bnxt_pf_wq, &bp->sp_task);
389 	else
390 		schedule_work(&bp->sp_task);
391 }
392 
393 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
394 {
395 	set_bit(event, &bp->sp_event);
396 	__bnxt_queue_sp_work(bp);
397 }
398 
399 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
400 {
401 	if (!rxr->bnapi->in_reset) {
402 		rxr->bnapi->in_reset = true;
403 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
404 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
405 		else
406 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
407 		__bnxt_queue_sp_work(bp);
408 	}
409 	rxr->rx_next_cons = 0xffff;
410 }
411 
412 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
413 			  u16 curr)
414 {
415 	struct bnxt_napi *bnapi = txr->bnapi;
416 
417 	if (bnapi->tx_fault)
418 		return;
419 
420 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
421 		   txr->txq_index, txr->tx_hw_cons,
422 		   txr->tx_cons, txr->tx_prod, curr);
423 	WARN_ON_ONCE(1);
424 	bnapi->tx_fault = 1;
425 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
426 }
427 
428 const u16 bnxt_lhint_arr[] = {
429 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
430 	TX_BD_FLAGS_LHINT_512_TO_1023,
431 	TX_BD_FLAGS_LHINT_1024_TO_2047,
432 	TX_BD_FLAGS_LHINT_1024_TO_2047,
433 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
434 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
444 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
445 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
446 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
447 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
448 };
449 
450 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
451 {
452 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
453 
454 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
455 		return 0;
456 
457 	return md_dst->u.port_info.port_id;
458 }
459 
460 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
461 			     u16 prod)
462 {
463 	/* Sync BD data before updating doorbell */
464 	wmb();
465 	bnxt_db_write(bp, &txr->tx_db, prod);
466 	txr->kick_pending = 0;
467 }
468 
469 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
470 {
471 	struct bnxt *bp = netdev_priv(dev);
472 	struct tx_bd *txbd, *txbd0;
473 	struct tx_bd_ext *txbd1;
474 	struct netdev_queue *txq;
475 	int i;
476 	dma_addr_t mapping;
477 	unsigned int length, pad = 0;
478 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
479 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
480 	struct pci_dev *pdev = bp->pdev;
481 	u16 prod, last_frag, txts_prod;
482 	struct bnxt_tx_ring_info *txr;
483 	struct bnxt_sw_tx_bd *tx_buf;
484 	__le32 lflags = 0;
485 	skb_frag_t *frag;
486 
487 	i = skb_get_queue_mapping(skb);
488 	if (unlikely(i >= bp->tx_nr_rings)) {
489 		dev_kfree_skb_any(skb);
490 		dev_core_stats_tx_dropped_inc(dev);
491 		return NETDEV_TX_OK;
492 	}
493 
494 	txq = netdev_get_tx_queue(dev, i);
495 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
496 	prod = txr->tx_prod;
497 
498 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
499 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
500 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
501 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
502 		if (skb_linearize(skb)) {
503 			dev_kfree_skb_any(skb);
504 			dev_core_stats_tx_dropped_inc(dev);
505 			return NETDEV_TX_OK;
506 		}
507 	}
508 #endif
509 	free_size = bnxt_tx_avail(bp, txr);
510 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
511 		/* We must have raced with NAPI cleanup */
512 		if (net_ratelimit() && txr->kick_pending)
513 			netif_warn(bp, tx_err, dev,
514 				   "bnxt: ring busy w/ flush pending!\n");
515 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
516 					bp->tx_wake_thresh))
517 			return NETDEV_TX_BUSY;
518 	}
519 
520 	length = skb->len;
521 	len = skb_headlen(skb);
522 	last_frag = skb_shinfo(skb)->nr_frags;
523 
524 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
525 
526 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
527 	tx_buf->skb = skb;
528 	tx_buf->nr_frags = last_frag;
529 
530 	vlan_tag_flags = 0;
531 	cfa_action = bnxt_xmit_get_cfa_action(skb);
532 	if (skb_vlan_tag_present(skb)) {
533 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
534 				 skb_vlan_tag_get(skb);
535 		/* Currently supports 8021Q, 8021AD vlan offloads
536 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
537 		 */
538 		if (skb->vlan_proto == htons(ETH_P_8021Q))
539 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
540 	}
541 
542 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
543 	    ptp->tx_tstamp_en) {
544 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
545 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
546 			tx_buf->is_ts_pkt = 1;
547 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
548 		} else if (!skb_is_gso(skb)) {
549 			u16 seq_id, hdr_off;
550 
551 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
552 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
553 				if (vlan_tag_flags)
554 					hdr_off += VLAN_HLEN;
555 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
556 				tx_buf->is_ts_pkt = 1;
557 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
558 
559 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
560 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
561 				tx_buf->txts_prod = txts_prod;
562 			}
563 		}
564 	}
565 	if (unlikely(skb->no_fcs))
566 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
567 
568 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
569 	    skb_frags_readable(skb) && !lflags) {
570 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
571 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
572 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
573 		void __iomem *db = txr->tx_db.doorbell;
574 		void *pdata = tx_push_buf->data;
575 		u64 *end;
576 		int j, push_len;
577 
578 		/* Set COAL_NOW to be ready quickly for the next push */
579 		tx_push->tx_bd_len_flags_type =
580 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
581 					TX_BD_TYPE_LONG_TX_BD |
582 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
583 					TX_BD_FLAGS_COAL_NOW |
584 					TX_BD_FLAGS_PACKET_END |
585 					TX_BD_CNT(2));
586 
587 		if (skb->ip_summed == CHECKSUM_PARTIAL)
588 			tx_push1->tx_bd_hsize_lflags =
589 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
590 		else
591 			tx_push1->tx_bd_hsize_lflags = 0;
592 
593 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
594 		tx_push1->tx_bd_cfa_action =
595 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
596 
597 		end = pdata + length;
598 		end = PTR_ALIGN(end, 8) - 1;
599 		*end = 0;
600 
601 		skb_copy_from_linear_data(skb, pdata, len);
602 		pdata += len;
603 		for (j = 0; j < last_frag; j++) {
604 			void *fptr;
605 
606 			frag = &skb_shinfo(skb)->frags[j];
607 			fptr = skb_frag_address_safe(frag);
608 			if (!fptr)
609 				goto normal_tx;
610 
611 			memcpy(pdata, fptr, skb_frag_size(frag));
612 			pdata += skb_frag_size(frag);
613 		}
614 
615 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
616 		txbd->tx_bd_haddr = txr->data_mapping;
617 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
618 		prod = NEXT_TX(prod);
619 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
620 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
621 		memcpy(txbd, tx_push1, sizeof(*txbd));
622 		prod = NEXT_TX(prod);
623 		tx_push->doorbell =
624 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
625 				    DB_RING_IDX(&txr->tx_db, prod));
626 		WRITE_ONCE(txr->tx_prod, prod);
627 
628 		tx_buf->is_push = 1;
629 		netdev_tx_sent_queue(txq, skb->len);
630 		wmb();	/* Sync is_push and byte queue before pushing data */
631 
632 		push_len = (length + sizeof(*tx_push) + 7) / 8;
633 		if (push_len > 16) {
634 			__iowrite64_copy(db, tx_push_buf, 16);
635 			__iowrite32_copy(db + 4, tx_push_buf + 1,
636 					 (push_len - 16) << 1);
637 		} else {
638 			__iowrite64_copy(db, tx_push_buf, push_len);
639 		}
640 
641 		goto tx_done;
642 	}
643 
644 normal_tx:
645 	if (length < BNXT_MIN_PKT_SIZE) {
646 		pad = BNXT_MIN_PKT_SIZE - length;
647 		if (skb_pad(skb, pad))
648 			/* SKB already freed. */
649 			goto tx_kick_pending;
650 		length = BNXT_MIN_PKT_SIZE;
651 	}
652 
653 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
654 
655 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
656 		goto tx_free;
657 
658 	dma_unmap_addr_set(tx_buf, mapping, mapping);
659 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
660 		TX_BD_CNT(last_frag + 2);
661 
662 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
663 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
664 
665 	prod = NEXT_TX(prod);
666 	txbd1 = (struct tx_bd_ext *)
667 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
668 
669 	txbd1->tx_bd_hsize_lflags = lflags;
670 	if (skb_is_gso(skb)) {
671 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
672 		u32 hdr_len;
673 
674 		if (skb->encapsulation) {
675 			if (udp_gso)
676 				hdr_len = skb_inner_transport_offset(skb) +
677 					  sizeof(struct udphdr);
678 			else
679 				hdr_len = skb_inner_tcp_all_headers(skb);
680 		} else if (udp_gso) {
681 			hdr_len = skb_transport_offset(skb) +
682 				  sizeof(struct udphdr);
683 		} else {
684 			hdr_len = skb_tcp_all_headers(skb);
685 		}
686 
687 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
688 					TX_BD_FLAGS_T_IPID |
689 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
690 		length = skb_shinfo(skb)->gso_size;
691 		txbd1->tx_bd_mss = cpu_to_le32(length);
692 		length += hdr_len;
693 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
694 		txbd1->tx_bd_hsize_lflags |=
695 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
696 		txbd1->tx_bd_mss = 0;
697 	}
698 
699 	length >>= 9;
700 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
701 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
702 				     skb->len);
703 		i = 0;
704 		goto tx_dma_error;
705 	}
706 	flags |= bnxt_lhint_arr[length];
707 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
708 
709 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
710 	txbd1->tx_bd_cfa_action =
711 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
712 	txbd0 = txbd;
713 	for (i = 0; i < last_frag; i++) {
714 		frag = &skb_shinfo(skb)->frags[i];
715 		prod = NEXT_TX(prod);
716 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
717 
718 		len = skb_frag_size(frag);
719 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
720 					   DMA_TO_DEVICE);
721 
722 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
723 			goto tx_dma_error;
724 
725 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
726 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
727 					  mapping, mapping);
728 
729 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
730 
731 		flags = len << TX_BD_LEN_SHIFT;
732 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
733 	}
734 
735 	flags &= ~TX_BD_LEN;
736 	txbd->tx_bd_len_flags_type =
737 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
738 			    TX_BD_FLAGS_PACKET_END);
739 
740 	netdev_tx_sent_queue(txq, skb->len);
741 
742 	skb_tx_timestamp(skb);
743 
744 	prod = NEXT_TX(prod);
745 	WRITE_ONCE(txr->tx_prod, prod);
746 
747 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
748 		bnxt_txr_db_kick(bp, txr, prod);
749 	} else {
750 		if (free_size >= bp->tx_wake_thresh)
751 			txbd0->tx_bd_len_flags_type |=
752 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
753 		txr->kick_pending = 1;
754 	}
755 
756 tx_done:
757 
758 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
759 		if (netdev_xmit_more() && !tx_buf->is_push) {
760 			txbd0->tx_bd_len_flags_type &=
761 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
762 			bnxt_txr_db_kick(bp, txr, prod);
763 		}
764 
765 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
766 				   bp->tx_wake_thresh);
767 	}
768 	return NETDEV_TX_OK;
769 
770 tx_dma_error:
771 	last_frag = i;
772 
773 	/* start back at beginning and unmap skb */
774 	prod = txr->tx_prod;
775 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
776 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
777 			 skb_headlen(skb), DMA_TO_DEVICE);
778 	prod = NEXT_TX(prod);
779 
780 	/* unmap remaining mapped pages */
781 	for (i = 0; i < last_frag; i++) {
782 		prod = NEXT_TX(prod);
783 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
784 		frag = &skb_shinfo(skb)->frags[i];
785 		netmem_dma_unmap_page_attrs(&pdev->dev,
786 					    dma_unmap_addr(tx_buf, mapping),
787 					    skb_frag_size(frag),
788 					    DMA_TO_DEVICE, 0);
789 	}
790 
791 tx_free:
792 	dev_kfree_skb_any(skb);
793 tx_kick_pending:
794 	if (BNXT_TX_PTP_IS_SET(lflags)) {
795 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
796 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
797 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
798 			/* set SKB to err so PTP worker will clean up */
799 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
800 	}
801 	if (txr->kick_pending)
802 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
803 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
804 	dev_core_stats_tx_dropped_inc(dev);
805 	return NETDEV_TX_OK;
806 }
807 
808 /* Returns true if some remaining TX packets not processed. */
809 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
810 			  int budget)
811 {
812 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
813 	struct pci_dev *pdev = bp->pdev;
814 	u16 hw_cons = txr->tx_hw_cons;
815 	unsigned int tx_bytes = 0;
816 	u16 cons = txr->tx_cons;
817 	skb_frag_t *frag;
818 	int tx_pkts = 0;
819 	bool rc = false;
820 
821 	while (RING_TX(bp, cons) != hw_cons) {
822 		struct bnxt_sw_tx_bd *tx_buf;
823 		struct sk_buff *skb;
824 		bool is_ts_pkt;
825 		int j, last;
826 
827 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
828 		skb = tx_buf->skb;
829 
830 		if (unlikely(!skb)) {
831 			bnxt_sched_reset_txr(bp, txr, cons);
832 			return rc;
833 		}
834 
835 		is_ts_pkt = tx_buf->is_ts_pkt;
836 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
837 			rc = true;
838 			break;
839 		}
840 
841 		cons = NEXT_TX(cons);
842 		tx_pkts++;
843 		tx_bytes += skb->len;
844 		tx_buf->skb = NULL;
845 		tx_buf->is_ts_pkt = 0;
846 
847 		if (tx_buf->is_push) {
848 			tx_buf->is_push = 0;
849 			goto next_tx_int;
850 		}
851 
852 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
853 				 skb_headlen(skb), DMA_TO_DEVICE);
854 		last = tx_buf->nr_frags;
855 
856 		for (j = 0; j < last; j++) {
857 			frag = &skb_shinfo(skb)->frags[j];
858 			cons = NEXT_TX(cons);
859 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
860 			netmem_dma_unmap_page_attrs(&pdev->dev,
861 						    dma_unmap_addr(tx_buf,
862 								   mapping),
863 						    skb_frag_size(frag),
864 						    DMA_TO_DEVICE, 0);
865 		}
866 		if (unlikely(is_ts_pkt)) {
867 			if (BNXT_CHIP_P5(bp)) {
868 				/* PTP worker takes ownership of the skb */
869 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
870 				skb = NULL;
871 			}
872 		}
873 
874 next_tx_int:
875 		cons = NEXT_TX(cons);
876 
877 		napi_consume_skb(skb, budget);
878 	}
879 
880 	WRITE_ONCE(txr->tx_cons, cons);
881 
882 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
883 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
884 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
885 
886 	return rc;
887 }
888 
889 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
890 {
891 	struct bnxt_tx_ring_info *txr;
892 	bool more = false;
893 	int i;
894 
895 	bnxt_for_each_napi_tx(i, bnapi, txr) {
896 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
897 			more |= __bnxt_tx_int(bp, txr, budget);
898 	}
899 	if (!more)
900 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
901 }
902 
903 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
904 {
905 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
906 }
907 
908 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
909 					 struct bnxt_rx_ring_info *rxr,
910 					 unsigned int *offset,
911 					 gfp_t gfp)
912 {
913 	struct page *page;
914 
915 	if (rxr->rx_page_size < PAGE_SIZE) {
916 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
917 						rxr->rx_page_size);
918 	} else {
919 		page = page_pool_dev_alloc_pages(rxr->page_pool);
920 		*offset = 0;
921 	}
922 	if (!page)
923 		return NULL;
924 
925 	*mapping = page_pool_get_dma_addr(page) + *offset;
926 	return page;
927 }
928 
929 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
930 					 struct bnxt_rx_ring_info *rxr,
931 					 unsigned int *offset,
932 					 gfp_t gfp)
933 {
934 	netmem_ref netmem;
935 
936 	if (rxr->rx_page_size < PAGE_SIZE) {
937 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
938 						     rxr->rx_page_size, gfp);
939 	} else {
940 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
941 		*offset = 0;
942 	}
943 	if (!netmem)
944 		return 0;
945 
946 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
947 	return netmem;
948 }
949 
950 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
951 				       struct bnxt_rx_ring_info *rxr,
952 				       gfp_t gfp)
953 {
954 	unsigned int offset;
955 	struct page *page;
956 
957 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
958 				    bp->rx_buf_size, gfp);
959 	if (!page)
960 		return NULL;
961 
962 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
963 	return page_address(page) + offset;
964 }
965 
966 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
967 		       u16 prod, gfp_t gfp)
968 {
969 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
970 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
971 	dma_addr_t mapping;
972 
973 	if (BNXT_RX_PAGE_MODE(bp)) {
974 		unsigned int offset;
975 		struct page *page =
976 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
977 
978 		if (!page)
979 			return -ENOMEM;
980 
981 		mapping += bp->rx_dma_offset;
982 		rx_buf->data = page;
983 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
984 	} else {
985 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
986 
987 		if (!data)
988 			return -ENOMEM;
989 
990 		rx_buf->data = data;
991 		rx_buf->data_ptr = data + bp->rx_offset;
992 	}
993 	rx_buf->mapping = mapping;
994 
995 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
996 	return 0;
997 }
998 
999 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1000 {
1001 	u16 prod = rxr->rx_prod;
1002 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1003 	struct bnxt *bp = rxr->bnapi->bp;
1004 	struct rx_bd *cons_bd, *prod_bd;
1005 
1006 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1007 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1008 
1009 	prod_rx_buf->data = data;
1010 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1011 
1012 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1013 
1014 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1015 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1016 
1017 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1018 }
1019 
1020 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1021 {
1022 	u16 next, max = rxr->rx_agg_bmap_size;
1023 
1024 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1025 	if (next >= max)
1026 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1027 	return next;
1028 }
1029 
1030 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1031 				u16 prod, gfp_t gfp)
1032 {
1033 	struct rx_bd *rxbd =
1034 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1035 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1036 	u16 sw_prod = rxr->rx_sw_agg_prod;
1037 	unsigned int offset = 0;
1038 	dma_addr_t mapping;
1039 	netmem_ref netmem;
1040 
1041 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1042 	if (!netmem)
1043 		return -ENOMEM;
1044 
1045 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1046 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1047 
1048 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1049 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1050 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1051 
1052 	rx_agg_buf->netmem = netmem;
1053 	rx_agg_buf->offset = offset;
1054 	rx_agg_buf->mapping = mapping;
1055 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1056 	rxbd->rx_bd_opaque = sw_prod;
1057 	return 0;
1058 }
1059 
1060 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1061 				       struct bnxt_cp_ring_info *cpr,
1062 				       u16 cp_cons, u16 curr)
1063 {
1064 	struct rx_agg_cmp *agg;
1065 
1066 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1067 	agg = (struct rx_agg_cmp *)
1068 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1069 	return agg;
1070 }
1071 
1072 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1073 					      struct bnxt_rx_ring_info *rxr,
1074 					      u16 agg_id, u16 curr)
1075 {
1076 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1077 
1078 	return &tpa_info->agg_arr[curr];
1079 }
1080 
1081 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1082 				   u16 start, u32 agg_bufs, bool tpa)
1083 {
1084 	struct bnxt_napi *bnapi = cpr->bnapi;
1085 	struct bnxt *bp = bnapi->bp;
1086 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1087 	u16 prod = rxr->rx_agg_prod;
1088 	u16 sw_prod = rxr->rx_sw_agg_prod;
1089 	bool p5_tpa = false;
1090 	u32 i;
1091 
1092 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1093 		p5_tpa = true;
1094 
1095 	for (i = 0; i < agg_bufs; i++) {
1096 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1097 		struct rx_agg_cmp *agg;
1098 		struct rx_bd *prod_bd;
1099 		netmem_ref netmem;
1100 		u16 cons;
1101 
1102 		if (p5_tpa)
1103 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1104 		else
1105 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1106 		cons = agg->rx_agg_cmp_opaque;
1107 		__clear_bit(cons, rxr->rx_agg_bmap);
1108 
1109 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1110 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1111 
1112 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1113 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1114 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1115 
1116 		/* It is possible for sw_prod to be equal to cons, so
1117 		 * set cons_rx_buf->netmem to 0 first.
1118 		 */
1119 		netmem = cons_rx_buf->netmem;
1120 		cons_rx_buf->netmem = 0;
1121 		prod_rx_buf->netmem = netmem;
1122 		prod_rx_buf->offset = cons_rx_buf->offset;
1123 
1124 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1125 
1126 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1127 
1128 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1129 		prod_bd->rx_bd_opaque = sw_prod;
1130 
1131 		prod = NEXT_RX_AGG(prod);
1132 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1133 	}
1134 	rxr->rx_agg_prod = prod;
1135 	rxr->rx_sw_agg_prod = sw_prod;
1136 }
1137 
1138 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1139 					      struct bnxt_rx_ring_info *rxr,
1140 					      u16 cons, void *data, u8 *data_ptr,
1141 					      dma_addr_t dma_addr,
1142 					      unsigned int offset_and_len)
1143 {
1144 	unsigned int len = offset_and_len & 0xffff;
1145 	struct page *page = data;
1146 	u16 prod = rxr->rx_prod;
1147 	struct sk_buff *skb;
1148 	int err;
1149 
1150 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1151 	if (unlikely(err)) {
1152 		bnxt_reuse_rx_data(rxr, cons, data);
1153 		return NULL;
1154 	}
1155 	dma_addr -= bp->rx_dma_offset;
1156 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1157 				bp->rx_dir);
1158 	skb = napi_build_skb(data_ptr - bp->rx_offset, rxr->rx_page_size);
1159 	if (!skb) {
1160 		page_pool_recycle_direct(rxr->page_pool, page);
1161 		return NULL;
1162 	}
1163 	skb_mark_for_recycle(skb);
1164 	skb_reserve(skb, bp->rx_offset);
1165 	__skb_put(skb, len);
1166 
1167 	return skb;
1168 }
1169 
1170 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1171 					struct bnxt_rx_ring_info *rxr,
1172 					u16 cons, void *data, u8 *data_ptr,
1173 					dma_addr_t dma_addr,
1174 					unsigned int offset_and_len)
1175 {
1176 	unsigned int payload = offset_and_len >> 16;
1177 	unsigned int len = offset_and_len & 0xffff;
1178 	skb_frag_t *frag;
1179 	struct page *page = data;
1180 	u16 prod = rxr->rx_prod;
1181 	struct sk_buff *skb;
1182 	int off, err;
1183 
1184 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1185 	if (unlikely(err)) {
1186 		bnxt_reuse_rx_data(rxr, cons, data);
1187 		return NULL;
1188 	}
1189 	dma_addr -= bp->rx_dma_offset;
1190 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1191 				bp->rx_dir);
1192 
1193 	if (unlikely(!payload))
1194 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1195 
1196 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1197 	if (!skb) {
1198 		page_pool_recycle_direct(rxr->page_pool, page);
1199 		return NULL;
1200 	}
1201 
1202 	skb_mark_for_recycle(skb);
1203 	off = (void *)data_ptr - page_address(page);
1204 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1205 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1206 	       payload + NET_IP_ALIGN);
1207 
1208 	frag = &skb_shinfo(skb)->frags[0];
1209 	skb_frag_size_sub(frag, payload);
1210 	skb_frag_off_add(frag, payload);
1211 	skb->data_len -= payload;
1212 	skb->tail += payload;
1213 
1214 	return skb;
1215 }
1216 
1217 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1218 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1219 				   void *data, u8 *data_ptr,
1220 				   dma_addr_t dma_addr,
1221 				   unsigned int offset_and_len)
1222 {
1223 	u16 prod = rxr->rx_prod;
1224 	struct sk_buff *skb;
1225 	int err;
1226 
1227 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1228 	if (unlikely(err)) {
1229 		bnxt_reuse_rx_data(rxr, cons, data);
1230 		return NULL;
1231 	}
1232 
1233 	skb = napi_build_skb(data, bp->rx_buf_size);
1234 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1235 				bp->rx_dir);
1236 	if (!skb) {
1237 		page_pool_free_va(rxr->head_pool, data, true);
1238 		return NULL;
1239 	}
1240 
1241 	skb_mark_for_recycle(skb);
1242 	skb_reserve(skb, bp->rx_offset);
1243 	skb_put(skb, offset_and_len & 0xffff);
1244 	return skb;
1245 }
1246 
1247 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1248 				 struct bnxt_cp_ring_info *cpr,
1249 				 u16 idx, u32 agg_bufs, bool tpa,
1250 				 struct sk_buff *skb,
1251 				 struct xdp_buff *xdp)
1252 {
1253 	struct bnxt_napi *bnapi = cpr->bnapi;
1254 	struct skb_shared_info *shinfo;
1255 	struct bnxt_rx_ring_info *rxr;
1256 	u32 i, total_frag_len = 0;
1257 	bool p5_tpa = false;
1258 	u16 prod;
1259 
1260 	rxr = bnapi->rx_ring;
1261 	prod = rxr->rx_agg_prod;
1262 
1263 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1264 		p5_tpa = true;
1265 
1266 	if (skb)
1267 		shinfo = skb_shinfo(skb);
1268 	else
1269 		shinfo = xdp_get_shared_info_from_buff(xdp);
1270 
1271 	for (i = 0; i < agg_bufs; i++) {
1272 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1273 		struct rx_agg_cmp *agg;
1274 		u16 cons, frag_len;
1275 		netmem_ref netmem;
1276 
1277 		if (p5_tpa)
1278 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1279 		else
1280 			agg = bnxt_get_agg(bp, cpr, idx, i);
1281 		cons = agg->rx_agg_cmp_opaque;
1282 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1283 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1284 
1285 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1286 		if (skb) {
1287 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1288 					       cons_rx_buf->offset,
1289 					       frag_len, rxr->rx_page_size);
1290 		} else {
1291 			skb_frag_t *frag = &shinfo->frags[i];
1292 
1293 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1294 						  cons_rx_buf->offset,
1295 						  frag_len);
1296 			shinfo->nr_frags = i + 1;
1297 		}
1298 		__clear_bit(cons, rxr->rx_agg_bmap);
1299 
1300 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1301 		 * a sw_prod index that equals the cons index, so we
1302 		 * need to clear the cons entry now.
1303 		 */
1304 		netmem = cons_rx_buf->netmem;
1305 		cons_rx_buf->netmem = 0;
1306 
1307 		if (xdp && netmem_is_pfmemalloc(netmem))
1308 			xdp_buff_set_frag_pfmemalloc(xdp);
1309 
1310 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1311 			if (skb) {
1312 				skb->len -= frag_len;
1313 				skb->data_len -= frag_len;
1314 				skb->truesize -= rxr->rx_page_size;
1315 			}
1316 
1317 			--shinfo->nr_frags;
1318 			cons_rx_buf->netmem = netmem;
1319 
1320 			/* Update prod since possibly some netmems have been
1321 			 * allocated already.
1322 			 */
1323 			rxr->rx_agg_prod = prod;
1324 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1325 			return 0;
1326 		}
1327 
1328 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1329 						  rxr->rx_page_size);
1330 
1331 		total_frag_len += frag_len;
1332 		prod = NEXT_RX_AGG(prod);
1333 	}
1334 	rxr->rx_agg_prod = prod;
1335 	return total_frag_len;
1336 }
1337 
1338 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1339 					       struct bnxt_cp_ring_info *cpr,
1340 					       struct sk_buff *skb, u16 idx,
1341 					       u32 agg_bufs, bool tpa)
1342 {
1343 	u32 total_frag_len = 0;
1344 
1345 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1346 					       skb, NULL);
1347 	if (!total_frag_len) {
1348 		skb_mark_for_recycle(skb);
1349 		dev_kfree_skb(skb);
1350 		return NULL;
1351 	}
1352 
1353 	return skb;
1354 }
1355 
1356 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1357 				   struct bnxt_cp_ring_info *cpr,
1358 				   struct xdp_buff *xdp, u16 idx,
1359 				   u32 agg_bufs, bool tpa)
1360 {
1361 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1362 	u32 total_frag_len = 0;
1363 
1364 	if (!xdp_buff_has_frags(xdp))
1365 		shinfo->nr_frags = 0;
1366 
1367 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1368 					       NULL, xdp);
1369 	if (total_frag_len) {
1370 		xdp_buff_set_frags_flag(xdp);
1371 		shinfo->nr_frags = agg_bufs;
1372 		shinfo->xdp_frags_size = total_frag_len;
1373 	}
1374 	return total_frag_len;
1375 }
1376 
1377 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1378 			       u8 agg_bufs, u32 *raw_cons)
1379 {
1380 	u16 last;
1381 	struct rx_agg_cmp *agg;
1382 
1383 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1384 	last = RING_CMP(*raw_cons);
1385 	agg = (struct rx_agg_cmp *)
1386 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1387 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1388 }
1389 
1390 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1391 				      unsigned int len,
1392 				      dma_addr_t mapping)
1393 {
1394 	struct bnxt *bp = bnapi->bp;
1395 	struct pci_dev *pdev = bp->pdev;
1396 	struct sk_buff *skb;
1397 
1398 	skb = napi_alloc_skb(&bnapi->napi, len);
1399 	if (!skb)
1400 		return NULL;
1401 
1402 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1403 				bp->rx_dir);
1404 
1405 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1406 	       len + NET_IP_ALIGN);
1407 
1408 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1409 				   bp->rx_dir);
1410 
1411 	skb_put(skb, len);
1412 
1413 	return skb;
1414 }
1415 
1416 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1417 				     unsigned int len,
1418 				     dma_addr_t mapping)
1419 {
1420 	return bnxt_copy_data(bnapi, data, len, mapping);
1421 }
1422 
1423 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1424 				     struct xdp_buff *xdp,
1425 				     unsigned int len,
1426 				     dma_addr_t mapping)
1427 {
1428 	unsigned int metasize = 0;
1429 	u8 *data = xdp->data;
1430 	struct sk_buff *skb;
1431 
1432 	len = xdp->data_end - xdp->data_meta;
1433 	metasize = xdp->data - xdp->data_meta;
1434 	data = xdp->data_meta;
1435 
1436 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1437 	if (!skb)
1438 		return skb;
1439 
1440 	if (metasize) {
1441 		skb_metadata_set(skb, metasize);
1442 		__skb_pull(skb, metasize);
1443 	}
1444 
1445 	return skb;
1446 }
1447 
1448 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1449 			   u32 *raw_cons, void *cmp)
1450 {
1451 	struct rx_cmp *rxcmp = cmp;
1452 	u32 tmp_raw_cons = *raw_cons;
1453 	u8 cmp_type, agg_bufs = 0;
1454 
1455 	cmp_type = RX_CMP_TYPE(rxcmp);
1456 
1457 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1458 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1459 			    RX_CMP_AGG_BUFS) >>
1460 			   RX_CMP_AGG_BUFS_SHIFT;
1461 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1462 		struct rx_tpa_end_cmp *tpa_end = cmp;
1463 
1464 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1465 			return 0;
1466 
1467 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1468 	}
1469 
1470 	if (agg_bufs) {
1471 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1472 			return -EBUSY;
1473 	}
1474 	*raw_cons = tmp_raw_cons;
1475 	return 0;
1476 }
1477 
1478 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1479 {
1480 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1481 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1482 
1483 	if (test_bit(idx, map->agg_idx_bmap)) {
1484 		idx = find_first_zero_bit(map->agg_idx_bmap, MAX_TPA_P5);
1485 		if (idx >= MAX_TPA_P5)
1486 			return INVALID_HW_RING_ID;
1487 	}
1488 	__set_bit(idx, map->agg_idx_bmap);
1489 	map->agg_id_tbl[agg_id] = idx;
1490 	return idx;
1491 }
1492 
1493 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1494 {
1495 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1496 
1497 	__clear_bit(idx, map->agg_idx_bmap);
1498 }
1499 
1500 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1501 {
1502 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1503 
1504 	return map->agg_id_tbl[agg_id];
1505 }
1506 
1507 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1508 			      struct rx_tpa_start_cmp *tpa_start,
1509 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1510 {
1511 	tpa_info->cfa_code_valid = 1;
1512 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1513 	tpa_info->vlan_valid = 0;
1514 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1515 		tpa_info->vlan_valid = 1;
1516 		tpa_info->metadata =
1517 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1518 	}
1519 }
1520 
1521 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1522 				 struct rx_tpa_start_cmp *tpa_start,
1523 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1524 {
1525 	tpa_info->vlan_valid = 0;
1526 	if (TPA_START_VLAN_VALID(tpa_start)) {
1527 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1528 		u32 vlan_proto = ETH_P_8021Q;
1529 
1530 		tpa_info->vlan_valid = 1;
1531 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1532 			vlan_proto = ETH_P_8021AD;
1533 		tpa_info->metadata = vlan_proto << 16 |
1534 				     TPA_START_METADATA0_TCI(tpa_start1);
1535 	}
1536 }
1537 
1538 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1539 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1540 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1541 {
1542 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1543 	struct bnxt_tpa_info *tpa_info;
1544 	u16 cons, prod, agg_id;
1545 	struct rx_bd *prod_bd;
1546 	dma_addr_t mapping;
1547 
1548 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1549 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1550 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1551 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1552 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1553 				    rxr->bnapi->index,
1554 				    TPA_START_AGG_ID_P5(tpa_start));
1555 			bnxt_sched_reset_rxr(bp, rxr);
1556 			return;
1557 		}
1558 	} else {
1559 		agg_id = TPA_START_AGG_ID(tpa_start);
1560 	}
1561 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1562 	prod = rxr->rx_prod;
1563 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1564 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1565 	tpa_info = &rxr->rx_tpa[agg_id];
1566 
1567 	if (unlikely(cons != rxr->rx_next_cons ||
1568 		     TPA_START_ERROR(tpa_start))) {
1569 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1570 			    cons, rxr->rx_next_cons,
1571 			    TPA_START_ERROR_CODE(tpa_start1));
1572 		bnxt_sched_reset_rxr(bp, rxr);
1573 		return;
1574 	}
1575 	prod_rx_buf->data = tpa_info->data;
1576 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1577 
1578 	mapping = tpa_info->mapping;
1579 	prod_rx_buf->mapping = mapping;
1580 
1581 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1582 
1583 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1584 
1585 	tpa_info->data = cons_rx_buf->data;
1586 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1587 	cons_rx_buf->data = NULL;
1588 	tpa_info->mapping = cons_rx_buf->mapping;
1589 
1590 	tpa_info->len =
1591 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1592 				RX_TPA_START_CMP_LEN_SHIFT;
1593 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1594 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1595 		tpa_info->gso_type = SKB_GSO_TCPV4;
1596 		if (TPA_START_IS_IPV6(tpa_start1))
1597 			tpa_info->gso_type = SKB_GSO_TCPV6;
1598 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1599 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1600 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1601 			tpa_info->gso_type = SKB_GSO_TCPV6;
1602 		tpa_info->rss_hash =
1603 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1604 	} else {
1605 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1606 		tpa_info->gso_type = 0;
1607 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1608 	}
1609 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1610 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1611 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1612 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1613 	else
1614 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1615 	tpa_info->agg_count = 0;
1616 
1617 	rxr->rx_prod = NEXT_RX(prod);
1618 	cons = RING_RX(bp, NEXT_RX(cons));
1619 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1620 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1621 
1622 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1623 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1624 	cons_rx_buf->data = NULL;
1625 }
1626 
1627 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1628 {
1629 	if (agg_bufs)
1630 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1631 }
1632 
1633 #ifdef CONFIG_INET
1634 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1635 {
1636 	struct udphdr *uh = NULL;
1637 
1638 	if (ip_proto == htons(ETH_P_IP)) {
1639 		struct iphdr *iph = (struct iphdr *)skb->data;
1640 
1641 		if (iph->protocol == IPPROTO_UDP)
1642 			uh = (struct udphdr *)(iph + 1);
1643 	} else {
1644 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1645 
1646 		if (iph->nexthdr == IPPROTO_UDP)
1647 			uh = (struct udphdr *)(iph + 1);
1648 	}
1649 	if (uh) {
1650 		if (uh->check)
1651 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1652 		else
1653 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1654 	}
1655 }
1656 #endif
1657 
1658 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1659 					   int payload_off, int tcp_ts,
1660 					   struct sk_buff *skb)
1661 {
1662 #ifdef CONFIG_INET
1663 	struct tcphdr *th;
1664 	int len, nw_off;
1665 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1666 	u32 hdr_info = tpa_info->hdr_info;
1667 	bool loopback = false;
1668 
1669 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1670 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1671 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1672 
1673 	/* If the packet is an internal loopback packet, the offsets will
1674 	 * have an extra 4 bytes.
1675 	 */
1676 	if (inner_mac_off == 4) {
1677 		loopback = true;
1678 	} else if (inner_mac_off > 4) {
1679 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1680 					    ETH_HLEN - 2));
1681 
1682 		/* We only support inner iPv4/ipv6.  If we don't see the
1683 		 * correct protocol ID, it must be a loopback packet where
1684 		 * the offsets are off by 4.
1685 		 */
1686 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1687 			loopback = true;
1688 	}
1689 	if (loopback) {
1690 		/* internal loopback packet, subtract all offsets by 4 */
1691 		inner_ip_off -= 4;
1692 		inner_mac_off -= 4;
1693 		outer_ip_off -= 4;
1694 	}
1695 
1696 	nw_off = inner_ip_off - ETH_HLEN;
1697 	skb_set_network_header(skb, nw_off);
1698 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1699 		struct ipv6hdr *iph = ipv6_hdr(skb);
1700 
1701 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1702 		len = skb->len - skb_transport_offset(skb);
1703 		th = tcp_hdr(skb);
1704 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1705 	} else {
1706 		struct iphdr *iph = ip_hdr(skb);
1707 
1708 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1709 		len = skb->len - skb_transport_offset(skb);
1710 		th = tcp_hdr(skb);
1711 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1712 	}
1713 
1714 	if (inner_mac_off) { /* tunnel */
1715 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1716 					    ETH_HLEN - 2));
1717 
1718 		bnxt_gro_tunnel(skb, proto);
1719 	}
1720 #endif
1721 	return skb;
1722 }
1723 
1724 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1725 					   int payload_off, int tcp_ts,
1726 					   struct sk_buff *skb)
1727 {
1728 #ifdef CONFIG_INET
1729 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1730 	u32 hdr_info = tpa_info->hdr_info;
1731 	int iphdr_len, nw_off;
1732 
1733 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1734 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1735 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1736 
1737 	nw_off = inner_ip_off - ETH_HLEN;
1738 	skb_set_network_header(skb, nw_off);
1739 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1740 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1741 	skb_set_transport_header(skb, nw_off + iphdr_len);
1742 
1743 	if (inner_mac_off) { /* tunnel */
1744 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1745 					    ETH_HLEN - 2));
1746 
1747 		bnxt_gro_tunnel(skb, proto);
1748 	}
1749 #endif
1750 	return skb;
1751 }
1752 
1753 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1754 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1755 
1756 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1757 					   int payload_off, int tcp_ts,
1758 					   struct sk_buff *skb)
1759 {
1760 #ifdef CONFIG_INET
1761 	struct tcphdr *th;
1762 	int len, nw_off, tcp_opt_len = 0;
1763 
1764 	if (tcp_ts)
1765 		tcp_opt_len = 12;
1766 
1767 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1768 		struct iphdr *iph;
1769 
1770 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1771 			 ETH_HLEN;
1772 		skb_set_network_header(skb, nw_off);
1773 		iph = ip_hdr(skb);
1774 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1775 		len = skb->len - skb_transport_offset(skb);
1776 		th = tcp_hdr(skb);
1777 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1778 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1779 		struct ipv6hdr *iph;
1780 
1781 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1782 			 ETH_HLEN;
1783 		skb_set_network_header(skb, nw_off);
1784 		iph = ipv6_hdr(skb);
1785 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1786 		len = skb->len - skb_transport_offset(skb);
1787 		th = tcp_hdr(skb);
1788 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1789 	} else {
1790 		dev_kfree_skb_any(skb);
1791 		return NULL;
1792 	}
1793 
1794 	if (nw_off) /* tunnel */
1795 		bnxt_gro_tunnel(skb, skb->protocol);
1796 #endif
1797 	return skb;
1798 }
1799 
1800 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1801 					   struct bnxt_tpa_info *tpa_info,
1802 					   struct rx_tpa_end_cmp *tpa_end,
1803 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1804 					   struct sk_buff *skb,
1805 					   struct bnxt_rx_sw_stats *rx_stats)
1806 {
1807 #ifdef CONFIG_INET
1808 	int payload_off;
1809 	u16 segs;
1810 
1811 	segs = TPA_END_TPA_SEGS(tpa_end);
1812 	if (segs == 1)
1813 		return skb;
1814 
1815 	rx_stats->rx_hw_gro_packets++;
1816 	rx_stats->rx_hw_gro_wire_packets += segs;
1817 
1818 	NAPI_GRO_CB(skb)->count = segs;
1819 	skb_shinfo(skb)->gso_size =
1820 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1821 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1822 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1823 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1824 	else
1825 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1826 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1827 	if (likely(skb))
1828 		tcp_gro_complete(skb);
1829 #endif
1830 	return skb;
1831 }
1832 
1833 /* Given the cfa_code of a received packet determine which
1834  * netdev (vf-rep or PF) the packet is destined to.
1835  */
1836 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1837 {
1838 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1839 
1840 	/* if vf-rep dev is NULL, it must belong to the PF */
1841 	return dev ? dev : bp->dev;
1842 }
1843 
1844 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1845 					   struct bnxt_cp_ring_info *cpr,
1846 					   u32 *raw_cons,
1847 					   struct rx_tpa_end_cmp *tpa_end,
1848 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1849 					   u8 *event)
1850 {
1851 	struct bnxt_napi *bnapi = cpr->bnapi;
1852 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1853 	struct net_device *dev = bp->dev;
1854 	u8 *data_ptr, agg_bufs;
1855 	unsigned int len;
1856 	struct bnxt_tpa_info *tpa_info;
1857 	dma_addr_t mapping;
1858 	struct sk_buff *skb;
1859 	u16 idx = 0, agg_id;
1860 	void *data;
1861 	bool gro;
1862 
1863 	if (unlikely(bnapi->in_reset)) {
1864 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1865 
1866 		if (rc < 0)
1867 			return ERR_PTR(-EBUSY);
1868 		return NULL;
1869 	}
1870 
1871 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1872 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1873 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1874 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1875 		tpa_info = &rxr->rx_tpa[agg_id];
1876 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1877 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1878 				    agg_bufs, tpa_info->agg_count);
1879 			agg_bufs = tpa_info->agg_count;
1880 		}
1881 		tpa_info->agg_count = 0;
1882 		*event |= BNXT_AGG_EVENT;
1883 		bnxt_free_agg_idx(rxr, agg_id);
1884 		idx = agg_id;
1885 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1886 	} else {
1887 		agg_id = TPA_END_AGG_ID(tpa_end);
1888 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1889 		tpa_info = &rxr->rx_tpa[agg_id];
1890 		idx = RING_CMP(*raw_cons);
1891 		if (agg_bufs) {
1892 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1893 				return ERR_PTR(-EBUSY);
1894 
1895 			*event |= BNXT_AGG_EVENT;
1896 			idx = NEXT_CMP(idx);
1897 		}
1898 		gro = !!TPA_END_GRO(tpa_end);
1899 	}
1900 	data = tpa_info->data;
1901 	data_ptr = tpa_info->data_ptr;
1902 	prefetch(data_ptr);
1903 	len = tpa_info->len;
1904 	mapping = tpa_info->mapping;
1905 
1906 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1907 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1908 		if (agg_bufs > MAX_SKB_FRAGS)
1909 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1910 				    agg_bufs, (int)MAX_SKB_FRAGS);
1911 		return NULL;
1912 	}
1913 
1914 	if (len <= bp->rx_copybreak) {
1915 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1916 		if (!skb) {
1917 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1918 			cpr->sw_stats->rx.rx_oom_discards += 1;
1919 			return NULL;
1920 		}
1921 	} else {
1922 		u8 *new_data;
1923 		dma_addr_t new_mapping;
1924 
1925 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1926 						GFP_ATOMIC);
1927 		if (!new_data) {
1928 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1929 			cpr->sw_stats->rx.rx_oom_discards += 1;
1930 			return NULL;
1931 		}
1932 
1933 		tpa_info->data = new_data;
1934 		tpa_info->data_ptr = new_data + bp->rx_offset;
1935 		tpa_info->mapping = new_mapping;
1936 
1937 		skb = napi_build_skb(data, bp->rx_buf_size);
1938 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1939 					bp->rx_buf_use_size, bp->rx_dir);
1940 
1941 		if (!skb) {
1942 			page_pool_free_va(rxr->head_pool, data, true);
1943 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1944 			cpr->sw_stats->rx.rx_oom_discards += 1;
1945 			return NULL;
1946 		}
1947 		skb_mark_for_recycle(skb);
1948 		skb_reserve(skb, bp->rx_offset);
1949 		skb_put(skb, len);
1950 	}
1951 
1952 	if (agg_bufs) {
1953 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1954 					      true);
1955 		if (!skb) {
1956 			/* Page reuse already handled by bnxt_rx_pages(). */
1957 			cpr->sw_stats->rx.rx_oom_discards += 1;
1958 			return NULL;
1959 		}
1960 	}
1961 
1962 	if (tpa_info->cfa_code_valid)
1963 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1964 	skb->protocol = eth_type_trans(skb, dev);
1965 
1966 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1967 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1968 
1969 	if (tpa_info->vlan_valid &&
1970 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1971 		__be16 vlan_proto = htons(tpa_info->metadata >>
1972 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1973 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1974 
1975 		if (eth_type_vlan(vlan_proto)) {
1976 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1977 		} else {
1978 			dev_kfree_skb(skb);
1979 			return NULL;
1980 		}
1981 	}
1982 
1983 	skb_checksum_none_assert(skb);
1984 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1985 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1986 		skb->csum_level =
1987 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1988 	}
1989 
1990 	if (gro)
1991 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
1992 				   &cpr->sw_stats->rx);
1993 
1994 	return skb;
1995 }
1996 
1997 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1998 			 struct rx_agg_cmp *rx_agg)
1999 {
2000 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2001 	struct bnxt_tpa_info *tpa_info;
2002 
2003 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2004 	tpa_info = &rxr->rx_tpa[agg_id];
2005 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2006 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2007 }
2008 
2009 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2010 			     struct sk_buff *skb)
2011 {
2012 	skb_mark_for_recycle(skb);
2013 
2014 	if (skb->dev != bp->dev) {
2015 		/* this packet belongs to a vf-rep */
2016 		bnxt_vf_rep_rx(bp, skb);
2017 		return;
2018 	}
2019 	skb_record_rx_queue(skb, bnapi->index);
2020 	napi_gro_receive(&bnapi->napi, skb);
2021 }
2022 
2023 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2024 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2025 {
2026 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2027 
2028 	if (BNXT_PTP_RX_TS_VALID(flags))
2029 		goto ts_valid;
2030 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2031 		return false;
2032 
2033 ts_valid:
2034 	*cmpl_ts = ts;
2035 	return true;
2036 }
2037 
2038 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2039 				    struct rx_cmp *rxcmp,
2040 				    struct rx_cmp_ext *rxcmp1)
2041 {
2042 	__be16 vlan_proto;
2043 	u16 vtag;
2044 
2045 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2046 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2047 		u32 meta_data;
2048 
2049 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2050 			return skb;
2051 
2052 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2053 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2054 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2055 		if (eth_type_vlan(vlan_proto))
2056 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2057 		else
2058 			goto vlan_err;
2059 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2060 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2061 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2062 
2063 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2064 				vlan_proto = htons(ETH_P_8021Q);
2065 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2066 				vlan_proto = htons(ETH_P_8021AD);
2067 			else
2068 				goto vlan_err;
2069 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2070 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2071 		}
2072 	}
2073 	return skb;
2074 vlan_err:
2075 	skb_mark_for_recycle(skb);
2076 	dev_kfree_skb(skb);
2077 	return NULL;
2078 }
2079 
2080 /* returns the following:
2081  * 1       - 1 packet successfully received
2082  * 0       - successful TPA_START, packet not completed yet
2083  * -EBUSY  - completion ring does not have all the agg buffers yet
2084  * -ENOMEM - packet aborted due to out of memory
2085  * -EIO    - packet aborted due to hw error indicated in BD
2086  */
2087 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2088 		       u32 *raw_cons, u8 *event)
2089 {
2090 	struct bnxt_napi *bnapi = cpr->bnapi;
2091 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2092 	struct net_device *dev = bp->dev;
2093 	struct rx_cmp *rxcmp;
2094 	struct rx_cmp_ext *rxcmp1;
2095 	u32 tmp_raw_cons = *raw_cons;
2096 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2097 	struct skb_shared_info *sinfo;
2098 	struct bnxt_xdp_buff bnxt_xdp;
2099 	struct bnxt_sw_rx_bd *rx_buf;
2100 	unsigned int len;
2101 	u8 *data_ptr, agg_bufs, cmp_type;
2102 	bool xdp_active = false;
2103 	dma_addr_t dma_addr;
2104 	struct sk_buff *skb;
2105 	u32 flags, misc;
2106 	u32 cmpl_ts;
2107 	void *data;
2108 	int rc = 0;
2109 
2110 	rxcmp = (struct rx_cmp *)
2111 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2112 
2113 	cmp_type = RX_CMP_TYPE(rxcmp);
2114 
2115 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2116 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2117 		goto next_rx_no_prod_no_len;
2118 	}
2119 
2120 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2121 	cp_cons = RING_CMP(tmp_raw_cons);
2122 	rxcmp1 = (struct rx_cmp_ext *)
2123 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2124 
2125 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2126 		return -EBUSY;
2127 
2128 	/* The valid test of the entry must be done first before
2129 	 * reading any further.
2130 	 */
2131 	dma_rmb();
2132 	prod = rxr->rx_prod;
2133 
2134 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2135 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2136 		bnxt_tpa_start(bp, rxr, cmp_type,
2137 			       (struct rx_tpa_start_cmp *)rxcmp,
2138 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2139 
2140 		*event |= BNXT_RX_EVENT;
2141 		goto next_rx_no_prod_no_len;
2142 
2143 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2144 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2145 				   (struct rx_tpa_end_cmp *)rxcmp,
2146 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2147 
2148 		if (IS_ERR(skb))
2149 			return -EBUSY;
2150 
2151 		rc = -ENOMEM;
2152 		if (likely(skb)) {
2153 			bnxt_deliver_skb(bp, bnapi, skb);
2154 			rc = 1;
2155 		}
2156 		*event |= BNXT_RX_EVENT;
2157 		goto next_rx_no_prod_no_len;
2158 	}
2159 
2160 	cons = rxcmp->rx_cmp_opaque;
2161 	if (unlikely(cons != rxr->rx_next_cons)) {
2162 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2163 
2164 		/* 0xffff is forced error, don't print it */
2165 		if (rxr->rx_next_cons != 0xffff)
2166 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2167 				    cons, rxr->rx_next_cons);
2168 		bnxt_sched_reset_rxr(bp, rxr);
2169 		if (rc1)
2170 			return rc1;
2171 		goto next_rx_no_prod_no_len;
2172 	}
2173 	rx_buf = &rxr->rx_buf_ring[cons];
2174 	data = rx_buf->data;
2175 	data_ptr = rx_buf->data_ptr;
2176 	prefetch(data_ptr);
2177 
2178 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2179 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2180 
2181 	if (agg_bufs) {
2182 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2183 			return -EBUSY;
2184 
2185 		cp_cons = NEXT_CMP(cp_cons);
2186 		*event |= BNXT_AGG_EVENT;
2187 	}
2188 	*event |= BNXT_RX_EVENT;
2189 
2190 	rx_buf->data = NULL;
2191 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2192 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2193 
2194 		bnxt_reuse_rx_data(rxr, cons, data);
2195 		if (agg_bufs)
2196 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2197 					       false);
2198 
2199 		rc = -EIO;
2200 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2201 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2202 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2203 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2204 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2205 						 rx_err);
2206 				bnxt_sched_reset_rxr(bp, rxr);
2207 			}
2208 		}
2209 		goto next_rx_no_len;
2210 	}
2211 
2212 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2213 	len = flags >> RX_CMP_LEN_SHIFT;
2214 	dma_addr = rx_buf->mapping;
2215 
2216 	if (bnxt_xdp_attached(bp, rxr)) {
2217 		bnxt_xdp.rxcmp = rxcmp;
2218 		bnxt_xdp.rxcmp1 = rxcmp1;
2219 		bnxt_xdp.cmp_type = cmp_type;
2220 
2221 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &bnxt_xdp.xdp);
2222 		if (agg_bufs) {
2223 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr,
2224 							       &bnxt_xdp.xdp,
2225 							       cp_cons,
2226 							       agg_bufs,
2227 							       false);
2228 			if (!frag_len)
2229 				goto oom_next_rx;
2230 
2231 		}
2232 		xdp_active = true;
2233 	}
2234 
2235 	if (xdp_active) {
2236 		if (bnxt_rx_xdp(bp, rxr, cons, &bnxt_xdp.xdp, data, &data_ptr,
2237 				&len, event)) {
2238 			rc = 1;
2239 			goto next_rx;
2240 		}
2241 		if (xdp_buff_has_frags(&bnxt_xdp.xdp)) {
2242 			sinfo = xdp_get_shared_info_from_buff(&bnxt_xdp.xdp);
2243 			agg_bufs = sinfo->nr_frags;
2244 		} else {
2245 			agg_bufs = 0;
2246 		}
2247 	}
2248 
2249 	if (len <= bp->rx_copybreak) {
2250 		if (!xdp_active)
2251 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2252 		else
2253 			skb = bnxt_copy_xdp(bnapi, &bnxt_xdp.xdp, len,
2254 					    dma_addr);
2255 		bnxt_reuse_rx_data(rxr, cons, data);
2256 		if (!skb) {
2257 			if (agg_bufs) {
2258 				if (!xdp_active)
2259 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2260 							       agg_bufs, false);
2261 				else
2262 					bnxt_xdp_buff_frags_free(rxr,
2263 								 &bnxt_xdp.xdp);
2264 			}
2265 			goto oom_next_rx;
2266 		}
2267 	} else {
2268 		u32 payload;
2269 
2270 		if (rx_buf->data_ptr == data_ptr)
2271 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2272 		else
2273 			payload = 0;
2274 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2275 				      payload | len);
2276 		if (!skb)
2277 			goto oom_next_rx;
2278 	}
2279 
2280 	if (agg_bufs) {
2281 		if (!xdp_active) {
2282 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2283 						      agg_bufs, false);
2284 			if (!skb)
2285 				goto oom_next_rx;
2286 		} else {
2287 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2288 						 rxr, &bnxt_xdp.xdp);
2289 			if (!skb) {
2290 				/* we should be able to free the old skb here */
2291 				bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2292 				goto oom_next_rx;
2293 			}
2294 		}
2295 	}
2296 
2297 	if (RX_CMP_HASH_VALID(rxcmp)) {
2298 		enum pkt_hash_types type;
2299 
2300 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2301 			type = bnxt_rss_ext_op(bp, rxcmp);
2302 		} else {
2303 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2304 
2305 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2306 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2307 				type = PKT_HASH_TYPE_L4;
2308 			else
2309 				type = PKT_HASH_TYPE_L3;
2310 		}
2311 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2312 	}
2313 
2314 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2315 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2316 	skb->protocol = eth_type_trans(skb, dev);
2317 
2318 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2319 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2320 		if (!skb)
2321 			goto next_rx;
2322 	}
2323 
2324 	skb_checksum_none_assert(skb);
2325 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2326 		if (dev->features & NETIF_F_RXCSUM) {
2327 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2328 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2329 		}
2330 	} else {
2331 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2332 			if (dev->features & NETIF_F_RXCSUM)
2333 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2334 		}
2335 	}
2336 
2337 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2338 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2339 			u64 ns, ts;
2340 
2341 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2342 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2343 
2344 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2345 				memset(skb_hwtstamps(skb), 0,
2346 				       sizeof(*skb_hwtstamps(skb)));
2347 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2348 			}
2349 		}
2350 	}
2351 	bnxt_deliver_skb(bp, bnapi, skb);
2352 	rc = 1;
2353 
2354 next_rx:
2355 	cpr->rx_packets += 1;
2356 	cpr->rx_bytes += len;
2357 
2358 next_rx_no_len:
2359 	rxr->rx_prod = NEXT_RX(prod);
2360 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2361 
2362 next_rx_no_prod_no_len:
2363 	*raw_cons = tmp_raw_cons;
2364 
2365 	return rc;
2366 
2367 oom_next_rx:
2368 	cpr->sw_stats->rx.rx_oom_discards += 1;
2369 	rc = -ENOMEM;
2370 	goto next_rx;
2371 }
2372 
2373 /* In netpoll mode, if we are using a combined completion ring, we need to
2374  * discard the rx packets and recycle the buffers.
2375  */
2376 static int bnxt_force_rx_discard(struct bnxt *bp,
2377 				 struct bnxt_cp_ring_info *cpr,
2378 				 u32 *raw_cons, u8 *event)
2379 {
2380 	u32 tmp_raw_cons = *raw_cons;
2381 	struct rx_cmp_ext *rxcmp1;
2382 	struct rx_cmp *rxcmp;
2383 	u16 cp_cons;
2384 	u8 cmp_type;
2385 	int rc;
2386 
2387 	cp_cons = RING_CMP(tmp_raw_cons);
2388 	rxcmp = (struct rx_cmp *)
2389 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2390 
2391 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2392 	cp_cons = RING_CMP(tmp_raw_cons);
2393 	rxcmp1 = (struct rx_cmp_ext *)
2394 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2395 
2396 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2397 		return -EBUSY;
2398 
2399 	/* The valid test of the entry must be done first before
2400 	 * reading any further.
2401 	 */
2402 	dma_rmb();
2403 	cmp_type = RX_CMP_TYPE(rxcmp);
2404 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2405 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2406 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2407 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2408 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2409 		struct rx_tpa_end_cmp_ext *tpa_end1;
2410 
2411 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2412 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2413 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2414 	}
2415 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2416 	if (rc && rc != -EBUSY)
2417 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2418 	return rc;
2419 }
2420 
2421 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2422 {
2423 	struct bnxt_fw_health *fw_health = bp->fw_health;
2424 	u32 reg = fw_health->regs[reg_idx];
2425 	u32 reg_type, reg_off, val = 0;
2426 
2427 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2428 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2429 	switch (reg_type) {
2430 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2431 		pci_read_config_dword(bp->pdev, reg_off, &val);
2432 		break;
2433 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2434 		reg_off = fw_health->mapped_regs[reg_idx];
2435 		fallthrough;
2436 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2437 		val = readl(bp->bar0 + reg_off);
2438 		break;
2439 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2440 		val = readl(bp->bar1 + reg_off);
2441 		break;
2442 	}
2443 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2444 		val &= fw_health->fw_reset_inprog_reg_mask;
2445 	return val;
2446 }
2447 
2448 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2449 {
2450 	int i;
2451 
2452 	for (i = 0; i < bp->rx_nr_rings; i++) {
2453 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2454 		struct bnxt_ring_grp_info *grp_info;
2455 
2456 		grp_info = &bp->grp_info[grp_idx];
2457 		if (grp_info->agg_fw_ring_id == ring_id)
2458 			return grp_idx;
2459 	}
2460 	return INVALID_HW_RING_ID;
2461 }
2462 
2463 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2464 {
2465 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2466 
2467 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2468 		return link_info->force_link_speed2;
2469 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2470 		return link_info->force_pam4_link_speed;
2471 	return link_info->force_link_speed;
2472 }
2473 
2474 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2475 {
2476 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2477 
2478 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2479 		link_info->req_link_speed = link_info->force_link_speed2;
2480 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2481 		switch (link_info->req_link_speed) {
2482 		case BNXT_LINK_SPEED_50GB_PAM4:
2483 		case BNXT_LINK_SPEED_100GB_PAM4:
2484 		case BNXT_LINK_SPEED_200GB_PAM4:
2485 		case BNXT_LINK_SPEED_400GB_PAM4:
2486 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2487 			break;
2488 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2489 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2490 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2491 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2492 			break;
2493 		default:
2494 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2495 		}
2496 		return;
2497 	}
2498 	link_info->req_link_speed = link_info->force_link_speed;
2499 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2500 	if (link_info->force_pam4_link_speed) {
2501 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2502 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2503 	}
2504 }
2505 
2506 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2507 {
2508 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2509 
2510 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2511 		link_info->advertising = link_info->auto_link_speeds2;
2512 		return;
2513 	}
2514 	link_info->advertising = link_info->auto_link_speeds;
2515 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2516 }
2517 
2518 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2519 {
2520 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2521 
2522 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2523 		if (link_info->req_link_speed != link_info->force_link_speed2)
2524 			return true;
2525 		return false;
2526 	}
2527 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2528 	    link_info->req_link_speed != link_info->force_link_speed)
2529 		return true;
2530 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2531 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2532 		return true;
2533 	return false;
2534 }
2535 
2536 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2537 {
2538 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2539 
2540 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2541 		if (link_info->advertising != link_info->auto_link_speeds2)
2542 			return true;
2543 		return false;
2544 	}
2545 	if (link_info->advertising != link_info->auto_link_speeds ||
2546 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2547 		return true;
2548 	return false;
2549 }
2550 
2551 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2552 {
2553 	u32 flags = bp->ctx->ctx_arr[type].flags;
2554 
2555 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2556 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2557 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2558 }
2559 
2560 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2561 {
2562 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2563 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2564 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2565 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2566 	struct bnxt_bs_trace_info *bs_trace;
2567 	int last_pg;
2568 
2569 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2570 		return;
2571 
2572 	mem_size = ctxm->max_entries * ctxm->entry_size;
2573 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2574 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2575 
2576 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2577 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2578 
2579 	rmem = &ctx_pg[0].ring_mem;
2580 	bs_trace = &bp->bs_trace[trace_type];
2581 	bs_trace->ctx_type = ctxm->type;
2582 	bs_trace->trace_type = trace_type;
2583 	if (pages > MAX_CTX_PAGES) {
2584 		int last_pg_dir = rmem->nr_pages - 1;
2585 
2586 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2587 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2588 	} else {
2589 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2590 	}
2591 	bs_trace->magic_byte += magic_byte_offset;
2592 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2593 }
2594 
2595 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2596 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2597 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2598 
2599 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2600 	(((data2) &							\
2601 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2602 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2603 
2604 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2605 	((data2) &							\
2606 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2607 
2608 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2609 	(((data2) &							\
2610 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2611 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2612 
2613 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2614 	((data1) &							\
2615 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2616 
2617 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2618 	(((data1) &							\
2619 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2620 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2621 
2622 /* Return true if the workqueue has to be scheduled */
2623 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2624 {
2625 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2626 
2627 	switch (err_type) {
2628 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2629 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2630 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2631 		break;
2632 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2633 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2634 		break;
2635 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2636 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2637 		break;
2638 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2639 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2640 		char *threshold_type;
2641 		bool notify = false;
2642 		char *dir_str;
2643 
2644 		switch (type) {
2645 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2646 			threshold_type = "warning";
2647 			break;
2648 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2649 			threshold_type = "critical";
2650 			break;
2651 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2652 			threshold_type = "fatal";
2653 			break;
2654 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2655 			threshold_type = "shutdown";
2656 			break;
2657 		default:
2658 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2659 			return false;
2660 		}
2661 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2662 			dir_str = "above";
2663 			notify = true;
2664 		} else {
2665 			dir_str = "below";
2666 		}
2667 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2668 			    dir_str, threshold_type);
2669 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2670 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2671 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2672 		if (notify) {
2673 			bp->thermal_threshold_type = type;
2674 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2675 			return true;
2676 		}
2677 		return false;
2678 	}
2679 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2680 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2681 		break;
2682 	default:
2683 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2684 			   err_type);
2685 		break;
2686 	}
2687 	return false;
2688 }
2689 
2690 #define BNXT_GET_EVENT_PORT(data)	\
2691 	((data) &			\
2692 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2693 
2694 #define BNXT_EVENT_RING_TYPE(data2)	\
2695 	((data2) &			\
2696 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2697 
2698 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2699 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2700 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2701 
2702 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2703 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2704 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2705 
2706 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2707 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2708 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2709 
2710 #define BNXT_PHC_BITS	48
2711 
2712 static int bnxt_async_event_process(struct bnxt *bp,
2713 				    struct hwrm_async_event_cmpl *cmpl)
2714 {
2715 	u16 event_id = le16_to_cpu(cmpl->event_id);
2716 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2717 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2718 
2719 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2720 		   event_id, data1, data2);
2721 
2722 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2723 	switch (event_id) {
2724 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2725 		struct bnxt_link_info *link_info = &bp->link_info;
2726 
2727 		if (BNXT_VF(bp))
2728 			goto async_event_process_exit;
2729 
2730 		/* print unsupported speed warning in forced speed mode only */
2731 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2732 		    (data1 & 0x20000)) {
2733 			u16 fw_speed = bnxt_get_force_speed(link_info);
2734 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2735 
2736 			if (speed != SPEED_UNKNOWN)
2737 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2738 					    speed);
2739 		}
2740 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2741 	}
2742 		fallthrough;
2743 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2744 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2745 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2746 		fallthrough;
2747 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2748 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2749 		break;
2750 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2751 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2752 		break;
2753 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2754 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2755 
2756 		if (BNXT_VF(bp))
2757 			break;
2758 
2759 		if (bp->pf.port_id != port_id)
2760 			break;
2761 
2762 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2763 		break;
2764 	}
2765 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2766 		if (BNXT_PF(bp))
2767 			goto async_event_process_exit;
2768 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2769 		break;
2770 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2771 		char *type_str = "Solicited";
2772 
2773 		if (!bp->fw_health)
2774 			goto async_event_process_exit;
2775 
2776 		bp->fw_reset_timestamp = jiffies;
2777 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2778 		if (!bp->fw_reset_min_dsecs)
2779 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2780 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2781 		if (!bp->fw_reset_max_dsecs)
2782 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2783 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2784 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2785 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2786 			type_str = "Fatal";
2787 			bp->fw_health->fatalities++;
2788 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2789 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2790 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2791 			type_str = "Non-fatal";
2792 			bp->fw_health->survivals++;
2793 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2794 		}
2795 		netif_warn(bp, hw, bp->dev,
2796 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2797 			   type_str, data1, data2,
2798 			   bp->fw_reset_min_dsecs * 100,
2799 			   bp->fw_reset_max_dsecs * 100);
2800 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2801 		break;
2802 	}
2803 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2804 		struct bnxt_fw_health *fw_health = bp->fw_health;
2805 		char *status_desc = "healthy";
2806 		u32 status;
2807 
2808 		if (!fw_health)
2809 			goto async_event_process_exit;
2810 
2811 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2812 			fw_health->enabled = false;
2813 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2814 			break;
2815 		}
2816 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2817 		fw_health->tmr_multiplier =
2818 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2819 				     bp->current_interval * 10);
2820 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2821 		if (!fw_health->enabled)
2822 			fw_health->last_fw_heartbeat =
2823 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2824 		fw_health->last_fw_reset_cnt =
2825 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2826 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2827 		if (status != BNXT_FW_STATUS_HEALTHY)
2828 			status_desc = "unhealthy";
2829 		netif_info(bp, drv, bp->dev,
2830 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2831 			   fw_health->primary ? "primary" : "backup", status,
2832 			   status_desc, fw_health->last_fw_reset_cnt);
2833 		if (!fw_health->enabled) {
2834 			/* Make sure tmr_counter is set and visible to
2835 			 * bnxt_health_check() before setting enabled to true.
2836 			 */
2837 			smp_wmb();
2838 			fw_health->enabled = true;
2839 		}
2840 		goto async_event_process_exit;
2841 	}
2842 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2843 		netif_notice(bp, hw, bp->dev,
2844 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2845 			     data1, data2);
2846 		goto async_event_process_exit;
2847 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2848 		struct bnxt_rx_ring_info *rxr;
2849 		u16 grp_idx;
2850 
2851 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2852 			goto async_event_process_exit;
2853 
2854 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2855 			    BNXT_EVENT_RING_TYPE(data2), data1);
2856 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2857 			goto async_event_process_exit;
2858 
2859 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2860 		if (grp_idx == INVALID_HW_RING_ID) {
2861 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2862 				    data1);
2863 			goto async_event_process_exit;
2864 		}
2865 		rxr = bp->bnapi[grp_idx]->rx_ring;
2866 		bnxt_sched_reset_rxr(bp, rxr);
2867 		goto async_event_process_exit;
2868 	}
2869 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2870 		struct bnxt_fw_health *fw_health = bp->fw_health;
2871 
2872 		netif_notice(bp, hw, bp->dev,
2873 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2874 			     data1, data2);
2875 		if (fw_health) {
2876 			fw_health->echo_req_data1 = data1;
2877 			fw_health->echo_req_data2 = data2;
2878 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2879 			break;
2880 		}
2881 		goto async_event_process_exit;
2882 	}
2883 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2884 		bnxt_ptp_pps_event(bp, data1, data2);
2885 		goto async_event_process_exit;
2886 	}
2887 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2888 		if (bnxt_event_error_report(bp, data1, data2))
2889 			break;
2890 		goto async_event_process_exit;
2891 	}
2892 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2893 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2894 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2895 			if (BNXT_PTP_USE_RTC(bp)) {
2896 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2897 				unsigned long flags;
2898 				u64 ns;
2899 
2900 				if (!ptp)
2901 					goto async_event_process_exit;
2902 
2903 				bnxt_ptp_update_current_time(bp);
2904 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2905 				       BNXT_PHC_BITS) | ptp->current_time);
2906 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2907 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2908 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2909 			}
2910 			break;
2911 		}
2912 		goto async_event_process_exit;
2913 	}
2914 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2915 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2916 
2917 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2918 		goto async_event_process_exit;
2919 	}
2920 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2921 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2922 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2923 
2924 		if (type >= ARRAY_SIZE(bp->bs_trace))
2925 			goto async_event_process_exit;
2926 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2927 		goto async_event_process_exit;
2928 	}
2929 	default:
2930 		goto async_event_process_exit;
2931 	}
2932 	__bnxt_queue_sp_work(bp);
2933 async_event_process_exit:
2934 	bnxt_ulp_async_events(bp, cmpl);
2935 	return 0;
2936 }
2937 
2938 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2939 {
2940 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2941 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2942 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2943 				(struct hwrm_fwd_req_cmpl *)txcmp;
2944 
2945 	switch (cmpl_type) {
2946 	case CMPL_BASE_TYPE_HWRM_DONE:
2947 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2948 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2949 		break;
2950 
2951 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2952 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2953 
2954 		if ((vf_id < bp->pf.first_vf_id) ||
2955 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2956 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2957 				   vf_id);
2958 			return -EINVAL;
2959 		}
2960 
2961 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2962 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2963 		break;
2964 
2965 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2966 		bnxt_async_event_process(bp,
2967 					 (struct hwrm_async_event_cmpl *)txcmp);
2968 		break;
2969 
2970 	default:
2971 		break;
2972 	}
2973 
2974 	return 0;
2975 }
2976 
2977 static bool bnxt_vnic_is_active(struct bnxt *bp)
2978 {
2979 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2980 
2981 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2982 }
2983 
2984 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2985 {
2986 	struct bnxt_napi *bnapi = dev_instance;
2987 	struct bnxt *bp = bnapi->bp;
2988 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2989 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2990 
2991 	cpr->event_ctr++;
2992 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2993 	napi_schedule(&bnapi->napi);
2994 	return IRQ_HANDLED;
2995 }
2996 
2997 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2998 {
2999 	u32 raw_cons = cpr->cp_raw_cons;
3000 	u16 cons = RING_CMP(raw_cons);
3001 	struct tx_cmp *txcmp;
3002 
3003 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3004 
3005 	return TX_CMP_VALID(txcmp, raw_cons);
3006 }
3007 
3008 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3009 			    int budget)
3010 {
3011 	struct bnxt_napi *bnapi = cpr->bnapi;
3012 	u32 raw_cons = cpr->cp_raw_cons;
3013 	bool flush_xdp = false;
3014 	u32 cons;
3015 	int rx_pkts = 0;
3016 	u8 event = 0;
3017 	struct tx_cmp *txcmp;
3018 
3019 	cpr->has_more_work = 0;
3020 	cpr->had_work_done = 1;
3021 	while (1) {
3022 		u8 cmp_type;
3023 		int rc;
3024 
3025 		cons = RING_CMP(raw_cons);
3026 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3027 
3028 		if (!TX_CMP_VALID(txcmp, raw_cons))
3029 			break;
3030 
3031 		/* The valid test of the entry must be done first before
3032 		 * reading any further.
3033 		 */
3034 		dma_rmb();
3035 		cmp_type = TX_CMP_TYPE(txcmp);
3036 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3037 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3038 			u32 opaque = txcmp->tx_cmp_opaque;
3039 			struct bnxt_tx_ring_info *txr;
3040 			u16 tx_freed;
3041 
3042 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3043 			event |= BNXT_TX_CMP_EVENT;
3044 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3045 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3046 			else
3047 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3048 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3049 				   bp->tx_ring_mask;
3050 			/* return full budget so NAPI will complete. */
3051 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3052 				rx_pkts = budget;
3053 				raw_cons = NEXT_RAW_CMP(raw_cons);
3054 				if (budget)
3055 					cpr->has_more_work = 1;
3056 				break;
3057 			}
3058 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3059 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3060 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3061 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3062 			if (likely(budget))
3063 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3064 			else
3065 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3066 							   &event);
3067 			if (event & BNXT_REDIRECT_EVENT)
3068 				flush_xdp = true;
3069 			if (likely(rc >= 0))
3070 				rx_pkts += rc;
3071 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3072 			 * the NAPI budget.  Otherwise, we may potentially loop
3073 			 * here forever if we consistently cannot allocate
3074 			 * buffers.
3075 			 */
3076 			else if (rc == -ENOMEM && budget)
3077 				rx_pkts++;
3078 			else if (rc == -EBUSY)	/* partial completion */
3079 				break;
3080 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3081 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3082 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3083 			bnxt_hwrm_handler(bp, txcmp);
3084 		}
3085 		raw_cons = NEXT_RAW_CMP(raw_cons);
3086 
3087 		if (rx_pkts && rx_pkts == budget) {
3088 			cpr->has_more_work = 1;
3089 			break;
3090 		}
3091 	}
3092 
3093 	if (flush_xdp) {
3094 		xdp_do_flush();
3095 		event &= ~BNXT_REDIRECT_EVENT;
3096 	}
3097 
3098 	if (event & BNXT_TX_EVENT) {
3099 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3100 		u16 prod = txr->tx_prod;
3101 
3102 		/* Sync BD data before updating doorbell */
3103 		wmb();
3104 
3105 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3106 		event &= ~BNXT_TX_EVENT;
3107 	}
3108 
3109 	cpr->cp_raw_cons = raw_cons;
3110 	bnapi->events |= event;
3111 	return rx_pkts;
3112 }
3113 
3114 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3115 				  int budget)
3116 {
3117 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3118 		bnapi->tx_int(bp, bnapi, budget);
3119 
3120 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3121 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3122 
3123 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3124 		bnapi->events &= ~BNXT_RX_EVENT;
3125 	}
3126 	if (bnapi->events & BNXT_AGG_EVENT) {
3127 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3128 
3129 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3130 		bnapi->events &= ~BNXT_AGG_EVENT;
3131 	}
3132 }
3133 
3134 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3135 			  int budget)
3136 {
3137 	struct bnxt_napi *bnapi = cpr->bnapi;
3138 	int rx_pkts;
3139 
3140 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3141 
3142 	/* ACK completion ring before freeing tx ring and producing new
3143 	 * buffers in rx/agg rings to prevent overflowing the completion
3144 	 * ring.
3145 	 */
3146 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3147 
3148 	__bnxt_poll_work_done(bp, bnapi, budget);
3149 	return rx_pkts;
3150 }
3151 
3152 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3153 {
3154 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3155 	struct bnxt *bp = bnapi->bp;
3156 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3157 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3158 	struct tx_cmp *txcmp;
3159 	struct rx_cmp_ext *rxcmp1;
3160 	u32 cp_cons, tmp_raw_cons;
3161 	u32 raw_cons = cpr->cp_raw_cons;
3162 	bool flush_xdp = false;
3163 	u32 rx_pkts = 0;
3164 	u8 event = 0;
3165 
3166 	while (1) {
3167 		int rc;
3168 
3169 		cp_cons = RING_CMP(raw_cons);
3170 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3171 
3172 		if (!TX_CMP_VALID(txcmp, raw_cons))
3173 			break;
3174 
3175 		/* The valid test of the entry must be done first before
3176 		 * reading any further.
3177 		 */
3178 		dma_rmb();
3179 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3180 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3181 			cp_cons = RING_CMP(tmp_raw_cons);
3182 			rxcmp1 = (struct rx_cmp_ext *)
3183 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3184 
3185 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3186 				break;
3187 
3188 			/* force an error to recycle the buffer */
3189 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3190 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3191 
3192 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3193 			if (likely(rc == -EIO) && budget)
3194 				rx_pkts++;
3195 			else if (rc == -EBUSY)	/* partial completion */
3196 				break;
3197 			if (event & BNXT_REDIRECT_EVENT)
3198 				flush_xdp = true;
3199 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3200 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3201 			bnxt_hwrm_handler(bp, txcmp);
3202 		} else {
3203 			netdev_err(bp->dev,
3204 				   "Invalid completion received on special ring\n");
3205 		}
3206 		raw_cons = NEXT_RAW_CMP(raw_cons);
3207 
3208 		if (rx_pkts == budget)
3209 			break;
3210 	}
3211 
3212 	cpr->cp_raw_cons = raw_cons;
3213 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3214 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3215 
3216 	if (event & BNXT_AGG_EVENT)
3217 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3218 	if (flush_xdp)
3219 		xdp_do_flush();
3220 
3221 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3222 		napi_complete_done(napi, rx_pkts);
3223 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3224 	}
3225 	return rx_pkts;
3226 }
3227 
3228 static int bnxt_poll(struct napi_struct *napi, int budget)
3229 {
3230 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3231 	struct bnxt *bp = bnapi->bp;
3232 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3233 	int work_done = 0;
3234 
3235 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3236 		napi_complete(napi);
3237 		return 0;
3238 	}
3239 	while (1) {
3240 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3241 
3242 		if (work_done >= budget) {
3243 			if (!budget)
3244 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3245 			break;
3246 		}
3247 
3248 		if (!bnxt_has_work(bp, cpr)) {
3249 			if (napi_complete_done(napi, work_done))
3250 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3251 			break;
3252 		}
3253 	}
3254 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3255 		struct dim_sample dim_sample = {};
3256 
3257 		dim_update_sample(cpr->event_ctr,
3258 				  cpr->rx_packets,
3259 				  cpr->rx_bytes,
3260 				  &dim_sample);
3261 		net_dim(&cpr->dim, &dim_sample);
3262 	}
3263 	return work_done;
3264 }
3265 
3266 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3267 {
3268 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3269 	int i, work_done = 0;
3270 
3271 	for (i = 0; i < cpr->cp_ring_count; i++) {
3272 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3273 
3274 		if (cpr2->had_nqe_notify) {
3275 			work_done += __bnxt_poll_work(bp, cpr2,
3276 						      budget - work_done);
3277 			cpr->has_more_work |= cpr2->has_more_work;
3278 		}
3279 	}
3280 	return work_done;
3281 }
3282 
3283 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3284 				 u64 dbr_type, int budget)
3285 {
3286 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3287 	int i;
3288 
3289 	for (i = 0; i < cpr->cp_ring_count; i++) {
3290 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3291 		struct bnxt_db_info *db;
3292 
3293 		if (cpr2->had_work_done) {
3294 			u32 tgl = 0;
3295 
3296 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3297 				cpr2->had_nqe_notify = 0;
3298 				tgl = cpr2->toggle;
3299 			}
3300 			db = &cpr2->cp_db;
3301 			bnxt_writeq(bp,
3302 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3303 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3304 				    db->doorbell);
3305 			cpr2->had_work_done = 0;
3306 		}
3307 	}
3308 	__bnxt_poll_work_done(bp, bnapi, budget);
3309 }
3310 
3311 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3312 {
3313 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3314 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3315 	struct bnxt_cp_ring_info *cpr_rx;
3316 	u32 raw_cons = cpr->cp_raw_cons;
3317 	struct bnxt *bp = bnapi->bp;
3318 	struct nqe_cn *nqcmp;
3319 	int work_done = 0;
3320 	u32 cons;
3321 
3322 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3323 		napi_complete(napi);
3324 		return 0;
3325 	}
3326 	if (cpr->has_more_work) {
3327 		cpr->has_more_work = 0;
3328 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3329 	}
3330 	while (1) {
3331 		u16 type;
3332 
3333 		cons = RING_CMP(raw_cons);
3334 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3335 
3336 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3337 			if (cpr->has_more_work)
3338 				break;
3339 
3340 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3341 					     budget);
3342 			cpr->cp_raw_cons = raw_cons;
3343 			if (napi_complete_done(napi, work_done))
3344 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3345 						  cpr->cp_raw_cons);
3346 			goto poll_done;
3347 		}
3348 
3349 		/* The valid test of the entry must be done first before
3350 		 * reading any further.
3351 		 */
3352 		dma_rmb();
3353 
3354 		type = le16_to_cpu(nqcmp->type);
3355 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3356 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3357 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3358 			struct bnxt_cp_ring_info *cpr2;
3359 
3360 			/* No more budget for RX work */
3361 			if (budget && work_done >= budget &&
3362 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3363 				break;
3364 
3365 			idx = BNXT_NQ_HDL_IDX(idx);
3366 			cpr2 = &cpr->cp_ring_arr[idx];
3367 			cpr2->had_nqe_notify = 1;
3368 			cpr2->toggle = NQE_CN_TOGGLE(type);
3369 			work_done += __bnxt_poll_work(bp, cpr2,
3370 						      budget - work_done);
3371 			cpr->has_more_work |= cpr2->has_more_work;
3372 		} else {
3373 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3374 		}
3375 		raw_cons = NEXT_RAW_CMP(raw_cons);
3376 	}
3377 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3378 	if (raw_cons != cpr->cp_raw_cons) {
3379 		cpr->cp_raw_cons = raw_cons;
3380 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3381 	}
3382 poll_done:
3383 	cpr_rx = &cpr->cp_ring_arr[0];
3384 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3385 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3386 		struct dim_sample dim_sample = {};
3387 
3388 		dim_update_sample(cpr->event_ctr,
3389 				  cpr_rx->rx_packets,
3390 				  cpr_rx->rx_bytes,
3391 				  &dim_sample);
3392 		net_dim(&cpr->dim, &dim_sample);
3393 	}
3394 	return work_done;
3395 }
3396 
3397 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3398 				       struct bnxt_tx_ring_info *txr, int idx)
3399 {
3400 	int i, max_idx;
3401 	struct pci_dev *pdev = bp->pdev;
3402 
3403 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3404 
3405 	for (i = 0; i < max_idx;) {
3406 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3407 		struct sk_buff *skb;
3408 		int j, last;
3409 
3410 		if (idx  < bp->tx_nr_rings_xdp &&
3411 		    tx_buf->action == XDP_REDIRECT) {
3412 			dma_unmap_single(&pdev->dev,
3413 					 dma_unmap_addr(tx_buf, mapping),
3414 					 dma_unmap_len(tx_buf, len),
3415 					 DMA_TO_DEVICE);
3416 			xdp_return_frame(tx_buf->xdpf);
3417 			tx_buf->action = 0;
3418 			tx_buf->xdpf = NULL;
3419 			i++;
3420 			continue;
3421 		}
3422 
3423 		skb = tx_buf->skb;
3424 		if (!skb) {
3425 			i++;
3426 			continue;
3427 		}
3428 
3429 		tx_buf->skb = NULL;
3430 
3431 		if (tx_buf->is_push) {
3432 			dev_kfree_skb(skb);
3433 			i += 2;
3434 			continue;
3435 		}
3436 
3437 		dma_unmap_single(&pdev->dev,
3438 				 dma_unmap_addr(tx_buf, mapping),
3439 				 skb_headlen(skb),
3440 				 DMA_TO_DEVICE);
3441 
3442 		last = tx_buf->nr_frags;
3443 		i += 2;
3444 		for (j = 0; j < last; j++, i++) {
3445 			int ring_idx = i & bp->tx_ring_mask;
3446 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3447 
3448 			tx_buf = &txr->tx_buf_ring[ring_idx];
3449 			netmem_dma_unmap_page_attrs(&pdev->dev,
3450 						    dma_unmap_addr(tx_buf,
3451 								   mapping),
3452 						    skb_frag_size(frag),
3453 						    DMA_TO_DEVICE, 0);
3454 		}
3455 		dev_kfree_skb(skb);
3456 	}
3457 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3458 }
3459 
3460 static void bnxt_free_tx_skbs(struct bnxt *bp)
3461 {
3462 	int i;
3463 
3464 	if (!bp->tx_ring)
3465 		return;
3466 
3467 	for (i = 0; i < bp->tx_nr_rings; i++) {
3468 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3469 
3470 		if (!txr->tx_buf_ring)
3471 			continue;
3472 
3473 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3474 	}
3475 
3476 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3477 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3478 }
3479 
3480 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3481 {
3482 	int i, max_idx;
3483 
3484 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3485 
3486 	for (i = 0; i < max_idx; i++) {
3487 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3488 		void *data = rx_buf->data;
3489 
3490 		if (!data)
3491 			continue;
3492 
3493 		rx_buf->data = NULL;
3494 		if (BNXT_RX_PAGE_MODE(bp))
3495 			page_pool_recycle_direct(rxr->page_pool, data);
3496 		else
3497 			page_pool_free_va(rxr->head_pool, data, true);
3498 	}
3499 }
3500 
3501 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3502 {
3503 	int i, max_idx;
3504 
3505 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3506 
3507 	for (i = 0; i < max_idx; i++) {
3508 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3509 		netmem_ref netmem = rx_agg_buf->netmem;
3510 
3511 		if (!netmem)
3512 			continue;
3513 
3514 		rx_agg_buf->netmem = 0;
3515 		__clear_bit(i, rxr->rx_agg_bmap);
3516 
3517 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3518 	}
3519 }
3520 
3521 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3522 					struct bnxt_rx_ring_info *rxr)
3523 {
3524 	int i;
3525 
3526 	for (i = 0; i < bp->max_tpa; i++) {
3527 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3528 		u8 *data = tpa_info->data;
3529 
3530 		if (!data)
3531 			continue;
3532 
3533 		tpa_info->data = NULL;
3534 		page_pool_free_va(rxr->head_pool, data, false);
3535 	}
3536 }
3537 
3538 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3539 				       struct bnxt_rx_ring_info *rxr)
3540 {
3541 	struct bnxt_tpa_idx_map *map;
3542 
3543 	if (!rxr->rx_tpa)
3544 		goto skip_rx_tpa_free;
3545 
3546 	bnxt_free_one_tpa_info_data(bp, rxr);
3547 
3548 skip_rx_tpa_free:
3549 	if (!rxr->rx_buf_ring)
3550 		goto skip_rx_buf_free;
3551 
3552 	bnxt_free_one_rx_ring(bp, rxr);
3553 
3554 skip_rx_buf_free:
3555 	if (!rxr->rx_agg_ring)
3556 		goto skip_rx_agg_free;
3557 
3558 	bnxt_free_one_rx_agg_ring(bp, rxr);
3559 
3560 skip_rx_agg_free:
3561 	map = rxr->rx_tpa_idx_map;
3562 	if (map)
3563 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3564 }
3565 
3566 static void bnxt_free_rx_skbs(struct bnxt *bp)
3567 {
3568 	int i;
3569 
3570 	if (!bp->rx_ring)
3571 		return;
3572 
3573 	for (i = 0; i < bp->rx_nr_rings; i++)
3574 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3575 }
3576 
3577 static void bnxt_free_skbs(struct bnxt *bp)
3578 {
3579 	bnxt_free_tx_skbs(bp);
3580 	bnxt_free_rx_skbs(bp);
3581 }
3582 
3583 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3584 {
3585 	u8 init_val = ctxm->init_value;
3586 	u16 offset = ctxm->init_offset;
3587 	u8 *p2 = p;
3588 	int i;
3589 
3590 	if (!init_val)
3591 		return;
3592 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3593 		memset(p, init_val, len);
3594 		return;
3595 	}
3596 	for (i = 0; i < len; i += ctxm->entry_size)
3597 		*(p2 + i + offset) = init_val;
3598 }
3599 
3600 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3601 			       void *buf, size_t offset, size_t head,
3602 			       size_t tail)
3603 {
3604 	int i, head_page, start_idx, source_offset;
3605 	size_t len, rem_len, total_len, max_bytes;
3606 
3607 	head_page = head / rmem->page_size;
3608 	source_offset = head % rmem->page_size;
3609 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3610 	if (!total_len)
3611 		total_len = MAX_CTX_BYTES;
3612 	start_idx = head_page % MAX_CTX_PAGES;
3613 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3614 		    source_offset;
3615 	total_len = min(total_len, max_bytes);
3616 	rem_len = total_len;
3617 
3618 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3619 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3620 		if (buf)
3621 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3622 			       len);
3623 		offset += len;
3624 		rem_len -= len;
3625 	}
3626 	return total_len;
3627 }
3628 
3629 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3630 {
3631 	struct pci_dev *pdev = bp->pdev;
3632 	int i;
3633 
3634 	if (!rmem->pg_arr)
3635 		goto skip_pages;
3636 
3637 	for (i = 0; i < rmem->nr_pages; i++) {
3638 		if (!rmem->pg_arr[i])
3639 			continue;
3640 
3641 		dma_free_coherent(&pdev->dev, rmem->page_size,
3642 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3643 
3644 		rmem->pg_arr[i] = NULL;
3645 	}
3646 skip_pages:
3647 	if (rmem->pg_tbl) {
3648 		size_t pg_tbl_size = rmem->nr_pages * 8;
3649 
3650 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3651 			pg_tbl_size = rmem->page_size;
3652 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3653 				  rmem->pg_tbl, rmem->pg_tbl_map);
3654 		rmem->pg_tbl = NULL;
3655 	}
3656 	if (rmem->vmem_size && *rmem->vmem) {
3657 		vfree(*rmem->vmem);
3658 		*rmem->vmem = NULL;
3659 	}
3660 }
3661 
3662 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3663 {
3664 	struct pci_dev *pdev = bp->pdev;
3665 	u64 valid_bit = 0;
3666 	int i;
3667 
3668 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3669 		valid_bit = PTU_PTE_VALID;
3670 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3671 		size_t pg_tbl_size = rmem->nr_pages * 8;
3672 
3673 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3674 			pg_tbl_size = rmem->page_size;
3675 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3676 						  &rmem->pg_tbl_map,
3677 						  GFP_KERNEL);
3678 		if (!rmem->pg_tbl)
3679 			return -ENOMEM;
3680 	}
3681 
3682 	for (i = 0; i < rmem->nr_pages; i++) {
3683 		u64 extra_bits = valid_bit;
3684 
3685 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3686 						     rmem->page_size,
3687 						     &rmem->dma_arr[i],
3688 						     GFP_KERNEL);
3689 		if (!rmem->pg_arr[i])
3690 			return -ENOMEM;
3691 
3692 		if (rmem->ctx_mem)
3693 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3694 					  rmem->page_size);
3695 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3696 			if (i == rmem->nr_pages - 2 &&
3697 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3698 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3699 			else if (i == rmem->nr_pages - 1 &&
3700 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3701 				extra_bits |= PTU_PTE_LAST;
3702 			rmem->pg_tbl[i] =
3703 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3704 		}
3705 	}
3706 
3707 	if (rmem->vmem_size) {
3708 		*rmem->vmem = vzalloc(rmem->vmem_size);
3709 		if (!(*rmem->vmem))
3710 			return -ENOMEM;
3711 	}
3712 	return 0;
3713 }
3714 
3715 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3716 				   struct bnxt_rx_ring_info *rxr)
3717 {
3718 	int i;
3719 
3720 	kfree(rxr->rx_tpa_idx_map);
3721 	rxr->rx_tpa_idx_map = NULL;
3722 	if (rxr->rx_tpa) {
3723 		for (i = 0; i < bp->max_tpa; i++) {
3724 			kfree(rxr->rx_tpa[i].agg_arr);
3725 			rxr->rx_tpa[i].agg_arr = NULL;
3726 		}
3727 	}
3728 	kfree(rxr->rx_tpa);
3729 	rxr->rx_tpa = NULL;
3730 }
3731 
3732 static void bnxt_free_tpa_info(struct bnxt *bp)
3733 {
3734 	int i;
3735 
3736 	for (i = 0; i < bp->rx_nr_rings; i++) {
3737 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3738 
3739 		bnxt_free_one_tpa_info(bp, rxr);
3740 	}
3741 }
3742 
3743 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3744 				   struct bnxt_rx_ring_info *rxr)
3745 {
3746 	struct rx_agg_cmp *agg;
3747 	int i;
3748 
3749 	rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info, bp->max_tpa);
3750 	if (!rxr->rx_tpa)
3751 		return -ENOMEM;
3752 
3753 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3754 		return 0;
3755 	for (i = 0; i < bp->max_tpa; i++) {
3756 		agg = kzalloc_objs(*agg, MAX_SKB_FRAGS);
3757 		if (!agg)
3758 			return -ENOMEM;
3759 		rxr->rx_tpa[i].agg_arr = agg;
3760 	}
3761 	rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map);
3762 	if (!rxr->rx_tpa_idx_map)
3763 		return -ENOMEM;
3764 
3765 	return 0;
3766 }
3767 
3768 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3769 {
3770 	int i, rc;
3771 
3772 	bp->max_tpa = MAX_TPA;
3773 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3774 		if (!bp->max_tpa_v2)
3775 			return 0;
3776 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3777 	}
3778 
3779 	for (i = 0; i < bp->rx_nr_rings; i++) {
3780 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3781 
3782 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3783 		if (rc)
3784 			return rc;
3785 	}
3786 	return 0;
3787 }
3788 
3789 static void bnxt_free_rx_rings(struct bnxt *bp)
3790 {
3791 	int i;
3792 
3793 	if (!bp->rx_ring)
3794 		return;
3795 
3796 	bnxt_free_tpa_info(bp);
3797 	for (i = 0; i < bp->rx_nr_rings; i++) {
3798 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3799 		struct bnxt_ring_struct *ring;
3800 
3801 		if (rxr->xdp_prog)
3802 			bpf_prog_put(rxr->xdp_prog);
3803 
3804 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3805 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3806 
3807 		page_pool_destroy(rxr->page_pool);
3808 		page_pool_destroy(rxr->head_pool);
3809 		rxr->page_pool = rxr->head_pool = NULL;
3810 
3811 		kfree(rxr->rx_agg_bmap);
3812 		rxr->rx_agg_bmap = NULL;
3813 
3814 		ring = &rxr->rx_ring_struct;
3815 		bnxt_free_ring(bp, &ring->ring_mem);
3816 
3817 		ring = &rxr->rx_agg_ring_struct;
3818 		bnxt_free_ring(bp, &ring->ring_mem);
3819 	}
3820 }
3821 
3822 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3823 				       struct bnxt_rx_ring_info *rxr)
3824 {
3825 	/* User may have chosen larger than default rx_page_size,
3826 	 * we keep the ring sizes uniform and also want uniform amount
3827 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3828 	 */
3829 	int fill_level = bp->rx_agg_ring_size;
3830 
3831 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3832 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3833 
3834 	return fill_level;
3835 }
3836 
3837 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3838 				   struct bnxt_rx_ring_info *rxr,
3839 				   int numa_node)
3840 {
3841 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3842 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3843 	struct page_pool_params pp = { 0 };
3844 	struct page_pool *pool;
3845 
3846 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3847 	if (BNXT_RX_PAGE_MODE(bp))
3848 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3849 
3850 	pp.order = get_order(rxr->rx_page_size);
3851 	pp.nid = numa_node;
3852 	pp.netdev = bp->dev;
3853 	pp.dev = &bp->pdev->dev;
3854 	pp.dma_dir = bp->rx_dir;
3855 	pp.max_len = PAGE_SIZE << pp.order;
3856 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3857 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3858 	pp.queue_idx = rxr->bnapi->index;
3859 
3860 	pool = page_pool_create(&pp);
3861 	if (IS_ERR(pool))
3862 		return PTR_ERR(pool);
3863 	rxr->page_pool = pool;
3864 
3865 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3866 	rxr->need_head_pool |= !!pp.order;
3867 	if (bnxt_separate_head_pool(rxr)) {
3868 		pp.order = 0;
3869 		pp.max_len = PAGE_SIZE;
3870 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3871 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3872 		pool = page_pool_create(&pp);
3873 		if (IS_ERR(pool))
3874 			goto err_destroy_pp;
3875 	} else {
3876 		page_pool_get(pool);
3877 	}
3878 	rxr->head_pool = pool;
3879 
3880 	return 0;
3881 
3882 err_destroy_pp:
3883 	page_pool_destroy(rxr->page_pool);
3884 	rxr->page_pool = NULL;
3885 	return PTR_ERR(pool);
3886 }
3887 
3888 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3889 {
3890 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3891 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3892 }
3893 
3894 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3895 {
3896 	u16 mem_size;
3897 
3898 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3899 	mem_size = rxr->rx_agg_bmap_size / 8;
3900 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3901 	if (!rxr->rx_agg_bmap)
3902 		return -ENOMEM;
3903 
3904 	return 0;
3905 }
3906 
3907 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3908 {
3909 	int numa_node = dev_to_node(&bp->pdev->dev);
3910 	int i, rc = 0, agg_rings = 0, cpu;
3911 
3912 	if (!bp->rx_ring)
3913 		return -ENOMEM;
3914 
3915 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3916 		agg_rings = 1;
3917 
3918 	for (i = 0; i < bp->rx_nr_rings; i++) {
3919 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3920 		struct bnxt_ring_struct *ring;
3921 		int cpu_node;
3922 
3923 		ring = &rxr->rx_ring_struct;
3924 
3925 		cpu = cpumask_local_spread(i, numa_node);
3926 		cpu_node = cpu_to_node(cpu);
3927 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3928 			   i, cpu_node);
3929 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3930 		if (rc)
3931 			return rc;
3932 		bnxt_enable_rx_page_pool(rxr);
3933 
3934 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3935 		if (rc < 0)
3936 			return rc;
3937 
3938 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3939 						MEM_TYPE_PAGE_POOL,
3940 						rxr->page_pool);
3941 		if (rc) {
3942 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3943 			return rc;
3944 		}
3945 
3946 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3947 		if (rc)
3948 			return rc;
3949 
3950 		ring->grp_idx = i;
3951 		if (agg_rings) {
3952 			ring = &rxr->rx_agg_ring_struct;
3953 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3954 			if (rc)
3955 				return rc;
3956 
3957 			ring->grp_idx = i;
3958 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3959 			if (rc)
3960 				return rc;
3961 		}
3962 	}
3963 	if (bp->flags & BNXT_FLAG_TPA)
3964 		rc = bnxt_alloc_tpa_info(bp);
3965 	return rc;
3966 }
3967 
3968 static void bnxt_free_tx_rings(struct bnxt *bp)
3969 {
3970 	int i;
3971 	struct pci_dev *pdev = bp->pdev;
3972 
3973 	if (!bp->tx_ring)
3974 		return;
3975 
3976 	for (i = 0; i < bp->tx_nr_rings; i++) {
3977 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3978 		struct bnxt_ring_struct *ring;
3979 
3980 		if (txr->tx_push) {
3981 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3982 					  txr->tx_push, txr->tx_push_mapping);
3983 			txr->tx_push = NULL;
3984 		}
3985 
3986 		ring = &txr->tx_ring_struct;
3987 
3988 		bnxt_free_ring(bp, &ring->ring_mem);
3989 	}
3990 }
3991 
3992 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3993 	((tc) * (bp)->tx_nr_rings_per_tc)
3994 
3995 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3996 	((tx) % (bp)->tx_nr_rings_per_tc)
3997 
3998 #define BNXT_RING_TO_TC(bp, tx)		\
3999 	((tx) / (bp)->tx_nr_rings_per_tc)
4000 
4001 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4002 {
4003 	int i, j, rc;
4004 	struct pci_dev *pdev = bp->pdev;
4005 
4006 	bp->tx_push_size = 0;
4007 	if (bp->tx_push_thresh) {
4008 		int push_size;
4009 
4010 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4011 					bp->tx_push_thresh);
4012 
4013 		if (push_size > 256) {
4014 			push_size = 0;
4015 			bp->tx_push_thresh = 0;
4016 		}
4017 
4018 		bp->tx_push_size = push_size;
4019 	}
4020 
4021 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4022 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4023 		struct bnxt_ring_struct *ring;
4024 		u8 qidx;
4025 
4026 		ring = &txr->tx_ring_struct;
4027 
4028 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4029 		if (rc)
4030 			return rc;
4031 
4032 		ring->grp_idx = txr->bnapi->index;
4033 		if (bp->tx_push_size) {
4034 			dma_addr_t mapping;
4035 
4036 			/* One pre-allocated DMA buffer to backup
4037 			 * TX push operation
4038 			 */
4039 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4040 						bp->tx_push_size,
4041 						&txr->tx_push_mapping,
4042 						GFP_KERNEL);
4043 
4044 			if (!txr->tx_push)
4045 				return -ENOMEM;
4046 
4047 			mapping = txr->tx_push_mapping +
4048 				sizeof(struct tx_push_bd);
4049 			txr->data_mapping = cpu_to_le64(mapping);
4050 		}
4051 		qidx = bp->tc_to_qidx[j];
4052 		ring->queue_id = bp->q_info[qidx].queue_id;
4053 		spin_lock_init(&txr->xdp_tx_lock);
4054 		if (i < bp->tx_nr_rings_xdp)
4055 			continue;
4056 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4057 			j++;
4058 	}
4059 	return 0;
4060 }
4061 
4062 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4063 {
4064 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4065 
4066 	kfree(cpr->cp_desc_ring);
4067 	cpr->cp_desc_ring = NULL;
4068 	ring->ring_mem.pg_arr = NULL;
4069 	kfree(cpr->cp_desc_mapping);
4070 	cpr->cp_desc_mapping = NULL;
4071 	ring->ring_mem.dma_arr = NULL;
4072 }
4073 
4074 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4075 {
4076 	cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n);
4077 	if (!cpr->cp_desc_ring)
4078 		return -ENOMEM;
4079 	cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n);
4080 	if (!cpr->cp_desc_mapping)
4081 		return -ENOMEM;
4082 	return 0;
4083 }
4084 
4085 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4086 {
4087 	int i;
4088 
4089 	if (!bp->bnapi)
4090 		return;
4091 	for (i = 0; i < bp->cp_nr_rings; i++) {
4092 		struct bnxt_napi *bnapi = bp->bnapi[i];
4093 
4094 		if (!bnapi)
4095 			continue;
4096 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4097 	}
4098 }
4099 
4100 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4101 {
4102 	int i, n = bp->cp_nr_pages;
4103 
4104 	for (i = 0; i < bp->cp_nr_rings; i++) {
4105 		struct bnxt_napi *bnapi = bp->bnapi[i];
4106 		int rc;
4107 
4108 		if (!bnapi)
4109 			continue;
4110 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4111 		if (rc)
4112 			return rc;
4113 	}
4114 	return 0;
4115 }
4116 
4117 static void bnxt_free_cp_rings(struct bnxt *bp)
4118 {
4119 	int i;
4120 
4121 	if (!bp->bnapi)
4122 		return;
4123 
4124 	for (i = 0; i < bp->cp_nr_rings; i++) {
4125 		struct bnxt_napi *bnapi = bp->bnapi[i];
4126 		struct bnxt_cp_ring_info *cpr;
4127 		struct bnxt_ring_struct *ring;
4128 		int j;
4129 
4130 		if (!bnapi)
4131 			continue;
4132 
4133 		cpr = &bnapi->cp_ring;
4134 		ring = &cpr->cp_ring_struct;
4135 
4136 		bnxt_free_ring(bp, &ring->ring_mem);
4137 
4138 		if (!cpr->cp_ring_arr)
4139 			continue;
4140 
4141 		for (j = 0; j < cpr->cp_ring_count; j++) {
4142 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4143 
4144 			ring = &cpr2->cp_ring_struct;
4145 			bnxt_free_ring(bp, &ring->ring_mem);
4146 			bnxt_free_cp_arrays(cpr2);
4147 		}
4148 		kfree(cpr->cp_ring_arr);
4149 		cpr->cp_ring_arr = NULL;
4150 		cpr->cp_ring_count = 0;
4151 	}
4152 }
4153 
4154 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4155 				  struct bnxt_cp_ring_info *cpr)
4156 {
4157 	struct bnxt_ring_mem_info *rmem;
4158 	struct bnxt_ring_struct *ring;
4159 	int rc;
4160 
4161 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4162 	if (rc) {
4163 		bnxt_free_cp_arrays(cpr);
4164 		return -ENOMEM;
4165 	}
4166 	ring = &cpr->cp_ring_struct;
4167 	rmem = &ring->ring_mem;
4168 	rmem->nr_pages = bp->cp_nr_pages;
4169 	rmem->page_size = HW_CMPD_RING_SIZE;
4170 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4171 	rmem->dma_arr = cpr->cp_desc_mapping;
4172 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4173 	rc = bnxt_alloc_ring(bp, rmem);
4174 	if (rc) {
4175 		bnxt_free_ring(bp, rmem);
4176 		bnxt_free_cp_arrays(cpr);
4177 	}
4178 	return rc;
4179 }
4180 
4181 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4182 {
4183 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4184 	int i, j, rc, ulp_msix;
4185 	int tcs = bp->num_tc;
4186 
4187 	if (!tcs)
4188 		tcs = 1;
4189 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4190 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4191 		struct bnxt_napi *bnapi = bp->bnapi[i];
4192 		struct bnxt_cp_ring_info *cpr, *cpr2;
4193 		struct bnxt_ring_struct *ring;
4194 		int cp_count = 0, k;
4195 		int rx = 0, tx = 0;
4196 
4197 		if (!bnapi)
4198 			continue;
4199 
4200 		cpr = &bnapi->cp_ring;
4201 		cpr->bnapi = bnapi;
4202 		ring = &cpr->cp_ring_struct;
4203 
4204 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4205 		if (rc)
4206 			return rc;
4207 
4208 		ring->map_idx = ulp_msix + i;
4209 
4210 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4211 			continue;
4212 
4213 		if (i < bp->rx_nr_rings) {
4214 			cp_count++;
4215 			rx = 1;
4216 		}
4217 		if (i < bp->tx_nr_rings_xdp) {
4218 			cp_count++;
4219 			tx = 1;
4220 		} else if ((sh && i < bp->tx_nr_rings) ||
4221 			 (!sh && i >= bp->rx_nr_rings)) {
4222 			cp_count += tcs;
4223 			tx = 1;
4224 		}
4225 
4226 		cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count);
4227 		if (!cpr->cp_ring_arr)
4228 			return -ENOMEM;
4229 		cpr->cp_ring_count = cp_count;
4230 
4231 		for (k = 0; k < cp_count; k++) {
4232 			cpr2 = &cpr->cp_ring_arr[k];
4233 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4234 			if (rc)
4235 				return rc;
4236 			cpr2->bnapi = bnapi;
4237 			cpr2->sw_stats = cpr->sw_stats;
4238 			cpr2->cp_idx = k;
4239 			if (!k && rx) {
4240 				bp->rx_ring[i].rx_cpr = cpr2;
4241 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4242 			} else {
4243 				int n, tc = k - rx;
4244 
4245 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4246 				bp->tx_ring[n].tx_cpr = cpr2;
4247 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4248 			}
4249 		}
4250 		if (tx)
4251 			j++;
4252 	}
4253 	return 0;
4254 }
4255 
4256 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4257 				     struct bnxt_rx_ring_info *rxr)
4258 {
4259 	struct bnxt_ring_mem_info *rmem;
4260 	struct bnxt_ring_struct *ring;
4261 
4262 	ring = &rxr->rx_ring_struct;
4263 	rmem = &ring->ring_mem;
4264 	rmem->nr_pages = bp->rx_nr_pages;
4265 	rmem->page_size = HW_RXBD_RING_SIZE;
4266 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4267 	rmem->dma_arr = rxr->rx_desc_mapping;
4268 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4269 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4270 
4271 	ring = &rxr->rx_agg_ring_struct;
4272 	rmem = &ring->ring_mem;
4273 	rmem->nr_pages = bp->rx_agg_nr_pages;
4274 	rmem->page_size = HW_RXBD_RING_SIZE;
4275 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4276 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4277 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4278 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4279 }
4280 
4281 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4282 				      struct bnxt_rx_ring_info *rxr)
4283 {
4284 	struct bnxt_ring_mem_info *rmem;
4285 	struct bnxt_ring_struct *ring;
4286 	int i;
4287 
4288 	rxr->page_pool->p.napi = NULL;
4289 	rxr->page_pool = NULL;
4290 	rxr->head_pool->p.napi = NULL;
4291 	rxr->head_pool = NULL;
4292 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4293 
4294 	ring = &rxr->rx_ring_struct;
4295 	rmem = &ring->ring_mem;
4296 	rmem->pg_tbl = NULL;
4297 	rmem->pg_tbl_map = 0;
4298 	for (i = 0; i < rmem->nr_pages; i++) {
4299 		rmem->pg_arr[i] = NULL;
4300 		rmem->dma_arr[i] = 0;
4301 	}
4302 	*rmem->vmem = NULL;
4303 
4304 	ring = &rxr->rx_agg_ring_struct;
4305 	rmem = &ring->ring_mem;
4306 	rmem->pg_tbl = NULL;
4307 	rmem->pg_tbl_map = 0;
4308 	for (i = 0; i < rmem->nr_pages; i++) {
4309 		rmem->pg_arr[i] = NULL;
4310 		rmem->dma_arr[i] = 0;
4311 	}
4312 	*rmem->vmem = NULL;
4313 }
4314 
4315 static void bnxt_init_ring_struct(struct bnxt *bp)
4316 {
4317 	int i, j;
4318 
4319 	for (i = 0; i < bp->cp_nr_rings; i++) {
4320 		struct bnxt_napi *bnapi = bp->bnapi[i];
4321 		struct netdev_queue_config qcfg;
4322 		struct bnxt_ring_mem_info *rmem;
4323 		struct bnxt_cp_ring_info *cpr;
4324 		struct bnxt_rx_ring_info *rxr;
4325 		struct bnxt_tx_ring_info *txr;
4326 		struct bnxt_ring_struct *ring;
4327 
4328 		if (!bnapi)
4329 			continue;
4330 
4331 		cpr = &bnapi->cp_ring;
4332 		ring = &cpr->cp_ring_struct;
4333 		rmem = &ring->ring_mem;
4334 		rmem->nr_pages = bp->cp_nr_pages;
4335 		rmem->page_size = HW_CMPD_RING_SIZE;
4336 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4337 		rmem->dma_arr = cpr->cp_desc_mapping;
4338 		rmem->vmem_size = 0;
4339 
4340 		rxr = bnapi->rx_ring;
4341 		if (!rxr)
4342 			goto skip_rx;
4343 
4344 		netdev_queue_config(bp->dev, i, &qcfg);
4345 		rxr->rx_page_size = qcfg.rx_page_size;
4346 
4347 		ring = &rxr->rx_ring_struct;
4348 		rmem = &ring->ring_mem;
4349 		rmem->nr_pages = bp->rx_nr_pages;
4350 		rmem->page_size = HW_RXBD_RING_SIZE;
4351 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4352 		rmem->dma_arr = rxr->rx_desc_mapping;
4353 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4354 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4355 
4356 		ring = &rxr->rx_agg_ring_struct;
4357 		rmem = &ring->ring_mem;
4358 		rmem->nr_pages = bp->rx_agg_nr_pages;
4359 		rmem->page_size = HW_RXBD_RING_SIZE;
4360 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4361 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4362 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4363 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4364 
4365 skip_rx:
4366 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4367 			ring = &txr->tx_ring_struct;
4368 			rmem = &ring->ring_mem;
4369 			rmem->nr_pages = bp->tx_nr_pages;
4370 			rmem->page_size = HW_TXBD_RING_SIZE;
4371 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4372 			rmem->dma_arr = txr->tx_desc_mapping;
4373 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4374 			rmem->vmem = (void **)&txr->tx_buf_ring;
4375 		}
4376 	}
4377 }
4378 
4379 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4380 {
4381 	int i;
4382 	u32 prod;
4383 	struct rx_bd **rx_buf_ring;
4384 
4385 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4386 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4387 		int j;
4388 		struct rx_bd *rxbd;
4389 
4390 		rxbd = rx_buf_ring[i];
4391 		if (!rxbd)
4392 			continue;
4393 
4394 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4395 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4396 			rxbd->rx_bd_opaque = prod;
4397 		}
4398 	}
4399 }
4400 
4401 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4402 				       struct bnxt_rx_ring_info *rxr,
4403 				       int ring_nr)
4404 {
4405 	u32 prod;
4406 	int i;
4407 
4408 	prod = rxr->rx_prod;
4409 	for (i = 0; i < bp->rx_ring_size; i++) {
4410 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4411 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4412 				    ring_nr, i, bp->rx_ring_size);
4413 			break;
4414 		}
4415 		prod = NEXT_RX(prod);
4416 	}
4417 	rxr->rx_prod = prod;
4418 }
4419 
4420 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4421 					  struct bnxt_rx_ring_info *rxr,
4422 					  int ring_nr)
4423 {
4424 	int fill_level, i;
4425 	u32 prod;
4426 
4427 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4428 
4429 	prod = rxr->rx_agg_prod;
4430 	for (i = 0; i < fill_level; i++) {
4431 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4432 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4433 				    ring_nr, i, bp->rx_agg_ring_size);
4434 			break;
4435 		}
4436 		prod = NEXT_RX_AGG(prod);
4437 	}
4438 	rxr->rx_agg_prod = prod;
4439 }
4440 
4441 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4442 					struct bnxt_rx_ring_info *rxr)
4443 {
4444 	dma_addr_t mapping;
4445 	u8 *data;
4446 	int i;
4447 
4448 	for (i = 0; i < bp->max_tpa; i++) {
4449 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4450 					    GFP_KERNEL);
4451 		if (!data)
4452 			return -ENOMEM;
4453 
4454 		rxr->rx_tpa[i].data = data;
4455 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4456 		rxr->rx_tpa[i].mapping = mapping;
4457 	}
4458 
4459 	return 0;
4460 }
4461 
4462 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4463 {
4464 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4465 	int rc;
4466 
4467 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4468 
4469 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4470 		return 0;
4471 
4472 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4473 
4474 	if (rxr->rx_tpa) {
4475 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4476 		if (rc)
4477 			return rc;
4478 	}
4479 	return 0;
4480 }
4481 
4482 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4483 				       struct bnxt_rx_ring_info *rxr)
4484 {
4485 	struct bnxt_ring_struct *ring;
4486 	u32 type;
4487 
4488 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4489 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4490 
4491 	if (NET_IP_ALIGN == 2)
4492 		type |= RX_BD_FLAGS_SOP;
4493 
4494 	ring = &rxr->rx_ring_struct;
4495 	bnxt_init_rxbd_pages(ring, type);
4496 	ring->fw_ring_id = INVALID_HW_RING_ID;
4497 }
4498 
4499 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4500 					   struct bnxt_rx_ring_info *rxr)
4501 {
4502 	struct bnxt_ring_struct *ring;
4503 	u32 type;
4504 
4505 	ring = &rxr->rx_agg_ring_struct;
4506 	ring->fw_ring_id = INVALID_HW_RING_ID;
4507 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4508 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4509 			RX_BD_TYPE_RX_AGG_BD;
4510 
4511 		/* On P7, setting EOP will cause the chip to disable
4512 		 * Relaxed Ordering (RO) for TPA data.  Disable EOP for
4513 		 * potentially higher performance with RO.
4514 		 */
4515 		if (BNXT_CHIP_P5_AND_MINUS(bp) || !(bp->flags & BNXT_FLAG_TPA))
4516 			type |= RX_BD_FLAGS_AGG_EOP;
4517 
4518 		bnxt_init_rxbd_pages(ring, type);
4519 	}
4520 }
4521 
4522 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4523 {
4524 	struct bnxt_rx_ring_info *rxr;
4525 
4526 	rxr = &bp->rx_ring[ring_nr];
4527 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4528 
4529 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4530 			     &rxr->bnapi->napi);
4531 
4532 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4533 		bpf_prog_add(bp->xdp_prog, 1);
4534 		rxr->xdp_prog = bp->xdp_prog;
4535 	}
4536 
4537 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4538 
4539 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4540 }
4541 
4542 static void bnxt_init_cp_rings(struct bnxt *bp)
4543 {
4544 	int i, j;
4545 
4546 	for (i = 0; i < bp->cp_nr_rings; i++) {
4547 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4548 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4549 
4550 		ring->fw_ring_id = INVALID_HW_RING_ID;
4551 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4552 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4553 		if (!cpr->cp_ring_arr)
4554 			continue;
4555 		for (j = 0; j < cpr->cp_ring_count; j++) {
4556 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4557 
4558 			ring = &cpr2->cp_ring_struct;
4559 			ring->fw_ring_id = INVALID_HW_RING_ID;
4560 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4561 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4562 		}
4563 	}
4564 }
4565 
4566 static int bnxt_init_rx_rings(struct bnxt *bp)
4567 {
4568 	int i, rc = 0;
4569 
4570 	if (BNXT_RX_PAGE_MODE(bp)) {
4571 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4572 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4573 	} else {
4574 		bp->rx_offset = BNXT_RX_OFFSET;
4575 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4576 	}
4577 
4578 	for (i = 0; i < bp->rx_nr_rings; i++) {
4579 		rc = bnxt_init_one_rx_ring(bp, i);
4580 		if (rc)
4581 			break;
4582 	}
4583 
4584 	return rc;
4585 }
4586 
4587 static int bnxt_init_tx_rings(struct bnxt *bp)
4588 {
4589 	u16 i;
4590 
4591 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4592 				   BNXT_MIN_TX_DESC_CNT);
4593 
4594 	for (i = 0; i < bp->tx_nr_rings; i++) {
4595 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4596 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4597 
4598 		ring->fw_ring_id = INVALID_HW_RING_ID;
4599 
4600 		if (i >= bp->tx_nr_rings_xdp)
4601 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4602 					     NETDEV_QUEUE_TYPE_TX,
4603 					     &txr->bnapi->napi);
4604 	}
4605 
4606 	return 0;
4607 }
4608 
4609 static void bnxt_free_ring_grps(struct bnxt *bp)
4610 {
4611 	kfree(bp->grp_info);
4612 	bp->grp_info = NULL;
4613 }
4614 
4615 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4616 {
4617 	int i;
4618 
4619 	if (irq_re_init) {
4620 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4621 					    bp->cp_nr_rings);
4622 		if (!bp->grp_info)
4623 			return -ENOMEM;
4624 	}
4625 	for (i = 0; i < bp->cp_nr_rings; i++) {
4626 		if (irq_re_init)
4627 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4628 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4629 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4630 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4631 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4632 	}
4633 	return 0;
4634 }
4635 
4636 static void bnxt_free_vnics(struct bnxt *bp)
4637 {
4638 	kfree(bp->vnic_info);
4639 	bp->vnic_info = NULL;
4640 	bp->nr_vnics = 0;
4641 }
4642 
4643 static int bnxt_alloc_vnics(struct bnxt *bp)
4644 {
4645 	int num_vnics = 1;
4646 
4647 #ifdef CONFIG_RFS_ACCEL
4648 	if (bp->flags & BNXT_FLAG_RFS) {
4649 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4650 			num_vnics++;
4651 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4652 			num_vnics += bp->rx_nr_rings;
4653 	}
4654 #endif
4655 
4656 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4657 		num_vnics++;
4658 
4659 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4660 	if (!bp->vnic_info)
4661 		return -ENOMEM;
4662 
4663 	bp->nr_vnics = num_vnics;
4664 	return 0;
4665 }
4666 
4667 static void bnxt_init_vnics(struct bnxt *bp)
4668 {
4669 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4670 	int i;
4671 
4672 	for (i = 0; i < bp->nr_vnics; i++) {
4673 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4674 		int j;
4675 
4676 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4677 		vnic->vnic_id = i;
4678 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4679 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4680 
4681 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4682 
4683 		if (bp->vnic_info[i].rss_hash_key) {
4684 			if (i == BNXT_VNIC_DEFAULT) {
4685 				u8 *key = (void *)vnic->rss_hash_key;
4686 				int k;
4687 
4688 				if (!bp->rss_hash_key_valid &&
4689 				    !bp->rss_hash_key_updated) {
4690 					get_random_bytes(bp->rss_hash_key,
4691 							 HW_HASH_KEY_SIZE);
4692 					bp->rss_hash_key_updated = true;
4693 				}
4694 
4695 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4696 				       HW_HASH_KEY_SIZE);
4697 
4698 				if (!bp->rss_hash_key_updated)
4699 					continue;
4700 
4701 				bp->rss_hash_key_updated = false;
4702 				bp->rss_hash_key_valid = true;
4703 
4704 				bp->toeplitz_prefix = 0;
4705 				for (k = 0; k < 8; k++) {
4706 					bp->toeplitz_prefix <<= 8;
4707 					bp->toeplitz_prefix |= key[k];
4708 				}
4709 			} else {
4710 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4711 				       HW_HASH_KEY_SIZE);
4712 			}
4713 		}
4714 	}
4715 }
4716 
4717 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4718 {
4719 	int pages;
4720 
4721 	pages = ring_size / desc_per_pg;
4722 
4723 	if (!pages)
4724 		return 1;
4725 
4726 	pages++;
4727 
4728 	while (pages & (pages - 1))
4729 		pages++;
4730 
4731 	return pages;
4732 }
4733 
4734 void bnxt_set_tpa_flags(struct bnxt *bp)
4735 {
4736 	bp->flags &= ~BNXT_FLAG_TPA;
4737 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4738 		return;
4739 	if (bp->dev->features & NETIF_F_LRO)
4740 		bp->flags |= BNXT_FLAG_LRO;
4741 	else if (bp->dev->features & NETIF_F_GRO_HW)
4742 		bp->flags |= BNXT_FLAG_GRO;
4743 }
4744 
4745 static void bnxt_init_ring_params(struct bnxt *bp)
4746 {
4747 	unsigned int rx_size;
4748 
4749 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4750 	/* Try to fit 4 chunks into a 4k page */
4751 	rx_size = SZ_1K -
4752 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4753 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4754 }
4755 
4756 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4757  * be set on entry.
4758  */
4759 void bnxt_set_ring_params(struct bnxt *bp)
4760 {
4761 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4762 	u32 agg_factor = 0, agg_ring_size = 0;
4763 
4764 	/* 8 for CRC and VLAN */
4765 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4766 
4767 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4768 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4769 
4770 	ring_size = bp->rx_ring_size;
4771 	bp->rx_agg_ring_size = 0;
4772 	bp->rx_agg_nr_pages = 0;
4773 
4774 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4775 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4776 
4777 	bp->flags &= ~BNXT_FLAG_JUMBO;
4778 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4779 		u32 jumbo_factor;
4780 
4781 		bp->flags |= BNXT_FLAG_JUMBO;
4782 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4783 		if (jumbo_factor > agg_factor)
4784 			agg_factor = jumbo_factor;
4785 	}
4786 	if (agg_factor) {
4787 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4788 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4789 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4790 				    bp->rx_ring_size, ring_size);
4791 			bp->rx_ring_size = ring_size;
4792 		}
4793 		agg_ring_size = ring_size * agg_factor;
4794 
4795 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4796 							RX_DESC_CNT);
4797 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4798 			u32 tmp = agg_ring_size;
4799 
4800 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4801 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4802 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4803 				    tmp, agg_ring_size);
4804 		}
4805 		bp->rx_agg_ring_size = agg_ring_size;
4806 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4807 
4808 		if (BNXT_RX_PAGE_MODE(bp)) {
4809 			rx_space = PAGE_SIZE;
4810 			rx_size = PAGE_SIZE -
4811 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4812 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4813 		} else {
4814 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4815 				       bp->rx_copybreak,
4816 				       bp->dev->cfg_pending->hds_thresh);
4817 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4818 			rx_space = rx_size + NET_SKB_PAD +
4819 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4820 		}
4821 	}
4822 
4823 	bp->rx_buf_use_size = rx_size;
4824 	bp->rx_buf_size = rx_space;
4825 
4826 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4827 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4828 
4829 	ring_size = bp->tx_ring_size;
4830 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4831 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4832 
4833 	max_rx_cmpl = bp->rx_ring_size;
4834 	/* MAX TPA needs to be added because TPA_START completions are
4835 	 * immediately recycled, so the TPA completions are not bound by
4836 	 * the RX ring size.
4837 	 */
4838 	if (bp->flags & BNXT_FLAG_TPA)
4839 		max_rx_cmpl += bp->max_tpa;
4840 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4841 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4842 	bp->cp_ring_size = ring_size;
4843 
4844 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4845 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4846 		bp->cp_nr_pages = MAX_CP_PAGES;
4847 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4848 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4849 			    ring_size, bp->cp_ring_size);
4850 	}
4851 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4852 	bp->cp_ring_mask = bp->cp_bit - 1;
4853 }
4854 
4855 /* Changing allocation mode of RX rings.
4856  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4857  */
4858 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4859 {
4860 	struct net_device *dev = bp->dev;
4861 
4862 	if (page_mode) {
4863 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4864 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4865 
4866 		if (bp->xdp_prog->aux->xdp_has_frags)
4867 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4868 		else
4869 			dev->max_mtu =
4870 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4871 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4872 			bp->flags |= BNXT_FLAG_JUMBO;
4873 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4874 		} else {
4875 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4876 			bp->rx_skb_func = bnxt_rx_page_skb;
4877 		}
4878 		bp->rx_dir = DMA_BIDIRECTIONAL;
4879 	} else {
4880 		dev->max_mtu = bp->max_mtu;
4881 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4882 		bp->rx_dir = DMA_FROM_DEVICE;
4883 		bp->rx_skb_func = bnxt_rx_skb;
4884 	}
4885 }
4886 
4887 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4888 {
4889 	__bnxt_set_rx_skb_mode(bp, page_mode);
4890 
4891 	if (!page_mode) {
4892 		int rx, tx;
4893 
4894 		bnxt_get_max_rings(bp, &rx, &tx, true);
4895 		if (rx > 1) {
4896 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4897 			bp->dev->hw_features |= NETIF_F_LRO;
4898 		}
4899 	}
4900 
4901 	/* Update LRO and GRO_HW availability */
4902 	netdev_update_features(bp->dev);
4903 }
4904 
4905 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4906 {
4907 	int i;
4908 	struct bnxt_vnic_info *vnic;
4909 	struct pci_dev *pdev = bp->pdev;
4910 
4911 	if (!bp->vnic_info)
4912 		return;
4913 
4914 	for (i = 0; i < bp->nr_vnics; i++) {
4915 		vnic = &bp->vnic_info[i];
4916 
4917 		kfree(vnic->fw_grp_ids);
4918 		vnic->fw_grp_ids = NULL;
4919 
4920 		kfree(vnic->uc_list);
4921 		vnic->uc_list = NULL;
4922 
4923 		if (vnic->mc_list) {
4924 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4925 					  vnic->mc_list, vnic->mc_list_mapping);
4926 			vnic->mc_list = NULL;
4927 		}
4928 
4929 		if (vnic->rss_table) {
4930 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4931 					  vnic->rss_table,
4932 					  vnic->rss_table_dma_addr);
4933 			vnic->rss_table = NULL;
4934 		}
4935 
4936 		vnic->rss_hash_key = NULL;
4937 		vnic->flags = 0;
4938 	}
4939 }
4940 
4941 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4942 {
4943 	int i, rc = 0, size;
4944 	struct bnxt_vnic_info *vnic;
4945 	struct pci_dev *pdev = bp->pdev;
4946 	int max_rings;
4947 
4948 	for (i = 0; i < bp->nr_vnics; i++) {
4949 		vnic = &bp->vnic_info[i];
4950 
4951 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4952 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4953 
4954 			if (mem_size > 0) {
4955 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4956 				if (!vnic->uc_list) {
4957 					rc = -ENOMEM;
4958 					goto out;
4959 				}
4960 			}
4961 		}
4962 
4963 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4964 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4965 			vnic->mc_list =
4966 				dma_alloc_coherent(&pdev->dev,
4967 						   vnic->mc_list_size,
4968 						   &vnic->mc_list_mapping,
4969 						   GFP_KERNEL);
4970 			if (!vnic->mc_list) {
4971 				rc = -ENOMEM;
4972 				goto out;
4973 			}
4974 		}
4975 
4976 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4977 			goto vnic_skip_grps;
4978 
4979 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4980 			max_rings = bp->rx_nr_rings;
4981 		else
4982 			max_rings = 1;
4983 
4984 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4985 		if (!vnic->fw_grp_ids) {
4986 			rc = -ENOMEM;
4987 			goto out;
4988 		}
4989 vnic_skip_grps:
4990 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4991 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4992 			continue;
4993 
4994 		/* Allocate rss table and hash key */
4995 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4996 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4997 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4998 
4999 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5000 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5001 						     vnic->rss_table_size,
5002 						     &vnic->rss_table_dma_addr,
5003 						     GFP_KERNEL);
5004 		if (!vnic->rss_table) {
5005 			rc = -ENOMEM;
5006 			goto out;
5007 		}
5008 
5009 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5010 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5011 	}
5012 	return 0;
5013 
5014 out:
5015 	return rc;
5016 }
5017 
5018 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5019 {
5020 	struct bnxt_hwrm_wait_token *token;
5021 
5022 	dma_pool_destroy(bp->hwrm_dma_pool);
5023 	bp->hwrm_dma_pool = NULL;
5024 
5025 	rcu_read_lock();
5026 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5027 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5028 	rcu_read_unlock();
5029 }
5030 
5031 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5032 {
5033 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5034 					    BNXT_HWRM_DMA_SIZE,
5035 					    BNXT_HWRM_DMA_ALIGN, 0);
5036 	if (!bp->hwrm_dma_pool)
5037 		return -ENOMEM;
5038 
5039 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5040 
5041 	return 0;
5042 }
5043 
5044 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5045 {
5046 	kfree(stats->hw_masks);
5047 	stats->hw_masks = NULL;
5048 	kfree(stats->sw_stats);
5049 	stats->sw_stats = NULL;
5050 	if (stats->hw_stats) {
5051 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5052 				  stats->hw_stats_map);
5053 		stats->hw_stats = NULL;
5054 	}
5055 }
5056 
5057 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5058 				bool alloc_masks)
5059 {
5060 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5061 					     &stats->hw_stats_map, GFP_KERNEL);
5062 	if (!stats->hw_stats)
5063 		return -ENOMEM;
5064 
5065 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5066 	if (!stats->sw_stats)
5067 		goto stats_mem_err;
5068 
5069 	if (alloc_masks) {
5070 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5071 		if (!stats->hw_masks)
5072 			goto stats_mem_err;
5073 	}
5074 	return 0;
5075 
5076 stats_mem_err:
5077 	bnxt_free_stats_mem(bp, stats);
5078 	return -ENOMEM;
5079 }
5080 
5081 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5082 {
5083 	int i;
5084 
5085 	for (i = 0; i < count; i++)
5086 		mask_arr[i] = mask;
5087 }
5088 
5089 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5090 {
5091 	int i;
5092 
5093 	for (i = 0; i < count; i++)
5094 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5095 }
5096 
5097 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5098 				    struct bnxt_stats_mem *stats)
5099 {
5100 	struct hwrm_func_qstats_ext_output *resp;
5101 	struct hwrm_func_qstats_ext_input *req;
5102 	__le64 *hw_masks;
5103 	int rc;
5104 
5105 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5106 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5107 		return -EOPNOTSUPP;
5108 
5109 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5110 	if (rc)
5111 		return rc;
5112 
5113 	req->fid = cpu_to_le16(0xffff);
5114 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5115 
5116 	resp = hwrm_req_hold(bp, req);
5117 	rc = hwrm_req_send(bp, req);
5118 	if (!rc) {
5119 		hw_masks = &resp->rx_ucast_pkts;
5120 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5121 	}
5122 	hwrm_req_drop(bp, req);
5123 	return rc;
5124 }
5125 
5126 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5127 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5128 
5129 static void bnxt_init_stats(struct bnxt *bp)
5130 {
5131 	struct bnxt_napi *bnapi = bp->bnapi[0];
5132 	struct bnxt_cp_ring_info *cpr;
5133 	struct bnxt_stats_mem *stats;
5134 	__le64 *rx_stats, *tx_stats;
5135 	int rc, rx_count, tx_count;
5136 	u64 *rx_masks, *tx_masks;
5137 	u64 mask;
5138 	u8 flags;
5139 
5140 	cpr = &bnapi->cp_ring;
5141 	stats = &cpr->stats;
5142 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5143 	if (rc) {
5144 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5145 			mask = (1ULL << 48) - 1;
5146 		else
5147 			mask = -1ULL;
5148 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5149 	}
5150 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5151 		stats = &bp->port_stats;
5152 		rx_stats = stats->hw_stats;
5153 		rx_masks = stats->hw_masks;
5154 		rx_count = sizeof(struct rx_port_stats) / 8;
5155 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5156 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5157 		tx_count = sizeof(struct tx_port_stats) / 8;
5158 
5159 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5160 		rc = bnxt_hwrm_port_qstats(bp, flags);
5161 		if (rc) {
5162 			mask = (1ULL << 40) - 1;
5163 
5164 			bnxt_fill_masks(rx_masks, mask, rx_count);
5165 			bnxt_fill_masks(tx_masks, mask, tx_count);
5166 		} else {
5167 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5168 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5169 			bnxt_hwrm_port_qstats(bp, 0);
5170 		}
5171 	}
5172 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5173 		stats = &bp->rx_port_stats_ext;
5174 		rx_stats = stats->hw_stats;
5175 		rx_masks = stats->hw_masks;
5176 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5177 		stats = &bp->tx_port_stats_ext;
5178 		tx_stats = stats->hw_stats;
5179 		tx_masks = stats->hw_masks;
5180 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5181 
5182 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5183 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5184 		if (rc) {
5185 			mask = (1ULL << 40) - 1;
5186 
5187 			bnxt_fill_masks(rx_masks, mask, rx_count);
5188 			if (tx_stats)
5189 				bnxt_fill_masks(tx_masks, mask, tx_count);
5190 		} else {
5191 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5192 			if (tx_stats)
5193 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5194 						   tx_count);
5195 			bnxt_hwrm_port_qstats_ext(bp, 0);
5196 		}
5197 	}
5198 }
5199 
5200 static void bnxt_free_port_stats(struct bnxt *bp)
5201 {
5202 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5203 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5204 
5205 	bnxt_free_stats_mem(bp, &bp->port_stats);
5206 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5207 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5208 }
5209 
5210 static void bnxt_free_ring_stats(struct bnxt *bp)
5211 {
5212 	int i;
5213 
5214 	if (!bp->bnapi)
5215 		return;
5216 
5217 	for (i = 0; i < bp->cp_nr_rings; i++) {
5218 		struct bnxt_napi *bnapi = bp->bnapi[i];
5219 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5220 
5221 		bnxt_free_stats_mem(bp, &cpr->stats);
5222 
5223 		kfree(cpr->sw_stats);
5224 		cpr->sw_stats = NULL;
5225 	}
5226 }
5227 
5228 static int bnxt_alloc_stats(struct bnxt *bp)
5229 {
5230 	u32 size, i;
5231 	int rc;
5232 
5233 	size = bp->hw_ring_stats_size;
5234 
5235 	for (i = 0; i < bp->cp_nr_rings; i++) {
5236 		struct bnxt_napi *bnapi = bp->bnapi[i];
5237 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5238 
5239 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5240 		if (!cpr->sw_stats)
5241 			return -ENOMEM;
5242 
5243 		cpr->stats.len = size;
5244 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5245 		if (rc)
5246 			return rc;
5247 
5248 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5249 	}
5250 
5251 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5252 		return 0;
5253 
5254 	if (bp->port_stats.hw_stats)
5255 		goto alloc_ext_stats;
5256 
5257 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5258 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5259 	if (rc)
5260 		return rc;
5261 
5262 	bp->flags |= BNXT_FLAG_PORT_STATS;
5263 
5264 alloc_ext_stats:
5265 	/* Display extended statistics only if FW supports it */
5266 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5267 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5268 			return 0;
5269 
5270 	if (bp->rx_port_stats_ext.hw_stats)
5271 		goto alloc_tx_ext_stats;
5272 
5273 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5274 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5275 	/* Extended stats are optional */
5276 	if (rc)
5277 		return 0;
5278 
5279 alloc_tx_ext_stats:
5280 	if (bp->tx_port_stats_ext.hw_stats)
5281 		return 0;
5282 
5283 	if (bp->hwrm_spec_code >= 0x10902 ||
5284 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5285 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5286 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5287 		/* Extended stats are optional */
5288 		if (rc)
5289 			return 0;
5290 	}
5291 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5292 	return 0;
5293 }
5294 
5295 static void bnxt_clear_ring_indices(struct bnxt *bp)
5296 {
5297 	int i, j;
5298 
5299 	if (!bp->bnapi)
5300 		return;
5301 
5302 	for (i = 0; i < bp->cp_nr_rings; i++) {
5303 		struct bnxt_napi *bnapi = bp->bnapi[i];
5304 		struct bnxt_cp_ring_info *cpr;
5305 		struct bnxt_rx_ring_info *rxr;
5306 		struct bnxt_tx_ring_info *txr;
5307 
5308 		if (!bnapi)
5309 			continue;
5310 
5311 		cpr = &bnapi->cp_ring;
5312 		cpr->cp_raw_cons = 0;
5313 
5314 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5315 			txr->tx_prod = 0;
5316 			txr->tx_cons = 0;
5317 			txr->tx_hw_cons = 0;
5318 		}
5319 
5320 		rxr = bnapi->rx_ring;
5321 		if (rxr) {
5322 			rxr->rx_prod = 0;
5323 			rxr->rx_agg_prod = 0;
5324 			rxr->rx_sw_agg_prod = 0;
5325 			rxr->rx_next_cons = 0;
5326 		}
5327 		bnapi->events = 0;
5328 	}
5329 }
5330 
5331 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5332 {
5333 	u8 type = fltr->type, flags = fltr->flags;
5334 
5335 	INIT_LIST_HEAD(&fltr->list);
5336 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5337 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5338 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5339 }
5340 
5341 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5342 {
5343 	if (!list_empty(&fltr->list))
5344 		list_del_init(&fltr->list);
5345 }
5346 
5347 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5348 {
5349 	struct bnxt_filter_base *usr_fltr, *tmp;
5350 
5351 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5352 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5353 			continue;
5354 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5355 	}
5356 }
5357 
5358 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5359 {
5360 	hlist_del(&fltr->hash);
5361 	bnxt_del_one_usr_fltr(bp, fltr);
5362 	if (fltr->flags) {
5363 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5364 		bp->ntp_fltr_count--;
5365 	}
5366 	kfree(fltr);
5367 }
5368 
5369 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5370 {
5371 	int i;
5372 
5373 	netdev_assert_locked_or_invisible(bp->dev);
5374 
5375 	/* Under netdev instance lock and all our NAPIs have been disabled.
5376 	 * It's safe to delete the hash table.
5377 	 */
5378 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5379 		struct hlist_head *head;
5380 		struct hlist_node *tmp;
5381 		struct bnxt_ntuple_filter *fltr;
5382 
5383 		head = &bp->ntp_fltr_hash_tbl[i];
5384 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5385 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5386 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5387 				     !list_empty(&fltr->base.list)))
5388 				continue;
5389 			bnxt_del_fltr(bp, &fltr->base);
5390 		}
5391 	}
5392 	if (!all)
5393 		return;
5394 
5395 	bitmap_free(bp->ntp_fltr_bmap);
5396 	bp->ntp_fltr_bmap = NULL;
5397 	bp->ntp_fltr_count = 0;
5398 }
5399 
5400 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5401 {
5402 	int i, rc = 0;
5403 
5404 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5405 		return 0;
5406 
5407 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5408 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5409 
5410 	bp->ntp_fltr_count = 0;
5411 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5412 
5413 	if (!bp->ntp_fltr_bmap)
5414 		rc = -ENOMEM;
5415 
5416 	return rc;
5417 }
5418 
5419 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5420 {
5421 	int i;
5422 
5423 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5424 		struct hlist_head *head;
5425 		struct hlist_node *tmp;
5426 		struct bnxt_l2_filter *fltr;
5427 
5428 		head = &bp->l2_fltr_hash_tbl[i];
5429 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5430 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5431 				     !list_empty(&fltr->base.list)))
5432 				continue;
5433 			bnxt_del_fltr(bp, &fltr->base);
5434 		}
5435 	}
5436 }
5437 
5438 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5439 {
5440 	int i;
5441 
5442 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5443 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5444 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5445 }
5446 
5447 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5448 {
5449 	bnxt_free_vnic_attributes(bp);
5450 	bnxt_free_tx_rings(bp);
5451 	bnxt_free_rx_rings(bp);
5452 	bnxt_free_cp_rings(bp);
5453 	bnxt_free_all_cp_arrays(bp);
5454 	bnxt_free_ntp_fltrs(bp, false);
5455 	bnxt_free_l2_filters(bp, false);
5456 	if (irq_re_init) {
5457 		bnxt_free_ring_stats(bp);
5458 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5459 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5460 			bnxt_free_port_stats(bp);
5461 		bnxt_free_ring_grps(bp);
5462 		bnxt_free_vnics(bp);
5463 		kfree(bp->tx_ring_map);
5464 		bp->tx_ring_map = NULL;
5465 		kfree(bp->tx_ring);
5466 		bp->tx_ring = NULL;
5467 		kfree(bp->rx_ring);
5468 		bp->rx_ring = NULL;
5469 		kfree(bp->bnapi);
5470 		bp->bnapi = NULL;
5471 	} else {
5472 		bnxt_clear_ring_indices(bp);
5473 	}
5474 }
5475 
5476 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5477 {
5478 	int i, j, rc, size, arr_size;
5479 	void *bnapi;
5480 
5481 	if (irq_re_init) {
5482 		/* Allocate bnapi mem pointer array and mem block for
5483 		 * all queues
5484 		 */
5485 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5486 				bp->cp_nr_rings);
5487 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5488 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5489 		if (!bnapi)
5490 			return -ENOMEM;
5491 
5492 		bp->bnapi = bnapi;
5493 		bnapi += arr_size;
5494 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5495 			bp->bnapi[i] = bnapi;
5496 			bp->bnapi[i]->index = i;
5497 			bp->bnapi[i]->bp = bp;
5498 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5499 				struct bnxt_cp_ring_info *cpr =
5500 					&bp->bnapi[i]->cp_ring;
5501 
5502 				cpr->cp_ring_struct.ring_mem.flags =
5503 					BNXT_RMEM_RING_PTE_FLAG;
5504 			}
5505 		}
5506 
5507 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5508 					   bp->rx_nr_rings);
5509 		if (!bp->rx_ring)
5510 			return -ENOMEM;
5511 
5512 		for (i = 0; i < bp->rx_nr_rings; i++) {
5513 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5514 
5515 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5516 				rxr->rx_ring_struct.ring_mem.flags =
5517 					BNXT_RMEM_RING_PTE_FLAG;
5518 				rxr->rx_agg_ring_struct.ring_mem.flags =
5519 					BNXT_RMEM_RING_PTE_FLAG;
5520 			} else {
5521 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5522 			}
5523 			rxr->bnapi = bp->bnapi[i];
5524 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5525 		}
5526 
5527 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5528 					   bp->tx_nr_rings);
5529 		if (!bp->tx_ring)
5530 			return -ENOMEM;
5531 
5532 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5533 					  GFP_KERNEL);
5534 
5535 		if (!bp->tx_ring_map)
5536 			return -ENOMEM;
5537 
5538 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5539 			j = 0;
5540 		else
5541 			j = bp->rx_nr_rings;
5542 
5543 		for (i = 0; i < bp->tx_nr_rings; i++) {
5544 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5545 			struct bnxt_napi *bnapi2;
5546 
5547 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5548 				txr->tx_ring_struct.ring_mem.flags =
5549 					BNXT_RMEM_RING_PTE_FLAG;
5550 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5551 			if (i >= bp->tx_nr_rings_xdp) {
5552 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5553 
5554 				bnapi2 = bp->bnapi[k];
5555 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5556 				txr->tx_napi_idx =
5557 					BNXT_RING_TO_TC(bp, txr->txq_index);
5558 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5559 				bnapi2->tx_int = bnxt_tx_int;
5560 			} else {
5561 				bnapi2 = bp->bnapi[j];
5562 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5563 				bnapi2->tx_ring[0] = txr;
5564 				bnapi2->tx_int = bnxt_tx_int_xdp;
5565 				j++;
5566 			}
5567 			txr->bnapi = bnapi2;
5568 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5569 				txr->tx_cpr = &bnapi2->cp_ring;
5570 		}
5571 
5572 		rc = bnxt_alloc_stats(bp);
5573 		if (rc)
5574 			goto alloc_mem_err;
5575 		bnxt_init_stats(bp);
5576 
5577 		rc = bnxt_alloc_ntp_fltrs(bp);
5578 		if (rc)
5579 			goto alloc_mem_err;
5580 
5581 		rc = bnxt_alloc_vnics(bp);
5582 		if (rc)
5583 			goto alloc_mem_err;
5584 	}
5585 
5586 	rc = bnxt_alloc_all_cp_arrays(bp);
5587 	if (rc)
5588 		goto alloc_mem_err;
5589 
5590 	bnxt_init_ring_struct(bp);
5591 
5592 	rc = bnxt_alloc_rx_rings(bp);
5593 	if (rc)
5594 		goto alloc_mem_err;
5595 
5596 	rc = bnxt_alloc_tx_rings(bp);
5597 	if (rc)
5598 		goto alloc_mem_err;
5599 
5600 	rc = bnxt_alloc_cp_rings(bp);
5601 	if (rc)
5602 		goto alloc_mem_err;
5603 
5604 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5605 						  BNXT_VNIC_MCAST_FLAG |
5606 						  BNXT_VNIC_UCAST_FLAG;
5607 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5608 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5609 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5610 
5611 	rc = bnxt_alloc_vnic_attributes(bp);
5612 	if (rc)
5613 		goto alloc_mem_err;
5614 	return 0;
5615 
5616 alloc_mem_err:
5617 	bnxt_free_mem(bp, true);
5618 	return rc;
5619 }
5620 
5621 static void bnxt_disable_int(struct bnxt *bp)
5622 {
5623 	int i;
5624 
5625 	if (!bp->bnapi)
5626 		return;
5627 
5628 	for (i = 0; i < bp->cp_nr_rings; i++) {
5629 		struct bnxt_napi *bnapi = bp->bnapi[i];
5630 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5631 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5632 
5633 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5634 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5635 	}
5636 }
5637 
5638 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5639 {
5640 	struct bnxt_napi *bnapi = bp->bnapi[n];
5641 	struct bnxt_cp_ring_info *cpr;
5642 
5643 	cpr = &bnapi->cp_ring;
5644 	return cpr->cp_ring_struct.map_idx;
5645 }
5646 
5647 static void bnxt_disable_int_sync(struct bnxt *bp)
5648 {
5649 	int i;
5650 
5651 	if (!bp->irq_tbl)
5652 		return;
5653 
5654 	atomic_inc(&bp->intr_sem);
5655 
5656 	bnxt_disable_int(bp);
5657 	for (i = 0; i < bp->cp_nr_rings; i++) {
5658 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5659 
5660 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5661 	}
5662 }
5663 
5664 static void bnxt_enable_int(struct bnxt *bp)
5665 {
5666 	int i;
5667 
5668 	atomic_set(&bp->intr_sem, 0);
5669 	for (i = 0; i < bp->cp_nr_rings; i++) {
5670 		struct bnxt_napi *bnapi = bp->bnapi[i];
5671 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5672 
5673 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5674 	}
5675 }
5676 
5677 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5678 			    bool async_only)
5679 {
5680 	DECLARE_BITMAP(async_events_bmap, 256);
5681 	u32 *events = (u32 *)async_events_bmap;
5682 	struct hwrm_func_drv_rgtr_output *resp;
5683 	struct hwrm_func_drv_rgtr_input *req;
5684 	u32 flags;
5685 	int rc, i;
5686 
5687 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5688 	if (rc)
5689 		return rc;
5690 
5691 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5692 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5693 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5694 
5695 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5696 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5697 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5698 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5699 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5700 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5701 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5702 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5703 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5704 	req->flags = cpu_to_le32(flags);
5705 	req->ver_maj_8b = DRV_VER_MAJ;
5706 	req->ver_min_8b = DRV_VER_MIN;
5707 	req->ver_upd_8b = DRV_VER_UPD;
5708 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5709 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5710 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5711 
5712 	if (BNXT_PF(bp)) {
5713 		u32 data[8];
5714 		int i;
5715 
5716 		memset(data, 0, sizeof(data));
5717 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5718 			u16 cmd = bnxt_vf_req_snif[i];
5719 			unsigned int bit, idx;
5720 
5721 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5722 			    cmd == HWRM_PORT_PHY_QCFG)
5723 				continue;
5724 
5725 			idx = cmd / 32;
5726 			bit = cmd % 32;
5727 			data[idx] |= 1 << bit;
5728 		}
5729 
5730 		for (i = 0; i < 8; i++)
5731 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5732 
5733 		req->enables |=
5734 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5735 	}
5736 
5737 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5738 		req->flags |= cpu_to_le32(
5739 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5740 
5741 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5742 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5743 		u16 event_id = bnxt_async_events_arr[i];
5744 
5745 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5746 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5747 			continue;
5748 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5749 		    !bp->ptp_cfg)
5750 			continue;
5751 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5752 	}
5753 	if (bmap && bmap_size) {
5754 		for (i = 0; i < bmap_size; i++) {
5755 			if (test_bit(i, bmap))
5756 				__set_bit(i, async_events_bmap);
5757 		}
5758 	}
5759 	for (i = 0; i < 8; i++)
5760 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5761 
5762 	if (async_only)
5763 		req->enables =
5764 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5765 
5766 	resp = hwrm_req_hold(bp, req);
5767 	rc = hwrm_req_send(bp, req);
5768 	if (!rc) {
5769 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5770 		if (resp->flags &
5771 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5772 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5773 	}
5774 	hwrm_req_drop(bp, req);
5775 	return rc;
5776 }
5777 
5778 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5779 {
5780 	struct hwrm_func_drv_unrgtr_input *req;
5781 	int rc;
5782 
5783 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5784 		return 0;
5785 
5786 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5787 	if (rc)
5788 		return rc;
5789 	return hwrm_req_send(bp, req);
5790 }
5791 
5792 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5793 
5794 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5795 {
5796 	struct hwrm_tunnel_dst_port_free_input *req;
5797 	int rc;
5798 
5799 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5800 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5801 		return 0;
5802 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5803 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5804 		return 0;
5805 
5806 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5807 	if (rc)
5808 		return rc;
5809 
5810 	req->tunnel_type = tunnel_type;
5811 
5812 	switch (tunnel_type) {
5813 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5814 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5815 		bp->vxlan_port = 0;
5816 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5817 		break;
5818 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5819 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5820 		bp->nge_port = 0;
5821 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5822 		break;
5823 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5824 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5825 		bp->vxlan_gpe_port = 0;
5826 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5827 		break;
5828 	default:
5829 		break;
5830 	}
5831 
5832 	rc = hwrm_req_send(bp, req);
5833 	if (rc)
5834 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5835 			   rc);
5836 	if (bp->flags & BNXT_FLAG_TPA)
5837 		bnxt_set_tpa(bp, true);
5838 	return rc;
5839 }
5840 
5841 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5842 					   u8 tunnel_type)
5843 {
5844 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5845 	struct hwrm_tunnel_dst_port_alloc_input *req;
5846 	int rc;
5847 
5848 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5849 	if (rc)
5850 		return rc;
5851 
5852 	req->tunnel_type = tunnel_type;
5853 	req->tunnel_dst_port_val = port;
5854 
5855 	resp = hwrm_req_hold(bp, req);
5856 	rc = hwrm_req_send(bp, req);
5857 	if (rc) {
5858 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5859 			   rc);
5860 		goto err_out;
5861 	}
5862 
5863 	switch (tunnel_type) {
5864 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5865 		bp->vxlan_port = port;
5866 		bp->vxlan_fw_dst_port_id =
5867 			le16_to_cpu(resp->tunnel_dst_port_id);
5868 		break;
5869 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5870 		bp->nge_port = port;
5871 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5872 		break;
5873 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5874 		bp->vxlan_gpe_port = port;
5875 		bp->vxlan_gpe_fw_dst_port_id =
5876 			le16_to_cpu(resp->tunnel_dst_port_id);
5877 		break;
5878 	default:
5879 		break;
5880 	}
5881 	if (bp->flags & BNXT_FLAG_TPA)
5882 		bnxt_set_tpa(bp, true);
5883 
5884 err_out:
5885 	hwrm_req_drop(bp, req);
5886 	return rc;
5887 }
5888 
5889 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5890 {
5891 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5892 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5893 	int rc;
5894 
5895 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5896 	if (rc)
5897 		return rc;
5898 
5899 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5900 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5901 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5902 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5903 	}
5904 	req->mask = cpu_to_le32(vnic->rx_mask);
5905 	return hwrm_req_send_silent(bp, req);
5906 }
5907 
5908 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5909 {
5910 	if (!atomic_dec_and_test(&fltr->refcnt))
5911 		return;
5912 	spin_lock_bh(&bp->ntp_fltr_lock);
5913 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5914 		spin_unlock_bh(&bp->ntp_fltr_lock);
5915 		return;
5916 	}
5917 	hlist_del_rcu(&fltr->base.hash);
5918 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5919 	if (fltr->base.flags) {
5920 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5921 		bp->ntp_fltr_count--;
5922 	}
5923 	spin_unlock_bh(&bp->ntp_fltr_lock);
5924 	kfree_rcu(fltr, base.rcu);
5925 }
5926 
5927 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5928 						      struct bnxt_l2_key *key,
5929 						      u32 idx)
5930 {
5931 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5932 	struct bnxt_l2_filter *fltr;
5933 
5934 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5935 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5936 
5937 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5938 		    l2_key->vlan == key->vlan)
5939 			return fltr;
5940 	}
5941 	return NULL;
5942 }
5943 
5944 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5945 						    struct bnxt_l2_key *key,
5946 						    u32 idx)
5947 {
5948 	struct bnxt_l2_filter *fltr = NULL;
5949 
5950 	rcu_read_lock();
5951 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5952 	if (fltr)
5953 		atomic_inc(&fltr->refcnt);
5954 	rcu_read_unlock();
5955 	return fltr;
5956 }
5957 
5958 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5959 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5960 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5961 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5962 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5963 
5964 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5965 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5966 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5967 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5968 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5969 
5970 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5971 {
5972 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5973 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5974 			return sizeof(fkeys->addrs.v4addrs) +
5975 			       sizeof(fkeys->ports);
5976 
5977 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5978 			return sizeof(fkeys->addrs.v4addrs);
5979 	}
5980 
5981 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5982 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5983 			return sizeof(fkeys->addrs.v6addrs) +
5984 			       sizeof(fkeys->ports);
5985 
5986 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5987 			return sizeof(fkeys->addrs.v6addrs);
5988 	}
5989 
5990 	return 0;
5991 }
5992 
5993 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5994 			 const unsigned char *key)
5995 {
5996 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5997 	struct bnxt_ipv4_tuple tuple4;
5998 	struct bnxt_ipv6_tuple tuple6;
5999 	int i, j, len = 0;
6000 	u8 *four_tuple;
6001 
6002 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6003 	if (!len)
6004 		return 0;
6005 
6006 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6007 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6008 		tuple4.ports = fkeys->ports;
6009 		four_tuple = (unsigned char *)&tuple4;
6010 	} else {
6011 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6012 		tuple6.ports = fkeys->ports;
6013 		four_tuple = (unsigned char *)&tuple6;
6014 	}
6015 
6016 	for (i = 0, j = 8; i < len; i++, j++) {
6017 		u8 byte = four_tuple[i];
6018 		int bit;
6019 
6020 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6021 			if (byte & 0x80)
6022 				hash ^= prefix;
6023 		}
6024 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6025 	}
6026 
6027 	/* The valid part of the hash is in the upper 32 bits. */
6028 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6029 }
6030 
6031 #ifdef CONFIG_RFS_ACCEL
6032 static struct bnxt_l2_filter *
6033 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6034 {
6035 	struct bnxt_l2_filter *fltr;
6036 	u32 idx;
6037 
6038 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6039 	      BNXT_L2_FLTR_HASH_MASK;
6040 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6041 	return fltr;
6042 }
6043 #endif
6044 
6045 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6046 			       struct bnxt_l2_key *key, u32 idx)
6047 {
6048 	struct hlist_head *head;
6049 
6050 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6051 	fltr->l2_key.vlan = key->vlan;
6052 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6053 	if (fltr->base.flags) {
6054 		int bit_id;
6055 
6056 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6057 						 bp->max_fltr, 0);
6058 		if (bit_id < 0)
6059 			return -ENOMEM;
6060 		fltr->base.sw_id = (u16)bit_id;
6061 		bp->ntp_fltr_count++;
6062 	}
6063 	head = &bp->l2_fltr_hash_tbl[idx];
6064 	hlist_add_head_rcu(&fltr->base.hash, head);
6065 	bnxt_insert_usr_fltr(bp, &fltr->base);
6066 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6067 	atomic_set(&fltr->refcnt, 1);
6068 	return 0;
6069 }
6070 
6071 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6072 						   struct bnxt_l2_key *key,
6073 						   gfp_t gfp)
6074 {
6075 	struct bnxt_l2_filter *fltr;
6076 	u32 idx;
6077 	int rc;
6078 
6079 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6080 	      BNXT_L2_FLTR_HASH_MASK;
6081 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6082 	if (fltr)
6083 		return fltr;
6084 
6085 	fltr = kzalloc_obj(*fltr, gfp);
6086 	if (!fltr)
6087 		return ERR_PTR(-ENOMEM);
6088 	spin_lock_bh(&bp->ntp_fltr_lock);
6089 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6090 	spin_unlock_bh(&bp->ntp_fltr_lock);
6091 	if (rc) {
6092 		bnxt_del_l2_filter(bp, fltr);
6093 		fltr = ERR_PTR(rc);
6094 	}
6095 	return fltr;
6096 }
6097 
6098 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6099 						struct bnxt_l2_key *key,
6100 						u16 flags)
6101 {
6102 	struct bnxt_l2_filter *fltr;
6103 	u32 idx;
6104 	int rc;
6105 
6106 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6107 	      BNXT_L2_FLTR_HASH_MASK;
6108 	spin_lock_bh(&bp->ntp_fltr_lock);
6109 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6110 	if (fltr) {
6111 		fltr = ERR_PTR(-EEXIST);
6112 		goto l2_filter_exit;
6113 	}
6114 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6115 	if (!fltr) {
6116 		fltr = ERR_PTR(-ENOMEM);
6117 		goto l2_filter_exit;
6118 	}
6119 	fltr->base.flags = flags;
6120 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6121 	if (rc) {
6122 		spin_unlock_bh(&bp->ntp_fltr_lock);
6123 		bnxt_del_l2_filter(bp, fltr);
6124 		return ERR_PTR(rc);
6125 	}
6126 
6127 l2_filter_exit:
6128 	spin_unlock_bh(&bp->ntp_fltr_lock);
6129 	return fltr;
6130 }
6131 
6132 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6133 {
6134 #ifdef CONFIG_BNXT_SRIOV
6135 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6136 
6137 	return vf->fw_fid;
6138 #else
6139 	return INVALID_HW_RING_ID;
6140 #endif
6141 }
6142 
6143 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6144 {
6145 	struct hwrm_cfa_l2_filter_free_input *req;
6146 	u16 target_id = 0xffff;
6147 	int rc;
6148 
6149 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6150 		struct bnxt_pf_info *pf = &bp->pf;
6151 
6152 		if (fltr->base.vf_idx >= pf->active_vfs)
6153 			return -EINVAL;
6154 
6155 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6156 		if (target_id == INVALID_HW_RING_ID)
6157 			return -EINVAL;
6158 	}
6159 
6160 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6161 	if (rc)
6162 		return rc;
6163 
6164 	req->target_id = cpu_to_le16(target_id);
6165 	req->l2_filter_id = fltr->base.filter_id;
6166 	return hwrm_req_send(bp, req);
6167 }
6168 
6169 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6170 {
6171 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6172 	struct hwrm_cfa_l2_filter_alloc_input *req;
6173 	u16 target_id = 0xffff;
6174 	int rc;
6175 
6176 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6177 		struct bnxt_pf_info *pf = &bp->pf;
6178 
6179 		if (fltr->base.vf_idx >= pf->active_vfs)
6180 			return -EINVAL;
6181 
6182 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6183 	}
6184 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6185 	if (rc)
6186 		return rc;
6187 
6188 	req->target_id = cpu_to_le16(target_id);
6189 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6190 
6191 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6192 		req->flags |=
6193 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6194 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6195 	req->enables =
6196 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6197 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6198 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6199 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6200 	eth_broadcast_addr(req->l2_addr_mask);
6201 
6202 	if (fltr->l2_key.vlan) {
6203 		req->enables |=
6204 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6205 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6206 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6207 		req->num_vlans = 1;
6208 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6209 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6210 	}
6211 
6212 	resp = hwrm_req_hold(bp, req);
6213 	rc = hwrm_req_send(bp, req);
6214 	if (!rc) {
6215 		fltr->base.filter_id = resp->l2_filter_id;
6216 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6217 	}
6218 	hwrm_req_drop(bp, req);
6219 	return rc;
6220 }
6221 
6222 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6223 				     struct bnxt_ntuple_filter *fltr)
6224 {
6225 	struct hwrm_cfa_ntuple_filter_free_input *req;
6226 	int rc;
6227 
6228 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6229 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6230 		return 0;
6231 
6232 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6233 	if (rc)
6234 		return rc;
6235 
6236 	req->ntuple_filter_id = fltr->base.filter_id;
6237 	return hwrm_req_send(bp, req);
6238 }
6239 
6240 #define BNXT_NTP_FLTR_FLAGS					\
6241 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6242 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6243 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6244 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6245 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6246 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6247 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6248 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6249 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6250 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6251 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6252 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6253 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6254 
6255 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6256 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6257 
6258 void bnxt_fill_ipv6_mask(__be32 mask[4])
6259 {
6260 	int i;
6261 
6262 	for (i = 0; i < 4; i++)
6263 		mask[i] = cpu_to_be32(~0);
6264 }
6265 
6266 static void
6267 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6268 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6269 			  struct bnxt_ntuple_filter *fltr)
6270 {
6271 	u16 rxq = fltr->base.rxq;
6272 
6273 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6274 		struct ethtool_rxfh_context *ctx;
6275 		struct bnxt_rss_ctx *rss_ctx;
6276 		struct bnxt_vnic_info *vnic;
6277 
6278 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6279 			      fltr->base.fw_vnic_id);
6280 		if (ctx) {
6281 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6282 			vnic = &rss_ctx->vnic;
6283 
6284 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6285 		}
6286 		return;
6287 	}
6288 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6289 		struct bnxt_vnic_info *vnic;
6290 		u32 enables;
6291 
6292 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6293 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6294 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6295 		req->enables |= cpu_to_le32(enables);
6296 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6297 	} else {
6298 		u32 flags;
6299 
6300 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6301 		req->flags |= cpu_to_le32(flags);
6302 		req->dst_id = cpu_to_le16(rxq);
6303 	}
6304 }
6305 
6306 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6307 				      struct bnxt_ntuple_filter *fltr)
6308 {
6309 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6310 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6311 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6312 	struct flow_keys *keys = &fltr->fkeys;
6313 	struct bnxt_l2_filter *l2_fltr;
6314 	struct bnxt_vnic_info *vnic;
6315 	int rc;
6316 
6317 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6318 	if (rc)
6319 		return rc;
6320 
6321 	l2_fltr = fltr->l2_fltr;
6322 	req->l2_filter_id = l2_fltr->base.filter_id;
6323 
6324 	if (fltr->base.flags & BNXT_ACT_DROP) {
6325 		req->flags =
6326 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6327 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6328 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6329 	} else {
6330 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6331 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6332 	}
6333 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6334 
6335 	req->ethertype = htons(ETH_P_IP);
6336 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6337 	req->ip_protocol = keys->basic.ip_proto;
6338 
6339 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6340 		req->ethertype = htons(ETH_P_IPV6);
6341 		req->ip_addr_type =
6342 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6343 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6344 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6345 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6346 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6347 	} else {
6348 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6349 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6350 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6351 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6352 	}
6353 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6354 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6355 		req->tunnel_type =
6356 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6357 	}
6358 
6359 	req->src_port = keys->ports.src;
6360 	req->src_port_mask = masks->ports.src;
6361 	req->dst_port = keys->ports.dst;
6362 	req->dst_port_mask = masks->ports.dst;
6363 
6364 	resp = hwrm_req_hold(bp, req);
6365 	rc = hwrm_req_send(bp, req);
6366 	if (!rc)
6367 		fltr->base.filter_id = resp->ntuple_filter_id;
6368 	hwrm_req_drop(bp, req);
6369 	return rc;
6370 }
6371 
6372 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6373 				     const u8 *mac_addr)
6374 {
6375 	struct bnxt_l2_filter *fltr;
6376 	struct bnxt_l2_key key;
6377 	int rc;
6378 
6379 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6380 	key.vlan = 0;
6381 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6382 	if (IS_ERR(fltr))
6383 		return PTR_ERR(fltr);
6384 
6385 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6386 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6387 	if (rc)
6388 		bnxt_del_l2_filter(bp, fltr);
6389 	else
6390 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6391 	return rc;
6392 }
6393 
6394 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6395 {
6396 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6397 
6398 	/* Any associated ntuple filters will also be cleared by firmware. */
6399 	for (i = 0; i < num_of_vnics; i++) {
6400 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6401 
6402 		for (j = 0; j < vnic->uc_filter_count; j++) {
6403 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6404 
6405 			bnxt_hwrm_l2_filter_free(bp, fltr);
6406 			bnxt_del_l2_filter(bp, fltr);
6407 		}
6408 		vnic->uc_filter_count = 0;
6409 	}
6410 }
6411 
6412 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6413 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6414 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6415 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6416 
6417 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6418 					   struct hwrm_vnic_tpa_cfg_input *req)
6419 {
6420 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6421 
6422 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6423 		return;
6424 
6425 	if (bp->vxlan_port)
6426 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6427 	if (bp->vxlan_gpe_port)
6428 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6429 	if (bp->nge_port)
6430 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6431 
6432 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6433 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6434 }
6435 
6436 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6437 			   u32 tpa_flags)
6438 {
6439 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6440 	struct hwrm_vnic_tpa_cfg_input *req;
6441 	int rc;
6442 
6443 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6444 		return 0;
6445 
6446 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6447 	if (rc)
6448 		return rc;
6449 
6450 	if (tpa_flags) {
6451 		u16 mss = bp->dev->mtu - 40;
6452 		u32 nsegs, n, segs = 0, flags;
6453 
6454 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6455 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6456 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6457 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6458 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6459 		if (tpa_flags & BNXT_FLAG_GRO)
6460 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6461 
6462 		req->flags = cpu_to_le32(flags);
6463 
6464 		req->enables =
6465 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6466 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6467 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6468 
6469 		/* Number of segs are log2 units, and first packet is not
6470 		 * included as part of this units.
6471 		 */
6472 		if (mss <= BNXT_RX_PAGE_SIZE) {
6473 			n = BNXT_RX_PAGE_SIZE / mss;
6474 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6475 		} else {
6476 			n = mss / BNXT_RX_PAGE_SIZE;
6477 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6478 				n++;
6479 			nsegs = (MAX_SKB_FRAGS - n) / n;
6480 		}
6481 
6482 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6483 			segs = MAX_TPA_SEGS_P5;
6484 			max_aggs = bp->max_tpa;
6485 		} else {
6486 			segs = ilog2(nsegs);
6487 		}
6488 		req->max_agg_segs = cpu_to_le16(segs);
6489 		req->max_aggs = cpu_to_le16(max_aggs);
6490 
6491 		req->min_agg_len = cpu_to_le32(512);
6492 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6493 	}
6494 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6495 
6496 	return hwrm_req_send(bp, req);
6497 }
6498 
6499 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6500 {
6501 	struct bnxt_ring_grp_info *grp_info;
6502 
6503 	grp_info = &bp->grp_info[ring->grp_idx];
6504 	return grp_info->cp_fw_ring_id;
6505 }
6506 
6507 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6508 {
6509 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6510 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6511 	else
6512 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6513 }
6514 
6515 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6516 {
6517 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6518 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6519 	else
6520 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6521 }
6522 
6523 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6524 {
6525 	int entries;
6526 
6527 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6528 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6529 	else
6530 		entries = HW_HASH_INDEX_SIZE;
6531 
6532 	bp->rss_indir_tbl_entries = entries;
6533 	bp->rss_indir_tbl =
6534 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6535 	if (!bp->rss_indir_tbl)
6536 		return -ENOMEM;
6537 
6538 	return 0;
6539 }
6540 
6541 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6542 				 struct ethtool_rxfh_context *rss_ctx)
6543 {
6544 	u16 max_rings, max_entries, pad, i;
6545 	u32 *rss_indir_tbl;
6546 
6547 	if (!bp->rx_nr_rings)
6548 		return;
6549 
6550 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6551 		max_rings = bp->rx_nr_rings - 1;
6552 	else
6553 		max_rings = bp->rx_nr_rings;
6554 
6555 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6556 	if (rss_ctx)
6557 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6558 	else
6559 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6560 
6561 	for (i = 0; i < max_entries; i++)
6562 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6563 
6564 	pad = bp->rss_indir_tbl_entries - max_entries;
6565 	if (pad)
6566 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6567 }
6568 
6569 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6570 {
6571 	u32 i, tbl_size, max_ring = 0;
6572 
6573 	if (!bp->rss_indir_tbl)
6574 		return 0;
6575 
6576 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6577 	for (i = 0; i < tbl_size; i++)
6578 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6579 	return max_ring;
6580 }
6581 
6582 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6583 {
6584 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6585 		if (!rx_rings)
6586 			return 0;
6587 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6588 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6589 
6590 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6591 					       BNXT_RSS_TABLE_ENTRIES_P5);
6592 	}
6593 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6594 		return 2;
6595 	return 1;
6596 }
6597 
6598 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6599 {
6600 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6601 	u16 i, j;
6602 
6603 	/* Fill the RSS indirection table with ring group ids */
6604 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6605 		if (!no_rss)
6606 			j = bp->rss_indir_tbl[i];
6607 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6608 	}
6609 }
6610 
6611 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6612 				    struct bnxt_vnic_info *vnic)
6613 {
6614 	__le16 *ring_tbl = vnic->rss_table;
6615 	struct bnxt_rx_ring_info *rxr;
6616 	u16 tbl_size, i;
6617 
6618 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6619 
6620 	for (i = 0; i < tbl_size; i++) {
6621 		u16 ring_id, j;
6622 
6623 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6624 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6625 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6626 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6627 		else
6628 			j = bp->rss_indir_tbl[i];
6629 		rxr = &bp->rx_ring[j];
6630 
6631 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6632 		*ring_tbl++ = cpu_to_le16(ring_id);
6633 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6634 		*ring_tbl++ = cpu_to_le16(ring_id);
6635 	}
6636 }
6637 
6638 static void
6639 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6640 			 struct bnxt_vnic_info *vnic)
6641 {
6642 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6643 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6644 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6645 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6646 	} else {
6647 		bnxt_fill_hw_rss_tbl(bp, vnic);
6648 	}
6649 
6650 	if (bp->rss_hash_delta) {
6651 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6652 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6653 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6654 		else
6655 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6656 	} else {
6657 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6658 	}
6659 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6660 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6661 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6662 }
6663 
6664 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6665 				  bool set_rss)
6666 {
6667 	struct hwrm_vnic_rss_cfg_input *req;
6668 	int rc;
6669 
6670 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6671 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6672 		return 0;
6673 
6674 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6675 	if (rc)
6676 		return rc;
6677 
6678 	if (set_rss)
6679 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6680 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6681 	return hwrm_req_send(bp, req);
6682 }
6683 
6684 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6685 				     struct bnxt_vnic_info *vnic, bool set_rss)
6686 {
6687 	struct hwrm_vnic_rss_cfg_input *req;
6688 	dma_addr_t ring_tbl_map;
6689 	u32 i, nr_ctxs;
6690 	int rc;
6691 
6692 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6693 	if (rc)
6694 		return rc;
6695 
6696 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6697 	if (!set_rss)
6698 		return hwrm_req_send(bp, req);
6699 
6700 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6701 	ring_tbl_map = vnic->rss_table_dma_addr;
6702 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6703 
6704 	hwrm_req_hold(bp, req);
6705 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6706 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6707 		req->ring_table_pair_index = i;
6708 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6709 		rc = hwrm_req_send(bp, req);
6710 		if (rc)
6711 			goto exit;
6712 	}
6713 
6714 exit:
6715 	hwrm_req_drop(bp, req);
6716 	return rc;
6717 }
6718 
6719 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6720 {
6721 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6722 	struct hwrm_vnic_rss_qcfg_output *resp;
6723 	struct hwrm_vnic_rss_qcfg_input *req;
6724 
6725 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6726 		return;
6727 
6728 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6729 	/* all contexts configured to same hash_type, zero always exists */
6730 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6731 	resp = hwrm_req_hold(bp, req);
6732 	if (!hwrm_req_send(bp, req)) {
6733 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6734 		bp->rss_hash_delta = 0;
6735 	}
6736 	hwrm_req_drop(bp, req);
6737 }
6738 
6739 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6740 {
6741 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6742 	struct hwrm_vnic_plcmodes_cfg_input *req;
6743 	int rc;
6744 
6745 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6746 	if (rc)
6747 		return rc;
6748 
6749 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6750 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6751 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6752 
6753 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6754 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6755 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6756 		req->enables |=
6757 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6758 		req->hds_threshold = cpu_to_le16(hds_thresh);
6759 	}
6760 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6761 	return hwrm_req_send(bp, req);
6762 }
6763 
6764 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6765 					struct bnxt_vnic_info *vnic,
6766 					u16 ctx_idx)
6767 {
6768 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6769 
6770 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6771 		return;
6772 
6773 	req->rss_cos_lb_ctx_id =
6774 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6775 
6776 	hwrm_req_send(bp, req);
6777 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6778 }
6779 
6780 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6781 {
6782 	int i, j;
6783 
6784 	for (i = 0; i < bp->nr_vnics; i++) {
6785 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6786 
6787 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6788 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6789 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6790 		}
6791 	}
6792 	bp->rsscos_nr_ctxs = 0;
6793 }
6794 
6795 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6796 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6797 {
6798 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6799 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6800 	int rc;
6801 
6802 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6803 	if (rc)
6804 		return rc;
6805 
6806 	resp = hwrm_req_hold(bp, req);
6807 	rc = hwrm_req_send(bp, req);
6808 	if (!rc)
6809 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6810 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6811 	hwrm_req_drop(bp, req);
6812 
6813 	return rc;
6814 }
6815 
6816 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6817 {
6818 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6819 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6820 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6821 }
6822 
6823 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6824 {
6825 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6826 	struct hwrm_vnic_cfg_input *req;
6827 	unsigned int ring = 0, grp_idx;
6828 	u16 def_vlan = 0;
6829 	int rc;
6830 
6831 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6832 	if (rc)
6833 		return rc;
6834 
6835 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6836 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6837 
6838 		req->default_rx_ring_id =
6839 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6840 		req->default_cmpl_ring_id =
6841 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6842 		req->enables =
6843 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6844 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6845 		goto vnic_mru;
6846 	}
6847 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6848 	/* Only RSS support for now TBD: COS & LB */
6849 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6850 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6851 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6852 					   VNIC_CFG_REQ_ENABLES_MRU);
6853 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6854 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6855 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6856 					   VNIC_CFG_REQ_ENABLES_MRU);
6857 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6858 	} else {
6859 		req->rss_rule = cpu_to_le16(0xffff);
6860 	}
6861 
6862 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6863 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6864 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6865 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6866 	} else {
6867 		req->cos_rule = cpu_to_le16(0xffff);
6868 	}
6869 
6870 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6871 		ring = 0;
6872 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6873 		ring = vnic->vnic_id - 1;
6874 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6875 		ring = bp->rx_nr_rings - 1;
6876 
6877 	grp_idx = bp->rx_ring[ring].bnapi->index;
6878 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6879 	req->lb_rule = cpu_to_le16(0xffff);
6880 vnic_mru:
6881 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
6882 	req->mru = cpu_to_le16(vnic->mru);
6883 
6884 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6885 #ifdef CONFIG_BNXT_SRIOV
6886 	if (BNXT_VF(bp))
6887 		def_vlan = bp->vf.vlan;
6888 #endif
6889 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6890 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6891 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6892 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6893 
6894 	return hwrm_req_send(bp, req);
6895 }
6896 
6897 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6898 				    struct bnxt_vnic_info *vnic)
6899 {
6900 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6901 		struct hwrm_vnic_free_input *req;
6902 
6903 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6904 			return;
6905 
6906 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6907 
6908 		hwrm_req_send(bp, req);
6909 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6910 	}
6911 }
6912 
6913 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6914 {
6915 	u16 i;
6916 
6917 	for (i = 0; i < bp->nr_vnics; i++)
6918 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6919 }
6920 
6921 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6922 			 unsigned int start_rx_ring_idx,
6923 			 unsigned int nr_rings)
6924 {
6925 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6926 	struct hwrm_vnic_alloc_output *resp;
6927 	struct hwrm_vnic_alloc_input *req;
6928 	int rc;
6929 
6930 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6931 	if (rc)
6932 		return rc;
6933 
6934 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6935 		goto vnic_no_ring_grps;
6936 
6937 	/* map ring groups to this vnic */
6938 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6939 		grp_idx = bp->rx_ring[i].bnapi->index;
6940 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6941 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6942 				   j, nr_rings);
6943 			break;
6944 		}
6945 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6946 	}
6947 
6948 vnic_no_ring_grps:
6949 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6950 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6951 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6952 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6953 
6954 	resp = hwrm_req_hold(bp, req);
6955 	rc = hwrm_req_send(bp, req);
6956 	if (!rc)
6957 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6958 	hwrm_req_drop(bp, req);
6959 	return rc;
6960 }
6961 
6962 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6963 {
6964 	struct hwrm_vnic_qcaps_output *resp;
6965 	struct hwrm_vnic_qcaps_input *req;
6966 	int rc;
6967 
6968 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6969 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6970 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6971 	if (bp->hwrm_spec_code < 0x10600)
6972 		return 0;
6973 
6974 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6975 	if (rc)
6976 		return rc;
6977 
6978 	resp = hwrm_req_hold(bp, req);
6979 	rc = hwrm_req_send(bp, req);
6980 	if (!rc) {
6981 		u32 flags = le32_to_cpu(resp->flags);
6982 
6983 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6984 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6985 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6986 		if (flags &
6987 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6988 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6989 
6990 		/* Older P5 fw before EXT_HW_STATS support did not set
6991 		 * VLAN_STRIP_CAP properly.
6992 		 */
6993 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6994 		    (BNXT_CHIP_P5(bp) &&
6995 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6996 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6997 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6998 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6999 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7000 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7001 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7002 		if (bp->max_tpa_v2) {
7003 			if (BNXT_CHIP_P5(bp))
7004 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7005 			else
7006 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7007 		}
7008 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7009 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7010 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7011 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7012 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7013 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7014 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7015 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7016 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7017 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7018 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7019 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7020 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7021 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7022 	}
7023 	hwrm_req_drop(bp, req);
7024 	return rc;
7025 }
7026 
7027 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7028 {
7029 	struct hwrm_ring_grp_alloc_output *resp;
7030 	struct hwrm_ring_grp_alloc_input *req;
7031 	int rc;
7032 	u16 i;
7033 
7034 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7035 		return 0;
7036 
7037 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7038 	if (rc)
7039 		return rc;
7040 
7041 	resp = hwrm_req_hold(bp, req);
7042 	for (i = 0; i < bp->rx_nr_rings; i++) {
7043 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7044 
7045 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7046 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7047 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7048 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7049 
7050 		rc = hwrm_req_send(bp, req);
7051 
7052 		if (rc)
7053 			break;
7054 
7055 		bp->grp_info[grp_idx].fw_grp_id =
7056 			le32_to_cpu(resp->ring_group_id);
7057 	}
7058 	hwrm_req_drop(bp, req);
7059 	return rc;
7060 }
7061 
7062 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7063 {
7064 	struct hwrm_ring_grp_free_input *req;
7065 	u16 i;
7066 
7067 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7068 		return;
7069 
7070 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7071 		return;
7072 
7073 	hwrm_req_hold(bp, req);
7074 	for (i = 0; i < bp->cp_nr_rings; i++) {
7075 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7076 			continue;
7077 		req->ring_group_id =
7078 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7079 
7080 		hwrm_req_send(bp, req);
7081 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7082 	}
7083 	hwrm_req_drop(bp, req);
7084 }
7085 
7086 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7087 				       struct hwrm_ring_alloc_input *req,
7088 				       struct bnxt_rx_ring_info *rxr,
7089 				       struct bnxt_ring_struct *ring)
7090 {
7091 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7092 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7093 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7094 
7095 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7096 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7097 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7098 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7099 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7100 	} else {
7101 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7102 		if (NET_IP_ALIGN == 2)
7103 			req->flags =
7104 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7105 	}
7106 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7107 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7108 	req->enables |= cpu_to_le32(enables);
7109 }
7110 
7111 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7112 				    struct bnxt_rx_ring_info *rxr,
7113 				    struct bnxt_ring_struct *ring,
7114 				    u32 ring_type, u32 map_index)
7115 {
7116 	struct hwrm_ring_alloc_output *resp;
7117 	struct hwrm_ring_alloc_input *req;
7118 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7119 	struct bnxt_ring_grp_info *grp_info;
7120 	int rc, err = 0;
7121 	u16 ring_id;
7122 
7123 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7124 	if (rc)
7125 		goto exit;
7126 
7127 	req->enables = 0;
7128 	if (rmem->nr_pages > 1) {
7129 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7130 		/* Page size is in log2 units */
7131 		req->page_size = BNXT_PAGE_SHIFT;
7132 		req->page_tbl_depth = 1;
7133 	} else {
7134 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7135 	}
7136 	req->fbo = 0;
7137 	/* Association of ring index with doorbell index and MSIX number */
7138 	req->logical_id = cpu_to_le16(map_index);
7139 
7140 	switch (ring_type) {
7141 	case HWRM_RING_ALLOC_TX: {
7142 		struct bnxt_tx_ring_info *txr;
7143 		u16 flags = 0;
7144 
7145 		txr = container_of(ring, struct bnxt_tx_ring_info,
7146 				   tx_ring_struct);
7147 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7148 		/* Association of transmit ring with completion ring */
7149 		grp_info = &bp->grp_info[ring->grp_idx];
7150 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7151 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7152 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7153 		req->queue_id = cpu_to_le16(ring->queue_id);
7154 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7155 			req->cmpl_coal_cnt =
7156 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7157 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7158 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7159 		req->flags = cpu_to_le16(flags);
7160 		break;
7161 	}
7162 	case HWRM_RING_ALLOC_RX:
7163 	case HWRM_RING_ALLOC_AGG:
7164 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7165 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7166 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7167 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7168 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7169 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7170 						   rxr, ring);
7171 		break;
7172 	case HWRM_RING_ALLOC_CMPL:
7173 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7174 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7175 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7176 			/* Association of cp ring with nq */
7177 			grp_info = &bp->grp_info[map_index];
7178 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7179 			req->cq_handle = cpu_to_le64(ring->handle);
7180 			req->enables |= cpu_to_le32(
7181 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7182 		} else {
7183 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7184 		}
7185 		break;
7186 	case HWRM_RING_ALLOC_NQ:
7187 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7188 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7189 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7190 		break;
7191 	default:
7192 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7193 			   ring_type);
7194 		return -EINVAL;
7195 	}
7196 
7197 	resp = hwrm_req_hold(bp, req);
7198 	rc = hwrm_req_send(bp, req);
7199 	err = le16_to_cpu(resp->error_code);
7200 	ring_id = le16_to_cpu(resp->ring_id);
7201 	hwrm_req_drop(bp, req);
7202 
7203 exit:
7204 	if (rc || err) {
7205 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7206 			   ring_type, rc, err);
7207 		return -EIO;
7208 	}
7209 	ring->fw_ring_id = ring_id;
7210 	return rc;
7211 }
7212 
7213 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7214 {
7215 	int rc;
7216 
7217 	if (BNXT_PF(bp)) {
7218 		struct hwrm_func_cfg_input *req;
7219 
7220 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7221 		if (rc)
7222 			return rc;
7223 
7224 		req->fid = cpu_to_le16(0xffff);
7225 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7226 		req->async_event_cr = cpu_to_le16(idx);
7227 		return hwrm_req_send(bp, req);
7228 	} else {
7229 		struct hwrm_func_vf_cfg_input *req;
7230 
7231 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7232 		if (rc)
7233 			return rc;
7234 
7235 		req->enables =
7236 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7237 		req->async_event_cr = cpu_to_le16(idx);
7238 		return hwrm_req_send(bp, req);
7239 	}
7240 }
7241 
7242 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7243 			     u32 ring_type)
7244 {
7245 	switch (ring_type) {
7246 	case HWRM_RING_ALLOC_TX:
7247 		db->db_ring_mask = bp->tx_ring_mask;
7248 		break;
7249 	case HWRM_RING_ALLOC_RX:
7250 		db->db_ring_mask = bp->rx_ring_mask;
7251 		break;
7252 	case HWRM_RING_ALLOC_AGG:
7253 		db->db_ring_mask = bp->rx_agg_ring_mask;
7254 		break;
7255 	case HWRM_RING_ALLOC_CMPL:
7256 	case HWRM_RING_ALLOC_NQ:
7257 		db->db_ring_mask = bp->cp_ring_mask;
7258 		break;
7259 	}
7260 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7261 		db->db_epoch_mask = db->db_ring_mask + 1;
7262 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7263 	}
7264 }
7265 
7266 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7267 			u32 map_idx, u32 xid)
7268 {
7269 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7270 		switch (ring_type) {
7271 		case HWRM_RING_ALLOC_TX:
7272 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7273 			break;
7274 		case HWRM_RING_ALLOC_RX:
7275 		case HWRM_RING_ALLOC_AGG:
7276 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7277 			break;
7278 		case HWRM_RING_ALLOC_CMPL:
7279 			db->db_key64 = DBR_PATH_L2;
7280 			break;
7281 		case HWRM_RING_ALLOC_NQ:
7282 			db->db_key64 = DBR_PATH_L2;
7283 			break;
7284 		}
7285 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7286 
7287 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7288 			db->db_key64 |= DBR_VALID;
7289 
7290 		db->doorbell = bp->bar1 + bp->db_offset;
7291 	} else {
7292 		db->doorbell = bp->bar1 + map_idx * 0x80;
7293 		switch (ring_type) {
7294 		case HWRM_RING_ALLOC_TX:
7295 			db->db_key32 = DB_KEY_TX;
7296 			break;
7297 		case HWRM_RING_ALLOC_RX:
7298 		case HWRM_RING_ALLOC_AGG:
7299 			db->db_key32 = DB_KEY_RX;
7300 			break;
7301 		case HWRM_RING_ALLOC_CMPL:
7302 			db->db_key32 = DB_KEY_CP;
7303 			break;
7304 		}
7305 	}
7306 	bnxt_set_db_mask(bp, db, ring_type);
7307 }
7308 
7309 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7310 				   struct bnxt_rx_ring_info *rxr)
7311 {
7312 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7313 	struct bnxt_napi *bnapi = rxr->bnapi;
7314 	u32 type = HWRM_RING_ALLOC_RX;
7315 	u32 map_idx = bnapi->index;
7316 	int rc;
7317 
7318 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7319 	if (rc)
7320 		return rc;
7321 
7322 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7323 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7324 
7325 	return 0;
7326 }
7327 
7328 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7329 				       struct bnxt_rx_ring_info *rxr)
7330 {
7331 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7332 	u32 type = HWRM_RING_ALLOC_AGG;
7333 	u32 grp_idx = ring->grp_idx;
7334 	u32 map_idx;
7335 	int rc;
7336 
7337 	map_idx = grp_idx + bp->rx_nr_rings;
7338 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7339 	if (rc)
7340 		return rc;
7341 
7342 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7343 		    ring->fw_ring_id);
7344 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7345 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7346 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7347 
7348 	return 0;
7349 }
7350 
7351 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7352 				      struct bnxt_cp_ring_info *cpr)
7353 {
7354 	const u32 type = HWRM_RING_ALLOC_CMPL;
7355 	struct bnxt_napi *bnapi = cpr->bnapi;
7356 	struct bnxt_ring_struct *ring;
7357 	u32 map_idx = bnapi->index;
7358 	int rc;
7359 
7360 	ring = &cpr->cp_ring_struct;
7361 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7362 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7363 	if (rc)
7364 		return rc;
7365 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7366 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7367 	return 0;
7368 }
7369 
7370 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7371 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7372 {
7373 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7374 	const u32 type = HWRM_RING_ALLOC_TX;
7375 	int rc;
7376 
7377 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7378 	if (rc)
7379 		return rc;
7380 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7381 	return 0;
7382 }
7383 
7384 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7385 {
7386 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7387 	int i, rc = 0;
7388 	u32 type;
7389 
7390 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7391 		type = HWRM_RING_ALLOC_NQ;
7392 	else
7393 		type = HWRM_RING_ALLOC_CMPL;
7394 	for (i = 0; i < bp->cp_nr_rings; i++) {
7395 		struct bnxt_napi *bnapi = bp->bnapi[i];
7396 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7397 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7398 		u32 map_idx = ring->map_idx;
7399 		unsigned int vector;
7400 
7401 		vector = bp->irq_tbl[map_idx].vector;
7402 		disable_irq_nosync(vector);
7403 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7404 		if (rc) {
7405 			enable_irq(vector);
7406 			goto err_out;
7407 		}
7408 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7409 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7410 		enable_irq(vector);
7411 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7412 
7413 		if (!i) {
7414 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7415 			if (rc)
7416 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7417 		}
7418 	}
7419 
7420 	for (i = 0; i < bp->tx_nr_rings; i++) {
7421 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7422 
7423 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7424 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7425 			if (rc)
7426 				goto err_out;
7427 		}
7428 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7429 		if (rc)
7430 			goto err_out;
7431 	}
7432 
7433 	for (i = 0; i < bp->rx_nr_rings; i++) {
7434 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7435 
7436 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7437 		if (rc)
7438 			goto err_out;
7439 		/* If we have agg rings, post agg buffers first. */
7440 		if (!agg_rings)
7441 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7442 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7443 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7444 			if (rc)
7445 				goto err_out;
7446 		}
7447 	}
7448 
7449 	if (agg_rings) {
7450 		for (i = 0; i < bp->rx_nr_rings; i++) {
7451 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7452 			if (rc)
7453 				goto err_out;
7454 		}
7455 	}
7456 err_out:
7457 	return rc;
7458 }
7459 
7460 static void bnxt_cancel_dim(struct bnxt *bp)
7461 {
7462 	int i;
7463 
7464 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7465 	 * if NAPI is enabled.
7466 	 */
7467 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7468 		return;
7469 
7470 	/* Make sure NAPI sees that the VNIC is disabled */
7471 	synchronize_net();
7472 	for (i = 0; i < bp->rx_nr_rings; i++) {
7473 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7474 		struct bnxt_napi *bnapi = rxr->bnapi;
7475 
7476 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7477 	}
7478 }
7479 
7480 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7481 				   struct bnxt_ring_struct *ring,
7482 				   u32 ring_type, int cmpl_ring_id)
7483 {
7484 	struct hwrm_ring_free_output *resp;
7485 	struct hwrm_ring_free_input *req;
7486 	u16 error_code = 0;
7487 	int rc;
7488 
7489 	if (BNXT_NO_FW_ACCESS(bp))
7490 		return 0;
7491 
7492 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7493 	if (rc)
7494 		goto exit;
7495 
7496 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7497 	req->ring_type = ring_type;
7498 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7499 
7500 	resp = hwrm_req_hold(bp, req);
7501 	rc = hwrm_req_send(bp, req);
7502 	error_code = le16_to_cpu(resp->error_code);
7503 	hwrm_req_drop(bp, req);
7504 exit:
7505 	if (rc || error_code) {
7506 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7507 			   ring_type, rc, error_code);
7508 		return -EIO;
7509 	}
7510 	return 0;
7511 }
7512 
7513 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7514 				   struct bnxt_tx_ring_info *txr,
7515 				   bool close_path)
7516 {
7517 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7518 	u32 cmpl_ring_id;
7519 
7520 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7521 		return;
7522 
7523 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7524 		       INVALID_HW_RING_ID;
7525 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7526 				cmpl_ring_id);
7527 	ring->fw_ring_id = INVALID_HW_RING_ID;
7528 }
7529 
7530 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7531 				   struct bnxt_rx_ring_info *rxr,
7532 				   bool close_path)
7533 {
7534 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7535 	u32 grp_idx = rxr->bnapi->index;
7536 	u32 cmpl_ring_id;
7537 
7538 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7539 		return;
7540 
7541 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7542 	hwrm_ring_free_send_msg(bp, ring,
7543 				RING_FREE_REQ_RING_TYPE_RX,
7544 				close_path ? cmpl_ring_id :
7545 				INVALID_HW_RING_ID);
7546 	ring->fw_ring_id = INVALID_HW_RING_ID;
7547 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7548 }
7549 
7550 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7551 				       struct bnxt_rx_ring_info *rxr,
7552 				       bool close_path)
7553 {
7554 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7555 	u32 grp_idx = rxr->bnapi->index;
7556 	u32 type, cmpl_ring_id;
7557 
7558 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7559 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7560 	else
7561 		type = RING_FREE_REQ_RING_TYPE_RX;
7562 
7563 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7564 		return;
7565 
7566 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7567 	hwrm_ring_free_send_msg(bp, ring, type,
7568 				close_path ? cmpl_ring_id :
7569 				INVALID_HW_RING_ID);
7570 	ring->fw_ring_id = INVALID_HW_RING_ID;
7571 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7572 }
7573 
7574 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7575 				   struct bnxt_cp_ring_info *cpr)
7576 {
7577 	struct bnxt_ring_struct *ring;
7578 
7579 	ring = &cpr->cp_ring_struct;
7580 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7581 		return;
7582 
7583 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7584 				INVALID_HW_RING_ID);
7585 	ring->fw_ring_id = INVALID_HW_RING_ID;
7586 }
7587 
7588 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7589 {
7590 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7591 	int i, size = ring->ring_mem.page_size;
7592 
7593 	cpr->cp_raw_cons = 0;
7594 	cpr->toggle = 0;
7595 
7596 	for (i = 0; i < bp->cp_nr_pages; i++)
7597 		if (cpr->cp_desc_ring[i])
7598 			memset(cpr->cp_desc_ring[i], 0, size);
7599 }
7600 
7601 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7602 {
7603 	u32 type;
7604 	int i;
7605 
7606 	if (!bp->bnapi)
7607 		return;
7608 
7609 	for (i = 0; i < bp->tx_nr_rings; i++)
7610 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7611 
7612 	bnxt_cancel_dim(bp);
7613 	for (i = 0; i < bp->rx_nr_rings; i++) {
7614 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7615 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7616 	}
7617 
7618 	/* The completion rings are about to be freed.  After that the
7619 	 * IRQ doorbell will not work anymore.  So we need to disable
7620 	 * IRQ here.
7621 	 */
7622 	bnxt_disable_int_sync(bp);
7623 
7624 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7625 		type = RING_FREE_REQ_RING_TYPE_NQ;
7626 	else
7627 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7628 	for (i = 0; i < bp->cp_nr_rings; i++) {
7629 		struct bnxt_napi *bnapi = bp->bnapi[i];
7630 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7631 		struct bnxt_ring_struct *ring;
7632 		int j;
7633 
7634 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7635 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7636 
7637 		ring = &cpr->cp_ring_struct;
7638 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7639 			hwrm_ring_free_send_msg(bp, ring, type,
7640 						INVALID_HW_RING_ID);
7641 			ring->fw_ring_id = INVALID_HW_RING_ID;
7642 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7643 		}
7644 	}
7645 }
7646 
7647 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7648 			     bool shared);
7649 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7650 			   bool shared);
7651 
7652 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7653 {
7654 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7655 	struct hwrm_func_qcfg_output *resp;
7656 	struct hwrm_func_qcfg_input *req;
7657 	int rc;
7658 
7659 	if (bp->hwrm_spec_code < 0x10601)
7660 		return 0;
7661 
7662 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7663 	if (rc)
7664 		return rc;
7665 
7666 	req->fid = cpu_to_le16(0xffff);
7667 	resp = hwrm_req_hold(bp, req);
7668 	rc = hwrm_req_send(bp, req);
7669 	if (rc) {
7670 		hwrm_req_drop(bp, req);
7671 		return rc;
7672 	}
7673 
7674 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7675 	if (BNXT_NEW_RM(bp)) {
7676 		u16 cp, stats;
7677 
7678 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7679 		hw_resc->resv_hw_ring_grps =
7680 			le32_to_cpu(resp->alloc_hw_ring_grps);
7681 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7682 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7683 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7684 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7685 		hw_resc->resv_irqs = cp;
7686 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7687 			int rx = hw_resc->resv_rx_rings;
7688 			int tx = hw_resc->resv_tx_rings;
7689 
7690 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7691 				rx >>= 1;
7692 			if (cp < (rx + tx)) {
7693 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7694 				if (rc)
7695 					goto get_rings_exit;
7696 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7697 					rx <<= 1;
7698 				hw_resc->resv_rx_rings = rx;
7699 				hw_resc->resv_tx_rings = tx;
7700 			}
7701 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7702 			hw_resc->resv_hw_ring_grps = rx;
7703 		}
7704 		hw_resc->resv_cp_rings = cp;
7705 		hw_resc->resv_stat_ctxs = stats;
7706 	}
7707 get_rings_exit:
7708 	hwrm_req_drop(bp, req);
7709 	return rc;
7710 }
7711 
7712 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7713 {
7714 	struct hwrm_func_qcfg_output *resp;
7715 	struct hwrm_func_qcfg_input *req;
7716 	int rc;
7717 
7718 	if (bp->hwrm_spec_code < 0x10601)
7719 		return 0;
7720 
7721 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7722 	if (rc)
7723 		return rc;
7724 
7725 	req->fid = cpu_to_le16(fid);
7726 	resp = hwrm_req_hold(bp, req);
7727 	rc = hwrm_req_send(bp, req);
7728 	if (!rc)
7729 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7730 
7731 	hwrm_req_drop(bp, req);
7732 	return rc;
7733 }
7734 
7735 static bool bnxt_rfs_supported(struct bnxt *bp);
7736 
7737 static struct hwrm_func_cfg_input *
7738 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7739 {
7740 	struct hwrm_func_cfg_input *req;
7741 	u32 enables = 0;
7742 
7743 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7744 		return NULL;
7745 
7746 	req->fid = cpu_to_le16(0xffff);
7747 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7748 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7749 	if (BNXT_NEW_RM(bp)) {
7750 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7751 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7752 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7753 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7754 			enables |= hwr->cp_p5 ?
7755 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7756 		} else {
7757 			enables |= hwr->cp ?
7758 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7759 			enables |= hwr->grp ?
7760 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7761 		}
7762 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7763 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7764 					  0;
7765 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7766 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7767 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7768 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7769 			req->num_msix = cpu_to_le16(hwr->cp);
7770 		} else {
7771 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7772 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7773 		}
7774 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7775 		req->num_vnics = cpu_to_le16(hwr->vnic);
7776 	}
7777 	req->enables = cpu_to_le32(enables);
7778 	return req;
7779 }
7780 
7781 static struct hwrm_func_vf_cfg_input *
7782 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7783 {
7784 	struct hwrm_func_vf_cfg_input *req;
7785 	u32 enables = 0;
7786 
7787 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7788 		return NULL;
7789 
7790 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7791 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7792 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7793 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7794 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7795 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7796 		enables |= hwr->cp_p5 ?
7797 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7798 	} else {
7799 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7800 		enables |= hwr->grp ?
7801 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7802 	}
7803 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7804 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7805 
7806 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7807 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7808 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7809 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7810 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7811 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7812 	} else {
7813 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7814 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7815 	}
7816 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7817 	req->num_vnics = cpu_to_le16(hwr->vnic);
7818 
7819 	req->enables = cpu_to_le32(enables);
7820 	return req;
7821 }
7822 
7823 static int
7824 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7825 {
7826 	struct hwrm_func_cfg_input *req;
7827 	int rc;
7828 
7829 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7830 	if (!req)
7831 		return -ENOMEM;
7832 
7833 	if (!req->enables) {
7834 		hwrm_req_drop(bp, req);
7835 		return 0;
7836 	}
7837 
7838 	rc = hwrm_req_send(bp, req);
7839 	if (rc)
7840 		return rc;
7841 
7842 	if (bp->hwrm_spec_code < 0x10601)
7843 		bp->hw_resc.resv_tx_rings = hwr->tx;
7844 
7845 	return bnxt_hwrm_get_rings(bp);
7846 }
7847 
7848 static int
7849 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7850 {
7851 	struct hwrm_func_vf_cfg_input *req;
7852 	int rc;
7853 
7854 	if (!BNXT_NEW_RM(bp)) {
7855 		bp->hw_resc.resv_tx_rings = hwr->tx;
7856 		return 0;
7857 	}
7858 
7859 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7860 	if (!req)
7861 		return -ENOMEM;
7862 
7863 	rc = hwrm_req_send(bp, req);
7864 	if (rc)
7865 		return rc;
7866 
7867 	return bnxt_hwrm_get_rings(bp);
7868 }
7869 
7870 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7871 {
7872 	if (BNXT_PF(bp))
7873 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7874 	else
7875 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7876 }
7877 
7878 int bnxt_nq_rings_in_use(struct bnxt *bp)
7879 {
7880 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7881 }
7882 
7883 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7884 {
7885 	int cp;
7886 
7887 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7888 		return bnxt_nq_rings_in_use(bp);
7889 
7890 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7891 	return cp;
7892 }
7893 
7894 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7895 {
7896 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7897 }
7898 
7899 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7900 {
7901 	if (!hwr->grp)
7902 		return 0;
7903 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7904 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7905 
7906 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7907 			rss_ctx *= hwr->vnic;
7908 		return rss_ctx;
7909 	}
7910 	if (BNXT_VF(bp))
7911 		return BNXT_VF_MAX_RSS_CTX;
7912 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7913 		return hwr->grp + 1;
7914 	return 1;
7915 }
7916 
7917 /* Check if a default RSS map needs to be setup.  This function is only
7918  * used on older firmware that does not require reserving RX rings.
7919  */
7920 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7921 {
7922 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7923 
7924 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7925 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7926 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7927 		if (!netif_is_rxfh_configured(bp->dev))
7928 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7929 	}
7930 }
7931 
7932 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7933 {
7934 	if (bp->flags & BNXT_FLAG_RFS) {
7935 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7936 			return 2 + bp->num_rss_ctx;
7937 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7938 			return rx_rings + 1;
7939 	}
7940 	return 1;
7941 }
7942 
7943 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7944 {
7945 	hwr->cp = bnxt_nq_rings_in_use(bp);
7946 	hwr->cp_p5 = 0;
7947 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7948 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
7949 	hwr->tx = bp->tx_nr_rings;
7950 	hwr->rx = bp->rx_nr_rings;
7951 	hwr->grp = hwr->rx;
7952 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
7953 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
7954 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7955 		hwr->rx <<= 1;
7956 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
7957 }
7958 
7959 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7960 {
7961 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7962 	struct bnxt_hw_rings hwr;
7963 
7964 	bnxt_get_total_resources(bp, &hwr);
7965 
7966 	/* Old firmware does not need RX ring reservations but we still
7967 	 * need to setup a default RSS map when needed.  With new firmware
7968 	 * we go through RX ring reservations first and then set up the
7969 	 * RSS map for the successfully reserved RX rings when needed.
7970 	 */
7971 	if (!BNXT_NEW_RM(bp))
7972 		bnxt_check_rss_tbl_no_rmgr(bp);
7973 
7974 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
7975 		return true;
7976 
7977 	if (!BNXT_NEW_RM(bp))
7978 		return false;
7979 
7980 	if (hw_resc->resv_rx_rings != hwr.rx ||
7981 	    hw_resc->resv_vnics != hwr.vnic ||
7982 	    hw_resc->resv_stat_ctxs != hwr.stat ||
7983 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
7984 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
7985 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7986 		return true;
7987 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7988 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
7989 			return true;
7990 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
7991 		return true;
7992 	}
7993 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7994 	    hw_resc->resv_irqs != hwr.cp)
7995 		return true;
7996 	return false;
7997 }
7998 
7999 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8000 {
8001 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8002 
8003 	hwr->tx = hw_resc->resv_tx_rings;
8004 	if (BNXT_NEW_RM(bp)) {
8005 		hwr->rx = hw_resc->resv_rx_rings;
8006 		hwr->cp = hw_resc->resv_irqs;
8007 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8008 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8009 		hwr->grp = hw_resc->resv_hw_ring_grps;
8010 		hwr->vnic = hw_resc->resv_vnics;
8011 		hwr->stat = hw_resc->resv_stat_ctxs;
8012 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8013 	}
8014 }
8015 
8016 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8017 {
8018 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8019 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8020 }
8021 
8022 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8023 
8024 static int __bnxt_reserve_rings(struct bnxt *bp)
8025 {
8026 	struct bnxt_hw_rings hwr = {0};
8027 	int rx_rings, old_rx_rings, rc;
8028 	int cp = bp->cp_nr_rings;
8029 	int ulp_msix = 0;
8030 	bool sh = false;
8031 	int tx_cp;
8032 
8033 	if (!bnxt_need_reserve_rings(bp))
8034 		return 0;
8035 
8036 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
8037 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8038 		if (!ulp_msix)
8039 			bnxt_set_ulp_stat_ctxs(bp, 0);
8040 		else
8041 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8042 
8043 		if (ulp_msix > bp->ulp_num_msix_want)
8044 			ulp_msix = bp->ulp_num_msix_want;
8045 		hwr.cp = cp + ulp_msix;
8046 	} else {
8047 		hwr.cp = bnxt_nq_rings_in_use(bp);
8048 	}
8049 
8050 	hwr.tx = bp->tx_nr_rings;
8051 	hwr.rx = bp->rx_nr_rings;
8052 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8053 		sh = true;
8054 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8055 		hwr.cp_p5 = hwr.rx + hwr.tx;
8056 
8057 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8058 
8059 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8060 		hwr.rx <<= 1;
8061 	hwr.grp = bp->rx_nr_rings;
8062 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8063 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8064 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8065 
8066 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8067 	if (rc)
8068 		return rc;
8069 
8070 	bnxt_copy_reserved_rings(bp, &hwr);
8071 
8072 	rx_rings = hwr.rx;
8073 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8074 		if (hwr.rx >= 2) {
8075 			rx_rings = hwr.rx >> 1;
8076 		} else {
8077 			if (netif_running(bp->dev))
8078 				return -ENOMEM;
8079 
8080 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8081 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8082 			bp->dev->hw_features &= ~NETIF_F_LRO;
8083 			bp->dev->features &= ~NETIF_F_LRO;
8084 			bnxt_set_ring_params(bp);
8085 		}
8086 	}
8087 	rx_rings = min_t(int, rx_rings, hwr.grp);
8088 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8089 	if (bnxt_ulp_registered(bp->edev) &&
8090 	    hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8091 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8092 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8093 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8094 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8095 		hwr.rx = rx_rings << 1;
8096 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8097 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8098 	if (hwr.tx != bp->tx_nr_rings) {
8099 		netdev_warn(bp->dev,
8100 			    "Able to reserve only %d out of %d requested TX rings\n",
8101 			    hwr.tx, bp->tx_nr_rings);
8102 	}
8103 	bp->tx_nr_rings = hwr.tx;
8104 
8105 	/* If we cannot reserve all the RX rings, reset the RSS map only
8106 	 * if absolutely necessary
8107 	 */
8108 	if (rx_rings != bp->rx_nr_rings) {
8109 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8110 			    rx_rings, bp->rx_nr_rings);
8111 		if (netif_is_rxfh_configured(bp->dev) &&
8112 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8113 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8114 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8115 			ethtool_rxfh_indir_lost(bp->dev);
8116 		}
8117 	}
8118 	bp->rx_nr_rings = rx_rings;
8119 	bp->cp_nr_rings = hwr.cp;
8120 
8121 	/* Fall back if we cannot reserve enough HW RSS contexts */
8122 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8123 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8124 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8125 
8126 	if (!bnxt_rings_ok(bp, &hwr))
8127 		return -ENOMEM;
8128 
8129 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8130 	    !netif_is_rxfh_configured(bp->dev))
8131 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8132 
8133 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8134 		int resv_msix, resv_ctx, ulp_ctxs;
8135 		struct bnxt_hw_resc *hw_resc;
8136 
8137 		hw_resc = &bp->hw_resc;
8138 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8139 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8140 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8141 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8142 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8143 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8144 	}
8145 
8146 	return rc;
8147 }
8148 
8149 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8150 {
8151 	struct hwrm_func_vf_cfg_input *req;
8152 	u32 flags;
8153 
8154 	if (!BNXT_NEW_RM(bp))
8155 		return 0;
8156 
8157 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8158 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8159 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8160 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8161 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8162 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8163 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8164 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8165 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8166 
8167 	req->flags = cpu_to_le32(flags);
8168 	return hwrm_req_send_silent(bp, req);
8169 }
8170 
8171 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8172 {
8173 	struct hwrm_func_cfg_input *req;
8174 	u32 flags;
8175 
8176 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8177 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8178 	if (BNXT_NEW_RM(bp)) {
8179 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8180 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8181 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8182 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8183 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8184 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8185 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8186 		else
8187 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8188 	}
8189 
8190 	req->flags = cpu_to_le32(flags);
8191 	return hwrm_req_send_silent(bp, req);
8192 }
8193 
8194 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8195 {
8196 	if (bp->hwrm_spec_code < 0x10801)
8197 		return 0;
8198 
8199 	if (BNXT_PF(bp))
8200 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8201 
8202 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8203 }
8204 
8205 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8206 {
8207 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8208 	struct hwrm_ring_aggint_qcaps_output *resp;
8209 	struct hwrm_ring_aggint_qcaps_input *req;
8210 	int rc;
8211 
8212 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8213 	coal_cap->num_cmpl_dma_aggr_max = 63;
8214 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8215 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8216 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8217 	coal_cap->int_lat_tmr_min_max = 65535;
8218 	coal_cap->int_lat_tmr_max_max = 65535;
8219 	coal_cap->num_cmpl_aggr_int_max = 65535;
8220 	coal_cap->timer_units = 80;
8221 
8222 	if (bp->hwrm_spec_code < 0x10902)
8223 		return;
8224 
8225 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8226 		return;
8227 
8228 	resp = hwrm_req_hold(bp, req);
8229 	rc = hwrm_req_send_silent(bp, req);
8230 	if (!rc) {
8231 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8232 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8233 		coal_cap->num_cmpl_dma_aggr_max =
8234 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8235 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8236 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8237 		coal_cap->cmpl_aggr_dma_tmr_max =
8238 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8239 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8240 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8241 		coal_cap->int_lat_tmr_min_max =
8242 			le16_to_cpu(resp->int_lat_tmr_min_max);
8243 		coal_cap->int_lat_tmr_max_max =
8244 			le16_to_cpu(resp->int_lat_tmr_max_max);
8245 		coal_cap->num_cmpl_aggr_int_max =
8246 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8247 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8248 	}
8249 	hwrm_req_drop(bp, req);
8250 }
8251 
8252 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8253 {
8254 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8255 
8256 	return usec * 1000 / coal_cap->timer_units;
8257 }
8258 
8259 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8260 	struct bnxt_coal *hw_coal,
8261 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8262 {
8263 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8264 	u16 val, tmr, max, flags = hw_coal->flags;
8265 	u32 cmpl_params = coal_cap->cmpl_params;
8266 
8267 	max = hw_coal->bufs_per_record * 128;
8268 	if (hw_coal->budget)
8269 		max = hw_coal->bufs_per_record * hw_coal->budget;
8270 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8271 
8272 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8273 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8274 
8275 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8276 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8277 
8278 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8279 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8280 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8281 
8282 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8283 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8284 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8285 
8286 	/* min timer set to 1/2 of interrupt timer */
8287 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8288 		val = tmr / 2;
8289 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8290 		req->int_lat_tmr_min = cpu_to_le16(val);
8291 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8292 	}
8293 
8294 	/* buf timer set to 1/4 of interrupt timer */
8295 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8296 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8297 
8298 	if (cmpl_params &
8299 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8300 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8301 		val = clamp_t(u16, tmr, 1,
8302 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8303 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8304 		req->enables |=
8305 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8306 	}
8307 
8308 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8309 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8310 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8311 	req->flags = cpu_to_le16(flags);
8312 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8313 }
8314 
8315 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8316 				   struct bnxt_coal *hw_coal)
8317 {
8318 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8319 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8320 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8321 	u32 nq_params = coal_cap->nq_params;
8322 	u16 tmr;
8323 	int rc;
8324 
8325 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8326 		return 0;
8327 
8328 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8329 	if (rc)
8330 		return rc;
8331 
8332 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8333 	req->flags =
8334 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8335 
8336 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8337 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8338 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8339 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8340 	return hwrm_req_send(bp, req);
8341 }
8342 
8343 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8344 {
8345 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8346 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8347 	struct bnxt_coal coal;
8348 	int rc;
8349 
8350 	/* Tick values in micro seconds.
8351 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8352 	 */
8353 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8354 
8355 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8356 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8357 
8358 	if (!bnapi->rx_ring)
8359 		return -ENODEV;
8360 
8361 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8362 	if (rc)
8363 		return rc;
8364 
8365 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8366 
8367 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8368 
8369 	return hwrm_req_send(bp, req_rx);
8370 }
8371 
8372 static int
8373 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8374 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8375 {
8376 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8377 
8378 	req->ring_id = cpu_to_le16(ring_id);
8379 	return hwrm_req_send(bp, req);
8380 }
8381 
8382 static int
8383 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8384 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8385 {
8386 	struct bnxt_tx_ring_info *txr;
8387 	int i, rc;
8388 
8389 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8390 		u16 ring_id;
8391 
8392 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8393 		req->ring_id = cpu_to_le16(ring_id);
8394 		rc = hwrm_req_send(bp, req);
8395 		if (rc)
8396 			return rc;
8397 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8398 			return 0;
8399 	}
8400 	return 0;
8401 }
8402 
8403 int bnxt_hwrm_set_coal(struct bnxt *bp)
8404 {
8405 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8406 	int i, rc;
8407 
8408 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8409 	if (rc)
8410 		return rc;
8411 
8412 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8413 	if (rc) {
8414 		hwrm_req_drop(bp, req_rx);
8415 		return rc;
8416 	}
8417 
8418 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8419 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8420 
8421 	hwrm_req_hold(bp, req_rx);
8422 	hwrm_req_hold(bp, req_tx);
8423 	for (i = 0; i < bp->cp_nr_rings; i++) {
8424 		struct bnxt_napi *bnapi = bp->bnapi[i];
8425 		struct bnxt_coal *hw_coal;
8426 
8427 		if (!bnapi->rx_ring)
8428 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8429 		else
8430 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8431 		if (rc)
8432 			break;
8433 
8434 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8435 			continue;
8436 
8437 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8438 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8439 			if (rc)
8440 				break;
8441 		}
8442 		if (bnapi->rx_ring)
8443 			hw_coal = &bp->rx_coal;
8444 		else
8445 			hw_coal = &bp->tx_coal;
8446 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8447 	}
8448 	hwrm_req_drop(bp, req_rx);
8449 	hwrm_req_drop(bp, req_tx);
8450 	return rc;
8451 }
8452 
8453 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8454 {
8455 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8456 	struct hwrm_stat_ctx_free_input *req;
8457 	int i;
8458 
8459 	if (!bp->bnapi)
8460 		return;
8461 
8462 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8463 		return;
8464 
8465 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8466 		return;
8467 	if (BNXT_FW_MAJ(bp) <= 20) {
8468 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8469 			hwrm_req_drop(bp, req);
8470 			return;
8471 		}
8472 		hwrm_req_hold(bp, req0);
8473 	}
8474 	hwrm_req_hold(bp, req);
8475 	for (i = 0; i < bp->cp_nr_rings; i++) {
8476 		struct bnxt_napi *bnapi = bp->bnapi[i];
8477 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8478 
8479 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8480 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8481 			if (req0) {
8482 				req0->stat_ctx_id = req->stat_ctx_id;
8483 				hwrm_req_send(bp, req0);
8484 			}
8485 			hwrm_req_send(bp, req);
8486 
8487 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8488 		}
8489 	}
8490 	hwrm_req_drop(bp, req);
8491 	if (req0)
8492 		hwrm_req_drop(bp, req0);
8493 }
8494 
8495 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8496 {
8497 	struct hwrm_stat_ctx_alloc_output *resp;
8498 	struct hwrm_stat_ctx_alloc_input *req;
8499 	int rc, i;
8500 
8501 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8502 		return 0;
8503 
8504 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8505 	if (rc)
8506 		return rc;
8507 
8508 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8509 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8510 
8511 	resp = hwrm_req_hold(bp, req);
8512 	for (i = 0; i < bp->cp_nr_rings; i++) {
8513 		struct bnxt_napi *bnapi = bp->bnapi[i];
8514 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8515 
8516 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8517 
8518 		rc = hwrm_req_send(bp, req);
8519 		if (rc)
8520 			break;
8521 
8522 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8523 
8524 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8525 	}
8526 	hwrm_req_drop(bp, req);
8527 	return rc;
8528 }
8529 
8530 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8531 {
8532 	struct hwrm_func_qcfg_output *resp;
8533 	struct hwrm_func_qcfg_input *req;
8534 	u16 flags;
8535 	int rc;
8536 
8537 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8538 	if (rc)
8539 		return rc;
8540 
8541 	req->fid = cpu_to_le16(0xffff);
8542 	resp = hwrm_req_hold(bp, req);
8543 	rc = hwrm_req_send(bp, req);
8544 	if (rc)
8545 		goto func_qcfg_exit;
8546 
8547 	flags = le16_to_cpu(resp->flags);
8548 #ifdef CONFIG_BNXT_SRIOV
8549 	if (BNXT_VF(bp)) {
8550 		struct bnxt_vf_info *vf = &bp->vf;
8551 
8552 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8553 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8554 			vf->flags |= BNXT_VF_TRUST;
8555 		else
8556 			vf->flags &= ~BNXT_VF_TRUST;
8557 	} else {
8558 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8559 	}
8560 #endif
8561 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8562 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8563 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8564 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8565 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8566 	}
8567 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8568 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8569 
8570 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8571 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8572 
8573 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8574 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8575 	if (resp->roce_bidi_opt_mode &
8576 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8577 		bp->cos0_cos1_shared = 1;
8578 	else
8579 		bp->cos0_cos1_shared = 0;
8580 
8581 	switch (resp->port_partition_type) {
8582 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8583 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8584 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8585 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8586 		bp->port_partition_type = resp->port_partition_type;
8587 		break;
8588 	}
8589 	if (bp->hwrm_spec_code < 0x10707 ||
8590 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8591 		bp->br_mode = BRIDGE_MODE_VEB;
8592 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8593 		bp->br_mode = BRIDGE_MODE_VEPA;
8594 	else
8595 		bp->br_mode = BRIDGE_MODE_UNDEF;
8596 
8597 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8598 	if (!bp->max_mtu)
8599 		bp->max_mtu = BNXT_MAX_MTU;
8600 
8601 	if (bp->db_size)
8602 		goto func_qcfg_exit;
8603 
8604 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8605 	if (BNXT_CHIP_P5(bp)) {
8606 		if (BNXT_PF(bp))
8607 			bp->db_offset = DB_PF_OFFSET_P5;
8608 		else
8609 			bp->db_offset = DB_VF_OFFSET_P5;
8610 	}
8611 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8612 				 1024);
8613 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8614 	    bp->db_size <= bp->db_offset)
8615 		bp->db_size = pci_resource_len(bp->pdev, 2);
8616 
8617 func_qcfg_exit:
8618 	hwrm_req_drop(bp, req);
8619 	return rc;
8620 }
8621 
8622 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8623 				      u8 init_val, u8 init_offset,
8624 				      bool init_mask_set)
8625 {
8626 	ctxm->init_value = init_val;
8627 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8628 	if (init_mask_set)
8629 		ctxm->init_offset = init_offset * 4;
8630 	else
8631 		ctxm->init_value = 0;
8632 }
8633 
8634 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8635 {
8636 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8637 	u16 type;
8638 
8639 	for (type = 0; type < ctx_max; type++) {
8640 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8641 		int n = 1;
8642 
8643 		if (!ctxm->max_entries || ctxm->pg_info)
8644 			continue;
8645 
8646 		if (ctxm->instance_bmap)
8647 			n = hweight32(ctxm->instance_bmap);
8648 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8649 		if (!ctxm->pg_info)
8650 			return -ENOMEM;
8651 	}
8652 	return 0;
8653 }
8654 
8655 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8656 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8657 
8658 #define BNXT_CTX_INIT_VALID(flags)	\
8659 	(!!((flags) &			\
8660 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8661 
8662 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8663 {
8664 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8665 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8666 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8667 	u16 type, next_type = 0;
8668 	int rc;
8669 
8670 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8671 	if (rc)
8672 		return rc;
8673 
8674 	if (!ctx) {
8675 		ctx = kzalloc_obj(*ctx);
8676 		if (!ctx)
8677 			return -ENOMEM;
8678 		bp->ctx = ctx;
8679 	}
8680 
8681 	resp = hwrm_req_hold(bp, req);
8682 
8683 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8684 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8685 		u8 init_val, init_off, i;
8686 		u32 max_entries;
8687 		u16 entry_size;
8688 		__le32 *p;
8689 		u32 flags;
8690 
8691 		req->type = cpu_to_le16(type);
8692 		rc = hwrm_req_send(bp, req);
8693 		if (rc)
8694 			goto ctx_done;
8695 		flags = le32_to_cpu(resp->flags);
8696 		next_type = le16_to_cpu(resp->next_valid_type);
8697 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8698 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8699 			continue;
8700 		}
8701 		entry_size = le16_to_cpu(resp->entry_size);
8702 		max_entries = le32_to_cpu(resp->max_num_entries);
8703 		if (ctxm->mem_valid) {
8704 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8705 			    ctxm->entry_size != entry_size ||
8706 			    ctxm->max_entries != max_entries)
8707 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8708 			else
8709 				continue;
8710 		}
8711 		ctxm->type = type;
8712 		ctxm->entry_size = entry_size;
8713 		ctxm->flags = flags;
8714 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8715 		ctxm->entry_multiple = resp->entry_multiple;
8716 		ctxm->max_entries = max_entries;
8717 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8718 		init_val = resp->ctx_init_value;
8719 		init_off = resp->ctx_init_offset;
8720 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8721 					  BNXT_CTX_INIT_VALID(flags));
8722 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8723 					      BNXT_MAX_SPLIT_ENTRY);
8724 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8725 		     i++, p++)
8726 			ctxm->split[i] = le32_to_cpu(*p);
8727 	}
8728 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8729 
8730 ctx_done:
8731 	hwrm_req_drop(bp, req);
8732 	return rc;
8733 }
8734 
8735 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8736 {
8737 	struct hwrm_func_backing_store_qcaps_output *resp;
8738 	struct hwrm_func_backing_store_qcaps_input *req;
8739 	int rc;
8740 
8741 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8742 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8743 		return 0;
8744 
8745 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8746 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8747 
8748 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8749 	if (rc)
8750 		return rc;
8751 
8752 	resp = hwrm_req_hold(bp, req);
8753 	rc = hwrm_req_send_silent(bp, req);
8754 	if (!rc) {
8755 		struct bnxt_ctx_mem_type *ctxm;
8756 		struct bnxt_ctx_mem_info *ctx;
8757 		u8 init_val, init_idx = 0;
8758 		u16 init_mask;
8759 
8760 		ctx = bp->ctx;
8761 		if (!ctx) {
8762 			ctx = kzalloc_obj(*ctx);
8763 			if (!ctx) {
8764 				rc = -ENOMEM;
8765 				goto ctx_err;
8766 			}
8767 			bp->ctx = ctx;
8768 		}
8769 		init_val = resp->ctx_kind_initializer;
8770 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8771 
8772 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8773 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8774 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8775 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8776 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8777 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8778 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8779 					  (init_mask & (1 << init_idx++)) != 0);
8780 
8781 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8782 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8783 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8784 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8785 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8786 					  (init_mask & (1 << init_idx++)) != 0);
8787 
8788 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8789 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8790 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8791 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8792 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8793 					  (init_mask & (1 << init_idx++)) != 0);
8794 
8795 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8796 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8797 		ctxm->max_entries = ctxm->vnic_entries +
8798 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8799 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8800 		bnxt_init_ctx_initializer(ctxm, init_val,
8801 					  resp->vnic_init_offset,
8802 					  (init_mask & (1 << init_idx++)) != 0);
8803 
8804 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8805 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8806 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8807 		bnxt_init_ctx_initializer(ctxm, init_val,
8808 					  resp->stat_init_offset,
8809 					  (init_mask & (1 << init_idx++)) != 0);
8810 
8811 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8812 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8813 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8814 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8815 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8816 		if (!ctxm->entry_multiple)
8817 			ctxm->entry_multiple = 1;
8818 
8819 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8820 
8821 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8822 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8823 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8824 		ctxm->mrav_num_entries_units =
8825 			le16_to_cpu(resp->mrav_num_entries_units);
8826 		bnxt_init_ctx_initializer(ctxm, init_val,
8827 					  resp->mrav_init_offset,
8828 					  (init_mask & (1 << init_idx++)) != 0);
8829 
8830 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8831 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8832 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8833 
8834 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8835 		if (!ctx->tqm_fp_rings_count)
8836 			ctx->tqm_fp_rings_count = bp->max_q;
8837 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8838 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8839 
8840 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8841 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8842 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8843 
8844 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8845 	} else {
8846 		rc = 0;
8847 	}
8848 ctx_err:
8849 	hwrm_req_drop(bp, req);
8850 	return rc;
8851 }
8852 
8853 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8854 				  __le64 *pg_dir)
8855 {
8856 	if (!rmem->nr_pages)
8857 		return;
8858 
8859 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8860 	if (rmem->depth >= 1) {
8861 		if (rmem->depth == 2)
8862 			*pg_attr |= 2;
8863 		else
8864 			*pg_attr |= 1;
8865 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8866 	} else {
8867 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8868 	}
8869 }
8870 
8871 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8872 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8873 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8874 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8875 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8876 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8877 
8878 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8879 {
8880 	struct hwrm_func_backing_store_cfg_input *req;
8881 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8882 	struct bnxt_ctx_pg_info *ctx_pg;
8883 	struct bnxt_ctx_mem_type *ctxm;
8884 	void **__req = (void **)&req;
8885 	u32 req_len = sizeof(*req);
8886 	__le32 *num_entries;
8887 	__le64 *pg_dir;
8888 	u32 flags = 0;
8889 	u8 *pg_attr;
8890 	u32 ena;
8891 	int rc;
8892 	int i;
8893 
8894 	if (!ctx)
8895 		return 0;
8896 
8897 	if (req_len > bp->hwrm_max_ext_req_len)
8898 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8899 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8900 	if (rc)
8901 		return rc;
8902 
8903 	req->enables = cpu_to_le32(enables);
8904 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8905 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8906 		ctx_pg = ctxm->pg_info;
8907 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8908 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8909 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8910 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8911 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8912 				      &req->qpc_pg_size_qpc_lvl,
8913 				      &req->qpc_page_dir);
8914 
8915 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8916 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8917 	}
8918 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8919 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8920 		ctx_pg = ctxm->pg_info;
8921 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8922 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8923 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8924 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8925 				      &req->srq_pg_size_srq_lvl,
8926 				      &req->srq_page_dir);
8927 	}
8928 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8929 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8930 		ctx_pg = ctxm->pg_info;
8931 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8932 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8933 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8934 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8935 				      &req->cq_pg_size_cq_lvl,
8936 				      &req->cq_page_dir);
8937 	}
8938 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8939 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8940 		ctx_pg = ctxm->pg_info;
8941 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8942 		req->vnic_num_ring_table_entries =
8943 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8944 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8945 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8946 				      &req->vnic_pg_size_vnic_lvl,
8947 				      &req->vnic_page_dir);
8948 	}
8949 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8950 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8951 		ctx_pg = ctxm->pg_info;
8952 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8953 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8954 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8955 				      &req->stat_pg_size_stat_lvl,
8956 				      &req->stat_page_dir);
8957 	}
8958 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8959 		u32 units;
8960 
8961 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8962 		ctx_pg = ctxm->pg_info;
8963 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8964 		units = ctxm->mrav_num_entries_units;
8965 		if (units) {
8966 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8967 			u32 entries;
8968 
8969 			num_mr = ctx_pg->entries - num_ah;
8970 			entries = ((num_mr / units) << 16) | (num_ah / units);
8971 			req->mrav_num_entries = cpu_to_le32(entries);
8972 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8973 		}
8974 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8975 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8976 				      &req->mrav_pg_size_mrav_lvl,
8977 				      &req->mrav_page_dir);
8978 	}
8979 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8980 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8981 		ctx_pg = ctxm->pg_info;
8982 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8983 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8984 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8985 				      &req->tim_pg_size_tim_lvl,
8986 				      &req->tim_page_dir);
8987 	}
8988 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8989 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8990 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8991 	     pg_dir = &req->tqm_sp_page_dir,
8992 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8993 	     ctx_pg = ctxm->pg_info;
8994 	     i < BNXT_MAX_TQM_RINGS;
8995 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8996 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8997 		if (!(enables & ena))
8998 			continue;
8999 
9000 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9001 		*num_entries = cpu_to_le32(ctx_pg->entries);
9002 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9003 	}
9004 	req->flags = cpu_to_le32(flags);
9005 	return hwrm_req_send(bp, req);
9006 }
9007 
9008 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9009 				  struct bnxt_ctx_pg_info *ctx_pg)
9010 {
9011 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9012 
9013 	rmem->page_size = BNXT_PAGE_SIZE;
9014 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9015 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9016 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9017 	if (rmem->depth >= 1)
9018 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9019 	return bnxt_alloc_ring(bp, rmem);
9020 }
9021 
9022 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9023 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9024 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9025 {
9026 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9027 	int rc;
9028 
9029 	if (!mem_size)
9030 		return -EINVAL;
9031 
9032 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9033 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9034 		ctx_pg->nr_pages = 0;
9035 		return -EINVAL;
9036 	}
9037 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9038 		int nr_tbls, i;
9039 
9040 		rmem->depth = 2;
9041 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9042 		if (!ctx_pg->ctx_pg_tbl)
9043 			return -ENOMEM;
9044 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9045 		rmem->nr_pages = nr_tbls;
9046 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9047 		if (rc)
9048 			return rc;
9049 		for (i = 0; i < nr_tbls; i++) {
9050 			struct bnxt_ctx_pg_info *pg_tbl;
9051 
9052 			pg_tbl = kzalloc_obj(*pg_tbl);
9053 			if (!pg_tbl)
9054 				return -ENOMEM;
9055 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9056 			rmem = &pg_tbl->ring_mem;
9057 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9058 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9059 			rmem->depth = 1;
9060 			rmem->nr_pages = MAX_CTX_PAGES;
9061 			rmem->ctx_mem = ctxm;
9062 			if (i == (nr_tbls - 1)) {
9063 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9064 
9065 				if (rem)
9066 					rmem->nr_pages = rem;
9067 			}
9068 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9069 			if (rc)
9070 				break;
9071 		}
9072 	} else {
9073 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9074 		if (rmem->nr_pages > 1 || depth)
9075 			rmem->depth = 1;
9076 		rmem->ctx_mem = ctxm;
9077 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9078 	}
9079 	return rc;
9080 }
9081 
9082 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9083 				    struct bnxt_ctx_pg_info *ctx_pg,
9084 				    void *buf, size_t offset, size_t head,
9085 				    size_t tail)
9086 {
9087 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9088 	size_t nr_pages = ctx_pg->nr_pages;
9089 	int page_size = rmem->page_size;
9090 	size_t len = 0, total_len = 0;
9091 	u16 depth = rmem->depth;
9092 
9093 	tail %= nr_pages * page_size;
9094 	do {
9095 		if (depth > 1) {
9096 			int i = head / (page_size * MAX_CTX_PAGES);
9097 			struct bnxt_ctx_pg_info *pg_tbl;
9098 
9099 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9100 			rmem = &pg_tbl->ring_mem;
9101 		}
9102 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9103 		head += len;
9104 		offset += len;
9105 		total_len += len;
9106 		if (head >= nr_pages * page_size)
9107 			head = 0;
9108 	} while (head != tail);
9109 	return total_len;
9110 }
9111 
9112 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9113 				  struct bnxt_ctx_pg_info *ctx_pg)
9114 {
9115 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9116 
9117 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9118 	    ctx_pg->ctx_pg_tbl) {
9119 		int i, nr_tbls = rmem->nr_pages;
9120 
9121 		for (i = 0; i < nr_tbls; i++) {
9122 			struct bnxt_ctx_pg_info *pg_tbl;
9123 			struct bnxt_ring_mem_info *rmem2;
9124 
9125 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9126 			if (!pg_tbl)
9127 				continue;
9128 			rmem2 = &pg_tbl->ring_mem;
9129 			bnxt_free_ring(bp, rmem2);
9130 			ctx_pg->ctx_pg_arr[i] = NULL;
9131 			kfree(pg_tbl);
9132 			ctx_pg->ctx_pg_tbl[i] = NULL;
9133 		}
9134 		kfree(ctx_pg->ctx_pg_tbl);
9135 		ctx_pg->ctx_pg_tbl = NULL;
9136 	}
9137 	bnxt_free_ring(bp, rmem);
9138 	ctx_pg->nr_pages = 0;
9139 }
9140 
9141 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9142 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9143 				   u8 pg_lvl)
9144 {
9145 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9146 	int i, rc = 0, n = 1;
9147 	u32 mem_size;
9148 
9149 	if (!ctxm->entry_size || !ctx_pg)
9150 		return -EINVAL;
9151 	if (ctxm->instance_bmap)
9152 		n = hweight32(ctxm->instance_bmap);
9153 	if (ctxm->entry_multiple)
9154 		entries = roundup(entries, ctxm->entry_multiple);
9155 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9156 	mem_size = entries * ctxm->entry_size;
9157 	for (i = 0; i < n && !rc; i++) {
9158 		ctx_pg[i].entries = entries;
9159 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9160 					    ctxm->init_value ? ctxm : NULL);
9161 	}
9162 	if (!rc)
9163 		ctxm->mem_valid = 1;
9164 	return rc;
9165 }
9166 
9167 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9168 					       struct bnxt_ctx_mem_type *ctxm,
9169 					       bool last)
9170 {
9171 	struct hwrm_func_backing_store_cfg_v2_input *req;
9172 	u32 instance_bmap = ctxm->instance_bmap;
9173 	int i, j, rc = 0, n = 1;
9174 	__le32 *p;
9175 
9176 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9177 		return 0;
9178 
9179 	if (instance_bmap)
9180 		n = hweight32(ctxm->instance_bmap);
9181 	else
9182 		instance_bmap = 1;
9183 
9184 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9185 	if (rc)
9186 		return rc;
9187 	hwrm_req_hold(bp, req);
9188 	req->type = cpu_to_le16(ctxm->type);
9189 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9190 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9191 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9192 		struct bnxt_bs_trace_info *bs_trace;
9193 		u32 enables;
9194 
9195 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9196 		req->enables = cpu_to_le32(enables);
9197 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9198 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9199 	}
9200 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9201 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9202 		p[i] = cpu_to_le32(ctxm->split[i]);
9203 	for (i = 0, j = 0; j < n && !rc; i++) {
9204 		struct bnxt_ctx_pg_info *ctx_pg;
9205 
9206 		if (!(instance_bmap & (1 << i)))
9207 			continue;
9208 		req->instance = cpu_to_le16(i);
9209 		ctx_pg = &ctxm->pg_info[j++];
9210 		if (!ctx_pg->entries)
9211 			continue;
9212 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9213 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9214 				      &req->page_size_pbl_level,
9215 				      &req->page_dir);
9216 		if (last && j == n)
9217 			req->flags =
9218 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9219 		rc = hwrm_req_send(bp, req);
9220 	}
9221 	hwrm_req_drop(bp, req);
9222 	return rc;
9223 }
9224 
9225 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9226 {
9227 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9228 	struct bnxt_ctx_mem_type *ctxm;
9229 	u16 last_type = BNXT_CTX_INV;
9230 	int rc = 0;
9231 	u16 type;
9232 
9233 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9234 		ctxm = &ctx->ctx_arr[type];
9235 		if (!bnxt_bs_trace_avail(bp, type))
9236 			continue;
9237 		if (!ctxm->mem_valid) {
9238 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9239 						     ctxm->max_entries, 1);
9240 			if (rc) {
9241 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9242 					    type);
9243 				continue;
9244 			}
9245 			bnxt_bs_trace_init(bp, ctxm);
9246 		}
9247 		last_type = type;
9248 	}
9249 
9250 	if (last_type == BNXT_CTX_INV) {
9251 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9252 			ctxm = &ctx->ctx_arr[type];
9253 			if (ctxm->mem_valid)
9254 				last_type = type;
9255 		}
9256 		if (last_type == BNXT_CTX_INV)
9257 			return 0;
9258 	}
9259 	ctx->ctx_arr[last_type].last = 1;
9260 
9261 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9262 		ctxm = &ctx->ctx_arr[type];
9263 
9264 		if (!ctxm->mem_valid)
9265 			continue;
9266 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9267 		if (rc)
9268 			return rc;
9269 	}
9270 	return 0;
9271 }
9272 
9273 /**
9274  * __bnxt_copy_ctx_mem - copy host context memory
9275  * @bp: The driver context
9276  * @ctxm: The pointer to the context memory type
9277  * @buf: The destination buffer or NULL to just obtain the length
9278  * @offset: The buffer offset to copy the data to
9279  * @head: The head offset of context memory to copy from
9280  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9281  *
9282  * This function is called for debugging purposes to dump the host context
9283  * used by the chip.
9284  *
9285  * Return: Length of memory copied
9286  */
9287 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9288 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9289 				  size_t offset, size_t head, size_t tail)
9290 {
9291 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9292 	size_t len = 0, total_len = 0;
9293 	int i, n = 1;
9294 
9295 	if (!ctx_pg)
9296 		return 0;
9297 
9298 	if (ctxm->instance_bmap)
9299 		n = hweight32(ctxm->instance_bmap);
9300 	for (i = 0; i < n; i++) {
9301 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9302 					    tail);
9303 		offset += len;
9304 		total_len += len;
9305 	}
9306 	return total_len;
9307 }
9308 
9309 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9310 			 void *buf, size_t offset)
9311 {
9312 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9313 
9314 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9315 }
9316 
9317 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9318 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9319 {
9320 	struct bnxt_ctx_pg_info *ctx_pg;
9321 	int i, n = 1;
9322 
9323 	ctxm->last = 0;
9324 
9325 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9326 		return;
9327 
9328 	ctx_pg = ctxm->pg_info;
9329 	if (ctx_pg) {
9330 		if (ctxm->instance_bmap)
9331 			n = hweight32(ctxm->instance_bmap);
9332 		for (i = 0; i < n; i++)
9333 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9334 
9335 		kfree(ctx_pg);
9336 		ctxm->pg_info = NULL;
9337 		ctxm->mem_valid = 0;
9338 	}
9339 	memset(ctxm, 0, sizeof(*ctxm));
9340 }
9341 
9342 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9343 {
9344 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9345 	u16 type;
9346 
9347 	if (!ctx)
9348 		return;
9349 
9350 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9351 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9352 
9353 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9354 	if (force) {
9355 		kfree(ctx);
9356 		bp->ctx = NULL;
9357 	}
9358 }
9359 
9360 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9361 {
9362 	struct bnxt_ctx_mem_type *ctxm;
9363 	struct bnxt_ctx_mem_info *ctx;
9364 	u32 l2_qps, qp1_qps, max_qps;
9365 	u32 ena, entries_sp, entries;
9366 	u32 srqs, max_srqs, min;
9367 	u32 num_mr, num_ah;
9368 	u32 extra_srqs = 0;
9369 	u32 extra_qps = 0;
9370 	u32 fast_qpmd_qps;
9371 	u8 pg_lvl = 1;
9372 	int i, rc;
9373 
9374 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9375 	if (rc) {
9376 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9377 			   rc);
9378 		return rc;
9379 	}
9380 	ctx = bp->ctx;
9381 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9382 		return 0;
9383 
9384 	ena = 0;
9385 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9386 		goto skip_legacy;
9387 
9388 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9389 	l2_qps = ctxm->qp_l2_entries;
9390 	qp1_qps = ctxm->qp_qp1_entries;
9391 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9392 	max_qps = ctxm->max_entries;
9393 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9394 	srqs = ctxm->srq_l2_entries;
9395 	max_srqs = ctxm->max_entries;
9396 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9397 		pg_lvl = 2;
9398 		if (BNXT_SW_RES_LMT(bp)) {
9399 			extra_qps = max_qps - l2_qps - qp1_qps;
9400 			extra_srqs = max_srqs - srqs;
9401 		} else {
9402 			extra_qps = min_t(u32, 65536,
9403 					  max_qps - l2_qps - qp1_qps);
9404 			/* allocate extra qps if fw supports RoCE fast qp
9405 			 * destroy feature
9406 			 */
9407 			extra_qps += fast_qpmd_qps;
9408 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9409 		}
9410 		if (fast_qpmd_qps)
9411 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9412 	}
9413 
9414 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9415 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9416 				     pg_lvl);
9417 	if (rc)
9418 		return rc;
9419 
9420 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9421 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9422 	if (rc)
9423 		return rc;
9424 
9425 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9426 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9427 				     extra_qps * 2, pg_lvl);
9428 	if (rc)
9429 		return rc;
9430 
9431 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9432 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9433 	if (rc)
9434 		return rc;
9435 
9436 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9437 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9438 	if (rc)
9439 		return rc;
9440 
9441 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9442 		goto skip_rdma;
9443 
9444 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9445 	if (BNXT_SW_RES_LMT(bp) &&
9446 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9447 		num_ah = ctxm->mrav_av_entries;
9448 		num_mr = ctxm->max_entries - num_ah;
9449 	} else {
9450 		/* 128K extra is needed to accommodate static AH context
9451 		 * allocation by f/w.
9452 		 */
9453 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9454 		num_ah = min_t(u32, num_mr, 1024 * 128);
9455 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9456 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9457 			ctxm->mrav_av_entries = num_ah;
9458 	}
9459 
9460 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9461 	if (rc)
9462 		return rc;
9463 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9464 
9465 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9466 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9467 	if (rc)
9468 		return rc;
9469 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9470 
9471 skip_rdma:
9472 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9473 	min = ctxm->min_entries;
9474 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9475 		     2 * (extra_qps + qp1_qps) + min;
9476 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9477 	if (rc)
9478 		return rc;
9479 
9480 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9481 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9482 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9483 	if (rc)
9484 		return rc;
9485 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9486 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9487 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9488 
9489 skip_legacy:
9490 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9491 		rc = bnxt_backing_store_cfg_v2(bp);
9492 	else
9493 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9494 	if (rc) {
9495 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9496 			   rc);
9497 		return rc;
9498 	}
9499 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9500 	return 0;
9501 }
9502 
9503 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9504 {
9505 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9506 	u16 page_attr;
9507 	int rc;
9508 
9509 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9510 		return 0;
9511 
9512 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9513 	if (rc)
9514 		return rc;
9515 
9516 	if (BNXT_PAGE_SIZE == 0x2000)
9517 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9518 	else if (BNXT_PAGE_SIZE == 0x10000)
9519 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9520 	else
9521 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9522 	req->pg_size_lvl = cpu_to_le16(page_attr |
9523 				       bp->fw_crash_mem->ring_mem.depth);
9524 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9525 	req->size = cpu_to_le32(bp->fw_crash_len);
9526 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9527 	return hwrm_req_send(bp, req);
9528 }
9529 
9530 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9531 {
9532 	if (bp->fw_crash_mem) {
9533 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9534 		kfree(bp->fw_crash_mem);
9535 		bp->fw_crash_mem = NULL;
9536 	}
9537 }
9538 
9539 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9540 {
9541 	u32 mem_size = 0;
9542 	int rc;
9543 
9544 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9545 		return 0;
9546 
9547 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9548 	if (rc)
9549 		return rc;
9550 
9551 	mem_size = round_up(mem_size, 4);
9552 
9553 	/* keep and use the existing pages */
9554 	if (bp->fw_crash_mem &&
9555 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9556 		goto alloc_done;
9557 
9558 	if (bp->fw_crash_mem)
9559 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9560 	else
9561 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9562 	if (!bp->fw_crash_mem)
9563 		return -ENOMEM;
9564 
9565 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9566 	if (rc) {
9567 		bnxt_free_crash_dump_mem(bp);
9568 		return rc;
9569 	}
9570 
9571 alloc_done:
9572 	bp->fw_crash_len = mem_size;
9573 	return 0;
9574 }
9575 
9576 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9577 {
9578 	struct hwrm_func_resource_qcaps_output *resp;
9579 	struct hwrm_func_resource_qcaps_input *req;
9580 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9581 	int rc;
9582 
9583 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9584 	if (rc)
9585 		return rc;
9586 
9587 	req->fid = cpu_to_le16(0xffff);
9588 	resp = hwrm_req_hold(bp, req);
9589 	rc = hwrm_req_send_silent(bp, req);
9590 	if (rc)
9591 		goto hwrm_func_resc_qcaps_exit;
9592 
9593 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9594 	if (!all)
9595 		goto hwrm_func_resc_qcaps_exit;
9596 
9597 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9598 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9599 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9600 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9601 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9602 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9603 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9604 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9605 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9606 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9607 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9608 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9609 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9610 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9611 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9612 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9613 
9614 	if (hw_resc->max_rsscos_ctxs >=
9615 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9616 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9617 
9618 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9619 		u16 max_msix = le16_to_cpu(resp->max_msix);
9620 
9621 		hw_resc->max_nqs = max_msix;
9622 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9623 	}
9624 
9625 	if (BNXT_PF(bp)) {
9626 		struct bnxt_pf_info *pf = &bp->pf;
9627 
9628 		pf->vf_resv_strategy =
9629 			le16_to_cpu(resp->vf_reservation_strategy);
9630 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9631 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9632 	}
9633 hwrm_func_resc_qcaps_exit:
9634 	hwrm_req_drop(bp, req);
9635 	return rc;
9636 }
9637 
9638 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9639 {
9640 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9641 	struct hwrm_port_mac_ptp_qcfg_input *req;
9642 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9643 	u8 flags;
9644 	int rc;
9645 
9646 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9647 		rc = -ENODEV;
9648 		goto no_ptp;
9649 	}
9650 
9651 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9652 	if (rc)
9653 		goto no_ptp;
9654 
9655 	req->port_id = cpu_to_le16(bp->pf.port_id);
9656 	resp = hwrm_req_hold(bp, req);
9657 	rc = hwrm_req_send(bp, req);
9658 	if (rc)
9659 		goto exit;
9660 
9661 	flags = resp->flags;
9662 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9663 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9664 		rc = -ENODEV;
9665 		goto exit;
9666 	}
9667 	if (!ptp) {
9668 		ptp = kzalloc_obj(*ptp);
9669 		if (!ptp) {
9670 			rc = -ENOMEM;
9671 			goto exit;
9672 		}
9673 		ptp->bp = bp;
9674 		bp->ptp_cfg = ptp;
9675 	}
9676 
9677 	if (flags &
9678 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9679 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9680 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9681 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9682 	} else if (BNXT_CHIP_P5(bp)) {
9683 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9684 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9685 	} else {
9686 		rc = -ENODEV;
9687 		goto exit;
9688 	}
9689 	ptp->rtc_configured =
9690 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9691 	rc = bnxt_ptp_init(bp);
9692 	if (rc)
9693 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9694 exit:
9695 	hwrm_req_drop(bp, req);
9696 	if (!rc)
9697 		return 0;
9698 
9699 no_ptp:
9700 	bnxt_ptp_clear(bp);
9701 	kfree(ptp);
9702 	bp->ptp_cfg = NULL;
9703 	return rc;
9704 }
9705 
9706 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9707 {
9708 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9709 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9710 	struct hwrm_func_qcaps_output *resp;
9711 	struct hwrm_func_qcaps_input *req;
9712 	int rc;
9713 
9714 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9715 	if (rc)
9716 		return rc;
9717 
9718 	req->fid = cpu_to_le16(0xffff);
9719 	resp = hwrm_req_hold(bp, req);
9720 	rc = hwrm_req_send(bp, req);
9721 	if (rc)
9722 		goto hwrm_func_qcaps_exit;
9723 
9724 	flags = le32_to_cpu(resp->flags);
9725 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9726 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9727 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9728 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9729 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9730 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9731 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9732 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9733 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9734 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9735 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9736 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9737 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9738 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9739 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9740 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9741 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9742 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9743 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9744 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9745 
9746 	flags_ext = le32_to_cpu(resp->flags_ext);
9747 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9748 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9749 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9750 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9751 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9752 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9753 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9754 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9755 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9756 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9757 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9758 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9759 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9760 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9761 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9762 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9763 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9764 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9765 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9766 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9767 
9768 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9769 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9770 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9771 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9772 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9773 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9774 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9775 	if (flags_ext2 &
9776 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9777 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9778 	if (BNXT_PF(bp) &&
9779 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9780 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9781 
9782 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9783 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9784 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9785 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9786 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9787 
9788 	bp->tx_push_thresh = 0;
9789 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9790 	    BNXT_FW_MAJ(bp) > 217)
9791 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9792 
9793 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9794 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9795 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9796 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9797 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9798 	if (!hw_resc->max_hw_ring_grps)
9799 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9800 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9801 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9802 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9803 
9804 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9805 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9806 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9807 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9808 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9809 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9810 
9811 	if (BNXT_PF(bp)) {
9812 		struct bnxt_pf_info *pf = &bp->pf;
9813 
9814 		pf->fw_fid = le16_to_cpu(resp->fid);
9815 		pf->port_id = le16_to_cpu(resp->port_id);
9816 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9817 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9818 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9819 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9820 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9821 			bp->flags |= BNXT_FLAG_WOL_CAP;
9822 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9823 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9824 		} else {
9825 			bnxt_ptp_clear(bp);
9826 			kfree(bp->ptp_cfg);
9827 			bp->ptp_cfg = NULL;
9828 		}
9829 	} else {
9830 #ifdef CONFIG_BNXT_SRIOV
9831 		struct bnxt_vf_info *vf = &bp->vf;
9832 
9833 		vf->fw_fid = le16_to_cpu(resp->fid);
9834 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9835 #endif
9836 	}
9837 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9838 
9839 hwrm_func_qcaps_exit:
9840 	hwrm_req_drop(bp, req);
9841 	return rc;
9842 }
9843 
9844 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9845 {
9846 	struct hwrm_dbg_qcaps_output *resp;
9847 	struct hwrm_dbg_qcaps_input *req;
9848 	int rc;
9849 
9850 	bp->fw_dbg_cap = 0;
9851 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9852 		return;
9853 
9854 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9855 	if (rc)
9856 		return;
9857 
9858 	req->fid = cpu_to_le16(0xffff);
9859 	resp = hwrm_req_hold(bp, req);
9860 	rc = hwrm_req_send(bp, req);
9861 	if (rc)
9862 		goto hwrm_dbg_qcaps_exit;
9863 
9864 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9865 
9866 hwrm_dbg_qcaps_exit:
9867 	hwrm_req_drop(bp, req);
9868 }
9869 
9870 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9871 
9872 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9873 {
9874 	int rc;
9875 
9876 	rc = __bnxt_hwrm_func_qcaps(bp);
9877 	if (rc)
9878 		return rc;
9879 
9880 	bnxt_hwrm_dbg_qcaps(bp);
9881 
9882 	rc = bnxt_hwrm_queue_qportcfg(bp);
9883 	if (rc) {
9884 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9885 		return rc;
9886 	}
9887 	if (bp->hwrm_spec_code >= 0x10803) {
9888 		rc = bnxt_alloc_ctx_mem(bp);
9889 		if (rc)
9890 			return rc;
9891 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9892 		if (!rc)
9893 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9894 	}
9895 	return 0;
9896 }
9897 
9898 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9899 {
9900 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9901 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9902 	u32 flags;
9903 	int rc;
9904 
9905 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9906 		return 0;
9907 
9908 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9909 	if (rc)
9910 		return rc;
9911 
9912 	resp = hwrm_req_hold(bp, req);
9913 	rc = hwrm_req_send(bp, req);
9914 	if (rc)
9915 		goto hwrm_cfa_adv_qcaps_exit;
9916 
9917 	flags = le32_to_cpu(resp->flags);
9918 	if (flags &
9919 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9920 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9921 
9922 	if (flags &
9923 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9924 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9925 
9926 	if (flags &
9927 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9928 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9929 
9930 hwrm_cfa_adv_qcaps_exit:
9931 	hwrm_req_drop(bp, req);
9932 	return rc;
9933 }
9934 
9935 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9936 {
9937 	if (bp->fw_health)
9938 		return 0;
9939 
9940 	bp->fw_health = kzalloc_obj(*bp->fw_health);
9941 	if (!bp->fw_health)
9942 		return -ENOMEM;
9943 
9944 	mutex_init(&bp->fw_health->lock);
9945 	return 0;
9946 }
9947 
9948 static int bnxt_alloc_fw_health(struct bnxt *bp)
9949 {
9950 	int rc;
9951 
9952 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9953 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9954 		return 0;
9955 
9956 	rc = __bnxt_alloc_fw_health(bp);
9957 	if (rc) {
9958 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9959 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9960 		return rc;
9961 	}
9962 
9963 	return 0;
9964 }
9965 
9966 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9967 {
9968 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9969 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9970 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9971 }
9972 
9973 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9974 {
9975 	struct bnxt_fw_health *fw_health = bp->fw_health;
9976 	u32 reg_type;
9977 
9978 	if (!fw_health)
9979 		return;
9980 
9981 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9982 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9983 		fw_health->status_reliable = false;
9984 
9985 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9986 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9987 		fw_health->resets_reliable = false;
9988 }
9989 
9990 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9991 {
9992 	void __iomem *hs;
9993 	u32 status_loc;
9994 	u32 reg_type;
9995 	u32 sig;
9996 
9997 	if (bp->fw_health)
9998 		bp->fw_health->status_reliable = false;
9999 
10000 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10001 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10002 
10003 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10004 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10005 		if (!bp->chip_num) {
10006 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10007 			bp->chip_num = readl(bp->bar0 +
10008 					     BNXT_FW_HEALTH_WIN_BASE +
10009 					     BNXT_GRC_REG_CHIP_NUM);
10010 		}
10011 		if (!BNXT_CHIP_P5_PLUS(bp))
10012 			return;
10013 
10014 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10015 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10016 	} else {
10017 		status_loc = readl(hs + offsetof(struct hcomm_status,
10018 						 fw_status_loc));
10019 	}
10020 
10021 	if (__bnxt_alloc_fw_health(bp)) {
10022 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10023 		return;
10024 	}
10025 
10026 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10027 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10028 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10029 		__bnxt_map_fw_health_reg(bp, status_loc);
10030 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10031 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10032 	}
10033 
10034 	bp->fw_health->status_reliable = true;
10035 }
10036 
10037 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10038 {
10039 	struct bnxt_fw_health *fw_health = bp->fw_health;
10040 	u32 reg_base = 0xffffffff;
10041 	int i;
10042 
10043 	bp->fw_health->status_reliable = false;
10044 	bp->fw_health->resets_reliable = false;
10045 	/* Only pre-map the monitoring GRC registers using window 3 */
10046 	for (i = 0; i < 4; i++) {
10047 		u32 reg = fw_health->regs[i];
10048 
10049 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10050 			continue;
10051 		if (reg_base == 0xffffffff)
10052 			reg_base = reg & BNXT_GRC_BASE_MASK;
10053 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10054 			return -ERANGE;
10055 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10056 	}
10057 	bp->fw_health->status_reliable = true;
10058 	bp->fw_health->resets_reliable = true;
10059 	if (reg_base == 0xffffffff)
10060 		return 0;
10061 
10062 	__bnxt_map_fw_health_reg(bp, reg_base);
10063 	return 0;
10064 }
10065 
10066 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10067 {
10068 	if (!bp->fw_health)
10069 		return;
10070 
10071 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10072 		bp->fw_health->status_reliable = true;
10073 		bp->fw_health->resets_reliable = true;
10074 	} else {
10075 		bnxt_try_map_fw_health_reg(bp);
10076 	}
10077 }
10078 
10079 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10080 {
10081 	struct bnxt_fw_health *fw_health = bp->fw_health;
10082 	struct hwrm_error_recovery_qcfg_output *resp;
10083 	struct hwrm_error_recovery_qcfg_input *req;
10084 	int rc, i;
10085 
10086 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10087 		return 0;
10088 
10089 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10090 	if (rc)
10091 		return rc;
10092 
10093 	resp = hwrm_req_hold(bp, req);
10094 	rc = hwrm_req_send(bp, req);
10095 	if (rc)
10096 		goto err_recovery_out;
10097 	fw_health->flags = le32_to_cpu(resp->flags);
10098 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10099 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10100 		rc = -EINVAL;
10101 		goto err_recovery_out;
10102 	}
10103 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10104 	fw_health->master_func_wait_dsecs =
10105 		le32_to_cpu(resp->master_func_wait_period);
10106 	fw_health->normal_func_wait_dsecs =
10107 		le32_to_cpu(resp->normal_func_wait_period);
10108 	fw_health->post_reset_wait_dsecs =
10109 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10110 	fw_health->post_reset_max_wait_dsecs =
10111 		le32_to_cpu(resp->max_bailout_time_after_reset);
10112 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10113 		le32_to_cpu(resp->fw_health_status_reg);
10114 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10115 		le32_to_cpu(resp->fw_heartbeat_reg);
10116 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10117 		le32_to_cpu(resp->fw_reset_cnt_reg);
10118 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10119 		le32_to_cpu(resp->reset_inprogress_reg);
10120 	fw_health->fw_reset_inprog_reg_mask =
10121 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10122 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10123 	if (fw_health->fw_reset_seq_cnt >= 16) {
10124 		rc = -EINVAL;
10125 		goto err_recovery_out;
10126 	}
10127 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10128 		fw_health->fw_reset_seq_regs[i] =
10129 			le32_to_cpu(resp->reset_reg[i]);
10130 		fw_health->fw_reset_seq_vals[i] =
10131 			le32_to_cpu(resp->reset_reg_val[i]);
10132 		fw_health->fw_reset_seq_delay_msec[i] =
10133 			resp->delay_after_reset[i];
10134 	}
10135 err_recovery_out:
10136 	hwrm_req_drop(bp, req);
10137 	if (!rc)
10138 		rc = bnxt_map_fw_health_regs(bp);
10139 	if (rc)
10140 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10141 	return rc;
10142 }
10143 
10144 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10145 {
10146 	struct hwrm_func_reset_input *req;
10147 	int rc;
10148 
10149 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10150 	if (rc)
10151 		return rc;
10152 
10153 	req->enables = 0;
10154 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10155 	return hwrm_req_send(bp, req);
10156 }
10157 
10158 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10159 {
10160 	struct hwrm_nvm_get_dev_info_output nvm_info;
10161 
10162 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10163 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10164 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10165 			 nvm_info.nvm_cfg_ver_upd);
10166 }
10167 
10168 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10169 {
10170 	struct hwrm_queue_qportcfg_output *resp;
10171 	struct hwrm_queue_qportcfg_input *req;
10172 	u8 i, j, *qptr;
10173 	bool no_rdma;
10174 	int rc = 0;
10175 
10176 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10177 	if (rc)
10178 		return rc;
10179 
10180 	resp = hwrm_req_hold(bp, req);
10181 	rc = hwrm_req_send(bp, req);
10182 	if (rc)
10183 		goto qportcfg_exit;
10184 
10185 	if (!resp->max_configurable_queues) {
10186 		rc = -EINVAL;
10187 		goto qportcfg_exit;
10188 	}
10189 	bp->max_tc = resp->max_configurable_queues;
10190 	bp->max_lltc = resp->max_configurable_lossless_queues;
10191 	if (bp->max_tc > BNXT_MAX_QUEUE)
10192 		bp->max_tc = BNXT_MAX_QUEUE;
10193 
10194 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10195 	qptr = &resp->queue_id0;
10196 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10197 		bp->q_info[j].queue_id = *qptr;
10198 		bp->q_ids[i] = *qptr++;
10199 		bp->q_info[j].queue_profile = *qptr++;
10200 		bp->tc_to_qidx[j] = j;
10201 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10202 		    (no_rdma && BNXT_PF(bp)))
10203 			j++;
10204 	}
10205 	bp->max_q = bp->max_tc;
10206 	bp->max_tc = max_t(u8, j, 1);
10207 
10208 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10209 		bp->max_tc = 1;
10210 
10211 	if (bp->max_lltc > bp->max_tc)
10212 		bp->max_lltc = bp->max_tc;
10213 
10214 qportcfg_exit:
10215 	hwrm_req_drop(bp, req);
10216 	return rc;
10217 }
10218 
10219 static int bnxt_hwrm_poll(struct bnxt *bp)
10220 {
10221 	struct hwrm_ver_get_input *req;
10222 	int rc;
10223 
10224 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10225 	if (rc)
10226 		return rc;
10227 
10228 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10229 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10230 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10231 
10232 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10233 	rc = hwrm_req_send(bp, req);
10234 	return rc;
10235 }
10236 
10237 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10238 {
10239 	struct hwrm_ver_get_output *resp;
10240 	struct hwrm_ver_get_input *req;
10241 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10242 	u32 dev_caps_cfg, hwrm_ver;
10243 	int rc, len, max_tmo_secs;
10244 
10245 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10246 	if (rc)
10247 		return rc;
10248 
10249 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10250 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10251 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10252 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10253 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10254 
10255 	resp = hwrm_req_hold(bp, req);
10256 	rc = hwrm_req_send(bp, req);
10257 	if (rc)
10258 		goto hwrm_ver_get_exit;
10259 
10260 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10261 
10262 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10263 			     resp->hwrm_intf_min_8b << 8 |
10264 			     resp->hwrm_intf_upd_8b;
10265 	if (resp->hwrm_intf_maj_8b < 1) {
10266 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10267 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10268 			    resp->hwrm_intf_upd_8b);
10269 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10270 	}
10271 
10272 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10273 			HWRM_VERSION_UPDATE;
10274 
10275 	if (bp->hwrm_spec_code > hwrm_ver)
10276 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10277 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10278 			 HWRM_VERSION_UPDATE);
10279 	else
10280 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10281 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10282 			 resp->hwrm_intf_upd_8b);
10283 
10284 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10285 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10286 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10287 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10288 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10289 		len = FW_VER_STR_LEN;
10290 	} else {
10291 		fw_maj = resp->hwrm_fw_maj_8b;
10292 		fw_min = resp->hwrm_fw_min_8b;
10293 		fw_bld = resp->hwrm_fw_bld_8b;
10294 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10295 		len = BC_HWRM_STR_LEN;
10296 	}
10297 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10298 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10299 		 fw_rsv);
10300 
10301 	if (strlen(resp->active_pkg_name)) {
10302 		int fw_ver_len = strlen(bp->fw_ver_str);
10303 
10304 		snprintf(bp->fw_ver_str + fw_ver_len,
10305 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10306 			 resp->active_pkg_name);
10307 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10308 	}
10309 
10310 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10311 	if (!bp->hwrm_cmd_timeout)
10312 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10313 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10314 	if (!bp->hwrm_cmd_max_timeout)
10315 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10316 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10317 #ifdef CONFIG_DETECT_HUNG_TASK
10318 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10319 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10320 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10321 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10322 	}
10323 #endif
10324 
10325 	if (resp->hwrm_intf_maj_8b >= 1) {
10326 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10327 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10328 	}
10329 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10330 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10331 
10332 	bp->chip_num = le16_to_cpu(resp->chip_num);
10333 	bp->chip_rev = resp->chip_rev;
10334 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10335 	    !resp->chip_metal)
10336 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10337 
10338 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10339 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10340 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10341 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10342 
10343 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10344 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10345 
10346 	if (dev_caps_cfg &
10347 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10348 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10349 
10350 	if (dev_caps_cfg &
10351 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10352 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10353 
10354 	if (dev_caps_cfg &
10355 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10356 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10357 
10358 hwrm_ver_get_exit:
10359 	hwrm_req_drop(bp, req);
10360 	return rc;
10361 }
10362 
10363 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10364 {
10365 	struct hwrm_fw_set_time_input *req;
10366 	struct tm tm;
10367 	time64_t now = ktime_get_real_seconds();
10368 	int rc;
10369 
10370 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10371 	    bp->hwrm_spec_code < 0x10400)
10372 		return -EOPNOTSUPP;
10373 
10374 	time64_to_tm(now, 0, &tm);
10375 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10376 	if (rc)
10377 		return rc;
10378 
10379 	req->year = cpu_to_le16(1900 + tm.tm_year);
10380 	req->month = 1 + tm.tm_mon;
10381 	req->day = tm.tm_mday;
10382 	req->hour = tm.tm_hour;
10383 	req->minute = tm.tm_min;
10384 	req->second = tm.tm_sec;
10385 	return hwrm_req_send(bp, req);
10386 }
10387 
10388 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10389 {
10390 	u64 sw_tmp;
10391 
10392 	hw &= mask;
10393 	sw_tmp = (*sw & ~mask) | hw;
10394 	if (hw < (*sw & mask))
10395 		sw_tmp += mask + 1;
10396 	WRITE_ONCE(*sw, sw_tmp);
10397 }
10398 
10399 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10400 				    int count, bool ignore_zero)
10401 {
10402 	int i;
10403 
10404 	for (i = 0; i < count; i++) {
10405 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10406 
10407 		if (ignore_zero && !hw)
10408 			continue;
10409 
10410 		if (masks[i] == -1ULL)
10411 			sw_stats[i] = hw;
10412 		else
10413 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10414 	}
10415 }
10416 
10417 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10418 {
10419 	if (!stats->hw_stats)
10420 		return;
10421 
10422 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10423 				stats->hw_masks, stats->len / 8, false);
10424 }
10425 
10426 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10427 {
10428 	struct bnxt_stats_mem *ring0_stats;
10429 	bool ignore_zero = false;
10430 	int i;
10431 
10432 	/* Chip bug.  Counter intermittently becomes 0. */
10433 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10434 		ignore_zero = true;
10435 
10436 	for (i = 0; i < bp->cp_nr_rings; i++) {
10437 		struct bnxt_napi *bnapi = bp->bnapi[i];
10438 		struct bnxt_cp_ring_info *cpr;
10439 		struct bnxt_stats_mem *stats;
10440 
10441 		cpr = &bnapi->cp_ring;
10442 		stats = &cpr->stats;
10443 		if (!i)
10444 			ring0_stats = stats;
10445 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10446 					ring0_stats->hw_masks,
10447 					ring0_stats->len / 8, ignore_zero);
10448 	}
10449 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10450 		struct bnxt_stats_mem *stats = &bp->port_stats;
10451 		__le64 *hw_stats = stats->hw_stats;
10452 		u64 *sw_stats = stats->sw_stats;
10453 		u64 *masks = stats->hw_masks;
10454 		int cnt;
10455 
10456 		cnt = sizeof(struct rx_port_stats) / 8;
10457 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10458 
10459 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10460 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10461 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10462 		cnt = sizeof(struct tx_port_stats) / 8;
10463 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10464 	}
10465 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10466 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10467 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10468 	}
10469 }
10470 
10471 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10472 {
10473 	struct hwrm_port_qstats_input *req;
10474 	struct bnxt_pf_info *pf = &bp->pf;
10475 	int rc;
10476 
10477 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10478 		return 0;
10479 
10480 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10481 		return -EOPNOTSUPP;
10482 
10483 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10484 	if (rc)
10485 		return rc;
10486 
10487 	req->flags = flags;
10488 	req->port_id = cpu_to_le16(pf->port_id);
10489 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10490 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10491 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10492 	return hwrm_req_send(bp, req);
10493 }
10494 
10495 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10496 {
10497 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10498 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10499 	struct hwrm_port_qstats_ext_output *resp_qs;
10500 	struct hwrm_port_qstats_ext_input *req_qs;
10501 	struct bnxt_pf_info *pf = &bp->pf;
10502 	u32 tx_stat_size;
10503 	int rc;
10504 
10505 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10506 		return 0;
10507 
10508 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10509 		return -EOPNOTSUPP;
10510 
10511 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10512 	if (rc)
10513 		return rc;
10514 
10515 	req_qs->flags = flags;
10516 	req_qs->port_id = cpu_to_le16(pf->port_id);
10517 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10518 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10519 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10520 		       sizeof(struct tx_port_stats_ext) : 0;
10521 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10522 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10523 	resp_qs = hwrm_req_hold(bp, req_qs);
10524 	rc = hwrm_req_send(bp, req_qs);
10525 	if (!rc) {
10526 		bp->fw_rx_stats_ext_size =
10527 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10528 		if (BNXT_FW_MAJ(bp) < 220 &&
10529 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10530 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10531 
10532 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10533 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10534 	} else {
10535 		bp->fw_rx_stats_ext_size = 0;
10536 		bp->fw_tx_stats_ext_size = 0;
10537 	}
10538 	hwrm_req_drop(bp, req_qs);
10539 
10540 	if (flags)
10541 		return rc;
10542 
10543 	if (bp->fw_tx_stats_ext_size <=
10544 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10545 		bp->pri2cos_valid = 0;
10546 		return rc;
10547 	}
10548 
10549 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10550 	if (rc)
10551 		return rc;
10552 
10553 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10554 
10555 	resp_qc = hwrm_req_hold(bp, req_qc);
10556 	rc = hwrm_req_send(bp, req_qc);
10557 	if (!rc) {
10558 		u8 *pri2cos;
10559 		int i, j;
10560 
10561 		pri2cos = &resp_qc->pri0_cos_queue_id;
10562 		for (i = 0; i < 8; i++) {
10563 			u8 queue_id = pri2cos[i];
10564 			u8 queue_idx;
10565 
10566 			/* Per port queue IDs start from 0, 10, 20, etc */
10567 			queue_idx = queue_id % 10;
10568 			if (queue_idx > BNXT_MAX_QUEUE) {
10569 				bp->pri2cos_valid = false;
10570 				hwrm_req_drop(bp, req_qc);
10571 				return rc;
10572 			}
10573 			for (j = 0; j < bp->max_q; j++) {
10574 				if (bp->q_ids[j] == queue_id)
10575 					bp->pri2cos_idx[i] = queue_idx;
10576 			}
10577 		}
10578 		bp->pri2cos_valid = true;
10579 	}
10580 	hwrm_req_drop(bp, req_qc);
10581 
10582 	return rc;
10583 }
10584 
10585 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10586 {
10587 	bnxt_hwrm_tunnel_dst_port_free(bp,
10588 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10589 	bnxt_hwrm_tunnel_dst_port_free(bp,
10590 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10591 }
10592 
10593 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10594 {
10595 	int rc, i;
10596 	u32 tpa_flags = 0;
10597 
10598 	if (set_tpa)
10599 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10600 	else if (BNXT_NO_FW_ACCESS(bp))
10601 		return 0;
10602 	for (i = 0; i < bp->nr_vnics; i++) {
10603 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10604 		if (rc) {
10605 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10606 				   i, rc);
10607 			return rc;
10608 		}
10609 	}
10610 	return 0;
10611 }
10612 
10613 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10614 {
10615 	int i;
10616 
10617 	for (i = 0; i < bp->nr_vnics; i++)
10618 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10619 }
10620 
10621 static void bnxt_clear_vnic(struct bnxt *bp)
10622 {
10623 	if (!bp->vnic_info)
10624 		return;
10625 
10626 	bnxt_hwrm_clear_vnic_filter(bp);
10627 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10628 		/* clear all RSS setting before free vnic ctx */
10629 		bnxt_hwrm_clear_vnic_rss(bp);
10630 		bnxt_hwrm_vnic_ctx_free(bp);
10631 	}
10632 	/* before free the vnic, undo the vnic tpa settings */
10633 	if (bp->flags & BNXT_FLAG_TPA)
10634 		bnxt_set_tpa(bp, false);
10635 	bnxt_hwrm_vnic_free(bp);
10636 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10637 		bnxt_hwrm_vnic_ctx_free(bp);
10638 }
10639 
10640 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10641 				    bool irq_re_init)
10642 {
10643 	bnxt_clear_vnic(bp);
10644 	bnxt_hwrm_ring_free(bp, close_path);
10645 	bnxt_hwrm_ring_grp_free(bp);
10646 	if (irq_re_init) {
10647 		bnxt_hwrm_stat_ctx_free(bp);
10648 		bnxt_hwrm_free_tunnel_ports(bp);
10649 	}
10650 }
10651 
10652 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10653 {
10654 	struct hwrm_func_cfg_input *req;
10655 	u8 evb_mode;
10656 	int rc;
10657 
10658 	if (br_mode == BRIDGE_MODE_VEB)
10659 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10660 	else if (br_mode == BRIDGE_MODE_VEPA)
10661 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10662 	else
10663 		return -EINVAL;
10664 
10665 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10666 	if (rc)
10667 		return rc;
10668 
10669 	req->fid = cpu_to_le16(0xffff);
10670 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10671 	req->evb_mode = evb_mode;
10672 	return hwrm_req_send(bp, req);
10673 }
10674 
10675 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10676 {
10677 	struct hwrm_func_cfg_input *req;
10678 	int rc;
10679 
10680 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10681 		return 0;
10682 
10683 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10684 	if (rc)
10685 		return rc;
10686 
10687 	req->fid = cpu_to_le16(0xffff);
10688 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10689 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10690 	if (size == 128)
10691 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10692 
10693 	return hwrm_req_send(bp, req);
10694 }
10695 
10696 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10697 {
10698 	int rc;
10699 
10700 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10701 		goto skip_rss_ctx;
10702 
10703 	/* allocate context for vnic */
10704 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10705 	if (rc) {
10706 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10707 			   vnic->vnic_id, rc);
10708 		goto vnic_setup_err;
10709 	}
10710 	bp->rsscos_nr_ctxs++;
10711 
10712 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10713 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10714 		if (rc) {
10715 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10716 				   vnic->vnic_id, rc);
10717 			goto vnic_setup_err;
10718 		}
10719 		bp->rsscos_nr_ctxs++;
10720 	}
10721 
10722 skip_rss_ctx:
10723 	/* configure default vnic, ring grp */
10724 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10725 	if (rc) {
10726 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10727 			   vnic->vnic_id, rc);
10728 		goto vnic_setup_err;
10729 	}
10730 
10731 	/* Enable RSS hashing on vnic */
10732 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10733 	if (rc) {
10734 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10735 			   vnic->vnic_id, rc);
10736 		goto vnic_setup_err;
10737 	}
10738 
10739 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10740 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10741 		if (rc) {
10742 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10743 				   vnic->vnic_id, rc);
10744 		}
10745 	}
10746 
10747 vnic_setup_err:
10748 	return rc;
10749 }
10750 
10751 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10752 			  u8 valid)
10753 {
10754 	struct hwrm_vnic_update_input *req;
10755 	int rc;
10756 
10757 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10758 	if (rc)
10759 		return rc;
10760 
10761 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10762 
10763 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10764 		req->mru = cpu_to_le16(vnic->mru);
10765 
10766 	req->enables = cpu_to_le32(valid);
10767 
10768 	return hwrm_req_send(bp, req);
10769 }
10770 
10771 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10772 {
10773 	int rc;
10774 
10775 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10776 	if (rc) {
10777 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10778 			   vnic->vnic_id, rc);
10779 		return rc;
10780 	}
10781 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10782 	if (rc)
10783 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10784 			   vnic->vnic_id, rc);
10785 	return rc;
10786 }
10787 
10788 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10789 {
10790 	int rc, i, nr_ctxs;
10791 
10792 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10793 	for (i = 0; i < nr_ctxs; i++) {
10794 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10795 		if (rc) {
10796 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10797 				   vnic->vnic_id, i, rc);
10798 			break;
10799 		}
10800 		bp->rsscos_nr_ctxs++;
10801 	}
10802 	if (i < nr_ctxs)
10803 		return -ENOMEM;
10804 
10805 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10806 	if (rc)
10807 		return rc;
10808 
10809 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10810 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10811 		if (rc) {
10812 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10813 				   vnic->vnic_id, rc);
10814 		}
10815 	}
10816 	return rc;
10817 }
10818 
10819 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10820 {
10821 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10822 		return __bnxt_setup_vnic_p5(bp, vnic);
10823 	else
10824 		return __bnxt_setup_vnic(bp, vnic);
10825 }
10826 
10827 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10828 				     struct bnxt_vnic_info *vnic,
10829 				     u16 start_rx_ring_idx, int rx_rings)
10830 {
10831 	int rc;
10832 
10833 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10834 	if (rc) {
10835 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10836 			   vnic->vnic_id, rc);
10837 		return rc;
10838 	}
10839 	return bnxt_setup_vnic(bp, vnic);
10840 }
10841 
10842 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10843 {
10844 	struct bnxt_vnic_info *vnic;
10845 	int i, rc = 0;
10846 
10847 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10848 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10849 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10850 	}
10851 
10852 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10853 		return 0;
10854 
10855 	for (i = 0; i < bp->rx_nr_rings; i++) {
10856 		u16 vnic_id = i + 1;
10857 		u16 ring_id = i;
10858 
10859 		if (vnic_id >= bp->nr_vnics)
10860 			break;
10861 
10862 		vnic = &bp->vnic_info[vnic_id];
10863 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10864 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10865 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10866 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10867 			break;
10868 	}
10869 	return rc;
10870 }
10871 
10872 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10873 			  bool all)
10874 {
10875 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10876 	struct bnxt_filter_base *usr_fltr, *tmp;
10877 	struct bnxt_ntuple_filter *ntp_fltr;
10878 	int i;
10879 
10880 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10881 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10882 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10883 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10884 	}
10885 	if (!all)
10886 		return;
10887 
10888 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10889 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10890 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10891 			ntp_fltr = container_of(usr_fltr,
10892 						struct bnxt_ntuple_filter,
10893 						base);
10894 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10895 			bnxt_del_ntp_filter(bp, ntp_fltr);
10896 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10897 		}
10898 	}
10899 
10900 	if (vnic->rss_table)
10901 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10902 				  vnic->rss_table,
10903 				  vnic->rss_table_dma_addr);
10904 	bp->num_rss_ctx--;
10905 }
10906 
10907 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10908 				  int rxr_id)
10909 {
10910 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10911 	int i, vnic_rx;
10912 
10913 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10914 	 * must be updated because a future filter may use it.
10915 	 */
10916 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10917 		return true;
10918 
10919 	for (i = 0; i < tbl_size; i++) {
10920 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10921 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10922 		else
10923 			vnic_rx = bp->rss_indir_tbl[i];
10924 
10925 		if (rxr_id == vnic_rx)
10926 			return true;
10927 	}
10928 
10929 	return false;
10930 }
10931 
10932 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10933 				u16 mru, int rxr_id)
10934 {
10935 	int rc;
10936 
10937 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10938 		return 0;
10939 
10940 	if (mru) {
10941 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10942 		if (rc) {
10943 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10944 				   vnic->vnic_id, rc);
10945 			return rc;
10946 		}
10947 	}
10948 	vnic->mru = mru;
10949 	bnxt_hwrm_vnic_update(bp, vnic,
10950 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10951 
10952 	return 0;
10953 }
10954 
10955 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10956 {
10957 	struct ethtool_rxfh_context *ctx;
10958 	unsigned long context;
10959 	int rc;
10960 
10961 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10962 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10963 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10964 
10965 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10966 		if (rc)
10967 			return rc;
10968 	}
10969 
10970 	return 0;
10971 }
10972 
10973 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10974 {
10975 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10976 	struct ethtool_rxfh_context *ctx;
10977 	unsigned long context;
10978 
10979 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10980 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10981 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10982 
10983 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10984 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10985 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10986 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10987 				   rss_ctx->index);
10988 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10989 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10990 		}
10991 	}
10992 }
10993 
10994 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10995 {
10996 	struct ethtool_rxfh_context *ctx;
10997 	unsigned long context;
10998 
10999 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11000 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11001 
11002 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11003 	}
11004 }
11005 
11006 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11007 static bool bnxt_promisc_ok(struct bnxt *bp)
11008 {
11009 #ifdef CONFIG_BNXT_SRIOV
11010 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11011 		return false;
11012 #endif
11013 	return true;
11014 }
11015 
11016 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11017 {
11018 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11019 	unsigned int rc = 0;
11020 
11021 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11022 	if (rc) {
11023 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11024 			   rc);
11025 		return rc;
11026 	}
11027 
11028 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11029 	if (rc) {
11030 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11031 			   rc);
11032 		return rc;
11033 	}
11034 	return rc;
11035 }
11036 
11037 static int bnxt_cfg_rx_mode(struct bnxt *);
11038 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
11039 
11040 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11041 {
11042 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11043 	int rc = 0;
11044 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11045 
11046 	if (irq_re_init) {
11047 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11048 		if (rc) {
11049 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11050 				   rc);
11051 			goto err_out;
11052 		}
11053 	}
11054 
11055 	rc = bnxt_hwrm_ring_alloc(bp);
11056 	if (rc) {
11057 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11058 		goto err_out;
11059 	}
11060 
11061 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11062 	if (rc) {
11063 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11064 		goto err_out;
11065 	}
11066 
11067 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11068 		rx_nr_rings--;
11069 
11070 	/* default vnic 0 */
11071 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11072 	if (rc) {
11073 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11074 		goto err_out;
11075 	}
11076 
11077 	if (BNXT_VF(bp))
11078 		bnxt_hwrm_func_qcfg(bp);
11079 
11080 	rc = bnxt_setup_vnic(bp, vnic);
11081 	if (rc)
11082 		goto err_out;
11083 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11084 		bnxt_hwrm_update_rss_hash_cfg(bp);
11085 
11086 	if (bp->flags & BNXT_FLAG_RFS) {
11087 		rc = bnxt_alloc_rfs_vnics(bp);
11088 		if (rc)
11089 			goto err_out;
11090 	}
11091 
11092 	if (bp->flags & BNXT_FLAG_TPA) {
11093 		rc = bnxt_set_tpa(bp, true);
11094 		if (rc)
11095 			goto err_out;
11096 	}
11097 
11098 	if (BNXT_VF(bp))
11099 		bnxt_update_vf_mac(bp);
11100 
11101 	/* Filter for default vnic 0 */
11102 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11103 	if (rc) {
11104 		if (BNXT_VF(bp) && rc == -ENODEV)
11105 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11106 		else
11107 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11108 		goto err_out;
11109 	}
11110 	vnic->uc_filter_count = 1;
11111 
11112 	vnic->rx_mask = 0;
11113 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11114 		goto skip_rx_mask;
11115 
11116 	if (bp->dev->flags & IFF_BROADCAST)
11117 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11118 
11119 	if (bp->dev->flags & IFF_PROMISC)
11120 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11121 
11122 	if (bp->dev->flags & IFF_ALLMULTI) {
11123 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11124 		vnic->mc_list_count = 0;
11125 	} else if (bp->dev->flags & IFF_MULTICAST) {
11126 		u32 mask = 0;
11127 
11128 		bnxt_mc_list_updated(bp, &mask);
11129 		vnic->rx_mask |= mask;
11130 	}
11131 
11132 	rc = bnxt_cfg_rx_mode(bp);
11133 	if (rc)
11134 		goto err_out;
11135 
11136 skip_rx_mask:
11137 	rc = bnxt_hwrm_set_coal(bp);
11138 	if (rc)
11139 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11140 				rc);
11141 
11142 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11143 		rc = bnxt_setup_nitroa0_vnic(bp);
11144 		if (rc)
11145 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11146 				   rc);
11147 	}
11148 
11149 	if (BNXT_VF(bp)) {
11150 		bnxt_hwrm_func_qcfg(bp);
11151 		netdev_update_features(bp->dev);
11152 	}
11153 
11154 	return 0;
11155 
11156 err_out:
11157 	bnxt_hwrm_resource_free(bp, 0, true);
11158 
11159 	return rc;
11160 }
11161 
11162 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11163 {
11164 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11165 	return 0;
11166 }
11167 
11168 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11169 {
11170 	bnxt_init_cp_rings(bp);
11171 	bnxt_init_rx_rings(bp);
11172 	bnxt_init_tx_rings(bp);
11173 	bnxt_init_ring_grps(bp, irq_re_init);
11174 	bnxt_init_vnics(bp);
11175 
11176 	return bnxt_init_chip(bp, irq_re_init);
11177 }
11178 
11179 static int bnxt_set_real_num_queues(struct bnxt *bp)
11180 {
11181 	int rc;
11182 	struct net_device *dev = bp->dev;
11183 
11184 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11185 					  bp->tx_nr_rings_xdp);
11186 	if (rc)
11187 		return rc;
11188 
11189 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11190 	if (rc)
11191 		return rc;
11192 
11193 #ifdef CONFIG_RFS_ACCEL
11194 	if (bp->flags & BNXT_FLAG_RFS)
11195 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11196 #endif
11197 
11198 	return rc;
11199 }
11200 
11201 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11202 			     bool shared)
11203 {
11204 	int _rx = *rx, _tx = *tx;
11205 
11206 	if (shared) {
11207 		*rx = min_t(int, _rx, max);
11208 		*tx = min_t(int, _tx, max);
11209 	} else {
11210 		if (max < 2)
11211 			return -ENOMEM;
11212 
11213 		while (_rx + _tx > max) {
11214 			if (_rx > _tx && _rx > 1)
11215 				_rx--;
11216 			else if (_tx > 1)
11217 				_tx--;
11218 		}
11219 		*rx = _rx;
11220 		*tx = _tx;
11221 	}
11222 	return 0;
11223 }
11224 
11225 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11226 {
11227 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11228 }
11229 
11230 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11231 {
11232 	int tcs = bp->num_tc;
11233 
11234 	if (!tcs)
11235 		tcs = 1;
11236 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11237 }
11238 
11239 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11240 {
11241 	int tcs = bp->num_tc;
11242 
11243 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11244 	       bp->tx_nr_rings_xdp;
11245 }
11246 
11247 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11248 			   bool sh)
11249 {
11250 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11251 
11252 	if (tx_cp != *tx) {
11253 		int tx_saved = tx_cp, rc;
11254 
11255 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11256 		if (rc)
11257 			return rc;
11258 		if (tx_cp != tx_saved)
11259 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11260 		return 0;
11261 	}
11262 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11263 }
11264 
11265 static void bnxt_setup_msix(struct bnxt *bp)
11266 {
11267 	const int len = sizeof(bp->irq_tbl[0].name);
11268 	struct net_device *dev = bp->dev;
11269 	int tcs, i;
11270 
11271 	tcs = bp->num_tc;
11272 	if (tcs) {
11273 		int i, off, count;
11274 
11275 		for (i = 0; i < tcs; i++) {
11276 			count = bp->tx_nr_rings_per_tc;
11277 			off = BNXT_TC_TO_RING_BASE(bp, i);
11278 			netdev_set_tc_queue(dev, i, count, off);
11279 		}
11280 	}
11281 
11282 	for (i = 0; i < bp->cp_nr_rings; i++) {
11283 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11284 		char *attr;
11285 
11286 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11287 			attr = "TxRx";
11288 		else if (i < bp->rx_nr_rings)
11289 			attr = "rx";
11290 		else
11291 			attr = "tx";
11292 
11293 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11294 			 attr, i);
11295 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11296 	}
11297 }
11298 
11299 static int bnxt_init_int_mode(struct bnxt *bp);
11300 
11301 static int bnxt_change_msix(struct bnxt *bp, int total)
11302 {
11303 	struct msi_map map;
11304 	int i;
11305 
11306 	/* add MSIX to the end if needed */
11307 	for (i = bp->total_irqs; i < total; i++) {
11308 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11309 		if (map.index < 0)
11310 			return bp->total_irqs;
11311 		bp->irq_tbl[i].vector = map.virq;
11312 		bp->total_irqs++;
11313 	}
11314 
11315 	/* trim MSIX from the end if needed */
11316 	for (i = bp->total_irqs; i > total; i--) {
11317 		map.index = i - 1;
11318 		map.virq = bp->irq_tbl[i - 1].vector;
11319 		pci_msix_free_irq(bp->pdev, map);
11320 		bp->total_irqs--;
11321 	}
11322 	return bp->total_irqs;
11323 }
11324 
11325 static int bnxt_setup_int_mode(struct bnxt *bp)
11326 {
11327 	int rc;
11328 
11329 	if (!bp->irq_tbl) {
11330 		rc = bnxt_init_int_mode(bp);
11331 		if (rc || !bp->irq_tbl)
11332 			return rc ?: -ENODEV;
11333 	}
11334 
11335 	bnxt_setup_msix(bp);
11336 
11337 	rc = bnxt_set_real_num_queues(bp);
11338 	return rc;
11339 }
11340 
11341 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11342 {
11343 	return bp->hw_resc.max_rsscos_ctxs;
11344 }
11345 
11346 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11347 {
11348 	return bp->hw_resc.max_vnics;
11349 }
11350 
11351 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11352 {
11353 	return bp->hw_resc.max_stat_ctxs;
11354 }
11355 
11356 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11357 {
11358 	return bp->hw_resc.max_cp_rings;
11359 }
11360 
11361 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11362 {
11363 	unsigned int cp = bp->hw_resc.max_cp_rings;
11364 
11365 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11366 		cp -= bnxt_get_ulp_msix_num(bp);
11367 
11368 	return cp;
11369 }
11370 
11371 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11372 {
11373 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11374 
11375 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11376 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11377 
11378 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11379 }
11380 
11381 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11382 {
11383 	bp->hw_resc.max_irqs = max_irqs;
11384 }
11385 
11386 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11387 {
11388 	unsigned int cp;
11389 
11390 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11391 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11392 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11393 	else
11394 		return cp - bp->cp_nr_rings;
11395 }
11396 
11397 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11398 {
11399 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11400 }
11401 
11402 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11403 {
11404 	int max_irq = bnxt_get_max_func_irqs(bp);
11405 	int total_req = bp->cp_nr_rings + num;
11406 
11407 	if (max_irq < total_req) {
11408 		num = max_irq - bp->cp_nr_rings;
11409 		if (num <= 0)
11410 			return 0;
11411 	}
11412 	return num;
11413 }
11414 
11415 static int bnxt_get_num_msix(struct bnxt *bp)
11416 {
11417 	if (!BNXT_NEW_RM(bp))
11418 		return bnxt_get_max_func_irqs(bp);
11419 
11420 	return bnxt_nq_rings_in_use(bp);
11421 }
11422 
11423 static int bnxt_init_int_mode(struct bnxt *bp)
11424 {
11425 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11426 
11427 	total_vecs = bnxt_get_num_msix(bp);
11428 	max = bnxt_get_max_func_irqs(bp);
11429 	if (total_vecs > max)
11430 		total_vecs = max;
11431 
11432 	if (!total_vecs)
11433 		return 0;
11434 
11435 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11436 		min = 2;
11437 
11438 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11439 					   PCI_IRQ_MSIX);
11440 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11441 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11442 		rc = -ENODEV;
11443 		goto msix_setup_exit;
11444 	}
11445 
11446 	tbl_size = total_vecs;
11447 	if (pci_msix_can_alloc_dyn(bp->pdev))
11448 		tbl_size = max;
11449 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11450 	if (bp->irq_tbl) {
11451 		for (i = 0; i < total_vecs; i++)
11452 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11453 
11454 		bp->total_irqs = total_vecs;
11455 		/* Trim rings based upon num of vectors allocated */
11456 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11457 				     total_vecs - ulp_msix, min == 1);
11458 		if (rc)
11459 			goto msix_setup_exit;
11460 
11461 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11462 		bp->cp_nr_rings = (min == 1) ?
11463 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11464 				  tx_cp + bp->rx_nr_rings;
11465 
11466 	} else {
11467 		rc = -ENOMEM;
11468 		goto msix_setup_exit;
11469 	}
11470 	return 0;
11471 
11472 msix_setup_exit:
11473 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11474 	kfree(bp->irq_tbl);
11475 	bp->irq_tbl = NULL;
11476 	pci_free_irq_vectors(bp->pdev);
11477 	return rc;
11478 }
11479 
11480 static void bnxt_clear_int_mode(struct bnxt *bp)
11481 {
11482 	pci_free_irq_vectors(bp->pdev);
11483 
11484 	kfree(bp->irq_tbl);
11485 	bp->irq_tbl = NULL;
11486 }
11487 
11488 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11489 {
11490 	bool irq_cleared = false;
11491 	bool irq_change = false;
11492 	int tcs = bp->num_tc;
11493 	int irqs_required;
11494 	int rc;
11495 
11496 	if (!bnxt_need_reserve_rings(bp))
11497 		return 0;
11498 
11499 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11500 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11501 
11502 		if (ulp_msix > bp->ulp_num_msix_want)
11503 			ulp_msix = bp->ulp_num_msix_want;
11504 		irqs_required = ulp_msix + bp->cp_nr_rings;
11505 	} else {
11506 		irqs_required = bnxt_get_num_msix(bp);
11507 	}
11508 
11509 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11510 		irq_change = true;
11511 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11512 			bnxt_ulp_irq_stop(bp);
11513 			bnxt_clear_int_mode(bp);
11514 			irq_cleared = true;
11515 		}
11516 	}
11517 	rc = __bnxt_reserve_rings(bp);
11518 	if (irq_cleared) {
11519 		if (!rc)
11520 			rc = bnxt_init_int_mode(bp);
11521 		bnxt_ulp_irq_restart(bp, rc);
11522 	} else if (irq_change && !rc) {
11523 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11524 			rc = -ENOSPC;
11525 	}
11526 	if (rc) {
11527 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11528 		return rc;
11529 	}
11530 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11531 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11532 		netdev_err(bp->dev, "tx ring reservation failure\n");
11533 		netdev_reset_tc(bp->dev);
11534 		bp->num_tc = 0;
11535 		if (bp->tx_nr_rings_xdp)
11536 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11537 		else
11538 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11539 		return -ENOMEM;
11540 	}
11541 	return 0;
11542 }
11543 
11544 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11545 {
11546 	struct bnxt_tx_ring_info *txr;
11547 	struct netdev_queue *txq;
11548 	struct bnxt_napi *bnapi;
11549 	int i;
11550 
11551 	bnapi = bp->bnapi[idx];
11552 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11553 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11554 		synchronize_net();
11555 
11556 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11557 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11558 			if (txq) {
11559 				__netif_tx_lock_bh(txq);
11560 				netif_tx_stop_queue(txq);
11561 				__netif_tx_unlock_bh(txq);
11562 			}
11563 		}
11564 
11565 		if (!bp->tph_mode)
11566 			continue;
11567 
11568 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11569 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11570 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11571 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11572 	}
11573 }
11574 
11575 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11576 {
11577 	struct bnxt_tx_ring_info *txr;
11578 	struct netdev_queue *txq;
11579 	struct bnxt_napi *bnapi;
11580 	int rc, i;
11581 
11582 	bnapi = bp->bnapi[idx];
11583 	/* All rings have been reserved and previously allocated.
11584 	 * Reallocating with the same parameters should never fail.
11585 	 */
11586 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11587 		if (!bp->tph_mode)
11588 			goto start_tx;
11589 
11590 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11591 		if (rc)
11592 			return rc;
11593 
11594 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11595 		if (rc)
11596 			return rc;
11597 
11598 		txr->tx_prod = 0;
11599 		txr->tx_cons = 0;
11600 		txr->tx_hw_cons = 0;
11601 start_tx:
11602 		WRITE_ONCE(txr->dev_state, 0);
11603 		synchronize_net();
11604 
11605 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11606 			continue;
11607 
11608 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11609 		if (txq)
11610 			netif_tx_start_queue(txq);
11611 	}
11612 
11613 	return 0;
11614 }
11615 
11616 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11617 				     const cpumask_t *mask)
11618 {
11619 	struct bnxt_irq *irq;
11620 	u16 tag;
11621 	int err;
11622 
11623 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11624 
11625 	if (!irq->bp->tph_mode)
11626 		return;
11627 
11628 	cpumask_copy(irq->cpu_mask, mask);
11629 
11630 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11631 		return;
11632 
11633 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11634 				cpumask_first(irq->cpu_mask), &tag))
11635 		return;
11636 
11637 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11638 		return;
11639 
11640 	netdev_lock(irq->bp->dev);
11641 	if (netif_running(irq->bp->dev)) {
11642 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11643 		if (err)
11644 			netdev_err(irq->bp->dev,
11645 				   "RX queue restart failed: err=%d\n", err);
11646 	}
11647 	netdev_unlock(irq->bp->dev);
11648 }
11649 
11650 static void bnxt_irq_affinity_release(struct kref *ref)
11651 {
11652 	struct irq_affinity_notify *notify =
11653 		container_of(ref, struct irq_affinity_notify, kref);
11654 	struct bnxt_irq *irq;
11655 
11656 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11657 
11658 	if (!irq->bp->tph_mode)
11659 		return;
11660 
11661 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11662 		netdev_err(irq->bp->dev,
11663 			   "Setting ST=0 for MSIX entry %d failed\n",
11664 			   irq->msix_nr);
11665 		return;
11666 	}
11667 }
11668 
11669 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11670 {
11671 	irq_set_affinity_notifier(irq->vector, NULL);
11672 }
11673 
11674 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11675 {
11676 	struct irq_affinity_notify *notify;
11677 
11678 	irq->bp = bp;
11679 
11680 	/* Nothing to do if TPH is not enabled */
11681 	if (!bp->tph_mode)
11682 		return;
11683 
11684 	/* Register IRQ affinity notifier */
11685 	notify = &irq->affinity_notify;
11686 	notify->irq = irq->vector;
11687 	notify->notify = bnxt_irq_affinity_notify;
11688 	notify->release = bnxt_irq_affinity_release;
11689 
11690 	irq_set_affinity_notifier(irq->vector, notify);
11691 }
11692 
11693 static void bnxt_free_irq(struct bnxt *bp)
11694 {
11695 	struct bnxt_irq *irq;
11696 	int i;
11697 
11698 #ifdef CONFIG_RFS_ACCEL
11699 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11700 	bp->dev->rx_cpu_rmap = NULL;
11701 #endif
11702 	if (!bp->irq_tbl || !bp->bnapi)
11703 		return;
11704 
11705 	for (i = 0; i < bp->cp_nr_rings; i++) {
11706 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11707 
11708 		irq = &bp->irq_tbl[map_idx];
11709 		if (irq->requested) {
11710 			if (irq->have_cpumask) {
11711 				irq_update_affinity_hint(irq->vector, NULL);
11712 				free_cpumask_var(irq->cpu_mask);
11713 				irq->have_cpumask = 0;
11714 			}
11715 
11716 			bnxt_release_irq_notifier(irq);
11717 
11718 			free_irq(irq->vector, bp->bnapi[i]);
11719 		}
11720 
11721 		irq->requested = 0;
11722 	}
11723 
11724 	/* Disable TPH support */
11725 	pcie_disable_tph(bp->pdev);
11726 	bp->tph_mode = 0;
11727 }
11728 
11729 static int bnxt_request_irq(struct bnxt *bp)
11730 {
11731 	struct cpu_rmap *rmap = NULL;
11732 	int i, j, rc = 0;
11733 	unsigned long flags = 0;
11734 
11735 	rc = bnxt_setup_int_mode(bp);
11736 	if (rc) {
11737 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11738 			   rc);
11739 		return rc;
11740 	}
11741 #ifdef CONFIG_RFS_ACCEL
11742 	rmap = bp->dev->rx_cpu_rmap;
11743 #endif
11744 
11745 	/* Enable TPH support as part of IRQ request */
11746 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11747 	if (!rc)
11748 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11749 
11750 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11751 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11752 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11753 
11754 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11755 		    rmap && bp->bnapi[i]->rx_ring) {
11756 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11757 			if (rc)
11758 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11759 					    j);
11760 			j++;
11761 		}
11762 
11763 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11764 				 bp->bnapi[i]);
11765 		if (rc)
11766 			break;
11767 
11768 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11769 		irq->requested = 1;
11770 
11771 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11772 			int numa_node = dev_to_node(&bp->pdev->dev);
11773 			u16 tag;
11774 
11775 			irq->have_cpumask = 1;
11776 			irq->msix_nr = map_idx;
11777 			irq->ring_nr = i;
11778 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11779 					irq->cpu_mask);
11780 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11781 			if (rc) {
11782 				netdev_warn(bp->dev,
11783 					    "Update affinity hint failed, IRQ = %d\n",
11784 					    irq->vector);
11785 				break;
11786 			}
11787 
11788 			bnxt_register_irq_notifier(bp, irq);
11789 
11790 			/* Init ST table entry */
11791 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11792 						cpumask_first(irq->cpu_mask),
11793 						&tag))
11794 				continue;
11795 
11796 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11797 		}
11798 	}
11799 	return rc;
11800 }
11801 
11802 static void bnxt_del_napi(struct bnxt *bp)
11803 {
11804 	int i;
11805 
11806 	if (!bp->bnapi)
11807 		return;
11808 
11809 	for (i = 0; i < bp->rx_nr_rings; i++)
11810 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11811 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11812 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11813 
11814 	for (i = 0; i < bp->cp_nr_rings; i++) {
11815 		struct bnxt_napi *bnapi = bp->bnapi[i];
11816 
11817 		__netif_napi_del_locked(&bnapi->napi);
11818 	}
11819 	/* We called __netif_napi_del_locked(), we need
11820 	 * to respect an RCU grace period before freeing napi structures.
11821 	 */
11822 	synchronize_net();
11823 }
11824 
11825 static void bnxt_init_napi(struct bnxt *bp)
11826 {
11827 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11828 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11829 	struct bnxt_napi *bnapi;
11830 	int i;
11831 
11832 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11833 		poll_fn = bnxt_poll_p5;
11834 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11835 		cp_nr_rings--;
11836 
11837 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11838 
11839 	for (i = 0; i < cp_nr_rings; i++) {
11840 		bnapi = bp->bnapi[i];
11841 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11842 					     bnapi->index);
11843 	}
11844 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11845 		bnapi = bp->bnapi[cp_nr_rings];
11846 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11847 	}
11848 }
11849 
11850 static void bnxt_disable_napi(struct bnxt *bp)
11851 {
11852 	int i;
11853 
11854 	if (!bp->bnapi ||
11855 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11856 		return;
11857 
11858 	for (i = 0; i < bp->cp_nr_rings; i++) {
11859 		struct bnxt_napi *bnapi = bp->bnapi[i];
11860 		struct bnxt_cp_ring_info *cpr;
11861 
11862 		cpr = &bnapi->cp_ring;
11863 		if (bnapi->tx_fault)
11864 			cpr->sw_stats->tx.tx_resets++;
11865 		if (bnapi->in_reset)
11866 			cpr->sw_stats->rx.rx_resets++;
11867 		napi_disable_locked(&bnapi->napi);
11868 	}
11869 }
11870 
11871 static void bnxt_enable_napi(struct bnxt *bp)
11872 {
11873 	int i;
11874 
11875 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11876 	for (i = 0; i < bp->cp_nr_rings; i++) {
11877 		struct bnxt_napi *bnapi = bp->bnapi[i];
11878 		struct bnxt_cp_ring_info *cpr;
11879 
11880 		bnapi->tx_fault = 0;
11881 
11882 		cpr = &bnapi->cp_ring;
11883 		bnapi->in_reset = false;
11884 
11885 		if (bnapi->rx_ring) {
11886 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11887 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11888 		}
11889 		napi_enable_locked(&bnapi->napi);
11890 	}
11891 }
11892 
11893 void bnxt_tx_disable(struct bnxt *bp)
11894 {
11895 	int i;
11896 	struct bnxt_tx_ring_info *txr;
11897 
11898 	if (bp->tx_ring) {
11899 		for (i = 0; i < bp->tx_nr_rings; i++) {
11900 			txr = &bp->tx_ring[i];
11901 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11902 		}
11903 	}
11904 	/* Make sure napi polls see @dev_state change */
11905 	synchronize_net();
11906 	/* Drop carrier first to prevent TX timeout */
11907 	netif_carrier_off(bp->dev);
11908 	/* Stop all TX queues */
11909 	netif_tx_disable(bp->dev);
11910 }
11911 
11912 void bnxt_tx_enable(struct bnxt *bp)
11913 {
11914 	int i;
11915 	struct bnxt_tx_ring_info *txr;
11916 
11917 	for (i = 0; i < bp->tx_nr_rings; i++) {
11918 		txr = &bp->tx_ring[i];
11919 		WRITE_ONCE(txr->dev_state, 0);
11920 	}
11921 	/* Make sure napi polls see @dev_state change */
11922 	synchronize_net();
11923 	netif_tx_wake_all_queues(bp->dev);
11924 	if (BNXT_LINK_IS_UP(bp))
11925 		netif_carrier_on(bp->dev);
11926 }
11927 
11928 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11929 {
11930 	u8 active_fec = link_info->active_fec_sig_mode &
11931 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11932 
11933 	switch (active_fec) {
11934 	default:
11935 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11936 		return "None";
11937 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11938 		return "Clause 74 BaseR";
11939 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11940 		return "Clause 91 RS(528,514)";
11941 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11942 		return "Clause 91 RS544_1XN";
11943 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11944 		return "Clause 91 RS(544,514)";
11945 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11946 		return "Clause 91 RS272_1XN";
11947 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11948 		return "Clause 91 RS(272,257)";
11949 	}
11950 }
11951 
11952 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
11953 {
11954 	u8 reason = link_info->link_down_reason;
11955 
11956 	/* Multiple bits can be set, we report 1 bit only in order of
11957 	 * priority.
11958 	 */
11959 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
11960 		return "(Remote fault)";
11961 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
11962 		return "(OTP Speed limit violation)";
11963 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
11964 		return "(Cable removed)";
11965 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
11966 		return "(Module fault)";
11967 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
11968 		return "(BMC request down)";
11969 	return "";
11970 }
11971 
11972 void bnxt_report_link(struct bnxt *bp)
11973 {
11974 	if (BNXT_LINK_IS_UP(bp)) {
11975 		const char *signal = "";
11976 		const char *flow_ctrl;
11977 		const char *duplex;
11978 		u32 speed;
11979 		u16 fec;
11980 
11981 		netif_carrier_on(bp->dev);
11982 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11983 		if (speed == SPEED_UNKNOWN) {
11984 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11985 			return;
11986 		}
11987 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11988 			duplex = "full";
11989 		else
11990 			duplex = "half";
11991 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11992 			flow_ctrl = "ON - receive & transmit";
11993 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11994 			flow_ctrl = "ON - transmit";
11995 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11996 			flow_ctrl = "ON - receive";
11997 		else
11998 			flow_ctrl = "none";
11999 		if (bp->link_info.phy_qcfg_resp.option_flags &
12000 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12001 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12002 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12003 			switch (sig_mode) {
12004 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12005 				signal = "(NRZ) ";
12006 				break;
12007 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12008 				signal = "(PAM4 56Gbps) ";
12009 				break;
12010 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12011 				signal = "(PAM4 112Gbps) ";
12012 				break;
12013 			default:
12014 				break;
12015 			}
12016 		}
12017 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12018 			    speed, signal, duplex, flow_ctrl);
12019 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12020 			netdev_info(bp->dev, "EEE is %s\n",
12021 				    bp->eee.eee_active ? "active" :
12022 							 "not active");
12023 		fec = bp->link_info.fec_cfg;
12024 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12025 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12026 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12027 				    bnxt_report_fec(&bp->link_info));
12028 	} else {
12029 		char *str = bnxt_link_down_reason(&bp->link_info);
12030 
12031 		netif_carrier_off(bp->dev);
12032 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12033 	}
12034 }
12035 
12036 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12037 {
12038 	if (!resp->supported_speeds_auto_mode &&
12039 	    !resp->supported_speeds_force_mode &&
12040 	    !resp->supported_pam4_speeds_auto_mode &&
12041 	    !resp->supported_pam4_speeds_force_mode &&
12042 	    !resp->supported_speeds2_auto_mode &&
12043 	    !resp->supported_speeds2_force_mode)
12044 		return true;
12045 	return false;
12046 }
12047 
12048 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12049 {
12050 	struct bnxt_link_info *link_info = &bp->link_info;
12051 	struct hwrm_port_phy_qcaps_output *resp;
12052 	struct hwrm_port_phy_qcaps_input *req;
12053 	int rc = 0;
12054 
12055 	if (bp->hwrm_spec_code < 0x10201)
12056 		return 0;
12057 
12058 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12059 	if (rc)
12060 		return rc;
12061 
12062 	resp = hwrm_req_hold(bp, req);
12063 	rc = hwrm_req_send(bp, req);
12064 	if (rc)
12065 		goto hwrm_phy_qcaps_exit;
12066 
12067 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12068 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12069 		struct ethtool_keee *eee = &bp->eee;
12070 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12071 
12072 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12073 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12074 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12075 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12076 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12077 	}
12078 
12079 	if (bp->hwrm_spec_code >= 0x10a01) {
12080 		if (bnxt_phy_qcaps_no_speed(resp)) {
12081 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12082 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12083 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12084 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12085 			netdev_info(bp->dev, "Ethernet link enabled\n");
12086 			/* Phy re-enabled, reprobe the speeds */
12087 			link_info->support_auto_speeds = 0;
12088 			link_info->support_pam4_auto_speeds = 0;
12089 			link_info->support_auto_speeds2 = 0;
12090 		}
12091 	}
12092 	if (resp->supported_speeds_auto_mode)
12093 		link_info->support_auto_speeds =
12094 			le16_to_cpu(resp->supported_speeds_auto_mode);
12095 	if (resp->supported_pam4_speeds_auto_mode)
12096 		link_info->support_pam4_auto_speeds =
12097 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12098 	if (resp->supported_speeds2_auto_mode)
12099 		link_info->support_auto_speeds2 =
12100 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12101 
12102 	bp->port_count = resp->port_cnt;
12103 
12104 hwrm_phy_qcaps_exit:
12105 	hwrm_req_drop(bp, req);
12106 	return rc;
12107 }
12108 
12109 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12110 {
12111 	struct hwrm_port_mac_qcaps_output *resp;
12112 	struct hwrm_port_mac_qcaps_input *req;
12113 	int rc;
12114 
12115 	if (bp->hwrm_spec_code < 0x10a03)
12116 		return;
12117 
12118 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12119 	if (rc)
12120 		return;
12121 
12122 	resp = hwrm_req_hold(bp, req);
12123 	rc = hwrm_req_send_silent(bp, req);
12124 	if (!rc)
12125 		bp->mac_flags = resp->flags;
12126 	hwrm_req_drop(bp, req);
12127 }
12128 
12129 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12130 {
12131 	u16 diff = advertising ^ supported;
12132 
12133 	return ((supported | diff) != supported);
12134 }
12135 
12136 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12137 {
12138 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12139 
12140 	/* Check if any advertised speeds are no longer supported. The caller
12141 	 * holds the link_lock mutex, so we can modify link_info settings.
12142 	 */
12143 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12144 		if (bnxt_support_dropped(link_info->advertising,
12145 					 link_info->support_auto_speeds2)) {
12146 			link_info->advertising = link_info->support_auto_speeds2;
12147 			return true;
12148 		}
12149 		return false;
12150 	}
12151 	if (bnxt_support_dropped(link_info->advertising,
12152 				 link_info->support_auto_speeds)) {
12153 		link_info->advertising = link_info->support_auto_speeds;
12154 		return true;
12155 	}
12156 	if (bnxt_support_dropped(link_info->advertising_pam4,
12157 				 link_info->support_pam4_auto_speeds)) {
12158 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12159 		return true;
12160 	}
12161 	return false;
12162 }
12163 
12164 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12165 {
12166 	struct bnxt_link_info *link_info = &bp->link_info;
12167 	struct hwrm_port_phy_qcfg_output *resp;
12168 	struct hwrm_port_phy_qcfg_input *req;
12169 	u8 link_state = link_info->link_state;
12170 	bool support_changed;
12171 	int rc;
12172 
12173 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12174 	if (rc)
12175 		return rc;
12176 
12177 	resp = hwrm_req_hold(bp, req);
12178 	rc = hwrm_req_send(bp, req);
12179 	if (rc) {
12180 		hwrm_req_drop(bp, req);
12181 		if (BNXT_VF(bp) && rc == -ENODEV) {
12182 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12183 			rc = 0;
12184 		}
12185 		return rc;
12186 	}
12187 
12188 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12189 	link_info->phy_link_status = resp->link;
12190 	link_info->duplex = resp->duplex_cfg;
12191 	if (bp->hwrm_spec_code >= 0x10800)
12192 		link_info->duplex = resp->duplex_state;
12193 	link_info->pause = resp->pause;
12194 	link_info->auto_mode = resp->auto_mode;
12195 	link_info->auto_pause_setting = resp->auto_pause;
12196 	link_info->lp_pause = resp->link_partner_adv_pause;
12197 	link_info->force_pause_setting = resp->force_pause;
12198 	link_info->duplex_setting = resp->duplex_cfg;
12199 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12200 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12201 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12202 			link_info->active_lanes = resp->active_lanes;
12203 	} else {
12204 		link_info->link_speed = 0;
12205 		link_info->active_lanes = 0;
12206 	}
12207 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12208 	link_info->force_pam4_link_speed =
12209 		le16_to_cpu(resp->force_pam4_link_speed);
12210 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12211 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12212 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12213 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12214 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12215 	link_info->auto_pam4_link_speeds =
12216 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12217 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12218 	link_info->lp_auto_link_speeds =
12219 		le16_to_cpu(resp->link_partner_adv_speeds);
12220 	link_info->lp_auto_pam4_link_speeds =
12221 		resp->link_partner_pam4_adv_speeds;
12222 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12223 	link_info->phy_ver[0] = resp->phy_maj;
12224 	link_info->phy_ver[1] = resp->phy_min;
12225 	link_info->phy_ver[2] = resp->phy_bld;
12226 	link_info->media_type = resp->media_type;
12227 	link_info->phy_type = resp->phy_type;
12228 	link_info->transceiver = resp->xcvr_pkg_type;
12229 	link_info->phy_addr = resp->eee_config_phy_addr &
12230 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12231 	link_info->module_status = resp->module_status;
12232 	link_info->link_down_reason = resp->link_down_reason;
12233 
12234 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12235 		struct ethtool_keee *eee = &bp->eee;
12236 		u16 fw_speeds;
12237 
12238 		eee->eee_active = 0;
12239 		if (resp->eee_config_phy_addr &
12240 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12241 			eee->eee_active = 1;
12242 			fw_speeds = le16_to_cpu(
12243 				resp->link_partner_adv_eee_link_speed_mask);
12244 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12245 		}
12246 
12247 		/* Pull initial EEE config */
12248 		if (!chng_link_state) {
12249 			if (resp->eee_config_phy_addr &
12250 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12251 				eee->eee_enabled = 1;
12252 
12253 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12254 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12255 
12256 			if (resp->eee_config_phy_addr &
12257 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12258 				__le32 tmr;
12259 
12260 				eee->tx_lpi_enabled = 1;
12261 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12262 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12263 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12264 			}
12265 		}
12266 	}
12267 
12268 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12269 	if (bp->hwrm_spec_code >= 0x10504) {
12270 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12271 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12272 	}
12273 	/* TODO: need to add more logic to report VF link */
12274 	if (chng_link_state) {
12275 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12276 			link_info->link_state = BNXT_LINK_STATE_UP;
12277 		else
12278 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12279 		if (link_state != link_info->link_state)
12280 			bnxt_report_link(bp);
12281 	} else {
12282 		/* always link down if not require to update link state */
12283 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12284 	}
12285 	hwrm_req_drop(bp, req);
12286 
12287 	if (!BNXT_PHY_CFG_ABLE(bp))
12288 		return 0;
12289 
12290 	support_changed = bnxt_support_speed_dropped(link_info);
12291 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12292 		bnxt_hwrm_set_link_setting(bp, true, false);
12293 	return 0;
12294 }
12295 
12296 static void bnxt_get_port_module_status(struct bnxt *bp)
12297 {
12298 	struct bnxt_link_info *link_info = &bp->link_info;
12299 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12300 	u8 module_status;
12301 
12302 	if (bnxt_update_link(bp, true))
12303 		return;
12304 
12305 	module_status = link_info->module_status;
12306 	switch (module_status) {
12307 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12308 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12309 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12310 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12311 			    bp->pf.port_id);
12312 		if (bp->hwrm_spec_code >= 0x10201) {
12313 			netdev_warn(bp->dev, "Module part number %s\n",
12314 				    resp->phy_vendor_partnumber);
12315 		}
12316 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12317 			netdev_warn(bp->dev, "TX is disabled\n");
12318 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12319 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12320 	}
12321 }
12322 
12323 static void
12324 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12325 {
12326 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12327 		if (bp->hwrm_spec_code >= 0x10201)
12328 			req->auto_pause =
12329 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12330 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12331 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12332 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12333 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12334 		req->enables |=
12335 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12336 	} else {
12337 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12338 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12339 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12340 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12341 		req->enables |=
12342 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12343 		if (bp->hwrm_spec_code >= 0x10201) {
12344 			req->auto_pause = req->force_pause;
12345 			req->enables |= cpu_to_le32(
12346 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12347 		}
12348 	}
12349 }
12350 
12351 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12352 {
12353 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12354 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12355 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12356 			req->enables |=
12357 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12358 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12359 		} else if (bp->link_info.advertising) {
12360 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12361 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12362 		}
12363 		if (bp->link_info.advertising_pam4) {
12364 			req->enables |=
12365 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12366 			req->auto_link_pam4_speed_mask =
12367 				cpu_to_le16(bp->link_info.advertising_pam4);
12368 		}
12369 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12370 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12371 	} else {
12372 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12373 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12374 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12375 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12376 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12377 				   (u32)bp->link_info.req_link_speed);
12378 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12379 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12380 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12381 		} else {
12382 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12383 		}
12384 	}
12385 
12386 	/* tell chimp that the setting takes effect immediately */
12387 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12388 }
12389 
12390 int bnxt_hwrm_set_pause(struct bnxt *bp)
12391 {
12392 	struct hwrm_port_phy_cfg_input *req;
12393 	int rc;
12394 
12395 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12396 	if (rc)
12397 		return rc;
12398 
12399 	bnxt_hwrm_set_pause_common(bp, req);
12400 
12401 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12402 	    bp->link_info.force_link_chng)
12403 		bnxt_hwrm_set_link_common(bp, req);
12404 
12405 	rc = hwrm_req_send(bp, req);
12406 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12407 		/* since changing of pause setting doesn't trigger any link
12408 		 * change event, the driver needs to update the current pause
12409 		 * result upon successfully return of the phy_cfg command
12410 		 */
12411 		bp->link_info.pause =
12412 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12413 		bp->link_info.auto_pause_setting = 0;
12414 		if (!bp->link_info.force_link_chng)
12415 			bnxt_report_link(bp);
12416 	}
12417 	bp->link_info.force_link_chng = false;
12418 	return rc;
12419 }
12420 
12421 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12422 			      struct hwrm_port_phy_cfg_input *req)
12423 {
12424 	struct ethtool_keee *eee = &bp->eee;
12425 
12426 	if (eee->eee_enabled) {
12427 		u16 eee_speeds;
12428 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12429 
12430 		if (eee->tx_lpi_enabled)
12431 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12432 		else
12433 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12434 
12435 		req->flags |= cpu_to_le32(flags);
12436 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12437 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12438 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12439 	} else {
12440 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12441 	}
12442 }
12443 
12444 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12445 {
12446 	struct hwrm_port_phy_cfg_input *req;
12447 	int rc;
12448 
12449 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12450 	if (rc)
12451 		return rc;
12452 
12453 	if (set_pause)
12454 		bnxt_hwrm_set_pause_common(bp, req);
12455 
12456 	bnxt_hwrm_set_link_common(bp, req);
12457 
12458 	if (set_eee)
12459 		bnxt_hwrm_set_eee(bp, req);
12460 	return hwrm_req_send(bp, req);
12461 }
12462 
12463 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12464 {
12465 	struct hwrm_port_phy_cfg_input *req;
12466 	int rc;
12467 
12468 	if (!BNXT_SINGLE_PF(bp))
12469 		return 0;
12470 
12471 	if (pci_num_vf(bp->pdev) &&
12472 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12473 		return 0;
12474 
12475 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12476 	if (rc)
12477 		return rc;
12478 
12479 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12480 	rc = hwrm_req_send(bp, req);
12481 	if (!rc) {
12482 		mutex_lock(&bp->link_lock);
12483 		/* Device is not obliged link down in certain scenarios, even
12484 		 * when forced. Setting the state unknown is consistent with
12485 		 * driver startup and will force link state to be reported
12486 		 * during subsequent open based on PORT_PHY_QCFG.
12487 		 */
12488 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12489 		mutex_unlock(&bp->link_lock);
12490 	}
12491 	return rc;
12492 }
12493 
12494 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12495 {
12496 #ifdef CONFIG_TEE_BNXT_FW
12497 	int rc = tee_bnxt_fw_load();
12498 
12499 	if (rc)
12500 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12501 
12502 	return rc;
12503 #else
12504 	netdev_err(bp->dev, "OP-TEE not supported\n");
12505 	return -ENODEV;
12506 #endif
12507 }
12508 
12509 static int bnxt_try_recover_fw(struct bnxt *bp)
12510 {
12511 	if (bp->fw_health && bp->fw_health->status_reliable) {
12512 		int retry = 0, rc;
12513 		u32 sts;
12514 
12515 		do {
12516 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12517 			rc = bnxt_hwrm_poll(bp);
12518 			if (!BNXT_FW_IS_BOOTING(sts) &&
12519 			    !BNXT_FW_IS_RECOVERING(sts))
12520 				break;
12521 			retry++;
12522 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12523 
12524 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12525 			netdev_err(bp->dev,
12526 				   "Firmware not responding, status: 0x%x\n",
12527 				   sts);
12528 			rc = -ENODEV;
12529 		}
12530 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12531 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12532 			return bnxt_fw_reset_via_optee(bp);
12533 		}
12534 		return rc;
12535 	}
12536 
12537 	return -ENODEV;
12538 }
12539 
12540 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12541 {
12542 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12543 
12544 	if (!BNXT_NEW_RM(bp))
12545 		return; /* no resource reservations required */
12546 
12547 	hw_resc->resv_cp_rings = 0;
12548 	hw_resc->resv_stat_ctxs = 0;
12549 	hw_resc->resv_irqs = 0;
12550 	hw_resc->resv_tx_rings = 0;
12551 	hw_resc->resv_rx_rings = 0;
12552 	hw_resc->resv_hw_ring_grps = 0;
12553 	hw_resc->resv_vnics = 0;
12554 	hw_resc->resv_rsscos_ctxs = 0;
12555 	if (!fw_reset) {
12556 		bp->tx_nr_rings = 0;
12557 		bp->rx_nr_rings = 0;
12558 	}
12559 }
12560 
12561 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12562 {
12563 	int rc;
12564 
12565 	if (!BNXT_NEW_RM(bp))
12566 		return 0; /* no resource reservations required */
12567 
12568 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12569 	if (rc)
12570 		netdev_err(bp->dev, "resc_qcaps failed\n");
12571 
12572 	bnxt_clear_reservations(bp, fw_reset);
12573 
12574 	return rc;
12575 }
12576 
12577 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12578 {
12579 	struct hwrm_func_drv_if_change_output *resp;
12580 	struct hwrm_func_drv_if_change_input *req;
12581 	bool resc_reinit = false;
12582 	bool caps_change = false;
12583 	int rc, retry = 0;
12584 	bool fw_reset;
12585 	u32 flags = 0;
12586 
12587 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12588 	bp->fw_reset_state = 0;
12589 
12590 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12591 		return 0;
12592 
12593 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12594 	if (rc)
12595 		return rc;
12596 
12597 	if (up)
12598 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12599 	resp = hwrm_req_hold(bp, req);
12600 
12601 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12602 	while (retry < BNXT_FW_IF_RETRY) {
12603 		rc = hwrm_req_send(bp, req);
12604 		if (rc != -EAGAIN)
12605 			break;
12606 
12607 		msleep(50);
12608 		retry++;
12609 	}
12610 
12611 	if (rc == -EAGAIN) {
12612 		hwrm_req_drop(bp, req);
12613 		return rc;
12614 	} else if (!rc) {
12615 		flags = le32_to_cpu(resp->flags);
12616 	} else if (up) {
12617 		rc = bnxt_try_recover_fw(bp);
12618 		fw_reset = true;
12619 	}
12620 	hwrm_req_drop(bp, req);
12621 	if (rc)
12622 		return rc;
12623 
12624 	if (!up) {
12625 		bnxt_inv_fw_health_reg(bp);
12626 		return 0;
12627 	}
12628 
12629 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12630 		resc_reinit = true;
12631 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12632 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12633 		fw_reset = true;
12634 	else
12635 		bnxt_remap_fw_health_regs(bp);
12636 
12637 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12638 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12639 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12640 		return -ENODEV;
12641 	}
12642 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12643 		caps_change = true;
12644 
12645 	if (resc_reinit || fw_reset || caps_change) {
12646 		if (fw_reset || caps_change) {
12647 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12648 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12649 				bnxt_ulp_irq_stop(bp);
12650 			bnxt_free_ctx_mem(bp, false);
12651 			bnxt_dcb_free(bp);
12652 			rc = bnxt_fw_init_one(bp);
12653 			if (rc) {
12654 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12655 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12656 				return rc;
12657 			}
12658 			/* IRQ will be initialized later in bnxt_request_irq()*/
12659 			bnxt_clear_int_mode(bp);
12660 		}
12661 		rc = bnxt_cancel_reservations(bp, fw_reset);
12662 	}
12663 	return rc;
12664 }
12665 
12666 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12667 {
12668 	struct hwrm_port_led_qcaps_output *resp;
12669 	struct hwrm_port_led_qcaps_input *req;
12670 	struct bnxt_pf_info *pf = &bp->pf;
12671 	int rc;
12672 
12673 	bp->num_leds = 0;
12674 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12675 		return 0;
12676 
12677 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12678 	if (rc)
12679 		return rc;
12680 
12681 	req->port_id = cpu_to_le16(pf->port_id);
12682 	resp = hwrm_req_hold(bp, req);
12683 	rc = hwrm_req_send(bp, req);
12684 	if (rc) {
12685 		hwrm_req_drop(bp, req);
12686 		return rc;
12687 	}
12688 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12689 		int i;
12690 
12691 		bp->num_leds = resp->num_leds;
12692 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12693 						 bp->num_leds);
12694 		for (i = 0; i < bp->num_leds; i++) {
12695 			struct bnxt_led_info *led = &bp->leds[i];
12696 			__le16 caps = led->led_state_caps;
12697 
12698 			if (!led->led_group_id ||
12699 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12700 				bp->num_leds = 0;
12701 				break;
12702 			}
12703 		}
12704 	}
12705 	hwrm_req_drop(bp, req);
12706 	return 0;
12707 }
12708 
12709 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12710 {
12711 	struct hwrm_wol_filter_alloc_output *resp;
12712 	struct hwrm_wol_filter_alloc_input *req;
12713 	int rc;
12714 
12715 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12716 	if (rc)
12717 		return rc;
12718 
12719 	req->port_id = cpu_to_le16(bp->pf.port_id);
12720 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12721 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12722 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12723 
12724 	resp = hwrm_req_hold(bp, req);
12725 	rc = hwrm_req_send(bp, req);
12726 	if (!rc)
12727 		bp->wol_filter_id = resp->wol_filter_id;
12728 	hwrm_req_drop(bp, req);
12729 	return rc;
12730 }
12731 
12732 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12733 {
12734 	struct hwrm_wol_filter_free_input *req;
12735 	int rc;
12736 
12737 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12738 	if (rc)
12739 		return rc;
12740 
12741 	req->port_id = cpu_to_le16(bp->pf.port_id);
12742 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12743 	req->wol_filter_id = bp->wol_filter_id;
12744 
12745 	return hwrm_req_send(bp, req);
12746 }
12747 
12748 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12749 {
12750 	struct hwrm_wol_filter_qcfg_output *resp;
12751 	struct hwrm_wol_filter_qcfg_input *req;
12752 	u16 next_handle = 0;
12753 	int rc;
12754 
12755 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12756 	if (rc)
12757 		return rc;
12758 
12759 	req->port_id = cpu_to_le16(bp->pf.port_id);
12760 	req->handle = cpu_to_le16(handle);
12761 	resp = hwrm_req_hold(bp, req);
12762 	rc = hwrm_req_send(bp, req);
12763 	if (!rc) {
12764 		next_handle = le16_to_cpu(resp->next_handle);
12765 		if (next_handle != 0) {
12766 			if (resp->wol_type ==
12767 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12768 				bp->wol = 1;
12769 				bp->wol_filter_id = resp->wol_filter_id;
12770 			}
12771 		}
12772 	}
12773 	hwrm_req_drop(bp, req);
12774 	return next_handle;
12775 }
12776 
12777 static void bnxt_get_wol_settings(struct bnxt *bp)
12778 {
12779 	u16 handle = 0;
12780 
12781 	bp->wol = 0;
12782 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12783 		return;
12784 
12785 	do {
12786 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12787 	} while (handle && handle != 0xffff);
12788 }
12789 
12790 static bool bnxt_eee_config_ok(struct bnxt *bp)
12791 {
12792 	struct ethtool_keee *eee = &bp->eee;
12793 	struct bnxt_link_info *link_info = &bp->link_info;
12794 
12795 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12796 		return true;
12797 
12798 	if (eee->eee_enabled) {
12799 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12800 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12801 
12802 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12803 
12804 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12805 			eee->eee_enabled = 0;
12806 			return false;
12807 		}
12808 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12809 			linkmode_and(eee->advertised, advertising,
12810 				     eee->supported);
12811 			return false;
12812 		}
12813 	}
12814 	return true;
12815 }
12816 
12817 static int bnxt_update_phy_setting(struct bnxt *bp)
12818 {
12819 	int rc;
12820 	bool update_link = false;
12821 	bool update_pause = false;
12822 	bool update_eee = false;
12823 	struct bnxt_link_info *link_info = &bp->link_info;
12824 
12825 	rc = bnxt_update_link(bp, true);
12826 	if (rc) {
12827 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12828 			   rc);
12829 		return rc;
12830 	}
12831 	if (!BNXT_SINGLE_PF(bp))
12832 		return 0;
12833 
12834 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12835 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12836 	    link_info->req_flow_ctrl)
12837 		update_pause = true;
12838 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12839 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12840 		update_pause = true;
12841 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12842 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12843 			update_link = true;
12844 		if (bnxt_force_speed_updated(link_info))
12845 			update_link = true;
12846 		if (link_info->req_duplex != link_info->duplex_setting)
12847 			update_link = true;
12848 	} else {
12849 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12850 			update_link = true;
12851 		if (bnxt_auto_speed_updated(link_info))
12852 			update_link = true;
12853 	}
12854 
12855 	/* The last close may have shutdown the link, so need to call
12856 	 * PHY_CFG to bring it back up.
12857 	 */
12858 	if (!BNXT_LINK_IS_UP(bp))
12859 		update_link = true;
12860 
12861 	if (!bnxt_eee_config_ok(bp))
12862 		update_eee = true;
12863 
12864 	if (update_link)
12865 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12866 	else if (update_pause)
12867 		rc = bnxt_hwrm_set_pause(bp);
12868 	if (rc) {
12869 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12870 			   rc);
12871 		return rc;
12872 	}
12873 
12874 	return rc;
12875 }
12876 
12877 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12878 
12879 static int bnxt_reinit_after_abort(struct bnxt *bp)
12880 {
12881 	int rc;
12882 
12883 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12884 		return -EBUSY;
12885 
12886 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12887 		return -ENODEV;
12888 
12889 	rc = bnxt_fw_init_one(bp);
12890 	if (!rc) {
12891 		bnxt_clear_int_mode(bp);
12892 		rc = bnxt_init_int_mode(bp);
12893 		if (!rc) {
12894 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12895 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12896 		}
12897 	}
12898 	return rc;
12899 }
12900 
12901 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12902 {
12903 	struct bnxt_ntuple_filter *ntp_fltr;
12904 	struct bnxt_l2_filter *l2_fltr;
12905 
12906 	if (list_empty(&fltr->list))
12907 		return;
12908 
12909 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12910 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12911 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12912 		atomic_inc(&l2_fltr->refcnt);
12913 		ntp_fltr->l2_fltr = l2_fltr;
12914 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12915 			bnxt_del_ntp_filter(bp, ntp_fltr);
12916 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12917 				   fltr->sw_id);
12918 		}
12919 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12920 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12921 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12922 			bnxt_del_l2_filter(bp, l2_fltr);
12923 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12924 				   fltr->sw_id);
12925 		}
12926 	}
12927 }
12928 
12929 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12930 {
12931 	struct bnxt_filter_base *usr_fltr, *tmp;
12932 
12933 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12934 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12935 }
12936 
12937 static int bnxt_set_xps_mapping(struct bnxt *bp)
12938 {
12939 	int numa_node = dev_to_node(&bp->pdev->dev);
12940 	unsigned int q_idx, map_idx, cpu, i;
12941 	const struct cpumask *cpu_mask_ptr;
12942 	int nr_cpus = num_online_cpus();
12943 	cpumask_t *q_map;
12944 	int rc = 0;
12945 
12946 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
12947 	if (!q_map)
12948 		return -ENOMEM;
12949 
12950 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12951 	 * Each TC has the same number of TX queues. The nth TX queue for each
12952 	 * TC will have the same CPU mask.
12953 	 */
12954 	for (i = 0; i < nr_cpus; i++) {
12955 		map_idx = i % bp->tx_nr_rings_per_tc;
12956 		cpu = cpumask_local_spread(i, numa_node);
12957 		cpu_mask_ptr = get_cpu_mask(cpu);
12958 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12959 	}
12960 
12961 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12962 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12963 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12964 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12965 		if (rc) {
12966 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12967 				    q_idx);
12968 			break;
12969 		}
12970 	}
12971 
12972 	kfree(q_map);
12973 
12974 	return rc;
12975 }
12976 
12977 static int bnxt_tx_nr_rings(struct bnxt *bp)
12978 {
12979 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
12980 			    bp->tx_nr_rings_per_tc;
12981 }
12982 
12983 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
12984 {
12985 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
12986 }
12987 
12988 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
12989 {
12990 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
12991 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
12992 }
12993 
12994 static void bnxt_adj_tx_rings(struct bnxt *bp)
12995 {
12996 	/* Make adjustments if reserved TX rings are less than requested */
12997 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
12998 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
12999 	if (bp->tx_nr_rings_xdp)
13000 		bnxt_set_xdp_tx_rings(bp);
13001 }
13002 
13003 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13004 {
13005 	int rc = 0;
13006 
13007 	netif_carrier_off(bp->dev);
13008 	if (irq_re_init) {
13009 		/* Reserve rings now if none were reserved at driver probe. */
13010 		rc = bnxt_init_dflt_ring_mode(bp);
13011 		if (rc) {
13012 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13013 			return rc;
13014 		}
13015 	}
13016 	rc = bnxt_reserve_rings(bp, irq_re_init);
13017 	if (rc)
13018 		return rc;
13019 
13020 	bnxt_adj_tx_rings(bp);
13021 	rc = bnxt_alloc_mem(bp, irq_re_init);
13022 	if (rc) {
13023 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13024 		goto open_err_free_mem;
13025 	}
13026 
13027 	if (irq_re_init) {
13028 		bnxt_init_napi(bp);
13029 		rc = bnxt_request_irq(bp);
13030 		if (rc) {
13031 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13032 			goto open_err_irq;
13033 		}
13034 	}
13035 
13036 	rc = bnxt_init_nic(bp, irq_re_init);
13037 	if (rc) {
13038 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13039 		goto open_err_irq;
13040 	}
13041 
13042 	bnxt_enable_napi(bp);
13043 	bnxt_debug_dev_init(bp);
13044 
13045 	if (link_re_init) {
13046 		mutex_lock(&bp->link_lock);
13047 		rc = bnxt_update_phy_setting(bp);
13048 		mutex_unlock(&bp->link_lock);
13049 		if (rc) {
13050 			netdev_warn(bp->dev, "failed to update phy settings\n");
13051 			if (BNXT_SINGLE_PF(bp)) {
13052 				bp->link_info.phy_retry = true;
13053 				bp->link_info.phy_retry_expires =
13054 					jiffies + 5 * HZ;
13055 			}
13056 		}
13057 	}
13058 
13059 	if (irq_re_init) {
13060 		udp_tunnel_nic_reset_ntf(bp->dev);
13061 		rc = bnxt_set_xps_mapping(bp);
13062 		if (rc)
13063 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13064 	}
13065 
13066 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13067 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13068 			static_branch_enable(&bnxt_xdp_locking_key);
13069 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13070 		static_branch_disable(&bnxt_xdp_locking_key);
13071 	}
13072 	set_bit(BNXT_STATE_OPEN, &bp->state);
13073 	bnxt_enable_int(bp);
13074 	/* Enable TX queues */
13075 	bnxt_tx_enable(bp);
13076 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13077 	/* Poll link status and check for SFP+ module status */
13078 	mutex_lock(&bp->link_lock);
13079 	bnxt_get_port_module_status(bp);
13080 	mutex_unlock(&bp->link_lock);
13081 
13082 	/* VF-reps may need to be re-opened after the PF is re-opened */
13083 	if (BNXT_PF(bp))
13084 		bnxt_vf_reps_open(bp);
13085 	bnxt_ptp_init_rtc(bp, true);
13086 	bnxt_ptp_cfg_tstamp_filters(bp);
13087 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13088 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13089 	bnxt_cfg_usr_fltrs(bp);
13090 	return 0;
13091 
13092 open_err_irq:
13093 	bnxt_del_napi(bp);
13094 
13095 open_err_free_mem:
13096 	bnxt_free_skbs(bp);
13097 	bnxt_free_irq(bp);
13098 	bnxt_free_mem(bp, true);
13099 	return rc;
13100 }
13101 
13102 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13103 {
13104 	int rc = 0;
13105 
13106 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13107 		rc = -EIO;
13108 	if (!rc)
13109 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13110 	if (rc) {
13111 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13112 		netif_close(bp->dev);
13113 	}
13114 	return rc;
13115 }
13116 
13117 /* netdev instance lock held, open the NIC half way by allocating all
13118  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13119  * for offline self tests.
13120  */
13121 int bnxt_half_open_nic(struct bnxt *bp)
13122 {
13123 	int rc = 0;
13124 
13125 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13126 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13127 		rc = -ENODEV;
13128 		goto half_open_err;
13129 	}
13130 
13131 	rc = bnxt_alloc_mem(bp, true);
13132 	if (rc) {
13133 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13134 		goto half_open_err;
13135 	}
13136 	bnxt_init_napi(bp);
13137 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13138 	rc = bnxt_init_nic(bp, true);
13139 	if (rc) {
13140 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13141 		bnxt_del_napi(bp);
13142 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13143 		goto half_open_err;
13144 	}
13145 	return 0;
13146 
13147 half_open_err:
13148 	bnxt_free_skbs(bp);
13149 	bnxt_free_mem(bp, true);
13150 	netif_close(bp->dev);
13151 	return rc;
13152 }
13153 
13154 /* netdev instance lock held, this call can only be made after a previous
13155  * successful call to bnxt_half_open_nic().
13156  */
13157 void bnxt_half_close_nic(struct bnxt *bp)
13158 {
13159 	bnxt_hwrm_resource_free(bp, false, true);
13160 	bnxt_del_napi(bp);
13161 	bnxt_free_skbs(bp);
13162 	bnxt_free_mem(bp, true);
13163 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13164 }
13165 
13166 void bnxt_reenable_sriov(struct bnxt *bp)
13167 {
13168 	if (BNXT_PF(bp)) {
13169 		struct bnxt_pf_info *pf = &bp->pf;
13170 		int n = pf->active_vfs;
13171 
13172 		if (n)
13173 			bnxt_cfg_hw_sriov(bp, &n, true);
13174 	}
13175 }
13176 
13177 static int bnxt_open(struct net_device *dev)
13178 {
13179 	struct bnxt *bp = netdev_priv(dev);
13180 	int rc;
13181 
13182 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13183 		rc = bnxt_reinit_after_abort(bp);
13184 		if (rc) {
13185 			if (rc == -EBUSY)
13186 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13187 			else
13188 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13189 			return -ENODEV;
13190 		}
13191 	}
13192 
13193 	rc = bnxt_hwrm_if_change(bp, true);
13194 	if (rc)
13195 		return rc;
13196 
13197 	rc = __bnxt_open_nic(bp, true, true);
13198 	if (rc) {
13199 		bnxt_hwrm_if_change(bp, false);
13200 	} else {
13201 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13202 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13203 				bnxt_queue_sp_work(bp,
13204 						   BNXT_RESTART_ULP_SP_EVENT);
13205 		}
13206 	}
13207 
13208 	return rc;
13209 }
13210 
13211 static bool bnxt_drv_busy(struct bnxt *bp)
13212 {
13213 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13214 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13215 }
13216 
13217 static void bnxt_get_ring_stats(struct bnxt *bp,
13218 				struct rtnl_link_stats64 *stats);
13219 
13220 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13221 			     bool link_re_init)
13222 {
13223 	/* Close the VF-reps before closing PF */
13224 	if (BNXT_PF(bp))
13225 		bnxt_vf_reps_close(bp);
13226 
13227 	/* Change device state to avoid TX queue wake up's */
13228 	bnxt_tx_disable(bp);
13229 
13230 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13231 	smp_mb__after_atomic();
13232 	while (bnxt_drv_busy(bp))
13233 		msleep(20);
13234 
13235 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13236 		bnxt_clear_rss_ctxs(bp);
13237 	/* Flush rings and disable interrupts */
13238 	bnxt_shutdown_nic(bp, irq_re_init);
13239 
13240 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13241 
13242 	bnxt_debug_dev_exit(bp);
13243 	bnxt_disable_napi(bp);
13244 	timer_delete_sync(&bp->timer);
13245 	bnxt_free_skbs(bp);
13246 
13247 	/* Save ring stats before shutdown */
13248 	if (bp->bnapi && irq_re_init) {
13249 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13250 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13251 	}
13252 	if (irq_re_init) {
13253 		bnxt_free_irq(bp);
13254 		bnxt_del_napi(bp);
13255 	}
13256 	bnxt_free_mem(bp, irq_re_init);
13257 }
13258 
13259 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13260 {
13261 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13262 		/* If we get here, it means firmware reset is in progress
13263 		 * while we are trying to close.  We can safely proceed with
13264 		 * the close because we are holding netdev instance lock.
13265 		 * Some firmware messages may fail as we proceed to close.
13266 		 * We set the ABORT_ERR flag here so that the FW reset thread
13267 		 * will later abort when it gets the netdev instance lock
13268 		 * and sees the flag.
13269 		 */
13270 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13271 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13272 	}
13273 
13274 #ifdef CONFIG_BNXT_SRIOV
13275 	if (bp->sriov_cfg) {
13276 		int rc;
13277 
13278 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13279 						      !bp->sriov_cfg,
13280 						      BNXT_SRIOV_CFG_WAIT_TMO);
13281 		if (!rc)
13282 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13283 		else if (rc < 0)
13284 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13285 	}
13286 #endif
13287 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13288 }
13289 
13290 static int bnxt_close(struct net_device *dev)
13291 {
13292 	struct bnxt *bp = netdev_priv(dev);
13293 
13294 	bnxt_close_nic(bp, true, true);
13295 	bnxt_hwrm_shutdown_link(bp);
13296 	bnxt_hwrm_if_change(bp, false);
13297 	return 0;
13298 }
13299 
13300 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13301 				   u16 *val)
13302 {
13303 	struct hwrm_port_phy_mdio_read_output *resp;
13304 	struct hwrm_port_phy_mdio_read_input *req;
13305 	int rc;
13306 
13307 	if (bp->hwrm_spec_code < 0x10a00)
13308 		return -EOPNOTSUPP;
13309 
13310 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13311 	if (rc)
13312 		return rc;
13313 
13314 	req->port_id = cpu_to_le16(bp->pf.port_id);
13315 	req->phy_addr = phy_addr;
13316 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13317 	if (mdio_phy_id_is_c45(phy_addr)) {
13318 		req->cl45_mdio = 1;
13319 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13320 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13321 		req->reg_addr = cpu_to_le16(reg);
13322 	}
13323 
13324 	resp = hwrm_req_hold(bp, req);
13325 	rc = hwrm_req_send(bp, req);
13326 	if (!rc)
13327 		*val = le16_to_cpu(resp->reg_data);
13328 	hwrm_req_drop(bp, req);
13329 	return rc;
13330 }
13331 
13332 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13333 				    u16 val)
13334 {
13335 	struct hwrm_port_phy_mdio_write_input *req;
13336 	int rc;
13337 
13338 	if (bp->hwrm_spec_code < 0x10a00)
13339 		return -EOPNOTSUPP;
13340 
13341 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13342 	if (rc)
13343 		return rc;
13344 
13345 	req->port_id = cpu_to_le16(bp->pf.port_id);
13346 	req->phy_addr = phy_addr;
13347 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13348 	if (mdio_phy_id_is_c45(phy_addr)) {
13349 		req->cl45_mdio = 1;
13350 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13351 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13352 		req->reg_addr = cpu_to_le16(reg);
13353 	}
13354 	req->reg_data = cpu_to_le16(val);
13355 
13356 	return hwrm_req_send(bp, req);
13357 }
13358 
13359 /* netdev instance lock held */
13360 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13361 {
13362 	struct mii_ioctl_data *mdio = if_mii(ifr);
13363 	struct bnxt *bp = netdev_priv(dev);
13364 	int rc;
13365 
13366 	switch (cmd) {
13367 	case SIOCGMIIPHY:
13368 		mdio->phy_id = bp->link_info.phy_addr;
13369 
13370 		fallthrough;
13371 	case SIOCGMIIREG: {
13372 		u16 mii_regval = 0;
13373 
13374 		if (!netif_running(dev))
13375 			return -EAGAIN;
13376 
13377 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13378 					     &mii_regval);
13379 		mdio->val_out = mii_regval;
13380 		return rc;
13381 	}
13382 
13383 	case SIOCSMIIREG:
13384 		if (!netif_running(dev))
13385 			return -EAGAIN;
13386 
13387 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13388 						mdio->val_in);
13389 
13390 	default:
13391 		/* do nothing */
13392 		break;
13393 	}
13394 	return -EOPNOTSUPP;
13395 }
13396 
13397 static void bnxt_get_ring_stats(struct bnxt *bp,
13398 				struct rtnl_link_stats64 *stats)
13399 {
13400 	int i;
13401 
13402 	for (i = 0; i < bp->cp_nr_rings; i++) {
13403 		struct bnxt_napi *bnapi = bp->bnapi[i];
13404 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13405 		u64 *sw = cpr->stats.sw_stats;
13406 
13407 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13408 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13409 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13410 
13411 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13412 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13413 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13414 
13415 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13416 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13417 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13418 
13419 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13420 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13421 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13422 
13423 		stats->rx_missed_errors +=
13424 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13425 
13426 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13427 
13428 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13429 
13430 		stats->rx_dropped +=
13431 			cpr->sw_stats->rx.rx_netpoll_discards +
13432 			cpr->sw_stats->rx.rx_oom_discards;
13433 	}
13434 }
13435 
13436 static void bnxt_add_prev_stats(struct bnxt *bp,
13437 				struct rtnl_link_stats64 *stats)
13438 {
13439 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13440 
13441 	stats->rx_packets += prev_stats->rx_packets;
13442 	stats->tx_packets += prev_stats->tx_packets;
13443 	stats->rx_bytes += prev_stats->rx_bytes;
13444 	stats->tx_bytes += prev_stats->tx_bytes;
13445 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13446 	stats->multicast += prev_stats->multicast;
13447 	stats->rx_dropped += prev_stats->rx_dropped;
13448 	stats->tx_dropped += prev_stats->tx_dropped;
13449 }
13450 
13451 static void
13452 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13453 {
13454 	struct bnxt *bp = netdev_priv(dev);
13455 
13456 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13457 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13458 	 * we check the BNXT_STATE_OPEN flag.
13459 	 */
13460 	smp_mb__after_atomic();
13461 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13462 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13463 		*stats = bp->net_stats_prev;
13464 		return;
13465 	}
13466 
13467 	bnxt_get_ring_stats(bp, stats);
13468 	bnxt_add_prev_stats(bp, stats);
13469 
13470 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13471 		u64 *rx = bp->port_stats.sw_stats;
13472 		u64 *tx = bp->port_stats.sw_stats +
13473 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13474 
13475 		stats->rx_crc_errors =
13476 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13477 		stats->rx_frame_errors =
13478 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13479 		stats->rx_length_errors =
13480 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13481 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13482 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13483 		stats->rx_errors =
13484 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13485 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13486 		stats->collisions =
13487 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13488 		stats->tx_fifo_errors =
13489 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13490 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13491 	}
13492 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13493 }
13494 
13495 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13496 					struct bnxt_total_ring_drv_stats *stats,
13497 					struct bnxt_cp_ring_info *cpr)
13498 {
13499 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13500 	u64 *hw_stats = cpr->stats.sw_stats;
13501 
13502 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13503 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13504 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13505 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13506 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13507 	stats->rx_total_ring_discards +=
13508 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13509 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13510 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13511 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13512 	stats->tx_total_ring_discards +=
13513 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13514 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13515 }
13516 
13517 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13518 			     struct bnxt_total_ring_drv_stats *stats)
13519 {
13520 	int i;
13521 
13522 	for (i = 0; i < bp->cp_nr_rings; i++)
13523 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13524 }
13525 
13526 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13527 {
13528 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13529 	struct net_device *dev = bp->dev;
13530 	struct netdev_hw_addr *ha;
13531 	u8 *haddr;
13532 	int mc_count = 0;
13533 	bool update = false;
13534 	int off = 0;
13535 
13536 	netdev_for_each_mc_addr(ha, dev) {
13537 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13538 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13539 			vnic->mc_list_count = 0;
13540 			return false;
13541 		}
13542 		haddr = ha->addr;
13543 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13544 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13545 			update = true;
13546 		}
13547 		off += ETH_ALEN;
13548 		mc_count++;
13549 	}
13550 	if (mc_count)
13551 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13552 
13553 	if (mc_count != vnic->mc_list_count) {
13554 		vnic->mc_list_count = mc_count;
13555 		update = true;
13556 	}
13557 	return update;
13558 }
13559 
13560 static bool bnxt_uc_list_updated(struct bnxt *bp)
13561 {
13562 	struct net_device *dev = bp->dev;
13563 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13564 	struct netdev_hw_addr *ha;
13565 	int off = 0;
13566 
13567 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13568 		return true;
13569 
13570 	netdev_for_each_uc_addr(ha, dev) {
13571 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13572 			return true;
13573 
13574 		off += ETH_ALEN;
13575 	}
13576 	return false;
13577 }
13578 
13579 static void bnxt_set_rx_mode(struct net_device *dev)
13580 {
13581 	struct bnxt *bp = netdev_priv(dev);
13582 	struct bnxt_vnic_info *vnic;
13583 	bool mc_update = false;
13584 	bool uc_update;
13585 	u32 mask;
13586 
13587 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13588 		return;
13589 
13590 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13591 	mask = vnic->rx_mask;
13592 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13593 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13594 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13595 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13596 
13597 	if (dev->flags & IFF_PROMISC)
13598 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13599 
13600 	uc_update = bnxt_uc_list_updated(bp);
13601 
13602 	if (dev->flags & IFF_BROADCAST)
13603 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13604 	if (dev->flags & IFF_ALLMULTI) {
13605 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13606 		vnic->mc_list_count = 0;
13607 	} else if (dev->flags & IFF_MULTICAST) {
13608 		mc_update = bnxt_mc_list_updated(bp, &mask);
13609 	}
13610 
13611 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13612 		vnic->rx_mask = mask;
13613 
13614 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13615 	}
13616 }
13617 
13618 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13619 {
13620 	struct net_device *dev = bp->dev;
13621 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13622 	struct netdev_hw_addr *ha;
13623 	int i, off = 0, rc;
13624 	bool uc_update;
13625 
13626 	netif_addr_lock_bh(dev);
13627 	uc_update = bnxt_uc_list_updated(bp);
13628 	netif_addr_unlock_bh(dev);
13629 
13630 	if (!uc_update)
13631 		goto skip_uc;
13632 
13633 	for (i = 1; i < vnic->uc_filter_count; i++) {
13634 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13635 
13636 		bnxt_hwrm_l2_filter_free(bp, fltr);
13637 		bnxt_del_l2_filter(bp, fltr);
13638 	}
13639 
13640 	vnic->uc_filter_count = 1;
13641 
13642 	netif_addr_lock_bh(dev);
13643 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13644 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13645 	} else {
13646 		netdev_for_each_uc_addr(ha, dev) {
13647 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13648 			off += ETH_ALEN;
13649 			vnic->uc_filter_count++;
13650 		}
13651 	}
13652 	netif_addr_unlock_bh(dev);
13653 
13654 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13655 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13656 		if (rc) {
13657 			if (BNXT_VF(bp) && rc == -ENODEV) {
13658 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13659 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13660 				else
13661 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13662 				rc = 0;
13663 			} else {
13664 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13665 			}
13666 			vnic->uc_filter_count = i;
13667 			return rc;
13668 		}
13669 	}
13670 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13671 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13672 
13673 skip_uc:
13674 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13675 	    !bnxt_promisc_ok(bp))
13676 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13677 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13678 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13679 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13680 			    rc);
13681 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13682 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13683 		vnic->mc_list_count = 0;
13684 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13685 	}
13686 	if (rc)
13687 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13688 			   rc);
13689 
13690 	return rc;
13691 }
13692 
13693 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13694 {
13695 #ifdef CONFIG_BNXT_SRIOV
13696 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13697 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13698 
13699 		/* No minimum rings were provisioned by the PF.  Don't
13700 		 * reserve rings by default when device is down.
13701 		 */
13702 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13703 			return true;
13704 
13705 		if (!netif_running(bp->dev))
13706 			return false;
13707 	}
13708 #endif
13709 	return true;
13710 }
13711 
13712 /* If the chip and firmware supports RFS */
13713 static bool bnxt_rfs_supported(struct bnxt *bp)
13714 {
13715 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13716 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13717 			return true;
13718 		return false;
13719 	}
13720 	/* 212 firmware is broken for aRFS */
13721 	if (BNXT_FW_MAJ(bp) == 212)
13722 		return false;
13723 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13724 		return true;
13725 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13726 		return true;
13727 	return false;
13728 }
13729 
13730 /* If runtime conditions support RFS */
13731 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13732 {
13733 	struct bnxt_hw_rings hwr = {0};
13734 	int max_vnics, max_rss_ctxs;
13735 
13736 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13737 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13738 		return bnxt_rfs_supported(bp);
13739 
13740 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13741 		return false;
13742 
13743 	hwr.grp = bp->rx_nr_rings;
13744 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13745 	if (new_rss_ctx)
13746 		hwr.vnic++;
13747 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13748 	max_vnics = bnxt_get_max_func_vnics(bp);
13749 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13750 
13751 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13752 		if (bp->rx_nr_rings > 1)
13753 			netdev_warn(bp->dev,
13754 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13755 				    min(max_rss_ctxs - 1, max_vnics - 1));
13756 		return false;
13757 	}
13758 
13759 	if (!BNXT_NEW_RM(bp))
13760 		return true;
13761 
13762 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13763 	 * issue that will mess up the default VNIC if we reduce the
13764 	 * reservations.
13765 	 */
13766 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13767 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13768 		return true;
13769 
13770 	bnxt_hwrm_reserve_rings(bp, &hwr);
13771 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13772 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13773 		return true;
13774 
13775 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13776 	hwr.vnic = 1;
13777 	hwr.rss_ctx = 0;
13778 	bnxt_hwrm_reserve_rings(bp, &hwr);
13779 	return false;
13780 }
13781 
13782 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13783 					   netdev_features_t features)
13784 {
13785 	struct bnxt *bp = netdev_priv(dev);
13786 	netdev_features_t vlan_features;
13787 
13788 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13789 		features &= ~NETIF_F_NTUPLE;
13790 
13791 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13792 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13793 
13794 	if (!(features & NETIF_F_GRO))
13795 		features &= ~NETIF_F_GRO_HW;
13796 
13797 	if (features & NETIF_F_GRO_HW)
13798 		features &= ~NETIF_F_LRO;
13799 
13800 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13801 	 * turned on or off together.
13802 	 */
13803 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13804 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13805 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13806 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13807 		else if (vlan_features)
13808 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13809 	}
13810 #ifdef CONFIG_BNXT_SRIOV
13811 	if (BNXT_VF(bp) && bp->vf.vlan)
13812 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13813 #endif
13814 	return features;
13815 }
13816 
13817 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13818 				bool link_re_init, u32 flags, bool update_tpa)
13819 {
13820 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13821 	bp->flags = flags;
13822 	if (update_tpa)
13823 		bnxt_set_ring_params(bp);
13824 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13825 }
13826 
13827 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13828 {
13829 	bool update_tpa = false, update_ntuple = false;
13830 	struct bnxt *bp = netdev_priv(dev);
13831 	u32 flags = bp->flags;
13832 	u32 changes;
13833 	int rc = 0;
13834 	bool re_init = false;
13835 
13836 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13837 	if (features & NETIF_F_GRO_HW)
13838 		flags |= BNXT_FLAG_GRO;
13839 	else if (features & NETIF_F_LRO)
13840 		flags |= BNXT_FLAG_LRO;
13841 
13842 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13843 		flags &= ~BNXT_FLAG_TPA;
13844 
13845 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13846 		flags |= BNXT_FLAG_STRIP_VLAN;
13847 
13848 	if (features & NETIF_F_NTUPLE)
13849 		flags |= BNXT_FLAG_RFS;
13850 	else
13851 		bnxt_clear_usr_fltrs(bp, true);
13852 
13853 	changes = flags ^ bp->flags;
13854 	if (changes & BNXT_FLAG_TPA) {
13855 		update_tpa = true;
13856 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13857 		    (flags & BNXT_FLAG_TPA) == 0 ||
13858 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13859 			re_init = true;
13860 	}
13861 
13862 	if (changes & ~BNXT_FLAG_TPA)
13863 		re_init = true;
13864 
13865 	if (changes & BNXT_FLAG_RFS)
13866 		update_ntuple = true;
13867 
13868 	if (flags != bp->flags) {
13869 		u32 old_flags = bp->flags;
13870 
13871 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13872 			bp->flags = flags;
13873 			if (update_tpa)
13874 				bnxt_set_ring_params(bp);
13875 			return rc;
13876 		}
13877 
13878 		if (update_ntuple)
13879 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13880 
13881 		if (re_init)
13882 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13883 
13884 		if (update_tpa) {
13885 			bp->flags = flags;
13886 			rc = bnxt_set_tpa(bp,
13887 					  (flags & BNXT_FLAG_TPA) ?
13888 					  true : false);
13889 			if (rc)
13890 				bp->flags = old_flags;
13891 		}
13892 	}
13893 	return rc;
13894 }
13895 
13896 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13897 			      u8 **nextp)
13898 {
13899 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13900 	int hdr_count = 0;
13901 	u8 *nexthdr;
13902 	int start;
13903 
13904 	/* Check that there are at most 2 IPv6 extension headers, no
13905 	 * fragment header, and each is <= 64 bytes.
13906 	 */
13907 	start = nw_off + sizeof(*ip6h);
13908 	nexthdr = &ip6h->nexthdr;
13909 	while (ipv6_ext_hdr(*nexthdr)) {
13910 		struct ipv6_opt_hdr *hp;
13911 		int hdrlen;
13912 
13913 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13914 		    *nexthdr == NEXTHDR_FRAGMENT)
13915 			return false;
13916 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13917 					  skb_headlen(skb), NULL);
13918 		if (!hp)
13919 			return false;
13920 		if (*nexthdr == NEXTHDR_AUTH)
13921 			hdrlen = ipv6_authlen(hp);
13922 		else
13923 			hdrlen = ipv6_optlen(hp);
13924 
13925 		if (hdrlen > 64)
13926 			return false;
13927 
13928 		hdr_count++;
13929 		nexthdr = &hp->nexthdr;
13930 		start += hdrlen;
13931 	}
13932 	if (nextp) {
13933 		/* Caller will check inner protocol */
13934 		if (skb->encapsulation) {
13935 			*nextp = nexthdr;
13936 			return true;
13937 		}
13938 		*nextp = NULL;
13939 	}
13940 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13941 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13942 }
13943 
13944 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13945 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13946 {
13947 	struct udphdr *uh = udp_hdr(skb);
13948 	__be16 udp_port = uh->dest;
13949 
13950 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13951 	    udp_port != bp->vxlan_gpe_port)
13952 		return false;
13953 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13954 		struct ethhdr *eh = inner_eth_hdr(skb);
13955 
13956 		switch (eh->h_proto) {
13957 		case htons(ETH_P_IP):
13958 			return true;
13959 		case htons(ETH_P_IPV6):
13960 			return bnxt_exthdr_check(bp, skb,
13961 						 skb_inner_network_offset(skb),
13962 						 NULL);
13963 		}
13964 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13965 		return true;
13966 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13967 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13968 					 NULL);
13969 	}
13970 	return false;
13971 }
13972 
13973 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13974 {
13975 	switch (l4_proto) {
13976 	case IPPROTO_UDP:
13977 		return bnxt_udp_tunl_check(bp, skb);
13978 	case IPPROTO_IPIP:
13979 		return true;
13980 	case IPPROTO_GRE: {
13981 		switch (skb->inner_protocol) {
13982 		default:
13983 			return false;
13984 		case htons(ETH_P_IP):
13985 			return true;
13986 		case htons(ETH_P_IPV6):
13987 			fallthrough;
13988 		}
13989 	}
13990 	case IPPROTO_IPV6:
13991 		/* Check ext headers of inner ipv6 */
13992 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13993 					 NULL);
13994 	}
13995 	return false;
13996 }
13997 
13998 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13999 					     struct net_device *dev,
14000 					     netdev_features_t features)
14001 {
14002 	struct bnxt *bp = netdev_priv(dev);
14003 	u8 *l4_proto;
14004 
14005 	features = vlan_features_check(skb, features);
14006 	switch (vlan_get_protocol(skb)) {
14007 	case htons(ETH_P_IP):
14008 		if (!skb->encapsulation)
14009 			return features;
14010 		l4_proto = &ip_hdr(skb)->protocol;
14011 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14012 			return features;
14013 		break;
14014 	case htons(ETH_P_IPV6):
14015 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14016 				       &l4_proto))
14017 			break;
14018 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14019 			return features;
14020 		break;
14021 	}
14022 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14023 }
14024 
14025 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14026 			 u32 *reg_buf)
14027 {
14028 	struct hwrm_dbg_read_direct_output *resp;
14029 	struct hwrm_dbg_read_direct_input *req;
14030 	__le32 *dbg_reg_buf;
14031 	dma_addr_t mapping;
14032 	int rc, i;
14033 
14034 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14035 	if (rc)
14036 		return rc;
14037 
14038 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14039 					 &mapping);
14040 	if (!dbg_reg_buf) {
14041 		rc = -ENOMEM;
14042 		goto dbg_rd_reg_exit;
14043 	}
14044 
14045 	req->host_dest_addr = cpu_to_le64(mapping);
14046 
14047 	resp = hwrm_req_hold(bp, req);
14048 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14049 	req->read_len32 = cpu_to_le32(num_words);
14050 
14051 	rc = hwrm_req_send(bp, req);
14052 	if (rc || resp->error_code) {
14053 		rc = -EIO;
14054 		goto dbg_rd_reg_exit;
14055 	}
14056 	for (i = 0; i < num_words; i++)
14057 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14058 
14059 dbg_rd_reg_exit:
14060 	hwrm_req_drop(bp, req);
14061 	return rc;
14062 }
14063 
14064 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14065 				       u32 ring_id, u32 *prod, u32 *cons)
14066 {
14067 	struct hwrm_dbg_ring_info_get_output *resp;
14068 	struct hwrm_dbg_ring_info_get_input *req;
14069 	int rc;
14070 
14071 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14072 	if (rc)
14073 		return rc;
14074 
14075 	req->ring_type = ring_type;
14076 	req->fw_ring_id = cpu_to_le32(ring_id);
14077 	resp = hwrm_req_hold(bp, req);
14078 	rc = hwrm_req_send(bp, req);
14079 	if (!rc) {
14080 		*prod = le32_to_cpu(resp->producer_index);
14081 		*cons = le32_to_cpu(resp->consumer_index);
14082 	}
14083 	hwrm_req_drop(bp, req);
14084 	return rc;
14085 }
14086 
14087 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14088 {
14089 	struct bnxt_tx_ring_info *txr;
14090 	int i = bnapi->index, j;
14091 
14092 	bnxt_for_each_napi_tx(j, bnapi, txr)
14093 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14094 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14095 			    txr->tx_cons);
14096 }
14097 
14098 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14099 {
14100 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14101 	int i = bnapi->index;
14102 
14103 	if (!rxr)
14104 		return;
14105 
14106 	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",
14107 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14108 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14109 		    rxr->rx_sw_agg_prod);
14110 }
14111 
14112 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14113 {
14114 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14115 	int i = bnapi->index, j;
14116 
14117 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14118 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14119 	for (j = 0; j < cpr->cp_ring_count; j++) {
14120 		cpr2 = &cpr->cp_ring_arr[j];
14121 		if (!cpr2->bnapi)
14122 			continue;
14123 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14124 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14125 			    cpr2->cp_raw_cons);
14126 	}
14127 }
14128 
14129 static void bnxt_dbg_dump_states(struct bnxt *bp)
14130 {
14131 	int i;
14132 	struct bnxt_napi *bnapi;
14133 
14134 	for (i = 0; i < bp->cp_nr_rings; i++) {
14135 		bnapi = bp->bnapi[i];
14136 		if (netif_msg_drv(bp)) {
14137 			bnxt_dump_tx_sw_state(bnapi);
14138 			bnxt_dump_rx_sw_state(bnapi);
14139 			bnxt_dump_cp_sw_state(bnapi);
14140 		}
14141 	}
14142 }
14143 
14144 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14145 {
14146 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14147 	struct hwrm_ring_reset_input *req;
14148 	struct bnxt_napi *bnapi = rxr->bnapi;
14149 	struct bnxt_cp_ring_info *cpr;
14150 	u16 cp_ring_id;
14151 	int rc;
14152 
14153 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14154 	if (rc)
14155 		return rc;
14156 
14157 	cpr = &bnapi->cp_ring;
14158 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14159 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14160 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14161 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14162 	return hwrm_req_send_silent(bp, req);
14163 }
14164 
14165 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14166 {
14167 	if (!silent)
14168 		bnxt_dbg_dump_states(bp);
14169 	if (netif_running(bp->dev)) {
14170 		bnxt_close_nic(bp, !silent, false);
14171 		bnxt_open_nic(bp, !silent, false);
14172 	}
14173 }
14174 
14175 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14176 {
14177 	struct bnxt *bp = netdev_priv(dev);
14178 
14179 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14180 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14181 }
14182 
14183 static void bnxt_fw_health_check(struct bnxt *bp)
14184 {
14185 	struct bnxt_fw_health *fw_health = bp->fw_health;
14186 	struct pci_dev *pdev = bp->pdev;
14187 	u32 val;
14188 
14189 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14190 		return;
14191 
14192 	/* Make sure it is enabled before checking the tmr_counter. */
14193 	smp_rmb();
14194 	if (fw_health->tmr_counter) {
14195 		fw_health->tmr_counter--;
14196 		return;
14197 	}
14198 
14199 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14200 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14201 		fw_health->arrests++;
14202 		goto fw_reset;
14203 	}
14204 
14205 	fw_health->last_fw_heartbeat = val;
14206 
14207 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14208 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14209 		fw_health->discoveries++;
14210 		goto fw_reset;
14211 	}
14212 
14213 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14214 	return;
14215 
14216 fw_reset:
14217 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14218 }
14219 
14220 static void bnxt_timer(struct timer_list *t)
14221 {
14222 	struct bnxt *bp = timer_container_of(bp, t, timer);
14223 	struct net_device *dev = bp->dev;
14224 
14225 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14226 		return;
14227 
14228 	if (atomic_read(&bp->intr_sem) != 0)
14229 		goto bnxt_restart_timer;
14230 
14231 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14232 		bnxt_fw_health_check(bp);
14233 
14234 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14235 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14236 
14237 	if (bnxt_tc_flower_enabled(bp))
14238 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14239 
14240 #ifdef CONFIG_RFS_ACCEL
14241 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14242 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14243 #endif /*CONFIG_RFS_ACCEL*/
14244 
14245 	if (bp->link_info.phy_retry) {
14246 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14247 			bp->link_info.phy_retry = false;
14248 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14249 		} else {
14250 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14251 		}
14252 	}
14253 
14254 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14255 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14256 
14257 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14258 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14259 
14260 bnxt_restart_timer:
14261 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14262 }
14263 
14264 static void bnxt_lock_sp(struct bnxt *bp)
14265 {
14266 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14267 	 * set.  If the device is being closed, bnxt_close() may be holding
14268 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14269 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14270 	 * instance lock.
14271 	 */
14272 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14273 	netdev_lock(bp->dev);
14274 }
14275 
14276 static void bnxt_unlock_sp(struct bnxt *bp)
14277 {
14278 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14279 	netdev_unlock(bp->dev);
14280 }
14281 
14282 /* Only called from bnxt_sp_task() */
14283 static void bnxt_reset(struct bnxt *bp, bool silent)
14284 {
14285 	bnxt_lock_sp(bp);
14286 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14287 		bnxt_reset_task(bp, silent);
14288 	bnxt_unlock_sp(bp);
14289 }
14290 
14291 /* Only called from bnxt_sp_task() */
14292 static void bnxt_rx_ring_reset(struct bnxt *bp)
14293 {
14294 	int i;
14295 
14296 	bnxt_lock_sp(bp);
14297 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14298 		bnxt_unlock_sp(bp);
14299 		return;
14300 	}
14301 	/* Disable and flush TPA before resetting the RX ring */
14302 	if (bp->flags & BNXT_FLAG_TPA)
14303 		bnxt_set_tpa(bp, false);
14304 	for (i = 0; i < bp->rx_nr_rings; i++) {
14305 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14306 		struct bnxt_cp_ring_info *cpr;
14307 		int rc;
14308 
14309 		if (!rxr->bnapi->in_reset)
14310 			continue;
14311 
14312 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14313 		if (rc) {
14314 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14315 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14316 			else
14317 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14318 					    rc);
14319 			bnxt_reset_task(bp, true);
14320 			break;
14321 		}
14322 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14323 		rxr->rx_prod = 0;
14324 		rxr->rx_agg_prod = 0;
14325 		rxr->rx_sw_agg_prod = 0;
14326 		rxr->rx_next_cons = 0;
14327 		rxr->bnapi->in_reset = false;
14328 		bnxt_alloc_one_rx_ring(bp, i);
14329 		cpr = &rxr->bnapi->cp_ring;
14330 		cpr->sw_stats->rx.rx_resets++;
14331 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14332 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14333 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14334 	}
14335 	if (bp->flags & BNXT_FLAG_TPA)
14336 		bnxt_set_tpa(bp, true);
14337 	bnxt_unlock_sp(bp);
14338 }
14339 
14340 static void bnxt_fw_fatal_close(struct bnxt *bp)
14341 {
14342 	bnxt_tx_disable(bp);
14343 	bnxt_disable_napi(bp);
14344 	bnxt_disable_int_sync(bp);
14345 	bnxt_free_irq(bp);
14346 	bnxt_clear_int_mode(bp);
14347 	pci_disable_device(bp->pdev);
14348 }
14349 
14350 static void bnxt_fw_reset_close(struct bnxt *bp)
14351 {
14352 	/* When firmware is in fatal state, quiesce device and disable
14353 	 * bus master to prevent any potential bad DMAs before freeing
14354 	 * kernel memory.
14355 	 */
14356 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14357 		u16 val = 0;
14358 
14359 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14360 		if (val == 0xffff)
14361 			bp->fw_reset_min_dsecs = 0;
14362 		bnxt_fw_fatal_close(bp);
14363 	}
14364 	__bnxt_close_nic(bp, true, false);
14365 	bnxt_vf_reps_free(bp);
14366 	bnxt_clear_int_mode(bp);
14367 	bnxt_hwrm_func_drv_unrgtr(bp);
14368 	if (pci_is_enabled(bp->pdev))
14369 		pci_disable_device(bp->pdev);
14370 	bnxt_free_ctx_mem(bp, false);
14371 }
14372 
14373 static bool is_bnxt_fw_ok(struct bnxt *bp)
14374 {
14375 	struct bnxt_fw_health *fw_health = bp->fw_health;
14376 	bool no_heartbeat = false, has_reset = false;
14377 	u32 val;
14378 
14379 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14380 	if (val == fw_health->last_fw_heartbeat)
14381 		no_heartbeat = true;
14382 
14383 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14384 	if (val != fw_health->last_fw_reset_cnt)
14385 		has_reset = true;
14386 
14387 	if (!no_heartbeat && has_reset)
14388 		return true;
14389 
14390 	return false;
14391 }
14392 
14393 /* netdev instance lock is acquired before calling this function */
14394 static void bnxt_force_fw_reset(struct bnxt *bp)
14395 {
14396 	struct bnxt_fw_health *fw_health = bp->fw_health;
14397 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14398 	u32 wait_dsecs;
14399 
14400 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14401 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14402 		return;
14403 
14404 	/* we have to serialize with bnxt_refclk_read()*/
14405 	if (ptp) {
14406 		unsigned long flags;
14407 
14408 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14409 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14410 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14411 	} else {
14412 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14413 	}
14414 	bnxt_fw_reset_close(bp);
14415 	wait_dsecs = fw_health->master_func_wait_dsecs;
14416 	if (fw_health->primary) {
14417 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14418 			wait_dsecs = 0;
14419 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14420 	} else {
14421 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14422 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14423 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14424 	}
14425 
14426 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14427 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14428 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14429 }
14430 
14431 void bnxt_fw_exception(struct bnxt *bp)
14432 {
14433 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14434 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14435 	bnxt_ulp_stop(bp);
14436 	bnxt_lock_sp(bp);
14437 	bnxt_force_fw_reset(bp);
14438 	bnxt_unlock_sp(bp);
14439 }
14440 
14441 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14442  * < 0 on error.
14443  */
14444 static int bnxt_get_registered_vfs(struct bnxt *bp)
14445 {
14446 #ifdef CONFIG_BNXT_SRIOV
14447 	int rc;
14448 
14449 	if (!BNXT_PF(bp))
14450 		return 0;
14451 
14452 	rc = bnxt_hwrm_func_qcfg(bp);
14453 	if (rc) {
14454 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14455 		return rc;
14456 	}
14457 	if (bp->pf.registered_vfs)
14458 		return bp->pf.registered_vfs;
14459 	if (bp->sriov_cfg)
14460 		return 1;
14461 #endif
14462 	return 0;
14463 }
14464 
14465 void bnxt_fw_reset(struct bnxt *bp)
14466 {
14467 	bnxt_ulp_stop(bp);
14468 	bnxt_lock_sp(bp);
14469 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14470 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14471 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14472 		int n = 0, tmo;
14473 
14474 		/* we have to serialize with bnxt_refclk_read()*/
14475 		if (ptp) {
14476 			unsigned long flags;
14477 
14478 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14479 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14480 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14481 		} else {
14482 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14483 		}
14484 		if (bp->pf.active_vfs &&
14485 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14486 			n = bnxt_get_registered_vfs(bp);
14487 		if (n < 0) {
14488 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14489 				   n);
14490 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14491 			netif_close(bp->dev);
14492 			goto fw_reset_exit;
14493 		} else if (n > 0) {
14494 			u16 vf_tmo_dsecs = n * 10;
14495 
14496 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14497 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14498 			bp->fw_reset_state =
14499 				BNXT_FW_RESET_STATE_POLL_VF;
14500 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14501 			goto fw_reset_exit;
14502 		}
14503 		bnxt_fw_reset_close(bp);
14504 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14505 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14506 			tmo = HZ / 10;
14507 		} else {
14508 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14509 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14510 		}
14511 		bnxt_queue_fw_reset_work(bp, tmo);
14512 	}
14513 fw_reset_exit:
14514 	bnxt_unlock_sp(bp);
14515 }
14516 
14517 static void bnxt_chk_missed_irq(struct bnxt *bp)
14518 {
14519 	int i;
14520 
14521 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14522 		return;
14523 
14524 	for (i = 0; i < bp->cp_nr_rings; i++) {
14525 		struct bnxt_napi *bnapi = bp->bnapi[i];
14526 		struct bnxt_cp_ring_info *cpr;
14527 		u32 fw_ring_id;
14528 		int j;
14529 
14530 		if (!bnapi)
14531 			continue;
14532 
14533 		cpr = &bnapi->cp_ring;
14534 		for (j = 0; j < cpr->cp_ring_count; j++) {
14535 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14536 			u32 val[2];
14537 
14538 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14539 				continue;
14540 
14541 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14542 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14543 				continue;
14544 			}
14545 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14546 			bnxt_dbg_hwrm_ring_info_get(bp,
14547 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14548 				fw_ring_id, &val[0], &val[1]);
14549 			cpr->sw_stats->cmn.missed_irqs++;
14550 		}
14551 	}
14552 }
14553 
14554 static void bnxt_cfg_ntp_filters(struct bnxt *);
14555 
14556 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14557 {
14558 	struct bnxt_link_info *link_info = &bp->link_info;
14559 
14560 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14561 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14562 		if (bp->hwrm_spec_code >= 0x10201) {
14563 			if (link_info->auto_pause_setting &
14564 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14565 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14566 		} else {
14567 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14568 		}
14569 		bnxt_set_auto_speed(link_info);
14570 	} else {
14571 		bnxt_set_force_speed(link_info);
14572 		link_info->req_duplex = link_info->duplex_setting;
14573 	}
14574 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14575 		link_info->req_flow_ctrl =
14576 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14577 	else
14578 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14579 }
14580 
14581 static void bnxt_fw_echo_reply(struct bnxt *bp)
14582 {
14583 	struct bnxt_fw_health *fw_health = bp->fw_health;
14584 	struct hwrm_func_echo_response_input *req;
14585 	int rc;
14586 
14587 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14588 	if (rc)
14589 		return;
14590 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14591 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14592 	hwrm_req_send(bp, req);
14593 }
14594 
14595 static void bnxt_ulp_restart(struct bnxt *bp)
14596 {
14597 	bnxt_ulp_stop(bp);
14598 	bnxt_ulp_start(bp, 0);
14599 }
14600 
14601 static void bnxt_sp_task(struct work_struct *work)
14602 {
14603 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14604 
14605 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14606 	smp_mb__after_atomic();
14607 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14608 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14609 		return;
14610 	}
14611 
14612 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14613 		bnxt_ulp_restart(bp);
14614 		bnxt_reenable_sriov(bp);
14615 	}
14616 
14617 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14618 		bnxt_cfg_rx_mode(bp);
14619 
14620 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14621 		bnxt_cfg_ntp_filters(bp);
14622 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14623 		bnxt_hwrm_exec_fwd_req(bp);
14624 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14625 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14626 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14627 		bnxt_hwrm_port_qstats(bp, 0);
14628 		bnxt_hwrm_port_qstats_ext(bp, 0);
14629 		bnxt_accumulate_all_stats(bp);
14630 	}
14631 
14632 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14633 		int rc;
14634 
14635 		mutex_lock(&bp->link_lock);
14636 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14637 				       &bp->sp_event))
14638 			bnxt_hwrm_phy_qcaps(bp);
14639 
14640 		rc = bnxt_update_link(bp, true);
14641 		if (rc)
14642 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14643 				   rc);
14644 
14645 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14646 				       &bp->sp_event))
14647 			bnxt_init_ethtool_link_settings(bp);
14648 		mutex_unlock(&bp->link_lock);
14649 	}
14650 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14651 		int rc;
14652 
14653 		mutex_lock(&bp->link_lock);
14654 		rc = bnxt_update_phy_setting(bp);
14655 		mutex_unlock(&bp->link_lock);
14656 		if (rc) {
14657 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14658 		} else {
14659 			bp->link_info.phy_retry = false;
14660 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14661 		}
14662 	}
14663 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14664 		mutex_lock(&bp->link_lock);
14665 		bnxt_get_port_module_status(bp);
14666 		mutex_unlock(&bp->link_lock);
14667 	}
14668 
14669 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14670 		bnxt_tc_flow_stats_work(bp);
14671 
14672 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14673 		bnxt_chk_missed_irq(bp);
14674 
14675 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14676 		bnxt_fw_echo_reply(bp);
14677 
14678 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14679 		bnxt_hwmon_notify_event(bp);
14680 
14681 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14682 	 * must be the last functions to be called before exiting.
14683 	 */
14684 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14685 		bnxt_reset(bp, false);
14686 
14687 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14688 		bnxt_reset(bp, true);
14689 
14690 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14691 		bnxt_rx_ring_reset(bp);
14692 
14693 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14694 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14695 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14696 			bnxt_devlink_health_fw_report(bp);
14697 		else
14698 			bnxt_fw_reset(bp);
14699 	}
14700 
14701 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14702 		if (!is_bnxt_fw_ok(bp))
14703 			bnxt_devlink_health_fw_report(bp);
14704 	}
14705 
14706 	smp_mb__before_atomic();
14707 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14708 }
14709 
14710 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14711 				int *max_cp);
14712 
14713 /* Under netdev instance lock */
14714 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14715 		     int tx_xdp)
14716 {
14717 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14718 	struct bnxt_hw_rings hwr = {0};
14719 	int rx_rings = rx;
14720 	int rc;
14721 
14722 	if (tcs)
14723 		tx_sets = tcs;
14724 
14725 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14726 
14727 	if (max_rx < rx_rings)
14728 		return -ENOMEM;
14729 
14730 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14731 		rx_rings <<= 1;
14732 
14733 	hwr.rx = rx_rings;
14734 	hwr.tx = tx * tx_sets + tx_xdp;
14735 	if (max_tx < hwr.tx)
14736 		return -ENOMEM;
14737 
14738 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14739 
14740 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14741 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14742 	if (max_cp < hwr.cp)
14743 		return -ENOMEM;
14744 	hwr.stat = hwr.cp;
14745 	if (BNXT_NEW_RM(bp)) {
14746 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14747 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14748 		hwr.grp = rx;
14749 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14750 	}
14751 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14752 		hwr.cp_p5 = hwr.tx + rx;
14753 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14754 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14755 		if (!bnxt_ulp_registered(bp->edev)) {
14756 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14757 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14758 		}
14759 		if (hwr.cp > bp->total_irqs) {
14760 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14761 
14762 			if (total_msix < hwr.cp) {
14763 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14764 					    hwr.cp, total_msix);
14765 				rc = -ENOSPC;
14766 			}
14767 		}
14768 	}
14769 	return rc;
14770 }
14771 
14772 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14773 {
14774 	if (bp->bar2) {
14775 		pci_iounmap(pdev, bp->bar2);
14776 		bp->bar2 = NULL;
14777 	}
14778 
14779 	if (bp->bar1) {
14780 		pci_iounmap(pdev, bp->bar1);
14781 		bp->bar1 = NULL;
14782 	}
14783 
14784 	if (bp->bar0) {
14785 		pci_iounmap(pdev, bp->bar0);
14786 		bp->bar0 = NULL;
14787 	}
14788 }
14789 
14790 static void bnxt_cleanup_pci(struct bnxt *bp)
14791 {
14792 	bnxt_unmap_bars(bp, bp->pdev);
14793 	pci_release_regions(bp->pdev);
14794 	if (pci_is_enabled(bp->pdev))
14795 		pci_disable_device(bp->pdev);
14796 }
14797 
14798 static void bnxt_init_dflt_coal(struct bnxt *bp)
14799 {
14800 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14801 	struct bnxt_coal *coal;
14802 	u16 flags = 0;
14803 
14804 	if (coal_cap->cmpl_params &
14805 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14806 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14807 
14808 	/* Tick values in micro seconds.
14809 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14810 	 */
14811 	coal = &bp->rx_coal;
14812 	coal->coal_ticks = 10;
14813 	coal->coal_bufs = 30;
14814 	coal->coal_ticks_irq = 1;
14815 	coal->coal_bufs_irq = 2;
14816 	coal->idle_thresh = 50;
14817 	coal->bufs_per_record = 2;
14818 	coal->budget = 64;		/* NAPI budget */
14819 	coal->flags = flags;
14820 
14821 	coal = &bp->tx_coal;
14822 	coal->coal_ticks = 28;
14823 	coal->coal_bufs = 30;
14824 	coal->coal_ticks_irq = 2;
14825 	coal->coal_bufs_irq = 2;
14826 	coal->bufs_per_record = 1;
14827 	coal->flags = flags;
14828 
14829 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14830 }
14831 
14832 /* FW that pre-reserves 1 VNIC per function */
14833 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14834 {
14835 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14836 
14837 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14838 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14839 		return true;
14840 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14841 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14842 		return true;
14843 	return false;
14844 }
14845 
14846 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
14847 {
14848 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
14849 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
14850 	int rc;
14851 
14852 	bp->max_pfcwd_tmo_ms = 0;
14853 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
14854 	if (rc)
14855 		return;
14856 	resp = hwrm_req_hold(bp, req);
14857 	rc = hwrm_req_send_silent(bp, req);
14858 	if (!rc)
14859 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
14860 	hwrm_req_drop(bp, req);
14861 }
14862 
14863 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14864 {
14865 	int rc;
14866 
14867 	bp->fw_cap = 0;
14868 	rc = bnxt_hwrm_ver_get(bp);
14869 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14870 	 * so wait before continuing with recovery.
14871 	 */
14872 	if (rc)
14873 		msleep(100);
14874 	bnxt_try_map_fw_health_reg(bp);
14875 	if (rc) {
14876 		rc = bnxt_try_recover_fw(bp);
14877 		if (rc)
14878 			return rc;
14879 		rc = bnxt_hwrm_ver_get(bp);
14880 		if (rc)
14881 			return rc;
14882 	}
14883 
14884 	bnxt_nvm_cfg_ver_get(bp);
14885 
14886 	rc = bnxt_hwrm_func_reset(bp);
14887 	if (rc)
14888 		return -ENODEV;
14889 
14890 	bnxt_hwrm_fw_set_time(bp);
14891 	return 0;
14892 }
14893 
14894 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14895 {
14896 	int rc;
14897 
14898 	/* Get the MAX capabilities for this function */
14899 	rc = bnxt_hwrm_func_qcaps(bp);
14900 	if (rc) {
14901 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14902 			   rc);
14903 		return -ENODEV;
14904 	}
14905 
14906 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14907 	if (rc)
14908 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14909 			    rc);
14910 
14911 	if (bnxt_alloc_fw_health(bp)) {
14912 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14913 	} else {
14914 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14915 		if (rc)
14916 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14917 				    rc);
14918 	}
14919 
14920 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14921 	if (rc)
14922 		return -ENODEV;
14923 
14924 	rc = bnxt_alloc_crash_dump_mem(bp);
14925 	if (rc)
14926 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14927 			    rc);
14928 	if (!rc) {
14929 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14930 		if (rc) {
14931 			bnxt_free_crash_dump_mem(bp);
14932 			netdev_warn(bp->dev,
14933 				    "hwrm crash dump mem failure rc: %d\n", rc);
14934 		}
14935 	}
14936 
14937 	if (bnxt_fw_pre_resv_vnics(bp))
14938 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14939 
14940 	bnxt_hwrm_pfcwd_qcaps(bp);
14941 	bnxt_hwrm_func_qcfg(bp);
14942 	bnxt_hwrm_vnic_qcaps(bp);
14943 	bnxt_hwrm_port_led_qcaps(bp);
14944 	bnxt_ethtool_init(bp);
14945 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14946 		__bnxt_hwrm_ptp_qcfg(bp);
14947 	bnxt_dcb_init(bp);
14948 	bnxt_hwmon_init(bp);
14949 	return 0;
14950 }
14951 
14952 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14953 {
14954 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14955 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14956 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14957 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14958 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14959 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14960 		bp->rss_hash_delta = bp->rss_hash_cfg;
14961 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14962 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14963 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14964 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14965 	}
14966 }
14967 
14968 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14969 {
14970 	struct net_device *dev = bp->dev;
14971 
14972 	dev->hw_features &= ~NETIF_F_NTUPLE;
14973 	dev->features &= ~NETIF_F_NTUPLE;
14974 	bp->flags &= ~BNXT_FLAG_RFS;
14975 	if (bnxt_rfs_supported(bp)) {
14976 		dev->hw_features |= NETIF_F_NTUPLE;
14977 		if (bnxt_rfs_capable(bp, false)) {
14978 			bp->flags |= BNXT_FLAG_RFS;
14979 			dev->features |= NETIF_F_NTUPLE;
14980 		}
14981 	}
14982 }
14983 
14984 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14985 {
14986 	struct pci_dev *pdev = bp->pdev;
14987 
14988 	bnxt_set_dflt_rss_hash_type(bp);
14989 	bnxt_set_dflt_rfs(bp);
14990 
14991 	bnxt_get_wol_settings(bp);
14992 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14993 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14994 	else
14995 		device_set_wakeup_capable(&pdev->dev, false);
14996 
14997 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14998 	bnxt_hwrm_coal_params_qcaps(bp);
14999 }
15000 
15001 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15002 
15003 int bnxt_fw_init_one(struct bnxt *bp)
15004 {
15005 	int rc;
15006 
15007 	rc = bnxt_fw_init_one_p1(bp);
15008 	if (rc) {
15009 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15010 		return rc;
15011 	}
15012 	rc = bnxt_fw_init_one_p2(bp);
15013 	if (rc) {
15014 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15015 		return rc;
15016 	}
15017 	rc = bnxt_probe_phy(bp, false);
15018 	if (rc)
15019 		return rc;
15020 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15021 	if (rc)
15022 		return rc;
15023 
15024 	bnxt_fw_init_one_p3(bp);
15025 	return 0;
15026 }
15027 
15028 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15029 {
15030 	struct bnxt_fw_health *fw_health = bp->fw_health;
15031 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15032 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15033 	u32 reg_type, reg_off, delay_msecs;
15034 
15035 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15036 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15037 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15038 	switch (reg_type) {
15039 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15040 		pci_write_config_dword(bp->pdev, reg_off, val);
15041 		break;
15042 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15043 		writel(reg_off & BNXT_GRC_BASE_MASK,
15044 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15045 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15046 		fallthrough;
15047 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15048 		writel(val, bp->bar0 + reg_off);
15049 		break;
15050 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15051 		writel(val, bp->bar1 + reg_off);
15052 		break;
15053 	}
15054 	if (delay_msecs) {
15055 		pci_read_config_dword(bp->pdev, 0, &val);
15056 		msleep(delay_msecs);
15057 	}
15058 }
15059 
15060 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15061 {
15062 	struct hwrm_func_qcfg_output *resp;
15063 	struct hwrm_func_qcfg_input *req;
15064 	bool result = true; /* firmware will enforce if unknown */
15065 
15066 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15067 		return result;
15068 
15069 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15070 		return result;
15071 
15072 	req->fid = cpu_to_le16(0xffff);
15073 	resp = hwrm_req_hold(bp, req);
15074 	if (!hwrm_req_send(bp, req))
15075 		result = !!(le16_to_cpu(resp->flags) &
15076 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15077 	hwrm_req_drop(bp, req);
15078 	return result;
15079 }
15080 
15081 static void bnxt_reset_all(struct bnxt *bp)
15082 {
15083 	struct bnxt_fw_health *fw_health = bp->fw_health;
15084 	int i, rc;
15085 
15086 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15087 		bnxt_fw_reset_via_optee(bp);
15088 		bp->fw_reset_timestamp = jiffies;
15089 		return;
15090 	}
15091 
15092 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15093 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15094 			bnxt_fw_reset_writel(bp, i);
15095 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15096 		struct hwrm_fw_reset_input *req;
15097 
15098 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15099 		if (!rc) {
15100 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15101 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15102 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15103 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15104 			rc = hwrm_req_send(bp, req);
15105 		}
15106 		if (rc != -ENODEV)
15107 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15108 	}
15109 	bp->fw_reset_timestamp = jiffies;
15110 }
15111 
15112 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15113 {
15114 	return time_after(jiffies, bp->fw_reset_timestamp +
15115 			  (bp->fw_reset_max_dsecs * HZ / 10));
15116 }
15117 
15118 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15119 {
15120 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15121 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15122 		bnxt_dl_health_fw_status_update(bp, false);
15123 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15124 	netif_close(bp->dev);
15125 }
15126 
15127 static void bnxt_fw_reset_task(struct work_struct *work)
15128 {
15129 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15130 	int rc = 0;
15131 
15132 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15133 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15134 		return;
15135 	}
15136 
15137 	switch (bp->fw_reset_state) {
15138 	case BNXT_FW_RESET_STATE_POLL_VF: {
15139 		int n = bnxt_get_registered_vfs(bp);
15140 		int tmo;
15141 
15142 		if (n < 0) {
15143 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15144 				   n, jiffies_to_msecs(jiffies -
15145 				   bp->fw_reset_timestamp));
15146 			goto fw_reset_abort;
15147 		} else if (n > 0) {
15148 			if (bnxt_fw_reset_timeout(bp)) {
15149 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15150 				bp->fw_reset_state = 0;
15151 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15152 					   n);
15153 				goto ulp_start;
15154 			}
15155 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15156 			return;
15157 		}
15158 		bp->fw_reset_timestamp = jiffies;
15159 		netdev_lock(bp->dev);
15160 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15161 			bnxt_fw_reset_abort(bp, rc);
15162 			netdev_unlock(bp->dev);
15163 			goto ulp_start;
15164 		}
15165 		bnxt_fw_reset_close(bp);
15166 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15167 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15168 			tmo = HZ / 10;
15169 		} else {
15170 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15171 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15172 		}
15173 		netdev_unlock(bp->dev);
15174 		bnxt_queue_fw_reset_work(bp, tmo);
15175 		return;
15176 	}
15177 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15178 		u32 val;
15179 
15180 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15181 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15182 		    !bnxt_fw_reset_timeout(bp)) {
15183 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15184 			return;
15185 		}
15186 
15187 		if (!bp->fw_health->primary) {
15188 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15189 
15190 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15191 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15192 			return;
15193 		}
15194 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15195 	}
15196 		fallthrough;
15197 	case BNXT_FW_RESET_STATE_RESET_FW:
15198 		bnxt_reset_all(bp);
15199 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15200 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15201 		return;
15202 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15203 		bnxt_inv_fw_health_reg(bp);
15204 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15205 		    !bp->fw_reset_min_dsecs) {
15206 			u16 val;
15207 
15208 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15209 			if (val == 0xffff) {
15210 				if (bnxt_fw_reset_timeout(bp)) {
15211 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15212 					rc = -ETIMEDOUT;
15213 					goto fw_reset_abort;
15214 				}
15215 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15216 				return;
15217 			}
15218 		}
15219 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15220 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15221 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15222 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15223 			bnxt_dl_remote_reload(bp);
15224 		if (pci_enable_device(bp->pdev)) {
15225 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15226 			rc = -ENODEV;
15227 			goto fw_reset_abort;
15228 		}
15229 		pci_set_master(bp->pdev);
15230 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15231 		fallthrough;
15232 	case BNXT_FW_RESET_STATE_POLL_FW:
15233 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15234 		rc = bnxt_hwrm_poll(bp);
15235 		if (rc) {
15236 			if (bnxt_fw_reset_timeout(bp)) {
15237 				netdev_err(bp->dev, "Firmware reset aborted\n");
15238 				goto fw_reset_abort_status;
15239 			}
15240 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15241 			return;
15242 		}
15243 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15244 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15245 		fallthrough;
15246 	case BNXT_FW_RESET_STATE_OPENING:
15247 		while (!netdev_trylock(bp->dev)) {
15248 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15249 			return;
15250 		}
15251 		rc = bnxt_open(bp->dev);
15252 		if (rc) {
15253 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15254 			bnxt_fw_reset_abort(bp, rc);
15255 			netdev_unlock(bp->dev);
15256 			goto ulp_start;
15257 		}
15258 
15259 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15260 		    bp->fw_health->enabled) {
15261 			bp->fw_health->last_fw_reset_cnt =
15262 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15263 		}
15264 		bp->fw_reset_state = 0;
15265 		/* Make sure fw_reset_state is 0 before clearing the flag */
15266 		smp_mb__before_atomic();
15267 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15268 		bnxt_ptp_reapply_pps(bp);
15269 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15270 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15271 			bnxt_dl_health_fw_recovery_done(bp);
15272 			bnxt_dl_health_fw_status_update(bp, true);
15273 		}
15274 		netdev_unlock(bp->dev);
15275 		bnxt_ulp_start(bp, 0);
15276 		bnxt_reenable_sriov(bp);
15277 		netdev_lock(bp->dev);
15278 		bnxt_vf_reps_alloc(bp);
15279 		bnxt_vf_reps_open(bp);
15280 		netdev_unlock(bp->dev);
15281 		break;
15282 	}
15283 	return;
15284 
15285 fw_reset_abort_status:
15286 	if (bp->fw_health->status_reliable ||
15287 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15288 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15289 
15290 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15291 	}
15292 fw_reset_abort:
15293 	netdev_lock(bp->dev);
15294 	bnxt_fw_reset_abort(bp, rc);
15295 	netdev_unlock(bp->dev);
15296 ulp_start:
15297 	bnxt_ulp_start(bp, rc);
15298 }
15299 
15300 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15301 {
15302 	int rc;
15303 	struct bnxt *bp = netdev_priv(dev);
15304 
15305 	SET_NETDEV_DEV(dev, &pdev->dev);
15306 
15307 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15308 	rc = pci_enable_device(pdev);
15309 	if (rc) {
15310 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15311 		goto init_err;
15312 	}
15313 
15314 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15315 		dev_err(&pdev->dev,
15316 			"Cannot find PCI device base address, aborting\n");
15317 		rc = -ENODEV;
15318 		goto init_err_disable;
15319 	}
15320 
15321 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15322 	if (rc) {
15323 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15324 		goto init_err_disable;
15325 	}
15326 
15327 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15328 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15329 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15330 		rc = -EIO;
15331 		goto init_err_release;
15332 	}
15333 
15334 	pci_set_master(pdev);
15335 
15336 	bp->dev = dev;
15337 	bp->pdev = pdev;
15338 
15339 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15340 	 * determines the BAR size.
15341 	 */
15342 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15343 	if (!bp->bar0) {
15344 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15345 		rc = -ENOMEM;
15346 		goto init_err_release;
15347 	}
15348 
15349 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15350 	if (!bp->bar2) {
15351 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15352 		rc = -ENOMEM;
15353 		goto init_err_release;
15354 	}
15355 
15356 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15357 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15358 
15359 	spin_lock_init(&bp->ntp_fltr_lock);
15360 #if BITS_PER_LONG == 32
15361 	spin_lock_init(&bp->db_lock);
15362 #endif
15363 
15364 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15365 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15366 
15367 	timer_setup(&bp->timer, bnxt_timer, 0);
15368 	bp->current_interval = BNXT_TIMER_INTERVAL;
15369 
15370 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15371 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15372 
15373 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15374 	return 0;
15375 
15376 init_err_release:
15377 	bnxt_unmap_bars(bp, pdev);
15378 	pci_release_regions(pdev);
15379 
15380 init_err_disable:
15381 	pci_disable_device(pdev);
15382 
15383 init_err:
15384 	return rc;
15385 }
15386 
15387 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15388 {
15389 	struct sockaddr *addr = p;
15390 	struct bnxt *bp = netdev_priv(dev);
15391 	int rc = 0;
15392 
15393 	netdev_assert_locked(dev);
15394 
15395 	if (!is_valid_ether_addr(addr->sa_data))
15396 		return -EADDRNOTAVAIL;
15397 
15398 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15399 		return 0;
15400 
15401 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15402 	if (rc)
15403 		return rc;
15404 
15405 	eth_hw_addr_set(dev, addr->sa_data);
15406 	bnxt_clear_usr_fltrs(bp, true);
15407 	if (netif_running(dev)) {
15408 		bnxt_close_nic(bp, false, false);
15409 		rc = bnxt_open_nic(bp, false, false);
15410 	}
15411 
15412 	return rc;
15413 }
15414 
15415 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15416 {
15417 	struct bnxt *bp = netdev_priv(dev);
15418 
15419 	netdev_assert_locked(dev);
15420 
15421 	if (netif_running(dev))
15422 		bnxt_close_nic(bp, true, false);
15423 
15424 	WRITE_ONCE(dev->mtu, new_mtu);
15425 
15426 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15427 	 * program is attached.  We need to set the AGG rings settings and
15428 	 * rx_skb_func accordingly.
15429 	 */
15430 	if (READ_ONCE(bp->xdp_prog))
15431 		bnxt_set_rx_skb_mode(bp, true);
15432 
15433 	bnxt_set_ring_params(bp);
15434 
15435 	if (netif_running(dev))
15436 		return bnxt_open_nic(bp, true, false);
15437 
15438 	return 0;
15439 }
15440 
15441 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15442 {
15443 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15444 
15445 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15446 			       tx_cp + bp->rx_nr_rings;
15447 }
15448 
15449 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15450 {
15451 	struct bnxt *bp = netdev_priv(dev);
15452 	bool sh = false;
15453 	int rc;
15454 
15455 	if (tc > bp->max_tc) {
15456 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15457 			   tc, bp->max_tc);
15458 		return -EINVAL;
15459 	}
15460 
15461 	if (bp->num_tc == tc)
15462 		return 0;
15463 
15464 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15465 		sh = true;
15466 
15467 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15468 			      sh, tc, bp->tx_nr_rings_xdp);
15469 	if (rc)
15470 		return rc;
15471 
15472 	/* Needs to close the device and do hw resource re-allocations */
15473 	if (netif_running(bp->dev))
15474 		bnxt_close_nic(bp, true, false);
15475 
15476 	if (tc) {
15477 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15478 		netdev_set_num_tc(dev, tc);
15479 		bp->num_tc = tc;
15480 	} else {
15481 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15482 		netdev_reset_tc(dev);
15483 		bp->num_tc = 0;
15484 	}
15485 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15486 	bnxt_set_cp_rings(bp, sh);
15487 
15488 	if (netif_running(bp->dev))
15489 		return bnxt_open_nic(bp, true, false);
15490 
15491 	return 0;
15492 }
15493 
15494 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15495 				  void *cb_priv)
15496 {
15497 	struct bnxt *bp = cb_priv;
15498 
15499 	if (!bnxt_tc_flower_enabled(bp) ||
15500 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15501 		return -EOPNOTSUPP;
15502 
15503 	switch (type) {
15504 	case TC_SETUP_CLSFLOWER:
15505 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15506 	default:
15507 		return -EOPNOTSUPP;
15508 	}
15509 }
15510 
15511 LIST_HEAD(bnxt_block_cb_list);
15512 
15513 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15514 			 void *type_data)
15515 {
15516 	struct bnxt *bp = netdev_priv(dev);
15517 
15518 	switch (type) {
15519 	case TC_SETUP_BLOCK:
15520 		return flow_block_cb_setup_simple(type_data,
15521 						  &bnxt_block_cb_list,
15522 						  bnxt_setup_tc_block_cb,
15523 						  bp, bp, true);
15524 	case TC_SETUP_QDISC_MQPRIO: {
15525 		struct tc_mqprio_qopt *mqprio = type_data;
15526 
15527 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15528 
15529 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15530 	}
15531 	default:
15532 		return -EOPNOTSUPP;
15533 	}
15534 }
15535 
15536 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15537 			    const struct sk_buff *skb)
15538 {
15539 	struct bnxt_vnic_info *vnic;
15540 
15541 	if (skb)
15542 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15543 
15544 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15545 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15546 }
15547 
15548 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15549 			   u32 idx)
15550 {
15551 	struct hlist_head *head;
15552 	int bit_id;
15553 
15554 	spin_lock_bh(&bp->ntp_fltr_lock);
15555 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15556 	if (bit_id < 0) {
15557 		spin_unlock_bh(&bp->ntp_fltr_lock);
15558 		return -ENOMEM;
15559 	}
15560 
15561 	fltr->base.sw_id = (u16)bit_id;
15562 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15563 	fltr->base.flags |= BNXT_ACT_RING_DST;
15564 	head = &bp->ntp_fltr_hash_tbl[idx];
15565 	hlist_add_head_rcu(&fltr->base.hash, head);
15566 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15567 	bnxt_insert_usr_fltr(bp, &fltr->base);
15568 	bp->ntp_fltr_count++;
15569 	spin_unlock_bh(&bp->ntp_fltr_lock);
15570 	return 0;
15571 }
15572 
15573 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15574 			    struct bnxt_ntuple_filter *f2)
15575 {
15576 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15577 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15578 	struct flow_keys *keys1 = &f1->fkeys;
15579 	struct flow_keys *keys2 = &f2->fkeys;
15580 
15581 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15582 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15583 		return false;
15584 
15585 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15586 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15587 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15588 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15589 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15590 			return false;
15591 	} else {
15592 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15593 				     &keys2->addrs.v6addrs.src) ||
15594 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15595 				     &masks2->addrs.v6addrs.src) ||
15596 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15597 				     &keys2->addrs.v6addrs.dst) ||
15598 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15599 				     &masks2->addrs.v6addrs.dst))
15600 			return false;
15601 	}
15602 
15603 	return keys1->ports.src == keys2->ports.src &&
15604 	       masks1->ports.src == masks2->ports.src &&
15605 	       keys1->ports.dst == keys2->ports.dst &&
15606 	       masks1->ports.dst == masks2->ports.dst &&
15607 	       keys1->control.flags == keys2->control.flags &&
15608 	       f1->l2_fltr == f2->l2_fltr;
15609 }
15610 
15611 struct bnxt_ntuple_filter *
15612 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15613 				struct bnxt_ntuple_filter *fltr, u32 idx)
15614 {
15615 	struct bnxt_ntuple_filter *f;
15616 	struct hlist_head *head;
15617 
15618 	head = &bp->ntp_fltr_hash_tbl[idx];
15619 	hlist_for_each_entry_rcu(f, head, base.hash) {
15620 		if (bnxt_fltr_match(f, fltr))
15621 			return f;
15622 	}
15623 	return NULL;
15624 }
15625 
15626 #ifdef CONFIG_RFS_ACCEL
15627 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15628 			      u16 rxq_index, u32 flow_id)
15629 {
15630 	struct bnxt *bp = netdev_priv(dev);
15631 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15632 	struct flow_keys *fkeys;
15633 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15634 	struct bnxt_l2_filter *l2_fltr;
15635 	int rc = 0, idx;
15636 	u32 flags;
15637 
15638 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15639 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15640 		atomic_inc(&l2_fltr->refcnt);
15641 	} else {
15642 		struct bnxt_l2_key key;
15643 
15644 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15645 		key.vlan = 0;
15646 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15647 		if (!l2_fltr)
15648 			return -EINVAL;
15649 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15650 			bnxt_del_l2_filter(bp, l2_fltr);
15651 			return -EINVAL;
15652 		}
15653 	}
15654 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
15655 	if (!new_fltr) {
15656 		bnxt_del_l2_filter(bp, l2_fltr);
15657 		return -ENOMEM;
15658 	}
15659 
15660 	fkeys = &new_fltr->fkeys;
15661 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15662 		rc = -EPROTONOSUPPORT;
15663 		goto err_free;
15664 	}
15665 
15666 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15667 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15668 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15669 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15670 		rc = -EPROTONOSUPPORT;
15671 		goto err_free;
15672 	}
15673 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15674 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15675 		if (bp->hwrm_spec_code < 0x10601) {
15676 			rc = -EPROTONOSUPPORT;
15677 			goto err_free;
15678 		}
15679 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15680 	}
15681 	flags = fkeys->control.flags;
15682 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15683 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15684 		rc = -EPROTONOSUPPORT;
15685 		goto err_free;
15686 	}
15687 	new_fltr->l2_fltr = l2_fltr;
15688 
15689 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15690 	rcu_read_lock();
15691 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15692 	if (fltr) {
15693 		rc = fltr->base.sw_id;
15694 		rcu_read_unlock();
15695 		goto err_free;
15696 	}
15697 	rcu_read_unlock();
15698 
15699 	new_fltr->flow_id = flow_id;
15700 	new_fltr->base.rxq = rxq_index;
15701 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15702 	if (!rc) {
15703 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15704 		return new_fltr->base.sw_id;
15705 	}
15706 
15707 err_free:
15708 	bnxt_del_l2_filter(bp, l2_fltr);
15709 	kfree(new_fltr);
15710 	return rc;
15711 }
15712 #endif
15713 
15714 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15715 {
15716 	spin_lock_bh(&bp->ntp_fltr_lock);
15717 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15718 		spin_unlock_bh(&bp->ntp_fltr_lock);
15719 		return;
15720 	}
15721 	hlist_del_rcu(&fltr->base.hash);
15722 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15723 	bp->ntp_fltr_count--;
15724 	spin_unlock_bh(&bp->ntp_fltr_lock);
15725 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15726 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15727 	kfree_rcu(fltr, base.rcu);
15728 }
15729 
15730 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15731 {
15732 #ifdef CONFIG_RFS_ACCEL
15733 	int i;
15734 
15735 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15736 		struct hlist_head *head;
15737 		struct hlist_node *tmp;
15738 		struct bnxt_ntuple_filter *fltr;
15739 		int rc;
15740 
15741 		head = &bp->ntp_fltr_hash_tbl[i];
15742 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15743 			bool del = false;
15744 
15745 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15746 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15747 					continue;
15748 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15749 							fltr->flow_id,
15750 							fltr->base.sw_id)) {
15751 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15752 									 fltr);
15753 					del = true;
15754 				}
15755 			} else {
15756 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15757 								       fltr);
15758 				if (rc)
15759 					del = true;
15760 				else
15761 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15762 			}
15763 
15764 			if (del)
15765 				bnxt_del_ntp_filter(bp, fltr);
15766 		}
15767 	}
15768 #endif
15769 }
15770 
15771 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15772 				    unsigned int entry, struct udp_tunnel_info *ti)
15773 {
15774 	struct bnxt *bp = netdev_priv(netdev);
15775 	unsigned int cmd;
15776 
15777 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15778 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15779 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15780 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15781 	else
15782 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15783 
15784 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15785 }
15786 
15787 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15788 				      unsigned int entry, struct udp_tunnel_info *ti)
15789 {
15790 	struct bnxt *bp = netdev_priv(netdev);
15791 	unsigned int cmd;
15792 
15793 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15794 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15795 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15796 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15797 	else
15798 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15799 
15800 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15801 }
15802 
15803 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15804 	.set_port	= bnxt_udp_tunnel_set_port,
15805 	.unset_port	= bnxt_udp_tunnel_unset_port,
15806 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15807 	.tables		= {
15808 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15809 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15810 	},
15811 }, bnxt_udp_tunnels_p7 = {
15812 	.set_port	= bnxt_udp_tunnel_set_port,
15813 	.unset_port	= bnxt_udp_tunnel_unset_port,
15814 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15815 	.tables		= {
15816 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15817 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15818 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15819 	},
15820 };
15821 
15822 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15823 			       struct net_device *dev, u32 filter_mask,
15824 			       int nlflags)
15825 {
15826 	struct bnxt *bp = netdev_priv(dev);
15827 
15828 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15829 				       nlflags, filter_mask, NULL);
15830 }
15831 
15832 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15833 			       u16 flags, struct netlink_ext_ack *extack)
15834 {
15835 	struct bnxt *bp = netdev_priv(dev);
15836 	struct nlattr *attr, *br_spec;
15837 	int rem, rc = 0;
15838 
15839 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15840 		return -EOPNOTSUPP;
15841 
15842 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15843 	if (!br_spec)
15844 		return -EINVAL;
15845 
15846 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15847 		u16 mode;
15848 
15849 		mode = nla_get_u16(attr);
15850 		if (mode == bp->br_mode)
15851 			break;
15852 
15853 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15854 		if (!rc)
15855 			bp->br_mode = mode;
15856 		break;
15857 	}
15858 	return rc;
15859 }
15860 
15861 int bnxt_get_port_parent_id(struct net_device *dev,
15862 			    struct netdev_phys_item_id *ppid)
15863 {
15864 	struct bnxt *bp = netdev_priv(dev);
15865 
15866 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15867 		return -EOPNOTSUPP;
15868 
15869 	/* The PF and it's VF-reps only support the switchdev framework */
15870 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15871 		return -EOPNOTSUPP;
15872 
15873 	ppid->id_len = sizeof(bp->dsn);
15874 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15875 
15876 	return 0;
15877 }
15878 
15879 static const struct net_device_ops bnxt_netdev_ops = {
15880 	.ndo_open		= bnxt_open,
15881 	.ndo_start_xmit		= bnxt_start_xmit,
15882 	.ndo_stop		= bnxt_close,
15883 	.ndo_get_stats64	= bnxt_get_stats64,
15884 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15885 	.ndo_eth_ioctl		= bnxt_ioctl,
15886 	.ndo_validate_addr	= eth_validate_addr,
15887 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15888 	.ndo_change_mtu		= bnxt_change_mtu,
15889 	.ndo_fix_features	= bnxt_fix_features,
15890 	.ndo_set_features	= bnxt_set_features,
15891 	.ndo_features_check	= bnxt_features_check,
15892 	.ndo_tx_timeout		= bnxt_tx_timeout,
15893 #ifdef CONFIG_BNXT_SRIOV
15894 	.ndo_get_vf_config	= bnxt_get_vf_config,
15895 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15896 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15897 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15898 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15899 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15900 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15901 #endif
15902 	.ndo_setup_tc           = bnxt_setup_tc,
15903 #ifdef CONFIG_RFS_ACCEL
15904 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15905 #endif
15906 	.ndo_bpf		= bnxt_xdp,
15907 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15908 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15909 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15910 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
15911 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
15912 };
15913 
15914 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
15915 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
15916 };
15917 
15918 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15919 				    struct netdev_queue_stats_rx *stats)
15920 {
15921 	struct bnxt *bp = netdev_priv(dev);
15922 	struct bnxt_cp_ring_info *cpr;
15923 	u64 *sw;
15924 
15925 	if (!bp->bnapi)
15926 		return;
15927 
15928 	cpr = &bp->bnapi[i]->cp_ring;
15929 	sw = cpr->stats.sw_stats;
15930 
15931 	stats->packets = 0;
15932 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15933 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15934 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15935 
15936 	stats->bytes = 0;
15937 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15938 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15939 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15940 
15941 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15942 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
15943 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
15944 }
15945 
15946 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15947 				    struct netdev_queue_stats_tx *stats)
15948 {
15949 	struct bnxt *bp = netdev_priv(dev);
15950 	struct bnxt_napi *bnapi;
15951 	u64 *sw;
15952 
15953 	if (!bp->tx_ring)
15954 		return;
15955 
15956 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15957 	sw = bnapi->cp_ring.stats.sw_stats;
15958 
15959 	stats->packets = 0;
15960 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15961 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15962 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15963 
15964 	stats->bytes = 0;
15965 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15966 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15967 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15968 }
15969 
15970 static void bnxt_get_base_stats(struct net_device *dev,
15971 				struct netdev_queue_stats_rx *rx,
15972 				struct netdev_queue_stats_tx *tx)
15973 {
15974 	struct bnxt *bp = netdev_priv(dev);
15975 
15976 	rx->packets = bp->net_stats_prev.rx_packets;
15977 	rx->bytes = bp->net_stats_prev.rx_bytes;
15978 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
15979 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
15980 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
15981 
15982 	tx->packets = bp->net_stats_prev.tx_packets;
15983 	tx->bytes = bp->net_stats_prev.tx_bytes;
15984 }
15985 
15986 static const struct netdev_stat_ops bnxt_stat_ops = {
15987 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15988 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15989 	.get_base_stats		= bnxt_get_base_stats,
15990 };
15991 
15992 static void bnxt_queue_default_qcfg(struct net_device *dev,
15993 				    struct netdev_queue_config *qcfg)
15994 {
15995 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
15996 }
15997 
15998 static int bnxt_validate_qcfg(struct net_device *dev,
15999 			      struct netdev_queue_config *qcfg,
16000 			      struct netlink_ext_ack *extack)
16001 {
16002 	struct bnxt *bp = netdev_priv(dev);
16003 
16004 	/* Older chips need MSS calc so rx_page_size is not supported */
16005 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16006 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16007 		return -EINVAL;
16008 
16009 	if (!is_power_of_2(qcfg->rx_page_size))
16010 		return -ERANGE;
16011 
16012 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16013 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16014 		return -ERANGE;
16015 
16016 	return 0;
16017 }
16018 
16019 static int bnxt_queue_mem_alloc(struct net_device *dev,
16020 				struct netdev_queue_config *qcfg,
16021 				void *qmem, int idx)
16022 {
16023 	struct bnxt_rx_ring_info *rxr, *clone;
16024 	struct bnxt *bp = netdev_priv(dev);
16025 	struct bnxt_ring_struct *ring;
16026 	int rc;
16027 
16028 	if (!bp->rx_ring)
16029 		return -ENETDOWN;
16030 
16031 	rxr = &bp->rx_ring[idx];
16032 	clone = qmem;
16033 	memcpy(clone, rxr, sizeof(*rxr));
16034 	bnxt_init_rx_ring_struct(bp, clone);
16035 	bnxt_reset_rx_ring_struct(bp, clone);
16036 
16037 	clone->rx_prod = 0;
16038 	clone->rx_agg_prod = 0;
16039 	clone->rx_sw_agg_prod = 0;
16040 	clone->rx_next_cons = 0;
16041 	clone->need_head_pool = false;
16042 	clone->rx_page_size = qcfg->rx_page_size;
16043 
16044 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16045 	if (rc)
16046 		return rc;
16047 
16048 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16049 	if (rc < 0)
16050 		goto err_page_pool_destroy;
16051 
16052 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16053 					MEM_TYPE_PAGE_POOL,
16054 					clone->page_pool);
16055 	if (rc)
16056 		goto err_rxq_info_unreg;
16057 
16058 	ring = &clone->rx_ring_struct;
16059 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16060 	if (rc)
16061 		goto err_free_rx_ring;
16062 
16063 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16064 		ring = &clone->rx_agg_ring_struct;
16065 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16066 		if (rc)
16067 			goto err_free_rx_agg_ring;
16068 
16069 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16070 		if (rc)
16071 			goto err_free_rx_agg_ring;
16072 	}
16073 
16074 	if (bp->flags & BNXT_FLAG_TPA) {
16075 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16076 		if (rc)
16077 			goto err_free_tpa_info;
16078 	}
16079 
16080 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16081 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16082 
16083 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16084 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16085 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16086 	if (bp->flags & BNXT_FLAG_TPA)
16087 		bnxt_alloc_one_tpa_info_data(bp, clone);
16088 
16089 	return 0;
16090 
16091 err_free_tpa_info:
16092 	bnxt_free_one_tpa_info(bp, clone);
16093 err_free_rx_agg_ring:
16094 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16095 err_free_rx_ring:
16096 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16097 err_rxq_info_unreg:
16098 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16099 err_page_pool_destroy:
16100 	page_pool_destroy(clone->page_pool);
16101 	page_pool_destroy(clone->head_pool);
16102 	clone->page_pool = NULL;
16103 	clone->head_pool = NULL;
16104 	return rc;
16105 }
16106 
16107 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16108 {
16109 	struct bnxt_rx_ring_info *rxr = qmem;
16110 	struct bnxt *bp = netdev_priv(dev);
16111 	struct bnxt_ring_struct *ring;
16112 
16113 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16114 	bnxt_free_one_tpa_info(bp, rxr);
16115 
16116 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16117 
16118 	page_pool_destroy(rxr->page_pool);
16119 	page_pool_destroy(rxr->head_pool);
16120 	rxr->page_pool = NULL;
16121 	rxr->head_pool = NULL;
16122 
16123 	ring = &rxr->rx_ring_struct;
16124 	bnxt_free_ring(bp, &ring->ring_mem);
16125 
16126 	ring = &rxr->rx_agg_ring_struct;
16127 	bnxt_free_ring(bp, &ring->ring_mem);
16128 
16129 	kfree(rxr->rx_agg_bmap);
16130 	rxr->rx_agg_bmap = NULL;
16131 }
16132 
16133 static void bnxt_copy_rx_ring(struct bnxt *bp,
16134 			      struct bnxt_rx_ring_info *dst,
16135 			      struct bnxt_rx_ring_info *src)
16136 {
16137 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16138 	struct bnxt_ring_struct *dst_ring, *src_ring;
16139 	int i;
16140 
16141 	dst_ring = &dst->rx_ring_struct;
16142 	dst_rmem = &dst_ring->ring_mem;
16143 	src_ring = &src->rx_ring_struct;
16144 	src_rmem = &src_ring->ring_mem;
16145 
16146 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16147 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16148 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16149 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16150 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16151 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16152 
16153 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16154 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16155 	*dst_rmem->vmem = *src_rmem->vmem;
16156 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16157 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16158 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16159 	}
16160 
16161 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16162 		return;
16163 
16164 	dst_ring = &dst->rx_agg_ring_struct;
16165 	dst_rmem = &dst_ring->ring_mem;
16166 	src_ring = &src->rx_agg_ring_struct;
16167 	src_rmem = &src_ring->ring_mem;
16168 
16169 	dst->rx_page_size = src->rx_page_size;
16170 
16171 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16172 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16173 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16174 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16175 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16176 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16177 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16178 
16179 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16180 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16181 	*dst_rmem->vmem = *src_rmem->vmem;
16182 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16183 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16184 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16185 	}
16186 
16187 	dst->rx_agg_bmap = src->rx_agg_bmap;
16188 }
16189 
16190 static int bnxt_queue_start(struct net_device *dev,
16191 			    struct netdev_queue_config *qcfg,
16192 			    void *qmem, int idx)
16193 {
16194 	struct bnxt *bp = netdev_priv(dev);
16195 	struct bnxt_rx_ring_info *rxr, *clone;
16196 	struct bnxt_cp_ring_info *cpr;
16197 	struct bnxt_vnic_info *vnic;
16198 	struct bnxt_napi *bnapi;
16199 	int i, rc;
16200 	u16 mru;
16201 
16202 	rxr = &bp->rx_ring[idx];
16203 	clone = qmem;
16204 
16205 	rxr->rx_prod = clone->rx_prod;
16206 	rxr->rx_agg_prod = clone->rx_agg_prod;
16207 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16208 	rxr->rx_next_cons = clone->rx_next_cons;
16209 	rxr->rx_tpa = clone->rx_tpa;
16210 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16211 	rxr->page_pool = clone->page_pool;
16212 	rxr->head_pool = clone->head_pool;
16213 	rxr->xdp_rxq = clone->xdp_rxq;
16214 	rxr->need_head_pool = clone->need_head_pool;
16215 
16216 	bnxt_copy_rx_ring(bp, rxr, clone);
16217 
16218 	bnapi = rxr->bnapi;
16219 	cpr = &bnapi->cp_ring;
16220 
16221 	/* All rings have been reserved and previously allocated.
16222 	 * Reallocating with the same parameters should never fail.
16223 	 */
16224 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16225 	if (rc)
16226 		goto err_reset;
16227 
16228 	if (bp->tph_mode) {
16229 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16230 		if (rc)
16231 			goto err_reset;
16232 	}
16233 
16234 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16235 	if (rc)
16236 		goto err_reset;
16237 
16238 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16239 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16240 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16241 
16242 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16243 		rc = bnxt_tx_queue_start(bp, idx);
16244 		if (rc)
16245 			goto err_reset;
16246 	}
16247 
16248 	bnxt_enable_rx_page_pool(rxr);
16249 	napi_enable_locked(&bnapi->napi);
16250 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16251 
16252 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16253 	for (i = 0; i < bp->nr_vnics; i++) {
16254 		vnic = &bp->vnic_info[i];
16255 
16256 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16257 		if (rc)
16258 			return rc;
16259 	}
16260 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16261 
16262 err_reset:
16263 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16264 		   rc);
16265 	napi_enable_locked(&bnapi->napi);
16266 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16267 	bnxt_reset_task(bp, true);
16268 	return rc;
16269 }
16270 
16271 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16272 {
16273 	struct bnxt *bp = netdev_priv(dev);
16274 	struct bnxt_rx_ring_info *rxr;
16275 	struct bnxt_cp_ring_info *cpr;
16276 	struct bnxt_vnic_info *vnic;
16277 	struct bnxt_napi *bnapi;
16278 	int i;
16279 
16280 	for (i = 0; i < bp->nr_vnics; i++) {
16281 		vnic = &bp->vnic_info[i];
16282 
16283 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16284 	}
16285 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16286 	/* Make sure NAPI sees that the VNIC is disabled */
16287 	synchronize_net();
16288 	rxr = &bp->rx_ring[idx];
16289 	bnapi = rxr->bnapi;
16290 	cpr = &bnapi->cp_ring;
16291 	cancel_work_sync(&cpr->dim.work);
16292 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16293 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16294 	page_pool_disable_direct_recycling(rxr->page_pool);
16295 	if (bnxt_separate_head_pool(rxr))
16296 		page_pool_disable_direct_recycling(rxr->head_pool);
16297 
16298 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16299 		bnxt_tx_queue_stop(bp, idx);
16300 
16301 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16302 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16303 	 * after seeing the completion.
16304 	 */
16305 	napi_disable_locked(&bnapi->napi);
16306 
16307 	if (bp->tph_mode) {
16308 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16309 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16310 	}
16311 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16312 
16313 	memcpy(qmem, rxr, sizeof(*rxr));
16314 	bnxt_init_rx_ring_struct(bp, qmem);
16315 
16316 	return 0;
16317 }
16318 
16319 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16320 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16321 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16322 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16323 	.ndo_queue_start	= bnxt_queue_start,
16324 	.ndo_queue_stop		= bnxt_queue_stop,
16325 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16326 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16327 	.supported_params	= QCFG_RX_PAGE_SIZE,
16328 };
16329 
16330 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16331 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16332 };
16333 
16334 static void bnxt_remove_one(struct pci_dev *pdev)
16335 {
16336 	struct net_device *dev = pci_get_drvdata(pdev);
16337 	struct bnxt *bp = netdev_priv(dev);
16338 
16339 	if (BNXT_PF(bp))
16340 		__bnxt_sriov_disable(bp);
16341 
16342 	bnxt_rdma_aux_device_del(bp);
16343 
16344 	unregister_netdev(dev);
16345 	bnxt_ptp_clear(bp);
16346 
16347 	bnxt_rdma_aux_device_uninit(bp);
16348 
16349 	bnxt_free_l2_filters(bp, true);
16350 	bnxt_free_ntp_fltrs(bp, true);
16351 	WARN_ON(bp->num_rss_ctx);
16352 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16353 	/* Flush any pending tasks */
16354 	cancel_work_sync(&bp->sp_task);
16355 	cancel_delayed_work_sync(&bp->fw_reset_task);
16356 	bp->sp_event = 0;
16357 
16358 	bnxt_dl_fw_reporters_destroy(bp);
16359 	bnxt_dl_unregister(bp);
16360 	bnxt_shutdown_tc(bp);
16361 
16362 	bnxt_clear_int_mode(bp);
16363 	bnxt_hwrm_func_drv_unrgtr(bp);
16364 	bnxt_free_hwrm_resources(bp);
16365 	bnxt_hwmon_uninit(bp);
16366 	bnxt_ethtool_free(bp);
16367 	bnxt_dcb_free(bp);
16368 	kfree(bp->ptp_cfg);
16369 	bp->ptp_cfg = NULL;
16370 	kfree(bp->fw_health);
16371 	bp->fw_health = NULL;
16372 	bnxt_cleanup_pci(bp);
16373 	bnxt_free_ctx_mem(bp, true);
16374 	bnxt_free_crash_dump_mem(bp);
16375 	kfree(bp->rss_indir_tbl);
16376 	bp->rss_indir_tbl = NULL;
16377 	bnxt_free_port_stats(bp);
16378 	free_netdev(dev);
16379 }
16380 
16381 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16382 {
16383 	int rc = 0;
16384 	struct bnxt_link_info *link_info = &bp->link_info;
16385 
16386 	bp->phy_flags = 0;
16387 	rc = bnxt_hwrm_phy_qcaps(bp);
16388 	if (rc) {
16389 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16390 			   rc);
16391 		return rc;
16392 	}
16393 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16394 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16395 	else
16396 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16397 
16398 	bp->mac_flags = 0;
16399 	bnxt_hwrm_mac_qcaps(bp);
16400 
16401 	if (!fw_dflt)
16402 		return 0;
16403 
16404 	mutex_lock(&bp->link_lock);
16405 	rc = bnxt_update_link(bp, false);
16406 	if (rc) {
16407 		mutex_unlock(&bp->link_lock);
16408 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16409 			   rc);
16410 		return rc;
16411 	}
16412 
16413 	/* Older firmware does not have supported_auto_speeds, so assume
16414 	 * that all supported speeds can be autonegotiated.
16415 	 */
16416 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16417 		link_info->support_auto_speeds = link_info->support_speeds;
16418 
16419 	bnxt_init_ethtool_link_settings(bp);
16420 	mutex_unlock(&bp->link_lock);
16421 	return 0;
16422 }
16423 
16424 static int bnxt_get_max_irq(struct pci_dev *pdev)
16425 {
16426 	u16 ctrl;
16427 
16428 	if (!pdev->msix_cap)
16429 		return 1;
16430 
16431 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16432 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16433 }
16434 
16435 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16436 				int *max_cp)
16437 {
16438 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16439 	int max_ring_grps = 0, max_irq;
16440 
16441 	*max_tx = hw_resc->max_tx_rings;
16442 	*max_rx = hw_resc->max_rx_rings;
16443 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16444 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16445 			bnxt_get_ulp_msix_num_in_use(bp),
16446 			hw_resc->max_stat_ctxs -
16447 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16448 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16449 		*max_cp = min_t(int, *max_cp, max_irq);
16450 	max_ring_grps = hw_resc->max_hw_ring_grps;
16451 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16452 		*max_cp -= 1;
16453 		*max_rx -= 2;
16454 	}
16455 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16456 		*max_rx >>= 1;
16457 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16458 		int rc;
16459 
16460 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16461 		if (rc) {
16462 			*max_rx = 0;
16463 			*max_tx = 0;
16464 		}
16465 		/* On P5 chips, max_cp output param should be available NQs */
16466 		*max_cp = max_irq;
16467 	}
16468 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16469 }
16470 
16471 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16472 {
16473 	int rx, tx, cp;
16474 
16475 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16476 	*max_rx = rx;
16477 	*max_tx = tx;
16478 	if (!rx || !tx || !cp)
16479 		return -ENOMEM;
16480 
16481 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16482 }
16483 
16484 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16485 			       bool shared)
16486 {
16487 	int rc;
16488 
16489 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16490 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16491 		/* Not enough rings, try disabling agg rings. */
16492 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16493 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16494 		if (rc) {
16495 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16496 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16497 			return rc;
16498 		}
16499 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16500 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16501 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16502 		bnxt_set_ring_params(bp);
16503 	}
16504 
16505 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16506 		int max_cp, max_stat, max_irq;
16507 
16508 		/* Reserve minimum resources for RoCE */
16509 		max_cp = bnxt_get_max_func_cp_rings(bp);
16510 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16511 		max_irq = bnxt_get_max_func_irqs(bp);
16512 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16513 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16514 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16515 			return 0;
16516 
16517 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16518 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16519 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16520 		max_cp = min_t(int, max_cp, max_irq);
16521 		max_cp = min_t(int, max_cp, max_stat);
16522 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16523 		if (rc)
16524 			rc = 0;
16525 	}
16526 	return rc;
16527 }
16528 
16529 /* In initial default shared ring setting, each shared ring must have a
16530  * RX/TX ring pair.
16531  */
16532 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16533 {
16534 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16535 	bp->rx_nr_rings = bp->cp_nr_rings;
16536 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16537 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16538 }
16539 
16540 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
16541 {
16542 	if (sh)
16543 		bnxt_trim_dflt_sh_rings(bp);
16544 	else
16545 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16546 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16547 	if (sh && READ_ONCE(bp->xdp_prog)) {
16548 		bnxt_set_xdp_tx_rings(bp);
16549 		bnxt_set_cp_rings(bp, true);
16550 	}
16551 }
16552 
16553 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16554 {
16555 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16556 	int avail_msix;
16557 
16558 	if (!bnxt_can_reserve_rings(bp))
16559 		return 0;
16560 
16561 	if (sh)
16562 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16563 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16564 	/* Reduce default rings on multi-port cards so that total default
16565 	 * rings do not exceed CPU count.
16566 	 */
16567 	if (bp->port_count > 1) {
16568 		int max_rings =
16569 			max_t(int, num_online_cpus() / bp->port_count, 1);
16570 
16571 		dflt_rings = min_t(int, dflt_rings, max_rings);
16572 	}
16573 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16574 	if (rc)
16575 		return rc;
16576 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16577 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16578 
16579 	bnxt_adj_dflt_rings(bp, sh);
16580 
16581 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16582 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16583 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16584 
16585 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16586 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16587 	}
16588 
16589 	rc = __bnxt_reserve_rings(bp);
16590 	if (rc && rc != -ENODEV)
16591 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16592 
16593 	bnxt_adj_tx_rings(bp);
16594 	if (sh)
16595 		bnxt_adj_dflt_rings(bp, true);
16596 
16597 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16598 	if (bnxt_need_reserve_rings(bp)) {
16599 		rc = __bnxt_reserve_rings(bp);
16600 		if (rc && rc != -ENODEV)
16601 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16602 		bnxt_adj_tx_rings(bp);
16603 	}
16604 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16605 		bp->rx_nr_rings++;
16606 		bp->cp_nr_rings++;
16607 	}
16608 	if (rc) {
16609 		bp->tx_nr_rings = 0;
16610 		bp->rx_nr_rings = 0;
16611 	}
16612 	return rc;
16613 }
16614 
16615 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16616 {
16617 	int rc;
16618 
16619 	if (bp->tx_nr_rings)
16620 		return 0;
16621 
16622 	bnxt_ulp_irq_stop(bp);
16623 	bnxt_clear_int_mode(bp);
16624 	rc = bnxt_set_dflt_rings(bp, true);
16625 	if (rc) {
16626 		if (BNXT_VF(bp) && rc == -ENODEV)
16627 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16628 		else
16629 			netdev_err(bp->dev, "Not enough rings available.\n");
16630 		goto init_dflt_ring_err;
16631 	}
16632 	rc = bnxt_init_int_mode(bp);
16633 	if (rc)
16634 		goto init_dflt_ring_err;
16635 
16636 	bnxt_adj_tx_rings(bp);
16637 
16638 	bnxt_set_dflt_rfs(bp);
16639 
16640 init_dflt_ring_err:
16641 	bnxt_ulp_irq_restart(bp, rc);
16642 	return rc;
16643 }
16644 
16645 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16646 {
16647 	int rc;
16648 
16649 	netdev_ops_assert_locked(bp->dev);
16650 	bnxt_hwrm_func_qcaps(bp);
16651 
16652 	if (netif_running(bp->dev))
16653 		__bnxt_close_nic(bp, true, false);
16654 
16655 	bnxt_ulp_irq_stop(bp);
16656 	bnxt_clear_int_mode(bp);
16657 	rc = bnxt_init_int_mode(bp);
16658 	bnxt_ulp_irq_restart(bp, rc);
16659 
16660 	if (netif_running(bp->dev)) {
16661 		if (rc)
16662 			netif_close(bp->dev);
16663 		else
16664 			rc = bnxt_open_nic(bp, true, false);
16665 	}
16666 
16667 	return rc;
16668 }
16669 
16670 static int bnxt_init_mac_addr(struct bnxt *bp)
16671 {
16672 	int rc = 0;
16673 
16674 	if (BNXT_PF(bp)) {
16675 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16676 	} else {
16677 #ifdef CONFIG_BNXT_SRIOV
16678 		struct bnxt_vf_info *vf = &bp->vf;
16679 		bool strict_approval = true;
16680 
16681 		if (is_valid_ether_addr(vf->mac_addr)) {
16682 			/* overwrite netdev dev_addr with admin VF MAC */
16683 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16684 			/* Older PF driver or firmware may not approve this
16685 			 * correctly.
16686 			 */
16687 			strict_approval = false;
16688 		} else {
16689 			eth_hw_addr_random(bp->dev);
16690 		}
16691 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16692 #endif
16693 	}
16694 	return rc;
16695 }
16696 
16697 static void bnxt_vpd_read_info(struct bnxt *bp)
16698 {
16699 	struct pci_dev *pdev = bp->pdev;
16700 	unsigned int vpd_size, kw_len;
16701 	int pos, size;
16702 	u8 *vpd_data;
16703 
16704 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16705 	if (IS_ERR(vpd_data)) {
16706 		pci_warn(pdev, "Unable to read VPD\n");
16707 		return;
16708 	}
16709 
16710 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16711 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16712 	if (pos < 0)
16713 		goto read_sn;
16714 
16715 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16716 	memcpy(bp->board_partno, &vpd_data[pos], size);
16717 
16718 read_sn:
16719 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16720 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16721 					   &kw_len);
16722 	if (pos < 0)
16723 		goto exit;
16724 
16725 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16726 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16727 exit:
16728 	kfree(vpd_data);
16729 }
16730 
16731 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16732 {
16733 	struct pci_dev *pdev = bp->pdev;
16734 	u64 qword;
16735 
16736 	qword = pci_get_dsn(pdev);
16737 	if (!qword) {
16738 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16739 		return -EOPNOTSUPP;
16740 	}
16741 
16742 	put_unaligned_le64(qword, dsn);
16743 
16744 	bp->flags |= BNXT_FLAG_DSN_VALID;
16745 	return 0;
16746 }
16747 
16748 static int bnxt_map_db_bar(struct bnxt *bp)
16749 {
16750 	if (!bp->db_size)
16751 		return -ENODEV;
16752 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16753 	if (!bp->bar1)
16754 		return -ENOMEM;
16755 	return 0;
16756 }
16757 
16758 void bnxt_print_device_info(struct bnxt *bp)
16759 {
16760 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16761 		    board_info[bp->board_idx].name,
16762 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16763 
16764 	pcie_print_link_status(bp->pdev);
16765 }
16766 
16767 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16768 {
16769 	struct bnxt_hw_resc *hw_resc;
16770 	struct net_device *dev;
16771 	struct bnxt *bp;
16772 	int rc, max_irqs;
16773 
16774 	if (pci_is_bridge(pdev))
16775 		return -ENODEV;
16776 
16777 	if (!pdev->msix_cap) {
16778 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16779 		return -ENODEV;
16780 	}
16781 
16782 	/* Clear any pending DMA transactions from crash kernel
16783 	 * while loading driver in capture kernel.
16784 	 */
16785 	if (is_kdump_kernel()) {
16786 		pci_clear_master(pdev);
16787 		pcie_flr(pdev);
16788 	}
16789 
16790 	max_irqs = bnxt_get_max_irq(pdev);
16791 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16792 				 max_irqs);
16793 	if (!dev)
16794 		return -ENOMEM;
16795 
16796 	bp = netdev_priv(dev);
16797 	bp->board_idx = ent->driver_data;
16798 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16799 	bnxt_set_max_func_irqs(bp, max_irqs);
16800 
16801 	if (bnxt_vf_pciid(bp->board_idx))
16802 		bp->flags |= BNXT_FLAG_VF;
16803 
16804 	/* No devlink port registration in case of a VF */
16805 	if (BNXT_PF(bp))
16806 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16807 
16808 	rc = bnxt_init_board(pdev, dev);
16809 	if (rc < 0)
16810 		goto init_err_free;
16811 
16812 	dev->netdev_ops = &bnxt_netdev_ops;
16813 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
16814 	dev->stat_ops = &bnxt_stat_ops;
16815 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16816 	dev->ethtool_ops = &bnxt_ethtool_ops;
16817 	pci_set_drvdata(pdev, dev);
16818 
16819 	rc = bnxt_alloc_hwrm_resources(bp);
16820 	if (rc)
16821 		goto init_err_pci_clean;
16822 
16823 	mutex_init(&bp->hwrm_cmd_lock);
16824 	mutex_init(&bp->link_lock);
16825 
16826 	rc = bnxt_fw_init_one_p1(bp);
16827 	if (rc)
16828 		goto init_err_pci_clean;
16829 
16830 	if (BNXT_PF(bp))
16831 		bnxt_vpd_read_info(bp);
16832 
16833 	if (BNXT_CHIP_P5_PLUS(bp)) {
16834 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16835 		if (BNXT_CHIP_P7(bp))
16836 			bp->flags |= BNXT_FLAG_CHIP_P7;
16837 	}
16838 
16839 	rc = bnxt_alloc_rss_indir_tbl(bp);
16840 	if (rc)
16841 		goto init_err_pci_clean;
16842 
16843 	rc = bnxt_fw_init_one_p2(bp);
16844 	if (rc)
16845 		goto init_err_pci_clean;
16846 
16847 	rc = bnxt_map_db_bar(bp);
16848 	if (rc) {
16849 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16850 			rc);
16851 		goto init_err_pci_clean;
16852 	}
16853 
16854 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16855 			   NETIF_F_TSO | NETIF_F_TSO6 |
16856 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16857 			   NETIF_F_GSO_IPXIP4 |
16858 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16859 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16860 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16861 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16862 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16863 
16864 	if (BNXT_SUPPORTS_TPA(bp))
16865 		dev->hw_features |= NETIF_F_LRO;
16866 
16867 	dev->hw_enc_features =
16868 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16869 			NETIF_F_TSO | NETIF_F_TSO6 |
16870 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16871 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16872 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16873 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16874 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16875 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16876 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16877 	else
16878 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16879 
16880 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16881 				    NETIF_F_GSO_GRE_CSUM;
16882 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16883 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16884 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16885 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16886 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16887 	if (BNXT_SUPPORTS_TPA(bp))
16888 		dev->hw_features |= NETIF_F_GRO_HW;
16889 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16890 	if (dev->features & NETIF_F_GRO_HW)
16891 		dev->features &= ~NETIF_F_LRO;
16892 	dev->priv_flags |= IFF_UNICAST_FLT;
16893 
16894 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16895 	if (bp->tso_max_segs)
16896 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16897 
16898 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16899 			    NETDEV_XDP_ACT_RX_SG;
16900 
16901 #ifdef CONFIG_BNXT_SRIOV
16902 	init_waitqueue_head(&bp->sriov_cfg_wait);
16903 #endif
16904 	if (BNXT_SUPPORTS_TPA(bp)) {
16905 		bp->gro_func = bnxt_gro_func_5730x;
16906 		if (BNXT_CHIP_P4(bp))
16907 			bp->gro_func = bnxt_gro_func_5731x;
16908 		else if (BNXT_CHIP_P5_PLUS(bp))
16909 			bp->gro_func = bnxt_gro_func_5750x;
16910 	}
16911 	if (!BNXT_CHIP_P4_PLUS(bp))
16912 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16913 
16914 	rc = bnxt_init_mac_addr(bp);
16915 	if (rc) {
16916 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16917 		rc = -EADDRNOTAVAIL;
16918 		goto init_err_pci_clean;
16919 	}
16920 
16921 	if (BNXT_PF(bp)) {
16922 		/* Read the adapter's DSN to use as the eswitch switch_id */
16923 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16924 	}
16925 
16926 	/* MTU range: 60 - FW defined max */
16927 	dev->min_mtu = ETH_ZLEN;
16928 	dev->max_mtu = bp->max_mtu;
16929 
16930 	rc = bnxt_probe_phy(bp, true);
16931 	if (rc)
16932 		goto init_err_pci_clean;
16933 
16934 	hw_resc = &bp->hw_resc;
16935 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16936 		       BNXT_L2_FLTR_MAX_FLTR;
16937 	/* Older firmware may not report these filters properly */
16938 	if (bp->max_fltr < BNXT_MAX_FLTR)
16939 		bp->max_fltr = BNXT_MAX_FLTR;
16940 	bnxt_init_l2_fltr_tbl(bp);
16941 	__bnxt_set_rx_skb_mode(bp, false);
16942 	bnxt_set_tpa_flags(bp);
16943 	bnxt_init_ring_params(bp);
16944 	bnxt_set_ring_params(bp);
16945 	bnxt_rdma_aux_device_init(bp);
16946 	rc = bnxt_set_dflt_rings(bp, true);
16947 	if (rc) {
16948 		if (BNXT_VF(bp) && rc == -ENODEV) {
16949 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16950 		} else {
16951 			netdev_err(bp->dev, "Not enough rings available.\n");
16952 			rc = -ENOMEM;
16953 		}
16954 		goto init_err_pci_clean;
16955 	}
16956 
16957 	bnxt_fw_init_one_p3(bp);
16958 
16959 	bnxt_init_dflt_coal(bp);
16960 
16961 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16962 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16963 
16964 	rc = bnxt_init_int_mode(bp);
16965 	if (rc)
16966 		goto init_err_pci_clean;
16967 
16968 	/* No TC has been set yet and rings may have been trimmed due to
16969 	 * limited MSIX, so we re-initialize the TX rings per TC.
16970 	 */
16971 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16972 
16973 	if (BNXT_PF(bp)) {
16974 		if (!bnxt_pf_wq) {
16975 			bnxt_pf_wq =
16976 				create_singlethread_workqueue("bnxt_pf_wq");
16977 			if (!bnxt_pf_wq) {
16978 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16979 				rc = -ENOMEM;
16980 				goto init_err_pci_clean;
16981 			}
16982 		}
16983 		rc = bnxt_init_tc(bp);
16984 		if (rc)
16985 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16986 				   rc);
16987 	}
16988 
16989 	bnxt_inv_fw_health_reg(bp);
16990 	rc = bnxt_dl_register(bp);
16991 	if (rc)
16992 		goto init_err_dl;
16993 
16994 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16995 
16996 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16997 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16998 
16999 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17000 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17001 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17002 	dev->netmem_tx = true;
17003 
17004 	rc = register_netdev(dev);
17005 	if (rc)
17006 		goto init_err_cleanup;
17007 
17008 	bnxt_dl_fw_reporters_create(bp);
17009 
17010 	bnxt_rdma_aux_device_add(bp);
17011 
17012 	bnxt_print_device_info(bp);
17013 
17014 	pci_save_state(pdev);
17015 
17016 	return 0;
17017 init_err_cleanup:
17018 	bnxt_rdma_aux_device_uninit(bp);
17019 	bnxt_dl_unregister(bp);
17020 init_err_dl:
17021 	bnxt_shutdown_tc(bp);
17022 	bnxt_clear_int_mode(bp);
17023 
17024 init_err_pci_clean:
17025 	bnxt_hwrm_func_drv_unrgtr(bp);
17026 	bnxt_ptp_clear(bp);
17027 	kfree(bp->ptp_cfg);
17028 	bp->ptp_cfg = NULL;
17029 	bnxt_free_hwrm_resources(bp);
17030 	bnxt_hwmon_uninit(bp);
17031 	bnxt_ethtool_free(bp);
17032 	kfree(bp->fw_health);
17033 	bp->fw_health = NULL;
17034 	bnxt_cleanup_pci(bp);
17035 	bnxt_free_ctx_mem(bp, true);
17036 	bnxt_free_crash_dump_mem(bp);
17037 	kfree(bp->rss_indir_tbl);
17038 	bp->rss_indir_tbl = NULL;
17039 
17040 init_err_free:
17041 	free_netdev(dev);
17042 	return rc;
17043 }
17044 
17045 static void bnxt_shutdown(struct pci_dev *pdev)
17046 {
17047 	struct net_device *dev = pci_get_drvdata(pdev);
17048 	struct bnxt *bp;
17049 
17050 	if (!dev)
17051 		return;
17052 
17053 	rtnl_lock();
17054 	netdev_lock(dev);
17055 	bp = netdev_priv(dev);
17056 	if (!bp)
17057 		goto shutdown_exit;
17058 
17059 	if (netif_running(dev))
17060 		netif_close(dev);
17061 
17062 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17063 		pcie_flr(pdev);
17064 		goto shutdown_exit;
17065 	}
17066 	bnxt_ptp_clear(bp);
17067 	bnxt_clear_int_mode(bp);
17068 	pci_disable_device(pdev);
17069 
17070 	if (system_state == SYSTEM_POWER_OFF) {
17071 		pci_wake_from_d3(pdev, bp->wol);
17072 		pci_set_power_state(pdev, PCI_D3hot);
17073 	}
17074 
17075 shutdown_exit:
17076 	netdev_unlock(dev);
17077 	rtnl_unlock();
17078 }
17079 
17080 #ifdef CONFIG_PM_SLEEP
17081 static int bnxt_suspend(struct device *device)
17082 {
17083 	struct net_device *dev = dev_get_drvdata(device);
17084 	struct bnxt *bp = netdev_priv(dev);
17085 	int rc = 0;
17086 
17087 	bnxt_ulp_stop(bp);
17088 
17089 	netdev_lock(dev);
17090 	if (netif_running(dev)) {
17091 		netif_device_detach(dev);
17092 		rc = bnxt_close(dev);
17093 	}
17094 	bnxt_hwrm_func_drv_unrgtr(bp);
17095 	bnxt_ptp_clear(bp);
17096 	pci_disable_device(bp->pdev);
17097 	bnxt_free_ctx_mem(bp, false);
17098 	netdev_unlock(dev);
17099 	return rc;
17100 }
17101 
17102 static int bnxt_resume(struct device *device)
17103 {
17104 	struct net_device *dev = dev_get_drvdata(device);
17105 	struct bnxt *bp = netdev_priv(dev);
17106 	int rc = 0;
17107 
17108 	netdev_lock(dev);
17109 	rc = pci_enable_device(bp->pdev);
17110 	if (rc) {
17111 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17112 			   rc);
17113 		goto resume_exit;
17114 	}
17115 	pci_set_master(bp->pdev);
17116 	if (bnxt_hwrm_ver_get(bp)) {
17117 		rc = -ENODEV;
17118 		goto resume_exit;
17119 	}
17120 	rc = bnxt_hwrm_func_reset(bp);
17121 	if (rc) {
17122 		rc = -EBUSY;
17123 		goto resume_exit;
17124 	}
17125 
17126 	rc = bnxt_hwrm_func_qcaps(bp);
17127 	if (rc)
17128 		goto resume_exit;
17129 
17130 	bnxt_clear_reservations(bp, true);
17131 
17132 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17133 		rc = -ENODEV;
17134 		goto resume_exit;
17135 	}
17136 	if (bp->fw_crash_mem)
17137 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17138 
17139 	if (bnxt_ptp_init(bp)) {
17140 		kfree(bp->ptp_cfg);
17141 		bp->ptp_cfg = NULL;
17142 	}
17143 	bnxt_get_wol_settings(bp);
17144 	if (netif_running(dev)) {
17145 		rc = bnxt_open(dev);
17146 		if (!rc)
17147 			netif_device_attach(dev);
17148 	}
17149 
17150 resume_exit:
17151 	netdev_unlock(bp->dev);
17152 	bnxt_ulp_start(bp, rc);
17153 	if (!rc)
17154 		bnxt_reenable_sriov(bp);
17155 	return rc;
17156 }
17157 
17158 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17159 #define BNXT_PM_OPS (&bnxt_pm_ops)
17160 
17161 #else
17162 
17163 #define BNXT_PM_OPS NULL
17164 
17165 #endif /* CONFIG_PM_SLEEP */
17166 
17167 /**
17168  * bnxt_io_error_detected - called when PCI error is detected
17169  * @pdev: Pointer to PCI device
17170  * @state: The current pci connection state
17171  *
17172  * This function is called after a PCI bus error affecting
17173  * this device has been detected.
17174  */
17175 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17176 					       pci_channel_state_t state)
17177 {
17178 	struct net_device *netdev = pci_get_drvdata(pdev);
17179 	struct bnxt *bp = netdev_priv(netdev);
17180 	bool abort = false;
17181 
17182 	netdev_info(netdev, "PCI I/O error detected\n");
17183 
17184 	bnxt_ulp_stop(bp);
17185 
17186 	netdev_lock(netdev);
17187 	netif_device_detach(netdev);
17188 
17189 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17190 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17191 		abort = true;
17192 	}
17193 
17194 	if (abort || state == pci_channel_io_perm_failure) {
17195 		netdev_unlock(netdev);
17196 		return PCI_ERS_RESULT_DISCONNECT;
17197 	}
17198 
17199 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17200 	 * so we disable bus master to prevent any potential bad DMAs before
17201 	 * freeing kernel memory.
17202 	 */
17203 	if (state == pci_channel_io_frozen) {
17204 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
17205 		bnxt_fw_fatal_close(bp);
17206 	}
17207 
17208 	if (netif_running(netdev))
17209 		__bnxt_close_nic(bp, true, true);
17210 
17211 	if (pci_is_enabled(pdev))
17212 		pci_disable_device(pdev);
17213 	bnxt_free_ctx_mem(bp, false);
17214 	netdev_unlock(netdev);
17215 
17216 	/* Request a slot reset. */
17217 	return PCI_ERS_RESULT_NEED_RESET;
17218 }
17219 
17220 /**
17221  * bnxt_io_slot_reset - called after the pci bus has been reset.
17222  * @pdev: Pointer to PCI device
17223  *
17224  * Restart the card from scratch, as if from a cold-boot.
17225  * At this point, the card has experienced a hard reset,
17226  * followed by fixups by BIOS, and has its config space
17227  * set up identically to what it was at cold boot.
17228  */
17229 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17230 {
17231 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17232 	struct net_device *netdev = pci_get_drvdata(pdev);
17233 	struct bnxt *bp = netdev_priv(netdev);
17234 	int retry = 0;
17235 	int err = 0;
17236 	int off;
17237 
17238 	netdev_info(bp->dev, "PCI Slot Reset\n");
17239 
17240 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17241 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17242 		msleep(900);
17243 
17244 	netdev_lock(netdev);
17245 
17246 	if (pci_enable_device(pdev)) {
17247 		dev_err(&pdev->dev,
17248 			"Cannot re-enable PCI device after reset.\n");
17249 	} else {
17250 		pci_set_master(pdev);
17251 		/* Upon fatal error, our device internal logic that latches to
17252 		 * BAR value is getting reset and will restore only upon
17253 		 * rewriting the BARs.
17254 		 *
17255 		 * As pci_restore_state() does not re-write the BARs if the
17256 		 * value is same as saved value earlier, driver needs to
17257 		 * write the BARs to 0 to force restore, in case of fatal error.
17258 		 */
17259 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17260 				       &bp->state)) {
17261 			for (off = PCI_BASE_ADDRESS_0;
17262 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17263 				pci_write_config_dword(bp->pdev, off, 0);
17264 		}
17265 		pci_restore_state(pdev);
17266 		pci_save_state(pdev);
17267 
17268 		bnxt_inv_fw_health_reg(bp);
17269 		bnxt_try_map_fw_health_reg(bp);
17270 
17271 		/* In some PCIe AER scenarios, firmware may take up to
17272 		 * 10 seconds to become ready in the worst case.
17273 		 */
17274 		do {
17275 			err = bnxt_try_recover_fw(bp);
17276 			if (!err)
17277 				break;
17278 			retry++;
17279 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17280 
17281 		if (err) {
17282 			dev_err(&pdev->dev, "Firmware not ready\n");
17283 			goto reset_exit;
17284 		}
17285 
17286 		err = bnxt_hwrm_func_reset(bp);
17287 		if (!err)
17288 			result = PCI_ERS_RESULT_RECOVERED;
17289 
17290 		/* IRQ will be initialized later in bnxt_io_resume */
17291 		bnxt_ulp_irq_stop(bp);
17292 		bnxt_clear_int_mode(bp);
17293 	}
17294 
17295 reset_exit:
17296 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17297 	bnxt_clear_reservations(bp, true);
17298 	netdev_unlock(netdev);
17299 
17300 	return result;
17301 }
17302 
17303 /**
17304  * bnxt_io_resume - called when traffic can start flowing again.
17305  * @pdev: Pointer to PCI device
17306  *
17307  * This callback is called when the error recovery driver tells
17308  * us that its OK to resume normal operation.
17309  */
17310 static void bnxt_io_resume(struct pci_dev *pdev)
17311 {
17312 	struct net_device *netdev = pci_get_drvdata(pdev);
17313 	struct bnxt *bp = netdev_priv(netdev);
17314 	int err;
17315 
17316 	netdev_info(bp->dev, "PCI Slot Resume\n");
17317 	netdev_lock(netdev);
17318 
17319 	err = bnxt_hwrm_func_qcaps(bp);
17320 	if (!err) {
17321 		if (netif_running(netdev)) {
17322 			err = bnxt_open(netdev);
17323 		} else {
17324 			err = bnxt_reserve_rings(bp, true);
17325 			if (!err)
17326 				err = bnxt_init_int_mode(bp);
17327 		}
17328 	}
17329 
17330 	if (!err)
17331 		netif_device_attach(netdev);
17332 
17333 	netdev_unlock(netdev);
17334 	bnxt_ulp_start(bp, err);
17335 	if (!err)
17336 		bnxt_reenable_sriov(bp);
17337 }
17338 
17339 static const struct pci_error_handlers bnxt_err_handler = {
17340 	.error_detected	= bnxt_io_error_detected,
17341 	.slot_reset	= bnxt_io_slot_reset,
17342 	.resume		= bnxt_io_resume
17343 };
17344 
17345 static struct pci_driver bnxt_pci_driver = {
17346 	.name		= DRV_MODULE_NAME,
17347 	.id_table	= bnxt_pci_tbl,
17348 	.probe		= bnxt_init_one,
17349 	.remove		= bnxt_remove_one,
17350 	.shutdown	= bnxt_shutdown,
17351 	.driver.pm	= BNXT_PM_OPS,
17352 	.err_handler	= &bnxt_err_handler,
17353 #if defined(CONFIG_BNXT_SRIOV)
17354 	.sriov_configure = bnxt_sriov_configure,
17355 #endif
17356 };
17357 
17358 static int __init bnxt_init(void)
17359 {
17360 	int err;
17361 
17362 	bnxt_debug_init();
17363 	err = pci_register_driver(&bnxt_pci_driver);
17364 	if (err) {
17365 		bnxt_debug_exit();
17366 		return err;
17367 	}
17368 
17369 	return 0;
17370 }
17371 
17372 static void __exit bnxt_exit(void)
17373 {
17374 	pci_unregister_driver(&bnxt_pci_driver);
17375 	if (bnxt_pf_wq)
17376 		destroy_workqueue(bnxt_pf_wq);
17377 	bnxt_debug_exit();
17378 }
17379 
17380 module_init(bnxt_init);
17381 module_exit(bnxt_exit);
17382