xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 37bef2170d4c88fc3d708eecf3ef0f4032bc1372)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 #include <linux/bnxt/hsi.h>
62 #include <linux/bnxt/ulp.h>
63 
64 #include "bnxt.h"
65 #include "bnxt_hwrm.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 #include "bnxt_gso.h"
78 #include <net/tso.h>
79 
80 #define BNXT_TX_TIMEOUT		(5 * HZ)
81 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
82 				 NETIF_MSG_TX_ERR)
83 
84 MODULE_IMPORT_NS("NETDEV_INTERNAL");
85 MODULE_LICENSE("GPL");
86 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
87 
88 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
89 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
90 
91 #define BNXT_TX_PUSH_THRESH 164
92 
93 /* indexed by enum board_idx */
94 static const struct {
95 	char *name;
96 } board_info[] = {
97 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
100 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
101 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
102 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
103 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
104 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
108 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
109 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
110 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
112 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
113 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
114 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
115 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
116 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
117 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
118 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
119 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
120 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
121 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
122 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
123 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
124 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
125 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
126 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
127 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
128 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
129 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
130 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
131 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
132 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
133 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
134 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
135 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
136 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
137 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
138 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
139 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
141 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
142 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
143 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
145 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
146 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
147 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
148 };
149 
150 static const struct pci_device_id bnxt_pci_tbl[] = {
151 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
155 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
170 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
183 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
184 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
185 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
192 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
193 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
198 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
199 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
200 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
201 #ifdef CONFIG_BNXT_SRIOV
202 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
219 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
220 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
221 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
223 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
224 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
225 #endif
226 	{ 0 }
227 };
228 
229 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
230 
231 static const u16 bnxt_vf_req_snif[] = {
232 	HWRM_FUNC_CFG,
233 	HWRM_FUNC_VF_CFG,
234 	HWRM_PORT_PHY_QCFG,
235 	HWRM_CFA_L2_FILTER_ALLOC,
236 };
237 
238 static const u16 bnxt_async_events_arr[] = {
239 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
242 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
243 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
246 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
247 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
248 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
249 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
250 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
253 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
254 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
255 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
256 };
257 
258 const u16 bnxt_bstore_to_trace[] = {
259 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
260 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
261 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
262 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
263 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
264 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
265 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
266 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
267 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
268 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
269 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
270 	[BNXT_CTX_KONG]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_AFM_KONG_HWRM_TRACE,
271 	[BNXT_CTX_QPC]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ERR_QPC_TRACE,
272 };
273 
274 static struct workqueue_struct *bnxt_pf_wq;
275 
276 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
277 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
278 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
279 
280 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
281 	.ports = {
282 		.src = 0,
283 		.dst = 0,
284 	},
285 	.addrs = {
286 		.v6addrs = {
287 			.src = BNXT_IPV6_MASK_NONE,
288 			.dst = BNXT_IPV6_MASK_NONE,
289 		},
290 	},
291 };
292 
293 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
294 	.ports = {
295 		.src = cpu_to_be16(0xffff),
296 		.dst = cpu_to_be16(0xffff),
297 	},
298 	.addrs = {
299 		.v6addrs = {
300 			.src = BNXT_IPV6_MASK_ALL,
301 			.dst = BNXT_IPV6_MASK_ALL,
302 		},
303 	},
304 };
305 
306 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
307 	.ports = {
308 		.src = cpu_to_be16(0xffff),
309 		.dst = cpu_to_be16(0xffff),
310 	},
311 	.addrs = {
312 		.v4addrs = {
313 			.src = cpu_to_be32(0xffffffff),
314 			.dst = cpu_to_be32(0xffffffff),
315 		},
316 	},
317 };
318 
319 static bool bnxt_vf_pciid(enum board_idx idx)
320 {
321 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
322 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
323 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
324 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
325 		idx == NETXTREME_E_P7_VF_HV);
326 }
327 
328 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
329 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
330 
331 #define BNXT_DB_CQ(db, idx)						\
332 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_NQ_P5(db, idx)						\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
336 		    (db)->doorbell)
337 
338 #define BNXT_DB_NQ_P7(db, idx)						\
339 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
340 		    DB_RING_IDX(db, idx), (db)->doorbell)
341 
342 #define BNXT_DB_CQ_ARM(db, idx)						\
343 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
344 
345 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
346 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
347 		    DB_RING_IDX(db, idx), (db)->doorbell)
348 
349 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
350 {
351 	if (bp->flags & BNXT_FLAG_CHIP_P7)
352 		BNXT_DB_NQ_P7(db, idx);
353 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
354 		BNXT_DB_NQ_P5(db, idx);
355 	else
356 		BNXT_DB_CQ(db, idx);
357 }
358 
359 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
360 {
361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
362 		BNXT_DB_NQ_ARM_P5(db, idx);
363 	else
364 		BNXT_DB_CQ_ARM(db, idx);
365 }
366 
367 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
368 {
369 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
370 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
371 			    DB_RING_IDX(db, idx), db->doorbell);
372 	else
373 		BNXT_DB_CQ(db, idx);
374 }
375 
376 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
377 {
378 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
379 		return;
380 
381 	if (BNXT_PF(bp))
382 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
383 	else
384 		schedule_delayed_work(&bp->fw_reset_task, delay);
385 }
386 
387 static void __bnxt_queue_sp_work(struct bnxt *bp)
388 {
389 	if (BNXT_PF(bp))
390 		queue_work(bnxt_pf_wq, &bp->sp_task);
391 	else
392 		schedule_work(&bp->sp_task);
393 }
394 
395 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
396 {
397 	set_bit(event, &bp->sp_event);
398 	__bnxt_queue_sp_work(bp);
399 }
400 
401 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
402 {
403 	if (!rxr->bnapi->in_reset) {
404 		rxr->bnapi->in_reset = true;
405 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
406 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
407 		else
408 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
409 		__bnxt_queue_sp_work(bp);
410 	}
411 	rxr->rx_next_cons = 0xffff;
412 }
413 
414 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
415 			  u16 curr)
416 {
417 	struct bnxt_napi *bnapi = txr->bnapi;
418 
419 	if (bnapi->tx_fault)
420 		return;
421 
422 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
423 		   txr->txq_index, txr->tx_hw_cons,
424 		   txr->tx_cons, txr->tx_prod, curr);
425 	WARN_ON_ONCE(1);
426 	bnapi->tx_fault = 1;
427 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
428 }
429 
430 const u16 bnxt_lhint_arr[] = {
431 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
432 	TX_BD_FLAGS_LHINT_512_TO_1023,
433 	TX_BD_FLAGS_LHINT_1024_TO_2047,
434 	TX_BD_FLAGS_LHINT_1024_TO_2047,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
444 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
445 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
446 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
447 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
448 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
449 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
450 };
451 
452 u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
453 {
454 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
455 
456 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
457 		return 0;
458 
459 	return md_dst->u.port_info.port_id;
460 }
461 
462 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
463 			     u16 prod)
464 {
465 	/* Sync BD data before updating doorbell */
466 	wmb();
467 	bnxt_db_write(bp, &txr->tx_db, prod);
468 	txr->kick_pending = 0;
469 }
470 
471 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
472 {
473 	struct bnxt *bp = netdev_priv(dev);
474 	struct tx_bd *txbd, *txbd0;
475 	struct tx_bd_ext *txbd1;
476 	struct netdev_queue *txq;
477 	int i;
478 	dma_addr_t mapping;
479 	unsigned int length, pad = 0;
480 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
481 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
482 	struct pci_dev *pdev = bp->pdev;
483 	u16 prod, last_frag, txts_prod;
484 	struct bnxt_tx_ring_info *txr;
485 	struct bnxt_sw_tx_bd *tx_buf;
486 	__le32 lflags = 0;
487 	skb_frag_t *frag;
488 	netdev_tx_t ret;
489 
490 	i = skb_get_queue_mapping(skb);
491 	if (unlikely(i >= bp->tx_nr_rings)) {
492 		dev_kfree_skb_any(skb);
493 		dev_core_stats_tx_dropped_inc(dev);
494 		return NETDEV_TX_OK;
495 	}
496 
497 	txq = netdev_get_tx_queue(dev, i);
498 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
499 	prod = txr->tx_prod;
500 
501 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
502 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
503 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
504 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
505 		if (skb_linearize(skb))
506 			goto tx_free;
507 	}
508 #endif
509 	if (skb_is_gso(skb) &&
510 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
511 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
512 		ret = bnxt_sw_udp_gso_xmit(bp, txr, txq, skb);
513 		if (txr->kick_pending)
514 			bnxt_txr_db_kick(bp, txr, txr->tx_prod);
515 
516 		return ret;
517 	}
518 
519 	free_size = bnxt_tx_avail(bp, txr);
520 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
521 		/* We must have raced with NAPI cleanup */
522 		if (net_ratelimit() && txr->kick_pending)
523 			netif_warn(bp, tx_err, dev,
524 				   "bnxt: ring busy w/ flush pending!\n");
525 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
526 					bp->tx_wake_thresh))
527 			return NETDEV_TX_BUSY;
528 	}
529 
530 	length = skb->len;
531 	len = skb_headlen(skb);
532 	last_frag = skb_shinfo(skb)->nr_frags;
533 
534 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
535 
536 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
537 	tx_buf->skb = skb;
538 	tx_buf->nr_frags = last_frag;
539 
540 	vlan_tag_flags = 0;
541 	cfa_action = bnxt_xmit_get_cfa_action(skb);
542 	if (skb_vlan_tag_present(skb)) {
543 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
544 				 skb_vlan_tag_get(skb);
545 		/* Currently supports 8021Q, 8021AD vlan offloads
546 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
547 		 */
548 		if (skb->vlan_proto == htons(ETH_P_8021Q))
549 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
550 	}
551 
552 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
553 	    ptp->tx_tstamp_en) {
554 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
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 		} else if (!skb_is_gso(skb)) {
559 			u16 seq_id, hdr_off;
560 
561 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
562 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
563 				if (vlan_tag_flags)
564 					hdr_off += VLAN_HLEN;
565 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
566 				tx_buf->is_ts_pkt = 1;
567 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
568 
569 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
570 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
571 				tx_buf->txts_prod = txts_prod;
572 			}
573 		}
574 	}
575 	if (unlikely(skb->no_fcs))
576 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
577 
578 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
579 	    skb_frags_readable(skb) && !lflags) {
580 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
581 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
582 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
583 		void __iomem *db = txr->tx_db.doorbell;
584 		void *pdata = tx_push_buf->data;
585 		u64 *end;
586 		int j, push_len;
587 
588 		/* Set COAL_NOW to be ready quickly for the next push */
589 		tx_push->tx_bd_len_flags_type =
590 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
591 					TX_BD_TYPE_LONG_TX_BD |
592 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
593 					TX_BD_FLAGS_COAL_NOW |
594 					TX_BD_FLAGS_PACKET_END |
595 					TX_BD_CNT(2));
596 
597 		if (skb->ip_summed == CHECKSUM_PARTIAL)
598 			tx_push1->tx_bd_hsize_lflags =
599 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
600 		else
601 			tx_push1->tx_bd_hsize_lflags = 0;
602 
603 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
604 		tx_push1->tx_bd_cfa_action =
605 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
606 
607 		end = pdata + length;
608 		end = PTR_ALIGN(end, 8) - 1;
609 		*end = 0;
610 
611 		skb_copy_from_linear_data(skb, pdata, len);
612 		pdata += len;
613 		for (j = 0; j < last_frag; j++) {
614 			void *fptr;
615 
616 			frag = &skb_shinfo(skb)->frags[j];
617 			fptr = skb_frag_address_safe(frag);
618 			if (!fptr)
619 				goto normal_tx;
620 
621 			memcpy(pdata, fptr, skb_frag_size(frag));
622 			pdata += skb_frag_size(frag);
623 		}
624 
625 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
626 		txbd->tx_bd_haddr = txr->data_mapping;
627 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
628 		prod = NEXT_TX(prod);
629 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
630 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
631 		memcpy(txbd, tx_push1, sizeof(*txbd));
632 		prod = NEXT_TX(prod);
633 		tx_push->doorbell =
634 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
635 				    DB_RING_IDX(&txr->tx_db, prod));
636 		WRITE_ONCE(txr->tx_prod, prod);
637 
638 		tx_buf->is_push = 1;
639 		netdev_tx_sent_queue(txq, skb->len);
640 		wmb();	/* Sync is_push and byte queue before pushing data */
641 
642 		push_len = (length + sizeof(*tx_push) + 7) / 8;
643 		if (push_len > 16) {
644 			__iowrite64_copy(db, tx_push_buf, 16);
645 			__iowrite32_copy(db + 4, tx_push_buf + 1,
646 					 (push_len - 16) << 1);
647 		} else {
648 			__iowrite64_copy(db, tx_push_buf, push_len);
649 		}
650 
651 		goto tx_done;
652 	}
653 
654 normal_tx:
655 	if (length < BNXT_MIN_PKT_SIZE) {
656 		pad = BNXT_MIN_PKT_SIZE - length;
657 		if (skb_pad(skb, pad))
658 			/* SKB already freed. */
659 			goto tx_kick_pending;
660 		length = BNXT_MIN_PKT_SIZE;
661 	}
662 
663 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
664 
665 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
666 		goto tx_free;
667 
668 	dma_unmap_addr_set(tx_buf, mapping, mapping);
669 	dma_unmap_len_set(tx_buf, len, len);
670 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
671 		TX_BD_CNT(last_frag + 2);
672 
673 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
674 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
675 
676 	prod = NEXT_TX(prod);
677 	txbd1 = bnxt_init_ext_bd(bp, txr, prod, lflags, vlan_tag_flags,
678 				 cfa_action);
679 
680 	if (skb_is_gso(skb)) {
681 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
682 		u32 hdr_len;
683 
684 		if (skb->encapsulation) {
685 			if (udp_gso)
686 				hdr_len = skb_inner_transport_offset(skb) +
687 					  sizeof(struct udphdr);
688 			else
689 				hdr_len = skb_inner_tcp_all_headers(skb);
690 		} else if (udp_gso) {
691 			hdr_len = skb_transport_offset(skb) +
692 				  sizeof(struct udphdr);
693 		} else {
694 			hdr_len = skb_tcp_all_headers(skb);
695 		}
696 
697 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
698 					TX_BD_FLAGS_T_IPID |
699 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
700 		length = skb_shinfo(skb)->gso_size;
701 		txbd1->tx_bd_mss = cpu_to_le32(length);
702 		length += hdr_len;
703 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
704 		txbd1->tx_bd_hsize_lflags |=
705 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
706 	}
707 
708 	length >>= 9;
709 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
710 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
711 				     skb->len);
712 		i = 0;
713 		goto tx_dma_error;
714 	}
715 	flags |= bnxt_lhint_arr[length];
716 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
717 
718 	txbd0 = txbd;
719 	for (i = 0; i < last_frag; i++) {
720 		frag = &skb_shinfo(skb)->frags[i];
721 		prod = NEXT_TX(prod);
722 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
723 
724 		len = skb_frag_size(frag);
725 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
726 					   DMA_TO_DEVICE);
727 
728 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
729 			goto tx_dma_error;
730 
731 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
732 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
733 					  mapping, mapping);
734 		dma_unmap_len_set(tx_buf, len, len);
735 
736 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
737 
738 		flags = len << TX_BD_LEN_SHIFT;
739 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
740 	}
741 
742 	flags &= ~TX_BD_LEN;
743 	txbd->tx_bd_len_flags_type =
744 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
745 			    TX_BD_FLAGS_PACKET_END);
746 
747 	netdev_tx_sent_queue(txq, skb->len);
748 
749 	skb_tx_timestamp(skb);
750 
751 	prod = NEXT_TX(prod);
752 	WRITE_ONCE(txr->tx_prod, prod);
753 
754 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
755 		bnxt_txr_db_kick(bp, txr, prod);
756 	} else {
757 		if (free_size >= bp->tx_wake_thresh)
758 			txbd0->tx_bd_len_flags_type |=
759 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
760 		txr->kick_pending = 1;
761 	}
762 
763 tx_done:
764 
765 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
766 		if (netdev_xmit_more() && !tx_buf->is_push) {
767 			txbd0->tx_bd_len_flags_type &=
768 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
769 			bnxt_txr_db_kick(bp, txr, prod);
770 		}
771 
772 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
773 				   bp->tx_wake_thresh);
774 	}
775 	return NETDEV_TX_OK;
776 
777 tx_dma_error:
778 	last_frag = i;
779 
780 	/* start back at beginning and unmap skb */
781 	prod = txr->tx_prod;
782 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
783 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
784 			 skb_headlen(skb), DMA_TO_DEVICE);
785 	prod = NEXT_TX(prod);
786 
787 	/* unmap remaining mapped pages */
788 	for (i = 0; i < last_frag; i++) {
789 		prod = NEXT_TX(prod);
790 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
791 		frag = &skb_shinfo(skb)->frags[i];
792 		netmem_dma_unmap_page_attrs(&pdev->dev,
793 					    dma_unmap_addr(tx_buf, mapping),
794 					    skb_frag_size(frag),
795 					    DMA_TO_DEVICE, 0);
796 	}
797 
798 tx_free:
799 	dev_kfree_skb_any(skb);
800 tx_kick_pending:
801 	if (BNXT_TX_PTP_IS_SET(lflags)) {
802 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
803 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
804 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
805 			/* set SKB to err so PTP worker will clean up */
806 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
807 	}
808 	if (txr->kick_pending)
809 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
810 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
811 	dev_core_stats_tx_dropped_inc(dev);
812 	return NETDEV_TX_OK;
813 }
814 
815 /* Returns true if some remaining TX packets not processed. */
816 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
817 			  int budget)
818 {
819 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
820 	struct pci_dev *pdev = bp->pdev;
821 	u16 hw_cons = txr->tx_hw_cons;
822 	unsigned int tx_bytes = 0;
823 	u16 cons = txr->tx_cons;
824 	unsigned int dma_len;
825 	dma_addr_t dma_addr;
826 	int tx_pkts = 0;
827 	bool rc = false;
828 
829 	while (RING_TX(bp, cons) != hw_cons) {
830 		struct bnxt_sw_tx_bd *tx_buf, *head_buf;
831 		struct sk_buff *skb;
832 		bool is_ts_pkt;
833 		int j, last;
834 
835 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
836 		head_buf = tx_buf;
837 		skb = tx_buf->skb;
838 
839 		if (unlikely(!skb)) {
840 			bnxt_sched_reset_txr(bp, txr, cons);
841 			return rc;
842 		}
843 
844 		is_ts_pkt = tx_buf->is_ts_pkt;
845 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
846 			rc = true;
847 			break;
848 		}
849 
850 		cons = NEXT_TX(cons);
851 		tx_pkts++;
852 		tx_bytes += skb->len;
853 		tx_buf->skb = NULL;
854 		tx_buf->is_ts_pkt = 0;
855 
856 		if (tx_buf->is_push) {
857 			tx_buf->is_push = 0;
858 			goto next_tx_int;
859 		}
860 
861 		if (dma_unmap_len(tx_buf, len)) {
862 			dma_addr = dma_unmap_addr(tx_buf, mapping);
863 			dma_len = dma_unmap_len(tx_buf, len);
864 
865 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
866 					 DMA_TO_DEVICE);
867 		}
868 
869 		last = tx_buf->nr_frags;
870 
871 		for (j = 0; j < last; j++) {
872 			cons = NEXT_TX(cons);
873 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
874 			if (dma_unmap_len(tx_buf, len)) {
875 				dma_addr = dma_unmap_addr(tx_buf, mapping);
876 				dma_len = dma_unmap_len(tx_buf, len);
877 
878 				netmem_dma_unmap_page_attrs(&pdev->dev,
879 							    dma_addr, dma_len,
880 							    DMA_TO_DEVICE, 0);
881 			}
882 		}
883 
884 		if (unlikely(head_buf->is_sw_gso)) {
885 			u16 inline_cons = txr->tx_inline_cons + 1;
886 
887 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
888 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
889 				tso_dma_map_complete(&pdev->dev,
890 						     &head_buf->sw_gso_cstate);
891 			} else {
892 				tx_pkts--;
893 				tx_bytes -= skb->len;
894 				skb = NULL;
895 			}
896 			head_buf->is_sw_gso = 0;
897 		}
898 
899 		if (unlikely(is_ts_pkt)) {
900 			if (BNXT_CHIP_P5(bp)) {
901 				/* PTP worker takes ownership of the skb */
902 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
903 				skb = NULL;
904 			}
905 		}
906 
907 next_tx_int:
908 		cons = NEXT_TX(cons);
909 
910 		napi_consume_skb(skb, budget);
911 	}
912 
913 	WRITE_ONCE(txr->tx_cons, cons);
914 
915 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
916 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
917 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
918 
919 	return rc;
920 }
921 
922 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
923 {
924 	struct bnxt_tx_ring_info *txr;
925 	bool more = false;
926 	int i;
927 
928 	bnxt_for_each_napi_tx(i, bnapi, txr) {
929 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
930 			more |= __bnxt_tx_int(bp, txr, budget);
931 	}
932 	if (!more)
933 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
934 }
935 
936 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
937 {
938 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
939 }
940 
941 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
942 					 struct bnxt_rx_ring_info *rxr,
943 					 unsigned int *offset,
944 					 gfp_t gfp)
945 {
946 	struct page *page;
947 
948 	if (rxr->rx_page_size < PAGE_SIZE) {
949 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
950 						rxr->rx_page_size);
951 	} else {
952 		page = page_pool_dev_alloc_pages(rxr->page_pool);
953 		*offset = 0;
954 	}
955 	if (!page)
956 		return NULL;
957 
958 	*mapping = page_pool_get_dma_addr(page) + *offset;
959 	return page;
960 }
961 
962 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
963 					 struct bnxt_rx_ring_info *rxr,
964 					 unsigned int *offset,
965 					 gfp_t gfp)
966 {
967 	netmem_ref netmem;
968 
969 	if (rxr->rx_page_size < PAGE_SIZE) {
970 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
971 						     rxr->rx_page_size, gfp);
972 	} else {
973 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
974 		*offset = 0;
975 	}
976 	if (!netmem)
977 		return 0;
978 
979 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
980 	return netmem;
981 }
982 
983 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
984 				       struct bnxt_rx_ring_info *rxr,
985 				       gfp_t gfp)
986 {
987 	unsigned int offset;
988 	struct page *page;
989 
990 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
991 				    bp->rx_buf_size, gfp);
992 	if (!page)
993 		return NULL;
994 
995 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
996 	return page_address(page) + offset;
997 }
998 
999 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1000 		       u16 prod, gfp_t gfp)
1001 {
1002 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1003 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1004 	dma_addr_t mapping;
1005 
1006 	if (BNXT_RX_PAGE_MODE(bp)) {
1007 		unsigned int offset;
1008 		struct page *page =
1009 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1010 
1011 		if (!page)
1012 			return -ENOMEM;
1013 
1014 		mapping += bp->rx_dma_offset;
1015 		rx_buf->data = page;
1016 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
1017 		rx_buf->offset = offset;
1018 	} else {
1019 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
1020 
1021 		if (!data)
1022 			return -ENOMEM;
1023 
1024 		rx_buf->data = data;
1025 		rx_buf->data_ptr = data + bp->rx_offset;
1026 		rx_buf->offset = 0;
1027 	}
1028 	rx_buf->mapping = mapping;
1029 
1030 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1031 	return 0;
1032 }
1033 
1034 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1035 {
1036 	u16 prod = rxr->rx_prod;
1037 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1038 	struct bnxt *bp = rxr->bnapi->bp;
1039 	struct rx_bd *cons_bd, *prod_bd;
1040 
1041 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1042 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1043 
1044 	prod_rx_buf->data = data;
1045 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1046 
1047 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1048 	prod_rx_buf->offset = cons_rx_buf->offset;
1049 
1050 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1051 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1052 
1053 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1054 }
1055 
1056 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1057 {
1058 	u16 next, max = rxr->rx_agg_bmap_size;
1059 
1060 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1061 	if (next >= max)
1062 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1063 	return next;
1064 }
1065 
1066 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1067 				u16 prod, gfp_t gfp)
1068 {
1069 	struct rx_bd *rxbd =
1070 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1071 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1072 	u16 sw_prod = rxr->rx_sw_agg_prod;
1073 	unsigned int offset = 0;
1074 	dma_addr_t mapping;
1075 	netmem_ref netmem;
1076 
1077 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1078 	if (!netmem)
1079 		return -ENOMEM;
1080 
1081 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1082 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1083 
1084 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1085 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1086 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1087 
1088 	rx_agg_buf->netmem = netmem;
1089 	rx_agg_buf->offset = offset;
1090 	rx_agg_buf->mapping = mapping;
1091 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1092 	rxbd->rx_bd_opaque = sw_prod;
1093 	return 0;
1094 }
1095 
1096 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1097 				       struct bnxt_cp_ring_info *cpr,
1098 				       u16 cp_cons, u16 curr)
1099 {
1100 	struct rx_agg_cmp *agg;
1101 
1102 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1103 	agg = (struct rx_agg_cmp *)
1104 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1105 	return agg;
1106 }
1107 
1108 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1109 					      struct bnxt_rx_ring_info *rxr,
1110 					      u16 agg_id, u16 curr)
1111 {
1112 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1113 
1114 	return &tpa_info->agg_arr[curr];
1115 }
1116 
1117 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1118 				   u16 start, u32 agg_bufs, bool tpa)
1119 {
1120 	struct bnxt_napi *bnapi = cpr->bnapi;
1121 	struct bnxt *bp = bnapi->bp;
1122 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1123 	u16 prod = rxr->rx_agg_prod;
1124 	u16 sw_prod = rxr->rx_sw_agg_prod;
1125 	bool p5_tpa = false;
1126 	u32 i;
1127 
1128 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1129 		p5_tpa = true;
1130 
1131 	for (i = 0; i < agg_bufs; i++) {
1132 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1133 		struct rx_agg_cmp *agg;
1134 		struct rx_bd *prod_bd;
1135 		netmem_ref netmem;
1136 		u16 cons;
1137 
1138 		if (p5_tpa)
1139 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1140 		else
1141 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1142 		cons = agg->rx_agg_cmp_opaque;
1143 		__clear_bit(cons, rxr->rx_agg_bmap);
1144 
1145 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1146 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1147 
1148 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1149 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1150 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1151 
1152 		/* It is possible for sw_prod to be equal to cons, so
1153 		 * set cons_rx_buf->netmem to 0 first.
1154 		 */
1155 		netmem = cons_rx_buf->netmem;
1156 		cons_rx_buf->netmem = 0;
1157 		prod_rx_buf->netmem = netmem;
1158 		prod_rx_buf->offset = cons_rx_buf->offset;
1159 
1160 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1161 
1162 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1163 
1164 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1165 		prod_bd->rx_bd_opaque = sw_prod;
1166 
1167 		prod = NEXT_RX_AGG(prod);
1168 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1169 	}
1170 	rxr->rx_agg_prod = prod;
1171 	rxr->rx_sw_agg_prod = sw_prod;
1172 }
1173 
1174 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1175 					      struct bnxt_rx_ring_info *rxr,
1176 					      u16 cons, void *data, u8 *data_ptr,
1177 					      dma_addr_t dma_addr,
1178 					      unsigned int offset_and_len)
1179 {
1180 	unsigned int len = offset_and_len & 0xffff;
1181 	struct page *page = data;
1182 	u16 prod = rxr->rx_prod;
1183 	struct sk_buff *skb;
1184 	void *frag_start;
1185 	int err;
1186 
1187 	frag_start = page_address(page) + rxr->rx_buf_ring[cons].offset;
1188 
1189 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1190 	if (unlikely(err)) {
1191 		bnxt_reuse_rx_data(rxr, cons, data);
1192 		return NULL;
1193 	}
1194 	dma_addr -= bp->rx_dma_offset;
1195 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1196 				bp->rx_dir);
1197 	skb = napi_build_skb(frag_start, rxr->rx_page_size);
1198 	if (!skb) {
1199 		page_pool_recycle_direct(rxr->page_pool, page);
1200 		return NULL;
1201 	}
1202 	skb_mark_for_recycle(skb);
1203 	skb_reserve(skb, data_ptr - (u8 *)frag_start);
1204 	__skb_put(skb, len);
1205 
1206 	return skb;
1207 }
1208 
1209 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1210 					struct bnxt_rx_ring_info *rxr,
1211 					u16 cons, void *data, u8 *data_ptr,
1212 					dma_addr_t dma_addr,
1213 					unsigned int offset_and_len)
1214 {
1215 	unsigned int payload = offset_and_len >> 16;
1216 	unsigned int len = offset_and_len & 0xffff;
1217 	skb_frag_t *frag;
1218 	struct page *page = data;
1219 	u16 prod = rxr->rx_prod;
1220 	struct sk_buff *skb;
1221 	int off, err;
1222 
1223 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1224 	if (unlikely(err)) {
1225 		bnxt_reuse_rx_data(rxr, cons, data);
1226 		return NULL;
1227 	}
1228 	dma_addr -= bp->rx_dma_offset;
1229 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1230 				bp->rx_dir);
1231 
1232 	if (unlikely(!payload))
1233 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1234 
1235 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1236 	if (!skb) {
1237 		page_pool_recycle_direct(rxr->page_pool, page);
1238 		return NULL;
1239 	}
1240 
1241 	skb_mark_for_recycle(skb);
1242 	off = (void *)data_ptr - page_address(page);
1243 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1244 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1245 	       payload + NET_IP_ALIGN);
1246 
1247 	frag = &skb_shinfo(skb)->frags[0];
1248 	skb_frag_size_sub(frag, payload);
1249 	skb_frag_off_add(frag, payload);
1250 	skb->data_len -= payload;
1251 	skb->tail += payload;
1252 
1253 	return skb;
1254 }
1255 
1256 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1257 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1258 				   void *data, u8 *data_ptr,
1259 				   dma_addr_t dma_addr,
1260 				   unsigned int offset_and_len)
1261 {
1262 	u16 prod = rxr->rx_prod;
1263 	struct sk_buff *skb;
1264 	int err;
1265 
1266 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1267 	if (unlikely(err)) {
1268 		bnxt_reuse_rx_data(rxr, cons, data);
1269 		return NULL;
1270 	}
1271 
1272 	skb = napi_build_skb(data, bp->rx_buf_size);
1273 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1274 				bp->rx_dir);
1275 	if (!skb) {
1276 		page_pool_free_va(rxr->head_pool, data, true);
1277 		return NULL;
1278 	}
1279 
1280 	skb_mark_for_recycle(skb);
1281 	skb_reserve(skb, bp->rx_offset);
1282 	skb_put(skb, offset_and_len & 0xffff);
1283 	return skb;
1284 }
1285 
1286 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1287 				 struct bnxt_cp_ring_info *cpr,
1288 				 u16 idx, u32 agg_bufs, bool tpa,
1289 				 struct sk_buff *skb,
1290 				 struct xdp_buff *xdp)
1291 {
1292 	struct bnxt_napi *bnapi = cpr->bnapi;
1293 	struct skb_shared_info *shinfo;
1294 	struct bnxt_rx_ring_info *rxr;
1295 	u32 i, total_frag_len = 0;
1296 	bool p5_tpa = false;
1297 	u16 prod;
1298 
1299 	rxr = bnapi->rx_ring;
1300 	prod = rxr->rx_agg_prod;
1301 
1302 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1303 		p5_tpa = true;
1304 
1305 	if (skb)
1306 		shinfo = skb_shinfo(skb);
1307 	else
1308 		shinfo = xdp_get_shared_info_from_buff(xdp);
1309 
1310 	for (i = 0; i < agg_bufs; i++) {
1311 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1312 		struct rx_agg_cmp *agg;
1313 		u16 cons, frag_len;
1314 		netmem_ref netmem;
1315 
1316 		if (p5_tpa)
1317 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1318 		else
1319 			agg = bnxt_get_agg(bp, cpr, idx, i);
1320 		cons = agg->rx_agg_cmp_opaque;
1321 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1322 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1323 
1324 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1325 		if (skb) {
1326 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1327 					       cons_rx_buf->offset,
1328 					       frag_len, rxr->rx_page_size);
1329 		} else {
1330 			skb_frag_t *frag = &shinfo->frags[i];
1331 
1332 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1333 						  cons_rx_buf->offset,
1334 						  frag_len);
1335 			shinfo->nr_frags = i + 1;
1336 		}
1337 		__clear_bit(cons, rxr->rx_agg_bmap);
1338 
1339 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1340 		 * a sw_prod index that equals the cons index, so we
1341 		 * need to clear the cons entry now.
1342 		 */
1343 		netmem = cons_rx_buf->netmem;
1344 		cons_rx_buf->netmem = 0;
1345 
1346 		if (xdp && netmem_is_pfmemalloc(netmem))
1347 			xdp_buff_set_frag_pfmemalloc(xdp);
1348 
1349 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1350 			if (skb) {
1351 				skb->len -= frag_len;
1352 				skb->data_len -= frag_len;
1353 				skb->truesize -= rxr->rx_page_size;
1354 			}
1355 
1356 			--shinfo->nr_frags;
1357 			cons_rx_buf->netmem = netmem;
1358 
1359 			/* Update prod since possibly some netmems have been
1360 			 * allocated already.
1361 			 */
1362 			rxr->rx_agg_prod = prod;
1363 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1364 			return 0;
1365 		}
1366 
1367 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1368 						  rxr->rx_page_size);
1369 
1370 		total_frag_len += frag_len;
1371 		prod = NEXT_RX_AGG(prod);
1372 	}
1373 	rxr->rx_agg_prod = prod;
1374 	return total_frag_len;
1375 }
1376 
1377 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1378 					       struct bnxt_cp_ring_info *cpr,
1379 					       struct sk_buff *skb, u16 idx,
1380 					       u32 agg_bufs, bool tpa)
1381 {
1382 	u32 total_frag_len = 0;
1383 
1384 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1385 					       skb, NULL);
1386 	if (!total_frag_len) {
1387 		skb_mark_for_recycle(skb);
1388 		dev_kfree_skb(skb);
1389 		return NULL;
1390 	}
1391 
1392 	return skb;
1393 }
1394 
1395 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1396 				   struct bnxt_cp_ring_info *cpr,
1397 				   struct xdp_buff *xdp, u16 idx,
1398 				   u32 agg_bufs, bool tpa)
1399 {
1400 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1401 	u32 total_frag_len = 0;
1402 
1403 	if (!xdp_buff_has_frags(xdp))
1404 		shinfo->nr_frags = 0;
1405 
1406 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1407 					       NULL, xdp);
1408 	if (total_frag_len) {
1409 		xdp_buff_set_frags_flag(xdp);
1410 		shinfo->nr_frags = agg_bufs;
1411 		shinfo->xdp_frags_size = total_frag_len;
1412 	}
1413 	return total_frag_len;
1414 }
1415 
1416 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1417 			       u8 agg_bufs, u32 *raw_cons)
1418 {
1419 	u16 last;
1420 	struct rx_agg_cmp *agg;
1421 
1422 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1423 	last = RING_CMP(*raw_cons);
1424 	agg = (struct rx_agg_cmp *)
1425 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1426 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1427 }
1428 
1429 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1430 				      unsigned int len,
1431 				      dma_addr_t mapping)
1432 {
1433 	struct bnxt *bp = bnapi->bp;
1434 	struct pci_dev *pdev = bp->pdev;
1435 	struct sk_buff *skb;
1436 
1437 	skb = napi_alloc_skb(&bnapi->napi, len);
1438 	if (!skb)
1439 		return NULL;
1440 
1441 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1442 				bp->rx_dir);
1443 
1444 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1445 	       len + NET_IP_ALIGN);
1446 
1447 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1448 				   bp->rx_dir);
1449 
1450 	skb_put(skb, len);
1451 
1452 	return skb;
1453 }
1454 
1455 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1456 				     unsigned int len,
1457 				     dma_addr_t mapping)
1458 {
1459 	return bnxt_copy_data(bnapi, data, len, mapping);
1460 }
1461 
1462 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1463 				     struct xdp_buff *xdp,
1464 				     unsigned int len,
1465 				     dma_addr_t mapping)
1466 {
1467 	unsigned int metasize = 0;
1468 	u8 *data = xdp->data;
1469 	struct sk_buff *skb;
1470 
1471 	len = xdp->data_end - xdp->data_meta;
1472 	metasize = xdp->data - xdp->data_meta;
1473 	data = xdp->data_meta;
1474 
1475 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1476 	if (!skb)
1477 		return skb;
1478 
1479 	if (metasize) {
1480 		skb_metadata_set(skb, metasize);
1481 		__skb_pull(skb, metasize);
1482 	}
1483 
1484 	return skb;
1485 }
1486 
1487 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1488 			   u32 *raw_cons, void *cmp)
1489 {
1490 	struct rx_cmp *rxcmp = cmp;
1491 	u32 tmp_raw_cons = *raw_cons;
1492 	u8 cmp_type, agg_bufs = 0;
1493 
1494 	cmp_type = RX_CMP_TYPE(rxcmp);
1495 
1496 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1497 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1498 			    RX_CMP_AGG_BUFS) >>
1499 			   RX_CMP_AGG_BUFS_SHIFT;
1500 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1501 		struct rx_tpa_end_cmp *tpa_end = cmp;
1502 
1503 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1504 			return 0;
1505 
1506 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1507 	}
1508 
1509 	if (agg_bufs) {
1510 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1511 			return -EBUSY;
1512 	}
1513 	*raw_cons = tmp_raw_cons;
1514 	return 0;
1515 }
1516 
1517 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1518 {
1519 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1520 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1521 
1522 	if (test_bit(idx, map->agg_idx_bmap)) {
1523 		idx = find_first_zero_bit(map->agg_idx_bmap, MAX_TPA_P5);
1524 		if (idx >= MAX_TPA_P5)
1525 			return INVALID_HW_RING_ID;
1526 	}
1527 	__set_bit(idx, map->agg_idx_bmap);
1528 	map->agg_id_tbl[agg_id] = idx;
1529 	return idx;
1530 }
1531 
1532 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1533 {
1534 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1535 
1536 	__clear_bit(idx, map->agg_idx_bmap);
1537 }
1538 
1539 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1540 {
1541 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1542 
1543 	return map->agg_id_tbl[agg_id];
1544 }
1545 
1546 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1547 			      struct rx_tpa_start_cmp *tpa_start,
1548 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1549 {
1550 	tpa_info->cfa_code_valid = 1;
1551 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1552 	tpa_info->vlan_valid = 0;
1553 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1554 		tpa_info->vlan_valid = 1;
1555 		tpa_info->metadata =
1556 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1557 	}
1558 }
1559 
1560 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1561 				 struct rx_tpa_start_cmp *tpa_start,
1562 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1563 {
1564 	tpa_info->vlan_valid = 0;
1565 	if (TPA_START_VLAN_VALID(tpa_start)) {
1566 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1567 		u32 vlan_proto = ETH_P_8021Q;
1568 
1569 		tpa_info->vlan_valid = 1;
1570 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1571 			vlan_proto = ETH_P_8021AD;
1572 		tpa_info->metadata = vlan_proto << 16 |
1573 				     TPA_START_METADATA0_TCI(tpa_start1);
1574 	}
1575 }
1576 
1577 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1578 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1579 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1580 {
1581 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1582 	struct bnxt_tpa_info *tpa_info;
1583 	u16 cons, prod, agg_id;
1584 	struct rx_bd *prod_bd;
1585 	dma_addr_t mapping;
1586 
1587 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1588 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1589 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1590 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1591 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1592 				    rxr->bnapi->index,
1593 				    TPA_START_AGG_ID_P5(tpa_start));
1594 			bnxt_sched_reset_rxr(bp, rxr);
1595 			return;
1596 		}
1597 	} else {
1598 		agg_id = TPA_START_AGG_ID(tpa_start);
1599 	}
1600 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1601 	prod = rxr->rx_prod;
1602 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1603 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1604 	tpa_info = &rxr->rx_tpa[agg_id];
1605 
1606 	if (unlikely(cons != rxr->rx_next_cons ||
1607 		     TPA_START_ERROR(tpa_start))) {
1608 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1609 			    cons, rxr->rx_next_cons,
1610 			    TPA_START_ERROR_CODE(tpa_start1));
1611 		bnxt_sched_reset_rxr(bp, rxr);
1612 		return;
1613 	}
1614 	prod_rx_buf->data = tpa_info->data;
1615 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1616 
1617 	mapping = tpa_info->mapping;
1618 	prod_rx_buf->mapping = mapping;
1619 
1620 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1621 
1622 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1623 
1624 	tpa_info->data = cons_rx_buf->data;
1625 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1626 	cons_rx_buf->data = NULL;
1627 	tpa_info->mapping = cons_rx_buf->mapping;
1628 
1629 	tpa_info->len =
1630 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1631 				RX_TPA_START_CMP_LEN_SHIFT;
1632 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1633 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1634 		tpa_info->gso_type = SKB_GSO_TCPV4;
1635 		if (TPA_START_IS_IPV6(tpa_start1))
1636 			tpa_info->gso_type = SKB_GSO_TCPV6;
1637 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1638 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1639 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1640 			tpa_info->gso_type = SKB_GSO_TCPV6;
1641 		tpa_info->rss_hash =
1642 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1643 	} else {
1644 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1645 		tpa_info->gso_type = 0;
1646 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1647 	}
1648 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1649 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1650 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1651 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1652 	else
1653 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1654 	tpa_info->agg_count = 0;
1655 
1656 	rxr->rx_prod = NEXT_RX(prod);
1657 	cons = RING_RX(bp, NEXT_RX(cons));
1658 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1659 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1660 
1661 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1662 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1663 	cons_rx_buf->data = NULL;
1664 }
1665 
1666 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1667 {
1668 	if (agg_bufs)
1669 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1670 }
1671 
1672 #ifdef CONFIG_INET
1673 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1674 {
1675 	struct udphdr *uh = NULL;
1676 
1677 	if (ip_proto == htons(ETH_P_IP)) {
1678 		struct iphdr *iph = (struct iphdr *)skb->data;
1679 
1680 		if (iph->protocol == IPPROTO_UDP)
1681 			uh = (struct udphdr *)(iph + 1);
1682 	} else {
1683 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1684 
1685 		if (iph->nexthdr == IPPROTO_UDP)
1686 			uh = (struct udphdr *)(iph + 1);
1687 	}
1688 	if (uh) {
1689 		if (uh->check)
1690 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1691 		else
1692 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1693 	}
1694 }
1695 #endif
1696 
1697 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1698 					   int payload_off, int tcp_ts,
1699 					   struct sk_buff *skb)
1700 {
1701 #ifdef CONFIG_INET
1702 	struct tcphdr *th;
1703 	int len, nw_off;
1704 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1705 	u32 hdr_info = tpa_info->hdr_info;
1706 	bool loopback = false;
1707 
1708 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1709 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1710 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1711 
1712 	/* If the packet is an internal loopback packet, the offsets will
1713 	 * have an extra 4 bytes.
1714 	 */
1715 	if (inner_mac_off == 4) {
1716 		loopback = true;
1717 	} else if (inner_mac_off > 4) {
1718 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1719 					    ETH_HLEN - 2));
1720 
1721 		/* We only support inner iPv4/ipv6.  If we don't see the
1722 		 * correct protocol ID, it must be a loopback packet where
1723 		 * the offsets are off by 4.
1724 		 */
1725 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1726 			loopback = true;
1727 	}
1728 	if (loopback) {
1729 		/* internal loopback packet, subtract all offsets by 4 */
1730 		inner_ip_off -= 4;
1731 		inner_mac_off -= 4;
1732 		outer_ip_off -= 4;
1733 	}
1734 
1735 	nw_off = inner_ip_off - ETH_HLEN;
1736 	skb_set_network_header(skb, nw_off);
1737 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1738 		struct ipv6hdr *iph = ipv6_hdr(skb);
1739 
1740 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1741 		len = skb->len - skb_transport_offset(skb);
1742 		th = tcp_hdr(skb);
1743 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1744 	} else {
1745 		struct iphdr *iph = ip_hdr(skb);
1746 
1747 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1748 		len = skb->len - skb_transport_offset(skb);
1749 		th = tcp_hdr(skb);
1750 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1751 	}
1752 
1753 	if (inner_mac_off) { /* tunnel */
1754 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1755 					    ETH_HLEN - 2));
1756 
1757 		bnxt_gro_tunnel(skb, proto);
1758 	}
1759 #endif
1760 	return skb;
1761 }
1762 
1763 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1764 					   int payload_off, int tcp_ts,
1765 					   struct sk_buff *skb)
1766 {
1767 #ifdef CONFIG_INET
1768 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1769 	u32 hdr_info = tpa_info->hdr_info;
1770 	int iphdr_len, nw_off;
1771 
1772 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1773 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1774 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1775 
1776 	nw_off = inner_ip_off - ETH_HLEN;
1777 	skb_set_network_header(skb, nw_off);
1778 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1779 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1780 	skb_set_transport_header(skb, nw_off + iphdr_len);
1781 
1782 	if (inner_mac_off) { /* tunnel */
1783 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1784 					    ETH_HLEN - 2));
1785 
1786 		bnxt_gro_tunnel(skb, proto);
1787 	}
1788 #endif
1789 	return skb;
1790 }
1791 
1792 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1793 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1794 
1795 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1796 					   int payload_off, int tcp_ts,
1797 					   struct sk_buff *skb)
1798 {
1799 #ifdef CONFIG_INET
1800 	struct tcphdr *th;
1801 	int len, nw_off, tcp_opt_len = 0;
1802 
1803 	if (tcp_ts)
1804 		tcp_opt_len = 12;
1805 
1806 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1807 		struct iphdr *iph;
1808 
1809 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1810 			 ETH_HLEN;
1811 		skb_set_network_header(skb, nw_off);
1812 		iph = ip_hdr(skb);
1813 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1814 		len = skb->len - skb_transport_offset(skb);
1815 		th = tcp_hdr(skb);
1816 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1817 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1818 		struct ipv6hdr *iph;
1819 
1820 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1821 			 ETH_HLEN;
1822 		skb_set_network_header(skb, nw_off);
1823 		iph = ipv6_hdr(skb);
1824 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1825 		len = skb->len - skb_transport_offset(skb);
1826 		th = tcp_hdr(skb);
1827 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1828 	} else {
1829 		dev_kfree_skb_any(skb);
1830 		return NULL;
1831 	}
1832 
1833 	if (nw_off) /* tunnel */
1834 		bnxt_gro_tunnel(skb, skb->protocol);
1835 #endif
1836 	return skb;
1837 }
1838 
1839 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1840 					   struct bnxt_tpa_info *tpa_info,
1841 					   struct rx_tpa_end_cmp *tpa_end,
1842 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1843 					   struct sk_buff *skb,
1844 					   struct bnxt_rx_sw_stats *rx_stats)
1845 {
1846 #ifdef CONFIG_INET
1847 	int payload_off;
1848 	u16 segs;
1849 
1850 	segs = TPA_END_TPA_SEGS(tpa_end);
1851 	if (segs == 1)
1852 		return skb;
1853 
1854 	rx_stats->rx_hw_gro_packets++;
1855 	rx_stats->rx_hw_gro_wire_packets += segs;
1856 
1857 	NAPI_GRO_CB(skb)->count = segs;
1858 	skb_shinfo(skb)->gso_size =
1859 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1860 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1861 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1862 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1863 	else
1864 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1865 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1866 	if (likely(skb))
1867 		tcp_gro_complete(skb);
1868 #endif
1869 	return skb;
1870 }
1871 
1872 /* Given the cfa_code of a received packet determine which
1873  * netdev (vf-rep or PF) the packet is destined to.
1874  */
1875 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1876 {
1877 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1878 
1879 	/* if vf-rep dev is NULL, it must belong to the PF */
1880 	return dev ? dev : bp->dev;
1881 }
1882 
1883 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1884 					   struct bnxt_cp_ring_info *cpr,
1885 					   u32 *raw_cons,
1886 					   struct rx_tpa_end_cmp *tpa_end,
1887 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1888 					   u8 *event)
1889 {
1890 	struct bnxt_napi *bnapi = cpr->bnapi;
1891 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1892 	struct net_device *dev = bp->dev;
1893 	u8 *data_ptr, agg_bufs;
1894 	unsigned int len;
1895 	struct bnxt_tpa_info *tpa_info;
1896 	dma_addr_t mapping;
1897 	struct sk_buff *skb;
1898 	u16 idx = 0, agg_id;
1899 	void *data;
1900 	bool gro;
1901 
1902 	if (unlikely(bnapi->in_reset)) {
1903 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1904 
1905 		if (rc < 0)
1906 			return ERR_PTR(-EBUSY);
1907 		return NULL;
1908 	}
1909 
1910 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1911 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1912 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1913 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1914 		tpa_info = &rxr->rx_tpa[agg_id];
1915 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1916 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1917 				    agg_bufs, tpa_info->agg_count);
1918 			agg_bufs = tpa_info->agg_count;
1919 		}
1920 		tpa_info->agg_count = 0;
1921 		*event |= BNXT_AGG_EVENT;
1922 		bnxt_free_agg_idx(rxr, agg_id);
1923 		idx = agg_id;
1924 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1925 	} else {
1926 		agg_id = TPA_END_AGG_ID(tpa_end);
1927 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1928 		tpa_info = &rxr->rx_tpa[agg_id];
1929 		idx = RING_CMP(*raw_cons);
1930 		if (agg_bufs) {
1931 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1932 				return ERR_PTR(-EBUSY);
1933 
1934 			*event |= BNXT_AGG_EVENT;
1935 			idx = NEXT_CMP(idx);
1936 		}
1937 		gro = !!TPA_END_GRO(tpa_end);
1938 	}
1939 	data = tpa_info->data;
1940 	data_ptr = tpa_info->data_ptr;
1941 	prefetch(data_ptr);
1942 	len = tpa_info->len;
1943 	mapping = tpa_info->mapping;
1944 
1945 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1946 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1947 		if (agg_bufs > MAX_SKB_FRAGS)
1948 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1949 				    agg_bufs, (int)MAX_SKB_FRAGS);
1950 		return NULL;
1951 	}
1952 
1953 	if (len <= bp->rx_copybreak) {
1954 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1955 		if (!skb) {
1956 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1957 			cpr->sw_stats->rx.rx_oom_discards += 1;
1958 			return NULL;
1959 		}
1960 	} else {
1961 		u8 *new_data;
1962 		dma_addr_t new_mapping;
1963 
1964 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1965 						GFP_ATOMIC);
1966 		if (!new_data) {
1967 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1968 			cpr->sw_stats->rx.rx_oom_discards += 1;
1969 			return NULL;
1970 		}
1971 
1972 		tpa_info->data = new_data;
1973 		tpa_info->data_ptr = new_data + bp->rx_offset;
1974 		tpa_info->mapping = new_mapping;
1975 
1976 		skb = napi_build_skb(data, bp->rx_buf_size);
1977 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1978 					bp->rx_buf_use_size, bp->rx_dir);
1979 
1980 		if (!skb) {
1981 			page_pool_free_va(rxr->head_pool, data, true);
1982 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1983 			cpr->sw_stats->rx.rx_oom_discards += 1;
1984 			return NULL;
1985 		}
1986 		skb_mark_for_recycle(skb);
1987 		skb_reserve(skb, bp->rx_offset);
1988 		skb_put(skb, len);
1989 	}
1990 
1991 	if (agg_bufs) {
1992 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1993 					      true);
1994 		if (!skb) {
1995 			/* Page reuse already handled by bnxt_rx_pages(). */
1996 			cpr->sw_stats->rx.rx_oom_discards += 1;
1997 			return NULL;
1998 		}
1999 	}
2000 
2001 	if (tpa_info->cfa_code_valid)
2002 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
2003 	skb->protocol = eth_type_trans(skb, dev);
2004 
2005 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
2006 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
2007 
2008 	if (tpa_info->vlan_valid &&
2009 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
2010 		__be16 vlan_proto = htons(tpa_info->metadata >>
2011 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
2012 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2013 
2014 		if (eth_type_vlan(vlan_proto)) {
2015 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2016 		} else {
2017 			dev_kfree_skb(skb);
2018 			return NULL;
2019 		}
2020 	}
2021 
2022 	skb_checksum_none_assert(skb);
2023 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
2024 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2025 		skb->csum_level =
2026 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
2027 	}
2028 
2029 	if (gro)
2030 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
2031 				   &cpr->sw_stats->rx);
2032 
2033 	return skb;
2034 }
2035 
2036 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
2037 			 struct rx_agg_cmp *rx_agg)
2038 {
2039 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2040 	struct bnxt_tpa_info *tpa_info;
2041 
2042 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2043 	tpa_info = &rxr->rx_tpa[agg_id];
2044 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2045 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2046 }
2047 
2048 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2049 			     struct sk_buff *skb)
2050 {
2051 	skb_mark_for_recycle(skb);
2052 
2053 	if (skb->dev != bp->dev) {
2054 		/* this packet belongs to a vf-rep */
2055 		bnxt_vf_rep_rx(bp, skb);
2056 		return;
2057 	}
2058 	skb_record_rx_queue(skb, bnapi->index);
2059 	napi_gro_receive(&bnapi->napi, skb);
2060 }
2061 
2062 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2063 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2064 {
2065 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2066 
2067 	if (BNXT_PTP_RX_TS_VALID(flags))
2068 		goto ts_valid;
2069 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2070 		return false;
2071 
2072 ts_valid:
2073 	*cmpl_ts = ts;
2074 	return true;
2075 }
2076 
2077 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2078 				    struct rx_cmp *rxcmp,
2079 				    struct rx_cmp_ext *rxcmp1)
2080 {
2081 	__be16 vlan_proto;
2082 	u16 vtag;
2083 
2084 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2085 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2086 		u32 meta_data;
2087 
2088 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2089 			return skb;
2090 
2091 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2092 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2093 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2094 		if (eth_type_vlan(vlan_proto))
2095 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2096 		else
2097 			goto vlan_err;
2098 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2099 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2100 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2101 
2102 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2103 				vlan_proto = htons(ETH_P_8021Q);
2104 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2105 				vlan_proto = htons(ETH_P_8021AD);
2106 			else
2107 				goto vlan_err;
2108 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2109 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2110 		}
2111 	}
2112 	return skb;
2113 vlan_err:
2114 	skb_mark_for_recycle(skb);
2115 	dev_kfree_skb(skb);
2116 	return NULL;
2117 }
2118 
2119 /* returns the following:
2120  * 1       - 1 packet successfully received
2121  * 0       - successful TPA_START, packet not completed yet
2122  * -EBUSY  - completion ring does not have all the agg buffers yet
2123  * -ENOMEM - packet aborted due to out of memory
2124  * -EIO    - packet aborted due to hw error indicated in BD
2125  */
2126 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2127 		       u32 *raw_cons, u8 *event)
2128 {
2129 	struct bnxt_napi *bnapi = cpr->bnapi;
2130 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2131 	struct net_device *dev = bp->dev;
2132 	struct rx_cmp *rxcmp;
2133 	struct rx_cmp_ext *rxcmp1;
2134 	u32 tmp_raw_cons = *raw_cons;
2135 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2136 	struct skb_shared_info *sinfo;
2137 	struct bnxt_xdp_buff bnxt_xdp;
2138 	struct bnxt_sw_rx_bd *rx_buf;
2139 	unsigned int len;
2140 	u8 *data_ptr, agg_bufs, cmp_type;
2141 	bool xdp_active = false;
2142 	dma_addr_t dma_addr;
2143 	struct sk_buff *skb;
2144 	u32 flags, misc;
2145 	u32 cmpl_ts;
2146 	void *data;
2147 	int rc = 0;
2148 
2149 	rxcmp = (struct rx_cmp *)
2150 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2151 
2152 	cmp_type = RX_CMP_TYPE(rxcmp);
2153 
2154 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2155 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2156 		goto next_rx_no_prod_no_len;
2157 	}
2158 
2159 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2160 	cp_cons = RING_CMP(tmp_raw_cons);
2161 	rxcmp1 = (struct rx_cmp_ext *)
2162 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2163 
2164 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2165 		return -EBUSY;
2166 
2167 	/* The valid test of the entry must be done first before
2168 	 * reading any further.
2169 	 */
2170 	dma_rmb();
2171 	prod = rxr->rx_prod;
2172 
2173 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2174 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2175 		bnxt_tpa_start(bp, rxr, cmp_type,
2176 			       (struct rx_tpa_start_cmp *)rxcmp,
2177 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2178 
2179 		*event |= BNXT_RX_EVENT;
2180 		goto next_rx_no_prod_no_len;
2181 
2182 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2183 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2184 				   (struct rx_tpa_end_cmp *)rxcmp,
2185 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2186 
2187 		if (IS_ERR(skb))
2188 			return -EBUSY;
2189 
2190 		rc = -ENOMEM;
2191 		if (likely(skb)) {
2192 			bnxt_deliver_skb(bp, bnapi, skb);
2193 			rc = 1;
2194 		}
2195 		*event |= BNXT_RX_EVENT;
2196 		goto next_rx_no_prod_no_len;
2197 	}
2198 
2199 	cons = rxcmp->rx_cmp_opaque;
2200 	if (unlikely(cons != rxr->rx_next_cons)) {
2201 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2202 
2203 		/* 0xffff is forced error, don't print it */
2204 		if (rxr->rx_next_cons != 0xffff)
2205 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2206 				    cons, rxr->rx_next_cons);
2207 		bnxt_sched_reset_rxr(bp, rxr);
2208 		if (rc1)
2209 			return rc1;
2210 		goto next_rx_no_prod_no_len;
2211 	}
2212 	rx_buf = &rxr->rx_buf_ring[cons];
2213 	data = rx_buf->data;
2214 	data_ptr = rx_buf->data_ptr;
2215 	prefetch(data_ptr);
2216 
2217 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2218 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2219 
2220 	if (agg_bufs) {
2221 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2222 			return -EBUSY;
2223 
2224 		cp_cons = NEXT_CMP(cp_cons);
2225 		*event |= BNXT_AGG_EVENT;
2226 	}
2227 	*event |= BNXT_RX_EVENT;
2228 
2229 	rx_buf->data = NULL;
2230 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2231 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2232 
2233 		bnxt_reuse_rx_data(rxr, cons, data);
2234 		if (agg_bufs)
2235 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2236 					       false);
2237 
2238 		rc = -EIO;
2239 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2240 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2241 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2242 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2243 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2244 						 rx_err);
2245 				bnxt_sched_reset_rxr(bp, rxr);
2246 			}
2247 		}
2248 		goto next_rx_no_len;
2249 	}
2250 
2251 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2252 	len = flags >> RX_CMP_LEN_SHIFT;
2253 	dma_addr = rx_buf->mapping;
2254 
2255 	if (bnxt_xdp_attached(bp, rxr)) {
2256 		bnxt_xdp.rxcmp = rxcmp;
2257 		bnxt_xdp.rxcmp1 = rxcmp1;
2258 		bnxt_xdp.cmp_type = cmp_type;
2259 
2260 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &bnxt_xdp.xdp);
2261 		if (agg_bufs) {
2262 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr,
2263 							       &bnxt_xdp.xdp,
2264 							       cp_cons,
2265 							       agg_bufs,
2266 							       false);
2267 			if (!frag_len)
2268 				goto oom_next_rx;
2269 
2270 		}
2271 		xdp_active = true;
2272 	}
2273 
2274 	if (xdp_active) {
2275 		if (bnxt_rx_xdp(bp, rxr, cons, &bnxt_xdp.xdp, data, &data_ptr,
2276 				&len, event)) {
2277 			rc = 1;
2278 			goto next_rx;
2279 		}
2280 		if (xdp_buff_has_frags(&bnxt_xdp.xdp)) {
2281 			sinfo = xdp_get_shared_info_from_buff(&bnxt_xdp.xdp);
2282 			agg_bufs = sinfo->nr_frags;
2283 		} else {
2284 			agg_bufs = 0;
2285 		}
2286 	}
2287 
2288 	if (len <= bp->rx_copybreak) {
2289 		if (!xdp_active)
2290 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2291 		else
2292 			skb = bnxt_copy_xdp(bnapi, &bnxt_xdp.xdp, len,
2293 					    dma_addr);
2294 		bnxt_reuse_rx_data(rxr, cons, data);
2295 		if (!skb) {
2296 			if (agg_bufs) {
2297 				if (!xdp_active)
2298 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2299 							       agg_bufs, false);
2300 				else
2301 					bnxt_xdp_buff_frags_free(rxr,
2302 								 &bnxt_xdp.xdp);
2303 			}
2304 			goto oom_next_rx;
2305 		}
2306 	} else {
2307 		u32 payload;
2308 
2309 		if (rx_buf->data_ptr == data_ptr)
2310 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2311 		else
2312 			payload = 0;
2313 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2314 				      payload | len);
2315 		if (!skb)
2316 			goto oom_next_rx;
2317 	}
2318 
2319 	if (agg_bufs) {
2320 		if (!xdp_active) {
2321 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2322 						      agg_bufs, false);
2323 			if (!skb)
2324 				goto oom_next_rx;
2325 		} else {
2326 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2327 						 rxr, &bnxt_xdp.xdp);
2328 			if (!skb) {
2329 				/* we should be able to free the old skb here */
2330 				bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2331 				goto oom_next_rx;
2332 			}
2333 		}
2334 	}
2335 
2336 	if (RX_CMP_HASH_VALID(rxcmp)) {
2337 		enum pkt_hash_types type;
2338 
2339 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2340 			type = bnxt_rss_ext_op(bp, rxcmp);
2341 		} else {
2342 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2343 
2344 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2345 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2346 				type = PKT_HASH_TYPE_L4;
2347 			else
2348 				type = PKT_HASH_TYPE_L3;
2349 		}
2350 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2351 	}
2352 
2353 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2354 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2355 	skb->protocol = eth_type_trans(skb, dev);
2356 
2357 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2358 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2359 		if (!skb)
2360 			goto next_rx;
2361 	}
2362 
2363 	skb_checksum_none_assert(skb);
2364 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2365 		if (dev->features & NETIF_F_RXCSUM) {
2366 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2367 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2368 		}
2369 	} else {
2370 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2371 			if (dev->features & NETIF_F_RXCSUM)
2372 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2373 		}
2374 	}
2375 
2376 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2377 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2378 			u64 ns, ts;
2379 
2380 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2381 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2382 
2383 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2384 				memset(skb_hwtstamps(skb), 0,
2385 				       sizeof(*skb_hwtstamps(skb)));
2386 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2387 			}
2388 		}
2389 	}
2390 	bnxt_deliver_skb(bp, bnapi, skb);
2391 	rc = 1;
2392 
2393 next_rx:
2394 	cpr->rx_packets += 1;
2395 	cpr->rx_bytes += len;
2396 
2397 next_rx_no_len:
2398 	rxr->rx_prod = NEXT_RX(prod);
2399 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2400 
2401 next_rx_no_prod_no_len:
2402 	*raw_cons = tmp_raw_cons;
2403 
2404 	return rc;
2405 
2406 oom_next_rx:
2407 	cpr->sw_stats->rx.rx_oom_discards += 1;
2408 	rc = -ENOMEM;
2409 	goto next_rx;
2410 }
2411 
2412 /* In netpoll mode, if we are using a combined completion ring, we need to
2413  * discard the rx packets and recycle the buffers.
2414  */
2415 static int bnxt_force_rx_discard(struct bnxt *bp,
2416 				 struct bnxt_cp_ring_info *cpr,
2417 				 u32 *raw_cons, u8 *event)
2418 {
2419 	u32 tmp_raw_cons = *raw_cons;
2420 	struct rx_cmp_ext *rxcmp1;
2421 	struct rx_cmp *rxcmp;
2422 	u16 cp_cons;
2423 	u8 cmp_type;
2424 	int rc;
2425 
2426 	cp_cons = RING_CMP(tmp_raw_cons);
2427 	rxcmp = (struct rx_cmp *)
2428 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2429 
2430 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2431 	cp_cons = RING_CMP(tmp_raw_cons);
2432 	rxcmp1 = (struct rx_cmp_ext *)
2433 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2434 
2435 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2436 		return -EBUSY;
2437 
2438 	/* The valid test of the entry must be done first before
2439 	 * reading any further.
2440 	 */
2441 	dma_rmb();
2442 	cmp_type = RX_CMP_TYPE(rxcmp);
2443 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2444 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2445 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2446 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2447 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2448 		struct rx_tpa_end_cmp_ext *tpa_end1;
2449 
2450 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2451 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2452 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2453 	}
2454 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2455 	if (rc && rc != -EBUSY)
2456 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2457 	return rc;
2458 }
2459 
2460 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2461 {
2462 	struct bnxt_fw_health *fw_health = bp->fw_health;
2463 	u32 reg = fw_health->regs[reg_idx];
2464 	u32 reg_type, reg_off, val = 0;
2465 
2466 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2467 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2468 	switch (reg_type) {
2469 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2470 		pci_read_config_dword(bp->pdev, reg_off, &val);
2471 		break;
2472 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2473 		reg_off = fw_health->mapped_regs[reg_idx];
2474 		fallthrough;
2475 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2476 		val = readl(bp->bar0 + reg_off);
2477 		break;
2478 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2479 		val = readl(bp->bar1 + reg_off);
2480 		break;
2481 	}
2482 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2483 		val &= fw_health->fw_reset_inprog_reg_mask;
2484 	return val;
2485 }
2486 
2487 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2488 {
2489 	int i;
2490 
2491 	for (i = 0; i < bp->rx_nr_rings; i++) {
2492 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2493 		struct bnxt_ring_grp_info *grp_info;
2494 
2495 		grp_info = &bp->grp_info[grp_idx];
2496 		if (grp_info->agg_fw_ring_id == ring_id)
2497 			return grp_idx;
2498 	}
2499 	return INVALID_HW_RING_ID;
2500 }
2501 
2502 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2503 {
2504 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2505 
2506 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2507 		return link_info->force_link_speed2;
2508 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2509 		return link_info->force_pam4_link_speed;
2510 	return link_info->force_link_speed;
2511 }
2512 
2513 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2514 {
2515 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2516 
2517 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2518 		link_info->req_link_speed = link_info->force_link_speed2;
2519 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2520 		switch (link_info->req_link_speed) {
2521 		case BNXT_LINK_SPEED_50GB_PAM4:
2522 		case BNXT_LINK_SPEED_100GB_PAM4:
2523 		case BNXT_LINK_SPEED_200GB_PAM4:
2524 		case BNXT_LINK_SPEED_400GB_PAM4:
2525 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2526 			break;
2527 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2528 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2529 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2530 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2531 			break;
2532 		default:
2533 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2534 		}
2535 		return;
2536 	}
2537 	link_info->req_link_speed = link_info->force_link_speed;
2538 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2539 	if (link_info->force_pam4_link_speed) {
2540 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2541 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2542 	}
2543 }
2544 
2545 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2546 {
2547 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2548 
2549 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2550 		link_info->advertising = link_info->auto_link_speeds2;
2551 		return;
2552 	}
2553 	link_info->advertising = link_info->auto_link_speeds;
2554 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2555 }
2556 
2557 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2558 {
2559 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2560 
2561 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2562 		if (link_info->req_link_speed != link_info->force_link_speed2)
2563 			return true;
2564 		return false;
2565 	}
2566 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2567 	    link_info->req_link_speed != link_info->force_link_speed)
2568 		return true;
2569 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2570 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2571 		return true;
2572 	return false;
2573 }
2574 
2575 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2576 {
2577 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2578 
2579 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2580 		if (link_info->advertising != link_info->auto_link_speeds2)
2581 			return true;
2582 		return false;
2583 	}
2584 	if (link_info->advertising != link_info->auto_link_speeds ||
2585 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2586 		return true;
2587 	return false;
2588 }
2589 
2590 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2591 {
2592 	u32 flags = bp->ctx->ctx_arr[type].flags;
2593 
2594 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2595 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2596 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2597 }
2598 
2599 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2600 {
2601 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2602 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2603 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2604 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2605 	struct bnxt_bs_trace_info *bs_trace;
2606 	int last_pg;
2607 
2608 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2609 		return;
2610 
2611 	mem_size = ctxm->max_entries * ctxm->entry_size;
2612 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2613 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2614 
2615 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2616 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2617 
2618 	rmem = &ctx_pg[0].ring_mem;
2619 	bs_trace = &bp->bs_trace[trace_type];
2620 	bs_trace->ctx_type = ctxm->type;
2621 	bs_trace->trace_type = trace_type;
2622 	if (pages > MAX_CTX_PAGES) {
2623 		int last_pg_dir = rmem->nr_pages - 1;
2624 
2625 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2626 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2627 	} else {
2628 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2629 	}
2630 	bs_trace->magic_byte += magic_byte_offset;
2631 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2632 }
2633 
2634 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2635 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2636 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2637 
2638 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2639 	(((data2) &							\
2640 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2641 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2642 
2643 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2644 	((data2) &							\
2645 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2646 
2647 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2648 	(((data2) &							\
2649 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2650 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2651 
2652 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2653 	((data1) &							\
2654 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2655 
2656 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2657 	(((data1) &							\
2658 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2659 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2660 
2661 /* Return true if the workqueue has to be scheduled */
2662 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2663 {
2664 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2665 
2666 	switch (err_type) {
2667 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2668 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2669 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2670 		break;
2671 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2672 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2673 		break;
2674 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2675 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2676 		break;
2677 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2678 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2679 		char *threshold_type;
2680 		bool notify = false;
2681 		char *dir_str;
2682 
2683 		switch (type) {
2684 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2685 			threshold_type = "warning";
2686 			break;
2687 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2688 			threshold_type = "critical";
2689 			break;
2690 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2691 			threshold_type = "fatal";
2692 			break;
2693 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2694 			threshold_type = "shutdown";
2695 			break;
2696 		default:
2697 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2698 			return false;
2699 		}
2700 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2701 			dir_str = "above";
2702 			notify = true;
2703 		} else {
2704 			dir_str = "below";
2705 		}
2706 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2707 			    dir_str, threshold_type);
2708 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2709 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2710 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2711 		if (notify) {
2712 			bp->thermal_threshold_type = type;
2713 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2714 			return true;
2715 		}
2716 		return false;
2717 	}
2718 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2719 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2720 		break;
2721 	default:
2722 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2723 			   err_type);
2724 		break;
2725 	}
2726 	return false;
2727 }
2728 
2729 #define BNXT_GET_EVENT_PORT(data)	\
2730 	((data) &			\
2731 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2732 
2733 #define BNXT_EVENT_RING_TYPE(data2)	\
2734 	((data2) &			\
2735 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2736 
2737 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2738 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2739 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2740 
2741 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2742 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2743 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2744 
2745 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2746 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2747 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2748 
2749 #define BNXT_PHC_BITS	48
2750 
2751 static int bnxt_async_event_process(struct bnxt *bp,
2752 				    struct hwrm_async_event_cmpl *cmpl)
2753 {
2754 	u16 event_id = le16_to_cpu(cmpl->event_id);
2755 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2756 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2757 
2758 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2759 		   event_id, data1, data2);
2760 
2761 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2762 	switch (event_id) {
2763 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2764 		struct bnxt_link_info *link_info = &bp->link_info;
2765 
2766 		if (BNXT_VF(bp))
2767 			goto async_event_process_exit;
2768 
2769 		/* print unsupported speed warning in forced speed mode only */
2770 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2771 		    (data1 & 0x20000)) {
2772 			u16 fw_speed = bnxt_get_force_speed(link_info);
2773 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2774 
2775 			if (speed != SPEED_UNKNOWN)
2776 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2777 					    speed);
2778 		}
2779 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2780 	}
2781 		fallthrough;
2782 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2783 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2784 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2785 		fallthrough;
2786 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2787 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2788 		break;
2789 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2790 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2791 		break;
2792 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2793 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2794 
2795 		if (BNXT_VF(bp))
2796 			break;
2797 
2798 		if (bp->pf.port_id != port_id)
2799 			break;
2800 
2801 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2802 		break;
2803 	}
2804 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2805 		if (BNXT_PF(bp))
2806 			goto async_event_process_exit;
2807 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2808 		break;
2809 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2810 		char *type_str = "Solicited";
2811 
2812 		if (!bp->fw_health)
2813 			goto async_event_process_exit;
2814 
2815 		bp->fw_reset_timestamp = jiffies;
2816 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2817 		if (!bp->fw_reset_min_dsecs)
2818 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2819 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2820 		if (!bp->fw_reset_max_dsecs)
2821 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2822 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2823 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2824 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2825 			type_str = "Fatal";
2826 			bp->fw_health->fatalities++;
2827 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2828 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2829 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2830 			type_str = "Non-fatal";
2831 			bp->fw_health->survivals++;
2832 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2833 		}
2834 		netif_warn(bp, hw, bp->dev,
2835 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2836 			   type_str, data1, data2,
2837 			   bp->fw_reset_min_dsecs * 100,
2838 			   bp->fw_reset_max_dsecs * 100);
2839 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2840 		break;
2841 	}
2842 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2843 		struct bnxt_fw_health *fw_health = bp->fw_health;
2844 		char *status_desc = "healthy";
2845 		u32 status;
2846 
2847 		if (!fw_health)
2848 			goto async_event_process_exit;
2849 
2850 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2851 			fw_health->enabled = false;
2852 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2853 			break;
2854 		}
2855 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2856 		fw_health->tmr_multiplier =
2857 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2858 				     bp->current_interval * 10);
2859 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2860 		if (!fw_health->enabled)
2861 			fw_health->last_fw_heartbeat =
2862 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2863 		fw_health->last_fw_reset_cnt =
2864 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2865 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2866 		if (status != BNXT_FW_STATUS_HEALTHY)
2867 			status_desc = "unhealthy";
2868 		netif_info(bp, drv, bp->dev,
2869 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2870 			   fw_health->primary ? "primary" : "backup", status,
2871 			   status_desc, fw_health->last_fw_reset_cnt);
2872 		if (!fw_health->enabled) {
2873 			/* Make sure tmr_counter is set and visible to
2874 			 * bnxt_health_check() before setting enabled to true.
2875 			 */
2876 			smp_wmb();
2877 			fw_health->enabled = true;
2878 		}
2879 		goto async_event_process_exit;
2880 	}
2881 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2882 		netif_notice(bp, hw, bp->dev,
2883 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2884 			     data1, data2);
2885 		goto async_event_process_exit;
2886 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2887 		struct bnxt_rx_ring_info *rxr;
2888 		u16 grp_idx;
2889 
2890 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2891 			goto async_event_process_exit;
2892 
2893 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2894 			    BNXT_EVENT_RING_TYPE(data2), data1);
2895 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2896 			goto async_event_process_exit;
2897 
2898 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2899 		if (grp_idx == INVALID_HW_RING_ID) {
2900 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2901 				    data1);
2902 			goto async_event_process_exit;
2903 		}
2904 		rxr = bp->bnapi[grp_idx]->rx_ring;
2905 		bnxt_sched_reset_rxr(bp, rxr);
2906 		goto async_event_process_exit;
2907 	}
2908 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2909 		struct bnxt_fw_health *fw_health = bp->fw_health;
2910 
2911 		netif_notice(bp, hw, bp->dev,
2912 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2913 			     data1, data2);
2914 		if (fw_health) {
2915 			fw_health->echo_req_data1 = data1;
2916 			fw_health->echo_req_data2 = data2;
2917 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2918 			break;
2919 		}
2920 		goto async_event_process_exit;
2921 	}
2922 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2923 		bnxt_ptp_pps_event(bp, data1, data2);
2924 		goto async_event_process_exit;
2925 	}
2926 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2927 		if (bnxt_event_error_report(bp, data1, data2))
2928 			break;
2929 		goto async_event_process_exit;
2930 	}
2931 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2932 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2933 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2934 			if (BNXT_PTP_USE_RTC(bp)) {
2935 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2936 				unsigned long flags;
2937 				u64 ns;
2938 
2939 				if (!ptp)
2940 					goto async_event_process_exit;
2941 
2942 				bnxt_ptp_update_current_time(bp);
2943 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2944 				       BNXT_PHC_BITS) | ptp->current_time);
2945 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2946 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2947 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2948 			}
2949 			break;
2950 		}
2951 		goto async_event_process_exit;
2952 	}
2953 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2954 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2955 
2956 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2957 		goto async_event_process_exit;
2958 	}
2959 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2960 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2961 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2962 
2963 		if (type >= ARRAY_SIZE(bp->bs_trace))
2964 			goto async_event_process_exit;
2965 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2966 		goto async_event_process_exit;
2967 	}
2968 	default:
2969 		goto async_event_process_exit;
2970 	}
2971 	__bnxt_queue_sp_work(bp);
2972 async_event_process_exit:
2973 	bnxt_ulp_async_events(bp, cmpl);
2974 	return 0;
2975 }
2976 
2977 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2978 {
2979 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2980 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2981 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2982 				(struct hwrm_fwd_req_cmpl *)txcmp;
2983 
2984 	switch (cmpl_type) {
2985 	case CMPL_BASE_TYPE_HWRM_DONE:
2986 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2987 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2988 		break;
2989 
2990 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2991 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2992 
2993 		if ((vf_id < bp->pf.first_vf_id) ||
2994 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2995 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2996 				   vf_id);
2997 			return -EINVAL;
2998 		}
2999 
3000 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
3001 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
3002 		break;
3003 
3004 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
3005 		bnxt_async_event_process(bp,
3006 					 (struct hwrm_async_event_cmpl *)txcmp);
3007 		break;
3008 
3009 	default:
3010 		break;
3011 	}
3012 
3013 	return 0;
3014 }
3015 
3016 static bool bnxt_vnic_is_active(struct bnxt *bp)
3017 {
3018 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
3019 
3020 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
3021 }
3022 
3023 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
3024 {
3025 	struct bnxt_napi *bnapi = dev_instance;
3026 	struct bnxt *bp = bnapi->bp;
3027 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3028 	u32 cons = RING_CMP(cpr->cp_raw_cons);
3029 
3030 	cpr->event_ctr++;
3031 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
3032 	napi_schedule(&bnapi->napi);
3033 	return IRQ_HANDLED;
3034 }
3035 
3036 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
3037 {
3038 	u32 raw_cons = cpr->cp_raw_cons;
3039 	u16 cons = RING_CMP(raw_cons);
3040 	struct tx_cmp *txcmp;
3041 
3042 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3043 
3044 	return TX_CMP_VALID(txcmp, raw_cons);
3045 }
3046 
3047 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3048 			    int budget)
3049 {
3050 	struct bnxt_napi *bnapi = cpr->bnapi;
3051 	u32 raw_cons = cpr->cp_raw_cons;
3052 	bool flush_xdp = false;
3053 	u32 cons;
3054 	int rx_pkts = 0;
3055 	u8 event = 0;
3056 	struct tx_cmp *txcmp;
3057 
3058 	cpr->has_more_work = 0;
3059 	cpr->had_work_done = 1;
3060 	while (1) {
3061 		u8 cmp_type;
3062 		int rc;
3063 
3064 		cons = RING_CMP(raw_cons);
3065 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3066 
3067 		if (!TX_CMP_VALID(txcmp, raw_cons))
3068 			break;
3069 
3070 		/* The valid test of the entry must be done first before
3071 		 * reading any further.
3072 		 */
3073 		dma_rmb();
3074 		cmp_type = TX_CMP_TYPE(txcmp);
3075 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3076 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3077 			u32 opaque = txcmp->tx_cmp_opaque;
3078 			struct bnxt_tx_ring_info *txr;
3079 			u16 tx_freed;
3080 
3081 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3082 			event |= BNXT_TX_CMP_EVENT;
3083 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3084 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3085 			else
3086 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3087 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3088 				   bp->tx_ring_mask;
3089 			/* return full budget so NAPI will complete. */
3090 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3091 				rx_pkts = budget;
3092 				raw_cons = NEXT_RAW_CMP(raw_cons);
3093 				if (budget)
3094 					cpr->has_more_work = 1;
3095 				break;
3096 			}
3097 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3098 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3099 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3100 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3101 			if (likely(budget))
3102 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3103 			else
3104 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3105 							   &event);
3106 			if (event & BNXT_REDIRECT_EVENT)
3107 				flush_xdp = true;
3108 			if (likely(rc >= 0))
3109 				rx_pkts += rc;
3110 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3111 			 * the NAPI budget.  Otherwise, we may potentially loop
3112 			 * here forever if we consistently cannot allocate
3113 			 * buffers.
3114 			 */
3115 			else if (rc == -ENOMEM && budget)
3116 				rx_pkts++;
3117 			else if (rc == -EBUSY)	/* partial completion */
3118 				break;
3119 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3120 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3121 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3122 			bnxt_hwrm_handler(bp, txcmp);
3123 		}
3124 		raw_cons = NEXT_RAW_CMP(raw_cons);
3125 
3126 		if (rx_pkts && rx_pkts == budget) {
3127 			cpr->has_more_work = 1;
3128 			break;
3129 		}
3130 	}
3131 
3132 	if (flush_xdp) {
3133 		xdp_do_flush();
3134 		event &= ~BNXT_REDIRECT_EVENT;
3135 	}
3136 
3137 	if (event & BNXT_TX_EVENT) {
3138 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3139 		u16 prod = txr->tx_prod;
3140 
3141 		/* Sync BD data before updating doorbell */
3142 		wmb();
3143 
3144 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3145 		event &= ~BNXT_TX_EVENT;
3146 	}
3147 
3148 	cpr->cp_raw_cons = raw_cons;
3149 	bnapi->events |= event;
3150 	return rx_pkts;
3151 }
3152 
3153 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3154 				  int budget)
3155 {
3156 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3157 		bnapi->tx_int(bp, bnapi, budget);
3158 
3159 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3160 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3161 
3162 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3163 		bnapi->events &= ~BNXT_RX_EVENT;
3164 	}
3165 	if (bnapi->events & BNXT_AGG_EVENT) {
3166 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3167 
3168 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3169 		bnapi->events &= ~BNXT_AGG_EVENT;
3170 	}
3171 }
3172 
3173 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3174 			  int budget)
3175 {
3176 	struct bnxt_napi *bnapi = cpr->bnapi;
3177 	int rx_pkts;
3178 
3179 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3180 
3181 	/* ACK completion ring before freeing tx ring and producing new
3182 	 * buffers in rx/agg rings to prevent overflowing the completion
3183 	 * ring.
3184 	 */
3185 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3186 
3187 	__bnxt_poll_work_done(bp, bnapi, budget);
3188 	return rx_pkts;
3189 }
3190 
3191 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3192 {
3193 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3194 	struct bnxt *bp = bnapi->bp;
3195 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3196 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3197 	struct tx_cmp *txcmp;
3198 	struct rx_cmp_ext *rxcmp1;
3199 	u32 cp_cons, tmp_raw_cons;
3200 	u32 raw_cons = cpr->cp_raw_cons;
3201 	bool flush_xdp = false;
3202 	u32 rx_pkts = 0;
3203 	u8 event = 0;
3204 
3205 	while (1) {
3206 		int rc;
3207 
3208 		cp_cons = RING_CMP(raw_cons);
3209 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3210 
3211 		if (!TX_CMP_VALID(txcmp, raw_cons))
3212 			break;
3213 
3214 		/* The valid test of the entry must be done first before
3215 		 * reading any further.
3216 		 */
3217 		dma_rmb();
3218 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3219 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3220 			cp_cons = RING_CMP(tmp_raw_cons);
3221 			rxcmp1 = (struct rx_cmp_ext *)
3222 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3223 
3224 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3225 				break;
3226 
3227 			/* force an error to recycle the buffer */
3228 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3229 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3230 
3231 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3232 			if (likely(rc == -EIO) && budget)
3233 				rx_pkts++;
3234 			else if (rc == -EBUSY)	/* partial completion */
3235 				break;
3236 			if (event & BNXT_REDIRECT_EVENT)
3237 				flush_xdp = true;
3238 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3239 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3240 			bnxt_hwrm_handler(bp, txcmp);
3241 		} else {
3242 			netdev_err(bp->dev,
3243 				   "Invalid completion received on special ring\n");
3244 		}
3245 		raw_cons = NEXT_RAW_CMP(raw_cons);
3246 
3247 		if (rx_pkts == budget)
3248 			break;
3249 	}
3250 
3251 	cpr->cp_raw_cons = raw_cons;
3252 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3253 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3254 
3255 	if (event & BNXT_AGG_EVENT)
3256 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3257 	if (flush_xdp)
3258 		xdp_do_flush();
3259 
3260 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3261 		napi_complete_done(napi, rx_pkts);
3262 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3263 	}
3264 	return rx_pkts;
3265 }
3266 
3267 static int bnxt_poll(struct napi_struct *napi, int budget)
3268 {
3269 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3270 	struct bnxt *bp = bnapi->bp;
3271 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3272 	int work_done = 0;
3273 
3274 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3275 		napi_complete(napi);
3276 		return 0;
3277 	}
3278 	while (1) {
3279 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3280 
3281 		if (work_done >= budget) {
3282 			if (!budget)
3283 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3284 			break;
3285 		}
3286 
3287 		if (!bnxt_has_work(bp, cpr)) {
3288 			if (napi_complete_done(napi, work_done))
3289 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3290 			break;
3291 		}
3292 	}
3293 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3294 		struct dim_sample dim_sample = {};
3295 
3296 		dim_update_sample(cpr->event_ctr,
3297 				  cpr->rx_packets,
3298 				  cpr->rx_bytes,
3299 				  &dim_sample);
3300 		net_dim(&cpr->dim, &dim_sample);
3301 	}
3302 	return work_done;
3303 }
3304 
3305 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3306 {
3307 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3308 	int i, work_done = 0;
3309 
3310 	for (i = 0; i < cpr->cp_ring_count; i++) {
3311 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3312 
3313 		if (cpr2->had_nqe_notify) {
3314 			work_done += __bnxt_poll_work(bp, cpr2,
3315 						      budget - work_done);
3316 			cpr->has_more_work |= cpr2->has_more_work;
3317 		}
3318 	}
3319 	return work_done;
3320 }
3321 
3322 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3323 				 u64 dbr_type, int budget)
3324 {
3325 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3326 	int i;
3327 
3328 	for (i = 0; i < cpr->cp_ring_count; i++) {
3329 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3330 		struct bnxt_db_info *db;
3331 
3332 		if (cpr2->had_work_done) {
3333 			u32 tgl = 0;
3334 
3335 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3336 				cpr2->had_nqe_notify = 0;
3337 				tgl = cpr2->toggle;
3338 			}
3339 			db = &cpr2->cp_db;
3340 			bnxt_writeq(bp,
3341 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3342 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3343 				    db->doorbell);
3344 			cpr2->had_work_done = 0;
3345 		}
3346 	}
3347 	__bnxt_poll_work_done(bp, bnapi, budget);
3348 }
3349 
3350 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3351 {
3352 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3353 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3354 	struct bnxt_cp_ring_info *cpr_rx;
3355 	u32 raw_cons = cpr->cp_raw_cons;
3356 	struct bnxt *bp = bnapi->bp;
3357 	struct nqe_cn *nqcmp;
3358 	int work_done = 0;
3359 	u32 cons;
3360 
3361 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3362 		napi_complete(napi);
3363 		return 0;
3364 	}
3365 	if (cpr->has_more_work) {
3366 		cpr->has_more_work = 0;
3367 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3368 	}
3369 	while (1) {
3370 		u16 type;
3371 
3372 		cons = RING_CMP(raw_cons);
3373 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3374 
3375 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3376 			if (cpr->has_more_work)
3377 				break;
3378 
3379 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3380 					     budget);
3381 			cpr->cp_raw_cons = raw_cons;
3382 			if (napi_complete_done(napi, work_done))
3383 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3384 						  cpr->cp_raw_cons);
3385 			goto poll_done;
3386 		}
3387 
3388 		/* The valid test of the entry must be done first before
3389 		 * reading any further.
3390 		 */
3391 		dma_rmb();
3392 
3393 		type = le16_to_cpu(nqcmp->type);
3394 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3395 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3396 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3397 			struct bnxt_cp_ring_info *cpr2;
3398 
3399 			/* No more budget for RX work */
3400 			if (budget && work_done >= budget &&
3401 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3402 				break;
3403 
3404 			idx = BNXT_NQ_HDL_IDX(idx);
3405 			cpr2 = &cpr->cp_ring_arr[idx];
3406 			cpr2->had_nqe_notify = 1;
3407 			cpr2->toggle = NQE_CN_TOGGLE(type);
3408 			work_done += __bnxt_poll_work(bp, cpr2,
3409 						      budget - work_done);
3410 			cpr->has_more_work |= cpr2->has_more_work;
3411 		} else {
3412 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3413 		}
3414 		raw_cons = NEXT_RAW_CMP(raw_cons);
3415 	}
3416 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3417 	if (raw_cons != cpr->cp_raw_cons) {
3418 		cpr->cp_raw_cons = raw_cons;
3419 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3420 	}
3421 poll_done:
3422 	cpr_rx = &cpr->cp_ring_arr[0];
3423 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3424 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3425 		struct dim_sample dim_sample = {};
3426 
3427 		dim_update_sample(cpr->event_ctr,
3428 				  cpr_rx->rx_packets,
3429 				  cpr_rx->rx_bytes,
3430 				  &dim_sample);
3431 		net_dim(&cpr->dim, &dim_sample);
3432 	}
3433 	return work_done;
3434 }
3435 
3436 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3437 				       struct bnxt_tx_ring_info *txr, int idx)
3438 {
3439 	int i, max_idx;
3440 	struct pci_dev *pdev = bp->pdev;
3441 	unsigned int dma_len;
3442 	dma_addr_t dma_addr;
3443 
3444 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3445 
3446 	for (i = 0; i < max_idx;) {
3447 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3448 		struct bnxt_sw_tx_bd *head_buf = tx_buf;
3449 		struct sk_buff *skb;
3450 		int j, last;
3451 
3452 		if (idx  < bp->tx_nr_rings_xdp &&
3453 		    tx_buf->action == XDP_REDIRECT) {
3454 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3455 			dma_len = dma_unmap_len(tx_buf, len);
3456 
3457 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3458 					 DMA_TO_DEVICE);
3459 			xdp_return_frame(tx_buf->xdpf);
3460 			tx_buf->action = 0;
3461 			tx_buf->xdpf = NULL;
3462 			i++;
3463 			continue;
3464 		}
3465 
3466 		skb = tx_buf->skb;
3467 		if (!skb) {
3468 			i++;
3469 			continue;
3470 		}
3471 
3472 		tx_buf->skb = NULL;
3473 
3474 		if (tx_buf->is_push) {
3475 			dev_kfree_skb(skb);
3476 			i += 2;
3477 			continue;
3478 		}
3479 
3480 		if (dma_unmap_len(tx_buf, len)) {
3481 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3482 			dma_len = dma_unmap_len(tx_buf, len);
3483 
3484 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3485 					 DMA_TO_DEVICE);
3486 		}
3487 
3488 		last = tx_buf->nr_frags;
3489 		i += 2;
3490 		for (j = 0; j < last; j++, i++) {
3491 			int ring_idx = i & bp->tx_ring_mask;
3492 
3493 			tx_buf = &txr->tx_buf_ring[ring_idx];
3494 			if (dma_unmap_len(tx_buf, len)) {
3495 				dma_addr = dma_unmap_addr(tx_buf, mapping);
3496 				dma_len = dma_unmap_len(tx_buf, len);
3497 
3498 				netmem_dma_unmap_page_attrs(&pdev->dev,
3499 							    dma_addr, dma_len,
3500 							    DMA_TO_DEVICE, 0);
3501 			}
3502 		}
3503 		if (head_buf->is_sw_gso) {
3504 			u16 inline_cons = txr->tx_inline_cons + 1;
3505 
3506 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
3507 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
3508 				tso_dma_map_complete(&pdev->dev,
3509 						     &head_buf->sw_gso_cstate);
3510 			} else {
3511 				skb = NULL;
3512 			}
3513 			head_buf->is_sw_gso = 0;
3514 		}
3515 		if (skb)
3516 			dev_kfree_skb(skb);
3517 	}
3518 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3519 }
3520 
3521 static void bnxt_free_tx_skbs(struct bnxt *bp)
3522 {
3523 	int i;
3524 
3525 	if (!bp->tx_ring)
3526 		return;
3527 
3528 	for (i = 0; i < bp->tx_nr_rings; i++) {
3529 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3530 
3531 		if (!txr->tx_buf_ring)
3532 			continue;
3533 
3534 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3535 	}
3536 
3537 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3538 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3539 }
3540 
3541 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3542 {
3543 	int i, max_idx;
3544 
3545 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3546 
3547 	for (i = 0; i < max_idx; i++) {
3548 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3549 		void *data = rx_buf->data;
3550 
3551 		if (!data)
3552 			continue;
3553 
3554 		rx_buf->data = NULL;
3555 		if (BNXT_RX_PAGE_MODE(bp))
3556 			page_pool_recycle_direct(rxr->page_pool, data);
3557 		else
3558 			page_pool_free_va(rxr->head_pool, data, true);
3559 	}
3560 }
3561 
3562 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3563 {
3564 	int i, max_idx;
3565 
3566 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3567 
3568 	for (i = 0; i < max_idx; i++) {
3569 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3570 		netmem_ref netmem = rx_agg_buf->netmem;
3571 
3572 		if (!netmem)
3573 			continue;
3574 
3575 		rx_agg_buf->netmem = 0;
3576 		__clear_bit(i, rxr->rx_agg_bmap);
3577 
3578 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3579 	}
3580 }
3581 
3582 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3583 					struct bnxt_rx_ring_info *rxr)
3584 {
3585 	int i;
3586 
3587 	for (i = 0; i < bp->max_tpa; i++) {
3588 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3589 		u8 *data = tpa_info->data;
3590 
3591 		if (!data)
3592 			continue;
3593 
3594 		tpa_info->data = NULL;
3595 		page_pool_free_va(rxr->head_pool, data, false);
3596 	}
3597 }
3598 
3599 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3600 				       struct bnxt_rx_ring_info *rxr)
3601 {
3602 	struct bnxt_tpa_idx_map *map;
3603 
3604 	if (!rxr->rx_tpa)
3605 		goto skip_rx_tpa_free;
3606 
3607 	bnxt_free_one_tpa_info_data(bp, rxr);
3608 
3609 skip_rx_tpa_free:
3610 	if (!rxr->rx_buf_ring)
3611 		goto skip_rx_buf_free;
3612 
3613 	bnxt_free_one_rx_ring(bp, rxr);
3614 
3615 skip_rx_buf_free:
3616 	if (!rxr->rx_agg_ring)
3617 		goto skip_rx_agg_free;
3618 
3619 	bnxt_free_one_rx_agg_ring(bp, rxr);
3620 
3621 skip_rx_agg_free:
3622 	map = rxr->rx_tpa_idx_map;
3623 	if (map)
3624 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3625 }
3626 
3627 static void bnxt_free_rx_skbs(struct bnxt *bp)
3628 {
3629 	int i;
3630 
3631 	if (!bp->rx_ring)
3632 		return;
3633 
3634 	for (i = 0; i < bp->rx_nr_rings; i++)
3635 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3636 }
3637 
3638 static void bnxt_free_skbs(struct bnxt *bp)
3639 {
3640 	bnxt_free_tx_skbs(bp);
3641 	bnxt_free_rx_skbs(bp);
3642 }
3643 
3644 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3645 {
3646 	u8 init_val = ctxm->init_value;
3647 	u16 offset = ctxm->init_offset;
3648 	u8 *p2 = p;
3649 	int i;
3650 
3651 	if (!init_val)
3652 		return;
3653 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3654 		memset(p, init_val, len);
3655 		return;
3656 	}
3657 	for (i = 0; i < len; i += ctxm->entry_size)
3658 		*(p2 + i + offset) = init_val;
3659 }
3660 
3661 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3662 			       void *buf, size_t offset, size_t head,
3663 			       size_t tail)
3664 {
3665 	int i, head_page, start_idx, source_offset;
3666 	size_t len, rem_len, total_len, max_bytes;
3667 
3668 	head_page = head / rmem->page_size;
3669 	source_offset = head % rmem->page_size;
3670 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3671 	if (!total_len)
3672 		total_len = MAX_CTX_BYTES;
3673 	start_idx = head_page % MAX_CTX_PAGES;
3674 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3675 		    source_offset;
3676 	total_len = min(total_len, max_bytes);
3677 	rem_len = total_len;
3678 
3679 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3680 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3681 		if (buf)
3682 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3683 			       len);
3684 		offset += len;
3685 		rem_len -= len;
3686 	}
3687 	return total_len;
3688 }
3689 
3690 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3691 {
3692 	struct pci_dev *pdev = bp->pdev;
3693 	int i;
3694 
3695 	if (!rmem->pg_arr)
3696 		goto skip_pages;
3697 
3698 	for (i = 0; i < rmem->nr_pages; i++) {
3699 		if (!rmem->pg_arr[i])
3700 			continue;
3701 
3702 		dma_free_coherent(&pdev->dev, rmem->page_size,
3703 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3704 
3705 		rmem->pg_arr[i] = NULL;
3706 	}
3707 skip_pages:
3708 	if (rmem->pg_tbl) {
3709 		size_t pg_tbl_size = rmem->nr_pages * 8;
3710 
3711 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3712 			pg_tbl_size = rmem->page_size;
3713 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3714 				  rmem->pg_tbl, rmem->pg_tbl_map);
3715 		rmem->pg_tbl = NULL;
3716 	}
3717 	if (rmem->vmem_size && *rmem->vmem) {
3718 		vfree(*rmem->vmem);
3719 		*rmem->vmem = NULL;
3720 	}
3721 }
3722 
3723 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3724 {
3725 	struct pci_dev *pdev = bp->pdev;
3726 	u64 valid_bit = 0;
3727 	int i;
3728 
3729 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3730 		valid_bit = PTU_PTE_VALID;
3731 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3732 		size_t pg_tbl_size = rmem->nr_pages * 8;
3733 
3734 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3735 			pg_tbl_size = rmem->page_size;
3736 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3737 						  &rmem->pg_tbl_map,
3738 						  GFP_KERNEL);
3739 		if (!rmem->pg_tbl)
3740 			return -ENOMEM;
3741 	}
3742 
3743 	for (i = 0; i < rmem->nr_pages; i++) {
3744 		u64 extra_bits = valid_bit;
3745 
3746 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3747 						     rmem->page_size,
3748 						     &rmem->dma_arr[i],
3749 						     GFP_KERNEL);
3750 		if (!rmem->pg_arr[i])
3751 			return -ENOMEM;
3752 
3753 		if (rmem->ctx_mem)
3754 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3755 					  rmem->page_size);
3756 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3757 			if (i == rmem->nr_pages - 2 &&
3758 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3759 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3760 			else if (i == rmem->nr_pages - 1 &&
3761 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3762 				extra_bits |= PTU_PTE_LAST;
3763 			rmem->pg_tbl[i] =
3764 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3765 		}
3766 	}
3767 
3768 	if (rmem->vmem_size) {
3769 		*rmem->vmem = vzalloc(rmem->vmem_size);
3770 		if (!(*rmem->vmem))
3771 			return -ENOMEM;
3772 	}
3773 	return 0;
3774 }
3775 
3776 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3777 				   struct bnxt_rx_ring_info *rxr)
3778 {
3779 	int i;
3780 
3781 	kfree(rxr->rx_tpa_idx_map);
3782 	rxr->rx_tpa_idx_map = NULL;
3783 	if (rxr->rx_tpa) {
3784 		for (i = 0; i < bp->max_tpa; i++) {
3785 			kfree(rxr->rx_tpa[i].agg_arr);
3786 			rxr->rx_tpa[i].agg_arr = NULL;
3787 		}
3788 	}
3789 	kfree(rxr->rx_tpa);
3790 	rxr->rx_tpa = NULL;
3791 }
3792 
3793 static void bnxt_free_tpa_info(struct bnxt *bp)
3794 {
3795 	int i;
3796 
3797 	for (i = 0; i < bp->rx_nr_rings; i++) {
3798 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3799 
3800 		bnxt_free_one_tpa_info(bp, rxr);
3801 	}
3802 }
3803 
3804 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3805 				   struct bnxt_rx_ring_info *rxr)
3806 {
3807 	struct rx_agg_cmp *agg;
3808 	int i;
3809 
3810 	rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info, bp->max_tpa);
3811 	if (!rxr->rx_tpa)
3812 		return -ENOMEM;
3813 
3814 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3815 		return 0;
3816 	for (i = 0; i < bp->max_tpa; i++) {
3817 		agg = kzalloc_objs(*agg, MAX_SKB_FRAGS);
3818 		if (!agg)
3819 			return -ENOMEM;
3820 		rxr->rx_tpa[i].agg_arr = agg;
3821 	}
3822 	rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map);
3823 	if (!rxr->rx_tpa_idx_map)
3824 		return -ENOMEM;
3825 
3826 	return 0;
3827 }
3828 
3829 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3830 {
3831 	int i, rc;
3832 
3833 	bp->max_tpa = MAX_TPA;
3834 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3835 		if (!bp->max_tpa_v2)
3836 			return 0;
3837 		bp->max_tpa = min_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3838 		/* Older P5 FW sets max_tpa_v2 low by mistake except NPAR */
3839 		if (bp->max_tpa <= 32 && BNXT_CHIP_P5(bp) && !BNXT_NPAR(bp))
3840 			bp->max_tpa = MAX_TPA_P5;
3841 	}
3842 
3843 	for (i = 0; i < bp->rx_nr_rings; i++) {
3844 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3845 
3846 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3847 		if (rc)
3848 			return rc;
3849 	}
3850 	return 0;
3851 }
3852 
3853 static void bnxt_free_rx_rings(struct bnxt *bp)
3854 {
3855 	int i;
3856 
3857 	if (!bp->rx_ring)
3858 		return;
3859 
3860 	bnxt_free_tpa_info(bp);
3861 	for (i = 0; i < bp->rx_nr_rings; i++) {
3862 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3863 		struct bnxt_ring_struct *ring;
3864 
3865 		if (rxr->xdp_prog)
3866 			bpf_prog_put(rxr->xdp_prog);
3867 
3868 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3869 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3870 
3871 		page_pool_destroy(rxr->page_pool);
3872 		page_pool_destroy(rxr->head_pool);
3873 		rxr->page_pool = rxr->head_pool = NULL;
3874 
3875 		kfree(rxr->rx_agg_bmap);
3876 		rxr->rx_agg_bmap = NULL;
3877 
3878 		ring = &rxr->rx_ring_struct;
3879 		bnxt_free_ring(bp, &ring->ring_mem);
3880 
3881 		ring = &rxr->rx_agg_ring_struct;
3882 		bnxt_free_ring(bp, &ring->ring_mem);
3883 	}
3884 }
3885 
3886 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3887 				       struct bnxt_rx_ring_info *rxr)
3888 {
3889 	/* User may have chosen larger than default rx_page_size,
3890 	 * we keep the ring sizes uniform and also want uniform amount
3891 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3892 	 */
3893 	int fill_level = bp->rx_agg_ring_size;
3894 
3895 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3896 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3897 
3898 	return fill_level;
3899 }
3900 
3901 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3902 				   struct bnxt_rx_ring_info *rxr,
3903 				   int numa_node)
3904 {
3905 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3906 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3907 	struct page_pool_params pp = { 0 };
3908 	struct page_pool *pool;
3909 
3910 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3911 	if (BNXT_RX_PAGE_MODE(bp))
3912 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3913 
3914 	pp.order = get_order(rxr->rx_page_size);
3915 	pp.nid = numa_node;
3916 	pp.netdev = bp->dev;
3917 	pp.dev = &bp->pdev->dev;
3918 	pp.dma_dir = bp->rx_dir;
3919 	pp.max_len = PAGE_SIZE << pp.order;
3920 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3921 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3922 	pp.queue_idx = rxr->bnapi->index;
3923 
3924 	pool = page_pool_create(&pp);
3925 	if (IS_ERR(pool))
3926 		return PTR_ERR(pool);
3927 	rxr->page_pool = pool;
3928 
3929 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3930 	rxr->need_head_pool |= !!pp.order;
3931 	if (bnxt_separate_head_pool(rxr)) {
3932 		pp.order = 0;
3933 		pp.max_len = PAGE_SIZE;
3934 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3935 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3936 		pool = page_pool_create(&pp);
3937 		if (IS_ERR(pool))
3938 			goto err_destroy_pp;
3939 	} else {
3940 		page_pool_get(pool);
3941 	}
3942 	rxr->head_pool = pool;
3943 
3944 	return 0;
3945 
3946 err_destroy_pp:
3947 	page_pool_destroy(rxr->page_pool);
3948 	rxr->page_pool = NULL;
3949 	return PTR_ERR(pool);
3950 }
3951 
3952 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3953 {
3954 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3955 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3956 }
3957 
3958 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3959 {
3960 	u16 mem_size;
3961 
3962 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3963 	mem_size = rxr->rx_agg_bmap_size / 8;
3964 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3965 	if (!rxr->rx_agg_bmap)
3966 		return -ENOMEM;
3967 
3968 	return 0;
3969 }
3970 
3971 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3972 {
3973 	int numa_node = dev_to_node(&bp->pdev->dev);
3974 	int i, rc = 0, agg_rings = 0, cpu;
3975 
3976 	if (!bp->rx_ring)
3977 		return -ENOMEM;
3978 
3979 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3980 		agg_rings = 1;
3981 
3982 	for (i = 0; i < bp->rx_nr_rings; i++) {
3983 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3984 		struct bnxt_ring_struct *ring;
3985 		int cpu_node;
3986 
3987 		ring = &rxr->rx_ring_struct;
3988 
3989 		cpu = cpumask_local_spread(i, numa_node);
3990 		cpu_node = cpu_to_node(cpu);
3991 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3992 			   i, cpu_node);
3993 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3994 		if (rc)
3995 			return rc;
3996 		bnxt_enable_rx_page_pool(rxr);
3997 
3998 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3999 		if (rc < 0)
4000 			return rc;
4001 
4002 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
4003 						MEM_TYPE_PAGE_POOL,
4004 						rxr->page_pool);
4005 		if (rc) {
4006 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
4007 			return rc;
4008 		}
4009 
4010 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4011 		if (rc)
4012 			return rc;
4013 
4014 		ring->grp_idx = i;
4015 		if (agg_rings) {
4016 			ring = &rxr->rx_agg_ring_struct;
4017 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4018 			if (rc)
4019 				return rc;
4020 
4021 			ring->grp_idx = i;
4022 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
4023 			if (rc)
4024 				return rc;
4025 		}
4026 	}
4027 	if (bp->flags & BNXT_FLAG_TPA)
4028 		rc = bnxt_alloc_tpa_info(bp);
4029 	return rc;
4030 }
4031 
4032 static void bnxt_free_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4033 				    struct pci_dev *pdev)
4034 {
4035 	if (!txr->tx_inline_buf)
4036 		return;
4037 
4038 	dma_unmap_single(&pdev->dev, txr->tx_inline_dma,
4039 			 txr->tx_inline_size, DMA_TO_DEVICE);
4040 	kfree(txr->tx_inline_buf);
4041 	txr->tx_inline_buf = NULL;
4042 	txr->tx_inline_size = 0;
4043 }
4044 
4045 static int bnxt_alloc_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4046 				    struct pci_dev *pdev,
4047 				    unsigned int size)
4048 {
4049 	txr->tx_inline_buf = kmalloc(size, GFP_KERNEL);
4050 	if (!txr->tx_inline_buf)
4051 		return -ENOMEM;
4052 
4053 	txr->tx_inline_dma = dma_map_single(&pdev->dev, txr->tx_inline_buf,
4054 					    size, DMA_TO_DEVICE);
4055 	if (dma_mapping_error(&pdev->dev, txr->tx_inline_dma)) {
4056 		kfree(txr->tx_inline_buf);
4057 		txr->tx_inline_buf = NULL;
4058 		return -ENOMEM;
4059 	}
4060 	txr->tx_inline_size = size;
4061 
4062 	return 0;
4063 }
4064 
4065 static void bnxt_free_tx_rings(struct bnxt *bp)
4066 {
4067 	int i;
4068 	struct pci_dev *pdev = bp->pdev;
4069 
4070 	if (!bp->tx_ring)
4071 		return;
4072 
4073 	for (i = 0; i < bp->tx_nr_rings; i++) {
4074 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4075 		struct bnxt_ring_struct *ring;
4076 
4077 		if (txr->tx_push) {
4078 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
4079 					  txr->tx_push, txr->tx_push_mapping);
4080 			txr->tx_push = NULL;
4081 		}
4082 
4083 		bnxt_free_tx_inline_buf(txr, pdev);
4084 
4085 		ring = &txr->tx_ring_struct;
4086 
4087 		bnxt_free_ring(bp, &ring->ring_mem);
4088 	}
4089 }
4090 
4091 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
4092 	((tc) * (bp)->tx_nr_rings_per_tc)
4093 
4094 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
4095 	((tx) % (bp)->tx_nr_rings_per_tc)
4096 
4097 #define BNXT_RING_TO_TC(bp, tx)		\
4098 	((tx) / (bp)->tx_nr_rings_per_tc)
4099 
4100 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4101 {
4102 	int i, j, rc;
4103 	struct pci_dev *pdev = bp->pdev;
4104 
4105 	bp->tx_push_size = 0;
4106 	if (bp->tx_push_thresh) {
4107 		int push_size;
4108 
4109 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4110 					bp->tx_push_thresh);
4111 
4112 		if (push_size > 256) {
4113 			push_size = 0;
4114 			bp->tx_push_thresh = 0;
4115 		}
4116 
4117 		bp->tx_push_size = push_size;
4118 	}
4119 
4120 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4121 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4122 		struct bnxt_ring_struct *ring;
4123 		u8 qidx;
4124 
4125 		ring = &txr->tx_ring_struct;
4126 
4127 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4128 		if (rc)
4129 			return rc;
4130 
4131 		ring->grp_idx = txr->bnapi->index;
4132 		if (bp->tx_push_size) {
4133 			dma_addr_t mapping;
4134 
4135 			/* One pre-allocated DMA buffer to backup
4136 			 * TX push operation
4137 			 */
4138 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4139 						bp->tx_push_size,
4140 						&txr->tx_push_mapping,
4141 						GFP_KERNEL);
4142 
4143 			if (!txr->tx_push)
4144 				return -ENOMEM;
4145 
4146 			mapping = txr->tx_push_mapping +
4147 				sizeof(struct tx_push_bd);
4148 			txr->data_mapping = cpu_to_le64(mapping);
4149 		}
4150 		if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
4151 			rc = bnxt_alloc_tx_inline_buf(txr, pdev,
4152 						      BNXT_SW_USO_MAX_SEGS *
4153 						      TSO_HEADER_SIZE);
4154 			if (rc)
4155 				return rc;
4156 		}
4157 		qidx = bp->tc_to_qidx[j];
4158 		ring->queue_id = bp->q_info[qidx].queue_id;
4159 		spin_lock_init(&txr->xdp_tx_lock);
4160 		if (i < bp->tx_nr_rings_xdp)
4161 			continue;
4162 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4163 			j++;
4164 	}
4165 	return 0;
4166 }
4167 
4168 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4169 {
4170 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4171 
4172 	kfree(cpr->cp_desc_ring);
4173 	cpr->cp_desc_ring = NULL;
4174 	ring->ring_mem.pg_arr = NULL;
4175 	kfree(cpr->cp_desc_mapping);
4176 	cpr->cp_desc_mapping = NULL;
4177 	ring->ring_mem.dma_arr = NULL;
4178 }
4179 
4180 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4181 {
4182 	cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n);
4183 	if (!cpr->cp_desc_ring)
4184 		return -ENOMEM;
4185 	cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n);
4186 	if (!cpr->cp_desc_mapping)
4187 		return -ENOMEM;
4188 	return 0;
4189 }
4190 
4191 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4192 {
4193 	int i;
4194 
4195 	if (!bp->bnapi)
4196 		return;
4197 	for (i = 0; i < bp->cp_nr_rings; i++) {
4198 		struct bnxt_napi *bnapi = bp->bnapi[i];
4199 
4200 		if (!bnapi)
4201 			continue;
4202 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4203 	}
4204 }
4205 
4206 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4207 {
4208 	int i, n = bp->cp_nr_pages;
4209 
4210 	for (i = 0; i < bp->cp_nr_rings; i++) {
4211 		struct bnxt_napi *bnapi = bp->bnapi[i];
4212 		int rc;
4213 
4214 		if (!bnapi)
4215 			continue;
4216 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4217 		if (rc)
4218 			return rc;
4219 	}
4220 	return 0;
4221 }
4222 
4223 static void bnxt_free_cp_rings(struct bnxt *bp)
4224 {
4225 	int i;
4226 
4227 	if (!bp->bnapi)
4228 		return;
4229 
4230 	for (i = 0; i < bp->cp_nr_rings; i++) {
4231 		struct bnxt_napi *bnapi = bp->bnapi[i];
4232 		struct bnxt_cp_ring_info *cpr;
4233 		struct bnxt_ring_struct *ring;
4234 		int j;
4235 
4236 		if (!bnapi)
4237 			continue;
4238 
4239 		cpr = &bnapi->cp_ring;
4240 		ring = &cpr->cp_ring_struct;
4241 
4242 		bnxt_free_ring(bp, &ring->ring_mem);
4243 
4244 		if (!cpr->cp_ring_arr)
4245 			continue;
4246 
4247 		for (j = 0; j < cpr->cp_ring_count; j++) {
4248 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4249 
4250 			ring = &cpr2->cp_ring_struct;
4251 			bnxt_free_ring(bp, &ring->ring_mem);
4252 			bnxt_free_cp_arrays(cpr2);
4253 		}
4254 		kfree(cpr->cp_ring_arr);
4255 		cpr->cp_ring_arr = NULL;
4256 		cpr->cp_ring_count = 0;
4257 	}
4258 }
4259 
4260 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4261 				  struct bnxt_cp_ring_info *cpr)
4262 {
4263 	struct bnxt_ring_mem_info *rmem;
4264 	struct bnxt_ring_struct *ring;
4265 	int rc;
4266 
4267 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4268 	if (rc) {
4269 		bnxt_free_cp_arrays(cpr);
4270 		return -ENOMEM;
4271 	}
4272 	ring = &cpr->cp_ring_struct;
4273 	rmem = &ring->ring_mem;
4274 	rmem->nr_pages = bp->cp_nr_pages;
4275 	rmem->page_size = HW_CMPD_RING_SIZE;
4276 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4277 	rmem->dma_arr = cpr->cp_desc_mapping;
4278 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4279 	rc = bnxt_alloc_ring(bp, rmem);
4280 	if (rc) {
4281 		bnxt_free_ring(bp, rmem);
4282 		bnxt_free_cp_arrays(cpr);
4283 	}
4284 	return rc;
4285 }
4286 
4287 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4288 {
4289 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4290 	int i, j, rc, ulp_msix;
4291 	int tcs = bp->num_tc;
4292 
4293 	if (!tcs)
4294 		tcs = 1;
4295 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4296 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4297 		struct bnxt_napi *bnapi = bp->bnapi[i];
4298 		struct bnxt_cp_ring_info *cpr, *cpr2;
4299 		struct bnxt_ring_struct *ring;
4300 		int cp_count = 0, k;
4301 		int rx = 0, tx = 0;
4302 
4303 		if (!bnapi)
4304 			continue;
4305 
4306 		cpr = &bnapi->cp_ring;
4307 		cpr->bnapi = bnapi;
4308 		ring = &cpr->cp_ring_struct;
4309 
4310 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4311 		if (rc)
4312 			return rc;
4313 
4314 		ring->map_idx = ulp_msix + i;
4315 
4316 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4317 			continue;
4318 
4319 		if (i < bp->rx_nr_rings) {
4320 			cp_count++;
4321 			rx = 1;
4322 		}
4323 		if (i < bp->tx_nr_rings_xdp) {
4324 			cp_count++;
4325 			tx = 1;
4326 		} else if ((sh && i < bp->tx_nr_rings) ||
4327 			 (!sh && i >= bp->rx_nr_rings)) {
4328 			cp_count += tcs;
4329 			tx = 1;
4330 		}
4331 
4332 		cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count);
4333 		if (!cpr->cp_ring_arr)
4334 			return -ENOMEM;
4335 		cpr->cp_ring_count = cp_count;
4336 
4337 		for (k = 0; k < cp_count; k++) {
4338 			cpr2 = &cpr->cp_ring_arr[k];
4339 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4340 			if (rc)
4341 				return rc;
4342 			cpr2->bnapi = bnapi;
4343 			cpr2->sw_stats = cpr->sw_stats;
4344 			cpr2->cp_idx = k;
4345 			if (!k && rx) {
4346 				bp->rx_ring[i].rx_cpr = cpr2;
4347 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4348 			} else {
4349 				int n, tc = k - rx;
4350 
4351 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4352 				bp->tx_ring[n].tx_cpr = cpr2;
4353 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4354 			}
4355 		}
4356 		if (tx)
4357 			j++;
4358 	}
4359 	return 0;
4360 }
4361 
4362 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4363 				     struct bnxt_rx_ring_info *rxr)
4364 {
4365 	struct bnxt_ring_mem_info *rmem;
4366 	struct bnxt_ring_struct *ring;
4367 
4368 	ring = &rxr->rx_ring_struct;
4369 	rmem = &ring->ring_mem;
4370 	rmem->nr_pages = bp->rx_nr_pages;
4371 	rmem->page_size = HW_RXBD_RING_SIZE;
4372 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4373 	rmem->dma_arr = rxr->rx_desc_mapping;
4374 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4375 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4376 
4377 	ring = &rxr->rx_agg_ring_struct;
4378 	rmem = &ring->ring_mem;
4379 	rmem->nr_pages = bp->rx_agg_nr_pages;
4380 	rmem->page_size = HW_RXBD_RING_SIZE;
4381 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4382 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4383 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4384 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4385 }
4386 
4387 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4388 				      struct bnxt_rx_ring_info *rxr)
4389 {
4390 	struct bnxt_ring_mem_info *rmem;
4391 	struct bnxt_ring_struct *ring;
4392 	int i;
4393 
4394 	rxr->page_pool->p.napi = NULL;
4395 	rxr->page_pool = NULL;
4396 	rxr->head_pool->p.napi = NULL;
4397 	rxr->head_pool = NULL;
4398 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4399 
4400 	ring = &rxr->rx_ring_struct;
4401 	rmem = &ring->ring_mem;
4402 	rmem->pg_tbl = NULL;
4403 	rmem->pg_tbl_map = 0;
4404 	for (i = 0; i < rmem->nr_pages; i++) {
4405 		rmem->pg_arr[i] = NULL;
4406 		rmem->dma_arr[i] = 0;
4407 	}
4408 	*rmem->vmem = NULL;
4409 
4410 	ring = &rxr->rx_agg_ring_struct;
4411 	rmem = &ring->ring_mem;
4412 	rmem->pg_tbl = NULL;
4413 	rmem->pg_tbl_map = 0;
4414 	for (i = 0; i < rmem->nr_pages; i++) {
4415 		rmem->pg_arr[i] = NULL;
4416 		rmem->dma_arr[i] = 0;
4417 	}
4418 	*rmem->vmem = NULL;
4419 }
4420 
4421 static void bnxt_init_ring_struct(struct bnxt *bp)
4422 {
4423 	int i, j;
4424 
4425 	for (i = 0; i < bp->cp_nr_rings; i++) {
4426 		struct bnxt_napi *bnapi = bp->bnapi[i];
4427 		struct netdev_queue_config qcfg;
4428 		struct bnxt_ring_mem_info *rmem;
4429 		struct bnxt_cp_ring_info *cpr;
4430 		struct bnxt_rx_ring_info *rxr;
4431 		struct bnxt_tx_ring_info *txr;
4432 		struct bnxt_ring_struct *ring;
4433 
4434 		if (!bnapi)
4435 			continue;
4436 
4437 		cpr = &bnapi->cp_ring;
4438 		ring = &cpr->cp_ring_struct;
4439 		rmem = &ring->ring_mem;
4440 		rmem->nr_pages = bp->cp_nr_pages;
4441 		rmem->page_size = HW_CMPD_RING_SIZE;
4442 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4443 		rmem->dma_arr = cpr->cp_desc_mapping;
4444 		rmem->vmem_size = 0;
4445 
4446 		rxr = bnapi->rx_ring;
4447 		if (!rxr)
4448 			goto skip_rx;
4449 
4450 		netdev_queue_config(bp->dev, i, &qcfg);
4451 		rxr->rx_page_size = qcfg.rx_page_size;
4452 
4453 		ring = &rxr->rx_ring_struct;
4454 		rmem = &ring->ring_mem;
4455 		rmem->nr_pages = bp->rx_nr_pages;
4456 		rmem->page_size = HW_RXBD_RING_SIZE;
4457 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4458 		rmem->dma_arr = rxr->rx_desc_mapping;
4459 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4460 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4461 
4462 		ring = &rxr->rx_agg_ring_struct;
4463 		rmem = &ring->ring_mem;
4464 		rmem->nr_pages = bp->rx_agg_nr_pages;
4465 		rmem->page_size = HW_RXBD_RING_SIZE;
4466 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4467 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4468 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4469 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4470 
4471 skip_rx:
4472 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4473 			ring = &txr->tx_ring_struct;
4474 			rmem = &ring->ring_mem;
4475 			rmem->nr_pages = bp->tx_nr_pages;
4476 			rmem->page_size = HW_TXBD_RING_SIZE;
4477 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4478 			rmem->dma_arr = txr->tx_desc_mapping;
4479 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4480 			rmem->vmem = (void **)&txr->tx_buf_ring;
4481 		}
4482 	}
4483 }
4484 
4485 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4486 {
4487 	int i;
4488 	u32 prod;
4489 	struct rx_bd **rx_buf_ring;
4490 
4491 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4492 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4493 		int j;
4494 		struct rx_bd *rxbd;
4495 
4496 		rxbd = rx_buf_ring[i];
4497 		if (!rxbd)
4498 			continue;
4499 
4500 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4501 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4502 			rxbd->rx_bd_opaque = prod;
4503 		}
4504 	}
4505 }
4506 
4507 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4508 				       struct bnxt_rx_ring_info *rxr,
4509 				       int ring_nr)
4510 {
4511 	u32 prod;
4512 	int i;
4513 
4514 	prod = rxr->rx_prod;
4515 	for (i = 0; i < bp->rx_ring_size; i++) {
4516 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4517 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4518 				    ring_nr, i, bp->rx_ring_size);
4519 			break;
4520 		}
4521 		prod = NEXT_RX(prod);
4522 	}
4523 	rxr->rx_prod = prod;
4524 }
4525 
4526 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4527 					  struct bnxt_rx_ring_info *rxr,
4528 					  int ring_nr)
4529 {
4530 	int fill_level, i;
4531 	u32 prod;
4532 
4533 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4534 
4535 	prod = rxr->rx_agg_prod;
4536 	for (i = 0; i < fill_level; i++) {
4537 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4538 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4539 				    ring_nr, i, bp->rx_agg_ring_size);
4540 			break;
4541 		}
4542 		prod = NEXT_RX_AGG(prod);
4543 	}
4544 	rxr->rx_agg_prod = prod;
4545 }
4546 
4547 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4548 					struct bnxt_rx_ring_info *rxr)
4549 {
4550 	dma_addr_t mapping;
4551 	u8 *data;
4552 	int i;
4553 
4554 	for (i = 0; i < bp->max_tpa; i++) {
4555 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4556 					    GFP_KERNEL);
4557 		if (!data)
4558 			return -ENOMEM;
4559 
4560 		rxr->rx_tpa[i].data = data;
4561 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4562 		rxr->rx_tpa[i].mapping = mapping;
4563 	}
4564 
4565 	return 0;
4566 }
4567 
4568 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4569 {
4570 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4571 	int rc;
4572 
4573 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4574 
4575 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4576 		return 0;
4577 
4578 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4579 
4580 	if (rxr->rx_tpa) {
4581 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4582 		if (rc)
4583 			return rc;
4584 	}
4585 	return 0;
4586 }
4587 
4588 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4589 				       struct bnxt_rx_ring_info *rxr)
4590 {
4591 	struct bnxt_ring_struct *ring;
4592 	u32 type;
4593 
4594 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4595 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4596 
4597 	if (NET_IP_ALIGN == 2)
4598 		type |= RX_BD_FLAGS_SOP;
4599 
4600 	ring = &rxr->rx_ring_struct;
4601 	bnxt_init_rxbd_pages(ring, type);
4602 	ring->fw_ring_id = INVALID_HW_RING_ID;
4603 }
4604 
4605 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4606 					   struct bnxt_rx_ring_info *rxr)
4607 {
4608 	struct bnxt_ring_struct *ring;
4609 	u32 type;
4610 
4611 	ring = &rxr->rx_agg_ring_struct;
4612 	ring->fw_ring_id = INVALID_HW_RING_ID;
4613 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4614 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4615 			RX_BD_TYPE_RX_AGG_BD;
4616 
4617 		/* Disable EOP if TPA is enabled to prevent overlapping zero
4618 		 * padding with the next segment's data.  On P7_PLUS, EOP will
4619 		 * automatically disable Relaxed Ordering (RO) to prevent
4620 		 * potential data corruption (and may degrade performance).  On
4621 		 * older chips, RO will not be automatically disabled and may
4622 		 * cause corruption.
4623 		 */
4624 		if (!(bp->flags & BNXT_FLAG_TPA))
4625 			type |= RX_BD_FLAGS_AGG_EOP;
4626 
4627 		bnxt_init_rxbd_pages(ring, type);
4628 	}
4629 }
4630 
4631 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4632 {
4633 	struct bnxt_rx_ring_info *rxr;
4634 
4635 	rxr = &bp->rx_ring[ring_nr];
4636 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4637 
4638 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4639 			     &rxr->bnapi->napi);
4640 
4641 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4642 		bpf_prog_add(bp->xdp_prog, 1);
4643 		rxr->xdp_prog = bp->xdp_prog;
4644 	}
4645 
4646 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4647 
4648 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4649 }
4650 
4651 static void bnxt_init_cp_rings(struct bnxt *bp)
4652 {
4653 	int i, j;
4654 
4655 	for (i = 0; i < bp->cp_nr_rings; i++) {
4656 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4657 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4658 
4659 		ring->fw_ring_id = INVALID_HW_RING_ID;
4660 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4661 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4662 		if (!cpr->cp_ring_arr)
4663 			continue;
4664 		for (j = 0; j < cpr->cp_ring_count; j++) {
4665 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4666 
4667 			ring = &cpr2->cp_ring_struct;
4668 			ring->fw_ring_id = INVALID_HW_RING_ID;
4669 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4670 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4671 		}
4672 	}
4673 }
4674 
4675 static int bnxt_init_rx_rings(struct bnxt *bp)
4676 {
4677 	int i, rc = 0;
4678 
4679 	if (BNXT_RX_PAGE_MODE(bp)) {
4680 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4681 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4682 	} else {
4683 		bp->rx_offset = BNXT_RX_OFFSET;
4684 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4685 	}
4686 
4687 	for (i = 0; i < bp->rx_nr_rings; i++) {
4688 		rc = bnxt_init_one_rx_ring(bp, i);
4689 		if (rc)
4690 			break;
4691 	}
4692 
4693 	return rc;
4694 }
4695 
4696 static int bnxt_init_tx_rings(struct bnxt *bp)
4697 {
4698 	netdev_features_t features;
4699 	u16 i;
4700 
4701 	features = bp->dev->features;
4702 
4703 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4704 				   bnxt_min_tx_desc_cnt(bp, features));
4705 
4706 	for (i = 0; i < bp->tx_nr_rings; i++) {
4707 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4708 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4709 
4710 		ring->fw_ring_id = INVALID_HW_RING_ID;
4711 
4712 		if (i >= bp->tx_nr_rings_xdp)
4713 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4714 					     NETDEV_QUEUE_TYPE_TX,
4715 					     &txr->bnapi->napi);
4716 	}
4717 
4718 	return 0;
4719 }
4720 
4721 static void bnxt_free_ring_grps(struct bnxt *bp)
4722 {
4723 	kfree(bp->grp_info);
4724 	bp->grp_info = NULL;
4725 }
4726 
4727 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4728 {
4729 	int i;
4730 
4731 	if (irq_re_init) {
4732 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4733 					    bp->cp_nr_rings);
4734 		if (!bp->grp_info)
4735 			return -ENOMEM;
4736 	}
4737 	for (i = 0; i < bp->cp_nr_rings; i++) {
4738 		if (irq_re_init)
4739 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4740 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4741 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4742 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4743 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4744 	}
4745 	return 0;
4746 }
4747 
4748 static void bnxt_free_vnics(struct bnxt *bp)
4749 {
4750 	kfree(bp->vnic_info);
4751 	bp->vnic_info = NULL;
4752 	bp->nr_vnics = 0;
4753 }
4754 
4755 static int bnxt_alloc_vnics(struct bnxt *bp)
4756 {
4757 	int num_vnics = 1;
4758 
4759 #ifdef CONFIG_RFS_ACCEL
4760 	if (bp->flags & BNXT_FLAG_RFS) {
4761 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4762 			num_vnics++;
4763 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4764 			num_vnics += bp->rx_nr_rings;
4765 	}
4766 #endif
4767 
4768 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4769 		num_vnics++;
4770 
4771 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4772 	if (!bp->vnic_info)
4773 		return -ENOMEM;
4774 
4775 	bp->nr_vnics = num_vnics;
4776 	return 0;
4777 }
4778 
4779 static void bnxt_init_vnics(struct bnxt *bp)
4780 {
4781 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4782 	int i;
4783 
4784 	for (i = 0; i < bp->nr_vnics; i++) {
4785 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4786 		int j;
4787 
4788 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4789 		vnic->vnic_id = i;
4790 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4791 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4792 
4793 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4794 
4795 		if (bp->vnic_info[i].rss_hash_key) {
4796 			if (i == BNXT_VNIC_DEFAULT) {
4797 				u8 *key = (void *)vnic->rss_hash_key;
4798 				int k;
4799 
4800 				if (!bp->rss_hash_key_valid &&
4801 				    !bp->rss_hash_key_updated) {
4802 					get_random_bytes(bp->rss_hash_key,
4803 							 HW_HASH_KEY_SIZE);
4804 					bp->rss_hash_key_updated = true;
4805 				}
4806 
4807 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4808 				       HW_HASH_KEY_SIZE);
4809 
4810 				if (!bp->rss_hash_key_updated)
4811 					continue;
4812 
4813 				bp->rss_hash_key_updated = false;
4814 				bp->rss_hash_key_valid = true;
4815 
4816 				bp->toeplitz_prefix = 0;
4817 				for (k = 0; k < 8; k++) {
4818 					bp->toeplitz_prefix <<= 8;
4819 					bp->toeplitz_prefix |= key[k];
4820 				}
4821 			} else {
4822 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4823 				       HW_HASH_KEY_SIZE);
4824 			}
4825 		}
4826 	}
4827 }
4828 
4829 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4830 {
4831 	int pages;
4832 
4833 	pages = ring_size / desc_per_pg;
4834 
4835 	if (!pages)
4836 		return 1;
4837 
4838 	pages++;
4839 
4840 	while (pages & (pages - 1))
4841 		pages++;
4842 
4843 	return pages;
4844 }
4845 
4846 void bnxt_set_tpa_flags(struct bnxt *bp)
4847 {
4848 	bp->flags &= ~BNXT_FLAG_TPA;
4849 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4850 		return;
4851 	if (bp->dev->features & NETIF_F_LRO)
4852 		bp->flags |= BNXT_FLAG_LRO;
4853 	else if (bp->dev->features & NETIF_F_GRO_HW)
4854 		bp->flags |= BNXT_FLAG_GRO;
4855 }
4856 
4857 static void bnxt_init_ring_params(struct bnxt *bp)
4858 {
4859 	unsigned int rx_size;
4860 
4861 	bp->rx_copybreak = 0; /* rx-copybreak disabled by default */
4862 	/* Try to fit 4 chunks into a 4k page */
4863 	rx_size = SZ_1K -
4864 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4865 	bp->dev->cfg->hds_thresh = max(BNXT_MIN_RX_HDR_BUF, rx_size);
4866 }
4867 
4868 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4869  * be set on entry.
4870  */
4871 void bnxt_set_ring_params(struct bnxt *bp)
4872 {
4873 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4874 	u32 agg_factor = 0, agg_ring_size = 0;
4875 
4876 	/* 8 for CRC and VLAN */
4877 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4878 
4879 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4880 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4881 
4882 	ring_size = bp->rx_ring_size;
4883 	bp->rx_agg_ring_size = 0;
4884 	bp->rx_agg_nr_pages = 0;
4885 
4886 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4887 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4888 
4889 	bp->flags &= ~BNXT_FLAG_JUMBO;
4890 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4891 		u32 jumbo_factor;
4892 
4893 		bp->flags |= BNXT_FLAG_JUMBO;
4894 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4895 		if (jumbo_factor > agg_factor)
4896 			agg_factor = jumbo_factor;
4897 	}
4898 	if (agg_factor) {
4899 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4900 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4901 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4902 				    bp->rx_ring_size, ring_size);
4903 			bp->rx_ring_size = ring_size;
4904 		}
4905 		agg_ring_size = ring_size * agg_factor;
4906 
4907 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4908 							RX_DESC_CNT);
4909 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4910 			u32 tmp = agg_ring_size;
4911 
4912 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4913 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4914 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4915 				    tmp, agg_ring_size);
4916 		}
4917 		bp->rx_agg_ring_size = agg_ring_size;
4918 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4919 
4920 		if (BNXT_RX_PAGE_MODE(bp)) {
4921 			rx_space = PAGE_SIZE;
4922 			rx_size = PAGE_SIZE -
4923 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4924 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4925 		} else {
4926 			rx_size = max3(BNXT_MIN_RX_HDR_BUF,
4927 				       bp->rx_copybreak,
4928 				       bp->dev->cfg_pending->hds_thresh);
4929 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4930 			rx_space = rx_size + NET_SKB_PAD +
4931 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4932 		}
4933 	}
4934 
4935 	bp->rx_buf_use_size = rx_size;
4936 	bp->rx_buf_size = rx_space;
4937 
4938 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4939 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4940 
4941 	ring_size = bp->tx_ring_size;
4942 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4943 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4944 
4945 	max_rx_cmpl = bp->rx_ring_size;
4946 	/* MAX TPA needs to be added because TPA_START completions are
4947 	 * immediately recycled, so the TPA completions are not bound by
4948 	 * the RX ring size.
4949 	 */
4950 	if (bp->flags & BNXT_FLAG_TPA)
4951 		max_rx_cmpl += bp->max_tpa;
4952 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4953 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4954 	bp->cp_ring_size = ring_size;
4955 
4956 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4957 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4958 		bp->cp_nr_pages = MAX_CP_PAGES;
4959 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4960 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4961 			    ring_size, bp->cp_ring_size);
4962 	}
4963 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4964 	bp->cp_ring_mask = bp->cp_bit - 1;
4965 }
4966 
4967 /* Changing allocation mode of RX rings.
4968  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4969  */
4970 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4971 {
4972 	struct net_device *dev = bp->dev;
4973 
4974 	if (page_mode) {
4975 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4976 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4977 
4978 		if (bp->xdp_prog->aux->xdp_has_frags)
4979 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4980 		else
4981 			dev->max_mtu =
4982 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4983 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4984 			bp->flags |= BNXT_FLAG_JUMBO;
4985 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4986 		} else {
4987 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4988 			bp->rx_skb_func = bnxt_rx_page_skb;
4989 		}
4990 		bp->rx_dir = DMA_BIDIRECTIONAL;
4991 	} else {
4992 		dev->max_mtu = bp->max_mtu;
4993 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4994 		bp->rx_dir = DMA_FROM_DEVICE;
4995 		bp->rx_skb_func = bnxt_rx_skb;
4996 	}
4997 }
4998 
4999 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
5000 {
5001 	__bnxt_set_rx_skb_mode(bp, page_mode);
5002 
5003 	if (!page_mode) {
5004 		int rx, tx;
5005 
5006 		bnxt_get_max_rings(bp, &rx, &tx, true);
5007 		if (rx > 1) {
5008 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
5009 			bp->dev->hw_features |= NETIF_F_LRO;
5010 		}
5011 	}
5012 
5013 	/* Update LRO and GRO_HW availability */
5014 	netdev_update_features(bp->dev);
5015 }
5016 
5017 static void bnxt_free_vnic_attributes(struct bnxt *bp)
5018 {
5019 	int i;
5020 	struct bnxt_vnic_info *vnic;
5021 	struct pci_dev *pdev = bp->pdev;
5022 
5023 	if (!bp->vnic_info)
5024 		return;
5025 
5026 	for (i = 0; i < bp->nr_vnics; i++) {
5027 		vnic = &bp->vnic_info[i];
5028 
5029 		kfree(vnic->fw_grp_ids);
5030 		vnic->fw_grp_ids = NULL;
5031 
5032 		kfree(vnic->uc_list);
5033 		vnic->uc_list = NULL;
5034 
5035 		if (vnic->mc_list) {
5036 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
5037 					  vnic->mc_list, vnic->mc_list_mapping);
5038 			vnic->mc_list = NULL;
5039 		}
5040 
5041 		if (vnic->rss_table) {
5042 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
5043 					  vnic->rss_table,
5044 					  vnic->rss_table_dma_addr);
5045 			vnic->rss_table = NULL;
5046 		}
5047 
5048 		vnic->rss_hash_key = NULL;
5049 		vnic->flags = 0;
5050 	}
5051 }
5052 
5053 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
5054 {
5055 	int i, rc = 0, size;
5056 	struct bnxt_vnic_info *vnic;
5057 	struct pci_dev *pdev = bp->pdev;
5058 	int max_rings;
5059 
5060 	for (i = 0; i < bp->nr_vnics; i++) {
5061 		vnic = &bp->vnic_info[i];
5062 
5063 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
5064 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
5065 
5066 			if (mem_size > 0) {
5067 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
5068 				if (!vnic->uc_list) {
5069 					rc = -ENOMEM;
5070 					goto out;
5071 				}
5072 			}
5073 		}
5074 
5075 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
5076 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
5077 			vnic->mc_list =
5078 				dma_alloc_coherent(&pdev->dev,
5079 						   vnic->mc_list_size,
5080 						   &vnic->mc_list_mapping,
5081 						   GFP_KERNEL);
5082 			if (!vnic->mc_list) {
5083 				rc = -ENOMEM;
5084 				goto out;
5085 			}
5086 		}
5087 
5088 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5089 			goto vnic_skip_grps;
5090 
5091 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5092 			max_rings = bp->rx_nr_rings;
5093 		else
5094 			max_rings = 1;
5095 
5096 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
5097 		if (!vnic->fw_grp_ids) {
5098 			rc = -ENOMEM;
5099 			goto out;
5100 		}
5101 vnic_skip_grps:
5102 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
5103 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
5104 			continue;
5105 
5106 		/* Allocate rss table and hash key */
5107 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5108 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5109 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5110 
5111 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5112 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5113 						     vnic->rss_table_size,
5114 						     &vnic->rss_table_dma_addr,
5115 						     GFP_KERNEL);
5116 		if (!vnic->rss_table) {
5117 			rc = -ENOMEM;
5118 			goto out;
5119 		}
5120 
5121 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5122 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5123 	}
5124 	return 0;
5125 
5126 out:
5127 	return rc;
5128 }
5129 
5130 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5131 {
5132 	struct bnxt_hwrm_wait_token *token;
5133 
5134 	dma_pool_destroy(bp->hwrm_dma_pool);
5135 	bp->hwrm_dma_pool = NULL;
5136 
5137 	rcu_read_lock();
5138 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5139 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5140 	rcu_read_unlock();
5141 }
5142 
5143 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5144 {
5145 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5146 					    BNXT_HWRM_DMA_SIZE,
5147 					    BNXT_HWRM_DMA_ALIGN, 0);
5148 	if (!bp->hwrm_dma_pool)
5149 		return -ENOMEM;
5150 
5151 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5152 
5153 	return 0;
5154 }
5155 
5156 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5157 {
5158 	kfree(stats->hw_masks);
5159 	stats->hw_masks = NULL;
5160 	kfree(stats->sw_stats);
5161 	stats->sw_stats = NULL;
5162 	if (stats->hw_stats) {
5163 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5164 				  stats->hw_stats_map);
5165 		stats->hw_stats = NULL;
5166 	}
5167 }
5168 
5169 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5170 				bool alloc_masks)
5171 {
5172 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5173 					     &stats->hw_stats_map, GFP_KERNEL);
5174 	if (!stats->hw_stats)
5175 		return -ENOMEM;
5176 
5177 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5178 	if (!stats->sw_stats)
5179 		goto stats_mem_err;
5180 
5181 	if (alloc_masks) {
5182 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5183 		if (!stats->hw_masks)
5184 			goto stats_mem_err;
5185 	}
5186 	return 0;
5187 
5188 stats_mem_err:
5189 	bnxt_free_stats_mem(bp, stats);
5190 	return -ENOMEM;
5191 }
5192 
5193 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5194 {
5195 	int i;
5196 
5197 	for (i = 0; i < count; i++)
5198 		mask_arr[i] = mask;
5199 }
5200 
5201 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5202 {
5203 	int i;
5204 
5205 	for (i = 0; i < count; i++)
5206 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5207 }
5208 
5209 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5210 				    struct bnxt_stats_mem *stats)
5211 {
5212 	struct hwrm_func_qstats_ext_output *resp;
5213 	struct hwrm_func_qstats_ext_input *req;
5214 	__le64 *hw_masks;
5215 	int rc;
5216 
5217 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5218 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5219 		return -EOPNOTSUPP;
5220 
5221 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5222 	if (rc)
5223 		return rc;
5224 
5225 	req->fid = cpu_to_le16(0xffff);
5226 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5227 
5228 	resp = hwrm_req_hold(bp, req);
5229 	rc = hwrm_req_send(bp, req);
5230 	if (!rc) {
5231 		hw_masks = &resp->rx_ucast_pkts;
5232 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5233 	}
5234 	hwrm_req_drop(bp, req);
5235 	return rc;
5236 }
5237 
5238 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5239 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5240 
5241 static void bnxt_init_stats(struct bnxt *bp)
5242 {
5243 	struct bnxt_napi *bnapi = bp->bnapi[0];
5244 	struct bnxt_cp_ring_info *cpr;
5245 	struct bnxt_stats_mem *stats;
5246 	__le64 *rx_stats, *tx_stats;
5247 	int rc, rx_count, tx_count;
5248 	u64 *rx_masks, *tx_masks;
5249 	u64 mask;
5250 	u8 flags;
5251 
5252 	cpr = &bnapi->cp_ring;
5253 	stats = &cpr->stats;
5254 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5255 	if (rc) {
5256 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5257 			mask = (1ULL << 48) - 1;
5258 		else
5259 			mask = -1ULL;
5260 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5261 	}
5262 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5263 		stats = &bp->port_stats;
5264 		rx_stats = stats->hw_stats;
5265 		rx_masks = stats->hw_masks;
5266 		rx_count = sizeof(struct rx_port_stats) / 8;
5267 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5268 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5269 		tx_count = sizeof(struct tx_port_stats) / 8;
5270 
5271 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5272 		rc = bnxt_hwrm_port_qstats(bp, flags);
5273 		if (rc) {
5274 			mask = (1ULL << 40) - 1;
5275 
5276 			bnxt_fill_masks(rx_masks, mask, rx_count);
5277 			bnxt_fill_masks(tx_masks, mask, tx_count);
5278 		} else {
5279 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5280 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5281 			bnxt_hwrm_port_qstats(bp, 0);
5282 		}
5283 	}
5284 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5285 		stats = &bp->rx_port_stats_ext;
5286 		rx_stats = stats->hw_stats;
5287 		rx_masks = stats->hw_masks;
5288 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5289 		stats = &bp->tx_port_stats_ext;
5290 		tx_stats = stats->hw_stats;
5291 		tx_masks = stats->hw_masks;
5292 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5293 
5294 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5295 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5296 		if (rc) {
5297 			mask = (1ULL << 40) - 1;
5298 
5299 			bnxt_fill_masks(rx_masks, mask, rx_count);
5300 			if (tx_stats)
5301 				bnxt_fill_masks(tx_masks, mask, tx_count);
5302 		} else {
5303 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5304 			if (tx_stats)
5305 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5306 						   tx_count);
5307 			bnxt_hwrm_port_qstats_ext(bp, 0);
5308 		}
5309 	}
5310 }
5311 
5312 static void bnxt_free_port_stats(struct bnxt *bp)
5313 {
5314 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5315 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5316 
5317 	bnxt_free_stats_mem(bp, &bp->port_stats);
5318 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5319 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5320 }
5321 
5322 static void bnxt_free_ring_stats(struct bnxt *bp)
5323 {
5324 	int i;
5325 
5326 	if (!bp->bnapi)
5327 		return;
5328 
5329 	for (i = 0; i < bp->cp_nr_rings; i++) {
5330 		struct bnxt_napi *bnapi = bp->bnapi[i];
5331 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5332 
5333 		bnxt_free_stats_mem(bp, &cpr->stats);
5334 
5335 		kfree(cpr->sw_stats);
5336 		cpr->sw_stats = NULL;
5337 	}
5338 }
5339 
5340 static int bnxt_alloc_stats(struct bnxt *bp)
5341 {
5342 	u32 size, i;
5343 	int rc;
5344 
5345 	size = bp->hw_ring_stats_size;
5346 
5347 	for (i = 0; i < bp->cp_nr_rings; i++) {
5348 		struct bnxt_napi *bnapi = bp->bnapi[i];
5349 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5350 
5351 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5352 		if (!cpr->sw_stats)
5353 			return -ENOMEM;
5354 
5355 		cpr->stats.len = size;
5356 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5357 		if (rc)
5358 			return rc;
5359 
5360 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5361 	}
5362 
5363 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5364 		return 0;
5365 
5366 	if (bp->port_stats.hw_stats)
5367 		goto alloc_ext_stats;
5368 
5369 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5370 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5371 	if (rc)
5372 		return rc;
5373 
5374 	bp->flags |= BNXT_FLAG_PORT_STATS;
5375 
5376 alloc_ext_stats:
5377 	/* Display extended statistics only if FW supports it */
5378 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5379 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5380 			return 0;
5381 
5382 	if (bp->rx_port_stats_ext.hw_stats)
5383 		goto alloc_tx_ext_stats;
5384 
5385 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5386 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5387 	/* Extended stats are optional */
5388 	if (rc)
5389 		return 0;
5390 
5391 alloc_tx_ext_stats:
5392 	if (bp->tx_port_stats_ext.hw_stats)
5393 		return 0;
5394 
5395 	if (bp->hwrm_spec_code >= 0x10902 ||
5396 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5397 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5398 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5399 		/* Extended stats are optional */
5400 		if (rc)
5401 			return 0;
5402 	}
5403 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5404 	return 0;
5405 }
5406 
5407 static void bnxt_clear_ring_indices(struct bnxt *bp)
5408 {
5409 	int i, j;
5410 
5411 	if (!bp->bnapi)
5412 		return;
5413 
5414 	for (i = 0; i < bp->cp_nr_rings; i++) {
5415 		struct bnxt_napi *bnapi = bp->bnapi[i];
5416 		struct bnxt_cp_ring_info *cpr;
5417 		struct bnxt_rx_ring_info *rxr;
5418 		struct bnxt_tx_ring_info *txr;
5419 
5420 		if (!bnapi)
5421 			continue;
5422 
5423 		cpr = &bnapi->cp_ring;
5424 		cpr->cp_raw_cons = 0;
5425 
5426 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5427 			txr->tx_prod = 0;
5428 			txr->tx_cons = 0;
5429 			txr->tx_hw_cons = 0;
5430 		}
5431 
5432 		rxr = bnapi->rx_ring;
5433 		if (rxr) {
5434 			rxr->rx_prod = 0;
5435 			rxr->rx_agg_prod = 0;
5436 			rxr->rx_sw_agg_prod = 0;
5437 			rxr->rx_next_cons = 0;
5438 		}
5439 		bnapi->events = 0;
5440 	}
5441 }
5442 
5443 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5444 {
5445 	u8 type = fltr->type, flags = fltr->flags;
5446 
5447 	INIT_LIST_HEAD(&fltr->list);
5448 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5449 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5450 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5451 }
5452 
5453 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5454 {
5455 	if (!list_empty(&fltr->list))
5456 		list_del_init(&fltr->list);
5457 }
5458 
5459 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5460 {
5461 	struct bnxt_filter_base *usr_fltr, *tmp;
5462 
5463 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5464 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5465 			continue;
5466 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5467 	}
5468 }
5469 
5470 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5471 {
5472 	hlist_del(&fltr->hash);
5473 	bnxt_del_one_usr_fltr(bp, fltr);
5474 	if (fltr->flags) {
5475 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5476 		bp->ntp_fltr_count--;
5477 	}
5478 	kfree(fltr);
5479 }
5480 
5481 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5482 {
5483 	int i;
5484 
5485 	netdev_assert_locked_or_invisible(bp->dev);
5486 
5487 	/* Under netdev instance lock and all our NAPIs have been disabled.
5488 	 * It's safe to delete the hash table.
5489 	 */
5490 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5491 		struct hlist_head *head;
5492 		struct hlist_node *tmp;
5493 		struct bnxt_ntuple_filter *fltr;
5494 
5495 		head = &bp->ntp_fltr_hash_tbl[i];
5496 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5497 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5498 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5499 				     !list_empty(&fltr->base.list)))
5500 				continue;
5501 			bnxt_del_fltr(bp, &fltr->base);
5502 		}
5503 	}
5504 	if (!all)
5505 		return;
5506 
5507 	bitmap_free(bp->ntp_fltr_bmap);
5508 	bp->ntp_fltr_bmap = NULL;
5509 	bp->ntp_fltr_count = 0;
5510 }
5511 
5512 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5513 {
5514 	int i, rc = 0;
5515 
5516 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5517 		return 0;
5518 
5519 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5520 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5521 
5522 	bp->ntp_fltr_count = 0;
5523 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5524 
5525 	if (!bp->ntp_fltr_bmap)
5526 		rc = -ENOMEM;
5527 
5528 	return rc;
5529 }
5530 
5531 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5532 {
5533 	int i;
5534 
5535 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5536 		struct hlist_head *head;
5537 		struct hlist_node *tmp;
5538 		struct bnxt_l2_filter *fltr;
5539 
5540 		head = &bp->l2_fltr_hash_tbl[i];
5541 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5542 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5543 				     !list_empty(&fltr->base.list)))
5544 				continue;
5545 			bnxt_del_fltr(bp, &fltr->base);
5546 		}
5547 	}
5548 }
5549 
5550 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5551 {
5552 	int i;
5553 
5554 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5555 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5556 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5557 }
5558 
5559 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5560 {
5561 	bnxt_free_vnic_attributes(bp);
5562 	bnxt_free_tx_rings(bp);
5563 	bnxt_free_rx_rings(bp);
5564 	bnxt_free_cp_rings(bp);
5565 	bnxt_free_all_cp_arrays(bp);
5566 	bnxt_free_ntp_fltrs(bp, false);
5567 	bnxt_free_l2_filters(bp, false);
5568 	if (irq_re_init) {
5569 		bnxt_free_ring_stats(bp);
5570 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5571 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5572 			bnxt_free_port_stats(bp);
5573 		bnxt_free_ring_grps(bp);
5574 		bnxt_free_vnics(bp);
5575 		kfree(bp->tx_ring_map);
5576 		bp->tx_ring_map = NULL;
5577 		kfree(bp->tx_ring);
5578 		bp->tx_ring = NULL;
5579 		kfree(bp->rx_ring);
5580 		bp->rx_ring = NULL;
5581 		kfree(bp->bnapi);
5582 		bp->bnapi = NULL;
5583 	} else {
5584 		bnxt_clear_ring_indices(bp);
5585 	}
5586 }
5587 
5588 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5589 {
5590 	int i, j, rc, size, arr_size;
5591 	void *bnapi;
5592 
5593 	if (irq_re_init) {
5594 		/* Allocate bnapi mem pointer array and mem block for
5595 		 * all queues
5596 		 */
5597 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5598 				bp->cp_nr_rings);
5599 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5600 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5601 		if (!bnapi)
5602 			return -ENOMEM;
5603 
5604 		bp->bnapi = bnapi;
5605 		bnapi += arr_size;
5606 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5607 			bp->bnapi[i] = bnapi;
5608 			bp->bnapi[i]->index = i;
5609 			bp->bnapi[i]->bp = bp;
5610 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5611 				struct bnxt_cp_ring_info *cpr =
5612 					&bp->bnapi[i]->cp_ring;
5613 
5614 				cpr->cp_ring_struct.ring_mem.flags =
5615 					BNXT_RMEM_RING_PTE_FLAG;
5616 			}
5617 		}
5618 
5619 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5620 					   bp->rx_nr_rings);
5621 		if (!bp->rx_ring)
5622 			return -ENOMEM;
5623 
5624 		for (i = 0; i < bp->rx_nr_rings; i++) {
5625 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5626 
5627 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5628 				rxr->rx_ring_struct.ring_mem.flags =
5629 					BNXT_RMEM_RING_PTE_FLAG;
5630 				rxr->rx_agg_ring_struct.ring_mem.flags =
5631 					BNXT_RMEM_RING_PTE_FLAG;
5632 			} else {
5633 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5634 			}
5635 			rxr->bnapi = bp->bnapi[i];
5636 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5637 		}
5638 
5639 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5640 					   bp->tx_nr_rings);
5641 		if (!bp->tx_ring)
5642 			return -ENOMEM;
5643 
5644 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5645 					  GFP_KERNEL);
5646 
5647 		if (!bp->tx_ring_map)
5648 			return -ENOMEM;
5649 
5650 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5651 			j = 0;
5652 		else
5653 			j = bp->rx_nr_rings;
5654 
5655 		for (i = 0; i < bp->tx_nr_rings; i++) {
5656 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5657 			struct bnxt_napi *bnapi2;
5658 
5659 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5660 				txr->tx_ring_struct.ring_mem.flags =
5661 					BNXT_RMEM_RING_PTE_FLAG;
5662 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5663 			if (i >= bp->tx_nr_rings_xdp) {
5664 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5665 
5666 				bnapi2 = bp->bnapi[k];
5667 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5668 				txr->tx_napi_idx =
5669 					BNXT_RING_TO_TC(bp, txr->txq_index);
5670 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5671 				bnapi2->tx_int = bnxt_tx_int;
5672 			} else {
5673 				bnapi2 = bp->bnapi[j];
5674 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5675 				bnapi2->tx_ring[0] = txr;
5676 				bnapi2->tx_int = bnxt_tx_int_xdp;
5677 				j++;
5678 			}
5679 			txr->bnapi = bnapi2;
5680 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5681 				txr->tx_cpr = &bnapi2->cp_ring;
5682 		}
5683 
5684 		rc = bnxt_alloc_stats(bp);
5685 		if (rc)
5686 			goto alloc_mem_err;
5687 		bnxt_init_stats(bp);
5688 
5689 		rc = bnxt_alloc_ntp_fltrs(bp);
5690 		if (rc)
5691 			goto alloc_mem_err;
5692 
5693 		rc = bnxt_alloc_vnics(bp);
5694 		if (rc)
5695 			goto alloc_mem_err;
5696 	}
5697 
5698 	rc = bnxt_alloc_all_cp_arrays(bp);
5699 	if (rc)
5700 		goto alloc_mem_err;
5701 
5702 	bnxt_init_ring_struct(bp);
5703 
5704 	rc = bnxt_alloc_rx_rings(bp);
5705 	if (rc)
5706 		goto alloc_mem_err;
5707 
5708 	rc = bnxt_alloc_tx_rings(bp);
5709 	if (rc)
5710 		goto alloc_mem_err;
5711 
5712 	rc = bnxt_alloc_cp_rings(bp);
5713 	if (rc)
5714 		goto alloc_mem_err;
5715 
5716 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5717 						  BNXT_VNIC_MCAST_FLAG |
5718 						  BNXT_VNIC_UCAST_FLAG;
5719 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5720 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5721 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5722 
5723 	rc = bnxt_alloc_vnic_attributes(bp);
5724 	if (rc)
5725 		goto alloc_mem_err;
5726 	return 0;
5727 
5728 alloc_mem_err:
5729 	bnxt_free_mem(bp, true);
5730 	return rc;
5731 }
5732 
5733 static void bnxt_disable_int(struct bnxt *bp)
5734 {
5735 	int i;
5736 
5737 	if (!bp->bnapi)
5738 		return;
5739 
5740 	for (i = 0; i < bp->cp_nr_rings; i++) {
5741 		struct bnxt_napi *bnapi = bp->bnapi[i];
5742 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5743 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5744 
5745 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5746 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5747 	}
5748 }
5749 
5750 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5751 {
5752 	struct bnxt_napi *bnapi = bp->bnapi[n];
5753 	struct bnxt_cp_ring_info *cpr;
5754 
5755 	cpr = &bnapi->cp_ring;
5756 	return cpr->cp_ring_struct.map_idx;
5757 }
5758 
5759 static void bnxt_disable_int_sync(struct bnxt *bp)
5760 {
5761 	int i;
5762 
5763 	if (!bp->irq_tbl || !bp->bnapi)
5764 		return;
5765 
5766 	atomic_inc(&bp->intr_sem);
5767 
5768 	bnxt_disable_int(bp);
5769 	for (i = 0; i < bp->cp_nr_rings; i++) {
5770 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5771 
5772 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5773 	}
5774 }
5775 
5776 static void bnxt_enable_int(struct bnxt *bp)
5777 {
5778 	int i;
5779 
5780 	atomic_set(&bp->intr_sem, 0);
5781 	for (i = 0; i < bp->cp_nr_rings; i++) {
5782 		struct bnxt_napi *bnapi = bp->bnapi[i];
5783 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5784 
5785 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5786 	}
5787 }
5788 
5789 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5790 			    bool async_only)
5791 {
5792 	DECLARE_BITMAP(async_events_bmap, 256);
5793 	u32 *events = (u32 *)async_events_bmap;
5794 	struct hwrm_func_drv_rgtr_output *resp;
5795 	struct hwrm_func_drv_rgtr_input *req;
5796 	u32 flags;
5797 	int rc, i;
5798 
5799 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5800 	if (rc)
5801 		return rc;
5802 
5803 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5804 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5805 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5806 
5807 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5808 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5809 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5810 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5811 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5812 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5813 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5814 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5815 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5816 	req->flags = cpu_to_le32(flags);
5817 	req->ver_maj_8b = DRV_VER_MAJ;
5818 	req->ver_min_8b = DRV_VER_MIN;
5819 	req->ver_upd_8b = DRV_VER_UPD;
5820 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5821 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5822 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5823 
5824 	if (BNXT_PF(bp)) {
5825 		u32 data[8];
5826 		int i;
5827 
5828 		memset(data, 0, sizeof(data));
5829 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5830 			u16 cmd = bnxt_vf_req_snif[i];
5831 			unsigned int bit, idx;
5832 
5833 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5834 			    cmd == HWRM_PORT_PHY_QCFG)
5835 				continue;
5836 
5837 			idx = cmd / 32;
5838 			bit = cmd % 32;
5839 			data[idx] |= 1 << bit;
5840 		}
5841 
5842 		for (i = 0; i < 8; i++)
5843 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5844 
5845 		req->enables |=
5846 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5847 	}
5848 
5849 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5850 		req->flags |= cpu_to_le32(
5851 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5852 
5853 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5854 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5855 		u16 event_id = bnxt_async_events_arr[i];
5856 
5857 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5858 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5859 			continue;
5860 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5861 		    !bp->ptp_cfg)
5862 			continue;
5863 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5864 	}
5865 	if (bmap && bmap_size) {
5866 		for (i = 0; i < bmap_size; i++) {
5867 			if (test_bit(i, bmap))
5868 				__set_bit(i, async_events_bmap);
5869 		}
5870 	}
5871 	for (i = 0; i < 8; i++)
5872 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5873 
5874 	if (async_only)
5875 		req->enables =
5876 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5877 
5878 	resp = hwrm_req_hold(bp, req);
5879 	rc = hwrm_req_send(bp, req);
5880 	if (!rc) {
5881 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5882 		if (resp->flags &
5883 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5884 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5885 	}
5886 	hwrm_req_drop(bp, req);
5887 	return rc;
5888 }
5889 
5890 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5891 {
5892 	struct hwrm_func_drv_unrgtr_input *req;
5893 	int rc;
5894 
5895 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5896 		return 0;
5897 
5898 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5899 	if (rc)
5900 		return rc;
5901 	return hwrm_req_send(bp, req);
5902 }
5903 
5904 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5905 
5906 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5907 {
5908 	struct hwrm_tunnel_dst_port_free_input *req;
5909 	int rc;
5910 
5911 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5912 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5913 		return 0;
5914 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5915 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5916 		return 0;
5917 
5918 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5919 	if (rc)
5920 		return rc;
5921 
5922 	req->tunnel_type = tunnel_type;
5923 
5924 	switch (tunnel_type) {
5925 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5926 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5927 		bp->vxlan_port = 0;
5928 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5929 		break;
5930 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5931 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5932 		bp->nge_port = 0;
5933 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5934 		break;
5935 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5936 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5937 		bp->vxlan_gpe_port = 0;
5938 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5939 		break;
5940 	default:
5941 		break;
5942 	}
5943 
5944 	rc = hwrm_req_send(bp, req);
5945 	if (rc)
5946 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5947 			   rc);
5948 	if (bp->flags & BNXT_FLAG_TPA)
5949 		bnxt_set_tpa(bp, true);
5950 	return rc;
5951 }
5952 
5953 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5954 					   u8 tunnel_type)
5955 {
5956 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5957 	struct hwrm_tunnel_dst_port_alloc_input *req;
5958 	int rc;
5959 
5960 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5961 	if (rc)
5962 		return rc;
5963 
5964 	req->tunnel_type = tunnel_type;
5965 	req->tunnel_dst_port_val = port;
5966 
5967 	resp = hwrm_req_hold(bp, req);
5968 	rc = hwrm_req_send(bp, req);
5969 	if (rc) {
5970 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5971 			   rc);
5972 		goto err_out;
5973 	}
5974 
5975 	switch (tunnel_type) {
5976 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5977 		bp->vxlan_port = port;
5978 		bp->vxlan_fw_dst_port_id =
5979 			le16_to_cpu(resp->tunnel_dst_port_id);
5980 		break;
5981 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5982 		bp->nge_port = port;
5983 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5984 		break;
5985 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5986 		bp->vxlan_gpe_port = port;
5987 		bp->vxlan_gpe_fw_dst_port_id =
5988 			le16_to_cpu(resp->tunnel_dst_port_id);
5989 		break;
5990 	default:
5991 		break;
5992 	}
5993 	if (bp->flags & BNXT_FLAG_TPA)
5994 		bnxt_set_tpa(bp, true);
5995 
5996 err_out:
5997 	hwrm_req_drop(bp, req);
5998 	return rc;
5999 }
6000 
6001 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
6002 {
6003 	struct hwrm_cfa_l2_set_rx_mask_input *req;
6004 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6005 	int rc;
6006 
6007 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
6008 	if (rc)
6009 		return rc;
6010 
6011 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6012 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
6013 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
6014 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
6015 	}
6016 	req->mask = cpu_to_le32(vnic->rx_mask);
6017 	return hwrm_req_send_silent(bp, req);
6018 }
6019 
6020 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6021 {
6022 	if (!atomic_dec_and_test(&fltr->refcnt))
6023 		return;
6024 	spin_lock_bh(&bp->ntp_fltr_lock);
6025 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
6026 		spin_unlock_bh(&bp->ntp_fltr_lock);
6027 		return;
6028 	}
6029 	hlist_del_rcu(&fltr->base.hash);
6030 	bnxt_del_one_usr_fltr(bp, &fltr->base);
6031 	if (fltr->base.flags) {
6032 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
6033 		bp->ntp_fltr_count--;
6034 	}
6035 	spin_unlock_bh(&bp->ntp_fltr_lock);
6036 	kfree_rcu(fltr, base.rcu);
6037 }
6038 
6039 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
6040 						      struct bnxt_l2_key *key,
6041 						      u32 idx)
6042 {
6043 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
6044 	struct bnxt_l2_filter *fltr;
6045 
6046 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
6047 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
6048 
6049 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
6050 		    l2_key->vlan == key->vlan)
6051 			return fltr;
6052 	}
6053 	return NULL;
6054 }
6055 
6056 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
6057 						    struct bnxt_l2_key *key,
6058 						    u32 idx)
6059 {
6060 	struct bnxt_l2_filter *fltr = NULL;
6061 
6062 	rcu_read_lock();
6063 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6064 	if (fltr)
6065 		atomic_inc(&fltr->refcnt);
6066 	rcu_read_unlock();
6067 	return fltr;
6068 }
6069 
6070 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
6071 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6072 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
6073 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6074 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
6075 
6076 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
6077 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6078 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
6079 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6080 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
6081 
6082 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
6083 {
6084 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6085 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
6086 			return sizeof(fkeys->addrs.v4addrs) +
6087 			       sizeof(fkeys->ports);
6088 
6089 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
6090 			return sizeof(fkeys->addrs.v4addrs);
6091 	}
6092 
6093 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
6094 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
6095 			return sizeof(fkeys->addrs.v6addrs) +
6096 			       sizeof(fkeys->ports);
6097 
6098 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6099 			return sizeof(fkeys->addrs.v6addrs);
6100 	}
6101 
6102 	return 0;
6103 }
6104 
6105 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6106 			 const unsigned char *key)
6107 {
6108 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6109 	struct bnxt_ipv4_tuple tuple4;
6110 	struct bnxt_ipv6_tuple tuple6;
6111 	int i, j, len = 0;
6112 	u8 *four_tuple;
6113 
6114 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6115 	if (!len)
6116 		return 0;
6117 
6118 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6119 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6120 		tuple4.ports = fkeys->ports;
6121 		four_tuple = (unsigned char *)&tuple4;
6122 	} else {
6123 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6124 		tuple6.ports = fkeys->ports;
6125 		four_tuple = (unsigned char *)&tuple6;
6126 	}
6127 
6128 	for (i = 0, j = 8; i < len; i++, j++) {
6129 		u8 byte = four_tuple[i];
6130 		int bit;
6131 
6132 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6133 			if (byte & 0x80)
6134 				hash ^= prefix;
6135 		}
6136 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6137 	}
6138 
6139 	/* The valid part of the hash is in the upper 32 bits. */
6140 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6141 }
6142 
6143 #ifdef CONFIG_RFS_ACCEL
6144 static struct bnxt_l2_filter *
6145 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6146 {
6147 	struct bnxt_l2_filter *fltr;
6148 	u32 idx;
6149 
6150 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6151 	      BNXT_L2_FLTR_HASH_MASK;
6152 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6153 	return fltr;
6154 }
6155 #endif
6156 
6157 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6158 			       struct bnxt_l2_key *key, u32 idx)
6159 {
6160 	struct hlist_head *head;
6161 
6162 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6163 	fltr->l2_key.vlan = key->vlan;
6164 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6165 	if (fltr->base.flags) {
6166 		int bit_id;
6167 
6168 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6169 						 bp->max_fltr, 0);
6170 		if (bit_id < 0)
6171 			return -ENOMEM;
6172 		fltr->base.sw_id = (u16)bit_id;
6173 		bp->ntp_fltr_count++;
6174 	}
6175 	head = &bp->l2_fltr_hash_tbl[idx];
6176 	hlist_add_head_rcu(&fltr->base.hash, head);
6177 	bnxt_insert_usr_fltr(bp, &fltr->base);
6178 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6179 	atomic_set(&fltr->refcnt, 1);
6180 	return 0;
6181 }
6182 
6183 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6184 						   struct bnxt_l2_key *key,
6185 						   gfp_t gfp)
6186 {
6187 	struct bnxt_l2_filter *fltr;
6188 	u32 idx;
6189 	int rc;
6190 
6191 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6192 	      BNXT_L2_FLTR_HASH_MASK;
6193 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6194 	if (fltr)
6195 		return fltr;
6196 
6197 	fltr = kzalloc_obj(*fltr, gfp);
6198 	if (!fltr)
6199 		return ERR_PTR(-ENOMEM);
6200 	spin_lock_bh(&bp->ntp_fltr_lock);
6201 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6202 	spin_unlock_bh(&bp->ntp_fltr_lock);
6203 	if (rc) {
6204 		bnxt_del_l2_filter(bp, fltr);
6205 		fltr = ERR_PTR(rc);
6206 	}
6207 	return fltr;
6208 }
6209 
6210 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6211 						struct bnxt_l2_key *key,
6212 						u16 flags)
6213 {
6214 	struct bnxt_l2_filter *fltr;
6215 	u32 idx;
6216 	int rc;
6217 
6218 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6219 	      BNXT_L2_FLTR_HASH_MASK;
6220 	spin_lock_bh(&bp->ntp_fltr_lock);
6221 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6222 	if (fltr) {
6223 		fltr = ERR_PTR(-EEXIST);
6224 		goto l2_filter_exit;
6225 	}
6226 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6227 	if (!fltr) {
6228 		fltr = ERR_PTR(-ENOMEM);
6229 		goto l2_filter_exit;
6230 	}
6231 	fltr->base.flags = flags;
6232 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6233 	if (rc) {
6234 		spin_unlock_bh(&bp->ntp_fltr_lock);
6235 		bnxt_del_l2_filter(bp, fltr);
6236 		return ERR_PTR(rc);
6237 	}
6238 
6239 l2_filter_exit:
6240 	spin_unlock_bh(&bp->ntp_fltr_lock);
6241 	return fltr;
6242 }
6243 
6244 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6245 {
6246 #ifdef CONFIG_BNXT_SRIOV
6247 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6248 
6249 	return vf->fw_fid;
6250 #else
6251 	return INVALID_HW_RING_ID;
6252 #endif
6253 }
6254 
6255 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6256 {
6257 	struct hwrm_cfa_l2_filter_free_input *req;
6258 	u16 target_id = 0xffff;
6259 	int rc;
6260 
6261 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6262 		struct bnxt_pf_info *pf = &bp->pf;
6263 
6264 		if (fltr->base.vf_idx >= pf->active_vfs)
6265 			return -EINVAL;
6266 
6267 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6268 		if (target_id == INVALID_HW_RING_ID)
6269 			return -EINVAL;
6270 	}
6271 
6272 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6273 	if (rc)
6274 		return rc;
6275 
6276 	req->target_id = cpu_to_le16(target_id);
6277 	req->l2_filter_id = fltr->base.filter_id;
6278 	return hwrm_req_send(bp, req);
6279 }
6280 
6281 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6282 {
6283 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6284 	struct hwrm_cfa_l2_filter_alloc_input *req;
6285 	u16 target_id = 0xffff;
6286 	int rc;
6287 
6288 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6289 		struct bnxt_pf_info *pf = &bp->pf;
6290 
6291 		if (fltr->base.vf_idx >= pf->active_vfs)
6292 			return -EINVAL;
6293 
6294 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6295 	}
6296 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6297 	if (rc)
6298 		return rc;
6299 
6300 	req->target_id = cpu_to_le16(target_id);
6301 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6302 
6303 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6304 		req->flags |=
6305 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6306 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6307 	req->enables =
6308 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6309 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6310 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6311 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6312 	eth_broadcast_addr(req->l2_addr_mask);
6313 
6314 	if (fltr->l2_key.vlan) {
6315 		req->enables |=
6316 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6317 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6318 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6319 		req->num_vlans = 1;
6320 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6321 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6322 	}
6323 
6324 	resp = hwrm_req_hold(bp, req);
6325 	rc = hwrm_req_send(bp, req);
6326 	if (!rc) {
6327 		fltr->base.filter_id = resp->l2_filter_id;
6328 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6329 	}
6330 	hwrm_req_drop(bp, req);
6331 	return rc;
6332 }
6333 
6334 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6335 				     struct bnxt_ntuple_filter *fltr)
6336 {
6337 	struct hwrm_cfa_ntuple_filter_free_input *req;
6338 	int rc;
6339 
6340 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6341 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6342 		return 0;
6343 
6344 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6345 	if (rc)
6346 		return rc;
6347 
6348 	req->ntuple_filter_id = fltr->base.filter_id;
6349 	return hwrm_req_send(bp, req);
6350 }
6351 
6352 #define BNXT_NTP_FLTR_FLAGS					\
6353 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6354 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6355 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6356 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6357 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6358 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6359 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6360 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6361 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6362 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6363 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6364 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6365 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6366 
6367 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6368 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6369 
6370 void bnxt_fill_ipv6_mask(__be32 mask[4])
6371 {
6372 	int i;
6373 
6374 	for (i = 0; i < 4; i++)
6375 		mask[i] = cpu_to_be32(~0);
6376 }
6377 
6378 static void
6379 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6380 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6381 			  struct bnxt_ntuple_filter *fltr)
6382 {
6383 	u16 rxq = fltr->base.rxq;
6384 
6385 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6386 		struct ethtool_rxfh_context *ctx;
6387 		struct bnxt_rss_ctx *rss_ctx;
6388 		struct bnxt_vnic_info *vnic;
6389 
6390 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6391 			      fltr->base.fw_vnic_id);
6392 		if (ctx) {
6393 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6394 			vnic = &rss_ctx->vnic;
6395 
6396 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6397 		}
6398 		return;
6399 	}
6400 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6401 		struct bnxt_vnic_info *vnic;
6402 		u32 enables;
6403 
6404 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6405 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6406 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6407 		req->enables |= cpu_to_le32(enables);
6408 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6409 	} else {
6410 		u32 flags;
6411 
6412 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6413 		req->flags |= cpu_to_le32(flags);
6414 		req->dst_id = cpu_to_le16(rxq);
6415 	}
6416 }
6417 
6418 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6419 				      struct bnxt_ntuple_filter *fltr)
6420 {
6421 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6422 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6423 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6424 	struct flow_keys *keys = &fltr->fkeys;
6425 	struct bnxt_l2_filter *l2_fltr;
6426 	struct bnxt_vnic_info *vnic;
6427 	int rc;
6428 
6429 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6430 	if (rc)
6431 		return rc;
6432 
6433 	l2_fltr = fltr->l2_fltr;
6434 	req->l2_filter_id = l2_fltr->base.filter_id;
6435 
6436 	if (fltr->base.flags & BNXT_ACT_DROP) {
6437 		req->flags =
6438 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6439 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6440 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6441 	} else {
6442 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6443 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6444 	}
6445 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6446 
6447 	req->ethertype = htons(ETH_P_IP);
6448 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6449 	req->ip_protocol = keys->basic.ip_proto;
6450 
6451 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6452 		req->ethertype = htons(ETH_P_IPV6);
6453 		req->ip_addr_type =
6454 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6455 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6456 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6457 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6458 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6459 	} else {
6460 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6461 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6462 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6463 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6464 	}
6465 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6466 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6467 		req->tunnel_type =
6468 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6469 	}
6470 
6471 	req->src_port = keys->ports.src;
6472 	req->src_port_mask = masks->ports.src;
6473 	req->dst_port = keys->ports.dst;
6474 	req->dst_port_mask = masks->ports.dst;
6475 
6476 	resp = hwrm_req_hold(bp, req);
6477 	rc = hwrm_req_send(bp, req);
6478 	if (!rc)
6479 		fltr->base.filter_id = resp->ntuple_filter_id;
6480 	hwrm_req_drop(bp, req);
6481 	return rc;
6482 }
6483 
6484 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6485 				     const u8 *mac_addr)
6486 {
6487 	struct bnxt_l2_filter *fltr;
6488 	struct bnxt_l2_key key;
6489 	int rc;
6490 
6491 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6492 	key.vlan = 0;
6493 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6494 	if (IS_ERR(fltr))
6495 		return PTR_ERR(fltr);
6496 
6497 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6498 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6499 	if (rc)
6500 		bnxt_del_l2_filter(bp, fltr);
6501 	else
6502 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6503 	return rc;
6504 }
6505 
6506 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6507 {
6508 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6509 
6510 	/* Any associated ntuple filters will also be cleared by firmware. */
6511 	for (i = 0; i < num_of_vnics; i++) {
6512 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6513 
6514 		for (j = 0; j < vnic->uc_filter_count; j++) {
6515 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6516 
6517 			bnxt_hwrm_l2_filter_free(bp, fltr);
6518 			bnxt_del_l2_filter(bp, fltr);
6519 		}
6520 		vnic->uc_filter_count = 0;
6521 	}
6522 }
6523 
6524 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6525 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6526 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6527 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6528 
6529 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6530 					   struct hwrm_vnic_tpa_cfg_input *req)
6531 {
6532 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6533 
6534 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6535 		return;
6536 
6537 	if (bp->vxlan_port)
6538 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6539 	if (bp->vxlan_gpe_port)
6540 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6541 	if (bp->nge_port)
6542 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6543 
6544 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6545 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6546 }
6547 
6548 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6549 			   u32 tpa_flags)
6550 {
6551 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6552 	struct hwrm_vnic_tpa_cfg_input *req;
6553 	int rc;
6554 
6555 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6556 		return 0;
6557 
6558 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6559 	if (rc)
6560 		return rc;
6561 
6562 	if (tpa_flags) {
6563 		u16 mss = bp->dev->mtu - 40;
6564 		u32 nsegs, n, segs = 0, flags;
6565 
6566 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6567 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6568 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6569 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6570 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6571 		if (tpa_flags & BNXT_FLAG_GRO)
6572 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6573 
6574 		req->flags = cpu_to_le32(flags);
6575 
6576 		req->enables =
6577 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6578 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6579 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6580 
6581 		/* Number of segs are log2 units, and first packet is not
6582 		 * included as part of this units.
6583 		 */
6584 		if (mss <= BNXT_RX_PAGE_SIZE) {
6585 			n = BNXT_RX_PAGE_SIZE / mss;
6586 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6587 		} else {
6588 			n = mss / BNXT_RX_PAGE_SIZE;
6589 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6590 				n++;
6591 			nsegs = (MAX_SKB_FRAGS - n) / n;
6592 		}
6593 
6594 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6595 			segs = MAX_TPA_SEGS_P5;
6596 			max_aggs = bp->max_tpa;
6597 		} else {
6598 			segs = ilog2(nsegs);
6599 		}
6600 		req->max_agg_segs = cpu_to_le16(segs);
6601 		req->max_aggs = cpu_to_le16(max_aggs);
6602 
6603 		req->min_agg_len = cpu_to_le32(512);
6604 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6605 	}
6606 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6607 
6608 	return hwrm_req_send(bp, req);
6609 }
6610 
6611 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6612 {
6613 	struct bnxt_ring_grp_info *grp_info;
6614 
6615 	grp_info = &bp->grp_info[ring->grp_idx];
6616 	return grp_info->cp_fw_ring_id;
6617 }
6618 
6619 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6620 {
6621 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6622 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6623 	else
6624 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6625 }
6626 
6627 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6628 {
6629 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6630 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6631 	else
6632 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6633 }
6634 
6635 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6636 {
6637 	int entries;
6638 
6639 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6640 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6641 	else
6642 		entries = HW_HASH_INDEX_SIZE;
6643 
6644 	bp->rss_indir_tbl_entries = entries;
6645 	bp->rss_indir_tbl =
6646 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6647 	if (!bp->rss_indir_tbl)
6648 		return -ENOMEM;
6649 
6650 	return 0;
6651 }
6652 
6653 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6654 				 struct ethtool_rxfh_context *rss_ctx)
6655 {
6656 	u16 max_rings, max_entries, pad, i;
6657 	u32 *rss_indir_tbl;
6658 
6659 	if (!bp->rx_nr_rings)
6660 		return;
6661 
6662 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6663 		max_rings = bp->rx_nr_rings - 1;
6664 	else
6665 		max_rings = bp->rx_nr_rings;
6666 
6667 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6668 	if (rss_ctx)
6669 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6670 	else
6671 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6672 
6673 	for (i = 0; i < max_entries; i++)
6674 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6675 
6676 	pad = bp->rss_indir_tbl_entries - max_entries;
6677 	if (pad)
6678 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6679 }
6680 
6681 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6682 {
6683 	u32 i, tbl_size, max_ring = 0;
6684 
6685 	if (!bp->rss_indir_tbl)
6686 		return 0;
6687 
6688 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6689 	for (i = 0; i < tbl_size; i++)
6690 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6691 	return max_ring;
6692 }
6693 
6694 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6695 {
6696 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6697 		if (!rx_rings)
6698 			return 0;
6699 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6700 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6701 
6702 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6703 					       BNXT_RSS_TABLE_ENTRIES_P5);
6704 	}
6705 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6706 		return 2;
6707 	return 1;
6708 }
6709 
6710 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6711 {
6712 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6713 	u16 i, j, min_j = bp->rx_nr_rings - 1;
6714 
6715 	if (!vnic->rss_table)
6716 		goto skip_rss_tbl;
6717 
6718 	/* Fill the RSS indirection table with ring group ids */
6719 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6720 		if (!no_rss)
6721 			j = bp->rss_indir_tbl[i];
6722 		min_j = min(j, min_j);
6723 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6724 	}
6725 
6726 skip_rss_tbl:
6727 	if (vnic->rss_table && !no_rss)
6728 		vnic->default_rx_ring = min_j;
6729 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6730 		vnic->default_rx_ring = vnic->vnic_id - 1;
6731 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6732 		vnic->default_rx_ring = bp->rx_nr_rings - 1;
6733 	else
6734 		vnic->default_rx_ring = 0;
6735 }
6736 
6737 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6738 				    struct bnxt_vnic_info *vnic)
6739 {
6740 	u16 tbl_size, i, min_j = bp->rx_nr_rings - 1;
6741 	__le16 *ring_tbl = vnic->rss_table;
6742 	struct bnxt_rx_ring_info *rxr;
6743 
6744 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6745 
6746 	for (i = 0; i < tbl_size; i++) {
6747 		u16 ring_id, j;
6748 
6749 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6750 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6751 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6752 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6753 		else
6754 			j = bp->rss_indir_tbl[i];
6755 		min_j = min(j, min_j);
6756 		rxr = &bp->rx_ring[j];
6757 
6758 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6759 		*ring_tbl++ = cpu_to_le16(ring_id);
6760 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6761 		*ring_tbl++ = cpu_to_le16(ring_id);
6762 	}
6763 	vnic->default_rx_ring = min_j;
6764 }
6765 
6766 static void
6767 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6768 			 struct bnxt_vnic_info *vnic)
6769 {
6770 	if (bp->flags & BNXT_FLAG_CHIP_P7)
6771 		req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6772 
6773 	if (bp->rss_hash_delta) {
6774 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6775 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6776 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6777 		else
6778 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6779 	} else {
6780 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6781 	}
6782 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6783 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6784 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6785 }
6786 
6787 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6788 				  bool set_rss)
6789 {
6790 	struct hwrm_vnic_rss_cfg_input *req;
6791 	int rc;
6792 
6793 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6794 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6795 		return 0;
6796 
6797 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6798 	if (rc)
6799 		return rc;
6800 
6801 	if (set_rss)
6802 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6803 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6804 	return hwrm_req_send(bp, req);
6805 }
6806 
6807 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6808 				     struct bnxt_vnic_info *vnic, bool set_rss)
6809 {
6810 	struct hwrm_vnic_rss_cfg_input *req;
6811 	dma_addr_t ring_tbl_map;
6812 	u32 i, nr_ctxs;
6813 	int rc;
6814 
6815 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6816 	if (rc)
6817 		return rc;
6818 
6819 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6820 	if (!set_rss)
6821 		return hwrm_req_send(bp, req);
6822 
6823 	bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6824 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6825 	ring_tbl_map = vnic->rss_table_dma_addr;
6826 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6827 
6828 	hwrm_req_hold(bp, req);
6829 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6830 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6831 		req->ring_table_pair_index = i;
6832 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6833 		rc = hwrm_req_send(bp, req);
6834 		if (rc)
6835 			goto exit;
6836 	}
6837 
6838 exit:
6839 	hwrm_req_drop(bp, req);
6840 	return rc;
6841 }
6842 
6843 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6844 {
6845 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6846 	struct hwrm_vnic_rss_qcfg_output *resp;
6847 	struct hwrm_vnic_rss_qcfg_input *req;
6848 
6849 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6850 		return;
6851 
6852 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6853 	/* all contexts configured to same hash_type, zero always exists */
6854 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6855 	resp = hwrm_req_hold(bp, req);
6856 	if (!hwrm_req_send(bp, req)) {
6857 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6858 		bp->rss_hash_delta = 0;
6859 	}
6860 	hwrm_req_drop(bp, req);
6861 }
6862 
6863 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6864 {
6865 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6866 	struct hwrm_vnic_plcmodes_cfg_input *req;
6867 	int rc;
6868 
6869 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6870 	if (rc)
6871 		return rc;
6872 
6873 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6874 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6875 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6876 
6877 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6878 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6879 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6880 		req->enables |=
6881 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6882 		req->hds_threshold = cpu_to_le16(hds_thresh);
6883 	}
6884 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6885 	return hwrm_req_send(bp, req);
6886 }
6887 
6888 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6889 					struct bnxt_vnic_info *vnic,
6890 					u16 ctx_idx)
6891 {
6892 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6893 
6894 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6895 		return;
6896 
6897 	req->rss_cos_lb_ctx_id =
6898 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6899 
6900 	hwrm_req_send(bp, req);
6901 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6902 }
6903 
6904 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6905 {
6906 	int i, j;
6907 
6908 	for (i = 0; i < bp->nr_vnics; i++) {
6909 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6910 
6911 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6912 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6913 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6914 		}
6915 	}
6916 	bp->rsscos_nr_ctxs = 0;
6917 }
6918 
6919 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6920 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6921 {
6922 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6923 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6924 	int rc;
6925 
6926 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6927 	if (rc)
6928 		return rc;
6929 
6930 	resp = hwrm_req_hold(bp, req);
6931 	rc = hwrm_req_send(bp, req);
6932 	if (!rc)
6933 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6934 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6935 	hwrm_req_drop(bp, req);
6936 
6937 	return rc;
6938 }
6939 
6940 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6941 {
6942 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6943 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6944 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6945 }
6946 
6947 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6948 {
6949 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6950 	struct hwrm_vnic_cfg_input *req;
6951 	unsigned int ring = 0, grp_idx;
6952 	u16 def_vlan = 0;
6953 	int rc;
6954 
6955 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6956 	if (rc)
6957 		return rc;
6958 
6959 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6960 		struct bnxt_rx_ring_info *rxr;
6961 
6962 		rxr = &bp->rx_ring[vnic->default_rx_ring];
6963 		req->default_rx_ring_id =
6964 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6965 		req->default_cmpl_ring_id =
6966 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6967 		req->enables =
6968 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6969 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6970 		goto vnic_mru;
6971 	}
6972 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6973 	/* Only RSS support for now TBD: COS & LB */
6974 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6975 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6976 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6977 					   VNIC_CFG_REQ_ENABLES_MRU);
6978 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6979 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6980 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6981 					   VNIC_CFG_REQ_ENABLES_MRU);
6982 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6983 	} else {
6984 		req->rss_rule = cpu_to_le16(0xffff);
6985 	}
6986 
6987 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6988 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6989 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6990 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6991 	} else {
6992 		req->cos_rule = cpu_to_le16(0xffff);
6993 	}
6994 
6995 	ring = vnic->default_rx_ring;
6996 	grp_idx = bp->rx_ring[ring].bnapi->index;
6997 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6998 	req->lb_rule = cpu_to_le16(0xffff);
6999 vnic_mru:
7000 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
7001 	req->mru = cpu_to_le16(vnic->mru);
7002 
7003 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
7004 #ifdef CONFIG_BNXT_SRIOV
7005 	if (BNXT_VF(bp))
7006 		def_vlan = bp->vf.vlan;
7007 #endif
7008 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
7009 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
7010 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT &&
7011 	    bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA]))
7012 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
7013 
7014 	return hwrm_req_send(bp, req);
7015 }
7016 
7017 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
7018 				    struct bnxt_vnic_info *vnic)
7019 {
7020 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
7021 		struct hwrm_vnic_free_input *req;
7022 
7023 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
7024 			return;
7025 
7026 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
7027 
7028 		hwrm_req_send(bp, req);
7029 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
7030 	}
7031 }
7032 
7033 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
7034 {
7035 	u16 i;
7036 
7037 	for (i = 0; i < bp->nr_vnics; i++)
7038 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
7039 }
7040 
7041 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
7042 			 unsigned int start_rx_ring_idx,
7043 			 unsigned int nr_rings)
7044 {
7045 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
7046 	struct hwrm_vnic_alloc_output *resp;
7047 	struct hwrm_vnic_alloc_input *req;
7048 	int rc;
7049 
7050 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
7051 	if (rc)
7052 		return rc;
7053 
7054 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7055 		goto vnic_no_ring_grps;
7056 
7057 	/* map ring groups to this vnic */
7058 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
7059 		grp_idx = bp->rx_ring[i].bnapi->index;
7060 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
7061 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
7062 				   j, nr_rings);
7063 			break;
7064 		}
7065 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
7066 	}
7067 
7068 vnic_no_ring_grps:
7069 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
7070 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
7071 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
7072 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
7073 
7074 	resp = hwrm_req_hold(bp, req);
7075 	rc = hwrm_req_send(bp, req);
7076 	if (!rc)
7077 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
7078 	hwrm_req_drop(bp, req);
7079 	return rc;
7080 }
7081 
7082 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
7083 {
7084 	struct hwrm_vnic_qcaps_output *resp;
7085 	struct hwrm_vnic_qcaps_input *req;
7086 	int rc;
7087 
7088 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
7089 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
7090 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
7091 	if (bp->hwrm_spec_code < 0x10600)
7092 		return 0;
7093 
7094 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
7095 	if (rc)
7096 		return rc;
7097 
7098 	resp = hwrm_req_hold(bp, req);
7099 	rc = hwrm_req_send(bp, req);
7100 	if (!rc) {
7101 		u32 flags = le32_to_cpu(resp->flags);
7102 
7103 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
7104 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
7105 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
7106 		if (flags &
7107 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
7108 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7109 
7110 		/* Older P5 fw before EXT_HW_STATS support did not set
7111 		 * VLAN_STRIP_CAP properly.
7112 		 */
7113 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7114 		    (BNXT_CHIP_P5(bp) &&
7115 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7116 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7117 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7118 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7119 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7120 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7121 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7122 		if (bp->max_tpa_v2) {
7123 			if (BNXT_CHIP_P5(bp))
7124 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7125 			else
7126 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7127 		}
7128 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7129 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7130 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7131 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7132 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7133 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7134 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7135 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7136 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7137 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7138 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7139 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7140 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7141 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7142 	}
7143 	hwrm_req_drop(bp, req);
7144 	return rc;
7145 }
7146 
7147 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7148 {
7149 	struct hwrm_ring_grp_alloc_output *resp;
7150 	struct hwrm_ring_grp_alloc_input *req;
7151 	int rc;
7152 	u16 i;
7153 
7154 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7155 		return 0;
7156 
7157 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7158 	if (rc)
7159 		return rc;
7160 
7161 	resp = hwrm_req_hold(bp, req);
7162 	for (i = 0; i < bp->rx_nr_rings; i++) {
7163 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7164 
7165 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7166 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7167 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7168 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7169 
7170 		rc = hwrm_req_send(bp, req);
7171 
7172 		if (rc)
7173 			break;
7174 
7175 		bp->grp_info[grp_idx].fw_grp_id =
7176 			le32_to_cpu(resp->ring_group_id);
7177 	}
7178 	hwrm_req_drop(bp, req);
7179 	return rc;
7180 }
7181 
7182 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7183 {
7184 	struct hwrm_ring_grp_free_input *req;
7185 	u16 i;
7186 
7187 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7188 		return;
7189 
7190 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7191 		return;
7192 
7193 	hwrm_req_hold(bp, req);
7194 	for (i = 0; i < bp->cp_nr_rings; i++) {
7195 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7196 			continue;
7197 		req->ring_group_id =
7198 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7199 
7200 		hwrm_req_send(bp, req);
7201 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7202 	}
7203 	hwrm_req_drop(bp, req);
7204 }
7205 
7206 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7207 				       struct hwrm_ring_alloc_input *req,
7208 				       struct bnxt_rx_ring_info *rxr,
7209 				       struct bnxt_ring_struct *ring)
7210 {
7211 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7212 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7213 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7214 
7215 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7216 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7217 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7218 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7219 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7220 	} else {
7221 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7222 		if (NET_IP_ALIGN == 2)
7223 			req->flags =
7224 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7225 	}
7226 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7227 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7228 	req->enables |= cpu_to_le32(enables);
7229 }
7230 
7231 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7232 				    struct bnxt_rx_ring_info *rxr,
7233 				    struct bnxt_ring_struct *ring,
7234 				    u32 ring_type, u32 map_index)
7235 {
7236 	struct hwrm_ring_alloc_output *resp;
7237 	struct hwrm_ring_alloc_input *req;
7238 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7239 	struct bnxt_ring_grp_info *grp_info;
7240 	int rc, err = 0;
7241 	u16 ring_id;
7242 
7243 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7244 	if (rc)
7245 		goto exit;
7246 
7247 	req->enables = 0;
7248 	if (rmem->nr_pages > 1) {
7249 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7250 		/* Page size is in log2 units */
7251 		req->page_size = BNXT_PAGE_SHIFT;
7252 		req->page_tbl_depth = 1;
7253 	} else {
7254 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7255 	}
7256 	req->fbo = 0;
7257 	/* Association of ring index with doorbell index and MSIX number */
7258 	req->logical_id = cpu_to_le16(map_index);
7259 
7260 	switch (ring_type) {
7261 	case HWRM_RING_ALLOC_TX: {
7262 		struct bnxt_tx_ring_info *txr;
7263 		u16 flags = 0;
7264 
7265 		txr = container_of(ring, struct bnxt_tx_ring_info,
7266 				   tx_ring_struct);
7267 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7268 		/* Association of transmit ring with completion ring */
7269 		grp_info = &bp->grp_info[ring->grp_idx];
7270 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7271 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7272 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7273 		req->queue_id = cpu_to_le16(ring->queue_id);
7274 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7275 			req->cmpl_coal_cnt =
7276 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7277 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7278 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7279 		req->flags = cpu_to_le16(flags);
7280 		break;
7281 	}
7282 	case HWRM_RING_ALLOC_RX:
7283 	case HWRM_RING_ALLOC_AGG:
7284 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7285 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7286 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7287 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7288 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7289 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7290 						   rxr, ring);
7291 		break;
7292 	case HWRM_RING_ALLOC_CMPL:
7293 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7294 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7295 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7296 			/* Association of cp ring with nq */
7297 			grp_info = &bp->grp_info[map_index];
7298 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7299 			req->cq_handle = cpu_to_le64(ring->handle);
7300 			req->enables |= cpu_to_le32(
7301 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7302 		} else {
7303 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7304 		}
7305 		break;
7306 	case HWRM_RING_ALLOC_NQ:
7307 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7308 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7309 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7310 		break;
7311 	default:
7312 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7313 			   ring_type);
7314 		return -EINVAL;
7315 	}
7316 
7317 	resp = hwrm_req_hold(bp, req);
7318 	rc = hwrm_req_send(bp, req);
7319 	err = le16_to_cpu(resp->error_code);
7320 	ring_id = le16_to_cpu(resp->ring_id);
7321 	hwrm_req_drop(bp, req);
7322 
7323 exit:
7324 	if (rc || err) {
7325 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7326 			   ring_type, rc, err);
7327 		return -EIO;
7328 	}
7329 	ring->fw_ring_id = ring_id;
7330 	return rc;
7331 }
7332 
7333 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7334 {
7335 	int rc;
7336 
7337 	if (BNXT_PF(bp)) {
7338 		struct hwrm_func_cfg_input *req;
7339 
7340 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7341 		if (rc)
7342 			return rc;
7343 
7344 		req->fid = cpu_to_le16(0xffff);
7345 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7346 		req->async_event_cr = cpu_to_le16(idx);
7347 		return hwrm_req_send(bp, req);
7348 	} else {
7349 		struct hwrm_func_vf_cfg_input *req;
7350 
7351 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7352 		if (rc)
7353 			return rc;
7354 
7355 		req->enables =
7356 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7357 		req->async_event_cr = cpu_to_le16(idx);
7358 		return hwrm_req_send(bp, req);
7359 	}
7360 }
7361 
7362 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7363 			     u32 ring_type)
7364 {
7365 	switch (ring_type) {
7366 	case HWRM_RING_ALLOC_TX:
7367 		db->db_ring_mask = bp->tx_ring_mask;
7368 		break;
7369 	case HWRM_RING_ALLOC_RX:
7370 		db->db_ring_mask = bp->rx_ring_mask;
7371 		break;
7372 	case HWRM_RING_ALLOC_AGG:
7373 		db->db_ring_mask = bp->rx_agg_ring_mask;
7374 		break;
7375 	case HWRM_RING_ALLOC_CMPL:
7376 	case HWRM_RING_ALLOC_NQ:
7377 		db->db_ring_mask = bp->cp_ring_mask;
7378 		break;
7379 	}
7380 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7381 		db->db_epoch_mask = db->db_ring_mask + 1;
7382 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7383 	}
7384 }
7385 
7386 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7387 			u32 map_idx, u32 xid)
7388 {
7389 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7390 		switch (ring_type) {
7391 		case HWRM_RING_ALLOC_TX:
7392 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7393 			break;
7394 		case HWRM_RING_ALLOC_RX:
7395 		case HWRM_RING_ALLOC_AGG:
7396 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7397 			break;
7398 		case HWRM_RING_ALLOC_CMPL:
7399 			db->db_key64 = DBR_PATH_L2;
7400 			break;
7401 		case HWRM_RING_ALLOC_NQ:
7402 			db->db_key64 = DBR_PATH_L2;
7403 			break;
7404 		}
7405 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7406 
7407 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7408 			db->db_key64 |= DBR_VALID;
7409 
7410 		db->doorbell = bp->bar1 + bp->db_offset;
7411 	} else {
7412 		db->doorbell = bp->bar1 + map_idx * 0x80;
7413 		switch (ring_type) {
7414 		case HWRM_RING_ALLOC_TX:
7415 			db->db_key32 = DB_KEY_TX;
7416 			break;
7417 		case HWRM_RING_ALLOC_RX:
7418 		case HWRM_RING_ALLOC_AGG:
7419 			db->db_key32 = DB_KEY_RX;
7420 			break;
7421 		case HWRM_RING_ALLOC_CMPL:
7422 			db->db_key32 = DB_KEY_CP;
7423 			break;
7424 		}
7425 	}
7426 	bnxt_set_db_mask(bp, db, ring_type);
7427 }
7428 
7429 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7430 				   struct bnxt_rx_ring_info *rxr)
7431 {
7432 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7433 	struct bnxt_napi *bnapi = rxr->bnapi;
7434 	u32 type = HWRM_RING_ALLOC_RX;
7435 	u32 map_idx = bnapi->index;
7436 	int rc;
7437 
7438 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7439 	if (rc)
7440 		return rc;
7441 
7442 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7443 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7444 
7445 	return 0;
7446 }
7447 
7448 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7449 				       struct bnxt_rx_ring_info *rxr)
7450 {
7451 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7452 	u32 type = HWRM_RING_ALLOC_AGG;
7453 	u32 grp_idx = ring->grp_idx;
7454 	u32 map_idx;
7455 	int rc;
7456 
7457 	map_idx = grp_idx + bp->rx_nr_rings;
7458 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7459 	if (rc)
7460 		return rc;
7461 
7462 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7463 		    ring->fw_ring_id);
7464 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7465 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7466 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7467 
7468 	return 0;
7469 }
7470 
7471 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7472 				      struct bnxt_cp_ring_info *cpr)
7473 {
7474 	const u32 type = HWRM_RING_ALLOC_CMPL;
7475 	struct bnxt_napi *bnapi = cpr->bnapi;
7476 	struct bnxt_ring_struct *ring;
7477 	u32 map_idx = bnapi->index;
7478 	int rc;
7479 
7480 	ring = &cpr->cp_ring_struct;
7481 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7482 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7483 	if (rc)
7484 		return rc;
7485 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7486 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7487 	return 0;
7488 }
7489 
7490 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7491 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7492 {
7493 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7494 	const u32 type = HWRM_RING_ALLOC_TX;
7495 	int rc;
7496 
7497 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7498 	if (rc)
7499 		return rc;
7500 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7501 	return 0;
7502 }
7503 
7504 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7505 {
7506 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7507 	int i, rc = 0;
7508 	u32 type;
7509 
7510 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7511 		type = HWRM_RING_ALLOC_NQ;
7512 	else
7513 		type = HWRM_RING_ALLOC_CMPL;
7514 	for (i = 0; i < bp->cp_nr_rings; i++) {
7515 		struct bnxt_napi *bnapi = bp->bnapi[i];
7516 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7517 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7518 		u32 map_idx = ring->map_idx;
7519 		unsigned int vector;
7520 
7521 		vector = bp->irq_tbl[map_idx].vector;
7522 		disable_irq_nosync(vector);
7523 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7524 		if (rc) {
7525 			enable_irq(vector);
7526 			goto err_out;
7527 		}
7528 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7529 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7530 		enable_irq(vector);
7531 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7532 
7533 		if (!i) {
7534 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7535 			if (rc)
7536 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7537 		}
7538 	}
7539 
7540 	for (i = 0; i < bp->tx_nr_rings; i++) {
7541 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7542 
7543 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7544 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7545 			if (rc)
7546 				goto err_out;
7547 		}
7548 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7549 		if (rc)
7550 			goto err_out;
7551 	}
7552 
7553 	for (i = 0; i < bp->rx_nr_rings; i++) {
7554 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7555 
7556 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7557 		if (rc)
7558 			goto err_out;
7559 		/* If we have agg rings, post agg buffers first. */
7560 		if (!agg_rings)
7561 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7562 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7563 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7564 			if (rc)
7565 				goto err_out;
7566 		}
7567 	}
7568 
7569 	if (agg_rings) {
7570 		for (i = 0; i < bp->rx_nr_rings; i++) {
7571 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7572 			if (rc)
7573 				goto err_out;
7574 		}
7575 	}
7576 err_out:
7577 	return rc;
7578 }
7579 
7580 static void bnxt_cancel_dim(struct bnxt *bp)
7581 {
7582 	int i;
7583 
7584 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7585 	 * if NAPI is enabled.
7586 	 */
7587 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7588 		return;
7589 
7590 	/* Make sure NAPI sees that the VNIC is disabled */
7591 	synchronize_net();
7592 	for (i = 0; i < bp->rx_nr_rings; i++) {
7593 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7594 		struct bnxt_napi *bnapi = rxr->bnapi;
7595 
7596 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7597 	}
7598 }
7599 
7600 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7601 				   struct bnxt_ring_struct *ring,
7602 				   u32 ring_type, int cmpl_ring_id)
7603 {
7604 	struct hwrm_ring_free_output *resp;
7605 	struct hwrm_ring_free_input *req;
7606 	u16 error_code = 0;
7607 	int rc;
7608 
7609 	if (BNXT_NO_FW_ACCESS(bp))
7610 		return 0;
7611 
7612 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7613 	if (rc)
7614 		goto exit;
7615 
7616 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7617 	req->ring_type = ring_type;
7618 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7619 
7620 	resp = hwrm_req_hold(bp, req);
7621 	rc = hwrm_req_send(bp, req);
7622 	error_code = le16_to_cpu(resp->error_code);
7623 	hwrm_req_drop(bp, req);
7624 exit:
7625 	if (rc || error_code) {
7626 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7627 			   ring_type, rc, error_code);
7628 		return -EIO;
7629 	}
7630 	return 0;
7631 }
7632 
7633 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7634 				   struct bnxt_tx_ring_info *txr,
7635 				   bool close_path)
7636 {
7637 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7638 	u32 cmpl_ring_id;
7639 
7640 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7641 		return;
7642 
7643 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7644 		       INVALID_HW_RING_ID;
7645 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7646 				cmpl_ring_id);
7647 	ring->fw_ring_id = INVALID_HW_RING_ID;
7648 }
7649 
7650 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7651 				   struct bnxt_rx_ring_info *rxr,
7652 				   bool close_path)
7653 {
7654 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7655 	u32 grp_idx = rxr->bnapi->index;
7656 	u32 cmpl_ring_id;
7657 
7658 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7659 		return;
7660 
7661 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7662 	hwrm_ring_free_send_msg(bp, ring,
7663 				RING_FREE_REQ_RING_TYPE_RX,
7664 				close_path ? cmpl_ring_id :
7665 				INVALID_HW_RING_ID);
7666 	ring->fw_ring_id = INVALID_HW_RING_ID;
7667 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7668 }
7669 
7670 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7671 				       struct bnxt_rx_ring_info *rxr,
7672 				       bool close_path)
7673 {
7674 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7675 	u32 grp_idx = rxr->bnapi->index;
7676 	u32 type, cmpl_ring_id;
7677 
7678 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7679 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7680 	else
7681 		type = RING_FREE_REQ_RING_TYPE_RX;
7682 
7683 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7684 		return;
7685 
7686 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7687 	hwrm_ring_free_send_msg(bp, ring, type,
7688 				close_path ? cmpl_ring_id :
7689 				INVALID_HW_RING_ID);
7690 	ring->fw_ring_id = INVALID_HW_RING_ID;
7691 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7692 }
7693 
7694 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7695 				   struct bnxt_cp_ring_info *cpr)
7696 {
7697 	struct bnxt_ring_struct *ring;
7698 
7699 	ring = &cpr->cp_ring_struct;
7700 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7701 		return;
7702 
7703 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7704 				INVALID_HW_RING_ID);
7705 	ring->fw_ring_id = INVALID_HW_RING_ID;
7706 }
7707 
7708 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7709 {
7710 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7711 	int i, size = ring->ring_mem.page_size;
7712 
7713 	cpr->cp_raw_cons = 0;
7714 	cpr->toggle = 0;
7715 
7716 	for (i = 0; i < bp->cp_nr_pages; i++)
7717 		if (cpr->cp_desc_ring[i])
7718 			memset(cpr->cp_desc_ring[i], 0, size);
7719 }
7720 
7721 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7722 {
7723 	u32 type;
7724 	int i;
7725 
7726 	if (!bp->bnapi)
7727 		return;
7728 
7729 	for (i = 0; i < bp->tx_nr_rings; i++)
7730 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7731 
7732 	bnxt_cancel_dim(bp);
7733 	for (i = 0; i < bp->rx_nr_rings; i++) {
7734 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7735 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7736 	}
7737 
7738 	/* The completion rings are about to be freed.  After that the
7739 	 * IRQ doorbell will not work anymore.  So we need to disable
7740 	 * IRQ here.
7741 	 */
7742 	bnxt_disable_int_sync(bp);
7743 
7744 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7745 		type = RING_FREE_REQ_RING_TYPE_NQ;
7746 	else
7747 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7748 	for (i = 0; i < bp->cp_nr_rings; i++) {
7749 		struct bnxt_napi *bnapi = bp->bnapi[i];
7750 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7751 		struct bnxt_ring_struct *ring;
7752 		int j;
7753 
7754 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7755 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7756 
7757 		ring = &cpr->cp_ring_struct;
7758 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7759 			hwrm_ring_free_send_msg(bp, ring, type,
7760 						INVALID_HW_RING_ID);
7761 			ring->fw_ring_id = INVALID_HW_RING_ID;
7762 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7763 		}
7764 	}
7765 }
7766 
7767 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7768 			     bool shared);
7769 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7770 			   bool shared);
7771 
7772 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7773 {
7774 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7775 	struct hwrm_func_qcfg_output *resp;
7776 	struct hwrm_func_qcfg_input *req;
7777 	int rc;
7778 
7779 	if (bp->hwrm_spec_code < 0x10601)
7780 		return 0;
7781 
7782 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7783 	if (rc)
7784 		return rc;
7785 
7786 	req->fid = cpu_to_le16(0xffff);
7787 	resp = hwrm_req_hold(bp, req);
7788 	rc = hwrm_req_send(bp, req);
7789 	if (rc) {
7790 		hwrm_req_drop(bp, req);
7791 		return rc;
7792 	}
7793 
7794 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7795 	if (BNXT_NEW_RM(bp)) {
7796 		u16 cp, stats;
7797 
7798 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7799 		hw_resc->resv_hw_ring_grps =
7800 			le32_to_cpu(resp->alloc_hw_ring_grps);
7801 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7802 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7803 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7804 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7805 		hw_resc->resv_irqs = cp;
7806 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7807 			int rx = hw_resc->resv_rx_rings;
7808 			int tx = hw_resc->resv_tx_rings;
7809 
7810 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7811 				rx >>= 1;
7812 			if (cp < (rx + tx)) {
7813 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7814 				if (rc)
7815 					goto get_rings_exit;
7816 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7817 					rx <<= 1;
7818 				hw_resc->resv_rx_rings = rx;
7819 				hw_resc->resv_tx_rings = tx;
7820 			}
7821 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7822 			hw_resc->resv_hw_ring_grps = rx;
7823 		}
7824 		hw_resc->resv_cp_rings = cp;
7825 		hw_resc->resv_stat_ctxs = stats;
7826 	}
7827 get_rings_exit:
7828 	hwrm_req_drop(bp, req);
7829 	return rc;
7830 }
7831 
7832 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7833 {
7834 	struct hwrm_func_qcfg_output *resp;
7835 	struct hwrm_func_qcfg_input *req;
7836 	int rc;
7837 
7838 	if (bp->hwrm_spec_code < 0x10601)
7839 		return 0;
7840 
7841 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7842 	if (rc)
7843 		return rc;
7844 
7845 	req->fid = cpu_to_le16(fid);
7846 	resp = hwrm_req_hold(bp, req);
7847 	rc = hwrm_req_send(bp, req);
7848 	if (!rc)
7849 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7850 
7851 	hwrm_req_drop(bp, req);
7852 	return rc;
7853 }
7854 
7855 static bool bnxt_rfs_supported(struct bnxt *bp);
7856 
7857 static struct hwrm_func_cfg_input *
7858 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7859 {
7860 	struct hwrm_func_cfg_input *req;
7861 	u32 enables = 0;
7862 
7863 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7864 		return NULL;
7865 
7866 	req->fid = cpu_to_le16(0xffff);
7867 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7868 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7869 	if (BNXT_NEW_RM(bp)) {
7870 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7871 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7872 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7873 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7874 			enables |= hwr->cp_p5 ?
7875 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7876 		} else {
7877 			enables |= hwr->cp ?
7878 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7879 			enables |= hwr->grp ?
7880 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7881 		}
7882 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7883 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7884 					  0;
7885 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7886 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7887 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7888 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7889 			req->num_msix = cpu_to_le16(hwr->cp);
7890 		} else {
7891 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7892 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7893 		}
7894 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7895 		req->num_vnics = cpu_to_le16(hwr->vnic);
7896 	}
7897 	req->enables = cpu_to_le32(enables);
7898 	return req;
7899 }
7900 
7901 static struct hwrm_func_vf_cfg_input *
7902 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7903 {
7904 	struct hwrm_func_vf_cfg_input *req;
7905 	u32 enables = 0;
7906 
7907 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7908 		return NULL;
7909 
7910 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7911 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7912 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7913 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7914 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7915 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7916 		enables |= hwr->cp_p5 ?
7917 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7918 	} else {
7919 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7920 		enables |= hwr->grp ?
7921 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7922 	}
7923 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7924 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7925 
7926 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7927 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7928 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7929 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7930 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7931 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7932 	} else {
7933 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7934 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7935 	}
7936 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7937 	req->num_vnics = cpu_to_le16(hwr->vnic);
7938 
7939 	req->enables = cpu_to_le32(enables);
7940 	return req;
7941 }
7942 
7943 static int
7944 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7945 {
7946 	struct hwrm_func_cfg_input *req;
7947 	int rc;
7948 
7949 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7950 	if (!req)
7951 		return -ENOMEM;
7952 
7953 	if (!req->enables) {
7954 		hwrm_req_drop(bp, req);
7955 		return 0;
7956 	}
7957 
7958 	rc = hwrm_req_send(bp, req);
7959 	if (rc)
7960 		return rc;
7961 
7962 	if (bp->hwrm_spec_code < 0x10601)
7963 		bp->hw_resc.resv_tx_rings = hwr->tx;
7964 
7965 	return bnxt_hwrm_get_rings(bp);
7966 }
7967 
7968 static int
7969 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7970 {
7971 	struct hwrm_func_vf_cfg_input *req;
7972 	int rc;
7973 
7974 	if (!BNXT_NEW_RM(bp)) {
7975 		bp->hw_resc.resv_tx_rings = hwr->tx;
7976 		return 0;
7977 	}
7978 
7979 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7980 	if (!req)
7981 		return -ENOMEM;
7982 
7983 	rc = hwrm_req_send(bp, req);
7984 	if (rc)
7985 		return rc;
7986 
7987 	return bnxt_hwrm_get_rings(bp);
7988 }
7989 
7990 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7991 {
7992 	if (BNXT_PF(bp))
7993 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7994 	else
7995 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7996 }
7997 
7998 int bnxt_nq_rings_in_use(struct bnxt *bp)
7999 {
8000 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
8001 }
8002 
8003 static int bnxt_cp_rings_in_use(struct bnxt *bp)
8004 {
8005 	int cp;
8006 
8007 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8008 		return bnxt_nq_rings_in_use(bp);
8009 
8010 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
8011 	return cp;
8012 }
8013 
8014 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
8015 {
8016 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
8017 }
8018 
8019 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8020 {
8021 	if (!hwr->grp)
8022 		return 0;
8023 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8024 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
8025 
8026 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8027 			rss_ctx *= hwr->vnic;
8028 		return rss_ctx;
8029 	}
8030 	if (BNXT_VF(bp))
8031 		return BNXT_VF_MAX_RSS_CTX;
8032 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
8033 		return hwr->grp + 1;
8034 	return 1;
8035 }
8036 
8037 /* Check if a default RSS map needs to be setup.  This function is only
8038  * used on older firmware that does not require reserving RX rings.
8039  */
8040 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
8041 {
8042 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8043 
8044 	/* The RSS map is valid for RX rings set to resv_rx_rings */
8045 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
8046 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
8047 		if (!netif_is_rxfh_configured(bp->dev))
8048 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8049 	}
8050 }
8051 
8052 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
8053 {
8054 	if (bp->flags & BNXT_FLAG_RFS) {
8055 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8056 			return 2 + bp->num_rss_ctx;
8057 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8058 			return rx_rings + 1;
8059 	}
8060 	return 1;
8061 }
8062 
8063 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8064 {
8065 	hwr->cp = bnxt_nq_rings_in_use(bp);
8066 	hwr->cp_p5 = 0;
8067 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8068 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
8069 	hwr->tx = bp->tx_nr_rings;
8070 	hwr->rx = bp->rx_nr_rings;
8071 	hwr->grp = hwr->rx;
8072 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
8073 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
8074 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8075 		hwr->rx <<= 1;
8076 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
8077 }
8078 
8079 static bool bnxt_need_reserve_rings(struct bnxt *bp)
8080 {
8081 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8082 	struct bnxt_hw_rings hwr;
8083 
8084 	bnxt_get_total_resources(bp, &hwr);
8085 
8086 	/* Old firmware does not need RX ring reservations but we still
8087 	 * need to setup a default RSS map when needed.  With new firmware
8088 	 * we go through RX ring reservations first and then set up the
8089 	 * RSS map for the successfully reserved RX rings when needed.
8090 	 */
8091 	if (!BNXT_NEW_RM(bp))
8092 		bnxt_check_rss_tbl_no_rmgr(bp);
8093 
8094 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
8095 		return true;
8096 
8097 	if (!BNXT_NEW_RM(bp))
8098 		return false;
8099 
8100 	if (hw_resc->resv_rx_rings != hwr.rx ||
8101 	    hw_resc->resv_vnics != hwr.vnic ||
8102 	    hw_resc->resv_stat_ctxs != hwr.stat ||
8103 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
8104 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
8105 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
8106 		return true;
8107 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8108 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
8109 			return true;
8110 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
8111 		return true;
8112 	}
8113 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
8114 	    hw_resc->resv_irqs != hwr.cp)
8115 		return true;
8116 	return false;
8117 }
8118 
8119 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8120 {
8121 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8122 
8123 	hwr->tx = hw_resc->resv_tx_rings;
8124 	if (BNXT_NEW_RM(bp)) {
8125 		hwr->rx = hw_resc->resv_rx_rings;
8126 		hwr->cp = hw_resc->resv_irqs;
8127 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8128 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8129 		hwr->grp = hw_resc->resv_hw_ring_grps;
8130 		hwr->vnic = hw_resc->resv_vnics;
8131 		hwr->stat = hw_resc->resv_stat_ctxs;
8132 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8133 	}
8134 }
8135 
8136 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8137 {
8138 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8139 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8140 }
8141 
8142 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8143 
8144 static int __bnxt_reserve_rings(struct bnxt *bp)
8145 {
8146 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
8147 	struct bnxt_hw_rings hwr = {0};
8148 	int rx_rings, old_rx_rings, rc;
8149 	int cp = bp->cp_nr_rings;
8150 	int ulp_msix = 0;
8151 	bool sh = false;
8152 	int tx_cp;
8153 
8154 	if (!bnxt_need_reserve_rings(bp))
8155 		return 0;
8156 
8157 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
8158 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8159 		if (!ulp_msix)
8160 			bnxt_set_ulp_stat_ctxs(bp, 0);
8161 		else
8162 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8163 
8164 		if (ulp_msix > bp->ulp_num_msix_want)
8165 			ulp_msix = bp->ulp_num_msix_want;
8166 		hwr.cp = cp + ulp_msix;
8167 	} else {
8168 		hwr.cp = bnxt_nq_rings_in_use(bp);
8169 	}
8170 
8171 	hwr.tx = bp->tx_nr_rings;
8172 	hwr.rx = bp->rx_nr_rings;
8173 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8174 		sh = true;
8175 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8176 		hwr.cp_p5 = hwr.rx + hwr.tx;
8177 
8178 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8179 
8180 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8181 		hwr.rx <<= 1;
8182 	hwr.grp = bp->rx_nr_rings;
8183 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8184 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8185 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8186 
8187 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8188 	if (rc)
8189 		return rc;
8190 
8191 	bnxt_copy_reserved_rings(bp, &hwr);
8192 
8193 	rx_rings = hwr.rx;
8194 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8195 		if (hwr.rx >= 2) {
8196 			rx_rings = hwr.rx >> 1;
8197 		} else {
8198 			if (netif_running(bp->dev))
8199 				return -ENOMEM;
8200 
8201 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8202 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8203 			bp->dev->hw_features &= ~NETIF_F_LRO;
8204 			bp->dev->features &= ~NETIF_F_LRO;
8205 			bnxt_set_ring_params(bp);
8206 		}
8207 	}
8208 	rx_rings = min_t(int, rx_rings, hwr.grp);
8209 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8210 	if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8211 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8212 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8213 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8214 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8215 		hwr.rx = rx_rings << 1;
8216 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8217 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8218 	if (hwr.tx != bp->tx_nr_rings) {
8219 		netdev_warn(bp->dev,
8220 			    "Able to reserve only %d out of %d requested TX rings\n",
8221 			    hwr.tx, bp->tx_nr_rings);
8222 	}
8223 	bp->tx_nr_rings = hwr.tx;
8224 
8225 	/* If we cannot reserve all the RX rings, reset the RSS map only
8226 	 * if absolutely necessary
8227 	 */
8228 	if (rx_rings != bp->rx_nr_rings) {
8229 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8230 			    rx_rings, bp->rx_nr_rings);
8231 		if (netif_is_rxfh_configured(bp->dev) &&
8232 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8233 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8234 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8235 			ethtool_rxfh_indir_lost(bp->dev);
8236 		}
8237 	}
8238 	bp->rx_nr_rings = rx_rings;
8239 	bp->cp_nr_rings = hwr.cp;
8240 
8241 	/* Fall back if we cannot reserve enough HW RSS contexts */
8242 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8243 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8244 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8245 
8246 	if (!bnxt_rings_ok(bp, &hwr))
8247 		return -ENOMEM;
8248 
8249 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8250 	    !netif_is_rxfh_configured(bp->dev))
8251 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8252 
8253 	if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) {
8254 		int resv_msix, resv_ctx, ulp_ctxs;
8255 		struct bnxt_hw_resc *hw_resc;
8256 
8257 		hw_resc = &bp->hw_resc;
8258 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8259 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8260 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8261 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8262 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8263 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8264 	}
8265 
8266 	return rc;
8267 }
8268 
8269 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8270 {
8271 	struct hwrm_func_vf_cfg_input *req;
8272 	u32 flags;
8273 
8274 	if (!BNXT_NEW_RM(bp))
8275 		return 0;
8276 
8277 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8278 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8279 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8280 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8281 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8282 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8283 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8284 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8285 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8286 
8287 	req->flags = cpu_to_le32(flags);
8288 	return hwrm_req_send_silent(bp, req);
8289 }
8290 
8291 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8292 {
8293 	struct hwrm_func_cfg_input *req;
8294 	u32 flags;
8295 
8296 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8297 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8298 	if (BNXT_NEW_RM(bp)) {
8299 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8300 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8301 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8302 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8303 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8304 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8305 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8306 		else
8307 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8308 	}
8309 
8310 	req->flags = cpu_to_le32(flags);
8311 	return hwrm_req_send_silent(bp, req);
8312 }
8313 
8314 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8315 {
8316 	if (bp->hwrm_spec_code < 0x10801)
8317 		return 0;
8318 
8319 	if (BNXT_PF(bp))
8320 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8321 
8322 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8323 }
8324 
8325 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8326 {
8327 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8328 	struct hwrm_ring_aggint_qcaps_output *resp;
8329 	struct hwrm_ring_aggint_qcaps_input *req;
8330 	int rc;
8331 
8332 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8333 	coal_cap->num_cmpl_dma_aggr_max = 63;
8334 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8335 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8336 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8337 	coal_cap->int_lat_tmr_min_max = 65535;
8338 	coal_cap->int_lat_tmr_max_max = 65535;
8339 	coal_cap->num_cmpl_aggr_int_max = 65535;
8340 	coal_cap->timer_units = 80;
8341 
8342 	if (bp->hwrm_spec_code < 0x10902)
8343 		return;
8344 
8345 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8346 		return;
8347 
8348 	resp = hwrm_req_hold(bp, req);
8349 	rc = hwrm_req_send_silent(bp, req);
8350 	if (!rc) {
8351 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8352 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8353 		coal_cap->num_cmpl_dma_aggr_max =
8354 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8355 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8356 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8357 		coal_cap->cmpl_aggr_dma_tmr_max =
8358 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8359 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8360 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8361 		coal_cap->int_lat_tmr_min_max =
8362 			le16_to_cpu(resp->int_lat_tmr_min_max);
8363 		coal_cap->int_lat_tmr_max_max =
8364 			le16_to_cpu(resp->int_lat_tmr_max_max);
8365 		coal_cap->num_cmpl_aggr_int_max =
8366 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8367 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8368 	}
8369 	hwrm_req_drop(bp, req);
8370 }
8371 
8372 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8373 {
8374 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8375 
8376 	return usec * 1000 / coal_cap->timer_units;
8377 }
8378 
8379 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8380 	struct bnxt_coal *hw_coal,
8381 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8382 {
8383 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8384 	u16 val, tmr, max, flags = hw_coal->flags;
8385 	u32 cmpl_params = coal_cap->cmpl_params;
8386 
8387 	max = hw_coal->bufs_per_record * 128;
8388 	if (hw_coal->budget)
8389 		max = hw_coal->bufs_per_record * hw_coal->budget;
8390 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8391 
8392 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8393 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8394 
8395 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8396 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8397 
8398 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8399 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8400 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8401 
8402 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8403 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8404 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8405 
8406 	/* min timer set to 1/2 of interrupt timer */
8407 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8408 		val = tmr / 2;
8409 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8410 		req->int_lat_tmr_min = cpu_to_le16(val);
8411 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8412 	}
8413 
8414 	/* buf timer set to 1/4 of interrupt timer */
8415 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8416 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8417 
8418 	if (cmpl_params &
8419 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8420 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8421 		val = clamp_t(u16, tmr, 1,
8422 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8423 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8424 		req->enables |=
8425 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8426 	}
8427 
8428 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8429 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8430 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8431 	req->flags = cpu_to_le16(flags);
8432 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8433 }
8434 
8435 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8436 				   struct bnxt_coal *hw_coal)
8437 {
8438 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8439 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8440 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8441 	u32 nq_params = coal_cap->nq_params;
8442 	u16 tmr;
8443 	int rc;
8444 
8445 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8446 		return 0;
8447 
8448 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8449 	if (rc)
8450 		return rc;
8451 
8452 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8453 	req->flags =
8454 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8455 
8456 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8457 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8458 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8459 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8460 	return hwrm_req_send(bp, req);
8461 }
8462 
8463 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8464 {
8465 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8466 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8467 	struct bnxt_coal coal;
8468 	int rc;
8469 
8470 	/* Tick values in micro seconds.
8471 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8472 	 */
8473 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8474 
8475 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8476 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8477 
8478 	if (!bnapi->rx_ring)
8479 		return -ENODEV;
8480 
8481 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8482 	if (rc)
8483 		return rc;
8484 
8485 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8486 
8487 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8488 
8489 	return hwrm_req_send(bp, req_rx);
8490 }
8491 
8492 static int
8493 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8494 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8495 {
8496 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8497 
8498 	req->ring_id = cpu_to_le16(ring_id);
8499 	return hwrm_req_send(bp, req);
8500 }
8501 
8502 static int
8503 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8504 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8505 {
8506 	struct bnxt_tx_ring_info *txr;
8507 	int i, rc;
8508 
8509 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8510 		u16 ring_id;
8511 
8512 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8513 		req->ring_id = cpu_to_le16(ring_id);
8514 		rc = hwrm_req_send(bp, req);
8515 		if (rc)
8516 			return rc;
8517 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8518 			return 0;
8519 	}
8520 	return 0;
8521 }
8522 
8523 int bnxt_hwrm_set_coal(struct bnxt *bp)
8524 {
8525 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8526 	int i, rc;
8527 
8528 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8529 	if (rc)
8530 		return rc;
8531 
8532 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8533 	if (rc) {
8534 		hwrm_req_drop(bp, req_rx);
8535 		return rc;
8536 	}
8537 
8538 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8539 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8540 
8541 	hwrm_req_hold(bp, req_rx);
8542 	hwrm_req_hold(bp, req_tx);
8543 	for (i = 0; i < bp->cp_nr_rings; i++) {
8544 		struct bnxt_napi *bnapi = bp->bnapi[i];
8545 		struct bnxt_coal *hw_coal;
8546 
8547 		if (!bnapi->rx_ring)
8548 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8549 		else
8550 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8551 		if (rc)
8552 			break;
8553 
8554 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8555 			continue;
8556 
8557 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8558 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8559 			if (rc)
8560 				break;
8561 		}
8562 		if (bnapi->rx_ring)
8563 			hw_coal = &bp->rx_coal;
8564 		else
8565 			hw_coal = &bp->tx_coal;
8566 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8567 	}
8568 	hwrm_req_drop(bp, req_rx);
8569 	hwrm_req_drop(bp, req_tx);
8570 	return rc;
8571 }
8572 
8573 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8574 {
8575 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8576 	struct hwrm_stat_ctx_free_input *req;
8577 	int i;
8578 
8579 	if (!bp->bnapi)
8580 		return;
8581 
8582 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8583 		return;
8584 
8585 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8586 		return;
8587 	if (BNXT_FW_MAJ(bp) <= 20) {
8588 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8589 			hwrm_req_drop(bp, req);
8590 			return;
8591 		}
8592 		hwrm_req_hold(bp, req0);
8593 	}
8594 	hwrm_req_hold(bp, req);
8595 	for (i = 0; i < bp->cp_nr_rings; i++) {
8596 		struct bnxt_napi *bnapi = bp->bnapi[i];
8597 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8598 
8599 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8600 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8601 			if (req0) {
8602 				req0->stat_ctx_id = req->stat_ctx_id;
8603 				hwrm_req_send(bp, req0);
8604 			}
8605 			hwrm_req_send(bp, req);
8606 
8607 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8608 		}
8609 	}
8610 	hwrm_req_drop(bp, req);
8611 	if (req0)
8612 		hwrm_req_drop(bp, req0);
8613 }
8614 
8615 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8616 {
8617 	struct hwrm_stat_ctx_alloc_output *resp;
8618 	struct hwrm_stat_ctx_alloc_input *req;
8619 	int rc, i;
8620 
8621 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8622 		return 0;
8623 
8624 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8625 	if (rc)
8626 		return rc;
8627 
8628 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8629 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8630 
8631 	resp = hwrm_req_hold(bp, req);
8632 	for (i = 0; i < bp->cp_nr_rings; i++) {
8633 		struct bnxt_napi *bnapi = bp->bnapi[i];
8634 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8635 
8636 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8637 
8638 		rc = hwrm_req_send(bp, req);
8639 		if (rc)
8640 			break;
8641 
8642 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8643 
8644 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8645 	}
8646 	hwrm_req_drop(bp, req);
8647 	return rc;
8648 }
8649 
8650 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8651 {
8652 	struct hwrm_func_qcfg_output *resp;
8653 	struct hwrm_func_qcfg_input *req;
8654 	u16 flags;
8655 	int rc;
8656 
8657 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8658 	if (rc)
8659 		return rc;
8660 
8661 	req->fid = cpu_to_le16(0xffff);
8662 	resp = hwrm_req_hold(bp, req);
8663 	rc = hwrm_req_send(bp, req);
8664 	if (rc)
8665 		goto func_qcfg_exit;
8666 
8667 	flags = le16_to_cpu(resp->flags);
8668 #ifdef CONFIG_BNXT_SRIOV
8669 	if (BNXT_VF(bp)) {
8670 		struct bnxt_vf_info *vf = &bp->vf;
8671 
8672 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8673 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8674 			vf->flags |= BNXT_VF_TRUST;
8675 		else
8676 			vf->flags &= ~BNXT_VF_TRUST;
8677 	} else {
8678 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8679 	}
8680 #endif
8681 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8682 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8683 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8684 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8685 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8686 	}
8687 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8688 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8689 
8690 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8691 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8692 
8693 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8694 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8695 	if (resp->roce_bidi_opt_mode &
8696 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8697 		bp->cos0_cos1_shared = 1;
8698 	else
8699 		bp->cos0_cos1_shared = 0;
8700 
8701 	switch (resp->port_partition_type) {
8702 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8703 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8704 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8705 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8706 		bp->port_partition_type = resp->port_partition_type;
8707 		break;
8708 	}
8709 	if (bp->hwrm_spec_code < 0x10707 ||
8710 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8711 		bp->br_mode = BRIDGE_MODE_VEB;
8712 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8713 		bp->br_mode = BRIDGE_MODE_VEPA;
8714 	else
8715 		bp->br_mode = BRIDGE_MODE_UNDEF;
8716 
8717 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8718 	if (!bp->max_mtu)
8719 		bp->max_mtu = BNXT_MAX_MTU;
8720 
8721 	if (bp->db_size)
8722 		goto func_qcfg_exit;
8723 
8724 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8725 	if (BNXT_CHIP_P5(bp)) {
8726 		if (BNXT_PF(bp))
8727 			bp->db_offset = DB_PF_OFFSET_P5;
8728 		else
8729 			bp->db_offset = DB_VF_OFFSET_P5;
8730 	}
8731 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8732 				 1024);
8733 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8734 	    bp->db_size <= bp->db_offset)
8735 		bp->db_size = pci_resource_len(bp->pdev, 2);
8736 
8737 func_qcfg_exit:
8738 	hwrm_req_drop(bp, req);
8739 	return rc;
8740 }
8741 
8742 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8743 				      u8 init_val, u8 init_offset,
8744 				      bool init_mask_set)
8745 {
8746 	ctxm->init_value = init_val;
8747 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8748 	if (init_mask_set)
8749 		ctxm->init_offset = init_offset * 4;
8750 	else
8751 		ctxm->init_value = 0;
8752 }
8753 
8754 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8755 {
8756 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8757 	u16 type;
8758 
8759 	for (type = 0; type < ctx_max; type++) {
8760 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8761 		int n = 1;
8762 
8763 		if (!ctxm->max_entries || ctxm->pg_info)
8764 			continue;
8765 
8766 		if (ctxm->instance_bmap)
8767 			n = hweight32(ctxm->instance_bmap);
8768 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8769 		if (!ctxm->pg_info)
8770 			return -ENOMEM;
8771 	}
8772 	return 0;
8773 }
8774 
8775 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8776 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8777 
8778 #define BNXT_CTX_INIT_VALID(flags)	\
8779 	(!!((flags) &			\
8780 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8781 
8782 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8783 {
8784 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8785 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8786 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8787 	u16 type, next_type = 0;
8788 	int rc;
8789 
8790 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8791 	if (rc)
8792 		return rc;
8793 
8794 	if (!ctx) {
8795 		ctx = kzalloc_obj(*ctx);
8796 		if (!ctx)
8797 			return -ENOMEM;
8798 		bp->ctx = ctx;
8799 	}
8800 
8801 	resp = hwrm_req_hold(bp, req);
8802 
8803 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8804 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8805 		u8 init_val, init_off, i;
8806 		u32 max_entries;
8807 		u16 entry_size;
8808 		__le32 *p;
8809 		u32 flags;
8810 
8811 		req->type = cpu_to_le16(type);
8812 		rc = hwrm_req_send(bp, req);
8813 		if (rc)
8814 			goto ctx_done;
8815 		flags = le32_to_cpu(resp->flags);
8816 		next_type = le16_to_cpu(resp->next_valid_type);
8817 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8818 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8819 			continue;
8820 		}
8821 		entry_size = le16_to_cpu(resp->entry_size);
8822 		max_entries = le32_to_cpu(resp->max_num_entries);
8823 		if (ctxm->mem_valid) {
8824 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8825 			    ctxm->entry_size != entry_size ||
8826 			    ctxm->max_entries != max_entries)
8827 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8828 			else
8829 				continue;
8830 		}
8831 		ctxm->type = type;
8832 		ctxm->entry_size = entry_size;
8833 		ctxm->flags = flags;
8834 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8835 		ctxm->entry_multiple = resp->entry_multiple;
8836 		ctxm->max_entries = max_entries;
8837 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8838 		init_val = resp->ctx_init_value;
8839 		init_off = resp->ctx_init_offset;
8840 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8841 					  BNXT_CTX_INIT_VALID(flags));
8842 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8843 					      BNXT_MAX_SPLIT_ENTRY);
8844 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8845 		     i++, p++)
8846 			ctxm->split[i] = le32_to_cpu(*p);
8847 	}
8848 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8849 
8850 ctx_done:
8851 	hwrm_req_drop(bp, req);
8852 	return rc;
8853 }
8854 
8855 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8856 {
8857 	struct hwrm_func_backing_store_qcaps_output *resp;
8858 	struct hwrm_func_backing_store_qcaps_input *req;
8859 	int rc;
8860 
8861 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8862 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8863 		return 0;
8864 
8865 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8866 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8867 
8868 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8869 	if (rc)
8870 		return rc;
8871 
8872 	resp = hwrm_req_hold(bp, req);
8873 	rc = hwrm_req_send_silent(bp, req);
8874 	if (!rc) {
8875 		struct bnxt_ctx_mem_type *ctxm;
8876 		struct bnxt_ctx_mem_info *ctx;
8877 		u8 init_val, init_idx = 0;
8878 		u16 init_mask;
8879 
8880 		ctx = bp->ctx;
8881 		if (!ctx) {
8882 			ctx = kzalloc_obj(*ctx);
8883 			if (!ctx) {
8884 				rc = -ENOMEM;
8885 				goto ctx_err;
8886 			}
8887 			bp->ctx = ctx;
8888 		}
8889 		init_val = resp->ctx_kind_initializer;
8890 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8891 
8892 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8893 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8894 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8895 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8896 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8897 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8898 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8899 					  (init_mask & (1 << init_idx++)) != 0);
8900 
8901 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8902 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8903 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8904 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8905 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8906 					  (init_mask & (1 << init_idx++)) != 0);
8907 
8908 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8909 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8910 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8911 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8912 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8913 					  (init_mask & (1 << init_idx++)) != 0);
8914 
8915 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8916 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8917 		ctxm->max_entries = ctxm->vnic_entries +
8918 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8919 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8920 		bnxt_init_ctx_initializer(ctxm, init_val,
8921 					  resp->vnic_init_offset,
8922 					  (init_mask & (1 << init_idx++)) != 0);
8923 
8924 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8925 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8926 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8927 		bnxt_init_ctx_initializer(ctxm, init_val,
8928 					  resp->stat_init_offset,
8929 					  (init_mask & (1 << init_idx++)) != 0);
8930 
8931 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8932 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8933 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8934 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8935 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8936 		if (!ctxm->entry_multiple)
8937 			ctxm->entry_multiple = 1;
8938 
8939 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8940 
8941 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8942 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8943 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8944 		ctxm->mrav_num_entries_units =
8945 			le16_to_cpu(resp->mrav_num_entries_units);
8946 		bnxt_init_ctx_initializer(ctxm, init_val,
8947 					  resp->mrav_init_offset,
8948 					  (init_mask & (1 << init_idx++)) != 0);
8949 
8950 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8951 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8952 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8953 
8954 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8955 		if (!ctx->tqm_fp_rings_count)
8956 			ctx->tqm_fp_rings_count = bp->max_q;
8957 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8958 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8959 
8960 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8961 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8962 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8963 
8964 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8965 	} else {
8966 		rc = 0;
8967 	}
8968 ctx_err:
8969 	hwrm_req_drop(bp, req);
8970 	return rc;
8971 }
8972 
8973 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8974 				  __le64 *pg_dir)
8975 {
8976 	if (!rmem->nr_pages)
8977 		return;
8978 
8979 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8980 	if (rmem->depth >= 1) {
8981 		if (rmem->depth == 2)
8982 			*pg_attr |= 2;
8983 		else
8984 			*pg_attr |= 1;
8985 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8986 	} else {
8987 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8988 	}
8989 }
8990 
8991 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8992 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8993 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8994 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8995 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8996 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8997 
8998 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8999 {
9000 	struct hwrm_func_backing_store_cfg_input *req;
9001 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9002 	struct bnxt_ctx_pg_info *ctx_pg;
9003 	struct bnxt_ctx_mem_type *ctxm;
9004 	void **__req = (void **)&req;
9005 	u32 req_len = sizeof(*req);
9006 	__le32 *num_entries;
9007 	__le64 *pg_dir;
9008 	u32 flags = 0;
9009 	u8 *pg_attr;
9010 	u32 ena;
9011 	int rc;
9012 	int i;
9013 
9014 	if (!ctx)
9015 		return 0;
9016 
9017 	if (req_len > bp->hwrm_max_ext_req_len)
9018 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
9019 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
9020 	if (rc)
9021 		return rc;
9022 
9023 	req->enables = cpu_to_le32(enables);
9024 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
9025 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9026 		ctx_pg = ctxm->pg_info;
9027 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
9028 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
9029 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
9030 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
9031 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9032 				      &req->qpc_pg_size_qpc_lvl,
9033 				      &req->qpc_page_dir);
9034 
9035 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
9036 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
9037 	}
9038 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
9039 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9040 		ctx_pg = ctxm->pg_info;
9041 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
9042 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
9043 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
9044 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9045 				      &req->srq_pg_size_srq_lvl,
9046 				      &req->srq_page_dir);
9047 	}
9048 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
9049 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9050 		ctx_pg = ctxm->pg_info;
9051 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
9052 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
9053 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
9054 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9055 				      &req->cq_pg_size_cq_lvl,
9056 				      &req->cq_page_dir);
9057 	}
9058 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
9059 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9060 		ctx_pg = ctxm->pg_info;
9061 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
9062 		req->vnic_num_ring_table_entries =
9063 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
9064 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
9065 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9066 				      &req->vnic_pg_size_vnic_lvl,
9067 				      &req->vnic_page_dir);
9068 	}
9069 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
9070 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9071 		ctx_pg = ctxm->pg_info;
9072 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
9073 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
9074 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9075 				      &req->stat_pg_size_stat_lvl,
9076 				      &req->stat_page_dir);
9077 	}
9078 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
9079 		u32 units;
9080 
9081 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9082 		ctx_pg = ctxm->pg_info;
9083 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
9084 		units = ctxm->mrav_num_entries_units;
9085 		if (units) {
9086 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
9087 			u32 entries;
9088 
9089 			num_mr = ctx_pg->entries - num_ah;
9090 			entries = ((num_mr / units) << 16) | (num_ah / units);
9091 			req->mrav_num_entries = cpu_to_le32(entries);
9092 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
9093 		}
9094 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
9095 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9096 				      &req->mrav_pg_size_mrav_lvl,
9097 				      &req->mrav_page_dir);
9098 	}
9099 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
9100 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9101 		ctx_pg = ctxm->pg_info;
9102 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
9103 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
9104 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9105 				      &req->tim_pg_size_tim_lvl,
9106 				      &req->tim_page_dir);
9107 	}
9108 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9109 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
9110 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
9111 	     pg_dir = &req->tqm_sp_page_dir,
9112 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
9113 	     ctx_pg = ctxm->pg_info;
9114 	     i < BNXT_MAX_TQM_RINGS;
9115 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
9116 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
9117 		if (!(enables & ena))
9118 			continue;
9119 
9120 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9121 		*num_entries = cpu_to_le32(ctx_pg->entries);
9122 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9123 	}
9124 	req->flags = cpu_to_le32(flags);
9125 	return hwrm_req_send(bp, req);
9126 }
9127 
9128 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9129 				  struct bnxt_ctx_pg_info *ctx_pg)
9130 {
9131 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9132 
9133 	rmem->page_size = BNXT_PAGE_SIZE;
9134 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9135 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9136 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9137 	if (rmem->depth >= 1)
9138 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9139 	return bnxt_alloc_ring(bp, rmem);
9140 }
9141 
9142 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9143 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9144 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9145 {
9146 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9147 	int rc;
9148 
9149 	if (!mem_size)
9150 		return -EINVAL;
9151 
9152 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9153 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9154 		ctx_pg->nr_pages = 0;
9155 		return -EINVAL;
9156 	}
9157 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9158 		int nr_tbls, i;
9159 
9160 		rmem->depth = 2;
9161 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9162 		if (!ctx_pg->ctx_pg_tbl)
9163 			return -ENOMEM;
9164 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9165 		rmem->nr_pages = nr_tbls;
9166 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9167 		if (rc)
9168 			return rc;
9169 		for (i = 0; i < nr_tbls; i++) {
9170 			struct bnxt_ctx_pg_info *pg_tbl;
9171 
9172 			pg_tbl = kzalloc_obj(*pg_tbl);
9173 			if (!pg_tbl)
9174 				return -ENOMEM;
9175 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9176 			rmem = &pg_tbl->ring_mem;
9177 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9178 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9179 			rmem->depth = 1;
9180 			rmem->nr_pages = MAX_CTX_PAGES;
9181 			rmem->ctx_mem = ctxm;
9182 			if (i == (nr_tbls - 1)) {
9183 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9184 
9185 				if (rem)
9186 					rmem->nr_pages = rem;
9187 			}
9188 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9189 			if (rc)
9190 				break;
9191 		}
9192 	} else {
9193 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9194 		if (rmem->nr_pages > 1 || depth)
9195 			rmem->depth = 1;
9196 		rmem->ctx_mem = ctxm;
9197 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9198 	}
9199 	return rc;
9200 }
9201 
9202 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9203 				    struct bnxt_ctx_pg_info *ctx_pg,
9204 				    void *buf, size_t offset, size_t head,
9205 				    size_t tail)
9206 {
9207 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9208 	size_t nr_pages = ctx_pg->nr_pages;
9209 	int page_size = rmem->page_size;
9210 	size_t len = 0, total_len = 0;
9211 	u16 depth = rmem->depth;
9212 
9213 	tail %= nr_pages * page_size;
9214 	do {
9215 		if (depth > 1) {
9216 			int i = head / (page_size * MAX_CTX_PAGES);
9217 			struct bnxt_ctx_pg_info *pg_tbl;
9218 
9219 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9220 			rmem = &pg_tbl->ring_mem;
9221 		}
9222 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9223 		head += len;
9224 		offset += len;
9225 		total_len += len;
9226 		if (head >= nr_pages * page_size)
9227 			head = 0;
9228 	} while (head != tail);
9229 	return total_len;
9230 }
9231 
9232 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9233 				  struct bnxt_ctx_pg_info *ctx_pg)
9234 {
9235 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9236 
9237 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9238 	    ctx_pg->ctx_pg_tbl) {
9239 		int i, nr_tbls = rmem->nr_pages;
9240 
9241 		for (i = 0; i < nr_tbls; i++) {
9242 			struct bnxt_ctx_pg_info *pg_tbl;
9243 			struct bnxt_ring_mem_info *rmem2;
9244 
9245 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9246 			if (!pg_tbl)
9247 				continue;
9248 			rmem2 = &pg_tbl->ring_mem;
9249 			bnxt_free_ring(bp, rmem2);
9250 			ctx_pg->ctx_pg_arr[i] = NULL;
9251 			kfree(pg_tbl);
9252 			ctx_pg->ctx_pg_tbl[i] = NULL;
9253 		}
9254 		kfree(ctx_pg->ctx_pg_tbl);
9255 		ctx_pg->ctx_pg_tbl = NULL;
9256 	}
9257 	bnxt_free_ring(bp, rmem);
9258 	ctx_pg->nr_pages = 0;
9259 }
9260 
9261 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9262 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9263 				   u8 pg_lvl)
9264 {
9265 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9266 	int i, rc = 0, n = 1;
9267 	u32 mem_size;
9268 
9269 	if (!ctxm->entry_size || !ctx_pg)
9270 		return -EINVAL;
9271 	if (ctxm->instance_bmap)
9272 		n = hweight32(ctxm->instance_bmap);
9273 	if (ctxm->entry_multiple)
9274 		entries = roundup(entries, ctxm->entry_multiple);
9275 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9276 	mem_size = entries * ctxm->entry_size;
9277 	for (i = 0; i < n && !rc; i++) {
9278 		ctx_pg[i].entries = entries;
9279 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9280 					    ctxm->init_value ? ctxm : NULL);
9281 	}
9282 	if (!rc)
9283 		ctxm->mem_valid = 1;
9284 	return rc;
9285 }
9286 
9287 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9288 					       struct bnxt_ctx_mem_type *ctxm,
9289 					       bool last)
9290 {
9291 	struct hwrm_func_backing_store_cfg_v2_input *req;
9292 	u32 instance_bmap = ctxm->instance_bmap;
9293 	int i, j, rc = 0, n = 1;
9294 	__le32 *p;
9295 
9296 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9297 		return 0;
9298 
9299 	if (instance_bmap)
9300 		n = hweight32(ctxm->instance_bmap);
9301 	else
9302 		instance_bmap = 1;
9303 
9304 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9305 	if (rc)
9306 		return rc;
9307 	hwrm_req_hold(bp, req);
9308 	req->type = cpu_to_le16(ctxm->type);
9309 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9310 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9311 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9312 		struct bnxt_bs_trace_info *bs_trace;
9313 		u32 enables;
9314 
9315 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9316 		req->enables = cpu_to_le32(enables);
9317 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9318 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9319 	}
9320 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9321 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9322 		p[i] = cpu_to_le32(ctxm->split[i]);
9323 	for (i = 0, j = 0; j < n && !rc; i++) {
9324 		struct bnxt_ctx_pg_info *ctx_pg;
9325 
9326 		if (!(instance_bmap & (1 << i)))
9327 			continue;
9328 		req->instance = cpu_to_le16(i);
9329 		ctx_pg = &ctxm->pg_info[j++];
9330 		if (!ctx_pg->entries)
9331 			continue;
9332 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9333 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9334 				      &req->page_size_pbl_level,
9335 				      &req->page_dir);
9336 		if (last && j == n)
9337 			req->flags =
9338 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9339 		rc = hwrm_req_send(bp, req);
9340 	}
9341 	hwrm_req_drop(bp, req);
9342 	return rc;
9343 }
9344 
9345 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9346 {
9347 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9348 	struct bnxt_ctx_mem_type *ctxm;
9349 	u16 last_type = BNXT_CTX_INV;
9350 	int rc = 0;
9351 	u16 type;
9352 
9353 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9354 		ctxm = &ctx->ctx_arr[type];
9355 		if (!bnxt_bs_trace_avail(bp, type))
9356 			continue;
9357 		if (!ctxm->mem_valid) {
9358 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9359 						     ctxm->max_entries, 1);
9360 			if (rc) {
9361 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9362 					    type);
9363 				continue;
9364 			}
9365 			bnxt_bs_trace_init(bp, ctxm);
9366 		}
9367 		last_type = type;
9368 	}
9369 
9370 	if (last_type == BNXT_CTX_INV) {
9371 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9372 			ctxm = &ctx->ctx_arr[type];
9373 			if (ctxm->mem_valid)
9374 				last_type = type;
9375 		}
9376 		if (last_type == BNXT_CTX_INV)
9377 			return 0;
9378 	}
9379 	ctx->ctx_arr[last_type].last = 1;
9380 
9381 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9382 		ctxm = &ctx->ctx_arr[type];
9383 
9384 		if (!ctxm->mem_valid)
9385 			continue;
9386 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9387 		if (rc)
9388 			return rc;
9389 	}
9390 	return 0;
9391 }
9392 
9393 /**
9394  * __bnxt_copy_ctx_mem - copy host context memory
9395  * @bp: The driver context
9396  * @ctxm: The pointer to the context memory type
9397  * @buf: The destination buffer or NULL to just obtain the length
9398  * @offset: The buffer offset to copy the data to
9399  * @head: The head offset of context memory to copy from
9400  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9401  *
9402  * This function is called for debugging purposes to dump the host context
9403  * used by the chip.
9404  *
9405  * Return: Length of memory copied
9406  */
9407 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9408 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9409 				  size_t offset, size_t head, size_t tail)
9410 {
9411 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9412 	size_t len = 0, total_len = 0;
9413 	int i, n = 1;
9414 
9415 	if (!ctx_pg)
9416 		return 0;
9417 
9418 	if (ctxm->instance_bmap)
9419 		n = hweight32(ctxm->instance_bmap);
9420 	for (i = 0; i < n; i++) {
9421 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9422 					    tail);
9423 		offset += len;
9424 		total_len += len;
9425 	}
9426 	return total_len;
9427 }
9428 
9429 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9430 			 void *buf, size_t offset)
9431 {
9432 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9433 
9434 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9435 }
9436 
9437 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9438 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9439 {
9440 	struct bnxt_ctx_pg_info *ctx_pg;
9441 	int i, n = 1;
9442 
9443 	ctxm->last = 0;
9444 
9445 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9446 		return;
9447 
9448 	ctx_pg = ctxm->pg_info;
9449 	if (ctx_pg) {
9450 		if (ctxm->instance_bmap)
9451 			n = hweight32(ctxm->instance_bmap);
9452 		for (i = 0; i < n; i++)
9453 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9454 
9455 		kfree(ctx_pg);
9456 		ctxm->pg_info = NULL;
9457 		ctxm->mem_valid = 0;
9458 	}
9459 	memset(ctxm, 0, sizeof(*ctxm));
9460 }
9461 
9462 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9463 {
9464 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9465 	u16 type;
9466 
9467 	if (!ctx)
9468 		return;
9469 
9470 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9471 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9472 
9473 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9474 	if (force) {
9475 		kfree(ctx);
9476 		bp->ctx = NULL;
9477 	}
9478 }
9479 
9480 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9481 {
9482 	struct bnxt_ctx_mem_type *ctxm;
9483 	struct bnxt_ctx_mem_info *ctx;
9484 	u32 l2_qps, qp1_qps, max_qps;
9485 	u32 ena, entries_sp, entries;
9486 	u32 srqs, max_srqs, min;
9487 	u32 num_mr, num_ah;
9488 	u32 extra_srqs = 0;
9489 	u32 extra_qps = 0;
9490 	u32 fast_qpmd_qps;
9491 	u8 pg_lvl = 1;
9492 	int i, rc;
9493 
9494 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9495 	if (rc) {
9496 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9497 			   rc);
9498 		return rc;
9499 	}
9500 	ctx = bp->ctx;
9501 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9502 		return 0;
9503 
9504 	ena = 0;
9505 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9506 		goto skip_legacy;
9507 
9508 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9509 	l2_qps = ctxm->qp_l2_entries;
9510 	qp1_qps = ctxm->qp_qp1_entries;
9511 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9512 	max_qps = ctxm->max_entries;
9513 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9514 	srqs = ctxm->srq_l2_entries;
9515 	max_srqs = ctxm->max_entries;
9516 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9517 		pg_lvl = 2;
9518 		if (BNXT_SW_RES_LMT(bp)) {
9519 			extra_qps = max_qps - l2_qps - qp1_qps;
9520 			extra_srqs = max_srqs - srqs;
9521 		} else {
9522 			extra_qps = min_t(u32, 65536,
9523 					  max_qps - l2_qps - qp1_qps);
9524 			/* allocate extra qps if fw supports RoCE fast qp
9525 			 * destroy feature
9526 			 */
9527 			extra_qps += fast_qpmd_qps;
9528 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9529 		}
9530 		if (fast_qpmd_qps)
9531 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9532 	}
9533 
9534 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9535 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9536 				     pg_lvl);
9537 	if (rc)
9538 		return rc;
9539 
9540 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9541 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9542 	if (rc)
9543 		return rc;
9544 
9545 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9546 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9547 				     extra_qps * 2, pg_lvl);
9548 	if (rc)
9549 		return rc;
9550 
9551 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9552 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9553 	if (rc)
9554 		return rc;
9555 
9556 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9557 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9558 	if (rc)
9559 		return rc;
9560 
9561 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9562 		goto skip_rdma;
9563 
9564 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9565 	if (BNXT_SW_RES_LMT(bp) &&
9566 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9567 		num_ah = ctxm->mrav_av_entries;
9568 		num_mr = ctxm->max_entries - num_ah;
9569 	} else {
9570 		/* 128K extra is needed to accommodate static AH context
9571 		 * allocation by f/w.
9572 		 */
9573 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9574 		num_ah = min_t(u32, num_mr, 1024 * 128);
9575 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9576 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9577 			ctxm->mrav_av_entries = num_ah;
9578 	}
9579 
9580 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9581 	if (rc)
9582 		return rc;
9583 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9584 
9585 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9586 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9587 	if (rc)
9588 		return rc;
9589 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9590 
9591 skip_rdma:
9592 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9593 	min = ctxm->min_entries;
9594 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9595 		     2 * (extra_qps + qp1_qps) + min;
9596 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9597 	if (rc)
9598 		return rc;
9599 
9600 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9601 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9602 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9603 	if (rc)
9604 		return rc;
9605 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9606 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9607 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9608 
9609 skip_legacy:
9610 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9611 		rc = bnxt_backing_store_cfg_v2(bp);
9612 	else
9613 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9614 	if (rc) {
9615 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9616 			   rc);
9617 		return rc;
9618 	}
9619 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9620 	return 0;
9621 }
9622 
9623 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9624 {
9625 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9626 	u16 page_attr;
9627 	int rc;
9628 
9629 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9630 		return 0;
9631 
9632 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9633 	if (rc)
9634 		return rc;
9635 
9636 	if (BNXT_PAGE_SIZE == 0x2000)
9637 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9638 	else if (BNXT_PAGE_SIZE == 0x10000)
9639 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9640 	else
9641 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9642 	req->pg_size_lvl = cpu_to_le16(page_attr |
9643 				       bp->fw_crash_mem->ring_mem.depth);
9644 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9645 	req->size = cpu_to_le32(bp->fw_crash_len);
9646 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9647 	return hwrm_req_send(bp, req);
9648 }
9649 
9650 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9651 {
9652 	if (bp->fw_crash_mem) {
9653 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9654 		kfree(bp->fw_crash_mem);
9655 		bp->fw_crash_mem = NULL;
9656 	}
9657 }
9658 
9659 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9660 {
9661 	u32 mem_size = 0;
9662 	int rc;
9663 
9664 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9665 		return 0;
9666 
9667 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9668 	if (rc)
9669 		return rc;
9670 
9671 	mem_size = round_up(mem_size, 4);
9672 
9673 	/* keep and use the existing pages */
9674 	if (bp->fw_crash_mem &&
9675 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9676 		goto alloc_done;
9677 
9678 	if (bp->fw_crash_mem)
9679 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9680 	else
9681 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9682 	if (!bp->fw_crash_mem)
9683 		return -ENOMEM;
9684 
9685 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9686 	if (rc) {
9687 		bnxt_free_crash_dump_mem(bp);
9688 		return rc;
9689 	}
9690 
9691 alloc_done:
9692 	bp->fw_crash_len = mem_size;
9693 	return 0;
9694 }
9695 
9696 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9697 {
9698 	struct hwrm_func_resource_qcaps_output *resp;
9699 	struct hwrm_func_resource_qcaps_input *req;
9700 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9701 	int rc;
9702 
9703 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9704 	if (rc)
9705 		return rc;
9706 
9707 	req->fid = cpu_to_le16(0xffff);
9708 	resp = hwrm_req_hold(bp, req);
9709 	rc = hwrm_req_send_silent(bp, req);
9710 	if (rc)
9711 		goto hwrm_func_resc_qcaps_exit;
9712 
9713 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9714 	if (!all)
9715 		goto hwrm_func_resc_qcaps_exit;
9716 
9717 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9718 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9719 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9720 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9721 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9722 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9723 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9724 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9725 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9726 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9727 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9728 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9729 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9730 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9731 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9732 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9733 
9734 	if (hw_resc->max_rsscos_ctxs >=
9735 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9736 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9737 
9738 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9739 		u16 max_msix = le16_to_cpu(resp->max_msix);
9740 
9741 		hw_resc->max_nqs = max_msix;
9742 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9743 	}
9744 
9745 	if (BNXT_PF(bp)) {
9746 		struct bnxt_pf_info *pf = &bp->pf;
9747 
9748 		pf->vf_resv_strategy =
9749 			le16_to_cpu(resp->vf_reservation_strategy);
9750 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9751 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9752 	}
9753 hwrm_func_resc_qcaps_exit:
9754 	hwrm_req_drop(bp, req);
9755 	return rc;
9756 }
9757 
9758 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9759 {
9760 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9761 	struct hwrm_port_mac_ptp_qcfg_input *req;
9762 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9763 	u8 flags;
9764 	int rc;
9765 
9766 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9767 		rc = -ENODEV;
9768 		goto no_ptp;
9769 	}
9770 
9771 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9772 	if (rc)
9773 		goto no_ptp;
9774 
9775 	req->port_id = cpu_to_le16(bp->pf.port_id);
9776 	resp = hwrm_req_hold(bp, req);
9777 	rc = hwrm_req_send(bp, req);
9778 	if (rc)
9779 		goto exit;
9780 
9781 	flags = resp->flags;
9782 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9783 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9784 		rc = -ENODEV;
9785 		goto exit;
9786 	}
9787 	if (!ptp) {
9788 		ptp = kzalloc_obj(*ptp);
9789 		if (!ptp) {
9790 			rc = -ENOMEM;
9791 			goto exit;
9792 		}
9793 		ptp->bp = bp;
9794 		bp->ptp_cfg = ptp;
9795 	}
9796 
9797 	if (flags &
9798 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9799 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9800 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9801 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9802 	} else if (BNXT_CHIP_P5(bp)) {
9803 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9804 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9805 	} else {
9806 		rc = -ENODEV;
9807 		goto exit;
9808 	}
9809 	ptp->rtc_configured =
9810 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9811 	rc = bnxt_ptp_init(bp);
9812 	if (rc)
9813 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9814 exit:
9815 	hwrm_req_drop(bp, req);
9816 	if (!rc)
9817 		return 0;
9818 
9819 no_ptp:
9820 	bnxt_ptp_clear(bp);
9821 	kfree(ptp);
9822 	bp->ptp_cfg = NULL;
9823 	return rc;
9824 }
9825 
9826 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9827 {
9828 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9829 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9830 	struct hwrm_func_qcaps_output *resp;
9831 	struct hwrm_func_qcaps_input *req;
9832 	int rc;
9833 
9834 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9835 	if (rc)
9836 		return rc;
9837 
9838 	req->fid = cpu_to_le16(0xffff);
9839 	resp = hwrm_req_hold(bp, req);
9840 	rc = hwrm_req_send(bp, req);
9841 	if (rc)
9842 		goto hwrm_func_qcaps_exit;
9843 
9844 	flags = le32_to_cpu(resp->flags);
9845 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9846 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9847 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9848 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9849 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9850 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9851 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9852 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9853 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9854 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9855 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9856 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9857 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9858 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9859 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9860 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9861 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9862 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9863 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9864 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9865 
9866 	flags_ext = le32_to_cpu(resp->flags_ext);
9867 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9868 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9869 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9870 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9871 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9872 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9873 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9874 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9875 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9876 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9877 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9878 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9879 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9880 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9881 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9882 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9883 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9884 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9885 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9886 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9887 
9888 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9889 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9890 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9891 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9892 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9893 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9894 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9895 	if (flags_ext2 &
9896 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9897 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9898 	if (BNXT_PF(bp) &&
9899 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9900 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9901 
9902 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9903 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9904 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9905 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9906 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9907 
9908 	bp->tx_push_thresh = 0;
9909 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9910 	    BNXT_FW_MAJ(bp) > 217)
9911 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9912 
9913 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9914 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9915 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9916 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9917 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9918 	if (!hw_resc->max_hw_ring_grps)
9919 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9920 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9921 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9922 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9923 
9924 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9925 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9926 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9927 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9928 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9929 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9930 
9931 	if (BNXT_PF(bp)) {
9932 		struct bnxt_pf_info *pf = &bp->pf;
9933 
9934 		pf->fw_fid = le16_to_cpu(resp->fid);
9935 		pf->port_id = le16_to_cpu(resp->port_id);
9936 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9937 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9938 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9939 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9940 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9941 			bp->flags |= BNXT_FLAG_WOL_CAP;
9942 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9943 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9944 		} else {
9945 			bnxt_ptp_clear(bp);
9946 			kfree(bp->ptp_cfg);
9947 			bp->ptp_cfg = NULL;
9948 		}
9949 	} else {
9950 #ifdef CONFIG_BNXT_SRIOV
9951 		struct bnxt_vf_info *vf = &bp->vf;
9952 
9953 		vf->fw_fid = le16_to_cpu(resp->fid);
9954 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9955 #endif
9956 	}
9957 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9958 
9959 hwrm_func_qcaps_exit:
9960 	hwrm_req_drop(bp, req);
9961 	return rc;
9962 }
9963 
9964 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9965 {
9966 	struct hwrm_dbg_qcaps_output *resp;
9967 	struct hwrm_dbg_qcaps_input *req;
9968 	int rc;
9969 
9970 	bp->fw_dbg_cap = 0;
9971 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9972 		return;
9973 
9974 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9975 	if (rc)
9976 		return;
9977 
9978 	req->fid = cpu_to_le16(0xffff);
9979 	resp = hwrm_req_hold(bp, req);
9980 	rc = hwrm_req_send(bp, req);
9981 	if (rc)
9982 		goto hwrm_dbg_qcaps_exit;
9983 
9984 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9985 
9986 hwrm_dbg_qcaps_exit:
9987 	hwrm_req_drop(bp, req);
9988 }
9989 
9990 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9991 
9992 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9993 {
9994 	int rc;
9995 
9996 	rc = __bnxt_hwrm_func_qcaps(bp);
9997 	if (rc)
9998 		return rc;
9999 
10000 	bnxt_hwrm_dbg_qcaps(bp);
10001 
10002 	rc = bnxt_hwrm_queue_qportcfg(bp);
10003 	if (rc) {
10004 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
10005 		return rc;
10006 	}
10007 	if (bp->hwrm_spec_code >= 0x10803) {
10008 		rc = bnxt_alloc_ctx_mem(bp);
10009 		if (rc)
10010 			return rc;
10011 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
10012 		if (!rc)
10013 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
10014 	}
10015 	return 0;
10016 }
10017 
10018 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
10019 {
10020 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
10021 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
10022 	u32 flags;
10023 	int rc;
10024 
10025 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
10026 		return 0;
10027 
10028 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
10029 	if (rc)
10030 		return rc;
10031 
10032 	resp = hwrm_req_hold(bp, req);
10033 	rc = hwrm_req_send(bp, req);
10034 	if (rc)
10035 		goto hwrm_cfa_adv_qcaps_exit;
10036 
10037 	flags = le32_to_cpu(resp->flags);
10038 	if (flags &
10039 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
10040 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
10041 
10042 	if (flags &
10043 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
10044 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
10045 
10046 	if (flags &
10047 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
10048 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
10049 
10050 hwrm_cfa_adv_qcaps_exit:
10051 	hwrm_req_drop(bp, req);
10052 	return rc;
10053 }
10054 
10055 static int __bnxt_alloc_fw_health(struct bnxt *bp)
10056 {
10057 	if (bp->fw_health)
10058 		return 0;
10059 
10060 	bp->fw_health = kzalloc_obj(*bp->fw_health);
10061 	if (!bp->fw_health)
10062 		return -ENOMEM;
10063 
10064 	mutex_init(&bp->fw_health->lock);
10065 	return 0;
10066 }
10067 
10068 static int bnxt_alloc_fw_health(struct bnxt *bp)
10069 {
10070 	int rc;
10071 
10072 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10073 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10074 		return 0;
10075 
10076 	rc = __bnxt_alloc_fw_health(bp);
10077 	if (rc) {
10078 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10079 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10080 		return rc;
10081 	}
10082 
10083 	return 0;
10084 }
10085 
10086 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
10087 {
10088 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
10089 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
10090 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
10091 }
10092 
10093 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
10094 {
10095 	struct bnxt_fw_health *fw_health = bp->fw_health;
10096 	u32 reg_type;
10097 
10098 	if (!fw_health)
10099 		return;
10100 
10101 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
10102 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10103 		fw_health->status_reliable = false;
10104 
10105 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
10106 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10107 		fw_health->resets_reliable = false;
10108 }
10109 
10110 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
10111 {
10112 	void __iomem *hs;
10113 	u32 status_loc;
10114 	u32 reg_type;
10115 	u32 sig;
10116 
10117 	if (bp->fw_health)
10118 		bp->fw_health->status_reliable = false;
10119 
10120 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10121 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10122 
10123 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10124 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10125 		if (!bp->chip_num) {
10126 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10127 			bp->chip_num = readl(bp->bar0 +
10128 					     BNXT_FW_HEALTH_WIN_BASE +
10129 					     BNXT_GRC_REG_CHIP_NUM);
10130 		}
10131 		if (!BNXT_CHIP_P5_PLUS(bp))
10132 			return;
10133 
10134 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10135 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10136 	} else {
10137 		status_loc = readl(hs + offsetof(struct hcomm_status,
10138 						 fw_status_loc));
10139 	}
10140 
10141 	if (__bnxt_alloc_fw_health(bp)) {
10142 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10143 		return;
10144 	}
10145 
10146 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10147 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10148 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10149 		__bnxt_map_fw_health_reg(bp, status_loc);
10150 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10151 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10152 	}
10153 
10154 	bp->fw_health->status_reliable = true;
10155 }
10156 
10157 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10158 {
10159 	struct bnxt_fw_health *fw_health = bp->fw_health;
10160 	u32 reg_base = 0xffffffff;
10161 	int i;
10162 
10163 	bp->fw_health->status_reliable = false;
10164 	bp->fw_health->resets_reliable = false;
10165 	/* Only pre-map the monitoring GRC registers using window 3 */
10166 	for (i = 0; i < 4; i++) {
10167 		u32 reg = fw_health->regs[i];
10168 
10169 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10170 			continue;
10171 		if (reg_base == 0xffffffff)
10172 			reg_base = reg & BNXT_GRC_BASE_MASK;
10173 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10174 			return -ERANGE;
10175 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10176 	}
10177 	bp->fw_health->status_reliable = true;
10178 	bp->fw_health->resets_reliable = true;
10179 	if (reg_base == 0xffffffff)
10180 		return 0;
10181 
10182 	__bnxt_map_fw_health_reg(bp, reg_base);
10183 	return 0;
10184 }
10185 
10186 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10187 {
10188 	if (!bp->fw_health)
10189 		return;
10190 
10191 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10192 		bp->fw_health->status_reliable = true;
10193 		bp->fw_health->resets_reliable = true;
10194 	} else {
10195 		bnxt_try_map_fw_health_reg(bp);
10196 	}
10197 }
10198 
10199 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10200 {
10201 	struct bnxt_fw_health *fw_health = bp->fw_health;
10202 	struct hwrm_error_recovery_qcfg_output *resp;
10203 	struct hwrm_error_recovery_qcfg_input *req;
10204 	int rc, i;
10205 
10206 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10207 		return 0;
10208 
10209 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10210 	if (rc)
10211 		return rc;
10212 
10213 	resp = hwrm_req_hold(bp, req);
10214 	rc = hwrm_req_send(bp, req);
10215 	if (rc)
10216 		goto err_recovery_out;
10217 	fw_health->flags = le32_to_cpu(resp->flags);
10218 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10219 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10220 		rc = -EINVAL;
10221 		goto err_recovery_out;
10222 	}
10223 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10224 	fw_health->master_func_wait_dsecs =
10225 		le32_to_cpu(resp->master_func_wait_period);
10226 	fw_health->normal_func_wait_dsecs =
10227 		le32_to_cpu(resp->normal_func_wait_period);
10228 	fw_health->post_reset_wait_dsecs =
10229 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10230 	fw_health->post_reset_max_wait_dsecs =
10231 		le32_to_cpu(resp->max_bailout_time_after_reset);
10232 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10233 		le32_to_cpu(resp->fw_health_status_reg);
10234 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10235 		le32_to_cpu(resp->fw_heartbeat_reg);
10236 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10237 		le32_to_cpu(resp->fw_reset_cnt_reg);
10238 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10239 		le32_to_cpu(resp->reset_inprogress_reg);
10240 	fw_health->fw_reset_inprog_reg_mask =
10241 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10242 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10243 	if (fw_health->fw_reset_seq_cnt >= 16) {
10244 		rc = -EINVAL;
10245 		goto err_recovery_out;
10246 	}
10247 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10248 		fw_health->fw_reset_seq_regs[i] =
10249 			le32_to_cpu(resp->reset_reg[i]);
10250 		fw_health->fw_reset_seq_vals[i] =
10251 			le32_to_cpu(resp->reset_reg_val[i]);
10252 		fw_health->fw_reset_seq_delay_msec[i] =
10253 			resp->delay_after_reset[i];
10254 	}
10255 err_recovery_out:
10256 	hwrm_req_drop(bp, req);
10257 	if (!rc)
10258 		rc = bnxt_map_fw_health_regs(bp);
10259 	if (rc)
10260 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10261 	return rc;
10262 }
10263 
10264 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10265 {
10266 	struct hwrm_func_reset_input *req;
10267 	int rc;
10268 
10269 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10270 	if (rc)
10271 		return rc;
10272 
10273 	req->enables = 0;
10274 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10275 	return hwrm_req_send(bp, req);
10276 }
10277 
10278 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10279 {
10280 	struct hwrm_nvm_get_dev_info_output nvm_info;
10281 
10282 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10283 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10284 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10285 			 nvm_info.nvm_cfg_ver_upd);
10286 }
10287 
10288 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10289 {
10290 	struct hwrm_queue_qportcfg_output *resp;
10291 	struct hwrm_queue_qportcfg_input *req;
10292 	u8 i, j, *qptr;
10293 	bool no_rdma;
10294 	int rc = 0;
10295 
10296 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10297 	if (rc)
10298 		return rc;
10299 
10300 	resp = hwrm_req_hold(bp, req);
10301 	rc = hwrm_req_send(bp, req);
10302 	if (rc)
10303 		goto qportcfg_exit;
10304 
10305 	if (!resp->max_configurable_queues) {
10306 		rc = -EINVAL;
10307 		goto qportcfg_exit;
10308 	}
10309 	bp->max_tc = resp->max_configurable_queues;
10310 	bp->max_lltc = resp->max_configurable_lossless_queues;
10311 	if (bp->max_tc > BNXT_MAX_QUEUE)
10312 		bp->max_tc = BNXT_MAX_QUEUE;
10313 
10314 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10315 	qptr = &resp->queue_id0;
10316 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10317 		bp->q_info[j].queue_id = *qptr;
10318 		bp->q_ids[i] = *qptr++;
10319 		bp->q_info[j].queue_profile = *qptr++;
10320 		bp->tc_to_qidx[j] = j;
10321 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10322 		    (no_rdma && BNXT_PF(bp)))
10323 			j++;
10324 	}
10325 	bp->max_q = bp->max_tc;
10326 	bp->max_tc = max_t(u8, j, 1);
10327 
10328 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10329 		bp->max_tc = 1;
10330 
10331 	if (bp->max_lltc > bp->max_tc)
10332 		bp->max_lltc = bp->max_tc;
10333 
10334 qportcfg_exit:
10335 	hwrm_req_drop(bp, req);
10336 	return rc;
10337 }
10338 
10339 static int bnxt_hwrm_poll(struct bnxt *bp)
10340 {
10341 	struct hwrm_ver_get_input *req;
10342 	int rc;
10343 
10344 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10345 	if (rc)
10346 		return rc;
10347 
10348 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10349 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10350 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10351 
10352 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10353 	rc = hwrm_req_send(bp, req);
10354 	return rc;
10355 }
10356 
10357 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10358 {
10359 	struct hwrm_ver_get_output *resp;
10360 	struct hwrm_ver_get_input *req;
10361 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10362 	u32 dev_caps_cfg, hwrm_ver;
10363 	int rc, len, max_tmo_secs;
10364 
10365 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10366 	if (rc)
10367 		return rc;
10368 
10369 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10370 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10371 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10372 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10373 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10374 
10375 	resp = hwrm_req_hold(bp, req);
10376 	rc = hwrm_req_send(bp, req);
10377 	if (rc)
10378 		goto hwrm_ver_get_exit;
10379 
10380 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10381 
10382 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10383 			     resp->hwrm_intf_min_8b << 8 |
10384 			     resp->hwrm_intf_upd_8b;
10385 	if (resp->hwrm_intf_maj_8b < 1) {
10386 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10387 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10388 			    resp->hwrm_intf_upd_8b);
10389 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10390 	}
10391 
10392 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10393 			HWRM_VERSION_UPDATE;
10394 
10395 	if (bp->hwrm_spec_code > hwrm_ver)
10396 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10397 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10398 			 HWRM_VERSION_UPDATE);
10399 	else
10400 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10401 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10402 			 resp->hwrm_intf_upd_8b);
10403 
10404 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10405 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10406 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10407 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10408 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10409 		len = FW_VER_STR_LEN;
10410 	} else {
10411 		fw_maj = resp->hwrm_fw_maj_8b;
10412 		fw_min = resp->hwrm_fw_min_8b;
10413 		fw_bld = resp->hwrm_fw_bld_8b;
10414 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10415 		len = BC_HWRM_STR_LEN;
10416 	}
10417 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10418 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10419 		 fw_rsv);
10420 
10421 	if (strlen(resp->active_pkg_name)) {
10422 		int fw_ver_len = strlen(bp->fw_ver_str);
10423 
10424 		snprintf(bp->fw_ver_str + fw_ver_len,
10425 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10426 			 resp->active_pkg_name);
10427 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10428 	}
10429 
10430 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10431 	if (!bp->hwrm_cmd_timeout)
10432 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10433 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10434 	if (!bp->hwrm_cmd_max_timeout)
10435 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10436 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10437 #ifdef CONFIG_DETECT_HUNG_TASK
10438 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10439 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10440 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10441 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10442 	}
10443 #endif
10444 
10445 	if (resp->hwrm_intf_maj_8b >= 1) {
10446 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10447 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10448 	}
10449 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10450 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10451 
10452 	bp->chip_num = le16_to_cpu(resp->chip_num);
10453 	bp->chip_rev = resp->chip_rev;
10454 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10455 	    !resp->chip_metal)
10456 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10457 
10458 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10459 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10460 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10461 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10462 
10463 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10464 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10465 
10466 	if (dev_caps_cfg &
10467 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10468 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10469 
10470 	if (dev_caps_cfg &
10471 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10472 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10473 
10474 	if (dev_caps_cfg &
10475 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10476 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10477 
10478 hwrm_ver_get_exit:
10479 	hwrm_req_drop(bp, req);
10480 	return rc;
10481 }
10482 
10483 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10484 {
10485 	struct hwrm_fw_set_time_input *req;
10486 	struct tm tm;
10487 	time64_t now = ktime_get_real_seconds();
10488 	int rc;
10489 
10490 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10491 	    bp->hwrm_spec_code < 0x10400)
10492 		return -EOPNOTSUPP;
10493 
10494 	time64_to_tm(now, 0, &tm);
10495 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10496 	if (rc)
10497 		return rc;
10498 
10499 	req->year = cpu_to_le16(1900 + tm.tm_year);
10500 	req->month = 1 + tm.tm_mon;
10501 	req->day = tm.tm_mday;
10502 	req->hour = tm.tm_hour;
10503 	req->minute = tm.tm_min;
10504 	req->second = tm.tm_sec;
10505 	return hwrm_req_send(bp, req);
10506 }
10507 
10508 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10509 {
10510 	u64 sw_tmp;
10511 
10512 	hw &= mask;
10513 	sw_tmp = (*sw & ~mask) | hw;
10514 	if (hw < (*sw & mask))
10515 		sw_tmp += mask + 1;
10516 	WRITE_ONCE(*sw, sw_tmp);
10517 }
10518 
10519 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10520 				    int count, bool ignore_zero)
10521 {
10522 	int i;
10523 
10524 	for (i = 0; i < count; i++) {
10525 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10526 
10527 		if (ignore_zero && !hw)
10528 			continue;
10529 
10530 		if (masks[i] == -1ULL)
10531 			sw_stats[i] = hw;
10532 		else
10533 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10534 	}
10535 }
10536 
10537 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10538 {
10539 	if (!stats->hw_stats)
10540 		return;
10541 
10542 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10543 				stats->hw_masks, stats->len / 8, false);
10544 }
10545 
10546 static void bnxt_accumulate_ring_stats(struct bnxt *bp)
10547 {
10548 	struct bnxt_stats_mem *ring0_stats;
10549 	bool ignore_zero = false;
10550 	int i;
10551 
10552 	/* Chip bug.  Counter intermittently becomes 0. */
10553 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10554 		ignore_zero = true;
10555 
10556 	for (i = 0; i < bp->cp_nr_rings; i++) {
10557 		struct bnxt_napi *bnapi = bp->bnapi[i];
10558 		struct bnxt_cp_ring_info *cpr;
10559 		struct bnxt_stats_mem *stats;
10560 
10561 		cpr = &bnapi->cp_ring;
10562 		stats = &cpr->stats;
10563 		if (!i)
10564 			ring0_stats = stats;
10565 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10566 					ring0_stats->hw_masks,
10567 					ring0_stats->len / 8, ignore_zero);
10568 	}
10569 }
10570 
10571 static void bnxt_accumulate_port_stats(struct bnxt *bp)
10572 {
10573 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10574 		struct bnxt_stats_mem *stats = &bp->port_stats;
10575 		__le64 *hw_stats = stats->hw_stats;
10576 		u64 *sw_stats = stats->sw_stats;
10577 		u64 *masks = stats->hw_masks;
10578 		int cnt;
10579 
10580 		cnt = sizeof(struct rx_port_stats) / 8;
10581 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10582 
10583 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10584 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10585 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10586 		cnt = sizeof(struct tx_port_stats) / 8;
10587 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10588 	}
10589 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10590 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10591 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10592 	}
10593 }
10594 
10595 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10596 {
10597 	bnxt_accumulate_ring_stats(bp);
10598 	bnxt_accumulate_port_stats(bp);
10599 }
10600 
10601 /* Re-accumulate ring stats from DMA buffers if stale.
10602  * uAPIs for reading sw_stats should call this first.
10603  *
10604  * We promise user space update frequency of bp->stats_coal_ticks but
10605  * the update is a two step process - first device updates the DMA buffer,
10606  * then we have to update from that buffer to driver stats in the service work.
10607  * Worst case we would be 2x off from the desired frequency.
10608  * Sync the stats sooner, if stale. The 20% threshold was chosen arbitrarily.
10609  *
10610  * Ideally we would split the user-configured time into two portions,
10611  * i.e. also lower the DMA period by the 20%. But the DMA timer seems to have
10612  * too coarse granularity to play such tricks.
10613  */
10614 void bnxt_sync_ring_stats(struct bnxt *bp)
10615 {
10616 	unsigned long stale;
10617 
10618 	if (!netif_running(bp->dev) || !bp->stats_coal_ticks)
10619 		return;
10620 
10621 	spin_lock(&bp->stats_lock);
10622 	stale = usecs_to_jiffies(bp->stats_coal_ticks / 5);
10623 	if (time_after_eq(jiffies, bp->stats_updated_jiffies + stale)) {
10624 		bnxt_accumulate_ring_stats(bp);
10625 		bp->stats_updated_jiffies = jiffies;
10626 	}
10627 	spin_unlock(&bp->stats_lock);
10628 }
10629 
10630 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10631 {
10632 	struct hwrm_port_qstats_input *req;
10633 	struct bnxt_pf_info *pf = &bp->pf;
10634 	int rc;
10635 
10636 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10637 		return 0;
10638 
10639 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10640 		return -EOPNOTSUPP;
10641 
10642 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10643 	if (rc)
10644 		return rc;
10645 
10646 	req->flags = flags;
10647 	req->port_id = cpu_to_le16(pf->port_id);
10648 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10649 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10650 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10651 	return hwrm_req_send(bp, req);
10652 }
10653 
10654 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10655 {
10656 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10657 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10658 	struct hwrm_port_qstats_ext_output *resp_qs;
10659 	struct hwrm_port_qstats_ext_input *req_qs;
10660 	struct bnxt_pf_info *pf = &bp->pf;
10661 	u32 tx_stat_size;
10662 	int rc;
10663 
10664 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10665 		return 0;
10666 
10667 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10668 		return -EOPNOTSUPP;
10669 
10670 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10671 	if (rc)
10672 		return rc;
10673 
10674 	req_qs->flags = flags;
10675 	req_qs->port_id = cpu_to_le16(pf->port_id);
10676 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10677 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10678 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10679 		       sizeof(struct tx_port_stats_ext) : 0;
10680 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10681 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10682 	resp_qs = hwrm_req_hold(bp, req_qs);
10683 	rc = hwrm_req_send(bp, req_qs);
10684 	if (!rc) {
10685 		bp->fw_rx_stats_ext_size =
10686 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10687 		if (BNXT_FW_MAJ(bp) < 220 &&
10688 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10689 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10690 
10691 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10692 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10693 	} else {
10694 		bp->fw_rx_stats_ext_size = 0;
10695 		bp->fw_tx_stats_ext_size = 0;
10696 	}
10697 	hwrm_req_drop(bp, req_qs);
10698 
10699 	if (flags)
10700 		return rc;
10701 
10702 	if (bp->fw_tx_stats_ext_size <=
10703 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10704 		bp->pri2cos_valid = 0;
10705 		return rc;
10706 	}
10707 
10708 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10709 	if (rc)
10710 		return rc;
10711 
10712 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10713 
10714 	resp_qc = hwrm_req_hold(bp, req_qc);
10715 	rc = hwrm_req_send(bp, req_qc);
10716 	if (!rc) {
10717 		u8 *pri2cos;
10718 		int i, j;
10719 
10720 		pri2cos = &resp_qc->pri0_cos_queue_id;
10721 		for (i = 0; i < 8; i++) {
10722 			u8 queue_id = pri2cos[i];
10723 			u8 queue_idx;
10724 
10725 			/* Per port queue IDs start from 0, 10, 20, etc */
10726 			queue_idx = queue_id % 10;
10727 			if (queue_idx > BNXT_MAX_QUEUE) {
10728 				bp->pri2cos_valid = false;
10729 				hwrm_req_drop(bp, req_qc);
10730 				return rc;
10731 			}
10732 			for (j = 0; j < bp->max_q; j++) {
10733 				if (bp->q_ids[j] == queue_id)
10734 					bp->pri2cos_idx[i] = queue_idx;
10735 			}
10736 		}
10737 		bp->pri2cos_valid = true;
10738 	}
10739 	hwrm_req_drop(bp, req_qc);
10740 
10741 	return rc;
10742 }
10743 
10744 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10745 {
10746 	bnxt_hwrm_tunnel_dst_port_free(bp,
10747 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10748 	bnxt_hwrm_tunnel_dst_port_free(bp,
10749 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10750 }
10751 
10752 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10753 {
10754 	int rc, i;
10755 	u32 tpa_flags = 0;
10756 
10757 	if (set_tpa)
10758 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10759 	else if (BNXT_NO_FW_ACCESS(bp))
10760 		return 0;
10761 	for (i = 0; i < bp->nr_vnics; i++) {
10762 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10763 		if (rc) {
10764 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10765 				   i, rc);
10766 			return rc;
10767 		}
10768 	}
10769 	return 0;
10770 }
10771 
10772 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10773 {
10774 	int i;
10775 
10776 	for (i = 0; i < bp->nr_vnics; i++)
10777 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10778 }
10779 
10780 static void bnxt_clear_vnic(struct bnxt *bp)
10781 {
10782 	if (!bp->vnic_info)
10783 		return;
10784 
10785 	bnxt_hwrm_clear_vnic_filter(bp);
10786 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10787 		/* clear all RSS setting before free vnic ctx */
10788 		bnxt_hwrm_clear_vnic_rss(bp);
10789 		bnxt_hwrm_vnic_ctx_free(bp);
10790 	}
10791 	/* before free the vnic, undo the vnic tpa settings */
10792 	if (bp->flags & BNXT_FLAG_TPA)
10793 		bnxt_set_tpa(bp, false);
10794 	bnxt_hwrm_vnic_free(bp);
10795 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10796 		bnxt_hwrm_vnic_ctx_free(bp);
10797 }
10798 
10799 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10800 				    bool irq_re_init)
10801 {
10802 	bnxt_clear_vnic(bp);
10803 	bnxt_hwrm_ring_free(bp, close_path);
10804 	bnxt_hwrm_ring_grp_free(bp);
10805 	if (irq_re_init) {
10806 		bnxt_hwrm_stat_ctx_free(bp);
10807 		bnxt_hwrm_free_tunnel_ports(bp);
10808 	}
10809 }
10810 
10811 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10812 {
10813 	struct hwrm_func_cfg_input *req;
10814 	u8 evb_mode;
10815 	int rc;
10816 
10817 	if (br_mode == BRIDGE_MODE_VEB)
10818 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10819 	else if (br_mode == BRIDGE_MODE_VEPA)
10820 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10821 	else
10822 		return -EINVAL;
10823 
10824 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10825 	if (rc)
10826 		return rc;
10827 
10828 	req->fid = cpu_to_le16(0xffff);
10829 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10830 	req->evb_mode = evb_mode;
10831 	return hwrm_req_send(bp, req);
10832 }
10833 
10834 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10835 {
10836 	struct hwrm_func_cfg_input *req;
10837 	int rc;
10838 
10839 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10840 		return 0;
10841 
10842 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10843 	if (rc)
10844 		return rc;
10845 
10846 	req->fid = cpu_to_le16(0xffff);
10847 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10848 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10849 	if (size == 128)
10850 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10851 
10852 	return hwrm_req_send(bp, req);
10853 }
10854 
10855 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10856 {
10857 	int rc;
10858 
10859 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10860 		goto skip_rss_ctx;
10861 
10862 	/* allocate context for vnic */
10863 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10864 	if (rc) {
10865 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10866 			   vnic->vnic_id, rc);
10867 		goto vnic_setup_err;
10868 	}
10869 	bp->rsscos_nr_ctxs++;
10870 
10871 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10872 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10873 		if (rc) {
10874 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10875 				   vnic->vnic_id, rc);
10876 			goto vnic_setup_err;
10877 		}
10878 		bp->rsscos_nr_ctxs++;
10879 	}
10880 
10881 skip_rss_ctx:
10882 	bnxt_fill_hw_rss_tbl(bp, vnic);
10883 	/* configure default vnic, ring grp */
10884 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10885 	if (rc) {
10886 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10887 			   vnic->vnic_id, rc);
10888 		goto vnic_setup_err;
10889 	}
10890 
10891 	/* Enable RSS hashing on vnic */
10892 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10893 	if (rc) {
10894 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10895 			   vnic->vnic_id, rc);
10896 		goto vnic_setup_err;
10897 	}
10898 
10899 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10900 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10901 		if (rc) {
10902 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10903 				   vnic->vnic_id, rc);
10904 		}
10905 	}
10906 
10907 vnic_setup_err:
10908 	return rc;
10909 }
10910 
10911 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10912 			  u8 valid)
10913 {
10914 	struct hwrm_vnic_update_input *req;
10915 	int rc;
10916 
10917 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10918 	if (rc)
10919 		return rc;
10920 
10921 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10922 
10923 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10924 		req->mru = cpu_to_le16(vnic->mru);
10925 
10926 	req->enables = cpu_to_le32(valid);
10927 
10928 	return hwrm_req_send(bp, req);
10929 }
10930 
10931 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10932 {
10933 	int rc;
10934 
10935 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10936 	if (rc) {
10937 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10938 			   vnic->vnic_id, rc);
10939 		return rc;
10940 	}
10941 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10942 	if (rc)
10943 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10944 			   vnic->vnic_id, rc);
10945 	return rc;
10946 }
10947 
10948 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10949 {
10950 	int rc, i, nr_ctxs;
10951 
10952 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10953 	for (i = 0; i < nr_ctxs; i++) {
10954 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10955 		if (rc) {
10956 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10957 				   vnic->vnic_id, i, rc);
10958 			break;
10959 		}
10960 		bp->rsscos_nr_ctxs++;
10961 	}
10962 	if (i < nr_ctxs)
10963 		return -ENOMEM;
10964 
10965 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10966 	if (rc)
10967 		return rc;
10968 
10969 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10970 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10971 		if (rc) {
10972 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10973 				   vnic->vnic_id, rc);
10974 		}
10975 	}
10976 	return rc;
10977 }
10978 
10979 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10980 {
10981 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10982 		return __bnxt_setup_vnic_p5(bp, vnic);
10983 	else
10984 		return __bnxt_setup_vnic(bp, vnic);
10985 }
10986 
10987 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10988 				     struct bnxt_vnic_info *vnic,
10989 				     u16 start_rx_ring_idx, int rx_rings)
10990 {
10991 	int rc;
10992 
10993 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10994 	if (rc) {
10995 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10996 			   vnic->vnic_id, rc);
10997 		return rc;
10998 	}
10999 	return bnxt_setup_vnic(bp, vnic);
11000 }
11001 
11002 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
11003 {
11004 	struct bnxt_vnic_info *vnic;
11005 	int i, rc = 0;
11006 
11007 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
11008 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
11009 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
11010 	}
11011 
11012 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11013 		return 0;
11014 
11015 	for (i = 0; i < bp->rx_nr_rings; i++) {
11016 		u16 vnic_id = i + 1;
11017 		u16 ring_id = i;
11018 
11019 		if (vnic_id >= bp->nr_vnics)
11020 			break;
11021 
11022 		vnic = &bp->vnic_info[vnic_id];
11023 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
11024 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
11025 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
11026 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
11027 			break;
11028 	}
11029 	return rc;
11030 }
11031 
11032 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
11033 			  bool all)
11034 {
11035 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11036 	struct bnxt_filter_base *usr_fltr, *tmp;
11037 	struct bnxt_ntuple_filter *ntp_fltr;
11038 	int i;
11039 
11040 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
11041 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
11042 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
11043 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
11044 	}
11045 	if (!all)
11046 		return;
11047 
11048 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
11049 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
11050 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
11051 			ntp_fltr = container_of(usr_fltr,
11052 						struct bnxt_ntuple_filter,
11053 						base);
11054 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
11055 			bnxt_del_ntp_filter(bp, ntp_fltr);
11056 			bnxt_del_one_usr_fltr(bp, usr_fltr);
11057 		}
11058 	}
11059 
11060 	if (vnic->rss_table)
11061 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
11062 				  vnic->rss_table,
11063 				  vnic->rss_table_dma_addr);
11064 	bp->num_rss_ctx--;
11065 }
11066 
11067 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11068 				  int rxr_id)
11069 {
11070 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
11071 	int i, vnic_rx;
11072 
11073 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
11074 	 * must be updated because a future filter may use it.
11075 	 */
11076 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
11077 		return true;
11078 
11079 	for (i = 0; i < tbl_size; i++) {
11080 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
11081 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
11082 		else
11083 			vnic_rx = bp->rss_indir_tbl[i];
11084 
11085 		if (rxr_id == vnic_rx)
11086 			return true;
11087 	}
11088 
11089 	return false;
11090 }
11091 
11092 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11093 				u16 mru, int rxr_id)
11094 {
11095 	int rc;
11096 
11097 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
11098 		return 0;
11099 
11100 	if (mru) {
11101 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11102 		if (rc) {
11103 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11104 				   vnic->vnic_id, rc);
11105 			return rc;
11106 		}
11107 		if (rxr_id == vnic->default_rx_ring) {
11108 			rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11109 			if (rc)
11110 				return rc;
11111 		}
11112 	}
11113 	vnic->mru = mru;
11114 	bnxt_hwrm_vnic_update(bp, vnic,
11115 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
11116 
11117 	return 0;
11118 }
11119 
11120 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
11121 {
11122 	struct ethtool_rxfh_context *ctx;
11123 	unsigned long context;
11124 	int rc;
11125 
11126 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11127 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11128 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11129 
11130 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
11131 		if (rc)
11132 			return rc;
11133 	}
11134 
11135 	return 0;
11136 }
11137 
11138 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
11139 {
11140 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
11141 	struct ethtool_rxfh_context *ctx;
11142 	unsigned long context;
11143 
11144 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11145 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11146 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11147 
11148 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
11149 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
11150 		    __bnxt_setup_vnic_p5(bp, vnic)) {
11151 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
11152 				   rss_ctx->index);
11153 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
11154 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
11155 		}
11156 	}
11157 }
11158 
11159 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
11160 {
11161 	struct ethtool_rxfh_context *ctx;
11162 	unsigned long context;
11163 
11164 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11165 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11166 
11167 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11168 	}
11169 }
11170 
11171 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11172 static bool bnxt_promisc_ok(struct bnxt *bp)
11173 {
11174 #ifdef CONFIG_BNXT_SRIOV
11175 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11176 		return false;
11177 #endif
11178 	return true;
11179 }
11180 
11181 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11182 {
11183 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11184 	unsigned int rc = 0;
11185 
11186 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11187 	if (rc) {
11188 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11189 			   rc);
11190 		return rc;
11191 	}
11192 
11193 	/* Setup the proper default RX ring */
11194 	bnxt_fill_hw_rss_tbl(bp, vnic);
11195 
11196 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11197 	if (rc) {
11198 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11199 			   rc);
11200 		return rc;
11201 	}
11202 	return rc;
11203 }
11204 
11205 static int bnxt_cfg_rx_mode(struct bnxt *, struct netdev_hw_addr_list *, bool);
11206 static bool bnxt_mc_list_updated(struct bnxt *, u32 *,
11207 				 const struct netdev_hw_addr_list *);
11208 
11209 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11210 {
11211 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11212 	int rc = 0;
11213 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11214 
11215 	if (irq_re_init) {
11216 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11217 		if (rc) {
11218 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11219 				   rc);
11220 			goto err_out;
11221 		}
11222 	}
11223 
11224 	rc = bnxt_hwrm_ring_alloc(bp);
11225 	if (rc) {
11226 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11227 		goto err_out;
11228 	}
11229 
11230 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11231 	if (rc) {
11232 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11233 		goto err_out;
11234 	}
11235 
11236 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11237 		rx_nr_rings--;
11238 
11239 	/* default vnic 0 */
11240 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11241 	if (rc) {
11242 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11243 		goto err_out;
11244 	}
11245 
11246 	if (BNXT_VF(bp))
11247 		bnxt_hwrm_func_qcfg(bp);
11248 
11249 	rc = bnxt_setup_vnic(bp, vnic);
11250 	if (rc)
11251 		goto err_out;
11252 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11253 		bnxt_hwrm_update_rss_hash_cfg(bp);
11254 
11255 	if (bp->flags & BNXT_FLAG_RFS) {
11256 		rc = bnxt_alloc_rfs_vnics(bp);
11257 		if (rc)
11258 			goto err_out;
11259 	}
11260 
11261 	if (bp->flags & BNXT_FLAG_TPA) {
11262 		rc = bnxt_set_tpa(bp, true);
11263 		if (rc)
11264 			goto err_out;
11265 	}
11266 
11267 	if (BNXT_VF(bp))
11268 		bnxt_update_vf_mac(bp);
11269 
11270 	/* Filter for default vnic 0 */
11271 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11272 	if (rc) {
11273 		if (BNXT_VF(bp) && rc == -ENODEV)
11274 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11275 		else
11276 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11277 		goto err_out;
11278 	}
11279 	vnic->uc_filter_count = 1;
11280 
11281 	vnic->rx_mask = 0;
11282 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11283 		goto skip_rx_mask;
11284 
11285 	if (bp->dev->flags & IFF_BROADCAST)
11286 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11287 
11288 	if (bp->dev->flags & IFF_PROMISC)
11289 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11290 
11291 	if (bp->dev->flags & IFF_ALLMULTI) {
11292 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11293 		vnic->mc_list_count = 0;
11294 	} else if (bp->dev->flags & IFF_MULTICAST) {
11295 		u32 mask = 0;
11296 
11297 		bnxt_mc_list_updated(bp, &mask, &bp->dev->mc);
11298 		vnic->rx_mask |= mask;
11299 	}
11300 
11301 	rc = bnxt_cfg_rx_mode(bp, &bp->dev->uc, true);
11302 	if (rc == -EAGAIN) {
11303 		netif_rx_mode_schedule_retry(bp->dev);
11304 		rc = 0;
11305 	} else if (rc) {
11306 		goto err_out;
11307 	}
11308 
11309 skip_rx_mask:
11310 	rc = bnxt_hwrm_set_coal(bp);
11311 	if (rc)
11312 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11313 				rc);
11314 
11315 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11316 		rc = bnxt_setup_nitroa0_vnic(bp);
11317 		if (rc)
11318 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11319 				   rc);
11320 	}
11321 
11322 	if (BNXT_VF(bp)) {
11323 		bnxt_hwrm_func_qcfg(bp);
11324 		netdev_update_features(bp->dev);
11325 	}
11326 
11327 	return 0;
11328 
11329 err_out:
11330 	bnxt_hwrm_resource_free(bp, 0, true);
11331 
11332 	return rc;
11333 }
11334 
11335 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11336 {
11337 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11338 	return 0;
11339 }
11340 
11341 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11342 {
11343 	bnxt_init_cp_rings(bp);
11344 	bnxt_init_rx_rings(bp);
11345 	bnxt_init_tx_rings(bp);
11346 	bnxt_init_ring_grps(bp, irq_re_init);
11347 	bnxt_init_vnics(bp);
11348 
11349 	return bnxt_init_chip(bp, irq_re_init);
11350 }
11351 
11352 static int bnxt_set_real_num_queues(struct bnxt *bp)
11353 {
11354 	int rc;
11355 	struct net_device *dev = bp->dev;
11356 
11357 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11358 					  bp->tx_nr_rings_xdp);
11359 	if (rc)
11360 		return rc;
11361 
11362 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11363 	if (rc)
11364 		return rc;
11365 
11366 #ifdef CONFIG_RFS_ACCEL
11367 	if (bp->flags & BNXT_FLAG_RFS)
11368 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11369 #endif
11370 
11371 	return rc;
11372 }
11373 
11374 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11375 			     bool shared)
11376 {
11377 	int _rx = *rx, _tx = *tx;
11378 
11379 	if (shared) {
11380 		*rx = min_t(int, _rx, max);
11381 		*tx = min_t(int, _tx, max);
11382 	} else {
11383 		if (max < 2)
11384 			return -ENOMEM;
11385 
11386 		while (_rx + _tx > max) {
11387 			if (_rx > _tx && _rx > 1)
11388 				_rx--;
11389 			else if (_tx > 1)
11390 				_tx--;
11391 		}
11392 		*rx = _rx;
11393 		*tx = _tx;
11394 	}
11395 	return 0;
11396 }
11397 
11398 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11399 {
11400 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11401 }
11402 
11403 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11404 {
11405 	int tcs = bp->num_tc;
11406 
11407 	if (!tcs)
11408 		tcs = 1;
11409 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11410 }
11411 
11412 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11413 {
11414 	int tcs = bp->num_tc;
11415 
11416 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11417 	       bp->tx_nr_rings_xdp;
11418 }
11419 
11420 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11421 			   bool sh)
11422 {
11423 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11424 
11425 	if (tx_cp != *tx) {
11426 		int tx_saved = tx_cp, rc;
11427 
11428 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11429 		if (rc)
11430 			return rc;
11431 		if (tx_cp != tx_saved)
11432 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11433 		return 0;
11434 	}
11435 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11436 }
11437 
11438 static void bnxt_setup_msix(struct bnxt *bp)
11439 {
11440 	const int len = sizeof(bp->irq_tbl[0].name);
11441 	struct net_device *dev = bp->dev;
11442 	int tcs, i;
11443 
11444 	tcs = bp->num_tc;
11445 	if (tcs) {
11446 		int i, off, count;
11447 
11448 		for (i = 0; i < tcs; i++) {
11449 			count = bp->tx_nr_rings_per_tc;
11450 			off = BNXT_TC_TO_RING_BASE(bp, i);
11451 			netdev_set_tc_queue(dev, i, count, off);
11452 		}
11453 	}
11454 
11455 	for (i = 0; i < bp->cp_nr_rings; i++) {
11456 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11457 		char *attr;
11458 
11459 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11460 			attr = "TxRx";
11461 		else if (i < bp->rx_nr_rings)
11462 			attr = "rx";
11463 		else
11464 			attr = "tx";
11465 
11466 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11467 			 attr, i);
11468 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11469 	}
11470 }
11471 
11472 static int bnxt_init_int_mode(struct bnxt *bp);
11473 
11474 static int bnxt_change_msix(struct bnxt *bp, int total)
11475 {
11476 	struct msi_map map;
11477 	int i;
11478 
11479 	/* add MSIX to the end if needed */
11480 	for (i = bp->total_irqs; i < total; i++) {
11481 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11482 		if (map.index < 0)
11483 			return bp->total_irqs;
11484 		bp->irq_tbl[i].vector = map.virq;
11485 		bp->total_irqs++;
11486 	}
11487 
11488 	/* trim MSIX from the end if needed */
11489 	for (i = bp->total_irqs; i > total; i--) {
11490 		map.index = i - 1;
11491 		map.virq = bp->irq_tbl[i - 1].vector;
11492 		pci_msix_free_irq(bp->pdev, map);
11493 		bp->total_irqs--;
11494 	}
11495 	return bp->total_irqs;
11496 }
11497 
11498 static int bnxt_setup_int_mode(struct bnxt *bp)
11499 {
11500 	int rc;
11501 
11502 	if (!bp->irq_tbl) {
11503 		rc = bnxt_init_int_mode(bp);
11504 		if (rc || !bp->irq_tbl)
11505 			return rc ?: -ENODEV;
11506 	}
11507 
11508 	bnxt_setup_msix(bp);
11509 
11510 	rc = bnxt_set_real_num_queues(bp);
11511 	return rc;
11512 }
11513 
11514 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11515 {
11516 	return bp->hw_resc.max_rsscos_ctxs;
11517 }
11518 
11519 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11520 {
11521 	return bp->hw_resc.max_vnics;
11522 }
11523 
11524 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11525 {
11526 	return bp->hw_resc.max_stat_ctxs;
11527 }
11528 
11529 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11530 {
11531 	return bp->hw_resc.max_cp_rings;
11532 }
11533 
11534 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11535 {
11536 	unsigned int cp = bp->hw_resc.max_cp_rings;
11537 
11538 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11539 		cp -= bnxt_get_ulp_msix_num(bp);
11540 
11541 	return cp;
11542 }
11543 
11544 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11545 {
11546 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11547 
11548 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11549 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11550 
11551 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11552 }
11553 
11554 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11555 {
11556 	bp->hw_resc.max_irqs = max_irqs;
11557 }
11558 
11559 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11560 {
11561 	unsigned int cp;
11562 
11563 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11564 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11565 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11566 	else
11567 		return cp - bp->cp_nr_rings;
11568 }
11569 
11570 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11571 {
11572 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11573 }
11574 
11575 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11576 {
11577 	int max_irq = bnxt_get_max_func_irqs(bp);
11578 	int total_req = bp->cp_nr_rings + num;
11579 
11580 	if (max_irq < total_req) {
11581 		num = max_irq - bp->cp_nr_rings;
11582 		if (num <= 0)
11583 			return 0;
11584 	}
11585 	return num;
11586 }
11587 
11588 static int bnxt_get_num_msix(struct bnxt *bp)
11589 {
11590 	if (!BNXT_NEW_RM(bp))
11591 		return bnxt_get_max_func_irqs(bp);
11592 
11593 	return bnxt_nq_rings_in_use(bp);
11594 }
11595 
11596 static int bnxt_init_int_mode(struct bnxt *bp)
11597 {
11598 	int i, total_vecs, max, rc, min = 1, ulp_msix, tx_cp, tbl_size;
11599 
11600 	total_vecs = bnxt_get_num_msix(bp);
11601 	max = bnxt_get_max_func_irqs(bp);
11602 	if (total_vecs > max)
11603 		total_vecs = max;
11604 
11605 	if (!total_vecs)
11606 		return 0;
11607 
11608 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11609 		min = 2;
11610 
11611 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11612 					   PCI_IRQ_MSIX);
11613 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11614 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11615 		rc = -ENODEV;
11616 		goto msix_setup_exit;
11617 	}
11618 
11619 	tbl_size = total_vecs;
11620 	if (pci_msix_can_alloc_dyn(bp->pdev))
11621 		tbl_size = max;
11622 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11623 	if (!bp->irq_tbl) {
11624 		rc = -ENOMEM;
11625 		goto msix_setup_exit;
11626 	}
11627 
11628 	for (i = 0; i < total_vecs; i++)
11629 		bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11630 
11631 	bp->total_irqs = total_vecs;
11632 	/* Trim rings based upon num of vectors allocated */
11633 	rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11634 			     total_vecs - ulp_msix, min == 1);
11635 	if (rc)
11636 		goto msix_setup_exit;
11637 
11638 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11639 	bp->cp_nr_rings = (min == 1) ?
11640 			  max_t(int, tx_cp, bp->rx_nr_rings) :
11641 			  tx_cp + bp->rx_nr_rings;
11642 
11643 	return 0;
11644 
11645 msix_setup_exit:
11646 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11647 	kfree(bp->irq_tbl);
11648 	bp->irq_tbl = NULL;
11649 	pci_free_irq_vectors(bp->pdev);
11650 	return rc;
11651 }
11652 
11653 static void bnxt_clear_int_mode(struct bnxt *bp)
11654 {
11655 	pci_free_irq_vectors(bp->pdev);
11656 
11657 	kfree(bp->irq_tbl);
11658 	bp->irq_tbl = NULL;
11659 }
11660 
11661 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11662 {
11663 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
11664 	bool irq_cleared = false;
11665 	bool irq_change = false;
11666 	int tcs = bp->num_tc;
11667 	int irqs_required;
11668 	int rc;
11669 
11670 	if (!bnxt_need_reserve_rings(bp))
11671 		return 0;
11672 
11673 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
11674 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11675 
11676 		if (ulp_msix > bp->ulp_num_msix_want)
11677 			ulp_msix = bp->ulp_num_msix_want;
11678 		irqs_required = ulp_msix + bp->cp_nr_rings;
11679 	} else {
11680 		irqs_required = bnxt_get_num_msix(bp);
11681 	}
11682 
11683 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11684 		irq_change = true;
11685 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11686 			bnxt_ulp_irq_stop(bp);
11687 			bnxt_clear_int_mode(bp);
11688 			irq_cleared = true;
11689 		}
11690 	}
11691 	rc = __bnxt_reserve_rings(bp);
11692 	if (irq_cleared) {
11693 		if (!rc)
11694 			rc = bnxt_init_int_mode(bp);
11695 		bnxt_ulp_irq_restart(bp, rc);
11696 	} else if (irq_change && !rc) {
11697 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11698 			rc = -ENOSPC;
11699 	}
11700 	if (rc) {
11701 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11702 		return rc;
11703 	}
11704 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11705 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11706 		netdev_err(bp->dev, "tx ring reservation failure\n");
11707 		netdev_reset_tc(bp->dev);
11708 		bp->num_tc = 0;
11709 		if (bp->tx_nr_rings_xdp)
11710 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11711 		else
11712 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11713 		return -ENOMEM;
11714 	}
11715 	return 0;
11716 }
11717 
11718 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11719 {
11720 	struct bnxt_tx_ring_info *txr;
11721 	struct netdev_queue *txq;
11722 	struct bnxt_napi *bnapi;
11723 	int i;
11724 
11725 	bnapi = bp->bnapi[idx];
11726 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11727 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11728 		synchronize_net();
11729 
11730 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11731 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11732 			if (txq) {
11733 				__netif_tx_lock_bh(txq);
11734 				netif_tx_stop_queue(txq);
11735 				__netif_tx_unlock_bh(txq);
11736 			}
11737 		}
11738 
11739 		if (!bp->tph_mode)
11740 			continue;
11741 
11742 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11743 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11744 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11745 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11746 	}
11747 }
11748 
11749 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11750 {
11751 	struct bnxt_tx_ring_info *txr;
11752 	struct netdev_queue *txq;
11753 	struct bnxt_napi *bnapi;
11754 	int rc, i;
11755 
11756 	bnapi = bp->bnapi[idx];
11757 	/* All rings have been reserved and previously allocated.
11758 	 * Reallocating with the same parameters should never fail.
11759 	 */
11760 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11761 		if (!bp->tph_mode)
11762 			goto start_tx;
11763 
11764 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11765 		if (rc)
11766 			return rc;
11767 
11768 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11769 		if (rc)
11770 			return rc;
11771 
11772 		txr->tx_prod = 0;
11773 		txr->tx_cons = 0;
11774 		txr->tx_hw_cons = 0;
11775 start_tx:
11776 		WRITE_ONCE(txr->dev_state, 0);
11777 		synchronize_net();
11778 
11779 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11780 			continue;
11781 
11782 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11783 		if (txq)
11784 			netif_tx_start_queue(txq);
11785 	}
11786 
11787 	return 0;
11788 }
11789 
11790 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11791 				     const cpumask_t *mask)
11792 {
11793 	struct bnxt_irq *irq;
11794 	u16 tag;
11795 
11796 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11797 
11798 	cpumask_copy(irq->bp->ring_cpu_mask[irq->ring_nr], mask);
11799 	set_bit(irq->ring_nr, irq->bp->ring_affinity_set);
11800 
11801 #ifdef CONFIG_RFS_ACCEL
11802 	if (irq->bp->dev->rx_cpu_rmap && irq->ring_nr < irq->bp->rx_nr_rings) {
11803 		int err;
11804 
11805 		err = cpu_rmap_update(irq->bp->dev->rx_cpu_rmap, irq->ring_nr,
11806 				      mask);
11807 		if (err)
11808 			netdev_warn(irq->bp->dev,
11809 				    "aRFS rmap update failed: %d\n", err);
11810 	}
11811 #endif
11812 
11813 	if (!irq->bp->tph_mode)
11814 		return;
11815 
11816 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11817 		return;
11818 
11819 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11820 				cpumask_first(mask), &tag))
11821 		return;
11822 
11823 	WRITE_ONCE(irq->new_tag, tag);
11824 	bnxt_queue_sp_work(irq->bp, BNXT_TPH_UPDATE_SP_EVENT);
11825 }
11826 
11827 static void bnxt_irq_affinity_release(struct kref *ref)
11828 {
11829 	struct irq_affinity_notify *notify =
11830 		container_of(ref, struct irq_affinity_notify, kref);
11831 	struct bnxt_irq *irq;
11832 
11833 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11834 
11835 	if (!irq->bp->tph_mode)
11836 		return;
11837 
11838 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11839 		netdev_err(irq->bp->dev,
11840 			   "Setting ST=0 for MSIX entry %d failed\n",
11841 			   irq->msix_nr);
11842 		return;
11843 	}
11844 }
11845 
11846 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11847 {
11848 	irq_set_affinity_notifier(irq->vector, NULL);
11849 }
11850 
11851 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11852 {
11853 	struct irq_affinity_notify *notify;
11854 
11855 	irq->bp = bp;
11856 
11857 	/* Register IRQ affinity notifier */
11858 	notify = &irq->affinity_notify;
11859 	notify->irq = irq->vector;
11860 	notify->notify = bnxt_irq_affinity_notify;
11861 	notify->release = bnxt_irq_affinity_release;
11862 
11863 	irq_set_affinity_notifier(irq->vector, notify);
11864 }
11865 
11866 static int bnxt_alloc_ring_cpu_masks(struct bnxt *bp)
11867 {
11868 	int i;
11869 
11870 	bp->ring_cpu_mask = kzalloc_objs(*bp->ring_cpu_mask, bp->max_irqs);
11871 	if (!bp->ring_cpu_mask)
11872 		return -ENOMEM;
11873 
11874 	bp->ring_affinity_set = bitmap_zalloc(bp->max_irqs, GFP_KERNEL);
11875 	if (!bp->ring_affinity_set)
11876 		return -ENOMEM;
11877 
11878 	for (i = 0; i < bp->max_irqs; i++)
11879 		if (!zalloc_cpumask_var(&bp->ring_cpu_mask[i], GFP_KERNEL))
11880 			return -ENOMEM;
11881 
11882 	return 0;
11883 }
11884 
11885 static void bnxt_free_ring_cpu_masks(struct bnxt *bp)
11886 {
11887 	int i;
11888 
11889 	if (!bp->ring_cpu_mask)
11890 		return;
11891 
11892 	for (i = 0; i < bp->max_irqs; i++)
11893 		free_cpumask_var(bp->ring_cpu_mask[i]);
11894 
11895 	bitmap_free(bp->ring_affinity_set);
11896 	bp->ring_affinity_set = NULL;
11897 	kfree(bp->ring_cpu_mask);
11898 	bp->ring_cpu_mask = NULL;
11899 }
11900 
11901 static void bnxt_free_irq(struct bnxt *bp)
11902 {
11903 	struct bnxt_irq *irq;
11904 	int i;
11905 
11906 	if (!bp->irq_tbl || !bp->bnapi)
11907 		return;
11908 
11909 	for (i = 0; i < bp->cp_nr_rings; i++) {
11910 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11911 
11912 		irq = &bp->irq_tbl[map_idx];
11913 		if (irq->requested) {
11914 			bnxt_release_irq_notifier(irq);
11915 			irq_update_affinity_hint(irq->vector, NULL);
11916 			free_irq(irq->vector, bp->bnapi[i]);
11917 		}
11918 
11919 		irq->requested = 0;
11920 		irq->tag = 0;
11921 		irq->new_tag = 0;
11922 	}
11923 
11924 	/* Disable TPH support */
11925 	pcie_disable_tph(bp->pdev);
11926 	bp->tph_mode = 0;
11927 
11928 #ifdef CONFIG_RFS_ACCEL
11929 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11930 	bp->dev->rx_cpu_rmap = NULL;
11931 #endif
11932 }
11933 
11934 static int bnxt_request_irq(struct bnxt *bp)
11935 {
11936 	const int numa_node = dev_to_node(&bp->pdev->dev);
11937 	struct cpu_rmap *rmap = NULL;
11938 	int i, j, rc = 0;
11939 	unsigned long flags = 0;
11940 
11941 	rc = bnxt_setup_int_mode(bp);
11942 	if (rc) {
11943 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11944 			   rc);
11945 		return rc;
11946 	}
11947 #ifdef CONFIG_RFS_ACCEL
11948 	rmap = bp->dev->rx_cpu_rmap;
11949 #endif
11950 
11951 	/* Enable TPH support as part of IRQ request */
11952 	if (BNXT_SUPPORTS_QUEUE_API(bp)) {
11953 		rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11954 		if (!rc)
11955 			bp->tph_mode = PCI_TPH_ST_IV_MODE;
11956 	}
11957 
11958 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11959 		struct cpumask *cpu_mask = bp->ring_cpu_mask[i];
11960 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11961 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11962 		u16 tag;
11963 
11964 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11965 		    rmap && bp->bnapi[i]->rx_ring) {
11966 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11967 			if (rc)
11968 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11969 					    j);
11970 			j++;
11971 		}
11972 
11973 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11974 				 bp->bnapi[i]);
11975 		if (rc)
11976 			break;
11977 
11978 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11979 		irq->requested = 1;
11980 		irq->msix_nr = map_idx;
11981 		irq->ring_nr = i;
11982 
11983 		/* Reuse the mask recorded before the IRQs were freed. Nothing
11984 		 * was recorded yet on the very first request, and the mask
11985 		 * may have gone stale if the CPUs went offline in between.
11986 		 */
11987 		if (!test_bit(i, bp->ring_affinity_set) ||
11988 		    !cpumask_intersects(cpu_mask, cpu_online_mask)) {
11989 			clear_bit(i, bp->ring_affinity_set);
11990 			cpumask_clear(cpu_mask);
11991 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11992 					cpu_mask);
11993 		}
11994 
11995 		/* Init ST table entry if we can get the mapping */
11996 		if (!pcie_tph_get_cpu_st(bp->pdev, TPH_MEM_TYPE_VM,
11997 					 cpumask_first(cpu_mask), &tag)) {
11998 			pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag);
11999 			irq->tag = tag;
12000 			irq->new_tag = tag;
12001 		}
12002 
12003 		bnxt_register_irq_notifier(bp, irq);
12004 
12005 		/* Only put the IRQ back where it was configured to be, our own
12006 		 * placement is just a hint, the core spreads within
12007 		 * irq_default_affinity which we know nothing about.
12008 		 * Set after installing the notifier, if we race with the user
12009 		 * it's better to overwrite than miss the notification.
12010 		 */
12011 		if (test_bit(i, bp->ring_affinity_set))
12012 			rc = irq_set_affinity_and_hint(irq->vector, cpu_mask);
12013 		else
12014 			rc = irq_update_affinity_hint(irq->vector, cpu_mask);
12015 		if (rc) {
12016 			netdev_warn(bp->dev,
12017 				    "Setting IRQ affinity failed, IRQ = %d\n",
12018 				    irq->vector);
12019 			break;
12020 		}
12021 	}
12022 	return rc;
12023 }
12024 
12025 static void bnxt_del_napi(struct bnxt *bp)
12026 {
12027 	int i;
12028 
12029 	if (!bp->bnapi)
12030 		return;
12031 
12032 	for (i = 0; i < bp->rx_nr_rings; i++)
12033 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
12034 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
12035 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
12036 
12037 	for (i = 0; i < bp->cp_nr_rings; i++) {
12038 		struct bnxt_napi *bnapi = bp->bnapi[i];
12039 
12040 		__netif_napi_del_locked(&bnapi->napi);
12041 	}
12042 	/* We called __netif_napi_del_locked(), we need
12043 	 * to respect an RCU grace period before freeing napi structures.
12044 	 */
12045 	synchronize_net();
12046 }
12047 
12048 static void bnxt_init_napi(struct bnxt *bp)
12049 {
12050 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
12051 	unsigned int cp_nr_rings = bp->cp_nr_rings;
12052 	struct bnxt_napi *bnapi;
12053 	int i;
12054 
12055 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
12056 		poll_fn = bnxt_poll_p5;
12057 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
12058 		cp_nr_rings--;
12059 
12060 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12061 
12062 	for (i = 0; i < cp_nr_rings; i++) {
12063 		bnapi = bp->bnapi[i];
12064 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
12065 					     bnapi->index);
12066 	}
12067 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12068 		bnapi = bp->bnapi[cp_nr_rings];
12069 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
12070 	}
12071 }
12072 
12073 static void bnxt_disable_napi(struct bnxt *bp)
12074 {
12075 	int i;
12076 
12077 	if (!bp->bnapi ||
12078 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
12079 		return;
12080 
12081 	for (i = 0; i < bp->cp_nr_rings; i++) {
12082 		struct bnxt_napi *bnapi = bp->bnapi[i];
12083 		struct bnxt_cp_ring_info *cpr;
12084 
12085 		cpr = &bnapi->cp_ring;
12086 		if (bnapi->tx_fault)
12087 			cpr->sw_stats->tx.tx_resets++;
12088 		if (bnapi->in_reset)
12089 			cpr->sw_stats->rx.rx_resets++;
12090 		napi_disable_locked(&bnapi->napi);
12091 	}
12092 }
12093 
12094 static void bnxt_enable_napi(struct bnxt *bp)
12095 {
12096 	int i;
12097 
12098 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12099 	for (i = 0; i < bp->cp_nr_rings; i++) {
12100 		struct bnxt_napi *bnapi = bp->bnapi[i];
12101 		struct bnxt_cp_ring_info *cpr;
12102 
12103 		bnapi->tx_fault = 0;
12104 
12105 		cpr = &bnapi->cp_ring;
12106 		bnapi->in_reset = false;
12107 
12108 		if (bnapi->rx_ring) {
12109 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
12110 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
12111 		}
12112 		napi_enable_locked(&bnapi->napi);
12113 	}
12114 }
12115 
12116 void bnxt_tx_disable(struct bnxt *bp)
12117 {
12118 	int i;
12119 	struct bnxt_tx_ring_info *txr;
12120 
12121 	if (bp->tx_ring) {
12122 		for (i = 0; i < bp->tx_nr_rings; i++) {
12123 			txr = &bp->tx_ring[i];
12124 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
12125 		}
12126 	}
12127 	/* Make sure napi polls see @dev_state change */
12128 	synchronize_net();
12129 	/* Drop carrier first to prevent TX timeout */
12130 	netif_carrier_off(bp->dev);
12131 	/* Stop all TX queues */
12132 	netif_tx_disable(bp->dev);
12133 }
12134 
12135 void bnxt_tx_enable(struct bnxt *bp)
12136 {
12137 	int i;
12138 	struct bnxt_tx_ring_info *txr;
12139 
12140 	for (i = 0; i < bp->tx_nr_rings; i++) {
12141 		txr = &bp->tx_ring[i];
12142 		WRITE_ONCE(txr->dev_state, 0);
12143 	}
12144 	/* Make sure napi polls see @dev_state change */
12145 	synchronize_net();
12146 	netif_tx_wake_all_queues(bp->dev);
12147 	if (BNXT_LINK_IS_UP(bp))
12148 		netif_carrier_on(bp->dev);
12149 }
12150 
12151 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
12152 {
12153 	u8 active_fec = link_info->active_fec_sig_mode &
12154 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
12155 
12156 	switch (active_fec) {
12157 	default:
12158 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
12159 		return "None";
12160 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
12161 		return "Clause 74 BaseR";
12162 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
12163 		return "Clause 91 RS(528,514)";
12164 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
12165 		return "Clause 91 RS544_1XN";
12166 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
12167 		return "Clause 91 RS(544,514)";
12168 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
12169 		return "Clause 91 RS272_1XN";
12170 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
12171 		return "Clause 91 RS(272,257)";
12172 	}
12173 }
12174 
12175 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
12176 {
12177 	u8 reason = link_info->link_down_reason;
12178 
12179 	/* Multiple bits can be set, we report 1 bit only in order of
12180 	 * priority.
12181 	 */
12182 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
12183 		return "(Remote fault)";
12184 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
12185 		return "(OTP Speed limit violation)";
12186 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
12187 		return "(Cable removed)";
12188 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
12189 		return "(Module fault)";
12190 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
12191 		return "(BMC request down)";
12192 	return "";
12193 }
12194 
12195 void bnxt_report_link(struct bnxt *bp)
12196 {
12197 	if (BNXT_LINK_IS_UP(bp)) {
12198 		const char *signal = "";
12199 		const char *flow_ctrl;
12200 		const char *duplex;
12201 		u32 speed;
12202 		u16 fec;
12203 
12204 		netif_carrier_on(bp->dev);
12205 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
12206 		if (speed == SPEED_UNKNOWN) {
12207 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
12208 			return;
12209 		}
12210 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
12211 			duplex = "full";
12212 		else
12213 			duplex = "half";
12214 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
12215 			flow_ctrl = "ON - receive & transmit";
12216 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
12217 			flow_ctrl = "ON - transmit";
12218 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12219 			flow_ctrl = "ON - receive";
12220 		else
12221 			flow_ctrl = "none";
12222 		if (bp->link_info.phy_qcfg_resp.option_flags &
12223 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12224 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12225 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12226 			switch (sig_mode) {
12227 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12228 				signal = "(NRZ) ";
12229 				break;
12230 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12231 				signal = "(PAM4 56Gbps) ";
12232 				break;
12233 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12234 				signal = "(PAM4 112Gbps) ";
12235 				break;
12236 			default:
12237 				break;
12238 			}
12239 		}
12240 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12241 			    speed, signal, duplex, flow_ctrl);
12242 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12243 			netdev_info(bp->dev, "EEE is %s\n",
12244 				    bp->eee.eee_active ? "active" :
12245 							 "not active");
12246 		fec = bp->link_info.fec_cfg;
12247 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12248 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12249 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12250 				    bnxt_report_fec(&bp->link_info));
12251 	} else {
12252 		char *str = bnxt_link_down_reason(&bp->link_info);
12253 
12254 		netif_carrier_off(bp->dev);
12255 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12256 	}
12257 }
12258 
12259 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12260 {
12261 	if (!resp->supported_speeds_auto_mode &&
12262 	    !resp->supported_speeds_force_mode &&
12263 	    !resp->supported_pam4_speeds_auto_mode &&
12264 	    !resp->supported_pam4_speeds_force_mode &&
12265 	    !resp->supported_speeds2_auto_mode &&
12266 	    !resp->supported_speeds2_force_mode)
12267 		return true;
12268 	return false;
12269 }
12270 
12271 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12272 {
12273 	struct bnxt_link_info *link_info = &bp->link_info;
12274 	struct hwrm_port_phy_qcaps_output *resp;
12275 	struct hwrm_port_phy_qcaps_input *req;
12276 	int rc = 0;
12277 
12278 	if (bp->hwrm_spec_code < 0x10201)
12279 		return 0;
12280 
12281 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12282 	if (rc)
12283 		return rc;
12284 
12285 	resp = hwrm_req_hold(bp, req);
12286 	rc = hwrm_req_send(bp, req);
12287 	if (rc)
12288 		goto hwrm_phy_qcaps_exit;
12289 
12290 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12291 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12292 		struct ethtool_keee *eee = &bp->eee;
12293 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12294 
12295 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12296 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12297 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12298 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12299 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12300 	}
12301 
12302 	if (bp->hwrm_spec_code >= 0x10a01) {
12303 		if (bnxt_phy_qcaps_no_speed(resp)) {
12304 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12305 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12306 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12307 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12308 			netdev_info(bp->dev, "Ethernet link enabled\n");
12309 			/* Phy re-enabled, reprobe the speeds */
12310 			link_info->support_auto_speeds = 0;
12311 			link_info->support_pam4_auto_speeds = 0;
12312 			link_info->support_auto_speeds2 = 0;
12313 		}
12314 	}
12315 	if (resp->supported_speeds_auto_mode)
12316 		link_info->support_auto_speeds =
12317 			le16_to_cpu(resp->supported_speeds_auto_mode);
12318 	if (resp->supported_pam4_speeds_auto_mode)
12319 		link_info->support_pam4_auto_speeds =
12320 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12321 	if (resp->supported_speeds2_auto_mode)
12322 		link_info->support_auto_speeds2 =
12323 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12324 
12325 	bp->port_count = resp->port_cnt;
12326 
12327 hwrm_phy_qcaps_exit:
12328 	hwrm_req_drop(bp, req);
12329 	return rc;
12330 }
12331 
12332 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12333 {
12334 	struct hwrm_port_mac_qcaps_output *resp;
12335 	struct hwrm_port_mac_qcaps_input *req;
12336 	int rc;
12337 
12338 	if (bp->hwrm_spec_code < 0x10a03)
12339 		return;
12340 
12341 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12342 	if (rc)
12343 		return;
12344 
12345 	resp = hwrm_req_hold(bp, req);
12346 	rc = hwrm_req_send_silent(bp, req);
12347 	if (!rc)
12348 		bp->mac_flags = resp->flags;
12349 	hwrm_req_drop(bp, req);
12350 }
12351 
12352 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12353 {
12354 	u16 diff = advertising ^ supported;
12355 
12356 	return ((supported | diff) != supported);
12357 }
12358 
12359 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12360 {
12361 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12362 
12363 	/* Check if any advertised speeds are no longer supported. The caller
12364 	 * holds the link_lock mutex, so we can modify link_info settings.
12365 	 */
12366 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12367 		if (bnxt_support_dropped(link_info->advertising,
12368 					 link_info->support_auto_speeds2)) {
12369 			link_info->advertising = link_info->support_auto_speeds2;
12370 			return true;
12371 		}
12372 		return false;
12373 	}
12374 	if (bnxt_support_dropped(link_info->advertising,
12375 				 link_info->support_auto_speeds)) {
12376 		link_info->advertising = link_info->support_auto_speeds;
12377 		return true;
12378 	}
12379 	if (bnxt_support_dropped(link_info->advertising_pam4,
12380 				 link_info->support_pam4_auto_speeds)) {
12381 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12382 		return true;
12383 	}
12384 	return false;
12385 }
12386 
12387 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12388 {
12389 	struct bnxt_link_info *link_info = &bp->link_info;
12390 	struct hwrm_port_phy_qcfg_output *resp;
12391 	struct hwrm_port_phy_qcfg_input *req;
12392 	u8 link_state = link_info->link_state;
12393 	bool support_changed;
12394 	int rc;
12395 
12396 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12397 	if (rc)
12398 		return rc;
12399 
12400 	resp = hwrm_req_hold(bp, req);
12401 	rc = hwrm_req_send(bp, req);
12402 	if (rc) {
12403 		hwrm_req_drop(bp, req);
12404 		if (BNXT_VF(bp) && rc == -ENODEV) {
12405 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12406 			rc = 0;
12407 		}
12408 		return rc;
12409 	}
12410 
12411 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12412 	link_info->phy_link_status = resp->link;
12413 	link_info->duplex = resp->duplex_cfg;
12414 	if (bp->hwrm_spec_code >= 0x10800)
12415 		link_info->duplex = resp->duplex_state;
12416 	link_info->pause = resp->pause;
12417 	link_info->auto_mode = resp->auto_mode;
12418 	link_info->auto_pause_setting = resp->auto_pause;
12419 	link_info->lp_pause = resp->link_partner_adv_pause;
12420 	link_info->force_pause_setting = resp->force_pause;
12421 	link_info->duplex_setting = resp->duplex_cfg;
12422 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12423 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12424 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12425 			link_info->active_lanes = resp->active_lanes;
12426 	} else {
12427 		link_info->link_speed = 0;
12428 		link_info->active_lanes = 0;
12429 	}
12430 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12431 	link_info->force_pam4_link_speed =
12432 		le16_to_cpu(resp->force_pam4_link_speed);
12433 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12434 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12435 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12436 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12437 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12438 	link_info->auto_pam4_link_speeds =
12439 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12440 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12441 	link_info->lp_auto_link_speeds =
12442 		le16_to_cpu(resp->link_partner_adv_speeds);
12443 	link_info->lp_auto_pam4_link_speeds =
12444 		resp->link_partner_pam4_adv_speeds;
12445 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12446 	link_info->phy_ver[0] = resp->phy_maj;
12447 	link_info->phy_ver[1] = resp->phy_min;
12448 	link_info->phy_ver[2] = resp->phy_bld;
12449 	link_info->media_type = resp->media_type;
12450 	link_info->phy_type = resp->phy_type;
12451 	link_info->transceiver = resp->xcvr_pkg_type;
12452 	link_info->phy_addr = resp->eee_config_phy_addr &
12453 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12454 	link_info->module_status = resp->module_status;
12455 	link_info->link_down_reason = resp->link_down_reason;
12456 
12457 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12458 		struct ethtool_keee *eee = &bp->eee;
12459 		u16 fw_speeds;
12460 
12461 		eee->eee_active = 0;
12462 		if (resp->eee_config_phy_addr &
12463 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12464 			eee->eee_active = 1;
12465 			fw_speeds = le16_to_cpu(
12466 				resp->link_partner_adv_eee_link_speed_mask);
12467 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12468 		}
12469 
12470 		/* Pull initial EEE config */
12471 		if (!chng_link_state) {
12472 			if (resp->eee_config_phy_addr &
12473 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12474 				eee->eee_enabled = 1;
12475 
12476 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12477 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12478 
12479 			if (resp->eee_config_phy_addr &
12480 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12481 				__le32 tmr;
12482 
12483 				eee->tx_lpi_enabled = 1;
12484 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12485 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12486 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12487 			}
12488 		}
12489 	}
12490 
12491 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12492 	if (bp->hwrm_spec_code >= 0x10504) {
12493 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12494 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12495 	}
12496 	/* TODO: need to add more logic to report VF link */
12497 	if (chng_link_state) {
12498 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12499 			link_info->link_state = BNXT_LINK_STATE_UP;
12500 		else
12501 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12502 		if (link_state != link_info->link_state)
12503 			bnxt_report_link(bp);
12504 	} else {
12505 		/* always link down if not require to update link state */
12506 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12507 	}
12508 	hwrm_req_drop(bp, req);
12509 
12510 	if (!BNXT_PHY_CFG_ABLE(bp))
12511 		return 0;
12512 
12513 	support_changed = bnxt_support_speed_dropped(link_info);
12514 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12515 		bnxt_hwrm_set_link_setting(bp, true, false);
12516 	return 0;
12517 }
12518 
12519 static void bnxt_get_port_module_status(struct bnxt *bp)
12520 {
12521 	struct bnxt_link_info *link_info = &bp->link_info;
12522 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12523 	u8 module_status;
12524 
12525 	if (bnxt_update_link(bp, true))
12526 		return;
12527 
12528 	module_status = link_info->module_status;
12529 	switch (module_status) {
12530 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12531 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12532 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12533 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12534 			    bp->pf.port_id);
12535 		if (bp->hwrm_spec_code >= 0x10201) {
12536 			netdev_warn(bp->dev, "Module part number %s\n",
12537 				    resp->phy_vendor_partnumber);
12538 		}
12539 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12540 			netdev_warn(bp->dev, "TX is disabled\n");
12541 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12542 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12543 	}
12544 }
12545 
12546 static void
12547 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12548 {
12549 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12550 		if (bp->hwrm_spec_code >= 0x10201)
12551 			req->auto_pause =
12552 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12553 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12554 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12555 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12556 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12557 		req->enables |=
12558 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12559 	} else {
12560 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12561 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12562 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12563 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12564 		req->enables |=
12565 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12566 		if (bp->hwrm_spec_code >= 0x10201) {
12567 			req->auto_pause = req->force_pause;
12568 			req->enables |= cpu_to_le32(
12569 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12570 		}
12571 	}
12572 }
12573 
12574 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12575 {
12576 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12577 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12578 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12579 			req->enables |=
12580 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12581 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12582 		} else if (bp->link_info.advertising) {
12583 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12584 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12585 		}
12586 		if (bp->link_info.advertising_pam4) {
12587 			req->enables |=
12588 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12589 			req->auto_link_pam4_speed_mask =
12590 				cpu_to_le16(bp->link_info.advertising_pam4);
12591 		}
12592 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12593 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12594 	} else {
12595 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12596 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12597 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12598 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12599 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12600 				   (u32)bp->link_info.req_link_speed);
12601 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12602 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12603 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12604 		} else {
12605 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12606 		}
12607 	}
12608 
12609 	/* tell chimp that the setting takes effect immediately */
12610 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12611 }
12612 
12613 int bnxt_hwrm_set_pause(struct bnxt *bp)
12614 {
12615 	struct hwrm_port_phy_cfg_input *req;
12616 	int rc;
12617 
12618 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12619 	if (rc)
12620 		return rc;
12621 
12622 	bnxt_hwrm_set_pause_common(bp, req);
12623 
12624 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12625 	    bp->link_info.force_link_chng)
12626 		bnxt_hwrm_set_link_common(bp, req);
12627 
12628 	rc = hwrm_req_send(bp, req);
12629 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12630 		/* since changing of pause setting doesn't trigger any link
12631 		 * change event, the driver needs to update the current pause
12632 		 * result upon successfully return of the phy_cfg command
12633 		 */
12634 		bp->link_info.pause =
12635 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12636 		bp->link_info.auto_pause_setting = 0;
12637 		if (!bp->link_info.force_link_chng)
12638 			bnxt_report_link(bp);
12639 	}
12640 	bp->link_info.force_link_chng = false;
12641 	return rc;
12642 }
12643 
12644 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12645 			      struct hwrm_port_phy_cfg_input *req)
12646 {
12647 	struct ethtool_keee *eee = &bp->eee;
12648 
12649 	if (eee->eee_enabled) {
12650 		u16 eee_speeds;
12651 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12652 
12653 		if (eee->tx_lpi_enabled)
12654 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12655 		else
12656 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12657 
12658 		req->flags |= cpu_to_le32(flags);
12659 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12660 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12661 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12662 	} else {
12663 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12664 	}
12665 }
12666 
12667 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12668 {
12669 	struct hwrm_port_phy_cfg_input *req;
12670 	int rc;
12671 
12672 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12673 	if (rc)
12674 		return rc;
12675 
12676 	if (set_pause)
12677 		bnxt_hwrm_set_pause_common(bp, req);
12678 
12679 	bnxt_hwrm_set_link_common(bp, req);
12680 
12681 	if (set_eee)
12682 		bnxt_hwrm_set_eee(bp, req);
12683 	return hwrm_req_send(bp, req);
12684 }
12685 
12686 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12687 {
12688 	struct hwrm_port_phy_cfg_input *req;
12689 	int rc;
12690 
12691 	if (!BNXT_SINGLE_PF(bp))
12692 		return 0;
12693 
12694 	if (pci_num_vf(bp->pdev) &&
12695 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12696 		return 0;
12697 
12698 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12699 	if (rc)
12700 		return rc;
12701 
12702 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12703 	rc = hwrm_req_send(bp, req);
12704 	if (!rc) {
12705 		mutex_lock(&bp->link_lock);
12706 		/* Device is not obliged link down in certain scenarios, even
12707 		 * when forced. Setting the state unknown is consistent with
12708 		 * driver startup and will force link state to be reported
12709 		 * during subsequent open based on PORT_PHY_QCFG.
12710 		 */
12711 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12712 		mutex_unlock(&bp->link_lock);
12713 	}
12714 	return rc;
12715 }
12716 
12717 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12718 {
12719 #ifdef CONFIG_TEE_BNXT_FW
12720 	int rc = tee_bnxt_fw_load();
12721 
12722 	if (rc)
12723 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12724 
12725 	return rc;
12726 #else
12727 	netdev_err(bp->dev, "OP-TEE not supported\n");
12728 	return -ENODEV;
12729 #endif
12730 }
12731 
12732 static int bnxt_try_recover_fw(struct bnxt *bp)
12733 {
12734 	if (bp->fw_health && bp->fw_health->status_reliable) {
12735 		int retry = 0, rc;
12736 		u32 sts;
12737 
12738 		do {
12739 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12740 			rc = bnxt_hwrm_poll(bp);
12741 			if (!BNXT_FW_IS_BOOTING(sts) &&
12742 			    !BNXT_FW_IS_RECOVERING(sts))
12743 				break;
12744 			retry++;
12745 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12746 
12747 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12748 			netdev_err(bp->dev,
12749 				   "Firmware not responding, status: 0x%x\n",
12750 				   sts);
12751 			rc = -ENODEV;
12752 		}
12753 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12754 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12755 			return bnxt_fw_reset_via_optee(bp);
12756 		}
12757 		return rc;
12758 	}
12759 
12760 	return -ENODEV;
12761 }
12762 
12763 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12764 {
12765 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12766 
12767 	if (!BNXT_NEW_RM(bp))
12768 		return; /* no resource reservations required */
12769 
12770 	hw_resc->resv_cp_rings = 0;
12771 	hw_resc->resv_stat_ctxs = 0;
12772 	hw_resc->resv_irqs = 0;
12773 	hw_resc->resv_tx_rings = 0;
12774 	hw_resc->resv_rx_rings = 0;
12775 	hw_resc->resv_hw_ring_grps = 0;
12776 	hw_resc->resv_vnics = 0;
12777 	hw_resc->resv_rsscos_ctxs = 0;
12778 	if (!fw_reset) {
12779 		bp->tx_nr_rings = 0;
12780 		bp->rx_nr_rings = 0;
12781 	}
12782 }
12783 
12784 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12785 {
12786 	int rc;
12787 
12788 	if (!BNXT_NEW_RM(bp))
12789 		return 0; /* no resource reservations required */
12790 
12791 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12792 	if (rc)
12793 		netdev_err(bp->dev, "resc_qcaps failed\n");
12794 
12795 	bnxt_clear_reservations(bp, fw_reset);
12796 
12797 	return rc;
12798 }
12799 
12800 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12801 {
12802 	struct hwrm_func_drv_if_change_output *resp;
12803 	struct hwrm_func_drv_if_change_input *req;
12804 	bool resc_reinit = false;
12805 	bool caps_change = false;
12806 	int rc, retry = 0;
12807 	bool fw_reset;
12808 	u32 flags = 0;
12809 
12810 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12811 	bp->fw_reset_state = 0;
12812 
12813 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12814 		return 0;
12815 
12816 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12817 	if (rc)
12818 		return rc;
12819 
12820 	if (up)
12821 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12822 	resp = hwrm_req_hold(bp, req);
12823 
12824 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12825 	while (retry < BNXT_FW_IF_RETRY) {
12826 		rc = hwrm_req_send(bp, req);
12827 		if (rc != -EAGAIN)
12828 			break;
12829 
12830 		msleep(50);
12831 		retry++;
12832 	}
12833 
12834 	if (rc == -EAGAIN) {
12835 		hwrm_req_drop(bp, req);
12836 		return rc;
12837 	} else if (!rc) {
12838 		flags = le32_to_cpu(resp->flags);
12839 	} else if (up) {
12840 		rc = bnxt_try_recover_fw(bp);
12841 		fw_reset = true;
12842 	}
12843 	hwrm_req_drop(bp, req);
12844 	if (rc)
12845 		return rc;
12846 
12847 	if (!up) {
12848 		bnxt_inv_fw_health_reg(bp);
12849 		return 0;
12850 	}
12851 
12852 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12853 		resc_reinit = true;
12854 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12855 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12856 		fw_reset = true;
12857 	else
12858 		bnxt_remap_fw_health_regs(bp);
12859 
12860 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12861 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12862 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12863 		return -ENODEV;
12864 	}
12865 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12866 		caps_change = true;
12867 
12868 	if (resc_reinit || fw_reset || caps_change) {
12869 		if (fw_reset || caps_change) {
12870 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12871 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12872 				bnxt_ulp_irq_stop(bp);
12873 			bnxt_free_ctx_mem(bp, false);
12874 			bnxt_dcb_free(bp);
12875 			rc = bnxt_fw_init_one(bp);
12876 			if (rc) {
12877 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12878 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12879 				return rc;
12880 			}
12881 			/* IRQ will be initialized later in bnxt_request_irq()*/
12882 			bnxt_clear_int_mode(bp);
12883 		}
12884 		rc = bnxt_cancel_reservations(bp, fw_reset);
12885 	}
12886 	return rc;
12887 }
12888 
12889 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12890 {
12891 	struct hwrm_port_led_qcaps_output *resp;
12892 	struct hwrm_port_led_qcaps_input *req;
12893 	struct bnxt_pf_info *pf = &bp->pf;
12894 	int rc;
12895 
12896 	bp->num_leds = 0;
12897 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12898 		return 0;
12899 
12900 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12901 	if (rc)
12902 		return rc;
12903 
12904 	req->port_id = cpu_to_le16(pf->port_id);
12905 	resp = hwrm_req_hold(bp, req);
12906 	rc = hwrm_req_send(bp, req);
12907 	if (rc) {
12908 		hwrm_req_drop(bp, req);
12909 		return rc;
12910 	}
12911 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12912 		int i;
12913 
12914 		bp->num_leds = resp->num_leds;
12915 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12916 						 bp->num_leds);
12917 		for (i = 0; i < bp->num_leds; i++) {
12918 			struct bnxt_led_info *led = &bp->leds[i];
12919 			__le16 caps = led->led_state_caps;
12920 
12921 			if (!led->led_group_id ||
12922 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12923 				bp->num_leds = 0;
12924 				break;
12925 			}
12926 		}
12927 	}
12928 	hwrm_req_drop(bp, req);
12929 	return 0;
12930 }
12931 
12932 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12933 {
12934 	struct hwrm_wol_filter_alloc_output *resp;
12935 	struct hwrm_wol_filter_alloc_input *req;
12936 	int rc;
12937 
12938 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12939 	if (rc)
12940 		return rc;
12941 
12942 	req->port_id = cpu_to_le16(bp->pf.port_id);
12943 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12944 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12945 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12946 
12947 	resp = hwrm_req_hold(bp, req);
12948 	rc = hwrm_req_send(bp, req);
12949 	if (!rc)
12950 		bp->wol_filter_id = resp->wol_filter_id;
12951 	hwrm_req_drop(bp, req);
12952 	return rc;
12953 }
12954 
12955 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12956 {
12957 	struct hwrm_wol_filter_free_input *req;
12958 	int rc;
12959 
12960 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12961 	if (rc)
12962 		return rc;
12963 
12964 	req->port_id = cpu_to_le16(bp->pf.port_id);
12965 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12966 	req->wol_filter_id = bp->wol_filter_id;
12967 
12968 	return hwrm_req_send(bp, req);
12969 }
12970 
12971 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12972 {
12973 	struct hwrm_wol_filter_qcfg_output *resp;
12974 	struct hwrm_wol_filter_qcfg_input *req;
12975 	u16 next_handle = 0;
12976 	int rc;
12977 
12978 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12979 	if (rc)
12980 		return rc;
12981 
12982 	req->port_id = cpu_to_le16(bp->pf.port_id);
12983 	req->handle = cpu_to_le16(handle);
12984 	resp = hwrm_req_hold(bp, req);
12985 	rc = hwrm_req_send(bp, req);
12986 	if (!rc) {
12987 		next_handle = le16_to_cpu(resp->next_handle);
12988 		if (next_handle != 0) {
12989 			if (resp->wol_type ==
12990 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12991 				bp->wol = 1;
12992 				bp->wol_filter_id = resp->wol_filter_id;
12993 			}
12994 		}
12995 	}
12996 	hwrm_req_drop(bp, req);
12997 	return next_handle;
12998 }
12999 
13000 static void bnxt_get_wol_settings(struct bnxt *bp)
13001 {
13002 	u16 handle = 0;
13003 
13004 	bp->wol = 0;
13005 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
13006 		return;
13007 
13008 	do {
13009 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
13010 	} while (handle && handle != 0xffff);
13011 }
13012 
13013 static bool bnxt_eee_config_ok(struct bnxt *bp)
13014 {
13015 	struct ethtool_keee *eee = &bp->eee;
13016 	struct bnxt_link_info *link_info = &bp->link_info;
13017 
13018 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
13019 		return true;
13020 
13021 	if (eee->eee_enabled) {
13022 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
13023 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
13024 
13025 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
13026 
13027 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13028 			eee->eee_enabled = 0;
13029 			return false;
13030 		}
13031 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
13032 			linkmode_and(eee->advertised, advertising,
13033 				     eee->supported);
13034 			return false;
13035 		}
13036 	}
13037 	return true;
13038 }
13039 
13040 static int bnxt_update_phy_setting(struct bnxt *bp)
13041 {
13042 	int rc;
13043 	bool update_link = false;
13044 	bool update_pause = false;
13045 	bool update_eee = false;
13046 	struct bnxt_link_info *link_info = &bp->link_info;
13047 
13048 	rc = bnxt_update_link(bp, true);
13049 	if (rc) {
13050 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
13051 			   rc);
13052 		return rc;
13053 	}
13054 	if (!BNXT_SINGLE_PF(bp))
13055 		return 0;
13056 
13057 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13058 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
13059 	    link_info->req_flow_ctrl)
13060 		update_pause = true;
13061 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13062 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
13063 		update_pause = true;
13064 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13065 		if (BNXT_AUTO_MODE(link_info->auto_mode))
13066 			update_link = true;
13067 		if (bnxt_force_speed_updated(link_info))
13068 			update_link = true;
13069 		if (link_info->req_duplex != link_info->duplex_setting)
13070 			update_link = true;
13071 	} else {
13072 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
13073 			update_link = true;
13074 		if (bnxt_auto_speed_updated(link_info))
13075 			update_link = true;
13076 	}
13077 
13078 	/* The last close may have shutdown the link, so need to call
13079 	 * PHY_CFG to bring it back up.
13080 	 */
13081 	if (!BNXT_LINK_IS_UP(bp))
13082 		update_link = true;
13083 
13084 	if (!bnxt_eee_config_ok(bp))
13085 		update_eee = true;
13086 
13087 	if (update_link)
13088 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
13089 	else if (update_pause)
13090 		rc = bnxt_hwrm_set_pause(bp);
13091 	if (rc) {
13092 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
13093 			   rc);
13094 		return rc;
13095 	}
13096 
13097 	return rc;
13098 }
13099 
13100 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
13101 
13102 static int bnxt_reinit_after_abort(struct bnxt *bp)
13103 {
13104 	int rc;
13105 
13106 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13107 		return -EBUSY;
13108 
13109 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
13110 		return -ENODEV;
13111 
13112 	rc = bnxt_fw_init_one(bp);
13113 	if (!rc) {
13114 		bnxt_clear_int_mode(bp);
13115 		rc = bnxt_init_int_mode(bp);
13116 		if (!rc) {
13117 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13118 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
13119 		}
13120 	}
13121 	return rc;
13122 }
13123 
13124 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
13125 {
13126 	struct bnxt_ntuple_filter *ntp_fltr;
13127 	struct bnxt_l2_filter *l2_fltr;
13128 
13129 	if (list_empty(&fltr->list))
13130 		return;
13131 
13132 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
13133 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
13134 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
13135 		atomic_inc(&l2_fltr->refcnt);
13136 		ntp_fltr->l2_fltr = l2_fltr;
13137 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
13138 			bnxt_del_ntp_filter(bp, ntp_fltr);
13139 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
13140 				   fltr->sw_id);
13141 		}
13142 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
13143 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
13144 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
13145 			bnxt_del_l2_filter(bp, l2_fltr);
13146 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
13147 				   fltr->sw_id);
13148 		}
13149 	}
13150 }
13151 
13152 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
13153 {
13154 	struct bnxt_filter_base *usr_fltr, *tmp;
13155 
13156 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
13157 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
13158 }
13159 
13160 static int bnxt_set_xps_mapping(struct bnxt *bp)
13161 {
13162 	int numa_node = dev_to_node(&bp->pdev->dev);
13163 	unsigned int q_idx, map_idx, cpu, i;
13164 	const struct cpumask *cpu_mask_ptr;
13165 	int nr_cpus = num_online_cpus();
13166 	cpumask_t *q_map;
13167 	int rc = 0;
13168 
13169 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
13170 	if (!q_map)
13171 		return -ENOMEM;
13172 
13173 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
13174 	 * Each TC has the same number of TX queues. The nth TX queue for each
13175 	 * TC will have the same CPU mask.
13176 	 */
13177 	for (i = 0; i < nr_cpus; i++) {
13178 		map_idx = i % bp->tx_nr_rings_per_tc;
13179 		cpu = cpumask_local_spread(i, numa_node);
13180 		cpu_mask_ptr = get_cpu_mask(cpu);
13181 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
13182 	}
13183 
13184 	/* Register CPU mask for each TX queue except the ones marked for XDP */
13185 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
13186 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
13187 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
13188 		if (rc) {
13189 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
13190 				    q_idx);
13191 			break;
13192 		}
13193 	}
13194 
13195 	kfree(q_map);
13196 
13197 	return rc;
13198 }
13199 
13200 static int bnxt_tx_nr_rings(struct bnxt *bp)
13201 {
13202 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
13203 			    bp->tx_nr_rings_per_tc;
13204 }
13205 
13206 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
13207 {
13208 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
13209 }
13210 
13211 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
13212 {
13213 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13214 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13215 }
13216 
13217 static void bnxt_adj_tx_rings(struct bnxt *bp)
13218 {
13219 	/* Make adjustments if reserved TX rings are less than requested */
13220 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13221 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13222 	if (bp->tx_nr_rings_xdp)
13223 		bnxt_set_xdp_tx_rings(bp);
13224 }
13225 
13226 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13227 {
13228 	int rc = 0;
13229 
13230 	netif_carrier_off(bp->dev);
13231 	if (irq_re_init) {
13232 		/* Reserve rings now if none were reserved at driver probe. */
13233 		rc = bnxt_init_dflt_ring_mode(bp);
13234 		if (rc) {
13235 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13236 			return rc;
13237 		}
13238 	}
13239 	rc = bnxt_reserve_rings(bp, irq_re_init);
13240 	if (rc)
13241 		return rc;
13242 
13243 	bnxt_adj_tx_rings(bp);
13244 	rc = bnxt_alloc_mem(bp, irq_re_init);
13245 	if (rc) {
13246 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13247 		goto open_err_free_mem;
13248 	}
13249 
13250 	if (irq_re_init) {
13251 		bnxt_init_napi(bp);
13252 		rc = bnxt_request_irq(bp);
13253 		if (rc) {
13254 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13255 			goto open_err_irq;
13256 		}
13257 	}
13258 
13259 	rc = bnxt_init_nic(bp, irq_re_init);
13260 	if (rc) {
13261 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13262 		goto open_err_irq;
13263 	}
13264 
13265 	bnxt_enable_napi(bp);
13266 	bnxt_debug_dev_init(bp);
13267 
13268 	if (link_re_init) {
13269 		mutex_lock(&bp->link_lock);
13270 		rc = bnxt_update_phy_setting(bp);
13271 		mutex_unlock(&bp->link_lock);
13272 		if (rc) {
13273 			netdev_warn(bp->dev, "failed to update phy settings\n");
13274 			if (BNXT_SINGLE_PF(bp)) {
13275 				bp->link_info.phy_retry = true;
13276 				bp->link_info.phy_retry_expires =
13277 					jiffies + 5 * HZ;
13278 			}
13279 		}
13280 	}
13281 
13282 	if (irq_re_init) {
13283 		udp_tunnel_nic_reset_ntf(bp->dev);
13284 		rc = bnxt_set_xps_mapping(bp);
13285 		if (rc)
13286 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13287 	}
13288 
13289 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13290 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13291 			static_branch_enable(&bnxt_xdp_locking_key);
13292 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13293 		static_branch_disable(&bnxt_xdp_locking_key);
13294 	}
13295 	set_bit(BNXT_STATE_OPEN, &bp->state);
13296 	bnxt_enable_int(bp);
13297 	/* Enable TX queues */
13298 	bnxt_tx_enable(bp);
13299 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13300 	/* Poll link status and check for SFP+ module status */
13301 	mutex_lock(&bp->link_lock);
13302 	bnxt_get_port_module_status(bp);
13303 	mutex_unlock(&bp->link_lock);
13304 
13305 	/* VF-reps may need to be re-opened after the PF is re-opened */
13306 	if (BNXT_PF(bp))
13307 		bnxt_vf_reps_open(bp);
13308 	bnxt_ptp_init_rtc(bp, true);
13309 	bnxt_ptp_cfg_tstamp_filters(bp);
13310 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13311 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13312 	bnxt_cfg_usr_fltrs(bp);
13313 	return 0;
13314 
13315 open_err_irq:
13316 	bnxt_del_napi(bp);
13317 
13318 open_err_free_mem:
13319 	bnxt_free_skbs(bp);
13320 	bnxt_free_irq(bp);
13321 	bnxt_free_mem(bp, true);
13322 	return rc;
13323 }
13324 
13325 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13326 {
13327 	int rc = 0;
13328 
13329 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13330 		rc = -EIO;
13331 	if (!rc)
13332 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13333 	if (rc) {
13334 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13335 		netif_close(bp->dev);
13336 	}
13337 	return rc;
13338 }
13339 
13340 /* netdev instance lock held, open the NIC half way by allocating all
13341  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13342  * for offline self tests.
13343  */
13344 int bnxt_half_open_nic(struct bnxt *bp)
13345 {
13346 	int rc = 0;
13347 
13348 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13349 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13350 		rc = -ENODEV;
13351 		goto half_open_err;
13352 	}
13353 
13354 	rc = bnxt_alloc_mem(bp, true);
13355 	if (rc) {
13356 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13357 		goto half_open_err;
13358 	}
13359 	bnxt_init_napi(bp);
13360 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13361 	rc = bnxt_init_nic(bp, true);
13362 	if (rc) {
13363 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13364 		bnxt_del_napi(bp);
13365 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13366 		goto half_open_err;
13367 	}
13368 	return 0;
13369 
13370 half_open_err:
13371 	bnxt_free_skbs(bp);
13372 	bnxt_free_mem(bp, true);
13373 	netif_close(bp->dev);
13374 	return rc;
13375 }
13376 
13377 /* netdev instance lock held, this call can only be made after a previous
13378  * successful call to bnxt_half_open_nic().
13379  */
13380 void bnxt_half_close_nic(struct bnxt *bp)
13381 {
13382 	bnxt_hwrm_resource_free(bp, false, true);
13383 	bnxt_del_napi(bp);
13384 	bnxt_free_skbs(bp);
13385 	bnxt_free_mem(bp, true);
13386 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13387 }
13388 
13389 void bnxt_reenable_sriov(struct bnxt *bp)
13390 {
13391 	if (BNXT_PF(bp)) {
13392 		struct bnxt_pf_info *pf = &bp->pf;
13393 		int n = pf->active_vfs;
13394 
13395 		if (n)
13396 			bnxt_cfg_hw_sriov(bp, &n, true);
13397 	}
13398 }
13399 
13400 static int bnxt_open(struct net_device *dev)
13401 {
13402 	struct bnxt *bp = netdev_priv(dev);
13403 	int rc;
13404 
13405 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13406 		rc = bnxt_reinit_after_abort(bp);
13407 		if (rc) {
13408 			if (rc == -EBUSY)
13409 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13410 			else
13411 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13412 			return -ENODEV;
13413 		}
13414 	}
13415 
13416 	rc = bnxt_hwrm_if_change(bp, true);
13417 	if (rc)
13418 		return rc;
13419 
13420 	rc = __bnxt_open_nic(bp, true, true);
13421 	if (rc) {
13422 		bnxt_hwrm_if_change(bp, false);
13423 	} else {
13424 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13425 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13426 				bnxt_queue_sp_work(bp,
13427 						   BNXT_RESTART_ULP_SP_EVENT);
13428 		}
13429 	}
13430 
13431 	return rc;
13432 }
13433 
13434 static bool bnxt_drv_busy(struct bnxt *bp)
13435 {
13436 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13437 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13438 }
13439 
13440 static void bnxt_get_ring_stats(struct bnxt *bp,
13441 				struct rtnl_link_stats64 *stats);
13442 
13443 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13444 			     bool link_re_init)
13445 {
13446 	/* Close the VF-reps before closing PF */
13447 	if (BNXT_PF(bp))
13448 		bnxt_vf_reps_close(bp);
13449 
13450 	/* Change device state to avoid TX queue wake up's */
13451 	bnxt_tx_disable(bp);
13452 
13453 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13454 	smp_mb__after_atomic();
13455 	while (bnxt_drv_busy(bp))
13456 		msleep(20);
13457 
13458 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13459 		bnxt_clear_rss_ctxs(bp);
13460 	/* Flush rings and disable interrupts */
13461 	bnxt_shutdown_nic(bp, irq_re_init);
13462 
13463 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13464 
13465 	bnxt_debug_dev_exit(bp);
13466 	bnxt_disable_napi(bp);
13467 	timer_delete_sync(&bp->timer);
13468 	bnxt_free_skbs(bp);
13469 
13470 	/* Save ring stats before shutdown */
13471 	if (bp->bnapi && irq_re_init) {
13472 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13473 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13474 	}
13475 	if (irq_re_init) {
13476 		bnxt_free_irq(bp);
13477 		bnxt_del_napi(bp);
13478 	}
13479 	bnxt_free_mem(bp, irq_re_init);
13480 }
13481 
13482 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13483 {
13484 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13485 		/* If we get here, it means firmware reset is in progress
13486 		 * while we are trying to close.  We can safely proceed with
13487 		 * the close because we are holding netdev instance lock.
13488 		 * Some firmware messages may fail as we proceed to close.
13489 		 * We set the ABORT_ERR flag here so that the FW reset thread
13490 		 * will later abort when it gets the netdev instance lock
13491 		 * and sees the flag.
13492 		 */
13493 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13494 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13495 	}
13496 
13497 #ifdef CONFIG_BNXT_SRIOV
13498 	if (bp->sriov_cfg) {
13499 		int rc;
13500 
13501 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13502 						      !bp->sriov_cfg,
13503 						      BNXT_SRIOV_CFG_WAIT_TMO);
13504 		if (!rc)
13505 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13506 		else if (rc < 0)
13507 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13508 	}
13509 #endif
13510 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13511 }
13512 
13513 static int bnxt_close(struct net_device *dev)
13514 {
13515 	struct bnxt *bp = netdev_priv(dev);
13516 
13517 	bnxt_close_nic(bp, true, true);
13518 	bnxt_hwrm_shutdown_link(bp);
13519 	bnxt_hwrm_if_change(bp, false);
13520 	return 0;
13521 }
13522 
13523 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13524 				   u16 *val)
13525 {
13526 	struct hwrm_port_phy_mdio_read_output *resp;
13527 	struct hwrm_port_phy_mdio_read_input *req;
13528 	int rc;
13529 
13530 	if (bp->hwrm_spec_code < 0x10a00)
13531 		return -EOPNOTSUPP;
13532 
13533 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13534 	if (rc)
13535 		return rc;
13536 
13537 	req->port_id = cpu_to_le16(bp->pf.port_id);
13538 	req->phy_addr = phy_addr;
13539 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13540 	if (mdio_phy_id_is_c45(phy_addr)) {
13541 		req->cl45_mdio = 1;
13542 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13543 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13544 		req->reg_addr = cpu_to_le16(reg);
13545 	}
13546 
13547 	resp = hwrm_req_hold(bp, req);
13548 	rc = hwrm_req_send(bp, req);
13549 	if (!rc)
13550 		*val = le16_to_cpu(resp->reg_data);
13551 	hwrm_req_drop(bp, req);
13552 	return rc;
13553 }
13554 
13555 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13556 				    u16 val)
13557 {
13558 	struct hwrm_port_phy_mdio_write_input *req;
13559 	int rc;
13560 
13561 	if (bp->hwrm_spec_code < 0x10a00)
13562 		return -EOPNOTSUPP;
13563 
13564 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13565 	if (rc)
13566 		return rc;
13567 
13568 	req->port_id = cpu_to_le16(bp->pf.port_id);
13569 	req->phy_addr = phy_addr;
13570 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13571 	if (mdio_phy_id_is_c45(phy_addr)) {
13572 		req->cl45_mdio = 1;
13573 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13574 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13575 		req->reg_addr = cpu_to_le16(reg);
13576 	}
13577 	req->reg_data = cpu_to_le16(val);
13578 
13579 	return hwrm_req_send(bp, req);
13580 }
13581 
13582 /* netdev instance lock held */
13583 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13584 {
13585 	struct mii_ioctl_data *mdio = if_mii(ifr);
13586 	struct bnxt *bp = netdev_priv(dev);
13587 	int rc;
13588 
13589 	switch (cmd) {
13590 	case SIOCGMIIPHY:
13591 		mdio->phy_id = bp->link_info.phy_addr;
13592 
13593 		fallthrough;
13594 	case SIOCGMIIREG: {
13595 		u16 mii_regval = 0;
13596 
13597 		if (!netif_running(dev))
13598 			return -EAGAIN;
13599 
13600 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13601 					     &mii_regval);
13602 		mdio->val_out = mii_regval;
13603 		return rc;
13604 	}
13605 
13606 	case SIOCSMIIREG:
13607 		if (!netif_running(dev))
13608 			return -EAGAIN;
13609 
13610 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13611 						mdio->val_in);
13612 
13613 	default:
13614 		/* do nothing */
13615 		break;
13616 	}
13617 	return -EOPNOTSUPP;
13618 }
13619 
13620 static void bnxt_get_ring_stats(struct bnxt *bp,
13621 				struct rtnl_link_stats64 *stats)
13622 {
13623 	int i;
13624 
13625 	for (i = 0; i < bp->cp_nr_rings; i++) {
13626 		struct bnxt_napi *bnapi = bp->bnapi[i];
13627 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13628 		u64 *sw = cpr->stats.sw_stats;
13629 
13630 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13631 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13632 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13633 
13634 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13635 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13636 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13637 
13638 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13639 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13640 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13641 
13642 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13643 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13644 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13645 
13646 		stats->rx_missed_errors +=
13647 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13648 
13649 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13650 
13651 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13652 
13653 		stats->rx_dropped +=
13654 			cpr->sw_stats->rx.rx_netpoll_discards +
13655 			cpr->sw_stats->rx.rx_oom_discards;
13656 	}
13657 }
13658 
13659 static void bnxt_add_prev_stats(struct bnxt *bp,
13660 				struct rtnl_link_stats64 *stats)
13661 {
13662 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13663 
13664 	stats->rx_packets += prev_stats->rx_packets;
13665 	stats->tx_packets += prev_stats->tx_packets;
13666 	stats->rx_bytes += prev_stats->rx_bytes;
13667 	stats->tx_bytes += prev_stats->tx_bytes;
13668 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13669 	stats->multicast += prev_stats->multicast;
13670 	stats->rx_dropped += prev_stats->rx_dropped;
13671 	stats->tx_dropped += prev_stats->tx_dropped;
13672 }
13673 
13674 static void
13675 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13676 {
13677 	struct bnxt *bp = netdev_priv(dev);
13678 
13679 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13680 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13681 	 * we check the BNXT_STATE_OPEN flag.
13682 	 */
13683 	smp_mb__after_atomic();
13684 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13685 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13686 		*stats = bp->net_stats_prev;
13687 		return;
13688 	}
13689 
13690 	bnxt_sync_ring_stats(bp);
13691 	bnxt_get_ring_stats(bp, stats);
13692 	bnxt_add_prev_stats(bp, stats);
13693 
13694 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13695 		u64 *rx = bp->port_stats.sw_stats;
13696 		u64 *tx = bp->port_stats.sw_stats +
13697 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13698 
13699 		stats->rx_crc_errors =
13700 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13701 		stats->rx_frame_errors =
13702 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13703 		stats->rx_length_errors =
13704 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13705 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13706 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13707 		stats->rx_errors =
13708 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13709 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13710 		stats->collisions =
13711 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13712 		stats->tx_fifo_errors =
13713 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13714 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13715 	}
13716 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13717 }
13718 
13719 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13720 					struct bnxt_total_ring_drv_stats *stats,
13721 					struct bnxt_cp_ring_info *cpr)
13722 {
13723 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13724 	u64 *hw_stats = cpr->stats.sw_stats;
13725 
13726 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13727 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13728 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13729 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13730 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13731 	stats->rx_total_ring_discards +=
13732 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13733 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13734 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13735 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13736 	stats->tx_total_ring_discards +=
13737 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13738 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13739 }
13740 
13741 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13742 			     struct bnxt_total_ring_drv_stats *stats)
13743 {
13744 	int i;
13745 
13746 	for (i = 0; i < bp->cp_nr_rings; i++)
13747 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13748 }
13749 
13750 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask,
13751 				 const struct netdev_hw_addr_list *mc)
13752 {
13753 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13754 	struct netdev_hw_addr *ha;
13755 	u8 *haddr;
13756 	int mc_count = 0;
13757 	bool update = false;
13758 	int off = 0;
13759 
13760 	netdev_hw_addr_list_for_each(ha, mc) {
13761 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13762 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13763 			vnic->mc_list_count = 0;
13764 			return false;
13765 		}
13766 		haddr = ha->addr;
13767 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13768 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13769 			update = true;
13770 		}
13771 		off += ETH_ALEN;
13772 		mc_count++;
13773 	}
13774 	if (mc_count)
13775 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13776 
13777 	if (mc_count != vnic->mc_list_count) {
13778 		vnic->mc_list_count = mc_count;
13779 		update = true;
13780 	}
13781 	return update;
13782 }
13783 
13784 static bool bnxt_uc_list_updated(struct bnxt *bp,
13785 				 const struct netdev_hw_addr_list *uc)
13786 {
13787 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13788 	struct netdev_hw_addr *ha;
13789 	int off = 0;
13790 
13791 	if (netdev_hw_addr_list_count(uc) != (vnic->uc_filter_count - 1))
13792 		return true;
13793 
13794 	netdev_hw_addr_list_for_each(ha, uc) {
13795 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13796 			return true;
13797 
13798 		off += ETH_ALEN;
13799 	}
13800 	return false;
13801 }
13802 
13803 static int bnxt_set_rx_mode(struct net_device *dev,
13804 			    struct netdev_hw_addr_list *uc,
13805 			    struct netdev_hw_addr_list *mc)
13806 {
13807 	struct bnxt *bp = netdev_priv(dev);
13808 	struct bnxt_vnic_info *vnic;
13809 	bool mc_update = false;
13810 	bool uc_update;
13811 	u32 mask;
13812 
13813 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13814 		return 0;
13815 
13816 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13817 	mask = vnic->rx_mask;
13818 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13819 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13820 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13821 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13822 
13823 	if (dev->flags & IFF_PROMISC)
13824 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13825 
13826 	uc_update = bnxt_uc_list_updated(bp, uc);
13827 
13828 	if (dev->flags & IFF_BROADCAST)
13829 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13830 	if (dev->flags & IFF_ALLMULTI) {
13831 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13832 		vnic->mc_list_count = 0;
13833 	} else if (dev->flags & IFF_MULTICAST) {
13834 		mc_update = bnxt_mc_list_updated(bp, &mask, mc);
13835 	}
13836 
13837 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13838 		vnic->rx_mask = mask;
13839 
13840 		return bnxt_cfg_rx_mode(bp, uc, uc_update);
13841 	}
13842 
13843 	return 0;
13844 }
13845 
13846 static int bnxt_cfg_rx_mode(struct bnxt *bp, struct netdev_hw_addr_list *uc,
13847 			    bool uc_update)
13848 {
13849 	struct net_device *dev = bp->dev;
13850 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13851 	struct netdev_hw_addr *ha;
13852 	int i, off = 0, rc;
13853 
13854 	if (!uc_update)
13855 		goto skip_uc;
13856 
13857 	for (i = 1; i < vnic->uc_filter_count; i++) {
13858 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13859 
13860 		bnxt_hwrm_l2_filter_free(bp, fltr);
13861 		bnxt_del_l2_filter(bp, fltr);
13862 	}
13863 
13864 	vnic->uc_filter_count = 1;
13865 
13866 	netif_addr_lock_bh(dev);
13867 	if (netdev_hw_addr_list_count(uc) > (BNXT_MAX_UC_ADDRS - 1)) {
13868 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13869 	} else {
13870 		netdev_hw_addr_list_for_each(ha, uc) {
13871 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13872 			off += ETH_ALEN;
13873 			vnic->uc_filter_count++;
13874 		}
13875 	}
13876 	netif_addr_unlock_bh(dev);
13877 
13878 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13879 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13880 		if (rc) {
13881 			if (BNXT_VF(bp) && rc == -ENODEV) {
13882 				netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13883 				rc = -EAGAIN;
13884 			} else if (rc == -EAGAIN) {
13885 				netdev_warn(bp->dev, "FW busy while setting vnic filter, will retry\n");
13886 			} else {
13887 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13888 			}
13889 			vnic->uc_filter_count = i;
13890 			return rc;
13891 		}
13892 	}
13893 
13894 skip_uc:
13895 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13896 	    !bnxt_promisc_ok(bp))
13897 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13898 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13899 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13900 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13901 			    rc);
13902 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13903 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13904 		vnic->mc_list_count = 0;
13905 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13906 	}
13907 	if (rc)
13908 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13909 			   rc);
13910 
13911 	return rc;
13912 }
13913 
13914 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13915 {
13916 #ifdef CONFIG_BNXT_SRIOV
13917 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13918 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13919 
13920 		/* No minimum rings were provisioned by the PF.  Don't
13921 		 * reserve rings by default when device is down.
13922 		 */
13923 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13924 			return true;
13925 
13926 		if (!netif_running(bp->dev))
13927 			return false;
13928 	}
13929 #endif
13930 	return true;
13931 }
13932 
13933 /* If the chip and firmware supports RFS */
13934 static bool bnxt_rfs_supported(struct bnxt *bp)
13935 {
13936 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13937 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13938 			return true;
13939 		return false;
13940 	}
13941 	/* 212 firmware is broken for aRFS */
13942 	if (BNXT_FW_MAJ(bp) == 212)
13943 		return false;
13944 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13945 		return true;
13946 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13947 		return true;
13948 	return false;
13949 }
13950 
13951 /* If runtime conditions support RFS */
13952 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13953 {
13954 	struct bnxt_hw_rings hwr = {0};
13955 	int max_vnics, max_rss_ctxs;
13956 
13957 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13958 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13959 		return bnxt_rfs_supported(bp);
13960 
13961 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13962 		return false;
13963 
13964 	hwr.grp = bp->rx_nr_rings;
13965 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13966 	if (new_rss_ctx)
13967 		hwr.vnic++;
13968 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13969 	max_vnics = bnxt_get_max_func_vnics(bp);
13970 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13971 
13972 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13973 		if (bp->rx_nr_rings > 1)
13974 			netdev_warn(bp->dev,
13975 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13976 				    min(max_rss_ctxs - 1, max_vnics - 1));
13977 		return false;
13978 	}
13979 
13980 	if (!BNXT_NEW_RM(bp))
13981 		return true;
13982 
13983 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13984 	 * issue that will mess up the default VNIC if we reduce the
13985 	 * reservations.
13986 	 */
13987 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13988 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13989 		return true;
13990 
13991 	bnxt_hwrm_reserve_rings(bp, &hwr);
13992 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13993 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13994 		return true;
13995 
13996 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13997 	hwr.vnic = 1;
13998 	hwr.rss_ctx = 0;
13999 	bnxt_hwrm_reserve_rings(bp, &hwr);
14000 	return false;
14001 }
14002 
14003 static netdev_features_t bnxt_fix_features(struct net_device *dev,
14004 					   netdev_features_t features)
14005 {
14006 	struct bnxt *bp = netdev_priv(dev);
14007 	netdev_features_t vlan_features;
14008 
14009 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
14010 		features &= ~NETIF_F_NTUPLE;
14011 
14012 	if ((features & NETIF_F_GSO_UDP_L4) &&
14013 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
14014 	    bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS)
14015 		features &= ~NETIF_F_GSO_UDP_L4;
14016 
14017 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
14018 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14019 
14020 	if (!(features & NETIF_F_GRO))
14021 		features &= ~NETIF_F_GRO_HW;
14022 
14023 	if (features & NETIF_F_GRO_HW)
14024 		features &= ~NETIF_F_LRO;
14025 
14026 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
14027 	 * turned on or off together.
14028 	 */
14029 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
14030 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
14031 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14032 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14033 		else if (vlan_features)
14034 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
14035 	}
14036 #ifdef CONFIG_BNXT_SRIOV
14037 	if (BNXT_VF(bp) && bp->vf.vlan)
14038 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14039 #endif
14040 	return features;
14041 }
14042 
14043 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
14044 				bool link_re_init, u32 flags, bool update_tpa)
14045 {
14046 	bnxt_close_nic(bp, irq_re_init, link_re_init);
14047 	bp->flags = flags;
14048 	if (update_tpa)
14049 		bnxt_set_ring_params(bp);
14050 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
14051 }
14052 
14053 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
14054 {
14055 	bool update_tpa = false, update_ntuple = false;
14056 	struct bnxt *bp = netdev_priv(dev);
14057 	u32 flags = bp->flags;
14058 	u32 changes;
14059 	int rc = 0;
14060 	bool re_init = false;
14061 
14062 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
14063 				   bnxt_min_tx_desc_cnt(bp, features));
14064 
14065 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
14066 	if (features & NETIF_F_GRO_HW)
14067 		flags |= BNXT_FLAG_GRO;
14068 	else if (features & NETIF_F_LRO)
14069 		flags |= BNXT_FLAG_LRO;
14070 
14071 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
14072 		flags &= ~BNXT_FLAG_TPA;
14073 
14074 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14075 		flags |= BNXT_FLAG_STRIP_VLAN;
14076 
14077 	if (features & NETIF_F_NTUPLE)
14078 		flags |= BNXT_FLAG_RFS;
14079 	else
14080 		bnxt_clear_usr_fltrs(bp, true);
14081 
14082 	changes = flags ^ bp->flags;
14083 	if (changes & BNXT_FLAG_TPA) {
14084 		update_tpa = true;
14085 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
14086 		    (flags & BNXT_FLAG_TPA) == 0 ||
14087 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14088 			re_init = true;
14089 	}
14090 
14091 	if (changes & ~BNXT_FLAG_TPA)
14092 		re_init = true;
14093 
14094 	if (changes & BNXT_FLAG_RFS)
14095 		update_ntuple = true;
14096 
14097 	if (flags != bp->flags) {
14098 		u32 old_flags = bp->flags;
14099 
14100 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14101 			bp->flags = flags;
14102 			if (update_tpa)
14103 				bnxt_set_ring_params(bp);
14104 			return rc;
14105 		}
14106 
14107 		if (update_ntuple)
14108 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
14109 
14110 		if (re_init)
14111 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
14112 
14113 		if (update_tpa) {
14114 			bp->flags = flags;
14115 			rc = bnxt_set_tpa(bp,
14116 					  (flags & BNXT_FLAG_TPA) ?
14117 					  true : false);
14118 			if (rc)
14119 				bp->flags = old_flags;
14120 		}
14121 	}
14122 	return rc;
14123 }
14124 
14125 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
14126 			      u8 **nextp)
14127 {
14128 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
14129 	int hdr_count = 0;
14130 	u8 *nexthdr;
14131 	int start;
14132 
14133 	/* Check that there are at most 2 IPv6 extension headers, no
14134 	 * fragment header, and each is <= 64 bytes.
14135 	 */
14136 	start = nw_off + sizeof(*ip6h);
14137 	nexthdr = &ip6h->nexthdr;
14138 	while (ipv6_ext_hdr(*nexthdr)) {
14139 		struct ipv6_opt_hdr *hp;
14140 		int hdrlen;
14141 
14142 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
14143 		    *nexthdr == NEXTHDR_FRAGMENT)
14144 			return false;
14145 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
14146 					  skb_headlen(skb), NULL);
14147 		if (!hp)
14148 			return false;
14149 		if (*nexthdr == NEXTHDR_AUTH)
14150 			hdrlen = ipv6_authlen(hp);
14151 		else
14152 			hdrlen = ipv6_optlen(hp);
14153 
14154 		if (hdrlen > 64)
14155 			return false;
14156 
14157 		hdr_count++;
14158 		nexthdr = &hp->nexthdr;
14159 		start += hdrlen;
14160 	}
14161 	if (nextp) {
14162 		/* Caller will check inner protocol */
14163 		if (skb->encapsulation) {
14164 			*nextp = nexthdr;
14165 			return true;
14166 		}
14167 		*nextp = NULL;
14168 	}
14169 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
14170 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
14171 }
14172 
14173 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
14174 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
14175 {
14176 	struct udphdr *uh = udp_hdr(skb);
14177 	__be16 udp_port = uh->dest;
14178 
14179 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
14180 	    udp_port != bp->vxlan_gpe_port)
14181 		return false;
14182 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
14183 		struct ethhdr *eh = inner_eth_hdr(skb);
14184 
14185 		switch (eh->h_proto) {
14186 		case htons(ETH_P_IP):
14187 			return true;
14188 		case htons(ETH_P_IPV6):
14189 			return bnxt_exthdr_check(bp, skb,
14190 						 skb_inner_network_offset(skb),
14191 						 NULL);
14192 		}
14193 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
14194 		return true;
14195 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
14196 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14197 					 NULL);
14198 	}
14199 	return false;
14200 }
14201 
14202 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
14203 {
14204 	switch (l4_proto) {
14205 	case IPPROTO_UDP:
14206 		return bnxt_udp_tunl_check(bp, skb);
14207 	case IPPROTO_IPIP:
14208 		return true;
14209 	case IPPROTO_GRE: {
14210 		switch (skb->inner_protocol) {
14211 		default:
14212 			return false;
14213 		case htons(ETH_P_IP):
14214 			return true;
14215 		case htons(ETH_P_IPV6):
14216 			fallthrough;
14217 		}
14218 	}
14219 	case IPPROTO_IPV6:
14220 		/* Check ext headers of inner ipv6 */
14221 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14222 					 NULL);
14223 	}
14224 	return false;
14225 }
14226 
14227 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
14228 					     struct net_device *dev,
14229 					     netdev_features_t features)
14230 {
14231 	struct bnxt *bp = netdev_priv(dev);
14232 	u8 *l4_proto;
14233 
14234 	features = vlan_features_check(skb, features);
14235 	switch (vlan_get_protocol(skb)) {
14236 	case htons(ETH_P_IP):
14237 		if (!skb->encapsulation)
14238 			return features;
14239 		l4_proto = &ip_hdr(skb)->protocol;
14240 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14241 			return features;
14242 		break;
14243 	case htons(ETH_P_IPV6):
14244 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14245 				       &l4_proto))
14246 			break;
14247 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14248 			return features;
14249 		break;
14250 	}
14251 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14252 }
14253 
14254 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14255 			 u32 *reg_buf)
14256 {
14257 	struct hwrm_dbg_read_direct_output *resp;
14258 	struct hwrm_dbg_read_direct_input *req;
14259 	__le32 *dbg_reg_buf;
14260 	dma_addr_t mapping;
14261 	int rc, i;
14262 
14263 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14264 	if (rc)
14265 		return rc;
14266 
14267 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14268 					 &mapping);
14269 	if (!dbg_reg_buf) {
14270 		rc = -ENOMEM;
14271 		goto dbg_rd_reg_exit;
14272 	}
14273 
14274 	req->host_dest_addr = cpu_to_le64(mapping);
14275 
14276 	resp = hwrm_req_hold(bp, req);
14277 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14278 	req->read_len32 = cpu_to_le32(num_words);
14279 
14280 	rc = hwrm_req_send(bp, req);
14281 	if (rc || resp->error_code) {
14282 		rc = -EIO;
14283 		goto dbg_rd_reg_exit;
14284 	}
14285 	for (i = 0; i < num_words; i++)
14286 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14287 
14288 dbg_rd_reg_exit:
14289 	hwrm_req_drop(bp, req);
14290 	return rc;
14291 }
14292 
14293 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14294 				       u32 ring_id, u32 *prod, u32 *cons)
14295 {
14296 	struct hwrm_dbg_ring_info_get_output *resp;
14297 	struct hwrm_dbg_ring_info_get_input *req;
14298 	int rc;
14299 
14300 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14301 	if (rc)
14302 		return rc;
14303 
14304 	req->ring_type = ring_type;
14305 	req->fw_ring_id = cpu_to_le32(ring_id);
14306 	resp = hwrm_req_hold(bp, req);
14307 	rc = hwrm_req_send(bp, req);
14308 	if (!rc) {
14309 		*prod = le32_to_cpu(resp->producer_index);
14310 		*cons = le32_to_cpu(resp->consumer_index);
14311 	}
14312 	hwrm_req_drop(bp, req);
14313 	return rc;
14314 }
14315 
14316 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14317 {
14318 	struct bnxt_tx_ring_info *txr;
14319 	int i = bnapi->index, j;
14320 
14321 	bnxt_for_each_napi_tx(j, bnapi, txr)
14322 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14323 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14324 			    txr->tx_cons);
14325 }
14326 
14327 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14328 {
14329 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14330 	int i = bnapi->index;
14331 
14332 	if (!rxr)
14333 		return;
14334 
14335 	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",
14336 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14337 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14338 		    rxr->rx_sw_agg_prod);
14339 }
14340 
14341 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14342 {
14343 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14344 	int i = bnapi->index, j;
14345 
14346 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14347 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14348 	for (j = 0; j < cpr->cp_ring_count; j++) {
14349 		cpr2 = &cpr->cp_ring_arr[j];
14350 		if (!cpr2->bnapi)
14351 			continue;
14352 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14353 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14354 			    cpr2->cp_raw_cons);
14355 	}
14356 }
14357 
14358 static void bnxt_dbg_dump_states(struct bnxt *bp)
14359 {
14360 	int i;
14361 	struct bnxt_napi *bnapi;
14362 
14363 	for (i = 0; i < bp->cp_nr_rings; i++) {
14364 		bnapi = bp->bnapi[i];
14365 		if (netif_msg_drv(bp)) {
14366 			bnxt_dump_tx_sw_state(bnapi);
14367 			bnxt_dump_rx_sw_state(bnapi);
14368 			bnxt_dump_cp_sw_state(bnapi);
14369 		}
14370 	}
14371 }
14372 
14373 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14374 {
14375 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14376 	struct hwrm_ring_reset_input *req;
14377 	struct bnxt_napi *bnapi = rxr->bnapi;
14378 	struct bnxt_cp_ring_info *cpr;
14379 	u16 cp_ring_id;
14380 	int rc;
14381 
14382 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14383 	if (rc)
14384 		return rc;
14385 
14386 	cpr = &bnapi->cp_ring;
14387 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14388 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14389 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14390 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14391 	return hwrm_req_send_silent(bp, req);
14392 }
14393 
14394 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14395 {
14396 	if (!silent)
14397 		bnxt_dbg_dump_states(bp);
14398 	if (netif_running(bp->dev)) {
14399 		bnxt_close_nic(bp, !silent, false);
14400 		bnxt_open_nic(bp, !silent, false);
14401 	}
14402 }
14403 
14404 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14405 {
14406 	struct bnxt *bp = netdev_priv(dev);
14407 
14408 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14409 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14410 }
14411 
14412 static void bnxt_fw_health_check(struct bnxt *bp)
14413 {
14414 	struct bnxt_fw_health *fw_health = bp->fw_health;
14415 	struct pci_dev *pdev = bp->pdev;
14416 	u32 val;
14417 
14418 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14419 		return;
14420 
14421 	/* Make sure it is enabled before checking the tmr_counter. */
14422 	smp_rmb();
14423 	if (fw_health->tmr_counter) {
14424 		fw_health->tmr_counter--;
14425 		return;
14426 	}
14427 
14428 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14429 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14430 		fw_health->arrests++;
14431 		goto fw_reset;
14432 	}
14433 
14434 	fw_health->last_fw_heartbeat = val;
14435 
14436 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14437 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14438 		fw_health->discoveries++;
14439 		goto fw_reset;
14440 	}
14441 
14442 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14443 	return;
14444 
14445 fw_reset:
14446 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14447 }
14448 
14449 static void bnxt_timer(struct timer_list *t)
14450 {
14451 	struct bnxt *bp = timer_container_of(bp, t, timer);
14452 	struct net_device *dev = bp->dev;
14453 
14454 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14455 		return;
14456 
14457 	if (atomic_read(&bp->intr_sem) != 0)
14458 		goto bnxt_restart_timer;
14459 
14460 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14461 		bnxt_fw_health_check(bp);
14462 
14463 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14464 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14465 
14466 	if (bnxt_tc_flower_enabled(bp))
14467 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14468 
14469 #ifdef CONFIG_RFS_ACCEL
14470 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14471 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14472 #endif /*CONFIG_RFS_ACCEL*/
14473 
14474 	if (bp->link_info.phy_retry) {
14475 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14476 			bp->link_info.phy_retry = false;
14477 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14478 		} else {
14479 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14480 		}
14481 	}
14482 
14483 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14484 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14485 
14486 bnxt_restart_timer:
14487 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14488 }
14489 
14490 static void bnxt_lock_sp(struct bnxt *bp)
14491 {
14492 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14493 	 * set.  If the device is being closed, bnxt_close() may be holding
14494 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14495 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14496 	 * instance lock.
14497 	 */
14498 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14499 	netdev_lock(bp->dev);
14500 }
14501 
14502 static void bnxt_unlock_sp(struct bnxt *bp)
14503 {
14504 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14505 	netdev_unlock(bp->dev);
14506 }
14507 
14508 /* Same as bnxt_lock_sp() with additional rtnl_lock */
14509 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
14510 {
14511 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14512 	rtnl_lock();
14513 	netdev_lock(bp->dev);
14514 }
14515 
14516 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
14517 {
14518 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14519 	netdev_unlock(bp->dev);
14520 	rtnl_unlock();
14521 }
14522 
14523 static void bnxt_tph_update(struct bnxt *bp)
14524 {
14525 	struct net_device *dev = bp->dev;
14526 	int i;
14527 
14528 	bnxt_lock_sp(bp);
14529 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
14530 		goto unlock;
14531 
14532 	for (i = 0; i < bp->rx_nr_rings; i++) {
14533 		struct bnxt_irq *irq;
14534 		int map_idx, err;
14535 		u16 tag;
14536 
14537 		map_idx = bnxt_cp_num_to_irq_num(bp, i);
14538 		irq = &bp->irq_tbl[map_idx];
14539 		tag = READ_ONCE(irq->new_tag);
14540 		if (irq->tag == tag)
14541 			continue;
14542 
14543 		if (pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag))
14544 			continue;
14545 
14546 		err = netdev_rx_queue_restart(dev, irq->ring_nr);
14547 		if (err) {
14548 			netdev_err(dev, "RX queue restart failed: err=%d\n",
14549 				   err);
14550 			continue;
14551 		}
14552 
14553 		irq->tag = tag;
14554 	}
14555 
14556 unlock:
14557 	bnxt_unlock_sp(bp);
14558 }
14559 
14560 /* Only called from bnxt_sp_task() */
14561 static void bnxt_reset(struct bnxt *bp, bool silent)
14562 {
14563 	bnxt_rtnl_lock_sp(bp);
14564 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14565 		bnxt_reset_task(bp, silent);
14566 	bnxt_rtnl_unlock_sp(bp);
14567 }
14568 
14569 /* Only called from bnxt_sp_task() */
14570 static void bnxt_rx_ring_reset(struct bnxt *bp)
14571 {
14572 	int i;
14573 
14574 	bnxt_rtnl_lock_sp(bp);
14575 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14576 		bnxt_rtnl_unlock_sp(bp);
14577 		return;
14578 	}
14579 	/* Disable and flush TPA before resetting the RX ring */
14580 	if (bp->flags & BNXT_FLAG_TPA)
14581 		bnxt_set_tpa(bp, false);
14582 	for (i = 0; i < bp->rx_nr_rings; i++) {
14583 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14584 		struct bnxt_cp_ring_info *cpr;
14585 		int rc;
14586 
14587 		if (!rxr->bnapi->in_reset)
14588 			continue;
14589 
14590 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14591 		if (rc) {
14592 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14593 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14594 			else
14595 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14596 					    rc);
14597 			bnxt_reset_task(bp, true);
14598 			break;
14599 		}
14600 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14601 		rxr->rx_prod = 0;
14602 		rxr->rx_agg_prod = 0;
14603 		rxr->rx_sw_agg_prod = 0;
14604 		rxr->rx_next_cons = 0;
14605 		rxr->bnapi->in_reset = false;
14606 		bnxt_alloc_one_rx_ring(bp, i);
14607 		cpr = &rxr->bnapi->cp_ring;
14608 		cpr->sw_stats->rx.rx_resets++;
14609 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14610 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14611 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14612 	}
14613 	if (bp->flags & BNXT_FLAG_TPA)
14614 		bnxt_set_tpa(bp, true);
14615 	bnxt_rtnl_unlock_sp(bp);
14616 }
14617 
14618 static void bnxt_fw_fatal_close(struct bnxt *bp)
14619 {
14620 	bnxt_tx_disable(bp);
14621 	bnxt_disable_napi(bp);
14622 	bnxt_disable_int_sync(bp);
14623 	bnxt_free_irq(bp);
14624 	bnxt_clear_int_mode(bp);
14625 	pci_disable_device(bp->pdev);
14626 }
14627 
14628 static void bnxt_fw_reset_close(struct bnxt *bp)
14629 {
14630 	/* When firmware is in fatal state, quiesce device and disable
14631 	 * bus master to prevent any potential bad DMAs before freeing
14632 	 * kernel memory.
14633 	 */
14634 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14635 		u16 val = 0;
14636 
14637 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14638 		if (val == 0xffff)
14639 			bp->fw_reset_min_dsecs = 0;
14640 		bnxt_fw_fatal_close(bp);
14641 	}
14642 	__bnxt_close_nic(bp, true, false);
14643 	bnxt_vf_reps_free(bp);
14644 	bnxt_clear_int_mode(bp);
14645 	bnxt_hwrm_func_drv_unrgtr(bp);
14646 	if (pci_is_enabled(bp->pdev))
14647 		pci_disable_device(bp->pdev);
14648 	bnxt_free_ctx_mem(bp, false);
14649 }
14650 
14651 static bool is_bnxt_fw_ok(struct bnxt *bp)
14652 {
14653 	struct bnxt_fw_health *fw_health = bp->fw_health;
14654 	bool no_heartbeat = false, has_reset = false;
14655 	u32 val;
14656 
14657 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14658 	if (val == fw_health->last_fw_heartbeat)
14659 		no_heartbeat = true;
14660 
14661 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14662 	if (val != fw_health->last_fw_reset_cnt)
14663 		has_reset = true;
14664 
14665 	if (!no_heartbeat && has_reset)
14666 		return true;
14667 
14668 	return false;
14669 }
14670 
14671 /* netdev instance lock is acquired before calling this function */
14672 static void bnxt_force_fw_reset(struct bnxt *bp)
14673 {
14674 	struct bnxt_fw_health *fw_health = bp->fw_health;
14675 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14676 	u32 wait_dsecs;
14677 
14678 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14679 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14680 		return;
14681 
14682 	/* we have to serialize with bnxt_refclk_read()*/
14683 	if (ptp) {
14684 		unsigned long flags;
14685 
14686 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14687 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14688 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14689 	} else {
14690 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14691 	}
14692 	bnxt_fw_reset_close(bp);
14693 	wait_dsecs = fw_health->master_func_wait_dsecs;
14694 	if (fw_health->primary) {
14695 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14696 			wait_dsecs = 0;
14697 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14698 	} else {
14699 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14700 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14701 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14702 	}
14703 
14704 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14705 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14706 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14707 }
14708 
14709 void bnxt_fw_exception(struct bnxt *bp)
14710 {
14711 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14712 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14713 	bnxt_ulp_stop(bp);
14714 	bnxt_lock_sp(bp);
14715 	bnxt_force_fw_reset(bp);
14716 	bnxt_unlock_sp(bp);
14717 }
14718 
14719 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14720  * < 0 on error.
14721  */
14722 static int bnxt_get_registered_vfs(struct bnxt *bp)
14723 {
14724 #ifdef CONFIG_BNXT_SRIOV
14725 	int rc;
14726 
14727 	if (!BNXT_PF(bp))
14728 		return 0;
14729 
14730 	rc = bnxt_hwrm_func_qcfg(bp);
14731 	if (rc) {
14732 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14733 		return rc;
14734 	}
14735 	if (bp->pf.registered_vfs)
14736 		return bp->pf.registered_vfs;
14737 	if (bp->sriov_cfg)
14738 		return 1;
14739 #endif
14740 	return 0;
14741 }
14742 
14743 void bnxt_fw_reset(struct bnxt *bp)
14744 {
14745 	bnxt_ulp_stop(bp);
14746 	bnxt_lock_sp(bp);
14747 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14748 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14749 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14750 		int n = 0, tmo;
14751 
14752 		/* we have to serialize with bnxt_refclk_read()*/
14753 		if (ptp) {
14754 			unsigned long flags;
14755 
14756 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14757 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14758 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14759 		} else {
14760 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14761 		}
14762 		if (bp->pf.active_vfs &&
14763 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14764 			n = bnxt_get_registered_vfs(bp);
14765 		if (n < 0) {
14766 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14767 				   n);
14768 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14769 			netif_close(bp->dev);
14770 			goto fw_reset_exit;
14771 		} else if (n > 0) {
14772 			u16 vf_tmo_dsecs = n * 10;
14773 
14774 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14775 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14776 			bp->fw_reset_state =
14777 				BNXT_FW_RESET_STATE_POLL_VF;
14778 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14779 			goto fw_reset_exit;
14780 		}
14781 		bnxt_fw_reset_close(bp);
14782 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14783 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14784 			tmo = HZ / 10;
14785 		} else {
14786 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14787 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14788 		}
14789 		bnxt_queue_fw_reset_work(bp, tmo);
14790 	}
14791 fw_reset_exit:
14792 	bnxt_unlock_sp(bp);
14793 }
14794 
14795 static void bnxt_chk_missed_irq(struct bnxt *bp)
14796 {
14797 	int i;
14798 
14799 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14800 		return;
14801 
14802 	for (i = 0; i < bp->cp_nr_rings; i++) {
14803 		struct bnxt_napi *bnapi = bp->bnapi[i];
14804 		struct bnxt_cp_ring_info *cpr;
14805 		u32 fw_ring_id;
14806 		int j;
14807 
14808 		if (!bnapi)
14809 			continue;
14810 
14811 		cpr = &bnapi->cp_ring;
14812 		for (j = 0; j < cpr->cp_ring_count; j++) {
14813 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14814 			u32 val[2];
14815 
14816 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14817 				continue;
14818 
14819 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14820 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14821 				continue;
14822 			}
14823 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14824 			bnxt_dbg_hwrm_ring_info_get(bp,
14825 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14826 				fw_ring_id, &val[0], &val[1]);
14827 			cpr->sw_stats->cmn.missed_irqs++;
14828 		}
14829 	}
14830 }
14831 
14832 static void bnxt_cfg_ntp_filters(struct bnxt *);
14833 
14834 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14835 {
14836 	struct bnxt_link_info *link_info = &bp->link_info;
14837 
14838 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14839 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14840 		if (bp->hwrm_spec_code >= 0x10201) {
14841 			if (link_info->auto_pause_setting &
14842 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14843 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14844 		} else {
14845 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14846 		}
14847 		bnxt_set_auto_speed(link_info);
14848 	} else {
14849 		bnxt_set_force_speed(link_info);
14850 		link_info->req_duplex = link_info->duplex_setting;
14851 	}
14852 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14853 		link_info->req_flow_ctrl =
14854 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14855 	else
14856 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14857 }
14858 
14859 static void bnxt_fw_echo_reply(struct bnxt *bp)
14860 {
14861 	struct bnxt_fw_health *fw_health = bp->fw_health;
14862 	struct hwrm_func_echo_response_input *req;
14863 	int rc;
14864 
14865 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14866 	if (rc)
14867 		return;
14868 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14869 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14870 	hwrm_req_send(bp, req);
14871 }
14872 
14873 static void bnxt_ulp_restart(struct bnxt *bp)
14874 {
14875 	bnxt_ulp_stop(bp);
14876 	bnxt_ulp_start(bp);
14877 }
14878 
14879 static void bnxt_sp_task(struct work_struct *work)
14880 {
14881 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14882 
14883 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14884 	smp_mb__after_atomic();
14885 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14886 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14887 		return;
14888 	}
14889 
14890 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14891 		bnxt_ulp_restart(bp);
14892 		bnxt_reenable_sriov(bp);
14893 	}
14894 
14895 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14896 		bnxt_cfg_ntp_filters(bp);
14897 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14898 		bnxt_hwrm_exec_fwd_req(bp);
14899 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14900 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14901 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14902 		bnxt_hwrm_port_qstats(bp, 0);
14903 		bnxt_hwrm_port_qstats_ext(bp, 0);
14904 		spin_lock(&bp->stats_lock);
14905 		bnxt_accumulate_all_stats(bp);
14906 		bp->stats_updated_jiffies = jiffies;
14907 		spin_unlock(&bp->stats_lock);
14908 	}
14909 
14910 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14911 		int rc;
14912 
14913 		mutex_lock(&bp->link_lock);
14914 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14915 				       &bp->sp_event))
14916 			bnxt_hwrm_phy_qcaps(bp);
14917 
14918 		rc = bnxt_update_link(bp, true);
14919 		if (rc)
14920 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14921 				   rc);
14922 
14923 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14924 				       &bp->sp_event))
14925 			bnxt_init_ethtool_link_settings(bp);
14926 		mutex_unlock(&bp->link_lock);
14927 	}
14928 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14929 		int rc;
14930 
14931 		mutex_lock(&bp->link_lock);
14932 		rc = bnxt_update_phy_setting(bp);
14933 		mutex_unlock(&bp->link_lock);
14934 		if (rc) {
14935 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14936 		} else {
14937 			bp->link_info.phy_retry = false;
14938 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14939 		}
14940 	}
14941 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14942 		mutex_lock(&bp->link_lock);
14943 		bnxt_get_port_module_status(bp);
14944 		mutex_unlock(&bp->link_lock);
14945 	}
14946 
14947 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14948 		bnxt_tc_flow_stats_work(bp);
14949 
14950 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14951 		bnxt_chk_missed_irq(bp);
14952 
14953 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14954 		bnxt_fw_echo_reply(bp);
14955 
14956 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14957 		bnxt_hwmon_notify_event(bp);
14958 
14959 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14960 	 * must be the last functions to be called before exiting.
14961 	 */
14962 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14963 		bnxt_reset(bp, false);
14964 
14965 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14966 		bnxt_reset(bp, true);
14967 
14968 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14969 		bnxt_rx_ring_reset(bp);
14970 
14971 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14972 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14973 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14974 			bnxt_devlink_health_fw_report(bp);
14975 		else
14976 			bnxt_fw_reset(bp);
14977 	}
14978 
14979 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14980 		if (!is_bnxt_fw_ok(bp))
14981 			bnxt_devlink_health_fw_report(bp);
14982 	}
14983 
14984 	if (test_and_clear_bit(BNXT_TPH_UPDATE_SP_EVENT, &bp->sp_event))
14985 		bnxt_tph_update(bp);
14986 
14987 	smp_mb__before_atomic();
14988 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14989 }
14990 
14991 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14992 				int *max_cp);
14993 
14994 /* Under netdev instance lock */
14995 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14996 		     int tx_xdp)
14997 {
14998 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14999 	struct bnxt_hw_rings hwr = {0};
15000 	int rx_rings = rx;
15001 	int rc;
15002 
15003 	if (tcs)
15004 		tx_sets = tcs;
15005 
15006 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
15007 
15008 	if (max_rx < rx_rings)
15009 		return -ENOMEM;
15010 
15011 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15012 		rx_rings <<= 1;
15013 
15014 	hwr.rx = rx_rings;
15015 	hwr.tx = tx * tx_sets + tx_xdp;
15016 	if (max_tx < hwr.tx)
15017 		return -ENOMEM;
15018 
15019 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
15020 
15021 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
15022 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
15023 	if (max_cp < hwr.cp)
15024 		return -ENOMEM;
15025 	hwr.stat = hwr.cp;
15026 	if (BNXT_NEW_RM(bp)) {
15027 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
15028 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
15029 		hwr.grp = rx;
15030 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
15031 	}
15032 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
15033 		hwr.cp_p5 = hwr.tx + rx;
15034 	rc = bnxt_hwrm_check_rings(bp, &hwr);
15035 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
15036 		if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) {
15037 			hwr.cp += bnxt_get_ulp_msix_num(bp);
15038 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
15039 		}
15040 		if (hwr.cp > bp->total_irqs) {
15041 			int total_msix = bnxt_change_msix(bp, hwr.cp);
15042 
15043 			if (total_msix < hwr.cp) {
15044 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
15045 					    hwr.cp, total_msix);
15046 				rc = -ENOSPC;
15047 			}
15048 		}
15049 	}
15050 	return rc;
15051 }
15052 
15053 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
15054 {
15055 	if (bp->bar2) {
15056 		pci_iounmap(pdev, bp->bar2);
15057 		bp->bar2 = NULL;
15058 	}
15059 
15060 	if (bp->bar1) {
15061 		pci_iounmap(pdev, bp->bar1);
15062 		bp->bar1 = NULL;
15063 	}
15064 
15065 	if (bp->bar0) {
15066 		pci_iounmap(pdev, bp->bar0);
15067 		bp->bar0 = NULL;
15068 	}
15069 }
15070 
15071 static void bnxt_cleanup_pci(struct bnxt *bp)
15072 {
15073 	pci_disable_ptm(bp->pdev);
15074 	bnxt_unmap_bars(bp, bp->pdev);
15075 	pci_release_regions(bp->pdev);
15076 	if (pci_is_enabled(bp->pdev))
15077 		pci_disable_device(bp->pdev);
15078 }
15079 
15080 static void bnxt_init_dflt_coal(struct bnxt *bp)
15081 {
15082 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
15083 	struct bnxt_coal *coal;
15084 	u16 flags = 0;
15085 
15086 	if (coal_cap->cmpl_params &
15087 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
15088 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
15089 
15090 	/* Tick values in micro seconds.
15091 	 * 1 coal_buf x bufs_per_record = 1 completion record.
15092 	 */
15093 	coal = &bp->rx_coal;
15094 	coal->coal_ticks = 10;
15095 	coal->coal_bufs = 30;
15096 	coal->coal_ticks_irq = 1;
15097 	coal->coal_bufs_irq = 2;
15098 	coal->idle_thresh = 50;
15099 	coal->bufs_per_record = 2;
15100 	coal->budget = 64;		/* NAPI budget */
15101 	coal->flags = flags;
15102 
15103 	coal = &bp->tx_coal;
15104 	coal->coal_ticks = 28;
15105 	coal->coal_bufs = 30;
15106 	coal->coal_ticks_irq = 2;
15107 	coal->coal_bufs_irq = 2;
15108 	coal->bufs_per_record = 1;
15109 	coal->flags = flags;
15110 
15111 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
15112 }
15113 
15114 /* FW that pre-reserves 1 VNIC per function */
15115 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
15116 {
15117 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
15118 
15119 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15120 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
15121 		return true;
15122 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15123 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
15124 		return true;
15125 	return false;
15126 }
15127 
15128 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
15129 {
15130 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
15131 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
15132 	int rc;
15133 
15134 	bp->max_pfcwd_tmo_ms = 0;
15135 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
15136 	if (rc)
15137 		return;
15138 	resp = hwrm_req_hold(bp, req);
15139 	rc = hwrm_req_send_silent(bp, req);
15140 	if (!rc)
15141 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
15142 	hwrm_req_drop(bp, req);
15143 }
15144 
15145 static int bnxt_fw_init_one_p1(struct bnxt *bp)
15146 {
15147 	int rc;
15148 
15149 	bp->fw_cap = 0;
15150 	rc = bnxt_hwrm_ver_get(bp);
15151 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
15152 	 * so wait before continuing with recovery.
15153 	 */
15154 	if (rc)
15155 		msleep(100);
15156 	bnxt_try_map_fw_health_reg(bp);
15157 	if (rc) {
15158 		rc = bnxt_try_recover_fw(bp);
15159 		if (rc)
15160 			return rc;
15161 		rc = bnxt_hwrm_ver_get(bp);
15162 		if (rc)
15163 			return rc;
15164 	}
15165 
15166 	bnxt_nvm_cfg_ver_get(bp);
15167 
15168 	rc = bnxt_hwrm_func_reset(bp);
15169 	if (rc)
15170 		return -ENODEV;
15171 
15172 	bnxt_hwrm_fw_set_time(bp);
15173 	return 0;
15174 }
15175 
15176 static int bnxt_fw_init_one_p2(struct bnxt *bp)
15177 {
15178 	int rc;
15179 
15180 	/* Get the MAX capabilities for this function */
15181 	rc = bnxt_hwrm_func_qcaps(bp);
15182 	if (rc) {
15183 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
15184 			   rc);
15185 		return -ENODEV;
15186 	}
15187 
15188 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
15189 	if (rc)
15190 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
15191 			    rc);
15192 
15193 	if (bnxt_alloc_fw_health(bp)) {
15194 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
15195 	} else {
15196 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
15197 		if (rc)
15198 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
15199 				    rc);
15200 	}
15201 
15202 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
15203 	if (rc)
15204 		return -ENODEV;
15205 
15206 	rc = bnxt_alloc_crash_dump_mem(bp);
15207 	if (rc)
15208 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
15209 			    rc);
15210 	if (!rc) {
15211 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
15212 		if (rc) {
15213 			bnxt_free_crash_dump_mem(bp);
15214 			netdev_warn(bp->dev,
15215 				    "hwrm crash dump mem failure rc: %d\n", rc);
15216 		}
15217 	}
15218 
15219 	if (bnxt_fw_pre_resv_vnics(bp))
15220 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
15221 
15222 	bnxt_hwrm_pfcwd_qcaps(bp);
15223 	bnxt_hwrm_func_qcfg(bp);
15224 	bnxt_hwrm_vnic_qcaps(bp);
15225 	bnxt_hwrm_port_led_qcaps(bp);
15226 	bnxt_ethtool_init(bp);
15227 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
15228 		__bnxt_hwrm_ptp_qcfg(bp);
15229 	bnxt_dcb_init(bp);
15230 	bnxt_hwmon_init(bp);
15231 	return 0;
15232 }
15233 
15234 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
15235 {
15236 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
15237 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
15238 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
15239 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
15240 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
15241 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
15242 		bp->rss_hash_delta = bp->rss_hash_cfg;
15243 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
15244 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
15245 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
15246 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
15247 	}
15248 }
15249 
15250 static void bnxt_set_dflt_rfs(struct bnxt *bp)
15251 {
15252 	struct net_device *dev = bp->dev;
15253 
15254 	dev->hw_features &= ~NETIF_F_NTUPLE;
15255 	dev->features &= ~NETIF_F_NTUPLE;
15256 	bp->flags &= ~BNXT_FLAG_RFS;
15257 	if (bnxt_rfs_supported(bp)) {
15258 		dev->hw_features |= NETIF_F_NTUPLE;
15259 		if (bnxt_rfs_capable(bp, false)) {
15260 			bp->flags |= BNXT_FLAG_RFS;
15261 			dev->features |= NETIF_F_NTUPLE;
15262 		}
15263 	}
15264 }
15265 
15266 static void bnxt_fw_init_one_p3(struct bnxt *bp)
15267 {
15268 	struct pci_dev *pdev = bp->pdev;
15269 
15270 	bnxt_set_dflt_rss_hash_type(bp);
15271 	bnxt_set_dflt_rfs(bp);
15272 
15273 	bnxt_get_wol_settings(bp);
15274 	if (bp->flags & BNXT_FLAG_WOL_CAP)
15275 		device_set_wakeup_enable(&pdev->dev, bp->wol);
15276 	else
15277 		device_set_wakeup_capable(&pdev->dev, false);
15278 
15279 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
15280 	bnxt_hwrm_coal_params_qcaps(bp);
15281 }
15282 
15283 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15284 
15285 int bnxt_fw_init_one(struct bnxt *bp)
15286 {
15287 	int rc;
15288 
15289 	rc = bnxt_fw_init_one_p1(bp);
15290 	if (rc) {
15291 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15292 		return rc;
15293 	}
15294 	rc = bnxt_fw_init_one_p2(bp);
15295 	if (rc) {
15296 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15297 		return rc;
15298 	}
15299 	rc = bnxt_probe_phy(bp, false);
15300 	if (rc)
15301 		return rc;
15302 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15303 	if (rc)
15304 		return rc;
15305 
15306 	bnxt_fw_init_one_p3(bp);
15307 	return 0;
15308 }
15309 
15310 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15311 {
15312 	struct bnxt_fw_health *fw_health = bp->fw_health;
15313 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15314 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15315 	u32 reg_type, reg_off, delay_msecs;
15316 
15317 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15318 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15319 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15320 	switch (reg_type) {
15321 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15322 		pci_write_config_dword(bp->pdev, reg_off, val);
15323 		break;
15324 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15325 		writel(reg_off & BNXT_GRC_BASE_MASK,
15326 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15327 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15328 		fallthrough;
15329 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15330 		writel(val, bp->bar0 + reg_off);
15331 		break;
15332 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15333 		writel(val, bp->bar1 + reg_off);
15334 		break;
15335 	}
15336 	if (delay_msecs) {
15337 		pci_read_config_dword(bp->pdev, 0, &val);
15338 		msleep(delay_msecs);
15339 	}
15340 }
15341 
15342 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15343 {
15344 	struct hwrm_func_qcfg_output *resp;
15345 	struct hwrm_func_qcfg_input *req;
15346 	bool result = true; /* firmware will enforce if unknown */
15347 
15348 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15349 		return result;
15350 
15351 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15352 		return result;
15353 
15354 	req->fid = cpu_to_le16(0xffff);
15355 	resp = hwrm_req_hold(bp, req);
15356 	if (!hwrm_req_send(bp, req))
15357 		result = !!(le16_to_cpu(resp->flags) &
15358 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15359 	hwrm_req_drop(bp, req);
15360 	return result;
15361 }
15362 
15363 static void bnxt_reset_all(struct bnxt *bp)
15364 {
15365 	struct bnxt_fw_health *fw_health = bp->fw_health;
15366 	int i, rc;
15367 
15368 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15369 		bnxt_fw_reset_via_optee(bp);
15370 		bp->fw_reset_timestamp = jiffies;
15371 		return;
15372 	}
15373 
15374 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15375 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15376 			bnxt_fw_reset_writel(bp, i);
15377 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15378 		struct hwrm_fw_reset_input *req;
15379 
15380 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15381 		if (!rc) {
15382 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15383 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15384 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15385 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15386 			rc = hwrm_req_send(bp, req);
15387 		}
15388 		if (rc != -ENODEV)
15389 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15390 	}
15391 	bp->fw_reset_timestamp = jiffies;
15392 }
15393 
15394 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15395 {
15396 	return time_after(jiffies, bp->fw_reset_timestamp +
15397 			  (bp->fw_reset_max_dsecs * HZ / 10));
15398 }
15399 
15400 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15401 {
15402 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15403 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15404 		bnxt_dl_health_fw_status_update(bp, false);
15405 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15406 	netif_close(bp->dev);
15407 }
15408 
15409 static void bnxt_fw_reset_task(struct work_struct *work)
15410 {
15411 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15412 	int rc = 0;
15413 
15414 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15415 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15416 		return;
15417 	}
15418 
15419 	switch (bp->fw_reset_state) {
15420 	case BNXT_FW_RESET_STATE_POLL_VF: {
15421 		int n = bnxt_get_registered_vfs(bp);
15422 		int tmo;
15423 
15424 		if (n < 0) {
15425 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15426 				   n, jiffies_to_msecs(jiffies -
15427 				   bp->fw_reset_timestamp));
15428 			goto fw_reset_abort;
15429 		} else if (n > 0) {
15430 			if (bnxt_fw_reset_timeout(bp)) {
15431 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15432 				bp->fw_reset_state = 0;
15433 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15434 					   n);
15435 				goto ulp_start;
15436 			}
15437 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15438 			return;
15439 		}
15440 		bp->fw_reset_timestamp = jiffies;
15441 		netdev_lock(bp->dev);
15442 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15443 			bnxt_fw_reset_abort(bp, rc);
15444 			netdev_unlock(bp->dev);
15445 			goto ulp_start;
15446 		}
15447 		bnxt_fw_reset_close(bp);
15448 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15449 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15450 			tmo = HZ / 10;
15451 		} else {
15452 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15453 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15454 		}
15455 		netdev_unlock(bp->dev);
15456 		bnxt_queue_fw_reset_work(bp, tmo);
15457 		return;
15458 	}
15459 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15460 		u32 val;
15461 
15462 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15463 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15464 		    !bnxt_fw_reset_timeout(bp)) {
15465 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15466 			return;
15467 		}
15468 
15469 		if (!bp->fw_health->primary) {
15470 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15471 
15472 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15473 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15474 			return;
15475 		}
15476 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15477 	}
15478 		fallthrough;
15479 	case BNXT_FW_RESET_STATE_RESET_FW:
15480 		bnxt_reset_all(bp);
15481 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15482 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15483 		return;
15484 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15485 		bnxt_inv_fw_health_reg(bp);
15486 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15487 		    !bp->fw_reset_min_dsecs) {
15488 			u16 val;
15489 
15490 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15491 			if (val == 0xffff) {
15492 				if (bnxt_fw_reset_timeout(bp)) {
15493 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15494 					rc = -ETIMEDOUT;
15495 					goto fw_reset_abort;
15496 				}
15497 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15498 				return;
15499 			}
15500 		}
15501 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15502 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15503 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15504 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15505 			bnxt_dl_remote_reload(bp);
15506 		if (pci_enable_device(bp->pdev)) {
15507 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15508 			rc = -ENODEV;
15509 			goto fw_reset_abort;
15510 		}
15511 		pci_set_master(bp->pdev);
15512 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15513 		fallthrough;
15514 	case BNXT_FW_RESET_STATE_POLL_FW:
15515 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15516 		rc = bnxt_hwrm_poll(bp);
15517 		if (rc) {
15518 			if (bnxt_fw_reset_timeout(bp)) {
15519 				netdev_err(bp->dev, "Firmware reset aborted\n");
15520 				goto fw_reset_abort_status;
15521 			}
15522 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15523 			return;
15524 		}
15525 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15526 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15527 		fallthrough;
15528 	case BNXT_FW_RESET_STATE_OPENING:
15529 		while (!rtnl_trylock()) {
15530 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15531 			return;
15532 		}
15533 		netdev_lock(bp->dev);
15534 		rc = bnxt_open(bp->dev);
15535 		if (rc) {
15536 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15537 			bnxt_fw_reset_abort(bp, rc);
15538 			netdev_unlock(bp->dev);
15539 			rtnl_unlock();
15540 			goto ulp_start;
15541 		}
15542 
15543 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15544 		    bp->fw_health->enabled) {
15545 			bp->fw_health->last_fw_reset_cnt =
15546 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15547 		}
15548 		bp->fw_reset_state = 0;
15549 		/* Make sure fw_reset_state is 0 before clearing the flag */
15550 		smp_mb__before_atomic();
15551 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15552 		bnxt_ptp_reapply_pps(bp);
15553 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15554 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15555 			bnxt_dl_health_fw_recovery_done(bp);
15556 			bnxt_dl_health_fw_status_update(bp, true);
15557 		}
15558 		netdev_unlock(bp->dev);
15559 		rtnl_unlock();
15560 		bnxt_ulp_start(bp);
15561 		bnxt_reenable_sriov(bp);
15562 		netdev_lock(bp->dev);
15563 		bnxt_vf_reps_alloc(bp);
15564 		bnxt_vf_reps_open(bp);
15565 		netdev_unlock(bp->dev);
15566 		break;
15567 	}
15568 	return;
15569 
15570 fw_reset_abort_status:
15571 	if (bp->fw_health->status_reliable ||
15572 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15573 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15574 
15575 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15576 	}
15577 fw_reset_abort:
15578 	netdev_lock(bp->dev);
15579 	bnxt_fw_reset_abort(bp, rc);
15580 	netdev_unlock(bp->dev);
15581 ulp_start:
15582 	if (!rc)
15583 		bnxt_ulp_start(bp);
15584 }
15585 
15586 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15587 {
15588 	int rc;
15589 	struct bnxt *bp = netdev_priv(dev);
15590 
15591 	SET_NETDEV_DEV(dev, &pdev->dev);
15592 
15593 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15594 	rc = pci_enable_device(pdev);
15595 	if (rc) {
15596 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15597 		goto init_err;
15598 	}
15599 
15600 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15601 		dev_err(&pdev->dev,
15602 			"Cannot find PCI device base address, aborting\n");
15603 		rc = -ENODEV;
15604 		goto init_err_disable;
15605 	}
15606 
15607 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15608 	if (rc) {
15609 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15610 		goto init_err_disable;
15611 	}
15612 
15613 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15614 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15615 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15616 		rc = -EIO;
15617 		goto init_err_release;
15618 	}
15619 
15620 	pci_set_master(pdev);
15621 
15622 	bp->dev = dev;
15623 	bp->pdev = pdev;
15624 
15625 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15626 	 * determines the BAR size.
15627 	 */
15628 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15629 	if (!bp->bar0) {
15630 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15631 		rc = -ENOMEM;
15632 		goto init_err_release;
15633 	}
15634 
15635 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15636 	if (!bp->bar2) {
15637 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15638 		rc = -ENOMEM;
15639 		goto init_err_release;
15640 	}
15641 
15642 	pci_enable_ptm(pdev);
15643 
15644 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15645 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15646 
15647 	spin_lock_init(&bp->ntp_fltr_lock);
15648 	spin_lock_init(&bp->stats_lock);
15649 #if BITS_PER_LONG == 32
15650 	spin_lock_init(&bp->db_lock);
15651 #endif
15652 
15653 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15654 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15655 
15656 	timer_setup(&bp->timer, bnxt_timer, 0);
15657 	bp->current_interval = BNXT_TIMER_INTERVAL;
15658 
15659 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15660 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15661 
15662 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15663 	return 0;
15664 
15665 init_err_release:
15666 	bnxt_unmap_bars(bp, pdev);
15667 	pci_release_regions(pdev);
15668 
15669 init_err_disable:
15670 	pci_disable_device(pdev);
15671 
15672 init_err:
15673 	return rc;
15674 }
15675 
15676 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15677 {
15678 	struct sockaddr *addr = p;
15679 	struct bnxt *bp = netdev_priv(dev);
15680 	int rc = 0;
15681 
15682 	netdev_assert_locked(dev);
15683 
15684 	if (!is_valid_ether_addr(addr->sa_data))
15685 		return -EADDRNOTAVAIL;
15686 
15687 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15688 		return 0;
15689 
15690 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15691 	if (rc)
15692 		return rc;
15693 
15694 	eth_hw_addr_set(dev, addr->sa_data);
15695 	bnxt_clear_usr_fltrs(bp, true);
15696 	if (netif_running(dev)) {
15697 		bnxt_close_nic(bp, false, false);
15698 		rc = bnxt_open_nic(bp, false, false);
15699 	}
15700 
15701 	return rc;
15702 }
15703 
15704 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15705 {
15706 	struct bnxt *bp = netdev_priv(dev);
15707 
15708 	netdev_assert_locked(dev);
15709 
15710 	if (netif_running(dev))
15711 		bnxt_close_nic(bp, true, false);
15712 
15713 	WRITE_ONCE(dev->mtu, new_mtu);
15714 
15715 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15716 	 * program is attached.  We need to set the AGG rings settings and
15717 	 * rx_skb_func accordingly.
15718 	 */
15719 	if (READ_ONCE(bp->xdp_prog))
15720 		bnxt_set_rx_skb_mode(bp, true);
15721 
15722 	bnxt_set_ring_params(bp);
15723 
15724 	if (netif_running(dev))
15725 		return bnxt_open_nic(bp, true, false);
15726 
15727 	return 0;
15728 }
15729 
15730 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15731 {
15732 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15733 
15734 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15735 			       tx_cp + bp->rx_nr_rings;
15736 }
15737 
15738 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15739 {
15740 	struct bnxt *bp = netdev_priv(dev);
15741 	bool sh = false;
15742 	int rc;
15743 
15744 	if (tc > bp->max_tc) {
15745 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15746 			   tc, bp->max_tc);
15747 		return -EINVAL;
15748 	}
15749 
15750 	if (bp->num_tc == tc)
15751 		return 0;
15752 
15753 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15754 		sh = true;
15755 
15756 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15757 			      sh, tc, bp->tx_nr_rings_xdp);
15758 	if (rc)
15759 		return rc;
15760 
15761 	/* Needs to close the device and do hw resource re-allocations */
15762 	if (netif_running(bp->dev))
15763 		bnxt_close_nic(bp, true, false);
15764 
15765 	if (tc) {
15766 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15767 		netdev_set_num_tc(dev, tc);
15768 		bp->num_tc = tc;
15769 	} else {
15770 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15771 		netdev_reset_tc(dev);
15772 		bp->num_tc = 0;
15773 	}
15774 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15775 	bnxt_set_cp_rings(bp, sh);
15776 
15777 	if (netif_running(bp->dev))
15778 		return bnxt_open_nic(bp, true, false);
15779 
15780 	return 0;
15781 }
15782 
15783 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15784 				  void *cb_priv)
15785 {
15786 	struct bnxt *bp = cb_priv;
15787 
15788 	if (!bnxt_tc_flower_enabled(bp) ||
15789 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15790 		return -EOPNOTSUPP;
15791 
15792 	switch (type) {
15793 	case TC_SETUP_CLSFLOWER:
15794 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15795 	default:
15796 		return -EOPNOTSUPP;
15797 	}
15798 }
15799 
15800 LIST_HEAD(bnxt_block_cb_list);
15801 
15802 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15803 			 void *type_data)
15804 {
15805 	struct bnxt *bp = netdev_priv(dev);
15806 
15807 	switch (type) {
15808 	case TC_SETUP_BLOCK:
15809 		return flow_block_cb_setup_simple(type_data,
15810 						  &bnxt_block_cb_list,
15811 						  bnxt_setup_tc_block_cb,
15812 						  bp, bp, true);
15813 	case TC_SETUP_QDISC_MQPRIO: {
15814 		struct tc_mqprio_qopt *mqprio = type_data;
15815 
15816 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15817 
15818 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15819 	}
15820 	default:
15821 		return -EOPNOTSUPP;
15822 	}
15823 }
15824 
15825 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15826 			    const struct sk_buff *skb)
15827 {
15828 	struct bnxt_vnic_info *vnic;
15829 
15830 	if (skb)
15831 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15832 
15833 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15834 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15835 }
15836 
15837 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15838 			   u32 idx)
15839 {
15840 	struct hlist_head *head;
15841 	int bit_id;
15842 
15843 	spin_lock_bh(&bp->ntp_fltr_lock);
15844 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15845 	if (bit_id < 0) {
15846 		spin_unlock_bh(&bp->ntp_fltr_lock);
15847 		return -ENOMEM;
15848 	}
15849 
15850 	fltr->base.sw_id = (u16)bit_id;
15851 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15852 	fltr->base.flags |= BNXT_ACT_RING_DST;
15853 	head = &bp->ntp_fltr_hash_tbl[idx];
15854 	hlist_add_head_rcu(&fltr->base.hash, head);
15855 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15856 	bnxt_insert_usr_fltr(bp, &fltr->base);
15857 	bp->ntp_fltr_count++;
15858 	spin_unlock_bh(&bp->ntp_fltr_lock);
15859 	return 0;
15860 }
15861 
15862 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15863 			    struct bnxt_ntuple_filter *f2)
15864 {
15865 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15866 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15867 	struct flow_keys *keys1 = &f1->fkeys;
15868 	struct flow_keys *keys2 = &f2->fkeys;
15869 
15870 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15871 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15872 		return false;
15873 
15874 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15875 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15876 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15877 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15878 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15879 			return false;
15880 	} else {
15881 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15882 				     &keys2->addrs.v6addrs.src) ||
15883 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15884 				     &masks2->addrs.v6addrs.src) ||
15885 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15886 				     &keys2->addrs.v6addrs.dst) ||
15887 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15888 				     &masks2->addrs.v6addrs.dst))
15889 			return false;
15890 	}
15891 
15892 	return keys1->ports.src == keys2->ports.src &&
15893 	       masks1->ports.src == masks2->ports.src &&
15894 	       keys1->ports.dst == keys2->ports.dst &&
15895 	       masks1->ports.dst == masks2->ports.dst &&
15896 	       keys1->control.flags == keys2->control.flags &&
15897 	       f1->l2_fltr == f2->l2_fltr;
15898 }
15899 
15900 struct bnxt_ntuple_filter *
15901 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15902 				struct bnxt_ntuple_filter *fltr, u32 idx)
15903 {
15904 	struct bnxt_ntuple_filter *f;
15905 	struct hlist_head *head;
15906 
15907 	head = &bp->ntp_fltr_hash_tbl[idx];
15908 	hlist_for_each_entry_rcu(f, head, base.hash) {
15909 		if (bnxt_fltr_match(f, fltr))
15910 			return f;
15911 	}
15912 	return NULL;
15913 }
15914 
15915 #ifdef CONFIG_RFS_ACCEL
15916 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15917 			      u16 rxq_index, u32 flow_id)
15918 {
15919 	struct bnxt *bp = netdev_priv(dev);
15920 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15921 	struct flow_keys *fkeys;
15922 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15923 	struct bnxt_l2_filter *l2_fltr;
15924 	int rc = 0, idx;
15925 	u32 flags;
15926 
15927 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15928 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15929 		atomic_inc(&l2_fltr->refcnt);
15930 	} else {
15931 		struct bnxt_l2_key key;
15932 
15933 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15934 		key.vlan = 0;
15935 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15936 		if (!l2_fltr)
15937 			return -EINVAL;
15938 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15939 			bnxt_del_l2_filter(bp, l2_fltr);
15940 			return -EINVAL;
15941 		}
15942 	}
15943 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
15944 	if (!new_fltr) {
15945 		bnxt_del_l2_filter(bp, l2_fltr);
15946 		return -ENOMEM;
15947 	}
15948 
15949 	fkeys = &new_fltr->fkeys;
15950 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15951 		rc = -EPROTONOSUPPORT;
15952 		goto err_free;
15953 	}
15954 
15955 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15956 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15957 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15958 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15959 		rc = -EPROTONOSUPPORT;
15960 		goto err_free;
15961 	}
15962 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15963 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15964 		if (bp->hwrm_spec_code < 0x10601) {
15965 			rc = -EPROTONOSUPPORT;
15966 			goto err_free;
15967 		}
15968 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15969 	}
15970 	flags = fkeys->control.flags;
15971 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15972 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15973 		rc = -EPROTONOSUPPORT;
15974 		goto err_free;
15975 	}
15976 	new_fltr->l2_fltr = l2_fltr;
15977 
15978 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15979 	rcu_read_lock();
15980 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15981 	if (fltr) {
15982 		rc = fltr->base.sw_id;
15983 		rcu_read_unlock();
15984 		goto err_free;
15985 	}
15986 	rcu_read_unlock();
15987 
15988 	new_fltr->flow_id = flow_id;
15989 	new_fltr->base.rxq = rxq_index;
15990 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15991 	if (!rc) {
15992 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15993 		return new_fltr->base.sw_id;
15994 	}
15995 
15996 err_free:
15997 	bnxt_del_l2_filter(bp, l2_fltr);
15998 	kfree(new_fltr);
15999 	return rc;
16000 }
16001 #endif
16002 
16003 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
16004 {
16005 	spin_lock_bh(&bp->ntp_fltr_lock);
16006 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
16007 		spin_unlock_bh(&bp->ntp_fltr_lock);
16008 		return;
16009 	}
16010 	hlist_del_rcu(&fltr->base.hash);
16011 	bnxt_del_one_usr_fltr(bp, &fltr->base);
16012 	bp->ntp_fltr_count--;
16013 	spin_unlock_bh(&bp->ntp_fltr_lock);
16014 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
16015 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
16016 	kfree_rcu(fltr, base.rcu);
16017 }
16018 
16019 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
16020 {
16021 #ifdef CONFIG_RFS_ACCEL
16022 	int i;
16023 
16024 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
16025 		struct hlist_head *head;
16026 		struct hlist_node *tmp;
16027 		struct bnxt_ntuple_filter *fltr;
16028 		int rc;
16029 
16030 		head = &bp->ntp_fltr_hash_tbl[i];
16031 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
16032 			bool del = false;
16033 
16034 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
16035 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
16036 					continue;
16037 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
16038 							fltr->flow_id,
16039 							fltr->base.sw_id)) {
16040 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
16041 									 fltr);
16042 					del = true;
16043 				}
16044 			} else {
16045 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
16046 								       fltr);
16047 				if (rc)
16048 					del = true;
16049 				else
16050 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
16051 			}
16052 
16053 			if (del)
16054 				bnxt_del_ntp_filter(bp, fltr);
16055 		}
16056 	}
16057 #endif
16058 }
16059 
16060 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
16061 				    unsigned int entry, struct udp_tunnel_info *ti)
16062 {
16063 	struct bnxt *bp = netdev_priv(netdev);
16064 	unsigned int cmd;
16065 
16066 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16067 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
16068 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16069 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
16070 	else
16071 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
16072 
16073 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
16074 }
16075 
16076 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
16077 				      unsigned int entry, struct udp_tunnel_info *ti)
16078 {
16079 	struct bnxt *bp = netdev_priv(netdev);
16080 	unsigned int cmd;
16081 
16082 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16083 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
16084 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16085 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
16086 	else
16087 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
16088 
16089 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
16090 }
16091 
16092 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
16093 	.set_port	= bnxt_udp_tunnel_set_port,
16094 	.unset_port	= bnxt_udp_tunnel_unset_port,
16095 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16096 	.tables		= {
16097 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16098 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16099 	},
16100 }, bnxt_udp_tunnels_p7 = {
16101 	.set_port	= bnxt_udp_tunnel_set_port,
16102 	.unset_port	= bnxt_udp_tunnel_unset_port,
16103 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16104 	.tables		= {
16105 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16106 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16107 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
16108 	},
16109 };
16110 
16111 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
16112 			       struct net_device *dev, u32 filter_mask,
16113 			       int nlflags)
16114 {
16115 	struct bnxt *bp = netdev_priv(dev);
16116 
16117 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
16118 				       nlflags, filter_mask, NULL);
16119 }
16120 
16121 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
16122 			       u16 flags, struct netlink_ext_ack *extack)
16123 {
16124 	struct bnxt *bp = netdev_priv(dev);
16125 	struct nlattr *attr, *br_spec;
16126 	int rem, rc = 0;
16127 
16128 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
16129 		return -EOPNOTSUPP;
16130 
16131 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
16132 	if (!br_spec)
16133 		return -EINVAL;
16134 
16135 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
16136 		u16 mode;
16137 
16138 		mode = nla_get_u16(attr);
16139 		if (mode == bp->br_mode)
16140 			break;
16141 
16142 		rc = bnxt_hwrm_set_br_mode(bp, mode);
16143 		if (!rc)
16144 			bp->br_mode = mode;
16145 		break;
16146 	}
16147 	return rc;
16148 }
16149 
16150 int bnxt_get_port_parent_id(struct net_device *dev,
16151 			    struct netdev_phys_item_id *ppid)
16152 {
16153 	struct bnxt *bp = netdev_priv(dev);
16154 
16155 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
16156 		return -EOPNOTSUPP;
16157 
16158 	/* The PF and it's VF-reps only support the switchdev framework */
16159 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
16160 		return -EOPNOTSUPP;
16161 
16162 	ppid->id_len = sizeof(bp->dsn);
16163 	memcpy(ppid->id, bp->dsn, ppid->id_len);
16164 
16165 	return 0;
16166 }
16167 
16168 static const struct net_device_ops bnxt_netdev_ops = {
16169 	.ndo_open		= bnxt_open,
16170 	.ndo_start_xmit		= bnxt_start_xmit,
16171 	.ndo_stop		= bnxt_close,
16172 	.ndo_get_stats64	= bnxt_get_stats64,
16173 	.ndo_set_rx_mode_async	= bnxt_set_rx_mode,
16174 	.ndo_eth_ioctl		= bnxt_ioctl,
16175 	.ndo_validate_addr	= eth_validate_addr,
16176 	.ndo_set_mac_address	= bnxt_change_mac_addr,
16177 	.ndo_change_mtu		= bnxt_change_mtu,
16178 	.ndo_fix_features	= bnxt_fix_features,
16179 	.ndo_set_features	= bnxt_set_features,
16180 	.ndo_features_check	= bnxt_features_check,
16181 	.ndo_tx_timeout		= bnxt_tx_timeout,
16182 #ifdef CONFIG_BNXT_SRIOV
16183 	.ndo_get_vf_config	= bnxt_get_vf_config,
16184 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
16185 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
16186 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
16187 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
16188 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
16189 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
16190 #endif
16191 	.ndo_setup_tc           = bnxt_setup_tc,
16192 #ifdef CONFIG_RFS_ACCEL
16193 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
16194 #endif
16195 	.ndo_bpf		= bnxt_xdp,
16196 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
16197 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
16198 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
16199 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
16200 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
16201 };
16202 
16203 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
16204 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
16205 };
16206 
16207 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
16208 				    struct netdev_queue_stats_rx *stats)
16209 {
16210 	struct bnxt *bp = netdev_priv(dev);
16211 	struct bnxt_cp_ring_info *cpr;
16212 	u64 *sw;
16213 
16214 	if (!bp->bnapi)
16215 		return;
16216 
16217 	bnxt_sync_ring_stats(bp);
16218 	cpr = &bp->bnapi[i]->cp_ring;
16219 	sw = cpr->stats.sw_stats;
16220 
16221 	stats->packets = 0;
16222 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
16223 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
16224 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
16225 
16226 	stats->bytes = 0;
16227 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
16228 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
16229 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
16230 
16231 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
16232 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
16233 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
16234 }
16235 
16236 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
16237 				    struct netdev_queue_stats_tx *stats)
16238 {
16239 	struct bnxt *bp = netdev_priv(dev);
16240 	struct bnxt_napi *bnapi;
16241 	u64 *sw;
16242 
16243 	if (!bp->tx_ring)
16244 		return;
16245 
16246 	bnxt_sync_ring_stats(bp);
16247 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
16248 	sw = bnapi->cp_ring.stats.sw_stats;
16249 
16250 	stats->packets = 0;
16251 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
16252 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
16253 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
16254 
16255 	stats->bytes = 0;
16256 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
16257 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
16258 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
16259 }
16260 
16261 static void bnxt_get_base_stats(struct net_device *dev,
16262 				struct netdev_queue_stats_rx *rx,
16263 				struct netdev_queue_stats_tx *tx)
16264 {
16265 	struct bnxt *bp = netdev_priv(dev);
16266 
16267 	rx->packets = bp->net_stats_prev.rx_packets;
16268 	rx->bytes = bp->net_stats_prev.rx_bytes;
16269 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
16270 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
16271 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
16272 
16273 	tx->packets = bp->net_stats_prev.tx_packets;
16274 	tx->bytes = bp->net_stats_prev.tx_bytes;
16275 }
16276 
16277 static const struct netdev_stat_ops bnxt_stat_ops = {
16278 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
16279 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
16280 	.get_base_stats		= bnxt_get_base_stats,
16281 };
16282 
16283 static void bnxt_queue_default_qcfg(struct net_device *dev,
16284 				    struct netdev_queue_config *qcfg)
16285 {
16286 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
16287 }
16288 
16289 static int bnxt_validate_qcfg(struct net_device *dev,
16290 			      struct netdev_queue_config *qcfg,
16291 			      struct netlink_ext_ack *extack)
16292 {
16293 	struct bnxt *bp = netdev_priv(dev);
16294 
16295 	/* Older chips need MSS calc so rx_page_size is not supported */
16296 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16297 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16298 		return -EINVAL;
16299 
16300 	if (!is_power_of_2(qcfg->rx_page_size))
16301 		return -ERANGE;
16302 
16303 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16304 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16305 		return -ERANGE;
16306 
16307 	return 0;
16308 }
16309 
16310 static int bnxt_queue_mem_alloc(struct net_device *dev,
16311 				struct netdev_queue_config *qcfg,
16312 				void *qmem, int idx)
16313 {
16314 	struct bnxt_rx_ring_info *rxr, *clone;
16315 	struct bnxt *bp = netdev_priv(dev);
16316 	struct bnxt_ring_struct *ring;
16317 	int rc;
16318 
16319 	if (!bp->rx_ring)
16320 		return -ENETDOWN;
16321 
16322 	rxr = &bp->rx_ring[idx];
16323 	clone = qmem;
16324 	memcpy(clone, rxr, sizeof(*rxr));
16325 	bnxt_init_rx_ring_struct(bp, clone);
16326 	bnxt_reset_rx_ring_struct(bp, clone);
16327 
16328 	clone->rx_prod = 0;
16329 	clone->rx_agg_prod = 0;
16330 	clone->rx_sw_agg_prod = 0;
16331 	clone->rx_next_cons = 0;
16332 	clone->need_head_pool = false;
16333 	clone->rx_page_size = qcfg->rx_page_size;
16334 	clone->rx_agg_bmap = NULL;
16335 
16336 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16337 	if (rc)
16338 		return rc;
16339 
16340 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16341 	if (rc < 0)
16342 		goto err_page_pool_destroy;
16343 
16344 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16345 					MEM_TYPE_PAGE_POOL,
16346 					clone->page_pool);
16347 	if (rc)
16348 		goto err_rxq_info_unreg;
16349 
16350 	ring = &clone->rx_ring_struct;
16351 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16352 	if (rc)
16353 		goto err_free_rx_ring;
16354 
16355 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16356 		ring = &clone->rx_agg_ring_struct;
16357 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16358 		if (rc)
16359 			goto err_free_rx_agg_ring;
16360 
16361 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16362 		if (rc)
16363 			goto err_free_rx_agg_ring;
16364 	}
16365 
16366 	if (bp->flags & BNXT_FLAG_TPA) {
16367 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16368 		if (rc)
16369 			goto err_free_tpa_info;
16370 	}
16371 
16372 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16373 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16374 
16375 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16376 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16377 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16378 	if (bp->flags & BNXT_FLAG_TPA)
16379 		bnxt_alloc_one_tpa_info_data(bp, clone);
16380 
16381 	return 0;
16382 
16383 err_free_tpa_info:
16384 	bnxt_free_one_tpa_info(bp, clone);
16385 err_free_rx_agg_ring:
16386 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16387 	kfree(clone->rx_agg_bmap);
16388 	clone->rx_agg_bmap = NULL;
16389 err_free_rx_ring:
16390 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16391 err_rxq_info_unreg:
16392 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16393 err_page_pool_destroy:
16394 	page_pool_destroy(clone->page_pool);
16395 	page_pool_destroy(clone->head_pool);
16396 	clone->page_pool = NULL;
16397 	clone->head_pool = NULL;
16398 	return rc;
16399 }
16400 
16401 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16402 {
16403 	struct bnxt_rx_ring_info *rxr = qmem;
16404 	struct bnxt *bp = netdev_priv(dev);
16405 	struct bnxt_ring_struct *ring;
16406 
16407 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16408 	bnxt_free_one_tpa_info(bp, rxr);
16409 
16410 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16411 
16412 	page_pool_destroy(rxr->page_pool);
16413 	page_pool_destroy(rxr->head_pool);
16414 	rxr->page_pool = NULL;
16415 	rxr->head_pool = NULL;
16416 
16417 	ring = &rxr->rx_ring_struct;
16418 	bnxt_free_ring(bp, &ring->ring_mem);
16419 
16420 	ring = &rxr->rx_agg_ring_struct;
16421 	bnxt_free_ring(bp, &ring->ring_mem);
16422 
16423 	kfree(rxr->rx_agg_bmap);
16424 	rxr->rx_agg_bmap = NULL;
16425 }
16426 
16427 static void bnxt_copy_rx_ring(struct bnxt *bp,
16428 			      struct bnxt_rx_ring_info *dst,
16429 			      struct bnxt_rx_ring_info *src)
16430 {
16431 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16432 	struct bnxt_ring_struct *dst_ring, *src_ring;
16433 	int i;
16434 
16435 	dst_ring = &dst->rx_ring_struct;
16436 	dst_rmem = &dst_ring->ring_mem;
16437 	src_ring = &src->rx_ring_struct;
16438 	src_rmem = &src_ring->ring_mem;
16439 
16440 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16441 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16442 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16443 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16444 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16445 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16446 
16447 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16448 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16449 	*dst_rmem->vmem = *src_rmem->vmem;
16450 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16451 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16452 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16453 	}
16454 
16455 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16456 		return;
16457 
16458 	dst_ring = &dst->rx_agg_ring_struct;
16459 	dst_rmem = &dst_ring->ring_mem;
16460 	src_ring = &src->rx_agg_ring_struct;
16461 	src_rmem = &src_ring->ring_mem;
16462 
16463 	dst->rx_page_size = src->rx_page_size;
16464 
16465 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16466 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16467 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16468 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16469 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16470 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16471 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16472 
16473 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16474 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16475 	*dst_rmem->vmem = *src_rmem->vmem;
16476 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16477 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16478 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16479 	}
16480 
16481 	dst->rx_agg_bmap = src->rx_agg_bmap;
16482 }
16483 
16484 static int bnxt_queue_start(struct net_device *dev,
16485 			    struct netdev_queue_config *qcfg,
16486 			    void *qmem, int idx)
16487 {
16488 	struct bnxt *bp = netdev_priv(dev);
16489 	struct bnxt_rx_ring_info *rxr, *clone;
16490 	struct bnxt_cp_ring_info *cpr;
16491 	struct bnxt_vnic_info *vnic;
16492 	struct bnxt_napi *bnapi;
16493 	int i, rc;
16494 	u16 mru;
16495 
16496 	rxr = &bp->rx_ring[idx];
16497 	clone = qmem;
16498 
16499 	rxr->rx_prod = clone->rx_prod;
16500 	rxr->rx_agg_prod = clone->rx_agg_prod;
16501 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16502 	rxr->rx_next_cons = clone->rx_next_cons;
16503 	rxr->rx_tpa = clone->rx_tpa;
16504 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16505 	rxr->page_pool = clone->page_pool;
16506 	rxr->head_pool = clone->head_pool;
16507 	rxr->xdp_rxq = clone->xdp_rxq;
16508 	rxr->need_head_pool = clone->need_head_pool;
16509 
16510 	bnxt_copy_rx_ring(bp, rxr, clone);
16511 
16512 	bnapi = rxr->bnapi;
16513 	cpr = &bnapi->cp_ring;
16514 
16515 	/* All rings have been reserved and previously allocated.
16516 	 * Reallocating with the same parameters should never fail.
16517 	 */
16518 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16519 	if (rc)
16520 		goto err_reset;
16521 
16522 	if (bp->tph_mode) {
16523 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16524 		if (rc)
16525 			goto err_reset;
16526 	}
16527 
16528 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16529 	if (rc)
16530 		goto err_reset;
16531 
16532 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16533 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16534 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16535 
16536 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16537 		rc = bnxt_tx_queue_start(bp, idx);
16538 		if (rc)
16539 			goto err_reset;
16540 	}
16541 
16542 	bnxt_enable_rx_page_pool(rxr);
16543 	napi_enable_locked(&bnapi->napi);
16544 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16545 
16546 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16547 	for (i = 0; i < bp->nr_vnics; i++) {
16548 		vnic = &bp->vnic_info[i];
16549 
16550 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16551 		if (rc)
16552 			return rc;
16553 	}
16554 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16555 
16556 err_reset:
16557 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16558 		   rc);
16559 	napi_enable_locked(&bnapi->napi);
16560 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16561 	netif_close(dev);
16562 	return rc;
16563 }
16564 
16565 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16566 {
16567 	struct bnxt *bp = netdev_priv(dev);
16568 	struct bnxt_rx_ring_info *rxr;
16569 	struct bnxt_cp_ring_info *cpr;
16570 	struct bnxt_vnic_info *vnic;
16571 	struct bnxt_napi *bnapi;
16572 	int i;
16573 
16574 	for (i = 0; i < bp->nr_vnics; i++) {
16575 		vnic = &bp->vnic_info[i];
16576 
16577 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16578 	}
16579 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16580 	/* Make sure NAPI sees that the VNIC is disabled */
16581 	synchronize_net();
16582 	rxr = &bp->rx_ring[idx];
16583 	bnapi = rxr->bnapi;
16584 	cpr = &bnapi->cp_ring;
16585 	cancel_work_sync(&cpr->dim.work);
16586 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16587 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16588 	page_pool_disable_direct_recycling(rxr->page_pool);
16589 	if (bnxt_separate_head_pool(rxr))
16590 		page_pool_disable_direct_recycling(rxr->head_pool);
16591 
16592 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16593 		bnxt_tx_queue_stop(bp, idx);
16594 
16595 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16596 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16597 	 * after seeing the completion.
16598 	 */
16599 	napi_disable_locked(&bnapi->napi);
16600 
16601 	if (bp->tph_mode) {
16602 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16603 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16604 	}
16605 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16606 
16607 	memcpy(qmem, rxr, sizeof(*rxr));
16608 	bnxt_init_rx_ring_struct(bp, qmem);
16609 
16610 	return 0;
16611 }
16612 
16613 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16614 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16615 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16616 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16617 	.ndo_queue_start	= bnxt_queue_start,
16618 	.ndo_queue_stop		= bnxt_queue_stop,
16619 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16620 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16621 	.supported_params	= QCFG_RX_PAGE_SIZE,
16622 };
16623 
16624 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16625 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16626 };
16627 
16628 static void bnxt_remove_one(struct pci_dev *pdev)
16629 {
16630 	struct net_device *dev = pci_get_drvdata(pdev);
16631 	struct bnxt *bp = netdev_priv(dev);
16632 
16633 	if (BNXT_PF(bp))
16634 		__bnxt_sriov_disable(bp);
16635 
16636 	bnxt_aux_devices_del(bp);
16637 
16638 	unregister_netdev(dev);
16639 	bnxt_ptp_clear(bp);
16640 
16641 	bnxt_aux_devices_uninit(bp);
16642 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
16643 
16644 	bnxt_free_l2_filters(bp, true);
16645 	bnxt_free_ntp_fltrs(bp, true);
16646 	WARN_ON(bp->num_rss_ctx);
16647 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16648 	/* Flush any pending tasks */
16649 	cancel_work_sync(&bp->sp_task);
16650 	cancel_delayed_work_sync(&bp->fw_reset_task);
16651 	bp->sp_event = 0;
16652 
16653 	bnxt_dl_fw_reporters_destroy(bp);
16654 	bnxt_dl_unregister(bp);
16655 	bnxt_shutdown_tc(bp);
16656 
16657 	bnxt_clear_int_mode(bp);
16658 	bnxt_free_ring_cpu_masks(bp);
16659 	bnxt_hwrm_func_drv_unrgtr(bp);
16660 	bnxt_free_hwrm_resources(bp);
16661 	bnxt_hwmon_uninit(bp);
16662 	bnxt_ethtool_free(bp);
16663 	bnxt_dcb_free(bp);
16664 	kfree(bp->ptp_cfg);
16665 	bp->ptp_cfg = NULL;
16666 	kfree(bp->fw_health);
16667 	bp->fw_health = NULL;
16668 	bnxt_cleanup_pci(bp);
16669 	bnxt_free_ctx_mem(bp, true);
16670 	bnxt_free_crash_dump_mem(bp);
16671 	kfree(bp->rss_indir_tbl);
16672 	bp->rss_indir_tbl = NULL;
16673 	bnxt_free_port_stats(bp);
16674 	free_netdev(dev);
16675 }
16676 
16677 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16678 {
16679 	int rc = 0;
16680 	struct bnxt_link_info *link_info = &bp->link_info;
16681 
16682 	bp->phy_flags = 0;
16683 	rc = bnxt_hwrm_phy_qcaps(bp);
16684 	if (rc) {
16685 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16686 			   rc);
16687 		return rc;
16688 	}
16689 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16690 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16691 	else
16692 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16693 
16694 	bp->mac_flags = 0;
16695 	bnxt_hwrm_mac_qcaps(bp);
16696 
16697 	if (!fw_dflt)
16698 		return 0;
16699 
16700 	mutex_lock(&bp->link_lock);
16701 	rc = bnxt_update_link(bp, false);
16702 	if (rc) {
16703 		mutex_unlock(&bp->link_lock);
16704 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16705 			   rc);
16706 		return rc;
16707 	}
16708 
16709 	/* Older firmware does not have supported_auto_speeds, so assume
16710 	 * that all supported speeds can be autonegotiated.
16711 	 */
16712 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16713 		link_info->support_auto_speeds = link_info->support_speeds;
16714 
16715 	bnxt_init_ethtool_link_settings(bp);
16716 	mutex_unlock(&bp->link_lock);
16717 	return 0;
16718 }
16719 
16720 static int bnxt_get_max_irq(struct pci_dev *pdev)
16721 {
16722 	u16 ctrl;
16723 
16724 	if (!pdev->msix_cap)
16725 		return 1;
16726 
16727 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16728 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16729 }
16730 
16731 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16732 				int *max_cp)
16733 {
16734 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16735 	int max_ring_grps = 0, max_irq;
16736 
16737 	*max_tx = hw_resc->max_tx_rings;
16738 	*max_rx = hw_resc->max_rx_rings;
16739 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16740 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16741 			bnxt_get_ulp_msix_num_in_use(bp),
16742 			hw_resc->max_stat_ctxs -
16743 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16744 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16745 		*max_cp = min_t(int, *max_cp, max_irq);
16746 	max_ring_grps = hw_resc->max_hw_ring_grps;
16747 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16748 		*max_cp -= 1;
16749 		*max_rx -= 2;
16750 	}
16751 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16752 		*max_rx >>= 1;
16753 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16754 		int rc;
16755 
16756 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16757 		if (rc) {
16758 			*max_rx = 0;
16759 			*max_tx = 0;
16760 		}
16761 		/* On P5 chips, max_cp output param should be available NQs */
16762 		*max_cp = max_irq;
16763 	}
16764 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16765 }
16766 
16767 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16768 {
16769 	int rx, tx, cp;
16770 
16771 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16772 	*max_rx = rx;
16773 	*max_tx = tx;
16774 	if (!rx || !tx || !cp)
16775 		return -ENOMEM;
16776 
16777 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16778 }
16779 
16780 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16781 			       bool shared)
16782 {
16783 	int rc;
16784 
16785 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16786 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16787 		/* Not enough rings, try disabling agg rings. */
16788 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16789 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16790 		if (rc) {
16791 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16792 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16793 			return rc;
16794 		}
16795 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16796 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16797 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16798 		bnxt_set_ring_params(bp);
16799 	}
16800 
16801 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16802 		int max_cp, max_stat, max_irq;
16803 
16804 		/* Reserve minimum resources for RoCE */
16805 		max_cp = bnxt_get_max_func_cp_rings(bp);
16806 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16807 		max_irq = bnxt_get_max_func_irqs(bp);
16808 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16809 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16810 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16811 			return 0;
16812 
16813 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16814 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16815 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16816 		max_cp = min_t(int, max_cp, max_irq);
16817 		max_cp = min_t(int, max_cp, max_stat);
16818 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16819 		if (rc)
16820 			rc = 0;
16821 	}
16822 	return rc;
16823 }
16824 
16825 /* In initial default shared ring setting, each shared ring must have a
16826  * RX/TX ring pair.
16827  */
16828 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16829 {
16830 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16831 	bp->rx_nr_rings = bp->cp_nr_rings;
16832 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16833 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16834 }
16835 
16836 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
16837 {
16838 	if (sh)
16839 		bnxt_trim_dflt_sh_rings(bp);
16840 	else
16841 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16842 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16843 	if (sh && READ_ONCE(bp->xdp_prog)) {
16844 		bnxt_set_xdp_tx_rings(bp);
16845 		bnxt_set_cp_rings(bp, true);
16846 	}
16847 }
16848 
16849 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16850 {
16851 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16852 	int avail_msix;
16853 
16854 	if (!bnxt_can_reserve_rings(bp))
16855 		return 0;
16856 
16857 	if (sh)
16858 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16859 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16860 	/* Reduce default rings on multi-port cards so that total default
16861 	 * rings do not exceed CPU count.
16862 	 */
16863 	if (bp->port_count > 1) {
16864 		int max_rings =
16865 			max_t(int, num_online_cpus() / bp->port_count, 1);
16866 
16867 		dflt_rings = min_t(int, dflt_rings, max_rings);
16868 	}
16869 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16870 	if (rc)
16871 		return rc;
16872 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16873 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16874 
16875 	bnxt_adj_dflt_rings(bp, sh);
16876 
16877 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16878 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16879 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16880 
16881 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16882 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16883 	}
16884 
16885 	rc = __bnxt_reserve_rings(bp);
16886 	if (rc && rc != -ENODEV)
16887 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16888 
16889 	bnxt_adj_tx_rings(bp);
16890 	if (sh)
16891 		bnxt_adj_dflt_rings(bp, true);
16892 
16893 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16894 	if (bnxt_need_reserve_rings(bp)) {
16895 		rc = __bnxt_reserve_rings(bp);
16896 		if (rc && rc != -ENODEV)
16897 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16898 		bnxt_adj_tx_rings(bp);
16899 	}
16900 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16901 		bp->rx_nr_rings++;
16902 		bp->cp_nr_rings++;
16903 	}
16904 	if (rc) {
16905 		bp->tx_nr_rings = 0;
16906 		bp->rx_nr_rings = 0;
16907 	}
16908 	return rc;
16909 }
16910 
16911 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16912 {
16913 	int rc;
16914 
16915 	if (bp->tx_nr_rings)
16916 		return 0;
16917 
16918 	bnxt_ulp_irq_stop(bp);
16919 	bnxt_clear_int_mode(bp);
16920 	rc = bnxt_set_dflt_rings(bp, true);
16921 	if (rc) {
16922 		if (BNXT_VF(bp) && rc == -ENODEV)
16923 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16924 		else
16925 			netdev_err(bp->dev, "Not enough rings available.\n");
16926 		goto init_dflt_ring_err;
16927 	}
16928 	rc = bnxt_init_int_mode(bp);
16929 	if (rc)
16930 		goto init_dflt_ring_err;
16931 
16932 	bnxt_adj_tx_rings(bp);
16933 
16934 	bnxt_set_dflt_rfs(bp);
16935 
16936 init_dflt_ring_err:
16937 	bnxt_ulp_irq_restart(bp, rc);
16938 	return rc;
16939 }
16940 
16941 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16942 {
16943 	int rc;
16944 
16945 	netdev_assert_locked_ops_compat(bp->dev);
16946 	bnxt_hwrm_func_qcaps(bp);
16947 
16948 	if (netif_running(bp->dev))
16949 		__bnxt_close_nic(bp, true, false);
16950 
16951 	bnxt_ulp_irq_stop(bp);
16952 	bnxt_clear_int_mode(bp);
16953 	rc = bnxt_init_int_mode(bp);
16954 	bnxt_ulp_irq_restart(bp, rc);
16955 
16956 	if (netif_running(bp->dev)) {
16957 		if (rc)
16958 			netif_close(bp->dev);
16959 		else
16960 			rc = bnxt_open_nic(bp, true, false);
16961 	}
16962 
16963 	return rc;
16964 }
16965 
16966 static int bnxt_init_mac_addr(struct bnxt *bp)
16967 {
16968 	int rc = 0;
16969 
16970 	if (BNXT_PF(bp)) {
16971 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16972 	} else {
16973 #ifdef CONFIG_BNXT_SRIOV
16974 		struct bnxt_vf_info *vf = &bp->vf;
16975 		bool strict_approval = true;
16976 
16977 		if (is_valid_ether_addr(vf->mac_addr)) {
16978 			/* overwrite netdev dev_addr with admin VF MAC */
16979 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16980 			/* Older PF driver or firmware may not approve this
16981 			 * correctly.
16982 			 */
16983 			strict_approval = false;
16984 		} else {
16985 			eth_hw_addr_random(bp->dev);
16986 		}
16987 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16988 #endif
16989 	}
16990 	return rc;
16991 }
16992 
16993 static void bnxt_vpd_read_info(struct bnxt *bp)
16994 {
16995 	struct pci_dev *pdev = bp->pdev;
16996 	unsigned int vpd_size, kw_len;
16997 	int pos, size;
16998 	u8 *vpd_data;
16999 
17000 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
17001 	if (IS_ERR(vpd_data)) {
17002 		pci_warn(pdev, "Unable to read VPD\n");
17003 		return;
17004 	}
17005 
17006 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17007 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
17008 	if (pos < 0)
17009 		goto read_sn;
17010 
17011 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17012 	memcpy(bp->board_partno, &vpd_data[pos], size);
17013 
17014 read_sn:
17015 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17016 					   PCI_VPD_RO_KEYWORD_SERIALNO,
17017 					   &kw_len);
17018 	if (pos < 0)
17019 		goto exit;
17020 
17021 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17022 	memcpy(bp->board_serialno, &vpd_data[pos], size);
17023 exit:
17024 	kfree(vpd_data);
17025 }
17026 
17027 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
17028 {
17029 	struct pci_dev *pdev = bp->pdev;
17030 	u64 qword;
17031 
17032 	qword = pci_get_dsn(pdev);
17033 	if (!qword) {
17034 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
17035 		return -EOPNOTSUPP;
17036 	}
17037 
17038 	put_unaligned_le64(qword, dsn);
17039 
17040 	bp->flags |= BNXT_FLAG_DSN_VALID;
17041 	return 0;
17042 }
17043 
17044 static int bnxt_map_db_bar(struct bnxt *bp)
17045 {
17046 	if (!bp->db_size)
17047 		return -ENODEV;
17048 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
17049 	if (!bp->bar1)
17050 		return -ENOMEM;
17051 	return 0;
17052 }
17053 
17054 void bnxt_print_device_info(struct bnxt *bp)
17055 {
17056 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
17057 		    board_info[bp->board_idx].name,
17058 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
17059 
17060 	pcie_print_link_status(bp->pdev);
17061 }
17062 
17063 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
17064 {
17065 	struct bnxt_hw_resc *hw_resc;
17066 	struct net_device *dev;
17067 	struct bnxt *bp;
17068 	int rc, max_irqs;
17069 
17070 	if (pci_is_bridge(pdev))
17071 		return -ENODEV;
17072 
17073 	if (!pdev->msix_cap) {
17074 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
17075 		return -ENODEV;
17076 	}
17077 
17078 	/* Clear any pending DMA transactions from crash kernel
17079 	 * while loading driver in capture kernel.
17080 	 */
17081 	if (is_kdump_kernel()) {
17082 		pci_clear_master(pdev);
17083 		pcie_flr(pdev);
17084 	}
17085 
17086 	max_irqs = bnxt_get_max_irq(pdev);
17087 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
17088 				 max_irqs);
17089 	if (!dev)
17090 		return -ENOMEM;
17091 
17092 	bp = netdev_priv(dev);
17093 	bp->board_idx = ent->driver_data;
17094 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
17095 	bnxt_set_max_func_irqs(bp, max_irqs);
17096 
17097 	bp->max_irqs = max_irqs;
17098 	rc = bnxt_alloc_ring_cpu_masks(bp);
17099 	if (rc)
17100 		goto init_err_free;
17101 
17102 	if (bnxt_vf_pciid(bp->board_idx))
17103 		bp->flags |= BNXT_FLAG_VF;
17104 
17105 	/* No devlink port registration in case of a VF */
17106 	if (BNXT_PF(bp))
17107 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
17108 
17109 	rc = bnxt_init_board(pdev, dev);
17110 	if (rc < 0)
17111 		goto init_err_free;
17112 
17113 	dev->netdev_ops = &bnxt_netdev_ops;
17114 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
17115 	dev->stat_ops = &bnxt_stat_ops;
17116 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
17117 	dev->ethtool_ops = &bnxt_ethtool_ops;
17118 	pci_set_drvdata(pdev, dev);
17119 
17120 	rc = bnxt_alloc_hwrm_resources(bp);
17121 	if (rc)
17122 		goto init_err_pci_clean;
17123 
17124 	mutex_init(&bp->hwrm_cmd_lock);
17125 	mutex_init(&bp->link_lock);
17126 
17127 	rc = bnxt_fw_init_one_p1(bp);
17128 	if (rc)
17129 		goto init_err_pci_clean;
17130 
17131 	if (BNXT_PF(bp))
17132 		bnxt_vpd_read_info(bp);
17133 
17134 	if (BNXT_CHIP_P5_PLUS(bp)) {
17135 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
17136 		if (BNXT_CHIP_P7(bp))
17137 			bp->flags |= BNXT_FLAG_CHIP_P7;
17138 	}
17139 
17140 	rc = bnxt_alloc_rss_indir_tbl(bp);
17141 	if (rc)
17142 		goto init_err_pci_clean;
17143 
17144 	rc = bnxt_fw_init_one_p2(bp);
17145 	if (rc)
17146 		goto init_err_pci_clean;
17147 
17148 	rc = bnxt_map_db_bar(bp);
17149 	if (rc) {
17150 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
17151 			rc);
17152 		goto init_err_pci_clean;
17153 	}
17154 
17155 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17156 			   NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17157 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17158 			   NETIF_F_GSO_IPXIP4 |
17159 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17160 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
17161 			   NETIF_F_RXCSUM | NETIF_F_GRO;
17162 	dev->hw_features |= NETIF_F_GSO_UDP_L4;
17163 
17164 	if (BNXT_SUPPORTS_TPA(bp))
17165 		dev->hw_features |= NETIF_F_LRO;
17166 
17167 	dev->hw_enc_features =
17168 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17169 			NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17170 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17171 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17172 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
17173 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
17174 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
17175 	if (bp->flags & BNXT_FLAG_CHIP_P7)
17176 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
17177 	else
17178 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
17179 
17180 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
17181 				    NETIF_F_GSO_GRE_CSUM;
17182 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
17183 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
17184 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
17185 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
17186 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
17187 	if (BNXT_SUPPORTS_TPA(bp))
17188 		dev->hw_features |= NETIF_F_GRO_HW;
17189 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
17190 	if (dev->features & NETIF_F_GRO_HW)
17191 		dev->features &= ~NETIF_F_LRO;
17192 	dev->priv_flags |= IFF_UNICAST_FLT;
17193 
17194 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
17195 	if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
17196 		u16 max_segs = BNXT_SW_USO_MAX_SEGS;
17197 
17198 		if (bp->tso_max_segs)
17199 			max_segs = min_t(u16, max_segs, bp->tso_max_segs);
17200 		netif_set_tso_max_segs(dev, max_segs);
17201 	} else if (bp->tso_max_segs) {
17202 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
17203 	}
17204 
17205 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
17206 			    NETDEV_XDP_ACT_RX_SG;
17207 
17208 #ifdef CONFIG_BNXT_SRIOV
17209 	init_waitqueue_head(&bp->sriov_cfg_wait);
17210 #endif
17211 	if (BNXT_SUPPORTS_TPA(bp)) {
17212 		bp->gro_func = bnxt_gro_func_5730x;
17213 		if (BNXT_CHIP_P4(bp))
17214 			bp->gro_func = bnxt_gro_func_5731x;
17215 		else if (BNXT_CHIP_P5_PLUS(bp))
17216 			bp->gro_func = bnxt_gro_func_5750x;
17217 	}
17218 	if (!BNXT_CHIP_P4_PLUS(bp))
17219 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
17220 
17221 	rc = bnxt_init_mac_addr(bp);
17222 	if (rc) {
17223 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
17224 		rc = -EADDRNOTAVAIL;
17225 		goto init_err_pci_clean;
17226 	}
17227 
17228 	if (BNXT_PF(bp)) {
17229 		/* Read the adapter's DSN to use as the eswitch switch_id */
17230 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
17231 	}
17232 
17233 	/* MTU range: 60 - FW defined max */
17234 	dev->min_mtu = ETH_ZLEN;
17235 	dev->max_mtu = bp->max_mtu;
17236 
17237 	rc = bnxt_probe_phy(bp, true);
17238 	if (rc)
17239 		goto init_err_pci_clean;
17240 
17241 	hw_resc = &bp->hw_resc;
17242 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
17243 		       BNXT_L2_FLTR_MAX_FLTR;
17244 	/* Older firmware may not report these filters properly */
17245 	if (bp->max_fltr < BNXT_MAX_FLTR)
17246 		bp->max_fltr = BNXT_MAX_FLTR;
17247 	bnxt_init_l2_fltr_tbl(bp);
17248 	__bnxt_set_rx_skb_mode(bp, false);
17249 	bnxt_set_tpa_flags(bp);
17250 	bnxt_init_ring_params(bp);
17251 	bnxt_set_ring_params(bp);
17252 	mutex_init(&bp->auxdev_lock);
17253 	if (!bnxt_auxdev_id_alloc(bp))
17254 		bnxt_aux_devices_init(bp);
17255 	rc = bnxt_set_dflt_rings(bp, true);
17256 	if (rc) {
17257 		if (BNXT_VF(bp) && rc == -ENODEV) {
17258 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17259 		} else {
17260 			netdev_err(bp->dev, "Not enough rings available.\n");
17261 			rc = -ENOMEM;
17262 		}
17263 		goto init_err_pci_clean;
17264 	}
17265 
17266 	bnxt_fw_init_one_p3(bp);
17267 
17268 	bnxt_init_dflt_coal(bp);
17269 
17270 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
17271 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
17272 
17273 	rc = bnxt_init_int_mode(bp);
17274 	if (rc)
17275 		goto init_err_pci_clean;
17276 
17277 	/* No TC has been set yet and rings may have been trimmed due to
17278 	 * limited MSIX, so we re-initialize the TX rings per TC.
17279 	 */
17280 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
17281 
17282 	if (BNXT_PF(bp)) {
17283 		if (!bnxt_pf_wq) {
17284 			bnxt_pf_wq =
17285 				create_singlethread_workqueue("bnxt_pf_wq");
17286 			if (!bnxt_pf_wq) {
17287 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
17288 				rc = -ENOMEM;
17289 				goto init_err_pci_clean;
17290 			}
17291 		}
17292 		rc = bnxt_init_tc(bp);
17293 		if (rc)
17294 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
17295 				   rc);
17296 	}
17297 
17298 	bnxt_inv_fw_health_reg(bp);
17299 	rc = bnxt_dl_register(bp);
17300 	if (rc)
17301 		goto init_err_dl;
17302 
17303 	INIT_LIST_HEAD(&bp->usr_fltr_list);
17304 
17305 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
17306 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
17307 
17308 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17309 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17310 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17311 	dev->netmem_tx = NETMEM_TX_DMA;
17312 
17313 	rc = register_netdev(dev);
17314 	if (rc)
17315 		goto init_err_cleanup;
17316 
17317 	bnxt_dl_fw_reporters_create(bp);
17318 
17319 	bnxt_aux_devices_add(bp);
17320 
17321 	bnxt_print_device_info(bp);
17322 
17323 	pci_save_state(pdev);
17324 
17325 	return 0;
17326 init_err_cleanup:
17327 	bnxt_aux_devices_uninit(bp);
17328 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
17329 	bnxt_dl_unregister(bp);
17330 init_err_dl:
17331 	bnxt_shutdown_tc(bp);
17332 	bnxt_clear_int_mode(bp);
17333 
17334 init_err_pci_clean:
17335 	bnxt_hwrm_func_drv_unrgtr(bp);
17336 	bnxt_ptp_clear(bp);
17337 	kfree(bp->ptp_cfg);
17338 	bp->ptp_cfg = NULL;
17339 	bnxt_free_hwrm_resources(bp);
17340 	bnxt_hwmon_uninit(bp);
17341 	bnxt_ethtool_free(bp);
17342 	kfree(bp->fw_health);
17343 	bp->fw_health = NULL;
17344 	bnxt_cleanup_pci(bp);
17345 	bnxt_free_ctx_mem(bp, true);
17346 	bnxt_free_crash_dump_mem(bp);
17347 	kfree(bp->rss_indir_tbl);
17348 	bp->rss_indir_tbl = NULL;
17349 
17350 init_err_free:
17351 	bnxt_free_ring_cpu_masks(bp);
17352 	free_netdev(dev);
17353 	return rc;
17354 }
17355 
17356 static void bnxt_shutdown(struct pci_dev *pdev)
17357 {
17358 	struct net_device *dev = pci_get_drvdata(pdev);
17359 	struct bnxt *bp;
17360 
17361 	if (!dev)
17362 		return;
17363 
17364 	rtnl_lock();
17365 	netdev_lock(dev);
17366 	bp = netdev_priv(dev);
17367 	if (!bp)
17368 		goto shutdown_exit;
17369 
17370 	if (netif_running(dev))
17371 		netif_close(dev);
17372 
17373 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17374 		pcie_flr(pdev);
17375 		goto shutdown_exit;
17376 	}
17377 	bnxt_ptp_clear(bp);
17378 	bnxt_clear_int_mode(bp);
17379 	pci_disable_device(pdev);
17380 
17381 	if (system_state == SYSTEM_POWER_OFF) {
17382 		pci_wake_from_d3(pdev, bp->wol);
17383 		pci_set_power_state(pdev, PCI_D3hot);
17384 	}
17385 
17386 shutdown_exit:
17387 	netdev_unlock(dev);
17388 	rtnl_unlock();
17389 }
17390 
17391 #ifdef CONFIG_PM_SLEEP
17392 static int bnxt_suspend(struct device *device)
17393 {
17394 	struct net_device *dev = dev_get_drvdata(device);
17395 	struct bnxt *bp = netdev_priv(dev);
17396 	int rc = 0;
17397 
17398 	bnxt_ulp_stop(bp);
17399 
17400 	netdev_lock(dev);
17401 	if (netif_running(dev)) {
17402 		netif_device_detach(dev);
17403 		rc = bnxt_close(dev);
17404 	}
17405 	bnxt_hwrm_func_drv_unrgtr(bp);
17406 	bnxt_ptp_clear(bp);
17407 	pci_disable_device(bp->pdev);
17408 	bnxt_free_ctx_mem(bp, false);
17409 	netdev_unlock(dev);
17410 	return rc;
17411 }
17412 
17413 static int bnxt_resume(struct device *device)
17414 {
17415 	struct net_device *dev = dev_get_drvdata(device);
17416 	struct bnxt *bp = netdev_priv(dev);
17417 	int rc = 0;
17418 
17419 	rtnl_lock();
17420 	netdev_lock(dev);
17421 	rc = pci_enable_device(bp->pdev);
17422 	if (rc) {
17423 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17424 			   rc);
17425 		goto resume_exit;
17426 	}
17427 	pci_set_master(bp->pdev);
17428 	if (bnxt_hwrm_ver_get(bp)) {
17429 		rc = -ENODEV;
17430 		goto resume_exit;
17431 	}
17432 	rc = bnxt_hwrm_func_reset(bp);
17433 	if (rc) {
17434 		rc = -EBUSY;
17435 		goto resume_exit;
17436 	}
17437 
17438 	rc = bnxt_hwrm_func_qcaps(bp);
17439 	if (rc)
17440 		goto resume_exit;
17441 
17442 	bnxt_clear_reservations(bp, true);
17443 
17444 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17445 		rc = -ENODEV;
17446 		goto resume_exit;
17447 	}
17448 	if (bp->fw_crash_mem)
17449 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17450 
17451 	if (bnxt_ptp_init(bp)) {
17452 		kfree(bp->ptp_cfg);
17453 		bp->ptp_cfg = NULL;
17454 	}
17455 	bnxt_get_wol_settings(bp);
17456 	if (netif_running(dev)) {
17457 		rc = bnxt_open(dev);
17458 		if (!rc)
17459 			netif_device_attach(dev);
17460 	}
17461 
17462 resume_exit:
17463 	netdev_unlock(bp->dev);
17464 	rtnl_unlock();
17465 	if (!rc) {
17466 		bnxt_ulp_start(bp);
17467 		bnxt_reenable_sriov(bp);
17468 	}
17469 	return rc;
17470 }
17471 
17472 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17473 #define BNXT_PM_OPS (&bnxt_pm_ops)
17474 
17475 #else
17476 
17477 #define BNXT_PM_OPS NULL
17478 
17479 #endif /* CONFIG_PM_SLEEP */
17480 
17481 /**
17482  * bnxt_io_error_detected - called when PCI error is detected
17483  * @pdev: Pointer to PCI device
17484  * @state: The current pci connection state
17485  *
17486  * This function is called after a PCI bus error affecting
17487  * this device has been detected.
17488  */
17489 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17490 					       pci_channel_state_t state)
17491 {
17492 	struct net_device *netdev = pci_get_drvdata(pdev);
17493 	struct bnxt *bp = netdev_priv(netdev);
17494 	bool abort = false;
17495 
17496 	netdev_info(netdev, "PCI I/O error detected\n");
17497 
17498 	bnxt_ulp_stop(bp);
17499 
17500 	netdev_lock(netdev);
17501 	netif_device_detach(netdev);
17502 
17503 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17504 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17505 		abort = true;
17506 	}
17507 
17508 	if (abort || state == pci_channel_io_perm_failure) {
17509 		netdev_unlock(netdev);
17510 		return PCI_ERS_RESULT_DISCONNECT;
17511 	}
17512 
17513 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17514 	 * so we disable bus master to prevent any potential bad DMAs before
17515 	 * freeing kernel memory.
17516 	 */
17517 	if (state == pci_channel_io_frozen) {
17518 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
17519 		bnxt_fw_fatal_close(bp);
17520 	}
17521 
17522 	if (netif_running(netdev))
17523 		__bnxt_close_nic(bp, true, true);
17524 
17525 	if (pci_is_enabled(pdev))
17526 		pci_disable_device(pdev);
17527 	bnxt_free_ctx_mem(bp, false);
17528 	netdev_unlock(netdev);
17529 
17530 	/* Request a slot reset. */
17531 	return PCI_ERS_RESULT_NEED_RESET;
17532 }
17533 
17534 /**
17535  * bnxt_io_slot_reset - called after the pci bus has been reset.
17536  * @pdev: Pointer to PCI device
17537  *
17538  * Restart the card from scratch, as if from a cold-boot.
17539  * At this point, the card has experienced a hard reset,
17540  * followed by fixups by BIOS, and has its config space
17541  * set up identically to what it was at cold boot.
17542  */
17543 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17544 {
17545 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17546 	struct net_device *netdev = pci_get_drvdata(pdev);
17547 	struct bnxt *bp = netdev_priv(netdev);
17548 	int retry = 0;
17549 	int err = 0;
17550 	int off;
17551 
17552 	netdev_info(bp->dev, "PCI Slot Reset\n");
17553 
17554 	if (test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state)) {
17555 		/* After DPC, the chip should return CRS when the vendor ID
17556 		 * config register is read until it is ready.  On all chips,
17557 		 * this is not happening reliably so add a 5-second delay as a
17558 		 * workaround.
17559 		 */
17560 		msleep(5000);
17561 	}
17562 
17563 	netdev_lock(netdev);
17564 
17565 	if (pci_enable_device(pdev)) {
17566 		dev_err(&pdev->dev,
17567 			"Cannot re-enable PCI device after reset.\n");
17568 	} else {
17569 		pci_set_master(pdev);
17570 		/* Upon fatal error, our device internal logic that latches to
17571 		 * BAR value is getting reset and will restore only upon
17572 		 * rewriting the BARs.
17573 		 *
17574 		 * As pci_restore_state() does not re-write the BARs if the
17575 		 * value is same as saved value earlier, driver needs to
17576 		 * write the BARs to 0 to force restore, in case of fatal error.
17577 		 */
17578 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17579 				       &bp->state)) {
17580 			for (off = PCI_BASE_ADDRESS_0;
17581 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17582 				pci_write_config_dword(bp->pdev, off, 0);
17583 		}
17584 		pci_restore_state(pdev);
17585 
17586 		bnxt_inv_fw_health_reg(bp);
17587 		bnxt_try_map_fw_health_reg(bp);
17588 
17589 		/* In some PCIe AER scenarios, firmware may take up to
17590 		 * 10 seconds to become ready in the worst case.
17591 		 */
17592 		do {
17593 			err = bnxt_try_recover_fw(bp);
17594 			if (!err)
17595 				break;
17596 			retry++;
17597 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17598 
17599 		if (err) {
17600 			dev_err(&pdev->dev, "Firmware not ready\n");
17601 			goto reset_exit;
17602 		}
17603 
17604 		err = bnxt_hwrm_func_reset(bp);
17605 		if (!err)
17606 			result = PCI_ERS_RESULT_RECOVERED;
17607 
17608 		/* IRQ will be initialized later in bnxt_io_resume */
17609 		bnxt_ulp_irq_stop(bp);
17610 		bnxt_clear_int_mode(bp);
17611 	}
17612 
17613 reset_exit:
17614 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17615 	bnxt_clear_reservations(bp, true);
17616 	netdev_unlock(netdev);
17617 
17618 	return result;
17619 }
17620 
17621 /**
17622  * bnxt_io_resume - called when traffic can start flowing again.
17623  * @pdev: Pointer to PCI device
17624  *
17625  * This callback is called when the error recovery driver tells
17626  * us that its OK to resume normal operation.
17627  */
17628 static void bnxt_io_resume(struct pci_dev *pdev)
17629 {
17630 	struct net_device *netdev = pci_get_drvdata(pdev);
17631 	struct bnxt *bp = netdev_priv(netdev);
17632 	int err;
17633 
17634 	netdev_info(bp->dev, "PCI Slot Resume\n");
17635 	rtnl_lock();
17636 	netdev_lock(netdev);
17637 
17638 	err = bnxt_hwrm_func_qcaps(bp);
17639 	if (!err) {
17640 		if (netif_running(netdev)) {
17641 			err = bnxt_open(netdev);
17642 		} else {
17643 			err = bnxt_reserve_rings(bp, true);
17644 			if (!err)
17645 				err = bnxt_init_int_mode(bp);
17646 		}
17647 	}
17648 
17649 	if (!err)
17650 		netif_device_attach(netdev);
17651 
17652 	netdev_unlock(netdev);
17653 	rtnl_unlock();
17654 	if (!err) {
17655 		bnxt_ulp_start(bp);
17656 		bnxt_reenable_sriov(bp);
17657 	}
17658 }
17659 
17660 static const struct pci_error_handlers bnxt_err_handler = {
17661 	.error_detected	= bnxt_io_error_detected,
17662 	.slot_reset	= bnxt_io_slot_reset,
17663 	.resume		= bnxt_io_resume
17664 };
17665 
17666 static struct pci_driver bnxt_pci_driver = {
17667 	.name		= DRV_MODULE_NAME,
17668 	.id_table	= bnxt_pci_tbl,
17669 	.probe		= bnxt_init_one,
17670 	.remove		= bnxt_remove_one,
17671 	.shutdown	= bnxt_shutdown,
17672 	.driver.pm	= BNXT_PM_OPS,
17673 	.err_handler	= &bnxt_err_handler,
17674 #if defined(CONFIG_BNXT_SRIOV)
17675 	.sriov_configure = bnxt_sriov_configure,
17676 #endif
17677 };
17678 
17679 static int __init bnxt_init(void)
17680 {
17681 	int err;
17682 
17683 	bnxt_debug_init();
17684 	err = pci_register_driver(&bnxt_pci_driver);
17685 	if (err) {
17686 		bnxt_debug_exit();
17687 		return err;
17688 	}
17689 
17690 	return 0;
17691 }
17692 
17693 static void __exit bnxt_exit(void)
17694 {
17695 	pci_unregister_driver(&bnxt_pci_driver);
17696 	if (bnxt_pf_wq)
17697 		destroy_workqueue(bnxt_pf_wq);
17698 	bnxt_debug_exit();
17699 }
17700 
17701 module_init(bnxt_init);
17702 module_exit(bnxt_exit);
17703