xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision a51d233ccd4889aadb71dace863525cc2b00271e)
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 	/* If the most recent BD has its completion suppressed, unset the bit
466 	 * so that a completion is generated, otherwise nothing is left to
467 	 * clean the ring and wake the queue.
468 	 */
469 	if (txr->kick_txbd0) {
470 		txr->kick_txbd0->tx_bd_len_flags_type &=
471 			cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
472 		txr->kick_txbd0 = NULL;
473 	}
474 
475 	/* Sync BD data before updating doorbell */
476 	wmb();
477 	bnxt_db_write(bp, &txr->tx_db, prod);
478 	txr->kick_pending = 0;
479 }
480 
481 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
482 {
483 	struct bnxt *bp = netdev_priv(dev);
484 	struct tx_bd *txbd, *txbd0;
485 	struct tx_bd_ext *txbd1;
486 	struct netdev_queue *txq;
487 	int i;
488 	dma_addr_t mapping;
489 	unsigned int length;
490 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
491 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
492 	struct pci_dev *pdev = bp->pdev;
493 	u16 prod, last_frag, txts_prod;
494 	struct bnxt_tx_ring_info *txr;
495 	struct bnxt_sw_tx_bd *tx_buf;
496 	__le32 lflags = 0;
497 	skb_frag_t *frag;
498 
499 	i = skb_get_queue_mapping(skb);
500 	if (unlikely(i >= bp->tx_nr_rings)) {
501 		dev_kfree_skb_any(skb);
502 		dev_core_stats_tx_dropped_inc(dev);
503 		return NETDEV_TX_OK;
504 	}
505 
506 	txq = netdev_get_tx_queue(dev, i);
507 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
508 	prod = txr->tx_prod;
509 
510 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
511 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
512 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
513 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
514 		if (skb_linearize(skb))
515 			goto tx_free;
516 	}
517 #endif
518 	if (skb_is_gso(skb) &&
519 	    (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) &&
520 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
521 		int rc = bnxt_sw_udp_gso_xmit(bp, txr, txq, skb);
522 
523 		/* if SW USO queued a packet, the doorbell will be written
524 		 * below and there is no reason to track the last BD with
525 		 * suppressed completions
526 		 */
527 		if (rc > 0)
528 			txr->kick_txbd0 = NULL;
529 
530 		/* if a packet was queued by SW USO or a doorbell was pending
531 		 * from a previous xmit that was deferred, write the doorbell.
532 		 */
533 		if (rc > 0 || txr->kick_pending)
534 			bnxt_txr_db_kick(bp, txr, txr->tx_prod);
535 
536 		return rc < 0 ? NETDEV_TX_BUSY : NETDEV_TX_OK;
537 	}
538 
539 	/* Pad after the SW USO branch: bnxt_sw_udp_gso_xmit() would
540 	 * otherwise account the padding as UDP payload.
541 	 * Must be done before skb_shinfo(skb)->nr_frags is sampled,
542 	 * because skb_put_padto() might linearize the skb.
543 	 */
544 	if (skb_put_padto(skb, BNXT_MIN_PKT_SIZE)) {
545 		/* SKB already freed. */
546 		goto tx_kick_pending;
547 	}
548 
549 	free_size = bnxt_tx_avail(bp, txr);
550 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
551 		/* We must have raced with NAPI cleanup */
552 		if (net_ratelimit() && txr->kick_pending)
553 			netif_warn(bp, tx_err, dev,
554 				   "bnxt: ring busy w/ flush pending!\n");
555 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
556 					bp->tx_wake_thresh))
557 			return NETDEV_TX_BUSY;
558 	}
559 
560 	length = skb->len;
561 	len = skb_headlen(skb);
562 	last_frag = skb_shinfo(skb)->nr_frags;
563 
564 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
565 
566 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
567 	tx_buf->skb = skb;
568 	tx_buf->nr_frags = last_frag;
569 
570 	vlan_tag_flags = 0;
571 	cfa_action = bnxt_xmit_get_cfa_action(skb);
572 	if (skb_vlan_tag_present(skb)) {
573 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
574 				 skb_vlan_tag_get(skb);
575 		/* Currently supports 8021Q, 8021AD vlan offloads
576 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
577 		 */
578 		if (skb->vlan_proto == htons(ETH_P_8021Q))
579 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
580 	}
581 
582 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
583 	    ptp->tx_tstamp_en) {
584 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
585 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
586 			tx_buf->is_ts_pkt = 1;
587 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
588 		} else if (!skb_is_gso(skb)) {
589 			u16 seq_id, hdr_off;
590 
591 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
592 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
593 				if (vlan_tag_flags)
594 					hdr_off += VLAN_HLEN;
595 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
596 				tx_buf->is_ts_pkt = 1;
597 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
598 
599 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
600 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
601 				tx_buf->txts_prod = txts_prod;
602 			}
603 		}
604 	}
605 	if (unlikely(skb->no_fcs))
606 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
607 
608 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
609 	    skb_frags_readable(skb) && !lflags) {
610 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
611 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
612 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
613 		void __iomem *db = txr->tx_db.doorbell;
614 		void *pdata = tx_push_buf->data;
615 		u64 *end;
616 		int j, push_len;
617 
618 		/* Set COAL_NOW to be ready quickly for the next push */
619 		tx_push->tx_bd_len_flags_type =
620 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
621 					TX_BD_TYPE_LONG_TX_BD |
622 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
623 					TX_BD_FLAGS_COAL_NOW |
624 					TX_BD_FLAGS_PACKET_END |
625 					TX_BD_CNT(2));
626 
627 		if (skb->ip_summed == CHECKSUM_PARTIAL)
628 			tx_push1->tx_bd_hsize_lflags =
629 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
630 		else
631 			tx_push1->tx_bd_hsize_lflags = 0;
632 
633 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
634 		tx_push1->tx_bd_cfa_action =
635 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
636 
637 		end = pdata + length;
638 		end = PTR_ALIGN(end, 8) - 1;
639 		*end = 0;
640 
641 		skb_copy_from_linear_data(skb, pdata, len);
642 		pdata += len;
643 		for (j = 0; j < last_frag; j++) {
644 			void *fptr;
645 
646 			frag = &skb_shinfo(skb)->frags[j];
647 			fptr = skb_frag_address_safe(frag);
648 			if (!fptr)
649 				goto normal_tx;
650 
651 			memcpy(pdata, fptr, skb_frag_size(frag));
652 			pdata += skb_frag_size(frag);
653 		}
654 
655 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
656 		txbd->tx_bd_haddr = txr->data_mapping;
657 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
658 		prod = NEXT_TX(prod);
659 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
660 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
661 		memcpy(txbd, tx_push1, sizeof(*txbd));
662 		prod = NEXT_TX(prod);
663 		tx_push->doorbell =
664 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
665 				    DB_RING_IDX(&txr->tx_db, prod));
666 		WRITE_ONCE(txr->tx_prod, prod);
667 
668 		tx_buf->is_push = 1;
669 		netdev_tx_sent_queue(txq, skb->len);
670 		wmb();	/* Sync is_push and byte queue before pushing data */
671 
672 		push_len = (length + sizeof(*tx_push) + 7) / 8;
673 		if (push_len > 16) {
674 			__iowrite64_copy(db, tx_push_buf, 16);
675 			__iowrite32_copy(db + 4, tx_push_buf + 1,
676 					 (push_len - 16) << 1);
677 		} else {
678 			__iowrite64_copy(db, tx_push_buf, push_len);
679 		}
680 
681 		goto tx_done;
682 	}
683 
684 normal_tx:
685 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
686 
687 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
688 		goto tx_free;
689 
690 	dma_unmap_addr_set(tx_buf, mapping, mapping);
691 	dma_unmap_len_set(tx_buf, len, len);
692 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
693 		TX_BD_CNT(last_frag + 2);
694 
695 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
696 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
697 
698 	prod = NEXT_TX(prod);
699 	txbd1 = bnxt_init_ext_bd(bp, txr, prod, lflags, vlan_tag_flags,
700 				 cfa_action);
701 
702 	if (skb_is_gso(skb)) {
703 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
704 		u32 hdr_len;
705 
706 		if (skb->encapsulation) {
707 			if (udp_gso)
708 				hdr_len = skb_inner_transport_offset(skb) +
709 					  sizeof(struct udphdr);
710 			else
711 				hdr_len = skb_inner_tcp_all_headers(skb);
712 		} else if (udp_gso) {
713 			hdr_len = skb_transport_offset(skb) +
714 				  sizeof(struct udphdr);
715 		} else {
716 			hdr_len = skb_tcp_all_headers(skb);
717 		}
718 
719 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
720 					TX_BD_FLAGS_T_IPID |
721 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
722 		length = skb_shinfo(skb)->gso_size;
723 		txbd1->tx_bd_mss = cpu_to_le32(length);
724 		length += hdr_len;
725 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
726 		txbd1->tx_bd_hsize_lflags |=
727 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
728 	}
729 
730 	length >>= 9;
731 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
732 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
733 				     skb->len);
734 		i = 0;
735 		goto tx_dma_error;
736 	}
737 	flags |= bnxt_lhint_arr[length];
738 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
739 
740 	txbd0 = txbd;
741 	for (i = 0; i < last_frag; i++) {
742 		frag = &skb_shinfo(skb)->frags[i];
743 		prod = NEXT_TX(prod);
744 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
745 
746 		len = skb_frag_size(frag);
747 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
748 					   DMA_TO_DEVICE);
749 
750 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
751 			goto tx_dma_error;
752 
753 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
754 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
755 					  mapping, mapping);
756 		dma_unmap_len_set(tx_buf, len, len);
757 
758 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
759 
760 		flags = len << TX_BD_LEN_SHIFT;
761 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
762 	}
763 
764 	txbd->tx_bd_len_flags_type |= cpu_to_le32(TX_BD_FLAGS_PACKET_END);
765 
766 	netdev_tx_sent_queue(txq, skb->len);
767 
768 	skb_tx_timestamp(skb);
769 
770 	prod = NEXT_TX(prod);
771 	WRITE_ONCE(txr->tx_prod, prod);
772 
773 	txr->kick_txbd0 = NULL;
774 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
775 		bnxt_txr_db_kick(bp, txr, prod);
776 	} else {
777 		if (free_size >= bp->tx_wake_thresh) {
778 			txbd0->tx_bd_len_flags_type |=
779 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
780 			txr->kick_txbd0 = txbd0;
781 		}
782 		txr->kick_pending = 1;
783 	}
784 
785 tx_done:
786 
787 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
788 		if (txr->kick_pending)
789 			bnxt_txr_db_kick(bp, txr, prod);
790 
791 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
792 				   bp->tx_wake_thresh);
793 	}
794 	return NETDEV_TX_OK;
795 
796 tx_dma_error:
797 	last_frag = i;
798 
799 	/* start back at beginning and unmap skb */
800 	prod = txr->tx_prod;
801 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
802 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
803 			 skb_headlen(skb), DMA_TO_DEVICE);
804 	prod = NEXT_TX(prod);
805 
806 	/* unmap remaining mapped pages */
807 	for (i = 0; i < last_frag; i++) {
808 		prod = NEXT_TX(prod);
809 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
810 		frag = &skb_shinfo(skb)->frags[i];
811 		netmem_dma_unmap_page_attrs(&pdev->dev,
812 					    dma_unmap_addr(tx_buf, mapping),
813 					    skb_frag_size(frag),
814 					    DMA_TO_DEVICE, 0);
815 	}
816 
817 tx_free:
818 	dev_kfree_skb_any(skb);
819 tx_kick_pending:
820 	if (BNXT_TX_PTP_IS_SET(lflags)) {
821 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
822 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
823 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
824 			/* set SKB to err so PTP worker will clean up */
825 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
826 	}
827 	if (txr->kick_pending)
828 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
829 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
830 	dev_core_stats_tx_dropped_inc(dev);
831 	return NETDEV_TX_OK;
832 }
833 
834 /* Returns true if some remaining TX packets not processed. */
835 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
836 			  int budget)
837 {
838 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
839 	struct pci_dev *pdev = bp->pdev;
840 	u16 hw_cons = txr->tx_hw_cons;
841 	unsigned int tx_bytes = 0;
842 	u16 cons = txr->tx_cons;
843 	unsigned int dma_len;
844 	dma_addr_t dma_addr;
845 	int tx_pkts = 0;
846 	bool rc = false;
847 
848 	while (RING_TX(bp, cons) != hw_cons) {
849 		struct bnxt_sw_tx_bd *tx_buf, *head_buf;
850 		struct sk_buff *skb;
851 		bool is_ts_pkt;
852 		int j, last;
853 
854 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
855 		head_buf = tx_buf;
856 		skb = tx_buf->skb;
857 
858 		if (unlikely(!skb)) {
859 			bnxt_sched_reset_txr(bp, txr, cons);
860 			return rc;
861 		}
862 
863 		is_ts_pkt = tx_buf->is_ts_pkt;
864 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
865 			rc = true;
866 			break;
867 		}
868 
869 		cons = NEXT_TX(cons);
870 		tx_pkts++;
871 		tx_bytes += skb->len;
872 		tx_buf->skb = NULL;
873 		tx_buf->is_ts_pkt = 0;
874 
875 		if (tx_buf->is_push) {
876 			tx_buf->is_push = 0;
877 			goto next_tx_int;
878 		}
879 
880 		if (dma_unmap_len(tx_buf, len)) {
881 			dma_addr = dma_unmap_addr(tx_buf, mapping);
882 			dma_len = dma_unmap_len(tx_buf, len);
883 
884 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
885 					 DMA_TO_DEVICE);
886 		}
887 
888 		last = tx_buf->nr_frags;
889 
890 		for (j = 0; j < last; j++) {
891 			cons = NEXT_TX(cons);
892 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
893 			if (dma_unmap_len(tx_buf, len)) {
894 				dma_addr = dma_unmap_addr(tx_buf, mapping);
895 				dma_len = dma_unmap_len(tx_buf, len);
896 
897 				netmem_dma_unmap_page_attrs(&pdev->dev,
898 							    dma_addr, dma_len,
899 							    DMA_TO_DEVICE, 0);
900 			}
901 		}
902 
903 		if (unlikely(head_buf->is_sw_gso)) {
904 			u16 inline_cons = txr->tx_inline_cons + 1;
905 
906 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
907 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
908 				tso_dma_map_complete(&pdev->dev,
909 						     &head_buf->sw_gso_cstate);
910 			} else {
911 				tx_pkts--;
912 				tx_bytes -= skb->len;
913 				skb = NULL;
914 			}
915 			head_buf->is_sw_gso = 0;
916 		}
917 
918 		if (unlikely(is_ts_pkt)) {
919 			if (BNXT_CHIP_P5(bp)) {
920 				/* PTP worker takes ownership of the skb */
921 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
922 				skb = NULL;
923 			}
924 		}
925 
926 next_tx_int:
927 		cons = NEXT_TX(cons);
928 
929 		napi_consume_skb(skb, budget);
930 	}
931 
932 	WRITE_ONCE(txr->tx_cons, cons);
933 
934 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
935 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
936 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
937 
938 	return rc;
939 }
940 
941 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
942 {
943 	struct bnxt_tx_ring_info *txr;
944 	bool more = false;
945 	int i;
946 
947 	bnxt_for_each_napi_tx(i, bnapi, txr) {
948 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
949 			more |= __bnxt_tx_int(bp, txr, budget);
950 	}
951 	if (!more)
952 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
953 }
954 
955 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
956 {
957 	return rxr->need_head_pool || rxr->rx_page_size < PAGE_SIZE;
958 }
959 
960 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
961 					 struct bnxt_rx_ring_info *rxr,
962 					 unsigned int *offset,
963 					 gfp_t gfp)
964 {
965 	struct page *page;
966 
967 	if (rxr->rx_page_size < PAGE_SIZE) {
968 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
969 						rxr->rx_page_size);
970 	} else {
971 		page = page_pool_dev_alloc_pages(rxr->page_pool);
972 		*offset = 0;
973 	}
974 	if (!page)
975 		return NULL;
976 
977 	*mapping = page_pool_get_dma_addr(page) + *offset;
978 	return page;
979 }
980 
981 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
982 					 struct bnxt_rx_ring_info *rxr,
983 					 unsigned int *offset,
984 					 gfp_t gfp)
985 {
986 	netmem_ref netmem;
987 
988 	if (rxr->rx_page_size < PAGE_SIZE) {
989 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset,
990 						     rxr->rx_page_size, gfp);
991 	} else {
992 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
993 		*offset = 0;
994 	}
995 	if (!netmem)
996 		return 0;
997 
998 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
999 	return netmem;
1000 }
1001 
1002 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
1003 				       struct bnxt_rx_ring_info *rxr,
1004 				       gfp_t gfp)
1005 {
1006 	unsigned int offset;
1007 	struct page *page;
1008 
1009 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
1010 				    bp->rx_buf_size, gfp);
1011 	if (!page)
1012 		return NULL;
1013 
1014 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
1015 	return page_address(page) + offset;
1016 }
1017 
1018 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1019 		       u16 prod, gfp_t gfp)
1020 {
1021 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1022 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1023 	dma_addr_t mapping;
1024 
1025 	if (BNXT_RX_PAGE_MODE(bp)) {
1026 		unsigned int offset;
1027 		struct page *page =
1028 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1029 
1030 		if (!page)
1031 			return -ENOMEM;
1032 
1033 		mapping += bp->rx_dma_offset;
1034 		rx_buf->data = page;
1035 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
1036 		rx_buf->offset = offset;
1037 	} else {
1038 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
1039 
1040 		if (!data)
1041 			return -ENOMEM;
1042 
1043 		rx_buf->data = data;
1044 		rx_buf->data_ptr = data + bp->rx_offset;
1045 		rx_buf->offset = 0;
1046 	}
1047 	rx_buf->mapping = mapping;
1048 
1049 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1050 	return 0;
1051 }
1052 
1053 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1054 {
1055 	u16 prod = rxr->rx_prod;
1056 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1057 	struct bnxt *bp = rxr->bnapi->bp;
1058 	struct rx_bd *cons_bd, *prod_bd;
1059 
1060 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1061 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1062 
1063 	prod_rx_buf->data = data;
1064 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1065 
1066 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1067 	prod_rx_buf->offset = cons_rx_buf->offset;
1068 
1069 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1070 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1071 
1072 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1073 }
1074 
1075 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1076 {
1077 	u16 next, max = rxr->rx_agg_bmap_size;
1078 
1079 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1080 	if (next >= max)
1081 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1082 	return next;
1083 }
1084 
1085 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1086 				u16 prod, gfp_t gfp)
1087 {
1088 	struct rx_bd *rxbd =
1089 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1090 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1091 	u16 sw_prod = rxr->rx_sw_agg_prod;
1092 	unsigned int offset = 0;
1093 	dma_addr_t mapping;
1094 	netmem_ref netmem;
1095 
1096 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1097 	if (!netmem)
1098 		return -ENOMEM;
1099 
1100 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1101 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1102 
1103 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1104 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1105 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1106 
1107 	rx_agg_buf->netmem = netmem;
1108 	rx_agg_buf->offset = offset;
1109 	rx_agg_buf->mapping = mapping;
1110 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1111 	rxbd->rx_bd_opaque = sw_prod;
1112 	return 0;
1113 }
1114 
1115 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1116 				       struct bnxt_cp_ring_info *cpr,
1117 				       u16 cp_cons, u16 curr)
1118 {
1119 	struct rx_agg_cmp *agg;
1120 
1121 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1122 	agg = (struct rx_agg_cmp *)
1123 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1124 	return agg;
1125 }
1126 
1127 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1128 					      struct bnxt_rx_ring_info *rxr,
1129 					      u16 agg_id, u16 curr)
1130 {
1131 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1132 
1133 	return &tpa_info->agg_arr[curr];
1134 }
1135 
1136 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1137 				   u16 start, u32 agg_bufs, bool tpa)
1138 {
1139 	struct bnxt_napi *bnapi = cpr->bnapi;
1140 	struct bnxt *bp = bnapi->bp;
1141 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1142 	u16 prod = rxr->rx_agg_prod;
1143 	u16 sw_prod = rxr->rx_sw_agg_prod;
1144 	bool p5_tpa = false;
1145 	u32 i;
1146 
1147 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1148 		p5_tpa = true;
1149 
1150 	for (i = 0; i < agg_bufs; i++) {
1151 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1152 		struct rx_agg_cmp *agg;
1153 		struct rx_bd *prod_bd;
1154 		netmem_ref netmem;
1155 		u16 cons;
1156 
1157 		if (p5_tpa)
1158 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1159 		else
1160 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1161 		cons = agg->rx_agg_cmp_opaque;
1162 		__clear_bit(cons, rxr->rx_agg_bmap);
1163 
1164 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1165 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1166 
1167 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1168 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1169 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1170 
1171 		/* It is possible for sw_prod to be equal to cons, so
1172 		 * set cons_rx_buf->netmem to 0 first.
1173 		 */
1174 		netmem = cons_rx_buf->netmem;
1175 		cons_rx_buf->netmem = 0;
1176 		prod_rx_buf->netmem = netmem;
1177 		prod_rx_buf->offset = cons_rx_buf->offset;
1178 
1179 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1180 
1181 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1182 
1183 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1184 		prod_bd->rx_bd_opaque = sw_prod;
1185 
1186 		prod = NEXT_RX_AGG(prod);
1187 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1188 	}
1189 	rxr->rx_agg_prod = prod;
1190 	rxr->rx_sw_agg_prod = sw_prod;
1191 }
1192 
1193 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1194 					      struct bnxt_rx_ring_info *rxr,
1195 					      u16 cons, void *data, u8 *data_ptr,
1196 					      dma_addr_t dma_addr,
1197 					      unsigned int offset_and_len)
1198 {
1199 	unsigned int len = offset_and_len & 0xffff;
1200 	struct page *page = data;
1201 	u16 prod = rxr->rx_prod;
1202 	struct sk_buff *skb;
1203 	void *frag_start;
1204 	int err;
1205 
1206 	frag_start = page_address(page) + rxr->rx_buf_ring[cons].offset;
1207 
1208 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1209 	if (unlikely(err)) {
1210 		bnxt_reuse_rx_data(rxr, cons, data);
1211 		return NULL;
1212 	}
1213 	dma_addr -= bp->rx_dma_offset;
1214 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1215 				bp->rx_dir);
1216 	skb = napi_build_skb(frag_start, rxr->rx_page_size);
1217 	if (!skb) {
1218 		page_pool_recycle_direct(rxr->page_pool, page);
1219 		return NULL;
1220 	}
1221 	skb_mark_for_recycle(skb);
1222 	skb_reserve(skb, data_ptr - (u8 *)frag_start);
1223 	__skb_put(skb, len);
1224 
1225 	return skb;
1226 }
1227 
1228 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1229 					struct bnxt_rx_ring_info *rxr,
1230 					u16 cons, void *data, u8 *data_ptr,
1231 					dma_addr_t dma_addr,
1232 					unsigned int offset_and_len)
1233 {
1234 	unsigned int payload = offset_and_len >> 16;
1235 	unsigned int len = offset_and_len & 0xffff;
1236 	skb_frag_t *frag;
1237 	struct page *page = data;
1238 	u16 prod = rxr->rx_prod;
1239 	struct sk_buff *skb;
1240 	int off, err;
1241 
1242 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1243 	if (unlikely(err)) {
1244 		bnxt_reuse_rx_data(rxr, cons, data);
1245 		return NULL;
1246 	}
1247 	dma_addr -= bp->rx_dma_offset;
1248 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, rxr->rx_page_size,
1249 				bp->rx_dir);
1250 
1251 	if (unlikely(!payload))
1252 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1253 
1254 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1255 	if (!skb) {
1256 		page_pool_recycle_direct(rxr->page_pool, page);
1257 		return NULL;
1258 	}
1259 
1260 	skb_mark_for_recycle(skb);
1261 	off = (void *)data_ptr - page_address(page);
1262 	skb_add_rx_frag(skb, 0, page, off, len, rxr->rx_page_size);
1263 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1264 	       payload + NET_IP_ALIGN);
1265 
1266 	frag = &skb_shinfo(skb)->frags[0];
1267 	skb_frag_size_sub(frag, payload);
1268 	skb_frag_off_add(frag, payload);
1269 	skb->data_len -= payload;
1270 	skb->tail += payload;
1271 
1272 	return skb;
1273 }
1274 
1275 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1276 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1277 				   void *data, u8 *data_ptr,
1278 				   dma_addr_t dma_addr,
1279 				   unsigned int offset_and_len)
1280 {
1281 	u16 prod = rxr->rx_prod;
1282 	struct sk_buff *skb;
1283 	int err;
1284 
1285 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1286 	if (unlikely(err)) {
1287 		bnxt_reuse_rx_data(rxr, cons, data);
1288 		return NULL;
1289 	}
1290 
1291 	skb = napi_build_skb(data, bp->rx_buf_size);
1292 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1293 				bp->rx_dir);
1294 	if (!skb) {
1295 		page_pool_free_va(rxr->head_pool, data, true);
1296 		return NULL;
1297 	}
1298 
1299 	skb_mark_for_recycle(skb);
1300 	skb_reserve(skb, bp->rx_offset);
1301 	skb_put(skb, offset_and_len & 0xffff);
1302 	return skb;
1303 }
1304 
1305 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1306 				 struct bnxt_cp_ring_info *cpr,
1307 				 u16 idx, u32 agg_bufs, bool tpa,
1308 				 struct sk_buff *skb,
1309 				 struct xdp_buff *xdp)
1310 {
1311 	struct bnxt_napi *bnapi = cpr->bnapi;
1312 	struct skb_shared_info *shinfo;
1313 	struct bnxt_rx_ring_info *rxr;
1314 	u32 i, total_frag_len = 0;
1315 	bool p5_tpa = false;
1316 	u16 prod;
1317 
1318 	rxr = bnapi->rx_ring;
1319 	prod = rxr->rx_agg_prod;
1320 
1321 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1322 		p5_tpa = true;
1323 
1324 	if (skb)
1325 		shinfo = skb_shinfo(skb);
1326 	else
1327 		shinfo = xdp_get_shared_info_from_buff(xdp);
1328 
1329 	for (i = 0; i < agg_bufs; i++) {
1330 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1331 		struct rx_agg_cmp *agg;
1332 		u16 cons, frag_len;
1333 		netmem_ref netmem;
1334 
1335 		if (p5_tpa)
1336 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1337 		else
1338 			agg = bnxt_get_agg(bp, cpr, idx, i);
1339 		cons = agg->rx_agg_cmp_opaque;
1340 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1341 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1342 
1343 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1344 		if (skb) {
1345 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1346 					       cons_rx_buf->offset,
1347 					       frag_len, rxr->rx_page_size);
1348 		} else {
1349 			skb_frag_t *frag = &shinfo->frags[i];
1350 
1351 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1352 						  cons_rx_buf->offset,
1353 						  frag_len);
1354 			shinfo->nr_frags = i + 1;
1355 		}
1356 		__clear_bit(cons, rxr->rx_agg_bmap);
1357 
1358 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1359 		 * a sw_prod index that equals the cons index, so we
1360 		 * need to clear the cons entry now.
1361 		 */
1362 		netmem = cons_rx_buf->netmem;
1363 		cons_rx_buf->netmem = 0;
1364 
1365 		if (xdp && netmem_is_pfmemalloc(netmem))
1366 			xdp_buff_set_frag_pfmemalloc(xdp);
1367 
1368 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1369 			if (skb) {
1370 				skb->len -= frag_len;
1371 				skb->data_len -= frag_len;
1372 				skb->truesize -= rxr->rx_page_size;
1373 			}
1374 
1375 			--shinfo->nr_frags;
1376 			cons_rx_buf->netmem = netmem;
1377 
1378 			/* Update prod since possibly some netmems have been
1379 			 * allocated already.
1380 			 */
1381 			rxr->rx_agg_prod = prod;
1382 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1383 			return 0;
1384 		}
1385 
1386 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1387 						  rxr->rx_page_size);
1388 
1389 		total_frag_len += frag_len;
1390 		prod = NEXT_RX_AGG(prod);
1391 	}
1392 	rxr->rx_agg_prod = prod;
1393 	return total_frag_len;
1394 }
1395 
1396 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1397 					       struct bnxt_cp_ring_info *cpr,
1398 					       struct sk_buff *skb, u16 idx,
1399 					       u32 agg_bufs, bool tpa)
1400 {
1401 	u32 total_frag_len = 0;
1402 
1403 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1404 					       skb, NULL);
1405 	if (!total_frag_len) {
1406 		skb_mark_for_recycle(skb);
1407 		dev_kfree_skb(skb);
1408 		return NULL;
1409 	}
1410 
1411 	return skb;
1412 }
1413 
1414 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1415 				   struct bnxt_cp_ring_info *cpr,
1416 				   struct xdp_buff *xdp, u16 idx,
1417 				   u32 agg_bufs, bool tpa)
1418 {
1419 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1420 	u32 total_frag_len = 0;
1421 
1422 	if (!xdp_buff_has_frags(xdp))
1423 		shinfo->nr_frags = 0;
1424 
1425 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1426 					       NULL, xdp);
1427 	if (total_frag_len) {
1428 		xdp_buff_set_frags_flag(xdp);
1429 		shinfo->nr_frags = agg_bufs;
1430 		shinfo->xdp_frags_size = total_frag_len;
1431 	}
1432 	return total_frag_len;
1433 }
1434 
1435 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1436 			       u8 agg_bufs, u32 *raw_cons)
1437 {
1438 	u16 last;
1439 	struct rx_agg_cmp *agg;
1440 
1441 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1442 	last = RING_CMP(*raw_cons);
1443 	agg = (struct rx_agg_cmp *)
1444 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1445 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1446 }
1447 
1448 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1449 				      unsigned int len,
1450 				      dma_addr_t mapping)
1451 {
1452 	struct bnxt *bp = bnapi->bp;
1453 	struct pci_dev *pdev = bp->pdev;
1454 	struct sk_buff *skb;
1455 
1456 	skb = napi_alloc_skb(&bnapi->napi, len);
1457 	if (!skb)
1458 		return NULL;
1459 
1460 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1461 				bp->rx_dir);
1462 
1463 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1464 	       len + NET_IP_ALIGN);
1465 
1466 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1467 				   bp->rx_dir);
1468 
1469 	skb_put(skb, len);
1470 
1471 	return skb;
1472 }
1473 
1474 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1475 				     unsigned int len,
1476 				     dma_addr_t mapping)
1477 {
1478 	return bnxt_copy_data(bnapi, data, len, mapping);
1479 }
1480 
1481 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1482 				     struct xdp_buff *xdp,
1483 				     unsigned int len,
1484 				     dma_addr_t mapping)
1485 {
1486 	unsigned int metasize = 0;
1487 	u8 *data = xdp->data;
1488 	struct sk_buff *skb;
1489 
1490 	len = xdp->data_end - xdp->data_meta;
1491 	metasize = xdp->data - xdp->data_meta;
1492 	data = xdp->data_meta;
1493 
1494 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1495 	if (!skb)
1496 		return skb;
1497 
1498 	if (metasize) {
1499 		skb_metadata_set(skb, metasize);
1500 		__skb_pull(skb, metasize);
1501 	}
1502 
1503 	return skb;
1504 }
1505 
1506 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1507 			   u32 *raw_cons, void *cmp)
1508 {
1509 	struct rx_cmp *rxcmp = cmp;
1510 	u32 tmp_raw_cons = *raw_cons;
1511 	u8 cmp_type, agg_bufs = 0;
1512 
1513 	cmp_type = RX_CMP_TYPE(rxcmp);
1514 
1515 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1516 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1517 			    RX_CMP_AGG_BUFS) >>
1518 			   RX_CMP_AGG_BUFS_SHIFT;
1519 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1520 		struct rx_tpa_end_cmp *tpa_end = cmp;
1521 
1522 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1523 			return 0;
1524 
1525 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1526 	}
1527 
1528 	if (agg_bufs) {
1529 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1530 			return -EBUSY;
1531 	}
1532 	*raw_cons = tmp_raw_cons;
1533 	return 0;
1534 }
1535 
1536 static u16 bnxt_alloc_agg_idx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1537 			      u16 agg_id)
1538 {
1539 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1540 	u16 idx = agg_id & (bp->max_tpa_roundup_size - 1);
1541 
1542 	if (test_bit(idx, map->agg_idx_bmap)) {
1543 		idx = find_first_zero_bit(map->agg_idx_bmap,
1544 					  bp->max_tpa_roundup_size);
1545 		if (idx >= bp->max_tpa_roundup_size)
1546 			return INVALID_HW_RING_ID;
1547 	}
1548 	__set_bit(idx, map->agg_idx_bmap);
1549 	map->agg_id_tbl[agg_id] = idx;
1550 	return idx;
1551 }
1552 
1553 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1554 {
1555 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1556 
1557 	__clear_bit(idx, map->agg_idx_bmap);
1558 }
1559 
1560 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1561 {
1562 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1563 
1564 	return map->agg_id_tbl[agg_id];
1565 }
1566 
1567 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1568 			      struct rx_tpa_start_cmp *tpa_start,
1569 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1570 {
1571 	tpa_info->cfa_code_valid = 1;
1572 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1573 	tpa_info->vlan_valid = 0;
1574 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1575 		tpa_info->vlan_valid = 1;
1576 		tpa_info->metadata =
1577 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1578 	}
1579 }
1580 
1581 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1582 				 struct rx_tpa_start_cmp *tpa_start,
1583 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1584 {
1585 	tpa_info->vlan_valid = 0;
1586 	if (TPA_START_VLAN_VALID(tpa_start)) {
1587 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1588 		u32 vlan_proto = ETH_P_8021Q;
1589 
1590 		tpa_info->vlan_valid = 1;
1591 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1592 			vlan_proto = ETH_P_8021AD;
1593 		tpa_info->metadata = vlan_proto << 16 |
1594 				     TPA_START_METADATA0_TCI(tpa_start1);
1595 	}
1596 }
1597 
1598 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1599 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1600 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1601 {
1602 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1603 	struct bnxt_tpa_info *tpa_info;
1604 	u16 cons, prod, agg_id;
1605 	struct rx_bd *prod_bd;
1606 	dma_addr_t mapping;
1607 
1608 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1609 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1610 		agg_id = bnxt_alloc_agg_idx(bp, rxr, agg_id);
1611 		if (unlikely(agg_id == INVALID_HW_RING_ID)) {
1612 			netdev_warn(bp->dev, "Unable to allocate agg ID for ring %d, agg 0x%x\n",
1613 				    rxr->bnapi->index,
1614 				    TPA_START_AGG_ID_P5(tpa_start));
1615 			bnxt_sched_reset_rxr(bp, rxr);
1616 			return;
1617 		}
1618 	} else {
1619 		agg_id = TPA_START_AGG_ID(tpa_start);
1620 	}
1621 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1622 	prod = rxr->rx_prod;
1623 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1624 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1625 	tpa_info = &rxr->rx_tpa[agg_id];
1626 
1627 	if (unlikely(cons != rxr->rx_next_cons ||
1628 		     TPA_START_ERROR(tpa_start))) {
1629 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1630 			    cons, rxr->rx_next_cons,
1631 			    TPA_START_ERROR_CODE(tpa_start1));
1632 		bnxt_sched_reset_rxr(bp, rxr);
1633 		return;
1634 	}
1635 	prod_rx_buf->data = tpa_info->data;
1636 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1637 
1638 	mapping = tpa_info->mapping;
1639 	prod_rx_buf->mapping = mapping;
1640 
1641 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1642 
1643 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1644 
1645 	tpa_info->data = cons_rx_buf->data;
1646 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1647 	cons_rx_buf->data = NULL;
1648 	tpa_info->mapping = cons_rx_buf->mapping;
1649 
1650 	tpa_info->len =
1651 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1652 				RX_TPA_START_CMP_LEN_SHIFT;
1653 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1654 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1655 		tpa_info->gso_type = SKB_GSO_TCPV4;
1656 		if (TPA_START_IS_IPV6(tpa_start1))
1657 			tpa_info->gso_type = SKB_GSO_TCPV6;
1658 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1659 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1660 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1661 			tpa_info->gso_type = SKB_GSO_TCPV6;
1662 		tpa_info->rss_hash =
1663 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1664 	} else {
1665 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1666 		tpa_info->gso_type = 0;
1667 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1668 	}
1669 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1670 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1671 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1672 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1673 	else
1674 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1675 	tpa_info->agg_count = 0;
1676 
1677 	rxr->rx_prod = NEXT_RX(prod);
1678 	cons = RING_RX(bp, NEXT_RX(cons));
1679 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1680 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1681 
1682 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1683 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1684 	cons_rx_buf->data = NULL;
1685 }
1686 
1687 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1688 {
1689 	if (agg_bufs)
1690 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1691 }
1692 
1693 #ifdef CONFIG_INET
1694 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1695 {
1696 	struct udphdr *uh = NULL;
1697 
1698 	if (ip_proto == htons(ETH_P_IP)) {
1699 		struct iphdr *iph = (struct iphdr *)skb->data;
1700 
1701 		if (iph->protocol == IPPROTO_UDP)
1702 			uh = (struct udphdr *)(iph + 1);
1703 	} else {
1704 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1705 
1706 		if (iph->nexthdr == IPPROTO_UDP)
1707 			uh = (struct udphdr *)(iph + 1);
1708 	}
1709 	if (uh) {
1710 		if (uh->check)
1711 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1712 		else
1713 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1714 	}
1715 }
1716 #endif
1717 
1718 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1719 					   int payload_off, int tcp_ts,
1720 					   struct sk_buff *skb)
1721 {
1722 #ifdef CONFIG_INET
1723 	struct tcphdr *th;
1724 	int len, nw_off;
1725 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1726 	u32 hdr_info = tpa_info->hdr_info;
1727 	bool loopback = false;
1728 
1729 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1730 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1731 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1732 
1733 	/* If the packet is an internal loopback packet, the offsets will
1734 	 * have an extra 4 bytes.
1735 	 */
1736 	if (inner_mac_off == 4) {
1737 		loopback = true;
1738 	} else if (inner_mac_off > 4) {
1739 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1740 					    ETH_HLEN - 2));
1741 
1742 		/* We only support inner iPv4/ipv6.  If we don't see the
1743 		 * correct protocol ID, it must be a loopback packet where
1744 		 * the offsets are off by 4.
1745 		 */
1746 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1747 			loopback = true;
1748 	}
1749 	if (loopback) {
1750 		/* internal loopback packet, subtract all offsets by 4 */
1751 		inner_ip_off -= 4;
1752 		inner_mac_off -= 4;
1753 		outer_ip_off -= 4;
1754 	}
1755 
1756 	nw_off = inner_ip_off - ETH_HLEN;
1757 	skb_set_network_header(skb, nw_off);
1758 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1759 		struct ipv6hdr *iph = ipv6_hdr(skb);
1760 
1761 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1762 		len = skb->len - skb_transport_offset(skb);
1763 		th = tcp_hdr(skb);
1764 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1765 	} else {
1766 		struct iphdr *iph = ip_hdr(skb);
1767 
1768 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1769 		len = skb->len - skb_transport_offset(skb);
1770 		th = tcp_hdr(skb);
1771 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1772 	}
1773 
1774 	if (inner_mac_off) { /* tunnel */
1775 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1776 					    ETH_HLEN - 2));
1777 
1778 		bnxt_gro_tunnel(skb, proto);
1779 	}
1780 #endif
1781 	return skb;
1782 }
1783 
1784 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1785 					   int payload_off, int tcp_ts,
1786 					   struct sk_buff *skb)
1787 {
1788 #ifdef CONFIG_INET
1789 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1790 	u32 hdr_info = tpa_info->hdr_info;
1791 	int iphdr_len, nw_off;
1792 
1793 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1794 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1795 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1796 
1797 	nw_off = inner_ip_off - ETH_HLEN;
1798 	skb_set_network_header(skb, nw_off);
1799 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1800 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1801 	skb_set_transport_header(skb, nw_off + iphdr_len);
1802 
1803 	if (inner_mac_off) { /* tunnel */
1804 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1805 					    ETH_HLEN - 2));
1806 
1807 		bnxt_gro_tunnel(skb, proto);
1808 	}
1809 #endif
1810 	return skb;
1811 }
1812 
1813 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1814 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1815 
1816 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1817 					   int payload_off, int tcp_ts,
1818 					   struct sk_buff *skb)
1819 {
1820 #ifdef CONFIG_INET
1821 	struct tcphdr *th;
1822 	int len, nw_off, tcp_opt_len = 0;
1823 
1824 	if (tcp_ts)
1825 		tcp_opt_len = 12;
1826 
1827 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1828 		struct iphdr *iph;
1829 
1830 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1831 			 ETH_HLEN;
1832 		skb_set_network_header(skb, nw_off);
1833 		iph = ip_hdr(skb);
1834 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1835 		len = skb->len - skb_transport_offset(skb);
1836 		th = tcp_hdr(skb);
1837 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1838 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1839 		struct ipv6hdr *iph;
1840 
1841 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1842 			 ETH_HLEN;
1843 		skb_set_network_header(skb, nw_off);
1844 		iph = ipv6_hdr(skb);
1845 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1846 		len = skb->len - skb_transport_offset(skb);
1847 		th = tcp_hdr(skb);
1848 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1849 	} else {
1850 		dev_kfree_skb_any(skb);
1851 		return NULL;
1852 	}
1853 
1854 	if (nw_off) /* tunnel */
1855 		bnxt_gro_tunnel(skb, skb->protocol);
1856 #endif
1857 	return skb;
1858 }
1859 
1860 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1861 					   struct bnxt_tpa_info *tpa_info,
1862 					   struct rx_tpa_end_cmp *tpa_end,
1863 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1864 					   struct sk_buff *skb,
1865 					   struct bnxt_rx_sw_stats *rx_stats)
1866 {
1867 #ifdef CONFIG_INET
1868 	int payload_off;
1869 	u16 segs;
1870 
1871 	segs = TPA_END_TPA_SEGS(tpa_end);
1872 	if (segs == 1)
1873 		return skb;
1874 
1875 	rx_stats->rx_hw_gro_packets++;
1876 	rx_stats->rx_hw_gro_wire_packets += segs;
1877 
1878 	NAPI_GRO_CB(skb)->count = segs;
1879 	skb_shinfo(skb)->gso_size =
1880 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1881 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1882 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1883 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1884 	else
1885 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1886 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1887 	if (likely(skb))
1888 		tcp_gro_complete(skb);
1889 #endif
1890 	return skb;
1891 }
1892 
1893 /* Given the cfa_code of a received packet determine which
1894  * netdev (vf-rep or PF) the packet is destined to.
1895  */
1896 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1897 {
1898 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1899 
1900 	/* if vf-rep dev is NULL, it must belong to the PF */
1901 	return dev ? dev : bp->dev;
1902 }
1903 
1904 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1905 					   struct bnxt_cp_ring_info *cpr,
1906 					   u32 *raw_cons,
1907 					   struct rx_tpa_end_cmp *tpa_end,
1908 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1909 					   u8 *event)
1910 {
1911 	struct bnxt_napi *bnapi = cpr->bnapi;
1912 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1913 	struct net_device *dev = bp->dev;
1914 	u8 *data_ptr, agg_bufs;
1915 	unsigned int len;
1916 	struct bnxt_tpa_info *tpa_info;
1917 	dma_addr_t mapping;
1918 	struct sk_buff *skb;
1919 	u16 idx = 0, agg_id;
1920 	void *data;
1921 	bool gro;
1922 
1923 	if (unlikely(bnapi->in_reset)) {
1924 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1925 
1926 		if (rc < 0)
1927 			return ERR_PTR(-EBUSY);
1928 		return NULL;
1929 	}
1930 
1931 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1932 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1933 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1934 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1935 		tpa_info = &rxr->rx_tpa[agg_id];
1936 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1937 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1938 				    agg_bufs, tpa_info->agg_count);
1939 			agg_bufs = tpa_info->agg_count;
1940 		}
1941 		tpa_info->agg_count = 0;
1942 		*event |= BNXT_AGG_EVENT;
1943 		bnxt_free_agg_idx(rxr, agg_id);
1944 		idx = agg_id;
1945 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1946 	} else {
1947 		agg_id = TPA_END_AGG_ID(tpa_end);
1948 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1949 		tpa_info = &rxr->rx_tpa[agg_id];
1950 		idx = RING_CMP(*raw_cons);
1951 		if (agg_bufs) {
1952 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1953 				return ERR_PTR(-EBUSY);
1954 
1955 			*event |= BNXT_AGG_EVENT;
1956 			idx = NEXT_CMP(idx);
1957 		}
1958 		gro = !!TPA_END_GRO(tpa_end);
1959 	}
1960 	data = tpa_info->data;
1961 	data_ptr = tpa_info->data_ptr;
1962 	prefetch(data_ptr);
1963 	len = tpa_info->len;
1964 	mapping = tpa_info->mapping;
1965 
1966 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1967 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1968 		if (agg_bufs > MAX_SKB_FRAGS)
1969 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1970 				    agg_bufs, (int)MAX_SKB_FRAGS);
1971 		return NULL;
1972 	}
1973 
1974 	if (len <= bp->rx_copybreak) {
1975 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1976 		if (!skb) {
1977 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1978 			cpr->sw_stats->rx.rx_oom_discards += 1;
1979 			return NULL;
1980 		}
1981 	} else {
1982 		u8 *new_data;
1983 		dma_addr_t new_mapping;
1984 
1985 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1986 						GFP_ATOMIC);
1987 		if (!new_data) {
1988 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1989 			cpr->sw_stats->rx.rx_oom_discards += 1;
1990 			return NULL;
1991 		}
1992 
1993 		tpa_info->data = new_data;
1994 		tpa_info->data_ptr = new_data + bp->rx_offset;
1995 		tpa_info->mapping = new_mapping;
1996 
1997 		skb = napi_build_skb(data, bp->rx_buf_size);
1998 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1999 					bp->rx_buf_use_size, bp->rx_dir);
2000 
2001 		if (!skb) {
2002 			page_pool_free_va(rxr->head_pool, data, true);
2003 			bnxt_abort_tpa(cpr, idx, agg_bufs);
2004 			cpr->sw_stats->rx.rx_oom_discards += 1;
2005 			return NULL;
2006 		}
2007 		skb_mark_for_recycle(skb);
2008 		skb_reserve(skb, bp->rx_offset);
2009 		skb_put(skb, len);
2010 	}
2011 
2012 	if (agg_bufs) {
2013 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
2014 					      true);
2015 		if (!skb) {
2016 			/* Page reuse already handled by bnxt_rx_pages(). */
2017 			cpr->sw_stats->rx.rx_oom_discards += 1;
2018 			return NULL;
2019 		}
2020 	}
2021 
2022 	if (tpa_info->cfa_code_valid)
2023 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
2024 	skb->protocol = eth_type_trans(skb, dev);
2025 
2026 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
2027 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
2028 
2029 	if (tpa_info->vlan_valid &&
2030 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
2031 		__be16 vlan_proto = htons(tpa_info->metadata >>
2032 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
2033 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2034 
2035 		if (eth_type_vlan(vlan_proto)) {
2036 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2037 		} else {
2038 			dev_kfree_skb(skb);
2039 			return NULL;
2040 		}
2041 	}
2042 
2043 	skb_checksum_none_assert(skb);
2044 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
2045 		skb->ip_summed = CHECKSUM_UNNECESSARY;
2046 		skb->csum_level =
2047 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
2048 	}
2049 
2050 	if (gro)
2051 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb,
2052 				   &cpr->sw_stats->rx);
2053 
2054 	return skb;
2055 }
2056 
2057 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
2058 			 struct rx_agg_cmp *rx_agg)
2059 {
2060 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
2061 	struct bnxt_tpa_info *tpa_info;
2062 
2063 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
2064 	tpa_info = &rxr->rx_tpa[agg_id];
2065 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
2066 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
2067 }
2068 
2069 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
2070 			     struct sk_buff *skb)
2071 {
2072 	skb_mark_for_recycle(skb);
2073 
2074 	if (skb->dev != bp->dev) {
2075 		/* this packet belongs to a vf-rep */
2076 		bnxt_vf_rep_rx(bp, skb);
2077 		return;
2078 	}
2079 	skb_record_rx_queue(skb, bnapi->index);
2080 	napi_gro_receive(&bnapi->napi, skb);
2081 }
2082 
2083 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2084 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2085 {
2086 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2087 
2088 	if (BNXT_PTP_RX_TS_VALID(flags))
2089 		goto ts_valid;
2090 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2091 		return false;
2092 
2093 ts_valid:
2094 	*cmpl_ts = ts;
2095 	return true;
2096 }
2097 
2098 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2099 				    struct rx_cmp *rxcmp,
2100 				    struct rx_cmp_ext *rxcmp1)
2101 {
2102 	__be16 vlan_proto;
2103 	u16 vtag;
2104 
2105 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2106 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2107 		u32 meta_data;
2108 
2109 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2110 			return skb;
2111 
2112 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2113 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2114 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2115 		if (eth_type_vlan(vlan_proto))
2116 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2117 		else
2118 			goto vlan_err;
2119 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2120 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2121 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2122 
2123 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2124 				vlan_proto = htons(ETH_P_8021Q);
2125 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2126 				vlan_proto = htons(ETH_P_8021AD);
2127 			else
2128 				goto vlan_err;
2129 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2130 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2131 		}
2132 	}
2133 	return skb;
2134 vlan_err:
2135 	skb_mark_for_recycle(skb);
2136 	dev_kfree_skb(skb);
2137 	return NULL;
2138 }
2139 
2140 /* returns the following:
2141  * 1       - 1 packet successfully received
2142  * 0       - successful TPA_START, packet not completed yet
2143  * -EBUSY  - completion ring does not have all the agg buffers yet
2144  * -ENOMEM - packet aborted due to out of memory
2145  * -EIO    - packet aborted due to hw error indicated in BD
2146  */
2147 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2148 		       u32 *raw_cons, u8 *event)
2149 {
2150 	struct bnxt_napi *bnapi = cpr->bnapi;
2151 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2152 	struct net_device *dev = bp->dev;
2153 	struct rx_cmp *rxcmp;
2154 	struct rx_cmp_ext *rxcmp1;
2155 	u32 tmp_raw_cons = *raw_cons;
2156 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2157 	struct skb_shared_info *sinfo;
2158 	struct bnxt_xdp_buff bnxt_xdp;
2159 	struct bnxt_sw_rx_bd *rx_buf;
2160 	unsigned int len;
2161 	u8 *data_ptr, agg_bufs, cmp_type;
2162 	bool xdp_active = false;
2163 	dma_addr_t dma_addr;
2164 	struct sk_buff *skb;
2165 	u32 flags, misc;
2166 	u32 cmpl_ts;
2167 	void *data;
2168 	int rc = 0;
2169 
2170 	rxcmp = (struct rx_cmp *)
2171 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2172 
2173 	cmp_type = RX_CMP_TYPE(rxcmp);
2174 
2175 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2176 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2177 		goto next_rx_no_prod_no_len;
2178 	}
2179 
2180 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2181 	cp_cons = RING_CMP(tmp_raw_cons);
2182 	rxcmp1 = (struct rx_cmp_ext *)
2183 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2184 
2185 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2186 		return -EBUSY;
2187 
2188 	/* The valid test of the entry must be done first before
2189 	 * reading any further.
2190 	 */
2191 	dma_rmb();
2192 	prod = rxr->rx_prod;
2193 
2194 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2195 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2196 		bnxt_tpa_start(bp, rxr, cmp_type,
2197 			       (struct rx_tpa_start_cmp *)rxcmp,
2198 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2199 
2200 		*event |= BNXT_RX_EVENT;
2201 		goto next_rx_no_prod_no_len;
2202 
2203 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2204 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2205 				   (struct rx_tpa_end_cmp *)rxcmp,
2206 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2207 
2208 		if (IS_ERR(skb))
2209 			return -EBUSY;
2210 
2211 		rc = -ENOMEM;
2212 		if (likely(skb)) {
2213 			bnxt_deliver_skb(bp, bnapi, skb);
2214 			rc = 1;
2215 		}
2216 		*event |= BNXT_RX_EVENT;
2217 		goto next_rx_no_prod_no_len;
2218 	}
2219 
2220 	cons = rxcmp->rx_cmp_opaque;
2221 	if (unlikely(cons != rxr->rx_next_cons)) {
2222 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2223 
2224 		/* 0xffff is forced error, don't print it */
2225 		if (rxr->rx_next_cons != 0xffff)
2226 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2227 				    cons, rxr->rx_next_cons);
2228 		bnxt_sched_reset_rxr(bp, rxr);
2229 		if (rc1)
2230 			return rc1;
2231 		goto next_rx_no_prod_no_len;
2232 	}
2233 	rx_buf = &rxr->rx_buf_ring[cons];
2234 	data = rx_buf->data;
2235 	data_ptr = rx_buf->data_ptr;
2236 	prefetch(data_ptr);
2237 
2238 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2239 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2240 
2241 	if (agg_bufs) {
2242 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2243 			return -EBUSY;
2244 
2245 		cp_cons = NEXT_CMP(cp_cons);
2246 		*event |= BNXT_AGG_EVENT;
2247 	}
2248 	*event |= BNXT_RX_EVENT;
2249 
2250 	rx_buf->data = NULL;
2251 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2252 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2253 
2254 		bnxt_reuse_rx_data(rxr, cons, data);
2255 		if (agg_bufs)
2256 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2257 					       false);
2258 
2259 		rc = -EIO;
2260 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2261 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2262 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2263 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2264 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2265 						 rx_err);
2266 				bnxt_sched_reset_rxr(bp, rxr);
2267 			}
2268 		}
2269 		goto next_rx_no_len;
2270 	}
2271 
2272 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2273 	len = flags >> RX_CMP_LEN_SHIFT;
2274 	dma_addr = rx_buf->mapping;
2275 
2276 	if (bnxt_xdp_attached(bp, rxr)) {
2277 		bnxt_xdp.rxcmp = rxcmp;
2278 		bnxt_xdp.rxcmp1 = rxcmp1;
2279 		bnxt_xdp.cmp_type = cmp_type;
2280 
2281 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &bnxt_xdp.xdp);
2282 		if (agg_bufs) {
2283 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr,
2284 							       &bnxt_xdp.xdp,
2285 							       cp_cons,
2286 							       agg_bufs,
2287 							       false);
2288 			if (!frag_len)
2289 				goto oom_next_rx;
2290 
2291 		}
2292 		xdp_active = true;
2293 	}
2294 
2295 	if (xdp_active) {
2296 		if (bnxt_rx_xdp(bp, rxr, cons, &bnxt_xdp.xdp, data, &data_ptr,
2297 				&len, event)) {
2298 			rc = 1;
2299 			goto next_rx;
2300 		}
2301 		if (xdp_buff_has_frags(&bnxt_xdp.xdp)) {
2302 			sinfo = xdp_get_shared_info_from_buff(&bnxt_xdp.xdp);
2303 			agg_bufs = sinfo->nr_frags;
2304 		} else {
2305 			agg_bufs = 0;
2306 		}
2307 	}
2308 
2309 	if (len <= bp->rx_copybreak) {
2310 		if (!xdp_active)
2311 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2312 		else
2313 			skb = bnxt_copy_xdp(bnapi, &bnxt_xdp.xdp, len,
2314 					    dma_addr);
2315 		bnxt_reuse_rx_data(rxr, cons, data);
2316 		if (!skb) {
2317 			if (agg_bufs) {
2318 				if (!xdp_active)
2319 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2320 							       agg_bufs, false);
2321 				else
2322 					bnxt_xdp_buff_frags_free(rxr,
2323 								 &bnxt_xdp.xdp);
2324 			}
2325 			goto oom_next_rx;
2326 		}
2327 	} else {
2328 		u32 payload;
2329 
2330 		if (rx_buf->data_ptr == data_ptr)
2331 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2332 		else
2333 			payload = 0;
2334 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2335 				      payload | len);
2336 		if (!skb)
2337 			goto oom_next_rx;
2338 	}
2339 
2340 	if (agg_bufs) {
2341 		if (!xdp_active) {
2342 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2343 						      agg_bufs, false);
2344 			if (!skb)
2345 				goto oom_next_rx;
2346 		} else {
2347 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2348 						 rxr, &bnxt_xdp.xdp);
2349 			if (!skb) {
2350 				/* we should be able to free the old skb here */
2351 				bnxt_xdp_buff_frags_free(rxr, &bnxt_xdp.xdp);
2352 				goto oom_next_rx;
2353 			}
2354 		}
2355 	}
2356 
2357 	if (RX_CMP_HASH_VALID(rxcmp)) {
2358 		enum pkt_hash_types type;
2359 
2360 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2361 			type = bnxt_rss_ext_op(bp, rxcmp);
2362 		} else {
2363 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2364 
2365 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2366 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2367 				type = PKT_HASH_TYPE_L4;
2368 			else
2369 				type = PKT_HASH_TYPE_L3;
2370 		}
2371 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2372 	}
2373 
2374 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2375 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2376 	skb->protocol = eth_type_trans(skb, dev);
2377 
2378 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2379 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2380 		if (!skb)
2381 			goto next_rx;
2382 	}
2383 
2384 	skb_checksum_none_assert(skb);
2385 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2386 		if (dev->features & NETIF_F_RXCSUM) {
2387 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2388 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2389 		}
2390 	} else {
2391 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2392 			if (dev->features & NETIF_F_RXCSUM)
2393 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2394 		}
2395 	}
2396 
2397 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2398 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2399 			u64 ns, ts;
2400 
2401 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2402 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2403 
2404 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2405 				memset(skb_hwtstamps(skb), 0,
2406 				       sizeof(*skb_hwtstamps(skb)));
2407 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2408 			}
2409 		}
2410 	}
2411 	bnxt_deliver_skb(bp, bnapi, skb);
2412 	rc = 1;
2413 
2414 next_rx:
2415 	cpr->rx_packets += 1;
2416 	cpr->rx_bytes += len;
2417 
2418 next_rx_no_len:
2419 	rxr->rx_prod = NEXT_RX(prod);
2420 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2421 
2422 next_rx_no_prod_no_len:
2423 	*raw_cons = tmp_raw_cons;
2424 
2425 	return rc;
2426 
2427 oom_next_rx:
2428 	cpr->sw_stats->rx.rx_oom_discards += 1;
2429 	rc = -ENOMEM;
2430 	goto next_rx;
2431 }
2432 
2433 /* In netpoll mode, if we are using a combined completion ring, we need to
2434  * discard the rx packets and recycle the buffers.
2435  */
2436 static int bnxt_force_rx_discard(struct bnxt *bp,
2437 				 struct bnxt_cp_ring_info *cpr,
2438 				 u32 *raw_cons, u8 *event)
2439 {
2440 	u32 tmp_raw_cons = *raw_cons;
2441 	struct rx_cmp_ext *rxcmp1;
2442 	struct rx_cmp *rxcmp;
2443 	u16 cp_cons;
2444 	u8 cmp_type;
2445 	int rc;
2446 
2447 	cp_cons = RING_CMP(tmp_raw_cons);
2448 	rxcmp = (struct rx_cmp *)
2449 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2450 
2451 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2452 	cp_cons = RING_CMP(tmp_raw_cons);
2453 	rxcmp1 = (struct rx_cmp_ext *)
2454 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2455 
2456 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2457 		return -EBUSY;
2458 
2459 	/* The valid test of the entry must be done first before
2460 	 * reading any further.
2461 	 */
2462 	dma_rmb();
2463 	cmp_type = RX_CMP_TYPE(rxcmp);
2464 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2465 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2466 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2467 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2468 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2469 		struct rx_tpa_end_cmp_ext *tpa_end1;
2470 
2471 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2472 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2473 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2474 	}
2475 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2476 	if (rc && rc != -EBUSY)
2477 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2478 	return rc;
2479 }
2480 
2481 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2482 {
2483 	struct bnxt_fw_health *fw_health = bp->fw_health;
2484 	u32 reg = fw_health->regs[reg_idx];
2485 	u32 reg_type, reg_off, val = 0;
2486 
2487 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2488 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2489 	switch (reg_type) {
2490 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2491 		pci_read_config_dword(bp->pdev, reg_off, &val);
2492 		break;
2493 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2494 		reg_off = fw_health->mapped_regs[reg_idx];
2495 		fallthrough;
2496 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2497 		val = readl(bp->bar0 + reg_off);
2498 		break;
2499 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2500 		val = readl(bp->bar1 + reg_off);
2501 		break;
2502 	}
2503 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2504 		val &= fw_health->fw_reset_inprog_reg_mask;
2505 	return val;
2506 }
2507 
2508 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2509 {
2510 	int i;
2511 
2512 	for (i = 0; i < bp->rx_nr_rings; i++) {
2513 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2514 		struct bnxt_ring_grp_info *grp_info;
2515 
2516 		grp_info = &bp->grp_info[grp_idx];
2517 		if (grp_info->agg_fw_ring_id == ring_id)
2518 			return grp_idx;
2519 	}
2520 	return INVALID_HW_RING_ID;
2521 }
2522 
2523 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2524 {
2525 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2526 
2527 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2528 		return link_info->force_link_speed2;
2529 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2530 		return link_info->force_pam4_link_speed;
2531 	return link_info->force_link_speed;
2532 }
2533 
2534 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2535 {
2536 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2537 
2538 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2539 		link_info->req_link_speed = link_info->force_link_speed2;
2540 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2541 		switch (link_info->req_link_speed) {
2542 		case BNXT_LINK_SPEED_50GB_PAM4:
2543 		case BNXT_LINK_SPEED_100GB_PAM4:
2544 		case BNXT_LINK_SPEED_200GB_PAM4:
2545 		case BNXT_LINK_SPEED_400GB_PAM4:
2546 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2547 			break;
2548 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2549 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2550 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2551 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2552 			break;
2553 		default:
2554 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2555 		}
2556 		return;
2557 	}
2558 	link_info->req_link_speed = link_info->force_link_speed;
2559 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2560 	if (link_info->force_pam4_link_speed) {
2561 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2562 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2563 	}
2564 }
2565 
2566 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2567 {
2568 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2569 
2570 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2571 		link_info->advertising = link_info->auto_link_speeds2;
2572 		return;
2573 	}
2574 	link_info->advertising = link_info->auto_link_speeds;
2575 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2576 }
2577 
2578 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2579 {
2580 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2581 
2582 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2583 		if (link_info->req_link_speed != link_info->force_link_speed2)
2584 			return true;
2585 		return false;
2586 	}
2587 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2588 	    link_info->req_link_speed != link_info->force_link_speed)
2589 		return true;
2590 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2591 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2592 		return true;
2593 	return false;
2594 }
2595 
2596 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2597 {
2598 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2599 
2600 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2601 		if (link_info->advertising != link_info->auto_link_speeds2)
2602 			return true;
2603 		return false;
2604 	}
2605 	if (link_info->advertising != link_info->auto_link_speeds ||
2606 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2607 		return true;
2608 	return false;
2609 }
2610 
2611 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2612 {
2613 	u32 flags = bp->ctx->ctx_arr[type].flags;
2614 
2615 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2616 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2617 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2618 }
2619 
2620 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2621 {
2622 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2623 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2624 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2625 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2626 	struct bnxt_bs_trace_info *bs_trace;
2627 	int last_pg;
2628 
2629 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2630 		return;
2631 
2632 	mem_size = ctxm->max_entries * ctxm->entry_size;
2633 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2634 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2635 
2636 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2637 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2638 
2639 	rmem = &ctx_pg[0].ring_mem;
2640 	bs_trace = &bp->bs_trace[trace_type];
2641 	bs_trace->ctx_type = ctxm->type;
2642 	bs_trace->trace_type = trace_type;
2643 	if (pages > MAX_CTX_PAGES) {
2644 		int last_pg_dir = rmem->nr_pages - 1;
2645 
2646 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2647 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2648 	} else {
2649 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2650 	}
2651 	bs_trace->magic_byte += magic_byte_offset;
2652 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2653 }
2654 
2655 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2656 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2657 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2658 
2659 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2660 	(((data2) &							\
2661 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2662 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2663 
2664 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2665 	((data2) &							\
2666 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2667 
2668 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2669 	(((data2) &							\
2670 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2671 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2672 
2673 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2674 	((data1) &							\
2675 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2676 
2677 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2678 	(((data1) &							\
2679 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2680 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2681 
2682 /* Return true if the workqueue has to be scheduled */
2683 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2684 {
2685 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2686 
2687 	switch (err_type) {
2688 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2689 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2690 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2691 		break;
2692 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2693 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2694 		break;
2695 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2696 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2697 		break;
2698 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2699 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2700 		char *threshold_type;
2701 		bool notify = false;
2702 		char *dir_str;
2703 
2704 		switch (type) {
2705 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2706 			threshold_type = "warning";
2707 			break;
2708 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2709 			threshold_type = "critical";
2710 			break;
2711 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2712 			threshold_type = "fatal";
2713 			break;
2714 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2715 			threshold_type = "shutdown";
2716 			break;
2717 		default:
2718 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2719 			return false;
2720 		}
2721 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2722 			dir_str = "above";
2723 			notify = true;
2724 		} else {
2725 			dir_str = "below";
2726 		}
2727 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2728 			    dir_str, threshold_type);
2729 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2730 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2731 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2732 		if (notify) {
2733 			bp->thermal_threshold_type = type;
2734 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2735 			return true;
2736 		}
2737 		return false;
2738 	}
2739 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2740 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2741 		break;
2742 	default:
2743 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2744 			   err_type);
2745 		break;
2746 	}
2747 	return false;
2748 }
2749 
2750 #define BNXT_GET_EVENT_PORT(data)	\
2751 	((data) &			\
2752 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2753 
2754 #define BNXT_EVENT_RING_TYPE(data2)	\
2755 	((data2) &			\
2756 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2757 
2758 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2759 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2760 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2761 
2762 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2763 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2764 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2765 
2766 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2767 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2768 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2769 
2770 #define BNXT_PHC_BITS	48
2771 
2772 static int bnxt_async_event_process(struct bnxt *bp,
2773 				    struct hwrm_async_event_cmpl *cmpl)
2774 {
2775 	u16 event_id = le16_to_cpu(cmpl->event_id);
2776 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2777 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2778 
2779 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2780 		   event_id, data1, data2);
2781 
2782 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2783 	switch (event_id) {
2784 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2785 		struct bnxt_link_info *link_info = &bp->link_info;
2786 
2787 		if (BNXT_VF(bp))
2788 			goto async_event_process_exit;
2789 
2790 		/* print unsupported speed warning in forced speed mode only */
2791 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2792 		    (data1 & 0x20000)) {
2793 			u16 fw_speed = bnxt_get_force_speed(link_info);
2794 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2795 
2796 			if (speed != SPEED_UNKNOWN)
2797 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2798 					    speed);
2799 		}
2800 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2801 	}
2802 		fallthrough;
2803 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2804 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2805 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2806 		fallthrough;
2807 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2808 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2809 		break;
2810 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2811 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2812 		break;
2813 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2814 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2815 
2816 		if (BNXT_VF(bp))
2817 			break;
2818 
2819 		if (bp->pf.port_id != port_id)
2820 			break;
2821 
2822 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2823 		break;
2824 	}
2825 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2826 		if (BNXT_PF(bp))
2827 			goto async_event_process_exit;
2828 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2829 		break;
2830 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2831 		char *type_str = "Solicited";
2832 
2833 		if (!bp->fw_health)
2834 			goto async_event_process_exit;
2835 
2836 		bp->fw_reset_timestamp = jiffies;
2837 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2838 		if (!bp->fw_reset_min_dsecs)
2839 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2840 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2841 		if (!bp->fw_reset_max_dsecs)
2842 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2843 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2844 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2845 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2846 			type_str = "Fatal";
2847 			bp->fw_health->fatalities++;
2848 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2849 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2850 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2851 			type_str = "Non-fatal";
2852 			bp->fw_health->survivals++;
2853 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2854 		}
2855 		netif_warn(bp, hw, bp->dev,
2856 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2857 			   type_str, data1, data2,
2858 			   bp->fw_reset_min_dsecs * 100,
2859 			   bp->fw_reset_max_dsecs * 100);
2860 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2861 		break;
2862 	}
2863 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2864 		struct bnxt_fw_health *fw_health = bp->fw_health;
2865 		char *status_desc = "healthy";
2866 		u32 status;
2867 
2868 		if (!fw_health)
2869 			goto async_event_process_exit;
2870 
2871 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2872 			fw_health->enabled = false;
2873 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2874 			break;
2875 		}
2876 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2877 		fw_health->tmr_multiplier =
2878 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2879 				     bp->current_interval * 10);
2880 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2881 		if (!fw_health->enabled)
2882 			fw_health->last_fw_heartbeat =
2883 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2884 		fw_health->last_fw_reset_cnt =
2885 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2886 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2887 		if (status != BNXT_FW_STATUS_HEALTHY)
2888 			status_desc = "unhealthy";
2889 		netif_info(bp, drv, bp->dev,
2890 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2891 			   fw_health->primary ? "primary" : "backup", status,
2892 			   status_desc, fw_health->last_fw_reset_cnt);
2893 		if (!fw_health->enabled) {
2894 			/* Make sure tmr_counter is set and visible to
2895 			 * bnxt_health_check() before setting enabled to true.
2896 			 */
2897 			smp_wmb();
2898 			fw_health->enabled = true;
2899 		}
2900 		goto async_event_process_exit;
2901 	}
2902 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2903 		netif_notice(bp, hw, bp->dev,
2904 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2905 			     data1, data2);
2906 		goto async_event_process_exit;
2907 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2908 		struct bnxt_rx_ring_info *rxr;
2909 		u16 grp_idx;
2910 
2911 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2912 			goto async_event_process_exit;
2913 
2914 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2915 			    BNXT_EVENT_RING_TYPE(data2), data1);
2916 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2917 			goto async_event_process_exit;
2918 
2919 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2920 		if (grp_idx == INVALID_HW_RING_ID) {
2921 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2922 				    data1);
2923 			goto async_event_process_exit;
2924 		}
2925 		rxr = bp->bnapi[grp_idx]->rx_ring;
2926 		bnxt_sched_reset_rxr(bp, rxr);
2927 		goto async_event_process_exit;
2928 	}
2929 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2930 		struct bnxt_fw_health *fw_health = bp->fw_health;
2931 
2932 		netif_notice(bp, hw, bp->dev,
2933 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2934 			     data1, data2);
2935 		if (fw_health) {
2936 			fw_health->echo_req_data1 = data1;
2937 			fw_health->echo_req_data2 = data2;
2938 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2939 			break;
2940 		}
2941 		goto async_event_process_exit;
2942 	}
2943 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2944 		bnxt_ptp_pps_event(bp, data1, data2);
2945 		goto async_event_process_exit;
2946 	}
2947 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2948 		if (bnxt_event_error_report(bp, data1, data2))
2949 			break;
2950 		goto async_event_process_exit;
2951 	}
2952 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2953 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2954 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2955 			if (BNXT_PTP_USE_RTC(bp)) {
2956 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2957 				unsigned long flags;
2958 				u64 ns;
2959 
2960 				if (!ptp)
2961 					goto async_event_process_exit;
2962 
2963 				bnxt_ptp_update_current_time(bp);
2964 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2965 				       BNXT_PHC_BITS) | ptp->current_time);
2966 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2967 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2968 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2969 			}
2970 			break;
2971 		}
2972 		goto async_event_process_exit;
2973 	}
2974 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2975 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2976 
2977 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2978 		goto async_event_process_exit;
2979 	}
2980 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2981 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2982 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2983 
2984 		if (type >= ARRAY_SIZE(bp->bs_trace))
2985 			goto async_event_process_exit;
2986 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2987 		goto async_event_process_exit;
2988 	}
2989 	default:
2990 		goto async_event_process_exit;
2991 	}
2992 	__bnxt_queue_sp_work(bp);
2993 async_event_process_exit:
2994 	bnxt_ulp_async_events(bp, cmpl);
2995 	return 0;
2996 }
2997 
2998 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2999 {
3000 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
3001 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
3002 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
3003 				(struct hwrm_fwd_req_cmpl *)txcmp;
3004 
3005 	switch (cmpl_type) {
3006 	case CMPL_BASE_TYPE_HWRM_DONE:
3007 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
3008 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
3009 		break;
3010 
3011 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
3012 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
3013 
3014 		if ((vf_id < bp->pf.first_vf_id) ||
3015 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
3016 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
3017 				   vf_id);
3018 			return -EINVAL;
3019 		}
3020 
3021 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
3022 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
3023 		break;
3024 
3025 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
3026 		bnxt_async_event_process(bp,
3027 					 (struct hwrm_async_event_cmpl *)txcmp);
3028 		break;
3029 
3030 	default:
3031 		break;
3032 	}
3033 
3034 	return 0;
3035 }
3036 
3037 static bool bnxt_vnic_is_active(struct bnxt *bp)
3038 {
3039 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
3040 
3041 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
3042 }
3043 
3044 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
3045 {
3046 	struct bnxt_napi *bnapi = dev_instance;
3047 	struct bnxt *bp = bnapi->bp;
3048 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3049 	u32 cons = RING_CMP(cpr->cp_raw_cons);
3050 
3051 	cpr->event_ctr++;
3052 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
3053 	napi_schedule(&bnapi->napi);
3054 	return IRQ_HANDLED;
3055 }
3056 
3057 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
3058 {
3059 	u32 raw_cons = cpr->cp_raw_cons;
3060 	u16 cons = RING_CMP(raw_cons);
3061 	struct tx_cmp *txcmp;
3062 
3063 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3064 
3065 	return TX_CMP_VALID(txcmp, raw_cons);
3066 }
3067 
3068 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3069 			    int budget)
3070 {
3071 	struct bnxt_napi *bnapi = cpr->bnapi;
3072 	u32 raw_cons = cpr->cp_raw_cons;
3073 	bool flush_xdp = false;
3074 	u32 cons;
3075 	int rx_pkts = 0;
3076 	u8 event = 0;
3077 	struct tx_cmp *txcmp;
3078 
3079 	cpr->has_more_work = 0;
3080 	cpr->had_work_done = 1;
3081 	while (1) {
3082 		u8 cmp_type;
3083 		int rc;
3084 
3085 		cons = RING_CMP(raw_cons);
3086 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3087 
3088 		if (!TX_CMP_VALID(txcmp, raw_cons))
3089 			break;
3090 
3091 		/* The valid test of the entry must be done first before
3092 		 * reading any further.
3093 		 */
3094 		dma_rmb();
3095 		cmp_type = TX_CMP_TYPE(txcmp);
3096 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3097 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3098 			u32 opaque = txcmp->tx_cmp_opaque;
3099 			struct bnxt_tx_ring_info *txr;
3100 			u16 tx_freed;
3101 
3102 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3103 			event |= BNXT_TX_CMP_EVENT;
3104 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3105 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3106 			else
3107 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3108 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3109 				   bp->tx_ring_mask;
3110 			/* return full budget so NAPI will complete. */
3111 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3112 				rx_pkts = budget;
3113 				raw_cons = NEXT_RAW_CMP(raw_cons);
3114 				if (budget)
3115 					cpr->has_more_work = 1;
3116 				break;
3117 			}
3118 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3119 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3120 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3121 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3122 			if (likely(budget))
3123 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3124 			else
3125 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3126 							   &event);
3127 			if (event & BNXT_REDIRECT_EVENT)
3128 				flush_xdp = true;
3129 			if (likely(rc >= 0))
3130 				rx_pkts += rc;
3131 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3132 			 * the NAPI budget.  Otherwise, we may potentially loop
3133 			 * here forever if we consistently cannot allocate
3134 			 * buffers.
3135 			 */
3136 			else if (rc == -ENOMEM && budget)
3137 				rx_pkts++;
3138 			else if (rc == -EBUSY)	/* partial completion */
3139 				break;
3140 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3141 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3142 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3143 			bnxt_hwrm_handler(bp, txcmp);
3144 		}
3145 		raw_cons = NEXT_RAW_CMP(raw_cons);
3146 
3147 		if (rx_pkts && rx_pkts == budget) {
3148 			cpr->has_more_work = 1;
3149 			break;
3150 		}
3151 	}
3152 
3153 	if (flush_xdp) {
3154 		xdp_do_flush();
3155 		event &= ~BNXT_REDIRECT_EVENT;
3156 	}
3157 
3158 	if (event & BNXT_TX_EVENT) {
3159 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3160 		u16 prod = txr->tx_prod;
3161 
3162 		/* Sync BD data before updating doorbell */
3163 		wmb();
3164 
3165 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3166 		event &= ~BNXT_TX_EVENT;
3167 	}
3168 
3169 	cpr->cp_raw_cons = raw_cons;
3170 	bnapi->events |= event;
3171 	return rx_pkts;
3172 }
3173 
3174 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3175 				  int budget)
3176 {
3177 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3178 		bnapi->tx_int(bp, bnapi, budget);
3179 
3180 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3181 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3182 
3183 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3184 		bnapi->events &= ~BNXT_RX_EVENT;
3185 	}
3186 	if (bnapi->events & BNXT_AGG_EVENT) {
3187 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3188 
3189 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3190 		bnapi->events &= ~BNXT_AGG_EVENT;
3191 	}
3192 }
3193 
3194 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3195 			  int budget)
3196 {
3197 	struct bnxt_napi *bnapi = cpr->bnapi;
3198 	int rx_pkts;
3199 
3200 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3201 
3202 	/* ACK completion ring before freeing tx ring and producing new
3203 	 * buffers in rx/agg rings to prevent overflowing the completion
3204 	 * ring.
3205 	 */
3206 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3207 
3208 	__bnxt_poll_work_done(bp, bnapi, budget);
3209 	return rx_pkts;
3210 }
3211 
3212 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3213 {
3214 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3215 	struct bnxt *bp = bnapi->bp;
3216 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3217 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3218 	struct tx_cmp *txcmp;
3219 	struct rx_cmp_ext *rxcmp1;
3220 	u32 cp_cons, tmp_raw_cons;
3221 	u32 raw_cons = cpr->cp_raw_cons;
3222 	bool flush_xdp = false;
3223 	u32 rx_pkts = 0;
3224 	u8 event = 0;
3225 
3226 	while (1) {
3227 		int rc;
3228 
3229 		cp_cons = RING_CMP(raw_cons);
3230 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3231 
3232 		if (!TX_CMP_VALID(txcmp, raw_cons))
3233 			break;
3234 
3235 		/* The valid test of the entry must be done first before
3236 		 * reading any further.
3237 		 */
3238 		dma_rmb();
3239 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3240 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3241 			cp_cons = RING_CMP(tmp_raw_cons);
3242 			rxcmp1 = (struct rx_cmp_ext *)
3243 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3244 
3245 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3246 				break;
3247 
3248 			/* force an error to recycle the buffer */
3249 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3250 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3251 
3252 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3253 			if (likely(rc == -EIO) && budget)
3254 				rx_pkts++;
3255 			else if (rc == -EBUSY)	/* partial completion */
3256 				break;
3257 			if (event & BNXT_REDIRECT_EVENT)
3258 				flush_xdp = true;
3259 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3260 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3261 			bnxt_hwrm_handler(bp, txcmp);
3262 		} else {
3263 			netdev_err(bp->dev,
3264 				   "Invalid completion received on special ring\n");
3265 		}
3266 		raw_cons = NEXT_RAW_CMP(raw_cons);
3267 
3268 		if (rx_pkts == budget)
3269 			break;
3270 	}
3271 
3272 	cpr->cp_raw_cons = raw_cons;
3273 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3274 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3275 
3276 	if (event & BNXT_AGG_EVENT)
3277 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3278 	if (flush_xdp)
3279 		xdp_do_flush();
3280 
3281 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3282 		napi_complete_done(napi, rx_pkts);
3283 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3284 	}
3285 	return rx_pkts;
3286 }
3287 
3288 static int bnxt_poll(struct napi_struct *napi, int budget)
3289 {
3290 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3291 	struct bnxt *bp = bnapi->bp;
3292 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3293 	int work_done = 0;
3294 
3295 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3296 		napi_complete(napi);
3297 		return 0;
3298 	}
3299 	while (1) {
3300 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3301 
3302 		if (work_done >= budget) {
3303 			if (!budget)
3304 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3305 			break;
3306 		}
3307 
3308 		if (!bnxt_has_work(bp, cpr)) {
3309 			if (napi_complete_done(napi, work_done))
3310 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3311 			break;
3312 		}
3313 	}
3314 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3315 		struct dim_sample dim_sample = {};
3316 
3317 		dim_update_sample(cpr->event_ctr,
3318 				  cpr->rx_packets,
3319 				  cpr->rx_bytes,
3320 				  &dim_sample);
3321 		net_dim(&cpr->dim, &dim_sample);
3322 	}
3323 	return work_done;
3324 }
3325 
3326 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3327 {
3328 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3329 	int i, work_done = 0;
3330 
3331 	for (i = 0; i < cpr->cp_ring_count; i++) {
3332 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3333 
3334 		if (cpr2->had_nqe_notify) {
3335 			work_done += __bnxt_poll_work(bp, cpr2,
3336 						      budget - work_done);
3337 			cpr->has_more_work |= cpr2->has_more_work;
3338 		}
3339 	}
3340 	return work_done;
3341 }
3342 
3343 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3344 				 u64 dbr_type, int budget)
3345 {
3346 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3347 	int i;
3348 
3349 	for (i = 0; i < cpr->cp_ring_count; i++) {
3350 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3351 		struct bnxt_db_info *db;
3352 
3353 		if (cpr2->had_work_done) {
3354 			u32 tgl = 0;
3355 
3356 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3357 				cpr2->had_nqe_notify = 0;
3358 				tgl = cpr2->toggle;
3359 			}
3360 			db = &cpr2->cp_db;
3361 			bnxt_writeq(bp,
3362 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3363 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3364 				    db->doorbell);
3365 			cpr2->had_work_done = 0;
3366 		}
3367 	}
3368 	__bnxt_poll_work_done(bp, bnapi, budget);
3369 }
3370 
3371 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3372 {
3373 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3374 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3375 	struct bnxt_cp_ring_info *cpr_rx;
3376 	u32 raw_cons = cpr->cp_raw_cons;
3377 	struct bnxt *bp = bnapi->bp;
3378 	struct nqe_cn *nqcmp;
3379 	int work_done = 0;
3380 	u32 cons;
3381 
3382 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3383 		napi_complete(napi);
3384 		return 0;
3385 	}
3386 	if (cpr->has_more_work) {
3387 		cpr->has_more_work = 0;
3388 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3389 	}
3390 	while (1) {
3391 		u16 type;
3392 
3393 		cons = RING_CMP(raw_cons);
3394 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3395 
3396 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3397 			if (cpr->has_more_work)
3398 				break;
3399 
3400 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3401 					     budget);
3402 			cpr->cp_raw_cons = raw_cons;
3403 			if (napi_complete_done(napi, work_done))
3404 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3405 						  cpr->cp_raw_cons);
3406 			goto poll_done;
3407 		}
3408 
3409 		/* The valid test of the entry must be done first before
3410 		 * reading any further.
3411 		 */
3412 		dma_rmb();
3413 
3414 		type = le16_to_cpu(nqcmp->type);
3415 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3416 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3417 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3418 			struct bnxt_cp_ring_info *cpr2;
3419 
3420 			/* No more budget for RX work */
3421 			if (budget && work_done >= budget &&
3422 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3423 				break;
3424 
3425 			idx = BNXT_NQ_HDL_IDX(idx);
3426 			cpr2 = &cpr->cp_ring_arr[idx];
3427 			cpr2->had_nqe_notify = 1;
3428 			cpr2->toggle = NQE_CN_TOGGLE(type);
3429 			work_done += __bnxt_poll_work(bp, cpr2,
3430 						      budget - work_done);
3431 			cpr->has_more_work |= cpr2->has_more_work;
3432 		} else {
3433 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3434 		}
3435 		raw_cons = NEXT_RAW_CMP(raw_cons);
3436 	}
3437 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3438 	if (raw_cons != cpr->cp_raw_cons) {
3439 		cpr->cp_raw_cons = raw_cons;
3440 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3441 	}
3442 poll_done:
3443 	cpr_rx = &cpr->cp_ring_arr[0];
3444 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3445 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3446 		struct dim_sample dim_sample = {};
3447 
3448 		dim_update_sample(cpr->event_ctr,
3449 				  cpr_rx->rx_packets,
3450 				  cpr_rx->rx_bytes,
3451 				  &dim_sample);
3452 		net_dim(&cpr->dim, &dim_sample);
3453 	}
3454 	return work_done;
3455 }
3456 
3457 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3458 				       struct bnxt_tx_ring_info *txr, int idx)
3459 {
3460 	int i, max_idx;
3461 	struct pci_dev *pdev = bp->pdev;
3462 	unsigned int dma_len;
3463 	dma_addr_t dma_addr;
3464 
3465 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3466 
3467 	for (i = 0; i < max_idx;) {
3468 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3469 		struct bnxt_sw_tx_bd *head_buf = tx_buf;
3470 		struct sk_buff *skb;
3471 		int j, last;
3472 
3473 		if (idx  < bp->tx_nr_rings_xdp &&
3474 		    tx_buf->action == XDP_REDIRECT) {
3475 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3476 			dma_len = dma_unmap_len(tx_buf, len);
3477 
3478 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3479 					 DMA_TO_DEVICE);
3480 			xdp_return_frame(tx_buf->xdpf);
3481 			tx_buf->action = 0;
3482 			tx_buf->xdpf = NULL;
3483 			i++;
3484 			continue;
3485 		}
3486 
3487 		skb = tx_buf->skb;
3488 		if (!skb) {
3489 			i++;
3490 			continue;
3491 		}
3492 
3493 		tx_buf->skb = NULL;
3494 
3495 		if (tx_buf->is_push) {
3496 			dev_kfree_skb(skb);
3497 			i += 2;
3498 			continue;
3499 		}
3500 
3501 		if (dma_unmap_len(tx_buf, len)) {
3502 			dma_addr = dma_unmap_addr(tx_buf, mapping);
3503 			dma_len = dma_unmap_len(tx_buf, len);
3504 
3505 			dma_unmap_single(&pdev->dev, dma_addr, dma_len,
3506 					 DMA_TO_DEVICE);
3507 		}
3508 
3509 		last = tx_buf->nr_frags;
3510 		i += 2;
3511 		for (j = 0; j < last; j++, i++) {
3512 			int ring_idx = i & bp->tx_ring_mask;
3513 
3514 			tx_buf = &txr->tx_buf_ring[ring_idx];
3515 			if (dma_unmap_len(tx_buf, len)) {
3516 				dma_addr = dma_unmap_addr(tx_buf, mapping);
3517 				dma_len = dma_unmap_len(tx_buf, len);
3518 
3519 				netmem_dma_unmap_page_attrs(&pdev->dev,
3520 							    dma_addr, dma_len,
3521 							    DMA_TO_DEVICE, 0);
3522 			}
3523 		}
3524 		if (head_buf->is_sw_gso) {
3525 			u16 inline_cons = txr->tx_inline_cons + 1;
3526 
3527 			WRITE_ONCE(txr->tx_inline_cons, inline_cons);
3528 			if (head_buf->is_sw_gso == BNXT_SW_GSO_LAST) {
3529 				tso_dma_map_complete(&pdev->dev,
3530 						     &head_buf->sw_gso_cstate);
3531 			} else {
3532 				skb = NULL;
3533 			}
3534 			head_buf->is_sw_gso = 0;
3535 		}
3536 		if (skb)
3537 			dev_kfree_skb(skb);
3538 	}
3539 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3540 }
3541 
3542 static void bnxt_free_tx_skbs(struct bnxt *bp)
3543 {
3544 	int i;
3545 
3546 	if (!bp->tx_ring)
3547 		return;
3548 
3549 	for (i = 0; i < bp->tx_nr_rings; i++) {
3550 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3551 
3552 		if (!txr->tx_buf_ring)
3553 			continue;
3554 
3555 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3556 	}
3557 
3558 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3559 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3560 }
3561 
3562 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3563 {
3564 	int i, max_idx;
3565 
3566 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3567 
3568 	for (i = 0; i < max_idx; i++) {
3569 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3570 		void *data = rx_buf->data;
3571 
3572 		if (!data)
3573 			continue;
3574 
3575 		rx_buf->data = NULL;
3576 		if (BNXT_RX_PAGE_MODE(bp))
3577 			page_pool_recycle_direct(rxr->page_pool, data);
3578 		else
3579 			page_pool_free_va(rxr->head_pool, data, true);
3580 	}
3581 }
3582 
3583 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3584 {
3585 	int i, max_idx;
3586 
3587 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3588 
3589 	for (i = 0; i < max_idx; i++) {
3590 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3591 		netmem_ref netmem = rx_agg_buf->netmem;
3592 
3593 		if (!netmem)
3594 			continue;
3595 
3596 		rx_agg_buf->netmem = 0;
3597 		__clear_bit(i, rxr->rx_agg_bmap);
3598 
3599 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3600 	}
3601 }
3602 
3603 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3604 					struct bnxt_rx_ring_info *rxr)
3605 {
3606 	int i;
3607 
3608 	for (i = 0; i < bp->max_tpa_roundup_size; i++) {
3609 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3610 		u8 *data = tpa_info->data;
3611 
3612 		if (!data)
3613 			continue;
3614 
3615 		tpa_info->data = NULL;
3616 		page_pool_free_va(rxr->head_pool, data, false);
3617 	}
3618 }
3619 
3620 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3621 				       struct bnxt_rx_ring_info *rxr)
3622 {
3623 	struct bnxt_tpa_idx_map *map;
3624 
3625 	if (!rxr->rx_tpa)
3626 		goto skip_rx_tpa_free;
3627 
3628 	bnxt_free_one_tpa_info_data(bp, rxr);
3629 
3630 skip_rx_tpa_free:
3631 	if (!rxr->rx_buf_ring)
3632 		goto skip_rx_buf_free;
3633 
3634 	bnxt_free_one_rx_ring(bp, rxr);
3635 
3636 skip_rx_buf_free:
3637 	if (!rxr->rx_agg_ring)
3638 		goto skip_rx_agg_free;
3639 
3640 	bnxt_free_one_rx_agg_ring(bp, rxr);
3641 
3642 skip_rx_agg_free:
3643 	map = rxr->rx_tpa_idx_map;
3644 	if (map)
3645 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3646 }
3647 
3648 static void bnxt_free_rx_skbs(struct bnxt *bp)
3649 {
3650 	int i;
3651 
3652 	if (!bp->rx_ring)
3653 		return;
3654 
3655 	for (i = 0; i < bp->rx_nr_rings; i++)
3656 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3657 }
3658 
3659 static void bnxt_free_skbs(struct bnxt *bp)
3660 {
3661 	bnxt_free_tx_skbs(bp);
3662 	bnxt_free_rx_skbs(bp);
3663 }
3664 
3665 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3666 {
3667 	u8 init_val = ctxm->init_value;
3668 	u16 offset = ctxm->init_offset;
3669 	u8 *p2 = p;
3670 	int i;
3671 
3672 	if (!init_val)
3673 		return;
3674 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3675 		memset(p, init_val, len);
3676 		return;
3677 	}
3678 	for (i = 0; i < len; i += ctxm->entry_size)
3679 		*(p2 + i + offset) = init_val;
3680 }
3681 
3682 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3683 			       void *buf, size_t offset, size_t head,
3684 			       size_t tail)
3685 {
3686 	int i, head_page, start_idx, source_offset;
3687 	size_t len, rem_len, total_len, max_bytes;
3688 
3689 	head_page = head / rmem->page_size;
3690 	source_offset = head % rmem->page_size;
3691 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3692 	if (!total_len)
3693 		total_len = MAX_CTX_BYTES;
3694 	start_idx = head_page % MAX_CTX_PAGES;
3695 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3696 		    source_offset;
3697 	total_len = min(total_len, max_bytes);
3698 	rem_len = total_len;
3699 
3700 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3701 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3702 		if (buf)
3703 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3704 			       len);
3705 		offset += len;
3706 		rem_len -= len;
3707 	}
3708 	return total_len;
3709 }
3710 
3711 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3712 {
3713 	struct pci_dev *pdev = bp->pdev;
3714 	int i;
3715 
3716 	if (!rmem->pg_arr)
3717 		goto skip_pages;
3718 
3719 	for (i = 0; i < rmem->nr_pages; i++) {
3720 		if (!rmem->pg_arr[i])
3721 			continue;
3722 
3723 		dma_free_coherent(&pdev->dev, rmem->page_size,
3724 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3725 
3726 		rmem->pg_arr[i] = NULL;
3727 	}
3728 skip_pages:
3729 	if (rmem->pg_tbl) {
3730 		size_t pg_tbl_size = rmem->nr_pages * 8;
3731 
3732 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3733 			pg_tbl_size = rmem->page_size;
3734 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3735 				  rmem->pg_tbl, rmem->pg_tbl_map);
3736 		rmem->pg_tbl = NULL;
3737 	}
3738 	if (rmem->vmem_size && *rmem->vmem) {
3739 		vfree(*rmem->vmem);
3740 		*rmem->vmem = NULL;
3741 	}
3742 }
3743 
3744 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3745 {
3746 	struct pci_dev *pdev = bp->pdev;
3747 	u64 valid_bit = 0;
3748 	int i;
3749 
3750 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3751 		valid_bit = PTU_PTE_VALID;
3752 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3753 		size_t pg_tbl_size = rmem->nr_pages * 8;
3754 
3755 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3756 			pg_tbl_size = rmem->page_size;
3757 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3758 						  &rmem->pg_tbl_map,
3759 						  GFP_KERNEL);
3760 		if (!rmem->pg_tbl)
3761 			return -ENOMEM;
3762 	}
3763 
3764 	for (i = 0; i < rmem->nr_pages; i++) {
3765 		u64 extra_bits = valid_bit;
3766 
3767 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3768 						     rmem->page_size,
3769 						     &rmem->dma_arr[i],
3770 						     GFP_KERNEL);
3771 		if (!rmem->pg_arr[i])
3772 			return -ENOMEM;
3773 
3774 		if (rmem->ctx_mem)
3775 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3776 					  rmem->page_size);
3777 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3778 			if (i == rmem->nr_pages - 2 &&
3779 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3780 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3781 			else if (i == rmem->nr_pages - 1 &&
3782 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3783 				extra_bits |= PTU_PTE_LAST;
3784 			rmem->pg_tbl[i] =
3785 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3786 		}
3787 	}
3788 
3789 	if (rmem->vmem_size) {
3790 		*rmem->vmem = vzalloc(rmem->vmem_size);
3791 		if (!(*rmem->vmem))
3792 			return -ENOMEM;
3793 	}
3794 	return 0;
3795 }
3796 
3797 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3798 				   struct bnxt_rx_ring_info *rxr)
3799 {
3800 	int i;
3801 
3802 	kfree(rxr->rx_tpa_idx_map);
3803 	rxr->rx_tpa_idx_map = NULL;
3804 	if (rxr->rx_tpa) {
3805 		for (i = 0; i < bp->max_tpa_roundup_size; i++) {
3806 			kfree(rxr->rx_tpa[i].agg_arr);
3807 			rxr->rx_tpa[i].agg_arr = NULL;
3808 		}
3809 	}
3810 	kfree(rxr->rx_tpa);
3811 	rxr->rx_tpa = NULL;
3812 }
3813 
3814 static void bnxt_free_tpa_info(struct bnxt *bp)
3815 {
3816 	int i;
3817 
3818 	for (i = 0; i < bp->rx_nr_rings; i++) {
3819 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3820 
3821 		bnxt_free_one_tpa_info(bp, rxr);
3822 	}
3823 }
3824 
3825 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3826 				   struct bnxt_rx_ring_info *rxr)
3827 {
3828 	struct rx_agg_cmp *agg;
3829 	int i;
3830 
3831 	rxr->rx_tpa = kzalloc_objs(struct bnxt_tpa_info,
3832 				   bp->max_tpa_roundup_size);
3833 	if (!rxr->rx_tpa)
3834 		return -ENOMEM;
3835 
3836 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3837 		return 0;
3838 	for (i = 0; i < bp->max_tpa_roundup_size; i++) {
3839 		agg = kzalloc_objs(*agg, MAX_SKB_FRAGS);
3840 		if (!agg)
3841 			return -ENOMEM;
3842 		rxr->rx_tpa[i].agg_arr = agg;
3843 	}
3844 	rxr->rx_tpa_idx_map = kzalloc_obj(*rxr->rx_tpa_idx_map);
3845 	if (!rxr->rx_tpa_idx_map)
3846 		return -ENOMEM;
3847 
3848 	return 0;
3849 }
3850 
3851 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3852 {
3853 	int i, rc;
3854 
3855 	bp->max_tpa = MAX_TPA;
3856 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3857 		/* TPA is not supported at all, so there is nothing to
3858 		 * allocate.
3859 		 */
3860 		if (!bp->max_tpa_v2)
3861 			return 0;
3862 		bp->max_tpa = min_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3863 		/* Older P5 FW sets max_tpa_v2 low by mistake except NPAR */
3864 		if (bp->max_tpa <= 32 && BNXT_CHIP_P5(bp) && !BNXT_NPAR(bp))
3865 			bp->max_tpa = MAX_TPA_P5;
3866 	}
3867 	bp->max_tpa_roundup_size = roundup_pow_of_two(bp->max_tpa);
3868 
3869 	for (i = 0; i < bp->rx_nr_rings; i++) {
3870 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3871 
3872 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3873 		if (rc)
3874 			return rc;
3875 	}
3876 	return 0;
3877 }
3878 
3879 static void bnxt_free_rx_rings(struct bnxt *bp)
3880 {
3881 	int i;
3882 
3883 	if (!bp->rx_ring)
3884 		return;
3885 
3886 	bnxt_free_tpa_info(bp);
3887 	for (i = 0; i < bp->rx_nr_rings; i++) {
3888 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3889 		struct bnxt_ring_struct *ring;
3890 
3891 		if (rxr->xdp_prog)
3892 			bpf_prog_put(rxr->xdp_prog);
3893 
3894 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3895 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3896 
3897 		page_pool_destroy(rxr->page_pool);
3898 		page_pool_destroy(rxr->head_pool);
3899 		rxr->page_pool = rxr->head_pool = NULL;
3900 
3901 		kfree(rxr->rx_agg_bmap);
3902 		rxr->rx_agg_bmap = NULL;
3903 
3904 		ring = &rxr->rx_ring_struct;
3905 		bnxt_free_ring(bp, &ring->ring_mem);
3906 
3907 		ring = &rxr->rx_agg_ring_struct;
3908 		bnxt_free_ring(bp, &ring->ring_mem);
3909 	}
3910 }
3911 
3912 static int bnxt_rx_agg_ring_fill_level(struct bnxt *bp,
3913 				       struct bnxt_rx_ring_info *rxr)
3914 {
3915 	/* User may have chosen larger than default rx_page_size,
3916 	 * we keep the ring sizes uniform and also want uniform amount
3917 	 * of bytes consumed per ring, so cap how much of the rings we fill.
3918 	 */
3919 	int fill_level = bp->rx_agg_ring_size;
3920 
3921 	if (rxr->rx_page_size > BNXT_RX_PAGE_SIZE)
3922 		fill_level /= rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3923 
3924 	return fill_level;
3925 }
3926 
3927 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3928 				   struct bnxt_rx_ring_info *rxr,
3929 				   int numa_node)
3930 {
3931 	unsigned int agg_size_fac = rxr->rx_page_size / BNXT_RX_PAGE_SIZE;
3932 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3933 	struct page_pool_params pp = { 0 };
3934 	struct page_pool *pool;
3935 
3936 	pp.pool_size = bnxt_rx_agg_ring_fill_level(bp, rxr) / agg_size_fac;
3937 	if (BNXT_RX_PAGE_MODE(bp))
3938 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3939 
3940 	pp.order = get_order(rxr->rx_page_size);
3941 	pp.nid = numa_node;
3942 	pp.netdev = bp->dev;
3943 	pp.dev = &bp->pdev->dev;
3944 	pp.dma_dir = bp->rx_dir;
3945 	pp.max_len = PAGE_SIZE << pp.order;
3946 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3947 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3948 	pp.queue_idx = rxr->bnapi->index;
3949 
3950 	pool = page_pool_create(&pp);
3951 	if (IS_ERR(pool))
3952 		return PTR_ERR(pool);
3953 	rxr->page_pool = pool;
3954 
3955 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3956 	rxr->need_head_pool |= !!pp.order;
3957 	if (bnxt_separate_head_pool(rxr)) {
3958 		pp.order = 0;
3959 		pp.max_len = PAGE_SIZE;
3960 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3961 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3962 		pool = page_pool_create(&pp);
3963 		if (IS_ERR(pool))
3964 			goto err_destroy_pp;
3965 	} else {
3966 		page_pool_get(pool);
3967 	}
3968 	rxr->head_pool = pool;
3969 
3970 	return 0;
3971 
3972 err_destroy_pp:
3973 	page_pool_destroy(rxr->page_pool);
3974 	rxr->page_pool = NULL;
3975 	return PTR_ERR(pool);
3976 }
3977 
3978 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3979 {
3980 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3981 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3982 }
3983 
3984 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3985 {
3986 	u16 mem_size;
3987 
3988 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3989 	mem_size = rxr->rx_agg_bmap_size / 8;
3990 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3991 	if (!rxr->rx_agg_bmap)
3992 		return -ENOMEM;
3993 
3994 	return 0;
3995 }
3996 
3997 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3998 {
3999 	int numa_node = dev_to_node(&bp->pdev->dev);
4000 	int i, rc = 0, agg_rings = 0, cpu;
4001 
4002 	if (!bp->rx_ring)
4003 		return -ENOMEM;
4004 
4005 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
4006 		agg_rings = 1;
4007 
4008 	for (i = 0; i < bp->rx_nr_rings; i++) {
4009 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
4010 		struct bnxt_ring_struct *ring;
4011 		int cpu_node;
4012 
4013 		ring = &rxr->rx_ring_struct;
4014 
4015 		cpu = cpumask_local_spread(i, numa_node);
4016 		cpu_node = cpu_to_node(cpu);
4017 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
4018 			   i, cpu_node);
4019 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
4020 		if (rc)
4021 			return rc;
4022 		bnxt_enable_rx_page_pool(rxr);
4023 
4024 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
4025 		if (rc < 0)
4026 			return rc;
4027 
4028 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
4029 						MEM_TYPE_PAGE_POOL,
4030 						rxr->page_pool);
4031 		if (rc) {
4032 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
4033 			return rc;
4034 		}
4035 
4036 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4037 		if (rc)
4038 			return rc;
4039 
4040 		ring->grp_idx = i;
4041 		if (agg_rings) {
4042 			ring = &rxr->rx_agg_ring_struct;
4043 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4044 			if (rc)
4045 				return rc;
4046 
4047 			ring->grp_idx = i;
4048 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
4049 			if (rc)
4050 				return rc;
4051 		}
4052 	}
4053 	if (bp->flags & BNXT_FLAG_TPA)
4054 		rc = bnxt_alloc_tpa_info(bp);
4055 	return rc;
4056 }
4057 
4058 static void bnxt_free_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4059 				    struct pci_dev *pdev)
4060 {
4061 	if (!txr->tx_inline_buf)
4062 		return;
4063 
4064 	dma_unmap_single(&pdev->dev, txr->tx_inline_dma,
4065 			 txr->tx_inline_size, DMA_TO_DEVICE);
4066 	kfree(txr->tx_inline_buf);
4067 	txr->tx_inline_buf = NULL;
4068 	txr->tx_inline_size = 0;
4069 }
4070 
4071 static int bnxt_alloc_tx_inline_buf(struct bnxt_tx_ring_info *txr,
4072 				    struct pci_dev *pdev,
4073 				    unsigned int size)
4074 {
4075 	txr->tx_inline_buf = kmalloc(size, GFP_KERNEL);
4076 	if (!txr->tx_inline_buf)
4077 		return -ENOMEM;
4078 
4079 	txr->tx_inline_dma = dma_map_single(&pdev->dev, txr->tx_inline_buf,
4080 					    size, DMA_TO_DEVICE);
4081 	if (dma_mapping_error(&pdev->dev, txr->tx_inline_dma)) {
4082 		kfree(txr->tx_inline_buf);
4083 		txr->tx_inline_buf = NULL;
4084 		return -ENOMEM;
4085 	}
4086 	txr->tx_inline_size = size;
4087 
4088 	return 0;
4089 }
4090 
4091 static void bnxt_free_tx_rings(struct bnxt *bp)
4092 {
4093 	int i;
4094 	struct pci_dev *pdev = bp->pdev;
4095 
4096 	if (!bp->tx_ring)
4097 		return;
4098 
4099 	for (i = 0; i < bp->tx_nr_rings; i++) {
4100 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4101 		struct bnxt_ring_struct *ring;
4102 
4103 		if (txr->tx_push) {
4104 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
4105 					  txr->tx_push, txr->tx_push_mapping);
4106 			txr->tx_push = NULL;
4107 		}
4108 
4109 		bnxt_free_tx_inline_buf(txr, pdev);
4110 
4111 		ring = &txr->tx_ring_struct;
4112 
4113 		bnxt_free_ring(bp, &ring->ring_mem);
4114 	}
4115 }
4116 
4117 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
4118 	((tc) * (bp)->tx_nr_rings_per_tc)
4119 
4120 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
4121 	((tx) % (bp)->tx_nr_rings_per_tc)
4122 
4123 #define BNXT_RING_TO_TC(bp, tx)		\
4124 	((tx) / (bp)->tx_nr_rings_per_tc)
4125 
4126 static int bnxt_alloc_tx_rings(struct bnxt *bp)
4127 {
4128 	int i, j, rc;
4129 	struct pci_dev *pdev = bp->pdev;
4130 
4131 	bp->tx_push_size = 0;
4132 	if (bp->tx_push_thresh) {
4133 		int push_size;
4134 
4135 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
4136 					bp->tx_push_thresh);
4137 
4138 		if (push_size > 256) {
4139 			push_size = 0;
4140 			bp->tx_push_thresh = 0;
4141 		}
4142 
4143 		bp->tx_push_size = push_size;
4144 	}
4145 
4146 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
4147 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4148 		struct bnxt_ring_struct *ring;
4149 		u8 qidx;
4150 
4151 		ring = &txr->tx_ring_struct;
4152 
4153 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4154 		if (rc)
4155 			return rc;
4156 
4157 		ring->grp_idx = txr->bnapi->index;
4158 		if (bp->tx_push_size) {
4159 			dma_addr_t mapping;
4160 
4161 			/* One pre-allocated DMA buffer to backup
4162 			 * TX push operation
4163 			 */
4164 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4165 						bp->tx_push_size,
4166 						&txr->tx_push_mapping,
4167 						GFP_KERNEL);
4168 
4169 			if (!txr->tx_push)
4170 				return -ENOMEM;
4171 
4172 			mapping = txr->tx_push_mapping +
4173 				sizeof(struct tx_push_bd);
4174 			txr->data_mapping = cpu_to_le64(mapping);
4175 		}
4176 		if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
4177 			rc = bnxt_alloc_tx_inline_buf(txr, pdev,
4178 						      BNXT_SW_USO_MAX_SEGS *
4179 						      TSO_HEADER_SIZE);
4180 			if (rc)
4181 				return rc;
4182 		}
4183 		qidx = bp->tc_to_qidx[j];
4184 		ring->queue_id = bp->q_info[qidx].queue_id;
4185 		spin_lock_init(&txr->xdp_tx_lock);
4186 		if (i < bp->tx_nr_rings_xdp)
4187 			continue;
4188 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4189 			j++;
4190 	}
4191 	return 0;
4192 }
4193 
4194 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4195 {
4196 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4197 
4198 	kfree(cpr->cp_desc_ring);
4199 	cpr->cp_desc_ring = NULL;
4200 	ring->ring_mem.pg_arr = NULL;
4201 	kfree(cpr->cp_desc_mapping);
4202 	cpr->cp_desc_mapping = NULL;
4203 	ring->ring_mem.dma_arr = NULL;
4204 }
4205 
4206 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4207 {
4208 	cpr->cp_desc_ring = kzalloc_objs(*cpr->cp_desc_ring, n);
4209 	if (!cpr->cp_desc_ring)
4210 		return -ENOMEM;
4211 	cpr->cp_desc_mapping = kzalloc_objs(*cpr->cp_desc_mapping, n);
4212 	if (!cpr->cp_desc_mapping)
4213 		return -ENOMEM;
4214 	return 0;
4215 }
4216 
4217 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4218 {
4219 	int i;
4220 
4221 	if (!bp->bnapi)
4222 		return;
4223 	for (i = 0; i < bp->cp_nr_rings; i++) {
4224 		struct bnxt_napi *bnapi = bp->bnapi[i];
4225 
4226 		if (!bnapi)
4227 			continue;
4228 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4229 	}
4230 }
4231 
4232 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4233 {
4234 	int i, n = bp->cp_nr_pages;
4235 
4236 	for (i = 0; i < bp->cp_nr_rings; i++) {
4237 		struct bnxt_napi *bnapi = bp->bnapi[i];
4238 		int rc;
4239 
4240 		if (!bnapi)
4241 			continue;
4242 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4243 		if (rc)
4244 			return rc;
4245 	}
4246 	return 0;
4247 }
4248 
4249 static void bnxt_free_cp_rings(struct bnxt *bp)
4250 {
4251 	int i;
4252 
4253 	if (!bp->bnapi)
4254 		return;
4255 
4256 	for (i = 0; i < bp->cp_nr_rings; i++) {
4257 		struct bnxt_napi *bnapi = bp->bnapi[i];
4258 		struct bnxt_cp_ring_info *cpr;
4259 		struct bnxt_ring_struct *ring;
4260 		int j;
4261 
4262 		if (!bnapi)
4263 			continue;
4264 
4265 		cpr = &bnapi->cp_ring;
4266 		ring = &cpr->cp_ring_struct;
4267 
4268 		bnxt_free_ring(bp, &ring->ring_mem);
4269 
4270 		if (!cpr->cp_ring_arr)
4271 			continue;
4272 
4273 		for (j = 0; j < cpr->cp_ring_count; j++) {
4274 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4275 
4276 			ring = &cpr2->cp_ring_struct;
4277 			bnxt_free_ring(bp, &ring->ring_mem);
4278 			bnxt_free_cp_arrays(cpr2);
4279 		}
4280 		kfree(cpr->cp_ring_arr);
4281 		cpr->cp_ring_arr = NULL;
4282 		cpr->cp_ring_count = 0;
4283 	}
4284 }
4285 
4286 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4287 				  struct bnxt_cp_ring_info *cpr)
4288 {
4289 	struct bnxt_ring_mem_info *rmem;
4290 	struct bnxt_ring_struct *ring;
4291 	int rc;
4292 
4293 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4294 	if (rc) {
4295 		bnxt_free_cp_arrays(cpr);
4296 		return -ENOMEM;
4297 	}
4298 	ring = &cpr->cp_ring_struct;
4299 	rmem = &ring->ring_mem;
4300 	rmem->nr_pages = bp->cp_nr_pages;
4301 	rmem->page_size = HW_CMPD_RING_SIZE;
4302 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4303 	rmem->dma_arr = cpr->cp_desc_mapping;
4304 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4305 	rc = bnxt_alloc_ring(bp, rmem);
4306 	if (rc) {
4307 		bnxt_free_ring(bp, rmem);
4308 		bnxt_free_cp_arrays(cpr);
4309 	}
4310 	return rc;
4311 }
4312 
4313 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4314 {
4315 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4316 	int i, j, rc, ulp_msix;
4317 	int tcs = bp->num_tc;
4318 
4319 	if (!tcs)
4320 		tcs = 1;
4321 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4322 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4323 		struct bnxt_napi *bnapi = bp->bnapi[i];
4324 		struct bnxt_cp_ring_info *cpr, *cpr2;
4325 		struct bnxt_ring_struct *ring;
4326 		int cp_count = 0, k;
4327 		int rx = 0, tx = 0;
4328 
4329 		if (!bnapi)
4330 			continue;
4331 
4332 		cpr = &bnapi->cp_ring;
4333 		cpr->bnapi = bnapi;
4334 		ring = &cpr->cp_ring_struct;
4335 
4336 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4337 		if (rc)
4338 			return rc;
4339 
4340 		ring->map_idx = ulp_msix + i;
4341 
4342 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4343 			continue;
4344 
4345 		if (i < bp->rx_nr_rings) {
4346 			cp_count++;
4347 			rx = 1;
4348 		}
4349 		if (i < bp->tx_nr_rings_xdp) {
4350 			cp_count++;
4351 			tx = 1;
4352 		} else if ((sh && i < bp->tx_nr_rings) ||
4353 			 (!sh && i >= bp->rx_nr_rings)) {
4354 			cp_count += tcs;
4355 			tx = 1;
4356 		}
4357 
4358 		cpr->cp_ring_arr = kzalloc_objs(*cpr, cp_count);
4359 		if (!cpr->cp_ring_arr)
4360 			return -ENOMEM;
4361 		cpr->cp_ring_count = cp_count;
4362 
4363 		for (k = 0; k < cp_count; k++) {
4364 			cpr2 = &cpr->cp_ring_arr[k];
4365 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4366 			if (rc)
4367 				return rc;
4368 			cpr2->bnapi = bnapi;
4369 			cpr2->sw_stats = cpr->sw_stats;
4370 			cpr2->cp_idx = k;
4371 			if (!k && rx) {
4372 				bp->rx_ring[i].rx_cpr = cpr2;
4373 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4374 			} else {
4375 				int n, tc = k - rx;
4376 
4377 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4378 				bp->tx_ring[n].tx_cpr = cpr2;
4379 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4380 			}
4381 		}
4382 		if (tx)
4383 			j++;
4384 	}
4385 	return 0;
4386 }
4387 
4388 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4389 				     struct bnxt_rx_ring_info *rxr)
4390 {
4391 	struct bnxt_ring_mem_info *rmem;
4392 	struct bnxt_ring_struct *ring;
4393 
4394 	ring = &rxr->rx_ring_struct;
4395 	rmem = &ring->ring_mem;
4396 	rmem->nr_pages = bp->rx_nr_pages;
4397 	rmem->page_size = HW_RXBD_RING_SIZE;
4398 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4399 	rmem->dma_arr = rxr->rx_desc_mapping;
4400 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4401 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4402 
4403 	ring = &rxr->rx_agg_ring_struct;
4404 	rmem = &ring->ring_mem;
4405 	rmem->nr_pages = bp->rx_agg_nr_pages;
4406 	rmem->page_size = HW_RXBD_RING_SIZE;
4407 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4408 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4409 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4410 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4411 }
4412 
4413 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4414 				      struct bnxt_rx_ring_info *rxr)
4415 {
4416 	struct bnxt_ring_mem_info *rmem;
4417 	struct bnxt_ring_struct *ring;
4418 	int i;
4419 
4420 	rxr->page_pool->p.napi = NULL;
4421 	rxr->page_pool = NULL;
4422 	rxr->head_pool->p.napi = NULL;
4423 	rxr->head_pool = NULL;
4424 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4425 
4426 	ring = &rxr->rx_ring_struct;
4427 	rmem = &ring->ring_mem;
4428 	rmem->pg_tbl = NULL;
4429 	rmem->pg_tbl_map = 0;
4430 	for (i = 0; i < rmem->nr_pages; i++) {
4431 		rmem->pg_arr[i] = NULL;
4432 		rmem->dma_arr[i] = 0;
4433 	}
4434 	*rmem->vmem = NULL;
4435 
4436 	ring = &rxr->rx_agg_ring_struct;
4437 	rmem = &ring->ring_mem;
4438 	rmem->pg_tbl = NULL;
4439 	rmem->pg_tbl_map = 0;
4440 	for (i = 0; i < rmem->nr_pages; i++) {
4441 		rmem->pg_arr[i] = NULL;
4442 		rmem->dma_arr[i] = 0;
4443 	}
4444 	*rmem->vmem = NULL;
4445 }
4446 
4447 static void bnxt_init_ring_struct(struct bnxt *bp)
4448 {
4449 	int i, j;
4450 
4451 	for (i = 0; i < bp->cp_nr_rings; i++) {
4452 		struct bnxt_napi *bnapi = bp->bnapi[i];
4453 		struct netdev_queue_config qcfg;
4454 		struct bnxt_ring_mem_info *rmem;
4455 		struct bnxt_cp_ring_info *cpr;
4456 		struct bnxt_rx_ring_info *rxr;
4457 		struct bnxt_tx_ring_info *txr;
4458 		struct bnxt_ring_struct *ring;
4459 
4460 		if (!bnapi)
4461 			continue;
4462 
4463 		cpr = &bnapi->cp_ring;
4464 		ring = &cpr->cp_ring_struct;
4465 		rmem = &ring->ring_mem;
4466 		rmem->nr_pages = bp->cp_nr_pages;
4467 		rmem->page_size = HW_CMPD_RING_SIZE;
4468 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4469 		rmem->dma_arr = cpr->cp_desc_mapping;
4470 		rmem->vmem_size = 0;
4471 
4472 		rxr = bnapi->rx_ring;
4473 		if (!rxr)
4474 			goto skip_rx;
4475 
4476 		netdev_queue_config(bp->dev, i, &qcfg);
4477 		rxr->rx_page_size = qcfg.rx_page_size;
4478 
4479 		ring = &rxr->rx_ring_struct;
4480 		rmem = &ring->ring_mem;
4481 		rmem->nr_pages = bp->rx_nr_pages;
4482 		rmem->page_size = HW_RXBD_RING_SIZE;
4483 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4484 		rmem->dma_arr = rxr->rx_desc_mapping;
4485 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4486 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4487 
4488 		ring = &rxr->rx_agg_ring_struct;
4489 		rmem = &ring->ring_mem;
4490 		rmem->nr_pages = bp->rx_agg_nr_pages;
4491 		rmem->page_size = HW_RXBD_RING_SIZE;
4492 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4493 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4494 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4495 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4496 
4497 skip_rx:
4498 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4499 			ring = &txr->tx_ring_struct;
4500 			rmem = &ring->ring_mem;
4501 			rmem->nr_pages = bp->tx_nr_pages;
4502 			rmem->page_size = HW_TXBD_RING_SIZE;
4503 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4504 			rmem->dma_arr = txr->tx_desc_mapping;
4505 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4506 			rmem->vmem = (void **)&txr->tx_buf_ring;
4507 		}
4508 	}
4509 }
4510 
4511 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4512 {
4513 	int i;
4514 	u32 prod;
4515 	struct rx_bd **rx_buf_ring;
4516 
4517 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4518 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4519 		int j;
4520 		struct rx_bd *rxbd;
4521 
4522 		rxbd = rx_buf_ring[i];
4523 		if (!rxbd)
4524 			continue;
4525 
4526 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4527 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4528 			rxbd->rx_bd_opaque = prod;
4529 		}
4530 	}
4531 }
4532 
4533 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4534 				       struct bnxt_rx_ring_info *rxr,
4535 				       int ring_nr)
4536 {
4537 	u32 prod;
4538 	int i;
4539 
4540 	prod = rxr->rx_prod;
4541 	for (i = 0; i < bp->rx_ring_size; i++) {
4542 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4543 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4544 				    ring_nr, i, bp->rx_ring_size);
4545 			break;
4546 		}
4547 		prod = NEXT_RX(prod);
4548 	}
4549 	rxr->rx_prod = prod;
4550 }
4551 
4552 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4553 					  struct bnxt_rx_ring_info *rxr,
4554 					  int ring_nr)
4555 {
4556 	int fill_level, i;
4557 	u32 prod;
4558 
4559 	fill_level = bnxt_rx_agg_ring_fill_level(bp, rxr);
4560 
4561 	prod = rxr->rx_agg_prod;
4562 	for (i = 0; i < fill_level; i++) {
4563 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4564 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4565 				    ring_nr, i, bp->rx_agg_ring_size);
4566 			break;
4567 		}
4568 		prod = NEXT_RX_AGG(prod);
4569 	}
4570 	rxr->rx_agg_prod = prod;
4571 }
4572 
4573 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4574 					struct bnxt_rx_ring_info *rxr)
4575 {
4576 	dma_addr_t mapping;
4577 	u8 *data;
4578 	int i;
4579 
4580 	for (i = 0; i < bp->max_tpa_roundup_size; i++) {
4581 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4582 					    GFP_KERNEL);
4583 		if (!data)
4584 			return -ENOMEM;
4585 
4586 		rxr->rx_tpa[i].data = data;
4587 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4588 		rxr->rx_tpa[i].mapping = mapping;
4589 	}
4590 
4591 	return 0;
4592 }
4593 
4594 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4595 {
4596 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4597 	int rc;
4598 
4599 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4600 
4601 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4602 		return 0;
4603 
4604 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4605 
4606 	if (rxr->rx_tpa) {
4607 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4608 		if (rc)
4609 			return rc;
4610 	}
4611 	return 0;
4612 }
4613 
4614 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4615 				       struct bnxt_rx_ring_info *rxr)
4616 {
4617 	struct bnxt_ring_struct *ring;
4618 	u32 type;
4619 
4620 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4621 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4622 
4623 	if (NET_IP_ALIGN == 2)
4624 		type |= RX_BD_FLAGS_SOP;
4625 
4626 	ring = &rxr->rx_ring_struct;
4627 	bnxt_init_rxbd_pages(ring, type);
4628 	ring->fw_ring_id = INVALID_HW_RING_ID;
4629 }
4630 
4631 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4632 					   struct bnxt_rx_ring_info *rxr)
4633 {
4634 	struct bnxt_ring_struct *ring;
4635 	u32 type;
4636 
4637 	ring = &rxr->rx_agg_ring_struct;
4638 	ring->fw_ring_id = INVALID_HW_RING_ID;
4639 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4640 		type = ((u32)rxr->rx_page_size << RX_BD_LEN_SHIFT) |
4641 			RX_BD_TYPE_RX_AGG_BD;
4642 
4643 		/* Disable EOP if TPA is enabled to prevent overlapping zero
4644 		 * padding with the next segment's data.  On P7_PLUS, EOP will
4645 		 * automatically disable Relaxed Ordering (RO) to prevent
4646 		 * potential data corruption (and may degrade performance).  On
4647 		 * older chips, RO will not be automatically disabled and may
4648 		 * cause corruption.
4649 		 */
4650 		if (!(bp->flags & BNXT_FLAG_TPA))
4651 			type |= RX_BD_FLAGS_AGG_EOP;
4652 
4653 		bnxt_init_rxbd_pages(ring, type);
4654 	}
4655 }
4656 
4657 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4658 {
4659 	struct bnxt_rx_ring_info *rxr;
4660 
4661 	rxr = &bp->rx_ring[ring_nr];
4662 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4663 
4664 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4665 			     &rxr->bnapi->napi);
4666 
4667 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4668 		bpf_prog_add(bp->xdp_prog, 1);
4669 		rxr->xdp_prog = bp->xdp_prog;
4670 	}
4671 
4672 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4673 
4674 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4675 }
4676 
4677 static void bnxt_init_cp_rings(struct bnxt *bp)
4678 {
4679 	int i, j;
4680 
4681 	for (i = 0; i < bp->cp_nr_rings; i++) {
4682 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4683 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4684 
4685 		ring->fw_ring_id = INVALID_HW_RING_ID;
4686 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4687 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4688 		if (!cpr->cp_ring_arr)
4689 			continue;
4690 		for (j = 0; j < cpr->cp_ring_count; j++) {
4691 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4692 
4693 			ring = &cpr2->cp_ring_struct;
4694 			ring->fw_ring_id = INVALID_HW_RING_ID;
4695 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4696 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4697 		}
4698 	}
4699 }
4700 
4701 static int bnxt_init_rx_rings(struct bnxt *bp)
4702 {
4703 	int i, rc = 0;
4704 
4705 	if (BNXT_RX_PAGE_MODE(bp)) {
4706 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4707 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4708 	} else {
4709 		bp->rx_offset = BNXT_RX_OFFSET;
4710 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4711 	}
4712 
4713 	for (i = 0; i < bp->rx_nr_rings; i++) {
4714 		rc = bnxt_init_one_rx_ring(bp, i);
4715 		if (rc)
4716 			break;
4717 	}
4718 
4719 	return rc;
4720 }
4721 
4722 static int bnxt_init_tx_rings(struct bnxt *bp)
4723 {
4724 	netdev_features_t features;
4725 	u16 i;
4726 
4727 	features = bp->dev->features;
4728 
4729 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4730 				   bnxt_min_tx_desc_cnt(bp, features));
4731 
4732 	for (i = 0; i < bp->tx_nr_rings; i++) {
4733 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4734 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4735 
4736 		ring->fw_ring_id = INVALID_HW_RING_ID;
4737 
4738 		if (i >= bp->tx_nr_rings_xdp)
4739 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4740 					     NETDEV_QUEUE_TYPE_TX,
4741 					     &txr->bnapi->napi);
4742 	}
4743 
4744 	return 0;
4745 }
4746 
4747 static void bnxt_free_ring_grps(struct bnxt *bp)
4748 {
4749 	kfree(bp->grp_info);
4750 	bp->grp_info = NULL;
4751 }
4752 
4753 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4754 {
4755 	int i;
4756 
4757 	if (irq_re_init) {
4758 		bp->grp_info = kzalloc_objs(struct bnxt_ring_grp_info,
4759 					    bp->cp_nr_rings);
4760 		if (!bp->grp_info)
4761 			return -ENOMEM;
4762 	}
4763 	for (i = 0; i < bp->cp_nr_rings; i++) {
4764 		if (irq_re_init)
4765 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4766 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4767 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4768 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4769 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4770 	}
4771 	return 0;
4772 }
4773 
4774 static void bnxt_free_vnics(struct bnxt *bp)
4775 {
4776 	kfree(bp->vnic_info);
4777 	bp->vnic_info = NULL;
4778 	bp->nr_vnics = 0;
4779 }
4780 
4781 static int bnxt_alloc_vnics(struct bnxt *bp)
4782 {
4783 	int num_vnics = 1;
4784 
4785 #ifdef CONFIG_RFS_ACCEL
4786 	if (bp->flags & BNXT_FLAG_RFS) {
4787 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4788 			num_vnics++;
4789 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4790 			num_vnics += bp->rx_nr_rings;
4791 	}
4792 #endif
4793 
4794 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4795 		num_vnics++;
4796 
4797 	bp->vnic_info = kzalloc_objs(struct bnxt_vnic_info, num_vnics);
4798 	if (!bp->vnic_info)
4799 		return -ENOMEM;
4800 
4801 	bp->nr_vnics = num_vnics;
4802 	return 0;
4803 }
4804 
4805 static void bnxt_init_vnics(struct bnxt *bp)
4806 {
4807 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4808 	int i;
4809 
4810 	for (i = 0; i < bp->nr_vnics; i++) {
4811 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4812 		int j;
4813 
4814 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4815 		vnic->vnic_id = i;
4816 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4817 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4818 
4819 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4820 
4821 		if (bp->vnic_info[i].rss_hash_key) {
4822 			if (i == BNXT_VNIC_DEFAULT) {
4823 				u8 *key = (void *)vnic->rss_hash_key;
4824 				int k;
4825 
4826 				if (!bp->rss_hash_key_valid &&
4827 				    !bp->rss_hash_key_updated) {
4828 					get_random_bytes(bp->rss_hash_key,
4829 							 HW_HASH_KEY_SIZE);
4830 					bp->rss_hash_key_updated = true;
4831 				}
4832 
4833 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4834 				       HW_HASH_KEY_SIZE);
4835 
4836 				if (!bp->rss_hash_key_updated)
4837 					continue;
4838 
4839 				bp->rss_hash_key_updated = false;
4840 				bp->rss_hash_key_valid = true;
4841 
4842 				bp->toeplitz_prefix = 0;
4843 				for (k = 0; k < 8; k++) {
4844 					bp->toeplitz_prefix <<= 8;
4845 					bp->toeplitz_prefix |= key[k];
4846 				}
4847 			} else {
4848 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4849 				       HW_HASH_KEY_SIZE);
4850 			}
4851 		}
4852 	}
4853 }
4854 
4855 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4856 {
4857 	int pages;
4858 
4859 	pages = ring_size / desc_per_pg;
4860 
4861 	if (!pages)
4862 		return 1;
4863 
4864 	pages++;
4865 
4866 	while (pages & (pages - 1))
4867 		pages++;
4868 
4869 	return pages;
4870 }
4871 
4872 void bnxt_set_tpa_flags(struct bnxt *bp)
4873 {
4874 	bp->flags &= ~BNXT_FLAG_TPA;
4875 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4876 		return;
4877 	if (bp->dev->features & NETIF_F_LRO)
4878 		bp->flags |= BNXT_FLAG_LRO;
4879 	else if (bp->dev->features & NETIF_F_GRO_HW)
4880 		bp->flags |= BNXT_FLAG_GRO;
4881 }
4882 
4883 static void bnxt_init_ring_params(struct bnxt *bp)
4884 {
4885 	unsigned int rx_size;
4886 
4887 	bp->rx_copybreak = 0; /* rx-copybreak disabled by default */
4888 	/* Try to fit 4 chunks into a 4k page */
4889 	rx_size = SZ_1K -
4890 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4891 	bp->dev->cfg->hds_thresh = max(BNXT_MIN_RX_HDR_BUF, rx_size);
4892 }
4893 
4894 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4895  * be set on entry.
4896  */
4897 void bnxt_set_ring_params(struct bnxt *bp)
4898 {
4899 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4900 	u32 agg_factor = 0, agg_ring_size = 0;
4901 
4902 	/* 8 for CRC and VLAN */
4903 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4904 
4905 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4906 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4907 
4908 	ring_size = bp->rx_ring_size;
4909 	bp->rx_agg_ring_size = 0;
4910 	bp->rx_agg_nr_pages = 0;
4911 
4912 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4913 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4914 
4915 	bp->flags &= ~BNXT_FLAG_JUMBO;
4916 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4917 		u32 jumbo_factor;
4918 
4919 		bp->flags |= BNXT_FLAG_JUMBO;
4920 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4921 		if (jumbo_factor > agg_factor)
4922 			agg_factor = jumbo_factor;
4923 	}
4924 	if (agg_factor) {
4925 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4926 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4927 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4928 				    bp->rx_ring_size, ring_size);
4929 			bp->rx_ring_size = ring_size;
4930 		}
4931 		agg_ring_size = ring_size * agg_factor;
4932 
4933 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4934 							RX_DESC_CNT);
4935 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4936 			u32 tmp = agg_ring_size;
4937 
4938 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4939 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4940 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4941 				    tmp, agg_ring_size);
4942 		}
4943 		bp->rx_agg_ring_size = agg_ring_size;
4944 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4945 
4946 		if (BNXT_RX_PAGE_MODE(bp)) {
4947 			rx_space = PAGE_SIZE;
4948 			rx_size = PAGE_SIZE -
4949 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4950 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4951 		} else {
4952 			rx_size = max3(BNXT_MIN_RX_HDR_BUF,
4953 				       bp->rx_copybreak,
4954 				       bp->dev->cfg_pending->hds_thresh);
4955 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4956 			rx_space = rx_size + NET_SKB_PAD +
4957 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4958 		}
4959 	}
4960 
4961 	bp->rx_buf_use_size = rx_size;
4962 	bp->rx_buf_size = rx_space;
4963 
4964 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4965 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4966 
4967 	ring_size = bp->tx_ring_size;
4968 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4969 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4970 
4971 	max_rx_cmpl = bp->rx_ring_size;
4972 	/* MAX TPA needs to be added because TPA_START completions are
4973 	 * immediately recycled, so the TPA completions are not bound by
4974 	 * the RX ring size.
4975 	 */
4976 	if (bp->flags & BNXT_FLAG_TPA)
4977 		max_rx_cmpl += bp->max_tpa;
4978 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4979 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4980 	bp->cp_ring_size = ring_size;
4981 
4982 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4983 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4984 		bp->cp_nr_pages = MAX_CP_PAGES;
4985 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4986 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4987 			    ring_size, bp->cp_ring_size);
4988 	}
4989 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4990 	bp->cp_ring_mask = bp->cp_bit - 1;
4991 }
4992 
4993 /* Changing allocation mode of RX rings.
4994  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4995  */
4996 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4997 {
4998 	struct net_device *dev = bp->dev;
4999 
5000 	if (page_mode) {
5001 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
5002 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
5003 
5004 		if (bp->xdp_prog->aux->xdp_has_frags)
5005 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
5006 		else
5007 			dev->max_mtu =
5008 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
5009 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
5010 			bp->flags |= BNXT_FLAG_JUMBO;
5011 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
5012 		} else {
5013 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
5014 			bp->rx_skb_func = bnxt_rx_page_skb;
5015 		}
5016 		bp->rx_dir = DMA_BIDIRECTIONAL;
5017 	} else {
5018 		dev->max_mtu = bp->max_mtu;
5019 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
5020 		bp->rx_dir = DMA_FROM_DEVICE;
5021 		bp->rx_skb_func = bnxt_rx_skb;
5022 	}
5023 }
5024 
5025 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
5026 {
5027 	__bnxt_set_rx_skb_mode(bp, page_mode);
5028 
5029 	if (!page_mode) {
5030 		int rx, tx;
5031 
5032 		bnxt_get_max_rings(bp, &rx, &tx, true);
5033 		if (rx > 1) {
5034 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
5035 			if (BNXT_SUPPORTS_TPA(bp))
5036 				bp->dev->hw_features |= NETIF_F_LRO;
5037 		}
5038 	}
5039 
5040 	/* Update LRO and GRO_HW availability */
5041 	netdev_update_features(bp->dev);
5042 }
5043 
5044 static void bnxt_free_vnic_attributes(struct bnxt *bp)
5045 {
5046 	int i;
5047 	struct bnxt_vnic_info *vnic;
5048 	struct pci_dev *pdev = bp->pdev;
5049 
5050 	if (!bp->vnic_info)
5051 		return;
5052 
5053 	for (i = 0; i < bp->nr_vnics; i++) {
5054 		vnic = &bp->vnic_info[i];
5055 
5056 		kfree(vnic->fw_grp_ids);
5057 		vnic->fw_grp_ids = NULL;
5058 
5059 		kfree(vnic->uc_list);
5060 		vnic->uc_list = NULL;
5061 
5062 		if (vnic->mc_list) {
5063 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
5064 					  vnic->mc_list, vnic->mc_list_mapping);
5065 			vnic->mc_list = NULL;
5066 		}
5067 
5068 		if (vnic->rss_table) {
5069 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
5070 					  vnic->rss_table,
5071 					  vnic->rss_table_dma_addr);
5072 			vnic->rss_table = NULL;
5073 		}
5074 
5075 		vnic->rss_hash_key = NULL;
5076 		vnic->flags = 0;
5077 	}
5078 }
5079 
5080 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
5081 {
5082 	int i, rc = 0, size;
5083 	struct bnxt_vnic_info *vnic;
5084 	struct pci_dev *pdev = bp->pdev;
5085 	int max_rings;
5086 
5087 	for (i = 0; i < bp->nr_vnics; i++) {
5088 		vnic = &bp->vnic_info[i];
5089 
5090 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
5091 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
5092 
5093 			if (mem_size > 0) {
5094 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
5095 				if (!vnic->uc_list) {
5096 					rc = -ENOMEM;
5097 					goto out;
5098 				}
5099 			}
5100 		}
5101 
5102 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
5103 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
5104 			vnic->mc_list =
5105 				dma_alloc_coherent(&pdev->dev,
5106 						   vnic->mc_list_size,
5107 						   &vnic->mc_list_mapping,
5108 						   GFP_KERNEL);
5109 			if (!vnic->mc_list) {
5110 				rc = -ENOMEM;
5111 				goto out;
5112 			}
5113 		}
5114 
5115 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5116 			goto vnic_skip_grps;
5117 
5118 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
5119 			max_rings = bp->rx_nr_rings;
5120 		else
5121 			max_rings = 1;
5122 
5123 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
5124 		if (!vnic->fw_grp_ids) {
5125 			rc = -ENOMEM;
5126 			goto out;
5127 		}
5128 vnic_skip_grps:
5129 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
5130 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
5131 			continue;
5132 
5133 		/* Allocate rss table and hash key */
5134 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
5135 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5136 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
5137 
5138 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
5139 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
5140 						     vnic->rss_table_size,
5141 						     &vnic->rss_table_dma_addr,
5142 						     GFP_KERNEL);
5143 		if (!vnic->rss_table) {
5144 			rc = -ENOMEM;
5145 			goto out;
5146 		}
5147 
5148 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
5149 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
5150 	}
5151 	return 0;
5152 
5153 out:
5154 	return rc;
5155 }
5156 
5157 static void bnxt_free_hwrm_resources(struct bnxt *bp)
5158 {
5159 	struct bnxt_hwrm_wait_token *token;
5160 
5161 	dma_pool_destroy(bp->hwrm_dma_pool);
5162 	bp->hwrm_dma_pool = NULL;
5163 
5164 	rcu_read_lock();
5165 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
5166 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
5167 	rcu_read_unlock();
5168 }
5169 
5170 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
5171 {
5172 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5173 					    BNXT_HWRM_DMA_SIZE,
5174 					    BNXT_HWRM_DMA_ALIGN, 0);
5175 	if (!bp->hwrm_dma_pool)
5176 		return -ENOMEM;
5177 
5178 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5179 
5180 	return 0;
5181 }
5182 
5183 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5184 {
5185 	kfree(stats->hw_masks);
5186 	stats->hw_masks = NULL;
5187 	kfree(stats->sw_stats);
5188 	stats->sw_stats = NULL;
5189 	if (stats->hw_stats) {
5190 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5191 				  stats->hw_stats_map);
5192 		stats->hw_stats = NULL;
5193 	}
5194 }
5195 
5196 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5197 				bool alloc_masks)
5198 {
5199 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5200 					     &stats->hw_stats_map, GFP_KERNEL);
5201 	if (!stats->hw_stats)
5202 		return -ENOMEM;
5203 
5204 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5205 	if (!stats->sw_stats)
5206 		goto stats_mem_err;
5207 
5208 	if (alloc_masks) {
5209 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5210 		if (!stats->hw_masks)
5211 			goto stats_mem_err;
5212 	}
5213 	return 0;
5214 
5215 stats_mem_err:
5216 	bnxt_free_stats_mem(bp, stats);
5217 	return -ENOMEM;
5218 }
5219 
5220 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5221 {
5222 	int i;
5223 
5224 	for (i = 0; i < count; i++)
5225 		mask_arr[i] = mask;
5226 }
5227 
5228 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5229 {
5230 	int i;
5231 
5232 	for (i = 0; i < count; i++)
5233 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5234 }
5235 
5236 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5237 				    struct bnxt_stats_mem *stats)
5238 {
5239 	struct hwrm_func_qstats_ext_output *resp;
5240 	struct hwrm_func_qstats_ext_input *req;
5241 	__le64 *hw_masks;
5242 	int rc;
5243 
5244 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5245 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5246 		return -EOPNOTSUPP;
5247 
5248 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5249 	if (rc)
5250 		return rc;
5251 
5252 	req->fid = cpu_to_le16(0xffff);
5253 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5254 
5255 	resp = hwrm_req_hold(bp, req);
5256 	rc = hwrm_req_send(bp, req);
5257 	if (!rc) {
5258 		hw_masks = &resp->rx_ucast_pkts;
5259 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5260 	}
5261 	hwrm_req_drop(bp, req);
5262 	return rc;
5263 }
5264 
5265 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5266 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5267 
5268 static void bnxt_init_stats(struct bnxt *bp)
5269 {
5270 	struct bnxt_napi *bnapi = bp->bnapi[0];
5271 	struct bnxt_cp_ring_info *cpr;
5272 	struct bnxt_stats_mem *stats;
5273 	__le64 *rx_stats, *tx_stats;
5274 	int rc, rx_count, tx_count;
5275 	u64 *rx_masks, *tx_masks;
5276 	u64 mask;
5277 	u8 flags;
5278 
5279 	cpr = &bnapi->cp_ring;
5280 	stats = &cpr->stats;
5281 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5282 	if (rc) {
5283 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5284 			mask = (1ULL << 48) - 1;
5285 		else
5286 			mask = -1ULL;
5287 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5288 	}
5289 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5290 		stats = &bp->port_stats;
5291 		rx_stats = stats->hw_stats;
5292 		rx_masks = stats->hw_masks;
5293 		rx_count = sizeof(struct rx_port_stats) / 8;
5294 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5295 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5296 		tx_count = sizeof(struct tx_port_stats) / 8;
5297 
5298 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5299 		rc = bnxt_hwrm_port_qstats(bp, flags);
5300 		if (rc) {
5301 			mask = (1ULL << 40) - 1;
5302 
5303 			bnxt_fill_masks(rx_masks, mask, rx_count);
5304 			bnxt_fill_masks(tx_masks, mask, tx_count);
5305 		} else {
5306 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5307 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5308 			bnxt_hwrm_port_qstats(bp, 0);
5309 		}
5310 	}
5311 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5312 		stats = &bp->rx_port_stats_ext;
5313 		rx_stats = stats->hw_stats;
5314 		rx_masks = stats->hw_masks;
5315 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5316 		stats = &bp->tx_port_stats_ext;
5317 		tx_stats = stats->hw_stats;
5318 		tx_masks = stats->hw_masks;
5319 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5320 
5321 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5322 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5323 		if (rc) {
5324 			mask = (1ULL << 40) - 1;
5325 
5326 			bnxt_fill_masks(rx_masks, mask, rx_count);
5327 			if (tx_stats)
5328 				bnxt_fill_masks(tx_masks, mask, tx_count);
5329 		} else {
5330 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5331 			if (tx_stats)
5332 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5333 						   tx_count);
5334 			bnxt_hwrm_port_qstats_ext(bp, 0);
5335 		}
5336 	}
5337 }
5338 
5339 static void bnxt_free_port_stats(struct bnxt *bp)
5340 {
5341 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5342 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5343 
5344 	bnxt_free_stats_mem(bp, &bp->port_stats);
5345 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5346 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5347 }
5348 
5349 static void bnxt_free_ring_stats(struct bnxt *bp)
5350 {
5351 	int i;
5352 
5353 	if (!bp->bnapi)
5354 		return;
5355 
5356 	for (i = 0; i < bp->cp_nr_rings; i++) {
5357 		struct bnxt_napi *bnapi = bp->bnapi[i];
5358 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5359 
5360 		bnxt_free_stats_mem(bp, &cpr->stats);
5361 
5362 		kfree(cpr->sw_stats);
5363 		cpr->sw_stats = NULL;
5364 	}
5365 }
5366 
5367 static int bnxt_alloc_stats(struct bnxt *bp)
5368 {
5369 	u32 size, i;
5370 	int rc;
5371 
5372 	size = bp->hw_ring_stats_size;
5373 
5374 	for (i = 0; i < bp->cp_nr_rings; i++) {
5375 		struct bnxt_napi *bnapi = bp->bnapi[i];
5376 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5377 
5378 		cpr->sw_stats = kzalloc_obj(*cpr->sw_stats);
5379 		if (!cpr->sw_stats)
5380 			return -ENOMEM;
5381 
5382 		cpr->stats.len = size;
5383 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5384 		if (rc)
5385 			return rc;
5386 
5387 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5388 	}
5389 
5390 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5391 		return 0;
5392 
5393 	if (bp->port_stats.hw_stats)
5394 		goto alloc_ext_stats;
5395 
5396 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5397 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5398 	if (rc)
5399 		return rc;
5400 
5401 	bp->flags |= BNXT_FLAG_PORT_STATS;
5402 
5403 alloc_ext_stats:
5404 	/* Display extended statistics only if FW supports it */
5405 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5406 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5407 			return 0;
5408 
5409 	if (bp->rx_port_stats_ext.hw_stats)
5410 		goto alloc_tx_ext_stats;
5411 
5412 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5413 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5414 	/* Extended stats are optional */
5415 	if (rc)
5416 		return 0;
5417 
5418 alloc_tx_ext_stats:
5419 	if (bp->tx_port_stats_ext.hw_stats)
5420 		return 0;
5421 
5422 	if (bp->hwrm_spec_code >= 0x10902 ||
5423 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5424 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5425 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5426 		/* Extended stats are optional */
5427 		if (rc)
5428 			return 0;
5429 	}
5430 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5431 	return 0;
5432 }
5433 
5434 static void bnxt_clear_ring_indices(struct bnxt *bp)
5435 {
5436 	int i, j;
5437 
5438 	if (!bp->bnapi)
5439 		return;
5440 
5441 	for (i = 0; i < bp->cp_nr_rings; i++) {
5442 		struct bnxt_napi *bnapi = bp->bnapi[i];
5443 		struct bnxt_cp_ring_info *cpr;
5444 		struct bnxt_rx_ring_info *rxr;
5445 		struct bnxt_tx_ring_info *txr;
5446 
5447 		if (!bnapi)
5448 			continue;
5449 
5450 		cpr = &bnapi->cp_ring;
5451 		cpr->cp_raw_cons = 0;
5452 
5453 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5454 			txr->tx_prod = 0;
5455 			txr->tx_cons = 0;
5456 			txr->tx_hw_cons = 0;
5457 			txr->kick_pending = 0;
5458 			txr->kick_txbd0 = NULL;
5459 		}
5460 
5461 		rxr = bnapi->rx_ring;
5462 		if (rxr) {
5463 			rxr->rx_prod = 0;
5464 			rxr->rx_agg_prod = 0;
5465 			rxr->rx_sw_agg_prod = 0;
5466 			rxr->rx_next_cons = 0;
5467 		}
5468 		bnapi->events = 0;
5469 	}
5470 }
5471 
5472 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5473 {
5474 	u8 type = fltr->type, flags = fltr->flags;
5475 
5476 	INIT_LIST_HEAD(&fltr->list);
5477 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5478 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5479 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5480 }
5481 
5482 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5483 {
5484 	if (!list_empty(&fltr->list))
5485 		list_del_init(&fltr->list);
5486 }
5487 
5488 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5489 {
5490 	struct bnxt_filter_base *usr_fltr, *tmp;
5491 
5492 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5493 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5494 			continue;
5495 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5496 	}
5497 }
5498 
5499 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5500 {
5501 	hlist_del(&fltr->hash);
5502 	bnxt_del_one_usr_fltr(bp, fltr);
5503 	if (fltr->flags) {
5504 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5505 		bp->ntp_fltr_count--;
5506 	}
5507 	kfree(fltr);
5508 }
5509 
5510 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5511 {
5512 	int i;
5513 
5514 	netdev_assert_locked_or_invisible(bp->dev);
5515 
5516 	/* Under netdev instance lock and all our NAPIs have been disabled.
5517 	 * It's safe to delete the hash table.
5518 	 */
5519 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5520 		struct hlist_head *head;
5521 		struct hlist_node *tmp;
5522 		struct bnxt_ntuple_filter *fltr;
5523 
5524 		head = &bp->ntp_fltr_hash_tbl[i];
5525 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5526 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5527 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5528 				     !list_empty(&fltr->base.list)))
5529 				continue;
5530 			bnxt_del_fltr(bp, &fltr->base);
5531 		}
5532 	}
5533 	if (!all)
5534 		return;
5535 
5536 	bitmap_free(bp->ntp_fltr_bmap);
5537 	bp->ntp_fltr_bmap = NULL;
5538 	bp->ntp_fltr_count = 0;
5539 }
5540 
5541 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5542 {
5543 	int i, rc = 0;
5544 
5545 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5546 		return 0;
5547 
5548 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5549 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5550 
5551 	bp->ntp_fltr_count = 0;
5552 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5553 
5554 	if (!bp->ntp_fltr_bmap)
5555 		rc = -ENOMEM;
5556 
5557 	return rc;
5558 }
5559 
5560 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5561 {
5562 	int i;
5563 
5564 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5565 		struct hlist_head *head;
5566 		struct hlist_node *tmp;
5567 		struct bnxt_l2_filter *fltr;
5568 
5569 		head = &bp->l2_fltr_hash_tbl[i];
5570 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5571 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5572 				     !list_empty(&fltr->base.list)))
5573 				continue;
5574 			bnxt_del_fltr(bp, &fltr->base);
5575 		}
5576 	}
5577 }
5578 
5579 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5580 {
5581 	int i;
5582 
5583 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5584 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5585 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5586 }
5587 
5588 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5589 {
5590 	bnxt_free_vnic_attributes(bp);
5591 	bnxt_free_tx_rings(bp);
5592 	bnxt_free_rx_rings(bp);
5593 	bnxt_free_cp_rings(bp);
5594 	bnxt_free_all_cp_arrays(bp);
5595 	bnxt_free_ntp_fltrs(bp, false);
5596 	bnxt_free_l2_filters(bp, false);
5597 	if (irq_re_init) {
5598 		bnxt_free_ring_stats(bp);
5599 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5600 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5601 			bnxt_free_port_stats(bp);
5602 		bnxt_free_ring_grps(bp);
5603 		bnxt_free_vnics(bp);
5604 		kfree(bp->tx_ring_map);
5605 		bp->tx_ring_map = NULL;
5606 		kfree(bp->tx_ring);
5607 		bp->tx_ring = NULL;
5608 		kfree(bp->rx_ring);
5609 		bp->rx_ring = NULL;
5610 		kfree(bp->bnapi);
5611 		bp->bnapi = NULL;
5612 	} else {
5613 		bnxt_clear_ring_indices(bp);
5614 	}
5615 }
5616 
5617 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5618 {
5619 	int i, j, rc, size, arr_size;
5620 	void *bnapi;
5621 
5622 	if (irq_re_init) {
5623 		/* Allocate bnapi mem pointer array and mem block for
5624 		 * all queues
5625 		 */
5626 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5627 				bp->cp_nr_rings);
5628 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5629 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5630 		if (!bnapi)
5631 			return -ENOMEM;
5632 
5633 		bp->bnapi = bnapi;
5634 		bnapi += arr_size;
5635 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5636 			bp->bnapi[i] = bnapi;
5637 			bp->bnapi[i]->index = i;
5638 			bp->bnapi[i]->bp = bp;
5639 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5640 				struct bnxt_cp_ring_info *cpr =
5641 					&bp->bnapi[i]->cp_ring;
5642 
5643 				cpr->cp_ring_struct.ring_mem.flags =
5644 					BNXT_RMEM_RING_PTE_FLAG;
5645 			}
5646 		}
5647 
5648 		bp->rx_ring = kzalloc_objs(struct bnxt_rx_ring_info,
5649 					   bp->rx_nr_rings);
5650 		if (!bp->rx_ring)
5651 			return -ENOMEM;
5652 
5653 		for (i = 0; i < bp->rx_nr_rings; i++) {
5654 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5655 
5656 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5657 				rxr->rx_ring_struct.ring_mem.flags =
5658 					BNXT_RMEM_RING_PTE_FLAG;
5659 				rxr->rx_agg_ring_struct.ring_mem.flags =
5660 					BNXT_RMEM_RING_PTE_FLAG;
5661 			} else {
5662 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5663 			}
5664 			rxr->bnapi = bp->bnapi[i];
5665 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5666 		}
5667 
5668 		bp->tx_ring = kzalloc_objs(struct bnxt_tx_ring_info,
5669 					   bp->tx_nr_rings);
5670 		if (!bp->tx_ring)
5671 			return -ENOMEM;
5672 
5673 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5674 					  GFP_KERNEL);
5675 
5676 		if (!bp->tx_ring_map)
5677 			return -ENOMEM;
5678 
5679 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5680 			j = 0;
5681 		else
5682 			j = bp->rx_nr_rings;
5683 
5684 		for (i = 0; i < bp->tx_nr_rings; i++) {
5685 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5686 			struct bnxt_napi *bnapi2;
5687 
5688 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5689 				txr->tx_ring_struct.ring_mem.flags =
5690 					BNXT_RMEM_RING_PTE_FLAG;
5691 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5692 			if (i >= bp->tx_nr_rings_xdp) {
5693 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5694 
5695 				bnapi2 = bp->bnapi[k];
5696 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5697 				txr->tx_napi_idx =
5698 					BNXT_RING_TO_TC(bp, txr->txq_index);
5699 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5700 				bnapi2->tx_int = bnxt_tx_int;
5701 			} else {
5702 				bnapi2 = bp->bnapi[j];
5703 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5704 				bnapi2->tx_ring[0] = txr;
5705 				bnapi2->tx_int = bnxt_tx_int_xdp;
5706 				j++;
5707 			}
5708 			txr->bnapi = bnapi2;
5709 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5710 				txr->tx_cpr = &bnapi2->cp_ring;
5711 		}
5712 
5713 		rc = bnxt_alloc_stats(bp);
5714 		if (rc)
5715 			goto alloc_mem_err;
5716 		bnxt_init_stats(bp);
5717 
5718 		rc = bnxt_alloc_ntp_fltrs(bp);
5719 		if (rc)
5720 			goto alloc_mem_err;
5721 
5722 		rc = bnxt_alloc_vnics(bp);
5723 		if (rc)
5724 			goto alloc_mem_err;
5725 	}
5726 
5727 	rc = bnxt_alloc_all_cp_arrays(bp);
5728 	if (rc)
5729 		goto alloc_mem_err;
5730 
5731 	bnxt_init_ring_struct(bp);
5732 
5733 	rc = bnxt_alloc_rx_rings(bp);
5734 	if (rc)
5735 		goto alloc_mem_err;
5736 
5737 	rc = bnxt_alloc_tx_rings(bp);
5738 	if (rc)
5739 		goto alloc_mem_err;
5740 
5741 	rc = bnxt_alloc_cp_rings(bp);
5742 	if (rc)
5743 		goto alloc_mem_err;
5744 
5745 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5746 						  BNXT_VNIC_MCAST_FLAG |
5747 						  BNXT_VNIC_UCAST_FLAG;
5748 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5749 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5750 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5751 
5752 	rc = bnxt_alloc_vnic_attributes(bp);
5753 	if (rc)
5754 		goto alloc_mem_err;
5755 	return 0;
5756 
5757 alloc_mem_err:
5758 	bnxt_free_mem(bp, true);
5759 	return rc;
5760 }
5761 
5762 static void bnxt_disable_int(struct bnxt *bp)
5763 {
5764 	int i;
5765 
5766 	if (!bp->bnapi)
5767 		return;
5768 
5769 	for (i = 0; i < bp->cp_nr_rings; i++) {
5770 		struct bnxt_napi *bnapi = bp->bnapi[i];
5771 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5772 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5773 
5774 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5775 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5776 	}
5777 }
5778 
5779 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5780 {
5781 	struct bnxt_napi *bnapi = bp->bnapi[n];
5782 	struct bnxt_cp_ring_info *cpr;
5783 
5784 	cpr = &bnapi->cp_ring;
5785 	return cpr->cp_ring_struct.map_idx;
5786 }
5787 
5788 static void bnxt_disable_int_sync(struct bnxt *bp)
5789 {
5790 	int i;
5791 
5792 	if (!bp->irq_tbl || !bp->bnapi)
5793 		return;
5794 
5795 	atomic_inc(&bp->intr_sem);
5796 
5797 	bnxt_disable_int(bp);
5798 	for (i = 0; i < bp->cp_nr_rings; i++) {
5799 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5800 
5801 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5802 	}
5803 }
5804 
5805 static void bnxt_enable_int(struct bnxt *bp)
5806 {
5807 	int i;
5808 
5809 	atomic_set(&bp->intr_sem, 0);
5810 	for (i = 0; i < bp->cp_nr_rings; i++) {
5811 		struct bnxt_napi *bnapi = bp->bnapi[i];
5812 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5813 
5814 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5815 	}
5816 }
5817 
5818 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5819 			    bool async_only)
5820 {
5821 	DECLARE_BITMAP(async_events_bmap, 256);
5822 	u32 *events = (u32 *)async_events_bmap;
5823 	struct hwrm_func_drv_rgtr_output *resp;
5824 	struct hwrm_func_drv_rgtr_input *req;
5825 	u32 flags;
5826 	int rc, i;
5827 
5828 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5829 	if (rc)
5830 		return rc;
5831 
5832 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5833 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5834 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5835 
5836 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5837 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5838 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5839 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5840 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5841 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5842 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5843 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5844 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5845 	req->flags = cpu_to_le32(flags);
5846 	req->ver_maj_8b = DRV_VER_MAJ;
5847 	req->ver_min_8b = DRV_VER_MIN;
5848 	req->ver_upd_8b = DRV_VER_UPD;
5849 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5850 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5851 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5852 
5853 	if (BNXT_PF(bp)) {
5854 		u32 data[8];
5855 		int i;
5856 
5857 		memset(data, 0, sizeof(data));
5858 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5859 			u16 cmd = bnxt_vf_req_snif[i];
5860 			unsigned int bit, idx;
5861 
5862 			if ((bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN) &&
5863 			    cmd == HWRM_PORT_PHY_QCFG)
5864 				continue;
5865 
5866 			idx = cmd / 32;
5867 			bit = cmd % 32;
5868 			data[idx] |= 1 << bit;
5869 		}
5870 
5871 		for (i = 0; i < 8; i++)
5872 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5873 
5874 		req->enables |=
5875 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5876 	}
5877 
5878 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5879 		req->flags |= cpu_to_le32(
5880 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5881 
5882 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5883 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5884 		u16 event_id = bnxt_async_events_arr[i];
5885 
5886 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5887 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5888 			continue;
5889 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5890 		    !bp->ptp_cfg)
5891 			continue;
5892 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5893 	}
5894 	if (bmap && bmap_size) {
5895 		for (i = 0; i < bmap_size; i++) {
5896 			if (test_bit(i, bmap))
5897 				__set_bit(i, async_events_bmap);
5898 		}
5899 	}
5900 	for (i = 0; i < 8; i++)
5901 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5902 
5903 	if (async_only)
5904 		req->enables =
5905 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5906 
5907 	resp = hwrm_req_hold(bp, req);
5908 	rc = hwrm_req_send(bp, req);
5909 	if (!rc) {
5910 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5911 		if (resp->flags &
5912 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5913 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5914 	}
5915 	hwrm_req_drop(bp, req);
5916 	return rc;
5917 }
5918 
5919 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5920 {
5921 	struct hwrm_func_drv_unrgtr_input *req;
5922 	int rc;
5923 
5924 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5925 		return 0;
5926 
5927 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5928 	if (rc)
5929 		return rc;
5930 	return hwrm_req_send(bp, req);
5931 }
5932 
5933 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5934 
5935 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5936 {
5937 	struct hwrm_tunnel_dst_port_free_input *req;
5938 	int rc;
5939 
5940 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5941 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5942 		return 0;
5943 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5944 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5945 		return 0;
5946 
5947 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5948 	if (rc)
5949 		return rc;
5950 
5951 	req->tunnel_type = tunnel_type;
5952 
5953 	switch (tunnel_type) {
5954 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5955 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5956 		bp->vxlan_port = 0;
5957 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5958 		break;
5959 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5960 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5961 		bp->nge_port = 0;
5962 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5963 		break;
5964 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5965 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5966 		bp->vxlan_gpe_port = 0;
5967 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5968 		break;
5969 	default:
5970 		break;
5971 	}
5972 
5973 	rc = hwrm_req_send(bp, req);
5974 	if (rc)
5975 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5976 			   rc);
5977 	if (bp->flags & BNXT_FLAG_TPA)
5978 		bnxt_set_tpa(bp, true);
5979 	return rc;
5980 }
5981 
5982 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5983 					   u8 tunnel_type)
5984 {
5985 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5986 	struct hwrm_tunnel_dst_port_alloc_input *req;
5987 	int rc;
5988 
5989 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5990 	if (rc)
5991 		return rc;
5992 
5993 	req->tunnel_type = tunnel_type;
5994 	req->tunnel_dst_port_val = port;
5995 
5996 	resp = hwrm_req_hold(bp, req);
5997 	rc = hwrm_req_send(bp, req);
5998 	if (rc) {
5999 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
6000 			   rc);
6001 		goto err_out;
6002 	}
6003 
6004 	switch (tunnel_type) {
6005 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
6006 		bp->vxlan_port = port;
6007 		bp->vxlan_fw_dst_port_id =
6008 			le16_to_cpu(resp->tunnel_dst_port_id);
6009 		break;
6010 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
6011 		bp->nge_port = port;
6012 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
6013 		break;
6014 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
6015 		bp->vxlan_gpe_port = port;
6016 		bp->vxlan_gpe_fw_dst_port_id =
6017 			le16_to_cpu(resp->tunnel_dst_port_id);
6018 		break;
6019 	default:
6020 		break;
6021 	}
6022 	if (bp->flags & BNXT_FLAG_TPA)
6023 		bnxt_set_tpa(bp, true);
6024 
6025 err_out:
6026 	hwrm_req_drop(bp, req);
6027 	return rc;
6028 }
6029 
6030 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
6031 {
6032 	struct hwrm_cfa_l2_set_rx_mask_input *req;
6033 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
6034 	int rc;
6035 
6036 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
6037 	if (rc)
6038 		return rc;
6039 
6040 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6041 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
6042 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
6043 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
6044 	}
6045 	req->mask = cpu_to_le32(vnic->rx_mask);
6046 	return hwrm_req_send_silent(bp, req);
6047 }
6048 
6049 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6050 {
6051 	if (!atomic_dec_and_test(&fltr->refcnt))
6052 		return;
6053 	spin_lock_bh(&bp->ntp_fltr_lock);
6054 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
6055 		spin_unlock_bh(&bp->ntp_fltr_lock);
6056 		return;
6057 	}
6058 	hlist_del_rcu(&fltr->base.hash);
6059 	bnxt_del_one_usr_fltr(bp, &fltr->base);
6060 	if (fltr->base.flags) {
6061 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
6062 		bp->ntp_fltr_count--;
6063 	}
6064 	spin_unlock_bh(&bp->ntp_fltr_lock);
6065 	kfree_rcu(fltr, base.rcu);
6066 }
6067 
6068 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
6069 						      struct bnxt_l2_key *key,
6070 						      u32 idx)
6071 {
6072 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
6073 	struct bnxt_l2_filter *fltr;
6074 
6075 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
6076 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
6077 
6078 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
6079 		    l2_key->vlan == key->vlan)
6080 			return fltr;
6081 	}
6082 	return NULL;
6083 }
6084 
6085 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
6086 						    struct bnxt_l2_key *key,
6087 						    u32 idx)
6088 {
6089 	struct bnxt_l2_filter *fltr = NULL;
6090 
6091 	rcu_read_lock();
6092 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6093 	if (fltr)
6094 		atomic_inc(&fltr->refcnt);
6095 	rcu_read_unlock();
6096 	return fltr;
6097 }
6098 
6099 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
6100 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6101 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
6102 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6103 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
6104 
6105 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
6106 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
6107 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
6108 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
6109 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
6110 
6111 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
6112 {
6113 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6114 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
6115 			return sizeof(fkeys->addrs.v4addrs) +
6116 			       sizeof(fkeys->ports);
6117 
6118 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
6119 			return sizeof(fkeys->addrs.v4addrs);
6120 	}
6121 
6122 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
6123 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
6124 			return sizeof(fkeys->addrs.v6addrs) +
6125 			       sizeof(fkeys->ports);
6126 
6127 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
6128 			return sizeof(fkeys->addrs.v6addrs);
6129 	}
6130 
6131 	return 0;
6132 }
6133 
6134 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
6135 			 const unsigned char *key)
6136 {
6137 	u64 prefix = bp->toeplitz_prefix, hash = 0;
6138 	struct bnxt_ipv4_tuple tuple4;
6139 	struct bnxt_ipv6_tuple tuple6;
6140 	int i, j, len = 0;
6141 	u8 *four_tuple;
6142 
6143 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
6144 	if (!len)
6145 		return 0;
6146 
6147 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
6148 		tuple4.v4addrs = fkeys->addrs.v4addrs;
6149 		tuple4.ports = fkeys->ports;
6150 		four_tuple = (unsigned char *)&tuple4;
6151 	} else {
6152 		tuple6.v6addrs = fkeys->addrs.v6addrs;
6153 		tuple6.ports = fkeys->ports;
6154 		four_tuple = (unsigned char *)&tuple6;
6155 	}
6156 
6157 	for (i = 0, j = 8; i < len; i++, j++) {
6158 		u8 byte = four_tuple[i];
6159 		int bit;
6160 
6161 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
6162 			if (byte & 0x80)
6163 				hash ^= prefix;
6164 		}
6165 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
6166 	}
6167 
6168 	/* The valid part of the hash is in the upper 32 bits. */
6169 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
6170 }
6171 
6172 #ifdef CONFIG_RFS_ACCEL
6173 static struct bnxt_l2_filter *
6174 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
6175 {
6176 	struct bnxt_l2_filter *fltr;
6177 	u32 idx;
6178 
6179 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6180 	      BNXT_L2_FLTR_HASH_MASK;
6181 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6182 	return fltr;
6183 }
6184 #endif
6185 
6186 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6187 			       struct bnxt_l2_key *key, u32 idx)
6188 {
6189 	struct hlist_head *head;
6190 
6191 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6192 	fltr->l2_key.vlan = key->vlan;
6193 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6194 	if (fltr->base.flags) {
6195 		int bit_id;
6196 
6197 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6198 						 bp->max_fltr, 0);
6199 		if (bit_id < 0)
6200 			return -ENOMEM;
6201 		fltr->base.sw_id = (u16)bit_id;
6202 		bp->ntp_fltr_count++;
6203 	}
6204 	head = &bp->l2_fltr_hash_tbl[idx];
6205 	hlist_add_head_rcu(&fltr->base.hash, head);
6206 	bnxt_insert_usr_fltr(bp, &fltr->base);
6207 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6208 	atomic_set(&fltr->refcnt, 1);
6209 	return 0;
6210 }
6211 
6212 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6213 						   struct bnxt_l2_key *key,
6214 						   gfp_t gfp)
6215 {
6216 	struct bnxt_l2_filter *fltr;
6217 	u32 idx;
6218 	int rc;
6219 
6220 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6221 	      BNXT_L2_FLTR_HASH_MASK;
6222 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6223 	if (fltr)
6224 		return fltr;
6225 
6226 	fltr = kzalloc_obj(*fltr, gfp);
6227 	if (!fltr)
6228 		return ERR_PTR(-ENOMEM);
6229 	spin_lock_bh(&bp->ntp_fltr_lock);
6230 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6231 	spin_unlock_bh(&bp->ntp_fltr_lock);
6232 	if (rc) {
6233 		bnxt_del_l2_filter(bp, fltr);
6234 		fltr = ERR_PTR(rc);
6235 	}
6236 	return fltr;
6237 }
6238 
6239 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6240 						struct bnxt_l2_key *key,
6241 						u16 flags)
6242 {
6243 	struct bnxt_l2_filter *fltr;
6244 	u32 idx;
6245 	int rc;
6246 
6247 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6248 	      BNXT_L2_FLTR_HASH_MASK;
6249 	spin_lock_bh(&bp->ntp_fltr_lock);
6250 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6251 	if (fltr) {
6252 		fltr = ERR_PTR(-EEXIST);
6253 		goto l2_filter_exit;
6254 	}
6255 	fltr = kzalloc_obj(*fltr, GFP_ATOMIC);
6256 	if (!fltr) {
6257 		fltr = ERR_PTR(-ENOMEM);
6258 		goto l2_filter_exit;
6259 	}
6260 	fltr->base.flags = flags;
6261 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6262 	if (rc) {
6263 		spin_unlock_bh(&bp->ntp_fltr_lock);
6264 		bnxt_del_l2_filter(bp, fltr);
6265 		return ERR_PTR(rc);
6266 	}
6267 
6268 l2_filter_exit:
6269 	spin_unlock_bh(&bp->ntp_fltr_lock);
6270 	return fltr;
6271 }
6272 
6273 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6274 {
6275 #ifdef CONFIG_BNXT_SRIOV
6276 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6277 
6278 	return vf->fw_fid;
6279 #else
6280 	return INVALID_HW_RING_ID;
6281 #endif
6282 }
6283 
6284 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6285 {
6286 	struct hwrm_cfa_l2_filter_free_input *req;
6287 	u16 target_id = 0xffff;
6288 	int rc;
6289 
6290 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6291 		struct bnxt_pf_info *pf = &bp->pf;
6292 
6293 		if (fltr->base.vf_idx >= pf->active_vfs)
6294 			return -EINVAL;
6295 
6296 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6297 		if (target_id == INVALID_HW_RING_ID)
6298 			return -EINVAL;
6299 	}
6300 
6301 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6302 	if (rc)
6303 		return rc;
6304 
6305 	req->target_id = cpu_to_le16(target_id);
6306 	req->l2_filter_id = fltr->base.filter_id;
6307 	return hwrm_req_send(bp, req);
6308 }
6309 
6310 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6311 {
6312 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6313 	struct hwrm_cfa_l2_filter_alloc_input *req;
6314 	u16 target_id = 0xffff;
6315 	int rc;
6316 
6317 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6318 		struct bnxt_pf_info *pf = &bp->pf;
6319 
6320 		if (fltr->base.vf_idx >= pf->active_vfs)
6321 			return -EINVAL;
6322 
6323 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6324 	}
6325 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6326 	if (rc)
6327 		return rc;
6328 
6329 	req->target_id = cpu_to_le16(target_id);
6330 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6331 
6332 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6333 		req->flags |=
6334 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6335 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6336 	req->enables =
6337 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6338 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6339 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6340 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6341 	eth_broadcast_addr(req->l2_addr_mask);
6342 
6343 	if (fltr->l2_key.vlan) {
6344 		req->enables |=
6345 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6346 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6347 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6348 		req->num_vlans = 1;
6349 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6350 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6351 	}
6352 
6353 	resp = hwrm_req_hold(bp, req);
6354 	rc = hwrm_req_send(bp, req);
6355 	if (!rc) {
6356 		fltr->base.filter_id = resp->l2_filter_id;
6357 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6358 	}
6359 	hwrm_req_drop(bp, req);
6360 	return rc;
6361 }
6362 
6363 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6364 				     struct bnxt_ntuple_filter *fltr)
6365 {
6366 	struct hwrm_cfa_ntuple_filter_free_input *req;
6367 	int rc;
6368 
6369 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6370 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
6371 		return 0;
6372 
6373 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6374 	if (rc)
6375 		return rc;
6376 
6377 	req->ntuple_filter_id = fltr->base.filter_id;
6378 	return hwrm_req_send(bp, req);
6379 }
6380 
6381 #define BNXT_NTP_FLTR_FLAGS					\
6382 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6383 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6384 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6385 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6386 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6387 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6388 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6389 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6390 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6391 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6392 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6393 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6394 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6395 
6396 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6397 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6398 
6399 void bnxt_fill_ipv6_mask(__be32 mask[4])
6400 {
6401 	int i;
6402 
6403 	for (i = 0; i < 4; i++)
6404 		mask[i] = cpu_to_be32(~0);
6405 }
6406 
6407 static void
6408 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6409 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6410 			  struct bnxt_ntuple_filter *fltr)
6411 {
6412 	u16 rxq = fltr->base.rxq;
6413 
6414 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6415 		struct ethtool_rxfh_context *ctx;
6416 		struct bnxt_rss_ctx *rss_ctx;
6417 		struct bnxt_vnic_info *vnic;
6418 
6419 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6420 			      fltr->base.fw_vnic_id);
6421 		if (ctx) {
6422 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6423 			vnic = &rss_ctx->vnic;
6424 
6425 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6426 		}
6427 		return;
6428 	}
6429 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6430 		struct bnxt_vnic_info *vnic;
6431 		u32 enables;
6432 
6433 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6434 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6435 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6436 		req->enables |= cpu_to_le32(enables);
6437 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6438 	} else {
6439 		u32 flags;
6440 
6441 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6442 		req->flags |= cpu_to_le32(flags);
6443 		req->dst_id = cpu_to_le16(rxq);
6444 	}
6445 }
6446 
6447 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6448 				      struct bnxt_ntuple_filter *fltr)
6449 {
6450 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6451 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6452 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6453 	struct flow_keys *keys = &fltr->fkeys;
6454 	struct bnxt_l2_filter *l2_fltr;
6455 	struct bnxt_vnic_info *vnic;
6456 	int rc;
6457 
6458 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6459 	if (rc)
6460 		return rc;
6461 
6462 	l2_fltr = fltr->l2_fltr;
6463 	req->l2_filter_id = l2_fltr->base.filter_id;
6464 
6465 	if (fltr->base.flags & BNXT_ACT_DROP) {
6466 		req->flags =
6467 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6468 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6469 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6470 	} else {
6471 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6472 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6473 	}
6474 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6475 
6476 	req->ethertype = htons(ETH_P_IP);
6477 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6478 	req->ip_protocol = keys->basic.ip_proto;
6479 
6480 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6481 		req->ethertype = htons(ETH_P_IPV6);
6482 		req->ip_addr_type =
6483 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6484 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6485 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6486 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6487 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6488 	} else {
6489 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6490 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6491 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6492 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6493 	}
6494 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6495 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6496 		req->tunnel_type =
6497 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6498 	}
6499 
6500 	req->src_port = keys->ports.src;
6501 	req->src_port_mask = masks->ports.src;
6502 	req->dst_port = keys->ports.dst;
6503 	req->dst_port_mask = masks->ports.dst;
6504 
6505 	resp = hwrm_req_hold(bp, req);
6506 	rc = hwrm_req_send(bp, req);
6507 	if (!rc)
6508 		fltr->base.filter_id = resp->ntuple_filter_id;
6509 	hwrm_req_drop(bp, req);
6510 	return rc;
6511 }
6512 
6513 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6514 				     const u8 *mac_addr)
6515 {
6516 	struct bnxt_l2_filter *fltr;
6517 	struct bnxt_l2_key key;
6518 	int rc;
6519 
6520 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6521 	key.vlan = 0;
6522 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6523 	if (IS_ERR(fltr))
6524 		return PTR_ERR(fltr);
6525 
6526 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6527 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6528 	if (rc)
6529 		bnxt_del_l2_filter(bp, fltr);
6530 	else
6531 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6532 	return rc;
6533 }
6534 
6535 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6536 {
6537 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6538 
6539 	/* Any associated ntuple filters will also be cleared by firmware. */
6540 	for (i = 0; i < num_of_vnics; i++) {
6541 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6542 
6543 		for (j = 0; j < vnic->uc_filter_count; j++) {
6544 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6545 
6546 			bnxt_hwrm_l2_filter_free(bp, fltr);
6547 			bnxt_del_l2_filter(bp, fltr);
6548 		}
6549 		vnic->uc_filter_count = 0;
6550 	}
6551 }
6552 
6553 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6554 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6555 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6556 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6557 
6558 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6559 					   struct hwrm_vnic_tpa_cfg_input *req)
6560 {
6561 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6562 
6563 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6564 		return;
6565 
6566 	if (bp->vxlan_port)
6567 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6568 	if (bp->vxlan_gpe_port)
6569 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6570 	if (bp->nge_port)
6571 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6572 
6573 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6574 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6575 }
6576 
6577 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6578 			   u32 tpa_flags)
6579 {
6580 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6581 	struct hwrm_vnic_tpa_cfg_input *req;
6582 	int rc;
6583 
6584 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6585 		return 0;
6586 
6587 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6588 	if (rc)
6589 		return rc;
6590 
6591 	if (tpa_flags) {
6592 		u16 mss = bp->dev->mtu - 40;
6593 		u32 nsegs, n, segs = 0, flags;
6594 
6595 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6596 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6597 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6598 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6599 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6600 		if (tpa_flags & BNXT_FLAG_GRO)
6601 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6602 
6603 		req->flags = cpu_to_le32(flags);
6604 
6605 		req->enables =
6606 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6607 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6608 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6609 
6610 		/* Number of segs are log2 units, and first packet is not
6611 		 * included as part of this units.
6612 		 */
6613 		if (mss <= BNXT_RX_PAGE_SIZE) {
6614 			n = BNXT_RX_PAGE_SIZE / mss;
6615 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6616 		} else {
6617 			n = mss / BNXT_RX_PAGE_SIZE;
6618 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6619 				n++;
6620 			nsegs = (MAX_SKB_FRAGS - n) / n;
6621 		}
6622 
6623 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6624 			segs = MAX_TPA_SEGS_P5;
6625 			max_aggs = bp->max_tpa;
6626 		} else {
6627 			segs = ilog2(nsegs);
6628 		}
6629 		req->max_agg_segs = cpu_to_le16(segs);
6630 		req->max_aggs = cpu_to_le16(max_aggs);
6631 
6632 		req->min_agg_len = cpu_to_le32(512);
6633 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6634 	}
6635 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6636 
6637 	return hwrm_req_send(bp, req);
6638 }
6639 
6640 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6641 {
6642 	struct bnxt_ring_grp_info *grp_info;
6643 
6644 	grp_info = &bp->grp_info[ring->grp_idx];
6645 	return grp_info->cp_fw_ring_id;
6646 }
6647 
6648 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6649 {
6650 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6651 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6652 	else
6653 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6654 }
6655 
6656 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6657 {
6658 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6659 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6660 	else
6661 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6662 }
6663 
6664 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6665 {
6666 	int entries;
6667 
6668 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6669 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6670 	else
6671 		entries = HW_HASH_INDEX_SIZE;
6672 
6673 	bp->rss_indir_tbl_entries = entries;
6674 	bp->rss_indir_tbl =
6675 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6676 	if (!bp->rss_indir_tbl)
6677 		return -ENOMEM;
6678 
6679 	return 0;
6680 }
6681 
6682 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6683 				 struct ethtool_rxfh_context *rss_ctx)
6684 {
6685 	u16 max_rings, max_entries, pad, i;
6686 	u32 *rss_indir_tbl;
6687 
6688 	if (!bp->rx_nr_rings)
6689 		return;
6690 
6691 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6692 		max_rings = bp->rx_nr_rings - 1;
6693 	else
6694 		max_rings = bp->rx_nr_rings;
6695 
6696 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6697 	if (rss_ctx)
6698 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6699 	else
6700 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6701 
6702 	for (i = 0; i < max_entries; i++)
6703 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6704 
6705 	pad = bp->rss_indir_tbl_entries - max_entries;
6706 	if (pad)
6707 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6708 }
6709 
6710 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6711 {
6712 	u32 i, tbl_size, max_ring = 0;
6713 
6714 	if (!bp->rss_indir_tbl)
6715 		return 0;
6716 
6717 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6718 	for (i = 0; i < tbl_size; i++)
6719 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6720 	return max_ring;
6721 }
6722 
6723 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6724 {
6725 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6726 		if (!rx_rings)
6727 			return 0;
6728 		if (bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX)
6729 			return BNXT_RSS_TABLE_MAX_TBL_P5;
6730 
6731 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6732 					       BNXT_RSS_TABLE_ENTRIES_P5);
6733 	}
6734 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6735 		return 2;
6736 	return 1;
6737 }
6738 
6739 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6740 {
6741 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6742 	u16 i, j, min_j = bp->rx_nr_rings - 1;
6743 
6744 	if (!vnic->rss_table)
6745 		goto skip_rss_tbl;
6746 
6747 	/* Fill the RSS indirection table with ring group ids */
6748 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6749 		if (!no_rss)
6750 			j = bp->rss_indir_tbl[i];
6751 		min_j = min(j, min_j);
6752 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6753 	}
6754 
6755 skip_rss_tbl:
6756 	if (vnic->rss_table && !no_rss)
6757 		vnic->default_rx_ring = min_j;
6758 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6759 		vnic->default_rx_ring = vnic->vnic_id - 1;
6760 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6761 		vnic->default_rx_ring = bp->rx_nr_rings - 1;
6762 	else
6763 		vnic->default_rx_ring = 0;
6764 }
6765 
6766 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6767 				    struct bnxt_vnic_info *vnic)
6768 {
6769 	u16 tbl_size, i, min_j = bp->rx_nr_rings - 1;
6770 	__le16 *ring_tbl = vnic->rss_table;
6771 	struct bnxt_rx_ring_info *rxr;
6772 
6773 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6774 
6775 	for (i = 0; i < tbl_size; i++) {
6776 		u16 ring_id, j;
6777 
6778 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6779 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6780 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6781 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6782 		else
6783 			j = bp->rss_indir_tbl[i];
6784 		min_j = min(j, min_j);
6785 		rxr = &bp->rx_ring[j];
6786 
6787 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6788 		*ring_tbl++ = cpu_to_le16(ring_id);
6789 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6790 		*ring_tbl++ = cpu_to_le16(ring_id);
6791 	}
6792 	vnic->default_rx_ring = min_j;
6793 }
6794 
6795 static void
6796 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6797 			 struct bnxt_vnic_info *vnic)
6798 {
6799 	if (bp->flags & BNXT_FLAG_CHIP_P7)
6800 		req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6801 
6802 	if (bp->rss_hash_delta) {
6803 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6804 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6805 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6806 		else
6807 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6808 	} else {
6809 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6810 	}
6811 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6812 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6813 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6814 }
6815 
6816 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6817 				  bool set_rss)
6818 {
6819 	struct hwrm_vnic_rss_cfg_input *req;
6820 	int rc;
6821 
6822 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6823 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6824 		return 0;
6825 
6826 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6827 	if (rc)
6828 		return rc;
6829 
6830 	if (set_rss)
6831 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6832 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6833 	return hwrm_req_send(bp, req);
6834 }
6835 
6836 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6837 				     struct bnxt_vnic_info *vnic, bool set_rss)
6838 {
6839 	struct hwrm_vnic_rss_cfg_input *req;
6840 	dma_addr_t ring_tbl_map;
6841 	u32 i, nr_ctxs;
6842 	int rc;
6843 
6844 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6845 	if (rc)
6846 		return rc;
6847 
6848 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6849 	if (!set_rss)
6850 		return hwrm_req_send(bp, req);
6851 
6852 	bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6853 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6854 	ring_tbl_map = vnic->rss_table_dma_addr;
6855 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6856 
6857 	hwrm_req_hold(bp, req);
6858 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6859 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6860 		req->ring_table_pair_index = i;
6861 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6862 		rc = hwrm_req_send(bp, req);
6863 		if (rc)
6864 			goto exit;
6865 	}
6866 
6867 exit:
6868 	hwrm_req_drop(bp, req);
6869 	return rc;
6870 }
6871 
6872 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6873 {
6874 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6875 	struct hwrm_vnic_rss_qcfg_output *resp;
6876 	struct hwrm_vnic_rss_qcfg_input *req;
6877 
6878 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6879 		return;
6880 
6881 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6882 	/* all contexts configured to same hash_type, zero always exists */
6883 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6884 	resp = hwrm_req_hold(bp, req);
6885 	if (!hwrm_req_send(bp, req)) {
6886 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6887 		bp->rss_hash_delta = 0;
6888 	}
6889 	hwrm_req_drop(bp, req);
6890 }
6891 
6892 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6893 {
6894 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6895 	struct hwrm_vnic_plcmodes_cfg_input *req;
6896 	int rc;
6897 
6898 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6899 	if (rc)
6900 		return rc;
6901 
6902 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6903 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6904 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6905 
6906 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6907 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6908 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6909 		req->enables |=
6910 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6911 		req->hds_threshold = cpu_to_le16(hds_thresh);
6912 	}
6913 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6914 	return hwrm_req_send(bp, req);
6915 }
6916 
6917 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6918 					struct bnxt_vnic_info *vnic,
6919 					u16 ctx_idx)
6920 {
6921 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6922 
6923 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6924 		return;
6925 
6926 	req->rss_cos_lb_ctx_id =
6927 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6928 
6929 	hwrm_req_send(bp, req);
6930 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6931 }
6932 
6933 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6934 {
6935 	int i, j;
6936 
6937 	for (i = 0; i < bp->nr_vnics; i++) {
6938 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6939 
6940 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6941 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6942 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6943 		}
6944 	}
6945 	bp->rsscos_nr_ctxs = 0;
6946 }
6947 
6948 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6949 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6950 {
6951 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6952 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6953 	int rc;
6954 
6955 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6956 	if (rc)
6957 		return rc;
6958 
6959 	resp = hwrm_req_hold(bp, req);
6960 	rc = hwrm_req_send(bp, req);
6961 	if (!rc)
6962 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6963 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6964 	hwrm_req_drop(bp, req);
6965 
6966 	return rc;
6967 }
6968 
6969 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6970 {
6971 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6972 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6973 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6974 }
6975 
6976 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6977 {
6978 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6979 	struct hwrm_vnic_cfg_input *req;
6980 	unsigned int ring = 0, grp_idx;
6981 	u16 def_vlan = 0;
6982 	int rc;
6983 
6984 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6985 	if (rc)
6986 		return rc;
6987 
6988 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6989 		struct bnxt_rx_ring_info *rxr;
6990 
6991 		rxr = &bp->rx_ring[vnic->default_rx_ring];
6992 		req->default_rx_ring_id =
6993 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6994 		req->default_cmpl_ring_id =
6995 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6996 		req->enables =
6997 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6998 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6999 		goto vnic_mru;
7000 	}
7001 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
7002 	/* Only RSS support for now TBD: COS & LB */
7003 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
7004 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
7005 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
7006 					   VNIC_CFG_REQ_ENABLES_MRU);
7007 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
7008 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
7009 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
7010 					   VNIC_CFG_REQ_ENABLES_MRU);
7011 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
7012 	} else {
7013 		req->rss_rule = cpu_to_le16(0xffff);
7014 	}
7015 
7016 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
7017 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
7018 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
7019 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
7020 	} else {
7021 		req->cos_rule = cpu_to_le16(0xffff);
7022 	}
7023 
7024 	ring = vnic->default_rx_ring;
7025 	grp_idx = bp->rx_ring[ring].bnapi->index;
7026 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
7027 	req->lb_rule = cpu_to_le16(0xffff);
7028 vnic_mru:
7029 	vnic->mru = bp->dev->mtu + VLAN_ETH_HLEN;
7030 	req->mru = cpu_to_le16(vnic->mru);
7031 
7032 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
7033 #ifdef CONFIG_BNXT_SRIOV
7034 	if (BNXT_VF(bp))
7035 		def_vlan = bp->vf.vlan;
7036 #endif
7037 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
7038 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
7039 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT &&
7040 	    bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA]))
7041 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
7042 
7043 	return hwrm_req_send(bp, req);
7044 }
7045 
7046 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
7047 				    struct bnxt_vnic_info *vnic)
7048 {
7049 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
7050 		struct hwrm_vnic_free_input *req;
7051 
7052 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
7053 			return;
7054 
7055 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
7056 
7057 		hwrm_req_send(bp, req);
7058 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
7059 	}
7060 }
7061 
7062 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
7063 {
7064 	u16 i;
7065 
7066 	for (i = 0; i < bp->nr_vnics; i++)
7067 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
7068 }
7069 
7070 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
7071 			 unsigned int start_rx_ring_idx,
7072 			 unsigned int nr_rings)
7073 {
7074 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
7075 	struct hwrm_vnic_alloc_output *resp;
7076 	struct hwrm_vnic_alloc_input *req;
7077 	int rc;
7078 
7079 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
7080 	if (rc)
7081 		return rc;
7082 
7083 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7084 		goto vnic_no_ring_grps;
7085 
7086 	/* map ring groups to this vnic */
7087 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
7088 		grp_idx = bp->rx_ring[i].bnapi->index;
7089 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
7090 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
7091 				   j, nr_rings);
7092 			break;
7093 		}
7094 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
7095 	}
7096 
7097 vnic_no_ring_grps:
7098 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
7099 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
7100 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
7101 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
7102 
7103 	resp = hwrm_req_hold(bp, req);
7104 	rc = hwrm_req_send(bp, req);
7105 	if (!rc)
7106 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
7107 	hwrm_req_drop(bp, req);
7108 	return rc;
7109 }
7110 
7111 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
7112 {
7113 	struct hwrm_vnic_qcaps_output *resp;
7114 	struct hwrm_vnic_qcaps_input *req;
7115 	int rc;
7116 
7117 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
7118 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
7119 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
7120 	if (bp->hwrm_spec_code < 0x10600)
7121 		return 0;
7122 
7123 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
7124 	if (rc)
7125 		return rc;
7126 
7127 	resp = hwrm_req_hold(bp, req);
7128 	rc = hwrm_req_send(bp, req);
7129 	if (!rc) {
7130 		u32 flags = le32_to_cpu(resp->flags);
7131 
7132 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
7133 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
7134 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
7135 		if (flags &
7136 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
7137 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
7138 
7139 		/* Older P5 fw before EXT_HW_STATS support did not set
7140 		 * VLAN_STRIP_CAP properly.
7141 		 */
7142 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
7143 		    (BNXT_CHIP_P5(bp) &&
7144 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
7145 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
7146 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
7147 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
7148 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
7149 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
7150 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
7151 		if (bp->max_tpa_v2) {
7152 			if (BNXT_CHIP_P5(bp))
7153 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
7154 			else
7155 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
7156 		}
7157 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
7158 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
7159 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
7160 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
7161 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
7162 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
7163 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
7164 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
7165 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
7166 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
7167 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
7168 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
7169 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
7170 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
7171 	}
7172 	hwrm_req_drop(bp, req);
7173 	return rc;
7174 }
7175 
7176 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
7177 {
7178 	struct hwrm_ring_grp_alloc_output *resp;
7179 	struct hwrm_ring_grp_alloc_input *req;
7180 	int rc;
7181 	u16 i;
7182 
7183 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7184 		return 0;
7185 
7186 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
7187 	if (rc)
7188 		return rc;
7189 
7190 	resp = hwrm_req_hold(bp, req);
7191 	for (i = 0; i < bp->rx_nr_rings; i++) {
7192 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7193 
7194 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7195 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7196 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7197 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7198 
7199 		rc = hwrm_req_send(bp, req);
7200 
7201 		if (rc)
7202 			break;
7203 
7204 		bp->grp_info[grp_idx].fw_grp_id =
7205 			le32_to_cpu(resp->ring_group_id);
7206 	}
7207 	hwrm_req_drop(bp, req);
7208 	return rc;
7209 }
7210 
7211 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7212 {
7213 	struct hwrm_ring_grp_free_input *req;
7214 	u16 i;
7215 
7216 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7217 		return;
7218 
7219 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7220 		return;
7221 
7222 	hwrm_req_hold(bp, req);
7223 	for (i = 0; i < bp->cp_nr_rings; i++) {
7224 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7225 			continue;
7226 		req->ring_group_id =
7227 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7228 
7229 		hwrm_req_send(bp, req);
7230 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7231 	}
7232 	hwrm_req_drop(bp, req);
7233 }
7234 
7235 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7236 				       struct hwrm_ring_alloc_input *req,
7237 				       struct bnxt_rx_ring_info *rxr,
7238 				       struct bnxt_ring_struct *ring)
7239 {
7240 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7241 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7242 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7243 
7244 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7245 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7246 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7247 		req->rx_buf_size = cpu_to_le16(rxr->rx_page_size);
7248 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7249 	} else {
7250 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7251 		if (NET_IP_ALIGN == 2)
7252 			req->flags =
7253 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7254 	}
7255 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7256 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7257 	req->enables |= cpu_to_le32(enables);
7258 }
7259 
7260 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7261 				    struct bnxt_rx_ring_info *rxr,
7262 				    struct bnxt_ring_struct *ring,
7263 				    u32 ring_type, u32 map_index)
7264 {
7265 	struct hwrm_ring_alloc_output *resp;
7266 	struct hwrm_ring_alloc_input *req;
7267 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7268 	struct bnxt_ring_grp_info *grp_info;
7269 	int rc, err = 0;
7270 	u16 ring_id;
7271 
7272 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7273 	if (rc)
7274 		goto exit;
7275 
7276 	req->enables = 0;
7277 	if (rmem->nr_pages > 1) {
7278 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7279 		/* Page size is in log2 units */
7280 		req->page_size = BNXT_PAGE_SHIFT;
7281 		req->page_tbl_depth = 1;
7282 	} else {
7283 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7284 	}
7285 	req->fbo = 0;
7286 	/* Association of ring index with doorbell index and MSIX number */
7287 	req->logical_id = cpu_to_le16(map_index);
7288 
7289 	switch (ring_type) {
7290 	case HWRM_RING_ALLOC_TX: {
7291 		struct bnxt_tx_ring_info *txr;
7292 		u16 flags = 0;
7293 
7294 		txr = container_of(ring, struct bnxt_tx_ring_info,
7295 				   tx_ring_struct);
7296 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7297 		/* Association of transmit ring with completion ring */
7298 		grp_info = &bp->grp_info[ring->grp_idx];
7299 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7300 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7301 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7302 		req->queue_id = cpu_to_le16(ring->queue_id);
7303 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7304 			req->cmpl_coal_cnt =
7305 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7306 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7307 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7308 		req->flags = cpu_to_le16(flags);
7309 		break;
7310 	}
7311 	case HWRM_RING_ALLOC_RX:
7312 	case HWRM_RING_ALLOC_AGG:
7313 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7314 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7315 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7316 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7317 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7318 			bnxt_set_rx_ring_params_p5(bp, ring_type, req,
7319 						   rxr, ring);
7320 		break;
7321 	case HWRM_RING_ALLOC_CMPL:
7322 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7323 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7324 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7325 			/* Association of cp ring with nq */
7326 			grp_info = &bp->grp_info[map_index];
7327 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7328 			req->cq_handle = cpu_to_le64(ring->handle);
7329 			req->enables |= cpu_to_le32(
7330 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7331 		} else {
7332 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7333 		}
7334 		break;
7335 	case HWRM_RING_ALLOC_NQ:
7336 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7337 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7338 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7339 		break;
7340 	default:
7341 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7342 			   ring_type);
7343 		return -EINVAL;
7344 	}
7345 
7346 	resp = hwrm_req_hold(bp, req);
7347 	rc = hwrm_req_send(bp, req);
7348 	err = le16_to_cpu(resp->error_code);
7349 	ring_id = le16_to_cpu(resp->ring_id);
7350 	hwrm_req_drop(bp, req);
7351 
7352 exit:
7353 	if (rc || err) {
7354 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7355 			   ring_type, rc, err);
7356 		return -EIO;
7357 	}
7358 	ring->fw_ring_id = ring_id;
7359 	return rc;
7360 }
7361 
7362 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7363 {
7364 	int rc;
7365 
7366 	if (BNXT_PF(bp)) {
7367 		struct hwrm_func_cfg_input *req;
7368 
7369 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7370 		if (rc)
7371 			return rc;
7372 
7373 		req->fid = cpu_to_le16(0xffff);
7374 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7375 		req->async_event_cr = cpu_to_le16(idx);
7376 		return hwrm_req_send(bp, req);
7377 	} else {
7378 		struct hwrm_func_vf_cfg_input *req;
7379 
7380 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7381 		if (rc)
7382 			return rc;
7383 
7384 		req->enables =
7385 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7386 		req->async_event_cr = cpu_to_le16(idx);
7387 		return hwrm_req_send(bp, req);
7388 	}
7389 }
7390 
7391 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7392 			     u32 ring_type)
7393 {
7394 	switch (ring_type) {
7395 	case HWRM_RING_ALLOC_TX:
7396 		db->db_ring_mask = bp->tx_ring_mask;
7397 		break;
7398 	case HWRM_RING_ALLOC_RX:
7399 		db->db_ring_mask = bp->rx_ring_mask;
7400 		break;
7401 	case HWRM_RING_ALLOC_AGG:
7402 		db->db_ring_mask = bp->rx_agg_ring_mask;
7403 		break;
7404 	case HWRM_RING_ALLOC_CMPL:
7405 	case HWRM_RING_ALLOC_NQ:
7406 		db->db_ring_mask = bp->cp_ring_mask;
7407 		break;
7408 	}
7409 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7410 		db->db_epoch_mask = db->db_ring_mask + 1;
7411 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7412 	}
7413 }
7414 
7415 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7416 			u32 map_idx, u32 xid)
7417 {
7418 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7419 		switch (ring_type) {
7420 		case HWRM_RING_ALLOC_TX:
7421 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7422 			break;
7423 		case HWRM_RING_ALLOC_RX:
7424 		case HWRM_RING_ALLOC_AGG:
7425 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7426 			break;
7427 		case HWRM_RING_ALLOC_CMPL:
7428 			db->db_key64 = DBR_PATH_L2;
7429 			break;
7430 		case HWRM_RING_ALLOC_NQ:
7431 			db->db_key64 = DBR_PATH_L2;
7432 			break;
7433 		}
7434 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7435 
7436 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7437 			db->db_key64 |= DBR_VALID;
7438 
7439 		db->doorbell = bp->bar1 + bp->db_offset;
7440 	} else {
7441 		db->doorbell = bp->bar1 + map_idx * 0x80;
7442 		switch (ring_type) {
7443 		case HWRM_RING_ALLOC_TX:
7444 			db->db_key32 = DB_KEY_TX;
7445 			break;
7446 		case HWRM_RING_ALLOC_RX:
7447 		case HWRM_RING_ALLOC_AGG:
7448 			db->db_key32 = DB_KEY_RX;
7449 			break;
7450 		case HWRM_RING_ALLOC_CMPL:
7451 			db->db_key32 = DB_KEY_CP;
7452 			break;
7453 		}
7454 	}
7455 	bnxt_set_db_mask(bp, db, ring_type);
7456 }
7457 
7458 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7459 				   struct bnxt_rx_ring_info *rxr)
7460 {
7461 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7462 	struct bnxt_napi *bnapi = rxr->bnapi;
7463 	u32 type = HWRM_RING_ALLOC_RX;
7464 	u32 map_idx = bnapi->index;
7465 	int rc;
7466 
7467 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7468 	if (rc)
7469 		return rc;
7470 
7471 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7472 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7473 
7474 	return 0;
7475 }
7476 
7477 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7478 				       struct bnxt_rx_ring_info *rxr)
7479 {
7480 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7481 	u32 type = HWRM_RING_ALLOC_AGG;
7482 	u32 grp_idx = ring->grp_idx;
7483 	u32 map_idx;
7484 	int rc;
7485 
7486 	map_idx = grp_idx + bp->rx_nr_rings;
7487 	rc = hwrm_ring_alloc_send_msg(bp, rxr, ring, type, map_idx);
7488 	if (rc)
7489 		return rc;
7490 
7491 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7492 		    ring->fw_ring_id);
7493 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7494 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7495 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7496 
7497 	return 0;
7498 }
7499 
7500 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7501 				      struct bnxt_cp_ring_info *cpr)
7502 {
7503 	const u32 type = HWRM_RING_ALLOC_CMPL;
7504 	struct bnxt_napi *bnapi = cpr->bnapi;
7505 	struct bnxt_ring_struct *ring;
7506 	u32 map_idx = bnapi->index;
7507 	int rc;
7508 
7509 	ring = &cpr->cp_ring_struct;
7510 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7511 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7512 	if (rc)
7513 		return rc;
7514 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7515 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7516 	return 0;
7517 }
7518 
7519 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7520 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7521 {
7522 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7523 	const u32 type = HWRM_RING_ALLOC_TX;
7524 	int rc;
7525 
7526 	rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, tx_idx);
7527 	if (rc)
7528 		return rc;
7529 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7530 	return 0;
7531 }
7532 
7533 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7534 {
7535 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7536 	int i, rc = 0;
7537 	u32 type;
7538 
7539 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7540 		type = HWRM_RING_ALLOC_NQ;
7541 	else
7542 		type = HWRM_RING_ALLOC_CMPL;
7543 	for (i = 0; i < bp->cp_nr_rings; i++) {
7544 		struct bnxt_napi *bnapi = bp->bnapi[i];
7545 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7546 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7547 		u32 map_idx = ring->map_idx;
7548 		unsigned int vector;
7549 
7550 		vector = bp->irq_tbl[map_idx].vector;
7551 		disable_irq_nosync(vector);
7552 		rc = hwrm_ring_alloc_send_msg(bp, NULL, ring, type, map_idx);
7553 		if (rc) {
7554 			enable_irq(vector);
7555 			goto err_out;
7556 		}
7557 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7558 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7559 		enable_irq(vector);
7560 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7561 
7562 		if (!i) {
7563 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7564 			if (rc)
7565 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7566 		}
7567 	}
7568 
7569 	for (i = 0; i < bp->tx_nr_rings; i++) {
7570 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7571 
7572 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7573 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7574 			if (rc)
7575 				goto err_out;
7576 		}
7577 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7578 		if (rc)
7579 			goto err_out;
7580 	}
7581 
7582 	for (i = 0; i < bp->rx_nr_rings; i++) {
7583 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7584 
7585 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7586 		if (rc)
7587 			goto err_out;
7588 		/* If we have agg rings, post agg buffers first. */
7589 		if (!agg_rings)
7590 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7591 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7592 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7593 			if (rc)
7594 				goto err_out;
7595 		}
7596 	}
7597 
7598 	if (agg_rings) {
7599 		for (i = 0; i < bp->rx_nr_rings; i++) {
7600 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7601 			if (rc)
7602 				goto err_out;
7603 		}
7604 	}
7605 err_out:
7606 	return rc;
7607 }
7608 
7609 static void bnxt_cancel_dim(struct bnxt *bp)
7610 {
7611 	int i;
7612 
7613 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7614 	 * if NAPI is enabled.
7615 	 */
7616 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7617 		return;
7618 
7619 	/* Make sure NAPI sees that the VNIC is disabled */
7620 	synchronize_net();
7621 	for (i = 0; i < bp->rx_nr_rings; i++) {
7622 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7623 		struct bnxt_napi *bnapi = rxr->bnapi;
7624 
7625 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7626 	}
7627 }
7628 
7629 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7630 				   struct bnxt_ring_struct *ring,
7631 				   u32 ring_type, int cmpl_ring_id)
7632 {
7633 	struct hwrm_ring_free_output *resp;
7634 	struct hwrm_ring_free_input *req;
7635 	u16 error_code = 0;
7636 	int rc;
7637 
7638 	if (BNXT_NO_FW_ACCESS(bp))
7639 		return 0;
7640 
7641 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7642 	if (rc)
7643 		goto exit;
7644 
7645 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7646 	req->ring_type = ring_type;
7647 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7648 
7649 	resp = hwrm_req_hold(bp, req);
7650 	rc = hwrm_req_send(bp, req);
7651 	error_code = le16_to_cpu(resp->error_code);
7652 	hwrm_req_drop(bp, req);
7653 exit:
7654 	if (rc || error_code) {
7655 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7656 			   ring_type, rc, error_code);
7657 		return -EIO;
7658 	}
7659 	return 0;
7660 }
7661 
7662 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7663 				   struct bnxt_tx_ring_info *txr,
7664 				   bool close_path)
7665 {
7666 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7667 	u32 cmpl_ring_id;
7668 
7669 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7670 		return;
7671 
7672 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7673 		       INVALID_HW_RING_ID;
7674 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7675 				cmpl_ring_id);
7676 	ring->fw_ring_id = INVALID_HW_RING_ID;
7677 }
7678 
7679 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7680 				   struct bnxt_rx_ring_info *rxr,
7681 				   bool close_path)
7682 {
7683 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7684 	u32 grp_idx = rxr->bnapi->index;
7685 	u32 cmpl_ring_id;
7686 
7687 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7688 		return;
7689 
7690 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7691 	hwrm_ring_free_send_msg(bp, ring,
7692 				RING_FREE_REQ_RING_TYPE_RX,
7693 				close_path ? cmpl_ring_id :
7694 				INVALID_HW_RING_ID);
7695 	ring->fw_ring_id = INVALID_HW_RING_ID;
7696 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7697 }
7698 
7699 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7700 				       struct bnxt_rx_ring_info *rxr,
7701 				       bool close_path)
7702 {
7703 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7704 	u32 grp_idx = rxr->bnapi->index;
7705 	u32 type, cmpl_ring_id;
7706 
7707 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7708 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7709 	else
7710 		type = RING_FREE_REQ_RING_TYPE_RX;
7711 
7712 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7713 		return;
7714 
7715 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7716 	hwrm_ring_free_send_msg(bp, ring, type,
7717 				close_path ? cmpl_ring_id :
7718 				INVALID_HW_RING_ID);
7719 	ring->fw_ring_id = INVALID_HW_RING_ID;
7720 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7721 }
7722 
7723 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7724 				   struct bnxt_cp_ring_info *cpr)
7725 {
7726 	struct bnxt_ring_struct *ring;
7727 
7728 	ring = &cpr->cp_ring_struct;
7729 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7730 		return;
7731 
7732 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7733 				INVALID_HW_RING_ID);
7734 	ring->fw_ring_id = INVALID_HW_RING_ID;
7735 }
7736 
7737 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7738 {
7739 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7740 	int i, size = ring->ring_mem.page_size;
7741 
7742 	cpr->cp_raw_cons = 0;
7743 	cpr->toggle = 0;
7744 
7745 	for (i = 0; i < bp->cp_nr_pages; i++)
7746 		if (cpr->cp_desc_ring[i])
7747 			memset(cpr->cp_desc_ring[i], 0, size);
7748 }
7749 
7750 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7751 {
7752 	u32 type;
7753 	int i;
7754 
7755 	if (!bp->bnapi)
7756 		return;
7757 
7758 	for (i = 0; i < bp->tx_nr_rings; i++)
7759 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7760 
7761 	bnxt_cancel_dim(bp);
7762 	for (i = 0; i < bp->rx_nr_rings; i++) {
7763 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7764 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7765 	}
7766 
7767 	/* The completion rings are about to be freed.  After that the
7768 	 * IRQ doorbell will not work anymore.  So we need to disable
7769 	 * IRQ here.
7770 	 */
7771 	bnxt_disable_int_sync(bp);
7772 
7773 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7774 		type = RING_FREE_REQ_RING_TYPE_NQ;
7775 	else
7776 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7777 	for (i = 0; i < bp->cp_nr_rings; i++) {
7778 		struct bnxt_napi *bnapi = bp->bnapi[i];
7779 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7780 		struct bnxt_ring_struct *ring;
7781 		int j;
7782 
7783 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7784 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7785 
7786 		ring = &cpr->cp_ring_struct;
7787 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7788 			hwrm_ring_free_send_msg(bp, ring, type,
7789 						INVALID_HW_RING_ID);
7790 			ring->fw_ring_id = INVALID_HW_RING_ID;
7791 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7792 		}
7793 	}
7794 }
7795 
7796 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7797 			     bool shared);
7798 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7799 			   bool shared);
7800 
7801 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7802 {
7803 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7804 	struct hwrm_func_qcfg_output *resp;
7805 	struct hwrm_func_qcfg_input *req;
7806 	int rc;
7807 
7808 	if (bp->hwrm_spec_code < 0x10601)
7809 		return 0;
7810 
7811 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7812 	if (rc)
7813 		return rc;
7814 
7815 	req->fid = cpu_to_le16(0xffff);
7816 	resp = hwrm_req_hold(bp, req);
7817 	rc = hwrm_req_send(bp, req);
7818 	if (rc) {
7819 		hwrm_req_drop(bp, req);
7820 		return rc;
7821 	}
7822 
7823 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7824 	if (BNXT_NEW_RM(bp)) {
7825 		u16 cp, stats;
7826 
7827 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7828 		hw_resc->resv_hw_ring_grps =
7829 			le32_to_cpu(resp->alloc_hw_ring_grps);
7830 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7831 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7832 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7833 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7834 		hw_resc->resv_irqs = cp;
7835 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7836 			int rx = hw_resc->resv_rx_rings;
7837 			int tx = hw_resc->resv_tx_rings;
7838 
7839 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7840 				rx >>= 1;
7841 			if (cp < (rx + tx)) {
7842 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7843 				if (rc)
7844 					goto get_rings_exit;
7845 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7846 					rx <<= 1;
7847 				hw_resc->resv_rx_rings = rx;
7848 				hw_resc->resv_tx_rings = tx;
7849 			}
7850 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7851 			hw_resc->resv_hw_ring_grps = rx;
7852 		}
7853 		hw_resc->resv_cp_rings = cp;
7854 		hw_resc->resv_stat_ctxs = stats;
7855 	}
7856 get_rings_exit:
7857 	hwrm_req_drop(bp, req);
7858 	return rc;
7859 }
7860 
7861 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7862 {
7863 	struct hwrm_func_qcfg_output *resp;
7864 	struct hwrm_func_qcfg_input *req;
7865 	int rc;
7866 
7867 	if (bp->hwrm_spec_code < 0x10601)
7868 		return 0;
7869 
7870 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7871 	if (rc)
7872 		return rc;
7873 
7874 	req->fid = cpu_to_le16(fid);
7875 	resp = hwrm_req_hold(bp, req);
7876 	rc = hwrm_req_send(bp, req);
7877 	if (!rc)
7878 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7879 
7880 	hwrm_req_drop(bp, req);
7881 	return rc;
7882 }
7883 
7884 static bool bnxt_rfs_supported(struct bnxt *bp);
7885 
7886 static struct hwrm_func_cfg_input *
7887 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7888 {
7889 	struct hwrm_func_cfg_input *req;
7890 	u32 enables = 0;
7891 
7892 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7893 		return NULL;
7894 
7895 	req->fid = cpu_to_le16(0xffff);
7896 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7897 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7898 	if (BNXT_NEW_RM(bp)) {
7899 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7900 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7901 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7902 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7903 			enables |= hwr->cp_p5 ?
7904 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7905 		} else {
7906 			enables |= hwr->cp ?
7907 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7908 			enables |= hwr->grp ?
7909 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7910 		}
7911 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7912 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7913 					  0;
7914 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7915 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7916 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7917 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7918 			req->num_msix = cpu_to_le16(hwr->cp);
7919 		} else {
7920 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7921 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7922 		}
7923 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7924 		req->num_vnics = cpu_to_le16(hwr->vnic);
7925 	}
7926 	req->enables = cpu_to_le32(enables);
7927 	return req;
7928 }
7929 
7930 static struct hwrm_func_vf_cfg_input *
7931 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7932 {
7933 	struct hwrm_func_vf_cfg_input *req;
7934 	u32 enables = 0;
7935 
7936 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7937 		return NULL;
7938 
7939 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7940 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7941 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7942 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7943 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7944 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7945 		enables |= hwr->cp_p5 ?
7946 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7947 	} else {
7948 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7949 		enables |= hwr->grp ?
7950 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7951 	}
7952 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7953 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7954 
7955 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7956 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7957 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7958 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7959 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7960 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7961 	} else {
7962 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7963 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7964 	}
7965 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7966 	req->num_vnics = cpu_to_le16(hwr->vnic);
7967 
7968 	req->enables = cpu_to_le32(enables);
7969 	return req;
7970 }
7971 
7972 static int
7973 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7974 {
7975 	struct hwrm_func_cfg_input *req;
7976 	int rc;
7977 
7978 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7979 	if (!req)
7980 		return -ENOMEM;
7981 
7982 	if (!req->enables) {
7983 		hwrm_req_drop(bp, req);
7984 		return 0;
7985 	}
7986 
7987 	rc = hwrm_req_send(bp, req);
7988 	if (rc)
7989 		return rc;
7990 
7991 	if (bp->hwrm_spec_code < 0x10601)
7992 		bp->hw_resc.resv_tx_rings = hwr->tx;
7993 
7994 	return bnxt_hwrm_get_rings(bp);
7995 }
7996 
7997 static int
7998 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7999 {
8000 	struct hwrm_func_vf_cfg_input *req;
8001 	int rc;
8002 
8003 	if (!BNXT_NEW_RM(bp)) {
8004 		bp->hw_resc.resv_tx_rings = hwr->tx;
8005 		return 0;
8006 	}
8007 
8008 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8009 	if (!req)
8010 		return -ENOMEM;
8011 
8012 	rc = hwrm_req_send(bp, req);
8013 	if (rc)
8014 		return rc;
8015 
8016 	return bnxt_hwrm_get_rings(bp);
8017 }
8018 
8019 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8020 {
8021 	if (BNXT_PF(bp))
8022 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
8023 	else
8024 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
8025 }
8026 
8027 int bnxt_nq_rings_in_use(struct bnxt *bp)
8028 {
8029 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
8030 }
8031 
8032 static int bnxt_cp_rings_in_use(struct bnxt *bp)
8033 {
8034 	int cp;
8035 
8036 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8037 		return bnxt_nq_rings_in_use(bp);
8038 
8039 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
8040 	return cp;
8041 }
8042 
8043 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
8044 {
8045 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
8046 }
8047 
8048 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8049 {
8050 	if (!hwr->grp)
8051 		return 0;
8052 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8053 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
8054 
8055 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8056 			rss_ctx *= hwr->vnic;
8057 		return rss_ctx;
8058 	}
8059 	if (BNXT_VF(bp))
8060 		return BNXT_VF_MAX_RSS_CTX;
8061 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
8062 		return hwr->grp + 1;
8063 	return 1;
8064 }
8065 
8066 /* Check if a default RSS map needs to be setup.  This function is only
8067  * used on older firmware that does not require reserving RX rings.
8068  */
8069 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
8070 {
8071 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8072 
8073 	/* The RSS map is valid for RX rings set to resv_rx_rings */
8074 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
8075 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
8076 		if (!netif_is_rxfh_configured(bp->dev))
8077 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8078 	}
8079 }
8080 
8081 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
8082 {
8083 	if (bp->flags & BNXT_FLAG_RFS) {
8084 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
8085 			return 2 + bp->num_rss_ctx;
8086 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8087 			return rx_rings + 1;
8088 	}
8089 	return 1;
8090 }
8091 
8092 static void bnxt_get_total_resources(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8093 {
8094 	hwr->cp = bnxt_nq_rings_in_use(bp);
8095 	hwr->cp_p5 = 0;
8096 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8097 		hwr->cp_p5 = bnxt_cp_rings_in_use(bp);
8098 	hwr->tx = bp->tx_nr_rings;
8099 	hwr->rx = bp->rx_nr_rings;
8100 	hwr->grp = hwr->rx;
8101 	hwr->vnic = bnxt_get_total_vnics(bp, hwr->rx);
8102 	hwr->rss_ctx = bnxt_get_total_rss_ctxs(bp, hwr);
8103 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8104 		hwr->rx <<= 1;
8105 	hwr->stat = bnxt_get_func_stat_ctxs(bp);
8106 }
8107 
8108 static bool bnxt_need_reserve_rings(struct bnxt *bp)
8109 {
8110 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8111 	struct bnxt_hw_rings hwr;
8112 
8113 	bnxt_get_total_resources(bp, &hwr);
8114 
8115 	/* Old firmware does not need RX ring reservations but we still
8116 	 * need to setup a default RSS map when needed.  With new firmware
8117 	 * we go through RX ring reservations first and then set up the
8118 	 * RSS map for the successfully reserved RX rings when needed.
8119 	 */
8120 	if (!BNXT_NEW_RM(bp))
8121 		bnxt_check_rss_tbl_no_rmgr(bp);
8122 
8123 	if (hw_resc->resv_tx_rings != hwr.tx && bp->hwrm_spec_code >= 0x10601)
8124 		return true;
8125 
8126 	if (!BNXT_NEW_RM(bp))
8127 		return false;
8128 
8129 	if (hw_resc->resv_rx_rings != hwr.rx ||
8130 	    hw_resc->resv_vnics != hwr.vnic ||
8131 	    hw_resc->resv_stat_ctxs != hwr.stat ||
8132 	    hw_resc->resv_rsscos_ctxs != hwr.rss_ctx ||
8133 	    (hw_resc->resv_hw_ring_grps != hwr.grp &&
8134 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
8135 		return true;
8136 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
8137 		if (hw_resc->resv_cp_rings != hwr.cp_p5)
8138 			return true;
8139 	} else if (hw_resc->resv_cp_rings != hwr.cp) {
8140 		return true;
8141 	}
8142 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
8143 	    hw_resc->resv_irqs != hwr.cp)
8144 		return true;
8145 	return false;
8146 }
8147 
8148 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8149 {
8150 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
8151 
8152 	hwr->tx = hw_resc->resv_tx_rings;
8153 	if (BNXT_NEW_RM(bp)) {
8154 		hwr->rx = hw_resc->resv_rx_rings;
8155 		hwr->cp = hw_resc->resv_irqs;
8156 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8157 			hwr->cp_p5 = hw_resc->resv_cp_rings;
8158 		hwr->grp = hw_resc->resv_hw_ring_grps;
8159 		hwr->vnic = hw_resc->resv_vnics;
8160 		hwr->stat = hw_resc->resv_stat_ctxs;
8161 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
8162 	}
8163 }
8164 
8165 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8166 {
8167 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
8168 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
8169 }
8170 
8171 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
8172 
8173 static int __bnxt_reserve_rings(struct bnxt *bp)
8174 {
8175 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
8176 	struct bnxt_hw_rings hwr = {0};
8177 	int rx_rings, old_rx_rings, rc;
8178 	int cp = bp->cp_nr_rings;
8179 	int ulp_msix = 0;
8180 	bool sh = false;
8181 	int tx_cp;
8182 
8183 	if (!bnxt_need_reserve_rings(bp))
8184 		return 0;
8185 
8186 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
8187 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
8188 		if (!ulp_msix)
8189 			bnxt_set_ulp_stat_ctxs(bp, 0);
8190 		else
8191 			bnxt_set_dflt_ulp_stat_ctxs(bp);
8192 
8193 		if (ulp_msix > bp->ulp_num_msix_want)
8194 			ulp_msix = bp->ulp_num_msix_want;
8195 		hwr.cp = cp + ulp_msix;
8196 	} else {
8197 		hwr.cp = bnxt_nq_rings_in_use(bp);
8198 	}
8199 
8200 	hwr.tx = bp->tx_nr_rings;
8201 	hwr.rx = bp->rx_nr_rings;
8202 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
8203 		sh = true;
8204 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8205 		hwr.cp_p5 = hwr.rx + hwr.tx;
8206 
8207 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
8208 
8209 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8210 		hwr.rx <<= 1;
8211 	hwr.grp = bp->rx_nr_rings;
8212 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
8213 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8214 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8215 
8216 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8217 	if (rc)
8218 		return rc;
8219 
8220 	bnxt_copy_reserved_rings(bp, &hwr);
8221 
8222 	rx_rings = hwr.rx;
8223 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8224 		if (hwr.rx >= 2) {
8225 			rx_rings = hwr.rx >> 1;
8226 		} else {
8227 			if (netif_running(bp->dev))
8228 				return -ENOMEM;
8229 
8230 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8231 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8232 			bp->dev->hw_features &= ~NETIF_F_LRO;
8233 			bp->dev->features &= ~NETIF_F_LRO;
8234 			bnxt_set_ring_params(bp);
8235 		}
8236 	}
8237 	rx_rings = min_t(int, rx_rings, hwr.grp);
8238 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8239 	if (bnxt_ulp_registered(edev) && hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8240 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8241 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8242 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8243 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8244 		hwr.rx = rx_rings << 1;
8245 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8246 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8247 	if (hwr.tx != bp->tx_nr_rings) {
8248 		netdev_warn(bp->dev,
8249 			    "Able to reserve only %d out of %d requested TX rings\n",
8250 			    hwr.tx, bp->tx_nr_rings);
8251 	}
8252 	bp->tx_nr_rings = hwr.tx;
8253 
8254 	/* If we cannot reserve all the RX rings, reset the RSS map only
8255 	 * if absolutely necessary
8256 	 */
8257 	if (rx_rings != bp->rx_nr_rings) {
8258 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8259 			    rx_rings, bp->rx_nr_rings);
8260 		if (netif_is_rxfh_configured(bp->dev) &&
8261 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8262 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8263 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8264 			ethtool_rxfh_indir_lost(bp->dev);
8265 		}
8266 	}
8267 	bp->rx_nr_rings = rx_rings;
8268 	bp->cp_nr_rings = hwr.cp;
8269 
8270 	/* Fall back if we cannot reserve enough HW RSS contexts */
8271 	if ((bp->rss_cap & BNXT_RSS_CAP_LARGE_RSS_CTX) &&
8272 	    hwr.rss_ctx < bnxt_get_total_rss_ctxs(bp, &hwr))
8273 		bp->rss_cap &= ~BNXT_RSS_CAP_LARGE_RSS_CTX;
8274 
8275 	if (!bnxt_rings_ok(bp, &hwr))
8276 		return -ENOMEM;
8277 
8278 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8279 	    !netif_is_rxfh_configured(bp->dev))
8280 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8281 
8282 	if (!bnxt_ulp_registered(edev) && BNXT_NEW_RM(bp)) {
8283 		int resv_msix, resv_ctx, ulp_ctxs;
8284 		struct bnxt_hw_resc *hw_resc;
8285 
8286 		hw_resc = &bp->hw_resc;
8287 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8288 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8289 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8290 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8291 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8292 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8293 	}
8294 
8295 	return rc;
8296 }
8297 
8298 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8299 {
8300 	struct hwrm_func_vf_cfg_input *req;
8301 	u32 flags;
8302 
8303 	if (!BNXT_NEW_RM(bp))
8304 		return 0;
8305 
8306 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8307 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8308 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8309 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8310 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8311 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8312 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8313 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8314 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8315 
8316 	req->flags = cpu_to_le32(flags);
8317 	return hwrm_req_send_silent(bp, req);
8318 }
8319 
8320 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8321 {
8322 	struct hwrm_func_cfg_input *req;
8323 	u32 flags;
8324 
8325 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8326 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8327 	if (BNXT_NEW_RM(bp)) {
8328 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8329 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8330 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8331 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8332 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8333 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8334 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8335 		else
8336 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8337 	}
8338 
8339 	req->flags = cpu_to_le32(flags);
8340 	return hwrm_req_send_silent(bp, req);
8341 }
8342 
8343 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8344 {
8345 	if (bp->hwrm_spec_code < 0x10801)
8346 		return 0;
8347 
8348 	if (BNXT_PF(bp))
8349 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8350 
8351 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8352 }
8353 
8354 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8355 {
8356 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8357 	struct hwrm_ring_aggint_qcaps_output *resp;
8358 	struct hwrm_ring_aggint_qcaps_input *req;
8359 	int rc;
8360 
8361 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8362 	coal_cap->num_cmpl_dma_aggr_max = 63;
8363 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8364 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8365 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8366 	coal_cap->int_lat_tmr_min_max = 65535;
8367 	coal_cap->int_lat_tmr_max_max = 65535;
8368 	coal_cap->num_cmpl_aggr_int_max = 65535;
8369 	coal_cap->timer_units = 80;
8370 
8371 	if (bp->hwrm_spec_code < 0x10902)
8372 		return;
8373 
8374 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8375 		return;
8376 
8377 	resp = hwrm_req_hold(bp, req);
8378 	rc = hwrm_req_send_silent(bp, req);
8379 	if (!rc) {
8380 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8381 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8382 		coal_cap->num_cmpl_dma_aggr_max =
8383 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8384 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8385 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8386 		coal_cap->cmpl_aggr_dma_tmr_max =
8387 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8388 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8389 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8390 		coal_cap->int_lat_tmr_min_max =
8391 			le16_to_cpu(resp->int_lat_tmr_min_max);
8392 		coal_cap->int_lat_tmr_max_max =
8393 			le16_to_cpu(resp->int_lat_tmr_max_max);
8394 		coal_cap->num_cmpl_aggr_int_max =
8395 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8396 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8397 	}
8398 	hwrm_req_drop(bp, req);
8399 }
8400 
8401 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8402 {
8403 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8404 
8405 	return usec * 1000 / coal_cap->timer_units;
8406 }
8407 
8408 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8409 	struct bnxt_coal *hw_coal,
8410 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8411 {
8412 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8413 	u16 val, tmr, max, flags = hw_coal->flags;
8414 	u32 cmpl_params = coal_cap->cmpl_params;
8415 
8416 	max = hw_coal->bufs_per_record * 128;
8417 	if (hw_coal->budget)
8418 		max = hw_coal->bufs_per_record * hw_coal->budget;
8419 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8420 
8421 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8422 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8423 
8424 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8425 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8426 
8427 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8428 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8429 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8430 
8431 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8432 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8433 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8434 
8435 	/* min timer set to 1/2 of interrupt timer */
8436 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8437 		val = tmr / 2;
8438 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8439 		req->int_lat_tmr_min = cpu_to_le16(val);
8440 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8441 	}
8442 
8443 	/* buf timer set to 1/4 of interrupt timer */
8444 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8445 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8446 
8447 	if (cmpl_params &
8448 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8449 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8450 		val = clamp_t(u16, tmr, 1,
8451 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8452 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8453 		req->enables |=
8454 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8455 	}
8456 
8457 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8458 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8459 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8460 	req->flags = cpu_to_le16(flags);
8461 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8462 }
8463 
8464 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8465 				   struct bnxt_coal *hw_coal)
8466 {
8467 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8468 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8469 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8470 	u32 nq_params = coal_cap->nq_params;
8471 	u16 tmr;
8472 	int rc;
8473 
8474 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8475 		return 0;
8476 
8477 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8478 	if (rc)
8479 		return rc;
8480 
8481 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8482 	req->flags =
8483 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8484 
8485 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8486 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8487 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8488 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8489 	return hwrm_req_send(bp, req);
8490 }
8491 
8492 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8493 {
8494 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8495 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8496 	struct bnxt_coal coal;
8497 	int rc;
8498 
8499 	/* Tick values in micro seconds.
8500 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8501 	 */
8502 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8503 
8504 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8505 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8506 
8507 	if (!bnapi->rx_ring)
8508 		return -ENODEV;
8509 
8510 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8511 	if (rc)
8512 		return rc;
8513 
8514 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8515 
8516 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8517 
8518 	return hwrm_req_send(bp, req_rx);
8519 }
8520 
8521 static int
8522 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8523 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8524 {
8525 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8526 
8527 	req->ring_id = cpu_to_le16(ring_id);
8528 	return hwrm_req_send(bp, req);
8529 }
8530 
8531 static int
8532 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8533 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8534 {
8535 	struct bnxt_tx_ring_info *txr;
8536 	int i, rc;
8537 
8538 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8539 		u16 ring_id;
8540 
8541 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8542 		req->ring_id = cpu_to_le16(ring_id);
8543 		rc = hwrm_req_send(bp, req);
8544 		if (rc)
8545 			return rc;
8546 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8547 			return 0;
8548 	}
8549 	return 0;
8550 }
8551 
8552 int bnxt_hwrm_set_coal(struct bnxt *bp)
8553 {
8554 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8555 	int i, rc;
8556 
8557 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8558 	if (rc)
8559 		return rc;
8560 
8561 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8562 	if (rc) {
8563 		hwrm_req_drop(bp, req_rx);
8564 		return rc;
8565 	}
8566 
8567 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8568 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8569 
8570 	hwrm_req_hold(bp, req_rx);
8571 	hwrm_req_hold(bp, req_tx);
8572 	for (i = 0; i < bp->cp_nr_rings; i++) {
8573 		struct bnxt_napi *bnapi = bp->bnapi[i];
8574 		struct bnxt_coal *hw_coal;
8575 
8576 		if (!bnapi->rx_ring)
8577 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8578 		else
8579 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8580 		if (rc)
8581 			break;
8582 
8583 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8584 			continue;
8585 
8586 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8587 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8588 			if (rc)
8589 				break;
8590 		}
8591 		if (bnapi->rx_ring)
8592 			hw_coal = &bp->rx_coal;
8593 		else
8594 			hw_coal = &bp->tx_coal;
8595 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8596 	}
8597 	hwrm_req_drop(bp, req_rx);
8598 	hwrm_req_drop(bp, req_tx);
8599 	return rc;
8600 }
8601 
8602 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8603 {
8604 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8605 	struct hwrm_stat_ctx_free_input *req;
8606 	int i;
8607 
8608 	if (!bp->bnapi)
8609 		return;
8610 
8611 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8612 		return;
8613 
8614 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8615 		return;
8616 	if (BNXT_FW_MAJ(bp) <= 20) {
8617 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8618 			hwrm_req_drop(bp, req);
8619 			return;
8620 		}
8621 		hwrm_req_hold(bp, req0);
8622 	}
8623 	hwrm_req_hold(bp, req);
8624 	for (i = 0; i < bp->cp_nr_rings; i++) {
8625 		struct bnxt_napi *bnapi = bp->bnapi[i];
8626 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8627 
8628 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8629 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8630 			if (req0) {
8631 				req0->stat_ctx_id = req->stat_ctx_id;
8632 				hwrm_req_send(bp, req0);
8633 			}
8634 			hwrm_req_send(bp, req);
8635 
8636 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8637 		}
8638 	}
8639 	hwrm_req_drop(bp, req);
8640 	if (req0)
8641 		hwrm_req_drop(bp, req0);
8642 }
8643 
8644 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8645 {
8646 	struct hwrm_stat_ctx_alloc_output *resp;
8647 	struct hwrm_stat_ctx_alloc_input *req;
8648 	int rc, i;
8649 
8650 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8651 		return 0;
8652 
8653 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8654 	if (rc)
8655 		return rc;
8656 
8657 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8658 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8659 
8660 	resp = hwrm_req_hold(bp, req);
8661 	for (i = 0; i < bp->cp_nr_rings; i++) {
8662 		struct bnxt_napi *bnapi = bp->bnapi[i];
8663 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8664 
8665 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8666 
8667 		rc = hwrm_req_send(bp, req);
8668 		if (rc)
8669 			break;
8670 
8671 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8672 
8673 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8674 	}
8675 	hwrm_req_drop(bp, req);
8676 	return rc;
8677 }
8678 
8679 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8680 {
8681 	struct hwrm_func_qcfg_output *resp;
8682 	struct hwrm_func_qcfg_input *req;
8683 	u16 flags;
8684 	int rc;
8685 
8686 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8687 	if (rc)
8688 		return rc;
8689 
8690 	req->fid = cpu_to_le16(0xffff);
8691 	resp = hwrm_req_hold(bp, req);
8692 	rc = hwrm_req_send(bp, req);
8693 	if (rc)
8694 		goto func_qcfg_exit;
8695 
8696 	flags = le16_to_cpu(resp->flags);
8697 #ifdef CONFIG_BNXT_SRIOV
8698 	if (BNXT_VF(bp)) {
8699 		struct bnxt_vf_info *vf = &bp->vf;
8700 
8701 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8702 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8703 			vf->flags |= BNXT_VF_TRUST;
8704 		else
8705 			vf->flags &= ~BNXT_VF_TRUST;
8706 	} else {
8707 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8708 	}
8709 #endif
8710 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8711 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8712 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8713 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8714 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8715 	}
8716 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8717 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8718 
8719 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8720 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8721 
8722 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8723 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8724 	if (resp->roce_bidi_opt_mode &
8725 	    FUNC_QCFG_RESP_ROCE_BIDI_OPT_MODE_DEDICATED)
8726 		bp->cos0_cos1_shared = 1;
8727 	else
8728 		bp->cos0_cos1_shared = 0;
8729 
8730 	switch (resp->port_partition_type) {
8731 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8732 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8733 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8734 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8735 		bp->port_partition_type = resp->port_partition_type;
8736 		break;
8737 	}
8738 	if (bp->hwrm_spec_code < 0x10707 ||
8739 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8740 		bp->br_mode = BRIDGE_MODE_VEB;
8741 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8742 		bp->br_mode = BRIDGE_MODE_VEPA;
8743 	else
8744 		bp->br_mode = BRIDGE_MODE_UNDEF;
8745 
8746 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8747 	if (!bp->max_mtu)
8748 		bp->max_mtu = BNXT_MAX_MTU;
8749 
8750 	if (bp->db_size)
8751 		goto func_qcfg_exit;
8752 
8753 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8754 	if (BNXT_CHIP_P5(bp)) {
8755 		if (BNXT_PF(bp))
8756 			bp->db_offset = DB_PF_OFFSET_P5;
8757 		else
8758 			bp->db_offset = DB_VF_OFFSET_P5;
8759 	}
8760 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8761 				 1024);
8762 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8763 	    bp->db_size <= bp->db_offset)
8764 		bp->db_size = pci_resource_len(bp->pdev, 2);
8765 
8766 func_qcfg_exit:
8767 	hwrm_req_drop(bp, req);
8768 	return rc;
8769 }
8770 
8771 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8772 				      u8 init_val, u8 init_offset,
8773 				      bool init_mask_set)
8774 {
8775 	ctxm->init_value = init_val;
8776 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8777 	if (init_mask_set)
8778 		ctxm->init_offset = init_offset * 4;
8779 	else
8780 		ctxm->init_value = 0;
8781 }
8782 
8783 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8784 {
8785 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8786 	u16 type;
8787 
8788 	for (type = 0; type < ctx_max; type++) {
8789 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8790 		int n = 1;
8791 
8792 		if (!ctxm->max_entries || ctxm->pg_info)
8793 			continue;
8794 
8795 		if (ctxm->instance_bmap)
8796 			n = hweight32(ctxm->instance_bmap);
8797 		ctxm->pg_info = kzalloc_objs(*ctxm->pg_info, n);
8798 		if (!ctxm->pg_info)
8799 			return -ENOMEM;
8800 	}
8801 	return 0;
8802 }
8803 
8804 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8805 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8806 
8807 #define BNXT_CTX_INIT_VALID(flags)	\
8808 	(!!((flags) &			\
8809 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8810 
8811 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8812 {
8813 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8814 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8815 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8816 	u16 type, next_type = 0;
8817 	int rc;
8818 
8819 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8820 	if (rc)
8821 		return rc;
8822 
8823 	if (!ctx) {
8824 		ctx = kzalloc_obj(*ctx);
8825 		if (!ctx)
8826 			return -ENOMEM;
8827 		bp->ctx = ctx;
8828 	}
8829 
8830 	resp = hwrm_req_hold(bp, req);
8831 
8832 	for (type = 0; type < BNXT_CTX_V2_MAX; type = next_type) {
8833 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8834 		u8 init_val, init_off, i;
8835 		u32 max_entries;
8836 		u16 entry_size;
8837 		__le32 *p;
8838 		u32 flags;
8839 
8840 		req->type = cpu_to_le16(type);
8841 		rc = hwrm_req_send(bp, req);
8842 		if (rc)
8843 			goto ctx_done;
8844 		flags = le32_to_cpu(resp->flags);
8845 		next_type = le16_to_cpu(resp->next_valid_type);
8846 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8847 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8848 			continue;
8849 		}
8850 		entry_size = le16_to_cpu(resp->entry_size);
8851 		max_entries = le32_to_cpu(resp->max_num_entries);
8852 		if (ctxm->mem_valid) {
8853 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8854 			    ctxm->entry_size != entry_size ||
8855 			    ctxm->max_entries != max_entries)
8856 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8857 			else
8858 				continue;
8859 		}
8860 		ctxm->type = type;
8861 		ctxm->entry_size = entry_size;
8862 		ctxm->flags = flags;
8863 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8864 		ctxm->entry_multiple = resp->entry_multiple;
8865 		ctxm->max_entries = max_entries;
8866 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8867 		init_val = resp->ctx_init_value;
8868 		init_off = resp->ctx_init_offset;
8869 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8870 					  BNXT_CTX_INIT_VALID(flags));
8871 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8872 					      BNXT_MAX_SPLIT_ENTRY);
8873 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8874 		     i++, p++)
8875 			ctxm->split[i] = le32_to_cpu(*p);
8876 	}
8877 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8878 
8879 ctx_done:
8880 	hwrm_req_drop(bp, req);
8881 	return rc;
8882 }
8883 
8884 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8885 {
8886 	struct hwrm_func_backing_store_qcaps_output *resp;
8887 	struct hwrm_func_backing_store_qcaps_input *req;
8888 	int rc;
8889 
8890 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8891 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8892 		return 0;
8893 
8894 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8895 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8896 
8897 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8898 	if (rc)
8899 		return rc;
8900 
8901 	resp = hwrm_req_hold(bp, req);
8902 	rc = hwrm_req_send_silent(bp, req);
8903 	if (!rc) {
8904 		struct bnxt_ctx_mem_type *ctxm;
8905 		struct bnxt_ctx_mem_info *ctx;
8906 		u8 init_val, init_idx = 0;
8907 		u16 init_mask;
8908 
8909 		ctx = bp->ctx;
8910 		if (!ctx) {
8911 			ctx = kzalloc_obj(*ctx);
8912 			if (!ctx) {
8913 				rc = -ENOMEM;
8914 				goto ctx_err;
8915 			}
8916 			bp->ctx = ctx;
8917 		}
8918 		init_val = resp->ctx_kind_initializer;
8919 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8920 
8921 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8922 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8923 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8924 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8925 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8926 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8927 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8928 					  (init_mask & (1 << init_idx++)) != 0);
8929 
8930 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8931 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8932 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8933 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8934 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8935 					  (init_mask & (1 << init_idx++)) != 0);
8936 
8937 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8938 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8939 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8940 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8941 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8942 					  (init_mask & (1 << init_idx++)) != 0);
8943 
8944 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8945 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8946 		ctxm->max_entries = ctxm->vnic_entries +
8947 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8948 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8949 		bnxt_init_ctx_initializer(ctxm, init_val,
8950 					  resp->vnic_init_offset,
8951 					  (init_mask & (1 << init_idx++)) != 0);
8952 
8953 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8954 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8955 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8956 		bnxt_init_ctx_initializer(ctxm, init_val,
8957 					  resp->stat_init_offset,
8958 					  (init_mask & (1 << init_idx++)) != 0);
8959 
8960 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8961 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8962 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8963 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8964 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8965 		if (!ctxm->entry_multiple)
8966 			ctxm->entry_multiple = 1;
8967 
8968 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8969 
8970 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8971 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8972 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8973 		ctxm->mrav_num_entries_units =
8974 			le16_to_cpu(resp->mrav_num_entries_units);
8975 		bnxt_init_ctx_initializer(ctxm, init_val,
8976 					  resp->mrav_init_offset,
8977 					  (init_mask & (1 << init_idx++)) != 0);
8978 
8979 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8980 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8981 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8982 
8983 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8984 		if (!ctx->tqm_fp_rings_count)
8985 			ctx->tqm_fp_rings_count = bp->max_q;
8986 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8987 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8988 
8989 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8990 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8991 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8992 
8993 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8994 	} else {
8995 		rc = 0;
8996 	}
8997 ctx_err:
8998 	hwrm_req_drop(bp, req);
8999 	return rc;
9000 }
9001 
9002 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
9003 				  __le64 *pg_dir)
9004 {
9005 	if (!rmem->nr_pages)
9006 		return;
9007 
9008 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
9009 	if (rmem->depth >= 1) {
9010 		if (rmem->depth == 2)
9011 			*pg_attr |= 2;
9012 		else
9013 			*pg_attr |= 1;
9014 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
9015 	} else {
9016 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
9017 	}
9018 }
9019 
9020 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
9021 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
9022 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
9023 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
9024 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
9025 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
9026 
9027 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
9028 {
9029 	struct hwrm_func_backing_store_cfg_input *req;
9030 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9031 	struct bnxt_ctx_pg_info *ctx_pg;
9032 	struct bnxt_ctx_mem_type *ctxm;
9033 	void **__req = (void **)&req;
9034 	u32 req_len = sizeof(*req);
9035 	__le32 *num_entries;
9036 	__le64 *pg_dir;
9037 	u32 flags = 0;
9038 	u8 *pg_attr;
9039 	u32 ena;
9040 	int rc;
9041 	int i;
9042 
9043 	if (!ctx)
9044 		return 0;
9045 
9046 	if (req_len > bp->hwrm_max_ext_req_len)
9047 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
9048 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
9049 	if (rc)
9050 		return rc;
9051 
9052 	req->enables = cpu_to_le32(enables);
9053 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
9054 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9055 		ctx_pg = ctxm->pg_info;
9056 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
9057 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
9058 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
9059 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
9060 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9061 				      &req->qpc_pg_size_qpc_lvl,
9062 				      &req->qpc_page_dir);
9063 
9064 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
9065 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
9066 	}
9067 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
9068 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9069 		ctx_pg = ctxm->pg_info;
9070 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
9071 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
9072 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
9073 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9074 				      &req->srq_pg_size_srq_lvl,
9075 				      &req->srq_page_dir);
9076 	}
9077 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
9078 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9079 		ctx_pg = ctxm->pg_info;
9080 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
9081 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
9082 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
9083 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9084 				      &req->cq_pg_size_cq_lvl,
9085 				      &req->cq_page_dir);
9086 	}
9087 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
9088 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9089 		ctx_pg = ctxm->pg_info;
9090 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
9091 		req->vnic_num_ring_table_entries =
9092 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
9093 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
9094 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9095 				      &req->vnic_pg_size_vnic_lvl,
9096 				      &req->vnic_page_dir);
9097 	}
9098 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
9099 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9100 		ctx_pg = ctxm->pg_info;
9101 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
9102 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
9103 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9104 				      &req->stat_pg_size_stat_lvl,
9105 				      &req->stat_page_dir);
9106 	}
9107 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
9108 		u32 units;
9109 
9110 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9111 		ctx_pg = ctxm->pg_info;
9112 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
9113 		units = ctxm->mrav_num_entries_units;
9114 		if (units) {
9115 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
9116 			u32 entries;
9117 
9118 			num_mr = ctx_pg->entries - num_ah;
9119 			entries = ((num_mr / units) << 16) | (num_ah / units);
9120 			req->mrav_num_entries = cpu_to_le32(entries);
9121 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
9122 		}
9123 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
9124 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9125 				      &req->mrav_pg_size_mrav_lvl,
9126 				      &req->mrav_page_dir);
9127 	}
9128 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
9129 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9130 		ctx_pg = ctxm->pg_info;
9131 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
9132 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
9133 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9134 				      &req->tim_pg_size_tim_lvl,
9135 				      &req->tim_page_dir);
9136 	}
9137 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9138 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
9139 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
9140 	     pg_dir = &req->tqm_sp_page_dir,
9141 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
9142 	     ctx_pg = ctxm->pg_info;
9143 	     i < BNXT_MAX_TQM_RINGS;
9144 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
9145 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
9146 		if (!(enables & ena))
9147 			continue;
9148 
9149 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
9150 		*num_entries = cpu_to_le32(ctx_pg->entries);
9151 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
9152 	}
9153 	req->flags = cpu_to_le32(flags);
9154 	return hwrm_req_send(bp, req);
9155 }
9156 
9157 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
9158 				  struct bnxt_ctx_pg_info *ctx_pg)
9159 {
9160 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9161 
9162 	rmem->page_size = BNXT_PAGE_SIZE;
9163 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
9164 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
9165 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
9166 	if (rmem->depth >= 1)
9167 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
9168 	return bnxt_alloc_ring(bp, rmem);
9169 }
9170 
9171 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
9172 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
9173 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
9174 {
9175 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9176 	int rc;
9177 
9178 	if (!mem_size)
9179 		return -EINVAL;
9180 
9181 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9182 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
9183 		ctx_pg->nr_pages = 0;
9184 		return -EINVAL;
9185 	}
9186 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
9187 		int nr_tbls, i;
9188 
9189 		rmem->depth = 2;
9190 		ctx_pg->ctx_pg_tbl = kzalloc_objs(ctx_pg, MAX_CTX_PAGES);
9191 		if (!ctx_pg->ctx_pg_tbl)
9192 			return -ENOMEM;
9193 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
9194 		rmem->nr_pages = nr_tbls;
9195 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9196 		if (rc)
9197 			return rc;
9198 		for (i = 0; i < nr_tbls; i++) {
9199 			struct bnxt_ctx_pg_info *pg_tbl;
9200 
9201 			pg_tbl = kzalloc_obj(*pg_tbl);
9202 			if (!pg_tbl)
9203 				return -ENOMEM;
9204 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
9205 			rmem = &pg_tbl->ring_mem;
9206 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
9207 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
9208 			rmem->depth = 1;
9209 			rmem->nr_pages = MAX_CTX_PAGES;
9210 			rmem->ctx_mem = ctxm;
9211 			if (i == (nr_tbls - 1)) {
9212 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
9213 
9214 				if (rem)
9215 					rmem->nr_pages = rem;
9216 			}
9217 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
9218 			if (rc)
9219 				break;
9220 		}
9221 	} else {
9222 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9223 		if (rmem->nr_pages > 1 || depth)
9224 			rmem->depth = 1;
9225 		rmem->ctx_mem = ctxm;
9226 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9227 	}
9228 	return rc;
9229 }
9230 
9231 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9232 				    struct bnxt_ctx_pg_info *ctx_pg,
9233 				    void *buf, size_t offset, size_t head,
9234 				    size_t tail)
9235 {
9236 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9237 	size_t nr_pages = ctx_pg->nr_pages;
9238 	int page_size = rmem->page_size;
9239 	size_t len = 0, total_len = 0;
9240 	u16 depth = rmem->depth;
9241 
9242 	tail %= nr_pages * page_size;
9243 	do {
9244 		if (depth > 1) {
9245 			int i = head / (page_size * MAX_CTX_PAGES);
9246 			struct bnxt_ctx_pg_info *pg_tbl;
9247 
9248 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9249 			rmem = &pg_tbl->ring_mem;
9250 		}
9251 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9252 		head += len;
9253 		offset += len;
9254 		total_len += len;
9255 		if (head >= nr_pages * page_size)
9256 			head = 0;
9257 	} while (head != tail);
9258 	return total_len;
9259 }
9260 
9261 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9262 				  struct bnxt_ctx_pg_info *ctx_pg)
9263 {
9264 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9265 
9266 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9267 	    ctx_pg->ctx_pg_tbl) {
9268 		int i, nr_tbls = rmem->nr_pages;
9269 
9270 		for (i = 0; i < nr_tbls; i++) {
9271 			struct bnxt_ctx_pg_info *pg_tbl;
9272 			struct bnxt_ring_mem_info *rmem2;
9273 
9274 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9275 			if (!pg_tbl)
9276 				continue;
9277 			rmem2 = &pg_tbl->ring_mem;
9278 			bnxt_free_ring(bp, rmem2);
9279 			ctx_pg->ctx_pg_arr[i] = NULL;
9280 			kfree(pg_tbl);
9281 			ctx_pg->ctx_pg_tbl[i] = NULL;
9282 		}
9283 		kfree(ctx_pg->ctx_pg_tbl);
9284 		ctx_pg->ctx_pg_tbl = NULL;
9285 	}
9286 	bnxt_free_ring(bp, rmem);
9287 	ctx_pg->nr_pages = 0;
9288 }
9289 
9290 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9291 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9292 				   u8 pg_lvl)
9293 {
9294 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9295 	int i, rc = 0, n = 1;
9296 	u32 mem_size;
9297 
9298 	if (!ctxm->entry_size || !ctx_pg)
9299 		return -EINVAL;
9300 	if (ctxm->instance_bmap)
9301 		n = hweight32(ctxm->instance_bmap);
9302 	if (ctxm->entry_multiple)
9303 		entries = roundup(entries, ctxm->entry_multiple);
9304 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9305 	mem_size = entries * ctxm->entry_size;
9306 	for (i = 0; i < n && !rc; i++) {
9307 		ctx_pg[i].entries = entries;
9308 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9309 					    ctxm->init_value ? ctxm : NULL);
9310 	}
9311 	if (!rc)
9312 		ctxm->mem_valid = 1;
9313 	return rc;
9314 }
9315 
9316 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9317 					       struct bnxt_ctx_mem_type *ctxm,
9318 					       bool last)
9319 {
9320 	struct hwrm_func_backing_store_cfg_v2_input *req;
9321 	u32 instance_bmap = ctxm->instance_bmap;
9322 	int i, j, rc = 0, n = 1;
9323 	__le32 *p;
9324 
9325 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9326 		return 0;
9327 
9328 	if (instance_bmap)
9329 		n = hweight32(ctxm->instance_bmap);
9330 	else
9331 		instance_bmap = 1;
9332 
9333 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9334 	if (rc)
9335 		return rc;
9336 	hwrm_req_hold(bp, req);
9337 	req->type = cpu_to_le16(ctxm->type);
9338 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9339 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9340 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9341 		struct bnxt_bs_trace_info *bs_trace;
9342 		u32 enables;
9343 
9344 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9345 		req->enables = cpu_to_le32(enables);
9346 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9347 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9348 	}
9349 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9350 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9351 		p[i] = cpu_to_le32(ctxm->split[i]);
9352 	for (i = 0, j = 0; j < n && !rc; i++) {
9353 		struct bnxt_ctx_pg_info *ctx_pg;
9354 
9355 		if (!(instance_bmap & (1 << i)))
9356 			continue;
9357 		req->instance = cpu_to_le16(i);
9358 		ctx_pg = &ctxm->pg_info[j++];
9359 		if (!ctx_pg->entries)
9360 			continue;
9361 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9362 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9363 				      &req->page_size_pbl_level,
9364 				      &req->page_dir);
9365 		if (last && j == n)
9366 			req->flags =
9367 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9368 		rc = hwrm_req_send(bp, req);
9369 	}
9370 	hwrm_req_drop(bp, req);
9371 	return rc;
9372 }
9373 
9374 static int bnxt_backing_store_cfg_v2(struct bnxt *bp)
9375 {
9376 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9377 	struct bnxt_ctx_mem_type *ctxm;
9378 	u16 last_type = BNXT_CTX_INV;
9379 	int rc = 0;
9380 	u16 type;
9381 
9382 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_QPC; type++) {
9383 		ctxm = &ctx->ctx_arr[type];
9384 		if (!bnxt_bs_trace_avail(bp, type))
9385 			continue;
9386 		if (!ctxm->mem_valid) {
9387 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9388 						     ctxm->max_entries, 1);
9389 			if (rc) {
9390 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9391 					    type);
9392 				continue;
9393 			}
9394 			bnxt_bs_trace_init(bp, ctxm);
9395 		}
9396 		last_type = type;
9397 	}
9398 
9399 	if (last_type == BNXT_CTX_INV) {
9400 		for (type = 0; type < BNXT_CTX_MAX; type++) {
9401 			ctxm = &ctx->ctx_arr[type];
9402 			if (ctxm->mem_valid)
9403 				last_type = type;
9404 		}
9405 		if (last_type == BNXT_CTX_INV)
9406 			return 0;
9407 	}
9408 	ctx->ctx_arr[last_type].last = 1;
9409 
9410 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9411 		ctxm = &ctx->ctx_arr[type];
9412 
9413 		if (!ctxm->mem_valid)
9414 			continue;
9415 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9416 		if (rc)
9417 			return rc;
9418 	}
9419 	return 0;
9420 }
9421 
9422 /**
9423  * __bnxt_copy_ctx_mem - copy host context memory
9424  * @bp: The driver context
9425  * @ctxm: The pointer to the context memory type
9426  * @buf: The destination buffer or NULL to just obtain the length
9427  * @offset: The buffer offset to copy the data to
9428  * @head: The head offset of context memory to copy from
9429  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9430  *
9431  * This function is called for debugging purposes to dump the host context
9432  * used by the chip.
9433  *
9434  * Return: Length of memory copied
9435  */
9436 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9437 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9438 				  size_t offset, size_t head, size_t tail)
9439 {
9440 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9441 	size_t len = 0, total_len = 0;
9442 	int i, n = 1;
9443 
9444 	if (!ctx_pg)
9445 		return 0;
9446 
9447 	if (ctxm->instance_bmap)
9448 		n = hweight32(ctxm->instance_bmap);
9449 	for (i = 0; i < n; i++) {
9450 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9451 					    tail);
9452 		offset += len;
9453 		total_len += len;
9454 	}
9455 	return total_len;
9456 }
9457 
9458 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9459 			 void *buf, size_t offset)
9460 {
9461 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9462 
9463 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9464 }
9465 
9466 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9467 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9468 {
9469 	struct bnxt_ctx_pg_info *ctx_pg;
9470 	int i, n = 1;
9471 
9472 	ctxm->last = 0;
9473 
9474 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9475 		return;
9476 
9477 	ctx_pg = ctxm->pg_info;
9478 	if (ctx_pg) {
9479 		if (ctxm->instance_bmap)
9480 			n = hweight32(ctxm->instance_bmap);
9481 		for (i = 0; i < n; i++)
9482 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9483 
9484 		kfree(ctx_pg);
9485 		ctxm->pg_info = NULL;
9486 		ctxm->mem_valid = 0;
9487 	}
9488 	memset(ctxm, 0, sizeof(*ctxm));
9489 }
9490 
9491 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9492 {
9493 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9494 	u16 type;
9495 
9496 	if (!ctx)
9497 		return;
9498 
9499 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9500 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9501 
9502 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9503 	if (force) {
9504 		kfree(ctx);
9505 		bp->ctx = NULL;
9506 	}
9507 }
9508 
9509 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9510 {
9511 	struct bnxt_ctx_mem_type *ctxm;
9512 	struct bnxt_ctx_mem_info *ctx;
9513 	u32 l2_qps, qp1_qps, max_qps;
9514 	u32 ena, entries_sp, entries;
9515 	u32 srqs, max_srqs, min;
9516 	u32 num_mr, num_ah;
9517 	u32 extra_srqs = 0;
9518 	u32 extra_qps = 0;
9519 	u32 fast_qpmd_qps;
9520 	u8 pg_lvl = 1;
9521 	int i, rc;
9522 
9523 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9524 	if (rc) {
9525 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9526 			   rc);
9527 		return rc;
9528 	}
9529 	ctx = bp->ctx;
9530 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9531 		return 0;
9532 
9533 	ena = 0;
9534 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
9535 		goto skip_legacy;
9536 
9537 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9538 	l2_qps = ctxm->qp_l2_entries;
9539 	qp1_qps = ctxm->qp_qp1_entries;
9540 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9541 	max_qps = ctxm->max_entries;
9542 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9543 	srqs = ctxm->srq_l2_entries;
9544 	max_srqs = ctxm->max_entries;
9545 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9546 		pg_lvl = 2;
9547 		if (BNXT_SW_RES_LMT(bp)) {
9548 			extra_qps = max_qps - l2_qps - qp1_qps;
9549 			extra_srqs = max_srqs - srqs;
9550 		} else {
9551 			extra_qps = min_t(u32, 65536,
9552 					  max_qps - l2_qps - qp1_qps);
9553 			/* allocate extra qps if fw supports RoCE fast qp
9554 			 * destroy feature
9555 			 */
9556 			extra_qps += fast_qpmd_qps;
9557 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9558 		}
9559 		if (fast_qpmd_qps)
9560 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9561 	}
9562 
9563 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9564 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9565 				     pg_lvl);
9566 	if (rc)
9567 		return rc;
9568 
9569 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9570 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9571 	if (rc)
9572 		return rc;
9573 
9574 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9575 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9576 				     extra_qps * 2, pg_lvl);
9577 	if (rc)
9578 		return rc;
9579 
9580 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9581 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9582 	if (rc)
9583 		return rc;
9584 
9585 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9586 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9587 	if (rc)
9588 		return rc;
9589 
9590 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9591 		goto skip_rdma;
9592 
9593 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9594 	if (BNXT_SW_RES_LMT(bp) &&
9595 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9596 		num_ah = ctxm->mrav_av_entries;
9597 		num_mr = ctxm->max_entries - num_ah;
9598 	} else {
9599 		/* 128K extra is needed to accommodate static AH context
9600 		 * allocation by f/w.
9601 		 */
9602 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9603 		num_ah = min_t(u32, num_mr, 1024 * 128);
9604 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9605 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9606 			ctxm->mrav_av_entries = num_ah;
9607 	}
9608 
9609 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9610 	if (rc)
9611 		return rc;
9612 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9613 
9614 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9615 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9616 	if (rc)
9617 		return rc;
9618 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9619 
9620 skip_rdma:
9621 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9622 	min = ctxm->min_entries;
9623 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9624 		     2 * (extra_qps + qp1_qps) + min;
9625 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9626 	if (rc)
9627 		return rc;
9628 
9629 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9630 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9631 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9632 	if (rc)
9633 		return rc;
9634 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9635 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9636 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9637 
9638 skip_legacy:
9639 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9640 		rc = bnxt_backing_store_cfg_v2(bp);
9641 	else
9642 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9643 	if (rc) {
9644 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9645 			   rc);
9646 		return rc;
9647 	}
9648 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9649 	return 0;
9650 }
9651 
9652 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9653 {
9654 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9655 	u16 page_attr;
9656 	int rc;
9657 
9658 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9659 		return 0;
9660 
9661 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9662 	if (rc)
9663 		return rc;
9664 
9665 	if (BNXT_PAGE_SIZE == 0x2000)
9666 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9667 	else if (BNXT_PAGE_SIZE == 0x10000)
9668 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9669 	else
9670 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9671 	req->pg_size_lvl = cpu_to_le16(page_attr |
9672 				       bp->fw_crash_mem->ring_mem.depth);
9673 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9674 	req->size = cpu_to_le32(bp->fw_crash_len);
9675 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9676 	return hwrm_req_send(bp, req);
9677 }
9678 
9679 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9680 {
9681 	if (bp->fw_crash_mem) {
9682 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9683 		kfree(bp->fw_crash_mem);
9684 		bp->fw_crash_mem = NULL;
9685 	}
9686 }
9687 
9688 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9689 {
9690 	u32 mem_size = 0;
9691 	int rc;
9692 
9693 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9694 		return 0;
9695 
9696 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9697 	if (rc)
9698 		return rc;
9699 
9700 	mem_size = round_up(mem_size, 4);
9701 
9702 	/* keep and use the existing pages */
9703 	if (bp->fw_crash_mem &&
9704 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9705 		goto alloc_done;
9706 
9707 	if (bp->fw_crash_mem)
9708 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9709 	else
9710 		bp->fw_crash_mem = kzalloc_obj(*bp->fw_crash_mem);
9711 	if (!bp->fw_crash_mem)
9712 		return -ENOMEM;
9713 
9714 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9715 	if (rc) {
9716 		bnxt_free_crash_dump_mem(bp);
9717 		return rc;
9718 	}
9719 
9720 alloc_done:
9721 	bp->fw_crash_len = mem_size;
9722 	return 0;
9723 }
9724 
9725 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9726 {
9727 	struct hwrm_func_resource_qcaps_output *resp;
9728 	struct hwrm_func_resource_qcaps_input *req;
9729 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9730 	int rc;
9731 
9732 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9733 	if (rc)
9734 		return rc;
9735 
9736 	req->fid = cpu_to_le16(0xffff);
9737 	resp = hwrm_req_hold(bp, req);
9738 	rc = hwrm_req_send_silent(bp, req);
9739 	if (rc)
9740 		goto hwrm_func_resc_qcaps_exit;
9741 
9742 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9743 	if (!all)
9744 		goto hwrm_func_resc_qcaps_exit;
9745 
9746 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9747 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9748 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9749 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9750 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9751 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9752 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9753 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9754 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9755 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9756 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9757 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9758 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9759 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9760 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9761 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9762 
9763 	if (hw_resc->max_rsscos_ctxs >=
9764 	    hw_resc->max_vnics * BNXT_LARGE_RSS_TO_VNIC_RATIO)
9765 		bp->rss_cap |= BNXT_RSS_CAP_LARGE_RSS_CTX;
9766 
9767 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9768 		u16 max_msix = le16_to_cpu(resp->max_msix);
9769 
9770 		hw_resc->max_nqs = max_msix;
9771 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9772 	}
9773 
9774 	if (BNXT_PF(bp)) {
9775 		struct bnxt_pf_info *pf = &bp->pf;
9776 
9777 		pf->vf_resv_strategy =
9778 			le16_to_cpu(resp->vf_reservation_strategy);
9779 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9780 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9781 	}
9782 hwrm_func_resc_qcaps_exit:
9783 	hwrm_req_drop(bp, req);
9784 	return rc;
9785 }
9786 
9787 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9788 {
9789 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9790 	struct hwrm_port_mac_ptp_qcfg_input *req;
9791 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9792 	u8 flags;
9793 	int rc;
9794 
9795 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9796 		rc = -ENODEV;
9797 		goto no_ptp;
9798 	}
9799 
9800 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9801 	if (rc)
9802 		goto no_ptp;
9803 
9804 	req->port_id = cpu_to_le16(bp->pf.port_id);
9805 	resp = hwrm_req_hold(bp, req);
9806 	rc = hwrm_req_send(bp, req);
9807 	if (rc)
9808 		goto exit;
9809 
9810 	flags = resp->flags;
9811 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9812 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9813 		rc = -ENODEV;
9814 		goto exit;
9815 	}
9816 	if (!ptp) {
9817 		ptp = kzalloc_obj(*ptp);
9818 		if (!ptp) {
9819 			rc = -ENOMEM;
9820 			goto exit;
9821 		}
9822 		ptp->bp = bp;
9823 		bp->ptp_cfg = ptp;
9824 	}
9825 
9826 	if (flags &
9827 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9828 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9829 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9830 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9831 	} else if (BNXT_CHIP_P5(bp)) {
9832 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9833 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9834 	} else {
9835 		rc = -ENODEV;
9836 		goto exit;
9837 	}
9838 	ptp->rtc_configured =
9839 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9840 	rc = bnxt_ptp_init(bp);
9841 	if (rc)
9842 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9843 exit:
9844 	hwrm_req_drop(bp, req);
9845 	if (!rc)
9846 		return 0;
9847 
9848 no_ptp:
9849 	bnxt_ptp_clear(bp);
9850 	kfree(ptp);
9851 	bp->ptp_cfg = NULL;
9852 	return rc;
9853 }
9854 
9855 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9856 {
9857 	u32 flags, flags_ext, flags_ext2, flags_ext3;
9858 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9859 	struct hwrm_func_qcaps_output *resp;
9860 	struct hwrm_func_qcaps_input *req;
9861 	int rc;
9862 
9863 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9864 	if (rc)
9865 		return rc;
9866 
9867 	req->fid = cpu_to_le16(0xffff);
9868 	resp = hwrm_req_hold(bp, req);
9869 	rc = hwrm_req_send(bp, req);
9870 	if (rc)
9871 		goto hwrm_func_qcaps_exit;
9872 
9873 	flags = le32_to_cpu(resp->flags);
9874 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9875 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9876 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9877 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9878 	if (flags & FUNC_QCAPS_RESP_FLAGS_LINK_ADMIN_STATUS_SUPPORTED)
9879 		bp->fw_cap |= BNXT_FW_CAP_LINK_ADMIN;
9880 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9881 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9882 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9883 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9884 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9885 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9886 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9887 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9888 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9889 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9890 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9891 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9892 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9893 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9894 
9895 	flags_ext = le32_to_cpu(resp->flags_ext);
9896 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9897 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9898 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9899 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9900 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PTM_SUPPORTED)
9901 		bp->fw_cap |= BNXT_FW_CAP_PTP_PTM;
9902 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9903 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9904 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9905 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9906 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9907 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9908 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9909 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9910 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9911 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9912 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9913 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9914 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9915 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9916 
9917 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9918 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9919 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9920 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9921 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9922 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9923 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9924 	if (flags_ext2 &
9925 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9926 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9927 	if (BNXT_PF(bp) &&
9928 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9929 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9930 
9931 	flags_ext3 = le32_to_cpu(resp->flags_ext3);
9932 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_ROCE_VF_DYN_ALLOC_SUPPORT)
9933 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_DYN_ALLOC_SUPPORT;
9934 	if (flags_ext3 & FUNC_QCAPS_RESP_FLAGS_EXT3_MIRROR_ON_ROCE_SUPPORTED)
9935 		bp->fw_cap |= BNXT_FW_CAP_MIRROR_ON_ROCE;
9936 
9937 	bp->tx_push_thresh = 0;
9938 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9939 	    BNXT_FW_MAJ(bp) > 217)
9940 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9941 
9942 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9943 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9944 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9945 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9946 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9947 	if (!hw_resc->max_hw_ring_grps)
9948 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9949 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9950 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9951 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9952 
9953 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9954 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9955 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9956 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9957 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9958 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9959 
9960 	if (BNXT_PF(bp)) {
9961 		struct bnxt_pf_info *pf = &bp->pf;
9962 
9963 		pf->fw_fid = le16_to_cpu(resp->fid);
9964 		pf->port_id = le16_to_cpu(resp->port_id);
9965 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9966 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9967 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9968 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9969 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9970 			bp->flags |= BNXT_FLAG_WOL_CAP;
9971 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9972 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9973 		} else {
9974 			bnxt_ptp_clear(bp);
9975 			kfree(bp->ptp_cfg);
9976 			bp->ptp_cfg = NULL;
9977 		}
9978 	} else {
9979 #ifdef CONFIG_BNXT_SRIOV
9980 		struct bnxt_vf_info *vf = &bp->vf;
9981 
9982 		vf->fw_fid = le16_to_cpu(resp->fid);
9983 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9984 #endif
9985 	}
9986 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9987 
9988 hwrm_func_qcaps_exit:
9989 	hwrm_req_drop(bp, req);
9990 	return rc;
9991 }
9992 
9993 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9994 {
9995 	struct hwrm_dbg_qcaps_output *resp;
9996 	struct hwrm_dbg_qcaps_input *req;
9997 	int rc;
9998 
9999 	bp->fw_dbg_cap = 0;
10000 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
10001 		return;
10002 
10003 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
10004 	if (rc)
10005 		return;
10006 
10007 	req->fid = cpu_to_le16(0xffff);
10008 	resp = hwrm_req_hold(bp, req);
10009 	rc = hwrm_req_send(bp, req);
10010 	if (rc)
10011 		goto hwrm_dbg_qcaps_exit;
10012 
10013 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
10014 
10015 hwrm_dbg_qcaps_exit:
10016 	hwrm_req_drop(bp, req);
10017 }
10018 
10019 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
10020 
10021 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
10022 {
10023 	int rc;
10024 
10025 	rc = __bnxt_hwrm_func_qcaps(bp);
10026 	if (rc)
10027 		return rc;
10028 
10029 	bnxt_hwrm_dbg_qcaps(bp);
10030 
10031 	rc = bnxt_hwrm_queue_qportcfg(bp);
10032 	if (rc) {
10033 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
10034 		return rc;
10035 	}
10036 	if (bp->hwrm_spec_code >= 0x10803) {
10037 		rc = bnxt_alloc_ctx_mem(bp);
10038 		if (rc)
10039 			return rc;
10040 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
10041 		if (!rc)
10042 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
10043 	}
10044 	return 0;
10045 }
10046 
10047 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
10048 {
10049 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
10050 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
10051 	u32 flags;
10052 	int rc;
10053 
10054 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
10055 		return 0;
10056 
10057 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
10058 	if (rc)
10059 		return rc;
10060 
10061 	resp = hwrm_req_hold(bp, req);
10062 	rc = hwrm_req_send(bp, req);
10063 	if (rc)
10064 		goto hwrm_cfa_adv_qcaps_exit;
10065 
10066 	flags = le32_to_cpu(resp->flags);
10067 	if (flags &
10068 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
10069 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
10070 
10071 	if (flags &
10072 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
10073 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
10074 
10075 	if (flags &
10076 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
10077 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
10078 
10079 hwrm_cfa_adv_qcaps_exit:
10080 	hwrm_req_drop(bp, req);
10081 	return rc;
10082 }
10083 
10084 static int __bnxt_alloc_fw_health(struct bnxt *bp)
10085 {
10086 	if (bp->fw_health)
10087 		return 0;
10088 
10089 	bp->fw_health = kzalloc_obj(*bp->fw_health);
10090 	if (!bp->fw_health)
10091 		return -ENOMEM;
10092 
10093 	mutex_init(&bp->fw_health->lock);
10094 	return 0;
10095 }
10096 
10097 static int bnxt_alloc_fw_health(struct bnxt *bp)
10098 {
10099 	int rc;
10100 
10101 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
10102 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10103 		return 0;
10104 
10105 	rc = __bnxt_alloc_fw_health(bp);
10106 	if (rc) {
10107 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
10108 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10109 		return rc;
10110 	}
10111 
10112 	return 0;
10113 }
10114 
10115 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
10116 {
10117 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
10118 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
10119 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
10120 }
10121 
10122 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
10123 {
10124 	struct bnxt_fw_health *fw_health = bp->fw_health;
10125 	u32 reg_type;
10126 
10127 	if (!fw_health)
10128 		return;
10129 
10130 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
10131 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10132 		fw_health->status_reliable = false;
10133 
10134 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
10135 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
10136 		fw_health->resets_reliable = false;
10137 }
10138 
10139 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
10140 {
10141 	void __iomem *hs;
10142 	u32 status_loc;
10143 	u32 reg_type;
10144 	u32 sig;
10145 
10146 	if (bp->fw_health)
10147 		bp->fw_health->status_reliable = false;
10148 
10149 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
10150 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
10151 
10152 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
10153 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
10154 		if (!bp->chip_num) {
10155 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
10156 			bp->chip_num = readl(bp->bar0 +
10157 					     BNXT_FW_HEALTH_WIN_BASE +
10158 					     BNXT_GRC_REG_CHIP_NUM);
10159 		}
10160 		if (!BNXT_CHIP_P5_PLUS(bp))
10161 			return;
10162 
10163 		status_loc = BNXT_GRC_REG_STATUS_P5 |
10164 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
10165 	} else {
10166 		status_loc = readl(hs + offsetof(struct hcomm_status,
10167 						 fw_status_loc));
10168 	}
10169 
10170 	if (__bnxt_alloc_fw_health(bp)) {
10171 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
10172 		return;
10173 	}
10174 
10175 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
10176 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
10177 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
10178 		__bnxt_map_fw_health_reg(bp, status_loc);
10179 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
10180 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
10181 	}
10182 
10183 	bp->fw_health->status_reliable = true;
10184 }
10185 
10186 static int bnxt_map_fw_health_regs(struct bnxt *bp)
10187 {
10188 	struct bnxt_fw_health *fw_health = bp->fw_health;
10189 	u32 reg_base = 0xffffffff;
10190 	int i;
10191 
10192 	bp->fw_health->status_reliable = false;
10193 	bp->fw_health->resets_reliable = false;
10194 	/* Only pre-map the monitoring GRC registers using window 3 */
10195 	for (i = 0; i < 4; i++) {
10196 		u32 reg = fw_health->regs[i];
10197 
10198 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
10199 			continue;
10200 		if (reg_base == 0xffffffff)
10201 			reg_base = reg & BNXT_GRC_BASE_MASK;
10202 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
10203 			return -ERANGE;
10204 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
10205 	}
10206 	bp->fw_health->status_reliable = true;
10207 	bp->fw_health->resets_reliable = true;
10208 	if (reg_base == 0xffffffff)
10209 		return 0;
10210 
10211 	__bnxt_map_fw_health_reg(bp, reg_base);
10212 	return 0;
10213 }
10214 
10215 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
10216 {
10217 	if (!bp->fw_health)
10218 		return;
10219 
10220 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
10221 		bp->fw_health->status_reliable = true;
10222 		bp->fw_health->resets_reliable = true;
10223 	} else {
10224 		bnxt_try_map_fw_health_reg(bp);
10225 	}
10226 }
10227 
10228 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
10229 {
10230 	struct bnxt_fw_health *fw_health = bp->fw_health;
10231 	struct hwrm_error_recovery_qcfg_output *resp;
10232 	struct hwrm_error_recovery_qcfg_input *req;
10233 	int rc, i;
10234 
10235 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
10236 		return 0;
10237 
10238 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10239 	if (rc)
10240 		return rc;
10241 
10242 	resp = hwrm_req_hold(bp, req);
10243 	rc = hwrm_req_send(bp, req);
10244 	if (rc)
10245 		goto err_recovery_out;
10246 	fw_health->flags = le32_to_cpu(resp->flags);
10247 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10248 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10249 		rc = -EINVAL;
10250 		goto err_recovery_out;
10251 	}
10252 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10253 	fw_health->master_func_wait_dsecs =
10254 		le32_to_cpu(resp->master_func_wait_period);
10255 	fw_health->normal_func_wait_dsecs =
10256 		le32_to_cpu(resp->normal_func_wait_period);
10257 	fw_health->post_reset_wait_dsecs =
10258 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10259 	fw_health->post_reset_max_wait_dsecs =
10260 		le32_to_cpu(resp->max_bailout_time_after_reset);
10261 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10262 		le32_to_cpu(resp->fw_health_status_reg);
10263 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10264 		le32_to_cpu(resp->fw_heartbeat_reg);
10265 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10266 		le32_to_cpu(resp->fw_reset_cnt_reg);
10267 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10268 		le32_to_cpu(resp->reset_inprogress_reg);
10269 	fw_health->fw_reset_inprog_reg_mask =
10270 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10271 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10272 	if (fw_health->fw_reset_seq_cnt >= 16) {
10273 		rc = -EINVAL;
10274 		goto err_recovery_out;
10275 	}
10276 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10277 		fw_health->fw_reset_seq_regs[i] =
10278 			le32_to_cpu(resp->reset_reg[i]);
10279 		fw_health->fw_reset_seq_vals[i] =
10280 			le32_to_cpu(resp->reset_reg_val[i]);
10281 		fw_health->fw_reset_seq_delay_msec[i] =
10282 			resp->delay_after_reset[i];
10283 	}
10284 err_recovery_out:
10285 	hwrm_req_drop(bp, req);
10286 	if (!rc)
10287 		rc = bnxt_map_fw_health_regs(bp);
10288 	if (rc)
10289 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10290 	return rc;
10291 }
10292 
10293 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10294 {
10295 	struct hwrm_func_reset_input *req;
10296 	int rc;
10297 
10298 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10299 	if (rc)
10300 		return rc;
10301 
10302 	req->enables = 0;
10303 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10304 	return hwrm_req_send(bp, req);
10305 }
10306 
10307 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10308 {
10309 	struct hwrm_nvm_get_dev_info_output nvm_info;
10310 
10311 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10312 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10313 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10314 			 nvm_info.nvm_cfg_ver_upd);
10315 }
10316 
10317 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10318 {
10319 	struct hwrm_queue_qportcfg_output *resp;
10320 	struct hwrm_queue_qportcfg_input *req;
10321 	u8 i, j, *qptr;
10322 	bool no_rdma;
10323 	int rc = 0;
10324 
10325 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10326 	if (rc)
10327 		return rc;
10328 
10329 	resp = hwrm_req_hold(bp, req);
10330 	rc = hwrm_req_send(bp, req);
10331 	if (rc)
10332 		goto qportcfg_exit;
10333 
10334 	if (!resp->max_configurable_queues) {
10335 		rc = -EINVAL;
10336 		goto qportcfg_exit;
10337 	}
10338 	bp->max_tc = resp->max_configurable_queues;
10339 	bp->max_lltc = resp->max_configurable_lossless_queues;
10340 	if (bp->max_tc > BNXT_MAX_QUEUE)
10341 		bp->max_tc = BNXT_MAX_QUEUE;
10342 
10343 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10344 	qptr = &resp->queue_id0;
10345 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10346 		bp->q_info[j].queue_id = *qptr;
10347 		bp->q_ids[i] = *qptr++;
10348 		bp->q_info[j].queue_profile = *qptr++;
10349 		bp->tc_to_qidx[j] = j;
10350 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10351 		    (no_rdma && BNXT_PF(bp)))
10352 			j++;
10353 	}
10354 	bp->max_q = bp->max_tc;
10355 	bp->max_tc = max_t(u8, j, 1);
10356 
10357 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10358 		bp->max_tc = 1;
10359 
10360 	if (bp->max_lltc > bp->max_tc)
10361 		bp->max_lltc = bp->max_tc;
10362 
10363 qportcfg_exit:
10364 	hwrm_req_drop(bp, req);
10365 	return rc;
10366 }
10367 
10368 static int bnxt_hwrm_poll(struct bnxt *bp)
10369 {
10370 	struct hwrm_ver_get_input *req;
10371 	int rc;
10372 
10373 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10374 	if (rc)
10375 		return rc;
10376 
10377 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10378 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10379 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10380 
10381 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10382 	rc = hwrm_req_send(bp, req);
10383 	return rc;
10384 }
10385 
10386 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10387 {
10388 	struct hwrm_ver_get_output *resp;
10389 	struct hwrm_ver_get_input *req;
10390 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10391 	u32 dev_caps_cfg, hwrm_ver;
10392 	int rc, len, max_tmo_secs;
10393 
10394 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10395 	if (rc)
10396 		return rc;
10397 
10398 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10399 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10400 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10401 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10402 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10403 
10404 	resp = hwrm_req_hold(bp, req);
10405 	rc = hwrm_req_send(bp, req);
10406 	if (rc)
10407 		goto hwrm_ver_get_exit;
10408 
10409 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10410 
10411 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10412 			     resp->hwrm_intf_min_8b << 8 |
10413 			     resp->hwrm_intf_upd_8b;
10414 	if (resp->hwrm_intf_maj_8b < 1) {
10415 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10416 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10417 			    resp->hwrm_intf_upd_8b);
10418 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10419 	}
10420 
10421 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10422 			HWRM_VERSION_UPDATE;
10423 
10424 	if (bp->hwrm_spec_code > hwrm_ver)
10425 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10426 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10427 			 HWRM_VERSION_UPDATE);
10428 	else
10429 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10430 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10431 			 resp->hwrm_intf_upd_8b);
10432 
10433 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10434 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10435 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10436 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10437 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10438 		len = FW_VER_STR_LEN;
10439 	} else {
10440 		fw_maj = resp->hwrm_fw_maj_8b;
10441 		fw_min = resp->hwrm_fw_min_8b;
10442 		fw_bld = resp->hwrm_fw_bld_8b;
10443 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10444 		len = BC_HWRM_STR_LEN;
10445 	}
10446 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10447 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10448 		 fw_rsv);
10449 
10450 	if (strlen(resp->active_pkg_name)) {
10451 		int fw_ver_len = strlen(bp->fw_ver_str);
10452 
10453 		snprintf(bp->fw_ver_str + fw_ver_len,
10454 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10455 			 resp->active_pkg_name);
10456 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10457 	}
10458 
10459 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10460 	if (!bp->hwrm_cmd_timeout)
10461 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10462 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10463 	if (!bp->hwrm_cmd_max_timeout)
10464 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10465 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10466 #ifdef CONFIG_DETECT_HUNG_TASK
10467 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10468 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10469 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10470 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10471 	}
10472 #endif
10473 
10474 	if (resp->hwrm_intf_maj_8b >= 1) {
10475 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10476 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10477 	}
10478 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10479 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10480 
10481 	bp->chip_num = le16_to_cpu(resp->chip_num);
10482 	bp->chip_rev = resp->chip_rev;
10483 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10484 	    !resp->chip_metal)
10485 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10486 
10487 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10488 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10489 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10490 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10491 
10492 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10493 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10494 
10495 	if (dev_caps_cfg &
10496 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10497 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10498 
10499 	if (dev_caps_cfg &
10500 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10501 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10502 
10503 	if (dev_caps_cfg &
10504 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10505 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10506 
10507 hwrm_ver_get_exit:
10508 	hwrm_req_drop(bp, req);
10509 	return rc;
10510 }
10511 
10512 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10513 {
10514 	struct hwrm_fw_set_time_input *req;
10515 	struct tm tm;
10516 	time64_t now = ktime_get_real_seconds();
10517 	int rc;
10518 
10519 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10520 	    bp->hwrm_spec_code < 0x10400)
10521 		return -EOPNOTSUPP;
10522 
10523 	time64_to_tm(now, 0, &tm);
10524 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10525 	if (rc)
10526 		return rc;
10527 
10528 	req->year = cpu_to_le16(1900 + tm.tm_year);
10529 	req->month = 1 + tm.tm_mon;
10530 	req->day = tm.tm_mday;
10531 	req->hour = tm.tm_hour;
10532 	req->minute = tm.tm_min;
10533 	req->second = tm.tm_sec;
10534 	return hwrm_req_send(bp, req);
10535 }
10536 
10537 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10538 {
10539 	u64 sw_tmp;
10540 
10541 	hw &= mask;
10542 	sw_tmp = (*sw & ~mask) | hw;
10543 	if (hw < (*sw & mask))
10544 		sw_tmp += mask + 1;
10545 	WRITE_ONCE(*sw, sw_tmp);
10546 }
10547 
10548 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10549 				    int count, bool ignore_zero)
10550 {
10551 	int i;
10552 
10553 	for (i = 0; i < count; i++) {
10554 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10555 
10556 		if (ignore_zero && !hw)
10557 			continue;
10558 
10559 		if (masks[i] == -1ULL)
10560 			sw_stats[i] = hw;
10561 		else
10562 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10563 	}
10564 }
10565 
10566 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10567 {
10568 	if (!stats->hw_stats)
10569 		return;
10570 
10571 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10572 				stats->hw_masks, stats->len / 8, false);
10573 }
10574 
10575 static void bnxt_accumulate_ring_stats(struct bnxt *bp)
10576 {
10577 	struct bnxt_stats_mem *ring0_stats;
10578 	bool ignore_zero = false;
10579 	int i;
10580 
10581 	/* Chip bug.  Counter intermittently becomes 0. */
10582 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10583 		ignore_zero = true;
10584 
10585 	for (i = 0; i < bp->cp_nr_rings; i++) {
10586 		struct bnxt_napi *bnapi = bp->bnapi[i];
10587 		struct bnxt_cp_ring_info *cpr;
10588 		struct bnxt_stats_mem *stats;
10589 
10590 		cpr = &bnapi->cp_ring;
10591 		stats = &cpr->stats;
10592 		if (!i)
10593 			ring0_stats = stats;
10594 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10595 					ring0_stats->hw_masks,
10596 					ring0_stats->len / 8, ignore_zero);
10597 	}
10598 }
10599 
10600 static void bnxt_accumulate_port_stats(struct bnxt *bp)
10601 {
10602 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10603 		struct bnxt_stats_mem *stats = &bp->port_stats;
10604 		__le64 *hw_stats = stats->hw_stats;
10605 		u64 *sw_stats = stats->sw_stats;
10606 		u64 *masks = stats->hw_masks;
10607 		int cnt;
10608 
10609 		cnt = sizeof(struct rx_port_stats) / 8;
10610 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10611 
10612 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10613 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10614 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10615 		cnt = sizeof(struct tx_port_stats) / 8;
10616 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10617 	}
10618 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10619 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10620 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10621 	}
10622 }
10623 
10624 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10625 {
10626 	bnxt_accumulate_ring_stats(bp);
10627 	bnxt_accumulate_port_stats(bp);
10628 }
10629 
10630 /* Re-accumulate ring stats from DMA buffers if stale.
10631  * uAPIs for reading sw_stats should call this first.
10632  *
10633  * We promise user space update frequency of bp->stats_coal_ticks but
10634  * the update is a two step process - first device updates the DMA buffer,
10635  * then we have to update from that buffer to driver stats in the service work.
10636  * Worst case we would be 2x off from the desired frequency.
10637  * Sync the stats sooner, if stale. The 20% threshold was chosen arbitrarily.
10638  *
10639  * Ideally we would split the user-configured time into two portions,
10640  * i.e. also lower the DMA period by the 20%. But the DMA timer seems to have
10641  * too coarse granularity to play such tricks.
10642  */
10643 void bnxt_sync_ring_stats(struct bnxt *bp)
10644 {
10645 	unsigned long stale;
10646 
10647 	if (!netif_running(bp->dev) || !bp->stats_coal_ticks)
10648 		return;
10649 
10650 	spin_lock(&bp->stats_lock);
10651 	stale = usecs_to_jiffies(bp->stats_coal_ticks / 5);
10652 	if (time_after_eq(jiffies, bp->stats_updated_jiffies + stale)) {
10653 		bnxt_accumulate_ring_stats(bp);
10654 		bp->stats_updated_jiffies = jiffies;
10655 	}
10656 	spin_unlock(&bp->stats_lock);
10657 }
10658 
10659 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10660 {
10661 	struct hwrm_port_qstats_input *req;
10662 	struct bnxt_pf_info *pf = &bp->pf;
10663 	int rc;
10664 
10665 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10666 		return 0;
10667 
10668 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10669 		return -EOPNOTSUPP;
10670 
10671 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10672 	if (rc)
10673 		return rc;
10674 
10675 	req->flags = flags;
10676 	req->port_id = cpu_to_le16(pf->port_id);
10677 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10678 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10679 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10680 	return hwrm_req_send(bp, req);
10681 }
10682 
10683 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10684 {
10685 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10686 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10687 	struct hwrm_port_qstats_ext_output *resp_qs;
10688 	struct hwrm_port_qstats_ext_input *req_qs;
10689 	struct bnxt_pf_info *pf = &bp->pf;
10690 	u32 tx_stat_size;
10691 	int rc;
10692 
10693 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10694 		return 0;
10695 
10696 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10697 		return -EOPNOTSUPP;
10698 
10699 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10700 	if (rc)
10701 		return rc;
10702 
10703 	req_qs->flags = flags;
10704 	req_qs->port_id = cpu_to_le16(pf->port_id);
10705 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10706 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10707 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10708 		       sizeof(struct tx_port_stats_ext) : 0;
10709 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10710 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10711 	resp_qs = hwrm_req_hold(bp, req_qs);
10712 	rc = hwrm_req_send(bp, req_qs);
10713 	if (!rc) {
10714 		bp->fw_rx_stats_ext_size =
10715 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10716 		if (BNXT_FW_MAJ(bp) < 220 &&
10717 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10718 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10719 
10720 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10721 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10722 	} else {
10723 		bp->fw_rx_stats_ext_size = 0;
10724 		bp->fw_tx_stats_ext_size = 0;
10725 	}
10726 	hwrm_req_drop(bp, req_qs);
10727 
10728 	if (flags)
10729 		return rc;
10730 
10731 	if (bp->fw_tx_stats_ext_size <=
10732 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10733 		bp->pri2cos_valid = 0;
10734 		return rc;
10735 	}
10736 
10737 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10738 	if (rc)
10739 		return rc;
10740 
10741 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10742 
10743 	resp_qc = hwrm_req_hold(bp, req_qc);
10744 	rc = hwrm_req_send(bp, req_qc);
10745 	if (!rc) {
10746 		u8 *pri2cos;
10747 		int i, j;
10748 
10749 		pri2cos = &resp_qc->pri0_cos_queue_id;
10750 		for (i = 0; i < 8; i++) {
10751 			u8 queue_id = pri2cos[i];
10752 			u8 queue_idx;
10753 
10754 			/* Per port queue IDs start from 0, 10, 20, etc */
10755 			queue_idx = queue_id % 10;
10756 			if (queue_idx > BNXT_MAX_QUEUE) {
10757 				bp->pri2cos_valid = false;
10758 				hwrm_req_drop(bp, req_qc);
10759 				return rc;
10760 			}
10761 			for (j = 0; j < bp->max_q; j++) {
10762 				if (bp->q_ids[j] == queue_id)
10763 					bp->pri2cos_idx[i] = queue_idx;
10764 			}
10765 		}
10766 		bp->pri2cos_valid = true;
10767 	}
10768 	hwrm_req_drop(bp, req_qc);
10769 
10770 	return rc;
10771 }
10772 
10773 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10774 {
10775 	bnxt_hwrm_tunnel_dst_port_free(bp,
10776 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10777 	bnxt_hwrm_tunnel_dst_port_free(bp,
10778 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10779 }
10780 
10781 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10782 {
10783 	int rc, i;
10784 	u32 tpa_flags = 0;
10785 
10786 	if (set_tpa)
10787 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10788 	else if (BNXT_NO_FW_ACCESS(bp))
10789 		return 0;
10790 	for (i = 0; i < bp->nr_vnics; i++) {
10791 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10792 		if (rc) {
10793 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10794 				   i, rc);
10795 			return rc;
10796 		}
10797 	}
10798 	return 0;
10799 }
10800 
10801 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10802 {
10803 	int i;
10804 
10805 	for (i = 0; i < bp->nr_vnics; i++)
10806 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10807 }
10808 
10809 static void bnxt_clear_vnic(struct bnxt *bp)
10810 {
10811 	if (!bp->vnic_info)
10812 		return;
10813 
10814 	bnxt_hwrm_clear_vnic_filter(bp);
10815 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10816 		/* clear all RSS setting before free vnic ctx */
10817 		bnxt_hwrm_clear_vnic_rss(bp);
10818 		bnxt_hwrm_vnic_ctx_free(bp);
10819 	}
10820 	/* before free the vnic, undo the vnic tpa settings */
10821 	if (bp->flags & BNXT_FLAG_TPA)
10822 		bnxt_set_tpa(bp, false);
10823 	bnxt_hwrm_vnic_free(bp);
10824 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10825 		bnxt_hwrm_vnic_ctx_free(bp);
10826 }
10827 
10828 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10829 				    bool irq_re_init)
10830 {
10831 	bnxt_clear_vnic(bp);
10832 	bnxt_hwrm_ring_free(bp, close_path);
10833 	bnxt_hwrm_ring_grp_free(bp);
10834 	if (irq_re_init) {
10835 		bnxt_hwrm_stat_ctx_free(bp);
10836 		bnxt_hwrm_free_tunnel_ports(bp);
10837 	}
10838 }
10839 
10840 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10841 {
10842 	struct hwrm_func_cfg_input *req;
10843 	u8 evb_mode;
10844 	int rc;
10845 
10846 	if (br_mode == BRIDGE_MODE_VEB)
10847 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10848 	else if (br_mode == BRIDGE_MODE_VEPA)
10849 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10850 	else
10851 		return -EINVAL;
10852 
10853 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10854 	if (rc)
10855 		return rc;
10856 
10857 	req->fid = cpu_to_le16(0xffff);
10858 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10859 	req->evb_mode = evb_mode;
10860 	return hwrm_req_send(bp, req);
10861 }
10862 
10863 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10864 {
10865 	struct hwrm_func_cfg_input *req;
10866 	int rc;
10867 
10868 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10869 		return 0;
10870 
10871 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10872 	if (rc)
10873 		return rc;
10874 
10875 	req->fid = cpu_to_le16(0xffff);
10876 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10877 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10878 	if (size == 128)
10879 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10880 
10881 	return hwrm_req_send(bp, req);
10882 }
10883 
10884 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10885 {
10886 	int rc;
10887 
10888 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10889 		goto skip_rss_ctx;
10890 
10891 	/* allocate context for vnic */
10892 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10893 	if (rc) {
10894 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10895 			   vnic->vnic_id, rc);
10896 		goto vnic_setup_err;
10897 	}
10898 	bp->rsscos_nr_ctxs++;
10899 
10900 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10901 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10902 		if (rc) {
10903 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10904 				   vnic->vnic_id, rc);
10905 			goto vnic_setup_err;
10906 		}
10907 		bp->rsscos_nr_ctxs++;
10908 	}
10909 
10910 skip_rss_ctx:
10911 	bnxt_fill_hw_rss_tbl(bp, vnic);
10912 	/* configure default vnic, ring grp */
10913 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10914 	if (rc) {
10915 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10916 			   vnic->vnic_id, rc);
10917 		goto vnic_setup_err;
10918 	}
10919 
10920 	/* Enable RSS hashing on vnic */
10921 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10922 	if (rc) {
10923 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10924 			   vnic->vnic_id, rc);
10925 		goto vnic_setup_err;
10926 	}
10927 
10928 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10929 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10930 		if (rc) {
10931 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10932 				   vnic->vnic_id, rc);
10933 		}
10934 	}
10935 
10936 vnic_setup_err:
10937 	return rc;
10938 }
10939 
10940 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10941 			  u8 valid)
10942 {
10943 	struct hwrm_vnic_update_input *req;
10944 	int rc;
10945 
10946 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10947 	if (rc)
10948 		return rc;
10949 
10950 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10951 
10952 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10953 		req->mru = cpu_to_le16(vnic->mru);
10954 
10955 	req->enables = cpu_to_le32(valid);
10956 
10957 	return hwrm_req_send(bp, req);
10958 }
10959 
10960 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10961 {
10962 	int rc;
10963 
10964 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10965 	if (rc) {
10966 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10967 			   vnic->vnic_id, rc);
10968 		return rc;
10969 	}
10970 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10971 	if (rc)
10972 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10973 			   vnic->vnic_id, rc);
10974 	return rc;
10975 }
10976 
10977 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10978 {
10979 	int rc, i, nr_ctxs;
10980 
10981 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10982 	for (i = 0; i < nr_ctxs; i++) {
10983 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10984 		if (rc) {
10985 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10986 				   vnic->vnic_id, i, rc);
10987 			break;
10988 		}
10989 		bp->rsscos_nr_ctxs++;
10990 	}
10991 	if (i < nr_ctxs)
10992 		return -ENOMEM;
10993 
10994 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10995 	if (rc)
10996 		return rc;
10997 
10998 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10999 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
11000 		if (rc) {
11001 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
11002 				   vnic->vnic_id, rc);
11003 		}
11004 	}
11005 	return rc;
11006 }
11007 
11008 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
11009 {
11010 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11011 		return __bnxt_setup_vnic_p5(bp, vnic);
11012 	else
11013 		return __bnxt_setup_vnic(bp, vnic);
11014 }
11015 
11016 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
11017 				     struct bnxt_vnic_info *vnic,
11018 				     u16 start_rx_ring_idx, int rx_rings)
11019 {
11020 	int rc;
11021 
11022 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
11023 	if (rc) {
11024 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
11025 			   vnic->vnic_id, rc);
11026 		return rc;
11027 	}
11028 	return bnxt_setup_vnic(bp, vnic);
11029 }
11030 
11031 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
11032 {
11033 	struct bnxt_vnic_info *vnic;
11034 	int i, rc = 0;
11035 
11036 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
11037 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
11038 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
11039 	}
11040 
11041 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11042 		return 0;
11043 
11044 	for (i = 0; i < bp->rx_nr_rings; i++) {
11045 		u16 vnic_id = i + 1;
11046 		u16 ring_id = i;
11047 
11048 		if (vnic_id >= bp->nr_vnics)
11049 			break;
11050 
11051 		vnic = &bp->vnic_info[vnic_id];
11052 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
11053 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
11054 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
11055 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
11056 			break;
11057 	}
11058 	return rc;
11059 }
11060 
11061 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
11062 			  bool all)
11063 {
11064 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11065 	struct bnxt_filter_base *usr_fltr, *tmp;
11066 	struct bnxt_ntuple_filter *ntp_fltr;
11067 	int i;
11068 
11069 	bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
11070 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
11071 		if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
11072 			bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
11073 	}
11074 	if (!all)
11075 		return;
11076 
11077 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
11078 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
11079 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
11080 			ntp_fltr = container_of(usr_fltr,
11081 						struct bnxt_ntuple_filter,
11082 						base);
11083 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
11084 			bnxt_del_ntp_filter(bp, ntp_fltr);
11085 			bnxt_del_one_usr_fltr(bp, usr_fltr);
11086 		}
11087 	}
11088 
11089 	if (vnic->rss_table)
11090 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
11091 				  vnic->rss_table,
11092 				  vnic->rss_table_dma_addr);
11093 	bp->num_rss_ctx--;
11094 }
11095 
11096 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11097 				  int rxr_id)
11098 {
11099 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
11100 	int i, vnic_rx;
11101 
11102 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
11103 	 * must be updated because a future filter may use it.
11104 	 */
11105 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
11106 		return true;
11107 
11108 	for (i = 0; i < tbl_size; i++) {
11109 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
11110 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
11111 		else
11112 			vnic_rx = bp->rss_indir_tbl[i];
11113 
11114 		if (rxr_id == vnic_rx)
11115 			return true;
11116 	}
11117 
11118 	return false;
11119 }
11120 
11121 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
11122 				u16 mru, int rxr_id)
11123 {
11124 	int rc;
11125 
11126 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
11127 		return 0;
11128 
11129 	if (mru) {
11130 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
11131 		if (rc) {
11132 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
11133 				   vnic->vnic_id, rc);
11134 			return rc;
11135 		}
11136 		if (rxr_id == vnic->default_rx_ring) {
11137 			rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11138 			if (rc)
11139 				return rc;
11140 		}
11141 	}
11142 	vnic->mru = mru;
11143 	bnxt_hwrm_vnic_update(bp, vnic,
11144 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
11145 
11146 	return 0;
11147 }
11148 
11149 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
11150 {
11151 	struct ethtool_rxfh_context *ctx;
11152 	unsigned long context;
11153 	int rc;
11154 
11155 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11156 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11157 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11158 
11159 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
11160 		if (rc)
11161 			return rc;
11162 	}
11163 
11164 	return 0;
11165 }
11166 
11167 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
11168 {
11169 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
11170 	struct ethtool_rxfh_context *ctx;
11171 	unsigned long context;
11172 
11173 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11174 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11175 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
11176 
11177 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
11178 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
11179 		    __bnxt_setup_vnic_p5(bp, vnic)) {
11180 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
11181 				   rss_ctx->index);
11182 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
11183 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
11184 		}
11185 	}
11186 }
11187 
11188 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
11189 {
11190 	struct ethtool_rxfh_context *ctx;
11191 	unsigned long context;
11192 
11193 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
11194 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
11195 
11196 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
11197 	}
11198 }
11199 
11200 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
11201 static bool bnxt_promisc_ok(struct bnxt *bp)
11202 {
11203 #ifdef CONFIG_BNXT_SRIOV
11204 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
11205 		return false;
11206 #endif
11207 	return true;
11208 }
11209 
11210 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
11211 {
11212 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
11213 	unsigned int rc = 0;
11214 
11215 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
11216 	if (rc) {
11217 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11218 			   rc);
11219 		return rc;
11220 	}
11221 
11222 	/* Setup the proper default RX ring */
11223 	bnxt_fill_hw_rss_tbl(bp, vnic);
11224 
11225 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
11226 	if (rc) {
11227 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
11228 			   rc);
11229 		return rc;
11230 	}
11231 	return rc;
11232 }
11233 
11234 static int bnxt_cfg_rx_mode(struct bnxt *, struct netdev_hw_addr_list *, bool);
11235 static bool bnxt_mc_list_updated(struct bnxt *, u32 *,
11236 				 const struct netdev_hw_addr_list *);
11237 
11238 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
11239 {
11240 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
11241 	int rc = 0;
11242 	unsigned int rx_nr_rings = bp->rx_nr_rings;
11243 
11244 	if (irq_re_init) {
11245 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
11246 		if (rc) {
11247 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
11248 				   rc);
11249 			goto err_out;
11250 		}
11251 	}
11252 
11253 	rc = bnxt_hwrm_ring_alloc(bp);
11254 	if (rc) {
11255 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
11256 		goto err_out;
11257 	}
11258 
11259 	rc = bnxt_hwrm_ring_grp_alloc(bp);
11260 	if (rc) {
11261 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
11262 		goto err_out;
11263 	}
11264 
11265 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11266 		rx_nr_rings--;
11267 
11268 	/* default vnic 0 */
11269 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
11270 	if (rc) {
11271 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
11272 		goto err_out;
11273 	}
11274 
11275 	if (BNXT_VF(bp))
11276 		bnxt_hwrm_func_qcfg(bp);
11277 
11278 	rc = bnxt_setup_vnic(bp, vnic);
11279 	if (rc)
11280 		goto err_out;
11281 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
11282 		bnxt_hwrm_update_rss_hash_cfg(bp);
11283 
11284 	if (bp->flags & BNXT_FLAG_RFS) {
11285 		rc = bnxt_alloc_rfs_vnics(bp);
11286 		if (rc)
11287 			goto err_out;
11288 	}
11289 
11290 	if (bp->flags & BNXT_FLAG_TPA) {
11291 		rc = bnxt_set_tpa(bp, true);
11292 		if (rc)
11293 			goto err_out;
11294 	}
11295 
11296 	if (BNXT_VF(bp))
11297 		bnxt_update_vf_mac(bp);
11298 
11299 	/* Filter for default vnic 0 */
11300 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11301 	if (rc) {
11302 		if (BNXT_VF(bp) && rc == -ENODEV)
11303 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11304 		else
11305 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11306 		goto err_out;
11307 	}
11308 	vnic->uc_filter_count = 1;
11309 
11310 	vnic->rx_mask = 0;
11311 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11312 		goto skip_rx_mask;
11313 
11314 	if (bp->dev->flags & IFF_BROADCAST)
11315 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11316 
11317 	if (bp->dev->flags & IFF_PROMISC)
11318 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11319 
11320 	if (bp->dev->flags & IFF_ALLMULTI) {
11321 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11322 		vnic->mc_list_count = 0;
11323 	} else if (bp->dev->flags & IFF_MULTICAST) {
11324 		u32 mask = 0;
11325 
11326 		bnxt_mc_list_updated(bp, &mask, &bp->dev->mc);
11327 		vnic->rx_mask |= mask;
11328 	}
11329 
11330 	rc = bnxt_cfg_rx_mode(bp, &bp->dev->uc, true);
11331 	if (rc == -EAGAIN) {
11332 		netif_rx_mode_schedule_retry(bp->dev);
11333 		rc = 0;
11334 	} else if (rc) {
11335 		goto err_out;
11336 	}
11337 
11338 skip_rx_mask:
11339 	rc = bnxt_hwrm_set_coal(bp);
11340 	if (rc)
11341 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11342 				rc);
11343 
11344 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11345 		rc = bnxt_setup_nitroa0_vnic(bp);
11346 		if (rc)
11347 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11348 				   rc);
11349 	}
11350 
11351 	if (BNXT_VF(bp)) {
11352 		bnxt_hwrm_func_qcfg(bp);
11353 		netdev_update_features(bp->dev);
11354 	}
11355 
11356 	return 0;
11357 
11358 err_out:
11359 	bnxt_hwrm_resource_free(bp, 0, true);
11360 
11361 	return rc;
11362 }
11363 
11364 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11365 {
11366 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11367 	return 0;
11368 }
11369 
11370 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11371 {
11372 	int rc;
11373 
11374 	bnxt_init_cp_rings(bp);
11375 	rc = bnxt_init_rx_rings(bp);
11376 	if (rc)
11377 		return rc;
11378 
11379 	bnxt_init_tx_rings(bp);
11380 	bnxt_init_ring_grps(bp, irq_re_init);
11381 	bnxt_init_vnics(bp);
11382 
11383 	return bnxt_init_chip(bp, irq_re_init);
11384 }
11385 
11386 static int bnxt_set_real_num_queues(struct bnxt *bp)
11387 {
11388 	int rc;
11389 	struct net_device *dev = bp->dev;
11390 
11391 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11392 					  bp->tx_nr_rings_xdp);
11393 	if (rc)
11394 		return rc;
11395 
11396 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11397 	if (rc)
11398 		return rc;
11399 
11400 #ifdef CONFIG_RFS_ACCEL
11401 	if (bp->flags & BNXT_FLAG_RFS)
11402 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11403 #endif
11404 
11405 	return rc;
11406 }
11407 
11408 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11409 			     bool shared)
11410 {
11411 	int _rx = *rx, _tx = *tx;
11412 
11413 	if (shared) {
11414 		*rx = min_t(int, _rx, max);
11415 		*tx = min_t(int, _tx, max);
11416 	} else {
11417 		if (max < 2)
11418 			return -ENOMEM;
11419 
11420 		while (_rx + _tx > max) {
11421 			if (_rx > _tx && _rx > 1)
11422 				_rx--;
11423 			else if (_tx > 1)
11424 				_tx--;
11425 		}
11426 		*rx = _rx;
11427 		*tx = _tx;
11428 	}
11429 	return 0;
11430 }
11431 
11432 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11433 {
11434 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11435 }
11436 
11437 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11438 {
11439 	int tcs = bp->num_tc;
11440 
11441 	if (!tcs)
11442 		tcs = 1;
11443 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11444 }
11445 
11446 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11447 {
11448 	int tcs = bp->num_tc;
11449 
11450 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11451 	       bp->tx_nr_rings_xdp;
11452 }
11453 
11454 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11455 			   bool sh)
11456 {
11457 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11458 
11459 	if (tx_cp != *tx) {
11460 		int tx_saved = tx_cp, rc;
11461 
11462 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11463 		if (rc)
11464 			return rc;
11465 		if (tx_cp != tx_saved)
11466 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11467 		return 0;
11468 	}
11469 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11470 }
11471 
11472 static void bnxt_setup_msix(struct bnxt *bp)
11473 {
11474 	const int len = sizeof(bp->irq_tbl[0].name);
11475 	struct net_device *dev = bp->dev;
11476 	int tcs, i;
11477 
11478 	tcs = bp->num_tc;
11479 	if (tcs) {
11480 		int i, off, count;
11481 
11482 		for (i = 0; i < tcs; i++) {
11483 			count = bp->tx_nr_rings_per_tc;
11484 			off = BNXT_TC_TO_RING_BASE(bp, i);
11485 			netdev_set_tc_queue(dev, i, count, off);
11486 		}
11487 	}
11488 
11489 	for (i = 0; i < bp->cp_nr_rings; i++) {
11490 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11491 		char *attr;
11492 
11493 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11494 			attr = "TxRx";
11495 		else if (i < bp->rx_nr_rings)
11496 			attr = "rx";
11497 		else
11498 			attr = "tx";
11499 
11500 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11501 			 attr, i);
11502 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11503 	}
11504 }
11505 
11506 static int bnxt_init_int_mode(struct bnxt *bp);
11507 
11508 static int bnxt_change_msix(struct bnxt *bp, int total)
11509 {
11510 	struct msi_map map;
11511 	int i;
11512 
11513 	/* add MSIX to the end if needed */
11514 	for (i = bp->total_irqs; i < total; i++) {
11515 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11516 		if (map.index < 0)
11517 			return bp->total_irqs;
11518 		bp->irq_tbl[i].vector = map.virq;
11519 		bp->total_irqs++;
11520 	}
11521 
11522 	/* trim MSIX from the end if needed */
11523 	for (i = bp->total_irqs; i > total; i--) {
11524 		map.index = i - 1;
11525 		map.virq = bp->irq_tbl[i - 1].vector;
11526 		pci_msix_free_irq(bp->pdev, map);
11527 		bp->total_irqs--;
11528 	}
11529 	return bp->total_irqs;
11530 }
11531 
11532 static int bnxt_setup_int_mode(struct bnxt *bp)
11533 {
11534 	int rc;
11535 
11536 	if (!bp->irq_tbl) {
11537 		rc = bnxt_init_int_mode(bp);
11538 		if (rc || !bp->irq_tbl)
11539 			return rc ?: -ENODEV;
11540 	}
11541 
11542 	bnxt_setup_msix(bp);
11543 
11544 	rc = bnxt_set_real_num_queues(bp);
11545 	return rc;
11546 }
11547 
11548 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11549 {
11550 	return bp->hw_resc.max_rsscos_ctxs;
11551 }
11552 
11553 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11554 {
11555 	return bp->hw_resc.max_vnics;
11556 }
11557 
11558 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11559 {
11560 	return bp->hw_resc.max_stat_ctxs;
11561 }
11562 
11563 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11564 {
11565 	return bp->hw_resc.max_cp_rings;
11566 }
11567 
11568 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11569 {
11570 	unsigned int cp = bp->hw_resc.max_cp_rings;
11571 
11572 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11573 		cp -= bnxt_get_ulp_msix_num(bp);
11574 
11575 	return cp;
11576 }
11577 
11578 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11579 {
11580 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11581 
11582 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11583 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11584 
11585 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11586 }
11587 
11588 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11589 {
11590 	bp->hw_resc.max_irqs = max_irqs;
11591 }
11592 
11593 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11594 {
11595 	unsigned int cp;
11596 
11597 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11598 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11599 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11600 	else
11601 		return cp - bp->cp_nr_rings;
11602 }
11603 
11604 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11605 {
11606 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11607 }
11608 
11609 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11610 {
11611 	int max_irq = bnxt_get_max_func_irqs(bp);
11612 	int total_req = bp->cp_nr_rings + num;
11613 
11614 	if (max_irq < total_req) {
11615 		num = max_irq - bp->cp_nr_rings;
11616 		if (num <= 0)
11617 			return 0;
11618 	}
11619 	return num;
11620 }
11621 
11622 static int bnxt_get_num_msix(struct bnxt *bp)
11623 {
11624 	if (!BNXT_NEW_RM(bp))
11625 		return bnxt_get_max_func_irqs(bp);
11626 
11627 	return bnxt_nq_rings_in_use(bp);
11628 }
11629 
11630 static int bnxt_init_int_mode(struct bnxt *bp)
11631 {
11632 	int i, total_vecs, max, rc, min = 1, ulp_msix, tx_cp, tbl_size;
11633 
11634 	total_vecs = bnxt_get_num_msix(bp);
11635 	max = bnxt_get_max_func_irqs(bp);
11636 	if (total_vecs > max)
11637 		total_vecs = max;
11638 
11639 	if (!total_vecs)
11640 		return 0;
11641 
11642 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11643 		min = 2;
11644 
11645 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11646 					   PCI_IRQ_MSIX);
11647 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11648 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11649 		rc = -ENODEV;
11650 		goto msix_setup_exit;
11651 	}
11652 
11653 	tbl_size = total_vecs;
11654 	if (pci_msix_can_alloc_dyn(bp->pdev))
11655 		tbl_size = max;
11656 	bp->irq_tbl = kzalloc_objs(*bp->irq_tbl, tbl_size);
11657 	if (!bp->irq_tbl) {
11658 		rc = -ENOMEM;
11659 		goto msix_setup_exit;
11660 	}
11661 
11662 	for (i = 0; i < total_vecs; i++)
11663 		bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11664 
11665 	bp->total_irqs = total_vecs;
11666 	/* Trim rings based upon num of vectors allocated */
11667 	rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11668 			     total_vecs - ulp_msix, min == 1);
11669 	if (rc)
11670 		goto msix_setup_exit;
11671 
11672 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11673 	bp->cp_nr_rings = (min == 1) ?
11674 			  max_t(int, tx_cp, bp->rx_nr_rings) :
11675 			  tx_cp + bp->rx_nr_rings;
11676 
11677 	return 0;
11678 
11679 msix_setup_exit:
11680 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11681 	kfree(bp->irq_tbl);
11682 	bp->irq_tbl = NULL;
11683 	pci_free_irq_vectors(bp->pdev);
11684 	return rc;
11685 }
11686 
11687 static void bnxt_clear_int_mode(struct bnxt *bp)
11688 {
11689 	pci_free_irq_vectors(bp->pdev);
11690 
11691 	kfree(bp->irq_tbl);
11692 	bp->irq_tbl = NULL;
11693 }
11694 
11695 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11696 {
11697 	struct bnxt_en_dev *edev = bp->edev[BNXT_AUXDEV_RDMA];
11698 	bool irq_cleared = false;
11699 	bool irq_change = false;
11700 	int tcs = bp->num_tc;
11701 	int irqs_required;
11702 	int rc;
11703 
11704 	if (!bnxt_need_reserve_rings(bp))
11705 		return 0;
11706 
11707 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(edev)) {
11708 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11709 
11710 		if (ulp_msix > bp->ulp_num_msix_want)
11711 			ulp_msix = bp->ulp_num_msix_want;
11712 		irqs_required = ulp_msix + bp->cp_nr_rings;
11713 	} else {
11714 		irqs_required = bnxt_get_num_msix(bp);
11715 	}
11716 
11717 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11718 		irq_change = true;
11719 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11720 			bnxt_ulp_irq_stop(bp);
11721 			bnxt_clear_int_mode(bp);
11722 			irq_cleared = true;
11723 		}
11724 	}
11725 	rc = __bnxt_reserve_rings(bp);
11726 	if (irq_cleared) {
11727 		if (!rc)
11728 			rc = bnxt_init_int_mode(bp);
11729 		bnxt_ulp_irq_restart(bp, rc);
11730 	} else if (irq_change && !rc) {
11731 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11732 			rc = -ENOSPC;
11733 	}
11734 	if (rc) {
11735 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11736 		return rc;
11737 	}
11738 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11739 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11740 		netdev_err(bp->dev, "tx ring reservation failure\n");
11741 		netdev_reset_tc(bp->dev);
11742 		bp->num_tc = 0;
11743 		if (bp->tx_nr_rings_xdp)
11744 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11745 		else
11746 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11747 		return -ENOMEM;
11748 	}
11749 	return 0;
11750 }
11751 
11752 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11753 {
11754 	struct bnxt_tx_ring_info *txr;
11755 	struct netdev_queue *txq;
11756 	struct bnxt_napi *bnapi;
11757 	int i;
11758 
11759 	bnapi = bp->bnapi[idx];
11760 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11761 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11762 		synchronize_net();
11763 
11764 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11765 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11766 			if (txq) {
11767 				__netif_tx_lock_bh(txq);
11768 				netif_tx_stop_queue(txq);
11769 				__netif_tx_unlock_bh(txq);
11770 			}
11771 		}
11772 
11773 		if (!bp->tph_mode)
11774 			continue;
11775 
11776 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11777 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11778 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11779 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11780 	}
11781 }
11782 
11783 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11784 {
11785 	struct bnxt_tx_ring_info *txr;
11786 	struct netdev_queue *txq;
11787 	struct bnxt_napi *bnapi;
11788 	int rc, i;
11789 
11790 	bnapi = bp->bnapi[idx];
11791 	/* All rings have been reserved and previously allocated.
11792 	 * Reallocating with the same parameters should never fail.
11793 	 */
11794 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11795 		if (!bp->tph_mode)
11796 			goto start_tx;
11797 
11798 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11799 		if (rc)
11800 			return rc;
11801 
11802 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11803 		if (rc)
11804 			return rc;
11805 
11806 		txr->tx_prod = 0;
11807 		txr->tx_cons = 0;
11808 		txr->tx_hw_cons = 0;
11809 		txr->kick_pending = 0;
11810 		txr->kick_txbd0 = NULL;
11811 start_tx:
11812 		WRITE_ONCE(txr->dev_state, 0);
11813 		synchronize_net();
11814 
11815 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11816 			continue;
11817 
11818 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11819 		if (txq)
11820 			netif_tx_start_queue(txq);
11821 	}
11822 
11823 	return 0;
11824 }
11825 
11826 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11827 				     const cpumask_t *mask)
11828 {
11829 	struct bnxt_irq *irq;
11830 	u16 tag;
11831 
11832 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11833 
11834 	cpumask_copy(irq->bp->ring_cpu_mask[irq->ring_nr], mask);
11835 	set_bit(irq->ring_nr, irq->bp->ring_affinity_set);
11836 
11837 #ifdef CONFIG_RFS_ACCEL
11838 	if (irq->bp->dev->rx_cpu_rmap && irq->ring_nr < irq->bp->rx_nr_rings) {
11839 		int err;
11840 
11841 		err = cpu_rmap_update(irq->bp->dev->rx_cpu_rmap, irq->ring_nr,
11842 				      mask);
11843 		if (err)
11844 			netdev_warn(irq->bp->dev,
11845 				    "aRFS rmap update failed: %d\n", err);
11846 	}
11847 #endif
11848 
11849 	if (!irq->bp->tph_mode)
11850 		return;
11851 
11852 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11853 		return;
11854 
11855 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11856 				cpumask_first(mask), &tag))
11857 		return;
11858 
11859 	WRITE_ONCE(irq->new_tag, tag);
11860 	bnxt_queue_sp_work(irq->bp, BNXT_TPH_UPDATE_SP_EVENT);
11861 }
11862 
11863 static void bnxt_irq_affinity_release(struct kref *ref)
11864 {
11865 	struct irq_affinity_notify *notify =
11866 		container_of(ref, struct irq_affinity_notify, kref);
11867 	struct bnxt_irq *irq;
11868 
11869 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11870 
11871 	if (!irq->bp->tph_mode)
11872 		return;
11873 
11874 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11875 		netdev_err(irq->bp->dev,
11876 			   "Setting ST=0 for MSIX entry %d failed\n",
11877 			   irq->msix_nr);
11878 		return;
11879 	}
11880 }
11881 
11882 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11883 {
11884 	irq_set_affinity_notifier(irq->vector, NULL);
11885 }
11886 
11887 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11888 {
11889 	struct irq_affinity_notify *notify;
11890 
11891 	irq->bp = bp;
11892 
11893 	/* Register IRQ affinity notifier */
11894 	notify = &irq->affinity_notify;
11895 	notify->irq = irq->vector;
11896 	notify->notify = bnxt_irq_affinity_notify;
11897 	notify->release = bnxt_irq_affinity_release;
11898 
11899 	irq_set_affinity_notifier(irq->vector, notify);
11900 }
11901 
11902 static int bnxt_alloc_ring_cpu_masks(struct bnxt *bp)
11903 {
11904 	int i;
11905 
11906 	bp->ring_cpu_mask = kzalloc_objs(*bp->ring_cpu_mask, bp->max_irqs);
11907 	if (!bp->ring_cpu_mask)
11908 		return -ENOMEM;
11909 
11910 	bp->ring_affinity_set = bitmap_zalloc(bp->max_irqs, GFP_KERNEL);
11911 	if (!bp->ring_affinity_set)
11912 		return -ENOMEM;
11913 
11914 	for (i = 0; i < bp->max_irqs; i++)
11915 		if (!zalloc_cpumask_var(&bp->ring_cpu_mask[i], GFP_KERNEL))
11916 			return -ENOMEM;
11917 
11918 	return 0;
11919 }
11920 
11921 static void bnxt_free_ring_cpu_masks(struct bnxt *bp)
11922 {
11923 	int i;
11924 
11925 	if (!bp->ring_cpu_mask)
11926 		return;
11927 
11928 	for (i = 0; i < bp->max_irqs; i++)
11929 		free_cpumask_var(bp->ring_cpu_mask[i]);
11930 
11931 	bitmap_free(bp->ring_affinity_set);
11932 	bp->ring_affinity_set = NULL;
11933 	kfree(bp->ring_cpu_mask);
11934 	bp->ring_cpu_mask = NULL;
11935 }
11936 
11937 static void bnxt_free_irq(struct bnxt *bp)
11938 {
11939 	struct bnxt_irq *irq;
11940 	int i;
11941 
11942 	if (!bp->irq_tbl || !bp->bnapi)
11943 		return;
11944 
11945 	for (i = 0; i < bp->cp_nr_rings; i++) {
11946 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11947 
11948 		irq = &bp->irq_tbl[map_idx];
11949 		if (irq->requested) {
11950 			bnxt_release_irq_notifier(irq);
11951 			irq_update_affinity_hint(irq->vector, NULL);
11952 			free_irq(irq->vector, bp->bnapi[i]);
11953 		}
11954 
11955 		irq->requested = 0;
11956 		irq->tag = 0;
11957 		irq->new_tag = 0;
11958 	}
11959 
11960 	/* Disable TPH support */
11961 	pcie_disable_tph(bp->pdev);
11962 	bp->tph_mode = 0;
11963 
11964 #ifdef CONFIG_RFS_ACCEL
11965 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11966 	bp->dev->rx_cpu_rmap = NULL;
11967 #endif
11968 }
11969 
11970 static int bnxt_request_irq(struct bnxt *bp)
11971 {
11972 	const int numa_node = dev_to_node(&bp->pdev->dev);
11973 	struct cpu_rmap *rmap = NULL;
11974 	int i, j, rc = 0;
11975 	unsigned long flags = 0;
11976 
11977 	rc = bnxt_setup_int_mode(bp);
11978 	if (rc) {
11979 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11980 			   rc);
11981 		return rc;
11982 	}
11983 #ifdef CONFIG_RFS_ACCEL
11984 	rmap = bp->dev->rx_cpu_rmap;
11985 #endif
11986 
11987 	/* Enable TPH support as part of IRQ request */
11988 	if (BNXT_SUPPORTS_QUEUE_API(bp)) {
11989 		rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11990 		if (!rc)
11991 			bp->tph_mode = PCI_TPH_ST_IV_MODE;
11992 	}
11993 
11994 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11995 		struct cpumask *cpu_mask = bp->ring_cpu_mask[i];
11996 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11997 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11998 		u16 tag;
11999 
12000 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
12001 		    rmap && bp->bnapi[i]->rx_ring) {
12002 			rc = irq_cpu_rmap_add(rmap, irq->vector);
12003 			if (rc)
12004 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
12005 					    j);
12006 			j++;
12007 		}
12008 
12009 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
12010 				 bp->bnapi[i]);
12011 		if (rc)
12012 			break;
12013 
12014 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
12015 		irq->requested = 1;
12016 		irq->msix_nr = map_idx;
12017 		irq->ring_nr = i;
12018 
12019 		/* Reuse the mask recorded before the IRQs were freed. Nothing
12020 		 * was recorded yet on the very first request, and the mask
12021 		 * may have gone stale if the CPUs went offline in between.
12022 		 */
12023 		if (!test_bit(i, bp->ring_affinity_set) ||
12024 		    !cpumask_intersects(cpu_mask, cpu_online_mask)) {
12025 			clear_bit(i, bp->ring_affinity_set);
12026 			cpumask_clear(cpu_mask);
12027 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
12028 					cpu_mask);
12029 		}
12030 
12031 		/* Init ST table entry if we can get the mapping */
12032 		if (!pcie_tph_get_cpu_st(bp->pdev, TPH_MEM_TYPE_VM,
12033 					 cpumask_first(cpu_mask), &tag)) {
12034 			pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag);
12035 			irq->tag = tag;
12036 			irq->new_tag = tag;
12037 		}
12038 
12039 		bnxt_register_irq_notifier(bp, irq);
12040 
12041 		/* Only put the IRQ back where it was configured to be, our own
12042 		 * placement is just a hint, the core spreads within
12043 		 * irq_default_affinity which we know nothing about.
12044 		 * Set after installing the notifier, if we race with the user
12045 		 * it's better to overwrite than miss the notification.
12046 		 */
12047 		if (test_bit(i, bp->ring_affinity_set))
12048 			rc = irq_set_affinity_and_hint(irq->vector, cpu_mask);
12049 		else
12050 			rc = irq_update_affinity_hint(irq->vector, cpu_mask);
12051 		if (rc) {
12052 			netdev_warn(bp->dev,
12053 				    "Setting IRQ affinity failed, IRQ = %d\n",
12054 				    irq->vector);
12055 			break;
12056 		}
12057 	}
12058 	return rc;
12059 }
12060 
12061 static void bnxt_del_napi(struct bnxt *bp)
12062 {
12063 	int i;
12064 
12065 	if (!bp->bnapi)
12066 		return;
12067 
12068 	for (i = 0; i < bp->rx_nr_rings; i++)
12069 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
12070 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
12071 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
12072 
12073 	for (i = 0; i < bp->cp_nr_rings; i++) {
12074 		struct bnxt_napi *bnapi = bp->bnapi[i];
12075 
12076 		__netif_napi_del_locked(&bnapi->napi);
12077 	}
12078 	/* We called __netif_napi_del_locked(), we need
12079 	 * to respect an RCU grace period before freeing napi structures.
12080 	 */
12081 	synchronize_net();
12082 }
12083 
12084 static void bnxt_init_napi(struct bnxt *bp)
12085 {
12086 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
12087 	unsigned int cp_nr_rings = bp->cp_nr_rings;
12088 	struct bnxt_napi *bnapi;
12089 	int i;
12090 
12091 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
12092 		poll_fn = bnxt_poll_p5;
12093 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
12094 		cp_nr_rings--;
12095 
12096 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12097 
12098 	for (i = 0; i < cp_nr_rings; i++) {
12099 		bnapi = bp->bnapi[i];
12100 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
12101 					     bnapi->index);
12102 	}
12103 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
12104 		bnapi = bp->bnapi[cp_nr_rings];
12105 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
12106 	}
12107 }
12108 
12109 static void bnxt_disable_napi(struct bnxt *bp)
12110 {
12111 	int i;
12112 
12113 	if (!bp->bnapi ||
12114 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
12115 		return;
12116 
12117 	for (i = 0; i < bp->cp_nr_rings; i++) {
12118 		struct bnxt_napi *bnapi = bp->bnapi[i];
12119 		struct bnxt_cp_ring_info *cpr;
12120 
12121 		cpr = &bnapi->cp_ring;
12122 		if (bnapi->tx_fault)
12123 			cpr->sw_stats->tx.tx_resets++;
12124 		if (bnapi->in_reset)
12125 			cpr->sw_stats->rx.rx_resets++;
12126 		napi_disable_locked(&bnapi->napi);
12127 	}
12128 }
12129 
12130 static void bnxt_enable_napi(struct bnxt *bp)
12131 {
12132 	int i;
12133 
12134 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
12135 	for (i = 0; i < bp->cp_nr_rings; i++) {
12136 		struct bnxt_napi *bnapi = bp->bnapi[i];
12137 		struct bnxt_cp_ring_info *cpr;
12138 
12139 		bnapi->tx_fault = 0;
12140 
12141 		cpr = &bnapi->cp_ring;
12142 		bnapi->in_reset = false;
12143 
12144 		if (bnapi->rx_ring) {
12145 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
12146 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
12147 		}
12148 		napi_enable_locked(&bnapi->napi);
12149 	}
12150 }
12151 
12152 void bnxt_tx_disable(struct bnxt *bp)
12153 {
12154 	int i;
12155 	struct bnxt_tx_ring_info *txr;
12156 
12157 	if (bp->tx_ring) {
12158 		for (i = 0; i < bp->tx_nr_rings; i++) {
12159 			txr = &bp->tx_ring[i];
12160 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
12161 		}
12162 	}
12163 	/* Make sure napi polls see @dev_state change */
12164 	synchronize_net();
12165 	/* Drop carrier first to prevent TX timeout */
12166 	netif_carrier_off(bp->dev);
12167 	/* Stop all TX queues */
12168 	netif_tx_disable(bp->dev);
12169 }
12170 
12171 void bnxt_tx_enable(struct bnxt *bp)
12172 {
12173 	int i;
12174 	struct bnxt_tx_ring_info *txr;
12175 
12176 	for (i = 0; i < bp->tx_nr_rings; i++) {
12177 		txr = &bp->tx_ring[i];
12178 		WRITE_ONCE(txr->dev_state, 0);
12179 	}
12180 	/* Make sure napi polls see @dev_state change */
12181 	synchronize_net();
12182 	netif_tx_wake_all_queues(bp->dev);
12183 	if (BNXT_LINK_IS_UP(bp))
12184 		netif_carrier_on(bp->dev);
12185 }
12186 
12187 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
12188 {
12189 	u8 active_fec = link_info->active_fec_sig_mode &
12190 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
12191 
12192 	switch (active_fec) {
12193 	default:
12194 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
12195 		return "None";
12196 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
12197 		return "Clause 74 BaseR";
12198 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
12199 		return "Clause 91 RS(528,514)";
12200 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
12201 		return "Clause 91 RS544_1XN";
12202 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
12203 		return "Clause 91 RS(544,514)";
12204 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
12205 		return "Clause 91 RS272_1XN";
12206 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
12207 		return "Clause 91 RS(272,257)";
12208 	}
12209 }
12210 
12211 static char *bnxt_link_down_reason(struct bnxt_link_info *link_info)
12212 {
12213 	u8 reason = link_info->link_down_reason;
12214 
12215 	/* Multiple bits can be set, we report 1 bit only in order of
12216 	 * priority.
12217 	 */
12218 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_RF)
12219 		return "(Remote fault)";
12220 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_OTP_SPEED_VIOLATION)
12221 		return "(OTP Speed limit violation)";
12222 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_CABLE_REMOVED)
12223 		return "(Cable removed)";
12224 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_MODULE_FAULT)
12225 		return "(Module fault)";
12226 	if (reason & PORT_PHY_QCFG_RESP_LINK_DOWN_REASON_BMC_REQUEST)
12227 		return "(BMC request down)";
12228 	return "";
12229 }
12230 
12231 void bnxt_report_link(struct bnxt *bp)
12232 {
12233 	if (BNXT_LINK_IS_UP(bp)) {
12234 		const char *signal = "";
12235 		const char *flow_ctrl;
12236 		const char *duplex;
12237 		u32 speed;
12238 		u16 fec;
12239 
12240 		netif_carrier_on(bp->dev);
12241 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
12242 		if (speed == SPEED_UNKNOWN) {
12243 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
12244 			return;
12245 		}
12246 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
12247 			duplex = "full";
12248 		else
12249 			duplex = "half";
12250 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
12251 			flow_ctrl = "ON - receive & transmit";
12252 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
12253 			flow_ctrl = "ON - transmit";
12254 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
12255 			flow_ctrl = "ON - receive";
12256 		else
12257 			flow_ctrl = "none";
12258 		if (bp->link_info.phy_qcfg_resp.option_flags &
12259 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
12260 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
12261 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
12262 			switch (sig_mode) {
12263 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
12264 				signal = "(NRZ) ";
12265 				break;
12266 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
12267 				signal = "(PAM4 56Gbps) ";
12268 				break;
12269 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
12270 				signal = "(PAM4 112Gbps) ";
12271 				break;
12272 			default:
12273 				break;
12274 			}
12275 		}
12276 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
12277 			    speed, signal, duplex, flow_ctrl);
12278 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
12279 			netdev_info(bp->dev, "EEE is %s\n",
12280 				    bp->eee.eee_active ? "active" :
12281 							 "not active");
12282 		fec = bp->link_info.fec_cfg;
12283 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
12284 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
12285 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
12286 				    bnxt_report_fec(&bp->link_info));
12287 	} else {
12288 		char *str = bnxt_link_down_reason(&bp->link_info);
12289 
12290 		netif_carrier_off(bp->dev);
12291 		netdev_err(bp->dev, "NIC Link is Down %s\n", str);
12292 	}
12293 }
12294 
12295 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
12296 {
12297 	if (!resp->supported_speeds_auto_mode &&
12298 	    !resp->supported_speeds_force_mode &&
12299 	    !resp->supported_pam4_speeds_auto_mode &&
12300 	    !resp->supported_pam4_speeds_force_mode &&
12301 	    !resp->supported_speeds2_auto_mode &&
12302 	    !resp->supported_speeds2_force_mode)
12303 		return true;
12304 	return false;
12305 }
12306 
12307 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
12308 {
12309 	struct bnxt_link_info *link_info = &bp->link_info;
12310 	struct hwrm_port_phy_qcaps_output *resp;
12311 	struct hwrm_port_phy_qcaps_input *req;
12312 	int rc = 0;
12313 
12314 	if (bp->hwrm_spec_code < 0x10201)
12315 		return 0;
12316 
12317 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
12318 	if (rc)
12319 		return rc;
12320 
12321 	resp = hwrm_req_hold(bp, req);
12322 	rc = hwrm_req_send(bp, req);
12323 	if (rc)
12324 		goto hwrm_phy_qcaps_exit;
12325 
12326 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
12327 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
12328 		struct ethtool_keee *eee = &bp->eee;
12329 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
12330 
12331 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
12332 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
12333 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
12334 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
12335 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
12336 	}
12337 
12338 	if (bp->hwrm_spec_code >= 0x10a01) {
12339 		if (bnxt_phy_qcaps_no_speed(resp)) {
12340 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
12341 			netdev_warn(bp->dev, "Ethernet link disabled\n");
12342 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
12343 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
12344 			netdev_info(bp->dev, "Ethernet link enabled\n");
12345 			/* Phy re-enabled, reprobe the speeds */
12346 			link_info->support_auto_speeds = 0;
12347 			link_info->support_pam4_auto_speeds = 0;
12348 			link_info->support_auto_speeds2 = 0;
12349 		}
12350 	}
12351 	if (resp->supported_speeds_auto_mode)
12352 		link_info->support_auto_speeds =
12353 			le16_to_cpu(resp->supported_speeds_auto_mode);
12354 	if (resp->supported_pam4_speeds_auto_mode)
12355 		link_info->support_pam4_auto_speeds =
12356 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
12357 	if (resp->supported_speeds2_auto_mode)
12358 		link_info->support_auto_speeds2 =
12359 			le16_to_cpu(resp->supported_speeds2_auto_mode);
12360 
12361 	bp->port_count = resp->port_cnt;
12362 
12363 hwrm_phy_qcaps_exit:
12364 	hwrm_req_drop(bp, req);
12365 	return rc;
12366 }
12367 
12368 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12369 {
12370 	struct hwrm_port_mac_qcaps_output *resp;
12371 	struct hwrm_port_mac_qcaps_input *req;
12372 	int rc;
12373 
12374 	if (bp->hwrm_spec_code < 0x10a03)
12375 		return;
12376 
12377 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12378 	if (rc)
12379 		return;
12380 
12381 	resp = hwrm_req_hold(bp, req);
12382 	rc = hwrm_req_send_silent(bp, req);
12383 	if (!rc)
12384 		bp->mac_flags = resp->flags;
12385 	hwrm_req_drop(bp, req);
12386 }
12387 
12388 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12389 {
12390 	u16 diff = advertising ^ supported;
12391 
12392 	return ((supported | diff) != supported);
12393 }
12394 
12395 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12396 {
12397 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12398 
12399 	/* Check if any advertised speeds are no longer supported. The caller
12400 	 * holds the link_lock mutex, so we can modify link_info settings.
12401 	 */
12402 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12403 		if (bnxt_support_dropped(link_info->advertising,
12404 					 link_info->support_auto_speeds2)) {
12405 			link_info->advertising = link_info->support_auto_speeds2;
12406 			return true;
12407 		}
12408 		return false;
12409 	}
12410 	if (bnxt_support_dropped(link_info->advertising,
12411 				 link_info->support_auto_speeds)) {
12412 		link_info->advertising = link_info->support_auto_speeds;
12413 		return true;
12414 	}
12415 	if (bnxt_support_dropped(link_info->advertising_pam4,
12416 				 link_info->support_pam4_auto_speeds)) {
12417 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12418 		return true;
12419 	}
12420 	return false;
12421 }
12422 
12423 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12424 {
12425 	struct bnxt_link_info *link_info = &bp->link_info;
12426 	struct hwrm_port_phy_qcfg_output *resp;
12427 	struct hwrm_port_phy_qcfg_input *req;
12428 	u8 link_state = link_info->link_state;
12429 	bool support_changed;
12430 	int rc;
12431 
12432 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12433 	if (rc)
12434 		return rc;
12435 
12436 	resp = hwrm_req_hold(bp, req);
12437 	rc = hwrm_req_send(bp, req);
12438 	if (rc) {
12439 		hwrm_req_drop(bp, req);
12440 		if (BNXT_VF(bp) && rc == -ENODEV) {
12441 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12442 			rc = 0;
12443 		}
12444 		return rc;
12445 	}
12446 
12447 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12448 	link_info->phy_link_status = resp->link;
12449 	link_info->duplex = resp->duplex_cfg;
12450 	if (bp->hwrm_spec_code >= 0x10800)
12451 		link_info->duplex = resp->duplex_state;
12452 	link_info->pause = resp->pause;
12453 	link_info->auto_mode = resp->auto_mode;
12454 	link_info->auto_pause_setting = resp->auto_pause;
12455 	link_info->lp_pause = resp->link_partner_adv_pause;
12456 	link_info->force_pause_setting = resp->force_pause;
12457 	link_info->duplex_setting = resp->duplex_cfg;
12458 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12459 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12460 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12461 			link_info->active_lanes = resp->active_lanes;
12462 	} else {
12463 		link_info->link_speed = 0;
12464 		link_info->active_lanes = 0;
12465 	}
12466 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12467 	link_info->force_pam4_link_speed =
12468 		le16_to_cpu(resp->force_pam4_link_speed);
12469 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12470 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12471 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12472 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12473 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12474 	link_info->auto_pam4_link_speeds =
12475 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12476 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12477 	link_info->lp_auto_link_speeds =
12478 		le16_to_cpu(resp->link_partner_adv_speeds);
12479 	link_info->lp_auto_pam4_link_speeds =
12480 		resp->link_partner_pam4_adv_speeds;
12481 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12482 	link_info->phy_ver[0] = resp->phy_maj;
12483 	link_info->phy_ver[1] = resp->phy_min;
12484 	link_info->phy_ver[2] = resp->phy_bld;
12485 	link_info->media_type = resp->media_type;
12486 	link_info->phy_type = resp->phy_type;
12487 	link_info->transceiver = resp->xcvr_pkg_type;
12488 	link_info->phy_addr = resp->eee_config_phy_addr &
12489 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12490 	link_info->module_status = resp->module_status;
12491 	link_info->link_down_reason = resp->link_down_reason;
12492 
12493 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12494 		struct ethtool_keee *eee = &bp->eee;
12495 		u16 fw_speeds;
12496 
12497 		eee->eee_active = 0;
12498 		if (resp->eee_config_phy_addr &
12499 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12500 			eee->eee_active = 1;
12501 			fw_speeds = le16_to_cpu(
12502 				resp->link_partner_adv_eee_link_speed_mask);
12503 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12504 		}
12505 
12506 		/* Pull initial EEE config */
12507 		if (!chng_link_state) {
12508 			if (resp->eee_config_phy_addr &
12509 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12510 				eee->eee_enabled = 1;
12511 
12512 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12513 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12514 
12515 			if (resp->eee_config_phy_addr &
12516 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12517 				__le32 tmr;
12518 
12519 				eee->tx_lpi_enabled = 1;
12520 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12521 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12522 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12523 			}
12524 		}
12525 	}
12526 
12527 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12528 	if (bp->hwrm_spec_code >= 0x10504) {
12529 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12530 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12531 	}
12532 	/* TODO: need to add more logic to report VF link */
12533 	if (chng_link_state) {
12534 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12535 			link_info->link_state = BNXT_LINK_STATE_UP;
12536 		else
12537 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12538 		if (link_state != link_info->link_state)
12539 			bnxt_report_link(bp);
12540 	} else {
12541 		/* always link down if not require to update link state */
12542 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12543 	}
12544 	hwrm_req_drop(bp, req);
12545 
12546 	if (!BNXT_PHY_CFG_ABLE(bp))
12547 		return 0;
12548 
12549 	support_changed = bnxt_support_speed_dropped(link_info);
12550 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12551 		bnxt_hwrm_set_link_setting(bp, true, false);
12552 	return 0;
12553 }
12554 
12555 static void bnxt_get_port_module_status(struct bnxt *bp)
12556 {
12557 	struct bnxt_link_info *link_info = &bp->link_info;
12558 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12559 	u8 module_status;
12560 
12561 	if (bnxt_update_link(bp, true))
12562 		return;
12563 
12564 	module_status = link_info->module_status;
12565 	switch (module_status) {
12566 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12567 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12568 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12569 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12570 			    bp->pf.port_id);
12571 		if (bp->hwrm_spec_code >= 0x10201) {
12572 			netdev_warn(bp->dev, "Module part number %s\n",
12573 				    resp->phy_vendor_partnumber);
12574 		}
12575 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12576 			netdev_warn(bp->dev, "TX is disabled\n");
12577 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12578 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12579 	}
12580 }
12581 
12582 static void
12583 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12584 {
12585 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12586 		if (bp->hwrm_spec_code >= 0x10201)
12587 			req->auto_pause =
12588 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12589 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12590 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12591 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12592 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12593 		req->enables |=
12594 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12595 	} else {
12596 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12597 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12598 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12599 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12600 		req->enables |=
12601 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12602 		if (bp->hwrm_spec_code >= 0x10201) {
12603 			req->auto_pause = req->force_pause;
12604 			req->enables |= cpu_to_le32(
12605 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12606 		}
12607 	}
12608 }
12609 
12610 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12611 {
12612 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12613 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12614 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12615 			req->enables |=
12616 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12617 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12618 		} else if (bp->link_info.advertising) {
12619 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12620 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12621 		}
12622 		if (bp->link_info.advertising_pam4) {
12623 			req->enables |=
12624 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12625 			req->auto_link_pam4_speed_mask =
12626 				cpu_to_le16(bp->link_info.advertising_pam4);
12627 		}
12628 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12629 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12630 	} else {
12631 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12632 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12633 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12634 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12635 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12636 				   (u32)bp->link_info.req_link_speed);
12637 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12638 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12639 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12640 		} else {
12641 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12642 		}
12643 	}
12644 
12645 	/* tell chimp that the setting takes effect immediately */
12646 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12647 }
12648 
12649 int bnxt_hwrm_set_pause(struct bnxt *bp)
12650 {
12651 	struct hwrm_port_phy_cfg_input *req;
12652 	int rc;
12653 
12654 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12655 	if (rc)
12656 		return rc;
12657 
12658 	bnxt_hwrm_set_pause_common(bp, req);
12659 
12660 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12661 	    bp->link_info.force_link_chng)
12662 		bnxt_hwrm_set_link_common(bp, req);
12663 
12664 	rc = hwrm_req_send(bp, req);
12665 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12666 		/* since changing of pause setting doesn't trigger any link
12667 		 * change event, the driver needs to update the current pause
12668 		 * result upon successfully return of the phy_cfg command
12669 		 */
12670 		bp->link_info.pause =
12671 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12672 		bp->link_info.auto_pause_setting = 0;
12673 		if (!bp->link_info.force_link_chng)
12674 			bnxt_report_link(bp);
12675 	}
12676 	bp->link_info.force_link_chng = false;
12677 	return rc;
12678 }
12679 
12680 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12681 			      struct hwrm_port_phy_cfg_input *req)
12682 {
12683 	struct ethtool_keee *eee = &bp->eee;
12684 
12685 	if (eee->eee_enabled) {
12686 		u16 eee_speeds;
12687 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12688 
12689 		if (eee->tx_lpi_enabled)
12690 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12691 		else
12692 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12693 
12694 		req->flags |= cpu_to_le32(flags);
12695 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12696 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12697 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12698 	} else {
12699 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12700 	}
12701 }
12702 
12703 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12704 {
12705 	struct hwrm_port_phy_cfg_input *req;
12706 	int rc;
12707 
12708 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12709 	if (rc)
12710 		return rc;
12711 
12712 	if (set_pause)
12713 		bnxt_hwrm_set_pause_common(bp, req);
12714 
12715 	bnxt_hwrm_set_link_common(bp, req);
12716 
12717 	if (set_eee)
12718 		bnxt_hwrm_set_eee(bp, req);
12719 	return hwrm_req_send(bp, req);
12720 }
12721 
12722 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12723 {
12724 	struct hwrm_port_phy_cfg_input *req;
12725 	int rc;
12726 
12727 	if (!BNXT_SINGLE_PF(bp))
12728 		return 0;
12729 
12730 	if (pci_num_vf(bp->pdev) &&
12731 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12732 		return 0;
12733 
12734 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12735 	if (rc)
12736 		return rc;
12737 
12738 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12739 	rc = hwrm_req_send(bp, req);
12740 	if (!rc) {
12741 		mutex_lock(&bp->link_lock);
12742 		/* Device is not obliged link down in certain scenarios, even
12743 		 * when forced. Setting the state unknown is consistent with
12744 		 * driver startup and will force link state to be reported
12745 		 * during subsequent open based on PORT_PHY_QCFG.
12746 		 */
12747 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12748 		mutex_unlock(&bp->link_lock);
12749 	}
12750 	return rc;
12751 }
12752 
12753 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12754 {
12755 #ifdef CONFIG_TEE_BNXT_FW
12756 	int rc = tee_bnxt_fw_load();
12757 
12758 	if (rc)
12759 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12760 
12761 	return rc;
12762 #else
12763 	netdev_err(bp->dev, "OP-TEE not supported\n");
12764 	return -ENODEV;
12765 #endif
12766 }
12767 
12768 static int bnxt_try_recover_fw(struct bnxt *bp)
12769 {
12770 	if (bp->fw_health && bp->fw_health->status_reliable) {
12771 		int retry = 0, rc;
12772 		u32 sts;
12773 
12774 		do {
12775 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12776 			rc = bnxt_hwrm_poll(bp);
12777 			if (!BNXT_FW_IS_BOOTING(sts) &&
12778 			    !BNXT_FW_IS_RECOVERING(sts))
12779 				break;
12780 			retry++;
12781 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12782 
12783 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12784 			netdev_err(bp->dev,
12785 				   "Firmware not responding, status: 0x%x\n",
12786 				   sts);
12787 			rc = -ENODEV;
12788 		}
12789 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12790 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12791 			return bnxt_fw_reset_via_optee(bp);
12792 		}
12793 		return rc;
12794 	}
12795 
12796 	return -ENODEV;
12797 }
12798 
12799 void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12800 {
12801 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12802 
12803 	if (!BNXT_NEW_RM(bp))
12804 		return; /* no resource reservations required */
12805 
12806 	hw_resc->resv_cp_rings = 0;
12807 	hw_resc->resv_stat_ctxs = 0;
12808 	hw_resc->resv_irqs = 0;
12809 	hw_resc->resv_tx_rings = 0;
12810 	hw_resc->resv_rx_rings = 0;
12811 	hw_resc->resv_hw_ring_grps = 0;
12812 	hw_resc->resv_vnics = 0;
12813 	hw_resc->resv_rsscos_ctxs = 0;
12814 	if (!fw_reset) {
12815 		bp->tx_nr_rings = 0;
12816 		bp->rx_nr_rings = 0;
12817 	}
12818 }
12819 
12820 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12821 {
12822 	int rc;
12823 
12824 	if (!BNXT_NEW_RM(bp))
12825 		return 0; /* no resource reservations required */
12826 
12827 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12828 	if (rc)
12829 		netdev_err(bp->dev, "resc_qcaps failed\n");
12830 
12831 	bnxt_clear_reservations(bp, fw_reset);
12832 
12833 	return rc;
12834 }
12835 
12836 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12837 {
12838 	struct hwrm_func_drv_if_change_output *resp;
12839 	struct hwrm_func_drv_if_change_input *req;
12840 	bool resc_reinit = false;
12841 	bool caps_change = false;
12842 	int rc, retry = 0;
12843 	bool fw_reset;
12844 	u32 flags = 0;
12845 
12846 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12847 	bp->fw_reset_state = 0;
12848 
12849 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12850 		return 0;
12851 
12852 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12853 	if (rc)
12854 		return rc;
12855 
12856 	if (up)
12857 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12858 	resp = hwrm_req_hold(bp, req);
12859 
12860 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12861 	while (retry < BNXT_FW_IF_RETRY) {
12862 		rc = hwrm_req_send(bp, req);
12863 		if (rc != -EAGAIN)
12864 			break;
12865 
12866 		msleep(50);
12867 		retry++;
12868 	}
12869 
12870 	if (rc == -EAGAIN) {
12871 		hwrm_req_drop(bp, req);
12872 		return rc;
12873 	} else if (!rc) {
12874 		flags = le32_to_cpu(resp->flags);
12875 	} else if (up) {
12876 		rc = bnxt_try_recover_fw(bp);
12877 		fw_reset = true;
12878 	}
12879 	hwrm_req_drop(bp, req);
12880 	if (rc)
12881 		return rc;
12882 
12883 	if (!up) {
12884 		bnxt_inv_fw_health_reg(bp);
12885 		return 0;
12886 	}
12887 
12888 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12889 		resc_reinit = true;
12890 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12891 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12892 		fw_reset = true;
12893 	else
12894 		bnxt_remap_fw_health_regs(bp);
12895 
12896 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12897 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12898 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12899 		return -ENODEV;
12900 	}
12901 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12902 		caps_change = true;
12903 
12904 	if (resc_reinit || fw_reset || caps_change) {
12905 		if (fw_reset || caps_change) {
12906 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12907 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12908 				bnxt_ulp_irq_stop(bp);
12909 			bnxt_free_ctx_mem(bp, false);
12910 			bnxt_dcb_free(bp);
12911 			rc = bnxt_fw_init_one(bp);
12912 			if (rc) {
12913 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12914 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12915 				return rc;
12916 			}
12917 			/* IRQ will be initialized later in bnxt_request_irq()*/
12918 			bnxt_clear_int_mode(bp);
12919 		}
12920 		rc = bnxt_cancel_reservations(bp, fw_reset);
12921 	}
12922 	return rc;
12923 }
12924 
12925 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12926 {
12927 	struct hwrm_port_led_qcaps_output *resp;
12928 	struct hwrm_port_led_qcaps_input *req;
12929 	struct bnxt_pf_info *pf = &bp->pf;
12930 	int rc;
12931 
12932 	bp->num_leds = 0;
12933 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12934 		return 0;
12935 
12936 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12937 	if (rc)
12938 		return rc;
12939 
12940 	req->port_id = cpu_to_le16(pf->port_id);
12941 	resp = hwrm_req_hold(bp, req);
12942 	rc = hwrm_req_send(bp, req);
12943 	if (rc) {
12944 		hwrm_req_drop(bp, req);
12945 		return rc;
12946 	}
12947 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12948 		int i;
12949 
12950 		bp->num_leds = resp->num_leds;
12951 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12952 						 bp->num_leds);
12953 		for (i = 0; i < bp->num_leds; i++) {
12954 			struct bnxt_led_info *led = &bp->leds[i];
12955 			__le16 caps = led->led_state_caps;
12956 
12957 			if (!led->led_group_id ||
12958 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12959 				bp->num_leds = 0;
12960 				break;
12961 			}
12962 		}
12963 	}
12964 	hwrm_req_drop(bp, req);
12965 	return 0;
12966 }
12967 
12968 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12969 {
12970 	struct hwrm_wol_filter_alloc_output *resp;
12971 	struct hwrm_wol_filter_alloc_input *req;
12972 	int rc;
12973 
12974 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12975 	if (rc)
12976 		return rc;
12977 
12978 	req->port_id = cpu_to_le16(bp->pf.port_id);
12979 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12980 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12981 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12982 
12983 	resp = hwrm_req_hold(bp, req);
12984 	rc = hwrm_req_send(bp, req);
12985 	if (!rc)
12986 		bp->wol_filter_id = resp->wol_filter_id;
12987 	hwrm_req_drop(bp, req);
12988 	return rc;
12989 }
12990 
12991 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12992 {
12993 	struct hwrm_wol_filter_free_input *req;
12994 	int rc;
12995 
12996 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12997 	if (rc)
12998 		return rc;
12999 
13000 	req->port_id = cpu_to_le16(bp->pf.port_id);
13001 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
13002 	req->wol_filter_id = bp->wol_filter_id;
13003 
13004 	return hwrm_req_send(bp, req);
13005 }
13006 
13007 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
13008 {
13009 	struct hwrm_wol_filter_qcfg_output *resp;
13010 	struct hwrm_wol_filter_qcfg_input *req;
13011 	u16 next_handle = 0;
13012 	int rc;
13013 
13014 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
13015 	if (rc)
13016 		return rc;
13017 
13018 	req->port_id = cpu_to_le16(bp->pf.port_id);
13019 	req->handle = cpu_to_le16(handle);
13020 	resp = hwrm_req_hold(bp, req);
13021 	rc = hwrm_req_send(bp, req);
13022 	if (!rc) {
13023 		next_handle = le16_to_cpu(resp->next_handle);
13024 		if (next_handle != 0) {
13025 			if (resp->wol_type ==
13026 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
13027 				bp->wol = 1;
13028 				bp->wol_filter_id = resp->wol_filter_id;
13029 			}
13030 		}
13031 	}
13032 	hwrm_req_drop(bp, req);
13033 	return next_handle;
13034 }
13035 
13036 static void bnxt_get_wol_settings(struct bnxt *bp)
13037 {
13038 	u16 handle = 0;
13039 
13040 	bp->wol = 0;
13041 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
13042 		return;
13043 
13044 	do {
13045 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
13046 	} while (handle && handle != 0xffff);
13047 }
13048 
13049 static bool bnxt_eee_config_ok(struct bnxt *bp)
13050 {
13051 	struct ethtool_keee *eee = &bp->eee;
13052 	struct bnxt_link_info *link_info = &bp->link_info;
13053 
13054 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
13055 		return true;
13056 
13057 	if (eee->eee_enabled) {
13058 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
13059 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
13060 
13061 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
13062 
13063 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13064 			eee->eee_enabled = 0;
13065 			return false;
13066 		}
13067 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
13068 			linkmode_and(eee->advertised, advertising,
13069 				     eee->supported);
13070 			return false;
13071 		}
13072 	}
13073 	return true;
13074 }
13075 
13076 static int bnxt_update_phy_setting(struct bnxt *bp)
13077 {
13078 	int rc;
13079 	bool update_link = false;
13080 	bool update_pause = false;
13081 	bool update_eee = false;
13082 	struct bnxt_link_info *link_info = &bp->link_info;
13083 
13084 	rc = bnxt_update_link(bp, true);
13085 	if (rc) {
13086 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
13087 			   rc);
13088 		return rc;
13089 	}
13090 	if (!BNXT_SINGLE_PF(bp))
13091 		return 0;
13092 
13093 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13094 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
13095 	    link_info->req_flow_ctrl)
13096 		update_pause = true;
13097 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
13098 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
13099 		update_pause = true;
13100 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
13101 		if (BNXT_AUTO_MODE(link_info->auto_mode))
13102 			update_link = true;
13103 		if (bnxt_force_speed_updated(link_info))
13104 			update_link = true;
13105 		if (link_info->req_duplex != link_info->duplex_setting)
13106 			update_link = true;
13107 	} else {
13108 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
13109 			update_link = true;
13110 		if (bnxt_auto_speed_updated(link_info))
13111 			update_link = true;
13112 	}
13113 
13114 	/* The last close may have shutdown the link, so need to call
13115 	 * PHY_CFG to bring it back up.
13116 	 */
13117 	if (!BNXT_LINK_IS_UP(bp))
13118 		update_link = true;
13119 
13120 	if (!bnxt_eee_config_ok(bp))
13121 		update_eee = true;
13122 
13123 	if (update_link)
13124 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
13125 	else if (update_pause)
13126 		rc = bnxt_hwrm_set_pause(bp);
13127 	if (rc) {
13128 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
13129 			   rc);
13130 		return rc;
13131 	}
13132 
13133 	return rc;
13134 }
13135 
13136 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
13137 
13138 static int bnxt_reinit_after_abort(struct bnxt *bp)
13139 {
13140 	int rc;
13141 
13142 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13143 		return -EBUSY;
13144 
13145 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
13146 		return -ENODEV;
13147 
13148 	rc = bnxt_fw_init_one(bp);
13149 	if (!rc) {
13150 		bnxt_clear_int_mode(bp);
13151 		rc = bnxt_init_int_mode(bp);
13152 		if (!rc) {
13153 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13154 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
13155 		}
13156 	}
13157 	return rc;
13158 }
13159 
13160 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
13161 {
13162 	struct bnxt_ntuple_filter *ntp_fltr;
13163 	struct bnxt_l2_filter *l2_fltr;
13164 
13165 	if (list_empty(&fltr->list))
13166 		return;
13167 
13168 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
13169 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
13170 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
13171 		atomic_inc(&l2_fltr->refcnt);
13172 		ntp_fltr->l2_fltr = l2_fltr;
13173 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
13174 			bnxt_del_ntp_filter(bp, ntp_fltr);
13175 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
13176 				   fltr->sw_id);
13177 		}
13178 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
13179 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
13180 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
13181 			bnxt_del_l2_filter(bp, l2_fltr);
13182 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
13183 				   fltr->sw_id);
13184 		}
13185 	}
13186 }
13187 
13188 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
13189 {
13190 	struct bnxt_filter_base *usr_fltr, *tmp;
13191 
13192 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
13193 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
13194 }
13195 
13196 static int bnxt_set_xps_mapping(struct bnxt *bp)
13197 {
13198 	int numa_node = dev_to_node(&bp->pdev->dev);
13199 	unsigned int q_idx, map_idx, cpu, i;
13200 	const struct cpumask *cpu_mask_ptr;
13201 	int nr_cpus = num_online_cpus();
13202 	cpumask_t *q_map;
13203 	int rc = 0;
13204 
13205 	q_map = kzalloc_objs(*q_map, bp->tx_nr_rings_per_tc);
13206 	if (!q_map)
13207 		return -ENOMEM;
13208 
13209 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
13210 	 * Each TC has the same number of TX queues. The nth TX queue for each
13211 	 * TC will have the same CPU mask.
13212 	 */
13213 	for (i = 0; i < nr_cpus; i++) {
13214 		map_idx = i % bp->tx_nr_rings_per_tc;
13215 		cpu = cpumask_local_spread(i, numa_node);
13216 		cpu_mask_ptr = get_cpu_mask(cpu);
13217 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
13218 	}
13219 
13220 	/* Register CPU mask for each TX queue except the ones marked for XDP */
13221 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
13222 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
13223 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
13224 		if (rc) {
13225 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
13226 				    q_idx);
13227 			break;
13228 		}
13229 	}
13230 
13231 	kfree(q_map);
13232 
13233 	return rc;
13234 }
13235 
13236 static int bnxt_tx_nr_rings(struct bnxt *bp)
13237 {
13238 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
13239 			    bp->tx_nr_rings_per_tc;
13240 }
13241 
13242 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
13243 {
13244 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
13245 }
13246 
13247 static void bnxt_set_xdp_tx_rings(struct bnxt *bp)
13248 {
13249 	bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
13250 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
13251 }
13252 
13253 static void bnxt_adj_tx_rings(struct bnxt *bp)
13254 {
13255 	/* Make adjustments if reserved TX rings are less than requested */
13256 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
13257 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
13258 	if (bp->tx_nr_rings_xdp)
13259 		bnxt_set_xdp_tx_rings(bp);
13260 }
13261 
13262 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13263 {
13264 	int rc = 0;
13265 
13266 	netif_carrier_off(bp->dev);
13267 	if (irq_re_init) {
13268 		/* Reserve rings now if none were reserved at driver probe. */
13269 		rc = bnxt_init_dflt_ring_mode(bp);
13270 		if (rc) {
13271 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
13272 			return rc;
13273 		}
13274 	}
13275 	rc = bnxt_reserve_rings(bp, irq_re_init);
13276 	if (rc)
13277 		return rc;
13278 
13279 	bnxt_adj_tx_rings(bp);
13280 	rc = bnxt_alloc_mem(bp, irq_re_init);
13281 	if (rc) {
13282 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13283 		goto open_err_free_mem;
13284 	}
13285 
13286 	if (irq_re_init) {
13287 		bnxt_init_napi(bp);
13288 		rc = bnxt_request_irq(bp);
13289 		if (rc) {
13290 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
13291 			goto open_err_irq;
13292 		}
13293 	}
13294 
13295 	rc = bnxt_init_nic(bp, irq_re_init);
13296 	if (rc) {
13297 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13298 		goto open_err_irq;
13299 	}
13300 
13301 	bnxt_enable_napi(bp);
13302 	bnxt_debug_dev_init(bp);
13303 
13304 	if (link_re_init) {
13305 		mutex_lock(&bp->link_lock);
13306 		rc = bnxt_update_phy_setting(bp);
13307 		mutex_unlock(&bp->link_lock);
13308 		if (rc) {
13309 			netdev_warn(bp->dev, "failed to update phy settings\n");
13310 			if (BNXT_SINGLE_PF(bp)) {
13311 				bp->link_info.phy_retry = true;
13312 				bp->link_info.phy_retry_expires =
13313 					jiffies + 5 * HZ;
13314 			}
13315 		}
13316 	}
13317 
13318 	if (irq_re_init) {
13319 		udp_tunnel_nic_reset_ntf(bp->dev);
13320 		rc = bnxt_set_xps_mapping(bp);
13321 		if (rc)
13322 			netdev_warn(bp->dev, "failed to set xps mapping\n");
13323 	}
13324 
13325 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
13326 		if (!static_key_enabled(&bnxt_xdp_locking_key))
13327 			static_branch_enable(&bnxt_xdp_locking_key);
13328 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
13329 		static_branch_disable(&bnxt_xdp_locking_key);
13330 	}
13331 	set_bit(BNXT_STATE_OPEN, &bp->state);
13332 	bnxt_enable_int(bp);
13333 	/* Enable TX queues */
13334 	bnxt_tx_enable(bp);
13335 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13336 	/* Poll link status and check for SFP+ module status */
13337 	mutex_lock(&bp->link_lock);
13338 	bnxt_get_port_module_status(bp);
13339 	mutex_unlock(&bp->link_lock);
13340 
13341 	/* VF-reps may need to be re-opened after the PF is re-opened */
13342 	if (BNXT_PF(bp))
13343 		bnxt_vf_reps_open(bp);
13344 	bnxt_ptp_init_rtc(bp, true);
13345 	bnxt_ptp_cfg_tstamp_filters(bp);
13346 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13347 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
13348 	bnxt_cfg_usr_fltrs(bp);
13349 	return 0;
13350 
13351 open_err_irq:
13352 	bnxt_del_napi(bp);
13353 
13354 open_err_free_mem:
13355 	bnxt_free_skbs(bp);
13356 	bnxt_free_irq(bp);
13357 	bnxt_free_mem(bp, true);
13358 	return rc;
13359 }
13360 
13361 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13362 {
13363 	int rc = 0;
13364 
13365 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
13366 		rc = -EIO;
13367 	if (!rc)
13368 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
13369 	if (rc) {
13370 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
13371 		netif_close(bp->dev);
13372 	}
13373 	return rc;
13374 }
13375 
13376 /* netdev instance lock held, open the NIC half way by allocating all
13377  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
13378  * for offline self tests.
13379  */
13380 int bnxt_half_open_nic(struct bnxt *bp)
13381 {
13382 	int rc = 0;
13383 
13384 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13385 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13386 		rc = -ENODEV;
13387 		goto half_open_err;
13388 	}
13389 
13390 	rc = bnxt_alloc_mem(bp, true);
13391 	if (rc) {
13392 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13393 		goto half_open_err;
13394 	}
13395 	bnxt_init_napi(bp);
13396 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13397 	rc = bnxt_init_nic(bp, true);
13398 	if (rc) {
13399 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13400 		bnxt_del_napi(bp);
13401 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13402 		goto half_open_err;
13403 	}
13404 	return 0;
13405 
13406 half_open_err:
13407 	bnxt_free_skbs(bp);
13408 	bnxt_free_mem(bp, true);
13409 	netif_close(bp->dev);
13410 	return rc;
13411 }
13412 
13413 /* netdev instance lock held, this call can only be made after a previous
13414  * successful call to bnxt_half_open_nic().
13415  */
13416 void bnxt_half_close_nic(struct bnxt *bp)
13417 {
13418 	bnxt_hwrm_resource_free(bp, false, true);
13419 	bnxt_del_napi(bp);
13420 	bnxt_free_skbs(bp);
13421 	bnxt_free_mem(bp, true);
13422 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13423 }
13424 
13425 void bnxt_reenable_sriov(struct bnxt *bp)
13426 {
13427 	if (BNXT_PF(bp)) {
13428 		struct bnxt_pf_info *pf = &bp->pf;
13429 		int n = pf->active_vfs;
13430 
13431 		if (n)
13432 			bnxt_cfg_hw_sriov(bp, &n, true);
13433 	}
13434 }
13435 
13436 static int bnxt_open(struct net_device *dev)
13437 {
13438 	struct bnxt *bp = netdev_priv(dev);
13439 	int rc;
13440 
13441 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13442 		rc = bnxt_reinit_after_abort(bp);
13443 		if (rc) {
13444 			if (rc == -EBUSY)
13445 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13446 			else
13447 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13448 			return -ENODEV;
13449 		}
13450 	}
13451 
13452 	rc = bnxt_hwrm_if_change(bp, true);
13453 	if (rc)
13454 		return rc;
13455 
13456 	rc = __bnxt_open_nic(bp, true, true);
13457 	if (rc) {
13458 		bnxt_hwrm_if_change(bp, false);
13459 	} else {
13460 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13461 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13462 				bnxt_queue_sp_work(bp,
13463 						   BNXT_RESTART_ULP_SP_EVENT);
13464 		}
13465 	}
13466 
13467 	return rc;
13468 }
13469 
13470 static bool bnxt_drv_busy(struct bnxt *bp)
13471 {
13472 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13473 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13474 }
13475 
13476 static void bnxt_get_ring_stats(struct bnxt *bp,
13477 				struct rtnl_link_stats64 *stats);
13478 
13479 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13480 			     bool link_re_init)
13481 {
13482 	/* Close the VF-reps before closing PF */
13483 	if (BNXT_PF(bp))
13484 		bnxt_vf_reps_close(bp);
13485 
13486 	/* Change device state to avoid TX queue wake up's */
13487 	bnxt_tx_disable(bp);
13488 
13489 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13490 	smp_mb__after_atomic();
13491 	while (bnxt_drv_busy(bp))
13492 		msleep(20);
13493 
13494 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13495 		bnxt_clear_rss_ctxs(bp);
13496 	/* Flush rings and disable interrupts */
13497 	bnxt_shutdown_nic(bp, irq_re_init);
13498 
13499 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13500 
13501 	bnxt_debug_dev_exit(bp);
13502 	bnxt_disable_napi(bp);
13503 	timer_delete_sync(&bp->timer);
13504 	bnxt_free_skbs(bp);
13505 
13506 	/* Save ring stats before shutdown */
13507 	if (bp->bnapi && irq_re_init) {
13508 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13509 		bnxt_get_ring_drv_stats(bp, &bp->ring_drv_stats_prev);
13510 	}
13511 	if (irq_re_init) {
13512 		bnxt_free_irq(bp);
13513 		bnxt_del_napi(bp);
13514 	}
13515 	bnxt_free_mem(bp, irq_re_init);
13516 }
13517 
13518 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13519 {
13520 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13521 		/* If we get here, it means firmware reset is in progress
13522 		 * while we are trying to close.  We can safely proceed with
13523 		 * the close because we are holding netdev instance lock.
13524 		 * Some firmware messages may fail as we proceed to close.
13525 		 * We set the ABORT_ERR flag here so that the FW reset thread
13526 		 * will later abort when it gets the netdev instance lock
13527 		 * and sees the flag.
13528 		 */
13529 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13530 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13531 	}
13532 
13533 #ifdef CONFIG_BNXT_SRIOV
13534 	if (bp->sriov_cfg) {
13535 		int rc;
13536 
13537 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13538 						      !bp->sriov_cfg,
13539 						      BNXT_SRIOV_CFG_WAIT_TMO);
13540 		if (!rc)
13541 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13542 		else if (rc < 0)
13543 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13544 	}
13545 #endif
13546 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13547 }
13548 
13549 static int bnxt_close(struct net_device *dev)
13550 {
13551 	struct bnxt *bp = netdev_priv(dev);
13552 
13553 	bnxt_close_nic(bp, true, true);
13554 	bnxt_hwrm_shutdown_link(bp);
13555 	bnxt_hwrm_if_change(bp, false);
13556 	return 0;
13557 }
13558 
13559 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13560 				   u16 *val)
13561 {
13562 	struct hwrm_port_phy_mdio_read_output *resp;
13563 	struct hwrm_port_phy_mdio_read_input *req;
13564 	int rc;
13565 
13566 	if (bp->hwrm_spec_code < 0x10a00)
13567 		return -EOPNOTSUPP;
13568 
13569 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13570 	if (rc)
13571 		return rc;
13572 
13573 	req->port_id = cpu_to_le16(bp->pf.port_id);
13574 	req->phy_addr = phy_addr;
13575 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13576 	if (mdio_phy_id_is_c45(phy_addr)) {
13577 		req->cl45_mdio = 1;
13578 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13579 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13580 		req->reg_addr = cpu_to_le16(reg);
13581 	}
13582 
13583 	resp = hwrm_req_hold(bp, req);
13584 	rc = hwrm_req_send(bp, req);
13585 	if (!rc)
13586 		*val = le16_to_cpu(resp->reg_data);
13587 	hwrm_req_drop(bp, req);
13588 	return rc;
13589 }
13590 
13591 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13592 				    u16 val)
13593 {
13594 	struct hwrm_port_phy_mdio_write_input *req;
13595 	int rc;
13596 
13597 	if (bp->hwrm_spec_code < 0x10a00)
13598 		return -EOPNOTSUPP;
13599 
13600 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13601 	if (rc)
13602 		return rc;
13603 
13604 	req->port_id = cpu_to_le16(bp->pf.port_id);
13605 	req->phy_addr = phy_addr;
13606 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13607 	if (mdio_phy_id_is_c45(phy_addr)) {
13608 		req->cl45_mdio = 1;
13609 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13610 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13611 		req->reg_addr = cpu_to_le16(reg);
13612 	}
13613 	req->reg_data = cpu_to_le16(val);
13614 
13615 	return hwrm_req_send(bp, req);
13616 }
13617 
13618 /* netdev instance lock held */
13619 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13620 {
13621 	struct mii_ioctl_data *mdio = if_mii(ifr);
13622 	struct bnxt *bp = netdev_priv(dev);
13623 	int rc;
13624 
13625 	switch (cmd) {
13626 	case SIOCGMIIPHY:
13627 		mdio->phy_id = bp->link_info.phy_addr;
13628 
13629 		fallthrough;
13630 	case SIOCGMIIREG: {
13631 		u16 mii_regval = 0;
13632 
13633 		if (!netif_running(dev))
13634 			return -EAGAIN;
13635 
13636 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13637 					     &mii_regval);
13638 		mdio->val_out = mii_regval;
13639 		return rc;
13640 	}
13641 
13642 	case SIOCSMIIREG:
13643 		if (!netif_running(dev))
13644 			return -EAGAIN;
13645 
13646 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13647 						mdio->val_in);
13648 
13649 	default:
13650 		/* do nothing */
13651 		break;
13652 	}
13653 	return -EOPNOTSUPP;
13654 }
13655 
13656 static void bnxt_get_ring_stats(struct bnxt *bp,
13657 				struct rtnl_link_stats64 *stats)
13658 {
13659 	int i;
13660 
13661 	for (i = 0; i < bp->cp_nr_rings; i++) {
13662 		struct bnxt_napi *bnapi = bp->bnapi[i];
13663 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13664 		u64 *sw = cpr->stats.sw_stats;
13665 
13666 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13667 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13668 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13669 
13670 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13671 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13672 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13673 
13674 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13675 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13676 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13677 
13678 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13679 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13680 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13681 
13682 		stats->rx_missed_errors +=
13683 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13684 
13685 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13686 
13687 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13688 
13689 		stats->rx_dropped +=
13690 			cpr->sw_stats->rx.rx_netpoll_discards +
13691 			cpr->sw_stats->rx.rx_oom_discards;
13692 	}
13693 }
13694 
13695 static void bnxt_add_prev_stats(struct bnxt *bp,
13696 				struct rtnl_link_stats64 *stats)
13697 {
13698 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13699 
13700 	stats->rx_packets += prev_stats->rx_packets;
13701 	stats->tx_packets += prev_stats->tx_packets;
13702 	stats->rx_bytes += prev_stats->rx_bytes;
13703 	stats->tx_bytes += prev_stats->tx_bytes;
13704 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13705 	stats->multicast += prev_stats->multicast;
13706 	stats->rx_dropped += prev_stats->rx_dropped;
13707 	stats->tx_dropped += prev_stats->tx_dropped;
13708 }
13709 
13710 static void
13711 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13712 {
13713 	struct bnxt *bp = netdev_priv(dev);
13714 
13715 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13716 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13717 	 * we check the BNXT_STATE_OPEN flag.
13718 	 */
13719 	smp_mb__after_atomic();
13720 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13721 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13722 		*stats = bp->net_stats_prev;
13723 		return;
13724 	}
13725 
13726 	bnxt_sync_ring_stats(bp);
13727 	bnxt_get_ring_stats(bp, stats);
13728 	bnxt_add_prev_stats(bp, stats);
13729 
13730 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13731 		u64 *rx = bp->port_stats.sw_stats;
13732 		u64 *tx = bp->port_stats.sw_stats +
13733 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13734 
13735 		stats->rx_crc_errors =
13736 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13737 		stats->rx_frame_errors =
13738 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13739 		stats->rx_length_errors =
13740 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13741 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13742 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13743 		stats->rx_errors =
13744 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13745 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13746 		stats->collisions =
13747 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13748 		stats->tx_fifo_errors =
13749 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13750 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13751 	}
13752 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13753 }
13754 
13755 static void bnxt_get_one_ring_drv_stats(struct bnxt *bp,
13756 					struct bnxt_total_ring_drv_stats *stats,
13757 					struct bnxt_cp_ring_info *cpr)
13758 {
13759 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13760 	u64 *hw_stats = cpr->stats.sw_stats;
13761 
13762 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13763 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13764 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13765 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13766 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13767 	stats->rx_total_ring_discards +=
13768 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13769 	stats->rx_total_hw_gro_packets += sw_stats->rx.rx_hw_gro_packets;
13770 	stats->rx_total_hw_gro_wire_packets += sw_stats->rx.rx_hw_gro_wire_packets;
13771 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13772 	stats->tx_total_ring_discards +=
13773 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13774 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13775 }
13776 
13777 void bnxt_get_ring_drv_stats(struct bnxt *bp,
13778 			     struct bnxt_total_ring_drv_stats *stats)
13779 {
13780 	int i;
13781 
13782 	for (i = 0; i < bp->cp_nr_rings; i++)
13783 		bnxt_get_one_ring_drv_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13784 }
13785 
13786 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask,
13787 				 const struct netdev_hw_addr_list *mc)
13788 {
13789 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13790 	struct netdev_hw_addr *ha;
13791 	u8 *haddr;
13792 	int mc_count = 0;
13793 	bool update = false;
13794 	int off = 0;
13795 
13796 	netdev_hw_addr_list_for_each(ha, mc) {
13797 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13798 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13799 			vnic->mc_list_count = 0;
13800 			return false;
13801 		}
13802 		haddr = ha->addr;
13803 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13804 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13805 			update = true;
13806 		}
13807 		off += ETH_ALEN;
13808 		mc_count++;
13809 	}
13810 	if (mc_count)
13811 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13812 
13813 	if (mc_count != vnic->mc_list_count) {
13814 		vnic->mc_list_count = mc_count;
13815 		update = true;
13816 	}
13817 	return update;
13818 }
13819 
13820 static bool bnxt_uc_list_updated(struct bnxt *bp,
13821 				 const struct netdev_hw_addr_list *uc)
13822 {
13823 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13824 	struct netdev_hw_addr *ha;
13825 	int off = 0;
13826 
13827 	if (netdev_hw_addr_list_count(uc) != (vnic->uc_filter_count - 1))
13828 		return true;
13829 
13830 	netdev_hw_addr_list_for_each(ha, uc) {
13831 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13832 			return true;
13833 
13834 		off += ETH_ALEN;
13835 	}
13836 	return false;
13837 }
13838 
13839 static int bnxt_set_rx_mode(struct net_device *dev,
13840 			    struct netdev_hw_addr_list *uc,
13841 			    struct netdev_hw_addr_list *mc)
13842 {
13843 	struct bnxt *bp = netdev_priv(dev);
13844 	struct bnxt_vnic_info *vnic;
13845 	bool mc_update = false;
13846 	bool uc_update;
13847 	u32 mask;
13848 
13849 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13850 		return 0;
13851 
13852 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13853 	mask = vnic->rx_mask;
13854 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13855 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13856 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13857 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13858 
13859 	if (dev->flags & IFF_PROMISC)
13860 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13861 
13862 	uc_update = bnxt_uc_list_updated(bp, uc);
13863 
13864 	if (dev->flags & IFF_BROADCAST)
13865 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13866 	if (dev->flags & IFF_ALLMULTI) {
13867 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13868 		vnic->mc_list_count = 0;
13869 	} else if (dev->flags & IFF_MULTICAST) {
13870 		mc_update = bnxt_mc_list_updated(bp, &mask, mc);
13871 	}
13872 
13873 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13874 		vnic->rx_mask = mask;
13875 
13876 		return bnxt_cfg_rx_mode(bp, uc, uc_update);
13877 	}
13878 
13879 	return 0;
13880 }
13881 
13882 static int bnxt_cfg_rx_mode(struct bnxt *bp, struct netdev_hw_addr_list *uc,
13883 			    bool uc_update)
13884 {
13885 	struct net_device *dev = bp->dev;
13886 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13887 	struct netdev_hw_addr *ha;
13888 	int i, off = 0, rc;
13889 
13890 	if (!uc_update)
13891 		goto skip_uc;
13892 
13893 	for (i = 1; i < vnic->uc_filter_count; i++) {
13894 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13895 
13896 		bnxt_hwrm_l2_filter_free(bp, fltr);
13897 		bnxt_del_l2_filter(bp, fltr);
13898 	}
13899 
13900 	vnic->uc_filter_count = 1;
13901 
13902 	netif_addr_lock_bh(dev);
13903 	if (netdev_hw_addr_list_count(uc) > (BNXT_MAX_UC_ADDRS - 1)) {
13904 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13905 	} else {
13906 		netdev_hw_addr_list_for_each(ha, uc) {
13907 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13908 			off += ETH_ALEN;
13909 			vnic->uc_filter_count++;
13910 		}
13911 	}
13912 	netif_addr_unlock_bh(dev);
13913 
13914 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13915 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13916 		if (rc) {
13917 			if (BNXT_VF(bp) && rc == -ENODEV) {
13918 				netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13919 				rc = -EAGAIN;
13920 			} else if (rc == -EAGAIN) {
13921 				netdev_warn(bp->dev, "FW busy while setting vnic filter, will retry\n");
13922 			} else {
13923 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13924 			}
13925 			vnic->uc_filter_count = i;
13926 			return rc;
13927 		}
13928 	}
13929 
13930 skip_uc:
13931 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13932 	    !bnxt_promisc_ok(bp))
13933 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13934 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13935 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13936 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13937 			    rc);
13938 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13939 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13940 		vnic->mc_list_count = 0;
13941 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13942 	}
13943 	if (rc)
13944 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13945 			   rc);
13946 
13947 	return rc;
13948 }
13949 
13950 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13951 {
13952 #ifdef CONFIG_BNXT_SRIOV
13953 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13954 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13955 
13956 		/* No minimum rings were provisioned by the PF.  Don't
13957 		 * reserve rings by default when device is down.
13958 		 */
13959 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13960 			return true;
13961 
13962 		if (!netif_running(bp->dev))
13963 			return false;
13964 	}
13965 #endif
13966 	return true;
13967 }
13968 
13969 /* If the chip and firmware supports RFS */
13970 static bool bnxt_rfs_supported(struct bnxt *bp)
13971 {
13972 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13973 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13974 			return true;
13975 		return false;
13976 	}
13977 	/* 212 firmware is broken for aRFS */
13978 	if (BNXT_FW_MAJ(bp) == 212)
13979 		return false;
13980 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13981 		return true;
13982 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13983 		return true;
13984 	return false;
13985 }
13986 
13987 /* If runtime conditions support RFS */
13988 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13989 {
13990 	struct bnxt_hw_rings hwr = {0};
13991 	int max_vnics, max_rss_ctxs;
13992 
13993 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13994 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13995 		return bnxt_rfs_supported(bp);
13996 
13997 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13998 		return false;
13999 
14000 	hwr.grp = bp->rx_nr_rings;
14001 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
14002 	if (new_rss_ctx)
14003 		hwr.vnic++;
14004 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14005 	max_vnics = bnxt_get_max_func_vnics(bp);
14006 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
14007 
14008 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
14009 		if (bp->rx_nr_rings > 1)
14010 			netdev_warn(bp->dev,
14011 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
14012 				    min(max_rss_ctxs - 1, max_vnics - 1));
14013 		return false;
14014 	}
14015 
14016 	if (!BNXT_NEW_RM(bp))
14017 		return true;
14018 
14019 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
14020 	 * issue that will mess up the default VNIC if we reduce the
14021 	 * reservations.
14022 	 */
14023 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
14024 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
14025 		return true;
14026 
14027 	bnxt_hwrm_reserve_rings(bp, &hwr);
14028 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
14029 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
14030 		return true;
14031 
14032 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
14033 	hwr.vnic = 1;
14034 	hwr.rss_ctx = 0;
14035 	bnxt_hwrm_reserve_rings(bp, &hwr);
14036 	return false;
14037 }
14038 
14039 static netdev_features_t bnxt_fix_features(struct net_device *dev,
14040 					   netdev_features_t features)
14041 {
14042 	struct bnxt *bp = netdev_priv(dev);
14043 	netdev_features_t vlan_features;
14044 
14045 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
14046 		features &= ~NETIF_F_NTUPLE;
14047 
14048 	if ((features & NETIF_F_GSO_UDP_L4) &&
14049 	    !(bp->flags & BNXT_FLAG_UDP_GSO_CAP) &&
14050 	    bp->tx_ring_size < 2 * BNXT_SW_USO_MAX_DESCS)
14051 		features &= ~NETIF_F_GSO_UDP_L4;
14052 
14053 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
14054 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
14055 
14056 	if (!(features & NETIF_F_GRO))
14057 		features &= ~NETIF_F_GRO_HW;
14058 
14059 	if (features & NETIF_F_GRO_HW)
14060 		features &= ~NETIF_F_LRO;
14061 
14062 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
14063 	 * turned on or off together.
14064 	 */
14065 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
14066 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
14067 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14068 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14069 		else if (vlan_features)
14070 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
14071 	}
14072 #ifdef CONFIG_BNXT_SRIOV
14073 	if (BNXT_VF(bp) && bp->vf.vlan)
14074 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
14075 #endif
14076 	return features;
14077 }
14078 
14079 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
14080 				bool link_re_init, u32 flags, bool update_tpa)
14081 {
14082 	bnxt_close_nic(bp, irq_re_init, link_re_init);
14083 	bp->flags = flags;
14084 	if (update_tpa)
14085 		bnxt_set_ring_params(bp);
14086 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
14087 }
14088 
14089 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
14090 {
14091 	bool update_tpa = false, update_ntuple = false;
14092 	struct bnxt *bp = netdev_priv(dev);
14093 	u32 flags = bp->flags;
14094 	u32 changes;
14095 	int rc = 0;
14096 	bool re_init = false;
14097 
14098 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
14099 				   bnxt_min_tx_desc_cnt(bp, features));
14100 
14101 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
14102 	if (features & NETIF_F_GRO_HW)
14103 		flags |= BNXT_FLAG_GRO;
14104 	else if (features & NETIF_F_LRO)
14105 		flags |= BNXT_FLAG_LRO;
14106 
14107 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
14108 		flags &= ~BNXT_FLAG_TPA;
14109 
14110 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
14111 		flags |= BNXT_FLAG_STRIP_VLAN;
14112 
14113 	if (features & NETIF_F_NTUPLE)
14114 		flags |= BNXT_FLAG_RFS;
14115 	else
14116 		bnxt_clear_usr_fltrs(bp, true);
14117 
14118 	changes = flags ^ bp->flags;
14119 	if (changes & BNXT_FLAG_TPA) {
14120 		update_tpa = true;
14121 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
14122 		    (flags & BNXT_FLAG_TPA) == 0 ||
14123 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14124 			re_init = true;
14125 	}
14126 
14127 	if (changes & ~BNXT_FLAG_TPA)
14128 		re_init = true;
14129 
14130 	if (changes & BNXT_FLAG_RFS)
14131 		update_ntuple = true;
14132 
14133 	if (flags != bp->flags) {
14134 		u32 old_flags = bp->flags;
14135 
14136 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14137 			bp->flags = flags;
14138 			if (update_tpa)
14139 				bnxt_set_ring_params(bp);
14140 			return rc;
14141 		}
14142 
14143 		if (update_ntuple)
14144 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
14145 
14146 		if (re_init)
14147 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
14148 
14149 		if (update_tpa) {
14150 			bp->flags = flags;
14151 			rc = bnxt_set_tpa(bp,
14152 					  (flags & BNXT_FLAG_TPA) ?
14153 					  true : false);
14154 			if (rc)
14155 				bp->flags = old_flags;
14156 		}
14157 	}
14158 	return rc;
14159 }
14160 
14161 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
14162 			      u8 **nextp)
14163 {
14164 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
14165 	int hdr_count = 0;
14166 	u8 *nexthdr;
14167 	int start;
14168 
14169 	/* Check that there are at most 2 IPv6 extension headers, no
14170 	 * fragment header, and each is <= 64 bytes.
14171 	 */
14172 	start = nw_off + sizeof(*ip6h);
14173 	nexthdr = &ip6h->nexthdr;
14174 	while (ipv6_ext_hdr(*nexthdr)) {
14175 		struct ipv6_opt_hdr *hp;
14176 		int hdrlen;
14177 
14178 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
14179 		    *nexthdr == NEXTHDR_FRAGMENT)
14180 			return false;
14181 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
14182 					  skb_headlen(skb), NULL);
14183 		if (!hp)
14184 			return false;
14185 		if (*nexthdr == NEXTHDR_AUTH)
14186 			hdrlen = ipv6_authlen(hp);
14187 		else
14188 			hdrlen = ipv6_optlen(hp);
14189 
14190 		if (hdrlen > 64)
14191 			return false;
14192 
14193 		hdr_count++;
14194 		nexthdr = &hp->nexthdr;
14195 		start += hdrlen;
14196 	}
14197 	if (nextp) {
14198 		/* Caller will check inner protocol */
14199 		if (skb->encapsulation) {
14200 			*nextp = nexthdr;
14201 			return true;
14202 		}
14203 		*nextp = NULL;
14204 	}
14205 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
14206 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
14207 }
14208 
14209 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
14210 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
14211 {
14212 	struct udphdr *uh = udp_hdr(skb);
14213 	__be16 udp_port = uh->dest;
14214 
14215 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
14216 	    udp_port != bp->vxlan_gpe_port)
14217 		return false;
14218 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
14219 		struct ethhdr *eh = inner_eth_hdr(skb);
14220 
14221 		switch (eh->h_proto) {
14222 		case htons(ETH_P_IP):
14223 			return true;
14224 		case htons(ETH_P_IPV6):
14225 			return bnxt_exthdr_check(bp, skb,
14226 						 skb_inner_network_offset(skb),
14227 						 NULL);
14228 		}
14229 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
14230 		return true;
14231 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
14232 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14233 					 NULL);
14234 	}
14235 	return false;
14236 }
14237 
14238 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
14239 {
14240 	switch (l4_proto) {
14241 	case IPPROTO_UDP:
14242 		return bnxt_udp_tunl_check(bp, skb);
14243 	case IPPROTO_IPIP:
14244 		return true;
14245 	case IPPROTO_GRE: {
14246 		switch (skb->inner_protocol) {
14247 		default:
14248 			return false;
14249 		case htons(ETH_P_IP):
14250 			return true;
14251 		case htons(ETH_P_IPV6):
14252 			fallthrough;
14253 		}
14254 	}
14255 	case IPPROTO_IPV6:
14256 		/* Check ext headers of inner ipv6 */
14257 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
14258 					 NULL);
14259 	}
14260 	return false;
14261 }
14262 
14263 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
14264 					     struct net_device *dev,
14265 					     netdev_features_t features)
14266 {
14267 	struct bnxt *bp = netdev_priv(dev);
14268 	u8 *l4_proto;
14269 
14270 	features = vlan_features_check(skb, features);
14271 	switch (vlan_get_protocol(skb)) {
14272 	case htons(ETH_P_IP):
14273 		if (!skb->encapsulation)
14274 			return features;
14275 		l4_proto = &ip_hdr(skb)->protocol;
14276 		if (bnxt_tunl_check(bp, skb, *l4_proto))
14277 			return features;
14278 		break;
14279 	case htons(ETH_P_IPV6):
14280 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
14281 				       &l4_proto))
14282 			break;
14283 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
14284 			return features;
14285 		break;
14286 	}
14287 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
14288 }
14289 
14290 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
14291 			 u32 *reg_buf)
14292 {
14293 	struct hwrm_dbg_read_direct_output *resp;
14294 	struct hwrm_dbg_read_direct_input *req;
14295 	__le32 *dbg_reg_buf;
14296 	dma_addr_t mapping;
14297 	int rc, i;
14298 
14299 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
14300 	if (rc)
14301 		return rc;
14302 
14303 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
14304 					 &mapping);
14305 	if (!dbg_reg_buf) {
14306 		rc = -ENOMEM;
14307 		goto dbg_rd_reg_exit;
14308 	}
14309 
14310 	req->host_dest_addr = cpu_to_le64(mapping);
14311 
14312 	resp = hwrm_req_hold(bp, req);
14313 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
14314 	req->read_len32 = cpu_to_le32(num_words);
14315 
14316 	rc = hwrm_req_send(bp, req);
14317 	if (rc || resp->error_code) {
14318 		rc = -EIO;
14319 		goto dbg_rd_reg_exit;
14320 	}
14321 	for (i = 0; i < num_words; i++)
14322 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
14323 
14324 dbg_rd_reg_exit:
14325 	hwrm_req_drop(bp, req);
14326 	return rc;
14327 }
14328 
14329 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
14330 				       u32 ring_id, u32 *prod, u32 *cons)
14331 {
14332 	struct hwrm_dbg_ring_info_get_output *resp;
14333 	struct hwrm_dbg_ring_info_get_input *req;
14334 	int rc;
14335 
14336 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
14337 	if (rc)
14338 		return rc;
14339 
14340 	req->ring_type = ring_type;
14341 	req->fw_ring_id = cpu_to_le32(ring_id);
14342 	resp = hwrm_req_hold(bp, req);
14343 	rc = hwrm_req_send(bp, req);
14344 	if (!rc) {
14345 		*prod = le32_to_cpu(resp->producer_index);
14346 		*cons = le32_to_cpu(resp->consumer_index);
14347 	}
14348 	hwrm_req_drop(bp, req);
14349 	return rc;
14350 }
14351 
14352 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
14353 {
14354 	struct bnxt_tx_ring_info *txr;
14355 	int i = bnapi->index, j;
14356 
14357 	bnxt_for_each_napi_tx(j, bnapi, txr)
14358 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
14359 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
14360 			    txr->tx_cons);
14361 }
14362 
14363 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14364 {
14365 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14366 	int i = bnapi->index;
14367 
14368 	if (!rxr)
14369 		return;
14370 
14371 	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",
14372 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14373 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14374 		    rxr->rx_sw_agg_prod);
14375 }
14376 
14377 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14378 {
14379 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring, *cpr2;
14380 	int i = bnapi->index, j;
14381 
14382 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14383 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14384 	for (j = 0; j < cpr->cp_ring_count; j++) {
14385 		cpr2 = &cpr->cp_ring_arr[j];
14386 		if (!cpr2->bnapi)
14387 			continue;
14388 		netdev_info(bnapi->bp->dev, "[%d.%d]: cp{fw_ring: %d raw_cons: %x}\n",
14389 			    i, j, cpr2->cp_ring_struct.fw_ring_id,
14390 			    cpr2->cp_raw_cons);
14391 	}
14392 }
14393 
14394 static void bnxt_dbg_dump_states(struct bnxt *bp)
14395 {
14396 	int i;
14397 	struct bnxt_napi *bnapi;
14398 
14399 	for (i = 0; i < bp->cp_nr_rings; i++) {
14400 		bnapi = bp->bnapi[i];
14401 		if (netif_msg_drv(bp)) {
14402 			bnxt_dump_tx_sw_state(bnapi);
14403 			bnxt_dump_rx_sw_state(bnapi);
14404 			bnxt_dump_cp_sw_state(bnapi);
14405 		}
14406 	}
14407 }
14408 
14409 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14410 {
14411 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14412 	struct hwrm_ring_reset_input *req;
14413 	struct bnxt_napi *bnapi = rxr->bnapi;
14414 	struct bnxt_cp_ring_info *cpr;
14415 	u16 cp_ring_id;
14416 	int rc;
14417 
14418 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14419 	if (rc)
14420 		return rc;
14421 
14422 	cpr = &bnapi->cp_ring;
14423 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14424 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14425 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14426 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14427 	return hwrm_req_send_silent(bp, req);
14428 }
14429 
14430 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14431 {
14432 	if (!silent)
14433 		bnxt_dbg_dump_states(bp);
14434 	if (netif_running(bp->dev)) {
14435 		bnxt_close_nic(bp, !silent, false);
14436 		bnxt_open_nic(bp, !silent, false);
14437 	}
14438 }
14439 
14440 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14441 {
14442 	struct bnxt *bp = netdev_priv(dev);
14443 
14444 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14445 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14446 }
14447 
14448 static void bnxt_fw_health_check(struct bnxt *bp)
14449 {
14450 	struct bnxt_fw_health *fw_health = bp->fw_health;
14451 	struct pci_dev *pdev = bp->pdev;
14452 	u32 val;
14453 
14454 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14455 		return;
14456 
14457 	/* Make sure it is enabled before checking the tmr_counter. */
14458 	smp_rmb();
14459 	if (fw_health->tmr_counter) {
14460 		fw_health->tmr_counter--;
14461 		return;
14462 	}
14463 
14464 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14465 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14466 		fw_health->arrests++;
14467 		goto fw_reset;
14468 	}
14469 
14470 	fw_health->last_fw_heartbeat = val;
14471 
14472 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14473 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14474 		fw_health->discoveries++;
14475 		goto fw_reset;
14476 	}
14477 
14478 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14479 	return;
14480 
14481 fw_reset:
14482 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14483 }
14484 
14485 static void bnxt_timer(struct timer_list *t)
14486 {
14487 	struct bnxt *bp = timer_container_of(bp, t, timer);
14488 	struct net_device *dev = bp->dev;
14489 
14490 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14491 		return;
14492 
14493 	if (atomic_read(&bp->intr_sem) != 0)
14494 		goto bnxt_restart_timer;
14495 
14496 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14497 		bnxt_fw_health_check(bp);
14498 
14499 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14500 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14501 
14502 	if (bnxt_tc_flower_enabled(bp))
14503 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14504 
14505 #ifdef CONFIG_RFS_ACCEL
14506 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14507 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14508 #endif /*CONFIG_RFS_ACCEL*/
14509 
14510 	if (bp->link_info.phy_retry) {
14511 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14512 			bp->link_info.phy_retry = false;
14513 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14514 		} else {
14515 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14516 		}
14517 	}
14518 
14519 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14520 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14521 
14522 bnxt_restart_timer:
14523 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14524 }
14525 
14526 static void bnxt_lock_sp(struct bnxt *bp)
14527 {
14528 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14529 	 * set.  If the device is being closed, bnxt_close() may be holding
14530 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14531 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14532 	 * instance lock.
14533 	 */
14534 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14535 	netdev_lock(bp->dev);
14536 }
14537 
14538 static void bnxt_unlock_sp(struct bnxt *bp)
14539 {
14540 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14541 	netdev_unlock(bp->dev);
14542 }
14543 
14544 /* Same as bnxt_lock_sp() with additional rtnl_lock */
14545 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
14546 {
14547 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14548 	rtnl_lock();
14549 	netdev_lock(bp->dev);
14550 }
14551 
14552 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
14553 {
14554 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14555 	netdev_unlock(bp->dev);
14556 	rtnl_unlock();
14557 }
14558 
14559 static void bnxt_tph_update(struct bnxt *bp)
14560 {
14561 	struct net_device *dev = bp->dev;
14562 	int i;
14563 
14564 	bnxt_lock_sp(bp);
14565 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
14566 		goto unlock;
14567 
14568 	for (i = 0; i < bp->rx_nr_rings; i++) {
14569 		struct bnxt_irq *irq;
14570 		int map_idx, err;
14571 		u16 tag;
14572 
14573 		map_idx = bnxt_cp_num_to_irq_num(bp, i);
14574 		irq = &bp->irq_tbl[map_idx];
14575 		tag = READ_ONCE(irq->new_tag);
14576 		if (irq->tag == tag)
14577 			continue;
14578 
14579 		if (pcie_tph_set_st_entry(bp->pdev, irq->msix_nr, tag))
14580 			continue;
14581 
14582 		err = netdev_rx_queue_restart(dev, irq->ring_nr);
14583 		if (err) {
14584 			netdev_err(dev, "RX queue restart failed: err=%d\n",
14585 				   err);
14586 			continue;
14587 		}
14588 
14589 		irq->tag = tag;
14590 	}
14591 
14592 unlock:
14593 	bnxt_unlock_sp(bp);
14594 }
14595 
14596 /* Only called from bnxt_sp_task() */
14597 static void bnxt_reset(struct bnxt *bp, bool silent)
14598 {
14599 	bnxt_rtnl_lock_sp(bp);
14600 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14601 		bnxt_reset_task(bp, silent);
14602 	bnxt_rtnl_unlock_sp(bp);
14603 }
14604 
14605 /* Only called from bnxt_sp_task() */
14606 static void bnxt_rx_ring_reset(struct bnxt *bp)
14607 {
14608 	int i;
14609 
14610 	bnxt_rtnl_lock_sp(bp);
14611 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14612 		bnxt_rtnl_unlock_sp(bp);
14613 		return;
14614 	}
14615 	/* Disable and flush TPA before resetting the RX ring */
14616 	if (bp->flags & BNXT_FLAG_TPA)
14617 		bnxt_set_tpa(bp, false);
14618 	for (i = 0; i < bp->rx_nr_rings; i++) {
14619 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14620 		struct bnxt_cp_ring_info *cpr;
14621 		int rc;
14622 
14623 		if (!rxr->bnapi->in_reset)
14624 			continue;
14625 
14626 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14627 		if (rc) {
14628 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14629 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14630 			else
14631 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14632 					    rc);
14633 			bnxt_reset_task(bp, true);
14634 			break;
14635 		}
14636 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14637 		rxr->rx_prod = 0;
14638 		rxr->rx_agg_prod = 0;
14639 		rxr->rx_sw_agg_prod = 0;
14640 		rxr->rx_next_cons = 0;
14641 		rxr->bnapi->in_reset = false;
14642 		rc = bnxt_alloc_one_rx_ring(bp, i);
14643 		if (rc) {
14644 			netdev_warn(bp->dev, "RX ring reset failed to allocate buffers, rc = %d, falling back to global reset\n",
14645 				    rc);
14646 			bnxt_reset_task(bp, true);
14647 			bnxt_rtnl_unlock_sp(bp);
14648 			return;
14649 		}
14650 		cpr = &rxr->bnapi->cp_ring;
14651 		cpr->sw_stats->rx.rx_resets++;
14652 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14653 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14654 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14655 	}
14656 	if (bp->flags & BNXT_FLAG_TPA)
14657 		bnxt_set_tpa(bp, true);
14658 	bnxt_rtnl_unlock_sp(bp);
14659 }
14660 
14661 static void bnxt_fw_fatal_close(struct bnxt *bp)
14662 {
14663 	bnxt_tx_disable(bp);
14664 	bnxt_disable_napi(bp);
14665 	bnxt_disable_int_sync(bp);
14666 	bnxt_free_irq(bp);
14667 	bnxt_clear_int_mode(bp);
14668 	pci_disable_device(bp->pdev);
14669 }
14670 
14671 static void bnxt_fw_reset_close(struct bnxt *bp)
14672 {
14673 	/* When firmware is in fatal state, quiesce device and disable
14674 	 * bus master to prevent any potential bad DMAs before freeing
14675 	 * kernel memory.
14676 	 */
14677 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14678 		u16 val = 0;
14679 
14680 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14681 		if (val == 0xffff)
14682 			bp->fw_reset_min_dsecs = 0;
14683 		bnxt_fw_fatal_close(bp);
14684 	}
14685 	__bnxt_close_nic(bp, true, false);
14686 	bnxt_vf_reps_free(bp);
14687 	bnxt_clear_int_mode(bp);
14688 	bnxt_hwrm_func_drv_unrgtr(bp);
14689 	if (pci_is_enabled(bp->pdev))
14690 		pci_disable_device(bp->pdev);
14691 	bnxt_free_ctx_mem(bp, false);
14692 }
14693 
14694 static bool is_bnxt_fw_ok(struct bnxt *bp)
14695 {
14696 	struct bnxt_fw_health *fw_health = bp->fw_health;
14697 	bool no_heartbeat = false, has_reset = false;
14698 	u32 val;
14699 
14700 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14701 	if (val == fw_health->last_fw_heartbeat)
14702 		no_heartbeat = true;
14703 
14704 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14705 	if (val != fw_health->last_fw_reset_cnt)
14706 		has_reset = true;
14707 
14708 	if (!no_heartbeat && has_reset)
14709 		return true;
14710 
14711 	return false;
14712 }
14713 
14714 /* netdev instance lock is acquired before calling this function */
14715 static void bnxt_force_fw_reset(struct bnxt *bp)
14716 {
14717 	struct bnxt_fw_health *fw_health = bp->fw_health;
14718 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14719 	u32 wait_dsecs;
14720 
14721 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14722 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14723 		return;
14724 
14725 	/* we have to serialize with bnxt_refclk_read()*/
14726 	if (ptp) {
14727 		unsigned long flags;
14728 
14729 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14730 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14731 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14732 	} else {
14733 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14734 	}
14735 	bnxt_fw_reset_close(bp);
14736 	wait_dsecs = fw_health->master_func_wait_dsecs;
14737 	if (fw_health->primary) {
14738 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14739 			wait_dsecs = 0;
14740 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14741 	} else {
14742 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14743 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14744 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14745 	}
14746 
14747 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14748 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14749 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14750 }
14751 
14752 void bnxt_fw_exception(struct bnxt *bp)
14753 {
14754 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14755 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14756 	bnxt_ulp_stop(bp);
14757 	bnxt_lock_sp(bp);
14758 	bnxt_force_fw_reset(bp);
14759 	bnxt_unlock_sp(bp);
14760 }
14761 
14762 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14763  * < 0 on error.
14764  */
14765 static int bnxt_get_registered_vfs(struct bnxt *bp)
14766 {
14767 #ifdef CONFIG_BNXT_SRIOV
14768 	int rc;
14769 
14770 	if (!BNXT_PF(bp))
14771 		return 0;
14772 
14773 	rc = bnxt_hwrm_func_qcfg(bp);
14774 	if (rc) {
14775 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14776 		return rc;
14777 	}
14778 	if (bp->pf.registered_vfs)
14779 		return bp->pf.registered_vfs;
14780 	if (bp->sriov_cfg)
14781 		return 1;
14782 #endif
14783 	return 0;
14784 }
14785 
14786 void bnxt_fw_reset(struct bnxt *bp)
14787 {
14788 	bnxt_ulp_stop(bp);
14789 	bnxt_lock_sp(bp);
14790 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14791 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14792 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14793 		int n = 0, tmo;
14794 
14795 		/* we have to serialize with bnxt_refclk_read()*/
14796 		if (ptp) {
14797 			unsigned long flags;
14798 
14799 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14800 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14801 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14802 		} else {
14803 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14804 		}
14805 		if (bp->pf.active_vfs &&
14806 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14807 			n = bnxt_get_registered_vfs(bp);
14808 		if (n < 0) {
14809 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14810 				   n);
14811 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14812 			netif_close(bp->dev);
14813 			goto fw_reset_exit;
14814 		} else if (n > 0) {
14815 			u16 vf_tmo_dsecs = n * 10;
14816 
14817 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14818 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14819 			bp->fw_reset_state =
14820 				BNXT_FW_RESET_STATE_POLL_VF;
14821 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14822 			goto fw_reset_exit;
14823 		}
14824 		bnxt_fw_reset_close(bp);
14825 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14826 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14827 			tmo = HZ / 10;
14828 		} else {
14829 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14830 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14831 		}
14832 		bnxt_queue_fw_reset_work(bp, tmo);
14833 	}
14834 fw_reset_exit:
14835 	bnxt_unlock_sp(bp);
14836 }
14837 
14838 static void bnxt_chk_missed_irq(struct bnxt *bp)
14839 {
14840 	int i;
14841 
14842 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14843 		return;
14844 
14845 	for (i = 0; i < bp->cp_nr_rings; i++) {
14846 		struct bnxt_napi *bnapi = bp->bnapi[i];
14847 		struct bnxt_cp_ring_info *cpr;
14848 		u32 fw_ring_id;
14849 		int j;
14850 
14851 		if (!bnapi)
14852 			continue;
14853 
14854 		cpr = &bnapi->cp_ring;
14855 		for (j = 0; j < cpr->cp_ring_count; j++) {
14856 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14857 			u32 val[2];
14858 
14859 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14860 				continue;
14861 
14862 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14863 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14864 				continue;
14865 			}
14866 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14867 			bnxt_dbg_hwrm_ring_info_get(bp,
14868 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14869 				fw_ring_id, &val[0], &val[1]);
14870 			cpr->sw_stats->cmn.missed_irqs++;
14871 		}
14872 	}
14873 }
14874 
14875 static void bnxt_cfg_ntp_filters(struct bnxt *);
14876 
14877 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14878 {
14879 	struct bnxt_link_info *link_info = &bp->link_info;
14880 
14881 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14882 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14883 		if (bp->hwrm_spec_code >= 0x10201) {
14884 			if (link_info->auto_pause_setting &
14885 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14886 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14887 		} else {
14888 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14889 		}
14890 		bnxt_set_auto_speed(link_info);
14891 	} else {
14892 		bnxt_set_force_speed(link_info);
14893 		link_info->req_duplex = link_info->duplex_setting;
14894 	}
14895 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14896 		link_info->req_flow_ctrl =
14897 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14898 	else
14899 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14900 }
14901 
14902 static void bnxt_fw_echo_reply(struct bnxt *bp)
14903 {
14904 	struct bnxt_fw_health *fw_health = bp->fw_health;
14905 	struct hwrm_func_echo_response_input *req;
14906 	int rc;
14907 
14908 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14909 	if (rc)
14910 		return;
14911 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14912 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14913 	hwrm_req_send(bp, req);
14914 }
14915 
14916 static void bnxt_ulp_restart(struct bnxt *bp)
14917 {
14918 	bnxt_ulp_stop(bp);
14919 	bnxt_ulp_start(bp);
14920 }
14921 
14922 static void bnxt_sp_task(struct work_struct *work)
14923 {
14924 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14925 
14926 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14927 	smp_mb__after_atomic();
14928 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14929 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14930 		return;
14931 	}
14932 
14933 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14934 		bnxt_ulp_restart(bp);
14935 		bnxt_reenable_sriov(bp);
14936 	}
14937 
14938 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14939 		bnxt_cfg_ntp_filters(bp);
14940 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14941 		bnxt_hwrm_exec_fwd_req(bp);
14942 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14943 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14944 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14945 		bnxt_hwrm_port_qstats(bp, 0);
14946 		bnxt_hwrm_port_qstats_ext(bp, 0);
14947 		spin_lock(&bp->stats_lock);
14948 		bnxt_accumulate_all_stats(bp);
14949 		bp->stats_updated_jiffies = jiffies;
14950 		spin_unlock(&bp->stats_lock);
14951 	}
14952 
14953 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14954 		int rc;
14955 
14956 		mutex_lock(&bp->link_lock);
14957 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14958 				       &bp->sp_event))
14959 			bnxt_hwrm_phy_qcaps(bp);
14960 
14961 		rc = bnxt_update_link(bp, true);
14962 		if (rc)
14963 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14964 				   rc);
14965 
14966 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14967 				       &bp->sp_event))
14968 			bnxt_init_ethtool_link_settings(bp);
14969 		mutex_unlock(&bp->link_lock);
14970 	}
14971 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14972 		int rc;
14973 
14974 		mutex_lock(&bp->link_lock);
14975 		rc = bnxt_update_phy_setting(bp);
14976 		mutex_unlock(&bp->link_lock);
14977 		if (rc) {
14978 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14979 		} else {
14980 			bp->link_info.phy_retry = false;
14981 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14982 		}
14983 	}
14984 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14985 		mutex_lock(&bp->link_lock);
14986 		bnxt_get_port_module_status(bp);
14987 		mutex_unlock(&bp->link_lock);
14988 	}
14989 
14990 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14991 		bnxt_tc_flow_stats_work(bp);
14992 
14993 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14994 		bnxt_chk_missed_irq(bp);
14995 
14996 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14997 		bnxt_fw_echo_reply(bp);
14998 
14999 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
15000 		bnxt_hwmon_notify_event(bp);
15001 
15002 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
15003 	 * must be the last functions to be called before exiting.
15004 	 */
15005 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
15006 		bnxt_reset(bp, false);
15007 
15008 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
15009 		bnxt_reset(bp, true);
15010 
15011 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
15012 		bnxt_rx_ring_reset(bp);
15013 
15014 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
15015 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
15016 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
15017 			bnxt_devlink_health_fw_report(bp);
15018 		else
15019 			bnxt_fw_reset(bp);
15020 	}
15021 
15022 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
15023 		if (!is_bnxt_fw_ok(bp))
15024 			bnxt_devlink_health_fw_report(bp);
15025 	}
15026 
15027 	if (test_and_clear_bit(BNXT_TPH_UPDATE_SP_EVENT, &bp->sp_event))
15028 		bnxt_tph_update(bp);
15029 
15030 	smp_mb__before_atomic();
15031 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
15032 }
15033 
15034 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15035 				int *max_cp);
15036 
15037 /* Under netdev instance lock */
15038 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
15039 		     int tx_xdp)
15040 {
15041 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
15042 	struct bnxt_hw_rings hwr = {0};
15043 	int rx_rings = rx;
15044 	int rc;
15045 
15046 	if (tcs)
15047 		tx_sets = tcs;
15048 
15049 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
15050 
15051 	if (max_rx < rx_rings)
15052 		return -ENOMEM;
15053 
15054 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15055 		rx_rings <<= 1;
15056 
15057 	hwr.rx = rx_rings;
15058 	hwr.tx = tx * tx_sets + tx_xdp;
15059 	if (max_tx < hwr.tx)
15060 		return -ENOMEM;
15061 
15062 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
15063 
15064 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
15065 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
15066 	if (max_cp < hwr.cp)
15067 		return -ENOMEM;
15068 	hwr.stat = hwr.cp;
15069 	if (BNXT_NEW_RM(bp)) {
15070 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
15071 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
15072 		hwr.grp = rx;
15073 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
15074 	}
15075 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
15076 		hwr.cp_p5 = hwr.tx + rx;
15077 	rc = bnxt_hwrm_check_rings(bp, &hwr);
15078 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
15079 		if (!bnxt_ulp_registered(bp->edev[BNXT_AUXDEV_RDMA])) {
15080 			hwr.cp += bnxt_get_ulp_msix_num(bp);
15081 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
15082 		}
15083 		if (hwr.cp > bp->total_irqs) {
15084 			int total_msix = bnxt_change_msix(bp, hwr.cp);
15085 
15086 			if (total_msix < hwr.cp) {
15087 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
15088 					    hwr.cp, total_msix);
15089 				rc = -ENOSPC;
15090 			}
15091 		}
15092 	}
15093 	return rc;
15094 }
15095 
15096 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
15097 {
15098 	if (bp->bar2) {
15099 		pci_iounmap(pdev, bp->bar2);
15100 		bp->bar2 = NULL;
15101 	}
15102 
15103 	if (bp->bar1) {
15104 		pci_iounmap(pdev, bp->bar1);
15105 		bp->bar1 = NULL;
15106 	}
15107 
15108 	if (bp->bar0) {
15109 		pci_iounmap(pdev, bp->bar0);
15110 		bp->bar0 = NULL;
15111 	}
15112 }
15113 
15114 static void bnxt_cleanup_pci(struct bnxt *bp)
15115 {
15116 	pci_disable_ptm(bp->pdev);
15117 	bnxt_unmap_bars(bp, bp->pdev);
15118 	pci_release_regions(bp->pdev);
15119 	if (pci_is_enabled(bp->pdev))
15120 		pci_disable_device(bp->pdev);
15121 }
15122 
15123 static void bnxt_init_dflt_coal(struct bnxt *bp)
15124 {
15125 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
15126 	struct bnxt_coal *coal;
15127 	u16 flags = 0;
15128 
15129 	if (coal_cap->cmpl_params &
15130 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
15131 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
15132 
15133 	/* Tick values in micro seconds.
15134 	 * 1 coal_buf x bufs_per_record = 1 completion record.
15135 	 */
15136 	coal = &bp->rx_coal;
15137 	coal->coal_ticks = 10;
15138 	coal->coal_bufs = 30;
15139 	coal->coal_ticks_irq = 1;
15140 	coal->coal_bufs_irq = 2;
15141 	coal->idle_thresh = 50;
15142 	coal->bufs_per_record = 2;
15143 	coal->budget = 64;		/* NAPI budget */
15144 	coal->flags = flags;
15145 
15146 	coal = &bp->tx_coal;
15147 	coal->coal_ticks = 28;
15148 	coal->coal_bufs = 30;
15149 	coal->coal_ticks_irq = 2;
15150 	coal->coal_bufs_irq = 2;
15151 	coal->bufs_per_record = 1;
15152 	coal->flags = flags;
15153 
15154 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
15155 }
15156 
15157 /* FW that pre-reserves 1 VNIC per function */
15158 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
15159 {
15160 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
15161 
15162 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15163 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
15164 		return true;
15165 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
15166 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
15167 		return true;
15168 	return false;
15169 }
15170 
15171 static void bnxt_hwrm_pfcwd_qcaps(struct bnxt *bp)
15172 {
15173 	struct hwrm_queue_pfcwd_timeout_qcaps_output *resp;
15174 	struct hwrm_queue_pfcwd_timeout_qcaps_input *req;
15175 	int rc;
15176 
15177 	bp->max_pfcwd_tmo_ms = 0;
15178 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_PFCWD_TIMEOUT_QCAPS);
15179 	if (rc)
15180 		return;
15181 	resp = hwrm_req_hold(bp, req);
15182 	rc = hwrm_req_send_silent(bp, req);
15183 	if (!rc)
15184 		bp->max_pfcwd_tmo_ms = le16_to_cpu(resp->max_pfcwd_timeout);
15185 	hwrm_req_drop(bp, req);
15186 }
15187 
15188 static int bnxt_fw_init_one_p1(struct bnxt *bp)
15189 {
15190 	int rc;
15191 
15192 	bp->fw_cap = 0;
15193 	rc = bnxt_hwrm_ver_get(bp);
15194 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
15195 	 * so wait before continuing with recovery.
15196 	 */
15197 	if (rc)
15198 		msleep(100);
15199 	bnxt_try_map_fw_health_reg(bp);
15200 	if (rc) {
15201 		rc = bnxt_try_recover_fw(bp);
15202 		if (rc)
15203 			return rc;
15204 		rc = bnxt_hwrm_ver_get(bp);
15205 		if (rc)
15206 			return rc;
15207 	}
15208 
15209 	bnxt_nvm_cfg_ver_get(bp);
15210 
15211 	rc = bnxt_hwrm_func_reset(bp);
15212 	if (rc)
15213 		return -ENODEV;
15214 
15215 	bnxt_hwrm_fw_set_time(bp);
15216 	return 0;
15217 }
15218 
15219 static int bnxt_fw_init_one_p2(struct bnxt *bp)
15220 {
15221 	int rc;
15222 
15223 	/* Get the MAX capabilities for this function */
15224 	rc = bnxt_hwrm_func_qcaps(bp);
15225 	if (rc) {
15226 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
15227 			   rc);
15228 		return -ENODEV;
15229 	}
15230 
15231 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
15232 	if (rc)
15233 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
15234 			    rc);
15235 
15236 	if (bnxt_alloc_fw_health(bp)) {
15237 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
15238 	} else {
15239 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
15240 		if (rc)
15241 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
15242 				    rc);
15243 	}
15244 
15245 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
15246 	if (rc)
15247 		return -ENODEV;
15248 
15249 	rc = bnxt_alloc_crash_dump_mem(bp);
15250 	if (rc)
15251 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
15252 			    rc);
15253 	if (!rc) {
15254 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
15255 		if (rc) {
15256 			bnxt_free_crash_dump_mem(bp);
15257 			netdev_warn(bp->dev,
15258 				    "hwrm crash dump mem failure rc: %d\n", rc);
15259 		}
15260 	}
15261 
15262 	if (bnxt_fw_pre_resv_vnics(bp))
15263 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
15264 
15265 	bnxt_hwrm_pfcwd_qcaps(bp);
15266 	bnxt_hwrm_func_qcfg(bp);
15267 	bnxt_hwrm_vnic_qcaps(bp);
15268 	bnxt_hwrm_port_led_qcaps(bp);
15269 	bnxt_ethtool_init(bp);
15270 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
15271 		__bnxt_hwrm_ptp_qcfg(bp);
15272 	bnxt_dcb_init(bp);
15273 	bnxt_hwmon_init(bp);
15274 	return 0;
15275 }
15276 
15277 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
15278 {
15279 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
15280 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
15281 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
15282 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
15283 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
15284 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
15285 		bp->rss_hash_delta = bp->rss_hash_cfg;
15286 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
15287 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
15288 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
15289 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
15290 	}
15291 }
15292 
15293 static void bnxt_set_dflt_rfs(struct bnxt *bp)
15294 {
15295 	struct net_device *dev = bp->dev;
15296 
15297 	dev->hw_features &= ~NETIF_F_NTUPLE;
15298 	dev->features &= ~NETIF_F_NTUPLE;
15299 	bp->flags &= ~BNXT_FLAG_RFS;
15300 	if (bnxt_rfs_supported(bp)) {
15301 		dev->hw_features |= NETIF_F_NTUPLE;
15302 		if (bnxt_rfs_capable(bp, false)) {
15303 			bp->flags |= BNXT_FLAG_RFS;
15304 			dev->features |= NETIF_F_NTUPLE;
15305 		}
15306 	}
15307 }
15308 
15309 static void bnxt_fw_init_one_p3(struct bnxt *bp)
15310 {
15311 	struct pci_dev *pdev = bp->pdev;
15312 
15313 	bnxt_set_dflt_rss_hash_type(bp);
15314 	bnxt_set_dflt_rfs(bp);
15315 
15316 	bnxt_get_wol_settings(bp);
15317 	if (bp->flags & BNXT_FLAG_WOL_CAP)
15318 		device_set_wakeup_enable(&pdev->dev, bp->wol);
15319 	else
15320 		device_set_wakeup_capable(&pdev->dev, false);
15321 
15322 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
15323 	bnxt_hwrm_coal_params_qcaps(bp);
15324 }
15325 
15326 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
15327 
15328 int bnxt_fw_init_one(struct bnxt *bp)
15329 {
15330 	int rc;
15331 
15332 	rc = bnxt_fw_init_one_p1(bp);
15333 	if (rc) {
15334 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
15335 		return rc;
15336 	}
15337 	rc = bnxt_fw_init_one_p2(bp);
15338 	if (rc) {
15339 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
15340 		return rc;
15341 	}
15342 	rc = bnxt_probe_phy(bp, false);
15343 	if (rc)
15344 		return rc;
15345 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
15346 	if (rc)
15347 		return rc;
15348 
15349 	bnxt_fw_init_one_p3(bp);
15350 	return 0;
15351 }
15352 
15353 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
15354 {
15355 	struct bnxt_fw_health *fw_health = bp->fw_health;
15356 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
15357 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
15358 	u32 reg_type, reg_off, delay_msecs;
15359 
15360 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
15361 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
15362 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
15363 	switch (reg_type) {
15364 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
15365 		pci_write_config_dword(bp->pdev, reg_off, val);
15366 		break;
15367 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
15368 		writel(reg_off & BNXT_GRC_BASE_MASK,
15369 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
15370 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
15371 		fallthrough;
15372 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
15373 		writel(val, bp->bar0 + reg_off);
15374 		break;
15375 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
15376 		writel(val, bp->bar1 + reg_off);
15377 		break;
15378 	}
15379 	if (delay_msecs) {
15380 		pci_read_config_dword(bp->pdev, 0, &val);
15381 		msleep(delay_msecs);
15382 	}
15383 }
15384 
15385 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
15386 {
15387 	struct hwrm_func_qcfg_output *resp;
15388 	struct hwrm_func_qcfg_input *req;
15389 	bool result = true; /* firmware will enforce if unknown */
15390 
15391 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
15392 		return result;
15393 
15394 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
15395 		return result;
15396 
15397 	req->fid = cpu_to_le16(0xffff);
15398 	resp = hwrm_req_hold(bp, req);
15399 	if (!hwrm_req_send(bp, req))
15400 		result = !!(le16_to_cpu(resp->flags) &
15401 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
15402 	hwrm_req_drop(bp, req);
15403 	return result;
15404 }
15405 
15406 static void bnxt_reset_all(struct bnxt *bp)
15407 {
15408 	struct bnxt_fw_health *fw_health = bp->fw_health;
15409 	int i, rc;
15410 
15411 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15412 		bnxt_fw_reset_via_optee(bp);
15413 		bp->fw_reset_timestamp = jiffies;
15414 		return;
15415 	}
15416 
15417 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
15418 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
15419 			bnxt_fw_reset_writel(bp, i);
15420 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
15421 		struct hwrm_fw_reset_input *req;
15422 
15423 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
15424 		if (!rc) {
15425 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
15426 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
15427 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
15428 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
15429 			rc = hwrm_req_send(bp, req);
15430 		}
15431 		if (rc != -ENODEV)
15432 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
15433 	}
15434 	bp->fw_reset_timestamp = jiffies;
15435 }
15436 
15437 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
15438 {
15439 	return time_after(jiffies, bp->fw_reset_timestamp +
15440 			  (bp->fw_reset_max_dsecs * HZ / 10));
15441 }
15442 
15443 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
15444 {
15445 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15446 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
15447 		bnxt_dl_health_fw_status_update(bp, false);
15448 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15449 	netif_close(bp->dev);
15450 }
15451 
15452 static void bnxt_fw_reset_task(struct work_struct *work)
15453 {
15454 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15455 	int rc = 0;
15456 
15457 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15458 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15459 		return;
15460 	}
15461 
15462 	switch (bp->fw_reset_state) {
15463 	case BNXT_FW_RESET_STATE_POLL_VF: {
15464 		int n = bnxt_get_registered_vfs(bp);
15465 		int tmo;
15466 
15467 		if (n < 0) {
15468 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15469 				   n, jiffies_to_msecs(jiffies -
15470 				   bp->fw_reset_timestamp));
15471 			goto fw_reset_abort;
15472 		} else if (n > 0) {
15473 			if (bnxt_fw_reset_timeout(bp)) {
15474 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15475 				bp->fw_reset_state = 0;
15476 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15477 					   n);
15478 				goto ulp_start;
15479 			}
15480 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15481 			return;
15482 		}
15483 		bp->fw_reset_timestamp = jiffies;
15484 		netdev_lock(bp->dev);
15485 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15486 			bnxt_fw_reset_abort(bp, rc);
15487 			netdev_unlock(bp->dev);
15488 			goto ulp_start;
15489 		}
15490 		bnxt_fw_reset_close(bp);
15491 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15492 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15493 			tmo = HZ / 10;
15494 		} else {
15495 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15496 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15497 		}
15498 		netdev_unlock(bp->dev);
15499 		bnxt_queue_fw_reset_work(bp, tmo);
15500 		return;
15501 	}
15502 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15503 		u32 val;
15504 
15505 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15506 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15507 		    !bnxt_fw_reset_timeout(bp)) {
15508 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15509 			return;
15510 		}
15511 
15512 		if (!bp->fw_health->primary) {
15513 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15514 
15515 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15516 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15517 			return;
15518 		}
15519 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15520 	}
15521 		fallthrough;
15522 	case BNXT_FW_RESET_STATE_RESET_FW:
15523 		bnxt_reset_all(bp);
15524 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15525 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15526 		return;
15527 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15528 		bnxt_inv_fw_health_reg(bp);
15529 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15530 		    !bp->fw_reset_min_dsecs) {
15531 			u16 val;
15532 
15533 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15534 			if (val == 0xffff) {
15535 				if (bnxt_fw_reset_timeout(bp)) {
15536 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15537 					rc = -ETIMEDOUT;
15538 					goto fw_reset_abort;
15539 				}
15540 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15541 				return;
15542 			}
15543 		}
15544 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15545 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15546 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15547 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15548 			bnxt_dl_remote_reload(bp);
15549 		if (!pci_is_enabled(bp->pdev) && pci_enable_device(bp->pdev)) {
15550 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15551 			rc = -ENODEV;
15552 			goto fw_reset_abort;
15553 		}
15554 		pci_set_master(bp->pdev);
15555 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15556 		fallthrough;
15557 	case BNXT_FW_RESET_STATE_POLL_FW:
15558 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15559 		rc = bnxt_hwrm_poll(bp);
15560 		if (rc) {
15561 			if (bnxt_fw_reset_timeout(bp)) {
15562 				netdev_err(bp->dev, "Firmware reset aborted\n");
15563 				goto fw_reset_abort_status;
15564 			}
15565 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15566 			return;
15567 		}
15568 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15569 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15570 		fallthrough;
15571 	case BNXT_FW_RESET_STATE_OPENING:
15572 		while (!rtnl_trylock()) {
15573 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15574 			return;
15575 		}
15576 		netdev_lock(bp->dev);
15577 		rc = bnxt_open(bp->dev);
15578 		if (rc) {
15579 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15580 			bnxt_fw_reset_abort(bp, rc);
15581 			netdev_unlock(bp->dev);
15582 			rtnl_unlock();
15583 			goto ulp_start;
15584 		}
15585 
15586 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15587 		    bp->fw_health->enabled) {
15588 			bp->fw_health->last_fw_reset_cnt =
15589 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15590 		}
15591 		bp->fw_reset_state = 0;
15592 		/* Make sure fw_reset_state is 0 before clearing the flag */
15593 		smp_mb__before_atomic();
15594 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15595 		bnxt_ptp_reapply_pps(bp);
15596 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15597 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15598 			bnxt_dl_health_fw_recovery_done(bp);
15599 			bnxt_dl_health_fw_status_update(bp, true);
15600 		}
15601 		netdev_unlock(bp->dev);
15602 		rtnl_unlock();
15603 		bnxt_ulp_start(bp);
15604 		bnxt_reenable_sriov(bp);
15605 		netdev_lock(bp->dev);
15606 		bnxt_vf_reps_alloc(bp);
15607 		bnxt_vf_reps_open(bp);
15608 		netdev_unlock(bp->dev);
15609 		break;
15610 	}
15611 	return;
15612 
15613 fw_reset_abort_status:
15614 	if (bp->fw_health->status_reliable ||
15615 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15616 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15617 
15618 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15619 	}
15620 fw_reset_abort:
15621 	netdev_lock(bp->dev);
15622 	bnxt_fw_reset_abort(bp, rc);
15623 	netdev_unlock(bp->dev);
15624 ulp_start:
15625 	if (!rc)
15626 		bnxt_ulp_start(bp);
15627 }
15628 
15629 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15630 {
15631 	int rc;
15632 	struct bnxt *bp = netdev_priv(dev);
15633 
15634 	SET_NETDEV_DEV(dev, &pdev->dev);
15635 
15636 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15637 	rc = pci_enable_device(pdev);
15638 	if (rc) {
15639 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15640 		goto init_err;
15641 	}
15642 
15643 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15644 		dev_err(&pdev->dev,
15645 			"Cannot find PCI device base address, aborting\n");
15646 		rc = -ENODEV;
15647 		goto init_err_disable;
15648 	}
15649 
15650 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15651 	if (rc) {
15652 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15653 		goto init_err_disable;
15654 	}
15655 
15656 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15657 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15658 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15659 		rc = -EIO;
15660 		goto init_err_release;
15661 	}
15662 
15663 	pci_set_master(pdev);
15664 
15665 	bp->dev = dev;
15666 	bp->pdev = pdev;
15667 
15668 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15669 	 * determines the BAR size.
15670 	 */
15671 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15672 	if (!bp->bar0) {
15673 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15674 		rc = -ENOMEM;
15675 		goto init_err_release;
15676 	}
15677 
15678 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15679 	if (!bp->bar2) {
15680 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15681 		rc = -ENOMEM;
15682 		goto init_err_release;
15683 	}
15684 
15685 	pci_enable_ptm(pdev);
15686 
15687 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15688 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15689 
15690 	spin_lock_init(&bp->ntp_fltr_lock);
15691 	spin_lock_init(&bp->stats_lock);
15692 #if BITS_PER_LONG == 32
15693 	spin_lock_init(&bp->db_lock);
15694 #endif
15695 
15696 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15697 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15698 
15699 	timer_setup(&bp->timer, bnxt_timer, 0);
15700 	bp->current_interval = BNXT_TIMER_INTERVAL;
15701 
15702 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15703 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15704 
15705 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15706 	return 0;
15707 
15708 init_err_release:
15709 	bnxt_unmap_bars(bp, pdev);
15710 	pci_release_regions(pdev);
15711 
15712 init_err_disable:
15713 	pci_disable_device(pdev);
15714 
15715 init_err:
15716 	return rc;
15717 }
15718 
15719 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15720 {
15721 	struct sockaddr *addr = p;
15722 	struct bnxt *bp = netdev_priv(dev);
15723 	int rc = 0;
15724 
15725 	netdev_assert_locked(dev);
15726 
15727 	if (!is_valid_ether_addr(addr->sa_data))
15728 		return -EADDRNOTAVAIL;
15729 
15730 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15731 		return 0;
15732 
15733 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15734 	if (rc)
15735 		return rc;
15736 
15737 	eth_hw_addr_set(dev, addr->sa_data);
15738 	bnxt_clear_usr_fltrs(bp, true);
15739 	if (netif_running(dev)) {
15740 		bnxt_close_nic(bp, false, false);
15741 		rc = bnxt_open_nic(bp, false, false);
15742 	}
15743 
15744 	return rc;
15745 }
15746 
15747 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15748 {
15749 	struct bnxt *bp = netdev_priv(dev);
15750 
15751 	netdev_assert_locked(dev);
15752 
15753 	if (netif_running(dev))
15754 		bnxt_close_nic(bp, true, false);
15755 
15756 	WRITE_ONCE(dev->mtu, new_mtu);
15757 
15758 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15759 	 * program is attached.  We need to set the AGG rings settings and
15760 	 * rx_skb_func accordingly.
15761 	 */
15762 	if (READ_ONCE(bp->xdp_prog))
15763 		bnxt_set_rx_skb_mode(bp, true);
15764 
15765 	bnxt_set_ring_params(bp);
15766 
15767 	if (netif_running(dev))
15768 		return bnxt_open_nic(bp, true, false);
15769 
15770 	return 0;
15771 }
15772 
15773 void bnxt_set_cp_rings(struct bnxt *bp, bool sh)
15774 {
15775 	int tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15776 
15777 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15778 			       tx_cp + bp->rx_nr_rings;
15779 }
15780 
15781 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15782 {
15783 	struct bnxt *bp = netdev_priv(dev);
15784 	bool sh = false;
15785 	int rc;
15786 
15787 	if (tc > bp->max_tc) {
15788 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15789 			   tc, bp->max_tc);
15790 		return -EINVAL;
15791 	}
15792 
15793 	if (bp->num_tc == tc)
15794 		return 0;
15795 
15796 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15797 		sh = true;
15798 
15799 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15800 			      sh, tc, bp->tx_nr_rings_xdp);
15801 	if (rc)
15802 		return rc;
15803 
15804 	/* Needs to close the device and do hw resource re-allocations */
15805 	if (netif_running(bp->dev))
15806 		bnxt_close_nic(bp, true, false);
15807 
15808 	if (tc) {
15809 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15810 		netdev_set_num_tc(dev, tc);
15811 		bp->num_tc = tc;
15812 	} else {
15813 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15814 		netdev_reset_tc(dev);
15815 		bp->num_tc = 0;
15816 	}
15817 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15818 	bnxt_set_cp_rings(bp, sh);
15819 
15820 	if (netif_running(bp->dev))
15821 		return bnxt_open_nic(bp, true, false);
15822 
15823 	return 0;
15824 }
15825 
15826 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15827 				  void *cb_priv)
15828 {
15829 	struct bnxt *bp = cb_priv;
15830 
15831 	if (!bnxt_tc_flower_enabled(bp) ||
15832 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15833 		return -EOPNOTSUPP;
15834 
15835 	switch (type) {
15836 	case TC_SETUP_CLSFLOWER:
15837 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15838 	default:
15839 		return -EOPNOTSUPP;
15840 	}
15841 }
15842 
15843 LIST_HEAD(bnxt_block_cb_list);
15844 
15845 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15846 			 void *type_data)
15847 {
15848 	struct bnxt *bp = netdev_priv(dev);
15849 
15850 	switch (type) {
15851 	case TC_SETUP_BLOCK:
15852 		return flow_block_cb_setup_simple(type_data,
15853 						  &bnxt_block_cb_list,
15854 						  bnxt_setup_tc_block_cb,
15855 						  bp, bp, true);
15856 	case TC_SETUP_QDISC_MQPRIO: {
15857 		struct tc_mqprio_qopt *mqprio = type_data;
15858 
15859 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15860 
15861 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15862 	}
15863 	default:
15864 		return -EOPNOTSUPP;
15865 	}
15866 }
15867 
15868 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15869 			    const struct sk_buff *skb)
15870 {
15871 	struct bnxt_vnic_info *vnic;
15872 
15873 	if (skb)
15874 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15875 
15876 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15877 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15878 }
15879 
15880 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15881 			   u32 idx)
15882 {
15883 	struct hlist_head *head;
15884 	int bit_id;
15885 
15886 	spin_lock_bh(&bp->ntp_fltr_lock);
15887 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15888 	if (bit_id < 0) {
15889 		spin_unlock_bh(&bp->ntp_fltr_lock);
15890 		return -ENOMEM;
15891 	}
15892 
15893 	fltr->base.sw_id = (u16)bit_id;
15894 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15895 	fltr->base.flags |= BNXT_ACT_RING_DST;
15896 	head = &bp->ntp_fltr_hash_tbl[idx];
15897 	hlist_add_head_rcu(&fltr->base.hash, head);
15898 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15899 	bnxt_insert_usr_fltr(bp, &fltr->base);
15900 	bp->ntp_fltr_count++;
15901 	spin_unlock_bh(&bp->ntp_fltr_lock);
15902 	return 0;
15903 }
15904 
15905 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15906 			    struct bnxt_ntuple_filter *f2)
15907 {
15908 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15909 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15910 	struct flow_keys *keys1 = &f1->fkeys;
15911 	struct flow_keys *keys2 = &f2->fkeys;
15912 
15913 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15914 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15915 		return false;
15916 
15917 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15918 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15919 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15920 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15921 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15922 			return false;
15923 	} else {
15924 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15925 				     &keys2->addrs.v6addrs.src) ||
15926 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15927 				     &masks2->addrs.v6addrs.src) ||
15928 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15929 				     &keys2->addrs.v6addrs.dst) ||
15930 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15931 				     &masks2->addrs.v6addrs.dst))
15932 			return false;
15933 	}
15934 
15935 	return keys1->ports.src == keys2->ports.src &&
15936 	       masks1->ports.src == masks2->ports.src &&
15937 	       keys1->ports.dst == keys2->ports.dst &&
15938 	       masks1->ports.dst == masks2->ports.dst &&
15939 	       keys1->control.flags == keys2->control.flags &&
15940 	       f1->l2_fltr == f2->l2_fltr;
15941 }
15942 
15943 struct bnxt_ntuple_filter *
15944 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15945 				struct bnxt_ntuple_filter *fltr, u32 idx)
15946 {
15947 	struct bnxt_ntuple_filter *f;
15948 	struct hlist_head *head;
15949 
15950 	head = &bp->ntp_fltr_hash_tbl[idx];
15951 	hlist_for_each_entry_rcu(f, head, base.hash) {
15952 		if (bnxt_fltr_match(f, fltr))
15953 			return f;
15954 	}
15955 	return NULL;
15956 }
15957 
15958 #ifdef CONFIG_RFS_ACCEL
15959 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15960 			      u16 rxq_index, u32 flow_id)
15961 {
15962 	struct bnxt *bp = netdev_priv(dev);
15963 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15964 	struct flow_keys *fkeys;
15965 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15966 	struct bnxt_l2_filter *l2_fltr;
15967 	int rc = 0, idx;
15968 	u32 flags;
15969 
15970 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15971 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15972 		atomic_inc(&l2_fltr->refcnt);
15973 	} else {
15974 		struct bnxt_l2_key key;
15975 
15976 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15977 		key.vlan = 0;
15978 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15979 		if (!l2_fltr)
15980 			return -EINVAL;
15981 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15982 			bnxt_del_l2_filter(bp, l2_fltr);
15983 			return -EINVAL;
15984 		}
15985 	}
15986 	new_fltr = kzalloc_obj(*new_fltr, GFP_ATOMIC);
15987 	if (!new_fltr) {
15988 		bnxt_del_l2_filter(bp, l2_fltr);
15989 		return -ENOMEM;
15990 	}
15991 
15992 	fkeys = &new_fltr->fkeys;
15993 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15994 		rc = -EPROTONOSUPPORT;
15995 		goto err_free;
15996 	}
15997 
15998 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15999 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
16000 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
16001 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
16002 		rc = -EPROTONOSUPPORT;
16003 		goto err_free;
16004 	}
16005 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
16006 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
16007 		if (bp->hwrm_spec_code < 0x10601) {
16008 			rc = -EPROTONOSUPPORT;
16009 			goto err_free;
16010 		}
16011 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
16012 	}
16013 	flags = fkeys->control.flags;
16014 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
16015 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
16016 		rc = -EPROTONOSUPPORT;
16017 		goto err_free;
16018 	}
16019 	new_fltr->l2_fltr = l2_fltr;
16020 
16021 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
16022 	rcu_read_lock();
16023 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
16024 	if (fltr) {
16025 		rc = fltr->base.sw_id;
16026 		rcu_read_unlock();
16027 		goto err_free;
16028 	}
16029 	rcu_read_unlock();
16030 
16031 	new_fltr->flow_id = flow_id;
16032 	new_fltr->base.rxq = rxq_index;
16033 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
16034 	if (!rc) {
16035 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
16036 		return new_fltr->base.sw_id;
16037 	}
16038 
16039 err_free:
16040 	bnxt_del_l2_filter(bp, l2_fltr);
16041 	kfree(new_fltr);
16042 	return rc;
16043 }
16044 #endif
16045 
16046 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
16047 {
16048 	spin_lock_bh(&bp->ntp_fltr_lock);
16049 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
16050 		spin_unlock_bh(&bp->ntp_fltr_lock);
16051 		return;
16052 	}
16053 	hlist_del_rcu(&fltr->base.hash);
16054 	bnxt_del_one_usr_fltr(bp, &fltr->base);
16055 	bp->ntp_fltr_count--;
16056 	spin_unlock_bh(&bp->ntp_fltr_lock);
16057 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
16058 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
16059 	kfree_rcu(fltr, base.rcu);
16060 }
16061 
16062 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
16063 {
16064 #ifdef CONFIG_RFS_ACCEL
16065 	int i;
16066 
16067 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
16068 		struct hlist_head *head;
16069 		struct hlist_node *tmp;
16070 		struct bnxt_ntuple_filter *fltr;
16071 		int rc;
16072 
16073 		head = &bp->ntp_fltr_hash_tbl[i];
16074 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
16075 			bool del = false;
16076 
16077 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
16078 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
16079 					continue;
16080 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
16081 							fltr->flow_id,
16082 							fltr->base.sw_id)) {
16083 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
16084 									 fltr);
16085 					del = true;
16086 				}
16087 			} else {
16088 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
16089 								       fltr);
16090 				if (rc)
16091 					del = true;
16092 				else
16093 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
16094 			}
16095 
16096 			if (del)
16097 				bnxt_del_ntp_filter(bp, fltr);
16098 		}
16099 	}
16100 #endif
16101 }
16102 
16103 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
16104 				    unsigned int entry, struct udp_tunnel_info *ti)
16105 {
16106 	struct bnxt *bp = netdev_priv(netdev);
16107 	unsigned int cmd;
16108 
16109 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16110 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
16111 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16112 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
16113 	else
16114 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
16115 
16116 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
16117 }
16118 
16119 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
16120 				      unsigned int entry, struct udp_tunnel_info *ti)
16121 {
16122 	struct bnxt *bp = netdev_priv(netdev);
16123 	unsigned int cmd;
16124 
16125 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
16126 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
16127 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
16128 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
16129 	else
16130 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
16131 
16132 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
16133 }
16134 
16135 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
16136 	.set_port	= bnxt_udp_tunnel_set_port,
16137 	.unset_port	= bnxt_udp_tunnel_unset_port,
16138 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16139 	.tables		= {
16140 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16141 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16142 	},
16143 }, bnxt_udp_tunnels_p7 = {
16144 	.set_port	= bnxt_udp_tunnel_set_port,
16145 	.unset_port	= bnxt_udp_tunnel_unset_port,
16146 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
16147 	.tables		= {
16148 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
16149 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
16150 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
16151 	},
16152 };
16153 
16154 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
16155 			       struct net_device *dev, u32 filter_mask,
16156 			       int nlflags)
16157 {
16158 	struct bnxt *bp = netdev_priv(dev);
16159 
16160 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
16161 				       nlflags, filter_mask, NULL);
16162 }
16163 
16164 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
16165 			       u16 flags, struct netlink_ext_ack *extack)
16166 {
16167 	struct bnxt *bp = netdev_priv(dev);
16168 	struct nlattr *attr, *br_spec;
16169 	int rem, rc = 0;
16170 
16171 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
16172 		return -EOPNOTSUPP;
16173 
16174 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
16175 	if (!br_spec)
16176 		return -EINVAL;
16177 
16178 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
16179 		u16 mode;
16180 
16181 		mode = nla_get_u16(attr);
16182 		if (mode == bp->br_mode)
16183 			break;
16184 
16185 		rc = bnxt_hwrm_set_br_mode(bp, mode);
16186 		if (!rc)
16187 			bp->br_mode = mode;
16188 		break;
16189 	}
16190 	return rc;
16191 }
16192 
16193 int bnxt_get_port_parent_id(struct net_device *dev,
16194 			    struct netdev_phys_item_id *ppid)
16195 {
16196 	struct bnxt *bp = netdev_priv(dev);
16197 
16198 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
16199 		return -EOPNOTSUPP;
16200 
16201 	/* The PF and it's VF-reps only support the switchdev framework */
16202 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
16203 		return -EOPNOTSUPP;
16204 
16205 	ppid->id_len = sizeof(bp->dsn);
16206 	memcpy(ppid->id, bp->dsn, ppid->id_len);
16207 
16208 	return 0;
16209 }
16210 
16211 static const struct net_device_ops bnxt_netdev_ops = {
16212 	.ndo_open		= bnxt_open,
16213 	.ndo_start_xmit		= bnxt_start_xmit,
16214 	.ndo_stop		= bnxt_close,
16215 	.ndo_get_stats64	= bnxt_get_stats64,
16216 	.ndo_set_rx_mode_async	= bnxt_set_rx_mode,
16217 	.ndo_eth_ioctl		= bnxt_ioctl,
16218 	.ndo_validate_addr	= eth_validate_addr,
16219 	.ndo_set_mac_address	= bnxt_change_mac_addr,
16220 	.ndo_change_mtu		= bnxt_change_mtu,
16221 	.ndo_fix_features	= bnxt_fix_features,
16222 	.ndo_set_features	= bnxt_set_features,
16223 	.ndo_features_check	= bnxt_features_check,
16224 	.ndo_tx_timeout		= bnxt_tx_timeout,
16225 #ifdef CONFIG_BNXT_SRIOV
16226 	.ndo_get_vf_config	= bnxt_get_vf_config,
16227 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
16228 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
16229 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
16230 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
16231 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
16232 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
16233 #endif
16234 	.ndo_setup_tc           = bnxt_setup_tc,
16235 #ifdef CONFIG_RFS_ACCEL
16236 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
16237 #endif
16238 	.ndo_bpf		= bnxt_xdp,
16239 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
16240 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
16241 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
16242 	.ndo_hwtstamp_get	= bnxt_hwtstamp_get,
16243 	.ndo_hwtstamp_set	= bnxt_hwtstamp_set,
16244 };
16245 
16246 static const struct xdp_metadata_ops bnxt_xdp_metadata_ops = {
16247 	.xmo_rx_hash		= bnxt_xdp_rx_hash,
16248 };
16249 
16250 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
16251 				    struct netdev_queue_stats_rx *stats)
16252 {
16253 	struct bnxt *bp = netdev_priv(dev);
16254 	struct bnxt_cp_ring_info *cpr;
16255 	u64 *sw;
16256 
16257 	if (!bp->bnapi)
16258 		return;
16259 
16260 	bnxt_sync_ring_stats(bp);
16261 	cpr = &bp->bnapi[i]->cp_ring;
16262 	sw = cpr->stats.sw_stats;
16263 
16264 	stats->packets = 0;
16265 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
16266 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
16267 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
16268 
16269 	stats->bytes = 0;
16270 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
16271 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
16272 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
16273 
16274 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
16275 	stats->hw_gro_packets = cpr->sw_stats->rx.rx_hw_gro_packets;
16276 	stats->hw_gro_wire_packets = cpr->sw_stats->rx.rx_hw_gro_wire_packets;
16277 }
16278 
16279 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
16280 				    struct netdev_queue_stats_tx *stats)
16281 {
16282 	struct bnxt *bp = netdev_priv(dev);
16283 	struct bnxt_napi *bnapi;
16284 	u64 *sw;
16285 
16286 	if (!bp->tx_ring)
16287 		return;
16288 
16289 	bnxt_sync_ring_stats(bp);
16290 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
16291 	sw = bnapi->cp_ring.stats.sw_stats;
16292 
16293 	stats->packets = 0;
16294 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
16295 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
16296 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
16297 
16298 	stats->bytes = 0;
16299 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
16300 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
16301 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
16302 }
16303 
16304 static void bnxt_get_base_stats(struct net_device *dev,
16305 				struct netdev_queue_stats_rx *rx,
16306 				struct netdev_queue_stats_tx *tx)
16307 {
16308 	struct bnxt *bp = netdev_priv(dev);
16309 
16310 	rx->packets = bp->net_stats_prev.rx_packets;
16311 	rx->bytes = bp->net_stats_prev.rx_bytes;
16312 	rx->alloc_fail = bp->ring_drv_stats_prev.rx_total_oom_discards;
16313 	rx->hw_gro_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_packets;
16314 	rx->hw_gro_wire_packets = bp->ring_drv_stats_prev.rx_total_hw_gro_wire_packets;
16315 
16316 	tx->packets = bp->net_stats_prev.tx_packets;
16317 	tx->bytes = bp->net_stats_prev.tx_bytes;
16318 }
16319 
16320 static const struct netdev_stat_ops bnxt_stat_ops = {
16321 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
16322 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
16323 	.get_base_stats		= bnxt_get_base_stats,
16324 };
16325 
16326 static void bnxt_queue_default_qcfg(struct net_device *dev,
16327 				    struct netdev_queue_config *qcfg)
16328 {
16329 	qcfg->rx_page_size = BNXT_RX_PAGE_SIZE;
16330 }
16331 
16332 static int bnxt_validate_qcfg(struct net_device *dev,
16333 			      struct netdev_queue_config *qcfg,
16334 			      struct netlink_ext_ack *extack)
16335 {
16336 	struct bnxt *bp = netdev_priv(dev);
16337 
16338 	/* Older chips need MSS calc so rx_page_size is not supported */
16339 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16340 	    qcfg->rx_page_size != BNXT_RX_PAGE_SIZE)
16341 		return -EINVAL;
16342 
16343 	if (!is_power_of_2(qcfg->rx_page_size))
16344 		return -ERANGE;
16345 
16346 	if (qcfg->rx_page_size < BNXT_RX_PAGE_SIZE ||
16347 	    qcfg->rx_page_size > BNXT_MAX_RX_PAGE_SIZE)
16348 		return -ERANGE;
16349 
16350 	return 0;
16351 }
16352 
16353 static int bnxt_queue_mem_alloc(struct net_device *dev,
16354 				struct netdev_queue_config *qcfg,
16355 				void *qmem, int idx)
16356 {
16357 	struct bnxt_rx_ring_info *rxr, *clone;
16358 	struct bnxt *bp = netdev_priv(dev);
16359 	struct bnxt_ring_struct *ring;
16360 	int rc;
16361 
16362 	if (!bp->rx_ring)
16363 		return -ENETDOWN;
16364 
16365 	rxr = &bp->rx_ring[idx];
16366 	clone = qmem;
16367 	memcpy(clone, rxr, sizeof(*rxr));
16368 	bnxt_init_rx_ring_struct(bp, clone);
16369 	bnxt_reset_rx_ring_struct(bp, clone);
16370 
16371 	clone->rx_prod = 0;
16372 	clone->rx_agg_prod = 0;
16373 	clone->rx_sw_agg_prod = 0;
16374 	clone->rx_next_cons = 0;
16375 	clone->need_head_pool = false;
16376 	clone->rx_page_size = qcfg->rx_page_size;
16377 	clone->rx_agg_bmap = NULL;
16378 	clone->rx_tpa = NULL;
16379 	clone->rx_tpa_idx_map = NULL;
16380 
16381 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
16382 	if (rc)
16383 		return rc;
16384 
16385 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
16386 	if (rc < 0)
16387 		goto err_page_pool_destroy;
16388 
16389 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
16390 					MEM_TYPE_PAGE_POOL,
16391 					clone->page_pool);
16392 	if (rc)
16393 		goto err_rxq_info_unreg;
16394 
16395 	ring = &clone->rx_ring_struct;
16396 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16397 	if (rc)
16398 		goto err_free_rx_ring;
16399 
16400 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
16401 		ring = &clone->rx_agg_ring_struct;
16402 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
16403 		if (rc)
16404 			goto err_free_rx_agg_ring;
16405 
16406 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
16407 		if (rc)
16408 			goto err_free_rx_agg_ring;
16409 	}
16410 
16411 	if (bp->flags & BNXT_FLAG_TPA) {
16412 		rc = bnxt_alloc_one_tpa_info(bp, clone);
16413 		if (rc)
16414 			goto err_free_tpa_info;
16415 	}
16416 
16417 	bnxt_init_one_rx_ring_rxbd(bp, clone);
16418 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
16419 
16420 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
16421 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16422 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
16423 	if (bp->flags & BNXT_FLAG_TPA) {
16424 		rc = bnxt_alloc_one_tpa_info_data(bp, clone);
16425 		if (rc)
16426 			goto err_free_rx_ring_skbs;
16427 	}
16428 
16429 	return 0;
16430 
16431 err_free_rx_ring_skbs:
16432 	bnxt_free_one_rx_ring_skbs(bp, clone);
16433 err_free_tpa_info:
16434 	bnxt_free_one_tpa_info(bp, clone);
16435 err_free_rx_agg_ring:
16436 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
16437 	kfree(clone->rx_agg_bmap);
16438 	clone->rx_agg_bmap = NULL;
16439 err_free_rx_ring:
16440 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
16441 err_rxq_info_unreg:
16442 	xdp_rxq_info_unreg(&clone->xdp_rxq);
16443 err_page_pool_destroy:
16444 	page_pool_destroy(clone->page_pool);
16445 	page_pool_destroy(clone->head_pool);
16446 	clone->page_pool = NULL;
16447 	clone->head_pool = NULL;
16448 	return rc;
16449 }
16450 
16451 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
16452 {
16453 	struct bnxt_rx_ring_info *rxr = qmem;
16454 	struct bnxt *bp = netdev_priv(dev);
16455 	struct bnxt_ring_struct *ring;
16456 
16457 	bnxt_free_one_rx_ring_skbs(bp, rxr);
16458 	bnxt_free_one_tpa_info(bp, rxr);
16459 
16460 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
16461 
16462 	page_pool_destroy(rxr->page_pool);
16463 	page_pool_destroy(rxr->head_pool);
16464 	rxr->page_pool = NULL;
16465 	rxr->head_pool = NULL;
16466 
16467 	ring = &rxr->rx_ring_struct;
16468 	bnxt_free_ring(bp, &ring->ring_mem);
16469 
16470 	ring = &rxr->rx_agg_ring_struct;
16471 	bnxt_free_ring(bp, &ring->ring_mem);
16472 
16473 	kfree(rxr->rx_agg_bmap);
16474 	rxr->rx_agg_bmap = NULL;
16475 }
16476 
16477 static void bnxt_copy_rx_ring(struct bnxt *bp,
16478 			      struct bnxt_rx_ring_info *dst,
16479 			      struct bnxt_rx_ring_info *src)
16480 {
16481 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
16482 	struct bnxt_ring_struct *dst_ring, *src_ring;
16483 	int i;
16484 
16485 	dst_ring = &dst->rx_ring_struct;
16486 	dst_rmem = &dst_ring->ring_mem;
16487 	src_ring = &src->rx_ring_struct;
16488 	src_rmem = &src_ring->ring_mem;
16489 
16490 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16491 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16492 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16493 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16494 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16495 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16496 
16497 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16498 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16499 	*dst_rmem->vmem = *src_rmem->vmem;
16500 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16501 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16502 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16503 	}
16504 
16505 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
16506 		return;
16507 
16508 	dst_ring = &dst->rx_agg_ring_struct;
16509 	dst_rmem = &dst_ring->ring_mem;
16510 	src_ring = &src->rx_agg_ring_struct;
16511 	src_rmem = &src_ring->ring_mem;
16512 
16513 	dst->rx_page_size = src->rx_page_size;
16514 
16515 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16516 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16517 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16518 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16519 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16520 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16521 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16522 
16523 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16524 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16525 	*dst_rmem->vmem = *src_rmem->vmem;
16526 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16527 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16528 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16529 	}
16530 
16531 	dst->rx_agg_bmap = src->rx_agg_bmap;
16532 }
16533 
16534 static int bnxt_queue_start(struct net_device *dev,
16535 			    struct netdev_queue_config *qcfg,
16536 			    void *qmem, int idx)
16537 {
16538 	struct bnxt *bp = netdev_priv(dev);
16539 	struct bnxt_rx_ring_info *rxr, *clone;
16540 	struct bnxt_cp_ring_info *cpr;
16541 	struct bnxt_vnic_info *vnic;
16542 	struct bnxt_napi *bnapi;
16543 	int i, rc;
16544 	u16 mru;
16545 
16546 	rxr = &bp->rx_ring[idx];
16547 	clone = qmem;
16548 
16549 	rxr->rx_prod = clone->rx_prod;
16550 	rxr->rx_agg_prod = clone->rx_agg_prod;
16551 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16552 	rxr->rx_next_cons = clone->rx_next_cons;
16553 	rxr->rx_tpa = clone->rx_tpa;
16554 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16555 	rxr->page_pool = clone->page_pool;
16556 	rxr->head_pool = clone->head_pool;
16557 	rxr->xdp_rxq = clone->xdp_rxq;
16558 	rxr->need_head_pool = clone->need_head_pool;
16559 
16560 	bnxt_copy_rx_ring(bp, rxr, clone);
16561 
16562 	bnapi = rxr->bnapi;
16563 	cpr = &bnapi->cp_ring;
16564 
16565 	/* All rings have been reserved and previously allocated.
16566 	 * Reallocating with the same parameters should never fail.
16567 	 */
16568 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16569 	if (rc)
16570 		goto err_reset;
16571 
16572 	if (bp->tph_mode) {
16573 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16574 		if (rc)
16575 			goto err_reset;
16576 	}
16577 
16578 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16579 	if (rc)
16580 		goto err_reset;
16581 
16582 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16583 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16584 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16585 
16586 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16587 		rc = bnxt_tx_queue_start(bp, idx);
16588 		if (rc)
16589 			goto err_reset;
16590 	}
16591 
16592 	bnxt_enable_rx_page_pool(rxr);
16593 	napi_enable_locked(&bnapi->napi);
16594 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16595 
16596 	mru = bp->dev->mtu + VLAN_ETH_HLEN;
16597 	for (i = 0; i < bp->nr_vnics; i++) {
16598 		vnic = &bp->vnic_info[i];
16599 
16600 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16601 		if (rc)
16602 			return rc;
16603 	}
16604 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16605 
16606 err_reset:
16607 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16608 		   rc);
16609 	napi_enable_locked(&bnapi->napi);
16610 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16611 	netif_close(dev);
16612 	return rc;
16613 }
16614 
16615 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16616 {
16617 	struct bnxt *bp = netdev_priv(dev);
16618 	struct bnxt_rx_ring_info *rxr;
16619 	struct bnxt_cp_ring_info *cpr;
16620 	struct bnxt_vnic_info *vnic;
16621 	struct bnxt_napi *bnapi;
16622 	int i;
16623 
16624 	for (i = 0; i < bp->nr_vnics; i++) {
16625 		vnic = &bp->vnic_info[i];
16626 
16627 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16628 	}
16629 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16630 	/* Make sure NAPI sees that the VNIC is disabled */
16631 	synchronize_net();
16632 	rxr = &bp->rx_ring[idx];
16633 	bnapi = rxr->bnapi;
16634 	cpr = &bnapi->cp_ring;
16635 	cancel_work_sync(&cpr->dim.work);
16636 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16637 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16638 	page_pool_disable_direct_recycling(rxr->page_pool);
16639 	if (bnxt_separate_head_pool(rxr))
16640 		page_pool_disable_direct_recycling(rxr->head_pool);
16641 
16642 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16643 		bnxt_tx_queue_stop(bp, idx);
16644 
16645 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16646 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16647 	 * after seeing the completion.
16648 	 */
16649 	napi_disable_locked(&bnapi->napi);
16650 
16651 	if (bp->tph_mode) {
16652 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16653 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16654 	}
16655 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16656 
16657 	memcpy(qmem, rxr, sizeof(*rxr));
16658 	bnxt_init_rx_ring_struct(bp, qmem);
16659 
16660 	return 0;
16661 }
16662 
16663 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16664 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16665 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16666 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16667 	.ndo_queue_start	= bnxt_queue_start,
16668 	.ndo_queue_stop		= bnxt_queue_stop,
16669 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16670 	.ndo_validate_qcfg	= bnxt_validate_qcfg,
16671 	.supported_params	= QCFG_RX_PAGE_SIZE,
16672 };
16673 
16674 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops_unsupp = {
16675 	.ndo_default_qcfg	= bnxt_queue_default_qcfg,
16676 };
16677 
16678 static void bnxt_remove_one(struct pci_dev *pdev)
16679 {
16680 	struct net_device *dev = pci_get_drvdata(pdev);
16681 	struct bnxt *bp = netdev_priv(dev);
16682 
16683 	if (BNXT_PF(bp))
16684 		__bnxt_sriov_disable(bp);
16685 
16686 	bnxt_aux_devices_del(bp);
16687 
16688 	unregister_netdev(dev);
16689 	bnxt_ptp_clear(bp);
16690 
16691 	bnxt_aux_devices_uninit(bp);
16692 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
16693 
16694 	bnxt_free_l2_filters(bp, true);
16695 	bnxt_free_ntp_fltrs(bp, true);
16696 	WARN_ON(bp->num_rss_ctx);
16697 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16698 	/* Flush any pending tasks */
16699 	cancel_work_sync(&bp->sp_task);
16700 	cancel_delayed_work_sync(&bp->fw_reset_task);
16701 	bp->sp_event = 0;
16702 
16703 	bnxt_dl_fw_reporters_destroy(bp);
16704 	bnxt_dl_unregister(bp);
16705 	bnxt_shutdown_tc(bp);
16706 
16707 	bnxt_clear_int_mode(bp);
16708 	bnxt_free_ring_cpu_masks(bp);
16709 	bnxt_hwrm_func_drv_unrgtr(bp);
16710 	bnxt_free_hwrm_resources(bp);
16711 	bnxt_hwmon_uninit(bp);
16712 	bnxt_ethtool_free(bp);
16713 	bnxt_dcb_free(bp);
16714 	kfree(bp->ptp_cfg);
16715 	bp->ptp_cfg = NULL;
16716 	kfree(bp->fw_health);
16717 	bp->fw_health = NULL;
16718 	bnxt_cleanup_pci(bp);
16719 	bnxt_free_ctx_mem(bp, true);
16720 	bnxt_free_crash_dump_mem(bp);
16721 	kfree(bp->rss_indir_tbl);
16722 	bp->rss_indir_tbl = NULL;
16723 	bnxt_free_port_stats(bp);
16724 	free_netdev(dev);
16725 }
16726 
16727 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16728 {
16729 	int rc = 0;
16730 	struct bnxt_link_info *link_info = &bp->link_info;
16731 
16732 	bp->phy_flags = 0;
16733 	rc = bnxt_hwrm_phy_qcaps(bp);
16734 	if (rc) {
16735 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16736 			   rc);
16737 		return rc;
16738 	}
16739 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16740 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16741 	else
16742 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16743 
16744 	bp->mac_flags = 0;
16745 	bnxt_hwrm_mac_qcaps(bp);
16746 
16747 	if (!fw_dflt)
16748 		return 0;
16749 
16750 	mutex_lock(&bp->link_lock);
16751 	rc = bnxt_update_link(bp, false);
16752 	if (rc) {
16753 		mutex_unlock(&bp->link_lock);
16754 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16755 			   rc);
16756 		return rc;
16757 	}
16758 
16759 	/* Older firmware does not have supported_auto_speeds, so assume
16760 	 * that all supported speeds can be autonegotiated.
16761 	 */
16762 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16763 		link_info->support_auto_speeds = link_info->support_speeds;
16764 
16765 	bnxt_init_ethtool_link_settings(bp);
16766 	mutex_unlock(&bp->link_lock);
16767 	return 0;
16768 }
16769 
16770 static int bnxt_get_max_irq(struct pci_dev *pdev)
16771 {
16772 	u16 ctrl;
16773 
16774 	if (!pdev->msix_cap)
16775 		return 1;
16776 
16777 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16778 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16779 }
16780 
16781 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16782 				int *max_cp)
16783 {
16784 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16785 	int max_ring_grps = 0, max_irq;
16786 
16787 	*max_tx = hw_resc->max_tx_rings;
16788 	*max_rx = hw_resc->max_rx_rings;
16789 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16790 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16791 			bnxt_get_ulp_msix_num_in_use(bp),
16792 			hw_resc->max_stat_ctxs -
16793 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16794 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16795 		*max_cp = min_t(int, *max_cp, max_irq);
16796 	max_ring_grps = hw_resc->max_hw_ring_grps;
16797 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16798 		*max_cp -= 1;
16799 		*max_rx -= 2;
16800 	}
16801 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16802 		*max_rx >>= 1;
16803 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16804 		int rc;
16805 
16806 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16807 		if (rc) {
16808 			*max_rx = 0;
16809 			*max_tx = 0;
16810 		}
16811 		/* On P5 chips, max_cp output param should be available NQs */
16812 		*max_cp = max_irq;
16813 	}
16814 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16815 }
16816 
16817 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16818 {
16819 	int rx, tx, cp;
16820 
16821 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16822 	*max_rx = rx;
16823 	*max_tx = tx;
16824 	if (!rx || !tx || !cp)
16825 		return -ENOMEM;
16826 
16827 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16828 }
16829 
16830 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16831 			       bool shared)
16832 {
16833 	int rc;
16834 
16835 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16836 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16837 		/* Not enough rings, try disabling agg rings. */
16838 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16839 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16840 		if (rc) {
16841 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16842 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16843 			return rc;
16844 		}
16845 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16846 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16847 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16848 		bnxt_set_ring_params(bp);
16849 	}
16850 
16851 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16852 		int max_cp, max_stat, max_irq;
16853 
16854 		/* Reserve minimum resources for RoCE */
16855 		max_cp = bnxt_get_max_func_cp_rings(bp);
16856 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16857 		max_irq = bnxt_get_max_func_irqs(bp);
16858 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16859 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16860 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16861 			return 0;
16862 
16863 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16864 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16865 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16866 		max_cp = min_t(int, max_cp, max_irq);
16867 		max_cp = min_t(int, max_cp, max_stat);
16868 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16869 		if (rc)
16870 			rc = 0;
16871 	}
16872 	return rc;
16873 }
16874 
16875 /* In initial default shared ring setting, each shared ring must have a
16876  * RX/TX ring pair.
16877  */
16878 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16879 {
16880 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16881 	bp->rx_nr_rings = bp->cp_nr_rings;
16882 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16883 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16884 }
16885 
16886 static void bnxt_adj_dflt_rings(struct bnxt *bp, bool sh)
16887 {
16888 	if (sh)
16889 		bnxt_trim_dflt_sh_rings(bp);
16890 	else
16891 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16892 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16893 	if (sh && READ_ONCE(bp->xdp_prog)) {
16894 		bnxt_set_xdp_tx_rings(bp);
16895 		bnxt_set_cp_rings(bp, true);
16896 	}
16897 }
16898 
16899 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16900 {
16901 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16902 	int avail_msix;
16903 
16904 	if (!bnxt_can_reserve_rings(bp))
16905 		return 0;
16906 
16907 	if (sh)
16908 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16909 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16910 	/* Reduce default rings on multi-port cards so that total default
16911 	 * rings do not exceed CPU count.
16912 	 */
16913 	if (bp->port_count > 1) {
16914 		int max_rings =
16915 			max_t(int, num_online_cpus() / bp->port_count, 1);
16916 
16917 		dflt_rings = min_t(int, dflt_rings, max_rings);
16918 	}
16919 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16920 	if (rc)
16921 		return rc;
16922 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16923 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16924 
16925 	bnxt_adj_dflt_rings(bp, sh);
16926 
16927 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16928 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16929 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16930 
16931 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16932 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16933 	}
16934 
16935 	rc = __bnxt_reserve_rings(bp);
16936 	if (rc && rc != -ENODEV)
16937 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16938 
16939 	bnxt_adj_tx_rings(bp);
16940 	if (sh)
16941 		bnxt_adj_dflt_rings(bp, true);
16942 
16943 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16944 	if (bnxt_need_reserve_rings(bp)) {
16945 		rc = __bnxt_reserve_rings(bp);
16946 		if (rc && rc != -ENODEV)
16947 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16948 		bnxt_adj_tx_rings(bp);
16949 	}
16950 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16951 		bp->rx_nr_rings++;
16952 		bp->cp_nr_rings++;
16953 	}
16954 	if (rc) {
16955 		bp->tx_nr_rings = 0;
16956 		bp->rx_nr_rings = 0;
16957 	}
16958 	return rc;
16959 }
16960 
16961 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16962 {
16963 	int rc;
16964 
16965 	if (bp->tx_nr_rings)
16966 		return 0;
16967 
16968 	bnxt_ulp_irq_stop(bp);
16969 	bnxt_clear_int_mode(bp);
16970 	rc = bnxt_set_dflt_rings(bp, true);
16971 	if (rc) {
16972 		if (BNXT_VF(bp) && rc == -ENODEV)
16973 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16974 		else
16975 			netdev_err(bp->dev, "Not enough rings available.\n");
16976 		goto init_dflt_ring_err;
16977 	}
16978 	rc = bnxt_init_int_mode(bp);
16979 	if (rc)
16980 		goto init_dflt_ring_err;
16981 
16982 	bnxt_adj_tx_rings(bp);
16983 
16984 	bnxt_set_dflt_rfs(bp);
16985 
16986 init_dflt_ring_err:
16987 	bnxt_ulp_irq_restart(bp, rc);
16988 	return rc;
16989 }
16990 
16991 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16992 {
16993 	int rc;
16994 
16995 	netdev_assert_locked_ops_compat(bp->dev);
16996 	bnxt_hwrm_func_qcaps(bp);
16997 
16998 	if (netif_running(bp->dev))
16999 		__bnxt_close_nic(bp, true, false);
17000 
17001 	bnxt_ulp_irq_stop(bp);
17002 	bnxt_clear_int_mode(bp);
17003 	rc = bnxt_init_int_mode(bp);
17004 	bnxt_ulp_irq_restart(bp, rc);
17005 
17006 	if (netif_running(bp->dev)) {
17007 		if (rc)
17008 			netif_close(bp->dev);
17009 		else
17010 			rc = bnxt_open_nic(bp, true, false);
17011 	}
17012 
17013 	return rc;
17014 }
17015 
17016 static int bnxt_init_mac_addr(struct bnxt *bp)
17017 {
17018 	int rc = 0;
17019 
17020 	if (BNXT_PF(bp)) {
17021 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
17022 	} else {
17023 #ifdef CONFIG_BNXT_SRIOV
17024 		struct bnxt_vf_info *vf = &bp->vf;
17025 		bool strict_approval = true;
17026 
17027 		if (is_valid_ether_addr(vf->mac_addr)) {
17028 			/* overwrite netdev dev_addr with admin VF MAC */
17029 			eth_hw_addr_set(bp->dev, vf->mac_addr);
17030 			/* Older PF driver or firmware may not approve this
17031 			 * correctly.
17032 			 */
17033 			strict_approval = false;
17034 		} else {
17035 			eth_hw_addr_random(bp->dev);
17036 		}
17037 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
17038 #endif
17039 	}
17040 	return rc;
17041 }
17042 
17043 static void bnxt_vpd_read_info(struct bnxt *bp)
17044 {
17045 	struct pci_dev *pdev = bp->pdev;
17046 	unsigned int vpd_size, kw_len;
17047 	int pos, size;
17048 	u8 *vpd_data;
17049 
17050 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
17051 	if (IS_ERR(vpd_data)) {
17052 		pci_warn(pdev, "Unable to read VPD\n");
17053 		return;
17054 	}
17055 
17056 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17057 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
17058 	if (pos < 0)
17059 		goto read_sn;
17060 
17061 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17062 	memcpy(bp->board_partno, &vpd_data[pos], size);
17063 
17064 read_sn:
17065 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
17066 					   PCI_VPD_RO_KEYWORD_SERIALNO,
17067 					   &kw_len);
17068 	if (pos < 0)
17069 		goto exit;
17070 
17071 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
17072 	memcpy(bp->board_serialno, &vpd_data[pos], size);
17073 exit:
17074 	kfree(vpd_data);
17075 }
17076 
17077 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
17078 {
17079 	struct pci_dev *pdev = bp->pdev;
17080 	u64 qword;
17081 
17082 	qword = pci_get_dsn(pdev);
17083 	if (!qword) {
17084 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
17085 		return -EOPNOTSUPP;
17086 	}
17087 
17088 	put_unaligned_le64(qword, dsn);
17089 
17090 	bp->flags |= BNXT_FLAG_DSN_VALID;
17091 	return 0;
17092 }
17093 
17094 static int bnxt_map_db_bar(struct bnxt *bp)
17095 {
17096 	if (!bp->db_size)
17097 		return -ENODEV;
17098 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
17099 	if (!bp->bar1)
17100 		return -ENOMEM;
17101 	return 0;
17102 }
17103 
17104 void bnxt_print_device_info(struct bnxt *bp)
17105 {
17106 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
17107 		    board_info[bp->board_idx].name,
17108 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
17109 
17110 	pcie_print_link_status(bp->pdev);
17111 }
17112 
17113 static void bnxt_clear_bars(struct pci_dev *pdev)
17114 {
17115 	int off;
17116 
17117 	for (off = PCI_BASE_ADDRESS_0; off <= PCI_BASE_ADDRESS_5; off += 4)
17118 		pci_write_config_dword(pdev, off, 0);
17119 }
17120 
17121 /* Clear any pending DMA transactions from crash kernel while loading driver in
17122  * capture kernel.
17123  */
17124 static int bnxt_kdump_reset(struct pci_dev *pdev)
17125 {
17126 	int rc, i;
17127 	u16 cmd;
17128 
17129 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17130 	cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
17131 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
17132 
17133 	if (pci_save_state(pdev))
17134 		dev_warn(&pdev->dev, "Failed to save PCI state, PCI restore may be incomplete\n");
17135 
17136 	rc = pcie_flr(pdev);
17137 	if (rc)
17138 		dev_warn(&pdev->dev, "pcie_flr() failed (rc: %d), trying to continue\n",
17139 			 rc);
17140 
17141 	/* In case device is not returning CRS, wait 5 seconds longer */
17142 	for (i = 0; i < 50; i++) {
17143 		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17144 		if (!PCI_POSSIBLE_ERROR(cmd))
17145 			break;
17146 		msleep(100);
17147 	}
17148 	if (PCI_POSSIBLE_ERROR(cmd)) {
17149 		dev_err(&pdev->dev, "PCI config space inaccessible after FLR, aborting\n");
17150 		return -ENODEV;
17151 	}
17152 
17153 	bnxt_clear_bars(pdev);
17154 	pci_restore_state(pdev);
17155 	return 0;
17156 }
17157 
17158 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
17159 {
17160 	struct bnxt_hw_resc *hw_resc;
17161 	struct net_device *dev;
17162 	struct bnxt *bp;
17163 	int rc, max_irqs;
17164 
17165 	if (pci_is_bridge(pdev))
17166 		return -ENODEV;
17167 
17168 	if (!pdev->msix_cap) {
17169 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
17170 		return -ENODEV;
17171 	}
17172 
17173 	if (is_kdump_kernel()) {
17174 		rc = bnxt_kdump_reset(pdev);
17175 		if (rc)
17176 			return rc;
17177 	}
17178 
17179 	max_irqs = bnxt_get_max_irq(pdev);
17180 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
17181 				 max_irqs);
17182 	if (!dev)
17183 		return -ENOMEM;
17184 
17185 	bp = netdev_priv(dev);
17186 	bp->board_idx = ent->driver_data;
17187 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
17188 	bnxt_set_max_func_irqs(bp, max_irqs);
17189 
17190 	bp->max_irqs = max_irqs;
17191 	rc = bnxt_alloc_ring_cpu_masks(bp);
17192 	if (rc)
17193 		goto init_err_free;
17194 
17195 	if (bnxt_vf_pciid(bp->board_idx))
17196 		bp->flags |= BNXT_FLAG_VF;
17197 
17198 	/* No devlink port registration in case of a VF */
17199 	if (BNXT_PF(bp))
17200 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
17201 
17202 	rc = bnxt_init_board(pdev, dev);
17203 	if (rc < 0)
17204 		goto init_err_free;
17205 
17206 	dev->netdev_ops = &bnxt_netdev_ops;
17207 	dev->xdp_metadata_ops = &bnxt_xdp_metadata_ops;
17208 	dev->stat_ops = &bnxt_stat_ops;
17209 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
17210 	dev->ethtool_ops = &bnxt_ethtool_ops;
17211 	pci_set_drvdata(pdev, dev);
17212 
17213 	rc = bnxt_alloc_hwrm_resources(bp);
17214 	if (rc)
17215 		goto init_err_pci_clean;
17216 
17217 	mutex_init(&bp->hwrm_cmd_lock);
17218 	mutex_init(&bp->link_lock);
17219 
17220 	rc = bnxt_fw_init_one_p1(bp);
17221 	if (rc)
17222 		goto init_err_pci_clean;
17223 
17224 	if (BNXT_PF(bp))
17225 		bnxt_vpd_read_info(bp);
17226 
17227 	if (BNXT_CHIP_P5_PLUS(bp)) {
17228 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
17229 		if (BNXT_CHIP_P7(bp))
17230 			bp->flags |= BNXT_FLAG_CHIP_P7;
17231 	}
17232 
17233 	rc = bnxt_alloc_rss_indir_tbl(bp);
17234 	if (rc)
17235 		goto init_err_pci_clean;
17236 
17237 	rc = bnxt_fw_init_one_p2(bp);
17238 	if (rc)
17239 		goto init_err_pci_clean;
17240 
17241 	rc = bnxt_map_db_bar(bp);
17242 	if (rc) {
17243 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
17244 			rc);
17245 		goto init_err_pci_clean;
17246 	}
17247 
17248 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17249 			   NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17250 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17251 			   NETIF_F_GSO_IPXIP4 |
17252 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17253 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
17254 			   NETIF_F_RXCSUM | NETIF_F_GRO;
17255 	dev->hw_features |= NETIF_F_GSO_UDP_L4;
17256 
17257 	if (BNXT_SUPPORTS_TPA(bp))
17258 		dev->hw_features |= NETIF_F_LRO;
17259 
17260 	dev->hw_enc_features =
17261 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
17262 			NETIF_F_TSO | NETIF_F_TSO6 | NETIF_F_TSO_ECN |
17263 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
17264 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
17265 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
17266 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
17267 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
17268 	if (bp->flags & BNXT_FLAG_CHIP_P7)
17269 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
17270 	else
17271 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
17272 
17273 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
17274 				    NETIF_F_GSO_GRE_CSUM;
17275 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
17276 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
17277 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
17278 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
17279 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
17280 	if (BNXT_SUPPORTS_TPA(bp))
17281 		dev->hw_features |= NETIF_F_GRO_HW;
17282 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
17283 	if (dev->features & NETIF_F_GRO_HW)
17284 		dev->features &= ~NETIF_F_LRO;
17285 	dev->priv_flags |= IFF_UNICAST_FLT;
17286 
17287 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
17288 	if (!(bp->flags & BNXT_FLAG_UDP_GSO_CAP)) {
17289 		u16 max_segs = BNXT_SW_USO_MAX_SEGS;
17290 
17291 		if (bp->tso_max_segs)
17292 			max_segs = min_t(u16, max_segs, bp->tso_max_segs);
17293 		netif_set_tso_max_segs(dev, max_segs);
17294 	} else if (bp->tso_max_segs) {
17295 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
17296 	}
17297 
17298 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
17299 			    NETDEV_XDP_ACT_RX_SG;
17300 
17301 #ifdef CONFIG_BNXT_SRIOV
17302 	init_waitqueue_head(&bp->sriov_cfg_wait);
17303 #endif
17304 	if (BNXT_SUPPORTS_TPA(bp)) {
17305 		bp->gro_func = bnxt_gro_func_5730x;
17306 		if (BNXT_CHIP_P4(bp))
17307 			bp->gro_func = bnxt_gro_func_5731x;
17308 		else if (BNXT_CHIP_P5_PLUS(bp))
17309 			bp->gro_func = bnxt_gro_func_5750x;
17310 	}
17311 	if (!BNXT_CHIP_P4_PLUS(bp))
17312 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
17313 
17314 	rc = bnxt_init_mac_addr(bp);
17315 	if (rc) {
17316 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
17317 		rc = -EADDRNOTAVAIL;
17318 		goto init_err_pci_clean;
17319 	}
17320 
17321 	if (BNXT_PF(bp)) {
17322 		/* Read the adapter's DSN to use as the eswitch switch_id */
17323 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
17324 	}
17325 
17326 	/* MTU range: 60 - FW defined max */
17327 	dev->min_mtu = ETH_ZLEN;
17328 	dev->max_mtu = bp->max_mtu;
17329 
17330 	rc = bnxt_probe_phy(bp, true);
17331 	if (rc)
17332 		goto init_err_pci_clean;
17333 
17334 	hw_resc = &bp->hw_resc;
17335 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
17336 		       BNXT_L2_FLTR_MAX_FLTR;
17337 	/* Older firmware may not report these filters properly */
17338 	if (bp->max_fltr < BNXT_MAX_FLTR)
17339 		bp->max_fltr = BNXT_MAX_FLTR;
17340 	bnxt_init_l2_fltr_tbl(bp);
17341 	__bnxt_set_rx_skb_mode(bp, false);
17342 	bnxt_set_tpa_flags(bp);
17343 	bnxt_init_ring_params(bp);
17344 	bnxt_set_ring_params(bp);
17345 	mutex_init(&bp->auxdev_lock);
17346 	if (!bnxt_auxdev_id_alloc(bp))
17347 		bnxt_aux_devices_init(bp);
17348 	rc = bnxt_set_dflt_rings(bp, true);
17349 	if (rc) {
17350 		if (BNXT_VF(bp) && rc == -ENODEV) {
17351 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
17352 		} else {
17353 			netdev_err(bp->dev, "Not enough rings available.\n");
17354 			rc = -ENOMEM;
17355 		}
17356 		goto init_err_pci_clean;
17357 	}
17358 
17359 	bnxt_fw_init_one_p3(bp);
17360 
17361 	bnxt_init_dflt_coal(bp);
17362 
17363 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
17364 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
17365 
17366 	rc = bnxt_init_int_mode(bp);
17367 	if (rc)
17368 		goto init_err_pci_clean;
17369 
17370 	/* No TC has been set yet and rings may have been trimmed due to
17371 	 * limited MSIX, so we re-initialize the TX rings per TC.
17372 	 */
17373 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
17374 
17375 	if (BNXT_PF(bp)) {
17376 		if (!bnxt_pf_wq) {
17377 			bnxt_pf_wq =
17378 				create_singlethread_workqueue("bnxt_pf_wq");
17379 			if (!bnxt_pf_wq) {
17380 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
17381 				rc = -ENOMEM;
17382 				goto init_err_pci_clean;
17383 			}
17384 		}
17385 		rc = bnxt_init_tc(bp);
17386 		if (rc)
17387 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
17388 				   rc);
17389 	}
17390 
17391 	bnxt_inv_fw_health_reg(bp);
17392 	rc = bnxt_dl_register(bp);
17393 	if (rc)
17394 		goto init_err_dl;
17395 
17396 	INIT_LIST_HEAD(&bp->usr_fltr_list);
17397 
17398 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
17399 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
17400 
17401 	dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops_unsupp;
17402 	if (BNXT_SUPPORTS_QUEUE_API(bp))
17403 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
17404 	dev->netmem_tx = NETMEM_TX_DMA;
17405 
17406 	rc = register_netdev(dev);
17407 	if (rc)
17408 		goto init_err_cleanup;
17409 
17410 	bnxt_dl_fw_reporters_create(bp);
17411 
17412 	bnxt_aux_devices_add(bp);
17413 
17414 	bnxt_print_device_info(bp);
17415 
17416 	pci_save_state(pdev);
17417 
17418 	return 0;
17419 init_err_cleanup:
17420 	bnxt_aux_devices_uninit(bp);
17421 	bnxt_auxdev_id_free(bp, bp->auxdev_id);
17422 	bnxt_dl_unregister(bp);
17423 init_err_dl:
17424 	bnxt_shutdown_tc(bp);
17425 	bnxt_clear_int_mode(bp);
17426 
17427 init_err_pci_clean:
17428 	bnxt_hwrm_func_drv_unrgtr(bp);
17429 	bnxt_ptp_clear(bp);
17430 	kfree(bp->ptp_cfg);
17431 	bp->ptp_cfg = NULL;
17432 	bnxt_free_hwrm_resources(bp);
17433 	bnxt_hwmon_uninit(bp);
17434 	bnxt_ethtool_free(bp);
17435 	kfree(bp->fw_health);
17436 	bp->fw_health = NULL;
17437 	bnxt_cleanup_pci(bp);
17438 	bnxt_free_ctx_mem(bp, true);
17439 	bnxt_free_crash_dump_mem(bp);
17440 	kfree(bp->rss_indir_tbl);
17441 	bp->rss_indir_tbl = NULL;
17442 
17443 init_err_free:
17444 	bnxt_free_ring_cpu_masks(bp);
17445 	free_netdev(dev);
17446 	return rc;
17447 }
17448 
17449 static void bnxt_shutdown(struct pci_dev *pdev)
17450 {
17451 	struct net_device *dev = pci_get_drvdata(pdev);
17452 	struct bnxt *bp;
17453 
17454 	if (!dev)
17455 		return;
17456 
17457 	rtnl_lock();
17458 	netdev_lock(dev);
17459 	bp = netdev_priv(dev);
17460 	if (!bp)
17461 		goto shutdown_exit;
17462 
17463 	if (netif_running(dev))
17464 		netif_close(dev);
17465 
17466 	if (bnxt_hwrm_func_drv_unrgtr(bp)) {
17467 		pcie_flr(pdev);
17468 		goto shutdown_exit;
17469 	}
17470 	bnxt_ptp_clear(bp);
17471 	bnxt_clear_int_mode(bp);
17472 	pci_disable_device(pdev);
17473 
17474 	if (system_state == SYSTEM_POWER_OFF) {
17475 		pci_wake_from_d3(pdev, bp->wol);
17476 		pci_set_power_state(pdev, PCI_D3hot);
17477 	}
17478 
17479 shutdown_exit:
17480 	netdev_unlock(dev);
17481 	rtnl_unlock();
17482 }
17483 
17484 #ifdef CONFIG_PM_SLEEP
17485 static int bnxt_suspend(struct device *device)
17486 {
17487 	struct net_device *dev = dev_get_drvdata(device);
17488 	struct bnxt *bp = netdev_priv(dev);
17489 	int rc = 0;
17490 
17491 	bnxt_ulp_stop(bp);
17492 
17493 	netdev_lock(dev);
17494 	if (netif_running(dev)) {
17495 		netif_device_detach(dev);
17496 		rc = bnxt_close(dev);
17497 	}
17498 	bnxt_hwrm_func_drv_unrgtr(bp);
17499 	bnxt_ptp_clear(bp);
17500 	pci_disable_device(bp->pdev);
17501 	bnxt_free_ctx_mem(bp, false);
17502 	netdev_unlock(dev);
17503 	return rc;
17504 }
17505 
17506 static int bnxt_resume(struct device *device)
17507 {
17508 	struct net_device *dev = dev_get_drvdata(device);
17509 	struct bnxt *bp = netdev_priv(dev);
17510 	int rc = 0;
17511 
17512 	rtnl_lock();
17513 	netdev_lock(dev);
17514 	rc = pci_enable_device(bp->pdev);
17515 	if (rc) {
17516 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
17517 			   rc);
17518 		goto resume_exit;
17519 	}
17520 	pci_set_master(bp->pdev);
17521 	if (bnxt_hwrm_ver_get(bp)) {
17522 		rc = -ENODEV;
17523 		goto resume_exit;
17524 	}
17525 	rc = bnxt_hwrm_func_reset(bp);
17526 	if (rc) {
17527 		rc = -EBUSY;
17528 		goto resume_exit;
17529 	}
17530 
17531 	rc = bnxt_hwrm_func_qcaps(bp);
17532 	if (rc)
17533 		goto resume_exit;
17534 
17535 	bnxt_clear_reservations(bp, true);
17536 
17537 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
17538 		rc = -ENODEV;
17539 		goto resume_exit;
17540 	}
17541 	if (bp->fw_crash_mem)
17542 		bnxt_hwrm_crash_dump_mem_cfg(bp);
17543 
17544 	if (bnxt_ptp_init(bp)) {
17545 		kfree(bp->ptp_cfg);
17546 		bp->ptp_cfg = NULL;
17547 	}
17548 	bnxt_get_wol_settings(bp);
17549 	if (netif_running(dev)) {
17550 		rc = bnxt_open(dev);
17551 		if (!rc)
17552 			netif_device_attach(dev);
17553 	}
17554 
17555 resume_exit:
17556 	netdev_unlock(bp->dev);
17557 	rtnl_unlock();
17558 	if (!rc) {
17559 		bnxt_ulp_start(bp);
17560 		bnxt_reenable_sriov(bp);
17561 	}
17562 	return rc;
17563 }
17564 
17565 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
17566 #define BNXT_PM_OPS (&bnxt_pm_ops)
17567 
17568 #else
17569 
17570 #define BNXT_PM_OPS NULL
17571 
17572 #endif /* CONFIG_PM_SLEEP */
17573 
17574 /**
17575  * bnxt_io_error_detected - called when PCI error is detected
17576  * @pdev: Pointer to PCI device
17577  * @state: The current pci connection state
17578  *
17579  * This function is called after a PCI bus error affecting
17580  * this device has been detected.
17581  */
17582 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
17583 					       pci_channel_state_t state)
17584 {
17585 	struct net_device *netdev = pci_get_drvdata(pdev);
17586 	struct bnxt *bp = netdev_priv(netdev);
17587 	bool abort = false;
17588 
17589 	netdev_info(netdev, "PCI I/O error detected\n");
17590 
17591 	bnxt_ulp_stop(bp);
17592 
17593 	netdev_lock(netdev);
17594 	netif_device_detach(netdev);
17595 
17596 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
17597 		netdev_err(bp->dev, "Firmware reset already in progress\n");
17598 		abort = true;
17599 	}
17600 
17601 	if (abort || state == pci_channel_io_perm_failure) {
17602 		netdev_unlock(netdev);
17603 		return PCI_ERS_RESULT_DISCONNECT;
17604 	}
17605 
17606 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17607 	 * so we disable bus master to prevent any potential bad DMAs before
17608 	 * freeing kernel memory.
17609 	 */
17610 	if (state == pci_channel_io_frozen)
17611 		bnxt_fw_fatal_close(bp);
17612 
17613 	if (netif_running(netdev))
17614 		__bnxt_close_nic(bp, true, true);
17615 
17616 	if (pci_is_enabled(pdev))
17617 		pci_disable_device(pdev);
17618 	bnxt_free_ctx_mem(bp, false);
17619 	netdev_unlock(netdev);
17620 
17621 	/* Request a slot reset. */
17622 	return PCI_ERS_RESULT_NEED_RESET;
17623 }
17624 
17625 /**
17626  * bnxt_io_slot_reset - called after the pci bus has been reset.
17627  * @pdev: Pointer to PCI device
17628  *
17629  * Restart the card from scratch, as if from a cold-boot.
17630  * At this point, the card has experienced a hard reset,
17631  * followed by fixups by BIOS, and has its config space
17632  * set up identically to what it was at cold boot.
17633  */
17634 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17635 {
17636 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17637 	struct net_device *netdev = pci_get_drvdata(pdev);
17638 	struct bnxt *bp = netdev_priv(netdev);
17639 	int retry = 0;
17640 	int err = 0;
17641 	u16 cmd;
17642 
17643 	netdev_info(bp->dev, "PCI Slot Reset\n");
17644 
17645 	/* After a PCIe hot reset, the chip should return CRS when the
17646 	 * vendor ID config register is read until it is ready.  On all
17647 	 * chips, this is not happening reliably so add a 5-second delay
17648 	 * as a workaround.
17649 	 */
17650 	msleep(5000);
17651 
17652 	netdev_lock(netdev);
17653 
17654 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17655 	if (PCI_POSSIBLE_ERROR(cmd)) {
17656 		dev_err(&pdev->dev,
17657 			"PCI config space inaccessible after reset\n");
17658 		goto reset_exit;
17659 	}
17660 
17661 	/* Upon PCIe error, our device internal logic that latches to
17662 	 * BAR value is getting reset and will restore only upon
17663 	 * rewriting the BARs.
17664 	 *
17665 	 * As pci_restore_state() does not re-write the BARs if the
17666 	 * value is same as saved value earlier, driver needs to
17667 	 * write the BARs to 0 to force restore.
17668 	 */
17669 	pci_clear_master(pdev);
17670 	pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17671 	cmd &= ~PCI_COMMAND_MEMORY;
17672 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
17673 
17674 	bnxt_clear_bars(pdev);
17675 	pci_restore_state(pdev);
17676 
17677 	if (!pci_is_enabled(pdev) && pci_enable_device(pdev)) {
17678 		dev_err(&pdev->dev,
17679 			"Cannot re-enable PCI device after reset.\n");
17680 		pci_read_config_word(pdev, PCI_COMMAND, &cmd);
17681 		if (!PCI_POSSIBLE_ERROR(cmd)) {
17682 			cmd &= ~(PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
17683 			pci_write_config_word(pdev, PCI_COMMAND, cmd);
17684 		}
17685 		goto reset_exit;
17686 	}
17687 	pci_set_master(pdev);
17688 
17689 	bnxt_inv_fw_health_reg(bp);
17690 	bnxt_try_map_fw_health_reg(bp);
17691 
17692 	/* In some PCIe AER scenarios, firmware may take up to
17693 	 * 10 seconds to become ready in the worst case.
17694 	 */
17695 	do {
17696 		err = bnxt_try_recover_fw(bp);
17697 		if (!err)
17698 			break;
17699 		retry++;
17700 	} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17701 
17702 	if (err) {
17703 		dev_err(&pdev->dev, "Firmware not ready\n");
17704 		goto reset_exit;
17705 	}
17706 
17707 	err = bnxt_hwrm_func_reset(bp);
17708 	if (!err)
17709 		result = PCI_ERS_RESULT_RECOVERED;
17710 
17711 	/* IRQ will be initialized later in bnxt_io_resume */
17712 	bnxt_ulp_irq_stop(bp);
17713 	bnxt_clear_int_mode(bp);
17714 
17715 reset_exit:
17716 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17717 	bnxt_clear_reservations(bp, true);
17718 	netdev_unlock(netdev);
17719 
17720 	return result;
17721 }
17722 
17723 /**
17724  * bnxt_io_resume - called when traffic can start flowing again.
17725  * @pdev: Pointer to PCI device
17726  *
17727  * This callback is called when the error recovery driver tells
17728  * us that its OK to resume normal operation.
17729  */
17730 static void bnxt_io_resume(struct pci_dev *pdev)
17731 {
17732 	struct net_device *netdev = pci_get_drvdata(pdev);
17733 	struct bnxt *bp = netdev_priv(netdev);
17734 	int err;
17735 
17736 	netdev_info(bp->dev, "PCI Slot Resume\n");
17737 	rtnl_lock();
17738 	netdev_lock(netdev);
17739 
17740 	err = bnxt_hwrm_func_qcaps(bp);
17741 	if (!err) {
17742 		if (netif_running(netdev)) {
17743 			err = bnxt_open(netdev);
17744 		} else {
17745 			err = bnxt_reserve_rings(bp, true);
17746 			if (!err)
17747 				err = bnxt_init_int_mode(bp);
17748 		}
17749 	}
17750 
17751 	if (!err)
17752 		netif_device_attach(netdev);
17753 
17754 	netdev_unlock(netdev);
17755 	rtnl_unlock();
17756 	if (!err) {
17757 		bnxt_ulp_start(bp);
17758 		bnxt_reenable_sriov(bp);
17759 	}
17760 }
17761 
17762 static const struct pci_error_handlers bnxt_err_handler = {
17763 	.error_detected	= bnxt_io_error_detected,
17764 	.slot_reset	= bnxt_io_slot_reset,
17765 	.resume		= bnxt_io_resume
17766 };
17767 
17768 static struct pci_driver bnxt_pci_driver = {
17769 	.name		= DRV_MODULE_NAME,
17770 	.id_table	= bnxt_pci_tbl,
17771 	.probe		= bnxt_init_one,
17772 	.remove		= bnxt_remove_one,
17773 	.shutdown	= bnxt_shutdown,
17774 	.driver.pm	= BNXT_PM_OPS,
17775 	.err_handler	= &bnxt_err_handler,
17776 #if defined(CONFIG_BNXT_SRIOV)
17777 	.sriov_configure = bnxt_sriov_configure,
17778 #endif
17779 };
17780 
17781 static int __init bnxt_init(void)
17782 {
17783 	int err;
17784 
17785 	bnxt_debug_init();
17786 	err = pci_register_driver(&bnxt_pci_driver);
17787 	if (err) {
17788 		bnxt_debug_exit();
17789 		return err;
17790 	}
17791 
17792 	return 0;
17793 }
17794 
17795 static void __exit bnxt_exit(void)
17796 {
17797 	pci_unregister_driver(&bnxt_pci_driver);
17798 	if (bnxt_pf_wq)
17799 		destroy_workqueue(bnxt_pf_wq);
17800 	bnxt_debug_exit();
17801 }
17802 
17803 module_init(bnxt_init);
17804 module_exit(bnxt_exit);
17805