xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 59cb902371227c2cd7932a565eda97ac7e4707bf)
1 /* Broadcom NetXtreme-C/E network driver.
2  *
3  * Copyright (c) 2014-2016 Broadcom Corporation
4  * Copyright (c) 2016-2019 Broadcom Limited
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  */
10 
11 #include <linux/module.h>
12 
13 #include <linux/stringify.h>
14 #include <linux/kernel.h>
15 #include <linux/timer.h>
16 #include <linux/errno.h>
17 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/vmalloc.h>
20 #include <linux/interrupt.h>
21 #include <linux/pci.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/bitops.h>
27 #include <linux/io.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <asm/byteorder.h>
31 #include <asm/page.h>
32 #include <linux/time.h>
33 #include <linux/mii.h>
34 #include <linux/mdio.h>
35 #include <linux/if.h>
36 #include <linux/if_vlan.h>
37 #include <linux/if_bridge.h>
38 #include <linux/rtc.h>
39 #include <linux/bpf.h>
40 #include <net/gro.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/udp.h>
44 #include <net/checksum.h>
45 #include <net/ip6_checksum.h>
46 #include <net/udp_tunnel.h>
47 #include <linux/workqueue.h>
48 #include <linux/prefetch.h>
49 #include <linux/cache.h>
50 #include <linux/log2.h>
51 #include <linux/bitmap.h>
52 #include <linux/cpu_rmap.h>
53 #include <linux/cpumask.h>
54 #include <net/pkt_cls.h>
55 #include <net/page_pool/helpers.h>
56 #include <linux/align.h>
57 #include <net/netdev_lock.h>
58 #include <net/netdev_queues.h>
59 #include <net/netdev_rx_queue.h>
60 #include <linux/pci-tph.h>
61 #include <linux/bnxt/hsi.h>
62 
63 #include "bnxt.h"
64 #include "bnxt_hwrm.h"
65 #include "bnxt_ulp.h"
66 #include "bnxt_sriov.h"
67 #include "bnxt_ethtool.h"
68 #include "bnxt_dcb.h"
69 #include "bnxt_xdp.h"
70 #include "bnxt_ptp.h"
71 #include "bnxt_vfr.h"
72 #include "bnxt_tc.h"
73 #include "bnxt_devlink.h"
74 #include "bnxt_debugfs.h"
75 #include "bnxt_coredump.h"
76 #include "bnxt_hwmon.h"
77 
78 #define BNXT_TX_TIMEOUT		(5 * HZ)
79 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
80 				 NETIF_MSG_TX_ERR)
81 
82 MODULE_IMPORT_NS("NETDEV_INTERNAL");
83 MODULE_LICENSE("GPL");
84 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
85 
86 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
87 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
88 
89 #define BNXT_TX_PUSH_THRESH 164
90 
91 /* indexed by enum board_idx */
92 static const struct {
93 	char *name;
94 } board_info[] = {
95 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
96 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
97 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
98 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
99 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
100 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
101 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
102 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
103 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
104 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
105 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
106 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
108 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
109 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
110 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
111 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
112 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
113 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
114 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
115 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
116 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
117 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
118 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
119 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
120 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
121 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
122 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
123 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
124 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
127 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
129 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
130 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
131 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
132 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
133 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
134 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
135 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
136 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
137 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
138 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
139 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
140 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
141 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
142 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
143 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
144 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
145 	[NETXTREME_E_P7_VF_HV] = { "Broadcom BCM5760X Virtual Function for Hyper-V" },
146 };
147 
148 static const struct pci_device_id bnxt_pci_tbl[] = {
149 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
150 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
151 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
157 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
169 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
182 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
183 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
191 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
197 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
198 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
199 #ifdef CONFIG_BNXT_SRIOV
200 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
204 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
210 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
215 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
218 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
219 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
220 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
221 	{ PCI_VDEVICE(BROADCOM, 0x181b), .driver_data = NETXTREME_E_P7_VF_HV },
222 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
223 #endif
224 	{ 0 }
225 };
226 
227 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
228 
229 static const u16 bnxt_vf_req_snif[] = {
230 	HWRM_FUNC_CFG,
231 	HWRM_FUNC_VF_CFG,
232 	HWRM_PORT_PHY_QCFG,
233 	HWRM_CFA_L2_FILTER_ALLOC,
234 };
235 
236 static const u16 bnxt_async_events_arr[] = {
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
240 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
241 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
244 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
246 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
247 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
248 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
249 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
250 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
251 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
252 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
253 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
254 };
255 
256 const u16 bnxt_bstore_to_trace[] = {
257 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
258 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
259 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
260 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
261 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
262 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
263 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
264 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
265 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
266 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
267 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
268 };
269 
270 static struct workqueue_struct *bnxt_pf_wq;
271 
272 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
273 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
274 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
275 
276 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
277 	.ports = {
278 		.src = 0,
279 		.dst = 0,
280 	},
281 	.addrs = {
282 		.v6addrs = {
283 			.src = BNXT_IPV6_MASK_NONE,
284 			.dst = BNXT_IPV6_MASK_NONE,
285 		},
286 	},
287 };
288 
289 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
290 	.ports = {
291 		.src = cpu_to_be16(0xffff),
292 		.dst = cpu_to_be16(0xffff),
293 	},
294 	.addrs = {
295 		.v6addrs = {
296 			.src = BNXT_IPV6_MASK_ALL,
297 			.dst = BNXT_IPV6_MASK_ALL,
298 		},
299 	},
300 };
301 
302 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
303 	.ports = {
304 		.src = cpu_to_be16(0xffff),
305 		.dst = cpu_to_be16(0xffff),
306 	},
307 	.addrs = {
308 		.v4addrs = {
309 			.src = cpu_to_be32(0xffffffff),
310 			.dst = cpu_to_be32(0xffffffff),
311 		},
312 	},
313 };
314 
315 static bool bnxt_vf_pciid(enum board_idx idx)
316 {
317 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
318 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
319 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
320 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF ||
321 		idx == NETXTREME_E_P7_VF_HV);
322 }
323 
324 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
325 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
326 
327 #define BNXT_DB_CQ(db, idx)						\
328 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
329 
330 #define BNXT_DB_NQ_P5(db, idx)						\
331 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
332 		    (db)->doorbell)
333 
334 #define BNXT_DB_NQ_P7(db, idx)						\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
336 		    DB_RING_IDX(db, idx), (db)->doorbell)
337 
338 #define BNXT_DB_CQ_ARM(db, idx)						\
339 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
340 
341 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
342 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
343 		    DB_RING_IDX(db, idx), (db)->doorbell)
344 
345 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
346 {
347 	if (bp->flags & BNXT_FLAG_CHIP_P7)
348 		BNXT_DB_NQ_P7(db, idx);
349 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
350 		BNXT_DB_NQ_P5(db, idx);
351 	else
352 		BNXT_DB_CQ(db, idx);
353 }
354 
355 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
356 {
357 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
358 		BNXT_DB_NQ_ARM_P5(db, idx);
359 	else
360 		BNXT_DB_CQ_ARM(db, idx);
361 }
362 
363 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
364 {
365 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
366 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
367 			    DB_RING_IDX(db, idx), db->doorbell);
368 	else
369 		BNXT_DB_CQ(db, idx);
370 }
371 
372 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
373 {
374 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
375 		return;
376 
377 	if (BNXT_PF(bp))
378 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
379 	else
380 		schedule_delayed_work(&bp->fw_reset_task, delay);
381 }
382 
383 static void __bnxt_queue_sp_work(struct bnxt *bp)
384 {
385 	if (BNXT_PF(bp))
386 		queue_work(bnxt_pf_wq, &bp->sp_task);
387 	else
388 		schedule_work(&bp->sp_task);
389 }
390 
391 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
392 {
393 	set_bit(event, &bp->sp_event);
394 	__bnxt_queue_sp_work(bp);
395 }
396 
397 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
398 {
399 	if (!rxr->bnapi->in_reset) {
400 		rxr->bnapi->in_reset = true;
401 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
402 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
403 		else
404 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
405 		__bnxt_queue_sp_work(bp);
406 	}
407 	rxr->rx_next_cons = 0xffff;
408 }
409 
410 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
411 			  u16 curr)
412 {
413 	struct bnxt_napi *bnapi = txr->bnapi;
414 
415 	if (bnapi->tx_fault)
416 		return;
417 
418 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
419 		   txr->txq_index, txr->tx_hw_cons,
420 		   txr->tx_cons, txr->tx_prod, curr);
421 	WARN_ON_ONCE(1);
422 	bnapi->tx_fault = 1;
423 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
424 }
425 
426 const u16 bnxt_lhint_arr[] = {
427 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
428 	TX_BD_FLAGS_LHINT_512_TO_1023,
429 	TX_BD_FLAGS_LHINT_1024_TO_2047,
430 	TX_BD_FLAGS_LHINT_1024_TO_2047,
431 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
432 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
433 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
434 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
435 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
436 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
437 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
438 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
439 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
441 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
442 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
443 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
444 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
445 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
446 };
447 
448 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
449 {
450 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
451 
452 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
453 		return 0;
454 
455 	return md_dst->u.port_info.port_id;
456 }
457 
458 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
459 			     u16 prod)
460 {
461 	/* Sync BD data before updating doorbell */
462 	wmb();
463 	bnxt_db_write(bp, &txr->tx_db, prod);
464 	txr->kick_pending = 0;
465 }
466 
467 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
468 {
469 	struct bnxt *bp = netdev_priv(dev);
470 	struct tx_bd *txbd, *txbd0;
471 	struct tx_bd_ext *txbd1;
472 	struct netdev_queue *txq;
473 	int i;
474 	dma_addr_t mapping;
475 	unsigned int length, pad = 0;
476 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
477 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
478 	struct pci_dev *pdev = bp->pdev;
479 	u16 prod, last_frag, txts_prod;
480 	struct bnxt_tx_ring_info *txr;
481 	struct bnxt_sw_tx_bd *tx_buf;
482 	__le32 lflags = 0;
483 	skb_frag_t *frag;
484 
485 	i = skb_get_queue_mapping(skb);
486 	if (unlikely(i >= bp->tx_nr_rings)) {
487 		dev_kfree_skb_any(skb);
488 		dev_core_stats_tx_dropped_inc(dev);
489 		return NETDEV_TX_OK;
490 	}
491 
492 	txq = netdev_get_tx_queue(dev, i);
493 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
494 	prod = txr->tx_prod;
495 
496 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
497 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
498 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
499 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
500 		if (skb_linearize(skb)) {
501 			dev_kfree_skb_any(skb);
502 			dev_core_stats_tx_dropped_inc(dev);
503 			return NETDEV_TX_OK;
504 		}
505 	}
506 #endif
507 	free_size = bnxt_tx_avail(bp, txr);
508 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
509 		/* We must have raced with NAPI cleanup */
510 		if (net_ratelimit() && txr->kick_pending)
511 			netif_warn(bp, tx_err, dev,
512 				   "bnxt: ring busy w/ flush pending!\n");
513 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
514 					bp->tx_wake_thresh))
515 			return NETDEV_TX_BUSY;
516 	}
517 
518 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
519 		goto tx_free;
520 
521 	length = skb->len;
522 	len = skb_headlen(skb);
523 	last_frag = skb_shinfo(skb)->nr_frags;
524 
525 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
526 
527 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
528 	tx_buf->skb = skb;
529 	tx_buf->nr_frags = last_frag;
530 
531 	vlan_tag_flags = 0;
532 	cfa_action = bnxt_xmit_get_cfa_action(skb);
533 	if (skb_vlan_tag_present(skb)) {
534 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
535 				 skb_vlan_tag_get(skb);
536 		/* Currently supports 8021Q, 8021AD vlan offloads
537 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
538 		 */
539 		if (skb->vlan_proto == htons(ETH_P_8021Q))
540 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
541 	}
542 
543 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
544 	    ptp->tx_tstamp_en) {
545 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
546 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
547 			tx_buf->is_ts_pkt = 1;
548 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
549 		} else if (!skb_is_gso(skb)) {
550 			u16 seq_id, hdr_off;
551 
552 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
553 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
554 				if (vlan_tag_flags)
555 					hdr_off += VLAN_HLEN;
556 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
557 				tx_buf->is_ts_pkt = 1;
558 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
559 
560 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
561 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
562 				tx_buf->txts_prod = txts_prod;
563 			}
564 		}
565 	}
566 	if (unlikely(skb->no_fcs))
567 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
568 
569 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
570 	    skb_frags_readable(skb) && !lflags) {
571 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
572 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
573 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
574 		void __iomem *db = txr->tx_db.doorbell;
575 		void *pdata = tx_push_buf->data;
576 		u64 *end;
577 		int j, push_len;
578 
579 		/* Set COAL_NOW to be ready quickly for the next push */
580 		tx_push->tx_bd_len_flags_type =
581 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
582 					TX_BD_TYPE_LONG_TX_BD |
583 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
584 					TX_BD_FLAGS_COAL_NOW |
585 					TX_BD_FLAGS_PACKET_END |
586 					TX_BD_CNT(2));
587 
588 		if (skb->ip_summed == CHECKSUM_PARTIAL)
589 			tx_push1->tx_bd_hsize_lflags =
590 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
591 		else
592 			tx_push1->tx_bd_hsize_lflags = 0;
593 
594 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
595 		tx_push1->tx_bd_cfa_action =
596 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
597 
598 		end = pdata + length;
599 		end = PTR_ALIGN(end, 8) - 1;
600 		*end = 0;
601 
602 		skb_copy_from_linear_data(skb, pdata, len);
603 		pdata += len;
604 		for (j = 0; j < last_frag; j++) {
605 			void *fptr;
606 
607 			frag = &skb_shinfo(skb)->frags[j];
608 			fptr = skb_frag_address_safe(frag);
609 			if (!fptr)
610 				goto normal_tx;
611 
612 			memcpy(pdata, fptr, skb_frag_size(frag));
613 			pdata += skb_frag_size(frag);
614 		}
615 
616 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
617 		txbd->tx_bd_haddr = txr->data_mapping;
618 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
619 		prod = NEXT_TX(prod);
620 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
621 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
622 		memcpy(txbd, tx_push1, sizeof(*txbd));
623 		prod = NEXT_TX(prod);
624 		tx_push->doorbell =
625 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
626 				    DB_RING_IDX(&txr->tx_db, prod));
627 		WRITE_ONCE(txr->tx_prod, prod);
628 
629 		tx_buf->is_push = 1;
630 		netdev_tx_sent_queue(txq, skb->len);
631 		wmb();	/* Sync is_push and byte queue before pushing data */
632 
633 		push_len = (length + sizeof(*tx_push) + 7) / 8;
634 		if (push_len > 16) {
635 			__iowrite64_copy(db, tx_push_buf, 16);
636 			__iowrite32_copy(db + 4, tx_push_buf + 1,
637 					 (push_len - 16) << 1);
638 		} else {
639 			__iowrite64_copy(db, tx_push_buf, push_len);
640 		}
641 
642 		goto tx_done;
643 	}
644 
645 normal_tx:
646 	if (length < BNXT_MIN_PKT_SIZE) {
647 		pad = BNXT_MIN_PKT_SIZE - length;
648 		if (skb_pad(skb, pad))
649 			/* SKB already freed. */
650 			goto tx_kick_pending;
651 		length = BNXT_MIN_PKT_SIZE;
652 	}
653 
654 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
655 
656 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
657 		goto tx_free;
658 
659 	dma_unmap_addr_set(tx_buf, mapping, mapping);
660 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
661 		TX_BD_CNT(last_frag + 2);
662 
663 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
664 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
665 
666 	prod = NEXT_TX(prod);
667 	txbd1 = (struct tx_bd_ext *)
668 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
669 
670 	txbd1->tx_bd_hsize_lflags = lflags;
671 	if (skb_is_gso(skb)) {
672 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
673 		u32 hdr_len;
674 
675 		if (skb->encapsulation) {
676 			if (udp_gso)
677 				hdr_len = skb_inner_transport_offset(skb) +
678 					  sizeof(struct udphdr);
679 			else
680 				hdr_len = skb_inner_tcp_all_headers(skb);
681 		} else if (udp_gso) {
682 			hdr_len = skb_transport_offset(skb) +
683 				  sizeof(struct udphdr);
684 		} else {
685 			hdr_len = skb_tcp_all_headers(skb);
686 		}
687 
688 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
689 					TX_BD_FLAGS_T_IPID |
690 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
691 		length = skb_shinfo(skb)->gso_size;
692 		txbd1->tx_bd_mss = cpu_to_le32(length);
693 		length += hdr_len;
694 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
695 		txbd1->tx_bd_hsize_lflags |=
696 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
697 		txbd1->tx_bd_mss = 0;
698 	}
699 
700 	length >>= 9;
701 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
702 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
703 				     skb->len);
704 		i = 0;
705 		goto tx_dma_error;
706 	}
707 	flags |= bnxt_lhint_arr[length];
708 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
709 
710 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
711 	txbd1->tx_bd_cfa_action =
712 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
713 	txbd0 = txbd;
714 	for (i = 0; i < last_frag; i++) {
715 		frag = &skb_shinfo(skb)->frags[i];
716 		prod = NEXT_TX(prod);
717 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
718 
719 		len = skb_frag_size(frag);
720 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
721 					   DMA_TO_DEVICE);
722 
723 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
724 			goto tx_dma_error;
725 
726 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
727 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
728 					  mapping, mapping);
729 
730 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
731 
732 		flags = len << TX_BD_LEN_SHIFT;
733 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
734 	}
735 
736 	flags &= ~TX_BD_LEN;
737 	txbd->tx_bd_len_flags_type =
738 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
739 			    TX_BD_FLAGS_PACKET_END);
740 
741 	netdev_tx_sent_queue(txq, skb->len);
742 
743 	skb_tx_timestamp(skb);
744 
745 	prod = NEXT_TX(prod);
746 	WRITE_ONCE(txr->tx_prod, prod);
747 
748 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
749 		bnxt_txr_db_kick(bp, txr, prod);
750 	} else {
751 		if (free_size >= bp->tx_wake_thresh)
752 			txbd0->tx_bd_len_flags_type |=
753 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
754 		txr->kick_pending = 1;
755 	}
756 
757 tx_done:
758 
759 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
760 		if (netdev_xmit_more() && !tx_buf->is_push) {
761 			txbd0->tx_bd_len_flags_type &=
762 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
763 			bnxt_txr_db_kick(bp, txr, prod);
764 		}
765 
766 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
767 				   bp->tx_wake_thresh);
768 	}
769 	return NETDEV_TX_OK;
770 
771 tx_dma_error:
772 	last_frag = i;
773 
774 	/* start back at beginning and unmap skb */
775 	prod = txr->tx_prod;
776 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
777 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
778 			 skb_headlen(skb), DMA_TO_DEVICE);
779 	prod = NEXT_TX(prod);
780 
781 	/* unmap remaining mapped pages */
782 	for (i = 0; i < last_frag; i++) {
783 		prod = NEXT_TX(prod);
784 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
785 		frag = &skb_shinfo(skb)->frags[i];
786 		netmem_dma_unmap_page_attrs(&pdev->dev,
787 					    dma_unmap_addr(tx_buf, mapping),
788 					    skb_frag_size(frag),
789 					    DMA_TO_DEVICE, 0);
790 	}
791 
792 tx_free:
793 	dev_kfree_skb_any(skb);
794 tx_kick_pending:
795 	if (BNXT_TX_PTP_IS_SET(lflags)) {
796 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
797 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
798 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
799 			/* set SKB to err so PTP worker will clean up */
800 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
801 	}
802 	if (txr->kick_pending)
803 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
804 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
805 	dev_core_stats_tx_dropped_inc(dev);
806 	return NETDEV_TX_OK;
807 }
808 
809 /* Returns true if some remaining TX packets not processed. */
810 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
811 			  int budget)
812 {
813 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
814 	struct pci_dev *pdev = bp->pdev;
815 	u16 hw_cons = txr->tx_hw_cons;
816 	unsigned int tx_bytes = 0;
817 	u16 cons = txr->tx_cons;
818 	skb_frag_t *frag;
819 	int tx_pkts = 0;
820 	bool rc = false;
821 
822 	while (RING_TX(bp, cons) != hw_cons) {
823 		struct bnxt_sw_tx_bd *tx_buf;
824 		struct sk_buff *skb;
825 		bool is_ts_pkt;
826 		int j, last;
827 
828 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
829 		skb = tx_buf->skb;
830 
831 		if (unlikely(!skb)) {
832 			bnxt_sched_reset_txr(bp, txr, cons);
833 			return rc;
834 		}
835 
836 		is_ts_pkt = tx_buf->is_ts_pkt;
837 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
838 			rc = true;
839 			break;
840 		}
841 
842 		cons = NEXT_TX(cons);
843 		tx_pkts++;
844 		tx_bytes += skb->len;
845 		tx_buf->skb = NULL;
846 		tx_buf->is_ts_pkt = 0;
847 
848 		if (tx_buf->is_push) {
849 			tx_buf->is_push = 0;
850 			goto next_tx_int;
851 		}
852 
853 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
854 				 skb_headlen(skb), DMA_TO_DEVICE);
855 		last = tx_buf->nr_frags;
856 
857 		for (j = 0; j < last; j++) {
858 			frag = &skb_shinfo(skb)->frags[j];
859 			cons = NEXT_TX(cons);
860 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
861 			netmem_dma_unmap_page_attrs(&pdev->dev,
862 						    dma_unmap_addr(tx_buf,
863 								   mapping),
864 						    skb_frag_size(frag),
865 						    DMA_TO_DEVICE, 0);
866 		}
867 		if (unlikely(is_ts_pkt)) {
868 			if (BNXT_CHIP_P5(bp)) {
869 				/* PTP worker takes ownership of the skb */
870 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
871 				skb = NULL;
872 			}
873 		}
874 
875 next_tx_int:
876 		cons = NEXT_TX(cons);
877 
878 		dev_consume_skb_any(skb);
879 	}
880 
881 	WRITE_ONCE(txr->tx_cons, cons);
882 
883 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
884 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
885 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
886 
887 	return rc;
888 }
889 
890 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
891 {
892 	struct bnxt_tx_ring_info *txr;
893 	bool more = false;
894 	int i;
895 
896 	bnxt_for_each_napi_tx(i, bnapi, txr) {
897 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
898 			more |= __bnxt_tx_int(bp, txr, budget);
899 	}
900 	if (!more)
901 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
902 }
903 
904 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
905 {
906 	return rxr->need_head_pool || PAGE_SIZE > BNXT_RX_PAGE_SIZE;
907 }
908 
909 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
910 					 struct bnxt_rx_ring_info *rxr,
911 					 unsigned int *offset,
912 					 gfp_t gfp)
913 {
914 	struct page *page;
915 
916 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
917 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
918 						BNXT_RX_PAGE_SIZE);
919 	} else {
920 		page = page_pool_dev_alloc_pages(rxr->page_pool);
921 		*offset = 0;
922 	}
923 	if (!page)
924 		return NULL;
925 
926 	*mapping = page_pool_get_dma_addr(page) + *offset;
927 	return page;
928 }
929 
930 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
931 					 struct bnxt_rx_ring_info *rxr,
932 					 unsigned int *offset,
933 					 gfp_t gfp)
934 {
935 	netmem_ref netmem;
936 
937 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
938 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset, BNXT_RX_PAGE_SIZE, gfp);
939 	} else {
940 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
941 		*offset = 0;
942 	}
943 	if (!netmem)
944 		return 0;
945 
946 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
947 	return netmem;
948 }
949 
950 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
951 				       struct bnxt_rx_ring_info *rxr,
952 				       gfp_t gfp)
953 {
954 	unsigned int offset;
955 	struct page *page;
956 
957 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
958 				    bp->rx_buf_size, gfp);
959 	if (!page)
960 		return NULL;
961 
962 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
963 	return page_address(page) + offset;
964 }
965 
966 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
967 		       u16 prod, gfp_t gfp)
968 {
969 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
970 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
971 	dma_addr_t mapping;
972 
973 	if (BNXT_RX_PAGE_MODE(bp)) {
974 		unsigned int offset;
975 		struct page *page =
976 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
977 
978 		if (!page)
979 			return -ENOMEM;
980 
981 		mapping += bp->rx_dma_offset;
982 		rx_buf->data = page;
983 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
984 	} else {
985 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
986 
987 		if (!data)
988 			return -ENOMEM;
989 
990 		rx_buf->data = data;
991 		rx_buf->data_ptr = data + bp->rx_offset;
992 	}
993 	rx_buf->mapping = mapping;
994 
995 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
996 	return 0;
997 }
998 
999 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
1000 {
1001 	u16 prod = rxr->rx_prod;
1002 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1003 	struct bnxt *bp = rxr->bnapi->bp;
1004 	struct rx_bd *cons_bd, *prod_bd;
1005 
1006 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1007 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1008 
1009 	prod_rx_buf->data = data;
1010 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1011 
1012 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1013 
1014 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1015 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1016 
1017 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1018 }
1019 
1020 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1021 {
1022 	u16 next, max = rxr->rx_agg_bmap_size;
1023 
1024 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1025 	if (next >= max)
1026 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1027 	return next;
1028 }
1029 
1030 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1031 				u16 prod, gfp_t gfp)
1032 {
1033 	struct rx_bd *rxbd =
1034 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1035 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1036 	u16 sw_prod = rxr->rx_sw_agg_prod;
1037 	unsigned int offset = 0;
1038 	dma_addr_t mapping;
1039 	netmem_ref netmem;
1040 
1041 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1042 	if (!netmem)
1043 		return -ENOMEM;
1044 
1045 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1046 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1047 
1048 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1049 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1050 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1051 
1052 	rx_agg_buf->netmem = netmem;
1053 	rx_agg_buf->offset = offset;
1054 	rx_agg_buf->mapping = mapping;
1055 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1056 	rxbd->rx_bd_opaque = sw_prod;
1057 	return 0;
1058 }
1059 
1060 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1061 				       struct bnxt_cp_ring_info *cpr,
1062 				       u16 cp_cons, u16 curr)
1063 {
1064 	struct rx_agg_cmp *agg;
1065 
1066 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1067 	agg = (struct rx_agg_cmp *)
1068 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1069 	return agg;
1070 }
1071 
1072 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1073 					      struct bnxt_rx_ring_info *rxr,
1074 					      u16 agg_id, u16 curr)
1075 {
1076 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1077 
1078 	return &tpa_info->agg_arr[curr];
1079 }
1080 
1081 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1082 				   u16 start, u32 agg_bufs, bool tpa)
1083 {
1084 	struct bnxt_napi *bnapi = cpr->bnapi;
1085 	struct bnxt *bp = bnapi->bp;
1086 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1087 	u16 prod = rxr->rx_agg_prod;
1088 	u16 sw_prod = rxr->rx_sw_agg_prod;
1089 	bool p5_tpa = false;
1090 	u32 i;
1091 
1092 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1093 		p5_tpa = true;
1094 
1095 	for (i = 0; i < agg_bufs; i++) {
1096 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1097 		struct rx_agg_cmp *agg;
1098 		struct rx_bd *prod_bd;
1099 		netmem_ref netmem;
1100 		u16 cons;
1101 
1102 		if (p5_tpa)
1103 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1104 		else
1105 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1106 		cons = agg->rx_agg_cmp_opaque;
1107 		__clear_bit(cons, rxr->rx_agg_bmap);
1108 
1109 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1110 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1111 
1112 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1113 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1114 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1115 
1116 		/* It is possible for sw_prod to be equal to cons, so
1117 		 * set cons_rx_buf->netmem to 0 first.
1118 		 */
1119 		netmem = cons_rx_buf->netmem;
1120 		cons_rx_buf->netmem = 0;
1121 		prod_rx_buf->netmem = netmem;
1122 		prod_rx_buf->offset = cons_rx_buf->offset;
1123 
1124 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1125 
1126 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1127 
1128 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1129 		prod_bd->rx_bd_opaque = sw_prod;
1130 
1131 		prod = NEXT_RX_AGG(prod);
1132 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1133 	}
1134 	rxr->rx_agg_prod = prod;
1135 	rxr->rx_sw_agg_prod = sw_prod;
1136 }
1137 
1138 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1139 					      struct bnxt_rx_ring_info *rxr,
1140 					      u16 cons, void *data, u8 *data_ptr,
1141 					      dma_addr_t dma_addr,
1142 					      unsigned int offset_and_len)
1143 {
1144 	unsigned int len = offset_and_len & 0xffff;
1145 	struct page *page = data;
1146 	u16 prod = rxr->rx_prod;
1147 	struct sk_buff *skb;
1148 	int err;
1149 
1150 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1151 	if (unlikely(err)) {
1152 		bnxt_reuse_rx_data(rxr, cons, data);
1153 		return NULL;
1154 	}
1155 	dma_addr -= bp->rx_dma_offset;
1156 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1157 				bp->rx_dir);
1158 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1159 	if (!skb) {
1160 		page_pool_recycle_direct(rxr->page_pool, page);
1161 		return NULL;
1162 	}
1163 	skb_mark_for_recycle(skb);
1164 	skb_reserve(skb, bp->rx_offset);
1165 	__skb_put(skb, len);
1166 
1167 	return skb;
1168 }
1169 
1170 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1171 					struct bnxt_rx_ring_info *rxr,
1172 					u16 cons, void *data, u8 *data_ptr,
1173 					dma_addr_t dma_addr,
1174 					unsigned int offset_and_len)
1175 {
1176 	unsigned int payload = offset_and_len >> 16;
1177 	unsigned int len = offset_and_len & 0xffff;
1178 	skb_frag_t *frag;
1179 	struct page *page = data;
1180 	u16 prod = rxr->rx_prod;
1181 	struct sk_buff *skb;
1182 	int off, err;
1183 
1184 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1185 	if (unlikely(err)) {
1186 		bnxt_reuse_rx_data(rxr, cons, data);
1187 		return NULL;
1188 	}
1189 	dma_addr -= bp->rx_dma_offset;
1190 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1191 				bp->rx_dir);
1192 
1193 	if (unlikely(!payload))
1194 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1195 
1196 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1197 	if (!skb) {
1198 		page_pool_recycle_direct(rxr->page_pool, page);
1199 		return NULL;
1200 	}
1201 
1202 	skb_mark_for_recycle(skb);
1203 	off = (void *)data_ptr - page_address(page);
1204 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1205 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1206 	       payload + NET_IP_ALIGN);
1207 
1208 	frag = &skb_shinfo(skb)->frags[0];
1209 	skb_frag_size_sub(frag, payload);
1210 	skb_frag_off_add(frag, payload);
1211 	skb->data_len -= payload;
1212 	skb->tail += payload;
1213 
1214 	return skb;
1215 }
1216 
1217 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1218 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1219 				   void *data, u8 *data_ptr,
1220 				   dma_addr_t dma_addr,
1221 				   unsigned int offset_and_len)
1222 {
1223 	u16 prod = rxr->rx_prod;
1224 	struct sk_buff *skb;
1225 	int err;
1226 
1227 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1228 	if (unlikely(err)) {
1229 		bnxt_reuse_rx_data(rxr, cons, data);
1230 		return NULL;
1231 	}
1232 
1233 	skb = napi_build_skb(data, bp->rx_buf_size);
1234 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1235 				bp->rx_dir);
1236 	if (!skb) {
1237 		page_pool_free_va(rxr->head_pool, data, true);
1238 		return NULL;
1239 	}
1240 
1241 	skb_mark_for_recycle(skb);
1242 	skb_reserve(skb, bp->rx_offset);
1243 	skb_put(skb, offset_and_len & 0xffff);
1244 	return skb;
1245 }
1246 
1247 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1248 				 struct bnxt_cp_ring_info *cpr,
1249 				 u16 idx, u32 agg_bufs, bool tpa,
1250 				 struct sk_buff *skb,
1251 				 struct xdp_buff *xdp)
1252 {
1253 	struct bnxt_napi *bnapi = cpr->bnapi;
1254 	struct skb_shared_info *shinfo;
1255 	struct bnxt_rx_ring_info *rxr;
1256 	u32 i, total_frag_len = 0;
1257 	bool p5_tpa = false;
1258 	u16 prod;
1259 
1260 	rxr = bnapi->rx_ring;
1261 	prod = rxr->rx_agg_prod;
1262 
1263 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1264 		p5_tpa = true;
1265 
1266 	if (skb)
1267 		shinfo = skb_shinfo(skb);
1268 	else
1269 		shinfo = xdp_get_shared_info_from_buff(xdp);
1270 
1271 	for (i = 0; i < agg_bufs; i++) {
1272 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1273 		struct rx_agg_cmp *agg;
1274 		u16 cons, frag_len;
1275 		netmem_ref netmem;
1276 
1277 		if (p5_tpa)
1278 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1279 		else
1280 			agg = bnxt_get_agg(bp, cpr, idx, i);
1281 		cons = agg->rx_agg_cmp_opaque;
1282 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1283 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1284 
1285 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1286 		if (skb) {
1287 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1288 					       cons_rx_buf->offset,
1289 					       frag_len, BNXT_RX_PAGE_SIZE);
1290 		} else {
1291 			skb_frag_t *frag = &shinfo->frags[i];
1292 
1293 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1294 						  cons_rx_buf->offset,
1295 						  frag_len);
1296 			shinfo->nr_frags = i + 1;
1297 		}
1298 		__clear_bit(cons, rxr->rx_agg_bmap);
1299 
1300 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1301 		 * a sw_prod index that equals the cons index, so we
1302 		 * need to clear the cons entry now.
1303 		 */
1304 		netmem = cons_rx_buf->netmem;
1305 		cons_rx_buf->netmem = 0;
1306 
1307 		if (xdp && netmem_is_pfmemalloc(netmem))
1308 			xdp_buff_set_frag_pfmemalloc(xdp);
1309 
1310 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1311 			if (skb) {
1312 				skb->len -= frag_len;
1313 				skb->data_len -= frag_len;
1314 				skb->truesize -= BNXT_RX_PAGE_SIZE;
1315 			}
1316 
1317 			--shinfo->nr_frags;
1318 			cons_rx_buf->netmem = netmem;
1319 
1320 			/* Update prod since possibly some netmems have been
1321 			 * allocated already.
1322 			 */
1323 			rxr->rx_agg_prod = prod;
1324 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1325 			return 0;
1326 		}
1327 
1328 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1329 						  BNXT_RX_PAGE_SIZE);
1330 
1331 		total_frag_len += frag_len;
1332 		prod = NEXT_RX_AGG(prod);
1333 	}
1334 	rxr->rx_agg_prod = prod;
1335 	return total_frag_len;
1336 }
1337 
1338 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1339 					       struct bnxt_cp_ring_info *cpr,
1340 					       struct sk_buff *skb, u16 idx,
1341 					       u32 agg_bufs, bool tpa)
1342 {
1343 	u32 total_frag_len = 0;
1344 
1345 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1346 					       skb, NULL);
1347 	if (!total_frag_len) {
1348 		skb_mark_for_recycle(skb);
1349 		dev_kfree_skb(skb);
1350 		return NULL;
1351 	}
1352 
1353 	return skb;
1354 }
1355 
1356 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1357 				   struct bnxt_cp_ring_info *cpr,
1358 				   struct xdp_buff *xdp, u16 idx,
1359 				   u32 agg_bufs, bool tpa)
1360 {
1361 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1362 	u32 total_frag_len = 0;
1363 
1364 	if (!xdp_buff_has_frags(xdp))
1365 		shinfo->nr_frags = 0;
1366 
1367 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1368 					       NULL, xdp);
1369 	if (total_frag_len) {
1370 		xdp_buff_set_frags_flag(xdp);
1371 		shinfo->nr_frags = agg_bufs;
1372 		shinfo->xdp_frags_size = total_frag_len;
1373 	}
1374 	return total_frag_len;
1375 }
1376 
1377 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1378 			       u8 agg_bufs, u32 *raw_cons)
1379 {
1380 	u16 last;
1381 	struct rx_agg_cmp *agg;
1382 
1383 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1384 	last = RING_CMP(*raw_cons);
1385 	agg = (struct rx_agg_cmp *)
1386 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1387 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1388 }
1389 
1390 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1391 				      unsigned int len,
1392 				      dma_addr_t mapping)
1393 {
1394 	struct bnxt *bp = bnapi->bp;
1395 	struct pci_dev *pdev = bp->pdev;
1396 	struct sk_buff *skb;
1397 
1398 	skb = napi_alloc_skb(&bnapi->napi, len);
1399 	if (!skb)
1400 		return NULL;
1401 
1402 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1403 				bp->rx_dir);
1404 
1405 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1406 	       len + NET_IP_ALIGN);
1407 
1408 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1409 				   bp->rx_dir);
1410 
1411 	skb_put(skb, len);
1412 
1413 	return skb;
1414 }
1415 
1416 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1417 				     unsigned int len,
1418 				     dma_addr_t mapping)
1419 {
1420 	return bnxt_copy_data(bnapi, data, len, mapping);
1421 }
1422 
1423 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1424 				     struct xdp_buff *xdp,
1425 				     unsigned int len,
1426 				     dma_addr_t mapping)
1427 {
1428 	unsigned int metasize = 0;
1429 	u8 *data = xdp->data;
1430 	struct sk_buff *skb;
1431 
1432 	len = xdp->data_end - xdp->data_meta;
1433 	metasize = xdp->data - xdp->data_meta;
1434 	data = xdp->data_meta;
1435 
1436 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1437 	if (!skb)
1438 		return skb;
1439 
1440 	if (metasize) {
1441 		skb_metadata_set(skb, metasize);
1442 		__skb_pull(skb, metasize);
1443 	}
1444 
1445 	return skb;
1446 }
1447 
1448 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1449 			   u32 *raw_cons, void *cmp)
1450 {
1451 	struct rx_cmp *rxcmp = cmp;
1452 	u32 tmp_raw_cons = *raw_cons;
1453 	u8 cmp_type, agg_bufs = 0;
1454 
1455 	cmp_type = RX_CMP_TYPE(rxcmp);
1456 
1457 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1458 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1459 			    RX_CMP_AGG_BUFS) >>
1460 			   RX_CMP_AGG_BUFS_SHIFT;
1461 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1462 		struct rx_tpa_end_cmp *tpa_end = cmp;
1463 
1464 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1465 			return 0;
1466 
1467 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1468 	}
1469 
1470 	if (agg_bufs) {
1471 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1472 			return -EBUSY;
1473 	}
1474 	*raw_cons = tmp_raw_cons;
1475 	return 0;
1476 }
1477 
1478 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1479 {
1480 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1481 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1482 
1483 	if (test_bit(idx, map->agg_idx_bmap))
1484 		idx = find_first_zero_bit(map->agg_idx_bmap,
1485 					  BNXT_AGG_IDX_BMAP_SIZE);
1486 	__set_bit(idx, map->agg_idx_bmap);
1487 	map->agg_id_tbl[agg_id] = idx;
1488 	return idx;
1489 }
1490 
1491 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1492 {
1493 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1494 
1495 	__clear_bit(idx, map->agg_idx_bmap);
1496 }
1497 
1498 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1499 {
1500 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1501 
1502 	return map->agg_id_tbl[agg_id];
1503 }
1504 
1505 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1506 			      struct rx_tpa_start_cmp *tpa_start,
1507 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1508 {
1509 	tpa_info->cfa_code_valid = 1;
1510 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1511 	tpa_info->vlan_valid = 0;
1512 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1513 		tpa_info->vlan_valid = 1;
1514 		tpa_info->metadata =
1515 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1516 	}
1517 }
1518 
1519 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1520 				 struct rx_tpa_start_cmp *tpa_start,
1521 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1522 {
1523 	tpa_info->vlan_valid = 0;
1524 	if (TPA_START_VLAN_VALID(tpa_start)) {
1525 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1526 		u32 vlan_proto = ETH_P_8021Q;
1527 
1528 		tpa_info->vlan_valid = 1;
1529 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1530 			vlan_proto = ETH_P_8021AD;
1531 		tpa_info->metadata = vlan_proto << 16 |
1532 				     TPA_START_METADATA0_TCI(tpa_start1);
1533 	}
1534 }
1535 
1536 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1537 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1538 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1539 {
1540 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1541 	struct bnxt_tpa_info *tpa_info;
1542 	u16 cons, prod, agg_id;
1543 	struct rx_bd *prod_bd;
1544 	dma_addr_t mapping;
1545 
1546 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1547 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1548 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1549 	} else {
1550 		agg_id = TPA_START_AGG_ID(tpa_start);
1551 	}
1552 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1553 	prod = rxr->rx_prod;
1554 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1555 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1556 	tpa_info = &rxr->rx_tpa[agg_id];
1557 
1558 	if (unlikely(cons != rxr->rx_next_cons ||
1559 		     TPA_START_ERROR(tpa_start))) {
1560 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1561 			    cons, rxr->rx_next_cons,
1562 			    TPA_START_ERROR_CODE(tpa_start1));
1563 		bnxt_sched_reset_rxr(bp, rxr);
1564 		return;
1565 	}
1566 	prod_rx_buf->data = tpa_info->data;
1567 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1568 
1569 	mapping = tpa_info->mapping;
1570 	prod_rx_buf->mapping = mapping;
1571 
1572 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1573 
1574 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1575 
1576 	tpa_info->data = cons_rx_buf->data;
1577 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1578 	cons_rx_buf->data = NULL;
1579 	tpa_info->mapping = cons_rx_buf->mapping;
1580 
1581 	tpa_info->len =
1582 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1583 				RX_TPA_START_CMP_LEN_SHIFT;
1584 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1585 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1586 		tpa_info->gso_type = SKB_GSO_TCPV4;
1587 		if (TPA_START_IS_IPV6(tpa_start1))
1588 			tpa_info->gso_type = SKB_GSO_TCPV6;
1589 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1590 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1591 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1592 			tpa_info->gso_type = SKB_GSO_TCPV6;
1593 		tpa_info->rss_hash =
1594 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1595 	} else {
1596 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1597 		tpa_info->gso_type = 0;
1598 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1599 	}
1600 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1601 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1602 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1603 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1604 	else
1605 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1606 	tpa_info->agg_count = 0;
1607 
1608 	rxr->rx_prod = NEXT_RX(prod);
1609 	cons = RING_RX(bp, NEXT_RX(cons));
1610 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1611 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1612 
1613 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1614 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1615 	cons_rx_buf->data = NULL;
1616 }
1617 
1618 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1619 {
1620 	if (agg_bufs)
1621 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1622 }
1623 
1624 #ifdef CONFIG_INET
1625 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1626 {
1627 	struct udphdr *uh = NULL;
1628 
1629 	if (ip_proto == htons(ETH_P_IP)) {
1630 		struct iphdr *iph = (struct iphdr *)skb->data;
1631 
1632 		if (iph->protocol == IPPROTO_UDP)
1633 			uh = (struct udphdr *)(iph + 1);
1634 	} else {
1635 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1636 
1637 		if (iph->nexthdr == IPPROTO_UDP)
1638 			uh = (struct udphdr *)(iph + 1);
1639 	}
1640 	if (uh) {
1641 		if (uh->check)
1642 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1643 		else
1644 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1645 	}
1646 }
1647 #endif
1648 
1649 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1650 					   int payload_off, int tcp_ts,
1651 					   struct sk_buff *skb)
1652 {
1653 #ifdef CONFIG_INET
1654 	struct tcphdr *th;
1655 	int len, nw_off;
1656 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1657 	u32 hdr_info = tpa_info->hdr_info;
1658 	bool loopback = false;
1659 
1660 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1661 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1662 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1663 
1664 	/* If the packet is an internal loopback packet, the offsets will
1665 	 * have an extra 4 bytes.
1666 	 */
1667 	if (inner_mac_off == 4) {
1668 		loopback = true;
1669 	} else if (inner_mac_off > 4) {
1670 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1671 					    ETH_HLEN - 2));
1672 
1673 		/* We only support inner iPv4/ipv6.  If we don't see the
1674 		 * correct protocol ID, it must be a loopback packet where
1675 		 * the offsets are off by 4.
1676 		 */
1677 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1678 			loopback = true;
1679 	}
1680 	if (loopback) {
1681 		/* internal loopback packet, subtract all offsets by 4 */
1682 		inner_ip_off -= 4;
1683 		inner_mac_off -= 4;
1684 		outer_ip_off -= 4;
1685 	}
1686 
1687 	nw_off = inner_ip_off - ETH_HLEN;
1688 	skb_set_network_header(skb, nw_off);
1689 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1690 		struct ipv6hdr *iph = ipv6_hdr(skb);
1691 
1692 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1693 		len = skb->len - skb_transport_offset(skb);
1694 		th = tcp_hdr(skb);
1695 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1696 	} else {
1697 		struct iphdr *iph = ip_hdr(skb);
1698 
1699 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1700 		len = skb->len - skb_transport_offset(skb);
1701 		th = tcp_hdr(skb);
1702 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1703 	}
1704 
1705 	if (inner_mac_off) { /* tunnel */
1706 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1707 					    ETH_HLEN - 2));
1708 
1709 		bnxt_gro_tunnel(skb, proto);
1710 	}
1711 #endif
1712 	return skb;
1713 }
1714 
1715 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1716 					   int payload_off, int tcp_ts,
1717 					   struct sk_buff *skb)
1718 {
1719 #ifdef CONFIG_INET
1720 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1721 	u32 hdr_info = tpa_info->hdr_info;
1722 	int iphdr_len, nw_off;
1723 
1724 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1725 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1726 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1727 
1728 	nw_off = inner_ip_off - ETH_HLEN;
1729 	skb_set_network_header(skb, nw_off);
1730 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1731 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1732 	skb_set_transport_header(skb, nw_off + iphdr_len);
1733 
1734 	if (inner_mac_off) { /* tunnel */
1735 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1736 					    ETH_HLEN - 2));
1737 
1738 		bnxt_gro_tunnel(skb, proto);
1739 	}
1740 #endif
1741 	return skb;
1742 }
1743 
1744 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1745 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1746 
1747 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1748 					   int payload_off, int tcp_ts,
1749 					   struct sk_buff *skb)
1750 {
1751 #ifdef CONFIG_INET
1752 	struct tcphdr *th;
1753 	int len, nw_off, tcp_opt_len = 0;
1754 
1755 	if (tcp_ts)
1756 		tcp_opt_len = 12;
1757 
1758 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1759 		struct iphdr *iph;
1760 
1761 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1762 			 ETH_HLEN;
1763 		skb_set_network_header(skb, nw_off);
1764 		iph = ip_hdr(skb);
1765 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1766 		len = skb->len - skb_transport_offset(skb);
1767 		th = tcp_hdr(skb);
1768 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1769 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1770 		struct ipv6hdr *iph;
1771 
1772 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1773 			 ETH_HLEN;
1774 		skb_set_network_header(skb, nw_off);
1775 		iph = ipv6_hdr(skb);
1776 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1777 		len = skb->len - skb_transport_offset(skb);
1778 		th = tcp_hdr(skb);
1779 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1780 	} else {
1781 		dev_kfree_skb_any(skb);
1782 		return NULL;
1783 	}
1784 
1785 	if (nw_off) /* tunnel */
1786 		bnxt_gro_tunnel(skb, skb->protocol);
1787 #endif
1788 	return skb;
1789 }
1790 
1791 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1792 					   struct bnxt_tpa_info *tpa_info,
1793 					   struct rx_tpa_end_cmp *tpa_end,
1794 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1795 					   struct sk_buff *skb)
1796 {
1797 #ifdef CONFIG_INET
1798 	int payload_off;
1799 	u16 segs;
1800 
1801 	segs = TPA_END_TPA_SEGS(tpa_end);
1802 	if (segs == 1)
1803 		return skb;
1804 
1805 	NAPI_GRO_CB(skb)->count = segs;
1806 	skb_shinfo(skb)->gso_size =
1807 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1808 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1809 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1810 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1811 	else
1812 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1813 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1814 	if (likely(skb))
1815 		tcp_gro_complete(skb);
1816 #endif
1817 	return skb;
1818 }
1819 
1820 /* Given the cfa_code of a received packet determine which
1821  * netdev (vf-rep or PF) the packet is destined to.
1822  */
1823 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1824 {
1825 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1826 
1827 	/* if vf-rep dev is NULL, it must belong to the PF */
1828 	return dev ? dev : bp->dev;
1829 }
1830 
1831 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1832 					   struct bnxt_cp_ring_info *cpr,
1833 					   u32 *raw_cons,
1834 					   struct rx_tpa_end_cmp *tpa_end,
1835 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1836 					   u8 *event)
1837 {
1838 	struct bnxt_napi *bnapi = cpr->bnapi;
1839 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1840 	struct net_device *dev = bp->dev;
1841 	u8 *data_ptr, agg_bufs;
1842 	unsigned int len;
1843 	struct bnxt_tpa_info *tpa_info;
1844 	dma_addr_t mapping;
1845 	struct sk_buff *skb;
1846 	u16 idx = 0, agg_id;
1847 	void *data;
1848 	bool gro;
1849 
1850 	if (unlikely(bnapi->in_reset)) {
1851 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1852 
1853 		if (rc < 0)
1854 			return ERR_PTR(-EBUSY);
1855 		return NULL;
1856 	}
1857 
1858 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1859 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1860 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1861 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1862 		tpa_info = &rxr->rx_tpa[agg_id];
1863 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1864 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1865 				    agg_bufs, tpa_info->agg_count);
1866 			agg_bufs = tpa_info->agg_count;
1867 		}
1868 		tpa_info->agg_count = 0;
1869 		*event |= BNXT_AGG_EVENT;
1870 		bnxt_free_agg_idx(rxr, agg_id);
1871 		idx = agg_id;
1872 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1873 	} else {
1874 		agg_id = TPA_END_AGG_ID(tpa_end);
1875 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1876 		tpa_info = &rxr->rx_tpa[agg_id];
1877 		idx = RING_CMP(*raw_cons);
1878 		if (agg_bufs) {
1879 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1880 				return ERR_PTR(-EBUSY);
1881 
1882 			*event |= BNXT_AGG_EVENT;
1883 			idx = NEXT_CMP(idx);
1884 		}
1885 		gro = !!TPA_END_GRO(tpa_end);
1886 	}
1887 	data = tpa_info->data;
1888 	data_ptr = tpa_info->data_ptr;
1889 	prefetch(data_ptr);
1890 	len = tpa_info->len;
1891 	mapping = tpa_info->mapping;
1892 
1893 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1894 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1895 		if (agg_bufs > MAX_SKB_FRAGS)
1896 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1897 				    agg_bufs, (int)MAX_SKB_FRAGS);
1898 		return NULL;
1899 	}
1900 
1901 	if (len <= bp->rx_copybreak) {
1902 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1903 		if (!skb) {
1904 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1905 			cpr->sw_stats->rx.rx_oom_discards += 1;
1906 			return NULL;
1907 		}
1908 	} else {
1909 		u8 *new_data;
1910 		dma_addr_t new_mapping;
1911 
1912 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1913 						GFP_ATOMIC);
1914 		if (!new_data) {
1915 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1916 			cpr->sw_stats->rx.rx_oom_discards += 1;
1917 			return NULL;
1918 		}
1919 
1920 		tpa_info->data = new_data;
1921 		tpa_info->data_ptr = new_data + bp->rx_offset;
1922 		tpa_info->mapping = new_mapping;
1923 
1924 		skb = napi_build_skb(data, bp->rx_buf_size);
1925 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1926 					bp->rx_buf_use_size, bp->rx_dir);
1927 
1928 		if (!skb) {
1929 			page_pool_free_va(rxr->head_pool, data, true);
1930 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1931 			cpr->sw_stats->rx.rx_oom_discards += 1;
1932 			return NULL;
1933 		}
1934 		skb_mark_for_recycle(skb);
1935 		skb_reserve(skb, bp->rx_offset);
1936 		skb_put(skb, len);
1937 	}
1938 
1939 	if (agg_bufs) {
1940 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1941 					      true);
1942 		if (!skb) {
1943 			/* Page reuse already handled by bnxt_rx_pages(). */
1944 			cpr->sw_stats->rx.rx_oom_discards += 1;
1945 			return NULL;
1946 		}
1947 	}
1948 
1949 	if (tpa_info->cfa_code_valid)
1950 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1951 	skb->protocol = eth_type_trans(skb, dev);
1952 
1953 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1954 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1955 
1956 	if (tpa_info->vlan_valid &&
1957 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1958 		__be16 vlan_proto = htons(tpa_info->metadata >>
1959 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1960 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1961 
1962 		if (eth_type_vlan(vlan_proto)) {
1963 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1964 		} else {
1965 			dev_kfree_skb(skb);
1966 			return NULL;
1967 		}
1968 	}
1969 
1970 	skb_checksum_none_assert(skb);
1971 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1972 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1973 		skb->csum_level =
1974 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1975 	}
1976 
1977 	if (gro)
1978 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1979 
1980 	return skb;
1981 }
1982 
1983 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1984 			 struct rx_agg_cmp *rx_agg)
1985 {
1986 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1987 	struct bnxt_tpa_info *tpa_info;
1988 
1989 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1990 	tpa_info = &rxr->rx_tpa[agg_id];
1991 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1992 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1993 }
1994 
1995 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1996 			     struct sk_buff *skb)
1997 {
1998 	skb_mark_for_recycle(skb);
1999 
2000 	if (skb->dev != bp->dev) {
2001 		/* this packet belongs to a vf-rep */
2002 		bnxt_vf_rep_rx(bp, skb);
2003 		return;
2004 	}
2005 	skb_record_rx_queue(skb, bnapi->index);
2006 	napi_gro_receive(&bnapi->napi, skb);
2007 }
2008 
2009 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2010 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2011 {
2012 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2013 
2014 	if (BNXT_PTP_RX_TS_VALID(flags))
2015 		goto ts_valid;
2016 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2017 		return false;
2018 
2019 ts_valid:
2020 	*cmpl_ts = ts;
2021 	return true;
2022 }
2023 
2024 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2025 				    struct rx_cmp *rxcmp,
2026 				    struct rx_cmp_ext *rxcmp1)
2027 {
2028 	__be16 vlan_proto;
2029 	u16 vtag;
2030 
2031 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2032 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2033 		u32 meta_data;
2034 
2035 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2036 			return skb;
2037 
2038 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2039 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2040 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2041 		if (eth_type_vlan(vlan_proto))
2042 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2043 		else
2044 			goto vlan_err;
2045 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2046 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2047 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2048 
2049 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2050 				vlan_proto = htons(ETH_P_8021Q);
2051 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2052 				vlan_proto = htons(ETH_P_8021AD);
2053 			else
2054 				goto vlan_err;
2055 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2056 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2057 		}
2058 	}
2059 	return skb;
2060 vlan_err:
2061 	skb_mark_for_recycle(skb);
2062 	dev_kfree_skb(skb);
2063 	return NULL;
2064 }
2065 
2066 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2067 					   struct rx_cmp *rxcmp)
2068 {
2069 	u8 ext_op;
2070 
2071 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2072 	switch (ext_op) {
2073 	case EXT_OP_INNER_4:
2074 	case EXT_OP_OUTER_4:
2075 	case EXT_OP_INNFL_3:
2076 	case EXT_OP_OUTFL_3:
2077 		return PKT_HASH_TYPE_L4;
2078 	default:
2079 		return PKT_HASH_TYPE_L3;
2080 	}
2081 }
2082 
2083 /* returns the following:
2084  * 1       - 1 packet successfully received
2085  * 0       - successful TPA_START, packet not completed yet
2086  * -EBUSY  - completion ring does not have all the agg buffers yet
2087  * -ENOMEM - packet aborted due to out of memory
2088  * -EIO    - packet aborted due to hw error indicated in BD
2089  */
2090 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2091 		       u32 *raw_cons, u8 *event)
2092 {
2093 	struct bnxt_napi *bnapi = cpr->bnapi;
2094 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2095 	struct net_device *dev = bp->dev;
2096 	struct rx_cmp *rxcmp;
2097 	struct rx_cmp_ext *rxcmp1;
2098 	u32 tmp_raw_cons = *raw_cons;
2099 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2100 	struct skb_shared_info *sinfo;
2101 	struct bnxt_sw_rx_bd *rx_buf;
2102 	unsigned int len;
2103 	u8 *data_ptr, agg_bufs, cmp_type;
2104 	bool xdp_active = false;
2105 	dma_addr_t dma_addr;
2106 	struct sk_buff *skb;
2107 	struct xdp_buff xdp;
2108 	u32 flags, misc;
2109 	u32 cmpl_ts;
2110 	void *data;
2111 	int rc = 0;
2112 
2113 	rxcmp = (struct rx_cmp *)
2114 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2115 
2116 	cmp_type = RX_CMP_TYPE(rxcmp);
2117 
2118 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2119 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2120 		goto next_rx_no_prod_no_len;
2121 	}
2122 
2123 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2124 	cp_cons = RING_CMP(tmp_raw_cons);
2125 	rxcmp1 = (struct rx_cmp_ext *)
2126 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2127 
2128 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2129 		return -EBUSY;
2130 
2131 	/* The valid test of the entry must be done first before
2132 	 * reading any further.
2133 	 */
2134 	dma_rmb();
2135 	prod = rxr->rx_prod;
2136 
2137 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2138 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2139 		bnxt_tpa_start(bp, rxr, cmp_type,
2140 			       (struct rx_tpa_start_cmp *)rxcmp,
2141 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2142 
2143 		*event |= BNXT_RX_EVENT;
2144 		goto next_rx_no_prod_no_len;
2145 
2146 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2147 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2148 				   (struct rx_tpa_end_cmp *)rxcmp,
2149 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2150 
2151 		if (IS_ERR(skb))
2152 			return -EBUSY;
2153 
2154 		rc = -ENOMEM;
2155 		if (likely(skb)) {
2156 			bnxt_deliver_skb(bp, bnapi, skb);
2157 			rc = 1;
2158 		}
2159 		*event |= BNXT_RX_EVENT;
2160 		goto next_rx_no_prod_no_len;
2161 	}
2162 
2163 	cons = rxcmp->rx_cmp_opaque;
2164 	if (unlikely(cons != rxr->rx_next_cons)) {
2165 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2166 
2167 		/* 0xffff is forced error, don't print it */
2168 		if (rxr->rx_next_cons != 0xffff)
2169 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2170 				    cons, rxr->rx_next_cons);
2171 		bnxt_sched_reset_rxr(bp, rxr);
2172 		if (rc1)
2173 			return rc1;
2174 		goto next_rx_no_prod_no_len;
2175 	}
2176 	rx_buf = &rxr->rx_buf_ring[cons];
2177 	data = rx_buf->data;
2178 	data_ptr = rx_buf->data_ptr;
2179 	prefetch(data_ptr);
2180 
2181 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2182 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2183 
2184 	if (agg_bufs) {
2185 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2186 			return -EBUSY;
2187 
2188 		cp_cons = NEXT_CMP(cp_cons);
2189 		*event |= BNXT_AGG_EVENT;
2190 	}
2191 	*event |= BNXT_RX_EVENT;
2192 
2193 	rx_buf->data = NULL;
2194 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2195 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2196 
2197 		bnxt_reuse_rx_data(rxr, cons, data);
2198 		if (agg_bufs)
2199 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2200 					       false);
2201 
2202 		rc = -EIO;
2203 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2204 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2205 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2206 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2207 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2208 						 rx_err);
2209 				bnxt_sched_reset_rxr(bp, rxr);
2210 			}
2211 		}
2212 		goto next_rx_no_len;
2213 	}
2214 
2215 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2216 	len = flags >> RX_CMP_LEN_SHIFT;
2217 	dma_addr = rx_buf->mapping;
2218 
2219 	if (bnxt_xdp_attached(bp, rxr)) {
2220 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2221 		if (agg_bufs) {
2222 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp,
2223 							       cp_cons,
2224 							       agg_bufs,
2225 							       false);
2226 			if (!frag_len)
2227 				goto oom_next_rx;
2228 
2229 		}
2230 		xdp_active = true;
2231 	}
2232 
2233 	if (xdp_active) {
2234 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2235 			rc = 1;
2236 			goto next_rx;
2237 		}
2238 		if (xdp_buff_has_frags(&xdp)) {
2239 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2240 			agg_bufs = sinfo->nr_frags;
2241 		} else {
2242 			agg_bufs = 0;
2243 		}
2244 	}
2245 
2246 	if (len <= bp->rx_copybreak) {
2247 		if (!xdp_active)
2248 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2249 		else
2250 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2251 		bnxt_reuse_rx_data(rxr, cons, data);
2252 		if (!skb) {
2253 			if (agg_bufs) {
2254 				if (!xdp_active)
2255 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2256 							       agg_bufs, false);
2257 				else
2258 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2259 			}
2260 			goto oom_next_rx;
2261 		}
2262 	} else {
2263 		u32 payload;
2264 
2265 		if (rx_buf->data_ptr == data_ptr)
2266 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2267 		else
2268 			payload = 0;
2269 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2270 				      payload | len);
2271 		if (!skb)
2272 			goto oom_next_rx;
2273 	}
2274 
2275 	if (agg_bufs) {
2276 		if (!xdp_active) {
2277 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2278 						      agg_bufs, false);
2279 			if (!skb)
2280 				goto oom_next_rx;
2281 		} else {
2282 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2283 						 rxr->page_pool, &xdp);
2284 			if (!skb) {
2285 				/* we should be able to free the old skb here */
2286 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2287 				goto oom_next_rx;
2288 			}
2289 		}
2290 	}
2291 
2292 	if (RX_CMP_HASH_VALID(rxcmp)) {
2293 		enum pkt_hash_types type;
2294 
2295 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2296 			type = bnxt_rss_ext_op(bp, rxcmp);
2297 		} else {
2298 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2299 
2300 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2301 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2302 				type = PKT_HASH_TYPE_L4;
2303 			else
2304 				type = PKT_HASH_TYPE_L3;
2305 		}
2306 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2307 	}
2308 
2309 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2310 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2311 	skb->protocol = eth_type_trans(skb, dev);
2312 
2313 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2314 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2315 		if (!skb)
2316 			goto next_rx;
2317 	}
2318 
2319 	skb_checksum_none_assert(skb);
2320 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2321 		if (dev->features & NETIF_F_RXCSUM) {
2322 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2323 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2324 		}
2325 	} else {
2326 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2327 			if (dev->features & NETIF_F_RXCSUM)
2328 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2329 		}
2330 	}
2331 
2332 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2333 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2334 			u64 ns, ts;
2335 
2336 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2337 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2338 
2339 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2340 				memset(skb_hwtstamps(skb), 0,
2341 				       sizeof(*skb_hwtstamps(skb)));
2342 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2343 			}
2344 		}
2345 	}
2346 	bnxt_deliver_skb(bp, bnapi, skb);
2347 	rc = 1;
2348 
2349 next_rx:
2350 	cpr->rx_packets += 1;
2351 	cpr->rx_bytes += len;
2352 
2353 next_rx_no_len:
2354 	rxr->rx_prod = NEXT_RX(prod);
2355 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2356 
2357 next_rx_no_prod_no_len:
2358 	*raw_cons = tmp_raw_cons;
2359 
2360 	return rc;
2361 
2362 oom_next_rx:
2363 	cpr->sw_stats->rx.rx_oom_discards += 1;
2364 	rc = -ENOMEM;
2365 	goto next_rx;
2366 }
2367 
2368 /* In netpoll mode, if we are using a combined completion ring, we need to
2369  * discard the rx packets and recycle the buffers.
2370  */
2371 static int bnxt_force_rx_discard(struct bnxt *bp,
2372 				 struct bnxt_cp_ring_info *cpr,
2373 				 u32 *raw_cons, u8 *event)
2374 {
2375 	u32 tmp_raw_cons = *raw_cons;
2376 	struct rx_cmp_ext *rxcmp1;
2377 	struct rx_cmp *rxcmp;
2378 	u16 cp_cons;
2379 	u8 cmp_type;
2380 	int rc;
2381 
2382 	cp_cons = RING_CMP(tmp_raw_cons);
2383 	rxcmp = (struct rx_cmp *)
2384 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2385 
2386 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2387 	cp_cons = RING_CMP(tmp_raw_cons);
2388 	rxcmp1 = (struct rx_cmp_ext *)
2389 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2390 
2391 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2392 		return -EBUSY;
2393 
2394 	/* The valid test of the entry must be done first before
2395 	 * reading any further.
2396 	 */
2397 	dma_rmb();
2398 	cmp_type = RX_CMP_TYPE(rxcmp);
2399 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2400 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2401 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2402 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2403 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2404 		struct rx_tpa_end_cmp_ext *tpa_end1;
2405 
2406 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2407 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2408 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2409 	}
2410 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2411 	if (rc && rc != -EBUSY)
2412 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2413 	return rc;
2414 }
2415 
2416 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2417 {
2418 	struct bnxt_fw_health *fw_health = bp->fw_health;
2419 	u32 reg = fw_health->regs[reg_idx];
2420 	u32 reg_type, reg_off, val = 0;
2421 
2422 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2423 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2424 	switch (reg_type) {
2425 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2426 		pci_read_config_dword(bp->pdev, reg_off, &val);
2427 		break;
2428 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2429 		reg_off = fw_health->mapped_regs[reg_idx];
2430 		fallthrough;
2431 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2432 		val = readl(bp->bar0 + reg_off);
2433 		break;
2434 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2435 		val = readl(bp->bar1 + reg_off);
2436 		break;
2437 	}
2438 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2439 		val &= fw_health->fw_reset_inprog_reg_mask;
2440 	return val;
2441 }
2442 
2443 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2444 {
2445 	int i;
2446 
2447 	for (i = 0; i < bp->rx_nr_rings; i++) {
2448 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2449 		struct bnxt_ring_grp_info *grp_info;
2450 
2451 		grp_info = &bp->grp_info[grp_idx];
2452 		if (grp_info->agg_fw_ring_id == ring_id)
2453 			return grp_idx;
2454 	}
2455 	return INVALID_HW_RING_ID;
2456 }
2457 
2458 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2459 {
2460 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2461 
2462 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2463 		return link_info->force_link_speed2;
2464 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2465 		return link_info->force_pam4_link_speed;
2466 	return link_info->force_link_speed;
2467 }
2468 
2469 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2470 {
2471 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2472 
2473 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2474 		link_info->req_link_speed = link_info->force_link_speed2;
2475 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2476 		switch (link_info->req_link_speed) {
2477 		case BNXT_LINK_SPEED_50GB_PAM4:
2478 		case BNXT_LINK_SPEED_100GB_PAM4:
2479 		case BNXT_LINK_SPEED_200GB_PAM4:
2480 		case BNXT_LINK_SPEED_400GB_PAM4:
2481 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2482 			break;
2483 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2484 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2485 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2486 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2487 			break;
2488 		default:
2489 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2490 		}
2491 		return;
2492 	}
2493 	link_info->req_link_speed = link_info->force_link_speed;
2494 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2495 	if (link_info->force_pam4_link_speed) {
2496 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2497 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2498 	}
2499 }
2500 
2501 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2502 {
2503 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2504 
2505 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2506 		link_info->advertising = link_info->auto_link_speeds2;
2507 		return;
2508 	}
2509 	link_info->advertising = link_info->auto_link_speeds;
2510 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2511 }
2512 
2513 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2514 {
2515 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2516 
2517 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2518 		if (link_info->req_link_speed != link_info->force_link_speed2)
2519 			return true;
2520 		return false;
2521 	}
2522 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2523 	    link_info->req_link_speed != link_info->force_link_speed)
2524 		return true;
2525 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2526 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2527 		return true;
2528 	return false;
2529 }
2530 
2531 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2532 {
2533 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2534 
2535 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2536 		if (link_info->advertising != link_info->auto_link_speeds2)
2537 			return true;
2538 		return false;
2539 	}
2540 	if (link_info->advertising != link_info->auto_link_speeds ||
2541 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2542 		return true;
2543 	return false;
2544 }
2545 
2546 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2547 {
2548 	u32 flags = bp->ctx->ctx_arr[type].flags;
2549 
2550 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2551 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2552 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2553 }
2554 
2555 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2556 {
2557 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2558 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2559 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2560 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2561 	struct bnxt_bs_trace_info *bs_trace;
2562 	int last_pg;
2563 
2564 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2565 		return;
2566 
2567 	mem_size = ctxm->max_entries * ctxm->entry_size;
2568 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2569 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2570 
2571 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2572 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2573 
2574 	rmem = &ctx_pg[0].ring_mem;
2575 	bs_trace = &bp->bs_trace[trace_type];
2576 	bs_trace->ctx_type = ctxm->type;
2577 	bs_trace->trace_type = trace_type;
2578 	if (pages > MAX_CTX_PAGES) {
2579 		int last_pg_dir = rmem->nr_pages - 1;
2580 
2581 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2582 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2583 	} else {
2584 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2585 	}
2586 	bs_trace->magic_byte += magic_byte_offset;
2587 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2588 }
2589 
2590 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2591 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2592 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2593 
2594 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2595 	(((data2) &							\
2596 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2597 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2598 
2599 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2600 	((data2) &							\
2601 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2602 
2603 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2604 	(((data2) &							\
2605 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2606 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2607 
2608 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2609 	((data1) &							\
2610 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2611 
2612 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2613 	(((data1) &							\
2614 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2615 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2616 
2617 /* Return true if the workqueue has to be scheduled */
2618 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2619 {
2620 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2621 
2622 	switch (err_type) {
2623 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2624 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2625 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2626 		break;
2627 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2628 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2629 		break;
2630 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2631 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2632 		break;
2633 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2634 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2635 		char *threshold_type;
2636 		bool notify = false;
2637 		char *dir_str;
2638 
2639 		switch (type) {
2640 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2641 			threshold_type = "warning";
2642 			break;
2643 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2644 			threshold_type = "critical";
2645 			break;
2646 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2647 			threshold_type = "fatal";
2648 			break;
2649 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2650 			threshold_type = "shutdown";
2651 			break;
2652 		default:
2653 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2654 			return false;
2655 		}
2656 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2657 			dir_str = "above";
2658 			notify = true;
2659 		} else {
2660 			dir_str = "below";
2661 		}
2662 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2663 			    dir_str, threshold_type);
2664 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2665 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2666 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2667 		if (notify) {
2668 			bp->thermal_threshold_type = type;
2669 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2670 			return true;
2671 		}
2672 		return false;
2673 	}
2674 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2675 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2676 		break;
2677 	default:
2678 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2679 			   err_type);
2680 		break;
2681 	}
2682 	return false;
2683 }
2684 
2685 #define BNXT_GET_EVENT_PORT(data)	\
2686 	((data) &			\
2687 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2688 
2689 #define BNXT_EVENT_RING_TYPE(data2)	\
2690 	((data2) &			\
2691 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2692 
2693 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2694 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2695 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2696 
2697 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2698 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2699 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2700 
2701 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2702 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2703 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2704 
2705 #define BNXT_PHC_BITS	48
2706 
2707 static int bnxt_async_event_process(struct bnxt *bp,
2708 				    struct hwrm_async_event_cmpl *cmpl)
2709 {
2710 	u16 event_id = le16_to_cpu(cmpl->event_id);
2711 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2712 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2713 
2714 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2715 		   event_id, data1, data2);
2716 
2717 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2718 	switch (event_id) {
2719 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2720 		struct bnxt_link_info *link_info = &bp->link_info;
2721 
2722 		if (BNXT_VF(bp))
2723 			goto async_event_process_exit;
2724 
2725 		/* print unsupported speed warning in forced speed mode only */
2726 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2727 		    (data1 & 0x20000)) {
2728 			u16 fw_speed = bnxt_get_force_speed(link_info);
2729 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2730 
2731 			if (speed != SPEED_UNKNOWN)
2732 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2733 					    speed);
2734 		}
2735 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2736 	}
2737 		fallthrough;
2738 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2739 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2740 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2741 		fallthrough;
2742 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2743 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2744 		break;
2745 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2746 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2747 		break;
2748 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2749 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2750 
2751 		if (BNXT_VF(bp))
2752 			break;
2753 
2754 		if (bp->pf.port_id != port_id)
2755 			break;
2756 
2757 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2758 		break;
2759 	}
2760 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2761 		if (BNXT_PF(bp))
2762 			goto async_event_process_exit;
2763 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2764 		break;
2765 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2766 		char *type_str = "Solicited";
2767 
2768 		if (!bp->fw_health)
2769 			goto async_event_process_exit;
2770 
2771 		bp->fw_reset_timestamp = jiffies;
2772 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2773 		if (!bp->fw_reset_min_dsecs)
2774 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2775 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2776 		if (!bp->fw_reset_max_dsecs)
2777 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2778 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2779 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2780 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2781 			type_str = "Fatal";
2782 			bp->fw_health->fatalities++;
2783 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2784 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2785 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2786 			type_str = "Non-fatal";
2787 			bp->fw_health->survivals++;
2788 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2789 		}
2790 		netif_warn(bp, hw, bp->dev,
2791 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2792 			   type_str, data1, data2,
2793 			   bp->fw_reset_min_dsecs * 100,
2794 			   bp->fw_reset_max_dsecs * 100);
2795 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2796 		break;
2797 	}
2798 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2799 		struct bnxt_fw_health *fw_health = bp->fw_health;
2800 		char *status_desc = "healthy";
2801 		u32 status;
2802 
2803 		if (!fw_health)
2804 			goto async_event_process_exit;
2805 
2806 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2807 			fw_health->enabled = false;
2808 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2809 			break;
2810 		}
2811 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2812 		fw_health->tmr_multiplier =
2813 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2814 				     bp->current_interval * 10);
2815 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2816 		if (!fw_health->enabled)
2817 			fw_health->last_fw_heartbeat =
2818 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2819 		fw_health->last_fw_reset_cnt =
2820 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2821 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2822 		if (status != BNXT_FW_STATUS_HEALTHY)
2823 			status_desc = "unhealthy";
2824 		netif_info(bp, drv, bp->dev,
2825 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2826 			   fw_health->primary ? "primary" : "backup", status,
2827 			   status_desc, fw_health->last_fw_reset_cnt);
2828 		if (!fw_health->enabled) {
2829 			/* Make sure tmr_counter is set and visible to
2830 			 * bnxt_health_check() before setting enabled to true.
2831 			 */
2832 			smp_wmb();
2833 			fw_health->enabled = true;
2834 		}
2835 		goto async_event_process_exit;
2836 	}
2837 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2838 		netif_notice(bp, hw, bp->dev,
2839 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2840 			     data1, data2);
2841 		goto async_event_process_exit;
2842 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2843 		struct bnxt_rx_ring_info *rxr;
2844 		u16 grp_idx;
2845 
2846 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2847 			goto async_event_process_exit;
2848 
2849 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2850 			    BNXT_EVENT_RING_TYPE(data2), data1);
2851 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2852 			goto async_event_process_exit;
2853 
2854 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2855 		if (grp_idx == INVALID_HW_RING_ID) {
2856 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2857 				    data1);
2858 			goto async_event_process_exit;
2859 		}
2860 		rxr = bp->bnapi[grp_idx]->rx_ring;
2861 		bnxt_sched_reset_rxr(bp, rxr);
2862 		goto async_event_process_exit;
2863 	}
2864 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2865 		struct bnxt_fw_health *fw_health = bp->fw_health;
2866 
2867 		netif_notice(bp, hw, bp->dev,
2868 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2869 			     data1, data2);
2870 		if (fw_health) {
2871 			fw_health->echo_req_data1 = data1;
2872 			fw_health->echo_req_data2 = data2;
2873 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2874 			break;
2875 		}
2876 		goto async_event_process_exit;
2877 	}
2878 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2879 		bnxt_ptp_pps_event(bp, data1, data2);
2880 		goto async_event_process_exit;
2881 	}
2882 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2883 		if (bnxt_event_error_report(bp, data1, data2))
2884 			break;
2885 		goto async_event_process_exit;
2886 	}
2887 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2888 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2889 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2890 			if (BNXT_PTP_USE_RTC(bp)) {
2891 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2892 				unsigned long flags;
2893 				u64 ns;
2894 
2895 				if (!ptp)
2896 					goto async_event_process_exit;
2897 
2898 				bnxt_ptp_update_current_time(bp);
2899 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2900 				       BNXT_PHC_BITS) | ptp->current_time);
2901 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2902 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2903 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2904 			}
2905 			break;
2906 		}
2907 		goto async_event_process_exit;
2908 	}
2909 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2910 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2911 
2912 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2913 		goto async_event_process_exit;
2914 	}
2915 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2916 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2917 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2918 
2919 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2920 		goto async_event_process_exit;
2921 	}
2922 	default:
2923 		goto async_event_process_exit;
2924 	}
2925 	__bnxt_queue_sp_work(bp);
2926 async_event_process_exit:
2927 	bnxt_ulp_async_events(bp, cmpl);
2928 	return 0;
2929 }
2930 
2931 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2932 {
2933 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2934 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2935 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2936 				(struct hwrm_fwd_req_cmpl *)txcmp;
2937 
2938 	switch (cmpl_type) {
2939 	case CMPL_BASE_TYPE_HWRM_DONE:
2940 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2941 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2942 		break;
2943 
2944 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2945 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2946 
2947 		if ((vf_id < bp->pf.first_vf_id) ||
2948 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2949 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2950 				   vf_id);
2951 			return -EINVAL;
2952 		}
2953 
2954 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2955 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2956 		break;
2957 
2958 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2959 		bnxt_async_event_process(bp,
2960 					 (struct hwrm_async_event_cmpl *)txcmp);
2961 		break;
2962 
2963 	default:
2964 		break;
2965 	}
2966 
2967 	return 0;
2968 }
2969 
2970 static bool bnxt_vnic_is_active(struct bnxt *bp)
2971 {
2972 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2973 
2974 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2975 }
2976 
2977 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2978 {
2979 	struct bnxt_napi *bnapi = dev_instance;
2980 	struct bnxt *bp = bnapi->bp;
2981 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2982 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2983 
2984 	cpr->event_ctr++;
2985 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2986 	napi_schedule(&bnapi->napi);
2987 	return IRQ_HANDLED;
2988 }
2989 
2990 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2991 {
2992 	u32 raw_cons = cpr->cp_raw_cons;
2993 	u16 cons = RING_CMP(raw_cons);
2994 	struct tx_cmp *txcmp;
2995 
2996 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2997 
2998 	return TX_CMP_VALID(txcmp, raw_cons);
2999 }
3000 
3001 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3002 			    int budget)
3003 {
3004 	struct bnxt_napi *bnapi = cpr->bnapi;
3005 	u32 raw_cons = cpr->cp_raw_cons;
3006 	bool flush_xdp = false;
3007 	u32 cons;
3008 	int rx_pkts = 0;
3009 	u8 event = 0;
3010 	struct tx_cmp *txcmp;
3011 
3012 	cpr->has_more_work = 0;
3013 	cpr->had_work_done = 1;
3014 	while (1) {
3015 		u8 cmp_type;
3016 		int rc;
3017 
3018 		cons = RING_CMP(raw_cons);
3019 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3020 
3021 		if (!TX_CMP_VALID(txcmp, raw_cons))
3022 			break;
3023 
3024 		/* The valid test of the entry must be done first before
3025 		 * reading any further.
3026 		 */
3027 		dma_rmb();
3028 		cmp_type = TX_CMP_TYPE(txcmp);
3029 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3030 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3031 			u32 opaque = txcmp->tx_cmp_opaque;
3032 			struct bnxt_tx_ring_info *txr;
3033 			u16 tx_freed;
3034 
3035 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3036 			event |= BNXT_TX_CMP_EVENT;
3037 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3038 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3039 			else
3040 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3041 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3042 				   bp->tx_ring_mask;
3043 			/* return full budget so NAPI will complete. */
3044 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3045 				rx_pkts = budget;
3046 				raw_cons = NEXT_RAW_CMP(raw_cons);
3047 				if (budget)
3048 					cpr->has_more_work = 1;
3049 				break;
3050 			}
3051 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3052 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3053 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3054 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3055 			if (likely(budget))
3056 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3057 			else
3058 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3059 							   &event);
3060 			if (event & BNXT_REDIRECT_EVENT)
3061 				flush_xdp = true;
3062 			if (likely(rc >= 0))
3063 				rx_pkts += rc;
3064 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3065 			 * the NAPI budget.  Otherwise, we may potentially loop
3066 			 * here forever if we consistently cannot allocate
3067 			 * buffers.
3068 			 */
3069 			else if (rc == -ENOMEM && budget)
3070 				rx_pkts++;
3071 			else if (rc == -EBUSY)	/* partial completion */
3072 				break;
3073 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3074 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3075 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3076 			bnxt_hwrm_handler(bp, txcmp);
3077 		}
3078 		raw_cons = NEXT_RAW_CMP(raw_cons);
3079 
3080 		if (rx_pkts && rx_pkts == budget) {
3081 			cpr->has_more_work = 1;
3082 			break;
3083 		}
3084 	}
3085 
3086 	if (flush_xdp) {
3087 		xdp_do_flush();
3088 		event &= ~BNXT_REDIRECT_EVENT;
3089 	}
3090 
3091 	if (event & BNXT_TX_EVENT) {
3092 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3093 		u16 prod = txr->tx_prod;
3094 
3095 		/* Sync BD data before updating doorbell */
3096 		wmb();
3097 
3098 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3099 		event &= ~BNXT_TX_EVENT;
3100 	}
3101 
3102 	cpr->cp_raw_cons = raw_cons;
3103 	bnapi->events |= event;
3104 	return rx_pkts;
3105 }
3106 
3107 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3108 				  int budget)
3109 {
3110 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3111 		bnapi->tx_int(bp, bnapi, budget);
3112 
3113 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3114 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3115 
3116 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3117 		bnapi->events &= ~BNXT_RX_EVENT;
3118 	}
3119 	if (bnapi->events & BNXT_AGG_EVENT) {
3120 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3121 
3122 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3123 		bnapi->events &= ~BNXT_AGG_EVENT;
3124 	}
3125 }
3126 
3127 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3128 			  int budget)
3129 {
3130 	struct bnxt_napi *bnapi = cpr->bnapi;
3131 	int rx_pkts;
3132 
3133 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3134 
3135 	/* ACK completion ring before freeing tx ring and producing new
3136 	 * buffers in rx/agg rings to prevent overflowing the completion
3137 	 * ring.
3138 	 */
3139 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3140 
3141 	__bnxt_poll_work_done(bp, bnapi, budget);
3142 	return rx_pkts;
3143 }
3144 
3145 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3146 {
3147 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3148 	struct bnxt *bp = bnapi->bp;
3149 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3150 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3151 	struct tx_cmp *txcmp;
3152 	struct rx_cmp_ext *rxcmp1;
3153 	u32 cp_cons, tmp_raw_cons;
3154 	u32 raw_cons = cpr->cp_raw_cons;
3155 	bool flush_xdp = false;
3156 	u32 rx_pkts = 0;
3157 	u8 event = 0;
3158 
3159 	while (1) {
3160 		int rc;
3161 
3162 		cp_cons = RING_CMP(raw_cons);
3163 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3164 
3165 		if (!TX_CMP_VALID(txcmp, raw_cons))
3166 			break;
3167 
3168 		/* The valid test of the entry must be done first before
3169 		 * reading any further.
3170 		 */
3171 		dma_rmb();
3172 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3173 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3174 			cp_cons = RING_CMP(tmp_raw_cons);
3175 			rxcmp1 = (struct rx_cmp_ext *)
3176 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3177 
3178 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3179 				break;
3180 
3181 			/* force an error to recycle the buffer */
3182 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3183 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3184 
3185 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3186 			if (likely(rc == -EIO) && budget)
3187 				rx_pkts++;
3188 			else if (rc == -EBUSY)	/* partial completion */
3189 				break;
3190 			if (event & BNXT_REDIRECT_EVENT)
3191 				flush_xdp = true;
3192 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3193 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3194 			bnxt_hwrm_handler(bp, txcmp);
3195 		} else {
3196 			netdev_err(bp->dev,
3197 				   "Invalid completion received on special ring\n");
3198 		}
3199 		raw_cons = NEXT_RAW_CMP(raw_cons);
3200 
3201 		if (rx_pkts == budget)
3202 			break;
3203 	}
3204 
3205 	cpr->cp_raw_cons = raw_cons;
3206 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3207 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3208 
3209 	if (event & BNXT_AGG_EVENT)
3210 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3211 	if (flush_xdp)
3212 		xdp_do_flush();
3213 
3214 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3215 		napi_complete_done(napi, rx_pkts);
3216 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3217 	}
3218 	return rx_pkts;
3219 }
3220 
3221 static int bnxt_poll(struct napi_struct *napi, int budget)
3222 {
3223 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3224 	struct bnxt *bp = bnapi->bp;
3225 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3226 	int work_done = 0;
3227 
3228 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3229 		napi_complete(napi);
3230 		return 0;
3231 	}
3232 	while (1) {
3233 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3234 
3235 		if (work_done >= budget) {
3236 			if (!budget)
3237 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3238 			break;
3239 		}
3240 
3241 		if (!bnxt_has_work(bp, cpr)) {
3242 			if (napi_complete_done(napi, work_done))
3243 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3244 			break;
3245 		}
3246 	}
3247 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3248 		struct dim_sample dim_sample = {};
3249 
3250 		dim_update_sample(cpr->event_ctr,
3251 				  cpr->rx_packets,
3252 				  cpr->rx_bytes,
3253 				  &dim_sample);
3254 		net_dim(&cpr->dim, &dim_sample);
3255 	}
3256 	return work_done;
3257 }
3258 
3259 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3260 {
3261 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3262 	int i, work_done = 0;
3263 
3264 	for (i = 0; i < cpr->cp_ring_count; i++) {
3265 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3266 
3267 		if (cpr2->had_nqe_notify) {
3268 			work_done += __bnxt_poll_work(bp, cpr2,
3269 						      budget - work_done);
3270 			cpr->has_more_work |= cpr2->has_more_work;
3271 		}
3272 	}
3273 	return work_done;
3274 }
3275 
3276 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3277 				 u64 dbr_type, int budget)
3278 {
3279 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3280 	int i;
3281 
3282 	for (i = 0; i < cpr->cp_ring_count; i++) {
3283 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3284 		struct bnxt_db_info *db;
3285 
3286 		if (cpr2->had_work_done) {
3287 			u32 tgl = 0;
3288 
3289 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3290 				cpr2->had_nqe_notify = 0;
3291 				tgl = cpr2->toggle;
3292 			}
3293 			db = &cpr2->cp_db;
3294 			bnxt_writeq(bp,
3295 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3296 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3297 				    db->doorbell);
3298 			cpr2->had_work_done = 0;
3299 		}
3300 	}
3301 	__bnxt_poll_work_done(bp, bnapi, budget);
3302 }
3303 
3304 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3305 {
3306 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3307 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3308 	struct bnxt_cp_ring_info *cpr_rx;
3309 	u32 raw_cons = cpr->cp_raw_cons;
3310 	struct bnxt *bp = bnapi->bp;
3311 	struct nqe_cn *nqcmp;
3312 	int work_done = 0;
3313 	u32 cons;
3314 
3315 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3316 		napi_complete(napi);
3317 		return 0;
3318 	}
3319 	if (cpr->has_more_work) {
3320 		cpr->has_more_work = 0;
3321 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3322 	}
3323 	while (1) {
3324 		u16 type;
3325 
3326 		cons = RING_CMP(raw_cons);
3327 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3328 
3329 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3330 			if (cpr->has_more_work)
3331 				break;
3332 
3333 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3334 					     budget);
3335 			cpr->cp_raw_cons = raw_cons;
3336 			if (napi_complete_done(napi, work_done))
3337 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3338 						  cpr->cp_raw_cons);
3339 			goto poll_done;
3340 		}
3341 
3342 		/* The valid test of the entry must be done first before
3343 		 * reading any further.
3344 		 */
3345 		dma_rmb();
3346 
3347 		type = le16_to_cpu(nqcmp->type);
3348 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3349 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3350 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3351 			struct bnxt_cp_ring_info *cpr2;
3352 
3353 			/* No more budget for RX work */
3354 			if (budget && work_done >= budget &&
3355 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3356 				break;
3357 
3358 			idx = BNXT_NQ_HDL_IDX(idx);
3359 			cpr2 = &cpr->cp_ring_arr[idx];
3360 			cpr2->had_nqe_notify = 1;
3361 			cpr2->toggle = NQE_CN_TOGGLE(type);
3362 			work_done += __bnxt_poll_work(bp, cpr2,
3363 						      budget - work_done);
3364 			cpr->has_more_work |= cpr2->has_more_work;
3365 		} else {
3366 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3367 		}
3368 		raw_cons = NEXT_RAW_CMP(raw_cons);
3369 	}
3370 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3371 	if (raw_cons != cpr->cp_raw_cons) {
3372 		cpr->cp_raw_cons = raw_cons;
3373 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3374 	}
3375 poll_done:
3376 	cpr_rx = &cpr->cp_ring_arr[0];
3377 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3378 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3379 		struct dim_sample dim_sample = {};
3380 
3381 		dim_update_sample(cpr->event_ctr,
3382 				  cpr_rx->rx_packets,
3383 				  cpr_rx->rx_bytes,
3384 				  &dim_sample);
3385 		net_dim(&cpr->dim, &dim_sample);
3386 	}
3387 	return work_done;
3388 }
3389 
3390 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3391 				       struct bnxt_tx_ring_info *txr, int idx)
3392 {
3393 	int i, max_idx;
3394 	struct pci_dev *pdev = bp->pdev;
3395 
3396 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3397 
3398 	for (i = 0; i < max_idx;) {
3399 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3400 		struct sk_buff *skb;
3401 		int j, last;
3402 
3403 		if (idx  < bp->tx_nr_rings_xdp &&
3404 		    tx_buf->action == XDP_REDIRECT) {
3405 			dma_unmap_single(&pdev->dev,
3406 					 dma_unmap_addr(tx_buf, mapping),
3407 					 dma_unmap_len(tx_buf, len),
3408 					 DMA_TO_DEVICE);
3409 			xdp_return_frame(tx_buf->xdpf);
3410 			tx_buf->action = 0;
3411 			tx_buf->xdpf = NULL;
3412 			i++;
3413 			continue;
3414 		}
3415 
3416 		skb = tx_buf->skb;
3417 		if (!skb) {
3418 			i++;
3419 			continue;
3420 		}
3421 
3422 		tx_buf->skb = NULL;
3423 
3424 		if (tx_buf->is_push) {
3425 			dev_kfree_skb(skb);
3426 			i += 2;
3427 			continue;
3428 		}
3429 
3430 		dma_unmap_single(&pdev->dev,
3431 				 dma_unmap_addr(tx_buf, mapping),
3432 				 skb_headlen(skb),
3433 				 DMA_TO_DEVICE);
3434 
3435 		last = tx_buf->nr_frags;
3436 		i += 2;
3437 		for (j = 0; j < last; j++, i++) {
3438 			int ring_idx = i & bp->tx_ring_mask;
3439 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3440 
3441 			tx_buf = &txr->tx_buf_ring[ring_idx];
3442 			netmem_dma_unmap_page_attrs(&pdev->dev,
3443 						    dma_unmap_addr(tx_buf,
3444 								   mapping),
3445 						    skb_frag_size(frag),
3446 						    DMA_TO_DEVICE, 0);
3447 		}
3448 		dev_kfree_skb(skb);
3449 	}
3450 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3451 }
3452 
3453 static void bnxt_free_tx_skbs(struct bnxt *bp)
3454 {
3455 	int i;
3456 
3457 	if (!bp->tx_ring)
3458 		return;
3459 
3460 	for (i = 0; i < bp->tx_nr_rings; i++) {
3461 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3462 
3463 		if (!txr->tx_buf_ring)
3464 			continue;
3465 
3466 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3467 	}
3468 
3469 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3470 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3471 }
3472 
3473 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3474 {
3475 	int i, max_idx;
3476 
3477 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3478 
3479 	for (i = 0; i < max_idx; i++) {
3480 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3481 		void *data = rx_buf->data;
3482 
3483 		if (!data)
3484 			continue;
3485 
3486 		rx_buf->data = NULL;
3487 		if (BNXT_RX_PAGE_MODE(bp))
3488 			page_pool_recycle_direct(rxr->page_pool, data);
3489 		else
3490 			page_pool_free_va(rxr->head_pool, data, true);
3491 	}
3492 }
3493 
3494 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3495 {
3496 	int i, max_idx;
3497 
3498 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3499 
3500 	for (i = 0; i < max_idx; i++) {
3501 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3502 		netmem_ref netmem = rx_agg_buf->netmem;
3503 
3504 		if (!netmem)
3505 			continue;
3506 
3507 		rx_agg_buf->netmem = 0;
3508 		__clear_bit(i, rxr->rx_agg_bmap);
3509 
3510 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3511 	}
3512 }
3513 
3514 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3515 					struct bnxt_rx_ring_info *rxr)
3516 {
3517 	int i;
3518 
3519 	for (i = 0; i < bp->max_tpa; i++) {
3520 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3521 		u8 *data = tpa_info->data;
3522 
3523 		if (!data)
3524 			continue;
3525 
3526 		tpa_info->data = NULL;
3527 		page_pool_free_va(rxr->head_pool, data, false);
3528 	}
3529 }
3530 
3531 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3532 				       struct bnxt_rx_ring_info *rxr)
3533 {
3534 	struct bnxt_tpa_idx_map *map;
3535 
3536 	if (!rxr->rx_tpa)
3537 		goto skip_rx_tpa_free;
3538 
3539 	bnxt_free_one_tpa_info_data(bp, rxr);
3540 
3541 skip_rx_tpa_free:
3542 	if (!rxr->rx_buf_ring)
3543 		goto skip_rx_buf_free;
3544 
3545 	bnxt_free_one_rx_ring(bp, rxr);
3546 
3547 skip_rx_buf_free:
3548 	if (!rxr->rx_agg_ring)
3549 		goto skip_rx_agg_free;
3550 
3551 	bnxt_free_one_rx_agg_ring(bp, rxr);
3552 
3553 skip_rx_agg_free:
3554 	map = rxr->rx_tpa_idx_map;
3555 	if (map)
3556 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3557 }
3558 
3559 static void bnxt_free_rx_skbs(struct bnxt *bp)
3560 {
3561 	int i;
3562 
3563 	if (!bp->rx_ring)
3564 		return;
3565 
3566 	for (i = 0; i < bp->rx_nr_rings; i++)
3567 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3568 }
3569 
3570 static void bnxt_free_skbs(struct bnxt *bp)
3571 {
3572 	bnxt_free_tx_skbs(bp);
3573 	bnxt_free_rx_skbs(bp);
3574 }
3575 
3576 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3577 {
3578 	u8 init_val = ctxm->init_value;
3579 	u16 offset = ctxm->init_offset;
3580 	u8 *p2 = p;
3581 	int i;
3582 
3583 	if (!init_val)
3584 		return;
3585 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3586 		memset(p, init_val, len);
3587 		return;
3588 	}
3589 	for (i = 0; i < len; i += ctxm->entry_size)
3590 		*(p2 + i + offset) = init_val;
3591 }
3592 
3593 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3594 			       void *buf, size_t offset, size_t head,
3595 			       size_t tail)
3596 {
3597 	int i, head_page, start_idx, source_offset;
3598 	size_t len, rem_len, total_len, max_bytes;
3599 
3600 	head_page = head / rmem->page_size;
3601 	source_offset = head % rmem->page_size;
3602 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3603 	if (!total_len)
3604 		total_len = MAX_CTX_BYTES;
3605 	start_idx = head_page % MAX_CTX_PAGES;
3606 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3607 		    source_offset;
3608 	total_len = min(total_len, max_bytes);
3609 	rem_len = total_len;
3610 
3611 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3612 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3613 		if (buf)
3614 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3615 			       len);
3616 		offset += len;
3617 		rem_len -= len;
3618 	}
3619 	return total_len;
3620 }
3621 
3622 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3623 {
3624 	struct pci_dev *pdev = bp->pdev;
3625 	int i;
3626 
3627 	if (!rmem->pg_arr)
3628 		goto skip_pages;
3629 
3630 	for (i = 0; i < rmem->nr_pages; i++) {
3631 		if (!rmem->pg_arr[i])
3632 			continue;
3633 
3634 		dma_free_coherent(&pdev->dev, rmem->page_size,
3635 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3636 
3637 		rmem->pg_arr[i] = NULL;
3638 	}
3639 skip_pages:
3640 	if (rmem->pg_tbl) {
3641 		size_t pg_tbl_size = rmem->nr_pages * 8;
3642 
3643 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3644 			pg_tbl_size = rmem->page_size;
3645 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3646 				  rmem->pg_tbl, rmem->pg_tbl_map);
3647 		rmem->pg_tbl = NULL;
3648 	}
3649 	if (rmem->vmem_size && *rmem->vmem) {
3650 		vfree(*rmem->vmem);
3651 		*rmem->vmem = NULL;
3652 	}
3653 }
3654 
3655 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3656 {
3657 	struct pci_dev *pdev = bp->pdev;
3658 	u64 valid_bit = 0;
3659 	int i;
3660 
3661 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3662 		valid_bit = PTU_PTE_VALID;
3663 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3664 		size_t pg_tbl_size = rmem->nr_pages * 8;
3665 
3666 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3667 			pg_tbl_size = rmem->page_size;
3668 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3669 						  &rmem->pg_tbl_map,
3670 						  GFP_KERNEL);
3671 		if (!rmem->pg_tbl)
3672 			return -ENOMEM;
3673 	}
3674 
3675 	for (i = 0; i < rmem->nr_pages; i++) {
3676 		u64 extra_bits = valid_bit;
3677 
3678 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3679 						     rmem->page_size,
3680 						     &rmem->dma_arr[i],
3681 						     GFP_KERNEL);
3682 		if (!rmem->pg_arr[i])
3683 			return -ENOMEM;
3684 
3685 		if (rmem->ctx_mem)
3686 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3687 					  rmem->page_size);
3688 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3689 			if (i == rmem->nr_pages - 2 &&
3690 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3691 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3692 			else if (i == rmem->nr_pages - 1 &&
3693 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3694 				extra_bits |= PTU_PTE_LAST;
3695 			rmem->pg_tbl[i] =
3696 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3697 		}
3698 	}
3699 
3700 	if (rmem->vmem_size) {
3701 		*rmem->vmem = vzalloc(rmem->vmem_size);
3702 		if (!(*rmem->vmem))
3703 			return -ENOMEM;
3704 	}
3705 	return 0;
3706 }
3707 
3708 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3709 				   struct bnxt_rx_ring_info *rxr)
3710 {
3711 	int i;
3712 
3713 	kfree(rxr->rx_tpa_idx_map);
3714 	rxr->rx_tpa_idx_map = NULL;
3715 	if (rxr->rx_tpa) {
3716 		for (i = 0; i < bp->max_tpa; i++) {
3717 			kfree(rxr->rx_tpa[i].agg_arr);
3718 			rxr->rx_tpa[i].agg_arr = NULL;
3719 		}
3720 	}
3721 	kfree(rxr->rx_tpa);
3722 	rxr->rx_tpa = NULL;
3723 }
3724 
3725 static void bnxt_free_tpa_info(struct bnxt *bp)
3726 {
3727 	int i;
3728 
3729 	for (i = 0; i < bp->rx_nr_rings; i++) {
3730 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3731 
3732 		bnxt_free_one_tpa_info(bp, rxr);
3733 	}
3734 }
3735 
3736 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3737 				   struct bnxt_rx_ring_info *rxr)
3738 {
3739 	struct rx_agg_cmp *agg;
3740 	int i;
3741 
3742 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3743 			      GFP_KERNEL);
3744 	if (!rxr->rx_tpa)
3745 		return -ENOMEM;
3746 
3747 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3748 		return 0;
3749 	for (i = 0; i < bp->max_tpa; i++) {
3750 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3751 		if (!agg)
3752 			return -ENOMEM;
3753 		rxr->rx_tpa[i].agg_arr = agg;
3754 	}
3755 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3756 				      GFP_KERNEL);
3757 	if (!rxr->rx_tpa_idx_map)
3758 		return -ENOMEM;
3759 
3760 	return 0;
3761 }
3762 
3763 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3764 {
3765 	int i, rc;
3766 
3767 	bp->max_tpa = MAX_TPA;
3768 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3769 		if (!bp->max_tpa_v2)
3770 			return 0;
3771 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3772 	}
3773 
3774 	for (i = 0; i < bp->rx_nr_rings; i++) {
3775 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3776 
3777 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3778 		if (rc)
3779 			return rc;
3780 	}
3781 	return 0;
3782 }
3783 
3784 static void bnxt_free_rx_rings(struct bnxt *bp)
3785 {
3786 	int i;
3787 
3788 	if (!bp->rx_ring)
3789 		return;
3790 
3791 	bnxt_free_tpa_info(bp);
3792 	for (i = 0; i < bp->rx_nr_rings; i++) {
3793 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3794 		struct bnxt_ring_struct *ring;
3795 
3796 		if (rxr->xdp_prog)
3797 			bpf_prog_put(rxr->xdp_prog);
3798 
3799 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3800 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3801 
3802 		page_pool_destroy(rxr->page_pool);
3803 		page_pool_destroy(rxr->head_pool);
3804 		rxr->page_pool = rxr->head_pool = NULL;
3805 
3806 		kfree(rxr->rx_agg_bmap);
3807 		rxr->rx_agg_bmap = NULL;
3808 
3809 		ring = &rxr->rx_ring_struct;
3810 		bnxt_free_ring(bp, &ring->ring_mem);
3811 
3812 		ring = &rxr->rx_agg_ring_struct;
3813 		bnxt_free_ring(bp, &ring->ring_mem);
3814 	}
3815 }
3816 
3817 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3818 				   struct bnxt_rx_ring_info *rxr,
3819 				   int numa_node)
3820 {
3821 	const unsigned int agg_size_fac = PAGE_SIZE / BNXT_RX_PAGE_SIZE;
3822 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3823 	struct page_pool_params pp = { 0 };
3824 	struct page_pool *pool;
3825 
3826 	pp.pool_size = bp->rx_agg_ring_size / agg_size_fac;
3827 	if (BNXT_RX_PAGE_MODE(bp))
3828 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3829 	pp.nid = numa_node;
3830 	pp.netdev = bp->dev;
3831 	pp.dev = &bp->pdev->dev;
3832 	pp.dma_dir = bp->rx_dir;
3833 	pp.max_len = PAGE_SIZE;
3834 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3835 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3836 	pp.queue_idx = rxr->bnapi->index;
3837 
3838 	pool = page_pool_create(&pp);
3839 	if (IS_ERR(pool))
3840 		return PTR_ERR(pool);
3841 	rxr->page_pool = pool;
3842 
3843 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3844 	if (bnxt_separate_head_pool(rxr)) {
3845 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3846 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3847 		pool = page_pool_create(&pp);
3848 		if (IS_ERR(pool))
3849 			goto err_destroy_pp;
3850 	} else {
3851 		page_pool_get(pool);
3852 	}
3853 	rxr->head_pool = pool;
3854 
3855 	return 0;
3856 
3857 err_destroy_pp:
3858 	page_pool_destroy(rxr->page_pool);
3859 	rxr->page_pool = NULL;
3860 	return PTR_ERR(pool);
3861 }
3862 
3863 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3864 {
3865 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3866 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3867 }
3868 
3869 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3870 {
3871 	u16 mem_size;
3872 
3873 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3874 	mem_size = rxr->rx_agg_bmap_size / 8;
3875 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3876 	if (!rxr->rx_agg_bmap)
3877 		return -ENOMEM;
3878 
3879 	return 0;
3880 }
3881 
3882 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3883 {
3884 	int numa_node = dev_to_node(&bp->pdev->dev);
3885 	int i, rc = 0, agg_rings = 0, cpu;
3886 
3887 	if (!bp->rx_ring)
3888 		return -ENOMEM;
3889 
3890 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3891 		agg_rings = 1;
3892 
3893 	for (i = 0; i < bp->rx_nr_rings; i++) {
3894 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3895 		struct bnxt_ring_struct *ring;
3896 		int cpu_node;
3897 
3898 		ring = &rxr->rx_ring_struct;
3899 
3900 		cpu = cpumask_local_spread(i, numa_node);
3901 		cpu_node = cpu_to_node(cpu);
3902 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3903 			   i, cpu_node);
3904 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3905 		if (rc)
3906 			return rc;
3907 		bnxt_enable_rx_page_pool(rxr);
3908 
3909 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3910 		if (rc < 0)
3911 			return rc;
3912 
3913 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3914 						MEM_TYPE_PAGE_POOL,
3915 						rxr->page_pool);
3916 		if (rc) {
3917 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3918 			return rc;
3919 		}
3920 
3921 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3922 		if (rc)
3923 			return rc;
3924 
3925 		ring->grp_idx = i;
3926 		if (agg_rings) {
3927 			ring = &rxr->rx_agg_ring_struct;
3928 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3929 			if (rc)
3930 				return rc;
3931 
3932 			ring->grp_idx = i;
3933 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3934 			if (rc)
3935 				return rc;
3936 		}
3937 	}
3938 	if (bp->flags & BNXT_FLAG_TPA)
3939 		rc = bnxt_alloc_tpa_info(bp);
3940 	return rc;
3941 }
3942 
3943 static void bnxt_free_tx_rings(struct bnxt *bp)
3944 {
3945 	int i;
3946 	struct pci_dev *pdev = bp->pdev;
3947 
3948 	if (!bp->tx_ring)
3949 		return;
3950 
3951 	for (i = 0; i < bp->tx_nr_rings; i++) {
3952 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3953 		struct bnxt_ring_struct *ring;
3954 
3955 		if (txr->tx_push) {
3956 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3957 					  txr->tx_push, txr->tx_push_mapping);
3958 			txr->tx_push = NULL;
3959 		}
3960 
3961 		ring = &txr->tx_ring_struct;
3962 
3963 		bnxt_free_ring(bp, &ring->ring_mem);
3964 	}
3965 }
3966 
3967 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3968 	((tc) * (bp)->tx_nr_rings_per_tc)
3969 
3970 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3971 	((tx) % (bp)->tx_nr_rings_per_tc)
3972 
3973 #define BNXT_RING_TO_TC(bp, tx)		\
3974 	((tx) / (bp)->tx_nr_rings_per_tc)
3975 
3976 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3977 {
3978 	int i, j, rc;
3979 	struct pci_dev *pdev = bp->pdev;
3980 
3981 	bp->tx_push_size = 0;
3982 	if (bp->tx_push_thresh) {
3983 		int push_size;
3984 
3985 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3986 					bp->tx_push_thresh);
3987 
3988 		if (push_size > 256) {
3989 			push_size = 0;
3990 			bp->tx_push_thresh = 0;
3991 		}
3992 
3993 		bp->tx_push_size = push_size;
3994 	}
3995 
3996 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3997 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3998 		struct bnxt_ring_struct *ring;
3999 		u8 qidx;
4000 
4001 		ring = &txr->tx_ring_struct;
4002 
4003 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4004 		if (rc)
4005 			return rc;
4006 
4007 		ring->grp_idx = txr->bnapi->index;
4008 		if (bp->tx_push_size) {
4009 			dma_addr_t mapping;
4010 
4011 			/* One pre-allocated DMA buffer to backup
4012 			 * TX push operation
4013 			 */
4014 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4015 						bp->tx_push_size,
4016 						&txr->tx_push_mapping,
4017 						GFP_KERNEL);
4018 
4019 			if (!txr->tx_push)
4020 				return -ENOMEM;
4021 
4022 			mapping = txr->tx_push_mapping +
4023 				sizeof(struct tx_push_bd);
4024 			txr->data_mapping = cpu_to_le64(mapping);
4025 		}
4026 		qidx = bp->tc_to_qidx[j];
4027 		ring->queue_id = bp->q_info[qidx].queue_id;
4028 		spin_lock_init(&txr->xdp_tx_lock);
4029 		if (i < bp->tx_nr_rings_xdp)
4030 			continue;
4031 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4032 			j++;
4033 	}
4034 	return 0;
4035 }
4036 
4037 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4038 {
4039 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4040 
4041 	kfree(cpr->cp_desc_ring);
4042 	cpr->cp_desc_ring = NULL;
4043 	ring->ring_mem.pg_arr = NULL;
4044 	kfree(cpr->cp_desc_mapping);
4045 	cpr->cp_desc_mapping = NULL;
4046 	ring->ring_mem.dma_arr = NULL;
4047 }
4048 
4049 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4050 {
4051 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
4052 	if (!cpr->cp_desc_ring)
4053 		return -ENOMEM;
4054 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
4055 				       GFP_KERNEL);
4056 	if (!cpr->cp_desc_mapping)
4057 		return -ENOMEM;
4058 	return 0;
4059 }
4060 
4061 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4062 {
4063 	int i;
4064 
4065 	if (!bp->bnapi)
4066 		return;
4067 	for (i = 0; i < bp->cp_nr_rings; i++) {
4068 		struct bnxt_napi *bnapi = bp->bnapi[i];
4069 
4070 		if (!bnapi)
4071 			continue;
4072 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4073 	}
4074 }
4075 
4076 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4077 {
4078 	int i, n = bp->cp_nr_pages;
4079 
4080 	for (i = 0; i < bp->cp_nr_rings; i++) {
4081 		struct bnxt_napi *bnapi = bp->bnapi[i];
4082 		int rc;
4083 
4084 		if (!bnapi)
4085 			continue;
4086 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4087 		if (rc)
4088 			return rc;
4089 	}
4090 	return 0;
4091 }
4092 
4093 static void bnxt_free_cp_rings(struct bnxt *bp)
4094 {
4095 	int i;
4096 
4097 	if (!bp->bnapi)
4098 		return;
4099 
4100 	for (i = 0; i < bp->cp_nr_rings; i++) {
4101 		struct bnxt_napi *bnapi = bp->bnapi[i];
4102 		struct bnxt_cp_ring_info *cpr;
4103 		struct bnxt_ring_struct *ring;
4104 		int j;
4105 
4106 		if (!bnapi)
4107 			continue;
4108 
4109 		cpr = &bnapi->cp_ring;
4110 		ring = &cpr->cp_ring_struct;
4111 
4112 		bnxt_free_ring(bp, &ring->ring_mem);
4113 
4114 		if (!cpr->cp_ring_arr)
4115 			continue;
4116 
4117 		for (j = 0; j < cpr->cp_ring_count; j++) {
4118 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4119 
4120 			ring = &cpr2->cp_ring_struct;
4121 			bnxt_free_ring(bp, &ring->ring_mem);
4122 			bnxt_free_cp_arrays(cpr2);
4123 		}
4124 		kfree(cpr->cp_ring_arr);
4125 		cpr->cp_ring_arr = NULL;
4126 		cpr->cp_ring_count = 0;
4127 	}
4128 }
4129 
4130 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4131 				  struct bnxt_cp_ring_info *cpr)
4132 {
4133 	struct bnxt_ring_mem_info *rmem;
4134 	struct bnxt_ring_struct *ring;
4135 	int rc;
4136 
4137 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4138 	if (rc) {
4139 		bnxt_free_cp_arrays(cpr);
4140 		return -ENOMEM;
4141 	}
4142 	ring = &cpr->cp_ring_struct;
4143 	rmem = &ring->ring_mem;
4144 	rmem->nr_pages = bp->cp_nr_pages;
4145 	rmem->page_size = HW_CMPD_RING_SIZE;
4146 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4147 	rmem->dma_arr = cpr->cp_desc_mapping;
4148 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4149 	rc = bnxt_alloc_ring(bp, rmem);
4150 	if (rc) {
4151 		bnxt_free_ring(bp, rmem);
4152 		bnxt_free_cp_arrays(cpr);
4153 	}
4154 	return rc;
4155 }
4156 
4157 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4158 {
4159 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4160 	int i, j, rc, ulp_msix;
4161 	int tcs = bp->num_tc;
4162 
4163 	if (!tcs)
4164 		tcs = 1;
4165 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4166 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4167 		struct bnxt_napi *bnapi = bp->bnapi[i];
4168 		struct bnxt_cp_ring_info *cpr, *cpr2;
4169 		struct bnxt_ring_struct *ring;
4170 		int cp_count = 0, k;
4171 		int rx = 0, tx = 0;
4172 
4173 		if (!bnapi)
4174 			continue;
4175 
4176 		cpr = &bnapi->cp_ring;
4177 		cpr->bnapi = bnapi;
4178 		ring = &cpr->cp_ring_struct;
4179 
4180 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4181 		if (rc)
4182 			return rc;
4183 
4184 		ring->map_idx = ulp_msix + i;
4185 
4186 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4187 			continue;
4188 
4189 		if (i < bp->rx_nr_rings) {
4190 			cp_count++;
4191 			rx = 1;
4192 		}
4193 		if (i < bp->tx_nr_rings_xdp) {
4194 			cp_count++;
4195 			tx = 1;
4196 		} else if ((sh && i < bp->tx_nr_rings) ||
4197 			 (!sh && i >= bp->rx_nr_rings)) {
4198 			cp_count += tcs;
4199 			tx = 1;
4200 		}
4201 
4202 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4203 					   GFP_KERNEL);
4204 		if (!cpr->cp_ring_arr)
4205 			return -ENOMEM;
4206 		cpr->cp_ring_count = cp_count;
4207 
4208 		for (k = 0; k < cp_count; k++) {
4209 			cpr2 = &cpr->cp_ring_arr[k];
4210 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4211 			if (rc)
4212 				return rc;
4213 			cpr2->bnapi = bnapi;
4214 			cpr2->sw_stats = cpr->sw_stats;
4215 			cpr2->cp_idx = k;
4216 			if (!k && rx) {
4217 				bp->rx_ring[i].rx_cpr = cpr2;
4218 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4219 			} else {
4220 				int n, tc = k - rx;
4221 
4222 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4223 				bp->tx_ring[n].tx_cpr = cpr2;
4224 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4225 			}
4226 		}
4227 		if (tx)
4228 			j++;
4229 	}
4230 	return 0;
4231 }
4232 
4233 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4234 				     struct bnxt_rx_ring_info *rxr)
4235 {
4236 	struct bnxt_ring_mem_info *rmem;
4237 	struct bnxt_ring_struct *ring;
4238 
4239 	ring = &rxr->rx_ring_struct;
4240 	rmem = &ring->ring_mem;
4241 	rmem->nr_pages = bp->rx_nr_pages;
4242 	rmem->page_size = HW_RXBD_RING_SIZE;
4243 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4244 	rmem->dma_arr = rxr->rx_desc_mapping;
4245 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4246 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4247 
4248 	ring = &rxr->rx_agg_ring_struct;
4249 	rmem = &ring->ring_mem;
4250 	rmem->nr_pages = bp->rx_agg_nr_pages;
4251 	rmem->page_size = HW_RXBD_RING_SIZE;
4252 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4253 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4254 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4255 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4256 }
4257 
4258 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4259 				      struct bnxt_rx_ring_info *rxr)
4260 {
4261 	struct bnxt_ring_mem_info *rmem;
4262 	struct bnxt_ring_struct *ring;
4263 	int i;
4264 
4265 	rxr->page_pool->p.napi = NULL;
4266 	rxr->page_pool = NULL;
4267 	rxr->head_pool->p.napi = NULL;
4268 	rxr->head_pool = NULL;
4269 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4270 
4271 	ring = &rxr->rx_ring_struct;
4272 	rmem = &ring->ring_mem;
4273 	rmem->pg_tbl = NULL;
4274 	rmem->pg_tbl_map = 0;
4275 	for (i = 0; i < rmem->nr_pages; i++) {
4276 		rmem->pg_arr[i] = NULL;
4277 		rmem->dma_arr[i] = 0;
4278 	}
4279 	*rmem->vmem = NULL;
4280 
4281 	ring = &rxr->rx_agg_ring_struct;
4282 	rmem = &ring->ring_mem;
4283 	rmem->pg_tbl = NULL;
4284 	rmem->pg_tbl_map = 0;
4285 	for (i = 0; i < rmem->nr_pages; i++) {
4286 		rmem->pg_arr[i] = NULL;
4287 		rmem->dma_arr[i] = 0;
4288 	}
4289 	*rmem->vmem = NULL;
4290 }
4291 
4292 static void bnxt_init_ring_struct(struct bnxt *bp)
4293 {
4294 	int i, j;
4295 
4296 	for (i = 0; i < bp->cp_nr_rings; i++) {
4297 		struct bnxt_napi *bnapi = bp->bnapi[i];
4298 		struct bnxt_ring_mem_info *rmem;
4299 		struct bnxt_cp_ring_info *cpr;
4300 		struct bnxt_rx_ring_info *rxr;
4301 		struct bnxt_tx_ring_info *txr;
4302 		struct bnxt_ring_struct *ring;
4303 
4304 		if (!bnapi)
4305 			continue;
4306 
4307 		cpr = &bnapi->cp_ring;
4308 		ring = &cpr->cp_ring_struct;
4309 		rmem = &ring->ring_mem;
4310 		rmem->nr_pages = bp->cp_nr_pages;
4311 		rmem->page_size = HW_CMPD_RING_SIZE;
4312 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4313 		rmem->dma_arr = cpr->cp_desc_mapping;
4314 		rmem->vmem_size = 0;
4315 
4316 		rxr = bnapi->rx_ring;
4317 		if (!rxr)
4318 			goto skip_rx;
4319 
4320 		ring = &rxr->rx_ring_struct;
4321 		rmem = &ring->ring_mem;
4322 		rmem->nr_pages = bp->rx_nr_pages;
4323 		rmem->page_size = HW_RXBD_RING_SIZE;
4324 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4325 		rmem->dma_arr = rxr->rx_desc_mapping;
4326 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4327 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4328 
4329 		ring = &rxr->rx_agg_ring_struct;
4330 		rmem = &ring->ring_mem;
4331 		rmem->nr_pages = bp->rx_agg_nr_pages;
4332 		rmem->page_size = HW_RXBD_RING_SIZE;
4333 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4334 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4335 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4336 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4337 
4338 skip_rx:
4339 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4340 			ring = &txr->tx_ring_struct;
4341 			rmem = &ring->ring_mem;
4342 			rmem->nr_pages = bp->tx_nr_pages;
4343 			rmem->page_size = HW_TXBD_RING_SIZE;
4344 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4345 			rmem->dma_arr = txr->tx_desc_mapping;
4346 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4347 			rmem->vmem = (void **)&txr->tx_buf_ring;
4348 		}
4349 	}
4350 }
4351 
4352 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4353 {
4354 	int i;
4355 	u32 prod;
4356 	struct rx_bd **rx_buf_ring;
4357 
4358 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4359 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4360 		int j;
4361 		struct rx_bd *rxbd;
4362 
4363 		rxbd = rx_buf_ring[i];
4364 		if (!rxbd)
4365 			continue;
4366 
4367 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4368 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4369 			rxbd->rx_bd_opaque = prod;
4370 		}
4371 	}
4372 }
4373 
4374 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4375 				       struct bnxt_rx_ring_info *rxr,
4376 				       int ring_nr)
4377 {
4378 	u32 prod;
4379 	int i;
4380 
4381 	prod = rxr->rx_prod;
4382 	for (i = 0; i < bp->rx_ring_size; i++) {
4383 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4384 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4385 				    ring_nr, i, bp->rx_ring_size);
4386 			break;
4387 		}
4388 		prod = NEXT_RX(prod);
4389 	}
4390 	rxr->rx_prod = prod;
4391 }
4392 
4393 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4394 					  struct bnxt_rx_ring_info *rxr,
4395 					  int ring_nr)
4396 {
4397 	u32 prod;
4398 	int i;
4399 
4400 	prod = rxr->rx_agg_prod;
4401 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4402 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4403 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4404 				    ring_nr, i, bp->rx_ring_size);
4405 			break;
4406 		}
4407 		prod = NEXT_RX_AGG(prod);
4408 	}
4409 	rxr->rx_agg_prod = prod;
4410 }
4411 
4412 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4413 					struct bnxt_rx_ring_info *rxr)
4414 {
4415 	dma_addr_t mapping;
4416 	u8 *data;
4417 	int i;
4418 
4419 	for (i = 0; i < bp->max_tpa; i++) {
4420 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4421 					    GFP_KERNEL);
4422 		if (!data)
4423 			return -ENOMEM;
4424 
4425 		rxr->rx_tpa[i].data = data;
4426 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4427 		rxr->rx_tpa[i].mapping = mapping;
4428 	}
4429 
4430 	return 0;
4431 }
4432 
4433 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4434 {
4435 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4436 	int rc;
4437 
4438 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4439 
4440 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4441 		return 0;
4442 
4443 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4444 
4445 	if (rxr->rx_tpa) {
4446 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4447 		if (rc)
4448 			return rc;
4449 	}
4450 	return 0;
4451 }
4452 
4453 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4454 				       struct bnxt_rx_ring_info *rxr)
4455 {
4456 	struct bnxt_ring_struct *ring;
4457 	u32 type;
4458 
4459 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4460 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4461 
4462 	if (NET_IP_ALIGN == 2)
4463 		type |= RX_BD_FLAGS_SOP;
4464 
4465 	ring = &rxr->rx_ring_struct;
4466 	bnxt_init_rxbd_pages(ring, type);
4467 	ring->fw_ring_id = INVALID_HW_RING_ID;
4468 }
4469 
4470 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4471 					   struct bnxt_rx_ring_info *rxr)
4472 {
4473 	struct bnxt_ring_struct *ring;
4474 	u32 type;
4475 
4476 	ring = &rxr->rx_agg_ring_struct;
4477 	ring->fw_ring_id = INVALID_HW_RING_ID;
4478 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4479 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4480 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4481 
4482 		bnxt_init_rxbd_pages(ring, type);
4483 	}
4484 }
4485 
4486 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4487 {
4488 	struct bnxt_rx_ring_info *rxr;
4489 
4490 	rxr = &bp->rx_ring[ring_nr];
4491 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4492 
4493 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4494 			     &rxr->bnapi->napi);
4495 
4496 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4497 		bpf_prog_add(bp->xdp_prog, 1);
4498 		rxr->xdp_prog = bp->xdp_prog;
4499 	}
4500 
4501 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4502 
4503 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4504 }
4505 
4506 static void bnxt_init_cp_rings(struct bnxt *bp)
4507 {
4508 	int i, j;
4509 
4510 	for (i = 0; i < bp->cp_nr_rings; i++) {
4511 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4512 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4513 
4514 		ring->fw_ring_id = INVALID_HW_RING_ID;
4515 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4516 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4517 		if (!cpr->cp_ring_arr)
4518 			continue;
4519 		for (j = 0; j < cpr->cp_ring_count; j++) {
4520 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4521 
4522 			ring = &cpr2->cp_ring_struct;
4523 			ring->fw_ring_id = INVALID_HW_RING_ID;
4524 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4525 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4526 		}
4527 	}
4528 }
4529 
4530 static int bnxt_init_rx_rings(struct bnxt *bp)
4531 {
4532 	int i, rc = 0;
4533 
4534 	if (BNXT_RX_PAGE_MODE(bp)) {
4535 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4536 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4537 	} else {
4538 		bp->rx_offset = BNXT_RX_OFFSET;
4539 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4540 	}
4541 
4542 	for (i = 0; i < bp->rx_nr_rings; i++) {
4543 		rc = bnxt_init_one_rx_ring(bp, i);
4544 		if (rc)
4545 			break;
4546 	}
4547 
4548 	return rc;
4549 }
4550 
4551 static int bnxt_init_tx_rings(struct bnxt *bp)
4552 {
4553 	u16 i;
4554 
4555 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4556 				   BNXT_MIN_TX_DESC_CNT);
4557 
4558 	for (i = 0; i < bp->tx_nr_rings; i++) {
4559 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4560 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4561 
4562 		ring->fw_ring_id = INVALID_HW_RING_ID;
4563 
4564 		if (i >= bp->tx_nr_rings_xdp)
4565 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4566 					     NETDEV_QUEUE_TYPE_TX,
4567 					     &txr->bnapi->napi);
4568 	}
4569 
4570 	return 0;
4571 }
4572 
4573 static void bnxt_free_ring_grps(struct bnxt *bp)
4574 {
4575 	kfree(bp->grp_info);
4576 	bp->grp_info = NULL;
4577 }
4578 
4579 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4580 {
4581 	int i;
4582 
4583 	if (irq_re_init) {
4584 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4585 				       sizeof(struct bnxt_ring_grp_info),
4586 				       GFP_KERNEL);
4587 		if (!bp->grp_info)
4588 			return -ENOMEM;
4589 	}
4590 	for (i = 0; i < bp->cp_nr_rings; i++) {
4591 		if (irq_re_init)
4592 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4593 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4594 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4595 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4596 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4597 	}
4598 	return 0;
4599 }
4600 
4601 static void bnxt_free_vnics(struct bnxt *bp)
4602 {
4603 	kfree(bp->vnic_info);
4604 	bp->vnic_info = NULL;
4605 	bp->nr_vnics = 0;
4606 }
4607 
4608 static int bnxt_alloc_vnics(struct bnxt *bp)
4609 {
4610 	int num_vnics = 1;
4611 
4612 #ifdef CONFIG_RFS_ACCEL
4613 	if (bp->flags & BNXT_FLAG_RFS) {
4614 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4615 			num_vnics++;
4616 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4617 			num_vnics += bp->rx_nr_rings;
4618 	}
4619 #endif
4620 
4621 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4622 		num_vnics++;
4623 
4624 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4625 				GFP_KERNEL);
4626 	if (!bp->vnic_info)
4627 		return -ENOMEM;
4628 
4629 	bp->nr_vnics = num_vnics;
4630 	return 0;
4631 }
4632 
4633 static void bnxt_init_vnics(struct bnxt *bp)
4634 {
4635 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4636 	int i;
4637 
4638 	for (i = 0; i < bp->nr_vnics; i++) {
4639 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4640 		int j;
4641 
4642 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4643 		vnic->vnic_id = i;
4644 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4645 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4646 
4647 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4648 
4649 		if (bp->vnic_info[i].rss_hash_key) {
4650 			if (i == BNXT_VNIC_DEFAULT) {
4651 				u8 *key = (void *)vnic->rss_hash_key;
4652 				int k;
4653 
4654 				if (!bp->rss_hash_key_valid &&
4655 				    !bp->rss_hash_key_updated) {
4656 					get_random_bytes(bp->rss_hash_key,
4657 							 HW_HASH_KEY_SIZE);
4658 					bp->rss_hash_key_updated = true;
4659 				}
4660 
4661 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4662 				       HW_HASH_KEY_SIZE);
4663 
4664 				if (!bp->rss_hash_key_updated)
4665 					continue;
4666 
4667 				bp->rss_hash_key_updated = false;
4668 				bp->rss_hash_key_valid = true;
4669 
4670 				bp->toeplitz_prefix = 0;
4671 				for (k = 0; k < 8; k++) {
4672 					bp->toeplitz_prefix <<= 8;
4673 					bp->toeplitz_prefix |= key[k];
4674 				}
4675 			} else {
4676 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4677 				       HW_HASH_KEY_SIZE);
4678 			}
4679 		}
4680 	}
4681 }
4682 
4683 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4684 {
4685 	int pages;
4686 
4687 	pages = ring_size / desc_per_pg;
4688 
4689 	if (!pages)
4690 		return 1;
4691 
4692 	pages++;
4693 
4694 	while (pages & (pages - 1))
4695 		pages++;
4696 
4697 	return pages;
4698 }
4699 
4700 void bnxt_set_tpa_flags(struct bnxt *bp)
4701 {
4702 	bp->flags &= ~BNXT_FLAG_TPA;
4703 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4704 		return;
4705 	if (bp->dev->features & NETIF_F_LRO)
4706 		bp->flags |= BNXT_FLAG_LRO;
4707 	else if (bp->dev->features & NETIF_F_GRO_HW)
4708 		bp->flags |= BNXT_FLAG_GRO;
4709 }
4710 
4711 static void bnxt_init_ring_params(struct bnxt *bp)
4712 {
4713 	unsigned int rx_size;
4714 
4715 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4716 	/* Try to fit 4 chunks into a 4k page */
4717 	rx_size = SZ_1K -
4718 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4719 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4720 }
4721 
4722 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4723  * be set on entry.
4724  */
4725 void bnxt_set_ring_params(struct bnxt *bp)
4726 {
4727 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4728 	u32 agg_factor = 0, agg_ring_size = 0;
4729 
4730 	/* 8 for CRC and VLAN */
4731 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4732 
4733 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4734 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4735 
4736 	ring_size = bp->rx_ring_size;
4737 	bp->rx_agg_ring_size = 0;
4738 	bp->rx_agg_nr_pages = 0;
4739 
4740 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4741 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4742 
4743 	bp->flags &= ~BNXT_FLAG_JUMBO;
4744 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4745 		u32 jumbo_factor;
4746 
4747 		bp->flags |= BNXT_FLAG_JUMBO;
4748 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4749 		if (jumbo_factor > agg_factor)
4750 			agg_factor = jumbo_factor;
4751 	}
4752 	if (agg_factor) {
4753 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4754 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4755 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4756 				    bp->rx_ring_size, ring_size);
4757 			bp->rx_ring_size = ring_size;
4758 		}
4759 		agg_ring_size = ring_size * agg_factor;
4760 
4761 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4762 							RX_DESC_CNT);
4763 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4764 			u32 tmp = agg_ring_size;
4765 
4766 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4767 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4768 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4769 				    tmp, agg_ring_size);
4770 		}
4771 		bp->rx_agg_ring_size = agg_ring_size;
4772 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4773 
4774 		if (BNXT_RX_PAGE_MODE(bp)) {
4775 			rx_space = PAGE_SIZE;
4776 			rx_size = PAGE_SIZE -
4777 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4778 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4779 		} else {
4780 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4781 				       bp->rx_copybreak,
4782 				       bp->dev->cfg_pending->hds_thresh);
4783 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4784 			rx_space = rx_size + NET_SKB_PAD +
4785 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4786 		}
4787 	}
4788 
4789 	bp->rx_buf_use_size = rx_size;
4790 	bp->rx_buf_size = rx_space;
4791 
4792 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4793 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4794 
4795 	ring_size = bp->tx_ring_size;
4796 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4797 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4798 
4799 	max_rx_cmpl = bp->rx_ring_size;
4800 	/* MAX TPA needs to be added because TPA_START completions are
4801 	 * immediately recycled, so the TPA completions are not bound by
4802 	 * the RX ring size.
4803 	 */
4804 	if (bp->flags & BNXT_FLAG_TPA)
4805 		max_rx_cmpl += bp->max_tpa;
4806 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4807 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4808 	bp->cp_ring_size = ring_size;
4809 
4810 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4811 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4812 		bp->cp_nr_pages = MAX_CP_PAGES;
4813 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4814 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4815 			    ring_size, bp->cp_ring_size);
4816 	}
4817 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4818 	bp->cp_ring_mask = bp->cp_bit - 1;
4819 }
4820 
4821 /* Changing allocation mode of RX rings.
4822  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4823  */
4824 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4825 {
4826 	struct net_device *dev = bp->dev;
4827 
4828 	if (page_mode) {
4829 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4830 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4831 
4832 		if (bp->xdp_prog->aux->xdp_has_frags)
4833 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4834 		else
4835 			dev->max_mtu =
4836 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4837 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4838 			bp->flags |= BNXT_FLAG_JUMBO;
4839 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4840 		} else {
4841 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4842 			bp->rx_skb_func = bnxt_rx_page_skb;
4843 		}
4844 		bp->rx_dir = DMA_BIDIRECTIONAL;
4845 	} else {
4846 		dev->max_mtu = bp->max_mtu;
4847 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4848 		bp->rx_dir = DMA_FROM_DEVICE;
4849 		bp->rx_skb_func = bnxt_rx_skb;
4850 	}
4851 }
4852 
4853 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4854 {
4855 	__bnxt_set_rx_skb_mode(bp, page_mode);
4856 
4857 	if (!page_mode) {
4858 		int rx, tx;
4859 
4860 		bnxt_get_max_rings(bp, &rx, &tx, true);
4861 		if (rx > 1) {
4862 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4863 			bp->dev->hw_features |= NETIF_F_LRO;
4864 		}
4865 	}
4866 
4867 	/* Update LRO and GRO_HW availability */
4868 	netdev_update_features(bp->dev);
4869 }
4870 
4871 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4872 {
4873 	int i;
4874 	struct bnxt_vnic_info *vnic;
4875 	struct pci_dev *pdev = bp->pdev;
4876 
4877 	if (!bp->vnic_info)
4878 		return;
4879 
4880 	for (i = 0; i < bp->nr_vnics; i++) {
4881 		vnic = &bp->vnic_info[i];
4882 
4883 		kfree(vnic->fw_grp_ids);
4884 		vnic->fw_grp_ids = NULL;
4885 
4886 		kfree(vnic->uc_list);
4887 		vnic->uc_list = NULL;
4888 
4889 		if (vnic->mc_list) {
4890 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4891 					  vnic->mc_list, vnic->mc_list_mapping);
4892 			vnic->mc_list = NULL;
4893 		}
4894 
4895 		if (vnic->rss_table) {
4896 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4897 					  vnic->rss_table,
4898 					  vnic->rss_table_dma_addr);
4899 			vnic->rss_table = NULL;
4900 		}
4901 
4902 		vnic->rss_hash_key = NULL;
4903 		vnic->flags = 0;
4904 	}
4905 }
4906 
4907 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4908 {
4909 	int i, rc = 0, size;
4910 	struct bnxt_vnic_info *vnic;
4911 	struct pci_dev *pdev = bp->pdev;
4912 	int max_rings;
4913 
4914 	for (i = 0; i < bp->nr_vnics; i++) {
4915 		vnic = &bp->vnic_info[i];
4916 
4917 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4918 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4919 
4920 			if (mem_size > 0) {
4921 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4922 				if (!vnic->uc_list) {
4923 					rc = -ENOMEM;
4924 					goto out;
4925 				}
4926 			}
4927 		}
4928 
4929 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4930 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4931 			vnic->mc_list =
4932 				dma_alloc_coherent(&pdev->dev,
4933 						   vnic->mc_list_size,
4934 						   &vnic->mc_list_mapping,
4935 						   GFP_KERNEL);
4936 			if (!vnic->mc_list) {
4937 				rc = -ENOMEM;
4938 				goto out;
4939 			}
4940 		}
4941 
4942 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4943 			goto vnic_skip_grps;
4944 
4945 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4946 			max_rings = bp->rx_nr_rings;
4947 		else
4948 			max_rings = 1;
4949 
4950 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4951 		if (!vnic->fw_grp_ids) {
4952 			rc = -ENOMEM;
4953 			goto out;
4954 		}
4955 vnic_skip_grps:
4956 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4957 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4958 			continue;
4959 
4960 		/* Allocate rss table and hash key */
4961 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4962 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4963 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4964 
4965 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4966 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4967 						     vnic->rss_table_size,
4968 						     &vnic->rss_table_dma_addr,
4969 						     GFP_KERNEL);
4970 		if (!vnic->rss_table) {
4971 			rc = -ENOMEM;
4972 			goto out;
4973 		}
4974 
4975 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4976 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4977 	}
4978 	return 0;
4979 
4980 out:
4981 	return rc;
4982 }
4983 
4984 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4985 {
4986 	struct bnxt_hwrm_wait_token *token;
4987 
4988 	dma_pool_destroy(bp->hwrm_dma_pool);
4989 	bp->hwrm_dma_pool = NULL;
4990 
4991 	rcu_read_lock();
4992 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4993 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4994 	rcu_read_unlock();
4995 }
4996 
4997 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4998 {
4999 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
5000 					    BNXT_HWRM_DMA_SIZE,
5001 					    BNXT_HWRM_DMA_ALIGN, 0);
5002 	if (!bp->hwrm_dma_pool)
5003 		return -ENOMEM;
5004 
5005 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5006 
5007 	return 0;
5008 }
5009 
5010 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5011 {
5012 	kfree(stats->hw_masks);
5013 	stats->hw_masks = NULL;
5014 	kfree(stats->sw_stats);
5015 	stats->sw_stats = NULL;
5016 	if (stats->hw_stats) {
5017 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5018 				  stats->hw_stats_map);
5019 		stats->hw_stats = NULL;
5020 	}
5021 }
5022 
5023 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5024 				bool alloc_masks)
5025 {
5026 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5027 					     &stats->hw_stats_map, GFP_KERNEL);
5028 	if (!stats->hw_stats)
5029 		return -ENOMEM;
5030 
5031 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5032 	if (!stats->sw_stats)
5033 		goto stats_mem_err;
5034 
5035 	if (alloc_masks) {
5036 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5037 		if (!stats->hw_masks)
5038 			goto stats_mem_err;
5039 	}
5040 	return 0;
5041 
5042 stats_mem_err:
5043 	bnxt_free_stats_mem(bp, stats);
5044 	return -ENOMEM;
5045 }
5046 
5047 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5048 {
5049 	int i;
5050 
5051 	for (i = 0; i < count; i++)
5052 		mask_arr[i] = mask;
5053 }
5054 
5055 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5056 {
5057 	int i;
5058 
5059 	for (i = 0; i < count; i++)
5060 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5061 }
5062 
5063 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5064 				    struct bnxt_stats_mem *stats)
5065 {
5066 	struct hwrm_func_qstats_ext_output *resp;
5067 	struct hwrm_func_qstats_ext_input *req;
5068 	__le64 *hw_masks;
5069 	int rc;
5070 
5071 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5072 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5073 		return -EOPNOTSUPP;
5074 
5075 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5076 	if (rc)
5077 		return rc;
5078 
5079 	req->fid = cpu_to_le16(0xffff);
5080 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5081 
5082 	resp = hwrm_req_hold(bp, req);
5083 	rc = hwrm_req_send(bp, req);
5084 	if (!rc) {
5085 		hw_masks = &resp->rx_ucast_pkts;
5086 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5087 	}
5088 	hwrm_req_drop(bp, req);
5089 	return rc;
5090 }
5091 
5092 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5093 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5094 
5095 static void bnxt_init_stats(struct bnxt *bp)
5096 {
5097 	struct bnxt_napi *bnapi = bp->bnapi[0];
5098 	struct bnxt_cp_ring_info *cpr;
5099 	struct bnxt_stats_mem *stats;
5100 	__le64 *rx_stats, *tx_stats;
5101 	int rc, rx_count, tx_count;
5102 	u64 *rx_masks, *tx_masks;
5103 	u64 mask;
5104 	u8 flags;
5105 
5106 	cpr = &bnapi->cp_ring;
5107 	stats = &cpr->stats;
5108 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5109 	if (rc) {
5110 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5111 			mask = (1ULL << 48) - 1;
5112 		else
5113 			mask = -1ULL;
5114 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5115 	}
5116 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5117 		stats = &bp->port_stats;
5118 		rx_stats = stats->hw_stats;
5119 		rx_masks = stats->hw_masks;
5120 		rx_count = sizeof(struct rx_port_stats) / 8;
5121 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5122 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5123 		tx_count = sizeof(struct tx_port_stats) / 8;
5124 
5125 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5126 		rc = bnxt_hwrm_port_qstats(bp, flags);
5127 		if (rc) {
5128 			mask = (1ULL << 40) - 1;
5129 
5130 			bnxt_fill_masks(rx_masks, mask, rx_count);
5131 			bnxt_fill_masks(tx_masks, mask, tx_count);
5132 		} else {
5133 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5134 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5135 			bnxt_hwrm_port_qstats(bp, 0);
5136 		}
5137 	}
5138 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5139 		stats = &bp->rx_port_stats_ext;
5140 		rx_stats = stats->hw_stats;
5141 		rx_masks = stats->hw_masks;
5142 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5143 		stats = &bp->tx_port_stats_ext;
5144 		tx_stats = stats->hw_stats;
5145 		tx_masks = stats->hw_masks;
5146 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5147 
5148 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5149 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5150 		if (rc) {
5151 			mask = (1ULL << 40) - 1;
5152 
5153 			bnxt_fill_masks(rx_masks, mask, rx_count);
5154 			if (tx_stats)
5155 				bnxt_fill_masks(tx_masks, mask, tx_count);
5156 		} else {
5157 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5158 			if (tx_stats)
5159 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5160 						   tx_count);
5161 			bnxt_hwrm_port_qstats_ext(bp, 0);
5162 		}
5163 	}
5164 }
5165 
5166 static void bnxt_free_port_stats(struct bnxt *bp)
5167 {
5168 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5169 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5170 
5171 	bnxt_free_stats_mem(bp, &bp->port_stats);
5172 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5173 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5174 }
5175 
5176 static void bnxt_free_ring_stats(struct bnxt *bp)
5177 {
5178 	int i;
5179 
5180 	if (!bp->bnapi)
5181 		return;
5182 
5183 	for (i = 0; i < bp->cp_nr_rings; i++) {
5184 		struct bnxt_napi *bnapi = bp->bnapi[i];
5185 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5186 
5187 		bnxt_free_stats_mem(bp, &cpr->stats);
5188 
5189 		kfree(cpr->sw_stats);
5190 		cpr->sw_stats = NULL;
5191 	}
5192 }
5193 
5194 static int bnxt_alloc_stats(struct bnxt *bp)
5195 {
5196 	u32 size, i;
5197 	int rc;
5198 
5199 	size = bp->hw_ring_stats_size;
5200 
5201 	for (i = 0; i < bp->cp_nr_rings; i++) {
5202 		struct bnxt_napi *bnapi = bp->bnapi[i];
5203 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5204 
5205 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5206 		if (!cpr->sw_stats)
5207 			return -ENOMEM;
5208 
5209 		cpr->stats.len = size;
5210 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5211 		if (rc)
5212 			return rc;
5213 
5214 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5215 	}
5216 
5217 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5218 		return 0;
5219 
5220 	if (bp->port_stats.hw_stats)
5221 		goto alloc_ext_stats;
5222 
5223 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5224 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5225 	if (rc)
5226 		return rc;
5227 
5228 	bp->flags |= BNXT_FLAG_PORT_STATS;
5229 
5230 alloc_ext_stats:
5231 	/* Display extended statistics only if FW supports it */
5232 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5233 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5234 			return 0;
5235 
5236 	if (bp->rx_port_stats_ext.hw_stats)
5237 		goto alloc_tx_ext_stats;
5238 
5239 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5240 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5241 	/* Extended stats are optional */
5242 	if (rc)
5243 		return 0;
5244 
5245 alloc_tx_ext_stats:
5246 	if (bp->tx_port_stats_ext.hw_stats)
5247 		return 0;
5248 
5249 	if (bp->hwrm_spec_code >= 0x10902 ||
5250 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5251 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5252 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5253 		/* Extended stats are optional */
5254 		if (rc)
5255 			return 0;
5256 	}
5257 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5258 	return 0;
5259 }
5260 
5261 static void bnxt_clear_ring_indices(struct bnxt *bp)
5262 {
5263 	int i, j;
5264 
5265 	if (!bp->bnapi)
5266 		return;
5267 
5268 	for (i = 0; i < bp->cp_nr_rings; i++) {
5269 		struct bnxt_napi *bnapi = bp->bnapi[i];
5270 		struct bnxt_cp_ring_info *cpr;
5271 		struct bnxt_rx_ring_info *rxr;
5272 		struct bnxt_tx_ring_info *txr;
5273 
5274 		if (!bnapi)
5275 			continue;
5276 
5277 		cpr = &bnapi->cp_ring;
5278 		cpr->cp_raw_cons = 0;
5279 
5280 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5281 			txr->tx_prod = 0;
5282 			txr->tx_cons = 0;
5283 			txr->tx_hw_cons = 0;
5284 		}
5285 
5286 		rxr = bnapi->rx_ring;
5287 		if (rxr) {
5288 			rxr->rx_prod = 0;
5289 			rxr->rx_agg_prod = 0;
5290 			rxr->rx_sw_agg_prod = 0;
5291 			rxr->rx_next_cons = 0;
5292 		}
5293 		bnapi->events = 0;
5294 	}
5295 }
5296 
5297 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5298 {
5299 	u8 type = fltr->type, flags = fltr->flags;
5300 
5301 	INIT_LIST_HEAD(&fltr->list);
5302 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5303 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5304 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5305 }
5306 
5307 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5308 {
5309 	if (!list_empty(&fltr->list))
5310 		list_del_init(&fltr->list);
5311 }
5312 
5313 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5314 {
5315 	struct bnxt_filter_base *usr_fltr, *tmp;
5316 
5317 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5318 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5319 			continue;
5320 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5321 	}
5322 }
5323 
5324 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5325 {
5326 	hlist_del(&fltr->hash);
5327 	bnxt_del_one_usr_fltr(bp, fltr);
5328 	if (fltr->flags) {
5329 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5330 		bp->ntp_fltr_count--;
5331 	}
5332 	kfree(fltr);
5333 }
5334 
5335 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5336 {
5337 	int i;
5338 
5339 	netdev_assert_locked_or_invisible(bp->dev);
5340 
5341 	/* Under netdev instance lock and all our NAPIs have been disabled.
5342 	 * It's safe to delete the hash table.
5343 	 */
5344 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5345 		struct hlist_head *head;
5346 		struct hlist_node *tmp;
5347 		struct bnxt_ntuple_filter *fltr;
5348 
5349 		head = &bp->ntp_fltr_hash_tbl[i];
5350 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5351 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5352 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5353 				     !list_empty(&fltr->base.list)))
5354 				continue;
5355 			bnxt_del_fltr(bp, &fltr->base);
5356 		}
5357 	}
5358 	if (!all)
5359 		return;
5360 
5361 	bitmap_free(bp->ntp_fltr_bmap);
5362 	bp->ntp_fltr_bmap = NULL;
5363 	bp->ntp_fltr_count = 0;
5364 }
5365 
5366 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5367 {
5368 	int i, rc = 0;
5369 
5370 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5371 		return 0;
5372 
5373 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5374 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5375 
5376 	bp->ntp_fltr_count = 0;
5377 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5378 
5379 	if (!bp->ntp_fltr_bmap)
5380 		rc = -ENOMEM;
5381 
5382 	return rc;
5383 }
5384 
5385 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5386 {
5387 	int i;
5388 
5389 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5390 		struct hlist_head *head;
5391 		struct hlist_node *tmp;
5392 		struct bnxt_l2_filter *fltr;
5393 
5394 		head = &bp->l2_fltr_hash_tbl[i];
5395 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5396 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5397 				     !list_empty(&fltr->base.list)))
5398 				continue;
5399 			bnxt_del_fltr(bp, &fltr->base);
5400 		}
5401 	}
5402 }
5403 
5404 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5405 {
5406 	int i;
5407 
5408 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5409 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5410 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5411 }
5412 
5413 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5414 {
5415 	bnxt_free_vnic_attributes(bp);
5416 	bnxt_free_tx_rings(bp);
5417 	bnxt_free_rx_rings(bp);
5418 	bnxt_free_cp_rings(bp);
5419 	bnxt_free_all_cp_arrays(bp);
5420 	bnxt_free_ntp_fltrs(bp, false);
5421 	bnxt_free_l2_filters(bp, false);
5422 	if (irq_re_init) {
5423 		bnxt_free_ring_stats(bp);
5424 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5425 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5426 			bnxt_free_port_stats(bp);
5427 		bnxt_free_ring_grps(bp);
5428 		bnxt_free_vnics(bp);
5429 		kfree(bp->tx_ring_map);
5430 		bp->tx_ring_map = NULL;
5431 		kfree(bp->tx_ring);
5432 		bp->tx_ring = NULL;
5433 		kfree(bp->rx_ring);
5434 		bp->rx_ring = NULL;
5435 		kfree(bp->bnapi);
5436 		bp->bnapi = NULL;
5437 	} else {
5438 		bnxt_clear_ring_indices(bp);
5439 	}
5440 }
5441 
5442 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5443 {
5444 	int i, j, rc, size, arr_size;
5445 	void *bnapi;
5446 
5447 	if (irq_re_init) {
5448 		/* Allocate bnapi mem pointer array and mem block for
5449 		 * all queues
5450 		 */
5451 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5452 				bp->cp_nr_rings);
5453 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5454 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5455 		if (!bnapi)
5456 			return -ENOMEM;
5457 
5458 		bp->bnapi = bnapi;
5459 		bnapi += arr_size;
5460 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5461 			bp->bnapi[i] = bnapi;
5462 			bp->bnapi[i]->index = i;
5463 			bp->bnapi[i]->bp = bp;
5464 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5465 				struct bnxt_cp_ring_info *cpr =
5466 					&bp->bnapi[i]->cp_ring;
5467 
5468 				cpr->cp_ring_struct.ring_mem.flags =
5469 					BNXT_RMEM_RING_PTE_FLAG;
5470 			}
5471 		}
5472 
5473 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5474 				      sizeof(struct bnxt_rx_ring_info),
5475 				      GFP_KERNEL);
5476 		if (!bp->rx_ring)
5477 			return -ENOMEM;
5478 
5479 		for (i = 0; i < bp->rx_nr_rings; i++) {
5480 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5481 
5482 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5483 				rxr->rx_ring_struct.ring_mem.flags =
5484 					BNXT_RMEM_RING_PTE_FLAG;
5485 				rxr->rx_agg_ring_struct.ring_mem.flags =
5486 					BNXT_RMEM_RING_PTE_FLAG;
5487 			} else {
5488 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5489 			}
5490 			rxr->bnapi = bp->bnapi[i];
5491 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5492 		}
5493 
5494 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5495 				      sizeof(struct bnxt_tx_ring_info),
5496 				      GFP_KERNEL);
5497 		if (!bp->tx_ring)
5498 			return -ENOMEM;
5499 
5500 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5501 					  GFP_KERNEL);
5502 
5503 		if (!bp->tx_ring_map)
5504 			return -ENOMEM;
5505 
5506 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5507 			j = 0;
5508 		else
5509 			j = bp->rx_nr_rings;
5510 
5511 		for (i = 0; i < bp->tx_nr_rings; i++) {
5512 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5513 			struct bnxt_napi *bnapi2;
5514 
5515 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5516 				txr->tx_ring_struct.ring_mem.flags =
5517 					BNXT_RMEM_RING_PTE_FLAG;
5518 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5519 			if (i >= bp->tx_nr_rings_xdp) {
5520 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5521 
5522 				bnapi2 = bp->bnapi[k];
5523 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5524 				txr->tx_napi_idx =
5525 					BNXT_RING_TO_TC(bp, txr->txq_index);
5526 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5527 				bnapi2->tx_int = bnxt_tx_int;
5528 			} else {
5529 				bnapi2 = bp->bnapi[j];
5530 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5531 				bnapi2->tx_ring[0] = txr;
5532 				bnapi2->tx_int = bnxt_tx_int_xdp;
5533 				j++;
5534 			}
5535 			txr->bnapi = bnapi2;
5536 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5537 				txr->tx_cpr = &bnapi2->cp_ring;
5538 		}
5539 
5540 		rc = bnxt_alloc_stats(bp);
5541 		if (rc)
5542 			goto alloc_mem_err;
5543 		bnxt_init_stats(bp);
5544 
5545 		rc = bnxt_alloc_ntp_fltrs(bp);
5546 		if (rc)
5547 			goto alloc_mem_err;
5548 
5549 		rc = bnxt_alloc_vnics(bp);
5550 		if (rc)
5551 			goto alloc_mem_err;
5552 	}
5553 
5554 	rc = bnxt_alloc_all_cp_arrays(bp);
5555 	if (rc)
5556 		goto alloc_mem_err;
5557 
5558 	bnxt_init_ring_struct(bp);
5559 
5560 	rc = bnxt_alloc_rx_rings(bp);
5561 	if (rc)
5562 		goto alloc_mem_err;
5563 
5564 	rc = bnxt_alloc_tx_rings(bp);
5565 	if (rc)
5566 		goto alloc_mem_err;
5567 
5568 	rc = bnxt_alloc_cp_rings(bp);
5569 	if (rc)
5570 		goto alloc_mem_err;
5571 
5572 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5573 						  BNXT_VNIC_MCAST_FLAG |
5574 						  BNXT_VNIC_UCAST_FLAG;
5575 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5576 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5577 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5578 
5579 	rc = bnxt_alloc_vnic_attributes(bp);
5580 	if (rc)
5581 		goto alloc_mem_err;
5582 	return 0;
5583 
5584 alloc_mem_err:
5585 	bnxt_free_mem(bp, true);
5586 	return rc;
5587 }
5588 
5589 static void bnxt_disable_int(struct bnxt *bp)
5590 {
5591 	int i;
5592 
5593 	if (!bp->bnapi)
5594 		return;
5595 
5596 	for (i = 0; i < bp->cp_nr_rings; i++) {
5597 		struct bnxt_napi *bnapi = bp->bnapi[i];
5598 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5599 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5600 
5601 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5602 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5603 	}
5604 }
5605 
5606 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5607 {
5608 	struct bnxt_napi *bnapi = bp->bnapi[n];
5609 	struct bnxt_cp_ring_info *cpr;
5610 
5611 	cpr = &bnapi->cp_ring;
5612 	return cpr->cp_ring_struct.map_idx;
5613 }
5614 
5615 static void bnxt_disable_int_sync(struct bnxt *bp)
5616 {
5617 	int i;
5618 
5619 	if (!bp->irq_tbl)
5620 		return;
5621 
5622 	atomic_inc(&bp->intr_sem);
5623 
5624 	bnxt_disable_int(bp);
5625 	for (i = 0; i < bp->cp_nr_rings; i++) {
5626 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5627 
5628 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5629 	}
5630 }
5631 
5632 static void bnxt_enable_int(struct bnxt *bp)
5633 {
5634 	int i;
5635 
5636 	atomic_set(&bp->intr_sem, 0);
5637 	for (i = 0; i < bp->cp_nr_rings; i++) {
5638 		struct bnxt_napi *bnapi = bp->bnapi[i];
5639 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5640 
5641 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5642 	}
5643 }
5644 
5645 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5646 			    bool async_only)
5647 {
5648 	DECLARE_BITMAP(async_events_bmap, 256);
5649 	u32 *events = (u32 *)async_events_bmap;
5650 	struct hwrm_func_drv_rgtr_output *resp;
5651 	struct hwrm_func_drv_rgtr_input *req;
5652 	u32 flags;
5653 	int rc, i;
5654 
5655 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5656 	if (rc)
5657 		return rc;
5658 
5659 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5660 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5661 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5662 
5663 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5664 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5665 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5666 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5667 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5668 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5669 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5670 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5671 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5672 	req->flags = cpu_to_le32(flags);
5673 	req->ver_maj_8b = DRV_VER_MAJ;
5674 	req->ver_min_8b = DRV_VER_MIN;
5675 	req->ver_upd_8b = DRV_VER_UPD;
5676 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5677 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5678 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5679 
5680 	if (BNXT_PF(bp)) {
5681 		u32 data[8];
5682 		int i;
5683 
5684 		memset(data, 0, sizeof(data));
5685 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5686 			u16 cmd = bnxt_vf_req_snif[i];
5687 			unsigned int bit, idx;
5688 
5689 			idx = cmd / 32;
5690 			bit = cmd % 32;
5691 			data[idx] |= 1 << bit;
5692 		}
5693 
5694 		for (i = 0; i < 8; i++)
5695 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5696 
5697 		req->enables |=
5698 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5699 	}
5700 
5701 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5702 		req->flags |= cpu_to_le32(
5703 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5704 
5705 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5706 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5707 		u16 event_id = bnxt_async_events_arr[i];
5708 
5709 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5710 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5711 			continue;
5712 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5713 		    !bp->ptp_cfg)
5714 			continue;
5715 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5716 	}
5717 	if (bmap && bmap_size) {
5718 		for (i = 0; i < bmap_size; i++) {
5719 			if (test_bit(i, bmap))
5720 				__set_bit(i, async_events_bmap);
5721 		}
5722 	}
5723 	for (i = 0; i < 8; i++)
5724 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5725 
5726 	if (async_only)
5727 		req->enables =
5728 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5729 
5730 	resp = hwrm_req_hold(bp, req);
5731 	rc = hwrm_req_send(bp, req);
5732 	if (!rc) {
5733 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5734 		if (resp->flags &
5735 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5736 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5737 	}
5738 	hwrm_req_drop(bp, req);
5739 	return rc;
5740 }
5741 
5742 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5743 {
5744 	struct hwrm_func_drv_unrgtr_input *req;
5745 	int rc;
5746 
5747 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5748 		return 0;
5749 
5750 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5751 	if (rc)
5752 		return rc;
5753 	return hwrm_req_send(bp, req);
5754 }
5755 
5756 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5757 
5758 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5759 {
5760 	struct hwrm_tunnel_dst_port_free_input *req;
5761 	int rc;
5762 
5763 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5764 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5765 		return 0;
5766 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5767 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5768 		return 0;
5769 
5770 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5771 	if (rc)
5772 		return rc;
5773 
5774 	req->tunnel_type = tunnel_type;
5775 
5776 	switch (tunnel_type) {
5777 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5778 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5779 		bp->vxlan_port = 0;
5780 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5781 		break;
5782 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5783 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5784 		bp->nge_port = 0;
5785 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5786 		break;
5787 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5788 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5789 		bp->vxlan_gpe_port = 0;
5790 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5791 		break;
5792 	default:
5793 		break;
5794 	}
5795 
5796 	rc = hwrm_req_send(bp, req);
5797 	if (rc)
5798 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5799 			   rc);
5800 	if (bp->flags & BNXT_FLAG_TPA)
5801 		bnxt_set_tpa(bp, true);
5802 	return rc;
5803 }
5804 
5805 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5806 					   u8 tunnel_type)
5807 {
5808 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5809 	struct hwrm_tunnel_dst_port_alloc_input *req;
5810 	int rc;
5811 
5812 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5813 	if (rc)
5814 		return rc;
5815 
5816 	req->tunnel_type = tunnel_type;
5817 	req->tunnel_dst_port_val = port;
5818 
5819 	resp = hwrm_req_hold(bp, req);
5820 	rc = hwrm_req_send(bp, req);
5821 	if (rc) {
5822 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5823 			   rc);
5824 		goto err_out;
5825 	}
5826 
5827 	switch (tunnel_type) {
5828 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5829 		bp->vxlan_port = port;
5830 		bp->vxlan_fw_dst_port_id =
5831 			le16_to_cpu(resp->tunnel_dst_port_id);
5832 		break;
5833 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5834 		bp->nge_port = port;
5835 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5836 		break;
5837 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5838 		bp->vxlan_gpe_port = port;
5839 		bp->vxlan_gpe_fw_dst_port_id =
5840 			le16_to_cpu(resp->tunnel_dst_port_id);
5841 		break;
5842 	default:
5843 		break;
5844 	}
5845 	if (bp->flags & BNXT_FLAG_TPA)
5846 		bnxt_set_tpa(bp, true);
5847 
5848 err_out:
5849 	hwrm_req_drop(bp, req);
5850 	return rc;
5851 }
5852 
5853 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5854 {
5855 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5856 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5857 	int rc;
5858 
5859 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5860 	if (rc)
5861 		return rc;
5862 
5863 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5864 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5865 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5866 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5867 	}
5868 	req->mask = cpu_to_le32(vnic->rx_mask);
5869 	return hwrm_req_send_silent(bp, req);
5870 }
5871 
5872 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5873 {
5874 	if (!atomic_dec_and_test(&fltr->refcnt))
5875 		return;
5876 	spin_lock_bh(&bp->ntp_fltr_lock);
5877 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5878 		spin_unlock_bh(&bp->ntp_fltr_lock);
5879 		return;
5880 	}
5881 	hlist_del_rcu(&fltr->base.hash);
5882 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5883 	if (fltr->base.flags) {
5884 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5885 		bp->ntp_fltr_count--;
5886 	}
5887 	spin_unlock_bh(&bp->ntp_fltr_lock);
5888 	kfree_rcu(fltr, base.rcu);
5889 }
5890 
5891 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5892 						      struct bnxt_l2_key *key,
5893 						      u32 idx)
5894 {
5895 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5896 	struct bnxt_l2_filter *fltr;
5897 
5898 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5899 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5900 
5901 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5902 		    l2_key->vlan == key->vlan)
5903 			return fltr;
5904 	}
5905 	return NULL;
5906 }
5907 
5908 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5909 						    struct bnxt_l2_key *key,
5910 						    u32 idx)
5911 {
5912 	struct bnxt_l2_filter *fltr = NULL;
5913 
5914 	rcu_read_lock();
5915 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5916 	if (fltr)
5917 		atomic_inc(&fltr->refcnt);
5918 	rcu_read_unlock();
5919 	return fltr;
5920 }
5921 
5922 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5923 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5924 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5925 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5926 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5927 
5928 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5929 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5930 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5931 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5932 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5933 
5934 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5935 {
5936 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5937 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5938 			return sizeof(fkeys->addrs.v4addrs) +
5939 			       sizeof(fkeys->ports);
5940 
5941 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5942 			return sizeof(fkeys->addrs.v4addrs);
5943 	}
5944 
5945 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5946 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5947 			return sizeof(fkeys->addrs.v6addrs) +
5948 			       sizeof(fkeys->ports);
5949 
5950 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5951 			return sizeof(fkeys->addrs.v6addrs);
5952 	}
5953 
5954 	return 0;
5955 }
5956 
5957 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5958 			 const unsigned char *key)
5959 {
5960 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5961 	struct bnxt_ipv4_tuple tuple4;
5962 	struct bnxt_ipv6_tuple tuple6;
5963 	int i, j, len = 0;
5964 	u8 *four_tuple;
5965 
5966 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5967 	if (!len)
5968 		return 0;
5969 
5970 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5971 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5972 		tuple4.ports = fkeys->ports;
5973 		four_tuple = (unsigned char *)&tuple4;
5974 	} else {
5975 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5976 		tuple6.ports = fkeys->ports;
5977 		four_tuple = (unsigned char *)&tuple6;
5978 	}
5979 
5980 	for (i = 0, j = 8; i < len; i++, j++) {
5981 		u8 byte = four_tuple[i];
5982 		int bit;
5983 
5984 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5985 			if (byte & 0x80)
5986 				hash ^= prefix;
5987 		}
5988 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5989 	}
5990 
5991 	/* The valid part of the hash is in the upper 32 bits. */
5992 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5993 }
5994 
5995 #ifdef CONFIG_RFS_ACCEL
5996 static struct bnxt_l2_filter *
5997 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5998 {
5999 	struct bnxt_l2_filter *fltr;
6000 	u32 idx;
6001 
6002 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6003 	      BNXT_L2_FLTR_HASH_MASK;
6004 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6005 	return fltr;
6006 }
6007 #endif
6008 
6009 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6010 			       struct bnxt_l2_key *key, u32 idx)
6011 {
6012 	struct hlist_head *head;
6013 
6014 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6015 	fltr->l2_key.vlan = key->vlan;
6016 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6017 	if (fltr->base.flags) {
6018 		int bit_id;
6019 
6020 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6021 						 bp->max_fltr, 0);
6022 		if (bit_id < 0)
6023 			return -ENOMEM;
6024 		fltr->base.sw_id = (u16)bit_id;
6025 		bp->ntp_fltr_count++;
6026 	}
6027 	head = &bp->l2_fltr_hash_tbl[idx];
6028 	hlist_add_head_rcu(&fltr->base.hash, head);
6029 	bnxt_insert_usr_fltr(bp, &fltr->base);
6030 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6031 	atomic_set(&fltr->refcnt, 1);
6032 	return 0;
6033 }
6034 
6035 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6036 						   struct bnxt_l2_key *key,
6037 						   gfp_t gfp)
6038 {
6039 	struct bnxt_l2_filter *fltr;
6040 	u32 idx;
6041 	int rc;
6042 
6043 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6044 	      BNXT_L2_FLTR_HASH_MASK;
6045 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6046 	if (fltr)
6047 		return fltr;
6048 
6049 	fltr = kzalloc(sizeof(*fltr), gfp);
6050 	if (!fltr)
6051 		return ERR_PTR(-ENOMEM);
6052 	spin_lock_bh(&bp->ntp_fltr_lock);
6053 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6054 	spin_unlock_bh(&bp->ntp_fltr_lock);
6055 	if (rc) {
6056 		bnxt_del_l2_filter(bp, fltr);
6057 		fltr = ERR_PTR(rc);
6058 	}
6059 	return fltr;
6060 }
6061 
6062 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6063 						struct bnxt_l2_key *key,
6064 						u16 flags)
6065 {
6066 	struct bnxt_l2_filter *fltr;
6067 	u32 idx;
6068 	int rc;
6069 
6070 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6071 	      BNXT_L2_FLTR_HASH_MASK;
6072 	spin_lock_bh(&bp->ntp_fltr_lock);
6073 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6074 	if (fltr) {
6075 		fltr = ERR_PTR(-EEXIST);
6076 		goto l2_filter_exit;
6077 	}
6078 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
6079 	if (!fltr) {
6080 		fltr = ERR_PTR(-ENOMEM);
6081 		goto l2_filter_exit;
6082 	}
6083 	fltr->base.flags = flags;
6084 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6085 	if (rc) {
6086 		spin_unlock_bh(&bp->ntp_fltr_lock);
6087 		bnxt_del_l2_filter(bp, fltr);
6088 		return ERR_PTR(rc);
6089 	}
6090 
6091 l2_filter_exit:
6092 	spin_unlock_bh(&bp->ntp_fltr_lock);
6093 	return fltr;
6094 }
6095 
6096 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6097 {
6098 #ifdef CONFIG_BNXT_SRIOV
6099 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6100 
6101 	return vf->fw_fid;
6102 #else
6103 	return INVALID_HW_RING_ID;
6104 #endif
6105 }
6106 
6107 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6108 {
6109 	struct hwrm_cfa_l2_filter_free_input *req;
6110 	u16 target_id = 0xffff;
6111 	int rc;
6112 
6113 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6114 		struct bnxt_pf_info *pf = &bp->pf;
6115 
6116 		if (fltr->base.vf_idx >= pf->active_vfs)
6117 			return -EINVAL;
6118 
6119 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6120 		if (target_id == INVALID_HW_RING_ID)
6121 			return -EINVAL;
6122 	}
6123 
6124 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6125 	if (rc)
6126 		return rc;
6127 
6128 	req->target_id = cpu_to_le16(target_id);
6129 	req->l2_filter_id = fltr->base.filter_id;
6130 	return hwrm_req_send(bp, req);
6131 }
6132 
6133 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6134 {
6135 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6136 	struct hwrm_cfa_l2_filter_alloc_input *req;
6137 	u16 target_id = 0xffff;
6138 	int rc;
6139 
6140 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6141 		struct bnxt_pf_info *pf = &bp->pf;
6142 
6143 		if (fltr->base.vf_idx >= pf->active_vfs)
6144 			return -EINVAL;
6145 
6146 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6147 	}
6148 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6149 	if (rc)
6150 		return rc;
6151 
6152 	req->target_id = cpu_to_le16(target_id);
6153 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6154 
6155 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6156 		req->flags |=
6157 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6158 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6159 	req->enables =
6160 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6161 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6162 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6163 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6164 	eth_broadcast_addr(req->l2_addr_mask);
6165 
6166 	if (fltr->l2_key.vlan) {
6167 		req->enables |=
6168 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6169 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6170 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6171 		req->num_vlans = 1;
6172 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6173 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6174 	}
6175 
6176 	resp = hwrm_req_hold(bp, req);
6177 	rc = hwrm_req_send(bp, req);
6178 	if (!rc) {
6179 		fltr->base.filter_id = resp->l2_filter_id;
6180 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6181 	}
6182 	hwrm_req_drop(bp, req);
6183 	return rc;
6184 }
6185 
6186 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6187 				     struct bnxt_ntuple_filter *fltr)
6188 {
6189 	struct hwrm_cfa_ntuple_filter_free_input *req;
6190 	int rc;
6191 
6192 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6193 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6194 	if (rc)
6195 		return rc;
6196 
6197 	req->ntuple_filter_id = fltr->base.filter_id;
6198 	return hwrm_req_send(bp, req);
6199 }
6200 
6201 #define BNXT_NTP_FLTR_FLAGS					\
6202 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6203 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6204 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6205 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6206 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6207 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6208 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6209 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6210 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6211 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6212 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6213 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6214 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6215 
6216 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6217 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6218 
6219 void bnxt_fill_ipv6_mask(__be32 mask[4])
6220 {
6221 	int i;
6222 
6223 	for (i = 0; i < 4; i++)
6224 		mask[i] = cpu_to_be32(~0);
6225 }
6226 
6227 static void
6228 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6229 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6230 			  struct bnxt_ntuple_filter *fltr)
6231 {
6232 	u16 rxq = fltr->base.rxq;
6233 
6234 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6235 		struct ethtool_rxfh_context *ctx;
6236 		struct bnxt_rss_ctx *rss_ctx;
6237 		struct bnxt_vnic_info *vnic;
6238 
6239 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6240 			      fltr->base.fw_vnic_id);
6241 		if (ctx) {
6242 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6243 			vnic = &rss_ctx->vnic;
6244 
6245 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6246 		}
6247 		return;
6248 	}
6249 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6250 		struct bnxt_vnic_info *vnic;
6251 		u32 enables;
6252 
6253 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6254 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6255 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6256 		req->enables |= cpu_to_le32(enables);
6257 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6258 	} else {
6259 		u32 flags;
6260 
6261 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6262 		req->flags |= cpu_to_le32(flags);
6263 		req->dst_id = cpu_to_le16(rxq);
6264 	}
6265 }
6266 
6267 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6268 				      struct bnxt_ntuple_filter *fltr)
6269 {
6270 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6271 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6272 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6273 	struct flow_keys *keys = &fltr->fkeys;
6274 	struct bnxt_l2_filter *l2_fltr;
6275 	struct bnxt_vnic_info *vnic;
6276 	int rc;
6277 
6278 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6279 	if (rc)
6280 		return rc;
6281 
6282 	l2_fltr = fltr->l2_fltr;
6283 	req->l2_filter_id = l2_fltr->base.filter_id;
6284 
6285 	if (fltr->base.flags & BNXT_ACT_DROP) {
6286 		req->flags =
6287 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6288 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6289 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6290 	} else {
6291 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6292 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6293 	}
6294 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6295 
6296 	req->ethertype = htons(ETH_P_IP);
6297 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6298 	req->ip_protocol = keys->basic.ip_proto;
6299 
6300 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6301 		req->ethertype = htons(ETH_P_IPV6);
6302 		req->ip_addr_type =
6303 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6304 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6305 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6306 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6307 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6308 	} else {
6309 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6310 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6311 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6312 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6313 	}
6314 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6315 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6316 		req->tunnel_type =
6317 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6318 	}
6319 
6320 	req->src_port = keys->ports.src;
6321 	req->src_port_mask = masks->ports.src;
6322 	req->dst_port = keys->ports.dst;
6323 	req->dst_port_mask = masks->ports.dst;
6324 
6325 	resp = hwrm_req_hold(bp, req);
6326 	rc = hwrm_req_send(bp, req);
6327 	if (!rc)
6328 		fltr->base.filter_id = resp->ntuple_filter_id;
6329 	hwrm_req_drop(bp, req);
6330 	return rc;
6331 }
6332 
6333 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6334 				     const u8 *mac_addr)
6335 {
6336 	struct bnxt_l2_filter *fltr;
6337 	struct bnxt_l2_key key;
6338 	int rc;
6339 
6340 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6341 	key.vlan = 0;
6342 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6343 	if (IS_ERR(fltr))
6344 		return PTR_ERR(fltr);
6345 
6346 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6347 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6348 	if (rc)
6349 		bnxt_del_l2_filter(bp, fltr);
6350 	else
6351 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6352 	return rc;
6353 }
6354 
6355 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6356 {
6357 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6358 
6359 	/* Any associated ntuple filters will also be cleared by firmware. */
6360 	for (i = 0; i < num_of_vnics; i++) {
6361 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6362 
6363 		for (j = 0; j < vnic->uc_filter_count; j++) {
6364 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6365 
6366 			bnxt_hwrm_l2_filter_free(bp, fltr);
6367 			bnxt_del_l2_filter(bp, fltr);
6368 		}
6369 		vnic->uc_filter_count = 0;
6370 	}
6371 }
6372 
6373 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6374 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6375 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6376 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6377 
6378 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6379 					   struct hwrm_vnic_tpa_cfg_input *req)
6380 {
6381 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6382 
6383 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6384 		return;
6385 
6386 	if (bp->vxlan_port)
6387 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6388 	if (bp->vxlan_gpe_port)
6389 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6390 	if (bp->nge_port)
6391 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6392 
6393 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6394 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6395 }
6396 
6397 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6398 			   u32 tpa_flags)
6399 {
6400 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6401 	struct hwrm_vnic_tpa_cfg_input *req;
6402 	int rc;
6403 
6404 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6405 		return 0;
6406 
6407 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6408 	if (rc)
6409 		return rc;
6410 
6411 	if (tpa_flags) {
6412 		u16 mss = bp->dev->mtu - 40;
6413 		u32 nsegs, n, segs = 0, flags;
6414 
6415 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6416 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6417 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6418 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6419 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6420 		if (tpa_flags & BNXT_FLAG_GRO)
6421 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6422 
6423 		req->flags = cpu_to_le32(flags);
6424 
6425 		req->enables =
6426 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6427 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6428 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6429 
6430 		/* Number of segs are log2 units, and first packet is not
6431 		 * included as part of this units.
6432 		 */
6433 		if (mss <= BNXT_RX_PAGE_SIZE) {
6434 			n = BNXT_RX_PAGE_SIZE / mss;
6435 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6436 		} else {
6437 			n = mss / BNXT_RX_PAGE_SIZE;
6438 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6439 				n++;
6440 			nsegs = (MAX_SKB_FRAGS - n) / n;
6441 		}
6442 
6443 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6444 			segs = MAX_TPA_SEGS_P5;
6445 			max_aggs = bp->max_tpa;
6446 		} else {
6447 			segs = ilog2(nsegs);
6448 		}
6449 		req->max_agg_segs = cpu_to_le16(segs);
6450 		req->max_aggs = cpu_to_le16(max_aggs);
6451 
6452 		req->min_agg_len = cpu_to_le32(512);
6453 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6454 	}
6455 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6456 
6457 	return hwrm_req_send(bp, req);
6458 }
6459 
6460 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6461 {
6462 	struct bnxt_ring_grp_info *grp_info;
6463 
6464 	grp_info = &bp->grp_info[ring->grp_idx];
6465 	return grp_info->cp_fw_ring_id;
6466 }
6467 
6468 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6469 {
6470 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6471 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6472 	else
6473 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6474 }
6475 
6476 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6477 {
6478 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6479 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6480 	else
6481 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6482 }
6483 
6484 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6485 {
6486 	int entries;
6487 
6488 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6489 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6490 	else
6491 		entries = HW_HASH_INDEX_SIZE;
6492 
6493 	bp->rss_indir_tbl_entries = entries;
6494 	bp->rss_indir_tbl =
6495 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6496 	if (!bp->rss_indir_tbl)
6497 		return -ENOMEM;
6498 
6499 	return 0;
6500 }
6501 
6502 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6503 				 struct ethtool_rxfh_context *rss_ctx)
6504 {
6505 	u16 max_rings, max_entries, pad, i;
6506 	u32 *rss_indir_tbl;
6507 
6508 	if (!bp->rx_nr_rings)
6509 		return;
6510 
6511 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6512 		max_rings = bp->rx_nr_rings - 1;
6513 	else
6514 		max_rings = bp->rx_nr_rings;
6515 
6516 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6517 	if (rss_ctx)
6518 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6519 	else
6520 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6521 
6522 	for (i = 0; i < max_entries; i++)
6523 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6524 
6525 	pad = bp->rss_indir_tbl_entries - max_entries;
6526 	if (pad)
6527 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6528 }
6529 
6530 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6531 {
6532 	u32 i, tbl_size, max_ring = 0;
6533 
6534 	if (!bp->rss_indir_tbl)
6535 		return 0;
6536 
6537 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6538 	for (i = 0; i < tbl_size; i++)
6539 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6540 	return max_ring;
6541 }
6542 
6543 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6544 {
6545 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6546 		if (!rx_rings)
6547 			return 0;
6548 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6549 					       BNXT_RSS_TABLE_ENTRIES_P5);
6550 	}
6551 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6552 		return 2;
6553 	return 1;
6554 }
6555 
6556 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6557 {
6558 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6559 	u16 i, j;
6560 
6561 	/* Fill the RSS indirection table with ring group ids */
6562 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6563 		if (!no_rss)
6564 			j = bp->rss_indir_tbl[i];
6565 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6566 	}
6567 }
6568 
6569 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6570 				    struct bnxt_vnic_info *vnic)
6571 {
6572 	__le16 *ring_tbl = vnic->rss_table;
6573 	struct bnxt_rx_ring_info *rxr;
6574 	u16 tbl_size, i;
6575 
6576 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6577 
6578 	for (i = 0; i < tbl_size; i++) {
6579 		u16 ring_id, j;
6580 
6581 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6582 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6583 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6584 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6585 		else
6586 			j = bp->rss_indir_tbl[i];
6587 		rxr = &bp->rx_ring[j];
6588 
6589 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6590 		*ring_tbl++ = cpu_to_le16(ring_id);
6591 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6592 		*ring_tbl++ = cpu_to_le16(ring_id);
6593 	}
6594 }
6595 
6596 static void
6597 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6598 			 struct bnxt_vnic_info *vnic)
6599 {
6600 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6601 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6602 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6603 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6604 	} else {
6605 		bnxt_fill_hw_rss_tbl(bp, vnic);
6606 	}
6607 
6608 	if (bp->rss_hash_delta) {
6609 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6610 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6611 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6612 		else
6613 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6614 	} else {
6615 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6616 	}
6617 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6618 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6619 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6620 }
6621 
6622 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6623 				  bool set_rss)
6624 {
6625 	struct hwrm_vnic_rss_cfg_input *req;
6626 	int rc;
6627 
6628 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6629 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6630 		return 0;
6631 
6632 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6633 	if (rc)
6634 		return rc;
6635 
6636 	if (set_rss)
6637 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6638 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6639 	return hwrm_req_send(bp, req);
6640 }
6641 
6642 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6643 				     struct bnxt_vnic_info *vnic, bool set_rss)
6644 {
6645 	struct hwrm_vnic_rss_cfg_input *req;
6646 	dma_addr_t ring_tbl_map;
6647 	u32 i, nr_ctxs;
6648 	int rc;
6649 
6650 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6651 	if (rc)
6652 		return rc;
6653 
6654 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6655 	if (!set_rss)
6656 		return hwrm_req_send(bp, req);
6657 
6658 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6659 	ring_tbl_map = vnic->rss_table_dma_addr;
6660 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6661 
6662 	hwrm_req_hold(bp, req);
6663 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6664 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6665 		req->ring_table_pair_index = i;
6666 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6667 		rc = hwrm_req_send(bp, req);
6668 		if (rc)
6669 			goto exit;
6670 	}
6671 
6672 exit:
6673 	hwrm_req_drop(bp, req);
6674 	return rc;
6675 }
6676 
6677 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6678 {
6679 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6680 	struct hwrm_vnic_rss_qcfg_output *resp;
6681 	struct hwrm_vnic_rss_qcfg_input *req;
6682 
6683 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6684 		return;
6685 
6686 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6687 	/* all contexts configured to same hash_type, zero always exists */
6688 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6689 	resp = hwrm_req_hold(bp, req);
6690 	if (!hwrm_req_send(bp, req)) {
6691 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6692 		bp->rss_hash_delta = 0;
6693 	}
6694 	hwrm_req_drop(bp, req);
6695 }
6696 
6697 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6698 {
6699 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6700 	struct hwrm_vnic_plcmodes_cfg_input *req;
6701 	int rc;
6702 
6703 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6704 	if (rc)
6705 		return rc;
6706 
6707 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6708 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6709 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6710 
6711 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6712 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6713 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6714 		req->enables |=
6715 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6716 		req->hds_threshold = cpu_to_le16(hds_thresh);
6717 	}
6718 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6719 	return hwrm_req_send(bp, req);
6720 }
6721 
6722 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6723 					struct bnxt_vnic_info *vnic,
6724 					u16 ctx_idx)
6725 {
6726 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6727 
6728 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6729 		return;
6730 
6731 	req->rss_cos_lb_ctx_id =
6732 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6733 
6734 	hwrm_req_send(bp, req);
6735 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6736 }
6737 
6738 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6739 {
6740 	int i, j;
6741 
6742 	for (i = 0; i < bp->nr_vnics; i++) {
6743 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6744 
6745 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6746 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6747 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6748 		}
6749 	}
6750 	bp->rsscos_nr_ctxs = 0;
6751 }
6752 
6753 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6754 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6755 {
6756 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6757 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6758 	int rc;
6759 
6760 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6761 	if (rc)
6762 		return rc;
6763 
6764 	resp = hwrm_req_hold(bp, req);
6765 	rc = hwrm_req_send(bp, req);
6766 	if (!rc)
6767 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6768 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6769 	hwrm_req_drop(bp, req);
6770 
6771 	return rc;
6772 }
6773 
6774 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6775 {
6776 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6777 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6778 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6779 }
6780 
6781 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6782 {
6783 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6784 	struct hwrm_vnic_cfg_input *req;
6785 	unsigned int ring = 0, grp_idx;
6786 	u16 def_vlan = 0;
6787 	int rc;
6788 
6789 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6790 	if (rc)
6791 		return rc;
6792 
6793 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6794 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6795 
6796 		req->default_rx_ring_id =
6797 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6798 		req->default_cmpl_ring_id =
6799 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6800 		req->enables =
6801 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6802 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6803 		goto vnic_mru;
6804 	}
6805 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6806 	/* Only RSS support for now TBD: COS & LB */
6807 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6808 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6809 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6810 					   VNIC_CFG_REQ_ENABLES_MRU);
6811 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6812 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6813 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6814 					   VNIC_CFG_REQ_ENABLES_MRU);
6815 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6816 	} else {
6817 		req->rss_rule = cpu_to_le16(0xffff);
6818 	}
6819 
6820 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6821 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6822 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6823 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6824 	} else {
6825 		req->cos_rule = cpu_to_le16(0xffff);
6826 	}
6827 
6828 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6829 		ring = 0;
6830 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6831 		ring = vnic->vnic_id - 1;
6832 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6833 		ring = bp->rx_nr_rings - 1;
6834 
6835 	grp_idx = bp->rx_ring[ring].bnapi->index;
6836 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6837 	req->lb_rule = cpu_to_le16(0xffff);
6838 vnic_mru:
6839 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6840 	req->mru = cpu_to_le16(vnic->mru);
6841 
6842 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6843 #ifdef CONFIG_BNXT_SRIOV
6844 	if (BNXT_VF(bp))
6845 		def_vlan = bp->vf.vlan;
6846 #endif
6847 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6848 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6849 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6850 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6851 
6852 	return hwrm_req_send(bp, req);
6853 }
6854 
6855 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6856 				    struct bnxt_vnic_info *vnic)
6857 {
6858 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6859 		struct hwrm_vnic_free_input *req;
6860 
6861 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6862 			return;
6863 
6864 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6865 
6866 		hwrm_req_send(bp, req);
6867 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6868 	}
6869 }
6870 
6871 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6872 {
6873 	u16 i;
6874 
6875 	for (i = 0; i < bp->nr_vnics; i++)
6876 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6877 }
6878 
6879 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6880 			 unsigned int start_rx_ring_idx,
6881 			 unsigned int nr_rings)
6882 {
6883 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6884 	struct hwrm_vnic_alloc_output *resp;
6885 	struct hwrm_vnic_alloc_input *req;
6886 	int rc;
6887 
6888 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6889 	if (rc)
6890 		return rc;
6891 
6892 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6893 		goto vnic_no_ring_grps;
6894 
6895 	/* map ring groups to this vnic */
6896 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6897 		grp_idx = bp->rx_ring[i].bnapi->index;
6898 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6899 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6900 				   j, nr_rings);
6901 			break;
6902 		}
6903 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6904 	}
6905 
6906 vnic_no_ring_grps:
6907 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6908 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6909 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6910 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6911 
6912 	resp = hwrm_req_hold(bp, req);
6913 	rc = hwrm_req_send(bp, req);
6914 	if (!rc)
6915 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6916 	hwrm_req_drop(bp, req);
6917 	return rc;
6918 }
6919 
6920 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6921 {
6922 	struct hwrm_vnic_qcaps_output *resp;
6923 	struct hwrm_vnic_qcaps_input *req;
6924 	int rc;
6925 
6926 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6927 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6928 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6929 	if (bp->hwrm_spec_code < 0x10600)
6930 		return 0;
6931 
6932 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6933 	if (rc)
6934 		return rc;
6935 
6936 	resp = hwrm_req_hold(bp, req);
6937 	rc = hwrm_req_send(bp, req);
6938 	if (!rc) {
6939 		u32 flags = le32_to_cpu(resp->flags);
6940 
6941 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6942 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6943 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6944 		if (flags &
6945 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6946 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6947 
6948 		/* Older P5 fw before EXT_HW_STATS support did not set
6949 		 * VLAN_STRIP_CAP properly.
6950 		 */
6951 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6952 		    (BNXT_CHIP_P5(bp) &&
6953 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6954 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6955 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6956 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6957 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6958 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6959 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6960 		if (bp->max_tpa_v2) {
6961 			if (BNXT_CHIP_P5(bp))
6962 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6963 			else
6964 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6965 		}
6966 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6967 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6968 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6969 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6970 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6971 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6972 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6973 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6974 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6975 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6976 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPV6_FLOW_LABEL_CAP)
6977 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
6978 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6979 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6980 	}
6981 	hwrm_req_drop(bp, req);
6982 	return rc;
6983 }
6984 
6985 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6986 {
6987 	struct hwrm_ring_grp_alloc_output *resp;
6988 	struct hwrm_ring_grp_alloc_input *req;
6989 	int rc;
6990 	u16 i;
6991 
6992 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6993 		return 0;
6994 
6995 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6996 	if (rc)
6997 		return rc;
6998 
6999 	resp = hwrm_req_hold(bp, req);
7000 	for (i = 0; i < bp->rx_nr_rings; i++) {
7001 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
7002 
7003 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7004 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7005 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7006 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7007 
7008 		rc = hwrm_req_send(bp, req);
7009 
7010 		if (rc)
7011 			break;
7012 
7013 		bp->grp_info[grp_idx].fw_grp_id =
7014 			le32_to_cpu(resp->ring_group_id);
7015 	}
7016 	hwrm_req_drop(bp, req);
7017 	return rc;
7018 }
7019 
7020 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7021 {
7022 	struct hwrm_ring_grp_free_input *req;
7023 	u16 i;
7024 
7025 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7026 		return;
7027 
7028 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7029 		return;
7030 
7031 	hwrm_req_hold(bp, req);
7032 	for (i = 0; i < bp->cp_nr_rings; i++) {
7033 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7034 			continue;
7035 		req->ring_group_id =
7036 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7037 
7038 		hwrm_req_send(bp, req);
7039 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7040 	}
7041 	hwrm_req_drop(bp, req);
7042 }
7043 
7044 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7045 				       struct hwrm_ring_alloc_input *req,
7046 				       struct bnxt_ring_struct *ring)
7047 {
7048 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7049 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7050 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7051 
7052 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7053 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7054 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7055 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7056 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7057 	} else {
7058 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7059 		if (NET_IP_ALIGN == 2)
7060 			req->flags =
7061 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7062 	}
7063 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7064 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7065 	req->enables |= cpu_to_le32(enables);
7066 }
7067 
7068 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7069 				    struct bnxt_ring_struct *ring,
7070 				    u32 ring_type, u32 map_index)
7071 {
7072 	struct hwrm_ring_alloc_output *resp;
7073 	struct hwrm_ring_alloc_input *req;
7074 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7075 	struct bnxt_ring_grp_info *grp_info;
7076 	int rc, err = 0;
7077 	u16 ring_id;
7078 
7079 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7080 	if (rc)
7081 		goto exit;
7082 
7083 	req->enables = 0;
7084 	if (rmem->nr_pages > 1) {
7085 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7086 		/* Page size is in log2 units */
7087 		req->page_size = BNXT_PAGE_SHIFT;
7088 		req->page_tbl_depth = 1;
7089 	} else {
7090 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7091 	}
7092 	req->fbo = 0;
7093 	/* Association of ring index with doorbell index and MSIX number */
7094 	req->logical_id = cpu_to_le16(map_index);
7095 
7096 	switch (ring_type) {
7097 	case HWRM_RING_ALLOC_TX: {
7098 		struct bnxt_tx_ring_info *txr;
7099 		u16 flags = 0;
7100 
7101 		txr = container_of(ring, struct bnxt_tx_ring_info,
7102 				   tx_ring_struct);
7103 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7104 		/* Association of transmit ring with completion ring */
7105 		grp_info = &bp->grp_info[ring->grp_idx];
7106 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7107 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7108 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7109 		req->queue_id = cpu_to_le16(ring->queue_id);
7110 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7111 			req->cmpl_coal_cnt =
7112 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7113 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7114 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7115 		req->flags = cpu_to_le16(flags);
7116 		break;
7117 	}
7118 	case HWRM_RING_ALLOC_RX:
7119 	case HWRM_RING_ALLOC_AGG:
7120 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7121 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7122 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7123 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7124 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7125 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7126 		break;
7127 	case HWRM_RING_ALLOC_CMPL:
7128 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7129 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7130 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7131 			/* Association of cp ring with nq */
7132 			grp_info = &bp->grp_info[map_index];
7133 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7134 			req->cq_handle = cpu_to_le64(ring->handle);
7135 			req->enables |= cpu_to_le32(
7136 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7137 		} else {
7138 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7139 		}
7140 		break;
7141 	case HWRM_RING_ALLOC_NQ:
7142 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7143 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7144 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7145 		break;
7146 	default:
7147 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7148 			   ring_type);
7149 		return -EINVAL;
7150 	}
7151 
7152 	resp = hwrm_req_hold(bp, req);
7153 	rc = hwrm_req_send(bp, req);
7154 	err = le16_to_cpu(resp->error_code);
7155 	ring_id = le16_to_cpu(resp->ring_id);
7156 	hwrm_req_drop(bp, req);
7157 
7158 exit:
7159 	if (rc || err) {
7160 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7161 			   ring_type, rc, err);
7162 		return -EIO;
7163 	}
7164 	ring->fw_ring_id = ring_id;
7165 	return rc;
7166 }
7167 
7168 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7169 {
7170 	int rc;
7171 
7172 	if (BNXT_PF(bp)) {
7173 		struct hwrm_func_cfg_input *req;
7174 
7175 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7176 		if (rc)
7177 			return rc;
7178 
7179 		req->fid = cpu_to_le16(0xffff);
7180 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7181 		req->async_event_cr = cpu_to_le16(idx);
7182 		return hwrm_req_send(bp, req);
7183 	} else {
7184 		struct hwrm_func_vf_cfg_input *req;
7185 
7186 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7187 		if (rc)
7188 			return rc;
7189 
7190 		req->enables =
7191 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7192 		req->async_event_cr = cpu_to_le16(idx);
7193 		return hwrm_req_send(bp, req);
7194 	}
7195 }
7196 
7197 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7198 			     u32 ring_type)
7199 {
7200 	switch (ring_type) {
7201 	case HWRM_RING_ALLOC_TX:
7202 		db->db_ring_mask = bp->tx_ring_mask;
7203 		break;
7204 	case HWRM_RING_ALLOC_RX:
7205 		db->db_ring_mask = bp->rx_ring_mask;
7206 		break;
7207 	case HWRM_RING_ALLOC_AGG:
7208 		db->db_ring_mask = bp->rx_agg_ring_mask;
7209 		break;
7210 	case HWRM_RING_ALLOC_CMPL:
7211 	case HWRM_RING_ALLOC_NQ:
7212 		db->db_ring_mask = bp->cp_ring_mask;
7213 		break;
7214 	}
7215 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7216 		db->db_epoch_mask = db->db_ring_mask + 1;
7217 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7218 	}
7219 }
7220 
7221 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7222 			u32 map_idx, u32 xid)
7223 {
7224 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7225 		switch (ring_type) {
7226 		case HWRM_RING_ALLOC_TX:
7227 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7228 			break;
7229 		case HWRM_RING_ALLOC_RX:
7230 		case HWRM_RING_ALLOC_AGG:
7231 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7232 			break;
7233 		case HWRM_RING_ALLOC_CMPL:
7234 			db->db_key64 = DBR_PATH_L2;
7235 			break;
7236 		case HWRM_RING_ALLOC_NQ:
7237 			db->db_key64 = DBR_PATH_L2;
7238 			break;
7239 		}
7240 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7241 
7242 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7243 			db->db_key64 |= DBR_VALID;
7244 
7245 		db->doorbell = bp->bar1 + bp->db_offset;
7246 	} else {
7247 		db->doorbell = bp->bar1 + map_idx * 0x80;
7248 		switch (ring_type) {
7249 		case HWRM_RING_ALLOC_TX:
7250 			db->db_key32 = DB_KEY_TX;
7251 			break;
7252 		case HWRM_RING_ALLOC_RX:
7253 		case HWRM_RING_ALLOC_AGG:
7254 			db->db_key32 = DB_KEY_RX;
7255 			break;
7256 		case HWRM_RING_ALLOC_CMPL:
7257 			db->db_key32 = DB_KEY_CP;
7258 			break;
7259 		}
7260 	}
7261 	bnxt_set_db_mask(bp, db, ring_type);
7262 }
7263 
7264 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7265 				   struct bnxt_rx_ring_info *rxr)
7266 {
7267 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7268 	struct bnxt_napi *bnapi = rxr->bnapi;
7269 	u32 type = HWRM_RING_ALLOC_RX;
7270 	u32 map_idx = bnapi->index;
7271 	int rc;
7272 
7273 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7274 	if (rc)
7275 		return rc;
7276 
7277 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7278 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7279 
7280 	return 0;
7281 }
7282 
7283 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7284 				       struct bnxt_rx_ring_info *rxr)
7285 {
7286 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7287 	u32 type = HWRM_RING_ALLOC_AGG;
7288 	u32 grp_idx = ring->grp_idx;
7289 	u32 map_idx;
7290 	int rc;
7291 
7292 	map_idx = grp_idx + bp->rx_nr_rings;
7293 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7294 	if (rc)
7295 		return rc;
7296 
7297 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7298 		    ring->fw_ring_id);
7299 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7300 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7301 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7302 
7303 	return 0;
7304 }
7305 
7306 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7307 				      struct bnxt_cp_ring_info *cpr)
7308 {
7309 	const u32 type = HWRM_RING_ALLOC_CMPL;
7310 	struct bnxt_napi *bnapi = cpr->bnapi;
7311 	struct bnxt_ring_struct *ring;
7312 	u32 map_idx = bnapi->index;
7313 	int rc;
7314 
7315 	ring = &cpr->cp_ring_struct;
7316 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7317 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7318 	if (rc)
7319 		return rc;
7320 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7321 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7322 	return 0;
7323 }
7324 
7325 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7326 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7327 {
7328 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7329 	const u32 type = HWRM_RING_ALLOC_TX;
7330 	int rc;
7331 
7332 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7333 	if (rc)
7334 		return rc;
7335 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7336 	return 0;
7337 }
7338 
7339 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7340 {
7341 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7342 	int i, rc = 0;
7343 	u32 type;
7344 
7345 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7346 		type = HWRM_RING_ALLOC_NQ;
7347 	else
7348 		type = HWRM_RING_ALLOC_CMPL;
7349 	for (i = 0; i < bp->cp_nr_rings; i++) {
7350 		struct bnxt_napi *bnapi = bp->bnapi[i];
7351 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7352 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7353 		u32 map_idx = ring->map_idx;
7354 		unsigned int vector;
7355 
7356 		vector = bp->irq_tbl[map_idx].vector;
7357 		disable_irq_nosync(vector);
7358 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7359 		if (rc) {
7360 			enable_irq(vector);
7361 			goto err_out;
7362 		}
7363 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7364 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7365 		enable_irq(vector);
7366 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7367 
7368 		if (!i) {
7369 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7370 			if (rc)
7371 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7372 		}
7373 	}
7374 
7375 	for (i = 0; i < bp->tx_nr_rings; i++) {
7376 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7377 
7378 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7379 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7380 			if (rc)
7381 				goto err_out;
7382 		}
7383 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7384 		if (rc)
7385 			goto err_out;
7386 	}
7387 
7388 	for (i = 0; i < bp->rx_nr_rings; i++) {
7389 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7390 
7391 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7392 		if (rc)
7393 			goto err_out;
7394 		/* If we have agg rings, post agg buffers first. */
7395 		if (!agg_rings)
7396 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7397 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7398 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7399 			if (rc)
7400 				goto err_out;
7401 		}
7402 	}
7403 
7404 	if (agg_rings) {
7405 		for (i = 0; i < bp->rx_nr_rings; i++) {
7406 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7407 			if (rc)
7408 				goto err_out;
7409 		}
7410 	}
7411 err_out:
7412 	return rc;
7413 }
7414 
7415 static void bnxt_cancel_dim(struct bnxt *bp)
7416 {
7417 	int i;
7418 
7419 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7420 	 * if NAPI is enabled.
7421 	 */
7422 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7423 		return;
7424 
7425 	/* Make sure NAPI sees that the VNIC is disabled */
7426 	synchronize_net();
7427 	for (i = 0; i < bp->rx_nr_rings; i++) {
7428 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7429 		struct bnxt_napi *bnapi = rxr->bnapi;
7430 
7431 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7432 	}
7433 }
7434 
7435 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7436 				   struct bnxt_ring_struct *ring,
7437 				   u32 ring_type, int cmpl_ring_id)
7438 {
7439 	struct hwrm_ring_free_output *resp;
7440 	struct hwrm_ring_free_input *req;
7441 	u16 error_code = 0;
7442 	int rc;
7443 
7444 	if (BNXT_NO_FW_ACCESS(bp))
7445 		return 0;
7446 
7447 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7448 	if (rc)
7449 		goto exit;
7450 
7451 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7452 	req->ring_type = ring_type;
7453 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7454 
7455 	resp = hwrm_req_hold(bp, req);
7456 	rc = hwrm_req_send(bp, req);
7457 	error_code = le16_to_cpu(resp->error_code);
7458 	hwrm_req_drop(bp, req);
7459 exit:
7460 	if (rc || error_code) {
7461 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7462 			   ring_type, rc, error_code);
7463 		return -EIO;
7464 	}
7465 	return 0;
7466 }
7467 
7468 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7469 				   struct bnxt_tx_ring_info *txr,
7470 				   bool close_path)
7471 {
7472 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7473 	u32 cmpl_ring_id;
7474 
7475 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7476 		return;
7477 
7478 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7479 		       INVALID_HW_RING_ID;
7480 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7481 				cmpl_ring_id);
7482 	ring->fw_ring_id = INVALID_HW_RING_ID;
7483 }
7484 
7485 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7486 				   struct bnxt_rx_ring_info *rxr,
7487 				   bool close_path)
7488 {
7489 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7490 	u32 grp_idx = rxr->bnapi->index;
7491 	u32 cmpl_ring_id;
7492 
7493 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7494 		return;
7495 
7496 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7497 	hwrm_ring_free_send_msg(bp, ring,
7498 				RING_FREE_REQ_RING_TYPE_RX,
7499 				close_path ? cmpl_ring_id :
7500 				INVALID_HW_RING_ID);
7501 	ring->fw_ring_id = INVALID_HW_RING_ID;
7502 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7503 }
7504 
7505 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7506 				       struct bnxt_rx_ring_info *rxr,
7507 				       bool close_path)
7508 {
7509 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7510 	u32 grp_idx = rxr->bnapi->index;
7511 	u32 type, cmpl_ring_id;
7512 
7513 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7514 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7515 	else
7516 		type = RING_FREE_REQ_RING_TYPE_RX;
7517 
7518 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7519 		return;
7520 
7521 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7522 	hwrm_ring_free_send_msg(bp, ring, type,
7523 				close_path ? cmpl_ring_id :
7524 				INVALID_HW_RING_ID);
7525 	ring->fw_ring_id = INVALID_HW_RING_ID;
7526 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7527 }
7528 
7529 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7530 				   struct bnxt_cp_ring_info *cpr)
7531 {
7532 	struct bnxt_ring_struct *ring;
7533 
7534 	ring = &cpr->cp_ring_struct;
7535 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7536 		return;
7537 
7538 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7539 				INVALID_HW_RING_ID);
7540 	ring->fw_ring_id = INVALID_HW_RING_ID;
7541 }
7542 
7543 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7544 {
7545 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7546 	int i, size = ring->ring_mem.page_size;
7547 
7548 	cpr->cp_raw_cons = 0;
7549 	cpr->toggle = 0;
7550 
7551 	for (i = 0; i < bp->cp_nr_pages; i++)
7552 		if (cpr->cp_desc_ring[i])
7553 			memset(cpr->cp_desc_ring[i], 0, size);
7554 }
7555 
7556 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7557 {
7558 	u32 type;
7559 	int i;
7560 
7561 	if (!bp->bnapi)
7562 		return;
7563 
7564 	for (i = 0; i < bp->tx_nr_rings; i++)
7565 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7566 
7567 	bnxt_cancel_dim(bp);
7568 	for (i = 0; i < bp->rx_nr_rings; i++) {
7569 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7570 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7571 	}
7572 
7573 	/* The completion rings are about to be freed.  After that the
7574 	 * IRQ doorbell will not work anymore.  So we need to disable
7575 	 * IRQ here.
7576 	 */
7577 	bnxt_disable_int_sync(bp);
7578 
7579 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7580 		type = RING_FREE_REQ_RING_TYPE_NQ;
7581 	else
7582 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7583 	for (i = 0; i < bp->cp_nr_rings; i++) {
7584 		struct bnxt_napi *bnapi = bp->bnapi[i];
7585 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7586 		struct bnxt_ring_struct *ring;
7587 		int j;
7588 
7589 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7590 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7591 
7592 		ring = &cpr->cp_ring_struct;
7593 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7594 			hwrm_ring_free_send_msg(bp, ring, type,
7595 						INVALID_HW_RING_ID);
7596 			ring->fw_ring_id = INVALID_HW_RING_ID;
7597 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7598 		}
7599 	}
7600 }
7601 
7602 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7603 			     bool shared);
7604 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7605 			   bool shared);
7606 
7607 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7608 {
7609 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7610 	struct hwrm_func_qcfg_output *resp;
7611 	struct hwrm_func_qcfg_input *req;
7612 	int rc;
7613 
7614 	if (bp->hwrm_spec_code < 0x10601)
7615 		return 0;
7616 
7617 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7618 	if (rc)
7619 		return rc;
7620 
7621 	req->fid = cpu_to_le16(0xffff);
7622 	resp = hwrm_req_hold(bp, req);
7623 	rc = hwrm_req_send(bp, req);
7624 	if (rc) {
7625 		hwrm_req_drop(bp, req);
7626 		return rc;
7627 	}
7628 
7629 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7630 	if (BNXT_NEW_RM(bp)) {
7631 		u16 cp, stats;
7632 
7633 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7634 		hw_resc->resv_hw_ring_grps =
7635 			le32_to_cpu(resp->alloc_hw_ring_grps);
7636 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7637 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7638 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7639 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7640 		hw_resc->resv_irqs = cp;
7641 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7642 			int rx = hw_resc->resv_rx_rings;
7643 			int tx = hw_resc->resv_tx_rings;
7644 
7645 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7646 				rx >>= 1;
7647 			if (cp < (rx + tx)) {
7648 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7649 				if (rc)
7650 					goto get_rings_exit;
7651 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7652 					rx <<= 1;
7653 				hw_resc->resv_rx_rings = rx;
7654 				hw_resc->resv_tx_rings = tx;
7655 			}
7656 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7657 			hw_resc->resv_hw_ring_grps = rx;
7658 		}
7659 		hw_resc->resv_cp_rings = cp;
7660 		hw_resc->resv_stat_ctxs = stats;
7661 	}
7662 get_rings_exit:
7663 	hwrm_req_drop(bp, req);
7664 	return rc;
7665 }
7666 
7667 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7668 {
7669 	struct hwrm_func_qcfg_output *resp;
7670 	struct hwrm_func_qcfg_input *req;
7671 	int rc;
7672 
7673 	if (bp->hwrm_spec_code < 0x10601)
7674 		return 0;
7675 
7676 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7677 	if (rc)
7678 		return rc;
7679 
7680 	req->fid = cpu_to_le16(fid);
7681 	resp = hwrm_req_hold(bp, req);
7682 	rc = hwrm_req_send(bp, req);
7683 	if (!rc)
7684 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7685 
7686 	hwrm_req_drop(bp, req);
7687 	return rc;
7688 }
7689 
7690 static bool bnxt_rfs_supported(struct bnxt *bp);
7691 
7692 static struct hwrm_func_cfg_input *
7693 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7694 {
7695 	struct hwrm_func_cfg_input *req;
7696 	u32 enables = 0;
7697 
7698 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7699 		return NULL;
7700 
7701 	req->fid = cpu_to_le16(0xffff);
7702 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7703 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7704 	if (BNXT_NEW_RM(bp)) {
7705 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7706 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7707 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7708 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7709 			enables |= hwr->cp_p5 ?
7710 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7711 		} else {
7712 			enables |= hwr->cp ?
7713 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7714 			enables |= hwr->grp ?
7715 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7716 		}
7717 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7718 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7719 					  0;
7720 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7721 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7722 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7723 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7724 			req->num_msix = cpu_to_le16(hwr->cp);
7725 		} else {
7726 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7727 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7728 		}
7729 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7730 		req->num_vnics = cpu_to_le16(hwr->vnic);
7731 	}
7732 	req->enables = cpu_to_le32(enables);
7733 	return req;
7734 }
7735 
7736 static struct hwrm_func_vf_cfg_input *
7737 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7738 {
7739 	struct hwrm_func_vf_cfg_input *req;
7740 	u32 enables = 0;
7741 
7742 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7743 		return NULL;
7744 
7745 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7746 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7747 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7748 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7749 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7750 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7751 		enables |= hwr->cp_p5 ?
7752 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7753 	} else {
7754 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7755 		enables |= hwr->grp ?
7756 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7757 	}
7758 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7759 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7760 
7761 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7762 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7763 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7764 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7765 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7766 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7767 	} else {
7768 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7769 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7770 	}
7771 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7772 	req->num_vnics = cpu_to_le16(hwr->vnic);
7773 
7774 	req->enables = cpu_to_le32(enables);
7775 	return req;
7776 }
7777 
7778 static int
7779 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7780 {
7781 	struct hwrm_func_cfg_input *req;
7782 	int rc;
7783 
7784 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7785 	if (!req)
7786 		return -ENOMEM;
7787 
7788 	if (!req->enables) {
7789 		hwrm_req_drop(bp, req);
7790 		return 0;
7791 	}
7792 
7793 	rc = hwrm_req_send(bp, req);
7794 	if (rc)
7795 		return rc;
7796 
7797 	if (bp->hwrm_spec_code < 0x10601)
7798 		bp->hw_resc.resv_tx_rings = hwr->tx;
7799 
7800 	return bnxt_hwrm_get_rings(bp);
7801 }
7802 
7803 static int
7804 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7805 {
7806 	struct hwrm_func_vf_cfg_input *req;
7807 	int rc;
7808 
7809 	if (!BNXT_NEW_RM(bp)) {
7810 		bp->hw_resc.resv_tx_rings = hwr->tx;
7811 		return 0;
7812 	}
7813 
7814 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7815 	if (!req)
7816 		return -ENOMEM;
7817 
7818 	rc = hwrm_req_send(bp, req);
7819 	if (rc)
7820 		return rc;
7821 
7822 	return bnxt_hwrm_get_rings(bp);
7823 }
7824 
7825 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7826 {
7827 	if (BNXT_PF(bp))
7828 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7829 	else
7830 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7831 }
7832 
7833 int bnxt_nq_rings_in_use(struct bnxt *bp)
7834 {
7835 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7836 }
7837 
7838 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7839 {
7840 	int cp;
7841 
7842 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7843 		return bnxt_nq_rings_in_use(bp);
7844 
7845 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7846 	return cp;
7847 }
7848 
7849 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7850 {
7851 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7852 }
7853 
7854 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7855 {
7856 	if (!hwr->grp)
7857 		return 0;
7858 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7859 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7860 
7861 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7862 			rss_ctx *= hwr->vnic;
7863 		return rss_ctx;
7864 	}
7865 	if (BNXT_VF(bp))
7866 		return BNXT_VF_MAX_RSS_CTX;
7867 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7868 		return hwr->grp + 1;
7869 	return 1;
7870 }
7871 
7872 /* Check if a default RSS map needs to be setup.  This function is only
7873  * used on older firmware that does not require reserving RX rings.
7874  */
7875 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7876 {
7877 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7878 
7879 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7880 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7881 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7882 		if (!netif_is_rxfh_configured(bp->dev))
7883 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7884 	}
7885 }
7886 
7887 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7888 {
7889 	if (bp->flags & BNXT_FLAG_RFS) {
7890 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7891 			return 2 + bp->num_rss_ctx;
7892 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7893 			return rx_rings + 1;
7894 	}
7895 	return 1;
7896 }
7897 
7898 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7899 {
7900 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7901 	int cp = bnxt_cp_rings_in_use(bp);
7902 	int nq = bnxt_nq_rings_in_use(bp);
7903 	int rx = bp->rx_nr_rings, stat;
7904 	int vnic, grp = rx;
7905 
7906 	/* Old firmware does not need RX ring reservations but we still
7907 	 * need to setup a default RSS map when needed.  With new firmware
7908 	 * we go through RX ring reservations first and then set up the
7909 	 * RSS map for the successfully reserved RX rings when needed.
7910 	 */
7911 	if (!BNXT_NEW_RM(bp))
7912 		bnxt_check_rss_tbl_no_rmgr(bp);
7913 
7914 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7915 	    bp->hwrm_spec_code >= 0x10601)
7916 		return true;
7917 
7918 	if (!BNXT_NEW_RM(bp))
7919 		return false;
7920 
7921 	vnic = bnxt_get_total_vnics(bp, rx);
7922 
7923 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7924 		rx <<= 1;
7925 	stat = bnxt_get_func_stat_ctxs(bp);
7926 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7927 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7928 	    (hw_resc->resv_hw_ring_grps != grp &&
7929 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7930 		return true;
7931 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7932 	    hw_resc->resv_irqs != nq)
7933 		return true;
7934 	return false;
7935 }
7936 
7937 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7938 {
7939 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7940 
7941 	hwr->tx = hw_resc->resv_tx_rings;
7942 	if (BNXT_NEW_RM(bp)) {
7943 		hwr->rx = hw_resc->resv_rx_rings;
7944 		hwr->cp = hw_resc->resv_irqs;
7945 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7946 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7947 		hwr->grp = hw_resc->resv_hw_ring_grps;
7948 		hwr->vnic = hw_resc->resv_vnics;
7949 		hwr->stat = hw_resc->resv_stat_ctxs;
7950 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7951 	}
7952 }
7953 
7954 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7955 {
7956 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7957 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7958 }
7959 
7960 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7961 
7962 static int __bnxt_reserve_rings(struct bnxt *bp)
7963 {
7964 	struct bnxt_hw_rings hwr = {0};
7965 	int rx_rings, old_rx_rings, rc;
7966 	int cp = bp->cp_nr_rings;
7967 	int ulp_msix = 0;
7968 	bool sh = false;
7969 	int tx_cp;
7970 
7971 	if (!bnxt_need_reserve_rings(bp))
7972 		return 0;
7973 
7974 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7975 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7976 		if (!ulp_msix)
7977 			bnxt_set_ulp_stat_ctxs(bp, 0);
7978 
7979 		if (ulp_msix > bp->ulp_num_msix_want)
7980 			ulp_msix = bp->ulp_num_msix_want;
7981 		hwr.cp = cp + ulp_msix;
7982 	} else {
7983 		hwr.cp = bnxt_nq_rings_in_use(bp);
7984 	}
7985 
7986 	hwr.tx = bp->tx_nr_rings;
7987 	hwr.rx = bp->rx_nr_rings;
7988 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7989 		sh = true;
7990 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7991 		hwr.cp_p5 = hwr.rx + hwr.tx;
7992 
7993 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7994 
7995 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7996 		hwr.rx <<= 1;
7997 	hwr.grp = bp->rx_nr_rings;
7998 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7999 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
8000 	old_rx_rings = bp->hw_resc.resv_rx_rings;
8001 
8002 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
8003 	if (rc)
8004 		return rc;
8005 
8006 	bnxt_copy_reserved_rings(bp, &hwr);
8007 
8008 	rx_rings = hwr.rx;
8009 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8010 		if (hwr.rx >= 2) {
8011 			rx_rings = hwr.rx >> 1;
8012 		} else {
8013 			if (netif_running(bp->dev))
8014 				return -ENOMEM;
8015 
8016 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8017 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8018 			bp->dev->hw_features &= ~NETIF_F_LRO;
8019 			bp->dev->features &= ~NETIF_F_LRO;
8020 			bnxt_set_ring_params(bp);
8021 		}
8022 	}
8023 	rx_rings = min_t(int, rx_rings, hwr.grp);
8024 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8025 	if (bnxt_ulp_registered(bp->edev) &&
8026 	    hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8027 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8028 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8029 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8030 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8031 		hwr.rx = rx_rings << 1;
8032 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8033 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8034 	if (hwr.tx != bp->tx_nr_rings) {
8035 		netdev_warn(bp->dev,
8036 			    "Able to reserve only %d out of %d requested TX rings\n",
8037 			    hwr.tx, bp->tx_nr_rings);
8038 	}
8039 	bp->tx_nr_rings = hwr.tx;
8040 
8041 	/* If we cannot reserve all the RX rings, reset the RSS map only
8042 	 * if absolutely necessary
8043 	 */
8044 	if (rx_rings != bp->rx_nr_rings) {
8045 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8046 			    rx_rings, bp->rx_nr_rings);
8047 		if (netif_is_rxfh_configured(bp->dev) &&
8048 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8049 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8050 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8051 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8052 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8053 		}
8054 	}
8055 	bp->rx_nr_rings = rx_rings;
8056 	bp->cp_nr_rings = hwr.cp;
8057 
8058 	if (!bnxt_rings_ok(bp, &hwr))
8059 		return -ENOMEM;
8060 
8061 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8062 	    !netif_is_rxfh_configured(bp->dev))
8063 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8064 
8065 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8066 		int resv_msix, resv_ctx, ulp_ctxs;
8067 		struct bnxt_hw_resc *hw_resc;
8068 
8069 		hw_resc = &bp->hw_resc;
8070 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8071 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8072 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8073 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8074 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8075 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8076 	}
8077 
8078 	return rc;
8079 }
8080 
8081 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8082 {
8083 	struct hwrm_func_vf_cfg_input *req;
8084 	u32 flags;
8085 
8086 	if (!BNXT_NEW_RM(bp))
8087 		return 0;
8088 
8089 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8090 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8091 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8092 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8093 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8094 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8095 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8096 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8097 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8098 
8099 	req->flags = cpu_to_le32(flags);
8100 	return hwrm_req_send_silent(bp, req);
8101 }
8102 
8103 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8104 {
8105 	struct hwrm_func_cfg_input *req;
8106 	u32 flags;
8107 
8108 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8109 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8110 	if (BNXT_NEW_RM(bp)) {
8111 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8112 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8113 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8114 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8115 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8116 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8117 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8118 		else
8119 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8120 	}
8121 
8122 	req->flags = cpu_to_le32(flags);
8123 	return hwrm_req_send_silent(bp, req);
8124 }
8125 
8126 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8127 {
8128 	if (bp->hwrm_spec_code < 0x10801)
8129 		return 0;
8130 
8131 	if (BNXT_PF(bp))
8132 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8133 
8134 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8135 }
8136 
8137 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8138 {
8139 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8140 	struct hwrm_ring_aggint_qcaps_output *resp;
8141 	struct hwrm_ring_aggint_qcaps_input *req;
8142 	int rc;
8143 
8144 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8145 	coal_cap->num_cmpl_dma_aggr_max = 63;
8146 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8147 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8148 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8149 	coal_cap->int_lat_tmr_min_max = 65535;
8150 	coal_cap->int_lat_tmr_max_max = 65535;
8151 	coal_cap->num_cmpl_aggr_int_max = 65535;
8152 	coal_cap->timer_units = 80;
8153 
8154 	if (bp->hwrm_spec_code < 0x10902)
8155 		return;
8156 
8157 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8158 		return;
8159 
8160 	resp = hwrm_req_hold(bp, req);
8161 	rc = hwrm_req_send_silent(bp, req);
8162 	if (!rc) {
8163 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8164 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8165 		coal_cap->num_cmpl_dma_aggr_max =
8166 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8167 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8168 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8169 		coal_cap->cmpl_aggr_dma_tmr_max =
8170 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8171 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8172 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8173 		coal_cap->int_lat_tmr_min_max =
8174 			le16_to_cpu(resp->int_lat_tmr_min_max);
8175 		coal_cap->int_lat_tmr_max_max =
8176 			le16_to_cpu(resp->int_lat_tmr_max_max);
8177 		coal_cap->num_cmpl_aggr_int_max =
8178 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8179 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8180 	}
8181 	hwrm_req_drop(bp, req);
8182 }
8183 
8184 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8185 {
8186 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8187 
8188 	return usec * 1000 / coal_cap->timer_units;
8189 }
8190 
8191 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8192 	struct bnxt_coal *hw_coal,
8193 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8194 {
8195 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8196 	u16 val, tmr, max, flags = hw_coal->flags;
8197 	u32 cmpl_params = coal_cap->cmpl_params;
8198 
8199 	max = hw_coal->bufs_per_record * 128;
8200 	if (hw_coal->budget)
8201 		max = hw_coal->bufs_per_record * hw_coal->budget;
8202 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8203 
8204 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8205 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8206 
8207 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8208 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8209 
8210 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8211 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8212 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8213 
8214 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8215 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8216 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8217 
8218 	/* min timer set to 1/2 of interrupt timer */
8219 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8220 		val = tmr / 2;
8221 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8222 		req->int_lat_tmr_min = cpu_to_le16(val);
8223 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8224 	}
8225 
8226 	/* buf timer set to 1/4 of interrupt timer */
8227 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8228 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8229 
8230 	if (cmpl_params &
8231 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8232 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8233 		val = clamp_t(u16, tmr, 1,
8234 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8235 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8236 		req->enables |=
8237 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8238 	}
8239 
8240 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8241 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8242 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8243 	req->flags = cpu_to_le16(flags);
8244 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8245 }
8246 
8247 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8248 				   struct bnxt_coal *hw_coal)
8249 {
8250 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8251 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8252 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8253 	u32 nq_params = coal_cap->nq_params;
8254 	u16 tmr;
8255 	int rc;
8256 
8257 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8258 		return 0;
8259 
8260 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8261 	if (rc)
8262 		return rc;
8263 
8264 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8265 	req->flags =
8266 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8267 
8268 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8269 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8270 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8271 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8272 	return hwrm_req_send(bp, req);
8273 }
8274 
8275 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8276 {
8277 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8278 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8279 	struct bnxt_coal coal;
8280 	int rc;
8281 
8282 	/* Tick values in micro seconds.
8283 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8284 	 */
8285 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8286 
8287 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8288 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8289 
8290 	if (!bnapi->rx_ring)
8291 		return -ENODEV;
8292 
8293 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8294 	if (rc)
8295 		return rc;
8296 
8297 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8298 
8299 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8300 
8301 	return hwrm_req_send(bp, req_rx);
8302 }
8303 
8304 static int
8305 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8306 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8307 {
8308 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8309 
8310 	req->ring_id = cpu_to_le16(ring_id);
8311 	return hwrm_req_send(bp, req);
8312 }
8313 
8314 static int
8315 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8316 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8317 {
8318 	struct bnxt_tx_ring_info *txr;
8319 	int i, rc;
8320 
8321 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8322 		u16 ring_id;
8323 
8324 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8325 		req->ring_id = cpu_to_le16(ring_id);
8326 		rc = hwrm_req_send(bp, req);
8327 		if (rc)
8328 			return rc;
8329 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8330 			return 0;
8331 	}
8332 	return 0;
8333 }
8334 
8335 int bnxt_hwrm_set_coal(struct bnxt *bp)
8336 {
8337 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8338 	int i, rc;
8339 
8340 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8341 	if (rc)
8342 		return rc;
8343 
8344 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8345 	if (rc) {
8346 		hwrm_req_drop(bp, req_rx);
8347 		return rc;
8348 	}
8349 
8350 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8351 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8352 
8353 	hwrm_req_hold(bp, req_rx);
8354 	hwrm_req_hold(bp, req_tx);
8355 	for (i = 0; i < bp->cp_nr_rings; i++) {
8356 		struct bnxt_napi *bnapi = bp->bnapi[i];
8357 		struct bnxt_coal *hw_coal;
8358 
8359 		if (!bnapi->rx_ring)
8360 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8361 		else
8362 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8363 		if (rc)
8364 			break;
8365 
8366 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8367 			continue;
8368 
8369 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8370 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8371 			if (rc)
8372 				break;
8373 		}
8374 		if (bnapi->rx_ring)
8375 			hw_coal = &bp->rx_coal;
8376 		else
8377 			hw_coal = &bp->tx_coal;
8378 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8379 	}
8380 	hwrm_req_drop(bp, req_rx);
8381 	hwrm_req_drop(bp, req_tx);
8382 	return rc;
8383 }
8384 
8385 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8386 {
8387 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8388 	struct hwrm_stat_ctx_free_input *req;
8389 	int i;
8390 
8391 	if (!bp->bnapi)
8392 		return;
8393 
8394 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8395 		return;
8396 
8397 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8398 		return;
8399 	if (BNXT_FW_MAJ(bp) <= 20) {
8400 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8401 			hwrm_req_drop(bp, req);
8402 			return;
8403 		}
8404 		hwrm_req_hold(bp, req0);
8405 	}
8406 	hwrm_req_hold(bp, req);
8407 	for (i = 0; i < bp->cp_nr_rings; i++) {
8408 		struct bnxt_napi *bnapi = bp->bnapi[i];
8409 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8410 
8411 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8412 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8413 			if (req0) {
8414 				req0->stat_ctx_id = req->stat_ctx_id;
8415 				hwrm_req_send(bp, req0);
8416 			}
8417 			hwrm_req_send(bp, req);
8418 
8419 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8420 		}
8421 	}
8422 	hwrm_req_drop(bp, req);
8423 	if (req0)
8424 		hwrm_req_drop(bp, req0);
8425 }
8426 
8427 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8428 {
8429 	struct hwrm_stat_ctx_alloc_output *resp;
8430 	struct hwrm_stat_ctx_alloc_input *req;
8431 	int rc, i;
8432 
8433 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8434 		return 0;
8435 
8436 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8437 	if (rc)
8438 		return rc;
8439 
8440 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8441 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8442 
8443 	resp = hwrm_req_hold(bp, req);
8444 	for (i = 0; i < bp->cp_nr_rings; i++) {
8445 		struct bnxt_napi *bnapi = bp->bnapi[i];
8446 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8447 
8448 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8449 
8450 		rc = hwrm_req_send(bp, req);
8451 		if (rc)
8452 			break;
8453 
8454 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8455 
8456 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8457 	}
8458 	hwrm_req_drop(bp, req);
8459 	return rc;
8460 }
8461 
8462 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8463 {
8464 	struct hwrm_func_qcfg_output *resp;
8465 	struct hwrm_func_qcfg_input *req;
8466 	u16 flags;
8467 	int rc;
8468 
8469 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8470 	if (rc)
8471 		return rc;
8472 
8473 	req->fid = cpu_to_le16(0xffff);
8474 	resp = hwrm_req_hold(bp, req);
8475 	rc = hwrm_req_send(bp, req);
8476 	if (rc)
8477 		goto func_qcfg_exit;
8478 
8479 	flags = le16_to_cpu(resp->flags);
8480 #ifdef CONFIG_BNXT_SRIOV
8481 	if (BNXT_VF(bp)) {
8482 		struct bnxt_vf_info *vf = &bp->vf;
8483 
8484 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8485 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8486 			vf->flags |= BNXT_VF_TRUST;
8487 		else
8488 			vf->flags &= ~BNXT_VF_TRUST;
8489 	} else {
8490 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8491 	}
8492 #endif
8493 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8494 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8495 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8496 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8497 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8498 	}
8499 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8500 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8501 
8502 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8503 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8504 
8505 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8506 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8507 
8508 	switch (resp->port_partition_type) {
8509 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8510 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8511 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8512 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8513 		bp->port_partition_type = resp->port_partition_type;
8514 		break;
8515 	}
8516 	if (bp->hwrm_spec_code < 0x10707 ||
8517 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8518 		bp->br_mode = BRIDGE_MODE_VEB;
8519 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8520 		bp->br_mode = BRIDGE_MODE_VEPA;
8521 	else
8522 		bp->br_mode = BRIDGE_MODE_UNDEF;
8523 
8524 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8525 	if (!bp->max_mtu)
8526 		bp->max_mtu = BNXT_MAX_MTU;
8527 
8528 	if (bp->db_size)
8529 		goto func_qcfg_exit;
8530 
8531 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8532 	if (BNXT_CHIP_P5(bp)) {
8533 		if (BNXT_PF(bp))
8534 			bp->db_offset = DB_PF_OFFSET_P5;
8535 		else
8536 			bp->db_offset = DB_VF_OFFSET_P5;
8537 	}
8538 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8539 				 1024);
8540 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8541 	    bp->db_size <= bp->db_offset)
8542 		bp->db_size = pci_resource_len(bp->pdev, 2);
8543 
8544 func_qcfg_exit:
8545 	hwrm_req_drop(bp, req);
8546 	return rc;
8547 }
8548 
8549 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8550 				      u8 init_val, u8 init_offset,
8551 				      bool init_mask_set)
8552 {
8553 	ctxm->init_value = init_val;
8554 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8555 	if (init_mask_set)
8556 		ctxm->init_offset = init_offset * 4;
8557 	else
8558 		ctxm->init_value = 0;
8559 }
8560 
8561 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8562 {
8563 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8564 	u16 type;
8565 
8566 	for (type = 0; type < ctx_max; type++) {
8567 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8568 		int n = 1;
8569 
8570 		if (!ctxm->max_entries || ctxm->pg_info)
8571 			continue;
8572 
8573 		if (ctxm->instance_bmap)
8574 			n = hweight32(ctxm->instance_bmap);
8575 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8576 		if (!ctxm->pg_info)
8577 			return -ENOMEM;
8578 	}
8579 	return 0;
8580 }
8581 
8582 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8583 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8584 
8585 #define BNXT_CTX_INIT_VALID(flags)	\
8586 	(!!((flags) &			\
8587 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8588 
8589 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8590 {
8591 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8592 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8593 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8594 	u16 type;
8595 	int rc;
8596 
8597 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8598 	if (rc)
8599 		return rc;
8600 
8601 	if (!ctx) {
8602 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8603 		if (!ctx)
8604 			return -ENOMEM;
8605 		bp->ctx = ctx;
8606 	}
8607 
8608 	resp = hwrm_req_hold(bp, req);
8609 
8610 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8611 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8612 		u8 init_val, init_off, i;
8613 		u32 max_entries;
8614 		u16 entry_size;
8615 		__le32 *p;
8616 		u32 flags;
8617 
8618 		req->type = cpu_to_le16(type);
8619 		rc = hwrm_req_send(bp, req);
8620 		if (rc)
8621 			goto ctx_done;
8622 		flags = le32_to_cpu(resp->flags);
8623 		type = le16_to_cpu(resp->next_valid_type);
8624 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8625 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8626 			continue;
8627 		}
8628 		entry_size = le16_to_cpu(resp->entry_size);
8629 		max_entries = le32_to_cpu(resp->max_num_entries);
8630 		if (ctxm->mem_valid) {
8631 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8632 			    ctxm->entry_size != entry_size ||
8633 			    ctxm->max_entries != max_entries)
8634 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8635 			else
8636 				continue;
8637 		}
8638 		ctxm->type = le16_to_cpu(resp->type);
8639 		ctxm->entry_size = entry_size;
8640 		ctxm->flags = flags;
8641 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8642 		ctxm->entry_multiple = resp->entry_multiple;
8643 		ctxm->max_entries = max_entries;
8644 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8645 		init_val = resp->ctx_init_value;
8646 		init_off = resp->ctx_init_offset;
8647 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8648 					  BNXT_CTX_INIT_VALID(flags));
8649 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8650 					      BNXT_MAX_SPLIT_ENTRY);
8651 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8652 		     i++, p++)
8653 			ctxm->split[i] = le32_to_cpu(*p);
8654 	}
8655 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8656 
8657 ctx_done:
8658 	hwrm_req_drop(bp, req);
8659 	return rc;
8660 }
8661 
8662 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8663 {
8664 	struct hwrm_func_backing_store_qcaps_output *resp;
8665 	struct hwrm_func_backing_store_qcaps_input *req;
8666 	int rc;
8667 
8668 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8669 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8670 		return 0;
8671 
8672 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8673 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8674 
8675 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8676 	if (rc)
8677 		return rc;
8678 
8679 	resp = hwrm_req_hold(bp, req);
8680 	rc = hwrm_req_send_silent(bp, req);
8681 	if (!rc) {
8682 		struct bnxt_ctx_mem_type *ctxm;
8683 		struct bnxt_ctx_mem_info *ctx;
8684 		u8 init_val, init_idx = 0;
8685 		u16 init_mask;
8686 
8687 		ctx = bp->ctx;
8688 		if (!ctx) {
8689 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8690 			if (!ctx) {
8691 				rc = -ENOMEM;
8692 				goto ctx_err;
8693 			}
8694 			bp->ctx = ctx;
8695 		}
8696 		init_val = resp->ctx_kind_initializer;
8697 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8698 
8699 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8700 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8701 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8702 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8703 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8704 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8705 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8706 					  (init_mask & (1 << init_idx++)) != 0);
8707 
8708 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8709 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8710 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8711 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8712 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8713 					  (init_mask & (1 << init_idx++)) != 0);
8714 
8715 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8716 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8717 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8718 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8719 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8720 					  (init_mask & (1 << init_idx++)) != 0);
8721 
8722 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8723 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8724 		ctxm->max_entries = ctxm->vnic_entries +
8725 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8726 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8727 		bnxt_init_ctx_initializer(ctxm, init_val,
8728 					  resp->vnic_init_offset,
8729 					  (init_mask & (1 << init_idx++)) != 0);
8730 
8731 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8732 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8733 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8734 		bnxt_init_ctx_initializer(ctxm, init_val,
8735 					  resp->stat_init_offset,
8736 					  (init_mask & (1 << init_idx++)) != 0);
8737 
8738 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8739 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8740 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8741 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8742 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8743 		if (!ctxm->entry_multiple)
8744 			ctxm->entry_multiple = 1;
8745 
8746 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8747 
8748 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8749 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8750 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8751 		ctxm->mrav_num_entries_units =
8752 			le16_to_cpu(resp->mrav_num_entries_units);
8753 		bnxt_init_ctx_initializer(ctxm, init_val,
8754 					  resp->mrav_init_offset,
8755 					  (init_mask & (1 << init_idx++)) != 0);
8756 
8757 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8758 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8759 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8760 
8761 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8762 		if (!ctx->tqm_fp_rings_count)
8763 			ctx->tqm_fp_rings_count = bp->max_q;
8764 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8765 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8766 
8767 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8768 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8769 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8770 
8771 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8772 	} else {
8773 		rc = 0;
8774 	}
8775 ctx_err:
8776 	hwrm_req_drop(bp, req);
8777 	return rc;
8778 }
8779 
8780 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8781 				  __le64 *pg_dir)
8782 {
8783 	if (!rmem->nr_pages)
8784 		return;
8785 
8786 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8787 	if (rmem->depth >= 1) {
8788 		if (rmem->depth == 2)
8789 			*pg_attr |= 2;
8790 		else
8791 			*pg_attr |= 1;
8792 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8793 	} else {
8794 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8795 	}
8796 }
8797 
8798 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8799 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8800 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8801 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8802 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8803 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8804 
8805 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8806 {
8807 	struct hwrm_func_backing_store_cfg_input *req;
8808 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8809 	struct bnxt_ctx_pg_info *ctx_pg;
8810 	struct bnxt_ctx_mem_type *ctxm;
8811 	void **__req = (void **)&req;
8812 	u32 req_len = sizeof(*req);
8813 	__le32 *num_entries;
8814 	__le64 *pg_dir;
8815 	u32 flags = 0;
8816 	u8 *pg_attr;
8817 	u32 ena;
8818 	int rc;
8819 	int i;
8820 
8821 	if (!ctx)
8822 		return 0;
8823 
8824 	if (req_len > bp->hwrm_max_ext_req_len)
8825 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8826 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8827 	if (rc)
8828 		return rc;
8829 
8830 	req->enables = cpu_to_le32(enables);
8831 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8832 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8833 		ctx_pg = ctxm->pg_info;
8834 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8835 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8836 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8837 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8838 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8839 				      &req->qpc_pg_size_qpc_lvl,
8840 				      &req->qpc_page_dir);
8841 
8842 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8843 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8844 	}
8845 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8846 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8847 		ctx_pg = ctxm->pg_info;
8848 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8849 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8850 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8851 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8852 				      &req->srq_pg_size_srq_lvl,
8853 				      &req->srq_page_dir);
8854 	}
8855 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8856 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8857 		ctx_pg = ctxm->pg_info;
8858 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8859 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8860 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8861 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8862 				      &req->cq_pg_size_cq_lvl,
8863 				      &req->cq_page_dir);
8864 	}
8865 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8866 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8867 		ctx_pg = ctxm->pg_info;
8868 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8869 		req->vnic_num_ring_table_entries =
8870 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8871 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8872 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8873 				      &req->vnic_pg_size_vnic_lvl,
8874 				      &req->vnic_page_dir);
8875 	}
8876 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8877 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8878 		ctx_pg = ctxm->pg_info;
8879 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8880 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8881 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8882 				      &req->stat_pg_size_stat_lvl,
8883 				      &req->stat_page_dir);
8884 	}
8885 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8886 		u32 units;
8887 
8888 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8889 		ctx_pg = ctxm->pg_info;
8890 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8891 		units = ctxm->mrav_num_entries_units;
8892 		if (units) {
8893 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8894 			u32 entries;
8895 
8896 			num_mr = ctx_pg->entries - num_ah;
8897 			entries = ((num_mr / units) << 16) | (num_ah / units);
8898 			req->mrav_num_entries = cpu_to_le32(entries);
8899 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8900 		}
8901 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8902 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8903 				      &req->mrav_pg_size_mrav_lvl,
8904 				      &req->mrav_page_dir);
8905 	}
8906 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8907 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8908 		ctx_pg = ctxm->pg_info;
8909 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8910 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8911 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8912 				      &req->tim_pg_size_tim_lvl,
8913 				      &req->tim_page_dir);
8914 	}
8915 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8916 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8917 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8918 	     pg_dir = &req->tqm_sp_page_dir,
8919 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8920 	     ctx_pg = ctxm->pg_info;
8921 	     i < BNXT_MAX_TQM_RINGS;
8922 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8923 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8924 		if (!(enables & ena))
8925 			continue;
8926 
8927 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8928 		*num_entries = cpu_to_le32(ctx_pg->entries);
8929 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8930 	}
8931 	req->flags = cpu_to_le32(flags);
8932 	return hwrm_req_send(bp, req);
8933 }
8934 
8935 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8936 				  struct bnxt_ctx_pg_info *ctx_pg)
8937 {
8938 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8939 
8940 	rmem->page_size = BNXT_PAGE_SIZE;
8941 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8942 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8943 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8944 	if (rmem->depth >= 1)
8945 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8946 	return bnxt_alloc_ring(bp, rmem);
8947 }
8948 
8949 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8950 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8951 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8952 {
8953 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8954 	int rc;
8955 
8956 	if (!mem_size)
8957 		return -EINVAL;
8958 
8959 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8960 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8961 		ctx_pg->nr_pages = 0;
8962 		return -EINVAL;
8963 	}
8964 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8965 		int nr_tbls, i;
8966 
8967 		rmem->depth = 2;
8968 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8969 					     GFP_KERNEL);
8970 		if (!ctx_pg->ctx_pg_tbl)
8971 			return -ENOMEM;
8972 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8973 		rmem->nr_pages = nr_tbls;
8974 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8975 		if (rc)
8976 			return rc;
8977 		for (i = 0; i < nr_tbls; i++) {
8978 			struct bnxt_ctx_pg_info *pg_tbl;
8979 
8980 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8981 			if (!pg_tbl)
8982 				return -ENOMEM;
8983 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8984 			rmem = &pg_tbl->ring_mem;
8985 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8986 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8987 			rmem->depth = 1;
8988 			rmem->nr_pages = MAX_CTX_PAGES;
8989 			rmem->ctx_mem = ctxm;
8990 			if (i == (nr_tbls - 1)) {
8991 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8992 
8993 				if (rem)
8994 					rmem->nr_pages = rem;
8995 			}
8996 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8997 			if (rc)
8998 				break;
8999 		}
9000 	} else {
9001 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
9002 		if (rmem->nr_pages > 1 || depth)
9003 			rmem->depth = 1;
9004 		rmem->ctx_mem = ctxm;
9005 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
9006 	}
9007 	return rc;
9008 }
9009 
9010 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9011 				    struct bnxt_ctx_pg_info *ctx_pg,
9012 				    void *buf, size_t offset, size_t head,
9013 				    size_t tail)
9014 {
9015 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9016 	size_t nr_pages = ctx_pg->nr_pages;
9017 	int page_size = rmem->page_size;
9018 	size_t len = 0, total_len = 0;
9019 	u16 depth = rmem->depth;
9020 
9021 	tail %= nr_pages * page_size;
9022 	do {
9023 		if (depth > 1) {
9024 			int i = head / (page_size * MAX_CTX_PAGES);
9025 			struct bnxt_ctx_pg_info *pg_tbl;
9026 
9027 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9028 			rmem = &pg_tbl->ring_mem;
9029 		}
9030 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9031 		head += len;
9032 		offset += len;
9033 		total_len += len;
9034 		if (head >= nr_pages * page_size)
9035 			head = 0;
9036 	} while (head != tail);
9037 	return total_len;
9038 }
9039 
9040 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9041 				  struct bnxt_ctx_pg_info *ctx_pg)
9042 {
9043 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9044 
9045 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9046 	    ctx_pg->ctx_pg_tbl) {
9047 		int i, nr_tbls = rmem->nr_pages;
9048 
9049 		for (i = 0; i < nr_tbls; i++) {
9050 			struct bnxt_ctx_pg_info *pg_tbl;
9051 			struct bnxt_ring_mem_info *rmem2;
9052 
9053 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9054 			if (!pg_tbl)
9055 				continue;
9056 			rmem2 = &pg_tbl->ring_mem;
9057 			bnxt_free_ring(bp, rmem2);
9058 			ctx_pg->ctx_pg_arr[i] = NULL;
9059 			kfree(pg_tbl);
9060 			ctx_pg->ctx_pg_tbl[i] = NULL;
9061 		}
9062 		kfree(ctx_pg->ctx_pg_tbl);
9063 		ctx_pg->ctx_pg_tbl = NULL;
9064 	}
9065 	bnxt_free_ring(bp, rmem);
9066 	ctx_pg->nr_pages = 0;
9067 }
9068 
9069 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9070 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9071 				   u8 pg_lvl)
9072 {
9073 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9074 	int i, rc = 0, n = 1;
9075 	u32 mem_size;
9076 
9077 	if (!ctxm->entry_size || !ctx_pg)
9078 		return -EINVAL;
9079 	if (ctxm->instance_bmap)
9080 		n = hweight32(ctxm->instance_bmap);
9081 	if (ctxm->entry_multiple)
9082 		entries = roundup(entries, ctxm->entry_multiple);
9083 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9084 	mem_size = entries * ctxm->entry_size;
9085 	for (i = 0; i < n && !rc; i++) {
9086 		ctx_pg[i].entries = entries;
9087 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9088 					    ctxm->init_value ? ctxm : NULL);
9089 	}
9090 	if (!rc)
9091 		ctxm->mem_valid = 1;
9092 	return rc;
9093 }
9094 
9095 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9096 					       struct bnxt_ctx_mem_type *ctxm,
9097 					       bool last)
9098 {
9099 	struct hwrm_func_backing_store_cfg_v2_input *req;
9100 	u32 instance_bmap = ctxm->instance_bmap;
9101 	int i, j, rc = 0, n = 1;
9102 	__le32 *p;
9103 
9104 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9105 		return 0;
9106 
9107 	if (instance_bmap)
9108 		n = hweight32(ctxm->instance_bmap);
9109 	else
9110 		instance_bmap = 1;
9111 
9112 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9113 	if (rc)
9114 		return rc;
9115 	hwrm_req_hold(bp, req);
9116 	req->type = cpu_to_le16(ctxm->type);
9117 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9118 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9119 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9120 		struct bnxt_bs_trace_info *bs_trace;
9121 		u32 enables;
9122 
9123 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9124 		req->enables = cpu_to_le32(enables);
9125 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9126 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9127 	}
9128 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9129 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9130 		p[i] = cpu_to_le32(ctxm->split[i]);
9131 	for (i = 0, j = 0; j < n && !rc; i++) {
9132 		struct bnxt_ctx_pg_info *ctx_pg;
9133 
9134 		if (!(instance_bmap & (1 << i)))
9135 			continue;
9136 		req->instance = cpu_to_le16(i);
9137 		ctx_pg = &ctxm->pg_info[j++];
9138 		if (!ctx_pg->entries)
9139 			continue;
9140 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9141 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9142 				      &req->page_size_pbl_level,
9143 				      &req->page_dir);
9144 		if (last && j == n)
9145 			req->flags =
9146 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9147 		rc = hwrm_req_send(bp, req);
9148 	}
9149 	hwrm_req_drop(bp, req);
9150 	return rc;
9151 }
9152 
9153 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9154 {
9155 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9156 	struct bnxt_ctx_mem_type *ctxm;
9157 	u16 last_type = BNXT_CTX_INV;
9158 	int rc = 0;
9159 	u16 type;
9160 
9161 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9162 		ctxm = &ctx->ctx_arr[type];
9163 		if (!bnxt_bs_trace_avail(bp, type))
9164 			continue;
9165 		if (!ctxm->mem_valid) {
9166 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9167 						     ctxm->max_entries, 1);
9168 			if (rc) {
9169 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9170 					    type);
9171 				continue;
9172 			}
9173 			bnxt_bs_trace_init(bp, ctxm);
9174 		}
9175 		last_type = type;
9176 	}
9177 
9178 	if (last_type == BNXT_CTX_INV) {
9179 		if (!ena)
9180 			return 0;
9181 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9182 			last_type = BNXT_CTX_MAX - 1;
9183 		else
9184 			last_type = BNXT_CTX_L2_MAX - 1;
9185 	}
9186 	ctx->ctx_arr[last_type].last = 1;
9187 
9188 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9189 		ctxm = &ctx->ctx_arr[type];
9190 
9191 		if (!ctxm->mem_valid)
9192 			continue;
9193 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9194 		if (rc)
9195 			return rc;
9196 	}
9197 	return 0;
9198 }
9199 
9200 /**
9201  * __bnxt_copy_ctx_mem - copy host context memory
9202  * @bp: The driver context
9203  * @ctxm: The pointer to the context memory type
9204  * @buf: The destination buffer or NULL to just obtain the length
9205  * @offset: The buffer offset to copy the data to
9206  * @head: The head offset of context memory to copy from
9207  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9208  *
9209  * This function is called for debugging purposes to dump the host context
9210  * used by the chip.
9211  *
9212  * Return: Length of memory copied
9213  */
9214 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9215 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9216 				  size_t offset, size_t head, size_t tail)
9217 {
9218 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9219 	size_t len = 0, total_len = 0;
9220 	int i, n = 1;
9221 
9222 	if (!ctx_pg)
9223 		return 0;
9224 
9225 	if (ctxm->instance_bmap)
9226 		n = hweight32(ctxm->instance_bmap);
9227 	for (i = 0; i < n; i++) {
9228 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9229 					    tail);
9230 		offset += len;
9231 		total_len += len;
9232 	}
9233 	return total_len;
9234 }
9235 
9236 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9237 			 void *buf, size_t offset)
9238 {
9239 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9240 
9241 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9242 }
9243 
9244 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9245 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9246 {
9247 	struct bnxt_ctx_pg_info *ctx_pg;
9248 	int i, n = 1;
9249 
9250 	ctxm->last = 0;
9251 
9252 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9253 		return;
9254 
9255 	ctx_pg = ctxm->pg_info;
9256 	if (ctx_pg) {
9257 		if (ctxm->instance_bmap)
9258 			n = hweight32(ctxm->instance_bmap);
9259 		for (i = 0; i < n; i++)
9260 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9261 
9262 		kfree(ctx_pg);
9263 		ctxm->pg_info = NULL;
9264 		ctxm->mem_valid = 0;
9265 	}
9266 	memset(ctxm, 0, sizeof(*ctxm));
9267 }
9268 
9269 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9270 {
9271 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9272 	u16 type;
9273 
9274 	if (!ctx)
9275 		return;
9276 
9277 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9278 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9279 
9280 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9281 	if (force) {
9282 		kfree(ctx);
9283 		bp->ctx = NULL;
9284 	}
9285 }
9286 
9287 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9288 {
9289 	struct bnxt_ctx_mem_type *ctxm;
9290 	struct bnxt_ctx_mem_info *ctx;
9291 	u32 l2_qps, qp1_qps, max_qps;
9292 	u32 ena, entries_sp, entries;
9293 	u32 srqs, max_srqs, min;
9294 	u32 num_mr, num_ah;
9295 	u32 extra_srqs = 0;
9296 	u32 extra_qps = 0;
9297 	u32 fast_qpmd_qps;
9298 	u8 pg_lvl = 1;
9299 	int i, rc;
9300 
9301 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9302 	if (rc) {
9303 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9304 			   rc);
9305 		return rc;
9306 	}
9307 	ctx = bp->ctx;
9308 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9309 		return 0;
9310 
9311 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9312 	l2_qps = ctxm->qp_l2_entries;
9313 	qp1_qps = ctxm->qp_qp1_entries;
9314 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9315 	max_qps = ctxm->max_entries;
9316 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9317 	srqs = ctxm->srq_l2_entries;
9318 	max_srqs = ctxm->max_entries;
9319 	ena = 0;
9320 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9321 		pg_lvl = 2;
9322 		if (BNXT_SW_RES_LMT(bp)) {
9323 			extra_qps = max_qps - l2_qps - qp1_qps;
9324 			extra_srqs = max_srqs - srqs;
9325 		} else {
9326 			extra_qps = min_t(u32, 65536,
9327 					  max_qps - l2_qps - qp1_qps);
9328 			/* allocate extra qps if fw supports RoCE fast qp
9329 			 * destroy feature
9330 			 */
9331 			extra_qps += fast_qpmd_qps;
9332 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9333 		}
9334 		if (fast_qpmd_qps)
9335 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9336 	}
9337 
9338 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9339 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9340 				     pg_lvl);
9341 	if (rc)
9342 		return rc;
9343 
9344 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9345 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9346 	if (rc)
9347 		return rc;
9348 
9349 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9350 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9351 				     extra_qps * 2, pg_lvl);
9352 	if (rc)
9353 		return rc;
9354 
9355 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9356 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9357 	if (rc)
9358 		return rc;
9359 
9360 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9361 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9362 	if (rc)
9363 		return rc;
9364 
9365 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9366 		goto skip_rdma;
9367 
9368 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9369 	if (BNXT_SW_RES_LMT(bp) &&
9370 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9371 		num_ah = ctxm->mrav_av_entries;
9372 		num_mr = ctxm->max_entries - num_ah;
9373 	} else {
9374 		/* 128K extra is needed to accommodate static AH context
9375 		 * allocation by f/w.
9376 		 */
9377 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9378 		num_ah = min_t(u32, num_mr, 1024 * 128);
9379 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9380 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9381 			ctxm->mrav_av_entries = num_ah;
9382 	}
9383 
9384 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9385 	if (rc)
9386 		return rc;
9387 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9388 
9389 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9390 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9391 	if (rc)
9392 		return rc;
9393 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9394 
9395 skip_rdma:
9396 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9397 	min = ctxm->min_entries;
9398 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9399 		     2 * (extra_qps + qp1_qps) + min;
9400 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9401 	if (rc)
9402 		return rc;
9403 
9404 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9405 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9406 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9407 	if (rc)
9408 		return rc;
9409 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9410 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9411 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9412 
9413 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9414 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9415 	else
9416 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9417 	if (rc) {
9418 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9419 			   rc);
9420 		return rc;
9421 	}
9422 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9423 	return 0;
9424 }
9425 
9426 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9427 {
9428 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9429 	u16 page_attr;
9430 	int rc;
9431 
9432 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9433 		return 0;
9434 
9435 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9436 	if (rc)
9437 		return rc;
9438 
9439 	if (BNXT_PAGE_SIZE == 0x2000)
9440 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9441 	else if (BNXT_PAGE_SIZE == 0x10000)
9442 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9443 	else
9444 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9445 	req->pg_size_lvl = cpu_to_le16(page_attr |
9446 				       bp->fw_crash_mem->ring_mem.depth);
9447 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9448 	req->size = cpu_to_le32(bp->fw_crash_len);
9449 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9450 	return hwrm_req_send(bp, req);
9451 }
9452 
9453 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9454 {
9455 	if (bp->fw_crash_mem) {
9456 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9457 		kfree(bp->fw_crash_mem);
9458 		bp->fw_crash_mem = NULL;
9459 	}
9460 }
9461 
9462 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9463 {
9464 	u32 mem_size = 0;
9465 	int rc;
9466 
9467 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9468 		return 0;
9469 
9470 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9471 	if (rc)
9472 		return rc;
9473 
9474 	mem_size = round_up(mem_size, 4);
9475 
9476 	/* keep and use the existing pages */
9477 	if (bp->fw_crash_mem &&
9478 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9479 		goto alloc_done;
9480 
9481 	if (bp->fw_crash_mem)
9482 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9483 	else
9484 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9485 					   GFP_KERNEL);
9486 	if (!bp->fw_crash_mem)
9487 		return -ENOMEM;
9488 
9489 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9490 	if (rc) {
9491 		bnxt_free_crash_dump_mem(bp);
9492 		return rc;
9493 	}
9494 
9495 alloc_done:
9496 	bp->fw_crash_len = mem_size;
9497 	return 0;
9498 }
9499 
9500 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9501 {
9502 	struct hwrm_func_resource_qcaps_output *resp;
9503 	struct hwrm_func_resource_qcaps_input *req;
9504 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9505 	int rc;
9506 
9507 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9508 	if (rc)
9509 		return rc;
9510 
9511 	req->fid = cpu_to_le16(0xffff);
9512 	resp = hwrm_req_hold(bp, req);
9513 	rc = hwrm_req_send_silent(bp, req);
9514 	if (rc)
9515 		goto hwrm_func_resc_qcaps_exit;
9516 
9517 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9518 	if (!all)
9519 		goto hwrm_func_resc_qcaps_exit;
9520 
9521 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9522 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9523 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9524 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9525 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9526 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9527 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9528 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9529 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9530 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9531 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9532 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9533 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9534 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9535 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9536 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9537 
9538 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9539 		u16 max_msix = le16_to_cpu(resp->max_msix);
9540 
9541 		hw_resc->max_nqs = max_msix;
9542 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9543 	}
9544 
9545 	if (BNXT_PF(bp)) {
9546 		struct bnxt_pf_info *pf = &bp->pf;
9547 
9548 		pf->vf_resv_strategy =
9549 			le16_to_cpu(resp->vf_reservation_strategy);
9550 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9551 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9552 	}
9553 hwrm_func_resc_qcaps_exit:
9554 	hwrm_req_drop(bp, req);
9555 	return rc;
9556 }
9557 
9558 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9559 {
9560 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9561 	struct hwrm_port_mac_ptp_qcfg_input *req;
9562 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9563 	u8 flags;
9564 	int rc;
9565 
9566 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9567 		rc = -ENODEV;
9568 		goto no_ptp;
9569 	}
9570 
9571 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9572 	if (rc)
9573 		goto no_ptp;
9574 
9575 	req->port_id = cpu_to_le16(bp->pf.port_id);
9576 	resp = hwrm_req_hold(bp, req);
9577 	rc = hwrm_req_send(bp, req);
9578 	if (rc)
9579 		goto exit;
9580 
9581 	flags = resp->flags;
9582 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9583 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9584 		rc = -ENODEV;
9585 		goto exit;
9586 	}
9587 	if (!ptp) {
9588 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9589 		if (!ptp) {
9590 			rc = -ENOMEM;
9591 			goto exit;
9592 		}
9593 		ptp->bp = bp;
9594 		bp->ptp_cfg = ptp;
9595 	}
9596 
9597 	if (flags &
9598 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9599 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9600 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9601 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9602 	} else if (BNXT_CHIP_P5(bp)) {
9603 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9604 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9605 	} else {
9606 		rc = -ENODEV;
9607 		goto exit;
9608 	}
9609 	ptp->rtc_configured =
9610 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9611 	rc = bnxt_ptp_init(bp);
9612 	if (rc)
9613 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9614 exit:
9615 	hwrm_req_drop(bp, req);
9616 	if (!rc)
9617 		return 0;
9618 
9619 no_ptp:
9620 	bnxt_ptp_clear(bp);
9621 	kfree(ptp);
9622 	bp->ptp_cfg = NULL;
9623 	return rc;
9624 }
9625 
9626 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9627 {
9628 	struct hwrm_func_qcaps_output *resp;
9629 	struct hwrm_func_qcaps_input *req;
9630 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9631 	u32 flags, flags_ext, flags_ext2;
9632 	int rc;
9633 
9634 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9635 	if (rc)
9636 		return rc;
9637 
9638 	req->fid = cpu_to_le16(0xffff);
9639 	resp = hwrm_req_hold(bp, req);
9640 	rc = hwrm_req_send(bp, req);
9641 	if (rc)
9642 		goto hwrm_func_qcaps_exit;
9643 
9644 	flags = le32_to_cpu(resp->flags);
9645 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9646 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9647 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9648 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9649 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9650 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9651 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9652 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9653 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9654 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9655 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9656 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9657 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9658 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9659 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9660 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9661 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9662 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9663 
9664 	flags_ext = le32_to_cpu(resp->flags_ext);
9665 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9666 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9667 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9668 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9669 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9670 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9671 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9672 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9673 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9674 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9675 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9676 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9677 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9678 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9679 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9680 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9681 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9682 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9683 
9684 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9685 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9686 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9687 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9688 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9689 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9690 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9691 	if (flags_ext2 &
9692 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9693 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9694 	if (BNXT_PF(bp) &&
9695 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9696 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9697 
9698 	bp->tx_push_thresh = 0;
9699 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9700 	    BNXT_FW_MAJ(bp) > 217)
9701 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9702 
9703 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9704 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9705 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9706 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9707 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9708 	if (!hw_resc->max_hw_ring_grps)
9709 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9710 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9711 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9712 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9713 
9714 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9715 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9716 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9717 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9718 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9719 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9720 
9721 	if (BNXT_PF(bp)) {
9722 		struct bnxt_pf_info *pf = &bp->pf;
9723 
9724 		pf->fw_fid = le16_to_cpu(resp->fid);
9725 		pf->port_id = le16_to_cpu(resp->port_id);
9726 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9727 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9728 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9729 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9730 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9731 			bp->flags |= BNXT_FLAG_WOL_CAP;
9732 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9733 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9734 		} else {
9735 			bnxt_ptp_clear(bp);
9736 			kfree(bp->ptp_cfg);
9737 			bp->ptp_cfg = NULL;
9738 		}
9739 	} else {
9740 #ifdef CONFIG_BNXT_SRIOV
9741 		struct bnxt_vf_info *vf = &bp->vf;
9742 
9743 		vf->fw_fid = le16_to_cpu(resp->fid);
9744 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9745 #endif
9746 	}
9747 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9748 
9749 hwrm_func_qcaps_exit:
9750 	hwrm_req_drop(bp, req);
9751 	return rc;
9752 }
9753 
9754 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9755 {
9756 	struct hwrm_dbg_qcaps_output *resp;
9757 	struct hwrm_dbg_qcaps_input *req;
9758 	int rc;
9759 
9760 	bp->fw_dbg_cap = 0;
9761 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9762 		return;
9763 
9764 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9765 	if (rc)
9766 		return;
9767 
9768 	req->fid = cpu_to_le16(0xffff);
9769 	resp = hwrm_req_hold(bp, req);
9770 	rc = hwrm_req_send(bp, req);
9771 	if (rc)
9772 		goto hwrm_dbg_qcaps_exit;
9773 
9774 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9775 
9776 hwrm_dbg_qcaps_exit:
9777 	hwrm_req_drop(bp, req);
9778 }
9779 
9780 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9781 
9782 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9783 {
9784 	int rc;
9785 
9786 	rc = __bnxt_hwrm_func_qcaps(bp);
9787 	if (rc)
9788 		return rc;
9789 
9790 	bnxt_hwrm_dbg_qcaps(bp);
9791 
9792 	rc = bnxt_hwrm_queue_qportcfg(bp);
9793 	if (rc) {
9794 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9795 		return rc;
9796 	}
9797 	if (bp->hwrm_spec_code >= 0x10803) {
9798 		rc = bnxt_alloc_ctx_mem(bp);
9799 		if (rc)
9800 			return rc;
9801 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9802 		if (!rc)
9803 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9804 	}
9805 	return 0;
9806 }
9807 
9808 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9809 {
9810 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9811 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9812 	u32 flags;
9813 	int rc;
9814 
9815 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9816 		return 0;
9817 
9818 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9819 	if (rc)
9820 		return rc;
9821 
9822 	resp = hwrm_req_hold(bp, req);
9823 	rc = hwrm_req_send(bp, req);
9824 	if (rc)
9825 		goto hwrm_cfa_adv_qcaps_exit;
9826 
9827 	flags = le32_to_cpu(resp->flags);
9828 	if (flags &
9829 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9830 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9831 
9832 	if (flags &
9833 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9834 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9835 
9836 	if (flags &
9837 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9838 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9839 
9840 hwrm_cfa_adv_qcaps_exit:
9841 	hwrm_req_drop(bp, req);
9842 	return rc;
9843 }
9844 
9845 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9846 {
9847 	if (bp->fw_health)
9848 		return 0;
9849 
9850 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9851 	if (!bp->fw_health)
9852 		return -ENOMEM;
9853 
9854 	mutex_init(&bp->fw_health->lock);
9855 	return 0;
9856 }
9857 
9858 static int bnxt_alloc_fw_health(struct bnxt *bp)
9859 {
9860 	int rc;
9861 
9862 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9863 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9864 		return 0;
9865 
9866 	rc = __bnxt_alloc_fw_health(bp);
9867 	if (rc) {
9868 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9869 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9870 		return rc;
9871 	}
9872 
9873 	return 0;
9874 }
9875 
9876 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9877 {
9878 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9879 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9880 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9881 }
9882 
9883 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9884 {
9885 	struct bnxt_fw_health *fw_health = bp->fw_health;
9886 	u32 reg_type;
9887 
9888 	if (!fw_health)
9889 		return;
9890 
9891 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9892 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9893 		fw_health->status_reliable = false;
9894 
9895 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9896 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9897 		fw_health->resets_reliable = false;
9898 }
9899 
9900 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9901 {
9902 	void __iomem *hs;
9903 	u32 status_loc;
9904 	u32 reg_type;
9905 	u32 sig;
9906 
9907 	if (bp->fw_health)
9908 		bp->fw_health->status_reliable = false;
9909 
9910 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9911 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9912 
9913 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9914 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9915 		if (!bp->chip_num) {
9916 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9917 			bp->chip_num = readl(bp->bar0 +
9918 					     BNXT_FW_HEALTH_WIN_BASE +
9919 					     BNXT_GRC_REG_CHIP_NUM);
9920 		}
9921 		if (!BNXT_CHIP_P5_PLUS(bp))
9922 			return;
9923 
9924 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9925 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9926 	} else {
9927 		status_loc = readl(hs + offsetof(struct hcomm_status,
9928 						 fw_status_loc));
9929 	}
9930 
9931 	if (__bnxt_alloc_fw_health(bp)) {
9932 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9933 		return;
9934 	}
9935 
9936 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9937 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9938 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9939 		__bnxt_map_fw_health_reg(bp, status_loc);
9940 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9941 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9942 	}
9943 
9944 	bp->fw_health->status_reliable = true;
9945 }
9946 
9947 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9948 {
9949 	struct bnxt_fw_health *fw_health = bp->fw_health;
9950 	u32 reg_base = 0xffffffff;
9951 	int i;
9952 
9953 	bp->fw_health->status_reliable = false;
9954 	bp->fw_health->resets_reliable = false;
9955 	/* Only pre-map the monitoring GRC registers using window 3 */
9956 	for (i = 0; i < 4; i++) {
9957 		u32 reg = fw_health->regs[i];
9958 
9959 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9960 			continue;
9961 		if (reg_base == 0xffffffff)
9962 			reg_base = reg & BNXT_GRC_BASE_MASK;
9963 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9964 			return -ERANGE;
9965 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9966 	}
9967 	bp->fw_health->status_reliable = true;
9968 	bp->fw_health->resets_reliable = true;
9969 	if (reg_base == 0xffffffff)
9970 		return 0;
9971 
9972 	__bnxt_map_fw_health_reg(bp, reg_base);
9973 	return 0;
9974 }
9975 
9976 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9977 {
9978 	if (!bp->fw_health)
9979 		return;
9980 
9981 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9982 		bp->fw_health->status_reliable = true;
9983 		bp->fw_health->resets_reliable = true;
9984 	} else {
9985 		bnxt_try_map_fw_health_reg(bp);
9986 	}
9987 }
9988 
9989 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9990 {
9991 	struct bnxt_fw_health *fw_health = bp->fw_health;
9992 	struct hwrm_error_recovery_qcfg_output *resp;
9993 	struct hwrm_error_recovery_qcfg_input *req;
9994 	int rc, i;
9995 
9996 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9997 		return 0;
9998 
9999 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
10000 	if (rc)
10001 		return rc;
10002 
10003 	resp = hwrm_req_hold(bp, req);
10004 	rc = hwrm_req_send(bp, req);
10005 	if (rc)
10006 		goto err_recovery_out;
10007 	fw_health->flags = le32_to_cpu(resp->flags);
10008 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
10009 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10010 		rc = -EINVAL;
10011 		goto err_recovery_out;
10012 	}
10013 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10014 	fw_health->master_func_wait_dsecs =
10015 		le32_to_cpu(resp->master_func_wait_period);
10016 	fw_health->normal_func_wait_dsecs =
10017 		le32_to_cpu(resp->normal_func_wait_period);
10018 	fw_health->post_reset_wait_dsecs =
10019 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10020 	fw_health->post_reset_max_wait_dsecs =
10021 		le32_to_cpu(resp->max_bailout_time_after_reset);
10022 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10023 		le32_to_cpu(resp->fw_health_status_reg);
10024 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10025 		le32_to_cpu(resp->fw_heartbeat_reg);
10026 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10027 		le32_to_cpu(resp->fw_reset_cnt_reg);
10028 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10029 		le32_to_cpu(resp->reset_inprogress_reg);
10030 	fw_health->fw_reset_inprog_reg_mask =
10031 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10032 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10033 	if (fw_health->fw_reset_seq_cnt >= 16) {
10034 		rc = -EINVAL;
10035 		goto err_recovery_out;
10036 	}
10037 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10038 		fw_health->fw_reset_seq_regs[i] =
10039 			le32_to_cpu(resp->reset_reg[i]);
10040 		fw_health->fw_reset_seq_vals[i] =
10041 			le32_to_cpu(resp->reset_reg_val[i]);
10042 		fw_health->fw_reset_seq_delay_msec[i] =
10043 			resp->delay_after_reset[i];
10044 	}
10045 err_recovery_out:
10046 	hwrm_req_drop(bp, req);
10047 	if (!rc)
10048 		rc = bnxt_map_fw_health_regs(bp);
10049 	if (rc)
10050 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10051 	return rc;
10052 }
10053 
10054 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10055 {
10056 	struct hwrm_func_reset_input *req;
10057 	int rc;
10058 
10059 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10060 	if (rc)
10061 		return rc;
10062 
10063 	req->enables = 0;
10064 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10065 	return hwrm_req_send(bp, req);
10066 }
10067 
10068 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10069 {
10070 	struct hwrm_nvm_get_dev_info_output nvm_info;
10071 
10072 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10073 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10074 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10075 			 nvm_info.nvm_cfg_ver_upd);
10076 }
10077 
10078 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10079 {
10080 	struct hwrm_queue_qportcfg_output *resp;
10081 	struct hwrm_queue_qportcfg_input *req;
10082 	u8 i, j, *qptr;
10083 	bool no_rdma;
10084 	int rc = 0;
10085 
10086 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10087 	if (rc)
10088 		return rc;
10089 
10090 	resp = hwrm_req_hold(bp, req);
10091 	rc = hwrm_req_send(bp, req);
10092 	if (rc)
10093 		goto qportcfg_exit;
10094 
10095 	if (!resp->max_configurable_queues) {
10096 		rc = -EINVAL;
10097 		goto qportcfg_exit;
10098 	}
10099 	bp->max_tc = resp->max_configurable_queues;
10100 	bp->max_lltc = resp->max_configurable_lossless_queues;
10101 	if (bp->max_tc > BNXT_MAX_QUEUE)
10102 		bp->max_tc = BNXT_MAX_QUEUE;
10103 
10104 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10105 	qptr = &resp->queue_id0;
10106 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10107 		bp->q_info[j].queue_id = *qptr;
10108 		bp->q_ids[i] = *qptr++;
10109 		bp->q_info[j].queue_profile = *qptr++;
10110 		bp->tc_to_qidx[j] = j;
10111 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10112 		    (no_rdma && BNXT_PF(bp)))
10113 			j++;
10114 	}
10115 	bp->max_q = bp->max_tc;
10116 	bp->max_tc = max_t(u8, j, 1);
10117 
10118 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10119 		bp->max_tc = 1;
10120 
10121 	if (bp->max_lltc > bp->max_tc)
10122 		bp->max_lltc = bp->max_tc;
10123 
10124 qportcfg_exit:
10125 	hwrm_req_drop(bp, req);
10126 	return rc;
10127 }
10128 
10129 static int bnxt_hwrm_poll(struct bnxt *bp)
10130 {
10131 	struct hwrm_ver_get_input *req;
10132 	int rc;
10133 
10134 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10135 	if (rc)
10136 		return rc;
10137 
10138 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10139 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10140 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10141 
10142 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10143 	rc = hwrm_req_send(bp, req);
10144 	return rc;
10145 }
10146 
10147 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10148 {
10149 	struct hwrm_ver_get_output *resp;
10150 	struct hwrm_ver_get_input *req;
10151 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10152 	u32 dev_caps_cfg, hwrm_ver;
10153 	int rc, len, max_tmo_secs;
10154 
10155 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10156 	if (rc)
10157 		return rc;
10158 
10159 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10160 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10161 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10162 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10163 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10164 
10165 	resp = hwrm_req_hold(bp, req);
10166 	rc = hwrm_req_send(bp, req);
10167 	if (rc)
10168 		goto hwrm_ver_get_exit;
10169 
10170 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10171 
10172 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10173 			     resp->hwrm_intf_min_8b << 8 |
10174 			     resp->hwrm_intf_upd_8b;
10175 	if (resp->hwrm_intf_maj_8b < 1) {
10176 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10177 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10178 			    resp->hwrm_intf_upd_8b);
10179 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10180 	}
10181 
10182 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10183 			HWRM_VERSION_UPDATE;
10184 
10185 	if (bp->hwrm_spec_code > hwrm_ver)
10186 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10187 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10188 			 HWRM_VERSION_UPDATE);
10189 	else
10190 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10191 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10192 			 resp->hwrm_intf_upd_8b);
10193 
10194 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10195 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10196 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10197 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10198 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10199 		len = FW_VER_STR_LEN;
10200 	} else {
10201 		fw_maj = resp->hwrm_fw_maj_8b;
10202 		fw_min = resp->hwrm_fw_min_8b;
10203 		fw_bld = resp->hwrm_fw_bld_8b;
10204 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10205 		len = BC_HWRM_STR_LEN;
10206 	}
10207 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10208 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10209 		 fw_rsv);
10210 
10211 	if (strlen(resp->active_pkg_name)) {
10212 		int fw_ver_len = strlen(bp->fw_ver_str);
10213 
10214 		snprintf(bp->fw_ver_str + fw_ver_len,
10215 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10216 			 resp->active_pkg_name);
10217 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10218 	}
10219 
10220 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10221 	if (!bp->hwrm_cmd_timeout)
10222 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10223 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10224 	if (!bp->hwrm_cmd_max_timeout)
10225 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10226 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10227 #ifdef CONFIG_DETECT_HUNG_TASK
10228 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10229 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10230 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10231 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10232 	}
10233 #endif
10234 
10235 	if (resp->hwrm_intf_maj_8b >= 1) {
10236 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10237 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10238 	}
10239 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10240 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10241 
10242 	bp->chip_num = le16_to_cpu(resp->chip_num);
10243 	bp->chip_rev = resp->chip_rev;
10244 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10245 	    !resp->chip_metal)
10246 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10247 
10248 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10249 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10250 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10251 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10252 
10253 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10254 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10255 
10256 	if (dev_caps_cfg &
10257 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10258 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10259 
10260 	if (dev_caps_cfg &
10261 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10262 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10263 
10264 	if (dev_caps_cfg &
10265 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10266 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10267 
10268 hwrm_ver_get_exit:
10269 	hwrm_req_drop(bp, req);
10270 	return rc;
10271 }
10272 
10273 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10274 {
10275 	struct hwrm_fw_set_time_input *req;
10276 	struct tm tm;
10277 	time64_t now = ktime_get_real_seconds();
10278 	int rc;
10279 
10280 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10281 	    bp->hwrm_spec_code < 0x10400)
10282 		return -EOPNOTSUPP;
10283 
10284 	time64_to_tm(now, 0, &tm);
10285 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10286 	if (rc)
10287 		return rc;
10288 
10289 	req->year = cpu_to_le16(1900 + tm.tm_year);
10290 	req->month = 1 + tm.tm_mon;
10291 	req->day = tm.tm_mday;
10292 	req->hour = tm.tm_hour;
10293 	req->minute = tm.tm_min;
10294 	req->second = tm.tm_sec;
10295 	return hwrm_req_send(bp, req);
10296 }
10297 
10298 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10299 {
10300 	u64 sw_tmp;
10301 
10302 	hw &= mask;
10303 	sw_tmp = (*sw & ~mask) | hw;
10304 	if (hw < (*sw & mask))
10305 		sw_tmp += mask + 1;
10306 	WRITE_ONCE(*sw, sw_tmp);
10307 }
10308 
10309 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10310 				    int count, bool ignore_zero)
10311 {
10312 	int i;
10313 
10314 	for (i = 0; i < count; i++) {
10315 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10316 
10317 		if (ignore_zero && !hw)
10318 			continue;
10319 
10320 		if (masks[i] == -1ULL)
10321 			sw_stats[i] = hw;
10322 		else
10323 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10324 	}
10325 }
10326 
10327 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10328 {
10329 	if (!stats->hw_stats)
10330 		return;
10331 
10332 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10333 				stats->hw_masks, stats->len / 8, false);
10334 }
10335 
10336 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10337 {
10338 	struct bnxt_stats_mem *ring0_stats;
10339 	bool ignore_zero = false;
10340 	int i;
10341 
10342 	/* Chip bug.  Counter intermittently becomes 0. */
10343 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10344 		ignore_zero = true;
10345 
10346 	for (i = 0; i < bp->cp_nr_rings; i++) {
10347 		struct bnxt_napi *bnapi = bp->bnapi[i];
10348 		struct bnxt_cp_ring_info *cpr;
10349 		struct bnxt_stats_mem *stats;
10350 
10351 		cpr = &bnapi->cp_ring;
10352 		stats = &cpr->stats;
10353 		if (!i)
10354 			ring0_stats = stats;
10355 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10356 					ring0_stats->hw_masks,
10357 					ring0_stats->len / 8, ignore_zero);
10358 	}
10359 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10360 		struct bnxt_stats_mem *stats = &bp->port_stats;
10361 		__le64 *hw_stats = stats->hw_stats;
10362 		u64 *sw_stats = stats->sw_stats;
10363 		u64 *masks = stats->hw_masks;
10364 		int cnt;
10365 
10366 		cnt = sizeof(struct rx_port_stats) / 8;
10367 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10368 
10369 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10370 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10371 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10372 		cnt = sizeof(struct tx_port_stats) / 8;
10373 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10374 	}
10375 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10376 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10377 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10378 	}
10379 }
10380 
10381 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10382 {
10383 	struct hwrm_port_qstats_input *req;
10384 	struct bnxt_pf_info *pf = &bp->pf;
10385 	int rc;
10386 
10387 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10388 		return 0;
10389 
10390 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10391 		return -EOPNOTSUPP;
10392 
10393 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10394 	if (rc)
10395 		return rc;
10396 
10397 	req->flags = flags;
10398 	req->port_id = cpu_to_le16(pf->port_id);
10399 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10400 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10401 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10402 	return hwrm_req_send(bp, req);
10403 }
10404 
10405 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10406 {
10407 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10408 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10409 	struct hwrm_port_qstats_ext_output *resp_qs;
10410 	struct hwrm_port_qstats_ext_input *req_qs;
10411 	struct bnxt_pf_info *pf = &bp->pf;
10412 	u32 tx_stat_size;
10413 	int rc;
10414 
10415 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10416 		return 0;
10417 
10418 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10419 		return -EOPNOTSUPP;
10420 
10421 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10422 	if (rc)
10423 		return rc;
10424 
10425 	req_qs->flags = flags;
10426 	req_qs->port_id = cpu_to_le16(pf->port_id);
10427 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10428 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10429 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10430 		       sizeof(struct tx_port_stats_ext) : 0;
10431 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10432 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10433 	resp_qs = hwrm_req_hold(bp, req_qs);
10434 	rc = hwrm_req_send(bp, req_qs);
10435 	if (!rc) {
10436 		bp->fw_rx_stats_ext_size =
10437 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10438 		if (BNXT_FW_MAJ(bp) < 220 &&
10439 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10440 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10441 
10442 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10443 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10444 	} else {
10445 		bp->fw_rx_stats_ext_size = 0;
10446 		bp->fw_tx_stats_ext_size = 0;
10447 	}
10448 	hwrm_req_drop(bp, req_qs);
10449 
10450 	if (flags)
10451 		return rc;
10452 
10453 	if (bp->fw_tx_stats_ext_size <=
10454 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10455 		bp->pri2cos_valid = 0;
10456 		return rc;
10457 	}
10458 
10459 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10460 	if (rc)
10461 		return rc;
10462 
10463 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10464 
10465 	resp_qc = hwrm_req_hold(bp, req_qc);
10466 	rc = hwrm_req_send(bp, req_qc);
10467 	if (!rc) {
10468 		u8 *pri2cos;
10469 		int i, j;
10470 
10471 		pri2cos = &resp_qc->pri0_cos_queue_id;
10472 		for (i = 0; i < 8; i++) {
10473 			u8 queue_id = pri2cos[i];
10474 			u8 queue_idx;
10475 
10476 			/* Per port queue IDs start from 0, 10, 20, etc */
10477 			queue_idx = queue_id % 10;
10478 			if (queue_idx > BNXT_MAX_QUEUE) {
10479 				bp->pri2cos_valid = false;
10480 				hwrm_req_drop(bp, req_qc);
10481 				return rc;
10482 			}
10483 			for (j = 0; j < bp->max_q; j++) {
10484 				if (bp->q_ids[j] == queue_id)
10485 					bp->pri2cos_idx[i] = queue_idx;
10486 			}
10487 		}
10488 		bp->pri2cos_valid = true;
10489 	}
10490 	hwrm_req_drop(bp, req_qc);
10491 
10492 	return rc;
10493 }
10494 
10495 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10496 {
10497 	bnxt_hwrm_tunnel_dst_port_free(bp,
10498 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10499 	bnxt_hwrm_tunnel_dst_port_free(bp,
10500 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10501 }
10502 
10503 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10504 {
10505 	int rc, i;
10506 	u32 tpa_flags = 0;
10507 
10508 	if (set_tpa)
10509 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10510 	else if (BNXT_NO_FW_ACCESS(bp))
10511 		return 0;
10512 	for (i = 0; i < bp->nr_vnics; i++) {
10513 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10514 		if (rc) {
10515 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10516 				   i, rc);
10517 			return rc;
10518 		}
10519 	}
10520 	return 0;
10521 }
10522 
10523 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10524 {
10525 	int i;
10526 
10527 	for (i = 0; i < bp->nr_vnics; i++)
10528 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10529 }
10530 
10531 static void bnxt_clear_vnic(struct bnxt *bp)
10532 {
10533 	if (!bp->vnic_info)
10534 		return;
10535 
10536 	bnxt_hwrm_clear_vnic_filter(bp);
10537 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10538 		/* clear all RSS setting before free vnic ctx */
10539 		bnxt_hwrm_clear_vnic_rss(bp);
10540 		bnxt_hwrm_vnic_ctx_free(bp);
10541 	}
10542 	/* before free the vnic, undo the vnic tpa settings */
10543 	if (bp->flags & BNXT_FLAG_TPA)
10544 		bnxt_set_tpa(bp, false);
10545 	bnxt_hwrm_vnic_free(bp);
10546 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10547 		bnxt_hwrm_vnic_ctx_free(bp);
10548 }
10549 
10550 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10551 				    bool irq_re_init)
10552 {
10553 	bnxt_clear_vnic(bp);
10554 	bnxt_hwrm_ring_free(bp, close_path);
10555 	bnxt_hwrm_ring_grp_free(bp);
10556 	if (irq_re_init) {
10557 		bnxt_hwrm_stat_ctx_free(bp);
10558 		bnxt_hwrm_free_tunnel_ports(bp);
10559 	}
10560 }
10561 
10562 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10563 {
10564 	struct hwrm_func_cfg_input *req;
10565 	u8 evb_mode;
10566 	int rc;
10567 
10568 	if (br_mode == BRIDGE_MODE_VEB)
10569 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10570 	else if (br_mode == BRIDGE_MODE_VEPA)
10571 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10572 	else
10573 		return -EINVAL;
10574 
10575 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10576 	if (rc)
10577 		return rc;
10578 
10579 	req->fid = cpu_to_le16(0xffff);
10580 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10581 	req->evb_mode = evb_mode;
10582 	return hwrm_req_send(bp, req);
10583 }
10584 
10585 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10586 {
10587 	struct hwrm_func_cfg_input *req;
10588 	int rc;
10589 
10590 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10591 		return 0;
10592 
10593 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10594 	if (rc)
10595 		return rc;
10596 
10597 	req->fid = cpu_to_le16(0xffff);
10598 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10599 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10600 	if (size == 128)
10601 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10602 
10603 	return hwrm_req_send(bp, req);
10604 }
10605 
10606 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10607 {
10608 	int rc;
10609 
10610 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10611 		goto skip_rss_ctx;
10612 
10613 	/* allocate context for vnic */
10614 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10615 	if (rc) {
10616 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10617 			   vnic->vnic_id, rc);
10618 		goto vnic_setup_err;
10619 	}
10620 	bp->rsscos_nr_ctxs++;
10621 
10622 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10623 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10624 		if (rc) {
10625 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10626 				   vnic->vnic_id, rc);
10627 			goto vnic_setup_err;
10628 		}
10629 		bp->rsscos_nr_ctxs++;
10630 	}
10631 
10632 skip_rss_ctx:
10633 	/* configure default vnic, ring grp */
10634 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10635 	if (rc) {
10636 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10637 			   vnic->vnic_id, rc);
10638 		goto vnic_setup_err;
10639 	}
10640 
10641 	/* Enable RSS hashing on vnic */
10642 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10643 	if (rc) {
10644 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10645 			   vnic->vnic_id, rc);
10646 		goto vnic_setup_err;
10647 	}
10648 
10649 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10650 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10651 		if (rc) {
10652 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10653 				   vnic->vnic_id, rc);
10654 		}
10655 	}
10656 
10657 vnic_setup_err:
10658 	return rc;
10659 }
10660 
10661 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10662 			  u8 valid)
10663 {
10664 	struct hwrm_vnic_update_input *req;
10665 	int rc;
10666 
10667 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10668 	if (rc)
10669 		return rc;
10670 
10671 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10672 
10673 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10674 		req->mru = cpu_to_le16(vnic->mru);
10675 
10676 	req->enables = cpu_to_le32(valid);
10677 
10678 	return hwrm_req_send(bp, req);
10679 }
10680 
10681 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10682 {
10683 	int rc;
10684 
10685 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10686 	if (rc) {
10687 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10688 			   vnic->vnic_id, rc);
10689 		return rc;
10690 	}
10691 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10692 	if (rc)
10693 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10694 			   vnic->vnic_id, rc);
10695 	return rc;
10696 }
10697 
10698 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10699 {
10700 	int rc, i, nr_ctxs;
10701 
10702 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10703 	for (i = 0; i < nr_ctxs; i++) {
10704 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10705 		if (rc) {
10706 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10707 				   vnic->vnic_id, i, rc);
10708 			break;
10709 		}
10710 		bp->rsscos_nr_ctxs++;
10711 	}
10712 	if (i < nr_ctxs)
10713 		return -ENOMEM;
10714 
10715 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10716 	if (rc)
10717 		return rc;
10718 
10719 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10720 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10721 		if (rc) {
10722 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10723 				   vnic->vnic_id, rc);
10724 		}
10725 	}
10726 	return rc;
10727 }
10728 
10729 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10730 {
10731 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10732 		return __bnxt_setup_vnic_p5(bp, vnic);
10733 	else
10734 		return __bnxt_setup_vnic(bp, vnic);
10735 }
10736 
10737 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10738 				     struct bnxt_vnic_info *vnic,
10739 				     u16 start_rx_ring_idx, int rx_rings)
10740 {
10741 	int rc;
10742 
10743 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10744 	if (rc) {
10745 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10746 			   vnic->vnic_id, rc);
10747 		return rc;
10748 	}
10749 	return bnxt_setup_vnic(bp, vnic);
10750 }
10751 
10752 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10753 {
10754 	struct bnxt_vnic_info *vnic;
10755 	int i, rc = 0;
10756 
10757 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10758 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10759 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10760 	}
10761 
10762 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10763 		return 0;
10764 
10765 	for (i = 0; i < bp->rx_nr_rings; i++) {
10766 		u16 vnic_id = i + 1;
10767 		u16 ring_id = i;
10768 
10769 		if (vnic_id >= bp->nr_vnics)
10770 			break;
10771 
10772 		vnic = &bp->vnic_info[vnic_id];
10773 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10774 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10775 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10776 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10777 			break;
10778 	}
10779 	return rc;
10780 }
10781 
10782 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10783 			  bool all)
10784 {
10785 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10786 	struct bnxt_filter_base *usr_fltr, *tmp;
10787 	struct bnxt_ntuple_filter *ntp_fltr;
10788 	int i;
10789 
10790 	if (netif_running(bp->dev)) {
10791 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10792 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10793 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10794 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10795 		}
10796 	}
10797 	if (!all)
10798 		return;
10799 
10800 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10801 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10802 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10803 			ntp_fltr = container_of(usr_fltr,
10804 						struct bnxt_ntuple_filter,
10805 						base);
10806 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10807 			bnxt_del_ntp_filter(bp, ntp_fltr);
10808 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10809 		}
10810 	}
10811 
10812 	if (vnic->rss_table)
10813 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10814 				  vnic->rss_table,
10815 				  vnic->rss_table_dma_addr);
10816 	bp->num_rss_ctx--;
10817 }
10818 
10819 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10820 				  int rxr_id)
10821 {
10822 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10823 	int i, vnic_rx;
10824 
10825 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10826 	 * must be updated because a future filter may use it.
10827 	 */
10828 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10829 		return true;
10830 
10831 	for (i = 0; i < tbl_size; i++) {
10832 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10833 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10834 		else
10835 			vnic_rx = bp->rss_indir_tbl[i];
10836 
10837 		if (rxr_id == vnic_rx)
10838 			return true;
10839 	}
10840 
10841 	return false;
10842 }
10843 
10844 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10845 				u16 mru, int rxr_id)
10846 {
10847 	int rc;
10848 
10849 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10850 		return 0;
10851 
10852 	if (mru) {
10853 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10854 		if (rc) {
10855 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10856 				   vnic->vnic_id, rc);
10857 			return rc;
10858 		}
10859 	}
10860 	vnic->mru = mru;
10861 	bnxt_hwrm_vnic_update(bp, vnic,
10862 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10863 
10864 	return 0;
10865 }
10866 
10867 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10868 {
10869 	struct ethtool_rxfh_context *ctx;
10870 	unsigned long context;
10871 	int rc;
10872 
10873 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10874 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10875 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10876 
10877 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10878 		if (rc)
10879 			return rc;
10880 	}
10881 
10882 	return 0;
10883 }
10884 
10885 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10886 {
10887 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10888 	struct ethtool_rxfh_context *ctx;
10889 	unsigned long context;
10890 
10891 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10892 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10893 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10894 
10895 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10896 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10897 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10898 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10899 				   rss_ctx->index);
10900 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10901 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10902 		}
10903 	}
10904 }
10905 
10906 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10907 {
10908 	struct ethtool_rxfh_context *ctx;
10909 	unsigned long context;
10910 
10911 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10912 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10913 
10914 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10915 	}
10916 }
10917 
10918 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10919 static bool bnxt_promisc_ok(struct bnxt *bp)
10920 {
10921 #ifdef CONFIG_BNXT_SRIOV
10922 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10923 		return false;
10924 #endif
10925 	return true;
10926 }
10927 
10928 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10929 {
10930 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10931 	unsigned int rc = 0;
10932 
10933 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10934 	if (rc) {
10935 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10936 			   rc);
10937 		return rc;
10938 	}
10939 
10940 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10941 	if (rc) {
10942 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10943 			   rc);
10944 		return rc;
10945 	}
10946 	return rc;
10947 }
10948 
10949 static int bnxt_cfg_rx_mode(struct bnxt *);
10950 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10951 
10952 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10953 {
10954 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10955 	int rc = 0;
10956 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10957 
10958 	if (irq_re_init) {
10959 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10960 		if (rc) {
10961 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10962 				   rc);
10963 			goto err_out;
10964 		}
10965 	}
10966 
10967 	rc = bnxt_hwrm_ring_alloc(bp);
10968 	if (rc) {
10969 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10970 		goto err_out;
10971 	}
10972 
10973 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10974 	if (rc) {
10975 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10976 		goto err_out;
10977 	}
10978 
10979 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10980 		rx_nr_rings--;
10981 
10982 	/* default vnic 0 */
10983 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10984 	if (rc) {
10985 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10986 		goto err_out;
10987 	}
10988 
10989 	if (BNXT_VF(bp))
10990 		bnxt_hwrm_func_qcfg(bp);
10991 
10992 	rc = bnxt_setup_vnic(bp, vnic);
10993 	if (rc)
10994 		goto err_out;
10995 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10996 		bnxt_hwrm_update_rss_hash_cfg(bp);
10997 
10998 	if (bp->flags & BNXT_FLAG_RFS) {
10999 		rc = bnxt_alloc_rfs_vnics(bp);
11000 		if (rc)
11001 			goto err_out;
11002 	}
11003 
11004 	if (bp->flags & BNXT_FLAG_TPA) {
11005 		rc = bnxt_set_tpa(bp, true);
11006 		if (rc)
11007 			goto err_out;
11008 	}
11009 
11010 	if (BNXT_VF(bp))
11011 		bnxt_update_vf_mac(bp);
11012 
11013 	/* Filter for default vnic 0 */
11014 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11015 	if (rc) {
11016 		if (BNXT_VF(bp) && rc == -ENODEV)
11017 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11018 		else
11019 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11020 		goto err_out;
11021 	}
11022 	vnic->uc_filter_count = 1;
11023 
11024 	vnic->rx_mask = 0;
11025 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11026 		goto skip_rx_mask;
11027 
11028 	if (bp->dev->flags & IFF_BROADCAST)
11029 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11030 
11031 	if (bp->dev->flags & IFF_PROMISC)
11032 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11033 
11034 	if (bp->dev->flags & IFF_ALLMULTI) {
11035 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11036 		vnic->mc_list_count = 0;
11037 	} else if (bp->dev->flags & IFF_MULTICAST) {
11038 		u32 mask = 0;
11039 
11040 		bnxt_mc_list_updated(bp, &mask);
11041 		vnic->rx_mask |= mask;
11042 	}
11043 
11044 	rc = bnxt_cfg_rx_mode(bp);
11045 	if (rc)
11046 		goto err_out;
11047 
11048 skip_rx_mask:
11049 	rc = bnxt_hwrm_set_coal(bp);
11050 	if (rc)
11051 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11052 				rc);
11053 
11054 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11055 		rc = bnxt_setup_nitroa0_vnic(bp);
11056 		if (rc)
11057 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11058 				   rc);
11059 	}
11060 
11061 	if (BNXT_VF(bp)) {
11062 		bnxt_hwrm_func_qcfg(bp);
11063 		netdev_update_features(bp->dev);
11064 	}
11065 
11066 	return 0;
11067 
11068 err_out:
11069 	bnxt_hwrm_resource_free(bp, 0, true);
11070 
11071 	return rc;
11072 }
11073 
11074 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11075 {
11076 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11077 	return 0;
11078 }
11079 
11080 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11081 {
11082 	bnxt_init_cp_rings(bp);
11083 	bnxt_init_rx_rings(bp);
11084 	bnxt_init_tx_rings(bp);
11085 	bnxt_init_ring_grps(bp, irq_re_init);
11086 	bnxt_init_vnics(bp);
11087 
11088 	return bnxt_init_chip(bp, irq_re_init);
11089 }
11090 
11091 static int bnxt_set_real_num_queues(struct bnxt *bp)
11092 {
11093 	int rc;
11094 	struct net_device *dev = bp->dev;
11095 
11096 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11097 					  bp->tx_nr_rings_xdp);
11098 	if (rc)
11099 		return rc;
11100 
11101 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11102 	if (rc)
11103 		return rc;
11104 
11105 #ifdef CONFIG_RFS_ACCEL
11106 	if (bp->flags & BNXT_FLAG_RFS)
11107 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11108 #endif
11109 
11110 	return rc;
11111 }
11112 
11113 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11114 			     bool shared)
11115 {
11116 	int _rx = *rx, _tx = *tx;
11117 
11118 	if (shared) {
11119 		*rx = min_t(int, _rx, max);
11120 		*tx = min_t(int, _tx, max);
11121 	} else {
11122 		if (max < 2)
11123 			return -ENOMEM;
11124 
11125 		while (_rx + _tx > max) {
11126 			if (_rx > _tx && _rx > 1)
11127 				_rx--;
11128 			else if (_tx > 1)
11129 				_tx--;
11130 		}
11131 		*rx = _rx;
11132 		*tx = _tx;
11133 	}
11134 	return 0;
11135 }
11136 
11137 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11138 {
11139 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11140 }
11141 
11142 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11143 {
11144 	int tcs = bp->num_tc;
11145 
11146 	if (!tcs)
11147 		tcs = 1;
11148 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11149 }
11150 
11151 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11152 {
11153 	int tcs = bp->num_tc;
11154 
11155 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11156 	       bp->tx_nr_rings_xdp;
11157 }
11158 
11159 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11160 			   bool sh)
11161 {
11162 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11163 
11164 	if (tx_cp != *tx) {
11165 		int tx_saved = tx_cp, rc;
11166 
11167 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11168 		if (rc)
11169 			return rc;
11170 		if (tx_cp != tx_saved)
11171 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11172 		return 0;
11173 	}
11174 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11175 }
11176 
11177 static void bnxt_setup_msix(struct bnxt *bp)
11178 {
11179 	const int len = sizeof(bp->irq_tbl[0].name);
11180 	struct net_device *dev = bp->dev;
11181 	int tcs, i;
11182 
11183 	tcs = bp->num_tc;
11184 	if (tcs) {
11185 		int i, off, count;
11186 
11187 		for (i = 0; i < tcs; i++) {
11188 			count = bp->tx_nr_rings_per_tc;
11189 			off = BNXT_TC_TO_RING_BASE(bp, i);
11190 			netdev_set_tc_queue(dev, i, count, off);
11191 		}
11192 	}
11193 
11194 	for (i = 0; i < bp->cp_nr_rings; i++) {
11195 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11196 		char *attr;
11197 
11198 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11199 			attr = "TxRx";
11200 		else if (i < bp->rx_nr_rings)
11201 			attr = "rx";
11202 		else
11203 			attr = "tx";
11204 
11205 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11206 			 attr, i);
11207 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11208 	}
11209 }
11210 
11211 static int bnxt_init_int_mode(struct bnxt *bp);
11212 
11213 static int bnxt_change_msix(struct bnxt *bp, int total)
11214 {
11215 	struct msi_map map;
11216 	int i;
11217 
11218 	/* add MSIX to the end if needed */
11219 	for (i = bp->total_irqs; i < total; i++) {
11220 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11221 		if (map.index < 0)
11222 			return bp->total_irqs;
11223 		bp->irq_tbl[i].vector = map.virq;
11224 		bp->total_irqs++;
11225 	}
11226 
11227 	/* trim MSIX from the end if needed */
11228 	for (i = bp->total_irqs; i > total; i--) {
11229 		map.index = i - 1;
11230 		map.virq = bp->irq_tbl[i - 1].vector;
11231 		pci_msix_free_irq(bp->pdev, map);
11232 		bp->total_irqs--;
11233 	}
11234 	return bp->total_irqs;
11235 }
11236 
11237 static int bnxt_setup_int_mode(struct bnxt *bp)
11238 {
11239 	int rc;
11240 
11241 	if (!bp->irq_tbl) {
11242 		rc = bnxt_init_int_mode(bp);
11243 		if (rc || !bp->irq_tbl)
11244 			return rc ?: -ENODEV;
11245 	}
11246 
11247 	bnxt_setup_msix(bp);
11248 
11249 	rc = bnxt_set_real_num_queues(bp);
11250 	return rc;
11251 }
11252 
11253 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11254 {
11255 	return bp->hw_resc.max_rsscos_ctxs;
11256 }
11257 
11258 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11259 {
11260 	return bp->hw_resc.max_vnics;
11261 }
11262 
11263 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11264 {
11265 	return bp->hw_resc.max_stat_ctxs;
11266 }
11267 
11268 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11269 {
11270 	return bp->hw_resc.max_cp_rings;
11271 }
11272 
11273 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11274 {
11275 	unsigned int cp = bp->hw_resc.max_cp_rings;
11276 
11277 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11278 		cp -= bnxt_get_ulp_msix_num(bp);
11279 
11280 	return cp;
11281 }
11282 
11283 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11284 {
11285 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11286 
11287 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11288 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11289 
11290 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11291 }
11292 
11293 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11294 {
11295 	bp->hw_resc.max_irqs = max_irqs;
11296 }
11297 
11298 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11299 {
11300 	unsigned int cp;
11301 
11302 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11303 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11304 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11305 	else
11306 		return cp - bp->cp_nr_rings;
11307 }
11308 
11309 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11310 {
11311 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11312 }
11313 
11314 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11315 {
11316 	int max_irq = bnxt_get_max_func_irqs(bp);
11317 	int total_req = bp->cp_nr_rings + num;
11318 
11319 	if (max_irq < total_req) {
11320 		num = max_irq - bp->cp_nr_rings;
11321 		if (num <= 0)
11322 			return 0;
11323 	}
11324 	return num;
11325 }
11326 
11327 static int bnxt_get_num_msix(struct bnxt *bp)
11328 {
11329 	if (!BNXT_NEW_RM(bp))
11330 		return bnxt_get_max_func_irqs(bp);
11331 
11332 	return bnxt_nq_rings_in_use(bp);
11333 }
11334 
11335 static int bnxt_init_int_mode(struct bnxt *bp)
11336 {
11337 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11338 
11339 	total_vecs = bnxt_get_num_msix(bp);
11340 	max = bnxt_get_max_func_irqs(bp);
11341 	if (total_vecs > max)
11342 		total_vecs = max;
11343 
11344 	if (!total_vecs)
11345 		return 0;
11346 
11347 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11348 		min = 2;
11349 
11350 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11351 					   PCI_IRQ_MSIX);
11352 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11353 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11354 		rc = -ENODEV;
11355 		goto msix_setup_exit;
11356 	}
11357 
11358 	tbl_size = total_vecs;
11359 	if (pci_msix_can_alloc_dyn(bp->pdev))
11360 		tbl_size = max;
11361 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11362 	if (bp->irq_tbl) {
11363 		for (i = 0; i < total_vecs; i++)
11364 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11365 
11366 		bp->total_irqs = total_vecs;
11367 		/* Trim rings based upon num of vectors allocated */
11368 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11369 				     total_vecs - ulp_msix, min == 1);
11370 		if (rc)
11371 			goto msix_setup_exit;
11372 
11373 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11374 		bp->cp_nr_rings = (min == 1) ?
11375 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11376 				  tx_cp + bp->rx_nr_rings;
11377 
11378 	} else {
11379 		rc = -ENOMEM;
11380 		goto msix_setup_exit;
11381 	}
11382 	return 0;
11383 
11384 msix_setup_exit:
11385 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11386 	kfree(bp->irq_tbl);
11387 	bp->irq_tbl = NULL;
11388 	pci_free_irq_vectors(bp->pdev);
11389 	return rc;
11390 }
11391 
11392 static void bnxt_clear_int_mode(struct bnxt *bp)
11393 {
11394 	pci_free_irq_vectors(bp->pdev);
11395 
11396 	kfree(bp->irq_tbl);
11397 	bp->irq_tbl = NULL;
11398 }
11399 
11400 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11401 {
11402 	bool irq_cleared = false;
11403 	bool irq_change = false;
11404 	int tcs = bp->num_tc;
11405 	int irqs_required;
11406 	int rc;
11407 
11408 	if (!bnxt_need_reserve_rings(bp))
11409 		return 0;
11410 
11411 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11412 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11413 
11414 		if (ulp_msix > bp->ulp_num_msix_want)
11415 			ulp_msix = bp->ulp_num_msix_want;
11416 		irqs_required = ulp_msix + bp->cp_nr_rings;
11417 	} else {
11418 		irqs_required = bnxt_get_num_msix(bp);
11419 	}
11420 
11421 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11422 		irq_change = true;
11423 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11424 			bnxt_ulp_irq_stop(bp);
11425 			bnxt_clear_int_mode(bp);
11426 			irq_cleared = true;
11427 		}
11428 	}
11429 	rc = __bnxt_reserve_rings(bp);
11430 	if (irq_cleared) {
11431 		if (!rc)
11432 			rc = bnxt_init_int_mode(bp);
11433 		bnxt_ulp_irq_restart(bp, rc);
11434 	} else if (irq_change && !rc) {
11435 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11436 			rc = -ENOSPC;
11437 	}
11438 	if (rc) {
11439 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11440 		return rc;
11441 	}
11442 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11443 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11444 		netdev_err(bp->dev, "tx ring reservation failure\n");
11445 		netdev_reset_tc(bp->dev);
11446 		bp->num_tc = 0;
11447 		if (bp->tx_nr_rings_xdp)
11448 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11449 		else
11450 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11451 		return -ENOMEM;
11452 	}
11453 	return 0;
11454 }
11455 
11456 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11457 {
11458 	struct bnxt_tx_ring_info *txr;
11459 	struct netdev_queue *txq;
11460 	struct bnxt_napi *bnapi;
11461 	int i;
11462 
11463 	bnapi = bp->bnapi[idx];
11464 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11465 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11466 		synchronize_net();
11467 
11468 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11469 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11470 			if (txq) {
11471 				__netif_tx_lock_bh(txq);
11472 				netif_tx_stop_queue(txq);
11473 				__netif_tx_unlock_bh(txq);
11474 			}
11475 		}
11476 
11477 		if (!bp->tph_mode)
11478 			continue;
11479 
11480 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11481 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11482 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11483 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11484 	}
11485 }
11486 
11487 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11488 {
11489 	struct bnxt_tx_ring_info *txr;
11490 	struct netdev_queue *txq;
11491 	struct bnxt_napi *bnapi;
11492 	int rc, i;
11493 
11494 	bnapi = bp->bnapi[idx];
11495 	/* All rings have been reserved and previously allocated.
11496 	 * Reallocating with the same parameters should never fail.
11497 	 */
11498 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11499 		if (!bp->tph_mode)
11500 			goto start_tx;
11501 
11502 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11503 		if (rc)
11504 			return rc;
11505 
11506 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11507 		if (rc)
11508 			return rc;
11509 
11510 		txr->tx_prod = 0;
11511 		txr->tx_cons = 0;
11512 		txr->tx_hw_cons = 0;
11513 start_tx:
11514 		WRITE_ONCE(txr->dev_state, 0);
11515 		synchronize_net();
11516 
11517 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11518 			continue;
11519 
11520 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11521 		if (txq)
11522 			netif_tx_start_queue(txq);
11523 	}
11524 
11525 	return 0;
11526 }
11527 
11528 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11529 				     const cpumask_t *mask)
11530 {
11531 	struct bnxt_irq *irq;
11532 	u16 tag;
11533 	int err;
11534 
11535 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11536 
11537 	if (!irq->bp->tph_mode)
11538 		return;
11539 
11540 	cpumask_copy(irq->cpu_mask, mask);
11541 
11542 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11543 		return;
11544 
11545 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11546 				cpumask_first(irq->cpu_mask), &tag))
11547 		return;
11548 
11549 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11550 		return;
11551 
11552 	netdev_lock(irq->bp->dev);
11553 	if (netif_running(irq->bp->dev)) {
11554 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11555 		if (err)
11556 			netdev_err(irq->bp->dev,
11557 				   "RX queue restart failed: err=%d\n", err);
11558 	}
11559 	netdev_unlock(irq->bp->dev);
11560 }
11561 
11562 static void bnxt_irq_affinity_release(struct kref *ref)
11563 {
11564 	struct irq_affinity_notify *notify =
11565 		container_of(ref, struct irq_affinity_notify, kref);
11566 	struct bnxt_irq *irq;
11567 
11568 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11569 
11570 	if (!irq->bp->tph_mode)
11571 		return;
11572 
11573 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11574 		netdev_err(irq->bp->dev,
11575 			   "Setting ST=0 for MSIX entry %d failed\n",
11576 			   irq->msix_nr);
11577 		return;
11578 	}
11579 }
11580 
11581 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11582 {
11583 	irq_set_affinity_notifier(irq->vector, NULL);
11584 }
11585 
11586 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11587 {
11588 	struct irq_affinity_notify *notify;
11589 
11590 	irq->bp = bp;
11591 
11592 	/* Nothing to do if TPH is not enabled */
11593 	if (!bp->tph_mode)
11594 		return;
11595 
11596 	/* Register IRQ affinity notifier */
11597 	notify = &irq->affinity_notify;
11598 	notify->irq = irq->vector;
11599 	notify->notify = bnxt_irq_affinity_notify;
11600 	notify->release = bnxt_irq_affinity_release;
11601 
11602 	irq_set_affinity_notifier(irq->vector, notify);
11603 }
11604 
11605 static void bnxt_free_irq(struct bnxt *bp)
11606 {
11607 	struct bnxt_irq *irq;
11608 	int i;
11609 
11610 #ifdef CONFIG_RFS_ACCEL
11611 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11612 	bp->dev->rx_cpu_rmap = NULL;
11613 #endif
11614 	if (!bp->irq_tbl || !bp->bnapi)
11615 		return;
11616 
11617 	for (i = 0; i < bp->cp_nr_rings; i++) {
11618 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11619 
11620 		irq = &bp->irq_tbl[map_idx];
11621 		if (irq->requested) {
11622 			if (irq->have_cpumask) {
11623 				irq_update_affinity_hint(irq->vector, NULL);
11624 				free_cpumask_var(irq->cpu_mask);
11625 				irq->have_cpumask = 0;
11626 			}
11627 
11628 			bnxt_release_irq_notifier(irq);
11629 
11630 			free_irq(irq->vector, bp->bnapi[i]);
11631 		}
11632 
11633 		irq->requested = 0;
11634 	}
11635 
11636 	/* Disable TPH support */
11637 	pcie_disable_tph(bp->pdev);
11638 	bp->tph_mode = 0;
11639 }
11640 
11641 static int bnxt_request_irq(struct bnxt *bp)
11642 {
11643 	struct cpu_rmap *rmap = NULL;
11644 	int i, j, rc = 0;
11645 	unsigned long flags = 0;
11646 
11647 	rc = bnxt_setup_int_mode(bp);
11648 	if (rc) {
11649 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11650 			   rc);
11651 		return rc;
11652 	}
11653 #ifdef CONFIG_RFS_ACCEL
11654 	rmap = bp->dev->rx_cpu_rmap;
11655 #endif
11656 
11657 	/* Enable TPH support as part of IRQ request */
11658 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11659 	if (!rc)
11660 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11661 
11662 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11663 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11664 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11665 
11666 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11667 		    rmap && bp->bnapi[i]->rx_ring) {
11668 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11669 			if (rc)
11670 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11671 					    j);
11672 			j++;
11673 		}
11674 
11675 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11676 				 bp->bnapi[i]);
11677 		if (rc)
11678 			break;
11679 
11680 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11681 		irq->requested = 1;
11682 
11683 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11684 			int numa_node = dev_to_node(&bp->pdev->dev);
11685 			u16 tag;
11686 
11687 			irq->have_cpumask = 1;
11688 			irq->msix_nr = map_idx;
11689 			irq->ring_nr = i;
11690 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11691 					irq->cpu_mask);
11692 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11693 			if (rc) {
11694 				netdev_warn(bp->dev,
11695 					    "Update affinity hint failed, IRQ = %d\n",
11696 					    irq->vector);
11697 				break;
11698 			}
11699 
11700 			bnxt_register_irq_notifier(bp, irq);
11701 
11702 			/* Init ST table entry */
11703 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11704 						cpumask_first(irq->cpu_mask),
11705 						&tag))
11706 				continue;
11707 
11708 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11709 		}
11710 	}
11711 	return rc;
11712 }
11713 
11714 static void bnxt_del_napi(struct bnxt *bp)
11715 {
11716 	int i;
11717 
11718 	if (!bp->bnapi)
11719 		return;
11720 
11721 	for (i = 0; i < bp->rx_nr_rings; i++)
11722 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11723 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11724 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11725 
11726 	for (i = 0; i < bp->cp_nr_rings; i++) {
11727 		struct bnxt_napi *bnapi = bp->bnapi[i];
11728 
11729 		__netif_napi_del_locked(&bnapi->napi);
11730 	}
11731 	/* We called __netif_napi_del_locked(), we need
11732 	 * to respect an RCU grace period before freeing napi structures.
11733 	 */
11734 	synchronize_net();
11735 }
11736 
11737 static void bnxt_init_napi(struct bnxt *bp)
11738 {
11739 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11740 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11741 	struct bnxt_napi *bnapi;
11742 	int i;
11743 
11744 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11745 		poll_fn = bnxt_poll_p5;
11746 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11747 		cp_nr_rings--;
11748 
11749 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11750 
11751 	for (i = 0; i < cp_nr_rings; i++) {
11752 		bnapi = bp->bnapi[i];
11753 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11754 					     bnapi->index);
11755 	}
11756 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11757 		bnapi = bp->bnapi[cp_nr_rings];
11758 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11759 	}
11760 }
11761 
11762 static void bnxt_disable_napi(struct bnxt *bp)
11763 {
11764 	int i;
11765 
11766 	if (!bp->bnapi ||
11767 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11768 		return;
11769 
11770 	for (i = 0; i < bp->cp_nr_rings; i++) {
11771 		struct bnxt_napi *bnapi = bp->bnapi[i];
11772 		struct bnxt_cp_ring_info *cpr;
11773 
11774 		cpr = &bnapi->cp_ring;
11775 		if (bnapi->tx_fault)
11776 			cpr->sw_stats->tx.tx_resets++;
11777 		if (bnapi->in_reset)
11778 			cpr->sw_stats->rx.rx_resets++;
11779 		napi_disable_locked(&bnapi->napi);
11780 	}
11781 }
11782 
11783 static void bnxt_enable_napi(struct bnxt *bp)
11784 {
11785 	int i;
11786 
11787 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11788 	for (i = 0; i < bp->cp_nr_rings; i++) {
11789 		struct bnxt_napi *bnapi = bp->bnapi[i];
11790 		struct bnxt_cp_ring_info *cpr;
11791 
11792 		bnapi->tx_fault = 0;
11793 
11794 		cpr = &bnapi->cp_ring;
11795 		bnapi->in_reset = false;
11796 
11797 		if (bnapi->rx_ring) {
11798 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11799 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11800 		}
11801 		napi_enable_locked(&bnapi->napi);
11802 	}
11803 }
11804 
11805 void bnxt_tx_disable(struct bnxt *bp)
11806 {
11807 	int i;
11808 	struct bnxt_tx_ring_info *txr;
11809 
11810 	if (bp->tx_ring) {
11811 		for (i = 0; i < bp->tx_nr_rings; i++) {
11812 			txr = &bp->tx_ring[i];
11813 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11814 		}
11815 	}
11816 	/* Make sure napi polls see @dev_state change */
11817 	synchronize_net();
11818 	/* Drop carrier first to prevent TX timeout */
11819 	netif_carrier_off(bp->dev);
11820 	/* Stop all TX queues */
11821 	netif_tx_disable(bp->dev);
11822 }
11823 
11824 void bnxt_tx_enable(struct bnxt *bp)
11825 {
11826 	int i;
11827 	struct bnxt_tx_ring_info *txr;
11828 
11829 	for (i = 0; i < bp->tx_nr_rings; i++) {
11830 		txr = &bp->tx_ring[i];
11831 		WRITE_ONCE(txr->dev_state, 0);
11832 	}
11833 	/* Make sure napi polls see @dev_state change */
11834 	synchronize_net();
11835 	netif_tx_wake_all_queues(bp->dev);
11836 	if (BNXT_LINK_IS_UP(bp))
11837 		netif_carrier_on(bp->dev);
11838 }
11839 
11840 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11841 {
11842 	u8 active_fec = link_info->active_fec_sig_mode &
11843 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11844 
11845 	switch (active_fec) {
11846 	default:
11847 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11848 		return "None";
11849 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11850 		return "Clause 74 BaseR";
11851 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11852 		return "Clause 91 RS(528,514)";
11853 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11854 		return "Clause 91 RS544_1XN";
11855 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11856 		return "Clause 91 RS(544,514)";
11857 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11858 		return "Clause 91 RS272_1XN";
11859 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11860 		return "Clause 91 RS(272,257)";
11861 	}
11862 }
11863 
11864 void bnxt_report_link(struct bnxt *bp)
11865 {
11866 	if (BNXT_LINK_IS_UP(bp)) {
11867 		const char *signal = "";
11868 		const char *flow_ctrl;
11869 		const char *duplex;
11870 		u32 speed;
11871 		u16 fec;
11872 
11873 		netif_carrier_on(bp->dev);
11874 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11875 		if (speed == SPEED_UNKNOWN) {
11876 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11877 			return;
11878 		}
11879 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11880 			duplex = "full";
11881 		else
11882 			duplex = "half";
11883 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11884 			flow_ctrl = "ON - receive & transmit";
11885 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11886 			flow_ctrl = "ON - transmit";
11887 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11888 			flow_ctrl = "ON - receive";
11889 		else
11890 			flow_ctrl = "none";
11891 		if (bp->link_info.phy_qcfg_resp.option_flags &
11892 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11893 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11894 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11895 			switch (sig_mode) {
11896 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11897 				signal = "(NRZ) ";
11898 				break;
11899 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11900 				signal = "(PAM4 56Gbps) ";
11901 				break;
11902 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11903 				signal = "(PAM4 112Gbps) ";
11904 				break;
11905 			default:
11906 				break;
11907 			}
11908 		}
11909 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11910 			    speed, signal, duplex, flow_ctrl);
11911 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11912 			netdev_info(bp->dev, "EEE is %s\n",
11913 				    bp->eee.eee_active ? "active" :
11914 							 "not active");
11915 		fec = bp->link_info.fec_cfg;
11916 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11917 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11918 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11919 				    bnxt_report_fec(&bp->link_info));
11920 	} else {
11921 		netif_carrier_off(bp->dev);
11922 		netdev_err(bp->dev, "NIC Link is Down\n");
11923 	}
11924 }
11925 
11926 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11927 {
11928 	if (!resp->supported_speeds_auto_mode &&
11929 	    !resp->supported_speeds_force_mode &&
11930 	    !resp->supported_pam4_speeds_auto_mode &&
11931 	    !resp->supported_pam4_speeds_force_mode &&
11932 	    !resp->supported_speeds2_auto_mode &&
11933 	    !resp->supported_speeds2_force_mode)
11934 		return true;
11935 	return false;
11936 }
11937 
11938 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11939 {
11940 	struct bnxt_link_info *link_info = &bp->link_info;
11941 	struct hwrm_port_phy_qcaps_output *resp;
11942 	struct hwrm_port_phy_qcaps_input *req;
11943 	int rc = 0;
11944 
11945 	if (bp->hwrm_spec_code < 0x10201)
11946 		return 0;
11947 
11948 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11949 	if (rc)
11950 		return rc;
11951 
11952 	resp = hwrm_req_hold(bp, req);
11953 	rc = hwrm_req_send(bp, req);
11954 	if (rc)
11955 		goto hwrm_phy_qcaps_exit;
11956 
11957 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11958 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11959 		struct ethtool_keee *eee = &bp->eee;
11960 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11961 
11962 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11963 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11964 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11965 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11966 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11967 	}
11968 
11969 	if (bp->hwrm_spec_code >= 0x10a01) {
11970 		if (bnxt_phy_qcaps_no_speed(resp)) {
11971 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11972 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11973 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11974 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11975 			netdev_info(bp->dev, "Ethernet link enabled\n");
11976 			/* Phy re-enabled, reprobe the speeds */
11977 			link_info->support_auto_speeds = 0;
11978 			link_info->support_pam4_auto_speeds = 0;
11979 			link_info->support_auto_speeds2 = 0;
11980 		}
11981 	}
11982 	if (resp->supported_speeds_auto_mode)
11983 		link_info->support_auto_speeds =
11984 			le16_to_cpu(resp->supported_speeds_auto_mode);
11985 	if (resp->supported_pam4_speeds_auto_mode)
11986 		link_info->support_pam4_auto_speeds =
11987 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11988 	if (resp->supported_speeds2_auto_mode)
11989 		link_info->support_auto_speeds2 =
11990 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11991 
11992 	bp->port_count = resp->port_cnt;
11993 
11994 hwrm_phy_qcaps_exit:
11995 	hwrm_req_drop(bp, req);
11996 	return rc;
11997 }
11998 
11999 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
12000 {
12001 	struct hwrm_port_mac_qcaps_output *resp;
12002 	struct hwrm_port_mac_qcaps_input *req;
12003 	int rc;
12004 
12005 	if (bp->hwrm_spec_code < 0x10a03)
12006 		return;
12007 
12008 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
12009 	if (rc)
12010 		return;
12011 
12012 	resp = hwrm_req_hold(bp, req);
12013 	rc = hwrm_req_send_silent(bp, req);
12014 	if (!rc)
12015 		bp->mac_flags = resp->flags;
12016 	hwrm_req_drop(bp, req);
12017 }
12018 
12019 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12020 {
12021 	u16 diff = advertising ^ supported;
12022 
12023 	return ((supported | diff) != supported);
12024 }
12025 
12026 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12027 {
12028 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12029 
12030 	/* Check if any advertised speeds are no longer supported. The caller
12031 	 * holds the link_lock mutex, so we can modify link_info settings.
12032 	 */
12033 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12034 		if (bnxt_support_dropped(link_info->advertising,
12035 					 link_info->support_auto_speeds2)) {
12036 			link_info->advertising = link_info->support_auto_speeds2;
12037 			return true;
12038 		}
12039 		return false;
12040 	}
12041 	if (bnxt_support_dropped(link_info->advertising,
12042 				 link_info->support_auto_speeds)) {
12043 		link_info->advertising = link_info->support_auto_speeds;
12044 		return true;
12045 	}
12046 	if (bnxt_support_dropped(link_info->advertising_pam4,
12047 				 link_info->support_pam4_auto_speeds)) {
12048 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12049 		return true;
12050 	}
12051 	return false;
12052 }
12053 
12054 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12055 {
12056 	struct bnxt_link_info *link_info = &bp->link_info;
12057 	struct hwrm_port_phy_qcfg_output *resp;
12058 	struct hwrm_port_phy_qcfg_input *req;
12059 	u8 link_state = link_info->link_state;
12060 	bool support_changed;
12061 	int rc;
12062 
12063 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12064 	if (rc)
12065 		return rc;
12066 
12067 	resp = hwrm_req_hold(bp, req);
12068 	rc = hwrm_req_send(bp, req);
12069 	if (rc) {
12070 		hwrm_req_drop(bp, req);
12071 		if (BNXT_VF(bp) && rc == -ENODEV) {
12072 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12073 			rc = 0;
12074 		}
12075 		return rc;
12076 	}
12077 
12078 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12079 	link_info->phy_link_status = resp->link;
12080 	link_info->duplex = resp->duplex_cfg;
12081 	if (bp->hwrm_spec_code >= 0x10800)
12082 		link_info->duplex = resp->duplex_state;
12083 	link_info->pause = resp->pause;
12084 	link_info->auto_mode = resp->auto_mode;
12085 	link_info->auto_pause_setting = resp->auto_pause;
12086 	link_info->lp_pause = resp->link_partner_adv_pause;
12087 	link_info->force_pause_setting = resp->force_pause;
12088 	link_info->duplex_setting = resp->duplex_cfg;
12089 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12090 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12091 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12092 			link_info->active_lanes = resp->active_lanes;
12093 	} else {
12094 		link_info->link_speed = 0;
12095 		link_info->active_lanes = 0;
12096 	}
12097 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12098 	link_info->force_pam4_link_speed =
12099 		le16_to_cpu(resp->force_pam4_link_speed);
12100 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12101 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12102 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12103 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12104 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12105 	link_info->auto_pam4_link_speeds =
12106 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12107 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12108 	link_info->lp_auto_link_speeds =
12109 		le16_to_cpu(resp->link_partner_adv_speeds);
12110 	link_info->lp_auto_pam4_link_speeds =
12111 		resp->link_partner_pam4_adv_speeds;
12112 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12113 	link_info->phy_ver[0] = resp->phy_maj;
12114 	link_info->phy_ver[1] = resp->phy_min;
12115 	link_info->phy_ver[2] = resp->phy_bld;
12116 	link_info->media_type = resp->media_type;
12117 	link_info->phy_type = resp->phy_type;
12118 	link_info->transceiver = resp->xcvr_pkg_type;
12119 	link_info->phy_addr = resp->eee_config_phy_addr &
12120 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12121 	link_info->module_status = resp->module_status;
12122 
12123 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12124 		struct ethtool_keee *eee = &bp->eee;
12125 		u16 fw_speeds;
12126 
12127 		eee->eee_active = 0;
12128 		if (resp->eee_config_phy_addr &
12129 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12130 			eee->eee_active = 1;
12131 			fw_speeds = le16_to_cpu(
12132 				resp->link_partner_adv_eee_link_speed_mask);
12133 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12134 		}
12135 
12136 		/* Pull initial EEE config */
12137 		if (!chng_link_state) {
12138 			if (resp->eee_config_phy_addr &
12139 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12140 				eee->eee_enabled = 1;
12141 
12142 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12143 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12144 
12145 			if (resp->eee_config_phy_addr &
12146 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12147 				__le32 tmr;
12148 
12149 				eee->tx_lpi_enabled = 1;
12150 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12151 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12152 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12153 			}
12154 		}
12155 	}
12156 
12157 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12158 	if (bp->hwrm_spec_code >= 0x10504) {
12159 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12160 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12161 	}
12162 	/* TODO: need to add more logic to report VF link */
12163 	if (chng_link_state) {
12164 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12165 			link_info->link_state = BNXT_LINK_STATE_UP;
12166 		else
12167 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12168 		if (link_state != link_info->link_state)
12169 			bnxt_report_link(bp);
12170 	} else {
12171 		/* always link down if not require to update link state */
12172 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12173 	}
12174 	hwrm_req_drop(bp, req);
12175 
12176 	if (!BNXT_PHY_CFG_ABLE(bp))
12177 		return 0;
12178 
12179 	support_changed = bnxt_support_speed_dropped(link_info);
12180 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12181 		bnxt_hwrm_set_link_setting(bp, true, false);
12182 	return 0;
12183 }
12184 
12185 static void bnxt_get_port_module_status(struct bnxt *bp)
12186 {
12187 	struct bnxt_link_info *link_info = &bp->link_info;
12188 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12189 	u8 module_status;
12190 
12191 	if (bnxt_update_link(bp, true))
12192 		return;
12193 
12194 	module_status = link_info->module_status;
12195 	switch (module_status) {
12196 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12197 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12198 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12199 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12200 			    bp->pf.port_id);
12201 		if (bp->hwrm_spec_code >= 0x10201) {
12202 			netdev_warn(bp->dev, "Module part number %s\n",
12203 				    resp->phy_vendor_partnumber);
12204 		}
12205 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12206 			netdev_warn(bp->dev, "TX is disabled\n");
12207 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12208 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12209 	}
12210 }
12211 
12212 static void
12213 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12214 {
12215 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12216 		if (bp->hwrm_spec_code >= 0x10201)
12217 			req->auto_pause =
12218 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12219 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12220 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12221 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12222 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12223 		req->enables |=
12224 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12225 	} else {
12226 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12227 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12228 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12229 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12230 		req->enables |=
12231 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12232 		if (bp->hwrm_spec_code >= 0x10201) {
12233 			req->auto_pause = req->force_pause;
12234 			req->enables |= cpu_to_le32(
12235 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12236 		}
12237 	}
12238 }
12239 
12240 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12241 {
12242 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12243 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12244 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12245 			req->enables |=
12246 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12247 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12248 		} else if (bp->link_info.advertising) {
12249 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12250 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12251 		}
12252 		if (bp->link_info.advertising_pam4) {
12253 			req->enables |=
12254 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12255 			req->auto_link_pam4_speed_mask =
12256 				cpu_to_le16(bp->link_info.advertising_pam4);
12257 		}
12258 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12259 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12260 	} else {
12261 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12262 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12263 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12264 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12265 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12266 				   (u32)bp->link_info.req_link_speed);
12267 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12268 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12269 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12270 		} else {
12271 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12272 		}
12273 	}
12274 
12275 	/* tell chimp that the setting takes effect immediately */
12276 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12277 }
12278 
12279 int bnxt_hwrm_set_pause(struct bnxt *bp)
12280 {
12281 	struct hwrm_port_phy_cfg_input *req;
12282 	int rc;
12283 
12284 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12285 	if (rc)
12286 		return rc;
12287 
12288 	bnxt_hwrm_set_pause_common(bp, req);
12289 
12290 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12291 	    bp->link_info.force_link_chng)
12292 		bnxt_hwrm_set_link_common(bp, req);
12293 
12294 	rc = hwrm_req_send(bp, req);
12295 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12296 		/* since changing of pause setting doesn't trigger any link
12297 		 * change event, the driver needs to update the current pause
12298 		 * result upon successfully return of the phy_cfg command
12299 		 */
12300 		bp->link_info.pause =
12301 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12302 		bp->link_info.auto_pause_setting = 0;
12303 		if (!bp->link_info.force_link_chng)
12304 			bnxt_report_link(bp);
12305 	}
12306 	bp->link_info.force_link_chng = false;
12307 	return rc;
12308 }
12309 
12310 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12311 			      struct hwrm_port_phy_cfg_input *req)
12312 {
12313 	struct ethtool_keee *eee = &bp->eee;
12314 
12315 	if (eee->eee_enabled) {
12316 		u16 eee_speeds;
12317 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12318 
12319 		if (eee->tx_lpi_enabled)
12320 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12321 		else
12322 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12323 
12324 		req->flags |= cpu_to_le32(flags);
12325 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12326 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12327 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12328 	} else {
12329 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12330 	}
12331 }
12332 
12333 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12334 {
12335 	struct hwrm_port_phy_cfg_input *req;
12336 	int rc;
12337 
12338 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12339 	if (rc)
12340 		return rc;
12341 
12342 	if (set_pause)
12343 		bnxt_hwrm_set_pause_common(bp, req);
12344 
12345 	bnxt_hwrm_set_link_common(bp, req);
12346 
12347 	if (set_eee)
12348 		bnxt_hwrm_set_eee(bp, req);
12349 	return hwrm_req_send(bp, req);
12350 }
12351 
12352 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12353 {
12354 	struct hwrm_port_phy_cfg_input *req;
12355 	int rc;
12356 
12357 	if (!BNXT_SINGLE_PF(bp))
12358 		return 0;
12359 
12360 	if (pci_num_vf(bp->pdev) &&
12361 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12362 		return 0;
12363 
12364 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12365 	if (rc)
12366 		return rc;
12367 
12368 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12369 	rc = hwrm_req_send(bp, req);
12370 	if (!rc) {
12371 		mutex_lock(&bp->link_lock);
12372 		/* Device is not obliged link down in certain scenarios, even
12373 		 * when forced. Setting the state unknown is consistent with
12374 		 * driver startup and will force link state to be reported
12375 		 * during subsequent open based on PORT_PHY_QCFG.
12376 		 */
12377 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12378 		mutex_unlock(&bp->link_lock);
12379 	}
12380 	return rc;
12381 }
12382 
12383 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12384 {
12385 #ifdef CONFIG_TEE_BNXT_FW
12386 	int rc = tee_bnxt_fw_load();
12387 
12388 	if (rc)
12389 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12390 
12391 	return rc;
12392 #else
12393 	netdev_err(bp->dev, "OP-TEE not supported\n");
12394 	return -ENODEV;
12395 #endif
12396 }
12397 
12398 static int bnxt_try_recover_fw(struct bnxt *bp)
12399 {
12400 	if (bp->fw_health && bp->fw_health->status_reliable) {
12401 		int retry = 0, rc;
12402 		u32 sts;
12403 
12404 		do {
12405 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12406 			rc = bnxt_hwrm_poll(bp);
12407 			if (!BNXT_FW_IS_BOOTING(sts) &&
12408 			    !BNXT_FW_IS_RECOVERING(sts))
12409 				break;
12410 			retry++;
12411 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12412 
12413 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12414 			netdev_err(bp->dev,
12415 				   "Firmware not responding, status: 0x%x\n",
12416 				   sts);
12417 			rc = -ENODEV;
12418 		}
12419 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12420 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12421 			return bnxt_fw_reset_via_optee(bp);
12422 		}
12423 		return rc;
12424 	}
12425 
12426 	return -ENODEV;
12427 }
12428 
12429 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12430 {
12431 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12432 
12433 	if (!BNXT_NEW_RM(bp))
12434 		return; /* no resource reservations required */
12435 
12436 	hw_resc->resv_cp_rings = 0;
12437 	hw_resc->resv_stat_ctxs = 0;
12438 	hw_resc->resv_irqs = 0;
12439 	hw_resc->resv_tx_rings = 0;
12440 	hw_resc->resv_rx_rings = 0;
12441 	hw_resc->resv_hw_ring_grps = 0;
12442 	hw_resc->resv_vnics = 0;
12443 	hw_resc->resv_rsscos_ctxs = 0;
12444 	if (!fw_reset) {
12445 		bp->tx_nr_rings = 0;
12446 		bp->rx_nr_rings = 0;
12447 	}
12448 }
12449 
12450 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12451 {
12452 	int rc;
12453 
12454 	if (!BNXT_NEW_RM(bp))
12455 		return 0; /* no resource reservations required */
12456 
12457 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12458 	if (rc)
12459 		netdev_err(bp->dev, "resc_qcaps failed\n");
12460 
12461 	bnxt_clear_reservations(bp, fw_reset);
12462 
12463 	return rc;
12464 }
12465 
12466 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12467 {
12468 	struct hwrm_func_drv_if_change_output *resp;
12469 	struct hwrm_func_drv_if_change_input *req;
12470 	bool resc_reinit = false;
12471 	bool caps_change = false;
12472 	int rc, retry = 0;
12473 	bool fw_reset;
12474 	u32 flags = 0;
12475 
12476 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12477 	bp->fw_reset_state = 0;
12478 
12479 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12480 		return 0;
12481 
12482 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12483 	if (rc)
12484 		return rc;
12485 
12486 	if (up)
12487 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12488 	resp = hwrm_req_hold(bp, req);
12489 
12490 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12491 	while (retry < BNXT_FW_IF_RETRY) {
12492 		rc = hwrm_req_send(bp, req);
12493 		if (rc != -EAGAIN)
12494 			break;
12495 
12496 		msleep(50);
12497 		retry++;
12498 	}
12499 
12500 	if (rc == -EAGAIN) {
12501 		hwrm_req_drop(bp, req);
12502 		return rc;
12503 	} else if (!rc) {
12504 		flags = le32_to_cpu(resp->flags);
12505 	} else if (up) {
12506 		rc = bnxt_try_recover_fw(bp);
12507 		fw_reset = true;
12508 	}
12509 	hwrm_req_drop(bp, req);
12510 	if (rc)
12511 		return rc;
12512 
12513 	if (!up) {
12514 		bnxt_inv_fw_health_reg(bp);
12515 		return 0;
12516 	}
12517 
12518 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12519 		resc_reinit = true;
12520 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12521 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12522 		fw_reset = true;
12523 	else
12524 		bnxt_remap_fw_health_regs(bp);
12525 
12526 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12527 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12528 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12529 		return -ENODEV;
12530 	}
12531 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12532 		caps_change = true;
12533 
12534 	if (resc_reinit || fw_reset || caps_change) {
12535 		if (fw_reset || caps_change) {
12536 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12537 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12538 				bnxt_ulp_irq_stop(bp);
12539 			bnxt_free_ctx_mem(bp, false);
12540 			bnxt_dcb_free(bp);
12541 			rc = bnxt_fw_init_one(bp);
12542 			if (rc) {
12543 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12544 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12545 				return rc;
12546 			}
12547 			/* IRQ will be initialized later in bnxt_request_irq()*/
12548 			bnxt_clear_int_mode(bp);
12549 		}
12550 		rc = bnxt_cancel_reservations(bp, fw_reset);
12551 	}
12552 	return rc;
12553 }
12554 
12555 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12556 {
12557 	struct hwrm_port_led_qcaps_output *resp;
12558 	struct hwrm_port_led_qcaps_input *req;
12559 	struct bnxt_pf_info *pf = &bp->pf;
12560 	int rc;
12561 
12562 	bp->num_leds = 0;
12563 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12564 		return 0;
12565 
12566 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12567 	if (rc)
12568 		return rc;
12569 
12570 	req->port_id = cpu_to_le16(pf->port_id);
12571 	resp = hwrm_req_hold(bp, req);
12572 	rc = hwrm_req_send(bp, req);
12573 	if (rc) {
12574 		hwrm_req_drop(bp, req);
12575 		return rc;
12576 	}
12577 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12578 		int i;
12579 
12580 		bp->num_leds = resp->num_leds;
12581 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12582 						 bp->num_leds);
12583 		for (i = 0; i < bp->num_leds; i++) {
12584 			struct bnxt_led_info *led = &bp->leds[i];
12585 			__le16 caps = led->led_state_caps;
12586 
12587 			if (!led->led_group_id ||
12588 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12589 				bp->num_leds = 0;
12590 				break;
12591 			}
12592 		}
12593 	}
12594 	hwrm_req_drop(bp, req);
12595 	return 0;
12596 }
12597 
12598 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12599 {
12600 	struct hwrm_wol_filter_alloc_output *resp;
12601 	struct hwrm_wol_filter_alloc_input *req;
12602 	int rc;
12603 
12604 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12605 	if (rc)
12606 		return rc;
12607 
12608 	req->port_id = cpu_to_le16(bp->pf.port_id);
12609 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12610 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12611 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12612 
12613 	resp = hwrm_req_hold(bp, req);
12614 	rc = hwrm_req_send(bp, req);
12615 	if (!rc)
12616 		bp->wol_filter_id = resp->wol_filter_id;
12617 	hwrm_req_drop(bp, req);
12618 	return rc;
12619 }
12620 
12621 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12622 {
12623 	struct hwrm_wol_filter_free_input *req;
12624 	int rc;
12625 
12626 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12627 	if (rc)
12628 		return rc;
12629 
12630 	req->port_id = cpu_to_le16(bp->pf.port_id);
12631 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12632 	req->wol_filter_id = bp->wol_filter_id;
12633 
12634 	return hwrm_req_send(bp, req);
12635 }
12636 
12637 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12638 {
12639 	struct hwrm_wol_filter_qcfg_output *resp;
12640 	struct hwrm_wol_filter_qcfg_input *req;
12641 	u16 next_handle = 0;
12642 	int rc;
12643 
12644 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12645 	if (rc)
12646 		return rc;
12647 
12648 	req->port_id = cpu_to_le16(bp->pf.port_id);
12649 	req->handle = cpu_to_le16(handle);
12650 	resp = hwrm_req_hold(bp, req);
12651 	rc = hwrm_req_send(bp, req);
12652 	if (!rc) {
12653 		next_handle = le16_to_cpu(resp->next_handle);
12654 		if (next_handle != 0) {
12655 			if (resp->wol_type ==
12656 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12657 				bp->wol = 1;
12658 				bp->wol_filter_id = resp->wol_filter_id;
12659 			}
12660 		}
12661 	}
12662 	hwrm_req_drop(bp, req);
12663 	return next_handle;
12664 }
12665 
12666 static void bnxt_get_wol_settings(struct bnxt *bp)
12667 {
12668 	u16 handle = 0;
12669 
12670 	bp->wol = 0;
12671 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12672 		return;
12673 
12674 	do {
12675 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12676 	} while (handle && handle != 0xffff);
12677 }
12678 
12679 static bool bnxt_eee_config_ok(struct bnxt *bp)
12680 {
12681 	struct ethtool_keee *eee = &bp->eee;
12682 	struct bnxt_link_info *link_info = &bp->link_info;
12683 
12684 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12685 		return true;
12686 
12687 	if (eee->eee_enabled) {
12688 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12689 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12690 
12691 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12692 
12693 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12694 			eee->eee_enabled = 0;
12695 			return false;
12696 		}
12697 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12698 			linkmode_and(eee->advertised, advertising,
12699 				     eee->supported);
12700 			return false;
12701 		}
12702 	}
12703 	return true;
12704 }
12705 
12706 static int bnxt_update_phy_setting(struct bnxt *bp)
12707 {
12708 	int rc;
12709 	bool update_link = false;
12710 	bool update_pause = false;
12711 	bool update_eee = false;
12712 	struct bnxt_link_info *link_info = &bp->link_info;
12713 
12714 	rc = bnxt_update_link(bp, true);
12715 	if (rc) {
12716 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12717 			   rc);
12718 		return rc;
12719 	}
12720 	if (!BNXT_SINGLE_PF(bp))
12721 		return 0;
12722 
12723 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12724 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12725 	    link_info->req_flow_ctrl)
12726 		update_pause = true;
12727 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12728 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12729 		update_pause = true;
12730 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12731 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12732 			update_link = true;
12733 		if (bnxt_force_speed_updated(link_info))
12734 			update_link = true;
12735 		if (link_info->req_duplex != link_info->duplex_setting)
12736 			update_link = true;
12737 	} else {
12738 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12739 			update_link = true;
12740 		if (bnxt_auto_speed_updated(link_info))
12741 			update_link = true;
12742 	}
12743 
12744 	/* The last close may have shutdown the link, so need to call
12745 	 * PHY_CFG to bring it back up.
12746 	 */
12747 	if (!BNXT_LINK_IS_UP(bp))
12748 		update_link = true;
12749 
12750 	if (!bnxt_eee_config_ok(bp))
12751 		update_eee = true;
12752 
12753 	if (update_link)
12754 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12755 	else if (update_pause)
12756 		rc = bnxt_hwrm_set_pause(bp);
12757 	if (rc) {
12758 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12759 			   rc);
12760 		return rc;
12761 	}
12762 
12763 	return rc;
12764 }
12765 
12766 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12767 
12768 static int bnxt_reinit_after_abort(struct bnxt *bp)
12769 {
12770 	int rc;
12771 
12772 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12773 		return -EBUSY;
12774 
12775 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12776 		return -ENODEV;
12777 
12778 	rc = bnxt_fw_init_one(bp);
12779 	if (!rc) {
12780 		bnxt_clear_int_mode(bp);
12781 		rc = bnxt_init_int_mode(bp);
12782 		if (!rc) {
12783 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12784 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12785 		}
12786 	}
12787 	return rc;
12788 }
12789 
12790 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12791 {
12792 	struct bnxt_ntuple_filter *ntp_fltr;
12793 	struct bnxt_l2_filter *l2_fltr;
12794 
12795 	if (list_empty(&fltr->list))
12796 		return;
12797 
12798 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12799 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12800 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12801 		atomic_inc(&l2_fltr->refcnt);
12802 		ntp_fltr->l2_fltr = l2_fltr;
12803 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12804 			bnxt_del_ntp_filter(bp, ntp_fltr);
12805 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12806 				   fltr->sw_id);
12807 		}
12808 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12809 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12810 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12811 			bnxt_del_l2_filter(bp, l2_fltr);
12812 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12813 				   fltr->sw_id);
12814 		}
12815 	}
12816 }
12817 
12818 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12819 {
12820 	struct bnxt_filter_base *usr_fltr, *tmp;
12821 
12822 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12823 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12824 }
12825 
12826 static int bnxt_set_xps_mapping(struct bnxt *bp)
12827 {
12828 	int numa_node = dev_to_node(&bp->pdev->dev);
12829 	unsigned int q_idx, map_idx, cpu, i;
12830 	const struct cpumask *cpu_mask_ptr;
12831 	int nr_cpus = num_online_cpus();
12832 	cpumask_t *q_map;
12833 	int rc = 0;
12834 
12835 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12836 	if (!q_map)
12837 		return -ENOMEM;
12838 
12839 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12840 	 * Each TC has the same number of TX queues. The nth TX queue for each
12841 	 * TC will have the same CPU mask.
12842 	 */
12843 	for (i = 0; i < nr_cpus; i++) {
12844 		map_idx = i % bp->tx_nr_rings_per_tc;
12845 		cpu = cpumask_local_spread(i, numa_node);
12846 		cpu_mask_ptr = get_cpu_mask(cpu);
12847 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12848 	}
12849 
12850 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12851 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12852 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12853 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12854 		if (rc) {
12855 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12856 				    q_idx);
12857 			break;
12858 		}
12859 	}
12860 
12861 	kfree(q_map);
12862 
12863 	return rc;
12864 }
12865 
12866 static int bnxt_tx_nr_rings(struct bnxt *bp)
12867 {
12868 	return bp->num_tc ? bp->tx_nr_rings_per_tc * bp->num_tc :
12869 			    bp->tx_nr_rings_per_tc;
12870 }
12871 
12872 static int bnxt_tx_nr_rings_per_tc(struct bnxt *bp)
12873 {
12874 	return bp->num_tc ? bp->tx_nr_rings / bp->num_tc : bp->tx_nr_rings;
12875 }
12876 
12877 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12878 {
12879 	int rc = 0;
12880 
12881 	netif_carrier_off(bp->dev);
12882 	if (irq_re_init) {
12883 		/* Reserve rings now if none were reserved at driver probe. */
12884 		rc = bnxt_init_dflt_ring_mode(bp);
12885 		if (rc) {
12886 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12887 			return rc;
12888 		}
12889 	}
12890 	rc = bnxt_reserve_rings(bp, irq_re_init);
12891 	if (rc)
12892 		return rc;
12893 
12894 	/* Make adjustments if reserved TX rings are less than requested */
12895 	bp->tx_nr_rings -= bp->tx_nr_rings_xdp;
12896 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
12897 	if (bp->tx_nr_rings_xdp) {
12898 		bp->tx_nr_rings_xdp = bp->tx_nr_rings_per_tc;
12899 		bp->tx_nr_rings += bp->tx_nr_rings_xdp;
12900 	}
12901 	rc = bnxt_alloc_mem(bp, irq_re_init);
12902 	if (rc) {
12903 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12904 		goto open_err_free_mem;
12905 	}
12906 
12907 	if (irq_re_init) {
12908 		bnxt_init_napi(bp);
12909 		rc = bnxt_request_irq(bp);
12910 		if (rc) {
12911 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12912 			goto open_err_irq;
12913 		}
12914 	}
12915 
12916 	rc = bnxt_init_nic(bp, irq_re_init);
12917 	if (rc) {
12918 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12919 		goto open_err_irq;
12920 	}
12921 
12922 	bnxt_enable_napi(bp);
12923 	bnxt_debug_dev_init(bp);
12924 
12925 	if (link_re_init) {
12926 		mutex_lock(&bp->link_lock);
12927 		rc = bnxt_update_phy_setting(bp);
12928 		mutex_unlock(&bp->link_lock);
12929 		if (rc) {
12930 			netdev_warn(bp->dev, "failed to update phy settings\n");
12931 			if (BNXT_SINGLE_PF(bp)) {
12932 				bp->link_info.phy_retry = true;
12933 				bp->link_info.phy_retry_expires =
12934 					jiffies + 5 * HZ;
12935 			}
12936 		}
12937 	}
12938 
12939 	if (irq_re_init) {
12940 		udp_tunnel_nic_reset_ntf(bp->dev);
12941 		rc = bnxt_set_xps_mapping(bp);
12942 		if (rc)
12943 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12944 	}
12945 
12946 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12947 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12948 			static_branch_enable(&bnxt_xdp_locking_key);
12949 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12950 		static_branch_disable(&bnxt_xdp_locking_key);
12951 	}
12952 	set_bit(BNXT_STATE_OPEN, &bp->state);
12953 	bnxt_enable_int(bp);
12954 	/* Enable TX queues */
12955 	bnxt_tx_enable(bp);
12956 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12957 	/* Poll link status and check for SFP+ module status */
12958 	mutex_lock(&bp->link_lock);
12959 	bnxt_get_port_module_status(bp);
12960 	mutex_unlock(&bp->link_lock);
12961 
12962 	/* VF-reps may need to be re-opened after the PF is re-opened */
12963 	if (BNXT_PF(bp))
12964 		bnxt_vf_reps_open(bp);
12965 	bnxt_ptp_init_rtc(bp, true);
12966 	bnxt_ptp_cfg_tstamp_filters(bp);
12967 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12968 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12969 	bnxt_cfg_usr_fltrs(bp);
12970 	return 0;
12971 
12972 open_err_irq:
12973 	bnxt_del_napi(bp);
12974 
12975 open_err_free_mem:
12976 	bnxt_free_skbs(bp);
12977 	bnxt_free_irq(bp);
12978 	bnxt_free_mem(bp, true);
12979 	return rc;
12980 }
12981 
12982 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12983 {
12984 	int rc = 0;
12985 
12986 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12987 		rc = -EIO;
12988 	if (!rc)
12989 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12990 	if (rc) {
12991 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12992 		netif_close(bp->dev);
12993 	}
12994 	return rc;
12995 }
12996 
12997 /* netdev instance lock held, open the NIC half way by allocating all
12998  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12999  * for offline self tests.
13000  */
13001 int bnxt_half_open_nic(struct bnxt *bp)
13002 {
13003 	int rc = 0;
13004 
13005 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13006 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
13007 		rc = -ENODEV;
13008 		goto half_open_err;
13009 	}
13010 
13011 	rc = bnxt_alloc_mem(bp, true);
13012 	if (rc) {
13013 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
13014 		goto half_open_err;
13015 	}
13016 	bnxt_init_napi(bp);
13017 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13018 	rc = bnxt_init_nic(bp, true);
13019 	if (rc) {
13020 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13021 		bnxt_del_napi(bp);
13022 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
13023 		goto half_open_err;
13024 	}
13025 	return 0;
13026 
13027 half_open_err:
13028 	bnxt_free_skbs(bp);
13029 	bnxt_free_mem(bp, true);
13030 	netif_close(bp->dev);
13031 	return rc;
13032 }
13033 
13034 /* netdev instance lock held, this call can only be made after a previous
13035  * successful call to bnxt_half_open_nic().
13036  */
13037 void bnxt_half_close_nic(struct bnxt *bp)
13038 {
13039 	bnxt_hwrm_resource_free(bp, false, true);
13040 	bnxt_del_napi(bp);
13041 	bnxt_free_skbs(bp);
13042 	bnxt_free_mem(bp, true);
13043 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13044 }
13045 
13046 void bnxt_reenable_sriov(struct bnxt *bp)
13047 {
13048 	if (BNXT_PF(bp)) {
13049 		struct bnxt_pf_info *pf = &bp->pf;
13050 		int n = pf->active_vfs;
13051 
13052 		if (n)
13053 			bnxt_cfg_hw_sriov(bp, &n, true);
13054 	}
13055 }
13056 
13057 static int bnxt_open(struct net_device *dev)
13058 {
13059 	struct bnxt *bp = netdev_priv(dev);
13060 	int rc;
13061 
13062 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13063 		rc = bnxt_reinit_after_abort(bp);
13064 		if (rc) {
13065 			if (rc == -EBUSY)
13066 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13067 			else
13068 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13069 			return -ENODEV;
13070 		}
13071 	}
13072 
13073 	rc = bnxt_hwrm_if_change(bp, true);
13074 	if (rc)
13075 		return rc;
13076 
13077 	rc = __bnxt_open_nic(bp, true, true);
13078 	if (rc) {
13079 		bnxt_hwrm_if_change(bp, false);
13080 	} else {
13081 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13082 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13083 				bnxt_queue_sp_work(bp,
13084 						   BNXT_RESTART_ULP_SP_EVENT);
13085 		}
13086 	}
13087 
13088 	return rc;
13089 }
13090 
13091 static bool bnxt_drv_busy(struct bnxt *bp)
13092 {
13093 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13094 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13095 }
13096 
13097 static void bnxt_get_ring_stats(struct bnxt *bp,
13098 				struct rtnl_link_stats64 *stats);
13099 
13100 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13101 			     bool link_re_init)
13102 {
13103 	/* Close the VF-reps before closing PF */
13104 	if (BNXT_PF(bp))
13105 		bnxt_vf_reps_close(bp);
13106 
13107 	/* Change device state to avoid TX queue wake up's */
13108 	bnxt_tx_disable(bp);
13109 
13110 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13111 	smp_mb__after_atomic();
13112 	while (bnxt_drv_busy(bp))
13113 		msleep(20);
13114 
13115 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13116 		bnxt_clear_rss_ctxs(bp);
13117 	/* Flush rings and disable interrupts */
13118 	bnxt_shutdown_nic(bp, irq_re_init);
13119 
13120 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13121 
13122 	bnxt_debug_dev_exit(bp);
13123 	bnxt_disable_napi(bp);
13124 	timer_delete_sync(&bp->timer);
13125 	bnxt_free_skbs(bp);
13126 
13127 	/* Save ring stats before shutdown */
13128 	if (bp->bnapi && irq_re_init) {
13129 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13130 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13131 	}
13132 	if (irq_re_init) {
13133 		bnxt_free_irq(bp);
13134 		bnxt_del_napi(bp);
13135 	}
13136 	bnxt_free_mem(bp, irq_re_init);
13137 }
13138 
13139 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13140 {
13141 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13142 		/* If we get here, it means firmware reset is in progress
13143 		 * while we are trying to close.  We can safely proceed with
13144 		 * the close because we are holding netdev instance lock.
13145 		 * Some firmware messages may fail as we proceed to close.
13146 		 * We set the ABORT_ERR flag here so that the FW reset thread
13147 		 * will later abort when it gets the netdev instance lock
13148 		 * and sees the flag.
13149 		 */
13150 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13151 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13152 	}
13153 
13154 #ifdef CONFIG_BNXT_SRIOV
13155 	if (bp->sriov_cfg) {
13156 		int rc;
13157 
13158 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13159 						      !bp->sriov_cfg,
13160 						      BNXT_SRIOV_CFG_WAIT_TMO);
13161 		if (!rc)
13162 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13163 		else if (rc < 0)
13164 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13165 	}
13166 #endif
13167 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13168 }
13169 
13170 static int bnxt_close(struct net_device *dev)
13171 {
13172 	struct bnxt *bp = netdev_priv(dev);
13173 
13174 	bnxt_close_nic(bp, true, true);
13175 	bnxt_hwrm_shutdown_link(bp);
13176 	bnxt_hwrm_if_change(bp, false);
13177 	return 0;
13178 }
13179 
13180 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13181 				   u16 *val)
13182 {
13183 	struct hwrm_port_phy_mdio_read_output *resp;
13184 	struct hwrm_port_phy_mdio_read_input *req;
13185 	int rc;
13186 
13187 	if (bp->hwrm_spec_code < 0x10a00)
13188 		return -EOPNOTSUPP;
13189 
13190 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13191 	if (rc)
13192 		return rc;
13193 
13194 	req->port_id = cpu_to_le16(bp->pf.port_id);
13195 	req->phy_addr = phy_addr;
13196 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13197 	if (mdio_phy_id_is_c45(phy_addr)) {
13198 		req->cl45_mdio = 1;
13199 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13200 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13201 		req->reg_addr = cpu_to_le16(reg);
13202 	}
13203 
13204 	resp = hwrm_req_hold(bp, req);
13205 	rc = hwrm_req_send(bp, req);
13206 	if (!rc)
13207 		*val = le16_to_cpu(resp->reg_data);
13208 	hwrm_req_drop(bp, req);
13209 	return rc;
13210 }
13211 
13212 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13213 				    u16 val)
13214 {
13215 	struct hwrm_port_phy_mdio_write_input *req;
13216 	int rc;
13217 
13218 	if (bp->hwrm_spec_code < 0x10a00)
13219 		return -EOPNOTSUPP;
13220 
13221 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13222 	if (rc)
13223 		return rc;
13224 
13225 	req->port_id = cpu_to_le16(bp->pf.port_id);
13226 	req->phy_addr = phy_addr;
13227 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13228 	if (mdio_phy_id_is_c45(phy_addr)) {
13229 		req->cl45_mdio = 1;
13230 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13231 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13232 		req->reg_addr = cpu_to_le16(reg);
13233 	}
13234 	req->reg_data = cpu_to_le16(val);
13235 
13236 	return hwrm_req_send(bp, req);
13237 }
13238 
13239 /* netdev instance lock held */
13240 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13241 {
13242 	struct mii_ioctl_data *mdio = if_mii(ifr);
13243 	struct bnxt *bp = netdev_priv(dev);
13244 	int rc;
13245 
13246 	switch (cmd) {
13247 	case SIOCGMIIPHY:
13248 		mdio->phy_id = bp->link_info.phy_addr;
13249 
13250 		fallthrough;
13251 	case SIOCGMIIREG: {
13252 		u16 mii_regval = 0;
13253 
13254 		if (!netif_running(dev))
13255 			return -EAGAIN;
13256 
13257 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13258 					     &mii_regval);
13259 		mdio->val_out = mii_regval;
13260 		return rc;
13261 	}
13262 
13263 	case SIOCSMIIREG:
13264 		if (!netif_running(dev))
13265 			return -EAGAIN;
13266 
13267 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13268 						mdio->val_in);
13269 
13270 	case SIOCSHWTSTAMP:
13271 		return bnxt_hwtstamp_set(dev, ifr);
13272 
13273 	case SIOCGHWTSTAMP:
13274 		return bnxt_hwtstamp_get(dev, ifr);
13275 
13276 	default:
13277 		/* do nothing */
13278 		break;
13279 	}
13280 	return -EOPNOTSUPP;
13281 }
13282 
13283 static void bnxt_get_ring_stats(struct bnxt *bp,
13284 				struct rtnl_link_stats64 *stats)
13285 {
13286 	int i;
13287 
13288 	for (i = 0; i < bp->cp_nr_rings; i++) {
13289 		struct bnxt_napi *bnapi = bp->bnapi[i];
13290 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13291 		u64 *sw = cpr->stats.sw_stats;
13292 
13293 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13294 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13295 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13296 
13297 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13298 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13299 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13300 
13301 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13302 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13303 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13304 
13305 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13306 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13307 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13308 
13309 		stats->rx_missed_errors +=
13310 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13311 
13312 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13313 
13314 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13315 
13316 		stats->rx_dropped +=
13317 			cpr->sw_stats->rx.rx_netpoll_discards +
13318 			cpr->sw_stats->rx.rx_oom_discards;
13319 	}
13320 }
13321 
13322 static void bnxt_add_prev_stats(struct bnxt *bp,
13323 				struct rtnl_link_stats64 *stats)
13324 {
13325 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13326 
13327 	stats->rx_packets += prev_stats->rx_packets;
13328 	stats->tx_packets += prev_stats->tx_packets;
13329 	stats->rx_bytes += prev_stats->rx_bytes;
13330 	stats->tx_bytes += prev_stats->tx_bytes;
13331 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13332 	stats->multicast += prev_stats->multicast;
13333 	stats->rx_dropped += prev_stats->rx_dropped;
13334 	stats->tx_dropped += prev_stats->tx_dropped;
13335 }
13336 
13337 static void
13338 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13339 {
13340 	struct bnxt *bp = netdev_priv(dev);
13341 
13342 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13343 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13344 	 * we check the BNXT_STATE_OPEN flag.
13345 	 */
13346 	smp_mb__after_atomic();
13347 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13348 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13349 		*stats = bp->net_stats_prev;
13350 		return;
13351 	}
13352 
13353 	bnxt_get_ring_stats(bp, stats);
13354 	bnxt_add_prev_stats(bp, stats);
13355 
13356 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13357 		u64 *rx = bp->port_stats.sw_stats;
13358 		u64 *tx = bp->port_stats.sw_stats +
13359 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13360 
13361 		stats->rx_crc_errors =
13362 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13363 		stats->rx_frame_errors =
13364 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13365 		stats->rx_length_errors =
13366 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13367 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13368 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13369 		stats->rx_errors =
13370 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13371 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13372 		stats->collisions =
13373 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13374 		stats->tx_fifo_errors =
13375 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13376 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13377 	}
13378 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13379 }
13380 
13381 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13382 					struct bnxt_total_ring_err_stats *stats,
13383 					struct bnxt_cp_ring_info *cpr)
13384 {
13385 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13386 	u64 *hw_stats = cpr->stats.sw_stats;
13387 
13388 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13389 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13390 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13391 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13392 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13393 	stats->rx_total_ring_discards +=
13394 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13395 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13396 	stats->tx_total_ring_discards +=
13397 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13398 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13399 }
13400 
13401 void bnxt_get_ring_err_stats(struct bnxt *bp,
13402 			     struct bnxt_total_ring_err_stats *stats)
13403 {
13404 	int i;
13405 
13406 	for (i = 0; i < bp->cp_nr_rings; i++)
13407 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13408 }
13409 
13410 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13411 {
13412 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13413 	struct net_device *dev = bp->dev;
13414 	struct netdev_hw_addr *ha;
13415 	u8 *haddr;
13416 	int mc_count = 0;
13417 	bool update = false;
13418 	int off = 0;
13419 
13420 	netdev_for_each_mc_addr(ha, dev) {
13421 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13422 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13423 			vnic->mc_list_count = 0;
13424 			return false;
13425 		}
13426 		haddr = ha->addr;
13427 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13428 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13429 			update = true;
13430 		}
13431 		off += ETH_ALEN;
13432 		mc_count++;
13433 	}
13434 	if (mc_count)
13435 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13436 
13437 	if (mc_count != vnic->mc_list_count) {
13438 		vnic->mc_list_count = mc_count;
13439 		update = true;
13440 	}
13441 	return update;
13442 }
13443 
13444 static bool bnxt_uc_list_updated(struct bnxt *bp)
13445 {
13446 	struct net_device *dev = bp->dev;
13447 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13448 	struct netdev_hw_addr *ha;
13449 	int off = 0;
13450 
13451 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13452 		return true;
13453 
13454 	netdev_for_each_uc_addr(ha, dev) {
13455 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13456 			return true;
13457 
13458 		off += ETH_ALEN;
13459 	}
13460 	return false;
13461 }
13462 
13463 static void bnxt_set_rx_mode(struct net_device *dev)
13464 {
13465 	struct bnxt *bp = netdev_priv(dev);
13466 	struct bnxt_vnic_info *vnic;
13467 	bool mc_update = false;
13468 	bool uc_update;
13469 	u32 mask;
13470 
13471 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13472 		return;
13473 
13474 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13475 	mask = vnic->rx_mask;
13476 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13477 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13478 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13479 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13480 
13481 	if (dev->flags & IFF_PROMISC)
13482 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13483 
13484 	uc_update = bnxt_uc_list_updated(bp);
13485 
13486 	if (dev->flags & IFF_BROADCAST)
13487 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13488 	if (dev->flags & IFF_ALLMULTI) {
13489 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13490 		vnic->mc_list_count = 0;
13491 	} else if (dev->flags & IFF_MULTICAST) {
13492 		mc_update = bnxt_mc_list_updated(bp, &mask);
13493 	}
13494 
13495 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13496 		vnic->rx_mask = mask;
13497 
13498 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13499 	}
13500 }
13501 
13502 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13503 {
13504 	struct net_device *dev = bp->dev;
13505 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13506 	struct netdev_hw_addr *ha;
13507 	int i, off = 0, rc;
13508 	bool uc_update;
13509 
13510 	netif_addr_lock_bh(dev);
13511 	uc_update = bnxt_uc_list_updated(bp);
13512 	netif_addr_unlock_bh(dev);
13513 
13514 	if (!uc_update)
13515 		goto skip_uc;
13516 
13517 	for (i = 1; i < vnic->uc_filter_count; i++) {
13518 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13519 
13520 		bnxt_hwrm_l2_filter_free(bp, fltr);
13521 		bnxt_del_l2_filter(bp, fltr);
13522 	}
13523 
13524 	vnic->uc_filter_count = 1;
13525 
13526 	netif_addr_lock_bh(dev);
13527 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13528 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13529 	} else {
13530 		netdev_for_each_uc_addr(ha, dev) {
13531 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13532 			off += ETH_ALEN;
13533 			vnic->uc_filter_count++;
13534 		}
13535 	}
13536 	netif_addr_unlock_bh(dev);
13537 
13538 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13539 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13540 		if (rc) {
13541 			if (BNXT_VF(bp) && rc == -ENODEV) {
13542 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13543 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13544 				else
13545 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13546 				rc = 0;
13547 			} else {
13548 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13549 			}
13550 			vnic->uc_filter_count = i;
13551 			return rc;
13552 		}
13553 	}
13554 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13555 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13556 
13557 skip_uc:
13558 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13559 	    !bnxt_promisc_ok(bp))
13560 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13561 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13562 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13563 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13564 			    rc);
13565 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13566 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13567 		vnic->mc_list_count = 0;
13568 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13569 	}
13570 	if (rc)
13571 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13572 			   rc);
13573 
13574 	return rc;
13575 }
13576 
13577 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13578 {
13579 #ifdef CONFIG_BNXT_SRIOV
13580 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13581 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13582 
13583 		/* No minimum rings were provisioned by the PF.  Don't
13584 		 * reserve rings by default when device is down.
13585 		 */
13586 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13587 			return true;
13588 
13589 		if (!netif_running(bp->dev))
13590 			return false;
13591 	}
13592 #endif
13593 	return true;
13594 }
13595 
13596 /* If the chip and firmware supports RFS */
13597 static bool bnxt_rfs_supported(struct bnxt *bp)
13598 {
13599 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13600 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13601 			return true;
13602 		return false;
13603 	}
13604 	/* 212 firmware is broken for aRFS */
13605 	if (BNXT_FW_MAJ(bp) == 212)
13606 		return false;
13607 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13608 		return true;
13609 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13610 		return true;
13611 	return false;
13612 }
13613 
13614 /* If runtime conditions support RFS */
13615 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13616 {
13617 	struct bnxt_hw_rings hwr = {0};
13618 	int max_vnics, max_rss_ctxs;
13619 
13620 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13621 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13622 		return bnxt_rfs_supported(bp);
13623 
13624 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13625 		return false;
13626 
13627 	hwr.grp = bp->rx_nr_rings;
13628 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13629 	if (new_rss_ctx)
13630 		hwr.vnic++;
13631 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13632 	max_vnics = bnxt_get_max_func_vnics(bp);
13633 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13634 
13635 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13636 		if (bp->rx_nr_rings > 1)
13637 			netdev_warn(bp->dev,
13638 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13639 				    min(max_rss_ctxs - 1, max_vnics - 1));
13640 		return false;
13641 	}
13642 
13643 	if (!BNXT_NEW_RM(bp))
13644 		return true;
13645 
13646 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13647 	 * issue that will mess up the default VNIC if we reduce the
13648 	 * reservations.
13649 	 */
13650 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13651 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13652 		return true;
13653 
13654 	bnxt_hwrm_reserve_rings(bp, &hwr);
13655 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13656 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13657 		return true;
13658 
13659 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13660 	hwr.vnic = 1;
13661 	hwr.rss_ctx = 0;
13662 	bnxt_hwrm_reserve_rings(bp, &hwr);
13663 	return false;
13664 }
13665 
13666 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13667 					   netdev_features_t features)
13668 {
13669 	struct bnxt *bp = netdev_priv(dev);
13670 	netdev_features_t vlan_features;
13671 
13672 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13673 		features &= ~NETIF_F_NTUPLE;
13674 
13675 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13676 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13677 
13678 	if (!(features & NETIF_F_GRO))
13679 		features &= ~NETIF_F_GRO_HW;
13680 
13681 	if (features & NETIF_F_GRO_HW)
13682 		features &= ~NETIF_F_LRO;
13683 
13684 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13685 	 * turned on or off together.
13686 	 */
13687 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13688 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13689 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13690 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13691 		else if (vlan_features)
13692 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13693 	}
13694 #ifdef CONFIG_BNXT_SRIOV
13695 	if (BNXT_VF(bp) && bp->vf.vlan)
13696 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13697 #endif
13698 	return features;
13699 }
13700 
13701 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13702 				bool link_re_init, u32 flags, bool update_tpa)
13703 {
13704 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13705 	bp->flags = flags;
13706 	if (update_tpa)
13707 		bnxt_set_ring_params(bp);
13708 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13709 }
13710 
13711 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13712 {
13713 	bool update_tpa = false, update_ntuple = false;
13714 	struct bnxt *bp = netdev_priv(dev);
13715 	u32 flags = bp->flags;
13716 	u32 changes;
13717 	int rc = 0;
13718 	bool re_init = false;
13719 
13720 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13721 	if (features & NETIF_F_GRO_HW)
13722 		flags |= BNXT_FLAG_GRO;
13723 	else if (features & NETIF_F_LRO)
13724 		flags |= BNXT_FLAG_LRO;
13725 
13726 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13727 		flags &= ~BNXT_FLAG_TPA;
13728 
13729 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13730 		flags |= BNXT_FLAG_STRIP_VLAN;
13731 
13732 	if (features & NETIF_F_NTUPLE)
13733 		flags |= BNXT_FLAG_RFS;
13734 	else
13735 		bnxt_clear_usr_fltrs(bp, true);
13736 
13737 	changes = flags ^ bp->flags;
13738 	if (changes & BNXT_FLAG_TPA) {
13739 		update_tpa = true;
13740 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13741 		    (flags & BNXT_FLAG_TPA) == 0 ||
13742 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13743 			re_init = true;
13744 	}
13745 
13746 	if (changes & ~BNXT_FLAG_TPA)
13747 		re_init = true;
13748 
13749 	if (changes & BNXT_FLAG_RFS)
13750 		update_ntuple = true;
13751 
13752 	if (flags != bp->flags) {
13753 		u32 old_flags = bp->flags;
13754 
13755 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13756 			bp->flags = flags;
13757 			if (update_tpa)
13758 				bnxt_set_ring_params(bp);
13759 			return rc;
13760 		}
13761 
13762 		if (update_ntuple)
13763 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13764 
13765 		if (re_init)
13766 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13767 
13768 		if (update_tpa) {
13769 			bp->flags = flags;
13770 			rc = bnxt_set_tpa(bp,
13771 					  (flags & BNXT_FLAG_TPA) ?
13772 					  true : false);
13773 			if (rc)
13774 				bp->flags = old_flags;
13775 		}
13776 	}
13777 	return rc;
13778 }
13779 
13780 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13781 			      u8 **nextp)
13782 {
13783 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13784 	struct hop_jumbo_hdr *jhdr;
13785 	int hdr_count = 0;
13786 	u8 *nexthdr;
13787 	int start;
13788 
13789 	/* Check that there are at most 2 IPv6 extension headers, no
13790 	 * fragment header, and each is <= 64 bytes.
13791 	 */
13792 	start = nw_off + sizeof(*ip6h);
13793 	nexthdr = &ip6h->nexthdr;
13794 	while (ipv6_ext_hdr(*nexthdr)) {
13795 		struct ipv6_opt_hdr *hp;
13796 		int hdrlen;
13797 
13798 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13799 		    *nexthdr == NEXTHDR_FRAGMENT)
13800 			return false;
13801 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13802 					  skb_headlen(skb), NULL);
13803 		if (!hp)
13804 			return false;
13805 		if (*nexthdr == NEXTHDR_AUTH)
13806 			hdrlen = ipv6_authlen(hp);
13807 		else
13808 			hdrlen = ipv6_optlen(hp);
13809 
13810 		if (hdrlen > 64)
13811 			return false;
13812 
13813 		/* The ext header may be a hop-by-hop header inserted for
13814 		 * big TCP purposes. This will be removed before sending
13815 		 * from NIC, so do not count it.
13816 		 */
13817 		if (*nexthdr == NEXTHDR_HOP) {
13818 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13819 				goto increment_hdr;
13820 
13821 			jhdr = (struct hop_jumbo_hdr *)hp;
13822 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13823 			    jhdr->nexthdr != IPPROTO_TCP)
13824 				goto increment_hdr;
13825 
13826 			goto next_hdr;
13827 		}
13828 increment_hdr:
13829 		hdr_count++;
13830 next_hdr:
13831 		nexthdr = &hp->nexthdr;
13832 		start += hdrlen;
13833 	}
13834 	if (nextp) {
13835 		/* Caller will check inner protocol */
13836 		if (skb->encapsulation) {
13837 			*nextp = nexthdr;
13838 			return true;
13839 		}
13840 		*nextp = NULL;
13841 	}
13842 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13843 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13844 }
13845 
13846 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13847 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13848 {
13849 	struct udphdr *uh = udp_hdr(skb);
13850 	__be16 udp_port = uh->dest;
13851 
13852 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13853 	    udp_port != bp->vxlan_gpe_port)
13854 		return false;
13855 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13856 		struct ethhdr *eh = inner_eth_hdr(skb);
13857 
13858 		switch (eh->h_proto) {
13859 		case htons(ETH_P_IP):
13860 			return true;
13861 		case htons(ETH_P_IPV6):
13862 			return bnxt_exthdr_check(bp, skb,
13863 						 skb_inner_network_offset(skb),
13864 						 NULL);
13865 		}
13866 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13867 		return true;
13868 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13869 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13870 					 NULL);
13871 	}
13872 	return false;
13873 }
13874 
13875 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13876 {
13877 	switch (l4_proto) {
13878 	case IPPROTO_UDP:
13879 		return bnxt_udp_tunl_check(bp, skb);
13880 	case IPPROTO_IPIP:
13881 		return true;
13882 	case IPPROTO_GRE: {
13883 		switch (skb->inner_protocol) {
13884 		default:
13885 			return false;
13886 		case htons(ETH_P_IP):
13887 			return true;
13888 		case htons(ETH_P_IPV6):
13889 			fallthrough;
13890 		}
13891 	}
13892 	case IPPROTO_IPV6:
13893 		/* Check ext headers of inner ipv6 */
13894 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13895 					 NULL);
13896 	}
13897 	return false;
13898 }
13899 
13900 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13901 					     struct net_device *dev,
13902 					     netdev_features_t features)
13903 {
13904 	struct bnxt *bp = netdev_priv(dev);
13905 	u8 *l4_proto;
13906 
13907 	features = vlan_features_check(skb, features);
13908 	switch (vlan_get_protocol(skb)) {
13909 	case htons(ETH_P_IP):
13910 		if (!skb->encapsulation)
13911 			return features;
13912 		l4_proto = &ip_hdr(skb)->protocol;
13913 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13914 			return features;
13915 		break;
13916 	case htons(ETH_P_IPV6):
13917 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13918 				       &l4_proto))
13919 			break;
13920 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13921 			return features;
13922 		break;
13923 	}
13924 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13925 }
13926 
13927 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13928 			 u32 *reg_buf)
13929 {
13930 	struct hwrm_dbg_read_direct_output *resp;
13931 	struct hwrm_dbg_read_direct_input *req;
13932 	__le32 *dbg_reg_buf;
13933 	dma_addr_t mapping;
13934 	int rc, i;
13935 
13936 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13937 	if (rc)
13938 		return rc;
13939 
13940 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13941 					 &mapping);
13942 	if (!dbg_reg_buf) {
13943 		rc = -ENOMEM;
13944 		goto dbg_rd_reg_exit;
13945 	}
13946 
13947 	req->host_dest_addr = cpu_to_le64(mapping);
13948 
13949 	resp = hwrm_req_hold(bp, req);
13950 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13951 	req->read_len32 = cpu_to_le32(num_words);
13952 
13953 	rc = hwrm_req_send(bp, req);
13954 	if (rc || resp->error_code) {
13955 		rc = -EIO;
13956 		goto dbg_rd_reg_exit;
13957 	}
13958 	for (i = 0; i < num_words; i++)
13959 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13960 
13961 dbg_rd_reg_exit:
13962 	hwrm_req_drop(bp, req);
13963 	return rc;
13964 }
13965 
13966 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13967 				       u32 ring_id, u32 *prod, u32 *cons)
13968 {
13969 	struct hwrm_dbg_ring_info_get_output *resp;
13970 	struct hwrm_dbg_ring_info_get_input *req;
13971 	int rc;
13972 
13973 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13974 	if (rc)
13975 		return rc;
13976 
13977 	req->ring_type = ring_type;
13978 	req->fw_ring_id = cpu_to_le32(ring_id);
13979 	resp = hwrm_req_hold(bp, req);
13980 	rc = hwrm_req_send(bp, req);
13981 	if (!rc) {
13982 		*prod = le32_to_cpu(resp->producer_index);
13983 		*cons = le32_to_cpu(resp->consumer_index);
13984 	}
13985 	hwrm_req_drop(bp, req);
13986 	return rc;
13987 }
13988 
13989 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13990 {
13991 	struct bnxt_tx_ring_info *txr;
13992 	int i = bnapi->index, j;
13993 
13994 	bnxt_for_each_napi_tx(j, bnapi, txr)
13995 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13996 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13997 			    txr->tx_cons);
13998 }
13999 
14000 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
14001 {
14002 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
14003 	int i = bnapi->index;
14004 
14005 	if (!rxr)
14006 		return;
14007 
14008 	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",
14009 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
14010 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
14011 		    rxr->rx_sw_agg_prod);
14012 }
14013 
14014 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
14015 {
14016 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
14017 	int i = bnapi->index;
14018 
14019 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
14020 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
14021 }
14022 
14023 static void bnxt_dbg_dump_states(struct bnxt *bp)
14024 {
14025 	int i;
14026 	struct bnxt_napi *bnapi;
14027 
14028 	for (i = 0; i < bp->cp_nr_rings; i++) {
14029 		bnapi = bp->bnapi[i];
14030 		if (netif_msg_drv(bp)) {
14031 			bnxt_dump_tx_sw_state(bnapi);
14032 			bnxt_dump_rx_sw_state(bnapi);
14033 			bnxt_dump_cp_sw_state(bnapi);
14034 		}
14035 	}
14036 }
14037 
14038 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14039 {
14040 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14041 	struct hwrm_ring_reset_input *req;
14042 	struct bnxt_napi *bnapi = rxr->bnapi;
14043 	struct bnxt_cp_ring_info *cpr;
14044 	u16 cp_ring_id;
14045 	int rc;
14046 
14047 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14048 	if (rc)
14049 		return rc;
14050 
14051 	cpr = &bnapi->cp_ring;
14052 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14053 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14054 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14055 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14056 	return hwrm_req_send_silent(bp, req);
14057 }
14058 
14059 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14060 {
14061 	if (!silent)
14062 		bnxt_dbg_dump_states(bp);
14063 	if (netif_running(bp->dev)) {
14064 		bnxt_close_nic(bp, !silent, false);
14065 		bnxt_open_nic(bp, !silent, false);
14066 	}
14067 }
14068 
14069 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14070 {
14071 	struct bnxt *bp = netdev_priv(dev);
14072 
14073 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14074 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14075 }
14076 
14077 static void bnxt_fw_health_check(struct bnxt *bp)
14078 {
14079 	struct bnxt_fw_health *fw_health = bp->fw_health;
14080 	struct pci_dev *pdev = bp->pdev;
14081 	u32 val;
14082 
14083 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14084 		return;
14085 
14086 	/* Make sure it is enabled before checking the tmr_counter. */
14087 	smp_rmb();
14088 	if (fw_health->tmr_counter) {
14089 		fw_health->tmr_counter--;
14090 		return;
14091 	}
14092 
14093 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14094 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14095 		fw_health->arrests++;
14096 		goto fw_reset;
14097 	}
14098 
14099 	fw_health->last_fw_heartbeat = val;
14100 
14101 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14102 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14103 		fw_health->discoveries++;
14104 		goto fw_reset;
14105 	}
14106 
14107 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14108 	return;
14109 
14110 fw_reset:
14111 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14112 }
14113 
14114 static void bnxt_timer(struct timer_list *t)
14115 {
14116 	struct bnxt *bp = timer_container_of(bp, t, timer);
14117 	struct net_device *dev = bp->dev;
14118 
14119 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14120 		return;
14121 
14122 	if (atomic_read(&bp->intr_sem) != 0)
14123 		goto bnxt_restart_timer;
14124 
14125 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14126 		bnxt_fw_health_check(bp);
14127 
14128 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14129 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14130 
14131 	if (bnxt_tc_flower_enabled(bp))
14132 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14133 
14134 #ifdef CONFIG_RFS_ACCEL
14135 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14136 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14137 #endif /*CONFIG_RFS_ACCEL*/
14138 
14139 	if (bp->link_info.phy_retry) {
14140 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14141 			bp->link_info.phy_retry = false;
14142 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14143 		} else {
14144 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14145 		}
14146 	}
14147 
14148 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14149 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14150 
14151 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14152 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14153 
14154 bnxt_restart_timer:
14155 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14156 }
14157 
14158 static void bnxt_lock_sp(struct bnxt *bp)
14159 {
14160 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14161 	 * set.  If the device is being closed, bnxt_close() may be holding
14162 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14163 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14164 	 * instance lock.
14165 	 */
14166 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14167 	netdev_lock(bp->dev);
14168 }
14169 
14170 static void bnxt_unlock_sp(struct bnxt *bp)
14171 {
14172 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14173 	netdev_unlock(bp->dev);
14174 }
14175 
14176 /* Only called from bnxt_sp_task() */
14177 static void bnxt_reset(struct bnxt *bp, bool silent)
14178 {
14179 	bnxt_lock_sp(bp);
14180 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14181 		bnxt_reset_task(bp, silent);
14182 	bnxt_unlock_sp(bp);
14183 }
14184 
14185 /* Only called from bnxt_sp_task() */
14186 static void bnxt_rx_ring_reset(struct bnxt *bp)
14187 {
14188 	int i;
14189 
14190 	bnxt_lock_sp(bp);
14191 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14192 		bnxt_unlock_sp(bp);
14193 		return;
14194 	}
14195 	/* Disable and flush TPA before resetting the RX ring */
14196 	if (bp->flags & BNXT_FLAG_TPA)
14197 		bnxt_set_tpa(bp, false);
14198 	for (i = 0; i < bp->rx_nr_rings; i++) {
14199 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14200 		struct bnxt_cp_ring_info *cpr;
14201 		int rc;
14202 
14203 		if (!rxr->bnapi->in_reset)
14204 			continue;
14205 
14206 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14207 		if (rc) {
14208 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14209 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14210 			else
14211 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14212 					    rc);
14213 			bnxt_reset_task(bp, true);
14214 			break;
14215 		}
14216 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14217 		rxr->rx_prod = 0;
14218 		rxr->rx_agg_prod = 0;
14219 		rxr->rx_sw_agg_prod = 0;
14220 		rxr->rx_next_cons = 0;
14221 		rxr->bnapi->in_reset = false;
14222 		bnxt_alloc_one_rx_ring(bp, i);
14223 		cpr = &rxr->bnapi->cp_ring;
14224 		cpr->sw_stats->rx.rx_resets++;
14225 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14226 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14227 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14228 	}
14229 	if (bp->flags & BNXT_FLAG_TPA)
14230 		bnxt_set_tpa(bp, true);
14231 	bnxt_unlock_sp(bp);
14232 }
14233 
14234 static void bnxt_fw_fatal_close(struct bnxt *bp)
14235 {
14236 	bnxt_tx_disable(bp);
14237 	bnxt_disable_napi(bp);
14238 	bnxt_disable_int_sync(bp);
14239 	bnxt_free_irq(bp);
14240 	bnxt_clear_int_mode(bp);
14241 	pci_disable_device(bp->pdev);
14242 }
14243 
14244 static void bnxt_fw_reset_close(struct bnxt *bp)
14245 {
14246 	/* When firmware is in fatal state, quiesce device and disable
14247 	 * bus master to prevent any potential bad DMAs before freeing
14248 	 * kernel memory.
14249 	 */
14250 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14251 		u16 val = 0;
14252 
14253 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14254 		if (val == 0xffff)
14255 			bp->fw_reset_min_dsecs = 0;
14256 		bnxt_fw_fatal_close(bp);
14257 	}
14258 	__bnxt_close_nic(bp, true, false);
14259 	bnxt_vf_reps_free(bp);
14260 	bnxt_clear_int_mode(bp);
14261 	bnxt_hwrm_func_drv_unrgtr(bp);
14262 	if (pci_is_enabled(bp->pdev))
14263 		pci_disable_device(bp->pdev);
14264 	bnxt_free_ctx_mem(bp, false);
14265 }
14266 
14267 static bool is_bnxt_fw_ok(struct bnxt *bp)
14268 {
14269 	struct bnxt_fw_health *fw_health = bp->fw_health;
14270 	bool no_heartbeat = false, has_reset = false;
14271 	u32 val;
14272 
14273 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14274 	if (val == fw_health->last_fw_heartbeat)
14275 		no_heartbeat = true;
14276 
14277 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14278 	if (val != fw_health->last_fw_reset_cnt)
14279 		has_reset = true;
14280 
14281 	if (!no_heartbeat && has_reset)
14282 		return true;
14283 
14284 	return false;
14285 }
14286 
14287 /* netdev instance lock is acquired before calling this function */
14288 static void bnxt_force_fw_reset(struct bnxt *bp)
14289 {
14290 	struct bnxt_fw_health *fw_health = bp->fw_health;
14291 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14292 	u32 wait_dsecs;
14293 
14294 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14295 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14296 		return;
14297 
14298 	/* we have to serialize with bnxt_refclk_read()*/
14299 	if (ptp) {
14300 		unsigned long flags;
14301 
14302 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14303 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14304 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14305 	} else {
14306 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14307 	}
14308 	bnxt_fw_reset_close(bp);
14309 	wait_dsecs = fw_health->master_func_wait_dsecs;
14310 	if (fw_health->primary) {
14311 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14312 			wait_dsecs = 0;
14313 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14314 	} else {
14315 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14316 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14317 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14318 	}
14319 
14320 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14321 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14322 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14323 }
14324 
14325 void bnxt_fw_exception(struct bnxt *bp)
14326 {
14327 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14328 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14329 	bnxt_ulp_stop(bp);
14330 	bnxt_lock_sp(bp);
14331 	bnxt_force_fw_reset(bp);
14332 	bnxt_unlock_sp(bp);
14333 }
14334 
14335 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14336  * < 0 on error.
14337  */
14338 static int bnxt_get_registered_vfs(struct bnxt *bp)
14339 {
14340 #ifdef CONFIG_BNXT_SRIOV
14341 	int rc;
14342 
14343 	if (!BNXT_PF(bp))
14344 		return 0;
14345 
14346 	rc = bnxt_hwrm_func_qcfg(bp);
14347 	if (rc) {
14348 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14349 		return rc;
14350 	}
14351 	if (bp->pf.registered_vfs)
14352 		return bp->pf.registered_vfs;
14353 	if (bp->sriov_cfg)
14354 		return 1;
14355 #endif
14356 	return 0;
14357 }
14358 
14359 void bnxt_fw_reset(struct bnxt *bp)
14360 {
14361 	bnxt_ulp_stop(bp);
14362 	bnxt_lock_sp(bp);
14363 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14364 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14365 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14366 		int n = 0, tmo;
14367 
14368 		/* we have to serialize with bnxt_refclk_read()*/
14369 		if (ptp) {
14370 			unsigned long flags;
14371 
14372 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14373 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14374 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14375 		} else {
14376 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14377 		}
14378 		if (bp->pf.active_vfs &&
14379 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14380 			n = bnxt_get_registered_vfs(bp);
14381 		if (n < 0) {
14382 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14383 				   n);
14384 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14385 			netif_close(bp->dev);
14386 			goto fw_reset_exit;
14387 		} else if (n > 0) {
14388 			u16 vf_tmo_dsecs = n * 10;
14389 
14390 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14391 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14392 			bp->fw_reset_state =
14393 				BNXT_FW_RESET_STATE_POLL_VF;
14394 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14395 			goto fw_reset_exit;
14396 		}
14397 		bnxt_fw_reset_close(bp);
14398 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14399 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14400 			tmo = HZ / 10;
14401 		} else {
14402 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14403 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14404 		}
14405 		bnxt_queue_fw_reset_work(bp, tmo);
14406 	}
14407 fw_reset_exit:
14408 	bnxt_unlock_sp(bp);
14409 }
14410 
14411 static void bnxt_chk_missed_irq(struct bnxt *bp)
14412 {
14413 	int i;
14414 
14415 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14416 		return;
14417 
14418 	for (i = 0; i < bp->cp_nr_rings; i++) {
14419 		struct bnxt_napi *bnapi = bp->bnapi[i];
14420 		struct bnxt_cp_ring_info *cpr;
14421 		u32 fw_ring_id;
14422 		int j;
14423 
14424 		if (!bnapi)
14425 			continue;
14426 
14427 		cpr = &bnapi->cp_ring;
14428 		for (j = 0; j < cpr->cp_ring_count; j++) {
14429 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14430 			u32 val[2];
14431 
14432 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14433 				continue;
14434 
14435 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14436 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14437 				continue;
14438 			}
14439 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14440 			bnxt_dbg_hwrm_ring_info_get(bp,
14441 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14442 				fw_ring_id, &val[0], &val[1]);
14443 			cpr->sw_stats->cmn.missed_irqs++;
14444 		}
14445 	}
14446 }
14447 
14448 static void bnxt_cfg_ntp_filters(struct bnxt *);
14449 
14450 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14451 {
14452 	struct bnxt_link_info *link_info = &bp->link_info;
14453 
14454 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14455 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14456 		if (bp->hwrm_spec_code >= 0x10201) {
14457 			if (link_info->auto_pause_setting &
14458 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14459 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14460 		} else {
14461 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14462 		}
14463 		bnxt_set_auto_speed(link_info);
14464 	} else {
14465 		bnxt_set_force_speed(link_info);
14466 		link_info->req_duplex = link_info->duplex_setting;
14467 	}
14468 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14469 		link_info->req_flow_ctrl =
14470 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14471 	else
14472 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14473 }
14474 
14475 static void bnxt_fw_echo_reply(struct bnxt *bp)
14476 {
14477 	struct bnxt_fw_health *fw_health = bp->fw_health;
14478 	struct hwrm_func_echo_response_input *req;
14479 	int rc;
14480 
14481 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14482 	if (rc)
14483 		return;
14484 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14485 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14486 	hwrm_req_send(bp, req);
14487 }
14488 
14489 static void bnxt_ulp_restart(struct bnxt *bp)
14490 {
14491 	bnxt_ulp_stop(bp);
14492 	bnxt_ulp_start(bp, 0);
14493 }
14494 
14495 static void bnxt_sp_task(struct work_struct *work)
14496 {
14497 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14498 
14499 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14500 	smp_mb__after_atomic();
14501 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14502 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14503 		return;
14504 	}
14505 
14506 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14507 		bnxt_ulp_restart(bp);
14508 		bnxt_reenable_sriov(bp);
14509 	}
14510 
14511 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14512 		bnxt_cfg_rx_mode(bp);
14513 
14514 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14515 		bnxt_cfg_ntp_filters(bp);
14516 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14517 		bnxt_hwrm_exec_fwd_req(bp);
14518 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14519 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14520 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14521 		bnxt_hwrm_port_qstats(bp, 0);
14522 		bnxt_hwrm_port_qstats_ext(bp, 0);
14523 		bnxt_accumulate_all_stats(bp);
14524 	}
14525 
14526 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14527 		int rc;
14528 
14529 		mutex_lock(&bp->link_lock);
14530 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14531 				       &bp->sp_event))
14532 			bnxt_hwrm_phy_qcaps(bp);
14533 
14534 		rc = bnxt_update_link(bp, true);
14535 		if (rc)
14536 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14537 				   rc);
14538 
14539 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14540 				       &bp->sp_event))
14541 			bnxt_init_ethtool_link_settings(bp);
14542 		mutex_unlock(&bp->link_lock);
14543 	}
14544 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14545 		int rc;
14546 
14547 		mutex_lock(&bp->link_lock);
14548 		rc = bnxt_update_phy_setting(bp);
14549 		mutex_unlock(&bp->link_lock);
14550 		if (rc) {
14551 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14552 		} else {
14553 			bp->link_info.phy_retry = false;
14554 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14555 		}
14556 	}
14557 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14558 		mutex_lock(&bp->link_lock);
14559 		bnxt_get_port_module_status(bp);
14560 		mutex_unlock(&bp->link_lock);
14561 	}
14562 
14563 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14564 		bnxt_tc_flow_stats_work(bp);
14565 
14566 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14567 		bnxt_chk_missed_irq(bp);
14568 
14569 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14570 		bnxt_fw_echo_reply(bp);
14571 
14572 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14573 		bnxt_hwmon_notify_event(bp);
14574 
14575 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14576 	 * must be the last functions to be called before exiting.
14577 	 */
14578 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14579 		bnxt_reset(bp, false);
14580 
14581 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14582 		bnxt_reset(bp, true);
14583 
14584 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14585 		bnxt_rx_ring_reset(bp);
14586 
14587 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14588 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14589 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14590 			bnxt_devlink_health_fw_report(bp);
14591 		else
14592 			bnxt_fw_reset(bp);
14593 	}
14594 
14595 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14596 		if (!is_bnxt_fw_ok(bp))
14597 			bnxt_devlink_health_fw_report(bp);
14598 	}
14599 
14600 	smp_mb__before_atomic();
14601 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14602 }
14603 
14604 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14605 				int *max_cp);
14606 
14607 /* Under netdev instance lock */
14608 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14609 		     int tx_xdp)
14610 {
14611 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14612 	struct bnxt_hw_rings hwr = {0};
14613 	int rx_rings = rx;
14614 	int rc;
14615 
14616 	if (tcs)
14617 		tx_sets = tcs;
14618 
14619 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14620 
14621 	if (max_rx < rx_rings)
14622 		return -ENOMEM;
14623 
14624 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14625 		rx_rings <<= 1;
14626 
14627 	hwr.rx = rx_rings;
14628 	hwr.tx = tx * tx_sets + tx_xdp;
14629 	if (max_tx < hwr.tx)
14630 		return -ENOMEM;
14631 
14632 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14633 
14634 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14635 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14636 	if (max_cp < hwr.cp)
14637 		return -ENOMEM;
14638 	hwr.stat = hwr.cp;
14639 	if (BNXT_NEW_RM(bp)) {
14640 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14641 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14642 		hwr.grp = rx;
14643 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14644 	}
14645 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14646 		hwr.cp_p5 = hwr.tx + rx;
14647 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14648 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14649 		if (!bnxt_ulp_registered(bp->edev)) {
14650 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14651 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14652 		}
14653 		if (hwr.cp > bp->total_irqs) {
14654 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14655 
14656 			if (total_msix < hwr.cp) {
14657 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14658 					    hwr.cp, total_msix);
14659 				rc = -ENOSPC;
14660 			}
14661 		}
14662 	}
14663 	return rc;
14664 }
14665 
14666 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14667 {
14668 	if (bp->bar2) {
14669 		pci_iounmap(pdev, bp->bar2);
14670 		bp->bar2 = NULL;
14671 	}
14672 
14673 	if (bp->bar1) {
14674 		pci_iounmap(pdev, bp->bar1);
14675 		bp->bar1 = NULL;
14676 	}
14677 
14678 	if (bp->bar0) {
14679 		pci_iounmap(pdev, bp->bar0);
14680 		bp->bar0 = NULL;
14681 	}
14682 }
14683 
14684 static void bnxt_cleanup_pci(struct bnxt *bp)
14685 {
14686 	bnxt_unmap_bars(bp, bp->pdev);
14687 	pci_release_regions(bp->pdev);
14688 	if (pci_is_enabled(bp->pdev))
14689 		pci_disable_device(bp->pdev);
14690 }
14691 
14692 static void bnxt_init_dflt_coal(struct bnxt *bp)
14693 {
14694 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14695 	struct bnxt_coal *coal;
14696 	u16 flags = 0;
14697 
14698 	if (coal_cap->cmpl_params &
14699 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14700 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14701 
14702 	/* Tick values in micro seconds.
14703 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14704 	 */
14705 	coal = &bp->rx_coal;
14706 	coal->coal_ticks = 10;
14707 	coal->coal_bufs = 30;
14708 	coal->coal_ticks_irq = 1;
14709 	coal->coal_bufs_irq = 2;
14710 	coal->idle_thresh = 50;
14711 	coal->bufs_per_record = 2;
14712 	coal->budget = 64;		/* NAPI budget */
14713 	coal->flags = flags;
14714 
14715 	coal = &bp->tx_coal;
14716 	coal->coal_ticks = 28;
14717 	coal->coal_bufs = 30;
14718 	coal->coal_ticks_irq = 2;
14719 	coal->coal_bufs_irq = 2;
14720 	coal->bufs_per_record = 1;
14721 	coal->flags = flags;
14722 
14723 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14724 }
14725 
14726 /* FW that pre-reserves 1 VNIC per function */
14727 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14728 {
14729 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14730 
14731 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14732 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14733 		return true;
14734 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14735 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14736 		return true;
14737 	return false;
14738 }
14739 
14740 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14741 {
14742 	int rc;
14743 
14744 	bp->fw_cap = 0;
14745 	rc = bnxt_hwrm_ver_get(bp);
14746 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14747 	 * so wait before continuing with recovery.
14748 	 */
14749 	if (rc)
14750 		msleep(100);
14751 	bnxt_try_map_fw_health_reg(bp);
14752 	if (rc) {
14753 		rc = bnxt_try_recover_fw(bp);
14754 		if (rc)
14755 			return rc;
14756 		rc = bnxt_hwrm_ver_get(bp);
14757 		if (rc)
14758 			return rc;
14759 	}
14760 
14761 	bnxt_nvm_cfg_ver_get(bp);
14762 
14763 	rc = bnxt_hwrm_func_reset(bp);
14764 	if (rc)
14765 		return -ENODEV;
14766 
14767 	bnxt_hwrm_fw_set_time(bp);
14768 	return 0;
14769 }
14770 
14771 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14772 {
14773 	int rc;
14774 
14775 	/* Get the MAX capabilities for this function */
14776 	rc = bnxt_hwrm_func_qcaps(bp);
14777 	if (rc) {
14778 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14779 			   rc);
14780 		return -ENODEV;
14781 	}
14782 
14783 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14784 	if (rc)
14785 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14786 			    rc);
14787 
14788 	if (bnxt_alloc_fw_health(bp)) {
14789 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14790 	} else {
14791 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14792 		if (rc)
14793 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14794 				    rc);
14795 	}
14796 
14797 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14798 	if (rc)
14799 		return -ENODEV;
14800 
14801 	rc = bnxt_alloc_crash_dump_mem(bp);
14802 	if (rc)
14803 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14804 			    rc);
14805 	if (!rc) {
14806 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14807 		if (rc) {
14808 			bnxt_free_crash_dump_mem(bp);
14809 			netdev_warn(bp->dev,
14810 				    "hwrm crash dump mem failure rc: %d\n", rc);
14811 		}
14812 	}
14813 
14814 	if (bnxt_fw_pre_resv_vnics(bp))
14815 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14816 
14817 	bnxt_hwrm_func_qcfg(bp);
14818 	bnxt_hwrm_vnic_qcaps(bp);
14819 	bnxt_hwrm_port_led_qcaps(bp);
14820 	bnxt_ethtool_init(bp);
14821 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14822 		__bnxt_hwrm_ptp_qcfg(bp);
14823 	bnxt_dcb_init(bp);
14824 	bnxt_hwmon_init(bp);
14825 	return 0;
14826 }
14827 
14828 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14829 {
14830 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14831 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14832 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14833 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14834 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14835 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14836 		bp->rss_hash_delta = bp->rss_hash_cfg;
14837 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14838 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14839 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14840 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14841 	}
14842 }
14843 
14844 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14845 {
14846 	struct net_device *dev = bp->dev;
14847 
14848 	dev->hw_features &= ~NETIF_F_NTUPLE;
14849 	dev->features &= ~NETIF_F_NTUPLE;
14850 	bp->flags &= ~BNXT_FLAG_RFS;
14851 	if (bnxt_rfs_supported(bp)) {
14852 		dev->hw_features |= NETIF_F_NTUPLE;
14853 		if (bnxt_rfs_capable(bp, false)) {
14854 			bp->flags |= BNXT_FLAG_RFS;
14855 			dev->features |= NETIF_F_NTUPLE;
14856 		}
14857 	}
14858 }
14859 
14860 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14861 {
14862 	struct pci_dev *pdev = bp->pdev;
14863 
14864 	bnxt_set_dflt_rss_hash_type(bp);
14865 	bnxt_set_dflt_rfs(bp);
14866 
14867 	bnxt_get_wol_settings(bp);
14868 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14869 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14870 	else
14871 		device_set_wakeup_capable(&pdev->dev, false);
14872 
14873 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14874 	bnxt_hwrm_coal_params_qcaps(bp);
14875 }
14876 
14877 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14878 
14879 int bnxt_fw_init_one(struct bnxt *bp)
14880 {
14881 	int rc;
14882 
14883 	rc = bnxt_fw_init_one_p1(bp);
14884 	if (rc) {
14885 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14886 		return rc;
14887 	}
14888 	rc = bnxt_fw_init_one_p2(bp);
14889 	if (rc) {
14890 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14891 		return rc;
14892 	}
14893 	rc = bnxt_probe_phy(bp, false);
14894 	if (rc)
14895 		return rc;
14896 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14897 	if (rc)
14898 		return rc;
14899 
14900 	bnxt_fw_init_one_p3(bp);
14901 	return 0;
14902 }
14903 
14904 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14905 {
14906 	struct bnxt_fw_health *fw_health = bp->fw_health;
14907 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14908 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14909 	u32 reg_type, reg_off, delay_msecs;
14910 
14911 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14912 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14913 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14914 	switch (reg_type) {
14915 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14916 		pci_write_config_dword(bp->pdev, reg_off, val);
14917 		break;
14918 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14919 		writel(reg_off & BNXT_GRC_BASE_MASK,
14920 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14921 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14922 		fallthrough;
14923 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14924 		writel(val, bp->bar0 + reg_off);
14925 		break;
14926 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14927 		writel(val, bp->bar1 + reg_off);
14928 		break;
14929 	}
14930 	if (delay_msecs) {
14931 		pci_read_config_dword(bp->pdev, 0, &val);
14932 		msleep(delay_msecs);
14933 	}
14934 }
14935 
14936 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14937 {
14938 	struct hwrm_func_qcfg_output *resp;
14939 	struct hwrm_func_qcfg_input *req;
14940 	bool result = true; /* firmware will enforce if unknown */
14941 
14942 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14943 		return result;
14944 
14945 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14946 		return result;
14947 
14948 	req->fid = cpu_to_le16(0xffff);
14949 	resp = hwrm_req_hold(bp, req);
14950 	if (!hwrm_req_send(bp, req))
14951 		result = !!(le16_to_cpu(resp->flags) &
14952 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14953 	hwrm_req_drop(bp, req);
14954 	return result;
14955 }
14956 
14957 static void bnxt_reset_all(struct bnxt *bp)
14958 {
14959 	struct bnxt_fw_health *fw_health = bp->fw_health;
14960 	int i, rc;
14961 
14962 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14963 		bnxt_fw_reset_via_optee(bp);
14964 		bp->fw_reset_timestamp = jiffies;
14965 		return;
14966 	}
14967 
14968 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14969 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14970 			bnxt_fw_reset_writel(bp, i);
14971 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14972 		struct hwrm_fw_reset_input *req;
14973 
14974 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14975 		if (!rc) {
14976 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14977 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14978 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14979 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14980 			rc = hwrm_req_send(bp, req);
14981 		}
14982 		if (rc != -ENODEV)
14983 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14984 	}
14985 	bp->fw_reset_timestamp = jiffies;
14986 }
14987 
14988 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14989 {
14990 	return time_after(jiffies, bp->fw_reset_timestamp +
14991 			  (bp->fw_reset_max_dsecs * HZ / 10));
14992 }
14993 
14994 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14995 {
14996 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14997 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14998 		bnxt_dl_health_fw_status_update(bp, false);
14999 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
15000 	netif_close(bp->dev);
15001 }
15002 
15003 static void bnxt_fw_reset_task(struct work_struct *work)
15004 {
15005 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
15006 	int rc = 0;
15007 
15008 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
15009 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
15010 		return;
15011 	}
15012 
15013 	switch (bp->fw_reset_state) {
15014 	case BNXT_FW_RESET_STATE_POLL_VF: {
15015 		int n = bnxt_get_registered_vfs(bp);
15016 		int tmo;
15017 
15018 		if (n < 0) {
15019 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
15020 				   n, jiffies_to_msecs(jiffies -
15021 				   bp->fw_reset_timestamp));
15022 			goto fw_reset_abort;
15023 		} else if (n > 0) {
15024 			if (bnxt_fw_reset_timeout(bp)) {
15025 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15026 				bp->fw_reset_state = 0;
15027 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15028 					   n);
15029 				goto ulp_start;
15030 			}
15031 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15032 			return;
15033 		}
15034 		bp->fw_reset_timestamp = jiffies;
15035 		netdev_lock(bp->dev);
15036 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15037 			bnxt_fw_reset_abort(bp, rc);
15038 			netdev_unlock(bp->dev);
15039 			goto ulp_start;
15040 		}
15041 		bnxt_fw_reset_close(bp);
15042 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15043 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15044 			tmo = HZ / 10;
15045 		} else {
15046 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15047 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15048 		}
15049 		netdev_unlock(bp->dev);
15050 		bnxt_queue_fw_reset_work(bp, tmo);
15051 		return;
15052 	}
15053 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15054 		u32 val;
15055 
15056 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15057 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15058 		    !bnxt_fw_reset_timeout(bp)) {
15059 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15060 			return;
15061 		}
15062 
15063 		if (!bp->fw_health->primary) {
15064 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15065 
15066 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15067 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15068 			return;
15069 		}
15070 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15071 	}
15072 		fallthrough;
15073 	case BNXT_FW_RESET_STATE_RESET_FW:
15074 		bnxt_reset_all(bp);
15075 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15076 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15077 		return;
15078 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15079 		bnxt_inv_fw_health_reg(bp);
15080 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15081 		    !bp->fw_reset_min_dsecs) {
15082 			u16 val;
15083 
15084 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15085 			if (val == 0xffff) {
15086 				if (bnxt_fw_reset_timeout(bp)) {
15087 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15088 					rc = -ETIMEDOUT;
15089 					goto fw_reset_abort;
15090 				}
15091 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15092 				return;
15093 			}
15094 		}
15095 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15096 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15097 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15098 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15099 			bnxt_dl_remote_reload(bp);
15100 		if (pci_enable_device(bp->pdev)) {
15101 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15102 			rc = -ENODEV;
15103 			goto fw_reset_abort;
15104 		}
15105 		pci_set_master(bp->pdev);
15106 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15107 		fallthrough;
15108 	case BNXT_FW_RESET_STATE_POLL_FW:
15109 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15110 		rc = bnxt_hwrm_poll(bp);
15111 		if (rc) {
15112 			if (bnxt_fw_reset_timeout(bp)) {
15113 				netdev_err(bp->dev, "Firmware reset aborted\n");
15114 				goto fw_reset_abort_status;
15115 			}
15116 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15117 			return;
15118 		}
15119 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15120 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15121 		fallthrough;
15122 	case BNXT_FW_RESET_STATE_OPENING:
15123 		while (!netdev_trylock(bp->dev)) {
15124 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15125 			return;
15126 		}
15127 		rc = bnxt_open(bp->dev);
15128 		if (rc) {
15129 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15130 			bnxt_fw_reset_abort(bp, rc);
15131 			netdev_unlock(bp->dev);
15132 			goto ulp_start;
15133 		}
15134 
15135 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15136 		    bp->fw_health->enabled) {
15137 			bp->fw_health->last_fw_reset_cnt =
15138 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15139 		}
15140 		bp->fw_reset_state = 0;
15141 		/* Make sure fw_reset_state is 0 before clearing the flag */
15142 		smp_mb__before_atomic();
15143 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15144 		bnxt_ptp_reapply_pps(bp);
15145 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15146 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15147 			bnxt_dl_health_fw_recovery_done(bp);
15148 			bnxt_dl_health_fw_status_update(bp, true);
15149 		}
15150 		netdev_unlock(bp->dev);
15151 		bnxt_ulp_start(bp, 0);
15152 		bnxt_reenable_sriov(bp);
15153 		netdev_lock(bp->dev);
15154 		bnxt_vf_reps_alloc(bp);
15155 		bnxt_vf_reps_open(bp);
15156 		netdev_unlock(bp->dev);
15157 		break;
15158 	}
15159 	return;
15160 
15161 fw_reset_abort_status:
15162 	if (bp->fw_health->status_reliable ||
15163 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15164 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15165 
15166 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15167 	}
15168 fw_reset_abort:
15169 	netdev_lock(bp->dev);
15170 	bnxt_fw_reset_abort(bp, rc);
15171 	netdev_unlock(bp->dev);
15172 ulp_start:
15173 	bnxt_ulp_start(bp, rc);
15174 }
15175 
15176 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15177 {
15178 	int rc;
15179 	struct bnxt *bp = netdev_priv(dev);
15180 
15181 	SET_NETDEV_DEV(dev, &pdev->dev);
15182 
15183 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15184 	rc = pci_enable_device(pdev);
15185 	if (rc) {
15186 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15187 		goto init_err;
15188 	}
15189 
15190 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15191 		dev_err(&pdev->dev,
15192 			"Cannot find PCI device base address, aborting\n");
15193 		rc = -ENODEV;
15194 		goto init_err_disable;
15195 	}
15196 
15197 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15198 	if (rc) {
15199 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15200 		goto init_err_disable;
15201 	}
15202 
15203 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15204 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15205 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15206 		rc = -EIO;
15207 		goto init_err_release;
15208 	}
15209 
15210 	pci_set_master(pdev);
15211 
15212 	bp->dev = dev;
15213 	bp->pdev = pdev;
15214 
15215 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15216 	 * determines the BAR size.
15217 	 */
15218 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15219 	if (!bp->bar0) {
15220 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15221 		rc = -ENOMEM;
15222 		goto init_err_release;
15223 	}
15224 
15225 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15226 	if (!bp->bar2) {
15227 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15228 		rc = -ENOMEM;
15229 		goto init_err_release;
15230 	}
15231 
15232 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15233 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15234 
15235 	spin_lock_init(&bp->ntp_fltr_lock);
15236 #if BITS_PER_LONG == 32
15237 	spin_lock_init(&bp->db_lock);
15238 #endif
15239 
15240 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15241 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15242 
15243 	timer_setup(&bp->timer, bnxt_timer, 0);
15244 	bp->current_interval = BNXT_TIMER_INTERVAL;
15245 
15246 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15247 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15248 
15249 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15250 	return 0;
15251 
15252 init_err_release:
15253 	bnxt_unmap_bars(bp, pdev);
15254 	pci_release_regions(pdev);
15255 
15256 init_err_disable:
15257 	pci_disable_device(pdev);
15258 
15259 init_err:
15260 	return rc;
15261 }
15262 
15263 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15264 {
15265 	struct sockaddr *addr = p;
15266 	struct bnxt *bp = netdev_priv(dev);
15267 	int rc = 0;
15268 
15269 	netdev_assert_locked(dev);
15270 
15271 	if (!is_valid_ether_addr(addr->sa_data))
15272 		return -EADDRNOTAVAIL;
15273 
15274 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15275 		return 0;
15276 
15277 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15278 	if (rc)
15279 		return rc;
15280 
15281 	eth_hw_addr_set(dev, addr->sa_data);
15282 	bnxt_clear_usr_fltrs(bp, true);
15283 	if (netif_running(dev)) {
15284 		bnxt_close_nic(bp, false, false);
15285 		rc = bnxt_open_nic(bp, false, false);
15286 	}
15287 
15288 	return rc;
15289 }
15290 
15291 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15292 {
15293 	struct bnxt *bp = netdev_priv(dev);
15294 
15295 	netdev_assert_locked(dev);
15296 
15297 	if (netif_running(dev))
15298 		bnxt_close_nic(bp, true, false);
15299 
15300 	WRITE_ONCE(dev->mtu, new_mtu);
15301 
15302 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15303 	 * program is attached.  We need to set the AGG rings settings and
15304 	 * rx_skb_func accordingly.
15305 	 */
15306 	if (READ_ONCE(bp->xdp_prog))
15307 		bnxt_set_rx_skb_mode(bp, true);
15308 
15309 	bnxt_set_ring_params(bp);
15310 
15311 	if (netif_running(dev))
15312 		return bnxt_open_nic(bp, true, false);
15313 
15314 	return 0;
15315 }
15316 
15317 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15318 {
15319 	struct bnxt *bp = netdev_priv(dev);
15320 	bool sh = false;
15321 	int rc, tx_cp;
15322 
15323 	if (tc > bp->max_tc) {
15324 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15325 			   tc, bp->max_tc);
15326 		return -EINVAL;
15327 	}
15328 
15329 	if (bp->num_tc == tc)
15330 		return 0;
15331 
15332 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15333 		sh = true;
15334 
15335 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15336 			      sh, tc, bp->tx_nr_rings_xdp);
15337 	if (rc)
15338 		return rc;
15339 
15340 	/* Needs to close the device and do hw resource re-allocations */
15341 	if (netif_running(bp->dev))
15342 		bnxt_close_nic(bp, true, false);
15343 
15344 	if (tc) {
15345 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15346 		netdev_set_num_tc(dev, tc);
15347 		bp->num_tc = tc;
15348 	} else {
15349 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15350 		netdev_reset_tc(dev);
15351 		bp->num_tc = 0;
15352 	}
15353 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15354 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15355 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15356 			       tx_cp + bp->rx_nr_rings;
15357 
15358 	if (netif_running(bp->dev))
15359 		return bnxt_open_nic(bp, true, false);
15360 
15361 	return 0;
15362 }
15363 
15364 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15365 				  void *cb_priv)
15366 {
15367 	struct bnxt *bp = cb_priv;
15368 
15369 	if (!bnxt_tc_flower_enabled(bp) ||
15370 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15371 		return -EOPNOTSUPP;
15372 
15373 	switch (type) {
15374 	case TC_SETUP_CLSFLOWER:
15375 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15376 	default:
15377 		return -EOPNOTSUPP;
15378 	}
15379 }
15380 
15381 LIST_HEAD(bnxt_block_cb_list);
15382 
15383 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15384 			 void *type_data)
15385 {
15386 	struct bnxt *bp = netdev_priv(dev);
15387 
15388 	switch (type) {
15389 	case TC_SETUP_BLOCK:
15390 		return flow_block_cb_setup_simple(type_data,
15391 						  &bnxt_block_cb_list,
15392 						  bnxt_setup_tc_block_cb,
15393 						  bp, bp, true);
15394 	case TC_SETUP_QDISC_MQPRIO: {
15395 		struct tc_mqprio_qopt *mqprio = type_data;
15396 
15397 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15398 
15399 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15400 	}
15401 	default:
15402 		return -EOPNOTSUPP;
15403 	}
15404 }
15405 
15406 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15407 			    const struct sk_buff *skb)
15408 {
15409 	struct bnxt_vnic_info *vnic;
15410 
15411 	if (skb)
15412 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15413 
15414 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15415 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15416 }
15417 
15418 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15419 			   u32 idx)
15420 {
15421 	struct hlist_head *head;
15422 	int bit_id;
15423 
15424 	spin_lock_bh(&bp->ntp_fltr_lock);
15425 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15426 	if (bit_id < 0) {
15427 		spin_unlock_bh(&bp->ntp_fltr_lock);
15428 		return -ENOMEM;
15429 	}
15430 
15431 	fltr->base.sw_id = (u16)bit_id;
15432 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15433 	fltr->base.flags |= BNXT_ACT_RING_DST;
15434 	head = &bp->ntp_fltr_hash_tbl[idx];
15435 	hlist_add_head_rcu(&fltr->base.hash, head);
15436 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15437 	bnxt_insert_usr_fltr(bp, &fltr->base);
15438 	bp->ntp_fltr_count++;
15439 	spin_unlock_bh(&bp->ntp_fltr_lock);
15440 	return 0;
15441 }
15442 
15443 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15444 			    struct bnxt_ntuple_filter *f2)
15445 {
15446 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15447 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15448 	struct flow_keys *keys1 = &f1->fkeys;
15449 	struct flow_keys *keys2 = &f2->fkeys;
15450 
15451 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15452 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15453 		return false;
15454 
15455 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15456 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15457 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15458 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15459 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15460 			return false;
15461 	} else {
15462 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15463 				     &keys2->addrs.v6addrs.src) ||
15464 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15465 				     &masks2->addrs.v6addrs.src) ||
15466 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15467 				     &keys2->addrs.v6addrs.dst) ||
15468 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15469 				     &masks2->addrs.v6addrs.dst))
15470 			return false;
15471 	}
15472 
15473 	return keys1->ports.src == keys2->ports.src &&
15474 	       masks1->ports.src == masks2->ports.src &&
15475 	       keys1->ports.dst == keys2->ports.dst &&
15476 	       masks1->ports.dst == masks2->ports.dst &&
15477 	       keys1->control.flags == keys2->control.flags &&
15478 	       f1->l2_fltr == f2->l2_fltr;
15479 }
15480 
15481 struct bnxt_ntuple_filter *
15482 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15483 				struct bnxt_ntuple_filter *fltr, u32 idx)
15484 {
15485 	struct bnxt_ntuple_filter *f;
15486 	struct hlist_head *head;
15487 
15488 	head = &bp->ntp_fltr_hash_tbl[idx];
15489 	hlist_for_each_entry_rcu(f, head, base.hash) {
15490 		if (bnxt_fltr_match(f, fltr))
15491 			return f;
15492 	}
15493 	return NULL;
15494 }
15495 
15496 #ifdef CONFIG_RFS_ACCEL
15497 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15498 			      u16 rxq_index, u32 flow_id)
15499 {
15500 	struct bnxt *bp = netdev_priv(dev);
15501 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15502 	struct flow_keys *fkeys;
15503 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15504 	struct bnxt_l2_filter *l2_fltr;
15505 	int rc = 0, idx;
15506 	u32 flags;
15507 
15508 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15509 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15510 		atomic_inc(&l2_fltr->refcnt);
15511 	} else {
15512 		struct bnxt_l2_key key;
15513 
15514 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15515 		key.vlan = 0;
15516 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15517 		if (!l2_fltr)
15518 			return -EINVAL;
15519 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15520 			bnxt_del_l2_filter(bp, l2_fltr);
15521 			return -EINVAL;
15522 		}
15523 	}
15524 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15525 	if (!new_fltr) {
15526 		bnxt_del_l2_filter(bp, l2_fltr);
15527 		return -ENOMEM;
15528 	}
15529 
15530 	fkeys = &new_fltr->fkeys;
15531 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15532 		rc = -EPROTONOSUPPORT;
15533 		goto err_free;
15534 	}
15535 
15536 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15537 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15538 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15539 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15540 		rc = -EPROTONOSUPPORT;
15541 		goto err_free;
15542 	}
15543 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15544 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15545 		if (bp->hwrm_spec_code < 0x10601) {
15546 			rc = -EPROTONOSUPPORT;
15547 			goto err_free;
15548 		}
15549 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15550 	}
15551 	flags = fkeys->control.flags;
15552 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15553 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15554 		rc = -EPROTONOSUPPORT;
15555 		goto err_free;
15556 	}
15557 	new_fltr->l2_fltr = l2_fltr;
15558 
15559 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15560 	rcu_read_lock();
15561 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15562 	if (fltr) {
15563 		rc = fltr->base.sw_id;
15564 		rcu_read_unlock();
15565 		goto err_free;
15566 	}
15567 	rcu_read_unlock();
15568 
15569 	new_fltr->flow_id = flow_id;
15570 	new_fltr->base.rxq = rxq_index;
15571 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15572 	if (!rc) {
15573 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15574 		return new_fltr->base.sw_id;
15575 	}
15576 
15577 err_free:
15578 	bnxt_del_l2_filter(bp, l2_fltr);
15579 	kfree(new_fltr);
15580 	return rc;
15581 }
15582 #endif
15583 
15584 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15585 {
15586 	spin_lock_bh(&bp->ntp_fltr_lock);
15587 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15588 		spin_unlock_bh(&bp->ntp_fltr_lock);
15589 		return;
15590 	}
15591 	hlist_del_rcu(&fltr->base.hash);
15592 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15593 	bp->ntp_fltr_count--;
15594 	spin_unlock_bh(&bp->ntp_fltr_lock);
15595 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15596 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15597 	kfree_rcu(fltr, base.rcu);
15598 }
15599 
15600 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15601 {
15602 #ifdef CONFIG_RFS_ACCEL
15603 	int i;
15604 
15605 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15606 		struct hlist_head *head;
15607 		struct hlist_node *tmp;
15608 		struct bnxt_ntuple_filter *fltr;
15609 		int rc;
15610 
15611 		head = &bp->ntp_fltr_hash_tbl[i];
15612 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15613 			bool del = false;
15614 
15615 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15616 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15617 					continue;
15618 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15619 							fltr->flow_id,
15620 							fltr->base.sw_id)) {
15621 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15622 									 fltr);
15623 					del = true;
15624 				}
15625 			} else {
15626 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15627 								       fltr);
15628 				if (rc)
15629 					del = true;
15630 				else
15631 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15632 			}
15633 
15634 			if (del)
15635 				bnxt_del_ntp_filter(bp, fltr);
15636 		}
15637 	}
15638 #endif
15639 }
15640 
15641 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15642 				    unsigned int entry, struct udp_tunnel_info *ti)
15643 {
15644 	struct bnxt *bp = netdev_priv(netdev);
15645 	unsigned int cmd;
15646 
15647 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15648 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15649 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15650 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15651 	else
15652 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15653 
15654 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15655 }
15656 
15657 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15658 				      unsigned int entry, struct udp_tunnel_info *ti)
15659 {
15660 	struct bnxt *bp = netdev_priv(netdev);
15661 	unsigned int cmd;
15662 
15663 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15664 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15665 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15666 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15667 	else
15668 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15669 
15670 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15671 }
15672 
15673 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15674 	.set_port	= bnxt_udp_tunnel_set_port,
15675 	.unset_port	= bnxt_udp_tunnel_unset_port,
15676 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15677 	.tables		= {
15678 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15679 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15680 	},
15681 }, bnxt_udp_tunnels_p7 = {
15682 	.set_port	= bnxt_udp_tunnel_set_port,
15683 	.unset_port	= bnxt_udp_tunnel_unset_port,
15684 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15685 	.tables		= {
15686 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15687 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15688 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15689 	},
15690 };
15691 
15692 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15693 			       struct net_device *dev, u32 filter_mask,
15694 			       int nlflags)
15695 {
15696 	struct bnxt *bp = netdev_priv(dev);
15697 
15698 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15699 				       nlflags, filter_mask, NULL);
15700 }
15701 
15702 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15703 			       u16 flags, struct netlink_ext_ack *extack)
15704 {
15705 	struct bnxt *bp = netdev_priv(dev);
15706 	struct nlattr *attr, *br_spec;
15707 	int rem, rc = 0;
15708 
15709 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15710 		return -EOPNOTSUPP;
15711 
15712 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15713 	if (!br_spec)
15714 		return -EINVAL;
15715 
15716 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15717 		u16 mode;
15718 
15719 		mode = nla_get_u16(attr);
15720 		if (mode == bp->br_mode)
15721 			break;
15722 
15723 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15724 		if (!rc)
15725 			bp->br_mode = mode;
15726 		break;
15727 	}
15728 	return rc;
15729 }
15730 
15731 int bnxt_get_port_parent_id(struct net_device *dev,
15732 			    struct netdev_phys_item_id *ppid)
15733 {
15734 	struct bnxt *bp = netdev_priv(dev);
15735 
15736 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15737 		return -EOPNOTSUPP;
15738 
15739 	/* The PF and it's VF-reps only support the switchdev framework */
15740 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15741 		return -EOPNOTSUPP;
15742 
15743 	ppid->id_len = sizeof(bp->dsn);
15744 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15745 
15746 	return 0;
15747 }
15748 
15749 static const struct net_device_ops bnxt_netdev_ops = {
15750 	.ndo_open		= bnxt_open,
15751 	.ndo_start_xmit		= bnxt_start_xmit,
15752 	.ndo_stop		= bnxt_close,
15753 	.ndo_get_stats64	= bnxt_get_stats64,
15754 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15755 	.ndo_eth_ioctl		= bnxt_ioctl,
15756 	.ndo_validate_addr	= eth_validate_addr,
15757 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15758 	.ndo_change_mtu		= bnxt_change_mtu,
15759 	.ndo_fix_features	= bnxt_fix_features,
15760 	.ndo_set_features	= bnxt_set_features,
15761 	.ndo_features_check	= bnxt_features_check,
15762 	.ndo_tx_timeout		= bnxt_tx_timeout,
15763 #ifdef CONFIG_BNXT_SRIOV
15764 	.ndo_get_vf_config	= bnxt_get_vf_config,
15765 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15766 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15767 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15768 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15769 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15770 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15771 #endif
15772 	.ndo_setup_tc           = bnxt_setup_tc,
15773 #ifdef CONFIG_RFS_ACCEL
15774 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15775 #endif
15776 	.ndo_bpf		= bnxt_xdp,
15777 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15778 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15779 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15780 };
15781 
15782 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15783 				    struct netdev_queue_stats_rx *stats)
15784 {
15785 	struct bnxt *bp = netdev_priv(dev);
15786 	struct bnxt_cp_ring_info *cpr;
15787 	u64 *sw;
15788 
15789 	if (!bp->bnapi)
15790 		return;
15791 
15792 	cpr = &bp->bnapi[i]->cp_ring;
15793 	sw = cpr->stats.sw_stats;
15794 
15795 	stats->packets = 0;
15796 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15797 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15798 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15799 
15800 	stats->bytes = 0;
15801 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15802 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15803 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15804 
15805 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15806 }
15807 
15808 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15809 				    struct netdev_queue_stats_tx *stats)
15810 {
15811 	struct bnxt *bp = netdev_priv(dev);
15812 	struct bnxt_napi *bnapi;
15813 	u64 *sw;
15814 
15815 	if (!bp->tx_ring)
15816 		return;
15817 
15818 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15819 	sw = bnapi->cp_ring.stats.sw_stats;
15820 
15821 	stats->packets = 0;
15822 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15823 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15824 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15825 
15826 	stats->bytes = 0;
15827 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15828 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15829 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15830 }
15831 
15832 static void bnxt_get_base_stats(struct net_device *dev,
15833 				struct netdev_queue_stats_rx *rx,
15834 				struct netdev_queue_stats_tx *tx)
15835 {
15836 	struct bnxt *bp = netdev_priv(dev);
15837 
15838 	rx->packets = bp->net_stats_prev.rx_packets;
15839 	rx->bytes = bp->net_stats_prev.rx_bytes;
15840 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15841 
15842 	tx->packets = bp->net_stats_prev.tx_packets;
15843 	tx->bytes = bp->net_stats_prev.tx_bytes;
15844 }
15845 
15846 static const struct netdev_stat_ops bnxt_stat_ops = {
15847 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15848 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15849 	.get_base_stats		= bnxt_get_base_stats,
15850 };
15851 
15852 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15853 {
15854 	struct bnxt_rx_ring_info *rxr, *clone;
15855 	struct bnxt *bp = netdev_priv(dev);
15856 	struct bnxt_ring_struct *ring;
15857 	int rc;
15858 
15859 	if (!bp->rx_ring)
15860 		return -ENETDOWN;
15861 
15862 	rxr = &bp->rx_ring[idx];
15863 	clone = qmem;
15864 	memcpy(clone, rxr, sizeof(*rxr));
15865 	bnxt_init_rx_ring_struct(bp, clone);
15866 	bnxt_reset_rx_ring_struct(bp, clone);
15867 
15868 	clone->rx_prod = 0;
15869 	clone->rx_agg_prod = 0;
15870 	clone->rx_sw_agg_prod = 0;
15871 	clone->rx_next_cons = 0;
15872 	clone->need_head_pool = false;
15873 
15874 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15875 	if (rc)
15876 		return rc;
15877 
15878 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15879 	if (rc < 0)
15880 		goto err_page_pool_destroy;
15881 
15882 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15883 					MEM_TYPE_PAGE_POOL,
15884 					clone->page_pool);
15885 	if (rc)
15886 		goto err_rxq_info_unreg;
15887 
15888 	ring = &clone->rx_ring_struct;
15889 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15890 	if (rc)
15891 		goto err_free_rx_ring;
15892 
15893 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15894 		ring = &clone->rx_agg_ring_struct;
15895 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15896 		if (rc)
15897 			goto err_free_rx_agg_ring;
15898 
15899 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15900 		if (rc)
15901 			goto err_free_rx_agg_ring;
15902 	}
15903 
15904 	if (bp->flags & BNXT_FLAG_TPA) {
15905 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15906 		if (rc)
15907 			goto err_free_tpa_info;
15908 	}
15909 
15910 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15911 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15912 
15913 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15914 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15915 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
15916 	if (bp->flags & BNXT_FLAG_TPA)
15917 		bnxt_alloc_one_tpa_info_data(bp, clone);
15918 
15919 	return 0;
15920 
15921 err_free_tpa_info:
15922 	bnxt_free_one_tpa_info(bp, clone);
15923 err_free_rx_agg_ring:
15924 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15925 err_free_rx_ring:
15926 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15927 err_rxq_info_unreg:
15928 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15929 err_page_pool_destroy:
15930 	page_pool_destroy(clone->page_pool);
15931 	page_pool_destroy(clone->head_pool);
15932 	clone->page_pool = NULL;
15933 	clone->head_pool = NULL;
15934 	return rc;
15935 }
15936 
15937 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15938 {
15939 	struct bnxt_rx_ring_info *rxr = qmem;
15940 	struct bnxt *bp = netdev_priv(dev);
15941 	struct bnxt_ring_struct *ring;
15942 
15943 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15944 	bnxt_free_one_tpa_info(bp, rxr);
15945 
15946 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15947 
15948 	page_pool_destroy(rxr->page_pool);
15949 	page_pool_destroy(rxr->head_pool);
15950 	rxr->page_pool = NULL;
15951 	rxr->head_pool = NULL;
15952 
15953 	ring = &rxr->rx_ring_struct;
15954 	bnxt_free_ring(bp, &ring->ring_mem);
15955 
15956 	ring = &rxr->rx_agg_ring_struct;
15957 	bnxt_free_ring(bp, &ring->ring_mem);
15958 
15959 	kfree(rxr->rx_agg_bmap);
15960 	rxr->rx_agg_bmap = NULL;
15961 }
15962 
15963 static void bnxt_copy_rx_ring(struct bnxt *bp,
15964 			      struct bnxt_rx_ring_info *dst,
15965 			      struct bnxt_rx_ring_info *src)
15966 {
15967 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15968 	struct bnxt_ring_struct *dst_ring, *src_ring;
15969 	int i;
15970 
15971 	dst_ring = &dst->rx_ring_struct;
15972 	dst_rmem = &dst_ring->ring_mem;
15973 	src_ring = &src->rx_ring_struct;
15974 	src_rmem = &src_ring->ring_mem;
15975 
15976 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15977 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15978 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15979 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15980 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15981 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15982 
15983 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15984 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15985 	*dst_rmem->vmem = *src_rmem->vmem;
15986 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15987 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15988 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15989 	}
15990 
15991 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15992 		return;
15993 
15994 	dst_ring = &dst->rx_agg_ring_struct;
15995 	dst_rmem = &dst_ring->ring_mem;
15996 	src_ring = &src->rx_agg_ring_struct;
15997 	src_rmem = &src_ring->ring_mem;
15998 
15999 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
16000 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
16001 	WARN_ON(dst_rmem->flags != src_rmem->flags);
16002 	WARN_ON(dst_rmem->depth != src_rmem->depth);
16003 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
16004 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
16005 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
16006 
16007 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
16008 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
16009 	*dst_rmem->vmem = *src_rmem->vmem;
16010 	for (i = 0; i < dst_rmem->nr_pages; i++) {
16011 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
16012 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
16013 	}
16014 
16015 	dst->rx_agg_bmap = src->rx_agg_bmap;
16016 }
16017 
16018 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
16019 {
16020 	struct bnxt *bp = netdev_priv(dev);
16021 	struct bnxt_rx_ring_info *rxr, *clone;
16022 	struct bnxt_cp_ring_info *cpr;
16023 	struct bnxt_vnic_info *vnic;
16024 	struct bnxt_napi *bnapi;
16025 	int i, rc;
16026 	u16 mru;
16027 
16028 	rxr = &bp->rx_ring[idx];
16029 	clone = qmem;
16030 
16031 	rxr->rx_prod = clone->rx_prod;
16032 	rxr->rx_agg_prod = clone->rx_agg_prod;
16033 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16034 	rxr->rx_next_cons = clone->rx_next_cons;
16035 	rxr->rx_tpa = clone->rx_tpa;
16036 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16037 	rxr->page_pool = clone->page_pool;
16038 	rxr->head_pool = clone->head_pool;
16039 	rxr->xdp_rxq = clone->xdp_rxq;
16040 	rxr->need_head_pool = clone->need_head_pool;
16041 
16042 	bnxt_copy_rx_ring(bp, rxr, clone);
16043 
16044 	bnapi = rxr->bnapi;
16045 	cpr = &bnapi->cp_ring;
16046 
16047 	/* All rings have been reserved and previously allocated.
16048 	 * Reallocating with the same parameters should never fail.
16049 	 */
16050 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16051 	if (rc)
16052 		goto err_reset;
16053 
16054 	if (bp->tph_mode) {
16055 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16056 		if (rc)
16057 			goto err_reset;
16058 	}
16059 
16060 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16061 	if (rc)
16062 		goto err_reset;
16063 
16064 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16065 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16066 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16067 
16068 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16069 		rc = bnxt_tx_queue_start(bp, idx);
16070 		if (rc)
16071 			goto err_reset;
16072 	}
16073 
16074 	bnxt_enable_rx_page_pool(rxr);
16075 	napi_enable_locked(&bnapi->napi);
16076 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16077 
16078 	mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
16079 	for (i = 0; i < bp->nr_vnics; i++) {
16080 		vnic = &bp->vnic_info[i];
16081 
16082 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16083 		if (rc)
16084 			return rc;
16085 	}
16086 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16087 
16088 err_reset:
16089 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16090 		   rc);
16091 	napi_enable_locked(&bnapi->napi);
16092 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16093 	bnxt_reset_task(bp, true);
16094 	return rc;
16095 }
16096 
16097 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16098 {
16099 	struct bnxt *bp = netdev_priv(dev);
16100 	struct bnxt_rx_ring_info *rxr;
16101 	struct bnxt_cp_ring_info *cpr;
16102 	struct bnxt_vnic_info *vnic;
16103 	struct bnxt_napi *bnapi;
16104 	int i;
16105 
16106 	for (i = 0; i < bp->nr_vnics; i++) {
16107 		vnic = &bp->vnic_info[i];
16108 
16109 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16110 	}
16111 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16112 	/* Make sure NAPI sees that the VNIC is disabled */
16113 	synchronize_net();
16114 	rxr = &bp->rx_ring[idx];
16115 	bnapi = rxr->bnapi;
16116 	cpr = &bnapi->cp_ring;
16117 	cancel_work_sync(&cpr->dim.work);
16118 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16119 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16120 	page_pool_disable_direct_recycling(rxr->page_pool);
16121 	if (bnxt_separate_head_pool(rxr))
16122 		page_pool_disable_direct_recycling(rxr->head_pool);
16123 
16124 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16125 		bnxt_tx_queue_stop(bp, idx);
16126 
16127 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16128 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16129 	 * after seeing the completion.
16130 	 */
16131 	napi_disable_locked(&bnapi->napi);
16132 
16133 	if (bp->tph_mode) {
16134 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16135 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16136 	}
16137 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16138 
16139 	memcpy(qmem, rxr, sizeof(*rxr));
16140 	bnxt_init_rx_ring_struct(bp, qmem);
16141 
16142 	return 0;
16143 }
16144 
16145 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16146 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16147 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16148 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16149 	.ndo_queue_start	= bnxt_queue_start,
16150 	.ndo_queue_stop		= bnxt_queue_stop,
16151 };
16152 
16153 static void bnxt_remove_one(struct pci_dev *pdev)
16154 {
16155 	struct net_device *dev = pci_get_drvdata(pdev);
16156 	struct bnxt *bp = netdev_priv(dev);
16157 
16158 	if (BNXT_PF(bp))
16159 		bnxt_sriov_disable(bp);
16160 
16161 	bnxt_rdma_aux_device_del(bp);
16162 
16163 	unregister_netdev(dev);
16164 	bnxt_ptp_clear(bp);
16165 
16166 	bnxt_rdma_aux_device_uninit(bp);
16167 
16168 	bnxt_free_l2_filters(bp, true);
16169 	bnxt_free_ntp_fltrs(bp, true);
16170 	WARN_ON(bp->num_rss_ctx);
16171 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16172 	/* Flush any pending tasks */
16173 	cancel_work_sync(&bp->sp_task);
16174 	cancel_delayed_work_sync(&bp->fw_reset_task);
16175 	bp->sp_event = 0;
16176 
16177 	bnxt_dl_fw_reporters_destroy(bp);
16178 	bnxt_dl_unregister(bp);
16179 	bnxt_shutdown_tc(bp);
16180 
16181 	bnxt_clear_int_mode(bp);
16182 	bnxt_hwrm_func_drv_unrgtr(bp);
16183 	bnxt_free_hwrm_resources(bp);
16184 	bnxt_hwmon_uninit(bp);
16185 	bnxt_ethtool_free(bp);
16186 	bnxt_dcb_free(bp);
16187 	kfree(bp->ptp_cfg);
16188 	bp->ptp_cfg = NULL;
16189 	kfree(bp->fw_health);
16190 	bp->fw_health = NULL;
16191 	bnxt_cleanup_pci(bp);
16192 	bnxt_free_ctx_mem(bp, true);
16193 	bnxt_free_crash_dump_mem(bp);
16194 	kfree(bp->rss_indir_tbl);
16195 	bp->rss_indir_tbl = NULL;
16196 	bnxt_free_port_stats(bp);
16197 	free_netdev(dev);
16198 }
16199 
16200 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16201 {
16202 	int rc = 0;
16203 	struct bnxt_link_info *link_info = &bp->link_info;
16204 
16205 	bp->phy_flags = 0;
16206 	rc = bnxt_hwrm_phy_qcaps(bp);
16207 	if (rc) {
16208 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16209 			   rc);
16210 		return rc;
16211 	}
16212 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16213 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16214 	else
16215 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16216 
16217 	bp->mac_flags = 0;
16218 	bnxt_hwrm_mac_qcaps(bp);
16219 
16220 	if (!fw_dflt)
16221 		return 0;
16222 
16223 	mutex_lock(&bp->link_lock);
16224 	rc = bnxt_update_link(bp, false);
16225 	if (rc) {
16226 		mutex_unlock(&bp->link_lock);
16227 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16228 			   rc);
16229 		return rc;
16230 	}
16231 
16232 	/* Older firmware does not have supported_auto_speeds, so assume
16233 	 * that all supported speeds can be autonegotiated.
16234 	 */
16235 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16236 		link_info->support_auto_speeds = link_info->support_speeds;
16237 
16238 	bnxt_init_ethtool_link_settings(bp);
16239 	mutex_unlock(&bp->link_lock);
16240 	return 0;
16241 }
16242 
16243 static int bnxt_get_max_irq(struct pci_dev *pdev)
16244 {
16245 	u16 ctrl;
16246 
16247 	if (!pdev->msix_cap)
16248 		return 1;
16249 
16250 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16251 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16252 }
16253 
16254 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16255 				int *max_cp)
16256 {
16257 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16258 	int max_ring_grps = 0, max_irq;
16259 
16260 	*max_tx = hw_resc->max_tx_rings;
16261 	*max_rx = hw_resc->max_rx_rings;
16262 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16263 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16264 			bnxt_get_ulp_msix_num_in_use(bp),
16265 			hw_resc->max_stat_ctxs -
16266 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16267 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16268 		*max_cp = min_t(int, *max_cp, max_irq);
16269 	max_ring_grps = hw_resc->max_hw_ring_grps;
16270 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16271 		*max_cp -= 1;
16272 		*max_rx -= 2;
16273 	}
16274 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16275 		*max_rx >>= 1;
16276 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16277 		int rc;
16278 
16279 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16280 		if (rc) {
16281 			*max_rx = 0;
16282 			*max_tx = 0;
16283 		}
16284 		/* On P5 chips, max_cp output param should be available NQs */
16285 		*max_cp = max_irq;
16286 	}
16287 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16288 }
16289 
16290 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16291 {
16292 	int rx, tx, cp;
16293 
16294 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16295 	*max_rx = rx;
16296 	*max_tx = tx;
16297 	if (!rx || !tx || !cp)
16298 		return -ENOMEM;
16299 
16300 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16301 }
16302 
16303 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16304 			       bool shared)
16305 {
16306 	int rc;
16307 
16308 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16309 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16310 		/* Not enough rings, try disabling agg rings. */
16311 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16312 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16313 		if (rc) {
16314 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16315 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16316 			return rc;
16317 		}
16318 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16319 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16320 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16321 		bnxt_set_ring_params(bp);
16322 	}
16323 
16324 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16325 		int max_cp, max_stat, max_irq;
16326 
16327 		/* Reserve minimum resources for RoCE */
16328 		max_cp = bnxt_get_max_func_cp_rings(bp);
16329 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16330 		max_irq = bnxt_get_max_func_irqs(bp);
16331 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16332 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16333 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16334 			return 0;
16335 
16336 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16337 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16338 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16339 		max_cp = min_t(int, max_cp, max_irq);
16340 		max_cp = min_t(int, max_cp, max_stat);
16341 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16342 		if (rc)
16343 			rc = 0;
16344 	}
16345 	return rc;
16346 }
16347 
16348 /* In initial default shared ring setting, each shared ring must have a
16349  * RX/TX ring pair.
16350  */
16351 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16352 {
16353 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16354 	bp->rx_nr_rings = bp->cp_nr_rings;
16355 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16356 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16357 }
16358 
16359 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16360 {
16361 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16362 	int avail_msix;
16363 
16364 	if (!bnxt_can_reserve_rings(bp))
16365 		return 0;
16366 
16367 	if (sh)
16368 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16369 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16370 	/* Reduce default rings on multi-port cards so that total default
16371 	 * rings do not exceed CPU count.
16372 	 */
16373 	if (bp->port_count > 1) {
16374 		int max_rings =
16375 			max_t(int, num_online_cpus() / bp->port_count, 1);
16376 
16377 		dflt_rings = min_t(int, dflt_rings, max_rings);
16378 	}
16379 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16380 	if (rc)
16381 		return rc;
16382 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16383 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16384 	if (sh)
16385 		bnxt_trim_dflt_sh_rings(bp);
16386 	else
16387 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16388 	bp->tx_nr_rings = bnxt_tx_nr_rings(bp);
16389 
16390 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16391 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16392 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16393 
16394 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16395 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16396 	}
16397 
16398 	rc = __bnxt_reserve_rings(bp);
16399 	if (rc && rc != -ENODEV)
16400 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16401 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
16402 	if (sh)
16403 		bnxt_trim_dflt_sh_rings(bp);
16404 
16405 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16406 	if (bnxt_need_reserve_rings(bp)) {
16407 		rc = __bnxt_reserve_rings(bp);
16408 		if (rc && rc != -ENODEV)
16409 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16410 		bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
16411 	}
16412 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16413 		bp->rx_nr_rings++;
16414 		bp->cp_nr_rings++;
16415 	}
16416 	if (rc) {
16417 		bp->tx_nr_rings = 0;
16418 		bp->rx_nr_rings = 0;
16419 	}
16420 	return rc;
16421 }
16422 
16423 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16424 {
16425 	int rc;
16426 
16427 	if (bp->tx_nr_rings)
16428 		return 0;
16429 
16430 	bnxt_ulp_irq_stop(bp);
16431 	bnxt_clear_int_mode(bp);
16432 	rc = bnxt_set_dflt_rings(bp, true);
16433 	if (rc) {
16434 		if (BNXT_VF(bp) && rc == -ENODEV)
16435 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16436 		else
16437 			netdev_err(bp->dev, "Not enough rings available.\n");
16438 		goto init_dflt_ring_err;
16439 	}
16440 	rc = bnxt_init_int_mode(bp);
16441 	if (rc)
16442 		goto init_dflt_ring_err;
16443 
16444 	bp->tx_nr_rings_per_tc = bnxt_tx_nr_rings_per_tc(bp);
16445 
16446 	bnxt_set_dflt_rfs(bp);
16447 
16448 init_dflt_ring_err:
16449 	bnxt_ulp_irq_restart(bp, rc);
16450 	return rc;
16451 }
16452 
16453 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16454 {
16455 	int rc;
16456 
16457 	netdev_ops_assert_locked(bp->dev);
16458 	bnxt_hwrm_func_qcaps(bp);
16459 
16460 	if (netif_running(bp->dev))
16461 		__bnxt_close_nic(bp, true, false);
16462 
16463 	bnxt_ulp_irq_stop(bp);
16464 	bnxt_clear_int_mode(bp);
16465 	rc = bnxt_init_int_mode(bp);
16466 	bnxt_ulp_irq_restart(bp, rc);
16467 
16468 	if (netif_running(bp->dev)) {
16469 		if (rc)
16470 			netif_close(bp->dev);
16471 		else
16472 			rc = bnxt_open_nic(bp, true, false);
16473 	}
16474 
16475 	return rc;
16476 }
16477 
16478 static int bnxt_init_mac_addr(struct bnxt *bp)
16479 {
16480 	int rc = 0;
16481 
16482 	if (BNXT_PF(bp)) {
16483 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16484 	} else {
16485 #ifdef CONFIG_BNXT_SRIOV
16486 		struct bnxt_vf_info *vf = &bp->vf;
16487 		bool strict_approval = true;
16488 
16489 		if (is_valid_ether_addr(vf->mac_addr)) {
16490 			/* overwrite netdev dev_addr with admin VF MAC */
16491 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16492 			/* Older PF driver or firmware may not approve this
16493 			 * correctly.
16494 			 */
16495 			strict_approval = false;
16496 		} else {
16497 			eth_hw_addr_random(bp->dev);
16498 		}
16499 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16500 #endif
16501 	}
16502 	return rc;
16503 }
16504 
16505 static void bnxt_vpd_read_info(struct bnxt *bp)
16506 {
16507 	struct pci_dev *pdev = bp->pdev;
16508 	unsigned int vpd_size, kw_len;
16509 	int pos, size;
16510 	u8 *vpd_data;
16511 
16512 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16513 	if (IS_ERR(vpd_data)) {
16514 		pci_warn(pdev, "Unable to read VPD\n");
16515 		return;
16516 	}
16517 
16518 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16519 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16520 	if (pos < 0)
16521 		goto read_sn;
16522 
16523 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16524 	memcpy(bp->board_partno, &vpd_data[pos], size);
16525 
16526 read_sn:
16527 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16528 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16529 					   &kw_len);
16530 	if (pos < 0)
16531 		goto exit;
16532 
16533 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16534 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16535 exit:
16536 	kfree(vpd_data);
16537 }
16538 
16539 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16540 {
16541 	struct pci_dev *pdev = bp->pdev;
16542 	u64 qword;
16543 
16544 	qword = pci_get_dsn(pdev);
16545 	if (!qword) {
16546 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16547 		return -EOPNOTSUPP;
16548 	}
16549 
16550 	put_unaligned_le64(qword, dsn);
16551 
16552 	bp->flags |= BNXT_FLAG_DSN_VALID;
16553 	return 0;
16554 }
16555 
16556 static int bnxt_map_db_bar(struct bnxt *bp)
16557 {
16558 	if (!bp->db_size)
16559 		return -ENODEV;
16560 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16561 	if (!bp->bar1)
16562 		return -ENOMEM;
16563 	return 0;
16564 }
16565 
16566 void bnxt_print_device_info(struct bnxt *bp)
16567 {
16568 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16569 		    board_info[bp->board_idx].name,
16570 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16571 
16572 	pcie_print_link_status(bp->pdev);
16573 }
16574 
16575 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16576 {
16577 	struct bnxt_hw_resc *hw_resc;
16578 	struct net_device *dev;
16579 	struct bnxt *bp;
16580 	int rc, max_irqs;
16581 
16582 	if (pci_is_bridge(pdev))
16583 		return -ENODEV;
16584 
16585 	if (!pdev->msix_cap) {
16586 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16587 		return -ENODEV;
16588 	}
16589 
16590 	/* Clear any pending DMA transactions from crash kernel
16591 	 * while loading driver in capture kernel.
16592 	 */
16593 	if (is_kdump_kernel()) {
16594 		pci_clear_master(pdev);
16595 		pcie_flr(pdev);
16596 	}
16597 
16598 	max_irqs = bnxt_get_max_irq(pdev);
16599 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16600 				 max_irqs);
16601 	if (!dev)
16602 		return -ENOMEM;
16603 
16604 	bp = netdev_priv(dev);
16605 	bp->board_idx = ent->driver_data;
16606 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16607 	bnxt_set_max_func_irqs(bp, max_irqs);
16608 
16609 	if (bnxt_vf_pciid(bp->board_idx))
16610 		bp->flags |= BNXT_FLAG_VF;
16611 
16612 	/* No devlink port registration in case of a VF */
16613 	if (BNXT_PF(bp))
16614 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16615 
16616 	rc = bnxt_init_board(pdev, dev);
16617 	if (rc < 0)
16618 		goto init_err_free;
16619 
16620 	dev->netdev_ops = &bnxt_netdev_ops;
16621 	dev->stat_ops = &bnxt_stat_ops;
16622 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16623 	dev->ethtool_ops = &bnxt_ethtool_ops;
16624 	pci_set_drvdata(pdev, dev);
16625 
16626 	rc = bnxt_alloc_hwrm_resources(bp);
16627 	if (rc)
16628 		goto init_err_pci_clean;
16629 
16630 	mutex_init(&bp->hwrm_cmd_lock);
16631 	mutex_init(&bp->link_lock);
16632 
16633 	rc = bnxt_fw_init_one_p1(bp);
16634 	if (rc)
16635 		goto init_err_pci_clean;
16636 
16637 	if (BNXT_PF(bp))
16638 		bnxt_vpd_read_info(bp);
16639 
16640 	if (BNXT_CHIP_P5_PLUS(bp)) {
16641 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16642 		if (BNXT_CHIP_P7(bp))
16643 			bp->flags |= BNXT_FLAG_CHIP_P7;
16644 	}
16645 
16646 	rc = bnxt_alloc_rss_indir_tbl(bp);
16647 	if (rc)
16648 		goto init_err_pci_clean;
16649 
16650 	rc = bnxt_fw_init_one_p2(bp);
16651 	if (rc)
16652 		goto init_err_pci_clean;
16653 
16654 	rc = bnxt_map_db_bar(bp);
16655 	if (rc) {
16656 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16657 			rc);
16658 		goto init_err_pci_clean;
16659 	}
16660 
16661 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16662 			   NETIF_F_TSO | NETIF_F_TSO6 |
16663 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16664 			   NETIF_F_GSO_IPXIP4 |
16665 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16666 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16667 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16668 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16669 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16670 
16671 	if (BNXT_SUPPORTS_TPA(bp))
16672 		dev->hw_features |= NETIF_F_LRO;
16673 
16674 	dev->hw_enc_features =
16675 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16676 			NETIF_F_TSO | NETIF_F_TSO6 |
16677 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16678 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16679 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16680 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16681 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16682 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16683 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16684 	else
16685 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16686 
16687 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16688 				    NETIF_F_GSO_GRE_CSUM;
16689 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16690 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16691 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16692 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16693 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16694 	if (BNXT_SUPPORTS_TPA(bp))
16695 		dev->hw_features |= NETIF_F_GRO_HW;
16696 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16697 	if (dev->features & NETIF_F_GRO_HW)
16698 		dev->features &= ~NETIF_F_LRO;
16699 	dev->priv_flags |= IFF_UNICAST_FLT;
16700 
16701 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16702 	if (bp->tso_max_segs)
16703 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16704 
16705 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16706 			    NETDEV_XDP_ACT_RX_SG;
16707 
16708 #ifdef CONFIG_BNXT_SRIOV
16709 	init_waitqueue_head(&bp->sriov_cfg_wait);
16710 #endif
16711 	if (BNXT_SUPPORTS_TPA(bp)) {
16712 		bp->gro_func = bnxt_gro_func_5730x;
16713 		if (BNXT_CHIP_P4(bp))
16714 			bp->gro_func = bnxt_gro_func_5731x;
16715 		else if (BNXT_CHIP_P5_PLUS(bp))
16716 			bp->gro_func = bnxt_gro_func_5750x;
16717 	}
16718 	if (!BNXT_CHIP_P4_PLUS(bp))
16719 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16720 
16721 	rc = bnxt_init_mac_addr(bp);
16722 	if (rc) {
16723 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16724 		rc = -EADDRNOTAVAIL;
16725 		goto init_err_pci_clean;
16726 	}
16727 
16728 	if (BNXT_PF(bp)) {
16729 		/* Read the adapter's DSN to use as the eswitch switch_id */
16730 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16731 	}
16732 
16733 	/* MTU range: 60 - FW defined max */
16734 	dev->min_mtu = ETH_ZLEN;
16735 	dev->max_mtu = bp->max_mtu;
16736 
16737 	rc = bnxt_probe_phy(bp, true);
16738 	if (rc)
16739 		goto init_err_pci_clean;
16740 
16741 	hw_resc = &bp->hw_resc;
16742 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16743 		       BNXT_L2_FLTR_MAX_FLTR;
16744 	/* Older firmware may not report these filters properly */
16745 	if (bp->max_fltr < BNXT_MAX_FLTR)
16746 		bp->max_fltr = BNXT_MAX_FLTR;
16747 	bnxt_init_l2_fltr_tbl(bp);
16748 	__bnxt_set_rx_skb_mode(bp, false);
16749 	bnxt_set_tpa_flags(bp);
16750 	bnxt_init_ring_params(bp);
16751 	bnxt_set_ring_params(bp);
16752 	bnxt_rdma_aux_device_init(bp);
16753 	rc = bnxt_set_dflt_rings(bp, true);
16754 	if (rc) {
16755 		if (BNXT_VF(bp) && rc == -ENODEV) {
16756 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16757 		} else {
16758 			netdev_err(bp->dev, "Not enough rings available.\n");
16759 			rc = -ENOMEM;
16760 		}
16761 		goto init_err_pci_clean;
16762 	}
16763 
16764 	bnxt_fw_init_one_p3(bp);
16765 
16766 	bnxt_init_dflt_coal(bp);
16767 
16768 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16769 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16770 
16771 	rc = bnxt_init_int_mode(bp);
16772 	if (rc)
16773 		goto init_err_pci_clean;
16774 
16775 	/* No TC has been set yet and rings may have been trimmed due to
16776 	 * limited MSIX, so we re-initialize the TX rings per TC.
16777 	 */
16778 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16779 
16780 	if (BNXT_PF(bp)) {
16781 		if (!bnxt_pf_wq) {
16782 			bnxt_pf_wq =
16783 				create_singlethread_workqueue("bnxt_pf_wq");
16784 			if (!bnxt_pf_wq) {
16785 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16786 				rc = -ENOMEM;
16787 				goto init_err_pci_clean;
16788 			}
16789 		}
16790 		rc = bnxt_init_tc(bp);
16791 		if (rc)
16792 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16793 				   rc);
16794 	}
16795 
16796 	bnxt_inv_fw_health_reg(bp);
16797 	rc = bnxt_dl_register(bp);
16798 	if (rc)
16799 		goto init_err_dl;
16800 
16801 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16802 
16803 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16804 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16805 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16806 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16807 	dev->request_ops_lock = true;
16808 	dev->netmem_tx = true;
16809 
16810 	rc = register_netdev(dev);
16811 	if (rc)
16812 		goto init_err_cleanup;
16813 
16814 	bnxt_dl_fw_reporters_create(bp);
16815 
16816 	bnxt_rdma_aux_device_add(bp);
16817 
16818 	bnxt_print_device_info(bp);
16819 
16820 	pci_save_state(pdev);
16821 
16822 	return 0;
16823 init_err_cleanup:
16824 	bnxt_rdma_aux_device_uninit(bp);
16825 	bnxt_dl_unregister(bp);
16826 init_err_dl:
16827 	bnxt_shutdown_tc(bp);
16828 	bnxt_clear_int_mode(bp);
16829 
16830 init_err_pci_clean:
16831 	bnxt_hwrm_func_drv_unrgtr(bp);
16832 	bnxt_free_hwrm_resources(bp);
16833 	bnxt_hwmon_uninit(bp);
16834 	bnxt_ethtool_free(bp);
16835 	bnxt_ptp_clear(bp);
16836 	kfree(bp->ptp_cfg);
16837 	bp->ptp_cfg = NULL;
16838 	kfree(bp->fw_health);
16839 	bp->fw_health = NULL;
16840 	bnxt_cleanup_pci(bp);
16841 	bnxt_free_ctx_mem(bp, true);
16842 	bnxt_free_crash_dump_mem(bp);
16843 	kfree(bp->rss_indir_tbl);
16844 	bp->rss_indir_tbl = NULL;
16845 
16846 init_err_free:
16847 	free_netdev(dev);
16848 	return rc;
16849 }
16850 
16851 static void bnxt_shutdown(struct pci_dev *pdev)
16852 {
16853 	struct net_device *dev = pci_get_drvdata(pdev);
16854 	struct bnxt *bp;
16855 
16856 	if (!dev)
16857 		return;
16858 
16859 	rtnl_lock();
16860 	netdev_lock(dev);
16861 	bp = netdev_priv(dev);
16862 	if (!bp)
16863 		goto shutdown_exit;
16864 
16865 	if (netif_running(dev))
16866 		netif_close(dev);
16867 
16868 	bnxt_ptp_clear(bp);
16869 	bnxt_clear_int_mode(bp);
16870 	pci_disable_device(pdev);
16871 
16872 	if (system_state == SYSTEM_POWER_OFF) {
16873 		pci_wake_from_d3(pdev, bp->wol);
16874 		pci_set_power_state(pdev, PCI_D3hot);
16875 	}
16876 
16877 shutdown_exit:
16878 	netdev_unlock(dev);
16879 	rtnl_unlock();
16880 }
16881 
16882 #ifdef CONFIG_PM_SLEEP
16883 static int bnxt_suspend(struct device *device)
16884 {
16885 	struct net_device *dev = dev_get_drvdata(device);
16886 	struct bnxt *bp = netdev_priv(dev);
16887 	int rc = 0;
16888 
16889 	bnxt_ulp_stop(bp);
16890 
16891 	netdev_lock(dev);
16892 	if (netif_running(dev)) {
16893 		netif_device_detach(dev);
16894 		rc = bnxt_close(dev);
16895 	}
16896 	bnxt_hwrm_func_drv_unrgtr(bp);
16897 	bnxt_ptp_clear(bp);
16898 	pci_disable_device(bp->pdev);
16899 	bnxt_free_ctx_mem(bp, false);
16900 	netdev_unlock(dev);
16901 	return rc;
16902 }
16903 
16904 static int bnxt_resume(struct device *device)
16905 {
16906 	struct net_device *dev = dev_get_drvdata(device);
16907 	struct bnxt *bp = netdev_priv(dev);
16908 	int rc = 0;
16909 
16910 	netdev_lock(dev);
16911 	rc = pci_enable_device(bp->pdev);
16912 	if (rc) {
16913 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16914 			   rc);
16915 		goto resume_exit;
16916 	}
16917 	pci_set_master(bp->pdev);
16918 	if (bnxt_hwrm_ver_get(bp)) {
16919 		rc = -ENODEV;
16920 		goto resume_exit;
16921 	}
16922 	rc = bnxt_hwrm_func_reset(bp);
16923 	if (rc) {
16924 		rc = -EBUSY;
16925 		goto resume_exit;
16926 	}
16927 
16928 	rc = bnxt_hwrm_func_qcaps(bp);
16929 	if (rc)
16930 		goto resume_exit;
16931 
16932 	bnxt_clear_reservations(bp, true);
16933 
16934 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16935 		rc = -ENODEV;
16936 		goto resume_exit;
16937 	}
16938 	if (bp->fw_crash_mem)
16939 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16940 
16941 	if (bnxt_ptp_init(bp)) {
16942 		kfree(bp->ptp_cfg);
16943 		bp->ptp_cfg = NULL;
16944 	}
16945 	bnxt_get_wol_settings(bp);
16946 	if (netif_running(dev)) {
16947 		rc = bnxt_open(dev);
16948 		if (!rc)
16949 			netif_device_attach(dev);
16950 	}
16951 
16952 resume_exit:
16953 	netdev_unlock(bp->dev);
16954 	bnxt_ulp_start(bp, rc);
16955 	if (!rc)
16956 		bnxt_reenable_sriov(bp);
16957 	return rc;
16958 }
16959 
16960 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16961 #define BNXT_PM_OPS (&bnxt_pm_ops)
16962 
16963 #else
16964 
16965 #define BNXT_PM_OPS NULL
16966 
16967 #endif /* CONFIG_PM_SLEEP */
16968 
16969 /**
16970  * bnxt_io_error_detected - called when PCI error is detected
16971  * @pdev: Pointer to PCI device
16972  * @state: The current pci connection state
16973  *
16974  * This function is called after a PCI bus error affecting
16975  * this device has been detected.
16976  */
16977 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16978 					       pci_channel_state_t state)
16979 {
16980 	struct net_device *netdev = pci_get_drvdata(pdev);
16981 	struct bnxt *bp = netdev_priv(netdev);
16982 	bool abort = false;
16983 
16984 	netdev_info(netdev, "PCI I/O error detected\n");
16985 
16986 	bnxt_ulp_stop(bp);
16987 
16988 	netdev_lock(netdev);
16989 	netif_device_detach(netdev);
16990 
16991 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16992 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16993 		abort = true;
16994 	}
16995 
16996 	if (abort || state == pci_channel_io_perm_failure) {
16997 		netdev_unlock(netdev);
16998 		return PCI_ERS_RESULT_DISCONNECT;
16999 	}
17000 
17001 	/* Link is not reliable anymore if state is pci_channel_io_frozen
17002 	 * so we disable bus master to prevent any potential bad DMAs before
17003 	 * freeing kernel memory.
17004 	 */
17005 	if (state == pci_channel_io_frozen) {
17006 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
17007 		bnxt_fw_fatal_close(bp);
17008 	}
17009 
17010 	if (netif_running(netdev))
17011 		__bnxt_close_nic(bp, true, true);
17012 
17013 	if (pci_is_enabled(pdev))
17014 		pci_disable_device(pdev);
17015 	bnxt_free_ctx_mem(bp, false);
17016 	netdev_unlock(netdev);
17017 
17018 	/* Request a slot reset. */
17019 	return PCI_ERS_RESULT_NEED_RESET;
17020 }
17021 
17022 /**
17023  * bnxt_io_slot_reset - called after the pci bus has been reset.
17024  * @pdev: Pointer to PCI device
17025  *
17026  * Restart the card from scratch, as if from a cold-boot.
17027  * At this point, the card has experienced a hard reset,
17028  * followed by fixups by BIOS, and has its config space
17029  * set up identically to what it was at cold boot.
17030  */
17031 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17032 {
17033 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17034 	struct net_device *netdev = pci_get_drvdata(pdev);
17035 	struct bnxt *bp = netdev_priv(netdev);
17036 	int retry = 0;
17037 	int err = 0;
17038 	int off;
17039 
17040 	netdev_info(bp->dev, "PCI Slot Reset\n");
17041 
17042 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17043 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17044 		msleep(900);
17045 
17046 	netdev_lock(netdev);
17047 
17048 	if (pci_enable_device(pdev)) {
17049 		dev_err(&pdev->dev,
17050 			"Cannot re-enable PCI device after reset.\n");
17051 	} else {
17052 		pci_set_master(pdev);
17053 		/* Upon fatal error, our device internal logic that latches to
17054 		 * BAR value is getting reset and will restore only upon
17055 		 * rewriting the BARs.
17056 		 *
17057 		 * As pci_restore_state() does not re-write the BARs if the
17058 		 * value is same as saved value earlier, driver needs to
17059 		 * write the BARs to 0 to force restore, in case of fatal error.
17060 		 */
17061 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17062 				       &bp->state)) {
17063 			for (off = PCI_BASE_ADDRESS_0;
17064 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17065 				pci_write_config_dword(bp->pdev, off, 0);
17066 		}
17067 		pci_restore_state(pdev);
17068 		pci_save_state(pdev);
17069 
17070 		bnxt_inv_fw_health_reg(bp);
17071 		bnxt_try_map_fw_health_reg(bp);
17072 
17073 		/* In some PCIe AER scenarios, firmware may take up to
17074 		 * 10 seconds to become ready in the worst case.
17075 		 */
17076 		do {
17077 			err = bnxt_try_recover_fw(bp);
17078 			if (!err)
17079 				break;
17080 			retry++;
17081 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17082 
17083 		if (err) {
17084 			dev_err(&pdev->dev, "Firmware not ready\n");
17085 			goto reset_exit;
17086 		}
17087 
17088 		err = bnxt_hwrm_func_reset(bp);
17089 		if (!err)
17090 			result = PCI_ERS_RESULT_RECOVERED;
17091 
17092 		/* IRQ will be initialized later in bnxt_io_resume */
17093 		bnxt_ulp_irq_stop(bp);
17094 		bnxt_clear_int_mode(bp);
17095 	}
17096 
17097 reset_exit:
17098 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17099 	bnxt_clear_reservations(bp, true);
17100 	netdev_unlock(netdev);
17101 
17102 	return result;
17103 }
17104 
17105 /**
17106  * bnxt_io_resume - called when traffic can start flowing again.
17107  * @pdev: Pointer to PCI device
17108  *
17109  * This callback is called when the error recovery driver tells
17110  * us that its OK to resume normal operation.
17111  */
17112 static void bnxt_io_resume(struct pci_dev *pdev)
17113 {
17114 	struct net_device *netdev = pci_get_drvdata(pdev);
17115 	struct bnxt *bp = netdev_priv(netdev);
17116 	int err;
17117 
17118 	netdev_info(bp->dev, "PCI Slot Resume\n");
17119 	netdev_lock(netdev);
17120 
17121 	err = bnxt_hwrm_func_qcaps(bp);
17122 	if (!err) {
17123 		if (netif_running(netdev)) {
17124 			err = bnxt_open(netdev);
17125 		} else {
17126 			err = bnxt_reserve_rings(bp, true);
17127 			if (!err)
17128 				err = bnxt_init_int_mode(bp);
17129 		}
17130 	}
17131 
17132 	if (!err)
17133 		netif_device_attach(netdev);
17134 
17135 	netdev_unlock(netdev);
17136 	bnxt_ulp_start(bp, err);
17137 	if (!err)
17138 		bnxt_reenable_sriov(bp);
17139 }
17140 
17141 static const struct pci_error_handlers bnxt_err_handler = {
17142 	.error_detected	= bnxt_io_error_detected,
17143 	.slot_reset	= bnxt_io_slot_reset,
17144 	.resume		= bnxt_io_resume
17145 };
17146 
17147 static struct pci_driver bnxt_pci_driver = {
17148 	.name		= DRV_MODULE_NAME,
17149 	.id_table	= bnxt_pci_tbl,
17150 	.probe		= bnxt_init_one,
17151 	.remove		= bnxt_remove_one,
17152 	.shutdown	= bnxt_shutdown,
17153 	.driver.pm	= BNXT_PM_OPS,
17154 	.err_handler	= &bnxt_err_handler,
17155 #if defined(CONFIG_BNXT_SRIOV)
17156 	.sriov_configure = bnxt_sriov_configure,
17157 #endif
17158 };
17159 
17160 static int __init bnxt_init(void)
17161 {
17162 	int err;
17163 
17164 	bnxt_debug_init();
17165 	err = pci_register_driver(&bnxt_pci_driver);
17166 	if (err) {
17167 		bnxt_debug_exit();
17168 		return err;
17169 	}
17170 
17171 	return 0;
17172 }
17173 
17174 static void __exit bnxt_exit(void)
17175 {
17176 	pci_unregister_driver(&bnxt_pci_driver);
17177 	if (bnxt_pf_wq)
17178 		destroy_workqueue(bnxt_pf_wq);
17179 	bnxt_debug_exit();
17180 }
17181 
17182 module_init(bnxt_init);
17183 module_exit(bnxt_exit);
17184