xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 300a0cfe9f375b2843bcb331bcfa7503475ef5dd)
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 };
146 
147 static const struct pci_device_id bnxt_pci_tbl[] = {
148 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
150 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
156 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
168 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
179 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
180 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
181 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
182 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
188 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
189 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
190 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
194 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
195 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
196 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
197 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
198 #ifdef CONFIG_BNXT_SRIOV
199 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
203 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
207 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
208 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
209 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
218 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
219 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
220 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
221 #endif
222 	{ 0 }
223 };
224 
225 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
226 
227 static const u16 bnxt_vf_req_snif[] = {
228 	HWRM_FUNC_CFG,
229 	HWRM_FUNC_VF_CFG,
230 	HWRM_PORT_PHY_QCFG,
231 	HWRM_CFA_L2_FILTER_ALLOC,
232 };
233 
234 static const u16 bnxt_async_events_arr[] = {
235 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
238 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
239 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
240 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
241 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
244 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
245 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
246 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
247 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
248 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
249 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
250 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
251 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
252 };
253 
254 const u16 bnxt_bstore_to_trace[] = {
255 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
256 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
257 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
258 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
259 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
260 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
261 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
262 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
263 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
264 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
265 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
266 };
267 
268 static struct workqueue_struct *bnxt_pf_wq;
269 
270 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
271 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
272 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
273 
274 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
275 	.ports = {
276 		.src = 0,
277 		.dst = 0,
278 	},
279 	.addrs = {
280 		.v6addrs = {
281 			.src = BNXT_IPV6_MASK_NONE,
282 			.dst = BNXT_IPV6_MASK_NONE,
283 		},
284 	},
285 };
286 
287 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
288 	.ports = {
289 		.src = cpu_to_be16(0xffff),
290 		.dst = cpu_to_be16(0xffff),
291 	},
292 	.addrs = {
293 		.v6addrs = {
294 			.src = BNXT_IPV6_MASK_ALL,
295 			.dst = BNXT_IPV6_MASK_ALL,
296 		},
297 	},
298 };
299 
300 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
301 	.ports = {
302 		.src = cpu_to_be16(0xffff),
303 		.dst = cpu_to_be16(0xffff),
304 	},
305 	.addrs = {
306 		.v4addrs = {
307 			.src = cpu_to_be32(0xffffffff),
308 			.dst = cpu_to_be32(0xffffffff),
309 		},
310 	},
311 };
312 
313 static bool bnxt_vf_pciid(enum board_idx idx)
314 {
315 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
316 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
317 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
318 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
319 }
320 
321 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
322 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
323 
324 #define BNXT_DB_CQ(db, idx)						\
325 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
326 
327 #define BNXT_DB_NQ_P5(db, idx)						\
328 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
329 		    (db)->doorbell)
330 
331 #define BNXT_DB_NQ_P7(db, idx)						\
332 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
333 		    DB_RING_IDX(db, idx), (db)->doorbell)
334 
335 #define BNXT_DB_CQ_ARM(db, idx)						\
336 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
337 
338 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
339 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
340 		    DB_RING_IDX(db, idx), (db)->doorbell)
341 
342 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
343 {
344 	if (bp->flags & BNXT_FLAG_CHIP_P7)
345 		BNXT_DB_NQ_P7(db, idx);
346 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
347 		BNXT_DB_NQ_P5(db, idx);
348 	else
349 		BNXT_DB_CQ(db, idx);
350 }
351 
352 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
353 {
354 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
355 		BNXT_DB_NQ_ARM_P5(db, idx);
356 	else
357 		BNXT_DB_CQ_ARM(db, idx);
358 }
359 
360 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
361 {
362 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
363 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
364 			    DB_RING_IDX(db, idx), db->doorbell);
365 	else
366 		BNXT_DB_CQ(db, idx);
367 }
368 
369 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
370 {
371 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
372 		return;
373 
374 	if (BNXT_PF(bp))
375 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
376 	else
377 		schedule_delayed_work(&bp->fw_reset_task, delay);
378 }
379 
380 static void __bnxt_queue_sp_work(struct bnxt *bp)
381 {
382 	if (BNXT_PF(bp))
383 		queue_work(bnxt_pf_wq, &bp->sp_task);
384 	else
385 		schedule_work(&bp->sp_task);
386 }
387 
388 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
389 {
390 	set_bit(event, &bp->sp_event);
391 	__bnxt_queue_sp_work(bp);
392 }
393 
394 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
395 {
396 	if (!rxr->bnapi->in_reset) {
397 		rxr->bnapi->in_reset = true;
398 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
399 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
400 		else
401 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
402 		__bnxt_queue_sp_work(bp);
403 	}
404 	rxr->rx_next_cons = 0xffff;
405 }
406 
407 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
408 			  u16 curr)
409 {
410 	struct bnxt_napi *bnapi = txr->bnapi;
411 
412 	if (bnapi->tx_fault)
413 		return;
414 
415 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
416 		   txr->txq_index, txr->tx_hw_cons,
417 		   txr->tx_cons, txr->tx_prod, curr);
418 	WARN_ON_ONCE(1);
419 	bnapi->tx_fault = 1;
420 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
421 }
422 
423 const u16 bnxt_lhint_arr[] = {
424 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
425 	TX_BD_FLAGS_LHINT_512_TO_1023,
426 	TX_BD_FLAGS_LHINT_1024_TO_2047,
427 	TX_BD_FLAGS_LHINT_1024_TO_2047,
428 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
429 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
430 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
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 };
444 
445 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
446 {
447 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
448 
449 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
450 		return 0;
451 
452 	return md_dst->u.port_info.port_id;
453 }
454 
455 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
456 			     u16 prod)
457 {
458 	/* Sync BD data before updating doorbell */
459 	wmb();
460 	bnxt_db_write(bp, &txr->tx_db, prod);
461 	txr->kick_pending = 0;
462 }
463 
464 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
465 {
466 	struct bnxt *bp = netdev_priv(dev);
467 	struct tx_bd *txbd, *txbd0;
468 	struct tx_bd_ext *txbd1;
469 	struct netdev_queue *txq;
470 	int i;
471 	dma_addr_t mapping;
472 	unsigned int length, pad = 0;
473 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
474 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
475 	struct pci_dev *pdev = bp->pdev;
476 	u16 prod, last_frag, txts_prod;
477 	struct bnxt_tx_ring_info *txr;
478 	struct bnxt_sw_tx_bd *tx_buf;
479 	__le32 lflags = 0;
480 	skb_frag_t *frag;
481 
482 	i = skb_get_queue_mapping(skb);
483 	if (unlikely(i >= bp->tx_nr_rings)) {
484 		dev_kfree_skb_any(skb);
485 		dev_core_stats_tx_dropped_inc(dev);
486 		return NETDEV_TX_OK;
487 	}
488 
489 	txq = netdev_get_tx_queue(dev, i);
490 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
491 	prod = txr->tx_prod;
492 
493 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
494 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
495 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
496 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
497 		if (skb_linearize(skb)) {
498 			dev_kfree_skb_any(skb);
499 			dev_core_stats_tx_dropped_inc(dev);
500 			return NETDEV_TX_OK;
501 		}
502 	}
503 #endif
504 	free_size = bnxt_tx_avail(bp, txr);
505 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
506 		/* We must have raced with NAPI cleanup */
507 		if (net_ratelimit() && txr->kick_pending)
508 			netif_warn(bp, tx_err, dev,
509 				   "bnxt: ring busy w/ flush pending!\n");
510 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
511 					bp->tx_wake_thresh))
512 			return NETDEV_TX_BUSY;
513 	}
514 
515 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
516 		goto tx_free;
517 
518 	length = skb->len;
519 	len = skb_headlen(skb);
520 	last_frag = skb_shinfo(skb)->nr_frags;
521 
522 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
523 
524 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
525 	tx_buf->skb = skb;
526 	tx_buf->nr_frags = last_frag;
527 
528 	vlan_tag_flags = 0;
529 	cfa_action = bnxt_xmit_get_cfa_action(skb);
530 	if (skb_vlan_tag_present(skb)) {
531 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
532 				 skb_vlan_tag_get(skb);
533 		/* Currently supports 8021Q, 8021AD vlan offloads
534 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
535 		 */
536 		if (skb->vlan_proto == htons(ETH_P_8021Q))
537 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
538 	}
539 
540 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
541 	    ptp->tx_tstamp_en) {
542 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
543 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
544 			tx_buf->is_ts_pkt = 1;
545 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
546 		} else if (!skb_is_gso(skb)) {
547 			u16 seq_id, hdr_off;
548 
549 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
550 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
551 				if (vlan_tag_flags)
552 					hdr_off += VLAN_HLEN;
553 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
554 				tx_buf->is_ts_pkt = 1;
555 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
556 
557 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
558 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
559 				tx_buf->txts_prod = txts_prod;
560 			}
561 		}
562 	}
563 	if (unlikely(skb->no_fcs))
564 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
565 
566 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
567 	    skb_frags_readable(skb) && !lflags) {
568 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
569 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
570 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
571 		void __iomem *db = txr->tx_db.doorbell;
572 		void *pdata = tx_push_buf->data;
573 		u64 *end;
574 		int j, push_len;
575 
576 		/* Set COAL_NOW to be ready quickly for the next push */
577 		tx_push->tx_bd_len_flags_type =
578 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
579 					TX_BD_TYPE_LONG_TX_BD |
580 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
581 					TX_BD_FLAGS_COAL_NOW |
582 					TX_BD_FLAGS_PACKET_END |
583 					TX_BD_CNT(2));
584 
585 		if (skb->ip_summed == CHECKSUM_PARTIAL)
586 			tx_push1->tx_bd_hsize_lflags =
587 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
588 		else
589 			tx_push1->tx_bd_hsize_lflags = 0;
590 
591 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
592 		tx_push1->tx_bd_cfa_action =
593 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
594 
595 		end = pdata + length;
596 		end = PTR_ALIGN(end, 8) - 1;
597 		*end = 0;
598 
599 		skb_copy_from_linear_data(skb, pdata, len);
600 		pdata += len;
601 		for (j = 0; j < last_frag; j++) {
602 			void *fptr;
603 
604 			frag = &skb_shinfo(skb)->frags[j];
605 			fptr = skb_frag_address_safe(frag);
606 			if (!fptr)
607 				goto normal_tx;
608 
609 			memcpy(pdata, fptr, skb_frag_size(frag));
610 			pdata += skb_frag_size(frag);
611 		}
612 
613 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
614 		txbd->tx_bd_haddr = txr->data_mapping;
615 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
616 		prod = NEXT_TX(prod);
617 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
618 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
619 		memcpy(txbd, tx_push1, sizeof(*txbd));
620 		prod = NEXT_TX(prod);
621 		tx_push->doorbell =
622 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
623 				    DB_RING_IDX(&txr->tx_db, prod));
624 		WRITE_ONCE(txr->tx_prod, prod);
625 
626 		tx_buf->is_push = 1;
627 		netdev_tx_sent_queue(txq, skb->len);
628 		wmb();	/* Sync is_push and byte queue before pushing data */
629 
630 		push_len = (length + sizeof(*tx_push) + 7) / 8;
631 		if (push_len > 16) {
632 			__iowrite64_copy(db, tx_push_buf, 16);
633 			__iowrite32_copy(db + 4, tx_push_buf + 1,
634 					 (push_len - 16) << 1);
635 		} else {
636 			__iowrite64_copy(db, tx_push_buf, push_len);
637 		}
638 
639 		goto tx_done;
640 	}
641 
642 normal_tx:
643 	if (length < BNXT_MIN_PKT_SIZE) {
644 		pad = BNXT_MIN_PKT_SIZE - length;
645 		if (skb_pad(skb, pad))
646 			/* SKB already freed. */
647 			goto tx_kick_pending;
648 		length = BNXT_MIN_PKT_SIZE;
649 	}
650 
651 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
652 
653 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
654 		goto tx_free;
655 
656 	dma_unmap_addr_set(tx_buf, mapping, mapping);
657 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
658 		TX_BD_CNT(last_frag + 2);
659 
660 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
661 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
662 
663 	prod = NEXT_TX(prod);
664 	txbd1 = (struct tx_bd_ext *)
665 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
666 
667 	txbd1->tx_bd_hsize_lflags = lflags;
668 	if (skb_is_gso(skb)) {
669 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
670 		u32 hdr_len;
671 
672 		if (skb->encapsulation) {
673 			if (udp_gso)
674 				hdr_len = skb_inner_transport_offset(skb) +
675 					  sizeof(struct udphdr);
676 			else
677 				hdr_len = skb_inner_tcp_all_headers(skb);
678 		} else if (udp_gso) {
679 			hdr_len = skb_transport_offset(skb) +
680 				  sizeof(struct udphdr);
681 		} else {
682 			hdr_len = skb_tcp_all_headers(skb);
683 		}
684 
685 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
686 					TX_BD_FLAGS_T_IPID |
687 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
688 		length = skb_shinfo(skb)->gso_size;
689 		txbd1->tx_bd_mss = cpu_to_le32(length);
690 		length += hdr_len;
691 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
692 		txbd1->tx_bd_hsize_lflags |=
693 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
694 		txbd1->tx_bd_mss = 0;
695 	}
696 
697 	length >>= 9;
698 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
699 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
700 				     skb->len);
701 		i = 0;
702 		goto tx_dma_error;
703 	}
704 	flags |= bnxt_lhint_arr[length];
705 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
706 
707 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
708 	txbd1->tx_bd_cfa_action =
709 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
710 	txbd0 = txbd;
711 	for (i = 0; i < last_frag; i++) {
712 		frag = &skb_shinfo(skb)->frags[i];
713 		prod = NEXT_TX(prod);
714 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
715 
716 		len = skb_frag_size(frag);
717 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
718 					   DMA_TO_DEVICE);
719 
720 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
721 			goto tx_dma_error;
722 
723 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
724 		netmem_dma_unmap_addr_set(skb_frag_netmem(frag), tx_buf,
725 					  mapping, mapping);
726 
727 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
728 
729 		flags = len << TX_BD_LEN_SHIFT;
730 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
731 	}
732 
733 	flags &= ~TX_BD_LEN;
734 	txbd->tx_bd_len_flags_type =
735 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
736 			    TX_BD_FLAGS_PACKET_END);
737 
738 	netdev_tx_sent_queue(txq, skb->len);
739 
740 	skb_tx_timestamp(skb);
741 
742 	prod = NEXT_TX(prod);
743 	WRITE_ONCE(txr->tx_prod, prod);
744 
745 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
746 		bnxt_txr_db_kick(bp, txr, prod);
747 	} else {
748 		if (free_size >= bp->tx_wake_thresh)
749 			txbd0->tx_bd_len_flags_type |=
750 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
751 		txr->kick_pending = 1;
752 	}
753 
754 tx_done:
755 
756 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
757 		if (netdev_xmit_more() && !tx_buf->is_push) {
758 			txbd0->tx_bd_len_flags_type &=
759 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
760 			bnxt_txr_db_kick(bp, txr, prod);
761 		}
762 
763 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
764 				   bp->tx_wake_thresh);
765 	}
766 	return NETDEV_TX_OK;
767 
768 tx_dma_error:
769 	last_frag = i;
770 
771 	/* start back at beginning and unmap skb */
772 	prod = txr->tx_prod;
773 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
774 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
775 			 skb_headlen(skb), DMA_TO_DEVICE);
776 	prod = NEXT_TX(prod);
777 
778 	/* unmap remaining mapped pages */
779 	for (i = 0; i < last_frag; i++) {
780 		prod = NEXT_TX(prod);
781 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
782 		frag = &skb_shinfo(skb)->frags[i];
783 		netmem_dma_unmap_page_attrs(&pdev->dev,
784 					    dma_unmap_addr(tx_buf, mapping),
785 					    skb_frag_size(frag),
786 					    DMA_TO_DEVICE, 0);
787 	}
788 
789 tx_free:
790 	dev_kfree_skb_any(skb);
791 tx_kick_pending:
792 	if (BNXT_TX_PTP_IS_SET(lflags)) {
793 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
794 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
795 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
796 			/* set SKB to err so PTP worker will clean up */
797 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
798 	}
799 	if (txr->kick_pending)
800 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
801 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
802 	dev_core_stats_tx_dropped_inc(dev);
803 	return NETDEV_TX_OK;
804 }
805 
806 /* Returns true if some remaining TX packets not processed. */
807 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
808 			  int budget)
809 {
810 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
811 	struct pci_dev *pdev = bp->pdev;
812 	u16 hw_cons = txr->tx_hw_cons;
813 	unsigned int tx_bytes = 0;
814 	u16 cons = txr->tx_cons;
815 	skb_frag_t *frag;
816 	int tx_pkts = 0;
817 	bool rc = false;
818 
819 	while (RING_TX(bp, cons) != hw_cons) {
820 		struct bnxt_sw_tx_bd *tx_buf;
821 		struct sk_buff *skb;
822 		bool is_ts_pkt;
823 		int j, last;
824 
825 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
826 		skb = tx_buf->skb;
827 
828 		if (unlikely(!skb)) {
829 			bnxt_sched_reset_txr(bp, txr, cons);
830 			return rc;
831 		}
832 
833 		is_ts_pkt = tx_buf->is_ts_pkt;
834 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
835 			rc = true;
836 			break;
837 		}
838 
839 		cons = NEXT_TX(cons);
840 		tx_pkts++;
841 		tx_bytes += skb->len;
842 		tx_buf->skb = NULL;
843 		tx_buf->is_ts_pkt = 0;
844 
845 		if (tx_buf->is_push) {
846 			tx_buf->is_push = 0;
847 			goto next_tx_int;
848 		}
849 
850 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
851 				 skb_headlen(skb), DMA_TO_DEVICE);
852 		last = tx_buf->nr_frags;
853 
854 		for (j = 0; j < last; j++) {
855 			frag = &skb_shinfo(skb)->frags[j];
856 			cons = NEXT_TX(cons);
857 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
858 			netmem_dma_unmap_page_attrs(&pdev->dev,
859 						    dma_unmap_addr(tx_buf,
860 								   mapping),
861 						    skb_frag_size(frag),
862 						    DMA_TO_DEVICE, 0);
863 		}
864 		if (unlikely(is_ts_pkt)) {
865 			if (BNXT_CHIP_P5(bp)) {
866 				/* PTP worker takes ownership of the skb */
867 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
868 				skb = NULL;
869 			}
870 		}
871 
872 next_tx_int:
873 		cons = NEXT_TX(cons);
874 
875 		dev_consume_skb_any(skb);
876 	}
877 
878 	WRITE_ONCE(txr->tx_cons, cons);
879 
880 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
881 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
882 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
883 
884 	return rc;
885 }
886 
887 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
888 {
889 	struct bnxt_tx_ring_info *txr;
890 	bool more = false;
891 	int i;
892 
893 	bnxt_for_each_napi_tx(i, bnapi, txr) {
894 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
895 			more |= __bnxt_tx_int(bp, txr, budget);
896 	}
897 	if (!more)
898 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
899 }
900 
901 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
902 {
903 	return rxr->need_head_pool || PAGE_SIZE > BNXT_RX_PAGE_SIZE;
904 }
905 
906 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
907 					 struct bnxt_rx_ring_info *rxr,
908 					 unsigned int *offset,
909 					 gfp_t gfp)
910 {
911 	struct page *page;
912 
913 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
914 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
915 						BNXT_RX_PAGE_SIZE);
916 	} else {
917 		page = page_pool_dev_alloc_pages(rxr->page_pool);
918 		*offset = 0;
919 	}
920 	if (!page)
921 		return NULL;
922 
923 	*mapping = page_pool_get_dma_addr(page) + *offset;
924 	return page;
925 }
926 
927 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
928 					 struct bnxt_rx_ring_info *rxr,
929 					 unsigned int *offset,
930 					 gfp_t gfp)
931 {
932 	netmem_ref netmem;
933 
934 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
935 		netmem = page_pool_alloc_frag_netmem(rxr->page_pool, offset, BNXT_RX_PAGE_SIZE, gfp);
936 	} else {
937 		netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
938 		*offset = 0;
939 	}
940 	if (!netmem)
941 		return 0;
942 
943 	*mapping = page_pool_get_dma_addr_netmem(netmem) + *offset;
944 	return netmem;
945 }
946 
947 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
948 				       struct bnxt_rx_ring_info *rxr,
949 				       gfp_t gfp)
950 {
951 	unsigned int offset;
952 	struct page *page;
953 
954 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
955 				    bp->rx_buf_size, gfp);
956 	if (!page)
957 		return NULL;
958 
959 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
960 	return page_address(page) + offset;
961 }
962 
963 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
964 		       u16 prod, gfp_t gfp)
965 {
966 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
967 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
968 	dma_addr_t mapping;
969 
970 	if (BNXT_RX_PAGE_MODE(bp)) {
971 		unsigned int offset;
972 		struct page *page =
973 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
974 
975 		if (!page)
976 			return -ENOMEM;
977 
978 		mapping += bp->rx_dma_offset;
979 		rx_buf->data = page;
980 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
981 	} else {
982 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
983 
984 		if (!data)
985 			return -ENOMEM;
986 
987 		rx_buf->data = data;
988 		rx_buf->data_ptr = data + bp->rx_offset;
989 	}
990 	rx_buf->mapping = mapping;
991 
992 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
993 	return 0;
994 }
995 
996 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
997 {
998 	u16 prod = rxr->rx_prod;
999 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1000 	struct bnxt *bp = rxr->bnapi->bp;
1001 	struct rx_bd *cons_bd, *prod_bd;
1002 
1003 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1004 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1005 
1006 	prod_rx_buf->data = data;
1007 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1008 
1009 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1010 
1011 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1012 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1013 
1014 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1015 }
1016 
1017 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1018 {
1019 	u16 next, max = rxr->rx_agg_bmap_size;
1020 
1021 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1022 	if (next >= max)
1023 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1024 	return next;
1025 }
1026 
1027 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1028 				u16 prod, gfp_t gfp)
1029 {
1030 	struct rx_bd *rxbd =
1031 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1032 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1033 	u16 sw_prod = rxr->rx_sw_agg_prod;
1034 	unsigned int offset = 0;
1035 	dma_addr_t mapping;
1036 	netmem_ref netmem;
1037 
1038 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, &offset, gfp);
1039 	if (!netmem)
1040 		return -ENOMEM;
1041 
1042 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1043 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1044 
1045 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1046 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1047 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1048 
1049 	rx_agg_buf->netmem = netmem;
1050 	rx_agg_buf->offset = offset;
1051 	rx_agg_buf->mapping = mapping;
1052 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1053 	rxbd->rx_bd_opaque = sw_prod;
1054 	return 0;
1055 }
1056 
1057 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1058 				       struct bnxt_cp_ring_info *cpr,
1059 				       u16 cp_cons, u16 curr)
1060 {
1061 	struct rx_agg_cmp *agg;
1062 
1063 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1064 	agg = (struct rx_agg_cmp *)
1065 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1066 	return agg;
1067 }
1068 
1069 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1070 					      struct bnxt_rx_ring_info *rxr,
1071 					      u16 agg_id, u16 curr)
1072 {
1073 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1074 
1075 	return &tpa_info->agg_arr[curr];
1076 }
1077 
1078 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1079 				   u16 start, u32 agg_bufs, bool tpa)
1080 {
1081 	struct bnxt_napi *bnapi = cpr->bnapi;
1082 	struct bnxt *bp = bnapi->bp;
1083 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1084 	u16 prod = rxr->rx_agg_prod;
1085 	u16 sw_prod = rxr->rx_sw_agg_prod;
1086 	bool p5_tpa = false;
1087 	u32 i;
1088 
1089 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1090 		p5_tpa = true;
1091 
1092 	for (i = 0; i < agg_bufs; i++) {
1093 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1094 		struct rx_agg_cmp *agg;
1095 		struct rx_bd *prod_bd;
1096 		netmem_ref netmem;
1097 		u16 cons;
1098 
1099 		if (p5_tpa)
1100 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1101 		else
1102 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1103 		cons = agg->rx_agg_cmp_opaque;
1104 		__clear_bit(cons, rxr->rx_agg_bmap);
1105 
1106 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1107 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1108 
1109 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1110 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1111 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1112 
1113 		/* It is possible for sw_prod to be equal to cons, so
1114 		 * set cons_rx_buf->netmem to 0 first.
1115 		 */
1116 		netmem = cons_rx_buf->netmem;
1117 		cons_rx_buf->netmem = 0;
1118 		prod_rx_buf->netmem = netmem;
1119 		prod_rx_buf->offset = cons_rx_buf->offset;
1120 
1121 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1122 
1123 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1124 
1125 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1126 		prod_bd->rx_bd_opaque = sw_prod;
1127 
1128 		prod = NEXT_RX_AGG(prod);
1129 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1130 	}
1131 	rxr->rx_agg_prod = prod;
1132 	rxr->rx_sw_agg_prod = sw_prod;
1133 }
1134 
1135 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1136 					      struct bnxt_rx_ring_info *rxr,
1137 					      u16 cons, void *data, u8 *data_ptr,
1138 					      dma_addr_t dma_addr,
1139 					      unsigned int offset_and_len)
1140 {
1141 	unsigned int len = offset_and_len & 0xffff;
1142 	struct page *page = data;
1143 	u16 prod = rxr->rx_prod;
1144 	struct sk_buff *skb;
1145 	int err;
1146 
1147 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1148 	if (unlikely(err)) {
1149 		bnxt_reuse_rx_data(rxr, cons, data);
1150 		return NULL;
1151 	}
1152 	dma_addr -= bp->rx_dma_offset;
1153 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1154 				bp->rx_dir);
1155 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1156 	if (!skb) {
1157 		page_pool_recycle_direct(rxr->page_pool, page);
1158 		return NULL;
1159 	}
1160 	skb_mark_for_recycle(skb);
1161 	skb_reserve(skb, bp->rx_offset);
1162 	__skb_put(skb, len);
1163 
1164 	return skb;
1165 }
1166 
1167 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1168 					struct bnxt_rx_ring_info *rxr,
1169 					u16 cons, void *data, u8 *data_ptr,
1170 					dma_addr_t dma_addr,
1171 					unsigned int offset_and_len)
1172 {
1173 	unsigned int payload = offset_and_len >> 16;
1174 	unsigned int len = offset_and_len & 0xffff;
1175 	skb_frag_t *frag;
1176 	struct page *page = data;
1177 	u16 prod = rxr->rx_prod;
1178 	struct sk_buff *skb;
1179 	int off, err;
1180 
1181 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1182 	if (unlikely(err)) {
1183 		bnxt_reuse_rx_data(rxr, cons, data);
1184 		return NULL;
1185 	}
1186 	dma_addr -= bp->rx_dma_offset;
1187 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1188 				bp->rx_dir);
1189 
1190 	if (unlikely(!payload))
1191 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1192 
1193 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1194 	if (!skb) {
1195 		page_pool_recycle_direct(rxr->page_pool, page);
1196 		return NULL;
1197 	}
1198 
1199 	skb_mark_for_recycle(skb);
1200 	off = (void *)data_ptr - page_address(page);
1201 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1202 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1203 	       payload + NET_IP_ALIGN);
1204 
1205 	frag = &skb_shinfo(skb)->frags[0];
1206 	skb_frag_size_sub(frag, payload);
1207 	skb_frag_off_add(frag, payload);
1208 	skb->data_len -= payload;
1209 	skb->tail += payload;
1210 
1211 	return skb;
1212 }
1213 
1214 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1215 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1216 				   void *data, u8 *data_ptr,
1217 				   dma_addr_t dma_addr,
1218 				   unsigned int offset_and_len)
1219 {
1220 	u16 prod = rxr->rx_prod;
1221 	struct sk_buff *skb;
1222 	int err;
1223 
1224 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1225 	if (unlikely(err)) {
1226 		bnxt_reuse_rx_data(rxr, cons, data);
1227 		return NULL;
1228 	}
1229 
1230 	skb = napi_build_skb(data, bp->rx_buf_size);
1231 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1232 				bp->rx_dir);
1233 	if (!skb) {
1234 		page_pool_free_va(rxr->head_pool, data, true);
1235 		return NULL;
1236 	}
1237 
1238 	skb_mark_for_recycle(skb);
1239 	skb_reserve(skb, bp->rx_offset);
1240 	skb_put(skb, offset_and_len & 0xffff);
1241 	return skb;
1242 }
1243 
1244 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1245 				 struct bnxt_cp_ring_info *cpr,
1246 				 u16 idx, u32 agg_bufs, bool tpa,
1247 				 struct sk_buff *skb,
1248 				 struct xdp_buff *xdp)
1249 {
1250 	struct bnxt_napi *bnapi = cpr->bnapi;
1251 	struct skb_shared_info *shinfo;
1252 	struct bnxt_rx_ring_info *rxr;
1253 	u32 i, total_frag_len = 0;
1254 	bool p5_tpa = false;
1255 	u16 prod;
1256 
1257 	rxr = bnapi->rx_ring;
1258 	prod = rxr->rx_agg_prod;
1259 
1260 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1261 		p5_tpa = true;
1262 
1263 	if (skb)
1264 		shinfo = skb_shinfo(skb);
1265 	else
1266 		shinfo = xdp_get_shared_info_from_buff(xdp);
1267 
1268 	for (i = 0; i < agg_bufs; i++) {
1269 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1270 		struct rx_agg_cmp *agg;
1271 		u16 cons, frag_len;
1272 		netmem_ref netmem;
1273 
1274 		if (p5_tpa)
1275 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1276 		else
1277 			agg = bnxt_get_agg(bp, cpr, idx, i);
1278 		cons = agg->rx_agg_cmp_opaque;
1279 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1280 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1281 
1282 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1283 		if (skb) {
1284 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1285 					       cons_rx_buf->offset,
1286 					       frag_len, BNXT_RX_PAGE_SIZE);
1287 		} else {
1288 			skb_frag_t *frag = &shinfo->frags[i];
1289 
1290 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1291 						  cons_rx_buf->offset,
1292 						  frag_len);
1293 			shinfo->nr_frags = i + 1;
1294 		}
1295 		__clear_bit(cons, rxr->rx_agg_bmap);
1296 
1297 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1298 		 * a sw_prod index that equals the cons index, so we
1299 		 * need to clear the cons entry now.
1300 		 */
1301 		netmem = cons_rx_buf->netmem;
1302 		cons_rx_buf->netmem = 0;
1303 
1304 		if (xdp && netmem_is_pfmemalloc(netmem))
1305 			xdp_buff_set_frag_pfmemalloc(xdp);
1306 
1307 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1308 			if (skb) {
1309 				skb->len -= frag_len;
1310 				skb->data_len -= frag_len;
1311 				skb->truesize -= BNXT_RX_PAGE_SIZE;
1312 			}
1313 
1314 			--shinfo->nr_frags;
1315 			cons_rx_buf->netmem = netmem;
1316 
1317 			/* Update prod since possibly some netmems have been
1318 			 * allocated already.
1319 			 */
1320 			rxr->rx_agg_prod = prod;
1321 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1322 			return 0;
1323 		}
1324 
1325 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1326 						  BNXT_RX_PAGE_SIZE);
1327 
1328 		total_frag_len += frag_len;
1329 		prod = NEXT_RX_AGG(prod);
1330 	}
1331 	rxr->rx_agg_prod = prod;
1332 	return total_frag_len;
1333 }
1334 
1335 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1336 					       struct bnxt_cp_ring_info *cpr,
1337 					       struct sk_buff *skb, u16 idx,
1338 					       u32 agg_bufs, bool tpa)
1339 {
1340 	u32 total_frag_len = 0;
1341 
1342 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1343 					       skb, NULL);
1344 	if (!total_frag_len) {
1345 		skb_mark_for_recycle(skb);
1346 		dev_kfree_skb(skb);
1347 		return NULL;
1348 	}
1349 
1350 	return skb;
1351 }
1352 
1353 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1354 				   struct bnxt_cp_ring_info *cpr,
1355 				   struct xdp_buff *xdp, u16 idx,
1356 				   u32 agg_bufs, bool tpa)
1357 {
1358 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1359 	u32 total_frag_len = 0;
1360 
1361 	if (!xdp_buff_has_frags(xdp))
1362 		shinfo->nr_frags = 0;
1363 
1364 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1365 					       NULL, xdp);
1366 	if (total_frag_len) {
1367 		xdp_buff_set_frags_flag(xdp);
1368 		shinfo->nr_frags = agg_bufs;
1369 		shinfo->xdp_frags_size = total_frag_len;
1370 	}
1371 	return total_frag_len;
1372 }
1373 
1374 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1375 			       u8 agg_bufs, u32 *raw_cons)
1376 {
1377 	u16 last;
1378 	struct rx_agg_cmp *agg;
1379 
1380 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1381 	last = RING_CMP(*raw_cons);
1382 	agg = (struct rx_agg_cmp *)
1383 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1384 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1385 }
1386 
1387 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1388 				      unsigned int len,
1389 				      dma_addr_t mapping)
1390 {
1391 	struct bnxt *bp = bnapi->bp;
1392 	struct pci_dev *pdev = bp->pdev;
1393 	struct sk_buff *skb;
1394 
1395 	skb = napi_alloc_skb(&bnapi->napi, len);
1396 	if (!skb)
1397 		return NULL;
1398 
1399 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1400 				bp->rx_dir);
1401 
1402 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1403 	       len + NET_IP_ALIGN);
1404 
1405 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1406 				   bp->rx_dir);
1407 
1408 	skb_put(skb, len);
1409 
1410 	return skb;
1411 }
1412 
1413 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1414 				     unsigned int len,
1415 				     dma_addr_t mapping)
1416 {
1417 	return bnxt_copy_data(bnapi, data, len, mapping);
1418 }
1419 
1420 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1421 				     struct xdp_buff *xdp,
1422 				     unsigned int len,
1423 				     dma_addr_t mapping)
1424 {
1425 	unsigned int metasize = 0;
1426 	u8 *data = xdp->data;
1427 	struct sk_buff *skb;
1428 
1429 	len = xdp->data_end - xdp->data_meta;
1430 	metasize = xdp->data - xdp->data_meta;
1431 	data = xdp->data_meta;
1432 
1433 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1434 	if (!skb)
1435 		return skb;
1436 
1437 	if (metasize) {
1438 		skb_metadata_set(skb, metasize);
1439 		__skb_pull(skb, metasize);
1440 	}
1441 
1442 	return skb;
1443 }
1444 
1445 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1446 			   u32 *raw_cons, void *cmp)
1447 {
1448 	struct rx_cmp *rxcmp = cmp;
1449 	u32 tmp_raw_cons = *raw_cons;
1450 	u8 cmp_type, agg_bufs = 0;
1451 
1452 	cmp_type = RX_CMP_TYPE(rxcmp);
1453 
1454 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1455 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1456 			    RX_CMP_AGG_BUFS) >>
1457 			   RX_CMP_AGG_BUFS_SHIFT;
1458 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1459 		struct rx_tpa_end_cmp *tpa_end = cmp;
1460 
1461 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1462 			return 0;
1463 
1464 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1465 	}
1466 
1467 	if (agg_bufs) {
1468 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1469 			return -EBUSY;
1470 	}
1471 	*raw_cons = tmp_raw_cons;
1472 	return 0;
1473 }
1474 
1475 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1476 {
1477 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1478 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1479 
1480 	if (test_bit(idx, map->agg_idx_bmap))
1481 		idx = find_first_zero_bit(map->agg_idx_bmap,
1482 					  BNXT_AGG_IDX_BMAP_SIZE);
1483 	__set_bit(idx, map->agg_idx_bmap);
1484 	map->agg_id_tbl[agg_id] = idx;
1485 	return idx;
1486 }
1487 
1488 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1489 {
1490 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1491 
1492 	__clear_bit(idx, map->agg_idx_bmap);
1493 }
1494 
1495 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1496 {
1497 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1498 
1499 	return map->agg_id_tbl[agg_id];
1500 }
1501 
1502 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1503 			      struct rx_tpa_start_cmp *tpa_start,
1504 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1505 {
1506 	tpa_info->cfa_code_valid = 1;
1507 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1508 	tpa_info->vlan_valid = 0;
1509 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1510 		tpa_info->vlan_valid = 1;
1511 		tpa_info->metadata =
1512 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1513 	}
1514 }
1515 
1516 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1517 				 struct rx_tpa_start_cmp *tpa_start,
1518 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1519 {
1520 	tpa_info->vlan_valid = 0;
1521 	if (TPA_START_VLAN_VALID(tpa_start)) {
1522 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1523 		u32 vlan_proto = ETH_P_8021Q;
1524 
1525 		tpa_info->vlan_valid = 1;
1526 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1527 			vlan_proto = ETH_P_8021AD;
1528 		tpa_info->metadata = vlan_proto << 16 |
1529 				     TPA_START_METADATA0_TCI(tpa_start1);
1530 	}
1531 }
1532 
1533 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1534 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1535 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1536 {
1537 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1538 	struct bnxt_tpa_info *tpa_info;
1539 	u16 cons, prod, agg_id;
1540 	struct rx_bd *prod_bd;
1541 	dma_addr_t mapping;
1542 
1543 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1544 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1545 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1546 	} else {
1547 		agg_id = TPA_START_AGG_ID(tpa_start);
1548 	}
1549 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1550 	prod = rxr->rx_prod;
1551 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1552 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1553 	tpa_info = &rxr->rx_tpa[agg_id];
1554 
1555 	if (unlikely(cons != rxr->rx_next_cons ||
1556 		     TPA_START_ERROR(tpa_start))) {
1557 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1558 			    cons, rxr->rx_next_cons,
1559 			    TPA_START_ERROR_CODE(tpa_start1));
1560 		bnxt_sched_reset_rxr(bp, rxr);
1561 		return;
1562 	}
1563 	prod_rx_buf->data = tpa_info->data;
1564 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1565 
1566 	mapping = tpa_info->mapping;
1567 	prod_rx_buf->mapping = mapping;
1568 
1569 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1570 
1571 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1572 
1573 	tpa_info->data = cons_rx_buf->data;
1574 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1575 	cons_rx_buf->data = NULL;
1576 	tpa_info->mapping = cons_rx_buf->mapping;
1577 
1578 	tpa_info->len =
1579 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1580 				RX_TPA_START_CMP_LEN_SHIFT;
1581 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1582 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1583 		tpa_info->gso_type = SKB_GSO_TCPV4;
1584 		if (TPA_START_IS_IPV6(tpa_start1))
1585 			tpa_info->gso_type = SKB_GSO_TCPV6;
1586 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1587 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1588 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1589 			tpa_info->gso_type = SKB_GSO_TCPV6;
1590 		tpa_info->rss_hash =
1591 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1592 	} else {
1593 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1594 		tpa_info->gso_type = 0;
1595 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1596 	}
1597 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1598 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1599 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1600 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1601 	else
1602 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1603 	tpa_info->agg_count = 0;
1604 
1605 	rxr->rx_prod = NEXT_RX(prod);
1606 	cons = RING_RX(bp, NEXT_RX(cons));
1607 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1608 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1609 
1610 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1611 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1612 	cons_rx_buf->data = NULL;
1613 }
1614 
1615 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1616 {
1617 	if (agg_bufs)
1618 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1619 }
1620 
1621 #ifdef CONFIG_INET
1622 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1623 {
1624 	struct udphdr *uh = NULL;
1625 
1626 	if (ip_proto == htons(ETH_P_IP)) {
1627 		struct iphdr *iph = (struct iphdr *)skb->data;
1628 
1629 		if (iph->protocol == IPPROTO_UDP)
1630 			uh = (struct udphdr *)(iph + 1);
1631 	} else {
1632 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1633 
1634 		if (iph->nexthdr == IPPROTO_UDP)
1635 			uh = (struct udphdr *)(iph + 1);
1636 	}
1637 	if (uh) {
1638 		if (uh->check)
1639 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1640 		else
1641 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1642 	}
1643 }
1644 #endif
1645 
1646 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1647 					   int payload_off, int tcp_ts,
1648 					   struct sk_buff *skb)
1649 {
1650 #ifdef CONFIG_INET
1651 	struct tcphdr *th;
1652 	int len, nw_off;
1653 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1654 	u32 hdr_info = tpa_info->hdr_info;
1655 	bool loopback = false;
1656 
1657 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1658 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1659 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1660 
1661 	/* If the packet is an internal loopback packet, the offsets will
1662 	 * have an extra 4 bytes.
1663 	 */
1664 	if (inner_mac_off == 4) {
1665 		loopback = true;
1666 	} else if (inner_mac_off > 4) {
1667 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1668 					    ETH_HLEN - 2));
1669 
1670 		/* We only support inner iPv4/ipv6.  If we don't see the
1671 		 * correct protocol ID, it must be a loopback packet where
1672 		 * the offsets are off by 4.
1673 		 */
1674 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1675 			loopback = true;
1676 	}
1677 	if (loopback) {
1678 		/* internal loopback packet, subtract all offsets by 4 */
1679 		inner_ip_off -= 4;
1680 		inner_mac_off -= 4;
1681 		outer_ip_off -= 4;
1682 	}
1683 
1684 	nw_off = inner_ip_off - ETH_HLEN;
1685 	skb_set_network_header(skb, nw_off);
1686 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1687 		struct ipv6hdr *iph = ipv6_hdr(skb);
1688 
1689 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1690 		len = skb->len - skb_transport_offset(skb);
1691 		th = tcp_hdr(skb);
1692 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1693 	} else {
1694 		struct iphdr *iph = ip_hdr(skb);
1695 
1696 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1697 		len = skb->len - skb_transport_offset(skb);
1698 		th = tcp_hdr(skb);
1699 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1700 	}
1701 
1702 	if (inner_mac_off) { /* tunnel */
1703 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1704 					    ETH_HLEN - 2));
1705 
1706 		bnxt_gro_tunnel(skb, proto);
1707 	}
1708 #endif
1709 	return skb;
1710 }
1711 
1712 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1713 					   int payload_off, int tcp_ts,
1714 					   struct sk_buff *skb)
1715 {
1716 #ifdef CONFIG_INET
1717 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1718 	u32 hdr_info = tpa_info->hdr_info;
1719 	int iphdr_len, nw_off;
1720 
1721 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1722 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1723 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1724 
1725 	nw_off = inner_ip_off - ETH_HLEN;
1726 	skb_set_network_header(skb, nw_off);
1727 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1728 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1729 	skb_set_transport_header(skb, nw_off + iphdr_len);
1730 
1731 	if (inner_mac_off) { /* tunnel */
1732 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1733 					    ETH_HLEN - 2));
1734 
1735 		bnxt_gro_tunnel(skb, proto);
1736 	}
1737 #endif
1738 	return skb;
1739 }
1740 
1741 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1742 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1743 
1744 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1745 					   int payload_off, int tcp_ts,
1746 					   struct sk_buff *skb)
1747 {
1748 #ifdef CONFIG_INET
1749 	struct tcphdr *th;
1750 	int len, nw_off, tcp_opt_len = 0;
1751 
1752 	if (tcp_ts)
1753 		tcp_opt_len = 12;
1754 
1755 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1756 		struct iphdr *iph;
1757 
1758 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1759 			 ETH_HLEN;
1760 		skb_set_network_header(skb, nw_off);
1761 		iph = ip_hdr(skb);
1762 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1763 		len = skb->len - skb_transport_offset(skb);
1764 		th = tcp_hdr(skb);
1765 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1766 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1767 		struct ipv6hdr *iph;
1768 
1769 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1770 			 ETH_HLEN;
1771 		skb_set_network_header(skb, nw_off);
1772 		iph = ipv6_hdr(skb);
1773 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1774 		len = skb->len - skb_transport_offset(skb);
1775 		th = tcp_hdr(skb);
1776 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1777 	} else {
1778 		dev_kfree_skb_any(skb);
1779 		return NULL;
1780 	}
1781 
1782 	if (nw_off) /* tunnel */
1783 		bnxt_gro_tunnel(skb, skb->protocol);
1784 #endif
1785 	return skb;
1786 }
1787 
1788 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1789 					   struct bnxt_tpa_info *tpa_info,
1790 					   struct rx_tpa_end_cmp *tpa_end,
1791 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1792 					   struct sk_buff *skb)
1793 {
1794 #ifdef CONFIG_INET
1795 	int payload_off;
1796 	u16 segs;
1797 
1798 	segs = TPA_END_TPA_SEGS(tpa_end);
1799 	if (segs == 1)
1800 		return skb;
1801 
1802 	NAPI_GRO_CB(skb)->count = segs;
1803 	skb_shinfo(skb)->gso_size =
1804 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1805 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1806 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1807 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1808 	else
1809 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1810 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1811 	if (likely(skb))
1812 		tcp_gro_complete(skb);
1813 #endif
1814 	return skb;
1815 }
1816 
1817 /* Given the cfa_code of a received packet determine which
1818  * netdev (vf-rep or PF) the packet is destined to.
1819  */
1820 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1821 {
1822 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1823 
1824 	/* if vf-rep dev is NULL, it must belong to the PF */
1825 	return dev ? dev : bp->dev;
1826 }
1827 
1828 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1829 					   struct bnxt_cp_ring_info *cpr,
1830 					   u32 *raw_cons,
1831 					   struct rx_tpa_end_cmp *tpa_end,
1832 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1833 					   u8 *event)
1834 {
1835 	struct bnxt_napi *bnapi = cpr->bnapi;
1836 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1837 	struct net_device *dev = bp->dev;
1838 	u8 *data_ptr, agg_bufs;
1839 	unsigned int len;
1840 	struct bnxt_tpa_info *tpa_info;
1841 	dma_addr_t mapping;
1842 	struct sk_buff *skb;
1843 	u16 idx = 0, agg_id;
1844 	void *data;
1845 	bool gro;
1846 
1847 	if (unlikely(bnapi->in_reset)) {
1848 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1849 
1850 		if (rc < 0)
1851 			return ERR_PTR(-EBUSY);
1852 		return NULL;
1853 	}
1854 
1855 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1856 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1857 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1858 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1859 		tpa_info = &rxr->rx_tpa[agg_id];
1860 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1861 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1862 				    agg_bufs, tpa_info->agg_count);
1863 			agg_bufs = tpa_info->agg_count;
1864 		}
1865 		tpa_info->agg_count = 0;
1866 		*event |= BNXT_AGG_EVENT;
1867 		bnxt_free_agg_idx(rxr, agg_id);
1868 		idx = agg_id;
1869 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1870 	} else {
1871 		agg_id = TPA_END_AGG_ID(tpa_end);
1872 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1873 		tpa_info = &rxr->rx_tpa[agg_id];
1874 		idx = RING_CMP(*raw_cons);
1875 		if (agg_bufs) {
1876 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1877 				return ERR_PTR(-EBUSY);
1878 
1879 			*event |= BNXT_AGG_EVENT;
1880 			idx = NEXT_CMP(idx);
1881 		}
1882 		gro = !!TPA_END_GRO(tpa_end);
1883 	}
1884 	data = tpa_info->data;
1885 	data_ptr = tpa_info->data_ptr;
1886 	prefetch(data_ptr);
1887 	len = tpa_info->len;
1888 	mapping = tpa_info->mapping;
1889 
1890 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1891 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1892 		if (agg_bufs > MAX_SKB_FRAGS)
1893 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1894 				    agg_bufs, (int)MAX_SKB_FRAGS);
1895 		return NULL;
1896 	}
1897 
1898 	if (len <= bp->rx_copybreak) {
1899 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1900 		if (!skb) {
1901 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1902 			cpr->sw_stats->rx.rx_oom_discards += 1;
1903 			return NULL;
1904 		}
1905 	} else {
1906 		u8 *new_data;
1907 		dma_addr_t new_mapping;
1908 
1909 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1910 						GFP_ATOMIC);
1911 		if (!new_data) {
1912 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1913 			cpr->sw_stats->rx.rx_oom_discards += 1;
1914 			return NULL;
1915 		}
1916 
1917 		tpa_info->data = new_data;
1918 		tpa_info->data_ptr = new_data + bp->rx_offset;
1919 		tpa_info->mapping = new_mapping;
1920 
1921 		skb = napi_build_skb(data, bp->rx_buf_size);
1922 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1923 					bp->rx_buf_use_size, bp->rx_dir);
1924 
1925 		if (!skb) {
1926 			page_pool_free_va(rxr->head_pool, data, true);
1927 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1928 			cpr->sw_stats->rx.rx_oom_discards += 1;
1929 			return NULL;
1930 		}
1931 		skb_mark_for_recycle(skb);
1932 		skb_reserve(skb, bp->rx_offset);
1933 		skb_put(skb, len);
1934 	}
1935 
1936 	if (agg_bufs) {
1937 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1938 					      true);
1939 		if (!skb) {
1940 			/* Page reuse already handled by bnxt_rx_pages(). */
1941 			cpr->sw_stats->rx.rx_oom_discards += 1;
1942 			return NULL;
1943 		}
1944 	}
1945 
1946 	if (tpa_info->cfa_code_valid)
1947 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1948 	skb->protocol = eth_type_trans(skb, dev);
1949 
1950 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1951 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1952 
1953 	if (tpa_info->vlan_valid &&
1954 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1955 		__be16 vlan_proto = htons(tpa_info->metadata >>
1956 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1957 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1958 
1959 		if (eth_type_vlan(vlan_proto)) {
1960 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1961 		} else {
1962 			dev_kfree_skb(skb);
1963 			return NULL;
1964 		}
1965 	}
1966 
1967 	skb_checksum_none_assert(skb);
1968 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1969 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1970 		skb->csum_level =
1971 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1972 	}
1973 
1974 	if (gro)
1975 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1976 
1977 	return skb;
1978 }
1979 
1980 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1981 			 struct rx_agg_cmp *rx_agg)
1982 {
1983 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1984 	struct bnxt_tpa_info *tpa_info;
1985 
1986 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1987 	tpa_info = &rxr->rx_tpa[agg_id];
1988 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1989 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1990 }
1991 
1992 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1993 			     struct sk_buff *skb)
1994 {
1995 	skb_mark_for_recycle(skb);
1996 
1997 	if (skb->dev != bp->dev) {
1998 		/* this packet belongs to a vf-rep */
1999 		bnxt_vf_rep_rx(bp, skb);
2000 		return;
2001 	}
2002 	skb_record_rx_queue(skb, bnapi->index);
2003 	napi_gro_receive(&bnapi->napi, skb);
2004 }
2005 
2006 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2007 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2008 {
2009 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2010 
2011 	if (BNXT_PTP_RX_TS_VALID(flags))
2012 		goto ts_valid;
2013 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2014 		return false;
2015 
2016 ts_valid:
2017 	*cmpl_ts = ts;
2018 	return true;
2019 }
2020 
2021 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2022 				    struct rx_cmp *rxcmp,
2023 				    struct rx_cmp_ext *rxcmp1)
2024 {
2025 	__be16 vlan_proto;
2026 	u16 vtag;
2027 
2028 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2029 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2030 		u32 meta_data;
2031 
2032 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2033 			return skb;
2034 
2035 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2036 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2037 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2038 		if (eth_type_vlan(vlan_proto))
2039 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2040 		else
2041 			goto vlan_err;
2042 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2043 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2044 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2045 
2046 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2047 				vlan_proto = htons(ETH_P_8021Q);
2048 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2049 				vlan_proto = htons(ETH_P_8021AD);
2050 			else
2051 				goto vlan_err;
2052 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2053 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2054 		}
2055 	}
2056 	return skb;
2057 vlan_err:
2058 	skb_mark_for_recycle(skb);
2059 	dev_kfree_skb(skb);
2060 	return NULL;
2061 }
2062 
2063 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2064 					   struct rx_cmp *rxcmp)
2065 {
2066 	u8 ext_op;
2067 
2068 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2069 	switch (ext_op) {
2070 	case EXT_OP_INNER_4:
2071 	case EXT_OP_OUTER_4:
2072 	case EXT_OP_INNFL_3:
2073 	case EXT_OP_OUTFL_3:
2074 		return PKT_HASH_TYPE_L4;
2075 	default:
2076 		return PKT_HASH_TYPE_L3;
2077 	}
2078 }
2079 
2080 /* returns the following:
2081  * 1       - 1 packet successfully received
2082  * 0       - successful TPA_START, packet not completed yet
2083  * -EBUSY  - completion ring does not have all the agg buffers yet
2084  * -ENOMEM - packet aborted due to out of memory
2085  * -EIO    - packet aborted due to hw error indicated in BD
2086  */
2087 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2088 		       u32 *raw_cons, u8 *event)
2089 {
2090 	struct bnxt_napi *bnapi = cpr->bnapi;
2091 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2092 	struct net_device *dev = bp->dev;
2093 	struct rx_cmp *rxcmp;
2094 	struct rx_cmp_ext *rxcmp1;
2095 	u32 tmp_raw_cons = *raw_cons;
2096 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2097 	struct skb_shared_info *sinfo;
2098 	struct bnxt_sw_rx_bd *rx_buf;
2099 	unsigned int len;
2100 	u8 *data_ptr, agg_bufs, cmp_type;
2101 	bool xdp_active = false;
2102 	dma_addr_t dma_addr;
2103 	struct sk_buff *skb;
2104 	struct xdp_buff xdp;
2105 	u32 flags, misc;
2106 	u32 cmpl_ts;
2107 	void *data;
2108 	int rc = 0;
2109 
2110 	rxcmp = (struct rx_cmp *)
2111 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2112 
2113 	cmp_type = RX_CMP_TYPE(rxcmp);
2114 
2115 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2116 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2117 		goto next_rx_no_prod_no_len;
2118 	}
2119 
2120 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2121 	cp_cons = RING_CMP(tmp_raw_cons);
2122 	rxcmp1 = (struct rx_cmp_ext *)
2123 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2124 
2125 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2126 		return -EBUSY;
2127 
2128 	/* The valid test of the entry must be done first before
2129 	 * reading any further.
2130 	 */
2131 	dma_rmb();
2132 	prod = rxr->rx_prod;
2133 
2134 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2135 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2136 		bnxt_tpa_start(bp, rxr, cmp_type,
2137 			       (struct rx_tpa_start_cmp *)rxcmp,
2138 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2139 
2140 		*event |= BNXT_RX_EVENT;
2141 		goto next_rx_no_prod_no_len;
2142 
2143 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2144 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2145 				   (struct rx_tpa_end_cmp *)rxcmp,
2146 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2147 
2148 		if (IS_ERR(skb))
2149 			return -EBUSY;
2150 
2151 		rc = -ENOMEM;
2152 		if (likely(skb)) {
2153 			bnxt_deliver_skb(bp, bnapi, skb);
2154 			rc = 1;
2155 		}
2156 		*event |= BNXT_RX_EVENT;
2157 		goto next_rx_no_prod_no_len;
2158 	}
2159 
2160 	cons = rxcmp->rx_cmp_opaque;
2161 	if (unlikely(cons != rxr->rx_next_cons)) {
2162 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2163 
2164 		/* 0xffff is forced error, don't print it */
2165 		if (rxr->rx_next_cons != 0xffff)
2166 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2167 				    cons, rxr->rx_next_cons);
2168 		bnxt_sched_reset_rxr(bp, rxr);
2169 		if (rc1)
2170 			return rc1;
2171 		goto next_rx_no_prod_no_len;
2172 	}
2173 	rx_buf = &rxr->rx_buf_ring[cons];
2174 	data = rx_buf->data;
2175 	data_ptr = rx_buf->data_ptr;
2176 	prefetch(data_ptr);
2177 
2178 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2179 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2180 
2181 	if (agg_bufs) {
2182 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2183 			return -EBUSY;
2184 
2185 		cp_cons = NEXT_CMP(cp_cons);
2186 		*event |= BNXT_AGG_EVENT;
2187 	}
2188 	*event |= BNXT_RX_EVENT;
2189 
2190 	rx_buf->data = NULL;
2191 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2192 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2193 
2194 		bnxt_reuse_rx_data(rxr, cons, data);
2195 		if (agg_bufs)
2196 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2197 					       false);
2198 
2199 		rc = -EIO;
2200 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2201 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2202 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2203 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2204 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2205 						 rx_err);
2206 				bnxt_sched_reset_rxr(bp, rxr);
2207 			}
2208 		}
2209 		goto next_rx_no_len;
2210 	}
2211 
2212 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2213 	len = flags >> RX_CMP_LEN_SHIFT;
2214 	dma_addr = rx_buf->mapping;
2215 
2216 	if (bnxt_xdp_attached(bp, rxr)) {
2217 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2218 		if (agg_bufs) {
2219 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp,
2220 							       cp_cons,
2221 							       agg_bufs,
2222 							       false);
2223 			if (!frag_len)
2224 				goto oom_next_rx;
2225 
2226 		}
2227 		xdp_active = true;
2228 	}
2229 
2230 	if (xdp_active) {
2231 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2232 			rc = 1;
2233 			goto next_rx;
2234 		}
2235 		if (xdp_buff_has_frags(&xdp)) {
2236 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2237 			agg_bufs = sinfo->nr_frags;
2238 		} else {
2239 			agg_bufs = 0;
2240 		}
2241 	}
2242 
2243 	if (len <= bp->rx_copybreak) {
2244 		if (!xdp_active)
2245 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2246 		else
2247 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2248 		bnxt_reuse_rx_data(rxr, cons, data);
2249 		if (!skb) {
2250 			if (agg_bufs) {
2251 				if (!xdp_active)
2252 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2253 							       agg_bufs, false);
2254 				else
2255 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2256 			}
2257 			goto oom_next_rx;
2258 		}
2259 	} else {
2260 		u32 payload;
2261 
2262 		if (rx_buf->data_ptr == data_ptr)
2263 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2264 		else
2265 			payload = 0;
2266 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2267 				      payload | len);
2268 		if (!skb)
2269 			goto oom_next_rx;
2270 	}
2271 
2272 	if (agg_bufs) {
2273 		if (!xdp_active) {
2274 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2275 						      agg_bufs, false);
2276 			if (!skb)
2277 				goto oom_next_rx;
2278 		} else {
2279 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2280 						 rxr->page_pool, &xdp);
2281 			if (!skb) {
2282 				/* we should be able to free the old skb here */
2283 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2284 				goto oom_next_rx;
2285 			}
2286 		}
2287 	}
2288 
2289 	if (RX_CMP_HASH_VALID(rxcmp)) {
2290 		enum pkt_hash_types type;
2291 
2292 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2293 			type = bnxt_rss_ext_op(bp, rxcmp);
2294 		} else {
2295 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2296 
2297 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2298 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2299 				type = PKT_HASH_TYPE_L4;
2300 			else
2301 				type = PKT_HASH_TYPE_L3;
2302 		}
2303 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2304 	}
2305 
2306 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2307 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2308 	skb->protocol = eth_type_trans(skb, dev);
2309 
2310 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2311 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2312 		if (!skb)
2313 			goto next_rx;
2314 	}
2315 
2316 	skb_checksum_none_assert(skb);
2317 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2318 		if (dev->features & NETIF_F_RXCSUM) {
2319 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2320 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2321 		}
2322 	} else {
2323 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2324 			if (dev->features & NETIF_F_RXCSUM)
2325 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2326 		}
2327 	}
2328 
2329 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2330 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2331 			u64 ns, ts;
2332 
2333 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2334 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2335 
2336 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2337 				memset(skb_hwtstamps(skb), 0,
2338 				       sizeof(*skb_hwtstamps(skb)));
2339 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2340 			}
2341 		}
2342 	}
2343 	bnxt_deliver_skb(bp, bnapi, skb);
2344 	rc = 1;
2345 
2346 next_rx:
2347 	cpr->rx_packets += 1;
2348 	cpr->rx_bytes += len;
2349 
2350 next_rx_no_len:
2351 	rxr->rx_prod = NEXT_RX(prod);
2352 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2353 
2354 next_rx_no_prod_no_len:
2355 	*raw_cons = tmp_raw_cons;
2356 
2357 	return rc;
2358 
2359 oom_next_rx:
2360 	cpr->sw_stats->rx.rx_oom_discards += 1;
2361 	rc = -ENOMEM;
2362 	goto next_rx;
2363 }
2364 
2365 /* In netpoll mode, if we are using a combined completion ring, we need to
2366  * discard the rx packets and recycle the buffers.
2367  */
2368 static int bnxt_force_rx_discard(struct bnxt *bp,
2369 				 struct bnxt_cp_ring_info *cpr,
2370 				 u32 *raw_cons, u8 *event)
2371 {
2372 	u32 tmp_raw_cons = *raw_cons;
2373 	struct rx_cmp_ext *rxcmp1;
2374 	struct rx_cmp *rxcmp;
2375 	u16 cp_cons;
2376 	u8 cmp_type;
2377 	int rc;
2378 
2379 	cp_cons = RING_CMP(tmp_raw_cons);
2380 	rxcmp = (struct rx_cmp *)
2381 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2382 
2383 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2384 	cp_cons = RING_CMP(tmp_raw_cons);
2385 	rxcmp1 = (struct rx_cmp_ext *)
2386 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2387 
2388 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2389 		return -EBUSY;
2390 
2391 	/* The valid test of the entry must be done first before
2392 	 * reading any further.
2393 	 */
2394 	dma_rmb();
2395 	cmp_type = RX_CMP_TYPE(rxcmp);
2396 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2397 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2398 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2399 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2400 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2401 		struct rx_tpa_end_cmp_ext *tpa_end1;
2402 
2403 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2404 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2405 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2406 	}
2407 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2408 	if (rc && rc != -EBUSY)
2409 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2410 	return rc;
2411 }
2412 
2413 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2414 {
2415 	struct bnxt_fw_health *fw_health = bp->fw_health;
2416 	u32 reg = fw_health->regs[reg_idx];
2417 	u32 reg_type, reg_off, val = 0;
2418 
2419 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2420 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2421 	switch (reg_type) {
2422 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2423 		pci_read_config_dword(bp->pdev, reg_off, &val);
2424 		break;
2425 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2426 		reg_off = fw_health->mapped_regs[reg_idx];
2427 		fallthrough;
2428 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2429 		val = readl(bp->bar0 + reg_off);
2430 		break;
2431 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2432 		val = readl(bp->bar1 + reg_off);
2433 		break;
2434 	}
2435 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2436 		val &= fw_health->fw_reset_inprog_reg_mask;
2437 	return val;
2438 }
2439 
2440 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2441 {
2442 	int i;
2443 
2444 	for (i = 0; i < bp->rx_nr_rings; i++) {
2445 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2446 		struct bnxt_ring_grp_info *grp_info;
2447 
2448 		grp_info = &bp->grp_info[grp_idx];
2449 		if (grp_info->agg_fw_ring_id == ring_id)
2450 			return grp_idx;
2451 	}
2452 	return INVALID_HW_RING_ID;
2453 }
2454 
2455 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2456 {
2457 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2458 
2459 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2460 		return link_info->force_link_speed2;
2461 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2462 		return link_info->force_pam4_link_speed;
2463 	return link_info->force_link_speed;
2464 }
2465 
2466 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2467 {
2468 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2469 
2470 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2471 		link_info->req_link_speed = link_info->force_link_speed2;
2472 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2473 		switch (link_info->req_link_speed) {
2474 		case BNXT_LINK_SPEED_50GB_PAM4:
2475 		case BNXT_LINK_SPEED_100GB_PAM4:
2476 		case BNXT_LINK_SPEED_200GB_PAM4:
2477 		case BNXT_LINK_SPEED_400GB_PAM4:
2478 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2479 			break;
2480 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2481 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2482 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2483 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2484 			break;
2485 		default:
2486 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2487 		}
2488 		return;
2489 	}
2490 	link_info->req_link_speed = link_info->force_link_speed;
2491 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2492 	if (link_info->force_pam4_link_speed) {
2493 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2494 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2495 	}
2496 }
2497 
2498 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2499 {
2500 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2501 
2502 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2503 		link_info->advertising = link_info->auto_link_speeds2;
2504 		return;
2505 	}
2506 	link_info->advertising = link_info->auto_link_speeds;
2507 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2508 }
2509 
2510 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2511 {
2512 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2513 
2514 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2515 		if (link_info->req_link_speed != link_info->force_link_speed2)
2516 			return true;
2517 		return false;
2518 	}
2519 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2520 	    link_info->req_link_speed != link_info->force_link_speed)
2521 		return true;
2522 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2523 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2524 		return true;
2525 	return false;
2526 }
2527 
2528 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2529 {
2530 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2531 
2532 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2533 		if (link_info->advertising != link_info->auto_link_speeds2)
2534 			return true;
2535 		return false;
2536 	}
2537 	if (link_info->advertising != link_info->auto_link_speeds ||
2538 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2539 		return true;
2540 	return false;
2541 }
2542 
2543 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2544 {
2545 	u32 flags = bp->ctx->ctx_arr[type].flags;
2546 
2547 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2548 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2549 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2550 }
2551 
2552 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2553 {
2554 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2555 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2556 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2557 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2558 	struct bnxt_bs_trace_info *bs_trace;
2559 	int last_pg;
2560 
2561 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2562 		return;
2563 
2564 	mem_size = ctxm->max_entries * ctxm->entry_size;
2565 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2566 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2567 
2568 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2569 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2570 
2571 	rmem = &ctx_pg[0].ring_mem;
2572 	bs_trace = &bp->bs_trace[trace_type];
2573 	bs_trace->ctx_type = ctxm->type;
2574 	bs_trace->trace_type = trace_type;
2575 	if (pages > MAX_CTX_PAGES) {
2576 		int last_pg_dir = rmem->nr_pages - 1;
2577 
2578 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2579 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2580 	} else {
2581 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2582 	}
2583 	bs_trace->magic_byte += magic_byte_offset;
2584 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2585 }
2586 
2587 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2588 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2589 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2590 
2591 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2592 	(((data2) &							\
2593 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2594 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2595 
2596 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2597 	((data2) &							\
2598 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2599 
2600 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2601 	(((data2) &							\
2602 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2603 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2604 
2605 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2606 	((data1) &							\
2607 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2608 
2609 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2610 	(((data1) &							\
2611 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2612 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2613 
2614 /* Return true if the workqueue has to be scheduled */
2615 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2616 {
2617 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2618 
2619 	switch (err_type) {
2620 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2621 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2622 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2623 		break;
2624 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2625 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2626 		break;
2627 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2628 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2629 		break;
2630 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2631 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2632 		char *threshold_type;
2633 		bool notify = false;
2634 		char *dir_str;
2635 
2636 		switch (type) {
2637 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2638 			threshold_type = "warning";
2639 			break;
2640 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2641 			threshold_type = "critical";
2642 			break;
2643 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2644 			threshold_type = "fatal";
2645 			break;
2646 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2647 			threshold_type = "shutdown";
2648 			break;
2649 		default:
2650 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2651 			return false;
2652 		}
2653 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2654 			dir_str = "above";
2655 			notify = true;
2656 		} else {
2657 			dir_str = "below";
2658 		}
2659 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2660 			    dir_str, threshold_type);
2661 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2662 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2663 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2664 		if (notify) {
2665 			bp->thermal_threshold_type = type;
2666 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2667 			return true;
2668 		}
2669 		return false;
2670 	}
2671 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2672 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2673 		break;
2674 	default:
2675 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2676 			   err_type);
2677 		break;
2678 	}
2679 	return false;
2680 }
2681 
2682 #define BNXT_GET_EVENT_PORT(data)	\
2683 	((data) &			\
2684 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2685 
2686 #define BNXT_EVENT_RING_TYPE(data2)	\
2687 	((data2) &			\
2688 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2689 
2690 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2691 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2692 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2693 
2694 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2695 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2696 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2697 
2698 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2699 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2700 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2701 
2702 #define BNXT_PHC_BITS	48
2703 
2704 static int bnxt_async_event_process(struct bnxt *bp,
2705 				    struct hwrm_async_event_cmpl *cmpl)
2706 {
2707 	u16 event_id = le16_to_cpu(cmpl->event_id);
2708 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2709 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2710 
2711 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2712 		   event_id, data1, data2);
2713 
2714 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2715 	switch (event_id) {
2716 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2717 		struct bnxt_link_info *link_info = &bp->link_info;
2718 
2719 		if (BNXT_VF(bp))
2720 			goto async_event_process_exit;
2721 
2722 		/* print unsupported speed warning in forced speed mode only */
2723 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2724 		    (data1 & 0x20000)) {
2725 			u16 fw_speed = bnxt_get_force_speed(link_info);
2726 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2727 
2728 			if (speed != SPEED_UNKNOWN)
2729 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2730 					    speed);
2731 		}
2732 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2733 	}
2734 		fallthrough;
2735 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2736 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2737 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2738 		fallthrough;
2739 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2740 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2741 		break;
2742 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2743 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2744 		break;
2745 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2746 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2747 
2748 		if (BNXT_VF(bp))
2749 			break;
2750 
2751 		if (bp->pf.port_id != port_id)
2752 			break;
2753 
2754 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2755 		break;
2756 	}
2757 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2758 		if (BNXT_PF(bp))
2759 			goto async_event_process_exit;
2760 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2761 		break;
2762 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2763 		char *type_str = "Solicited";
2764 
2765 		if (!bp->fw_health)
2766 			goto async_event_process_exit;
2767 
2768 		bp->fw_reset_timestamp = jiffies;
2769 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2770 		if (!bp->fw_reset_min_dsecs)
2771 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2772 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2773 		if (!bp->fw_reset_max_dsecs)
2774 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2775 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2776 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2777 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2778 			type_str = "Fatal";
2779 			bp->fw_health->fatalities++;
2780 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2781 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2782 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2783 			type_str = "Non-fatal";
2784 			bp->fw_health->survivals++;
2785 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2786 		}
2787 		netif_warn(bp, hw, bp->dev,
2788 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2789 			   type_str, data1, data2,
2790 			   bp->fw_reset_min_dsecs * 100,
2791 			   bp->fw_reset_max_dsecs * 100);
2792 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2793 		break;
2794 	}
2795 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2796 		struct bnxt_fw_health *fw_health = bp->fw_health;
2797 		char *status_desc = "healthy";
2798 		u32 status;
2799 
2800 		if (!fw_health)
2801 			goto async_event_process_exit;
2802 
2803 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2804 			fw_health->enabled = false;
2805 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2806 			break;
2807 		}
2808 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2809 		fw_health->tmr_multiplier =
2810 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2811 				     bp->current_interval * 10);
2812 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2813 		if (!fw_health->enabled)
2814 			fw_health->last_fw_heartbeat =
2815 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2816 		fw_health->last_fw_reset_cnt =
2817 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2818 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2819 		if (status != BNXT_FW_STATUS_HEALTHY)
2820 			status_desc = "unhealthy";
2821 		netif_info(bp, drv, bp->dev,
2822 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2823 			   fw_health->primary ? "primary" : "backup", status,
2824 			   status_desc, fw_health->last_fw_reset_cnt);
2825 		if (!fw_health->enabled) {
2826 			/* Make sure tmr_counter is set and visible to
2827 			 * bnxt_health_check() before setting enabled to true.
2828 			 */
2829 			smp_wmb();
2830 			fw_health->enabled = true;
2831 		}
2832 		goto async_event_process_exit;
2833 	}
2834 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2835 		netif_notice(bp, hw, bp->dev,
2836 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2837 			     data1, data2);
2838 		goto async_event_process_exit;
2839 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2840 		struct bnxt_rx_ring_info *rxr;
2841 		u16 grp_idx;
2842 
2843 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2844 			goto async_event_process_exit;
2845 
2846 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2847 			    BNXT_EVENT_RING_TYPE(data2), data1);
2848 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2849 			goto async_event_process_exit;
2850 
2851 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2852 		if (grp_idx == INVALID_HW_RING_ID) {
2853 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2854 				    data1);
2855 			goto async_event_process_exit;
2856 		}
2857 		rxr = bp->bnapi[grp_idx]->rx_ring;
2858 		bnxt_sched_reset_rxr(bp, rxr);
2859 		goto async_event_process_exit;
2860 	}
2861 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2862 		struct bnxt_fw_health *fw_health = bp->fw_health;
2863 
2864 		netif_notice(bp, hw, bp->dev,
2865 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2866 			     data1, data2);
2867 		if (fw_health) {
2868 			fw_health->echo_req_data1 = data1;
2869 			fw_health->echo_req_data2 = data2;
2870 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2871 			break;
2872 		}
2873 		goto async_event_process_exit;
2874 	}
2875 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2876 		bnxt_ptp_pps_event(bp, data1, data2);
2877 		goto async_event_process_exit;
2878 	}
2879 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2880 		if (bnxt_event_error_report(bp, data1, data2))
2881 			break;
2882 		goto async_event_process_exit;
2883 	}
2884 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2885 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2886 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2887 			if (BNXT_PTP_USE_RTC(bp)) {
2888 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2889 				unsigned long flags;
2890 				u64 ns;
2891 
2892 				if (!ptp)
2893 					goto async_event_process_exit;
2894 
2895 				bnxt_ptp_update_current_time(bp);
2896 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2897 				       BNXT_PHC_BITS) | ptp->current_time);
2898 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2899 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2900 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2901 			}
2902 			break;
2903 		}
2904 		goto async_event_process_exit;
2905 	}
2906 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2907 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2908 
2909 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2910 		goto async_event_process_exit;
2911 	}
2912 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2913 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2914 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2915 
2916 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2917 		goto async_event_process_exit;
2918 	}
2919 	default:
2920 		goto async_event_process_exit;
2921 	}
2922 	__bnxt_queue_sp_work(bp);
2923 async_event_process_exit:
2924 	bnxt_ulp_async_events(bp, cmpl);
2925 	return 0;
2926 }
2927 
2928 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2929 {
2930 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2931 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2932 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2933 				(struct hwrm_fwd_req_cmpl *)txcmp;
2934 
2935 	switch (cmpl_type) {
2936 	case CMPL_BASE_TYPE_HWRM_DONE:
2937 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2938 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2939 		break;
2940 
2941 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2942 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2943 
2944 		if ((vf_id < bp->pf.first_vf_id) ||
2945 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2946 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2947 				   vf_id);
2948 			return -EINVAL;
2949 		}
2950 
2951 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2952 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2953 		break;
2954 
2955 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2956 		bnxt_async_event_process(bp,
2957 					 (struct hwrm_async_event_cmpl *)txcmp);
2958 		break;
2959 
2960 	default:
2961 		break;
2962 	}
2963 
2964 	return 0;
2965 }
2966 
2967 static bool bnxt_vnic_is_active(struct bnxt *bp)
2968 {
2969 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2970 
2971 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2972 }
2973 
2974 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2975 {
2976 	struct bnxt_napi *bnapi = dev_instance;
2977 	struct bnxt *bp = bnapi->bp;
2978 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2979 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2980 
2981 	cpr->event_ctr++;
2982 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2983 	napi_schedule(&bnapi->napi);
2984 	return IRQ_HANDLED;
2985 }
2986 
2987 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2988 {
2989 	u32 raw_cons = cpr->cp_raw_cons;
2990 	u16 cons = RING_CMP(raw_cons);
2991 	struct tx_cmp *txcmp;
2992 
2993 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2994 
2995 	return TX_CMP_VALID(txcmp, raw_cons);
2996 }
2997 
2998 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2999 			    int budget)
3000 {
3001 	struct bnxt_napi *bnapi = cpr->bnapi;
3002 	u32 raw_cons = cpr->cp_raw_cons;
3003 	bool flush_xdp = false;
3004 	u32 cons;
3005 	int rx_pkts = 0;
3006 	u8 event = 0;
3007 	struct tx_cmp *txcmp;
3008 
3009 	cpr->has_more_work = 0;
3010 	cpr->had_work_done = 1;
3011 	while (1) {
3012 		u8 cmp_type;
3013 		int rc;
3014 
3015 		cons = RING_CMP(raw_cons);
3016 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3017 
3018 		if (!TX_CMP_VALID(txcmp, raw_cons))
3019 			break;
3020 
3021 		/* The valid test of the entry must be done first before
3022 		 * reading any further.
3023 		 */
3024 		dma_rmb();
3025 		cmp_type = TX_CMP_TYPE(txcmp);
3026 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3027 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3028 			u32 opaque = txcmp->tx_cmp_opaque;
3029 			struct bnxt_tx_ring_info *txr;
3030 			u16 tx_freed;
3031 
3032 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3033 			event |= BNXT_TX_CMP_EVENT;
3034 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3035 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3036 			else
3037 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3038 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3039 				   bp->tx_ring_mask;
3040 			/* return full budget so NAPI will complete. */
3041 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3042 				rx_pkts = budget;
3043 				raw_cons = NEXT_RAW_CMP(raw_cons);
3044 				if (budget)
3045 					cpr->has_more_work = 1;
3046 				break;
3047 			}
3048 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3049 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3050 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3051 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3052 			if (likely(budget))
3053 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3054 			else
3055 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3056 							   &event);
3057 			if (event & BNXT_REDIRECT_EVENT)
3058 				flush_xdp = true;
3059 			if (likely(rc >= 0))
3060 				rx_pkts += rc;
3061 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3062 			 * the NAPI budget.  Otherwise, we may potentially loop
3063 			 * here forever if we consistently cannot allocate
3064 			 * buffers.
3065 			 */
3066 			else if (rc == -ENOMEM && budget)
3067 				rx_pkts++;
3068 			else if (rc == -EBUSY)	/* partial completion */
3069 				break;
3070 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3071 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3072 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3073 			bnxt_hwrm_handler(bp, txcmp);
3074 		}
3075 		raw_cons = NEXT_RAW_CMP(raw_cons);
3076 
3077 		if (rx_pkts && rx_pkts == budget) {
3078 			cpr->has_more_work = 1;
3079 			break;
3080 		}
3081 	}
3082 
3083 	if (flush_xdp) {
3084 		xdp_do_flush();
3085 		event &= ~BNXT_REDIRECT_EVENT;
3086 	}
3087 
3088 	if (event & BNXT_TX_EVENT) {
3089 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3090 		u16 prod = txr->tx_prod;
3091 
3092 		/* Sync BD data before updating doorbell */
3093 		wmb();
3094 
3095 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3096 		event &= ~BNXT_TX_EVENT;
3097 	}
3098 
3099 	cpr->cp_raw_cons = raw_cons;
3100 	bnapi->events |= event;
3101 	return rx_pkts;
3102 }
3103 
3104 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3105 				  int budget)
3106 {
3107 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3108 		bnapi->tx_int(bp, bnapi, budget);
3109 
3110 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3111 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3112 
3113 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3114 		bnapi->events &= ~BNXT_RX_EVENT;
3115 	}
3116 	if (bnapi->events & BNXT_AGG_EVENT) {
3117 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3118 
3119 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3120 		bnapi->events &= ~BNXT_AGG_EVENT;
3121 	}
3122 }
3123 
3124 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3125 			  int budget)
3126 {
3127 	struct bnxt_napi *bnapi = cpr->bnapi;
3128 	int rx_pkts;
3129 
3130 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3131 
3132 	/* ACK completion ring before freeing tx ring and producing new
3133 	 * buffers in rx/agg rings to prevent overflowing the completion
3134 	 * ring.
3135 	 */
3136 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3137 
3138 	__bnxt_poll_work_done(bp, bnapi, budget);
3139 	return rx_pkts;
3140 }
3141 
3142 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3143 {
3144 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3145 	struct bnxt *bp = bnapi->bp;
3146 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3147 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3148 	struct tx_cmp *txcmp;
3149 	struct rx_cmp_ext *rxcmp1;
3150 	u32 cp_cons, tmp_raw_cons;
3151 	u32 raw_cons = cpr->cp_raw_cons;
3152 	bool flush_xdp = false;
3153 	u32 rx_pkts = 0;
3154 	u8 event = 0;
3155 
3156 	while (1) {
3157 		int rc;
3158 
3159 		cp_cons = RING_CMP(raw_cons);
3160 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3161 
3162 		if (!TX_CMP_VALID(txcmp, raw_cons))
3163 			break;
3164 
3165 		/* The valid test of the entry must be done first before
3166 		 * reading any further.
3167 		 */
3168 		dma_rmb();
3169 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3170 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3171 			cp_cons = RING_CMP(tmp_raw_cons);
3172 			rxcmp1 = (struct rx_cmp_ext *)
3173 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3174 
3175 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3176 				break;
3177 
3178 			/* force an error to recycle the buffer */
3179 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3180 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3181 
3182 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3183 			if (likely(rc == -EIO) && budget)
3184 				rx_pkts++;
3185 			else if (rc == -EBUSY)	/* partial completion */
3186 				break;
3187 			if (event & BNXT_REDIRECT_EVENT)
3188 				flush_xdp = true;
3189 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3190 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3191 			bnxt_hwrm_handler(bp, txcmp);
3192 		} else {
3193 			netdev_err(bp->dev,
3194 				   "Invalid completion received on special ring\n");
3195 		}
3196 		raw_cons = NEXT_RAW_CMP(raw_cons);
3197 
3198 		if (rx_pkts == budget)
3199 			break;
3200 	}
3201 
3202 	cpr->cp_raw_cons = raw_cons;
3203 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3204 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3205 
3206 	if (event & BNXT_AGG_EVENT)
3207 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3208 	if (flush_xdp)
3209 		xdp_do_flush();
3210 
3211 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3212 		napi_complete_done(napi, rx_pkts);
3213 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3214 	}
3215 	return rx_pkts;
3216 }
3217 
3218 static int bnxt_poll(struct napi_struct *napi, int budget)
3219 {
3220 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3221 	struct bnxt *bp = bnapi->bp;
3222 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3223 	int work_done = 0;
3224 
3225 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3226 		napi_complete(napi);
3227 		return 0;
3228 	}
3229 	while (1) {
3230 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3231 
3232 		if (work_done >= budget) {
3233 			if (!budget)
3234 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3235 			break;
3236 		}
3237 
3238 		if (!bnxt_has_work(bp, cpr)) {
3239 			if (napi_complete_done(napi, work_done))
3240 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3241 			break;
3242 		}
3243 	}
3244 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3245 		struct dim_sample dim_sample = {};
3246 
3247 		dim_update_sample(cpr->event_ctr,
3248 				  cpr->rx_packets,
3249 				  cpr->rx_bytes,
3250 				  &dim_sample);
3251 		net_dim(&cpr->dim, &dim_sample);
3252 	}
3253 	return work_done;
3254 }
3255 
3256 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3257 {
3258 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3259 	int i, work_done = 0;
3260 
3261 	for (i = 0; i < cpr->cp_ring_count; i++) {
3262 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3263 
3264 		if (cpr2->had_nqe_notify) {
3265 			work_done += __bnxt_poll_work(bp, cpr2,
3266 						      budget - work_done);
3267 			cpr->has_more_work |= cpr2->has_more_work;
3268 		}
3269 	}
3270 	return work_done;
3271 }
3272 
3273 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3274 				 u64 dbr_type, int budget)
3275 {
3276 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3277 	int i;
3278 
3279 	for (i = 0; i < cpr->cp_ring_count; i++) {
3280 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3281 		struct bnxt_db_info *db;
3282 
3283 		if (cpr2->had_work_done) {
3284 			u32 tgl = 0;
3285 
3286 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3287 				cpr2->had_nqe_notify = 0;
3288 				tgl = cpr2->toggle;
3289 			}
3290 			db = &cpr2->cp_db;
3291 			bnxt_writeq(bp,
3292 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3293 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3294 				    db->doorbell);
3295 			cpr2->had_work_done = 0;
3296 		}
3297 	}
3298 	__bnxt_poll_work_done(bp, bnapi, budget);
3299 }
3300 
3301 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3302 {
3303 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3304 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3305 	struct bnxt_cp_ring_info *cpr_rx;
3306 	u32 raw_cons = cpr->cp_raw_cons;
3307 	struct bnxt *bp = bnapi->bp;
3308 	struct nqe_cn *nqcmp;
3309 	int work_done = 0;
3310 	u32 cons;
3311 
3312 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3313 		napi_complete(napi);
3314 		return 0;
3315 	}
3316 	if (cpr->has_more_work) {
3317 		cpr->has_more_work = 0;
3318 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3319 	}
3320 	while (1) {
3321 		u16 type;
3322 
3323 		cons = RING_CMP(raw_cons);
3324 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3325 
3326 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3327 			if (cpr->has_more_work)
3328 				break;
3329 
3330 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3331 					     budget);
3332 			cpr->cp_raw_cons = raw_cons;
3333 			if (napi_complete_done(napi, work_done))
3334 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3335 						  cpr->cp_raw_cons);
3336 			goto poll_done;
3337 		}
3338 
3339 		/* The valid test of the entry must be done first before
3340 		 * reading any further.
3341 		 */
3342 		dma_rmb();
3343 
3344 		type = le16_to_cpu(nqcmp->type);
3345 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3346 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3347 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3348 			struct bnxt_cp_ring_info *cpr2;
3349 
3350 			/* No more budget for RX work */
3351 			if (budget && work_done >= budget &&
3352 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3353 				break;
3354 
3355 			idx = BNXT_NQ_HDL_IDX(idx);
3356 			cpr2 = &cpr->cp_ring_arr[idx];
3357 			cpr2->had_nqe_notify = 1;
3358 			cpr2->toggle = NQE_CN_TOGGLE(type);
3359 			work_done += __bnxt_poll_work(bp, cpr2,
3360 						      budget - work_done);
3361 			cpr->has_more_work |= cpr2->has_more_work;
3362 		} else {
3363 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3364 		}
3365 		raw_cons = NEXT_RAW_CMP(raw_cons);
3366 	}
3367 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3368 	if (raw_cons != cpr->cp_raw_cons) {
3369 		cpr->cp_raw_cons = raw_cons;
3370 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3371 	}
3372 poll_done:
3373 	cpr_rx = &cpr->cp_ring_arr[0];
3374 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3375 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3376 		struct dim_sample dim_sample = {};
3377 
3378 		dim_update_sample(cpr->event_ctr,
3379 				  cpr_rx->rx_packets,
3380 				  cpr_rx->rx_bytes,
3381 				  &dim_sample);
3382 		net_dim(&cpr->dim, &dim_sample);
3383 	}
3384 	return work_done;
3385 }
3386 
3387 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3388 				       struct bnxt_tx_ring_info *txr, int idx)
3389 {
3390 	int i, max_idx;
3391 	struct pci_dev *pdev = bp->pdev;
3392 
3393 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3394 
3395 	for (i = 0; i < max_idx;) {
3396 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3397 		struct sk_buff *skb;
3398 		int j, last;
3399 
3400 		if (idx  < bp->tx_nr_rings_xdp &&
3401 		    tx_buf->action == XDP_REDIRECT) {
3402 			dma_unmap_single(&pdev->dev,
3403 					 dma_unmap_addr(tx_buf, mapping),
3404 					 dma_unmap_len(tx_buf, len),
3405 					 DMA_TO_DEVICE);
3406 			xdp_return_frame(tx_buf->xdpf);
3407 			tx_buf->action = 0;
3408 			tx_buf->xdpf = NULL;
3409 			i++;
3410 			continue;
3411 		}
3412 
3413 		skb = tx_buf->skb;
3414 		if (!skb) {
3415 			i++;
3416 			continue;
3417 		}
3418 
3419 		tx_buf->skb = NULL;
3420 
3421 		if (tx_buf->is_push) {
3422 			dev_kfree_skb(skb);
3423 			i += 2;
3424 			continue;
3425 		}
3426 
3427 		dma_unmap_single(&pdev->dev,
3428 				 dma_unmap_addr(tx_buf, mapping),
3429 				 skb_headlen(skb),
3430 				 DMA_TO_DEVICE);
3431 
3432 		last = tx_buf->nr_frags;
3433 		i += 2;
3434 		for (j = 0; j < last; j++, i++) {
3435 			int ring_idx = i & bp->tx_ring_mask;
3436 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3437 
3438 			tx_buf = &txr->tx_buf_ring[ring_idx];
3439 			netmem_dma_unmap_page_attrs(&pdev->dev,
3440 						    dma_unmap_addr(tx_buf,
3441 								   mapping),
3442 						    skb_frag_size(frag),
3443 						    DMA_TO_DEVICE, 0);
3444 		}
3445 		dev_kfree_skb(skb);
3446 	}
3447 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3448 }
3449 
3450 static void bnxt_free_tx_skbs(struct bnxt *bp)
3451 {
3452 	int i;
3453 
3454 	if (!bp->tx_ring)
3455 		return;
3456 
3457 	for (i = 0; i < bp->tx_nr_rings; i++) {
3458 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3459 
3460 		if (!txr->tx_buf_ring)
3461 			continue;
3462 
3463 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3464 	}
3465 
3466 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3467 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3468 }
3469 
3470 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3471 {
3472 	int i, max_idx;
3473 
3474 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3475 
3476 	for (i = 0; i < max_idx; i++) {
3477 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3478 		void *data = rx_buf->data;
3479 
3480 		if (!data)
3481 			continue;
3482 
3483 		rx_buf->data = NULL;
3484 		if (BNXT_RX_PAGE_MODE(bp))
3485 			page_pool_recycle_direct(rxr->page_pool, data);
3486 		else
3487 			page_pool_free_va(rxr->head_pool, data, true);
3488 	}
3489 }
3490 
3491 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3492 {
3493 	int i, max_idx;
3494 
3495 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3496 
3497 	for (i = 0; i < max_idx; i++) {
3498 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3499 		netmem_ref netmem = rx_agg_buf->netmem;
3500 
3501 		if (!netmem)
3502 			continue;
3503 
3504 		rx_agg_buf->netmem = 0;
3505 		__clear_bit(i, rxr->rx_agg_bmap);
3506 
3507 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3508 	}
3509 }
3510 
3511 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3512 					struct bnxt_rx_ring_info *rxr)
3513 {
3514 	int i;
3515 
3516 	for (i = 0; i < bp->max_tpa; i++) {
3517 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3518 		u8 *data = tpa_info->data;
3519 
3520 		if (!data)
3521 			continue;
3522 
3523 		tpa_info->data = NULL;
3524 		page_pool_free_va(rxr->head_pool, data, false);
3525 	}
3526 }
3527 
3528 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3529 				       struct bnxt_rx_ring_info *rxr)
3530 {
3531 	struct bnxt_tpa_idx_map *map;
3532 
3533 	if (!rxr->rx_tpa)
3534 		goto skip_rx_tpa_free;
3535 
3536 	bnxt_free_one_tpa_info_data(bp, rxr);
3537 
3538 skip_rx_tpa_free:
3539 	if (!rxr->rx_buf_ring)
3540 		goto skip_rx_buf_free;
3541 
3542 	bnxt_free_one_rx_ring(bp, rxr);
3543 
3544 skip_rx_buf_free:
3545 	if (!rxr->rx_agg_ring)
3546 		goto skip_rx_agg_free;
3547 
3548 	bnxt_free_one_rx_agg_ring(bp, rxr);
3549 
3550 skip_rx_agg_free:
3551 	map = rxr->rx_tpa_idx_map;
3552 	if (map)
3553 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3554 }
3555 
3556 static void bnxt_free_rx_skbs(struct bnxt *bp)
3557 {
3558 	int i;
3559 
3560 	if (!bp->rx_ring)
3561 		return;
3562 
3563 	for (i = 0; i < bp->rx_nr_rings; i++)
3564 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3565 }
3566 
3567 static void bnxt_free_skbs(struct bnxt *bp)
3568 {
3569 	bnxt_free_tx_skbs(bp);
3570 	bnxt_free_rx_skbs(bp);
3571 }
3572 
3573 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3574 {
3575 	u8 init_val = ctxm->init_value;
3576 	u16 offset = ctxm->init_offset;
3577 	u8 *p2 = p;
3578 	int i;
3579 
3580 	if (!init_val)
3581 		return;
3582 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3583 		memset(p, init_val, len);
3584 		return;
3585 	}
3586 	for (i = 0; i < len; i += ctxm->entry_size)
3587 		*(p2 + i + offset) = init_val;
3588 }
3589 
3590 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3591 			       void *buf, size_t offset, size_t head,
3592 			       size_t tail)
3593 {
3594 	int i, head_page, start_idx, source_offset;
3595 	size_t len, rem_len, total_len, max_bytes;
3596 
3597 	head_page = head / rmem->page_size;
3598 	source_offset = head % rmem->page_size;
3599 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3600 	if (!total_len)
3601 		total_len = MAX_CTX_BYTES;
3602 	start_idx = head_page % MAX_CTX_PAGES;
3603 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3604 		    source_offset;
3605 	total_len = min(total_len, max_bytes);
3606 	rem_len = total_len;
3607 
3608 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3609 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3610 		if (buf)
3611 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3612 			       len);
3613 		offset += len;
3614 		rem_len -= len;
3615 	}
3616 	return total_len;
3617 }
3618 
3619 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3620 {
3621 	struct pci_dev *pdev = bp->pdev;
3622 	int i;
3623 
3624 	if (!rmem->pg_arr)
3625 		goto skip_pages;
3626 
3627 	for (i = 0; i < rmem->nr_pages; i++) {
3628 		if (!rmem->pg_arr[i])
3629 			continue;
3630 
3631 		dma_free_coherent(&pdev->dev, rmem->page_size,
3632 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3633 
3634 		rmem->pg_arr[i] = NULL;
3635 	}
3636 skip_pages:
3637 	if (rmem->pg_tbl) {
3638 		size_t pg_tbl_size = rmem->nr_pages * 8;
3639 
3640 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3641 			pg_tbl_size = rmem->page_size;
3642 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3643 				  rmem->pg_tbl, rmem->pg_tbl_map);
3644 		rmem->pg_tbl = NULL;
3645 	}
3646 	if (rmem->vmem_size && *rmem->vmem) {
3647 		vfree(*rmem->vmem);
3648 		*rmem->vmem = NULL;
3649 	}
3650 }
3651 
3652 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3653 {
3654 	struct pci_dev *pdev = bp->pdev;
3655 	u64 valid_bit = 0;
3656 	int i;
3657 
3658 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3659 		valid_bit = PTU_PTE_VALID;
3660 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3661 		size_t pg_tbl_size = rmem->nr_pages * 8;
3662 
3663 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3664 			pg_tbl_size = rmem->page_size;
3665 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3666 						  &rmem->pg_tbl_map,
3667 						  GFP_KERNEL);
3668 		if (!rmem->pg_tbl)
3669 			return -ENOMEM;
3670 	}
3671 
3672 	for (i = 0; i < rmem->nr_pages; i++) {
3673 		u64 extra_bits = valid_bit;
3674 
3675 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3676 						     rmem->page_size,
3677 						     &rmem->dma_arr[i],
3678 						     GFP_KERNEL);
3679 		if (!rmem->pg_arr[i])
3680 			return -ENOMEM;
3681 
3682 		if (rmem->ctx_mem)
3683 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3684 					  rmem->page_size);
3685 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3686 			if (i == rmem->nr_pages - 2 &&
3687 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3688 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3689 			else if (i == rmem->nr_pages - 1 &&
3690 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3691 				extra_bits |= PTU_PTE_LAST;
3692 			rmem->pg_tbl[i] =
3693 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3694 		}
3695 	}
3696 
3697 	if (rmem->vmem_size) {
3698 		*rmem->vmem = vzalloc(rmem->vmem_size);
3699 		if (!(*rmem->vmem))
3700 			return -ENOMEM;
3701 	}
3702 	return 0;
3703 }
3704 
3705 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3706 				   struct bnxt_rx_ring_info *rxr)
3707 {
3708 	int i;
3709 
3710 	kfree(rxr->rx_tpa_idx_map);
3711 	rxr->rx_tpa_idx_map = NULL;
3712 	if (rxr->rx_tpa) {
3713 		for (i = 0; i < bp->max_tpa; i++) {
3714 			kfree(rxr->rx_tpa[i].agg_arr);
3715 			rxr->rx_tpa[i].agg_arr = NULL;
3716 		}
3717 	}
3718 	kfree(rxr->rx_tpa);
3719 	rxr->rx_tpa = NULL;
3720 }
3721 
3722 static void bnxt_free_tpa_info(struct bnxt *bp)
3723 {
3724 	int i;
3725 
3726 	for (i = 0; i < bp->rx_nr_rings; i++) {
3727 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3728 
3729 		bnxt_free_one_tpa_info(bp, rxr);
3730 	}
3731 }
3732 
3733 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3734 				   struct bnxt_rx_ring_info *rxr)
3735 {
3736 	struct rx_agg_cmp *agg;
3737 	int i;
3738 
3739 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3740 			      GFP_KERNEL);
3741 	if (!rxr->rx_tpa)
3742 		return -ENOMEM;
3743 
3744 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3745 		return 0;
3746 	for (i = 0; i < bp->max_tpa; i++) {
3747 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3748 		if (!agg)
3749 			return -ENOMEM;
3750 		rxr->rx_tpa[i].agg_arr = agg;
3751 	}
3752 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3753 				      GFP_KERNEL);
3754 	if (!rxr->rx_tpa_idx_map)
3755 		return -ENOMEM;
3756 
3757 	return 0;
3758 }
3759 
3760 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3761 {
3762 	int i, rc;
3763 
3764 	bp->max_tpa = MAX_TPA;
3765 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3766 		if (!bp->max_tpa_v2)
3767 			return 0;
3768 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3769 	}
3770 
3771 	for (i = 0; i < bp->rx_nr_rings; i++) {
3772 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3773 
3774 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3775 		if (rc)
3776 			return rc;
3777 	}
3778 	return 0;
3779 }
3780 
3781 static void bnxt_free_rx_rings(struct bnxt *bp)
3782 {
3783 	int i;
3784 
3785 	if (!bp->rx_ring)
3786 		return;
3787 
3788 	bnxt_free_tpa_info(bp);
3789 	for (i = 0; i < bp->rx_nr_rings; i++) {
3790 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3791 		struct bnxt_ring_struct *ring;
3792 
3793 		if (rxr->xdp_prog)
3794 			bpf_prog_put(rxr->xdp_prog);
3795 
3796 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3797 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3798 
3799 		page_pool_destroy(rxr->page_pool);
3800 		if (bnxt_separate_head_pool(rxr))
3801 			page_pool_destroy(rxr->head_pool);
3802 		rxr->page_pool = rxr->head_pool = NULL;
3803 
3804 		kfree(rxr->rx_agg_bmap);
3805 		rxr->rx_agg_bmap = NULL;
3806 
3807 		ring = &rxr->rx_ring_struct;
3808 		bnxt_free_ring(bp, &ring->ring_mem);
3809 
3810 		ring = &rxr->rx_agg_ring_struct;
3811 		bnxt_free_ring(bp, &ring->ring_mem);
3812 	}
3813 }
3814 
3815 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3816 				   struct bnxt_rx_ring_info *rxr,
3817 				   int numa_node)
3818 {
3819 	const unsigned int agg_size_fac = PAGE_SIZE / BNXT_RX_PAGE_SIZE;
3820 	const unsigned int rx_size_fac = PAGE_SIZE / SZ_4K;
3821 	struct page_pool_params pp = { 0 };
3822 	struct page_pool *pool;
3823 
3824 	pp.pool_size = bp->rx_agg_ring_size / agg_size_fac;
3825 	if (BNXT_RX_PAGE_MODE(bp))
3826 		pp.pool_size += bp->rx_ring_size / rx_size_fac;
3827 	pp.nid = numa_node;
3828 	pp.netdev = bp->dev;
3829 	pp.dev = &bp->pdev->dev;
3830 	pp.dma_dir = bp->rx_dir;
3831 	pp.max_len = PAGE_SIZE;
3832 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3833 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3834 	pp.queue_idx = rxr->bnapi->index;
3835 
3836 	pool = page_pool_create(&pp);
3837 	if (IS_ERR(pool))
3838 		return PTR_ERR(pool);
3839 	rxr->page_pool = pool;
3840 
3841 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3842 	if (bnxt_separate_head_pool(rxr)) {
3843 		pp.pool_size = min(bp->rx_ring_size / rx_size_fac, 1024);
3844 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3845 		pool = page_pool_create(&pp);
3846 		if (IS_ERR(pool))
3847 			goto err_destroy_pp;
3848 	}
3849 	rxr->head_pool = pool;
3850 
3851 	return 0;
3852 
3853 err_destroy_pp:
3854 	page_pool_destroy(rxr->page_pool);
3855 	rxr->page_pool = NULL;
3856 	return PTR_ERR(pool);
3857 }
3858 
3859 static void bnxt_enable_rx_page_pool(struct bnxt_rx_ring_info *rxr)
3860 {
3861 	page_pool_enable_direct_recycling(rxr->head_pool, &rxr->bnapi->napi);
3862 	page_pool_enable_direct_recycling(rxr->page_pool, &rxr->bnapi->napi);
3863 }
3864 
3865 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3866 {
3867 	u16 mem_size;
3868 
3869 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3870 	mem_size = rxr->rx_agg_bmap_size / 8;
3871 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3872 	if (!rxr->rx_agg_bmap)
3873 		return -ENOMEM;
3874 
3875 	return 0;
3876 }
3877 
3878 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3879 {
3880 	int numa_node = dev_to_node(&bp->pdev->dev);
3881 	int i, rc = 0, agg_rings = 0, cpu;
3882 
3883 	if (!bp->rx_ring)
3884 		return -ENOMEM;
3885 
3886 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3887 		agg_rings = 1;
3888 
3889 	for (i = 0; i < bp->rx_nr_rings; i++) {
3890 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3891 		struct bnxt_ring_struct *ring;
3892 		int cpu_node;
3893 
3894 		ring = &rxr->rx_ring_struct;
3895 
3896 		cpu = cpumask_local_spread(i, numa_node);
3897 		cpu_node = cpu_to_node(cpu);
3898 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3899 			   i, cpu_node);
3900 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3901 		if (rc)
3902 			return rc;
3903 		bnxt_enable_rx_page_pool(rxr);
3904 
3905 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3906 		if (rc < 0)
3907 			return rc;
3908 
3909 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3910 						MEM_TYPE_PAGE_POOL,
3911 						rxr->page_pool);
3912 		if (rc) {
3913 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3914 			return rc;
3915 		}
3916 
3917 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3918 		if (rc)
3919 			return rc;
3920 
3921 		ring->grp_idx = i;
3922 		if (agg_rings) {
3923 			ring = &rxr->rx_agg_ring_struct;
3924 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3925 			if (rc)
3926 				return rc;
3927 
3928 			ring->grp_idx = i;
3929 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3930 			if (rc)
3931 				return rc;
3932 		}
3933 	}
3934 	if (bp->flags & BNXT_FLAG_TPA)
3935 		rc = bnxt_alloc_tpa_info(bp);
3936 	return rc;
3937 }
3938 
3939 static void bnxt_free_tx_rings(struct bnxt *bp)
3940 {
3941 	int i;
3942 	struct pci_dev *pdev = bp->pdev;
3943 
3944 	if (!bp->tx_ring)
3945 		return;
3946 
3947 	for (i = 0; i < bp->tx_nr_rings; i++) {
3948 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3949 		struct bnxt_ring_struct *ring;
3950 
3951 		if (txr->tx_push) {
3952 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3953 					  txr->tx_push, txr->tx_push_mapping);
3954 			txr->tx_push = NULL;
3955 		}
3956 
3957 		ring = &txr->tx_ring_struct;
3958 
3959 		bnxt_free_ring(bp, &ring->ring_mem);
3960 	}
3961 }
3962 
3963 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3964 	((tc) * (bp)->tx_nr_rings_per_tc)
3965 
3966 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3967 	((tx) % (bp)->tx_nr_rings_per_tc)
3968 
3969 #define BNXT_RING_TO_TC(bp, tx)		\
3970 	((tx) / (bp)->tx_nr_rings_per_tc)
3971 
3972 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3973 {
3974 	int i, j, rc;
3975 	struct pci_dev *pdev = bp->pdev;
3976 
3977 	bp->tx_push_size = 0;
3978 	if (bp->tx_push_thresh) {
3979 		int push_size;
3980 
3981 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3982 					bp->tx_push_thresh);
3983 
3984 		if (push_size > 256) {
3985 			push_size = 0;
3986 			bp->tx_push_thresh = 0;
3987 		}
3988 
3989 		bp->tx_push_size = push_size;
3990 	}
3991 
3992 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3993 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3994 		struct bnxt_ring_struct *ring;
3995 		u8 qidx;
3996 
3997 		ring = &txr->tx_ring_struct;
3998 
3999 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4000 		if (rc)
4001 			return rc;
4002 
4003 		ring->grp_idx = txr->bnapi->index;
4004 		if (bp->tx_push_size) {
4005 			dma_addr_t mapping;
4006 
4007 			/* One pre-allocated DMA buffer to backup
4008 			 * TX push operation
4009 			 */
4010 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
4011 						bp->tx_push_size,
4012 						&txr->tx_push_mapping,
4013 						GFP_KERNEL);
4014 
4015 			if (!txr->tx_push)
4016 				return -ENOMEM;
4017 
4018 			mapping = txr->tx_push_mapping +
4019 				sizeof(struct tx_push_bd);
4020 			txr->data_mapping = cpu_to_le64(mapping);
4021 		}
4022 		qidx = bp->tc_to_qidx[j];
4023 		ring->queue_id = bp->q_info[qidx].queue_id;
4024 		spin_lock_init(&txr->xdp_tx_lock);
4025 		if (i < bp->tx_nr_rings_xdp)
4026 			continue;
4027 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4028 			j++;
4029 	}
4030 	return 0;
4031 }
4032 
4033 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4034 {
4035 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4036 
4037 	kfree(cpr->cp_desc_ring);
4038 	cpr->cp_desc_ring = NULL;
4039 	ring->ring_mem.pg_arr = NULL;
4040 	kfree(cpr->cp_desc_mapping);
4041 	cpr->cp_desc_mapping = NULL;
4042 	ring->ring_mem.dma_arr = NULL;
4043 }
4044 
4045 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4046 {
4047 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
4048 	if (!cpr->cp_desc_ring)
4049 		return -ENOMEM;
4050 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
4051 				       GFP_KERNEL);
4052 	if (!cpr->cp_desc_mapping)
4053 		return -ENOMEM;
4054 	return 0;
4055 }
4056 
4057 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4058 {
4059 	int i;
4060 
4061 	if (!bp->bnapi)
4062 		return;
4063 	for (i = 0; i < bp->cp_nr_rings; i++) {
4064 		struct bnxt_napi *bnapi = bp->bnapi[i];
4065 
4066 		if (!bnapi)
4067 			continue;
4068 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4069 	}
4070 }
4071 
4072 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4073 {
4074 	int i, n = bp->cp_nr_pages;
4075 
4076 	for (i = 0; i < bp->cp_nr_rings; i++) {
4077 		struct bnxt_napi *bnapi = bp->bnapi[i];
4078 		int rc;
4079 
4080 		if (!bnapi)
4081 			continue;
4082 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4083 		if (rc)
4084 			return rc;
4085 	}
4086 	return 0;
4087 }
4088 
4089 static void bnxt_free_cp_rings(struct bnxt *bp)
4090 {
4091 	int i;
4092 
4093 	if (!bp->bnapi)
4094 		return;
4095 
4096 	for (i = 0; i < bp->cp_nr_rings; i++) {
4097 		struct bnxt_napi *bnapi = bp->bnapi[i];
4098 		struct bnxt_cp_ring_info *cpr;
4099 		struct bnxt_ring_struct *ring;
4100 		int j;
4101 
4102 		if (!bnapi)
4103 			continue;
4104 
4105 		cpr = &bnapi->cp_ring;
4106 		ring = &cpr->cp_ring_struct;
4107 
4108 		bnxt_free_ring(bp, &ring->ring_mem);
4109 
4110 		if (!cpr->cp_ring_arr)
4111 			continue;
4112 
4113 		for (j = 0; j < cpr->cp_ring_count; j++) {
4114 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4115 
4116 			ring = &cpr2->cp_ring_struct;
4117 			bnxt_free_ring(bp, &ring->ring_mem);
4118 			bnxt_free_cp_arrays(cpr2);
4119 		}
4120 		kfree(cpr->cp_ring_arr);
4121 		cpr->cp_ring_arr = NULL;
4122 		cpr->cp_ring_count = 0;
4123 	}
4124 }
4125 
4126 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4127 				  struct bnxt_cp_ring_info *cpr)
4128 {
4129 	struct bnxt_ring_mem_info *rmem;
4130 	struct bnxt_ring_struct *ring;
4131 	int rc;
4132 
4133 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4134 	if (rc) {
4135 		bnxt_free_cp_arrays(cpr);
4136 		return -ENOMEM;
4137 	}
4138 	ring = &cpr->cp_ring_struct;
4139 	rmem = &ring->ring_mem;
4140 	rmem->nr_pages = bp->cp_nr_pages;
4141 	rmem->page_size = HW_CMPD_RING_SIZE;
4142 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4143 	rmem->dma_arr = cpr->cp_desc_mapping;
4144 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4145 	rc = bnxt_alloc_ring(bp, rmem);
4146 	if (rc) {
4147 		bnxt_free_ring(bp, rmem);
4148 		bnxt_free_cp_arrays(cpr);
4149 	}
4150 	return rc;
4151 }
4152 
4153 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4154 {
4155 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4156 	int i, j, rc, ulp_msix;
4157 	int tcs = bp->num_tc;
4158 
4159 	if (!tcs)
4160 		tcs = 1;
4161 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4162 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4163 		struct bnxt_napi *bnapi = bp->bnapi[i];
4164 		struct bnxt_cp_ring_info *cpr, *cpr2;
4165 		struct bnxt_ring_struct *ring;
4166 		int cp_count = 0, k;
4167 		int rx = 0, tx = 0;
4168 
4169 		if (!bnapi)
4170 			continue;
4171 
4172 		cpr = &bnapi->cp_ring;
4173 		cpr->bnapi = bnapi;
4174 		ring = &cpr->cp_ring_struct;
4175 
4176 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4177 		if (rc)
4178 			return rc;
4179 
4180 		ring->map_idx = ulp_msix + i;
4181 
4182 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4183 			continue;
4184 
4185 		if (i < bp->rx_nr_rings) {
4186 			cp_count++;
4187 			rx = 1;
4188 		}
4189 		if (i < bp->tx_nr_rings_xdp) {
4190 			cp_count++;
4191 			tx = 1;
4192 		} else if ((sh && i < bp->tx_nr_rings) ||
4193 			 (!sh && i >= bp->rx_nr_rings)) {
4194 			cp_count += tcs;
4195 			tx = 1;
4196 		}
4197 
4198 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4199 					   GFP_KERNEL);
4200 		if (!cpr->cp_ring_arr)
4201 			return -ENOMEM;
4202 		cpr->cp_ring_count = cp_count;
4203 
4204 		for (k = 0; k < cp_count; k++) {
4205 			cpr2 = &cpr->cp_ring_arr[k];
4206 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4207 			if (rc)
4208 				return rc;
4209 			cpr2->bnapi = bnapi;
4210 			cpr2->sw_stats = cpr->sw_stats;
4211 			cpr2->cp_idx = k;
4212 			if (!k && rx) {
4213 				bp->rx_ring[i].rx_cpr = cpr2;
4214 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4215 			} else {
4216 				int n, tc = k - rx;
4217 
4218 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4219 				bp->tx_ring[n].tx_cpr = cpr2;
4220 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4221 			}
4222 		}
4223 		if (tx)
4224 			j++;
4225 	}
4226 	return 0;
4227 }
4228 
4229 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4230 				     struct bnxt_rx_ring_info *rxr)
4231 {
4232 	struct bnxt_ring_mem_info *rmem;
4233 	struct bnxt_ring_struct *ring;
4234 
4235 	ring = &rxr->rx_ring_struct;
4236 	rmem = &ring->ring_mem;
4237 	rmem->nr_pages = bp->rx_nr_pages;
4238 	rmem->page_size = HW_RXBD_RING_SIZE;
4239 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4240 	rmem->dma_arr = rxr->rx_desc_mapping;
4241 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4242 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4243 
4244 	ring = &rxr->rx_agg_ring_struct;
4245 	rmem = &ring->ring_mem;
4246 	rmem->nr_pages = bp->rx_agg_nr_pages;
4247 	rmem->page_size = HW_RXBD_RING_SIZE;
4248 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4249 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4250 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4251 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4252 }
4253 
4254 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4255 				      struct bnxt_rx_ring_info *rxr)
4256 {
4257 	struct bnxt_ring_mem_info *rmem;
4258 	struct bnxt_ring_struct *ring;
4259 	int i;
4260 
4261 	rxr->page_pool->p.napi = NULL;
4262 	rxr->page_pool = NULL;
4263 	rxr->head_pool->p.napi = NULL;
4264 	rxr->head_pool = NULL;
4265 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4266 
4267 	ring = &rxr->rx_ring_struct;
4268 	rmem = &ring->ring_mem;
4269 	rmem->pg_tbl = NULL;
4270 	rmem->pg_tbl_map = 0;
4271 	for (i = 0; i < rmem->nr_pages; i++) {
4272 		rmem->pg_arr[i] = NULL;
4273 		rmem->dma_arr[i] = 0;
4274 	}
4275 	*rmem->vmem = NULL;
4276 
4277 	ring = &rxr->rx_agg_ring_struct;
4278 	rmem = &ring->ring_mem;
4279 	rmem->pg_tbl = NULL;
4280 	rmem->pg_tbl_map = 0;
4281 	for (i = 0; i < rmem->nr_pages; i++) {
4282 		rmem->pg_arr[i] = NULL;
4283 		rmem->dma_arr[i] = 0;
4284 	}
4285 	*rmem->vmem = NULL;
4286 }
4287 
4288 static void bnxt_init_ring_struct(struct bnxt *bp)
4289 {
4290 	int i, j;
4291 
4292 	for (i = 0; i < bp->cp_nr_rings; i++) {
4293 		struct bnxt_napi *bnapi = bp->bnapi[i];
4294 		struct bnxt_ring_mem_info *rmem;
4295 		struct bnxt_cp_ring_info *cpr;
4296 		struct bnxt_rx_ring_info *rxr;
4297 		struct bnxt_tx_ring_info *txr;
4298 		struct bnxt_ring_struct *ring;
4299 
4300 		if (!bnapi)
4301 			continue;
4302 
4303 		cpr = &bnapi->cp_ring;
4304 		ring = &cpr->cp_ring_struct;
4305 		rmem = &ring->ring_mem;
4306 		rmem->nr_pages = bp->cp_nr_pages;
4307 		rmem->page_size = HW_CMPD_RING_SIZE;
4308 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4309 		rmem->dma_arr = cpr->cp_desc_mapping;
4310 		rmem->vmem_size = 0;
4311 
4312 		rxr = bnapi->rx_ring;
4313 		if (!rxr)
4314 			goto skip_rx;
4315 
4316 		ring = &rxr->rx_ring_struct;
4317 		rmem = &ring->ring_mem;
4318 		rmem->nr_pages = bp->rx_nr_pages;
4319 		rmem->page_size = HW_RXBD_RING_SIZE;
4320 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4321 		rmem->dma_arr = rxr->rx_desc_mapping;
4322 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4323 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4324 
4325 		ring = &rxr->rx_agg_ring_struct;
4326 		rmem = &ring->ring_mem;
4327 		rmem->nr_pages = bp->rx_agg_nr_pages;
4328 		rmem->page_size = HW_RXBD_RING_SIZE;
4329 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4330 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4331 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4332 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4333 
4334 skip_rx:
4335 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4336 			ring = &txr->tx_ring_struct;
4337 			rmem = &ring->ring_mem;
4338 			rmem->nr_pages = bp->tx_nr_pages;
4339 			rmem->page_size = HW_TXBD_RING_SIZE;
4340 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4341 			rmem->dma_arr = txr->tx_desc_mapping;
4342 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4343 			rmem->vmem = (void **)&txr->tx_buf_ring;
4344 		}
4345 	}
4346 }
4347 
4348 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4349 {
4350 	int i;
4351 	u32 prod;
4352 	struct rx_bd **rx_buf_ring;
4353 
4354 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4355 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4356 		int j;
4357 		struct rx_bd *rxbd;
4358 
4359 		rxbd = rx_buf_ring[i];
4360 		if (!rxbd)
4361 			continue;
4362 
4363 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4364 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4365 			rxbd->rx_bd_opaque = prod;
4366 		}
4367 	}
4368 }
4369 
4370 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4371 				       struct bnxt_rx_ring_info *rxr,
4372 				       int ring_nr)
4373 {
4374 	u32 prod;
4375 	int i;
4376 
4377 	prod = rxr->rx_prod;
4378 	for (i = 0; i < bp->rx_ring_size; i++) {
4379 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4380 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4381 				    ring_nr, i, bp->rx_ring_size);
4382 			break;
4383 		}
4384 		prod = NEXT_RX(prod);
4385 	}
4386 	rxr->rx_prod = prod;
4387 }
4388 
4389 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4390 					  struct bnxt_rx_ring_info *rxr,
4391 					  int ring_nr)
4392 {
4393 	u32 prod;
4394 	int i;
4395 
4396 	prod = rxr->rx_agg_prod;
4397 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4398 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4399 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4400 				    ring_nr, i, bp->rx_ring_size);
4401 			break;
4402 		}
4403 		prod = NEXT_RX_AGG(prod);
4404 	}
4405 	rxr->rx_agg_prod = prod;
4406 }
4407 
4408 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4409 					struct bnxt_rx_ring_info *rxr)
4410 {
4411 	dma_addr_t mapping;
4412 	u8 *data;
4413 	int i;
4414 
4415 	for (i = 0; i < bp->max_tpa; i++) {
4416 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4417 					    GFP_KERNEL);
4418 		if (!data)
4419 			return -ENOMEM;
4420 
4421 		rxr->rx_tpa[i].data = data;
4422 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4423 		rxr->rx_tpa[i].mapping = mapping;
4424 	}
4425 
4426 	return 0;
4427 }
4428 
4429 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4430 {
4431 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4432 	int rc;
4433 
4434 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4435 
4436 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4437 		return 0;
4438 
4439 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4440 
4441 	if (rxr->rx_tpa) {
4442 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4443 		if (rc)
4444 			return rc;
4445 	}
4446 	return 0;
4447 }
4448 
4449 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4450 				       struct bnxt_rx_ring_info *rxr)
4451 {
4452 	struct bnxt_ring_struct *ring;
4453 	u32 type;
4454 
4455 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4456 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4457 
4458 	if (NET_IP_ALIGN == 2)
4459 		type |= RX_BD_FLAGS_SOP;
4460 
4461 	ring = &rxr->rx_ring_struct;
4462 	bnxt_init_rxbd_pages(ring, type);
4463 	ring->fw_ring_id = INVALID_HW_RING_ID;
4464 }
4465 
4466 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4467 					   struct bnxt_rx_ring_info *rxr)
4468 {
4469 	struct bnxt_ring_struct *ring;
4470 	u32 type;
4471 
4472 	ring = &rxr->rx_agg_ring_struct;
4473 	ring->fw_ring_id = INVALID_HW_RING_ID;
4474 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4475 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4476 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4477 
4478 		bnxt_init_rxbd_pages(ring, type);
4479 	}
4480 }
4481 
4482 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4483 {
4484 	struct bnxt_rx_ring_info *rxr;
4485 
4486 	rxr = &bp->rx_ring[ring_nr];
4487 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4488 
4489 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4490 			     &rxr->bnapi->napi);
4491 
4492 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4493 		bpf_prog_add(bp->xdp_prog, 1);
4494 		rxr->xdp_prog = bp->xdp_prog;
4495 	}
4496 
4497 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4498 
4499 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4500 }
4501 
4502 static void bnxt_init_cp_rings(struct bnxt *bp)
4503 {
4504 	int i, j;
4505 
4506 	for (i = 0; i < bp->cp_nr_rings; i++) {
4507 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4508 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4509 
4510 		ring->fw_ring_id = INVALID_HW_RING_ID;
4511 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4512 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4513 		if (!cpr->cp_ring_arr)
4514 			continue;
4515 		for (j = 0; j < cpr->cp_ring_count; j++) {
4516 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4517 
4518 			ring = &cpr2->cp_ring_struct;
4519 			ring->fw_ring_id = INVALID_HW_RING_ID;
4520 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4521 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4522 		}
4523 	}
4524 }
4525 
4526 static int bnxt_init_rx_rings(struct bnxt *bp)
4527 {
4528 	int i, rc = 0;
4529 
4530 	if (BNXT_RX_PAGE_MODE(bp)) {
4531 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4532 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4533 	} else {
4534 		bp->rx_offset = BNXT_RX_OFFSET;
4535 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4536 	}
4537 
4538 	for (i = 0; i < bp->rx_nr_rings; i++) {
4539 		rc = bnxt_init_one_rx_ring(bp, i);
4540 		if (rc)
4541 			break;
4542 	}
4543 
4544 	return rc;
4545 }
4546 
4547 static int bnxt_init_tx_rings(struct bnxt *bp)
4548 {
4549 	u16 i;
4550 
4551 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4552 				   BNXT_MIN_TX_DESC_CNT);
4553 
4554 	for (i = 0; i < bp->tx_nr_rings; i++) {
4555 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4556 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4557 
4558 		ring->fw_ring_id = INVALID_HW_RING_ID;
4559 
4560 		if (i >= bp->tx_nr_rings_xdp)
4561 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4562 					     NETDEV_QUEUE_TYPE_TX,
4563 					     &txr->bnapi->napi);
4564 	}
4565 
4566 	return 0;
4567 }
4568 
4569 static void bnxt_free_ring_grps(struct bnxt *bp)
4570 {
4571 	kfree(bp->grp_info);
4572 	bp->grp_info = NULL;
4573 }
4574 
4575 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4576 {
4577 	int i;
4578 
4579 	if (irq_re_init) {
4580 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4581 				       sizeof(struct bnxt_ring_grp_info),
4582 				       GFP_KERNEL);
4583 		if (!bp->grp_info)
4584 			return -ENOMEM;
4585 	}
4586 	for (i = 0; i < bp->cp_nr_rings; i++) {
4587 		if (irq_re_init)
4588 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4589 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4590 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4591 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4592 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4593 	}
4594 	return 0;
4595 }
4596 
4597 static void bnxt_free_vnics(struct bnxt *bp)
4598 {
4599 	kfree(bp->vnic_info);
4600 	bp->vnic_info = NULL;
4601 	bp->nr_vnics = 0;
4602 }
4603 
4604 static int bnxt_alloc_vnics(struct bnxt *bp)
4605 {
4606 	int num_vnics = 1;
4607 
4608 #ifdef CONFIG_RFS_ACCEL
4609 	if (bp->flags & BNXT_FLAG_RFS) {
4610 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4611 			num_vnics++;
4612 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4613 			num_vnics += bp->rx_nr_rings;
4614 	}
4615 #endif
4616 
4617 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4618 		num_vnics++;
4619 
4620 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4621 				GFP_KERNEL);
4622 	if (!bp->vnic_info)
4623 		return -ENOMEM;
4624 
4625 	bp->nr_vnics = num_vnics;
4626 	return 0;
4627 }
4628 
4629 static void bnxt_init_vnics(struct bnxt *bp)
4630 {
4631 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4632 	int i;
4633 
4634 	for (i = 0; i < bp->nr_vnics; i++) {
4635 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4636 		int j;
4637 
4638 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4639 		vnic->vnic_id = i;
4640 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4641 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4642 
4643 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4644 
4645 		if (bp->vnic_info[i].rss_hash_key) {
4646 			if (i == BNXT_VNIC_DEFAULT) {
4647 				u8 *key = (void *)vnic->rss_hash_key;
4648 				int k;
4649 
4650 				if (!bp->rss_hash_key_valid &&
4651 				    !bp->rss_hash_key_updated) {
4652 					get_random_bytes(bp->rss_hash_key,
4653 							 HW_HASH_KEY_SIZE);
4654 					bp->rss_hash_key_updated = true;
4655 				}
4656 
4657 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4658 				       HW_HASH_KEY_SIZE);
4659 
4660 				if (!bp->rss_hash_key_updated)
4661 					continue;
4662 
4663 				bp->rss_hash_key_updated = false;
4664 				bp->rss_hash_key_valid = true;
4665 
4666 				bp->toeplitz_prefix = 0;
4667 				for (k = 0; k < 8; k++) {
4668 					bp->toeplitz_prefix <<= 8;
4669 					bp->toeplitz_prefix |= key[k];
4670 				}
4671 			} else {
4672 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4673 				       HW_HASH_KEY_SIZE);
4674 			}
4675 		}
4676 	}
4677 }
4678 
4679 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4680 {
4681 	int pages;
4682 
4683 	pages = ring_size / desc_per_pg;
4684 
4685 	if (!pages)
4686 		return 1;
4687 
4688 	pages++;
4689 
4690 	while (pages & (pages - 1))
4691 		pages++;
4692 
4693 	return pages;
4694 }
4695 
4696 void bnxt_set_tpa_flags(struct bnxt *bp)
4697 {
4698 	bp->flags &= ~BNXT_FLAG_TPA;
4699 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4700 		return;
4701 	if (bp->dev->features & NETIF_F_LRO)
4702 		bp->flags |= BNXT_FLAG_LRO;
4703 	else if (bp->dev->features & NETIF_F_GRO_HW)
4704 		bp->flags |= BNXT_FLAG_GRO;
4705 }
4706 
4707 static void bnxt_init_ring_params(struct bnxt *bp)
4708 {
4709 	unsigned int rx_size;
4710 
4711 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4712 	/* Try to fit 4 chunks into a 4k page */
4713 	rx_size = SZ_1K -
4714 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4715 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4716 }
4717 
4718 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4719  * be set on entry.
4720  */
4721 void bnxt_set_ring_params(struct bnxt *bp)
4722 {
4723 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4724 	u32 agg_factor = 0, agg_ring_size = 0;
4725 
4726 	/* 8 for CRC and VLAN */
4727 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4728 
4729 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4730 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4731 
4732 	ring_size = bp->rx_ring_size;
4733 	bp->rx_agg_ring_size = 0;
4734 	bp->rx_agg_nr_pages = 0;
4735 
4736 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4737 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4738 
4739 	bp->flags &= ~BNXT_FLAG_JUMBO;
4740 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4741 		u32 jumbo_factor;
4742 
4743 		bp->flags |= BNXT_FLAG_JUMBO;
4744 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4745 		if (jumbo_factor > agg_factor)
4746 			agg_factor = jumbo_factor;
4747 	}
4748 	if (agg_factor) {
4749 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4750 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4751 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4752 				    bp->rx_ring_size, ring_size);
4753 			bp->rx_ring_size = ring_size;
4754 		}
4755 		agg_ring_size = ring_size * agg_factor;
4756 
4757 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4758 							RX_DESC_CNT);
4759 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4760 			u32 tmp = agg_ring_size;
4761 
4762 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4763 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4764 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4765 				    tmp, agg_ring_size);
4766 		}
4767 		bp->rx_agg_ring_size = agg_ring_size;
4768 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4769 
4770 		if (BNXT_RX_PAGE_MODE(bp)) {
4771 			rx_space = PAGE_SIZE;
4772 			rx_size = PAGE_SIZE -
4773 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4774 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4775 		} else {
4776 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4777 				       bp->rx_copybreak,
4778 				       bp->dev->cfg_pending->hds_thresh);
4779 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4780 			rx_space = rx_size + NET_SKB_PAD +
4781 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4782 		}
4783 	}
4784 
4785 	bp->rx_buf_use_size = rx_size;
4786 	bp->rx_buf_size = rx_space;
4787 
4788 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4789 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4790 
4791 	ring_size = bp->tx_ring_size;
4792 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4793 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4794 
4795 	max_rx_cmpl = bp->rx_ring_size;
4796 	/* MAX TPA needs to be added because TPA_START completions are
4797 	 * immediately recycled, so the TPA completions are not bound by
4798 	 * the RX ring size.
4799 	 */
4800 	if (bp->flags & BNXT_FLAG_TPA)
4801 		max_rx_cmpl += bp->max_tpa;
4802 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4803 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4804 	bp->cp_ring_size = ring_size;
4805 
4806 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4807 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4808 		bp->cp_nr_pages = MAX_CP_PAGES;
4809 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4810 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4811 			    ring_size, bp->cp_ring_size);
4812 	}
4813 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4814 	bp->cp_ring_mask = bp->cp_bit - 1;
4815 }
4816 
4817 /* Changing allocation mode of RX rings.
4818  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4819  */
4820 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4821 {
4822 	struct net_device *dev = bp->dev;
4823 
4824 	if (page_mode) {
4825 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4826 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4827 
4828 		if (bp->xdp_prog->aux->xdp_has_frags)
4829 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4830 		else
4831 			dev->max_mtu =
4832 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4833 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4834 			bp->flags |= BNXT_FLAG_JUMBO;
4835 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4836 		} else {
4837 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4838 			bp->rx_skb_func = bnxt_rx_page_skb;
4839 		}
4840 		bp->rx_dir = DMA_BIDIRECTIONAL;
4841 	} else {
4842 		dev->max_mtu = bp->max_mtu;
4843 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4844 		bp->rx_dir = DMA_FROM_DEVICE;
4845 		bp->rx_skb_func = bnxt_rx_skb;
4846 	}
4847 }
4848 
4849 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4850 {
4851 	__bnxt_set_rx_skb_mode(bp, page_mode);
4852 
4853 	if (!page_mode) {
4854 		int rx, tx;
4855 
4856 		bnxt_get_max_rings(bp, &rx, &tx, true);
4857 		if (rx > 1) {
4858 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4859 			bp->dev->hw_features |= NETIF_F_LRO;
4860 		}
4861 	}
4862 
4863 	/* Update LRO and GRO_HW availability */
4864 	netdev_update_features(bp->dev);
4865 }
4866 
4867 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4868 {
4869 	int i;
4870 	struct bnxt_vnic_info *vnic;
4871 	struct pci_dev *pdev = bp->pdev;
4872 
4873 	if (!bp->vnic_info)
4874 		return;
4875 
4876 	for (i = 0; i < bp->nr_vnics; i++) {
4877 		vnic = &bp->vnic_info[i];
4878 
4879 		kfree(vnic->fw_grp_ids);
4880 		vnic->fw_grp_ids = NULL;
4881 
4882 		kfree(vnic->uc_list);
4883 		vnic->uc_list = NULL;
4884 
4885 		if (vnic->mc_list) {
4886 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4887 					  vnic->mc_list, vnic->mc_list_mapping);
4888 			vnic->mc_list = NULL;
4889 		}
4890 
4891 		if (vnic->rss_table) {
4892 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4893 					  vnic->rss_table,
4894 					  vnic->rss_table_dma_addr);
4895 			vnic->rss_table = NULL;
4896 		}
4897 
4898 		vnic->rss_hash_key = NULL;
4899 		vnic->flags = 0;
4900 	}
4901 }
4902 
4903 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4904 {
4905 	int i, rc = 0, size;
4906 	struct bnxt_vnic_info *vnic;
4907 	struct pci_dev *pdev = bp->pdev;
4908 	int max_rings;
4909 
4910 	for (i = 0; i < bp->nr_vnics; i++) {
4911 		vnic = &bp->vnic_info[i];
4912 
4913 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4914 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4915 
4916 			if (mem_size > 0) {
4917 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4918 				if (!vnic->uc_list) {
4919 					rc = -ENOMEM;
4920 					goto out;
4921 				}
4922 			}
4923 		}
4924 
4925 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4926 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4927 			vnic->mc_list =
4928 				dma_alloc_coherent(&pdev->dev,
4929 						   vnic->mc_list_size,
4930 						   &vnic->mc_list_mapping,
4931 						   GFP_KERNEL);
4932 			if (!vnic->mc_list) {
4933 				rc = -ENOMEM;
4934 				goto out;
4935 			}
4936 		}
4937 
4938 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4939 			goto vnic_skip_grps;
4940 
4941 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4942 			max_rings = bp->rx_nr_rings;
4943 		else
4944 			max_rings = 1;
4945 
4946 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4947 		if (!vnic->fw_grp_ids) {
4948 			rc = -ENOMEM;
4949 			goto out;
4950 		}
4951 vnic_skip_grps:
4952 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4953 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4954 			continue;
4955 
4956 		/* Allocate rss table and hash key */
4957 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4958 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4959 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4960 
4961 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4962 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4963 						     vnic->rss_table_size,
4964 						     &vnic->rss_table_dma_addr,
4965 						     GFP_KERNEL);
4966 		if (!vnic->rss_table) {
4967 			rc = -ENOMEM;
4968 			goto out;
4969 		}
4970 
4971 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4972 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4973 	}
4974 	return 0;
4975 
4976 out:
4977 	return rc;
4978 }
4979 
4980 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4981 {
4982 	struct bnxt_hwrm_wait_token *token;
4983 
4984 	dma_pool_destroy(bp->hwrm_dma_pool);
4985 	bp->hwrm_dma_pool = NULL;
4986 
4987 	rcu_read_lock();
4988 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4989 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4990 	rcu_read_unlock();
4991 }
4992 
4993 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4994 {
4995 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4996 					    BNXT_HWRM_DMA_SIZE,
4997 					    BNXT_HWRM_DMA_ALIGN, 0);
4998 	if (!bp->hwrm_dma_pool)
4999 		return -ENOMEM;
5000 
5001 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
5002 
5003 	return 0;
5004 }
5005 
5006 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
5007 {
5008 	kfree(stats->hw_masks);
5009 	stats->hw_masks = NULL;
5010 	kfree(stats->sw_stats);
5011 	stats->sw_stats = NULL;
5012 	if (stats->hw_stats) {
5013 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5014 				  stats->hw_stats_map);
5015 		stats->hw_stats = NULL;
5016 	}
5017 }
5018 
5019 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5020 				bool alloc_masks)
5021 {
5022 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5023 					     &stats->hw_stats_map, GFP_KERNEL);
5024 	if (!stats->hw_stats)
5025 		return -ENOMEM;
5026 
5027 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5028 	if (!stats->sw_stats)
5029 		goto stats_mem_err;
5030 
5031 	if (alloc_masks) {
5032 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5033 		if (!stats->hw_masks)
5034 			goto stats_mem_err;
5035 	}
5036 	return 0;
5037 
5038 stats_mem_err:
5039 	bnxt_free_stats_mem(bp, stats);
5040 	return -ENOMEM;
5041 }
5042 
5043 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5044 {
5045 	int i;
5046 
5047 	for (i = 0; i < count; i++)
5048 		mask_arr[i] = mask;
5049 }
5050 
5051 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5052 {
5053 	int i;
5054 
5055 	for (i = 0; i < count; i++)
5056 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5057 }
5058 
5059 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5060 				    struct bnxt_stats_mem *stats)
5061 {
5062 	struct hwrm_func_qstats_ext_output *resp;
5063 	struct hwrm_func_qstats_ext_input *req;
5064 	__le64 *hw_masks;
5065 	int rc;
5066 
5067 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5068 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5069 		return -EOPNOTSUPP;
5070 
5071 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5072 	if (rc)
5073 		return rc;
5074 
5075 	req->fid = cpu_to_le16(0xffff);
5076 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5077 
5078 	resp = hwrm_req_hold(bp, req);
5079 	rc = hwrm_req_send(bp, req);
5080 	if (!rc) {
5081 		hw_masks = &resp->rx_ucast_pkts;
5082 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5083 	}
5084 	hwrm_req_drop(bp, req);
5085 	return rc;
5086 }
5087 
5088 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5089 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5090 
5091 static void bnxt_init_stats(struct bnxt *bp)
5092 {
5093 	struct bnxt_napi *bnapi = bp->bnapi[0];
5094 	struct bnxt_cp_ring_info *cpr;
5095 	struct bnxt_stats_mem *stats;
5096 	__le64 *rx_stats, *tx_stats;
5097 	int rc, rx_count, tx_count;
5098 	u64 *rx_masks, *tx_masks;
5099 	u64 mask;
5100 	u8 flags;
5101 
5102 	cpr = &bnapi->cp_ring;
5103 	stats = &cpr->stats;
5104 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5105 	if (rc) {
5106 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5107 			mask = (1ULL << 48) - 1;
5108 		else
5109 			mask = -1ULL;
5110 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5111 	}
5112 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5113 		stats = &bp->port_stats;
5114 		rx_stats = stats->hw_stats;
5115 		rx_masks = stats->hw_masks;
5116 		rx_count = sizeof(struct rx_port_stats) / 8;
5117 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5118 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5119 		tx_count = sizeof(struct tx_port_stats) / 8;
5120 
5121 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5122 		rc = bnxt_hwrm_port_qstats(bp, flags);
5123 		if (rc) {
5124 			mask = (1ULL << 40) - 1;
5125 
5126 			bnxt_fill_masks(rx_masks, mask, rx_count);
5127 			bnxt_fill_masks(tx_masks, mask, tx_count);
5128 		} else {
5129 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5130 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5131 			bnxt_hwrm_port_qstats(bp, 0);
5132 		}
5133 	}
5134 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5135 		stats = &bp->rx_port_stats_ext;
5136 		rx_stats = stats->hw_stats;
5137 		rx_masks = stats->hw_masks;
5138 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5139 		stats = &bp->tx_port_stats_ext;
5140 		tx_stats = stats->hw_stats;
5141 		tx_masks = stats->hw_masks;
5142 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5143 
5144 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5145 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5146 		if (rc) {
5147 			mask = (1ULL << 40) - 1;
5148 
5149 			bnxt_fill_masks(rx_masks, mask, rx_count);
5150 			if (tx_stats)
5151 				bnxt_fill_masks(tx_masks, mask, tx_count);
5152 		} else {
5153 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5154 			if (tx_stats)
5155 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5156 						   tx_count);
5157 			bnxt_hwrm_port_qstats_ext(bp, 0);
5158 		}
5159 	}
5160 }
5161 
5162 static void bnxt_free_port_stats(struct bnxt *bp)
5163 {
5164 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5165 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5166 
5167 	bnxt_free_stats_mem(bp, &bp->port_stats);
5168 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5169 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5170 }
5171 
5172 static void bnxt_free_ring_stats(struct bnxt *bp)
5173 {
5174 	int i;
5175 
5176 	if (!bp->bnapi)
5177 		return;
5178 
5179 	for (i = 0; i < bp->cp_nr_rings; i++) {
5180 		struct bnxt_napi *bnapi = bp->bnapi[i];
5181 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5182 
5183 		bnxt_free_stats_mem(bp, &cpr->stats);
5184 
5185 		kfree(cpr->sw_stats);
5186 		cpr->sw_stats = NULL;
5187 	}
5188 }
5189 
5190 static int bnxt_alloc_stats(struct bnxt *bp)
5191 {
5192 	u32 size, i;
5193 	int rc;
5194 
5195 	size = bp->hw_ring_stats_size;
5196 
5197 	for (i = 0; i < bp->cp_nr_rings; i++) {
5198 		struct bnxt_napi *bnapi = bp->bnapi[i];
5199 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5200 
5201 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5202 		if (!cpr->sw_stats)
5203 			return -ENOMEM;
5204 
5205 		cpr->stats.len = size;
5206 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5207 		if (rc)
5208 			return rc;
5209 
5210 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5211 	}
5212 
5213 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5214 		return 0;
5215 
5216 	if (bp->port_stats.hw_stats)
5217 		goto alloc_ext_stats;
5218 
5219 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5220 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5221 	if (rc)
5222 		return rc;
5223 
5224 	bp->flags |= BNXT_FLAG_PORT_STATS;
5225 
5226 alloc_ext_stats:
5227 	/* Display extended statistics only if FW supports it */
5228 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5229 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5230 			return 0;
5231 
5232 	if (bp->rx_port_stats_ext.hw_stats)
5233 		goto alloc_tx_ext_stats;
5234 
5235 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5236 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5237 	/* Extended stats are optional */
5238 	if (rc)
5239 		return 0;
5240 
5241 alloc_tx_ext_stats:
5242 	if (bp->tx_port_stats_ext.hw_stats)
5243 		return 0;
5244 
5245 	if (bp->hwrm_spec_code >= 0x10902 ||
5246 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5247 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5248 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5249 		/* Extended stats are optional */
5250 		if (rc)
5251 			return 0;
5252 	}
5253 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5254 	return 0;
5255 }
5256 
5257 static void bnxt_clear_ring_indices(struct bnxt *bp)
5258 {
5259 	int i, j;
5260 
5261 	if (!bp->bnapi)
5262 		return;
5263 
5264 	for (i = 0; i < bp->cp_nr_rings; i++) {
5265 		struct bnxt_napi *bnapi = bp->bnapi[i];
5266 		struct bnxt_cp_ring_info *cpr;
5267 		struct bnxt_rx_ring_info *rxr;
5268 		struct bnxt_tx_ring_info *txr;
5269 
5270 		if (!bnapi)
5271 			continue;
5272 
5273 		cpr = &bnapi->cp_ring;
5274 		cpr->cp_raw_cons = 0;
5275 
5276 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5277 			txr->tx_prod = 0;
5278 			txr->tx_cons = 0;
5279 			txr->tx_hw_cons = 0;
5280 		}
5281 
5282 		rxr = bnapi->rx_ring;
5283 		if (rxr) {
5284 			rxr->rx_prod = 0;
5285 			rxr->rx_agg_prod = 0;
5286 			rxr->rx_sw_agg_prod = 0;
5287 			rxr->rx_next_cons = 0;
5288 		}
5289 		bnapi->events = 0;
5290 	}
5291 }
5292 
5293 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5294 {
5295 	u8 type = fltr->type, flags = fltr->flags;
5296 
5297 	INIT_LIST_HEAD(&fltr->list);
5298 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5299 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5300 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5301 }
5302 
5303 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5304 {
5305 	if (!list_empty(&fltr->list))
5306 		list_del_init(&fltr->list);
5307 }
5308 
5309 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5310 {
5311 	struct bnxt_filter_base *usr_fltr, *tmp;
5312 
5313 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5314 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5315 			continue;
5316 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5317 	}
5318 }
5319 
5320 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5321 {
5322 	hlist_del(&fltr->hash);
5323 	bnxt_del_one_usr_fltr(bp, fltr);
5324 	if (fltr->flags) {
5325 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5326 		bp->ntp_fltr_count--;
5327 	}
5328 	kfree(fltr);
5329 }
5330 
5331 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5332 {
5333 	int i;
5334 
5335 	netdev_assert_locked(bp->dev);
5336 
5337 	/* Under netdev instance lock and all our NAPIs have been disabled.
5338 	 * It's safe to delete the hash table.
5339 	 */
5340 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5341 		struct hlist_head *head;
5342 		struct hlist_node *tmp;
5343 		struct bnxt_ntuple_filter *fltr;
5344 
5345 		head = &bp->ntp_fltr_hash_tbl[i];
5346 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5347 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5348 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5349 				     !list_empty(&fltr->base.list)))
5350 				continue;
5351 			bnxt_del_fltr(bp, &fltr->base);
5352 		}
5353 	}
5354 	if (!all)
5355 		return;
5356 
5357 	bitmap_free(bp->ntp_fltr_bmap);
5358 	bp->ntp_fltr_bmap = NULL;
5359 	bp->ntp_fltr_count = 0;
5360 }
5361 
5362 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5363 {
5364 	int i, rc = 0;
5365 
5366 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5367 		return 0;
5368 
5369 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5370 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5371 
5372 	bp->ntp_fltr_count = 0;
5373 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5374 
5375 	if (!bp->ntp_fltr_bmap)
5376 		rc = -ENOMEM;
5377 
5378 	return rc;
5379 }
5380 
5381 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5382 {
5383 	int i;
5384 
5385 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5386 		struct hlist_head *head;
5387 		struct hlist_node *tmp;
5388 		struct bnxt_l2_filter *fltr;
5389 
5390 		head = &bp->l2_fltr_hash_tbl[i];
5391 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5392 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5393 				     !list_empty(&fltr->base.list)))
5394 				continue;
5395 			bnxt_del_fltr(bp, &fltr->base);
5396 		}
5397 	}
5398 }
5399 
5400 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5401 {
5402 	int i;
5403 
5404 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5405 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5406 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5407 }
5408 
5409 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5410 {
5411 	bnxt_free_vnic_attributes(bp);
5412 	bnxt_free_tx_rings(bp);
5413 	bnxt_free_rx_rings(bp);
5414 	bnxt_free_cp_rings(bp);
5415 	bnxt_free_all_cp_arrays(bp);
5416 	bnxt_free_ntp_fltrs(bp, false);
5417 	bnxt_free_l2_filters(bp, false);
5418 	if (irq_re_init) {
5419 		bnxt_free_ring_stats(bp);
5420 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5421 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5422 			bnxt_free_port_stats(bp);
5423 		bnxt_free_ring_grps(bp);
5424 		bnxt_free_vnics(bp);
5425 		kfree(bp->tx_ring_map);
5426 		bp->tx_ring_map = NULL;
5427 		kfree(bp->tx_ring);
5428 		bp->tx_ring = NULL;
5429 		kfree(bp->rx_ring);
5430 		bp->rx_ring = NULL;
5431 		kfree(bp->bnapi);
5432 		bp->bnapi = NULL;
5433 	} else {
5434 		bnxt_clear_ring_indices(bp);
5435 	}
5436 }
5437 
5438 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5439 {
5440 	int i, j, rc, size, arr_size;
5441 	void *bnapi;
5442 
5443 	if (irq_re_init) {
5444 		/* Allocate bnapi mem pointer array and mem block for
5445 		 * all queues
5446 		 */
5447 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5448 				bp->cp_nr_rings);
5449 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5450 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5451 		if (!bnapi)
5452 			return -ENOMEM;
5453 
5454 		bp->bnapi = bnapi;
5455 		bnapi += arr_size;
5456 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5457 			bp->bnapi[i] = bnapi;
5458 			bp->bnapi[i]->index = i;
5459 			bp->bnapi[i]->bp = bp;
5460 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5461 				struct bnxt_cp_ring_info *cpr =
5462 					&bp->bnapi[i]->cp_ring;
5463 
5464 				cpr->cp_ring_struct.ring_mem.flags =
5465 					BNXT_RMEM_RING_PTE_FLAG;
5466 			}
5467 		}
5468 
5469 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5470 				      sizeof(struct bnxt_rx_ring_info),
5471 				      GFP_KERNEL);
5472 		if (!bp->rx_ring)
5473 			return -ENOMEM;
5474 
5475 		for (i = 0; i < bp->rx_nr_rings; i++) {
5476 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5477 
5478 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5479 				rxr->rx_ring_struct.ring_mem.flags =
5480 					BNXT_RMEM_RING_PTE_FLAG;
5481 				rxr->rx_agg_ring_struct.ring_mem.flags =
5482 					BNXT_RMEM_RING_PTE_FLAG;
5483 			} else {
5484 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5485 			}
5486 			rxr->bnapi = bp->bnapi[i];
5487 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5488 		}
5489 
5490 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5491 				      sizeof(struct bnxt_tx_ring_info),
5492 				      GFP_KERNEL);
5493 		if (!bp->tx_ring)
5494 			return -ENOMEM;
5495 
5496 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5497 					  GFP_KERNEL);
5498 
5499 		if (!bp->tx_ring_map)
5500 			return -ENOMEM;
5501 
5502 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5503 			j = 0;
5504 		else
5505 			j = bp->rx_nr_rings;
5506 
5507 		for (i = 0; i < bp->tx_nr_rings; i++) {
5508 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5509 			struct bnxt_napi *bnapi2;
5510 
5511 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5512 				txr->tx_ring_struct.ring_mem.flags =
5513 					BNXT_RMEM_RING_PTE_FLAG;
5514 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5515 			if (i >= bp->tx_nr_rings_xdp) {
5516 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5517 
5518 				bnapi2 = bp->bnapi[k];
5519 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5520 				txr->tx_napi_idx =
5521 					BNXT_RING_TO_TC(bp, txr->txq_index);
5522 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5523 				bnapi2->tx_int = bnxt_tx_int;
5524 			} else {
5525 				bnapi2 = bp->bnapi[j];
5526 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5527 				bnapi2->tx_ring[0] = txr;
5528 				bnapi2->tx_int = bnxt_tx_int_xdp;
5529 				j++;
5530 			}
5531 			txr->bnapi = bnapi2;
5532 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5533 				txr->tx_cpr = &bnapi2->cp_ring;
5534 		}
5535 
5536 		rc = bnxt_alloc_stats(bp);
5537 		if (rc)
5538 			goto alloc_mem_err;
5539 		bnxt_init_stats(bp);
5540 
5541 		rc = bnxt_alloc_ntp_fltrs(bp);
5542 		if (rc)
5543 			goto alloc_mem_err;
5544 
5545 		rc = bnxt_alloc_vnics(bp);
5546 		if (rc)
5547 			goto alloc_mem_err;
5548 	}
5549 
5550 	rc = bnxt_alloc_all_cp_arrays(bp);
5551 	if (rc)
5552 		goto alloc_mem_err;
5553 
5554 	bnxt_init_ring_struct(bp);
5555 
5556 	rc = bnxt_alloc_rx_rings(bp);
5557 	if (rc)
5558 		goto alloc_mem_err;
5559 
5560 	rc = bnxt_alloc_tx_rings(bp);
5561 	if (rc)
5562 		goto alloc_mem_err;
5563 
5564 	rc = bnxt_alloc_cp_rings(bp);
5565 	if (rc)
5566 		goto alloc_mem_err;
5567 
5568 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5569 						  BNXT_VNIC_MCAST_FLAG |
5570 						  BNXT_VNIC_UCAST_FLAG;
5571 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5572 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5573 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5574 
5575 	rc = bnxt_alloc_vnic_attributes(bp);
5576 	if (rc)
5577 		goto alloc_mem_err;
5578 	return 0;
5579 
5580 alloc_mem_err:
5581 	bnxt_free_mem(bp, true);
5582 	return rc;
5583 }
5584 
5585 static void bnxt_disable_int(struct bnxt *bp)
5586 {
5587 	int i;
5588 
5589 	if (!bp->bnapi)
5590 		return;
5591 
5592 	for (i = 0; i < bp->cp_nr_rings; i++) {
5593 		struct bnxt_napi *bnapi = bp->bnapi[i];
5594 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5595 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5596 
5597 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5598 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5599 	}
5600 }
5601 
5602 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5603 {
5604 	struct bnxt_napi *bnapi = bp->bnapi[n];
5605 	struct bnxt_cp_ring_info *cpr;
5606 
5607 	cpr = &bnapi->cp_ring;
5608 	return cpr->cp_ring_struct.map_idx;
5609 }
5610 
5611 static void bnxt_disable_int_sync(struct bnxt *bp)
5612 {
5613 	int i;
5614 
5615 	if (!bp->irq_tbl)
5616 		return;
5617 
5618 	atomic_inc(&bp->intr_sem);
5619 
5620 	bnxt_disable_int(bp);
5621 	for (i = 0; i < bp->cp_nr_rings; i++) {
5622 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5623 
5624 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5625 	}
5626 }
5627 
5628 static void bnxt_enable_int(struct bnxt *bp)
5629 {
5630 	int i;
5631 
5632 	atomic_set(&bp->intr_sem, 0);
5633 	for (i = 0; i < bp->cp_nr_rings; i++) {
5634 		struct bnxt_napi *bnapi = bp->bnapi[i];
5635 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5636 
5637 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5638 	}
5639 }
5640 
5641 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5642 			    bool async_only)
5643 {
5644 	DECLARE_BITMAP(async_events_bmap, 256);
5645 	u32 *events = (u32 *)async_events_bmap;
5646 	struct hwrm_func_drv_rgtr_output *resp;
5647 	struct hwrm_func_drv_rgtr_input *req;
5648 	u32 flags;
5649 	int rc, i;
5650 
5651 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5652 	if (rc)
5653 		return rc;
5654 
5655 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5656 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5657 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5658 
5659 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5660 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5661 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5662 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5663 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5664 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5665 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5666 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5667 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5668 	req->flags = cpu_to_le32(flags);
5669 	req->ver_maj_8b = DRV_VER_MAJ;
5670 	req->ver_min_8b = DRV_VER_MIN;
5671 	req->ver_upd_8b = DRV_VER_UPD;
5672 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5673 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5674 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5675 
5676 	if (BNXT_PF(bp)) {
5677 		u32 data[8];
5678 		int i;
5679 
5680 		memset(data, 0, sizeof(data));
5681 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5682 			u16 cmd = bnxt_vf_req_snif[i];
5683 			unsigned int bit, idx;
5684 
5685 			idx = cmd / 32;
5686 			bit = cmd % 32;
5687 			data[idx] |= 1 << bit;
5688 		}
5689 
5690 		for (i = 0; i < 8; i++)
5691 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5692 
5693 		req->enables |=
5694 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5695 	}
5696 
5697 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5698 		req->flags |= cpu_to_le32(
5699 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5700 
5701 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5702 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5703 		u16 event_id = bnxt_async_events_arr[i];
5704 
5705 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5706 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5707 			continue;
5708 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5709 		    !bp->ptp_cfg)
5710 			continue;
5711 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5712 	}
5713 	if (bmap && bmap_size) {
5714 		for (i = 0; i < bmap_size; i++) {
5715 			if (test_bit(i, bmap))
5716 				__set_bit(i, async_events_bmap);
5717 		}
5718 	}
5719 	for (i = 0; i < 8; i++)
5720 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5721 
5722 	if (async_only)
5723 		req->enables =
5724 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5725 
5726 	resp = hwrm_req_hold(bp, req);
5727 	rc = hwrm_req_send(bp, req);
5728 	if (!rc) {
5729 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5730 		if (resp->flags &
5731 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5732 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5733 	}
5734 	hwrm_req_drop(bp, req);
5735 	return rc;
5736 }
5737 
5738 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5739 {
5740 	struct hwrm_func_drv_unrgtr_input *req;
5741 	int rc;
5742 
5743 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5744 		return 0;
5745 
5746 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5747 	if (rc)
5748 		return rc;
5749 	return hwrm_req_send(bp, req);
5750 }
5751 
5752 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5753 
5754 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5755 {
5756 	struct hwrm_tunnel_dst_port_free_input *req;
5757 	int rc;
5758 
5759 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5760 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5761 		return 0;
5762 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5763 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5764 		return 0;
5765 
5766 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5767 	if (rc)
5768 		return rc;
5769 
5770 	req->tunnel_type = tunnel_type;
5771 
5772 	switch (tunnel_type) {
5773 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5774 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5775 		bp->vxlan_port = 0;
5776 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5777 		break;
5778 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5779 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5780 		bp->nge_port = 0;
5781 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5782 		break;
5783 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5784 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5785 		bp->vxlan_gpe_port = 0;
5786 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5787 		break;
5788 	default:
5789 		break;
5790 	}
5791 
5792 	rc = hwrm_req_send(bp, req);
5793 	if (rc)
5794 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5795 			   rc);
5796 	if (bp->flags & BNXT_FLAG_TPA)
5797 		bnxt_set_tpa(bp, true);
5798 	return rc;
5799 }
5800 
5801 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5802 					   u8 tunnel_type)
5803 {
5804 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5805 	struct hwrm_tunnel_dst_port_alloc_input *req;
5806 	int rc;
5807 
5808 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5809 	if (rc)
5810 		return rc;
5811 
5812 	req->tunnel_type = tunnel_type;
5813 	req->tunnel_dst_port_val = port;
5814 
5815 	resp = hwrm_req_hold(bp, req);
5816 	rc = hwrm_req_send(bp, req);
5817 	if (rc) {
5818 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5819 			   rc);
5820 		goto err_out;
5821 	}
5822 
5823 	switch (tunnel_type) {
5824 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5825 		bp->vxlan_port = port;
5826 		bp->vxlan_fw_dst_port_id =
5827 			le16_to_cpu(resp->tunnel_dst_port_id);
5828 		break;
5829 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5830 		bp->nge_port = port;
5831 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5832 		break;
5833 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5834 		bp->vxlan_gpe_port = port;
5835 		bp->vxlan_gpe_fw_dst_port_id =
5836 			le16_to_cpu(resp->tunnel_dst_port_id);
5837 		break;
5838 	default:
5839 		break;
5840 	}
5841 	if (bp->flags & BNXT_FLAG_TPA)
5842 		bnxt_set_tpa(bp, true);
5843 
5844 err_out:
5845 	hwrm_req_drop(bp, req);
5846 	return rc;
5847 }
5848 
5849 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5850 {
5851 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5852 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5853 	int rc;
5854 
5855 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5856 	if (rc)
5857 		return rc;
5858 
5859 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5860 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5861 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5862 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5863 	}
5864 	req->mask = cpu_to_le32(vnic->rx_mask);
5865 	return hwrm_req_send_silent(bp, req);
5866 }
5867 
5868 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5869 {
5870 	if (!atomic_dec_and_test(&fltr->refcnt))
5871 		return;
5872 	spin_lock_bh(&bp->ntp_fltr_lock);
5873 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5874 		spin_unlock_bh(&bp->ntp_fltr_lock);
5875 		return;
5876 	}
5877 	hlist_del_rcu(&fltr->base.hash);
5878 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5879 	if (fltr->base.flags) {
5880 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5881 		bp->ntp_fltr_count--;
5882 	}
5883 	spin_unlock_bh(&bp->ntp_fltr_lock);
5884 	kfree_rcu(fltr, base.rcu);
5885 }
5886 
5887 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5888 						      struct bnxt_l2_key *key,
5889 						      u32 idx)
5890 {
5891 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5892 	struct bnxt_l2_filter *fltr;
5893 
5894 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5895 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5896 
5897 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5898 		    l2_key->vlan == key->vlan)
5899 			return fltr;
5900 	}
5901 	return NULL;
5902 }
5903 
5904 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5905 						    struct bnxt_l2_key *key,
5906 						    u32 idx)
5907 {
5908 	struct bnxt_l2_filter *fltr = NULL;
5909 
5910 	rcu_read_lock();
5911 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5912 	if (fltr)
5913 		atomic_inc(&fltr->refcnt);
5914 	rcu_read_unlock();
5915 	return fltr;
5916 }
5917 
5918 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5919 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5920 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5921 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5922 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5923 
5924 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5925 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5926 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5927 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5928 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5929 
5930 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5931 {
5932 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5933 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5934 			return sizeof(fkeys->addrs.v4addrs) +
5935 			       sizeof(fkeys->ports);
5936 
5937 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5938 			return sizeof(fkeys->addrs.v4addrs);
5939 	}
5940 
5941 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5942 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5943 			return sizeof(fkeys->addrs.v6addrs) +
5944 			       sizeof(fkeys->ports);
5945 
5946 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5947 			return sizeof(fkeys->addrs.v6addrs);
5948 	}
5949 
5950 	return 0;
5951 }
5952 
5953 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5954 			 const unsigned char *key)
5955 {
5956 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5957 	struct bnxt_ipv4_tuple tuple4;
5958 	struct bnxt_ipv6_tuple tuple6;
5959 	int i, j, len = 0;
5960 	u8 *four_tuple;
5961 
5962 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5963 	if (!len)
5964 		return 0;
5965 
5966 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5967 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5968 		tuple4.ports = fkeys->ports;
5969 		four_tuple = (unsigned char *)&tuple4;
5970 	} else {
5971 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5972 		tuple6.ports = fkeys->ports;
5973 		four_tuple = (unsigned char *)&tuple6;
5974 	}
5975 
5976 	for (i = 0, j = 8; i < len; i++, j++) {
5977 		u8 byte = four_tuple[i];
5978 		int bit;
5979 
5980 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5981 			if (byte & 0x80)
5982 				hash ^= prefix;
5983 		}
5984 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5985 	}
5986 
5987 	/* The valid part of the hash is in the upper 32 bits. */
5988 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5989 }
5990 
5991 #ifdef CONFIG_RFS_ACCEL
5992 static struct bnxt_l2_filter *
5993 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5994 {
5995 	struct bnxt_l2_filter *fltr;
5996 	u32 idx;
5997 
5998 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5999 	      BNXT_L2_FLTR_HASH_MASK;
6000 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6001 	return fltr;
6002 }
6003 #endif
6004 
6005 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
6006 			       struct bnxt_l2_key *key, u32 idx)
6007 {
6008 	struct hlist_head *head;
6009 
6010 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
6011 	fltr->l2_key.vlan = key->vlan;
6012 	fltr->base.type = BNXT_FLTR_TYPE_L2;
6013 	if (fltr->base.flags) {
6014 		int bit_id;
6015 
6016 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6017 						 bp->max_fltr, 0);
6018 		if (bit_id < 0)
6019 			return -ENOMEM;
6020 		fltr->base.sw_id = (u16)bit_id;
6021 		bp->ntp_fltr_count++;
6022 	}
6023 	head = &bp->l2_fltr_hash_tbl[idx];
6024 	hlist_add_head_rcu(&fltr->base.hash, head);
6025 	bnxt_insert_usr_fltr(bp, &fltr->base);
6026 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6027 	atomic_set(&fltr->refcnt, 1);
6028 	return 0;
6029 }
6030 
6031 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6032 						   struct bnxt_l2_key *key,
6033 						   gfp_t gfp)
6034 {
6035 	struct bnxt_l2_filter *fltr;
6036 	u32 idx;
6037 	int rc;
6038 
6039 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6040 	      BNXT_L2_FLTR_HASH_MASK;
6041 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6042 	if (fltr)
6043 		return fltr;
6044 
6045 	fltr = kzalloc(sizeof(*fltr), gfp);
6046 	if (!fltr)
6047 		return ERR_PTR(-ENOMEM);
6048 	spin_lock_bh(&bp->ntp_fltr_lock);
6049 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6050 	spin_unlock_bh(&bp->ntp_fltr_lock);
6051 	if (rc) {
6052 		bnxt_del_l2_filter(bp, fltr);
6053 		fltr = ERR_PTR(rc);
6054 	}
6055 	return fltr;
6056 }
6057 
6058 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6059 						struct bnxt_l2_key *key,
6060 						u16 flags)
6061 {
6062 	struct bnxt_l2_filter *fltr;
6063 	u32 idx;
6064 	int rc;
6065 
6066 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6067 	      BNXT_L2_FLTR_HASH_MASK;
6068 	spin_lock_bh(&bp->ntp_fltr_lock);
6069 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6070 	if (fltr) {
6071 		fltr = ERR_PTR(-EEXIST);
6072 		goto l2_filter_exit;
6073 	}
6074 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
6075 	if (!fltr) {
6076 		fltr = ERR_PTR(-ENOMEM);
6077 		goto l2_filter_exit;
6078 	}
6079 	fltr->base.flags = flags;
6080 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6081 	if (rc) {
6082 		spin_unlock_bh(&bp->ntp_fltr_lock);
6083 		bnxt_del_l2_filter(bp, fltr);
6084 		return ERR_PTR(rc);
6085 	}
6086 
6087 l2_filter_exit:
6088 	spin_unlock_bh(&bp->ntp_fltr_lock);
6089 	return fltr;
6090 }
6091 
6092 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6093 {
6094 #ifdef CONFIG_BNXT_SRIOV
6095 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6096 
6097 	return vf->fw_fid;
6098 #else
6099 	return INVALID_HW_RING_ID;
6100 #endif
6101 }
6102 
6103 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6104 {
6105 	struct hwrm_cfa_l2_filter_free_input *req;
6106 	u16 target_id = 0xffff;
6107 	int rc;
6108 
6109 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6110 		struct bnxt_pf_info *pf = &bp->pf;
6111 
6112 		if (fltr->base.vf_idx >= pf->active_vfs)
6113 			return -EINVAL;
6114 
6115 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6116 		if (target_id == INVALID_HW_RING_ID)
6117 			return -EINVAL;
6118 	}
6119 
6120 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6121 	if (rc)
6122 		return rc;
6123 
6124 	req->target_id = cpu_to_le16(target_id);
6125 	req->l2_filter_id = fltr->base.filter_id;
6126 	return hwrm_req_send(bp, req);
6127 }
6128 
6129 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6130 {
6131 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6132 	struct hwrm_cfa_l2_filter_alloc_input *req;
6133 	u16 target_id = 0xffff;
6134 	int rc;
6135 
6136 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6137 		struct bnxt_pf_info *pf = &bp->pf;
6138 
6139 		if (fltr->base.vf_idx >= pf->active_vfs)
6140 			return -EINVAL;
6141 
6142 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6143 	}
6144 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6145 	if (rc)
6146 		return rc;
6147 
6148 	req->target_id = cpu_to_le16(target_id);
6149 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6150 
6151 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6152 		req->flags |=
6153 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6154 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6155 	req->enables =
6156 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6157 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6158 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6159 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6160 	eth_broadcast_addr(req->l2_addr_mask);
6161 
6162 	if (fltr->l2_key.vlan) {
6163 		req->enables |=
6164 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6165 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6166 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6167 		req->num_vlans = 1;
6168 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6169 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6170 	}
6171 
6172 	resp = hwrm_req_hold(bp, req);
6173 	rc = hwrm_req_send(bp, req);
6174 	if (!rc) {
6175 		fltr->base.filter_id = resp->l2_filter_id;
6176 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6177 	}
6178 	hwrm_req_drop(bp, req);
6179 	return rc;
6180 }
6181 
6182 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6183 				     struct bnxt_ntuple_filter *fltr)
6184 {
6185 	struct hwrm_cfa_ntuple_filter_free_input *req;
6186 	int rc;
6187 
6188 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6189 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6190 	if (rc)
6191 		return rc;
6192 
6193 	req->ntuple_filter_id = fltr->base.filter_id;
6194 	return hwrm_req_send(bp, req);
6195 }
6196 
6197 #define BNXT_NTP_FLTR_FLAGS					\
6198 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6199 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6200 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6201 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6202 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6203 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6204 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6205 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6206 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6207 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6208 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6209 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6210 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6211 
6212 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6213 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6214 
6215 void bnxt_fill_ipv6_mask(__be32 mask[4])
6216 {
6217 	int i;
6218 
6219 	for (i = 0; i < 4; i++)
6220 		mask[i] = cpu_to_be32(~0);
6221 }
6222 
6223 static void
6224 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6225 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6226 			  struct bnxt_ntuple_filter *fltr)
6227 {
6228 	u16 rxq = fltr->base.rxq;
6229 
6230 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6231 		struct ethtool_rxfh_context *ctx;
6232 		struct bnxt_rss_ctx *rss_ctx;
6233 		struct bnxt_vnic_info *vnic;
6234 
6235 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6236 			      fltr->base.fw_vnic_id);
6237 		if (ctx) {
6238 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6239 			vnic = &rss_ctx->vnic;
6240 
6241 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6242 		}
6243 		return;
6244 	}
6245 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6246 		struct bnxt_vnic_info *vnic;
6247 		u32 enables;
6248 
6249 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6250 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6251 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6252 		req->enables |= cpu_to_le32(enables);
6253 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6254 	} else {
6255 		u32 flags;
6256 
6257 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6258 		req->flags |= cpu_to_le32(flags);
6259 		req->dst_id = cpu_to_le16(rxq);
6260 	}
6261 }
6262 
6263 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6264 				      struct bnxt_ntuple_filter *fltr)
6265 {
6266 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6267 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6268 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6269 	struct flow_keys *keys = &fltr->fkeys;
6270 	struct bnxt_l2_filter *l2_fltr;
6271 	struct bnxt_vnic_info *vnic;
6272 	int rc;
6273 
6274 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6275 	if (rc)
6276 		return rc;
6277 
6278 	l2_fltr = fltr->l2_fltr;
6279 	req->l2_filter_id = l2_fltr->base.filter_id;
6280 
6281 	if (fltr->base.flags & BNXT_ACT_DROP) {
6282 		req->flags =
6283 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6284 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6285 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6286 	} else {
6287 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6288 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6289 	}
6290 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6291 
6292 	req->ethertype = htons(ETH_P_IP);
6293 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6294 	req->ip_protocol = keys->basic.ip_proto;
6295 
6296 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6297 		req->ethertype = htons(ETH_P_IPV6);
6298 		req->ip_addr_type =
6299 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6300 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6301 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6302 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6303 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6304 	} else {
6305 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6306 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6307 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6308 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6309 	}
6310 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6311 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6312 		req->tunnel_type =
6313 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6314 	}
6315 
6316 	req->src_port = keys->ports.src;
6317 	req->src_port_mask = masks->ports.src;
6318 	req->dst_port = keys->ports.dst;
6319 	req->dst_port_mask = masks->ports.dst;
6320 
6321 	resp = hwrm_req_hold(bp, req);
6322 	rc = hwrm_req_send(bp, req);
6323 	if (!rc)
6324 		fltr->base.filter_id = resp->ntuple_filter_id;
6325 	hwrm_req_drop(bp, req);
6326 	return rc;
6327 }
6328 
6329 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6330 				     const u8 *mac_addr)
6331 {
6332 	struct bnxt_l2_filter *fltr;
6333 	struct bnxt_l2_key key;
6334 	int rc;
6335 
6336 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6337 	key.vlan = 0;
6338 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6339 	if (IS_ERR(fltr))
6340 		return PTR_ERR(fltr);
6341 
6342 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6343 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6344 	if (rc)
6345 		bnxt_del_l2_filter(bp, fltr);
6346 	else
6347 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6348 	return rc;
6349 }
6350 
6351 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6352 {
6353 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6354 
6355 	/* Any associated ntuple filters will also be cleared by firmware. */
6356 	for (i = 0; i < num_of_vnics; i++) {
6357 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6358 
6359 		for (j = 0; j < vnic->uc_filter_count; j++) {
6360 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6361 
6362 			bnxt_hwrm_l2_filter_free(bp, fltr);
6363 			bnxt_del_l2_filter(bp, fltr);
6364 		}
6365 		vnic->uc_filter_count = 0;
6366 	}
6367 }
6368 
6369 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6370 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6371 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6372 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6373 
6374 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6375 					   struct hwrm_vnic_tpa_cfg_input *req)
6376 {
6377 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6378 
6379 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6380 		return;
6381 
6382 	if (bp->vxlan_port)
6383 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6384 	if (bp->vxlan_gpe_port)
6385 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6386 	if (bp->nge_port)
6387 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6388 
6389 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6390 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6391 }
6392 
6393 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6394 			   u32 tpa_flags)
6395 {
6396 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6397 	struct hwrm_vnic_tpa_cfg_input *req;
6398 	int rc;
6399 
6400 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6401 		return 0;
6402 
6403 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6404 	if (rc)
6405 		return rc;
6406 
6407 	if (tpa_flags) {
6408 		u16 mss = bp->dev->mtu - 40;
6409 		u32 nsegs, n, segs = 0, flags;
6410 
6411 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6412 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6413 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6414 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6415 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6416 		if (tpa_flags & BNXT_FLAG_GRO)
6417 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6418 
6419 		req->flags = cpu_to_le32(flags);
6420 
6421 		req->enables =
6422 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6423 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6424 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6425 
6426 		/* Number of segs are log2 units, and first packet is not
6427 		 * included as part of this units.
6428 		 */
6429 		if (mss <= BNXT_RX_PAGE_SIZE) {
6430 			n = BNXT_RX_PAGE_SIZE / mss;
6431 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6432 		} else {
6433 			n = mss / BNXT_RX_PAGE_SIZE;
6434 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6435 				n++;
6436 			nsegs = (MAX_SKB_FRAGS - n) / n;
6437 		}
6438 
6439 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6440 			segs = MAX_TPA_SEGS_P5;
6441 			max_aggs = bp->max_tpa;
6442 		} else {
6443 			segs = ilog2(nsegs);
6444 		}
6445 		req->max_agg_segs = cpu_to_le16(segs);
6446 		req->max_aggs = cpu_to_le16(max_aggs);
6447 
6448 		req->min_agg_len = cpu_to_le32(512);
6449 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6450 	}
6451 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6452 
6453 	return hwrm_req_send(bp, req);
6454 }
6455 
6456 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6457 {
6458 	struct bnxt_ring_grp_info *grp_info;
6459 
6460 	grp_info = &bp->grp_info[ring->grp_idx];
6461 	return grp_info->cp_fw_ring_id;
6462 }
6463 
6464 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6465 {
6466 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6467 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6468 	else
6469 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6470 }
6471 
6472 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6473 {
6474 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6475 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6476 	else
6477 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6478 }
6479 
6480 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6481 {
6482 	int entries;
6483 
6484 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6485 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6486 	else
6487 		entries = HW_HASH_INDEX_SIZE;
6488 
6489 	bp->rss_indir_tbl_entries = entries;
6490 	bp->rss_indir_tbl =
6491 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6492 	if (!bp->rss_indir_tbl)
6493 		return -ENOMEM;
6494 
6495 	return 0;
6496 }
6497 
6498 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6499 				 struct ethtool_rxfh_context *rss_ctx)
6500 {
6501 	u16 max_rings, max_entries, pad, i;
6502 	u32 *rss_indir_tbl;
6503 
6504 	if (!bp->rx_nr_rings)
6505 		return;
6506 
6507 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6508 		max_rings = bp->rx_nr_rings - 1;
6509 	else
6510 		max_rings = bp->rx_nr_rings;
6511 
6512 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6513 	if (rss_ctx)
6514 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6515 	else
6516 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6517 
6518 	for (i = 0; i < max_entries; i++)
6519 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6520 
6521 	pad = bp->rss_indir_tbl_entries - max_entries;
6522 	if (pad)
6523 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6524 }
6525 
6526 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6527 {
6528 	u32 i, tbl_size, max_ring = 0;
6529 
6530 	if (!bp->rss_indir_tbl)
6531 		return 0;
6532 
6533 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6534 	for (i = 0; i < tbl_size; i++)
6535 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6536 	return max_ring;
6537 }
6538 
6539 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6540 {
6541 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6542 		if (!rx_rings)
6543 			return 0;
6544 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6545 					       BNXT_RSS_TABLE_ENTRIES_P5);
6546 	}
6547 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6548 		return 2;
6549 	return 1;
6550 }
6551 
6552 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6553 {
6554 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6555 	u16 i, j;
6556 
6557 	/* Fill the RSS indirection table with ring group ids */
6558 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6559 		if (!no_rss)
6560 			j = bp->rss_indir_tbl[i];
6561 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6562 	}
6563 }
6564 
6565 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6566 				    struct bnxt_vnic_info *vnic)
6567 {
6568 	__le16 *ring_tbl = vnic->rss_table;
6569 	struct bnxt_rx_ring_info *rxr;
6570 	u16 tbl_size, i;
6571 
6572 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6573 
6574 	for (i = 0; i < tbl_size; i++) {
6575 		u16 ring_id, j;
6576 
6577 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6578 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6579 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6580 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6581 		else
6582 			j = bp->rss_indir_tbl[i];
6583 		rxr = &bp->rx_ring[j];
6584 
6585 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6586 		*ring_tbl++ = cpu_to_le16(ring_id);
6587 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6588 		*ring_tbl++ = cpu_to_le16(ring_id);
6589 	}
6590 }
6591 
6592 static void
6593 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6594 			 struct bnxt_vnic_info *vnic)
6595 {
6596 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6597 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6598 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6599 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6600 	} else {
6601 		bnxt_fill_hw_rss_tbl(bp, vnic);
6602 	}
6603 
6604 	if (bp->rss_hash_delta) {
6605 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6606 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6607 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6608 		else
6609 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6610 	} else {
6611 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6612 	}
6613 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6614 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6615 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6616 }
6617 
6618 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6619 				  bool set_rss)
6620 {
6621 	struct hwrm_vnic_rss_cfg_input *req;
6622 	int rc;
6623 
6624 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6625 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6626 		return 0;
6627 
6628 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6629 	if (rc)
6630 		return rc;
6631 
6632 	if (set_rss)
6633 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6634 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6635 	return hwrm_req_send(bp, req);
6636 }
6637 
6638 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6639 				     struct bnxt_vnic_info *vnic, bool set_rss)
6640 {
6641 	struct hwrm_vnic_rss_cfg_input *req;
6642 	dma_addr_t ring_tbl_map;
6643 	u32 i, nr_ctxs;
6644 	int rc;
6645 
6646 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6647 	if (rc)
6648 		return rc;
6649 
6650 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6651 	if (!set_rss)
6652 		return hwrm_req_send(bp, req);
6653 
6654 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6655 	ring_tbl_map = vnic->rss_table_dma_addr;
6656 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6657 
6658 	hwrm_req_hold(bp, req);
6659 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6660 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6661 		req->ring_table_pair_index = i;
6662 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6663 		rc = hwrm_req_send(bp, req);
6664 		if (rc)
6665 			goto exit;
6666 	}
6667 
6668 exit:
6669 	hwrm_req_drop(bp, req);
6670 	return rc;
6671 }
6672 
6673 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6674 {
6675 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6676 	struct hwrm_vnic_rss_qcfg_output *resp;
6677 	struct hwrm_vnic_rss_qcfg_input *req;
6678 
6679 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6680 		return;
6681 
6682 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6683 	/* all contexts configured to same hash_type, zero always exists */
6684 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6685 	resp = hwrm_req_hold(bp, req);
6686 	if (!hwrm_req_send(bp, req)) {
6687 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6688 		bp->rss_hash_delta = 0;
6689 	}
6690 	hwrm_req_drop(bp, req);
6691 }
6692 
6693 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6694 {
6695 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6696 	struct hwrm_vnic_plcmodes_cfg_input *req;
6697 	int rc;
6698 
6699 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6700 	if (rc)
6701 		return rc;
6702 
6703 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6704 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6705 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6706 
6707 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6708 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6709 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6710 		req->enables |=
6711 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6712 		req->hds_threshold = cpu_to_le16(hds_thresh);
6713 	}
6714 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6715 	return hwrm_req_send(bp, req);
6716 }
6717 
6718 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6719 					struct bnxt_vnic_info *vnic,
6720 					u16 ctx_idx)
6721 {
6722 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6723 
6724 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6725 		return;
6726 
6727 	req->rss_cos_lb_ctx_id =
6728 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6729 
6730 	hwrm_req_send(bp, req);
6731 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6732 }
6733 
6734 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6735 {
6736 	int i, j;
6737 
6738 	for (i = 0; i < bp->nr_vnics; i++) {
6739 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6740 
6741 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6742 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6743 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6744 		}
6745 	}
6746 	bp->rsscos_nr_ctxs = 0;
6747 }
6748 
6749 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6750 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6751 {
6752 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6753 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6754 	int rc;
6755 
6756 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6757 	if (rc)
6758 		return rc;
6759 
6760 	resp = hwrm_req_hold(bp, req);
6761 	rc = hwrm_req_send(bp, req);
6762 	if (!rc)
6763 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6764 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6765 	hwrm_req_drop(bp, req);
6766 
6767 	return rc;
6768 }
6769 
6770 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6771 {
6772 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6773 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6774 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6775 }
6776 
6777 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6778 {
6779 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6780 	struct hwrm_vnic_cfg_input *req;
6781 	unsigned int ring = 0, grp_idx;
6782 	u16 def_vlan = 0;
6783 	int rc;
6784 
6785 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6786 	if (rc)
6787 		return rc;
6788 
6789 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6790 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6791 
6792 		req->default_rx_ring_id =
6793 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6794 		req->default_cmpl_ring_id =
6795 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6796 		req->enables =
6797 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6798 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6799 		goto vnic_mru;
6800 	}
6801 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6802 	/* Only RSS support for now TBD: COS & LB */
6803 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6804 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6805 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6806 					   VNIC_CFG_REQ_ENABLES_MRU);
6807 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6808 		req->rss_rule = cpu_to_le16(vnic0->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 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6812 	} else {
6813 		req->rss_rule = cpu_to_le16(0xffff);
6814 	}
6815 
6816 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6817 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6818 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6819 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6820 	} else {
6821 		req->cos_rule = cpu_to_le16(0xffff);
6822 	}
6823 
6824 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6825 		ring = 0;
6826 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6827 		ring = vnic->vnic_id - 1;
6828 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6829 		ring = bp->rx_nr_rings - 1;
6830 
6831 	grp_idx = bp->rx_ring[ring].bnapi->index;
6832 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6833 	req->lb_rule = cpu_to_le16(0xffff);
6834 vnic_mru:
6835 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6836 	req->mru = cpu_to_le16(vnic->mru);
6837 
6838 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6839 #ifdef CONFIG_BNXT_SRIOV
6840 	if (BNXT_VF(bp))
6841 		def_vlan = bp->vf.vlan;
6842 #endif
6843 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6844 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6845 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6846 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6847 
6848 	return hwrm_req_send(bp, req);
6849 }
6850 
6851 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6852 				    struct bnxt_vnic_info *vnic)
6853 {
6854 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6855 		struct hwrm_vnic_free_input *req;
6856 
6857 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6858 			return;
6859 
6860 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6861 
6862 		hwrm_req_send(bp, req);
6863 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6864 	}
6865 }
6866 
6867 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6868 {
6869 	u16 i;
6870 
6871 	for (i = 0; i < bp->nr_vnics; i++)
6872 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6873 }
6874 
6875 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6876 			 unsigned int start_rx_ring_idx,
6877 			 unsigned int nr_rings)
6878 {
6879 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6880 	struct hwrm_vnic_alloc_output *resp;
6881 	struct hwrm_vnic_alloc_input *req;
6882 	int rc;
6883 
6884 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6885 	if (rc)
6886 		return rc;
6887 
6888 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6889 		goto vnic_no_ring_grps;
6890 
6891 	/* map ring groups to this vnic */
6892 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6893 		grp_idx = bp->rx_ring[i].bnapi->index;
6894 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6895 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6896 				   j, nr_rings);
6897 			break;
6898 		}
6899 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6900 	}
6901 
6902 vnic_no_ring_grps:
6903 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6904 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6905 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6906 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6907 
6908 	resp = hwrm_req_hold(bp, req);
6909 	rc = hwrm_req_send(bp, req);
6910 	if (!rc)
6911 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6912 	hwrm_req_drop(bp, req);
6913 	return rc;
6914 }
6915 
6916 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6917 {
6918 	struct hwrm_vnic_qcaps_output *resp;
6919 	struct hwrm_vnic_qcaps_input *req;
6920 	int rc;
6921 
6922 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6923 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6924 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6925 	if (bp->hwrm_spec_code < 0x10600)
6926 		return 0;
6927 
6928 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6929 	if (rc)
6930 		return rc;
6931 
6932 	resp = hwrm_req_hold(bp, req);
6933 	rc = hwrm_req_send(bp, req);
6934 	if (!rc) {
6935 		u32 flags = le32_to_cpu(resp->flags);
6936 
6937 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6938 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6939 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6940 		if (flags &
6941 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6942 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6943 
6944 		/* Older P5 fw before EXT_HW_STATS support did not set
6945 		 * VLAN_STRIP_CAP properly.
6946 		 */
6947 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6948 		    (BNXT_CHIP_P5(bp) &&
6949 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6950 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6951 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6952 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6953 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6954 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6955 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6956 		if (bp->max_tpa_v2) {
6957 			if (BNXT_CHIP_P5(bp))
6958 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6959 			else
6960 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6961 		}
6962 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6963 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6964 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6965 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6966 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6967 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6968 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6969 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6970 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6971 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6972 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6973 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6974 	}
6975 	hwrm_req_drop(bp, req);
6976 	return rc;
6977 }
6978 
6979 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6980 {
6981 	struct hwrm_ring_grp_alloc_output *resp;
6982 	struct hwrm_ring_grp_alloc_input *req;
6983 	int rc;
6984 	u16 i;
6985 
6986 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6987 		return 0;
6988 
6989 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6990 	if (rc)
6991 		return rc;
6992 
6993 	resp = hwrm_req_hold(bp, req);
6994 	for (i = 0; i < bp->rx_nr_rings; i++) {
6995 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6996 
6997 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6998 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6999 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7000 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7001 
7002 		rc = hwrm_req_send(bp, req);
7003 
7004 		if (rc)
7005 			break;
7006 
7007 		bp->grp_info[grp_idx].fw_grp_id =
7008 			le32_to_cpu(resp->ring_group_id);
7009 	}
7010 	hwrm_req_drop(bp, req);
7011 	return rc;
7012 }
7013 
7014 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7015 {
7016 	struct hwrm_ring_grp_free_input *req;
7017 	u16 i;
7018 
7019 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7020 		return;
7021 
7022 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7023 		return;
7024 
7025 	hwrm_req_hold(bp, req);
7026 	for (i = 0; i < bp->cp_nr_rings; i++) {
7027 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7028 			continue;
7029 		req->ring_group_id =
7030 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7031 
7032 		hwrm_req_send(bp, req);
7033 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7034 	}
7035 	hwrm_req_drop(bp, req);
7036 }
7037 
7038 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7039 				       struct hwrm_ring_alloc_input *req,
7040 				       struct bnxt_ring_struct *ring)
7041 {
7042 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7043 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7044 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7045 
7046 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7047 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7048 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7049 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7050 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7051 	} else {
7052 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7053 		if (NET_IP_ALIGN == 2)
7054 			req->flags =
7055 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7056 	}
7057 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7058 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7059 	req->enables |= cpu_to_le32(enables);
7060 }
7061 
7062 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7063 				    struct bnxt_ring_struct *ring,
7064 				    u32 ring_type, u32 map_index)
7065 {
7066 	struct hwrm_ring_alloc_output *resp;
7067 	struct hwrm_ring_alloc_input *req;
7068 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7069 	struct bnxt_ring_grp_info *grp_info;
7070 	int rc, err = 0;
7071 	u16 ring_id;
7072 
7073 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7074 	if (rc)
7075 		goto exit;
7076 
7077 	req->enables = 0;
7078 	if (rmem->nr_pages > 1) {
7079 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7080 		/* Page size is in log2 units */
7081 		req->page_size = BNXT_PAGE_SHIFT;
7082 		req->page_tbl_depth = 1;
7083 	} else {
7084 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7085 	}
7086 	req->fbo = 0;
7087 	/* Association of ring index with doorbell index and MSIX number */
7088 	req->logical_id = cpu_to_le16(map_index);
7089 
7090 	switch (ring_type) {
7091 	case HWRM_RING_ALLOC_TX: {
7092 		struct bnxt_tx_ring_info *txr;
7093 		u16 flags = 0;
7094 
7095 		txr = container_of(ring, struct bnxt_tx_ring_info,
7096 				   tx_ring_struct);
7097 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7098 		/* Association of transmit ring with completion ring */
7099 		grp_info = &bp->grp_info[ring->grp_idx];
7100 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7101 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7102 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7103 		req->queue_id = cpu_to_le16(ring->queue_id);
7104 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7105 			req->cmpl_coal_cnt =
7106 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7107 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7108 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7109 		req->flags = cpu_to_le16(flags);
7110 		break;
7111 	}
7112 	case HWRM_RING_ALLOC_RX:
7113 	case HWRM_RING_ALLOC_AGG:
7114 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7115 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7116 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7117 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7118 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7119 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7120 		break;
7121 	case HWRM_RING_ALLOC_CMPL:
7122 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7123 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7124 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7125 			/* Association of cp ring with nq */
7126 			grp_info = &bp->grp_info[map_index];
7127 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7128 			req->cq_handle = cpu_to_le64(ring->handle);
7129 			req->enables |= cpu_to_le32(
7130 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7131 		} else {
7132 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7133 		}
7134 		break;
7135 	case HWRM_RING_ALLOC_NQ:
7136 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7137 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7138 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7139 		break;
7140 	default:
7141 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7142 			   ring_type);
7143 		return -EINVAL;
7144 	}
7145 
7146 	resp = hwrm_req_hold(bp, req);
7147 	rc = hwrm_req_send(bp, req);
7148 	err = le16_to_cpu(resp->error_code);
7149 	ring_id = le16_to_cpu(resp->ring_id);
7150 	hwrm_req_drop(bp, req);
7151 
7152 exit:
7153 	if (rc || err) {
7154 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7155 			   ring_type, rc, err);
7156 		return -EIO;
7157 	}
7158 	ring->fw_ring_id = ring_id;
7159 	return rc;
7160 }
7161 
7162 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7163 {
7164 	int rc;
7165 
7166 	if (BNXT_PF(bp)) {
7167 		struct hwrm_func_cfg_input *req;
7168 
7169 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7170 		if (rc)
7171 			return rc;
7172 
7173 		req->fid = cpu_to_le16(0xffff);
7174 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7175 		req->async_event_cr = cpu_to_le16(idx);
7176 		return hwrm_req_send(bp, req);
7177 	} else {
7178 		struct hwrm_func_vf_cfg_input *req;
7179 
7180 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7181 		if (rc)
7182 			return rc;
7183 
7184 		req->enables =
7185 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7186 		req->async_event_cr = cpu_to_le16(idx);
7187 		return hwrm_req_send(bp, req);
7188 	}
7189 }
7190 
7191 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7192 			     u32 ring_type)
7193 {
7194 	switch (ring_type) {
7195 	case HWRM_RING_ALLOC_TX:
7196 		db->db_ring_mask = bp->tx_ring_mask;
7197 		break;
7198 	case HWRM_RING_ALLOC_RX:
7199 		db->db_ring_mask = bp->rx_ring_mask;
7200 		break;
7201 	case HWRM_RING_ALLOC_AGG:
7202 		db->db_ring_mask = bp->rx_agg_ring_mask;
7203 		break;
7204 	case HWRM_RING_ALLOC_CMPL:
7205 	case HWRM_RING_ALLOC_NQ:
7206 		db->db_ring_mask = bp->cp_ring_mask;
7207 		break;
7208 	}
7209 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7210 		db->db_epoch_mask = db->db_ring_mask + 1;
7211 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7212 	}
7213 }
7214 
7215 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7216 			u32 map_idx, u32 xid)
7217 {
7218 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7219 		switch (ring_type) {
7220 		case HWRM_RING_ALLOC_TX:
7221 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7222 			break;
7223 		case HWRM_RING_ALLOC_RX:
7224 		case HWRM_RING_ALLOC_AGG:
7225 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7226 			break;
7227 		case HWRM_RING_ALLOC_CMPL:
7228 			db->db_key64 = DBR_PATH_L2;
7229 			break;
7230 		case HWRM_RING_ALLOC_NQ:
7231 			db->db_key64 = DBR_PATH_L2;
7232 			break;
7233 		}
7234 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7235 
7236 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7237 			db->db_key64 |= DBR_VALID;
7238 
7239 		db->doorbell = bp->bar1 + bp->db_offset;
7240 	} else {
7241 		db->doorbell = bp->bar1 + map_idx * 0x80;
7242 		switch (ring_type) {
7243 		case HWRM_RING_ALLOC_TX:
7244 			db->db_key32 = DB_KEY_TX;
7245 			break;
7246 		case HWRM_RING_ALLOC_RX:
7247 		case HWRM_RING_ALLOC_AGG:
7248 			db->db_key32 = DB_KEY_RX;
7249 			break;
7250 		case HWRM_RING_ALLOC_CMPL:
7251 			db->db_key32 = DB_KEY_CP;
7252 			break;
7253 		}
7254 	}
7255 	bnxt_set_db_mask(bp, db, ring_type);
7256 }
7257 
7258 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7259 				   struct bnxt_rx_ring_info *rxr)
7260 {
7261 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7262 	struct bnxt_napi *bnapi = rxr->bnapi;
7263 	u32 type = HWRM_RING_ALLOC_RX;
7264 	u32 map_idx = bnapi->index;
7265 	int rc;
7266 
7267 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7268 	if (rc)
7269 		return rc;
7270 
7271 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7272 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7273 
7274 	return 0;
7275 }
7276 
7277 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7278 				       struct bnxt_rx_ring_info *rxr)
7279 {
7280 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7281 	u32 type = HWRM_RING_ALLOC_AGG;
7282 	u32 grp_idx = ring->grp_idx;
7283 	u32 map_idx;
7284 	int rc;
7285 
7286 	map_idx = grp_idx + bp->rx_nr_rings;
7287 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7288 	if (rc)
7289 		return rc;
7290 
7291 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7292 		    ring->fw_ring_id);
7293 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7294 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7295 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7296 
7297 	return 0;
7298 }
7299 
7300 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7301 				      struct bnxt_cp_ring_info *cpr)
7302 {
7303 	const u32 type = HWRM_RING_ALLOC_CMPL;
7304 	struct bnxt_napi *bnapi = cpr->bnapi;
7305 	struct bnxt_ring_struct *ring;
7306 	u32 map_idx = bnapi->index;
7307 	int rc;
7308 
7309 	ring = &cpr->cp_ring_struct;
7310 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7311 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7312 	if (rc)
7313 		return rc;
7314 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7315 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7316 	return 0;
7317 }
7318 
7319 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7320 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7321 {
7322 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7323 	const u32 type = HWRM_RING_ALLOC_TX;
7324 	int rc;
7325 
7326 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7327 	if (rc)
7328 		return rc;
7329 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7330 	return 0;
7331 }
7332 
7333 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7334 {
7335 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7336 	int i, rc = 0;
7337 	u32 type;
7338 
7339 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7340 		type = HWRM_RING_ALLOC_NQ;
7341 	else
7342 		type = HWRM_RING_ALLOC_CMPL;
7343 	for (i = 0; i < bp->cp_nr_rings; i++) {
7344 		struct bnxt_napi *bnapi = bp->bnapi[i];
7345 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7346 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7347 		u32 map_idx = ring->map_idx;
7348 		unsigned int vector;
7349 
7350 		vector = bp->irq_tbl[map_idx].vector;
7351 		disable_irq_nosync(vector);
7352 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7353 		if (rc) {
7354 			enable_irq(vector);
7355 			goto err_out;
7356 		}
7357 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7358 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7359 		enable_irq(vector);
7360 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7361 
7362 		if (!i) {
7363 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7364 			if (rc)
7365 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7366 		}
7367 	}
7368 
7369 	for (i = 0; i < bp->tx_nr_rings; i++) {
7370 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7371 
7372 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7373 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7374 			if (rc)
7375 				goto err_out;
7376 		}
7377 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7378 		if (rc)
7379 			goto err_out;
7380 	}
7381 
7382 	for (i = 0; i < bp->rx_nr_rings; i++) {
7383 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7384 
7385 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7386 		if (rc)
7387 			goto err_out;
7388 		/* If we have agg rings, post agg buffers first. */
7389 		if (!agg_rings)
7390 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7391 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7392 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7393 			if (rc)
7394 				goto err_out;
7395 		}
7396 	}
7397 
7398 	if (agg_rings) {
7399 		for (i = 0; i < bp->rx_nr_rings; i++) {
7400 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7401 			if (rc)
7402 				goto err_out;
7403 		}
7404 	}
7405 err_out:
7406 	return rc;
7407 }
7408 
7409 static void bnxt_cancel_dim(struct bnxt *bp)
7410 {
7411 	int i;
7412 
7413 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7414 	 * if NAPI is enabled.
7415 	 */
7416 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7417 		return;
7418 
7419 	/* Make sure NAPI sees that the VNIC is disabled */
7420 	synchronize_net();
7421 	for (i = 0; i < bp->rx_nr_rings; i++) {
7422 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7423 		struct bnxt_napi *bnapi = rxr->bnapi;
7424 
7425 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7426 	}
7427 }
7428 
7429 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7430 				   struct bnxt_ring_struct *ring,
7431 				   u32 ring_type, int cmpl_ring_id)
7432 {
7433 	struct hwrm_ring_free_output *resp;
7434 	struct hwrm_ring_free_input *req;
7435 	u16 error_code = 0;
7436 	int rc;
7437 
7438 	if (BNXT_NO_FW_ACCESS(bp))
7439 		return 0;
7440 
7441 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7442 	if (rc)
7443 		goto exit;
7444 
7445 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7446 	req->ring_type = ring_type;
7447 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7448 
7449 	resp = hwrm_req_hold(bp, req);
7450 	rc = hwrm_req_send(bp, req);
7451 	error_code = le16_to_cpu(resp->error_code);
7452 	hwrm_req_drop(bp, req);
7453 exit:
7454 	if (rc || error_code) {
7455 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7456 			   ring_type, rc, error_code);
7457 		return -EIO;
7458 	}
7459 	return 0;
7460 }
7461 
7462 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7463 				   struct bnxt_tx_ring_info *txr,
7464 				   bool close_path)
7465 {
7466 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7467 	u32 cmpl_ring_id;
7468 
7469 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7470 		return;
7471 
7472 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7473 		       INVALID_HW_RING_ID;
7474 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7475 				cmpl_ring_id);
7476 	ring->fw_ring_id = INVALID_HW_RING_ID;
7477 }
7478 
7479 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7480 				   struct bnxt_rx_ring_info *rxr,
7481 				   bool close_path)
7482 {
7483 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7484 	u32 grp_idx = rxr->bnapi->index;
7485 	u32 cmpl_ring_id;
7486 
7487 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7488 		return;
7489 
7490 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7491 	hwrm_ring_free_send_msg(bp, ring,
7492 				RING_FREE_REQ_RING_TYPE_RX,
7493 				close_path ? cmpl_ring_id :
7494 				INVALID_HW_RING_ID);
7495 	ring->fw_ring_id = INVALID_HW_RING_ID;
7496 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7497 }
7498 
7499 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7500 				       struct bnxt_rx_ring_info *rxr,
7501 				       bool close_path)
7502 {
7503 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7504 	u32 grp_idx = rxr->bnapi->index;
7505 	u32 type, cmpl_ring_id;
7506 
7507 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7508 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7509 	else
7510 		type = RING_FREE_REQ_RING_TYPE_RX;
7511 
7512 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7513 		return;
7514 
7515 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7516 	hwrm_ring_free_send_msg(bp, ring, type,
7517 				close_path ? cmpl_ring_id :
7518 				INVALID_HW_RING_ID);
7519 	ring->fw_ring_id = INVALID_HW_RING_ID;
7520 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7521 }
7522 
7523 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7524 				   struct bnxt_cp_ring_info *cpr)
7525 {
7526 	struct bnxt_ring_struct *ring;
7527 
7528 	ring = &cpr->cp_ring_struct;
7529 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7530 		return;
7531 
7532 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7533 				INVALID_HW_RING_ID);
7534 	ring->fw_ring_id = INVALID_HW_RING_ID;
7535 }
7536 
7537 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7538 {
7539 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7540 	int i, size = ring->ring_mem.page_size;
7541 
7542 	cpr->cp_raw_cons = 0;
7543 	cpr->toggle = 0;
7544 
7545 	for (i = 0; i < bp->cp_nr_pages; i++)
7546 		if (cpr->cp_desc_ring[i])
7547 			memset(cpr->cp_desc_ring[i], 0, size);
7548 }
7549 
7550 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7551 {
7552 	u32 type;
7553 	int i;
7554 
7555 	if (!bp->bnapi)
7556 		return;
7557 
7558 	for (i = 0; i < bp->tx_nr_rings; i++)
7559 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7560 
7561 	bnxt_cancel_dim(bp);
7562 	for (i = 0; i < bp->rx_nr_rings; i++) {
7563 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7564 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7565 	}
7566 
7567 	/* The completion rings are about to be freed.  After that the
7568 	 * IRQ doorbell will not work anymore.  So we need to disable
7569 	 * IRQ here.
7570 	 */
7571 	bnxt_disable_int_sync(bp);
7572 
7573 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7574 		type = RING_FREE_REQ_RING_TYPE_NQ;
7575 	else
7576 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7577 	for (i = 0; i < bp->cp_nr_rings; i++) {
7578 		struct bnxt_napi *bnapi = bp->bnapi[i];
7579 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7580 		struct bnxt_ring_struct *ring;
7581 		int j;
7582 
7583 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7584 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7585 
7586 		ring = &cpr->cp_ring_struct;
7587 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7588 			hwrm_ring_free_send_msg(bp, ring, type,
7589 						INVALID_HW_RING_ID);
7590 			ring->fw_ring_id = INVALID_HW_RING_ID;
7591 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7592 		}
7593 	}
7594 }
7595 
7596 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7597 			     bool shared);
7598 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7599 			   bool shared);
7600 
7601 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7602 {
7603 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7604 	struct hwrm_func_qcfg_output *resp;
7605 	struct hwrm_func_qcfg_input *req;
7606 	int rc;
7607 
7608 	if (bp->hwrm_spec_code < 0x10601)
7609 		return 0;
7610 
7611 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7612 	if (rc)
7613 		return rc;
7614 
7615 	req->fid = cpu_to_le16(0xffff);
7616 	resp = hwrm_req_hold(bp, req);
7617 	rc = hwrm_req_send(bp, req);
7618 	if (rc) {
7619 		hwrm_req_drop(bp, req);
7620 		return rc;
7621 	}
7622 
7623 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7624 	if (BNXT_NEW_RM(bp)) {
7625 		u16 cp, stats;
7626 
7627 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7628 		hw_resc->resv_hw_ring_grps =
7629 			le32_to_cpu(resp->alloc_hw_ring_grps);
7630 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7631 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7632 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7633 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7634 		hw_resc->resv_irqs = cp;
7635 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7636 			int rx = hw_resc->resv_rx_rings;
7637 			int tx = hw_resc->resv_tx_rings;
7638 
7639 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7640 				rx >>= 1;
7641 			if (cp < (rx + tx)) {
7642 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7643 				if (rc)
7644 					goto get_rings_exit;
7645 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7646 					rx <<= 1;
7647 				hw_resc->resv_rx_rings = rx;
7648 				hw_resc->resv_tx_rings = tx;
7649 			}
7650 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7651 			hw_resc->resv_hw_ring_grps = rx;
7652 		}
7653 		hw_resc->resv_cp_rings = cp;
7654 		hw_resc->resv_stat_ctxs = stats;
7655 	}
7656 get_rings_exit:
7657 	hwrm_req_drop(bp, req);
7658 	return rc;
7659 }
7660 
7661 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7662 {
7663 	struct hwrm_func_qcfg_output *resp;
7664 	struct hwrm_func_qcfg_input *req;
7665 	int rc;
7666 
7667 	if (bp->hwrm_spec_code < 0x10601)
7668 		return 0;
7669 
7670 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7671 	if (rc)
7672 		return rc;
7673 
7674 	req->fid = cpu_to_le16(fid);
7675 	resp = hwrm_req_hold(bp, req);
7676 	rc = hwrm_req_send(bp, req);
7677 	if (!rc)
7678 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7679 
7680 	hwrm_req_drop(bp, req);
7681 	return rc;
7682 }
7683 
7684 static bool bnxt_rfs_supported(struct bnxt *bp);
7685 
7686 static struct hwrm_func_cfg_input *
7687 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7688 {
7689 	struct hwrm_func_cfg_input *req;
7690 	u32 enables = 0;
7691 
7692 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7693 		return NULL;
7694 
7695 	req->fid = cpu_to_le16(0xffff);
7696 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7697 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7698 	if (BNXT_NEW_RM(bp)) {
7699 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7700 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7701 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7702 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7703 			enables |= hwr->cp_p5 ?
7704 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7705 		} else {
7706 			enables |= hwr->cp ?
7707 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7708 			enables |= hwr->grp ?
7709 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7710 		}
7711 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7712 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7713 					  0;
7714 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7715 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7716 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7717 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7718 			req->num_msix = cpu_to_le16(hwr->cp);
7719 		} else {
7720 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7721 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7722 		}
7723 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7724 		req->num_vnics = cpu_to_le16(hwr->vnic);
7725 	}
7726 	req->enables = cpu_to_le32(enables);
7727 	return req;
7728 }
7729 
7730 static struct hwrm_func_vf_cfg_input *
7731 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7732 {
7733 	struct hwrm_func_vf_cfg_input *req;
7734 	u32 enables = 0;
7735 
7736 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7737 		return NULL;
7738 
7739 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7740 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7741 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7742 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7743 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7744 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7745 		enables |= hwr->cp_p5 ?
7746 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7747 	} else {
7748 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7749 		enables |= hwr->grp ?
7750 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7751 	}
7752 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7753 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7754 
7755 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7756 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7757 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7758 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7759 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7760 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7761 	} else {
7762 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7763 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7764 	}
7765 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7766 	req->num_vnics = cpu_to_le16(hwr->vnic);
7767 
7768 	req->enables = cpu_to_le32(enables);
7769 	return req;
7770 }
7771 
7772 static int
7773 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7774 {
7775 	struct hwrm_func_cfg_input *req;
7776 	int rc;
7777 
7778 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7779 	if (!req)
7780 		return -ENOMEM;
7781 
7782 	if (!req->enables) {
7783 		hwrm_req_drop(bp, req);
7784 		return 0;
7785 	}
7786 
7787 	rc = hwrm_req_send(bp, req);
7788 	if (rc)
7789 		return rc;
7790 
7791 	if (bp->hwrm_spec_code < 0x10601)
7792 		bp->hw_resc.resv_tx_rings = hwr->tx;
7793 
7794 	return bnxt_hwrm_get_rings(bp);
7795 }
7796 
7797 static int
7798 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7799 {
7800 	struct hwrm_func_vf_cfg_input *req;
7801 	int rc;
7802 
7803 	if (!BNXT_NEW_RM(bp)) {
7804 		bp->hw_resc.resv_tx_rings = hwr->tx;
7805 		return 0;
7806 	}
7807 
7808 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7809 	if (!req)
7810 		return -ENOMEM;
7811 
7812 	rc = hwrm_req_send(bp, req);
7813 	if (rc)
7814 		return rc;
7815 
7816 	return bnxt_hwrm_get_rings(bp);
7817 }
7818 
7819 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7820 {
7821 	if (BNXT_PF(bp))
7822 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7823 	else
7824 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7825 }
7826 
7827 int bnxt_nq_rings_in_use(struct bnxt *bp)
7828 {
7829 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7830 }
7831 
7832 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7833 {
7834 	int cp;
7835 
7836 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7837 		return bnxt_nq_rings_in_use(bp);
7838 
7839 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7840 	return cp;
7841 }
7842 
7843 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7844 {
7845 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7846 }
7847 
7848 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7849 {
7850 	if (!hwr->grp)
7851 		return 0;
7852 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7853 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7854 
7855 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7856 			rss_ctx *= hwr->vnic;
7857 		return rss_ctx;
7858 	}
7859 	if (BNXT_VF(bp))
7860 		return BNXT_VF_MAX_RSS_CTX;
7861 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7862 		return hwr->grp + 1;
7863 	return 1;
7864 }
7865 
7866 /* Check if a default RSS map needs to be setup.  This function is only
7867  * used on older firmware that does not require reserving RX rings.
7868  */
7869 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7870 {
7871 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7872 
7873 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7874 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7875 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7876 		if (!netif_is_rxfh_configured(bp->dev))
7877 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7878 	}
7879 }
7880 
7881 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7882 {
7883 	if (bp->flags & BNXT_FLAG_RFS) {
7884 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7885 			return 2 + bp->num_rss_ctx;
7886 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7887 			return rx_rings + 1;
7888 	}
7889 	return 1;
7890 }
7891 
7892 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7893 {
7894 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7895 	int cp = bnxt_cp_rings_in_use(bp);
7896 	int nq = bnxt_nq_rings_in_use(bp);
7897 	int rx = bp->rx_nr_rings, stat;
7898 	int vnic, grp = rx;
7899 
7900 	/* Old firmware does not need RX ring reservations but we still
7901 	 * need to setup a default RSS map when needed.  With new firmware
7902 	 * we go through RX ring reservations first and then set up the
7903 	 * RSS map for the successfully reserved RX rings when needed.
7904 	 */
7905 	if (!BNXT_NEW_RM(bp))
7906 		bnxt_check_rss_tbl_no_rmgr(bp);
7907 
7908 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7909 	    bp->hwrm_spec_code >= 0x10601)
7910 		return true;
7911 
7912 	if (!BNXT_NEW_RM(bp))
7913 		return false;
7914 
7915 	vnic = bnxt_get_total_vnics(bp, rx);
7916 
7917 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7918 		rx <<= 1;
7919 	stat = bnxt_get_func_stat_ctxs(bp);
7920 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7921 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7922 	    (hw_resc->resv_hw_ring_grps != grp &&
7923 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7924 		return true;
7925 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7926 	    hw_resc->resv_irqs != nq)
7927 		return true;
7928 	return false;
7929 }
7930 
7931 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7932 {
7933 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7934 
7935 	hwr->tx = hw_resc->resv_tx_rings;
7936 	if (BNXT_NEW_RM(bp)) {
7937 		hwr->rx = hw_resc->resv_rx_rings;
7938 		hwr->cp = hw_resc->resv_irqs;
7939 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7940 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7941 		hwr->grp = hw_resc->resv_hw_ring_grps;
7942 		hwr->vnic = hw_resc->resv_vnics;
7943 		hwr->stat = hw_resc->resv_stat_ctxs;
7944 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7945 	}
7946 }
7947 
7948 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7949 {
7950 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7951 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7952 }
7953 
7954 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7955 
7956 static int __bnxt_reserve_rings(struct bnxt *bp)
7957 {
7958 	struct bnxt_hw_rings hwr = {0};
7959 	int rx_rings, old_rx_rings, rc;
7960 	int cp = bp->cp_nr_rings;
7961 	int ulp_msix = 0;
7962 	bool sh = false;
7963 	int tx_cp;
7964 
7965 	if (!bnxt_need_reserve_rings(bp))
7966 		return 0;
7967 
7968 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7969 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7970 		if (!ulp_msix)
7971 			bnxt_set_ulp_stat_ctxs(bp, 0);
7972 
7973 		if (ulp_msix > bp->ulp_num_msix_want)
7974 			ulp_msix = bp->ulp_num_msix_want;
7975 		hwr.cp = cp + ulp_msix;
7976 	} else {
7977 		hwr.cp = bnxt_nq_rings_in_use(bp);
7978 	}
7979 
7980 	hwr.tx = bp->tx_nr_rings;
7981 	hwr.rx = bp->rx_nr_rings;
7982 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7983 		sh = true;
7984 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7985 		hwr.cp_p5 = hwr.rx + hwr.tx;
7986 
7987 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7988 
7989 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7990 		hwr.rx <<= 1;
7991 	hwr.grp = bp->rx_nr_rings;
7992 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7993 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7994 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7995 
7996 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7997 	if (rc)
7998 		return rc;
7999 
8000 	bnxt_copy_reserved_rings(bp, &hwr);
8001 
8002 	rx_rings = hwr.rx;
8003 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8004 		if (hwr.rx >= 2) {
8005 			rx_rings = hwr.rx >> 1;
8006 		} else {
8007 			if (netif_running(bp->dev))
8008 				return -ENOMEM;
8009 
8010 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8011 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8012 			bp->dev->hw_features &= ~NETIF_F_LRO;
8013 			bp->dev->features &= ~NETIF_F_LRO;
8014 			bnxt_set_ring_params(bp);
8015 		}
8016 	}
8017 	rx_rings = min_t(int, rx_rings, hwr.grp);
8018 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8019 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8020 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8021 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8022 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8023 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8024 		hwr.rx = rx_rings << 1;
8025 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8026 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8027 	bp->tx_nr_rings = hwr.tx;
8028 
8029 	/* If we cannot reserve all the RX rings, reset the RSS map only
8030 	 * if absolutely necessary
8031 	 */
8032 	if (rx_rings != bp->rx_nr_rings) {
8033 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8034 			    rx_rings, bp->rx_nr_rings);
8035 		if (netif_is_rxfh_configured(bp->dev) &&
8036 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8037 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8038 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8039 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8040 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8041 		}
8042 	}
8043 	bp->rx_nr_rings = rx_rings;
8044 	bp->cp_nr_rings = hwr.cp;
8045 
8046 	if (!bnxt_rings_ok(bp, &hwr))
8047 		return -ENOMEM;
8048 
8049 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8050 	    !netif_is_rxfh_configured(bp->dev))
8051 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8052 
8053 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8054 		int resv_msix, resv_ctx, ulp_ctxs;
8055 		struct bnxt_hw_resc *hw_resc;
8056 
8057 		hw_resc = &bp->hw_resc;
8058 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8059 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8060 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8061 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8062 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8063 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8064 	}
8065 
8066 	return rc;
8067 }
8068 
8069 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8070 {
8071 	struct hwrm_func_vf_cfg_input *req;
8072 	u32 flags;
8073 
8074 	if (!BNXT_NEW_RM(bp))
8075 		return 0;
8076 
8077 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8078 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8079 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8080 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8081 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8082 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8083 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8084 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8085 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8086 
8087 	req->flags = cpu_to_le32(flags);
8088 	return hwrm_req_send_silent(bp, req);
8089 }
8090 
8091 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8092 {
8093 	struct hwrm_func_cfg_input *req;
8094 	u32 flags;
8095 
8096 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8097 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8098 	if (BNXT_NEW_RM(bp)) {
8099 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8100 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8101 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8102 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8103 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8104 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8105 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8106 		else
8107 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8108 	}
8109 
8110 	req->flags = cpu_to_le32(flags);
8111 	return hwrm_req_send_silent(bp, req);
8112 }
8113 
8114 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8115 {
8116 	if (bp->hwrm_spec_code < 0x10801)
8117 		return 0;
8118 
8119 	if (BNXT_PF(bp))
8120 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8121 
8122 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8123 }
8124 
8125 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8126 {
8127 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8128 	struct hwrm_ring_aggint_qcaps_output *resp;
8129 	struct hwrm_ring_aggint_qcaps_input *req;
8130 	int rc;
8131 
8132 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8133 	coal_cap->num_cmpl_dma_aggr_max = 63;
8134 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8135 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8136 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8137 	coal_cap->int_lat_tmr_min_max = 65535;
8138 	coal_cap->int_lat_tmr_max_max = 65535;
8139 	coal_cap->num_cmpl_aggr_int_max = 65535;
8140 	coal_cap->timer_units = 80;
8141 
8142 	if (bp->hwrm_spec_code < 0x10902)
8143 		return;
8144 
8145 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8146 		return;
8147 
8148 	resp = hwrm_req_hold(bp, req);
8149 	rc = hwrm_req_send_silent(bp, req);
8150 	if (!rc) {
8151 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8152 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8153 		coal_cap->num_cmpl_dma_aggr_max =
8154 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8155 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8156 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8157 		coal_cap->cmpl_aggr_dma_tmr_max =
8158 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8159 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8160 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8161 		coal_cap->int_lat_tmr_min_max =
8162 			le16_to_cpu(resp->int_lat_tmr_min_max);
8163 		coal_cap->int_lat_tmr_max_max =
8164 			le16_to_cpu(resp->int_lat_tmr_max_max);
8165 		coal_cap->num_cmpl_aggr_int_max =
8166 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8167 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8168 	}
8169 	hwrm_req_drop(bp, req);
8170 }
8171 
8172 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8173 {
8174 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8175 
8176 	return usec * 1000 / coal_cap->timer_units;
8177 }
8178 
8179 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8180 	struct bnxt_coal *hw_coal,
8181 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8182 {
8183 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8184 	u16 val, tmr, max, flags = hw_coal->flags;
8185 	u32 cmpl_params = coal_cap->cmpl_params;
8186 
8187 	max = hw_coal->bufs_per_record * 128;
8188 	if (hw_coal->budget)
8189 		max = hw_coal->bufs_per_record * hw_coal->budget;
8190 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8191 
8192 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8193 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8194 
8195 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8196 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8197 
8198 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8199 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8200 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8201 
8202 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8203 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8204 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8205 
8206 	/* min timer set to 1/2 of interrupt timer */
8207 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8208 		val = tmr / 2;
8209 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8210 		req->int_lat_tmr_min = cpu_to_le16(val);
8211 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8212 	}
8213 
8214 	/* buf timer set to 1/4 of interrupt timer */
8215 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8216 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8217 
8218 	if (cmpl_params &
8219 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8220 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8221 		val = clamp_t(u16, tmr, 1,
8222 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8223 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8224 		req->enables |=
8225 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8226 	}
8227 
8228 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8229 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8230 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8231 	req->flags = cpu_to_le16(flags);
8232 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8233 }
8234 
8235 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8236 				   struct bnxt_coal *hw_coal)
8237 {
8238 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8239 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8240 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8241 	u32 nq_params = coal_cap->nq_params;
8242 	u16 tmr;
8243 	int rc;
8244 
8245 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8246 		return 0;
8247 
8248 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8249 	if (rc)
8250 		return rc;
8251 
8252 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8253 	req->flags =
8254 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8255 
8256 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8257 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8258 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8259 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8260 	return hwrm_req_send(bp, req);
8261 }
8262 
8263 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8264 {
8265 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8266 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8267 	struct bnxt_coal coal;
8268 	int rc;
8269 
8270 	/* Tick values in micro seconds.
8271 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8272 	 */
8273 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8274 
8275 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8276 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8277 
8278 	if (!bnapi->rx_ring)
8279 		return -ENODEV;
8280 
8281 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8282 	if (rc)
8283 		return rc;
8284 
8285 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8286 
8287 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8288 
8289 	return hwrm_req_send(bp, req_rx);
8290 }
8291 
8292 static int
8293 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8294 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8295 {
8296 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8297 
8298 	req->ring_id = cpu_to_le16(ring_id);
8299 	return hwrm_req_send(bp, req);
8300 }
8301 
8302 static int
8303 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8304 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8305 {
8306 	struct bnxt_tx_ring_info *txr;
8307 	int i, rc;
8308 
8309 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8310 		u16 ring_id;
8311 
8312 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8313 		req->ring_id = cpu_to_le16(ring_id);
8314 		rc = hwrm_req_send(bp, req);
8315 		if (rc)
8316 			return rc;
8317 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8318 			return 0;
8319 	}
8320 	return 0;
8321 }
8322 
8323 int bnxt_hwrm_set_coal(struct bnxt *bp)
8324 {
8325 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8326 	int i, rc;
8327 
8328 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8329 	if (rc)
8330 		return rc;
8331 
8332 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8333 	if (rc) {
8334 		hwrm_req_drop(bp, req_rx);
8335 		return rc;
8336 	}
8337 
8338 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8339 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8340 
8341 	hwrm_req_hold(bp, req_rx);
8342 	hwrm_req_hold(bp, req_tx);
8343 	for (i = 0; i < bp->cp_nr_rings; i++) {
8344 		struct bnxt_napi *bnapi = bp->bnapi[i];
8345 		struct bnxt_coal *hw_coal;
8346 
8347 		if (!bnapi->rx_ring)
8348 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8349 		else
8350 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8351 		if (rc)
8352 			break;
8353 
8354 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8355 			continue;
8356 
8357 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8358 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8359 			if (rc)
8360 				break;
8361 		}
8362 		if (bnapi->rx_ring)
8363 			hw_coal = &bp->rx_coal;
8364 		else
8365 			hw_coal = &bp->tx_coal;
8366 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8367 	}
8368 	hwrm_req_drop(bp, req_rx);
8369 	hwrm_req_drop(bp, req_tx);
8370 	return rc;
8371 }
8372 
8373 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8374 {
8375 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8376 	struct hwrm_stat_ctx_free_input *req;
8377 	int i;
8378 
8379 	if (!bp->bnapi)
8380 		return;
8381 
8382 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8383 		return;
8384 
8385 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8386 		return;
8387 	if (BNXT_FW_MAJ(bp) <= 20) {
8388 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8389 			hwrm_req_drop(bp, req);
8390 			return;
8391 		}
8392 		hwrm_req_hold(bp, req0);
8393 	}
8394 	hwrm_req_hold(bp, req);
8395 	for (i = 0; i < bp->cp_nr_rings; i++) {
8396 		struct bnxt_napi *bnapi = bp->bnapi[i];
8397 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8398 
8399 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8400 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8401 			if (req0) {
8402 				req0->stat_ctx_id = req->stat_ctx_id;
8403 				hwrm_req_send(bp, req0);
8404 			}
8405 			hwrm_req_send(bp, req);
8406 
8407 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8408 		}
8409 	}
8410 	hwrm_req_drop(bp, req);
8411 	if (req0)
8412 		hwrm_req_drop(bp, req0);
8413 }
8414 
8415 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8416 {
8417 	struct hwrm_stat_ctx_alloc_output *resp;
8418 	struct hwrm_stat_ctx_alloc_input *req;
8419 	int rc, i;
8420 
8421 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8422 		return 0;
8423 
8424 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8425 	if (rc)
8426 		return rc;
8427 
8428 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8429 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8430 
8431 	resp = hwrm_req_hold(bp, req);
8432 	for (i = 0; i < bp->cp_nr_rings; i++) {
8433 		struct bnxt_napi *bnapi = bp->bnapi[i];
8434 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8435 
8436 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8437 
8438 		rc = hwrm_req_send(bp, req);
8439 		if (rc)
8440 			break;
8441 
8442 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8443 
8444 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8445 	}
8446 	hwrm_req_drop(bp, req);
8447 	return rc;
8448 }
8449 
8450 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8451 {
8452 	struct hwrm_func_qcfg_output *resp;
8453 	struct hwrm_func_qcfg_input *req;
8454 	u16 flags;
8455 	int rc;
8456 
8457 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8458 	if (rc)
8459 		return rc;
8460 
8461 	req->fid = cpu_to_le16(0xffff);
8462 	resp = hwrm_req_hold(bp, req);
8463 	rc = hwrm_req_send(bp, req);
8464 	if (rc)
8465 		goto func_qcfg_exit;
8466 
8467 	flags = le16_to_cpu(resp->flags);
8468 #ifdef CONFIG_BNXT_SRIOV
8469 	if (BNXT_VF(bp)) {
8470 		struct bnxt_vf_info *vf = &bp->vf;
8471 
8472 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8473 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8474 			vf->flags |= BNXT_VF_TRUST;
8475 		else
8476 			vf->flags &= ~BNXT_VF_TRUST;
8477 	} else {
8478 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8479 	}
8480 #endif
8481 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8482 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8483 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8484 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8485 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8486 	}
8487 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8488 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8489 
8490 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8491 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8492 
8493 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8494 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8495 
8496 	switch (resp->port_partition_type) {
8497 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8498 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8499 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8500 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8501 		bp->port_partition_type = resp->port_partition_type;
8502 		break;
8503 	}
8504 	if (bp->hwrm_spec_code < 0x10707 ||
8505 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8506 		bp->br_mode = BRIDGE_MODE_VEB;
8507 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8508 		bp->br_mode = BRIDGE_MODE_VEPA;
8509 	else
8510 		bp->br_mode = BRIDGE_MODE_UNDEF;
8511 
8512 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8513 	if (!bp->max_mtu)
8514 		bp->max_mtu = BNXT_MAX_MTU;
8515 
8516 	if (bp->db_size)
8517 		goto func_qcfg_exit;
8518 
8519 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8520 	if (BNXT_CHIP_P5(bp)) {
8521 		if (BNXT_PF(bp))
8522 			bp->db_offset = DB_PF_OFFSET_P5;
8523 		else
8524 			bp->db_offset = DB_VF_OFFSET_P5;
8525 	}
8526 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8527 				 1024);
8528 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8529 	    bp->db_size <= bp->db_offset)
8530 		bp->db_size = pci_resource_len(bp->pdev, 2);
8531 
8532 func_qcfg_exit:
8533 	hwrm_req_drop(bp, req);
8534 	return rc;
8535 }
8536 
8537 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8538 				      u8 init_val, u8 init_offset,
8539 				      bool init_mask_set)
8540 {
8541 	ctxm->init_value = init_val;
8542 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8543 	if (init_mask_set)
8544 		ctxm->init_offset = init_offset * 4;
8545 	else
8546 		ctxm->init_value = 0;
8547 }
8548 
8549 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8550 {
8551 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8552 	u16 type;
8553 
8554 	for (type = 0; type < ctx_max; type++) {
8555 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8556 		int n = 1;
8557 
8558 		if (!ctxm->max_entries || ctxm->pg_info)
8559 			continue;
8560 
8561 		if (ctxm->instance_bmap)
8562 			n = hweight32(ctxm->instance_bmap);
8563 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8564 		if (!ctxm->pg_info)
8565 			return -ENOMEM;
8566 	}
8567 	return 0;
8568 }
8569 
8570 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8571 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8572 
8573 #define BNXT_CTX_INIT_VALID(flags)	\
8574 	(!!((flags) &			\
8575 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8576 
8577 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8578 {
8579 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8580 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8581 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8582 	u16 type;
8583 	int rc;
8584 
8585 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8586 	if (rc)
8587 		return rc;
8588 
8589 	if (!ctx) {
8590 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8591 		if (!ctx)
8592 			return -ENOMEM;
8593 		bp->ctx = ctx;
8594 	}
8595 
8596 	resp = hwrm_req_hold(bp, req);
8597 
8598 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8599 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8600 		u8 init_val, init_off, i;
8601 		u32 max_entries;
8602 		u16 entry_size;
8603 		__le32 *p;
8604 		u32 flags;
8605 
8606 		req->type = cpu_to_le16(type);
8607 		rc = hwrm_req_send(bp, req);
8608 		if (rc)
8609 			goto ctx_done;
8610 		flags = le32_to_cpu(resp->flags);
8611 		type = le16_to_cpu(resp->next_valid_type);
8612 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8613 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8614 			continue;
8615 		}
8616 		entry_size = le16_to_cpu(resp->entry_size);
8617 		max_entries = le32_to_cpu(resp->max_num_entries);
8618 		if (ctxm->mem_valid) {
8619 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8620 			    ctxm->entry_size != entry_size ||
8621 			    ctxm->max_entries != max_entries)
8622 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8623 			else
8624 				continue;
8625 		}
8626 		ctxm->type = le16_to_cpu(resp->type);
8627 		ctxm->entry_size = entry_size;
8628 		ctxm->flags = flags;
8629 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8630 		ctxm->entry_multiple = resp->entry_multiple;
8631 		ctxm->max_entries = max_entries;
8632 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8633 		init_val = resp->ctx_init_value;
8634 		init_off = resp->ctx_init_offset;
8635 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8636 					  BNXT_CTX_INIT_VALID(flags));
8637 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8638 					      BNXT_MAX_SPLIT_ENTRY);
8639 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8640 		     i++, p++)
8641 			ctxm->split[i] = le32_to_cpu(*p);
8642 	}
8643 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8644 
8645 ctx_done:
8646 	hwrm_req_drop(bp, req);
8647 	return rc;
8648 }
8649 
8650 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8651 {
8652 	struct hwrm_func_backing_store_qcaps_output *resp;
8653 	struct hwrm_func_backing_store_qcaps_input *req;
8654 	int rc;
8655 
8656 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8657 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8658 		return 0;
8659 
8660 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8661 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8662 
8663 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8664 	if (rc)
8665 		return rc;
8666 
8667 	resp = hwrm_req_hold(bp, req);
8668 	rc = hwrm_req_send_silent(bp, req);
8669 	if (!rc) {
8670 		struct bnxt_ctx_mem_type *ctxm;
8671 		struct bnxt_ctx_mem_info *ctx;
8672 		u8 init_val, init_idx = 0;
8673 		u16 init_mask;
8674 
8675 		ctx = bp->ctx;
8676 		if (!ctx) {
8677 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8678 			if (!ctx) {
8679 				rc = -ENOMEM;
8680 				goto ctx_err;
8681 			}
8682 			bp->ctx = ctx;
8683 		}
8684 		init_val = resp->ctx_kind_initializer;
8685 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8686 
8687 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8688 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8689 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8690 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8691 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8692 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8693 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8694 					  (init_mask & (1 << init_idx++)) != 0);
8695 
8696 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8697 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8698 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8699 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8700 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8701 					  (init_mask & (1 << init_idx++)) != 0);
8702 
8703 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8704 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8705 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8706 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8707 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8708 					  (init_mask & (1 << init_idx++)) != 0);
8709 
8710 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8711 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8712 		ctxm->max_entries = ctxm->vnic_entries +
8713 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8714 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8715 		bnxt_init_ctx_initializer(ctxm, init_val,
8716 					  resp->vnic_init_offset,
8717 					  (init_mask & (1 << init_idx++)) != 0);
8718 
8719 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8720 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8721 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8722 		bnxt_init_ctx_initializer(ctxm, init_val,
8723 					  resp->stat_init_offset,
8724 					  (init_mask & (1 << init_idx++)) != 0);
8725 
8726 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8727 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8728 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8729 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8730 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8731 		if (!ctxm->entry_multiple)
8732 			ctxm->entry_multiple = 1;
8733 
8734 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8735 
8736 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8737 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8738 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8739 		ctxm->mrav_num_entries_units =
8740 			le16_to_cpu(resp->mrav_num_entries_units);
8741 		bnxt_init_ctx_initializer(ctxm, init_val,
8742 					  resp->mrav_init_offset,
8743 					  (init_mask & (1 << init_idx++)) != 0);
8744 
8745 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8746 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8747 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8748 
8749 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8750 		if (!ctx->tqm_fp_rings_count)
8751 			ctx->tqm_fp_rings_count = bp->max_q;
8752 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8753 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8754 
8755 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8756 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8757 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8758 
8759 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8760 	} else {
8761 		rc = 0;
8762 	}
8763 ctx_err:
8764 	hwrm_req_drop(bp, req);
8765 	return rc;
8766 }
8767 
8768 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8769 				  __le64 *pg_dir)
8770 {
8771 	if (!rmem->nr_pages)
8772 		return;
8773 
8774 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8775 	if (rmem->depth >= 1) {
8776 		if (rmem->depth == 2)
8777 			*pg_attr |= 2;
8778 		else
8779 			*pg_attr |= 1;
8780 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8781 	} else {
8782 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8783 	}
8784 }
8785 
8786 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8787 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8788 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8789 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8790 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8791 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8792 
8793 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8794 {
8795 	struct hwrm_func_backing_store_cfg_input *req;
8796 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8797 	struct bnxt_ctx_pg_info *ctx_pg;
8798 	struct bnxt_ctx_mem_type *ctxm;
8799 	void **__req = (void **)&req;
8800 	u32 req_len = sizeof(*req);
8801 	__le32 *num_entries;
8802 	__le64 *pg_dir;
8803 	u32 flags = 0;
8804 	u8 *pg_attr;
8805 	u32 ena;
8806 	int rc;
8807 	int i;
8808 
8809 	if (!ctx)
8810 		return 0;
8811 
8812 	if (req_len > bp->hwrm_max_ext_req_len)
8813 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8814 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8815 	if (rc)
8816 		return rc;
8817 
8818 	req->enables = cpu_to_le32(enables);
8819 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8820 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8821 		ctx_pg = ctxm->pg_info;
8822 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8823 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8824 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8825 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8826 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8827 				      &req->qpc_pg_size_qpc_lvl,
8828 				      &req->qpc_page_dir);
8829 
8830 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8831 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8832 	}
8833 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8834 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8835 		ctx_pg = ctxm->pg_info;
8836 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8837 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8838 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8839 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8840 				      &req->srq_pg_size_srq_lvl,
8841 				      &req->srq_page_dir);
8842 	}
8843 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8844 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8845 		ctx_pg = ctxm->pg_info;
8846 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8847 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8848 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8849 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8850 				      &req->cq_pg_size_cq_lvl,
8851 				      &req->cq_page_dir);
8852 	}
8853 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8854 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8855 		ctx_pg = ctxm->pg_info;
8856 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8857 		req->vnic_num_ring_table_entries =
8858 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8859 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8860 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8861 				      &req->vnic_pg_size_vnic_lvl,
8862 				      &req->vnic_page_dir);
8863 	}
8864 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8865 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8866 		ctx_pg = ctxm->pg_info;
8867 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8868 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8869 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8870 				      &req->stat_pg_size_stat_lvl,
8871 				      &req->stat_page_dir);
8872 	}
8873 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8874 		u32 units;
8875 
8876 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8877 		ctx_pg = ctxm->pg_info;
8878 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8879 		units = ctxm->mrav_num_entries_units;
8880 		if (units) {
8881 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8882 			u32 entries;
8883 
8884 			num_mr = ctx_pg->entries - num_ah;
8885 			entries = ((num_mr / units) << 16) | (num_ah / units);
8886 			req->mrav_num_entries = cpu_to_le32(entries);
8887 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8888 		}
8889 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8890 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8891 				      &req->mrav_pg_size_mrav_lvl,
8892 				      &req->mrav_page_dir);
8893 	}
8894 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8895 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8896 		ctx_pg = ctxm->pg_info;
8897 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8898 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8899 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8900 				      &req->tim_pg_size_tim_lvl,
8901 				      &req->tim_page_dir);
8902 	}
8903 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8904 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8905 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8906 	     pg_dir = &req->tqm_sp_page_dir,
8907 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8908 	     ctx_pg = ctxm->pg_info;
8909 	     i < BNXT_MAX_TQM_RINGS;
8910 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8911 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8912 		if (!(enables & ena))
8913 			continue;
8914 
8915 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8916 		*num_entries = cpu_to_le32(ctx_pg->entries);
8917 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8918 	}
8919 	req->flags = cpu_to_le32(flags);
8920 	return hwrm_req_send(bp, req);
8921 }
8922 
8923 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8924 				  struct bnxt_ctx_pg_info *ctx_pg)
8925 {
8926 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8927 
8928 	rmem->page_size = BNXT_PAGE_SIZE;
8929 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8930 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8931 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8932 	if (rmem->depth >= 1)
8933 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8934 	return bnxt_alloc_ring(bp, rmem);
8935 }
8936 
8937 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8938 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8939 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8940 {
8941 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8942 	int rc;
8943 
8944 	if (!mem_size)
8945 		return -EINVAL;
8946 
8947 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8948 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8949 		ctx_pg->nr_pages = 0;
8950 		return -EINVAL;
8951 	}
8952 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8953 		int nr_tbls, i;
8954 
8955 		rmem->depth = 2;
8956 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8957 					     GFP_KERNEL);
8958 		if (!ctx_pg->ctx_pg_tbl)
8959 			return -ENOMEM;
8960 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8961 		rmem->nr_pages = nr_tbls;
8962 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8963 		if (rc)
8964 			return rc;
8965 		for (i = 0; i < nr_tbls; i++) {
8966 			struct bnxt_ctx_pg_info *pg_tbl;
8967 
8968 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8969 			if (!pg_tbl)
8970 				return -ENOMEM;
8971 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8972 			rmem = &pg_tbl->ring_mem;
8973 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8974 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8975 			rmem->depth = 1;
8976 			rmem->nr_pages = MAX_CTX_PAGES;
8977 			rmem->ctx_mem = ctxm;
8978 			if (i == (nr_tbls - 1)) {
8979 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8980 
8981 				if (rem)
8982 					rmem->nr_pages = rem;
8983 			}
8984 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8985 			if (rc)
8986 				break;
8987 		}
8988 	} else {
8989 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8990 		if (rmem->nr_pages > 1 || depth)
8991 			rmem->depth = 1;
8992 		rmem->ctx_mem = ctxm;
8993 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8994 	}
8995 	return rc;
8996 }
8997 
8998 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8999 				    struct bnxt_ctx_pg_info *ctx_pg,
9000 				    void *buf, size_t offset, size_t head,
9001 				    size_t tail)
9002 {
9003 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9004 	size_t nr_pages = ctx_pg->nr_pages;
9005 	int page_size = rmem->page_size;
9006 	size_t len = 0, total_len = 0;
9007 	u16 depth = rmem->depth;
9008 
9009 	tail %= nr_pages * page_size;
9010 	do {
9011 		if (depth > 1) {
9012 			int i = head / (page_size * MAX_CTX_PAGES);
9013 			struct bnxt_ctx_pg_info *pg_tbl;
9014 
9015 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9016 			rmem = &pg_tbl->ring_mem;
9017 		}
9018 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9019 		head += len;
9020 		offset += len;
9021 		total_len += len;
9022 		if (head >= nr_pages * page_size)
9023 			head = 0;
9024 	} while (head != tail);
9025 	return total_len;
9026 }
9027 
9028 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9029 				  struct bnxt_ctx_pg_info *ctx_pg)
9030 {
9031 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9032 
9033 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9034 	    ctx_pg->ctx_pg_tbl) {
9035 		int i, nr_tbls = rmem->nr_pages;
9036 
9037 		for (i = 0; i < nr_tbls; i++) {
9038 			struct bnxt_ctx_pg_info *pg_tbl;
9039 			struct bnxt_ring_mem_info *rmem2;
9040 
9041 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9042 			if (!pg_tbl)
9043 				continue;
9044 			rmem2 = &pg_tbl->ring_mem;
9045 			bnxt_free_ring(bp, rmem2);
9046 			ctx_pg->ctx_pg_arr[i] = NULL;
9047 			kfree(pg_tbl);
9048 			ctx_pg->ctx_pg_tbl[i] = NULL;
9049 		}
9050 		kfree(ctx_pg->ctx_pg_tbl);
9051 		ctx_pg->ctx_pg_tbl = NULL;
9052 	}
9053 	bnxt_free_ring(bp, rmem);
9054 	ctx_pg->nr_pages = 0;
9055 }
9056 
9057 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9058 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9059 				   u8 pg_lvl)
9060 {
9061 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9062 	int i, rc = 0, n = 1;
9063 	u32 mem_size;
9064 
9065 	if (!ctxm->entry_size || !ctx_pg)
9066 		return -EINVAL;
9067 	if (ctxm->instance_bmap)
9068 		n = hweight32(ctxm->instance_bmap);
9069 	if (ctxm->entry_multiple)
9070 		entries = roundup(entries, ctxm->entry_multiple);
9071 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9072 	mem_size = entries * ctxm->entry_size;
9073 	for (i = 0; i < n && !rc; i++) {
9074 		ctx_pg[i].entries = entries;
9075 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9076 					    ctxm->init_value ? ctxm : NULL);
9077 	}
9078 	if (!rc)
9079 		ctxm->mem_valid = 1;
9080 	return rc;
9081 }
9082 
9083 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9084 					       struct bnxt_ctx_mem_type *ctxm,
9085 					       bool last)
9086 {
9087 	struct hwrm_func_backing_store_cfg_v2_input *req;
9088 	u32 instance_bmap = ctxm->instance_bmap;
9089 	int i, j, rc = 0, n = 1;
9090 	__le32 *p;
9091 
9092 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9093 		return 0;
9094 
9095 	if (instance_bmap)
9096 		n = hweight32(ctxm->instance_bmap);
9097 	else
9098 		instance_bmap = 1;
9099 
9100 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9101 	if (rc)
9102 		return rc;
9103 	hwrm_req_hold(bp, req);
9104 	req->type = cpu_to_le16(ctxm->type);
9105 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9106 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9107 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9108 		struct bnxt_bs_trace_info *bs_trace;
9109 		u32 enables;
9110 
9111 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9112 		req->enables = cpu_to_le32(enables);
9113 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9114 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9115 	}
9116 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9117 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9118 		p[i] = cpu_to_le32(ctxm->split[i]);
9119 	for (i = 0, j = 0; j < n && !rc; i++) {
9120 		struct bnxt_ctx_pg_info *ctx_pg;
9121 
9122 		if (!(instance_bmap & (1 << i)))
9123 			continue;
9124 		req->instance = cpu_to_le16(i);
9125 		ctx_pg = &ctxm->pg_info[j++];
9126 		if (!ctx_pg->entries)
9127 			continue;
9128 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9129 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9130 				      &req->page_size_pbl_level,
9131 				      &req->page_dir);
9132 		if (last && j == n)
9133 			req->flags =
9134 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9135 		rc = hwrm_req_send(bp, req);
9136 	}
9137 	hwrm_req_drop(bp, req);
9138 	return rc;
9139 }
9140 
9141 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9142 {
9143 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9144 	struct bnxt_ctx_mem_type *ctxm;
9145 	u16 last_type = BNXT_CTX_INV;
9146 	int rc = 0;
9147 	u16 type;
9148 
9149 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9150 		ctxm = &ctx->ctx_arr[type];
9151 		if (!bnxt_bs_trace_avail(bp, type))
9152 			continue;
9153 		if (!ctxm->mem_valid) {
9154 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9155 						     ctxm->max_entries, 1);
9156 			if (rc) {
9157 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9158 					    type);
9159 				continue;
9160 			}
9161 			bnxt_bs_trace_init(bp, ctxm);
9162 		}
9163 		last_type = type;
9164 	}
9165 
9166 	if (last_type == BNXT_CTX_INV) {
9167 		if (!ena)
9168 			return 0;
9169 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9170 			last_type = BNXT_CTX_MAX - 1;
9171 		else
9172 			last_type = BNXT_CTX_L2_MAX - 1;
9173 	}
9174 	ctx->ctx_arr[last_type].last = 1;
9175 
9176 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9177 		ctxm = &ctx->ctx_arr[type];
9178 
9179 		if (!ctxm->mem_valid)
9180 			continue;
9181 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9182 		if (rc)
9183 			return rc;
9184 	}
9185 	return 0;
9186 }
9187 
9188 /**
9189  * __bnxt_copy_ctx_mem - copy host context memory
9190  * @bp: The driver context
9191  * @ctxm: The pointer to the context memory type
9192  * @buf: The destination buffer or NULL to just obtain the length
9193  * @offset: The buffer offset to copy the data to
9194  * @head: The head offset of context memory to copy from
9195  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9196  *
9197  * This function is called for debugging purposes to dump the host context
9198  * used by the chip.
9199  *
9200  * Return: Length of memory copied
9201  */
9202 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9203 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9204 				  size_t offset, size_t head, size_t tail)
9205 {
9206 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9207 	size_t len = 0, total_len = 0;
9208 	int i, n = 1;
9209 
9210 	if (!ctx_pg)
9211 		return 0;
9212 
9213 	if (ctxm->instance_bmap)
9214 		n = hweight32(ctxm->instance_bmap);
9215 	for (i = 0; i < n; i++) {
9216 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9217 					    tail);
9218 		offset += len;
9219 		total_len += len;
9220 	}
9221 	return total_len;
9222 }
9223 
9224 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9225 			 void *buf, size_t offset)
9226 {
9227 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9228 
9229 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9230 }
9231 
9232 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9233 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9234 {
9235 	struct bnxt_ctx_pg_info *ctx_pg;
9236 	int i, n = 1;
9237 
9238 	ctxm->last = 0;
9239 
9240 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9241 		return;
9242 
9243 	ctx_pg = ctxm->pg_info;
9244 	if (ctx_pg) {
9245 		if (ctxm->instance_bmap)
9246 			n = hweight32(ctxm->instance_bmap);
9247 		for (i = 0; i < n; i++)
9248 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9249 
9250 		kfree(ctx_pg);
9251 		ctxm->pg_info = NULL;
9252 		ctxm->mem_valid = 0;
9253 	}
9254 	memset(ctxm, 0, sizeof(*ctxm));
9255 }
9256 
9257 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9258 {
9259 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9260 	u16 type;
9261 
9262 	if (!ctx)
9263 		return;
9264 
9265 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9266 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9267 
9268 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9269 	if (force) {
9270 		kfree(ctx);
9271 		bp->ctx = NULL;
9272 	}
9273 }
9274 
9275 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9276 {
9277 	struct bnxt_ctx_mem_type *ctxm;
9278 	struct bnxt_ctx_mem_info *ctx;
9279 	u32 l2_qps, qp1_qps, max_qps;
9280 	u32 ena, entries_sp, entries;
9281 	u32 srqs, max_srqs, min;
9282 	u32 num_mr, num_ah;
9283 	u32 extra_srqs = 0;
9284 	u32 extra_qps = 0;
9285 	u32 fast_qpmd_qps;
9286 	u8 pg_lvl = 1;
9287 	int i, rc;
9288 
9289 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9290 	if (rc) {
9291 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9292 			   rc);
9293 		return rc;
9294 	}
9295 	ctx = bp->ctx;
9296 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9297 		return 0;
9298 
9299 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9300 	l2_qps = ctxm->qp_l2_entries;
9301 	qp1_qps = ctxm->qp_qp1_entries;
9302 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9303 	max_qps = ctxm->max_entries;
9304 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9305 	srqs = ctxm->srq_l2_entries;
9306 	max_srqs = ctxm->max_entries;
9307 	ena = 0;
9308 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9309 		pg_lvl = 2;
9310 		if (BNXT_SW_RES_LMT(bp)) {
9311 			extra_qps = max_qps - l2_qps - qp1_qps;
9312 			extra_srqs = max_srqs - srqs;
9313 		} else {
9314 			extra_qps = min_t(u32, 65536,
9315 					  max_qps - l2_qps - qp1_qps);
9316 			/* allocate extra qps if fw supports RoCE fast qp
9317 			 * destroy feature
9318 			 */
9319 			extra_qps += fast_qpmd_qps;
9320 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9321 		}
9322 		if (fast_qpmd_qps)
9323 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9324 	}
9325 
9326 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9327 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9328 				     pg_lvl);
9329 	if (rc)
9330 		return rc;
9331 
9332 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9333 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9334 	if (rc)
9335 		return rc;
9336 
9337 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9338 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9339 				     extra_qps * 2, pg_lvl);
9340 	if (rc)
9341 		return rc;
9342 
9343 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9344 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9345 	if (rc)
9346 		return rc;
9347 
9348 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9349 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9350 	if (rc)
9351 		return rc;
9352 
9353 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9354 		goto skip_rdma;
9355 
9356 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9357 	if (BNXT_SW_RES_LMT(bp) &&
9358 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9359 		num_ah = ctxm->mrav_av_entries;
9360 		num_mr = ctxm->max_entries - num_ah;
9361 	} else {
9362 		/* 128K extra is needed to accommodate static AH context
9363 		 * allocation by f/w.
9364 		 */
9365 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9366 		num_ah = min_t(u32, num_mr, 1024 * 128);
9367 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9368 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9369 			ctxm->mrav_av_entries = num_ah;
9370 	}
9371 
9372 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9373 	if (rc)
9374 		return rc;
9375 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9376 
9377 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9378 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9379 	if (rc)
9380 		return rc;
9381 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9382 
9383 skip_rdma:
9384 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9385 	min = ctxm->min_entries;
9386 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9387 		     2 * (extra_qps + qp1_qps) + min;
9388 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9389 	if (rc)
9390 		return rc;
9391 
9392 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9393 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9394 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9395 	if (rc)
9396 		return rc;
9397 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9398 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9399 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9400 
9401 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9402 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9403 	else
9404 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9405 	if (rc) {
9406 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9407 			   rc);
9408 		return rc;
9409 	}
9410 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9411 	return 0;
9412 }
9413 
9414 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9415 {
9416 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9417 	u16 page_attr;
9418 	int rc;
9419 
9420 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9421 		return 0;
9422 
9423 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9424 	if (rc)
9425 		return rc;
9426 
9427 	if (BNXT_PAGE_SIZE == 0x2000)
9428 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9429 	else if (BNXT_PAGE_SIZE == 0x10000)
9430 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9431 	else
9432 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9433 	req->pg_size_lvl = cpu_to_le16(page_attr |
9434 				       bp->fw_crash_mem->ring_mem.depth);
9435 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9436 	req->size = cpu_to_le32(bp->fw_crash_len);
9437 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9438 	return hwrm_req_send(bp, req);
9439 }
9440 
9441 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9442 {
9443 	if (bp->fw_crash_mem) {
9444 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9445 		kfree(bp->fw_crash_mem);
9446 		bp->fw_crash_mem = NULL;
9447 	}
9448 }
9449 
9450 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9451 {
9452 	u32 mem_size = 0;
9453 	int rc;
9454 
9455 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9456 		return 0;
9457 
9458 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9459 	if (rc)
9460 		return rc;
9461 
9462 	mem_size = round_up(mem_size, 4);
9463 
9464 	/* keep and use the existing pages */
9465 	if (bp->fw_crash_mem &&
9466 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9467 		goto alloc_done;
9468 
9469 	if (bp->fw_crash_mem)
9470 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9471 	else
9472 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9473 					   GFP_KERNEL);
9474 	if (!bp->fw_crash_mem)
9475 		return -ENOMEM;
9476 
9477 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9478 	if (rc) {
9479 		bnxt_free_crash_dump_mem(bp);
9480 		return rc;
9481 	}
9482 
9483 alloc_done:
9484 	bp->fw_crash_len = mem_size;
9485 	return 0;
9486 }
9487 
9488 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9489 {
9490 	struct hwrm_func_resource_qcaps_output *resp;
9491 	struct hwrm_func_resource_qcaps_input *req;
9492 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9493 	int rc;
9494 
9495 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9496 	if (rc)
9497 		return rc;
9498 
9499 	req->fid = cpu_to_le16(0xffff);
9500 	resp = hwrm_req_hold(bp, req);
9501 	rc = hwrm_req_send_silent(bp, req);
9502 	if (rc)
9503 		goto hwrm_func_resc_qcaps_exit;
9504 
9505 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9506 	if (!all)
9507 		goto hwrm_func_resc_qcaps_exit;
9508 
9509 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9510 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9511 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9512 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9513 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9514 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9515 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9516 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9517 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9518 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9519 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9520 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9521 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9522 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9523 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9524 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9525 
9526 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9527 		u16 max_msix = le16_to_cpu(resp->max_msix);
9528 
9529 		hw_resc->max_nqs = max_msix;
9530 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9531 	}
9532 
9533 	if (BNXT_PF(bp)) {
9534 		struct bnxt_pf_info *pf = &bp->pf;
9535 
9536 		pf->vf_resv_strategy =
9537 			le16_to_cpu(resp->vf_reservation_strategy);
9538 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9539 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9540 	}
9541 hwrm_func_resc_qcaps_exit:
9542 	hwrm_req_drop(bp, req);
9543 	return rc;
9544 }
9545 
9546 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9547 {
9548 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9549 	struct hwrm_port_mac_ptp_qcfg_input *req;
9550 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9551 	u8 flags;
9552 	int rc;
9553 
9554 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9555 		rc = -ENODEV;
9556 		goto no_ptp;
9557 	}
9558 
9559 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9560 	if (rc)
9561 		goto no_ptp;
9562 
9563 	req->port_id = cpu_to_le16(bp->pf.port_id);
9564 	resp = hwrm_req_hold(bp, req);
9565 	rc = hwrm_req_send(bp, req);
9566 	if (rc)
9567 		goto exit;
9568 
9569 	flags = resp->flags;
9570 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9571 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9572 		rc = -ENODEV;
9573 		goto exit;
9574 	}
9575 	if (!ptp) {
9576 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9577 		if (!ptp) {
9578 			rc = -ENOMEM;
9579 			goto exit;
9580 		}
9581 		ptp->bp = bp;
9582 		bp->ptp_cfg = ptp;
9583 	}
9584 
9585 	if (flags &
9586 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9587 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9588 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9589 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9590 	} else if (BNXT_CHIP_P5(bp)) {
9591 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9592 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9593 	} else {
9594 		rc = -ENODEV;
9595 		goto exit;
9596 	}
9597 	ptp->rtc_configured =
9598 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9599 	rc = bnxt_ptp_init(bp);
9600 	if (rc)
9601 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9602 exit:
9603 	hwrm_req_drop(bp, req);
9604 	if (!rc)
9605 		return 0;
9606 
9607 no_ptp:
9608 	bnxt_ptp_clear(bp);
9609 	kfree(ptp);
9610 	bp->ptp_cfg = NULL;
9611 	return rc;
9612 }
9613 
9614 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9615 {
9616 	struct hwrm_func_qcaps_output *resp;
9617 	struct hwrm_func_qcaps_input *req;
9618 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9619 	u32 flags, flags_ext, flags_ext2;
9620 	int rc;
9621 
9622 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9623 	if (rc)
9624 		return rc;
9625 
9626 	req->fid = cpu_to_le16(0xffff);
9627 	resp = hwrm_req_hold(bp, req);
9628 	rc = hwrm_req_send(bp, req);
9629 	if (rc)
9630 		goto hwrm_func_qcaps_exit;
9631 
9632 	flags = le32_to_cpu(resp->flags);
9633 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9634 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9635 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9636 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9637 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9638 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9639 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9640 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9641 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9642 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9643 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9644 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9645 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9646 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9647 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9648 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9649 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9650 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9651 
9652 	flags_ext = le32_to_cpu(resp->flags_ext);
9653 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9654 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9655 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9656 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9657 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9658 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9659 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9660 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9661 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9662 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9663 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9664 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9665 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9666 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9667 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9668 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9669 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9670 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9671 
9672 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9673 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9674 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9675 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9676 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9677 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9678 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9679 	if (flags_ext2 &
9680 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9681 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9682 	if (BNXT_PF(bp) &&
9683 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9684 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9685 
9686 	bp->tx_push_thresh = 0;
9687 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9688 	    BNXT_FW_MAJ(bp) > 217)
9689 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9690 
9691 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9692 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9693 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9694 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9695 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9696 	if (!hw_resc->max_hw_ring_grps)
9697 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9698 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9699 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9700 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9701 
9702 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9703 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9704 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9705 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9706 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9707 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9708 
9709 	if (BNXT_PF(bp)) {
9710 		struct bnxt_pf_info *pf = &bp->pf;
9711 
9712 		pf->fw_fid = le16_to_cpu(resp->fid);
9713 		pf->port_id = le16_to_cpu(resp->port_id);
9714 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9715 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9716 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9717 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9718 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9719 			bp->flags |= BNXT_FLAG_WOL_CAP;
9720 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9721 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9722 		} else {
9723 			bnxt_ptp_clear(bp);
9724 			kfree(bp->ptp_cfg);
9725 			bp->ptp_cfg = NULL;
9726 		}
9727 	} else {
9728 #ifdef CONFIG_BNXT_SRIOV
9729 		struct bnxt_vf_info *vf = &bp->vf;
9730 
9731 		vf->fw_fid = le16_to_cpu(resp->fid);
9732 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9733 #endif
9734 	}
9735 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9736 
9737 hwrm_func_qcaps_exit:
9738 	hwrm_req_drop(bp, req);
9739 	return rc;
9740 }
9741 
9742 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9743 {
9744 	struct hwrm_dbg_qcaps_output *resp;
9745 	struct hwrm_dbg_qcaps_input *req;
9746 	int rc;
9747 
9748 	bp->fw_dbg_cap = 0;
9749 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9750 		return;
9751 
9752 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9753 	if (rc)
9754 		return;
9755 
9756 	req->fid = cpu_to_le16(0xffff);
9757 	resp = hwrm_req_hold(bp, req);
9758 	rc = hwrm_req_send(bp, req);
9759 	if (rc)
9760 		goto hwrm_dbg_qcaps_exit;
9761 
9762 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9763 
9764 hwrm_dbg_qcaps_exit:
9765 	hwrm_req_drop(bp, req);
9766 }
9767 
9768 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9769 
9770 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9771 {
9772 	int rc;
9773 
9774 	rc = __bnxt_hwrm_func_qcaps(bp);
9775 	if (rc)
9776 		return rc;
9777 
9778 	bnxt_hwrm_dbg_qcaps(bp);
9779 
9780 	rc = bnxt_hwrm_queue_qportcfg(bp);
9781 	if (rc) {
9782 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9783 		return rc;
9784 	}
9785 	if (bp->hwrm_spec_code >= 0x10803) {
9786 		rc = bnxt_alloc_ctx_mem(bp);
9787 		if (rc)
9788 			return rc;
9789 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9790 		if (!rc)
9791 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9792 	}
9793 	return 0;
9794 }
9795 
9796 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9797 {
9798 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9799 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9800 	u32 flags;
9801 	int rc;
9802 
9803 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9804 		return 0;
9805 
9806 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9807 	if (rc)
9808 		return rc;
9809 
9810 	resp = hwrm_req_hold(bp, req);
9811 	rc = hwrm_req_send(bp, req);
9812 	if (rc)
9813 		goto hwrm_cfa_adv_qcaps_exit;
9814 
9815 	flags = le32_to_cpu(resp->flags);
9816 	if (flags &
9817 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9818 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9819 
9820 	if (flags &
9821 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9822 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9823 
9824 	if (flags &
9825 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9826 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9827 
9828 hwrm_cfa_adv_qcaps_exit:
9829 	hwrm_req_drop(bp, req);
9830 	return rc;
9831 }
9832 
9833 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9834 {
9835 	if (bp->fw_health)
9836 		return 0;
9837 
9838 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9839 	if (!bp->fw_health)
9840 		return -ENOMEM;
9841 
9842 	mutex_init(&bp->fw_health->lock);
9843 	return 0;
9844 }
9845 
9846 static int bnxt_alloc_fw_health(struct bnxt *bp)
9847 {
9848 	int rc;
9849 
9850 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9851 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9852 		return 0;
9853 
9854 	rc = __bnxt_alloc_fw_health(bp);
9855 	if (rc) {
9856 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9857 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9858 		return rc;
9859 	}
9860 
9861 	return 0;
9862 }
9863 
9864 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9865 {
9866 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9867 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9868 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9869 }
9870 
9871 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9872 {
9873 	struct bnxt_fw_health *fw_health = bp->fw_health;
9874 	u32 reg_type;
9875 
9876 	if (!fw_health)
9877 		return;
9878 
9879 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9880 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9881 		fw_health->status_reliable = false;
9882 
9883 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9884 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9885 		fw_health->resets_reliable = false;
9886 }
9887 
9888 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9889 {
9890 	void __iomem *hs;
9891 	u32 status_loc;
9892 	u32 reg_type;
9893 	u32 sig;
9894 
9895 	if (bp->fw_health)
9896 		bp->fw_health->status_reliable = false;
9897 
9898 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9899 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9900 
9901 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9902 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9903 		if (!bp->chip_num) {
9904 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9905 			bp->chip_num = readl(bp->bar0 +
9906 					     BNXT_FW_HEALTH_WIN_BASE +
9907 					     BNXT_GRC_REG_CHIP_NUM);
9908 		}
9909 		if (!BNXT_CHIP_P5_PLUS(bp))
9910 			return;
9911 
9912 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9913 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9914 	} else {
9915 		status_loc = readl(hs + offsetof(struct hcomm_status,
9916 						 fw_status_loc));
9917 	}
9918 
9919 	if (__bnxt_alloc_fw_health(bp)) {
9920 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9921 		return;
9922 	}
9923 
9924 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9925 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9926 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9927 		__bnxt_map_fw_health_reg(bp, status_loc);
9928 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9929 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9930 	}
9931 
9932 	bp->fw_health->status_reliable = true;
9933 }
9934 
9935 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9936 {
9937 	struct bnxt_fw_health *fw_health = bp->fw_health;
9938 	u32 reg_base = 0xffffffff;
9939 	int i;
9940 
9941 	bp->fw_health->status_reliable = false;
9942 	bp->fw_health->resets_reliable = false;
9943 	/* Only pre-map the monitoring GRC registers using window 3 */
9944 	for (i = 0; i < 4; i++) {
9945 		u32 reg = fw_health->regs[i];
9946 
9947 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9948 			continue;
9949 		if (reg_base == 0xffffffff)
9950 			reg_base = reg & BNXT_GRC_BASE_MASK;
9951 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9952 			return -ERANGE;
9953 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9954 	}
9955 	bp->fw_health->status_reliable = true;
9956 	bp->fw_health->resets_reliable = true;
9957 	if (reg_base == 0xffffffff)
9958 		return 0;
9959 
9960 	__bnxt_map_fw_health_reg(bp, reg_base);
9961 	return 0;
9962 }
9963 
9964 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9965 {
9966 	if (!bp->fw_health)
9967 		return;
9968 
9969 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9970 		bp->fw_health->status_reliable = true;
9971 		bp->fw_health->resets_reliable = true;
9972 	} else {
9973 		bnxt_try_map_fw_health_reg(bp);
9974 	}
9975 }
9976 
9977 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9978 {
9979 	struct bnxt_fw_health *fw_health = bp->fw_health;
9980 	struct hwrm_error_recovery_qcfg_output *resp;
9981 	struct hwrm_error_recovery_qcfg_input *req;
9982 	int rc, i;
9983 
9984 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9985 		return 0;
9986 
9987 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9988 	if (rc)
9989 		return rc;
9990 
9991 	resp = hwrm_req_hold(bp, req);
9992 	rc = hwrm_req_send(bp, req);
9993 	if (rc)
9994 		goto err_recovery_out;
9995 	fw_health->flags = le32_to_cpu(resp->flags);
9996 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9997 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9998 		rc = -EINVAL;
9999 		goto err_recovery_out;
10000 	}
10001 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10002 	fw_health->master_func_wait_dsecs =
10003 		le32_to_cpu(resp->master_func_wait_period);
10004 	fw_health->normal_func_wait_dsecs =
10005 		le32_to_cpu(resp->normal_func_wait_period);
10006 	fw_health->post_reset_wait_dsecs =
10007 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10008 	fw_health->post_reset_max_wait_dsecs =
10009 		le32_to_cpu(resp->max_bailout_time_after_reset);
10010 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10011 		le32_to_cpu(resp->fw_health_status_reg);
10012 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10013 		le32_to_cpu(resp->fw_heartbeat_reg);
10014 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10015 		le32_to_cpu(resp->fw_reset_cnt_reg);
10016 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10017 		le32_to_cpu(resp->reset_inprogress_reg);
10018 	fw_health->fw_reset_inprog_reg_mask =
10019 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10020 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10021 	if (fw_health->fw_reset_seq_cnt >= 16) {
10022 		rc = -EINVAL;
10023 		goto err_recovery_out;
10024 	}
10025 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10026 		fw_health->fw_reset_seq_regs[i] =
10027 			le32_to_cpu(resp->reset_reg[i]);
10028 		fw_health->fw_reset_seq_vals[i] =
10029 			le32_to_cpu(resp->reset_reg_val[i]);
10030 		fw_health->fw_reset_seq_delay_msec[i] =
10031 			resp->delay_after_reset[i];
10032 	}
10033 err_recovery_out:
10034 	hwrm_req_drop(bp, req);
10035 	if (!rc)
10036 		rc = bnxt_map_fw_health_regs(bp);
10037 	if (rc)
10038 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10039 	return rc;
10040 }
10041 
10042 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10043 {
10044 	struct hwrm_func_reset_input *req;
10045 	int rc;
10046 
10047 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10048 	if (rc)
10049 		return rc;
10050 
10051 	req->enables = 0;
10052 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10053 	return hwrm_req_send(bp, req);
10054 }
10055 
10056 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10057 {
10058 	struct hwrm_nvm_get_dev_info_output nvm_info;
10059 
10060 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10061 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10062 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10063 			 nvm_info.nvm_cfg_ver_upd);
10064 }
10065 
10066 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10067 {
10068 	struct hwrm_queue_qportcfg_output *resp;
10069 	struct hwrm_queue_qportcfg_input *req;
10070 	u8 i, j, *qptr;
10071 	bool no_rdma;
10072 	int rc = 0;
10073 
10074 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10075 	if (rc)
10076 		return rc;
10077 
10078 	resp = hwrm_req_hold(bp, req);
10079 	rc = hwrm_req_send(bp, req);
10080 	if (rc)
10081 		goto qportcfg_exit;
10082 
10083 	if (!resp->max_configurable_queues) {
10084 		rc = -EINVAL;
10085 		goto qportcfg_exit;
10086 	}
10087 	bp->max_tc = resp->max_configurable_queues;
10088 	bp->max_lltc = resp->max_configurable_lossless_queues;
10089 	if (bp->max_tc > BNXT_MAX_QUEUE)
10090 		bp->max_tc = BNXT_MAX_QUEUE;
10091 
10092 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10093 	qptr = &resp->queue_id0;
10094 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10095 		bp->q_info[j].queue_id = *qptr;
10096 		bp->q_ids[i] = *qptr++;
10097 		bp->q_info[j].queue_profile = *qptr++;
10098 		bp->tc_to_qidx[j] = j;
10099 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10100 		    (no_rdma && BNXT_PF(bp)))
10101 			j++;
10102 	}
10103 	bp->max_q = bp->max_tc;
10104 	bp->max_tc = max_t(u8, j, 1);
10105 
10106 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10107 		bp->max_tc = 1;
10108 
10109 	if (bp->max_lltc > bp->max_tc)
10110 		bp->max_lltc = bp->max_tc;
10111 
10112 qportcfg_exit:
10113 	hwrm_req_drop(bp, req);
10114 	return rc;
10115 }
10116 
10117 static int bnxt_hwrm_poll(struct bnxt *bp)
10118 {
10119 	struct hwrm_ver_get_input *req;
10120 	int rc;
10121 
10122 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10123 	if (rc)
10124 		return rc;
10125 
10126 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10127 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10128 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10129 
10130 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10131 	rc = hwrm_req_send(bp, req);
10132 	return rc;
10133 }
10134 
10135 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10136 {
10137 	struct hwrm_ver_get_output *resp;
10138 	struct hwrm_ver_get_input *req;
10139 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10140 	u32 dev_caps_cfg, hwrm_ver;
10141 	int rc, len, max_tmo_secs;
10142 
10143 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10144 	if (rc)
10145 		return rc;
10146 
10147 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10148 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10149 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10150 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10151 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10152 
10153 	resp = hwrm_req_hold(bp, req);
10154 	rc = hwrm_req_send(bp, req);
10155 	if (rc)
10156 		goto hwrm_ver_get_exit;
10157 
10158 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10159 
10160 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10161 			     resp->hwrm_intf_min_8b << 8 |
10162 			     resp->hwrm_intf_upd_8b;
10163 	if (resp->hwrm_intf_maj_8b < 1) {
10164 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10165 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10166 			    resp->hwrm_intf_upd_8b);
10167 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10168 	}
10169 
10170 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10171 			HWRM_VERSION_UPDATE;
10172 
10173 	if (bp->hwrm_spec_code > hwrm_ver)
10174 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10175 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10176 			 HWRM_VERSION_UPDATE);
10177 	else
10178 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10179 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10180 			 resp->hwrm_intf_upd_8b);
10181 
10182 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10183 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10184 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10185 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10186 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10187 		len = FW_VER_STR_LEN;
10188 	} else {
10189 		fw_maj = resp->hwrm_fw_maj_8b;
10190 		fw_min = resp->hwrm_fw_min_8b;
10191 		fw_bld = resp->hwrm_fw_bld_8b;
10192 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10193 		len = BC_HWRM_STR_LEN;
10194 	}
10195 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10196 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10197 		 fw_rsv);
10198 
10199 	if (strlen(resp->active_pkg_name)) {
10200 		int fw_ver_len = strlen(bp->fw_ver_str);
10201 
10202 		snprintf(bp->fw_ver_str + fw_ver_len,
10203 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10204 			 resp->active_pkg_name);
10205 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10206 	}
10207 
10208 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10209 	if (!bp->hwrm_cmd_timeout)
10210 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10211 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10212 	if (!bp->hwrm_cmd_max_timeout)
10213 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10214 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10215 #ifdef CONFIG_DETECT_HUNG_TASK
10216 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10217 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10218 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10219 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10220 	}
10221 #endif
10222 
10223 	if (resp->hwrm_intf_maj_8b >= 1) {
10224 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10225 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10226 	}
10227 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10228 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10229 
10230 	bp->chip_num = le16_to_cpu(resp->chip_num);
10231 	bp->chip_rev = resp->chip_rev;
10232 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10233 	    !resp->chip_metal)
10234 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10235 
10236 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10237 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10238 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10239 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10240 
10241 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10242 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10243 
10244 	if (dev_caps_cfg &
10245 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10246 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10247 
10248 	if (dev_caps_cfg &
10249 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10250 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10251 
10252 	if (dev_caps_cfg &
10253 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10254 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10255 
10256 hwrm_ver_get_exit:
10257 	hwrm_req_drop(bp, req);
10258 	return rc;
10259 }
10260 
10261 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10262 {
10263 	struct hwrm_fw_set_time_input *req;
10264 	struct tm tm;
10265 	time64_t now = ktime_get_real_seconds();
10266 	int rc;
10267 
10268 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10269 	    bp->hwrm_spec_code < 0x10400)
10270 		return -EOPNOTSUPP;
10271 
10272 	time64_to_tm(now, 0, &tm);
10273 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10274 	if (rc)
10275 		return rc;
10276 
10277 	req->year = cpu_to_le16(1900 + tm.tm_year);
10278 	req->month = 1 + tm.tm_mon;
10279 	req->day = tm.tm_mday;
10280 	req->hour = tm.tm_hour;
10281 	req->minute = tm.tm_min;
10282 	req->second = tm.tm_sec;
10283 	return hwrm_req_send(bp, req);
10284 }
10285 
10286 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10287 {
10288 	u64 sw_tmp;
10289 
10290 	hw &= mask;
10291 	sw_tmp = (*sw & ~mask) | hw;
10292 	if (hw < (*sw & mask))
10293 		sw_tmp += mask + 1;
10294 	WRITE_ONCE(*sw, sw_tmp);
10295 }
10296 
10297 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10298 				    int count, bool ignore_zero)
10299 {
10300 	int i;
10301 
10302 	for (i = 0; i < count; i++) {
10303 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10304 
10305 		if (ignore_zero && !hw)
10306 			continue;
10307 
10308 		if (masks[i] == -1ULL)
10309 			sw_stats[i] = hw;
10310 		else
10311 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10312 	}
10313 }
10314 
10315 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10316 {
10317 	if (!stats->hw_stats)
10318 		return;
10319 
10320 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10321 				stats->hw_masks, stats->len / 8, false);
10322 }
10323 
10324 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10325 {
10326 	struct bnxt_stats_mem *ring0_stats;
10327 	bool ignore_zero = false;
10328 	int i;
10329 
10330 	/* Chip bug.  Counter intermittently becomes 0. */
10331 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10332 		ignore_zero = true;
10333 
10334 	for (i = 0; i < bp->cp_nr_rings; i++) {
10335 		struct bnxt_napi *bnapi = bp->bnapi[i];
10336 		struct bnxt_cp_ring_info *cpr;
10337 		struct bnxt_stats_mem *stats;
10338 
10339 		cpr = &bnapi->cp_ring;
10340 		stats = &cpr->stats;
10341 		if (!i)
10342 			ring0_stats = stats;
10343 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10344 					ring0_stats->hw_masks,
10345 					ring0_stats->len / 8, ignore_zero);
10346 	}
10347 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10348 		struct bnxt_stats_mem *stats = &bp->port_stats;
10349 		__le64 *hw_stats = stats->hw_stats;
10350 		u64 *sw_stats = stats->sw_stats;
10351 		u64 *masks = stats->hw_masks;
10352 		int cnt;
10353 
10354 		cnt = sizeof(struct rx_port_stats) / 8;
10355 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10356 
10357 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10358 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10359 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10360 		cnt = sizeof(struct tx_port_stats) / 8;
10361 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10362 	}
10363 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10364 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10365 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10366 	}
10367 }
10368 
10369 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10370 {
10371 	struct hwrm_port_qstats_input *req;
10372 	struct bnxt_pf_info *pf = &bp->pf;
10373 	int rc;
10374 
10375 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10376 		return 0;
10377 
10378 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10379 		return -EOPNOTSUPP;
10380 
10381 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10382 	if (rc)
10383 		return rc;
10384 
10385 	req->flags = flags;
10386 	req->port_id = cpu_to_le16(pf->port_id);
10387 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10388 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10389 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10390 	return hwrm_req_send(bp, req);
10391 }
10392 
10393 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10394 {
10395 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10396 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10397 	struct hwrm_port_qstats_ext_output *resp_qs;
10398 	struct hwrm_port_qstats_ext_input *req_qs;
10399 	struct bnxt_pf_info *pf = &bp->pf;
10400 	u32 tx_stat_size;
10401 	int rc;
10402 
10403 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10404 		return 0;
10405 
10406 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10407 		return -EOPNOTSUPP;
10408 
10409 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10410 	if (rc)
10411 		return rc;
10412 
10413 	req_qs->flags = flags;
10414 	req_qs->port_id = cpu_to_le16(pf->port_id);
10415 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10416 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10417 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10418 		       sizeof(struct tx_port_stats_ext) : 0;
10419 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10420 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10421 	resp_qs = hwrm_req_hold(bp, req_qs);
10422 	rc = hwrm_req_send(bp, req_qs);
10423 	if (!rc) {
10424 		bp->fw_rx_stats_ext_size =
10425 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10426 		if (BNXT_FW_MAJ(bp) < 220 &&
10427 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10428 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10429 
10430 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10431 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10432 	} else {
10433 		bp->fw_rx_stats_ext_size = 0;
10434 		bp->fw_tx_stats_ext_size = 0;
10435 	}
10436 	hwrm_req_drop(bp, req_qs);
10437 
10438 	if (flags)
10439 		return rc;
10440 
10441 	if (bp->fw_tx_stats_ext_size <=
10442 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10443 		bp->pri2cos_valid = 0;
10444 		return rc;
10445 	}
10446 
10447 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10448 	if (rc)
10449 		return rc;
10450 
10451 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10452 
10453 	resp_qc = hwrm_req_hold(bp, req_qc);
10454 	rc = hwrm_req_send(bp, req_qc);
10455 	if (!rc) {
10456 		u8 *pri2cos;
10457 		int i, j;
10458 
10459 		pri2cos = &resp_qc->pri0_cos_queue_id;
10460 		for (i = 0; i < 8; i++) {
10461 			u8 queue_id = pri2cos[i];
10462 			u8 queue_idx;
10463 
10464 			/* Per port queue IDs start from 0, 10, 20, etc */
10465 			queue_idx = queue_id % 10;
10466 			if (queue_idx > BNXT_MAX_QUEUE) {
10467 				bp->pri2cos_valid = false;
10468 				hwrm_req_drop(bp, req_qc);
10469 				return rc;
10470 			}
10471 			for (j = 0; j < bp->max_q; j++) {
10472 				if (bp->q_ids[j] == queue_id)
10473 					bp->pri2cos_idx[i] = queue_idx;
10474 			}
10475 		}
10476 		bp->pri2cos_valid = true;
10477 	}
10478 	hwrm_req_drop(bp, req_qc);
10479 
10480 	return rc;
10481 }
10482 
10483 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10484 {
10485 	bnxt_hwrm_tunnel_dst_port_free(bp,
10486 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10487 	bnxt_hwrm_tunnel_dst_port_free(bp,
10488 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10489 }
10490 
10491 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10492 {
10493 	int rc, i;
10494 	u32 tpa_flags = 0;
10495 
10496 	if (set_tpa)
10497 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10498 	else if (BNXT_NO_FW_ACCESS(bp))
10499 		return 0;
10500 	for (i = 0; i < bp->nr_vnics; i++) {
10501 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10502 		if (rc) {
10503 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10504 				   i, rc);
10505 			return rc;
10506 		}
10507 	}
10508 	return 0;
10509 }
10510 
10511 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10512 {
10513 	int i;
10514 
10515 	for (i = 0; i < bp->nr_vnics; i++)
10516 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10517 }
10518 
10519 static void bnxt_clear_vnic(struct bnxt *bp)
10520 {
10521 	if (!bp->vnic_info)
10522 		return;
10523 
10524 	bnxt_hwrm_clear_vnic_filter(bp);
10525 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10526 		/* clear all RSS setting before free vnic ctx */
10527 		bnxt_hwrm_clear_vnic_rss(bp);
10528 		bnxt_hwrm_vnic_ctx_free(bp);
10529 	}
10530 	/* before free the vnic, undo the vnic tpa settings */
10531 	if (bp->flags & BNXT_FLAG_TPA)
10532 		bnxt_set_tpa(bp, false);
10533 	bnxt_hwrm_vnic_free(bp);
10534 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10535 		bnxt_hwrm_vnic_ctx_free(bp);
10536 }
10537 
10538 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10539 				    bool irq_re_init)
10540 {
10541 	bnxt_clear_vnic(bp);
10542 	bnxt_hwrm_ring_free(bp, close_path);
10543 	bnxt_hwrm_ring_grp_free(bp);
10544 	if (irq_re_init) {
10545 		bnxt_hwrm_stat_ctx_free(bp);
10546 		bnxt_hwrm_free_tunnel_ports(bp);
10547 	}
10548 }
10549 
10550 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10551 {
10552 	struct hwrm_func_cfg_input *req;
10553 	u8 evb_mode;
10554 	int rc;
10555 
10556 	if (br_mode == BRIDGE_MODE_VEB)
10557 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10558 	else if (br_mode == BRIDGE_MODE_VEPA)
10559 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10560 	else
10561 		return -EINVAL;
10562 
10563 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10564 	if (rc)
10565 		return rc;
10566 
10567 	req->fid = cpu_to_le16(0xffff);
10568 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10569 	req->evb_mode = evb_mode;
10570 	return hwrm_req_send(bp, req);
10571 }
10572 
10573 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10574 {
10575 	struct hwrm_func_cfg_input *req;
10576 	int rc;
10577 
10578 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10579 		return 0;
10580 
10581 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10582 	if (rc)
10583 		return rc;
10584 
10585 	req->fid = cpu_to_le16(0xffff);
10586 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10587 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10588 	if (size == 128)
10589 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10590 
10591 	return hwrm_req_send(bp, req);
10592 }
10593 
10594 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10595 {
10596 	int rc;
10597 
10598 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10599 		goto skip_rss_ctx;
10600 
10601 	/* allocate context for vnic */
10602 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10603 	if (rc) {
10604 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10605 			   vnic->vnic_id, rc);
10606 		goto vnic_setup_err;
10607 	}
10608 	bp->rsscos_nr_ctxs++;
10609 
10610 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10611 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10612 		if (rc) {
10613 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10614 				   vnic->vnic_id, rc);
10615 			goto vnic_setup_err;
10616 		}
10617 		bp->rsscos_nr_ctxs++;
10618 	}
10619 
10620 skip_rss_ctx:
10621 	/* configure default vnic, ring grp */
10622 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10623 	if (rc) {
10624 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10625 			   vnic->vnic_id, rc);
10626 		goto vnic_setup_err;
10627 	}
10628 
10629 	/* Enable RSS hashing on vnic */
10630 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10631 	if (rc) {
10632 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10633 			   vnic->vnic_id, rc);
10634 		goto vnic_setup_err;
10635 	}
10636 
10637 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10638 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10639 		if (rc) {
10640 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10641 				   vnic->vnic_id, rc);
10642 		}
10643 	}
10644 
10645 vnic_setup_err:
10646 	return rc;
10647 }
10648 
10649 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10650 			  u8 valid)
10651 {
10652 	struct hwrm_vnic_update_input *req;
10653 	int rc;
10654 
10655 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10656 	if (rc)
10657 		return rc;
10658 
10659 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10660 
10661 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10662 		req->mru = cpu_to_le16(vnic->mru);
10663 
10664 	req->enables = cpu_to_le32(valid);
10665 
10666 	return hwrm_req_send(bp, req);
10667 }
10668 
10669 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10670 {
10671 	int rc;
10672 
10673 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10674 	if (rc) {
10675 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10676 			   vnic->vnic_id, rc);
10677 		return rc;
10678 	}
10679 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10680 	if (rc)
10681 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10682 			   vnic->vnic_id, rc);
10683 	return rc;
10684 }
10685 
10686 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10687 {
10688 	int rc, i, nr_ctxs;
10689 
10690 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10691 	for (i = 0; i < nr_ctxs; i++) {
10692 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10693 		if (rc) {
10694 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10695 				   vnic->vnic_id, i, rc);
10696 			break;
10697 		}
10698 		bp->rsscos_nr_ctxs++;
10699 	}
10700 	if (i < nr_ctxs)
10701 		return -ENOMEM;
10702 
10703 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10704 	if (rc)
10705 		return rc;
10706 
10707 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10708 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10709 		if (rc) {
10710 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10711 				   vnic->vnic_id, rc);
10712 		}
10713 	}
10714 	return rc;
10715 }
10716 
10717 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10718 {
10719 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10720 		return __bnxt_setup_vnic_p5(bp, vnic);
10721 	else
10722 		return __bnxt_setup_vnic(bp, vnic);
10723 }
10724 
10725 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10726 				     struct bnxt_vnic_info *vnic,
10727 				     u16 start_rx_ring_idx, int rx_rings)
10728 {
10729 	int rc;
10730 
10731 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10732 	if (rc) {
10733 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10734 			   vnic->vnic_id, rc);
10735 		return rc;
10736 	}
10737 	return bnxt_setup_vnic(bp, vnic);
10738 }
10739 
10740 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10741 {
10742 	struct bnxt_vnic_info *vnic;
10743 	int i, rc = 0;
10744 
10745 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10746 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10747 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10748 	}
10749 
10750 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10751 		return 0;
10752 
10753 	for (i = 0; i < bp->rx_nr_rings; i++) {
10754 		u16 vnic_id = i + 1;
10755 		u16 ring_id = i;
10756 
10757 		if (vnic_id >= bp->nr_vnics)
10758 			break;
10759 
10760 		vnic = &bp->vnic_info[vnic_id];
10761 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10762 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10763 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10764 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10765 			break;
10766 	}
10767 	return rc;
10768 }
10769 
10770 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10771 			  bool all)
10772 {
10773 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10774 	struct bnxt_filter_base *usr_fltr, *tmp;
10775 	struct bnxt_ntuple_filter *ntp_fltr;
10776 	int i;
10777 
10778 	if (netif_running(bp->dev)) {
10779 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10780 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10781 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10782 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10783 		}
10784 	}
10785 	if (!all)
10786 		return;
10787 
10788 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10789 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10790 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10791 			ntp_fltr = container_of(usr_fltr,
10792 						struct bnxt_ntuple_filter,
10793 						base);
10794 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10795 			bnxt_del_ntp_filter(bp, ntp_fltr);
10796 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10797 		}
10798 	}
10799 
10800 	if (vnic->rss_table)
10801 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10802 				  vnic->rss_table,
10803 				  vnic->rss_table_dma_addr);
10804 	bp->num_rss_ctx--;
10805 }
10806 
10807 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10808 				  int rxr_id)
10809 {
10810 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10811 	int i, vnic_rx;
10812 
10813 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10814 	 * must be updated because a future filter may use it.
10815 	 */
10816 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10817 		return true;
10818 
10819 	for (i = 0; i < tbl_size; i++) {
10820 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10821 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10822 		else
10823 			vnic_rx = bp->rss_indir_tbl[i];
10824 
10825 		if (rxr_id == vnic_rx)
10826 			return true;
10827 	}
10828 
10829 	return false;
10830 }
10831 
10832 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10833 				u16 mru, int rxr_id)
10834 {
10835 	int rc;
10836 
10837 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10838 		return 0;
10839 
10840 	if (mru) {
10841 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10842 		if (rc) {
10843 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10844 				   vnic->vnic_id, rc);
10845 			return rc;
10846 		}
10847 	}
10848 	vnic->mru = mru;
10849 	bnxt_hwrm_vnic_update(bp, vnic,
10850 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10851 
10852 	return 0;
10853 }
10854 
10855 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10856 {
10857 	struct ethtool_rxfh_context *ctx;
10858 	unsigned long context;
10859 	int rc;
10860 
10861 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10862 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10863 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10864 
10865 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10866 		if (rc)
10867 			return rc;
10868 	}
10869 
10870 	return 0;
10871 }
10872 
10873 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10874 {
10875 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10876 	struct ethtool_rxfh_context *ctx;
10877 	unsigned long context;
10878 
10879 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10880 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10881 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10882 
10883 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10884 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10885 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10886 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10887 				   rss_ctx->index);
10888 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10889 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10890 		}
10891 	}
10892 }
10893 
10894 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10895 {
10896 	struct ethtool_rxfh_context *ctx;
10897 	unsigned long context;
10898 
10899 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10900 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10901 
10902 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10903 	}
10904 }
10905 
10906 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10907 static bool bnxt_promisc_ok(struct bnxt *bp)
10908 {
10909 #ifdef CONFIG_BNXT_SRIOV
10910 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10911 		return false;
10912 #endif
10913 	return true;
10914 }
10915 
10916 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10917 {
10918 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10919 	unsigned int rc = 0;
10920 
10921 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10922 	if (rc) {
10923 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10924 			   rc);
10925 		return rc;
10926 	}
10927 
10928 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10929 	if (rc) {
10930 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10931 			   rc);
10932 		return rc;
10933 	}
10934 	return rc;
10935 }
10936 
10937 static int bnxt_cfg_rx_mode(struct bnxt *);
10938 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10939 
10940 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10941 {
10942 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10943 	int rc = 0;
10944 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10945 
10946 	if (irq_re_init) {
10947 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10948 		if (rc) {
10949 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10950 				   rc);
10951 			goto err_out;
10952 		}
10953 	}
10954 
10955 	rc = bnxt_hwrm_ring_alloc(bp);
10956 	if (rc) {
10957 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10958 		goto err_out;
10959 	}
10960 
10961 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10962 	if (rc) {
10963 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10964 		goto err_out;
10965 	}
10966 
10967 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10968 		rx_nr_rings--;
10969 
10970 	/* default vnic 0 */
10971 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10972 	if (rc) {
10973 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10974 		goto err_out;
10975 	}
10976 
10977 	if (BNXT_VF(bp))
10978 		bnxt_hwrm_func_qcfg(bp);
10979 
10980 	rc = bnxt_setup_vnic(bp, vnic);
10981 	if (rc)
10982 		goto err_out;
10983 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10984 		bnxt_hwrm_update_rss_hash_cfg(bp);
10985 
10986 	if (bp->flags & BNXT_FLAG_RFS) {
10987 		rc = bnxt_alloc_rfs_vnics(bp);
10988 		if (rc)
10989 			goto err_out;
10990 	}
10991 
10992 	if (bp->flags & BNXT_FLAG_TPA) {
10993 		rc = bnxt_set_tpa(bp, true);
10994 		if (rc)
10995 			goto err_out;
10996 	}
10997 
10998 	if (BNXT_VF(bp))
10999 		bnxt_update_vf_mac(bp);
11000 
11001 	/* Filter for default vnic 0 */
11002 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11003 	if (rc) {
11004 		if (BNXT_VF(bp) && rc == -ENODEV)
11005 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11006 		else
11007 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11008 		goto err_out;
11009 	}
11010 	vnic->uc_filter_count = 1;
11011 
11012 	vnic->rx_mask = 0;
11013 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11014 		goto skip_rx_mask;
11015 
11016 	if (bp->dev->flags & IFF_BROADCAST)
11017 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11018 
11019 	if (bp->dev->flags & IFF_PROMISC)
11020 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11021 
11022 	if (bp->dev->flags & IFF_ALLMULTI) {
11023 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11024 		vnic->mc_list_count = 0;
11025 	} else if (bp->dev->flags & IFF_MULTICAST) {
11026 		u32 mask = 0;
11027 
11028 		bnxt_mc_list_updated(bp, &mask);
11029 		vnic->rx_mask |= mask;
11030 	}
11031 
11032 	rc = bnxt_cfg_rx_mode(bp);
11033 	if (rc)
11034 		goto err_out;
11035 
11036 skip_rx_mask:
11037 	rc = bnxt_hwrm_set_coal(bp);
11038 	if (rc)
11039 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11040 				rc);
11041 
11042 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11043 		rc = bnxt_setup_nitroa0_vnic(bp);
11044 		if (rc)
11045 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11046 				   rc);
11047 	}
11048 
11049 	if (BNXT_VF(bp)) {
11050 		bnxt_hwrm_func_qcfg(bp);
11051 		netdev_update_features(bp->dev);
11052 	}
11053 
11054 	return 0;
11055 
11056 err_out:
11057 	bnxt_hwrm_resource_free(bp, 0, true);
11058 
11059 	return rc;
11060 }
11061 
11062 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11063 {
11064 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11065 	return 0;
11066 }
11067 
11068 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11069 {
11070 	bnxt_init_cp_rings(bp);
11071 	bnxt_init_rx_rings(bp);
11072 	bnxt_init_tx_rings(bp);
11073 	bnxt_init_ring_grps(bp, irq_re_init);
11074 	bnxt_init_vnics(bp);
11075 
11076 	return bnxt_init_chip(bp, irq_re_init);
11077 }
11078 
11079 static int bnxt_set_real_num_queues(struct bnxt *bp)
11080 {
11081 	int rc;
11082 	struct net_device *dev = bp->dev;
11083 
11084 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11085 					  bp->tx_nr_rings_xdp);
11086 	if (rc)
11087 		return rc;
11088 
11089 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11090 	if (rc)
11091 		return rc;
11092 
11093 #ifdef CONFIG_RFS_ACCEL
11094 	if (bp->flags & BNXT_FLAG_RFS)
11095 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11096 #endif
11097 
11098 	return rc;
11099 }
11100 
11101 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11102 			     bool shared)
11103 {
11104 	int _rx = *rx, _tx = *tx;
11105 
11106 	if (shared) {
11107 		*rx = min_t(int, _rx, max);
11108 		*tx = min_t(int, _tx, max);
11109 	} else {
11110 		if (max < 2)
11111 			return -ENOMEM;
11112 
11113 		while (_rx + _tx > max) {
11114 			if (_rx > _tx && _rx > 1)
11115 				_rx--;
11116 			else if (_tx > 1)
11117 				_tx--;
11118 		}
11119 		*rx = _rx;
11120 		*tx = _tx;
11121 	}
11122 	return 0;
11123 }
11124 
11125 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11126 {
11127 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11128 }
11129 
11130 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11131 {
11132 	int tcs = bp->num_tc;
11133 
11134 	if (!tcs)
11135 		tcs = 1;
11136 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11137 }
11138 
11139 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11140 {
11141 	int tcs = bp->num_tc;
11142 
11143 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11144 	       bp->tx_nr_rings_xdp;
11145 }
11146 
11147 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11148 			   bool sh)
11149 {
11150 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11151 
11152 	if (tx_cp != *tx) {
11153 		int tx_saved = tx_cp, rc;
11154 
11155 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11156 		if (rc)
11157 			return rc;
11158 		if (tx_cp != tx_saved)
11159 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11160 		return 0;
11161 	}
11162 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11163 }
11164 
11165 static void bnxt_setup_msix(struct bnxt *bp)
11166 {
11167 	const int len = sizeof(bp->irq_tbl[0].name);
11168 	struct net_device *dev = bp->dev;
11169 	int tcs, i;
11170 
11171 	tcs = bp->num_tc;
11172 	if (tcs) {
11173 		int i, off, count;
11174 
11175 		for (i = 0; i < tcs; i++) {
11176 			count = bp->tx_nr_rings_per_tc;
11177 			off = BNXT_TC_TO_RING_BASE(bp, i);
11178 			netdev_set_tc_queue(dev, i, count, off);
11179 		}
11180 	}
11181 
11182 	for (i = 0; i < bp->cp_nr_rings; i++) {
11183 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11184 		char *attr;
11185 
11186 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11187 			attr = "TxRx";
11188 		else if (i < bp->rx_nr_rings)
11189 			attr = "rx";
11190 		else
11191 			attr = "tx";
11192 
11193 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11194 			 attr, i);
11195 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11196 	}
11197 }
11198 
11199 static int bnxt_init_int_mode(struct bnxt *bp);
11200 
11201 static int bnxt_change_msix(struct bnxt *bp, int total)
11202 {
11203 	struct msi_map map;
11204 	int i;
11205 
11206 	/* add MSIX to the end if needed */
11207 	for (i = bp->total_irqs; i < total; i++) {
11208 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11209 		if (map.index < 0)
11210 			return bp->total_irqs;
11211 		bp->irq_tbl[i].vector = map.virq;
11212 		bp->total_irqs++;
11213 	}
11214 
11215 	/* trim MSIX from the end if needed */
11216 	for (i = bp->total_irqs; i > total; i--) {
11217 		map.index = i - 1;
11218 		map.virq = bp->irq_tbl[i - 1].vector;
11219 		pci_msix_free_irq(bp->pdev, map);
11220 		bp->total_irqs--;
11221 	}
11222 	return bp->total_irqs;
11223 }
11224 
11225 static int bnxt_setup_int_mode(struct bnxt *bp)
11226 {
11227 	int rc;
11228 
11229 	if (!bp->irq_tbl) {
11230 		rc = bnxt_init_int_mode(bp);
11231 		if (rc || !bp->irq_tbl)
11232 			return rc ?: -ENODEV;
11233 	}
11234 
11235 	bnxt_setup_msix(bp);
11236 
11237 	rc = bnxt_set_real_num_queues(bp);
11238 	return rc;
11239 }
11240 
11241 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11242 {
11243 	return bp->hw_resc.max_rsscos_ctxs;
11244 }
11245 
11246 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11247 {
11248 	return bp->hw_resc.max_vnics;
11249 }
11250 
11251 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11252 {
11253 	return bp->hw_resc.max_stat_ctxs;
11254 }
11255 
11256 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11257 {
11258 	return bp->hw_resc.max_cp_rings;
11259 }
11260 
11261 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11262 {
11263 	unsigned int cp = bp->hw_resc.max_cp_rings;
11264 
11265 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11266 		cp -= bnxt_get_ulp_msix_num(bp);
11267 
11268 	return cp;
11269 }
11270 
11271 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11272 {
11273 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11274 
11275 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11276 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11277 
11278 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11279 }
11280 
11281 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11282 {
11283 	bp->hw_resc.max_irqs = max_irqs;
11284 }
11285 
11286 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11287 {
11288 	unsigned int cp;
11289 
11290 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11291 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11292 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11293 	else
11294 		return cp - bp->cp_nr_rings;
11295 }
11296 
11297 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11298 {
11299 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11300 }
11301 
11302 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11303 {
11304 	int max_irq = bnxt_get_max_func_irqs(bp);
11305 	int total_req = bp->cp_nr_rings + num;
11306 
11307 	if (max_irq < total_req) {
11308 		num = max_irq - bp->cp_nr_rings;
11309 		if (num <= 0)
11310 			return 0;
11311 	}
11312 	return num;
11313 }
11314 
11315 static int bnxt_get_num_msix(struct bnxt *bp)
11316 {
11317 	if (!BNXT_NEW_RM(bp))
11318 		return bnxt_get_max_func_irqs(bp);
11319 
11320 	return bnxt_nq_rings_in_use(bp);
11321 }
11322 
11323 static int bnxt_init_int_mode(struct bnxt *bp)
11324 {
11325 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11326 
11327 	total_vecs = bnxt_get_num_msix(bp);
11328 	max = bnxt_get_max_func_irqs(bp);
11329 	if (total_vecs > max)
11330 		total_vecs = max;
11331 
11332 	if (!total_vecs)
11333 		return 0;
11334 
11335 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11336 		min = 2;
11337 
11338 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11339 					   PCI_IRQ_MSIX);
11340 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11341 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11342 		rc = -ENODEV;
11343 		goto msix_setup_exit;
11344 	}
11345 
11346 	tbl_size = total_vecs;
11347 	if (pci_msix_can_alloc_dyn(bp->pdev))
11348 		tbl_size = max;
11349 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11350 	if (bp->irq_tbl) {
11351 		for (i = 0; i < total_vecs; i++)
11352 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11353 
11354 		bp->total_irqs = total_vecs;
11355 		/* Trim rings based upon num of vectors allocated */
11356 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11357 				     total_vecs - ulp_msix, min == 1);
11358 		if (rc)
11359 			goto msix_setup_exit;
11360 
11361 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11362 		bp->cp_nr_rings = (min == 1) ?
11363 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11364 				  tx_cp + bp->rx_nr_rings;
11365 
11366 	} else {
11367 		rc = -ENOMEM;
11368 		goto msix_setup_exit;
11369 	}
11370 	return 0;
11371 
11372 msix_setup_exit:
11373 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11374 	kfree(bp->irq_tbl);
11375 	bp->irq_tbl = NULL;
11376 	pci_free_irq_vectors(bp->pdev);
11377 	return rc;
11378 }
11379 
11380 static void bnxt_clear_int_mode(struct bnxt *bp)
11381 {
11382 	pci_free_irq_vectors(bp->pdev);
11383 
11384 	kfree(bp->irq_tbl);
11385 	bp->irq_tbl = NULL;
11386 }
11387 
11388 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11389 {
11390 	bool irq_cleared = false;
11391 	bool irq_change = false;
11392 	int tcs = bp->num_tc;
11393 	int irqs_required;
11394 	int rc;
11395 
11396 	if (!bnxt_need_reserve_rings(bp))
11397 		return 0;
11398 
11399 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11400 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11401 
11402 		if (ulp_msix > bp->ulp_num_msix_want)
11403 			ulp_msix = bp->ulp_num_msix_want;
11404 		irqs_required = ulp_msix + bp->cp_nr_rings;
11405 	} else {
11406 		irqs_required = bnxt_get_num_msix(bp);
11407 	}
11408 
11409 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11410 		irq_change = true;
11411 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11412 			bnxt_ulp_irq_stop(bp);
11413 			bnxt_clear_int_mode(bp);
11414 			irq_cleared = true;
11415 		}
11416 	}
11417 	rc = __bnxt_reserve_rings(bp);
11418 	if (irq_cleared) {
11419 		if (!rc)
11420 			rc = bnxt_init_int_mode(bp);
11421 		bnxt_ulp_irq_restart(bp, rc);
11422 	} else if (irq_change && !rc) {
11423 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11424 			rc = -ENOSPC;
11425 	}
11426 	if (rc) {
11427 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11428 		return rc;
11429 	}
11430 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11431 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11432 		netdev_err(bp->dev, "tx ring reservation failure\n");
11433 		netdev_reset_tc(bp->dev);
11434 		bp->num_tc = 0;
11435 		if (bp->tx_nr_rings_xdp)
11436 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11437 		else
11438 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11439 		return -ENOMEM;
11440 	}
11441 	return 0;
11442 }
11443 
11444 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11445 {
11446 	struct bnxt_tx_ring_info *txr;
11447 	struct netdev_queue *txq;
11448 	struct bnxt_napi *bnapi;
11449 	int i;
11450 
11451 	bnapi = bp->bnapi[idx];
11452 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11453 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11454 		synchronize_net();
11455 
11456 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11457 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11458 			if (txq) {
11459 				__netif_tx_lock_bh(txq);
11460 				netif_tx_stop_queue(txq);
11461 				__netif_tx_unlock_bh(txq);
11462 			}
11463 		}
11464 
11465 		if (!bp->tph_mode)
11466 			continue;
11467 
11468 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11469 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11470 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11471 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11472 	}
11473 }
11474 
11475 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11476 {
11477 	struct bnxt_tx_ring_info *txr;
11478 	struct netdev_queue *txq;
11479 	struct bnxt_napi *bnapi;
11480 	int rc, i;
11481 
11482 	bnapi = bp->bnapi[idx];
11483 	/* All rings have been reserved and previously allocated.
11484 	 * Reallocating with the same parameters should never fail.
11485 	 */
11486 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11487 		if (!bp->tph_mode)
11488 			goto start_tx;
11489 
11490 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11491 		if (rc)
11492 			return rc;
11493 
11494 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11495 		if (rc)
11496 			return rc;
11497 
11498 		txr->tx_prod = 0;
11499 		txr->tx_cons = 0;
11500 		txr->tx_hw_cons = 0;
11501 start_tx:
11502 		WRITE_ONCE(txr->dev_state, 0);
11503 		synchronize_net();
11504 
11505 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11506 			continue;
11507 
11508 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11509 		if (txq)
11510 			netif_tx_start_queue(txq);
11511 	}
11512 
11513 	return 0;
11514 }
11515 
11516 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11517 				     const cpumask_t *mask)
11518 {
11519 	struct bnxt_irq *irq;
11520 	u16 tag;
11521 	int err;
11522 
11523 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11524 
11525 	if (!irq->bp->tph_mode)
11526 		return;
11527 
11528 	cpumask_copy(irq->cpu_mask, mask);
11529 
11530 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11531 		return;
11532 
11533 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11534 				cpumask_first(irq->cpu_mask), &tag))
11535 		return;
11536 
11537 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11538 		return;
11539 
11540 	netdev_lock(irq->bp->dev);
11541 	if (netif_running(irq->bp->dev)) {
11542 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11543 		if (err)
11544 			netdev_err(irq->bp->dev,
11545 				   "RX queue restart failed: err=%d\n", err);
11546 	}
11547 	netdev_unlock(irq->bp->dev);
11548 }
11549 
11550 static void bnxt_irq_affinity_release(struct kref *ref)
11551 {
11552 	struct irq_affinity_notify *notify =
11553 		container_of(ref, struct irq_affinity_notify, kref);
11554 	struct bnxt_irq *irq;
11555 
11556 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11557 
11558 	if (!irq->bp->tph_mode)
11559 		return;
11560 
11561 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11562 		netdev_err(irq->bp->dev,
11563 			   "Setting ST=0 for MSIX entry %d failed\n",
11564 			   irq->msix_nr);
11565 		return;
11566 	}
11567 }
11568 
11569 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11570 {
11571 	irq_set_affinity_notifier(irq->vector, NULL);
11572 }
11573 
11574 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11575 {
11576 	struct irq_affinity_notify *notify;
11577 
11578 	irq->bp = bp;
11579 
11580 	/* Nothing to do if TPH is not enabled */
11581 	if (!bp->tph_mode)
11582 		return;
11583 
11584 	/* Register IRQ affinity notifier */
11585 	notify = &irq->affinity_notify;
11586 	notify->irq = irq->vector;
11587 	notify->notify = bnxt_irq_affinity_notify;
11588 	notify->release = bnxt_irq_affinity_release;
11589 
11590 	irq_set_affinity_notifier(irq->vector, notify);
11591 }
11592 
11593 static void bnxt_free_irq(struct bnxt *bp)
11594 {
11595 	struct bnxt_irq *irq;
11596 	int i;
11597 
11598 #ifdef CONFIG_RFS_ACCEL
11599 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11600 	bp->dev->rx_cpu_rmap = NULL;
11601 #endif
11602 	if (!bp->irq_tbl || !bp->bnapi)
11603 		return;
11604 
11605 	for (i = 0; i < bp->cp_nr_rings; i++) {
11606 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11607 
11608 		irq = &bp->irq_tbl[map_idx];
11609 		if (irq->requested) {
11610 			if (irq->have_cpumask) {
11611 				irq_update_affinity_hint(irq->vector, NULL);
11612 				free_cpumask_var(irq->cpu_mask);
11613 				irq->have_cpumask = 0;
11614 			}
11615 
11616 			bnxt_release_irq_notifier(irq);
11617 
11618 			free_irq(irq->vector, bp->bnapi[i]);
11619 		}
11620 
11621 		irq->requested = 0;
11622 	}
11623 
11624 	/* Disable TPH support */
11625 	pcie_disable_tph(bp->pdev);
11626 	bp->tph_mode = 0;
11627 }
11628 
11629 static int bnxt_request_irq(struct bnxt *bp)
11630 {
11631 	struct cpu_rmap *rmap = NULL;
11632 	int i, j, rc = 0;
11633 	unsigned long flags = 0;
11634 
11635 	rc = bnxt_setup_int_mode(bp);
11636 	if (rc) {
11637 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11638 			   rc);
11639 		return rc;
11640 	}
11641 #ifdef CONFIG_RFS_ACCEL
11642 	rmap = bp->dev->rx_cpu_rmap;
11643 #endif
11644 
11645 	/* Enable TPH support as part of IRQ request */
11646 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11647 	if (!rc)
11648 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11649 
11650 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11651 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11652 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11653 
11654 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11655 		    rmap && bp->bnapi[i]->rx_ring) {
11656 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11657 			if (rc)
11658 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11659 					    j);
11660 			j++;
11661 		}
11662 
11663 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11664 				 bp->bnapi[i]);
11665 		if (rc)
11666 			break;
11667 
11668 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11669 		irq->requested = 1;
11670 
11671 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11672 			int numa_node = dev_to_node(&bp->pdev->dev);
11673 			u16 tag;
11674 
11675 			irq->have_cpumask = 1;
11676 			irq->msix_nr = map_idx;
11677 			irq->ring_nr = i;
11678 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11679 					irq->cpu_mask);
11680 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11681 			if (rc) {
11682 				netdev_warn(bp->dev,
11683 					    "Update affinity hint failed, IRQ = %d\n",
11684 					    irq->vector);
11685 				break;
11686 			}
11687 
11688 			bnxt_register_irq_notifier(bp, irq);
11689 
11690 			/* Init ST table entry */
11691 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11692 						cpumask_first(irq->cpu_mask),
11693 						&tag))
11694 				continue;
11695 
11696 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11697 		}
11698 	}
11699 	return rc;
11700 }
11701 
11702 static void bnxt_del_napi(struct bnxt *bp)
11703 {
11704 	int i;
11705 
11706 	if (!bp->bnapi)
11707 		return;
11708 
11709 	for (i = 0; i < bp->rx_nr_rings; i++)
11710 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11711 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11712 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11713 
11714 	for (i = 0; i < bp->cp_nr_rings; i++) {
11715 		struct bnxt_napi *bnapi = bp->bnapi[i];
11716 
11717 		__netif_napi_del_locked(&bnapi->napi);
11718 	}
11719 	/* We called __netif_napi_del_locked(), we need
11720 	 * to respect an RCU grace period before freeing napi structures.
11721 	 */
11722 	synchronize_net();
11723 }
11724 
11725 static void bnxt_init_napi(struct bnxt *bp)
11726 {
11727 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11728 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11729 	struct bnxt_napi *bnapi;
11730 	int i;
11731 
11732 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11733 		poll_fn = bnxt_poll_p5;
11734 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11735 		cp_nr_rings--;
11736 
11737 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11738 
11739 	for (i = 0; i < cp_nr_rings; i++) {
11740 		bnapi = bp->bnapi[i];
11741 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11742 					     bnapi->index);
11743 	}
11744 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11745 		bnapi = bp->bnapi[cp_nr_rings];
11746 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11747 	}
11748 }
11749 
11750 static void bnxt_disable_napi(struct bnxt *bp)
11751 {
11752 	int i;
11753 
11754 	if (!bp->bnapi ||
11755 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11756 		return;
11757 
11758 	for (i = 0; i < bp->cp_nr_rings; i++) {
11759 		struct bnxt_napi *bnapi = bp->bnapi[i];
11760 		struct bnxt_cp_ring_info *cpr;
11761 
11762 		cpr = &bnapi->cp_ring;
11763 		if (bnapi->tx_fault)
11764 			cpr->sw_stats->tx.tx_resets++;
11765 		if (bnapi->in_reset)
11766 			cpr->sw_stats->rx.rx_resets++;
11767 		napi_disable_locked(&bnapi->napi);
11768 	}
11769 }
11770 
11771 static void bnxt_enable_napi(struct bnxt *bp)
11772 {
11773 	int i;
11774 
11775 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11776 	for (i = 0; i < bp->cp_nr_rings; i++) {
11777 		struct bnxt_napi *bnapi = bp->bnapi[i];
11778 		struct bnxt_cp_ring_info *cpr;
11779 
11780 		bnapi->tx_fault = 0;
11781 
11782 		cpr = &bnapi->cp_ring;
11783 		bnapi->in_reset = false;
11784 
11785 		if (bnapi->rx_ring) {
11786 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11787 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11788 		}
11789 		napi_enable_locked(&bnapi->napi);
11790 	}
11791 }
11792 
11793 void bnxt_tx_disable(struct bnxt *bp)
11794 {
11795 	int i;
11796 	struct bnxt_tx_ring_info *txr;
11797 
11798 	if (bp->tx_ring) {
11799 		for (i = 0; i < bp->tx_nr_rings; i++) {
11800 			txr = &bp->tx_ring[i];
11801 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11802 		}
11803 	}
11804 	/* Make sure napi polls see @dev_state change */
11805 	synchronize_net();
11806 	/* Drop carrier first to prevent TX timeout */
11807 	netif_carrier_off(bp->dev);
11808 	/* Stop all TX queues */
11809 	netif_tx_disable(bp->dev);
11810 }
11811 
11812 void bnxt_tx_enable(struct bnxt *bp)
11813 {
11814 	int i;
11815 	struct bnxt_tx_ring_info *txr;
11816 
11817 	for (i = 0; i < bp->tx_nr_rings; i++) {
11818 		txr = &bp->tx_ring[i];
11819 		WRITE_ONCE(txr->dev_state, 0);
11820 	}
11821 	/* Make sure napi polls see @dev_state change */
11822 	synchronize_net();
11823 	netif_tx_wake_all_queues(bp->dev);
11824 	if (BNXT_LINK_IS_UP(bp))
11825 		netif_carrier_on(bp->dev);
11826 }
11827 
11828 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11829 {
11830 	u8 active_fec = link_info->active_fec_sig_mode &
11831 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11832 
11833 	switch (active_fec) {
11834 	default:
11835 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11836 		return "None";
11837 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11838 		return "Clause 74 BaseR";
11839 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11840 		return "Clause 91 RS(528,514)";
11841 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11842 		return "Clause 91 RS544_1XN";
11843 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11844 		return "Clause 91 RS(544,514)";
11845 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11846 		return "Clause 91 RS272_1XN";
11847 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11848 		return "Clause 91 RS(272,257)";
11849 	}
11850 }
11851 
11852 void bnxt_report_link(struct bnxt *bp)
11853 {
11854 	if (BNXT_LINK_IS_UP(bp)) {
11855 		const char *signal = "";
11856 		const char *flow_ctrl;
11857 		const char *duplex;
11858 		u32 speed;
11859 		u16 fec;
11860 
11861 		netif_carrier_on(bp->dev);
11862 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11863 		if (speed == SPEED_UNKNOWN) {
11864 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11865 			return;
11866 		}
11867 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11868 			duplex = "full";
11869 		else
11870 			duplex = "half";
11871 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11872 			flow_ctrl = "ON - receive & transmit";
11873 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11874 			flow_ctrl = "ON - transmit";
11875 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11876 			flow_ctrl = "ON - receive";
11877 		else
11878 			flow_ctrl = "none";
11879 		if (bp->link_info.phy_qcfg_resp.option_flags &
11880 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11881 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11882 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11883 			switch (sig_mode) {
11884 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11885 				signal = "(NRZ) ";
11886 				break;
11887 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11888 				signal = "(PAM4 56Gbps) ";
11889 				break;
11890 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11891 				signal = "(PAM4 112Gbps) ";
11892 				break;
11893 			default:
11894 				break;
11895 			}
11896 		}
11897 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11898 			    speed, signal, duplex, flow_ctrl);
11899 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11900 			netdev_info(bp->dev, "EEE is %s\n",
11901 				    bp->eee.eee_active ? "active" :
11902 							 "not active");
11903 		fec = bp->link_info.fec_cfg;
11904 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11905 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11906 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11907 				    bnxt_report_fec(&bp->link_info));
11908 	} else {
11909 		netif_carrier_off(bp->dev);
11910 		netdev_err(bp->dev, "NIC Link is Down\n");
11911 	}
11912 }
11913 
11914 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11915 {
11916 	if (!resp->supported_speeds_auto_mode &&
11917 	    !resp->supported_speeds_force_mode &&
11918 	    !resp->supported_pam4_speeds_auto_mode &&
11919 	    !resp->supported_pam4_speeds_force_mode &&
11920 	    !resp->supported_speeds2_auto_mode &&
11921 	    !resp->supported_speeds2_force_mode)
11922 		return true;
11923 	return false;
11924 }
11925 
11926 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11927 {
11928 	struct bnxt_link_info *link_info = &bp->link_info;
11929 	struct hwrm_port_phy_qcaps_output *resp;
11930 	struct hwrm_port_phy_qcaps_input *req;
11931 	int rc = 0;
11932 
11933 	if (bp->hwrm_spec_code < 0x10201)
11934 		return 0;
11935 
11936 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11937 	if (rc)
11938 		return rc;
11939 
11940 	resp = hwrm_req_hold(bp, req);
11941 	rc = hwrm_req_send(bp, req);
11942 	if (rc)
11943 		goto hwrm_phy_qcaps_exit;
11944 
11945 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11946 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11947 		struct ethtool_keee *eee = &bp->eee;
11948 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11949 
11950 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11951 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11952 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11953 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11954 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11955 	}
11956 
11957 	if (bp->hwrm_spec_code >= 0x10a01) {
11958 		if (bnxt_phy_qcaps_no_speed(resp)) {
11959 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11960 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11961 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11962 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11963 			netdev_info(bp->dev, "Ethernet link enabled\n");
11964 			/* Phy re-enabled, reprobe the speeds */
11965 			link_info->support_auto_speeds = 0;
11966 			link_info->support_pam4_auto_speeds = 0;
11967 			link_info->support_auto_speeds2 = 0;
11968 		}
11969 	}
11970 	if (resp->supported_speeds_auto_mode)
11971 		link_info->support_auto_speeds =
11972 			le16_to_cpu(resp->supported_speeds_auto_mode);
11973 	if (resp->supported_pam4_speeds_auto_mode)
11974 		link_info->support_pam4_auto_speeds =
11975 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11976 	if (resp->supported_speeds2_auto_mode)
11977 		link_info->support_auto_speeds2 =
11978 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11979 
11980 	bp->port_count = resp->port_cnt;
11981 
11982 hwrm_phy_qcaps_exit:
11983 	hwrm_req_drop(bp, req);
11984 	return rc;
11985 }
11986 
11987 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11988 {
11989 	struct hwrm_port_mac_qcaps_output *resp;
11990 	struct hwrm_port_mac_qcaps_input *req;
11991 	int rc;
11992 
11993 	if (bp->hwrm_spec_code < 0x10a03)
11994 		return;
11995 
11996 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11997 	if (rc)
11998 		return;
11999 
12000 	resp = hwrm_req_hold(bp, req);
12001 	rc = hwrm_req_send_silent(bp, req);
12002 	if (!rc)
12003 		bp->mac_flags = resp->flags;
12004 	hwrm_req_drop(bp, req);
12005 }
12006 
12007 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12008 {
12009 	u16 diff = advertising ^ supported;
12010 
12011 	return ((supported | diff) != supported);
12012 }
12013 
12014 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12015 {
12016 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12017 
12018 	/* Check if any advertised speeds are no longer supported. The caller
12019 	 * holds the link_lock mutex, so we can modify link_info settings.
12020 	 */
12021 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12022 		if (bnxt_support_dropped(link_info->advertising,
12023 					 link_info->support_auto_speeds2)) {
12024 			link_info->advertising = link_info->support_auto_speeds2;
12025 			return true;
12026 		}
12027 		return false;
12028 	}
12029 	if (bnxt_support_dropped(link_info->advertising,
12030 				 link_info->support_auto_speeds)) {
12031 		link_info->advertising = link_info->support_auto_speeds;
12032 		return true;
12033 	}
12034 	if (bnxt_support_dropped(link_info->advertising_pam4,
12035 				 link_info->support_pam4_auto_speeds)) {
12036 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12037 		return true;
12038 	}
12039 	return false;
12040 }
12041 
12042 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12043 {
12044 	struct bnxt_link_info *link_info = &bp->link_info;
12045 	struct hwrm_port_phy_qcfg_output *resp;
12046 	struct hwrm_port_phy_qcfg_input *req;
12047 	u8 link_state = link_info->link_state;
12048 	bool support_changed;
12049 	int rc;
12050 
12051 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12052 	if (rc)
12053 		return rc;
12054 
12055 	resp = hwrm_req_hold(bp, req);
12056 	rc = hwrm_req_send(bp, req);
12057 	if (rc) {
12058 		hwrm_req_drop(bp, req);
12059 		if (BNXT_VF(bp) && rc == -ENODEV) {
12060 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12061 			rc = 0;
12062 		}
12063 		return rc;
12064 	}
12065 
12066 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12067 	link_info->phy_link_status = resp->link;
12068 	link_info->duplex = resp->duplex_cfg;
12069 	if (bp->hwrm_spec_code >= 0x10800)
12070 		link_info->duplex = resp->duplex_state;
12071 	link_info->pause = resp->pause;
12072 	link_info->auto_mode = resp->auto_mode;
12073 	link_info->auto_pause_setting = resp->auto_pause;
12074 	link_info->lp_pause = resp->link_partner_adv_pause;
12075 	link_info->force_pause_setting = resp->force_pause;
12076 	link_info->duplex_setting = resp->duplex_cfg;
12077 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12078 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12079 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12080 			link_info->active_lanes = resp->active_lanes;
12081 	} else {
12082 		link_info->link_speed = 0;
12083 		link_info->active_lanes = 0;
12084 	}
12085 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12086 	link_info->force_pam4_link_speed =
12087 		le16_to_cpu(resp->force_pam4_link_speed);
12088 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12089 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12090 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12091 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12092 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12093 	link_info->auto_pam4_link_speeds =
12094 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12095 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12096 	link_info->lp_auto_link_speeds =
12097 		le16_to_cpu(resp->link_partner_adv_speeds);
12098 	link_info->lp_auto_pam4_link_speeds =
12099 		resp->link_partner_pam4_adv_speeds;
12100 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12101 	link_info->phy_ver[0] = resp->phy_maj;
12102 	link_info->phy_ver[1] = resp->phy_min;
12103 	link_info->phy_ver[2] = resp->phy_bld;
12104 	link_info->media_type = resp->media_type;
12105 	link_info->phy_type = resp->phy_type;
12106 	link_info->transceiver = resp->xcvr_pkg_type;
12107 	link_info->phy_addr = resp->eee_config_phy_addr &
12108 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12109 	link_info->module_status = resp->module_status;
12110 
12111 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12112 		struct ethtool_keee *eee = &bp->eee;
12113 		u16 fw_speeds;
12114 
12115 		eee->eee_active = 0;
12116 		if (resp->eee_config_phy_addr &
12117 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12118 			eee->eee_active = 1;
12119 			fw_speeds = le16_to_cpu(
12120 				resp->link_partner_adv_eee_link_speed_mask);
12121 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12122 		}
12123 
12124 		/* Pull initial EEE config */
12125 		if (!chng_link_state) {
12126 			if (resp->eee_config_phy_addr &
12127 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12128 				eee->eee_enabled = 1;
12129 
12130 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12131 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12132 
12133 			if (resp->eee_config_phy_addr &
12134 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12135 				__le32 tmr;
12136 
12137 				eee->tx_lpi_enabled = 1;
12138 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12139 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12140 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12141 			}
12142 		}
12143 	}
12144 
12145 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12146 	if (bp->hwrm_spec_code >= 0x10504) {
12147 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12148 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12149 	}
12150 	/* TODO: need to add more logic to report VF link */
12151 	if (chng_link_state) {
12152 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12153 			link_info->link_state = BNXT_LINK_STATE_UP;
12154 		else
12155 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12156 		if (link_state != link_info->link_state)
12157 			bnxt_report_link(bp);
12158 	} else {
12159 		/* always link down if not require to update link state */
12160 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12161 	}
12162 	hwrm_req_drop(bp, req);
12163 
12164 	if (!BNXT_PHY_CFG_ABLE(bp))
12165 		return 0;
12166 
12167 	support_changed = bnxt_support_speed_dropped(link_info);
12168 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12169 		bnxt_hwrm_set_link_setting(bp, true, false);
12170 	return 0;
12171 }
12172 
12173 static void bnxt_get_port_module_status(struct bnxt *bp)
12174 {
12175 	struct bnxt_link_info *link_info = &bp->link_info;
12176 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12177 	u8 module_status;
12178 
12179 	if (bnxt_update_link(bp, true))
12180 		return;
12181 
12182 	module_status = link_info->module_status;
12183 	switch (module_status) {
12184 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12185 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12186 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12187 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12188 			    bp->pf.port_id);
12189 		if (bp->hwrm_spec_code >= 0x10201) {
12190 			netdev_warn(bp->dev, "Module part number %s\n",
12191 				    resp->phy_vendor_partnumber);
12192 		}
12193 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12194 			netdev_warn(bp->dev, "TX is disabled\n");
12195 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12196 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12197 	}
12198 }
12199 
12200 static void
12201 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12202 {
12203 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12204 		if (bp->hwrm_spec_code >= 0x10201)
12205 			req->auto_pause =
12206 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12207 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12208 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12209 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12210 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12211 		req->enables |=
12212 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12213 	} else {
12214 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12215 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12216 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12217 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12218 		req->enables |=
12219 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12220 		if (bp->hwrm_spec_code >= 0x10201) {
12221 			req->auto_pause = req->force_pause;
12222 			req->enables |= cpu_to_le32(
12223 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12224 		}
12225 	}
12226 }
12227 
12228 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12229 {
12230 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12231 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12232 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12233 			req->enables |=
12234 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12235 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12236 		} else if (bp->link_info.advertising) {
12237 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12238 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12239 		}
12240 		if (bp->link_info.advertising_pam4) {
12241 			req->enables |=
12242 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12243 			req->auto_link_pam4_speed_mask =
12244 				cpu_to_le16(bp->link_info.advertising_pam4);
12245 		}
12246 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12247 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12248 	} else {
12249 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12250 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12251 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12252 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12253 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12254 				   (u32)bp->link_info.req_link_speed);
12255 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12256 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12257 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12258 		} else {
12259 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12260 		}
12261 	}
12262 
12263 	/* tell chimp that the setting takes effect immediately */
12264 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12265 }
12266 
12267 int bnxt_hwrm_set_pause(struct bnxt *bp)
12268 {
12269 	struct hwrm_port_phy_cfg_input *req;
12270 	int rc;
12271 
12272 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12273 	if (rc)
12274 		return rc;
12275 
12276 	bnxt_hwrm_set_pause_common(bp, req);
12277 
12278 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12279 	    bp->link_info.force_link_chng)
12280 		bnxt_hwrm_set_link_common(bp, req);
12281 
12282 	rc = hwrm_req_send(bp, req);
12283 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12284 		/* since changing of pause setting doesn't trigger any link
12285 		 * change event, the driver needs to update the current pause
12286 		 * result upon successfully return of the phy_cfg command
12287 		 */
12288 		bp->link_info.pause =
12289 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12290 		bp->link_info.auto_pause_setting = 0;
12291 		if (!bp->link_info.force_link_chng)
12292 			bnxt_report_link(bp);
12293 	}
12294 	bp->link_info.force_link_chng = false;
12295 	return rc;
12296 }
12297 
12298 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12299 			      struct hwrm_port_phy_cfg_input *req)
12300 {
12301 	struct ethtool_keee *eee = &bp->eee;
12302 
12303 	if (eee->eee_enabled) {
12304 		u16 eee_speeds;
12305 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12306 
12307 		if (eee->tx_lpi_enabled)
12308 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12309 		else
12310 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12311 
12312 		req->flags |= cpu_to_le32(flags);
12313 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12314 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12315 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12316 	} else {
12317 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12318 	}
12319 }
12320 
12321 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12322 {
12323 	struct hwrm_port_phy_cfg_input *req;
12324 	int rc;
12325 
12326 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12327 	if (rc)
12328 		return rc;
12329 
12330 	if (set_pause)
12331 		bnxt_hwrm_set_pause_common(bp, req);
12332 
12333 	bnxt_hwrm_set_link_common(bp, req);
12334 
12335 	if (set_eee)
12336 		bnxt_hwrm_set_eee(bp, req);
12337 	return hwrm_req_send(bp, req);
12338 }
12339 
12340 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12341 {
12342 	struct hwrm_port_phy_cfg_input *req;
12343 	int rc;
12344 
12345 	if (!BNXT_SINGLE_PF(bp))
12346 		return 0;
12347 
12348 	if (pci_num_vf(bp->pdev) &&
12349 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12350 		return 0;
12351 
12352 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12353 	if (rc)
12354 		return rc;
12355 
12356 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12357 	rc = hwrm_req_send(bp, req);
12358 	if (!rc) {
12359 		mutex_lock(&bp->link_lock);
12360 		/* Device is not obliged link down in certain scenarios, even
12361 		 * when forced. Setting the state unknown is consistent with
12362 		 * driver startup and will force link state to be reported
12363 		 * during subsequent open based on PORT_PHY_QCFG.
12364 		 */
12365 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12366 		mutex_unlock(&bp->link_lock);
12367 	}
12368 	return rc;
12369 }
12370 
12371 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12372 {
12373 #ifdef CONFIG_TEE_BNXT_FW
12374 	int rc = tee_bnxt_fw_load();
12375 
12376 	if (rc)
12377 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12378 
12379 	return rc;
12380 #else
12381 	netdev_err(bp->dev, "OP-TEE not supported\n");
12382 	return -ENODEV;
12383 #endif
12384 }
12385 
12386 static int bnxt_try_recover_fw(struct bnxt *bp)
12387 {
12388 	if (bp->fw_health && bp->fw_health->status_reliable) {
12389 		int retry = 0, rc;
12390 		u32 sts;
12391 
12392 		do {
12393 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12394 			rc = bnxt_hwrm_poll(bp);
12395 			if (!BNXT_FW_IS_BOOTING(sts) &&
12396 			    !BNXT_FW_IS_RECOVERING(sts))
12397 				break;
12398 			retry++;
12399 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12400 
12401 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12402 			netdev_err(bp->dev,
12403 				   "Firmware not responding, status: 0x%x\n",
12404 				   sts);
12405 			rc = -ENODEV;
12406 		}
12407 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12408 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12409 			return bnxt_fw_reset_via_optee(bp);
12410 		}
12411 		return rc;
12412 	}
12413 
12414 	return -ENODEV;
12415 }
12416 
12417 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12418 {
12419 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12420 
12421 	if (!BNXT_NEW_RM(bp))
12422 		return; /* no resource reservations required */
12423 
12424 	hw_resc->resv_cp_rings = 0;
12425 	hw_resc->resv_stat_ctxs = 0;
12426 	hw_resc->resv_irqs = 0;
12427 	hw_resc->resv_tx_rings = 0;
12428 	hw_resc->resv_rx_rings = 0;
12429 	hw_resc->resv_hw_ring_grps = 0;
12430 	hw_resc->resv_vnics = 0;
12431 	hw_resc->resv_rsscos_ctxs = 0;
12432 	if (!fw_reset) {
12433 		bp->tx_nr_rings = 0;
12434 		bp->rx_nr_rings = 0;
12435 	}
12436 }
12437 
12438 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12439 {
12440 	int rc;
12441 
12442 	if (!BNXT_NEW_RM(bp))
12443 		return 0; /* no resource reservations required */
12444 
12445 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12446 	if (rc)
12447 		netdev_err(bp->dev, "resc_qcaps failed\n");
12448 
12449 	bnxt_clear_reservations(bp, fw_reset);
12450 
12451 	return rc;
12452 }
12453 
12454 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12455 {
12456 	struct hwrm_func_drv_if_change_output *resp;
12457 	struct hwrm_func_drv_if_change_input *req;
12458 	bool resc_reinit = false;
12459 	bool caps_change = false;
12460 	int rc, retry = 0;
12461 	bool fw_reset;
12462 	u32 flags = 0;
12463 
12464 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12465 	bp->fw_reset_state = 0;
12466 
12467 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12468 		return 0;
12469 
12470 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12471 	if (rc)
12472 		return rc;
12473 
12474 	if (up)
12475 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12476 	resp = hwrm_req_hold(bp, req);
12477 
12478 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12479 	while (retry < BNXT_FW_IF_RETRY) {
12480 		rc = hwrm_req_send(bp, req);
12481 		if (rc != -EAGAIN)
12482 			break;
12483 
12484 		msleep(50);
12485 		retry++;
12486 	}
12487 
12488 	if (rc == -EAGAIN) {
12489 		hwrm_req_drop(bp, req);
12490 		return rc;
12491 	} else if (!rc) {
12492 		flags = le32_to_cpu(resp->flags);
12493 	} else if (up) {
12494 		rc = bnxt_try_recover_fw(bp);
12495 		fw_reset = true;
12496 	}
12497 	hwrm_req_drop(bp, req);
12498 	if (rc)
12499 		return rc;
12500 
12501 	if (!up) {
12502 		bnxt_inv_fw_health_reg(bp);
12503 		return 0;
12504 	}
12505 
12506 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12507 		resc_reinit = true;
12508 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12509 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12510 		fw_reset = true;
12511 	else
12512 		bnxt_remap_fw_health_regs(bp);
12513 
12514 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12515 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12516 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12517 		return -ENODEV;
12518 	}
12519 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12520 		caps_change = true;
12521 
12522 	if (resc_reinit || fw_reset || caps_change) {
12523 		if (fw_reset || caps_change) {
12524 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12525 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12526 				bnxt_ulp_irq_stop(bp);
12527 			bnxt_free_ctx_mem(bp, false);
12528 			bnxt_dcb_free(bp);
12529 			rc = bnxt_fw_init_one(bp);
12530 			if (rc) {
12531 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12532 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12533 				return rc;
12534 			}
12535 			/* IRQ will be initialized later in bnxt_request_irq()*/
12536 			bnxt_clear_int_mode(bp);
12537 		}
12538 		rc = bnxt_cancel_reservations(bp, fw_reset);
12539 	}
12540 	return rc;
12541 }
12542 
12543 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12544 {
12545 	struct hwrm_port_led_qcaps_output *resp;
12546 	struct hwrm_port_led_qcaps_input *req;
12547 	struct bnxt_pf_info *pf = &bp->pf;
12548 	int rc;
12549 
12550 	bp->num_leds = 0;
12551 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12552 		return 0;
12553 
12554 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12555 	if (rc)
12556 		return rc;
12557 
12558 	req->port_id = cpu_to_le16(pf->port_id);
12559 	resp = hwrm_req_hold(bp, req);
12560 	rc = hwrm_req_send(bp, req);
12561 	if (rc) {
12562 		hwrm_req_drop(bp, req);
12563 		return rc;
12564 	}
12565 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12566 		int i;
12567 
12568 		bp->num_leds = resp->num_leds;
12569 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12570 						 bp->num_leds);
12571 		for (i = 0; i < bp->num_leds; i++) {
12572 			struct bnxt_led_info *led = &bp->leds[i];
12573 			__le16 caps = led->led_state_caps;
12574 
12575 			if (!led->led_group_id ||
12576 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12577 				bp->num_leds = 0;
12578 				break;
12579 			}
12580 		}
12581 	}
12582 	hwrm_req_drop(bp, req);
12583 	return 0;
12584 }
12585 
12586 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12587 {
12588 	struct hwrm_wol_filter_alloc_output *resp;
12589 	struct hwrm_wol_filter_alloc_input *req;
12590 	int rc;
12591 
12592 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12593 	if (rc)
12594 		return rc;
12595 
12596 	req->port_id = cpu_to_le16(bp->pf.port_id);
12597 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12598 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12599 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12600 
12601 	resp = hwrm_req_hold(bp, req);
12602 	rc = hwrm_req_send(bp, req);
12603 	if (!rc)
12604 		bp->wol_filter_id = resp->wol_filter_id;
12605 	hwrm_req_drop(bp, req);
12606 	return rc;
12607 }
12608 
12609 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12610 {
12611 	struct hwrm_wol_filter_free_input *req;
12612 	int rc;
12613 
12614 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12615 	if (rc)
12616 		return rc;
12617 
12618 	req->port_id = cpu_to_le16(bp->pf.port_id);
12619 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12620 	req->wol_filter_id = bp->wol_filter_id;
12621 
12622 	return hwrm_req_send(bp, req);
12623 }
12624 
12625 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12626 {
12627 	struct hwrm_wol_filter_qcfg_output *resp;
12628 	struct hwrm_wol_filter_qcfg_input *req;
12629 	u16 next_handle = 0;
12630 	int rc;
12631 
12632 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12633 	if (rc)
12634 		return rc;
12635 
12636 	req->port_id = cpu_to_le16(bp->pf.port_id);
12637 	req->handle = cpu_to_le16(handle);
12638 	resp = hwrm_req_hold(bp, req);
12639 	rc = hwrm_req_send(bp, req);
12640 	if (!rc) {
12641 		next_handle = le16_to_cpu(resp->next_handle);
12642 		if (next_handle != 0) {
12643 			if (resp->wol_type ==
12644 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12645 				bp->wol = 1;
12646 				bp->wol_filter_id = resp->wol_filter_id;
12647 			}
12648 		}
12649 	}
12650 	hwrm_req_drop(bp, req);
12651 	return next_handle;
12652 }
12653 
12654 static void bnxt_get_wol_settings(struct bnxt *bp)
12655 {
12656 	u16 handle = 0;
12657 
12658 	bp->wol = 0;
12659 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12660 		return;
12661 
12662 	do {
12663 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12664 	} while (handle && handle != 0xffff);
12665 }
12666 
12667 static bool bnxt_eee_config_ok(struct bnxt *bp)
12668 {
12669 	struct ethtool_keee *eee = &bp->eee;
12670 	struct bnxt_link_info *link_info = &bp->link_info;
12671 
12672 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12673 		return true;
12674 
12675 	if (eee->eee_enabled) {
12676 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12677 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12678 
12679 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12680 
12681 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12682 			eee->eee_enabled = 0;
12683 			return false;
12684 		}
12685 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12686 			linkmode_and(eee->advertised, advertising,
12687 				     eee->supported);
12688 			return false;
12689 		}
12690 	}
12691 	return true;
12692 }
12693 
12694 static int bnxt_update_phy_setting(struct bnxt *bp)
12695 {
12696 	int rc;
12697 	bool update_link = false;
12698 	bool update_pause = false;
12699 	bool update_eee = false;
12700 	struct bnxt_link_info *link_info = &bp->link_info;
12701 
12702 	rc = bnxt_update_link(bp, true);
12703 	if (rc) {
12704 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12705 			   rc);
12706 		return rc;
12707 	}
12708 	if (!BNXT_SINGLE_PF(bp))
12709 		return 0;
12710 
12711 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12712 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12713 	    link_info->req_flow_ctrl)
12714 		update_pause = true;
12715 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12716 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12717 		update_pause = true;
12718 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12719 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12720 			update_link = true;
12721 		if (bnxt_force_speed_updated(link_info))
12722 			update_link = true;
12723 		if (link_info->req_duplex != link_info->duplex_setting)
12724 			update_link = true;
12725 	} else {
12726 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12727 			update_link = true;
12728 		if (bnxt_auto_speed_updated(link_info))
12729 			update_link = true;
12730 	}
12731 
12732 	/* The last close may have shutdown the link, so need to call
12733 	 * PHY_CFG to bring it back up.
12734 	 */
12735 	if (!BNXT_LINK_IS_UP(bp))
12736 		update_link = true;
12737 
12738 	if (!bnxt_eee_config_ok(bp))
12739 		update_eee = true;
12740 
12741 	if (update_link)
12742 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12743 	else if (update_pause)
12744 		rc = bnxt_hwrm_set_pause(bp);
12745 	if (rc) {
12746 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12747 			   rc);
12748 		return rc;
12749 	}
12750 
12751 	return rc;
12752 }
12753 
12754 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12755 
12756 static int bnxt_reinit_after_abort(struct bnxt *bp)
12757 {
12758 	int rc;
12759 
12760 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12761 		return -EBUSY;
12762 
12763 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12764 		return -ENODEV;
12765 
12766 	rc = bnxt_fw_init_one(bp);
12767 	if (!rc) {
12768 		bnxt_clear_int_mode(bp);
12769 		rc = bnxt_init_int_mode(bp);
12770 		if (!rc) {
12771 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12772 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12773 		}
12774 	}
12775 	return rc;
12776 }
12777 
12778 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12779 {
12780 	struct bnxt_ntuple_filter *ntp_fltr;
12781 	struct bnxt_l2_filter *l2_fltr;
12782 
12783 	if (list_empty(&fltr->list))
12784 		return;
12785 
12786 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12787 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12788 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12789 		atomic_inc(&l2_fltr->refcnt);
12790 		ntp_fltr->l2_fltr = l2_fltr;
12791 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12792 			bnxt_del_ntp_filter(bp, ntp_fltr);
12793 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12794 				   fltr->sw_id);
12795 		}
12796 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12797 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12798 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12799 			bnxt_del_l2_filter(bp, l2_fltr);
12800 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12801 				   fltr->sw_id);
12802 		}
12803 	}
12804 }
12805 
12806 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12807 {
12808 	struct bnxt_filter_base *usr_fltr, *tmp;
12809 
12810 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12811 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12812 }
12813 
12814 static int bnxt_set_xps_mapping(struct bnxt *bp)
12815 {
12816 	int numa_node = dev_to_node(&bp->pdev->dev);
12817 	unsigned int q_idx, map_idx, cpu, i;
12818 	const struct cpumask *cpu_mask_ptr;
12819 	int nr_cpus = num_online_cpus();
12820 	cpumask_t *q_map;
12821 	int rc = 0;
12822 
12823 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12824 	if (!q_map)
12825 		return -ENOMEM;
12826 
12827 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12828 	 * Each TC has the same number of TX queues. The nth TX queue for each
12829 	 * TC will have the same CPU mask.
12830 	 */
12831 	for (i = 0; i < nr_cpus; i++) {
12832 		map_idx = i % bp->tx_nr_rings_per_tc;
12833 		cpu = cpumask_local_spread(i, numa_node);
12834 		cpu_mask_ptr = get_cpu_mask(cpu);
12835 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12836 	}
12837 
12838 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12839 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12840 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12841 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12842 		if (rc) {
12843 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12844 				    q_idx);
12845 			break;
12846 		}
12847 	}
12848 
12849 	kfree(q_map);
12850 
12851 	return rc;
12852 }
12853 
12854 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12855 {
12856 	int rc = 0;
12857 
12858 	netif_carrier_off(bp->dev);
12859 	if (irq_re_init) {
12860 		/* Reserve rings now if none were reserved at driver probe. */
12861 		rc = bnxt_init_dflt_ring_mode(bp);
12862 		if (rc) {
12863 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12864 			return rc;
12865 		}
12866 	}
12867 	rc = bnxt_reserve_rings(bp, irq_re_init);
12868 	if (rc)
12869 		return rc;
12870 
12871 	rc = bnxt_alloc_mem(bp, irq_re_init);
12872 	if (rc) {
12873 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12874 		goto open_err_free_mem;
12875 	}
12876 
12877 	if (irq_re_init) {
12878 		bnxt_init_napi(bp);
12879 		rc = bnxt_request_irq(bp);
12880 		if (rc) {
12881 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12882 			goto open_err_irq;
12883 		}
12884 	}
12885 
12886 	rc = bnxt_init_nic(bp, irq_re_init);
12887 	if (rc) {
12888 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12889 		goto open_err_irq;
12890 	}
12891 
12892 	bnxt_enable_napi(bp);
12893 	bnxt_debug_dev_init(bp);
12894 
12895 	if (link_re_init) {
12896 		mutex_lock(&bp->link_lock);
12897 		rc = bnxt_update_phy_setting(bp);
12898 		mutex_unlock(&bp->link_lock);
12899 		if (rc) {
12900 			netdev_warn(bp->dev, "failed to update phy settings\n");
12901 			if (BNXT_SINGLE_PF(bp)) {
12902 				bp->link_info.phy_retry = true;
12903 				bp->link_info.phy_retry_expires =
12904 					jiffies + 5 * HZ;
12905 			}
12906 		}
12907 	}
12908 
12909 	if (irq_re_init) {
12910 		udp_tunnel_nic_reset_ntf(bp->dev);
12911 		rc = bnxt_set_xps_mapping(bp);
12912 		if (rc)
12913 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12914 	}
12915 
12916 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12917 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12918 			static_branch_enable(&bnxt_xdp_locking_key);
12919 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12920 		static_branch_disable(&bnxt_xdp_locking_key);
12921 	}
12922 	set_bit(BNXT_STATE_OPEN, &bp->state);
12923 	bnxt_enable_int(bp);
12924 	/* Enable TX queues */
12925 	bnxt_tx_enable(bp);
12926 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12927 	/* Poll link status and check for SFP+ module status */
12928 	mutex_lock(&bp->link_lock);
12929 	bnxt_get_port_module_status(bp);
12930 	mutex_unlock(&bp->link_lock);
12931 
12932 	/* VF-reps may need to be re-opened after the PF is re-opened */
12933 	if (BNXT_PF(bp))
12934 		bnxt_vf_reps_open(bp);
12935 	bnxt_ptp_init_rtc(bp, true);
12936 	bnxt_ptp_cfg_tstamp_filters(bp);
12937 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12938 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12939 	bnxt_cfg_usr_fltrs(bp);
12940 	return 0;
12941 
12942 open_err_irq:
12943 	bnxt_del_napi(bp);
12944 
12945 open_err_free_mem:
12946 	bnxt_free_skbs(bp);
12947 	bnxt_free_irq(bp);
12948 	bnxt_free_mem(bp, true);
12949 	return rc;
12950 }
12951 
12952 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12953 {
12954 	int rc = 0;
12955 
12956 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12957 		rc = -EIO;
12958 	if (!rc)
12959 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12960 	if (rc) {
12961 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12962 		netif_close(bp->dev);
12963 	}
12964 	return rc;
12965 }
12966 
12967 /* netdev instance lock held, open the NIC half way by allocating all
12968  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12969  * for offline self tests.
12970  */
12971 int bnxt_half_open_nic(struct bnxt *bp)
12972 {
12973 	int rc = 0;
12974 
12975 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12976 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12977 		rc = -ENODEV;
12978 		goto half_open_err;
12979 	}
12980 
12981 	rc = bnxt_alloc_mem(bp, true);
12982 	if (rc) {
12983 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12984 		goto half_open_err;
12985 	}
12986 	bnxt_init_napi(bp);
12987 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12988 	rc = bnxt_init_nic(bp, true);
12989 	if (rc) {
12990 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12991 		bnxt_del_napi(bp);
12992 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12993 		goto half_open_err;
12994 	}
12995 	return 0;
12996 
12997 half_open_err:
12998 	bnxt_free_skbs(bp);
12999 	bnxt_free_mem(bp, true);
13000 	netif_close(bp->dev);
13001 	return rc;
13002 }
13003 
13004 /* netdev instance lock held, this call can only be made after a previous
13005  * successful call to bnxt_half_open_nic().
13006  */
13007 void bnxt_half_close_nic(struct bnxt *bp)
13008 {
13009 	bnxt_hwrm_resource_free(bp, false, true);
13010 	bnxt_del_napi(bp);
13011 	bnxt_free_skbs(bp);
13012 	bnxt_free_mem(bp, true);
13013 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13014 }
13015 
13016 void bnxt_reenable_sriov(struct bnxt *bp)
13017 {
13018 	if (BNXT_PF(bp)) {
13019 		struct bnxt_pf_info *pf = &bp->pf;
13020 		int n = pf->active_vfs;
13021 
13022 		if (n)
13023 			bnxt_cfg_hw_sriov(bp, &n, true);
13024 	}
13025 }
13026 
13027 static int bnxt_open(struct net_device *dev)
13028 {
13029 	struct bnxt *bp = netdev_priv(dev);
13030 	int rc;
13031 
13032 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13033 		rc = bnxt_reinit_after_abort(bp);
13034 		if (rc) {
13035 			if (rc == -EBUSY)
13036 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13037 			else
13038 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13039 			return -ENODEV;
13040 		}
13041 	}
13042 
13043 	rc = bnxt_hwrm_if_change(bp, true);
13044 	if (rc)
13045 		return rc;
13046 
13047 	rc = __bnxt_open_nic(bp, true, true);
13048 	if (rc) {
13049 		bnxt_hwrm_if_change(bp, false);
13050 	} else {
13051 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13052 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13053 				bnxt_queue_sp_work(bp,
13054 						   BNXT_RESTART_ULP_SP_EVENT);
13055 		}
13056 	}
13057 
13058 	return rc;
13059 }
13060 
13061 static bool bnxt_drv_busy(struct bnxt *bp)
13062 {
13063 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13064 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13065 }
13066 
13067 static void bnxt_get_ring_stats(struct bnxt *bp,
13068 				struct rtnl_link_stats64 *stats);
13069 
13070 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13071 			     bool link_re_init)
13072 {
13073 	/* Close the VF-reps before closing PF */
13074 	if (BNXT_PF(bp))
13075 		bnxt_vf_reps_close(bp);
13076 
13077 	/* Change device state to avoid TX queue wake up's */
13078 	bnxt_tx_disable(bp);
13079 
13080 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13081 	smp_mb__after_atomic();
13082 	while (bnxt_drv_busy(bp))
13083 		msleep(20);
13084 
13085 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13086 		bnxt_clear_rss_ctxs(bp);
13087 	/* Flush rings and disable interrupts */
13088 	bnxt_shutdown_nic(bp, irq_re_init);
13089 
13090 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13091 
13092 	bnxt_debug_dev_exit(bp);
13093 	bnxt_disable_napi(bp);
13094 	timer_delete_sync(&bp->timer);
13095 	bnxt_free_skbs(bp);
13096 
13097 	/* Save ring stats before shutdown */
13098 	if (bp->bnapi && irq_re_init) {
13099 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13100 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13101 	}
13102 	if (irq_re_init) {
13103 		bnxt_free_irq(bp);
13104 		bnxt_del_napi(bp);
13105 	}
13106 	bnxt_free_mem(bp, irq_re_init);
13107 }
13108 
13109 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13110 {
13111 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13112 		/* If we get here, it means firmware reset is in progress
13113 		 * while we are trying to close.  We can safely proceed with
13114 		 * the close because we are holding netdev instance lock.
13115 		 * Some firmware messages may fail as we proceed to close.
13116 		 * We set the ABORT_ERR flag here so that the FW reset thread
13117 		 * will later abort when it gets the netdev instance lock
13118 		 * and sees the flag.
13119 		 */
13120 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13121 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13122 	}
13123 
13124 #ifdef CONFIG_BNXT_SRIOV
13125 	if (bp->sriov_cfg) {
13126 		int rc;
13127 
13128 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13129 						      !bp->sriov_cfg,
13130 						      BNXT_SRIOV_CFG_WAIT_TMO);
13131 		if (!rc)
13132 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13133 		else if (rc < 0)
13134 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13135 	}
13136 #endif
13137 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13138 }
13139 
13140 static int bnxt_close(struct net_device *dev)
13141 {
13142 	struct bnxt *bp = netdev_priv(dev);
13143 
13144 	bnxt_close_nic(bp, true, true);
13145 	bnxt_hwrm_shutdown_link(bp);
13146 	bnxt_hwrm_if_change(bp, false);
13147 	return 0;
13148 }
13149 
13150 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13151 				   u16 *val)
13152 {
13153 	struct hwrm_port_phy_mdio_read_output *resp;
13154 	struct hwrm_port_phy_mdio_read_input *req;
13155 	int rc;
13156 
13157 	if (bp->hwrm_spec_code < 0x10a00)
13158 		return -EOPNOTSUPP;
13159 
13160 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13161 	if (rc)
13162 		return rc;
13163 
13164 	req->port_id = cpu_to_le16(bp->pf.port_id);
13165 	req->phy_addr = phy_addr;
13166 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13167 	if (mdio_phy_id_is_c45(phy_addr)) {
13168 		req->cl45_mdio = 1;
13169 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13170 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13171 		req->reg_addr = cpu_to_le16(reg);
13172 	}
13173 
13174 	resp = hwrm_req_hold(bp, req);
13175 	rc = hwrm_req_send(bp, req);
13176 	if (!rc)
13177 		*val = le16_to_cpu(resp->reg_data);
13178 	hwrm_req_drop(bp, req);
13179 	return rc;
13180 }
13181 
13182 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13183 				    u16 val)
13184 {
13185 	struct hwrm_port_phy_mdio_write_input *req;
13186 	int rc;
13187 
13188 	if (bp->hwrm_spec_code < 0x10a00)
13189 		return -EOPNOTSUPP;
13190 
13191 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13192 	if (rc)
13193 		return rc;
13194 
13195 	req->port_id = cpu_to_le16(bp->pf.port_id);
13196 	req->phy_addr = phy_addr;
13197 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13198 	if (mdio_phy_id_is_c45(phy_addr)) {
13199 		req->cl45_mdio = 1;
13200 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13201 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13202 		req->reg_addr = cpu_to_le16(reg);
13203 	}
13204 	req->reg_data = cpu_to_le16(val);
13205 
13206 	return hwrm_req_send(bp, req);
13207 }
13208 
13209 /* netdev instance lock held */
13210 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13211 {
13212 	struct mii_ioctl_data *mdio = if_mii(ifr);
13213 	struct bnxt *bp = netdev_priv(dev);
13214 	int rc;
13215 
13216 	switch (cmd) {
13217 	case SIOCGMIIPHY:
13218 		mdio->phy_id = bp->link_info.phy_addr;
13219 
13220 		fallthrough;
13221 	case SIOCGMIIREG: {
13222 		u16 mii_regval = 0;
13223 
13224 		if (!netif_running(dev))
13225 			return -EAGAIN;
13226 
13227 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13228 					     &mii_regval);
13229 		mdio->val_out = mii_regval;
13230 		return rc;
13231 	}
13232 
13233 	case SIOCSMIIREG:
13234 		if (!netif_running(dev))
13235 			return -EAGAIN;
13236 
13237 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13238 						mdio->val_in);
13239 
13240 	case SIOCSHWTSTAMP:
13241 		return bnxt_hwtstamp_set(dev, ifr);
13242 
13243 	case SIOCGHWTSTAMP:
13244 		return bnxt_hwtstamp_get(dev, ifr);
13245 
13246 	default:
13247 		/* do nothing */
13248 		break;
13249 	}
13250 	return -EOPNOTSUPP;
13251 }
13252 
13253 static void bnxt_get_ring_stats(struct bnxt *bp,
13254 				struct rtnl_link_stats64 *stats)
13255 {
13256 	int i;
13257 
13258 	for (i = 0; i < bp->cp_nr_rings; i++) {
13259 		struct bnxt_napi *bnapi = bp->bnapi[i];
13260 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13261 		u64 *sw = cpr->stats.sw_stats;
13262 
13263 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13264 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13265 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13266 
13267 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13268 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13269 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13270 
13271 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13272 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13273 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13274 
13275 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13276 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13277 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13278 
13279 		stats->rx_missed_errors +=
13280 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13281 
13282 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13283 
13284 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13285 
13286 		stats->rx_dropped +=
13287 			cpr->sw_stats->rx.rx_netpoll_discards +
13288 			cpr->sw_stats->rx.rx_oom_discards;
13289 	}
13290 }
13291 
13292 static void bnxt_add_prev_stats(struct bnxt *bp,
13293 				struct rtnl_link_stats64 *stats)
13294 {
13295 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13296 
13297 	stats->rx_packets += prev_stats->rx_packets;
13298 	stats->tx_packets += prev_stats->tx_packets;
13299 	stats->rx_bytes += prev_stats->rx_bytes;
13300 	stats->tx_bytes += prev_stats->tx_bytes;
13301 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13302 	stats->multicast += prev_stats->multicast;
13303 	stats->rx_dropped += prev_stats->rx_dropped;
13304 	stats->tx_dropped += prev_stats->tx_dropped;
13305 }
13306 
13307 static void
13308 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13309 {
13310 	struct bnxt *bp = netdev_priv(dev);
13311 
13312 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13313 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13314 	 * we check the BNXT_STATE_OPEN flag.
13315 	 */
13316 	smp_mb__after_atomic();
13317 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13318 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13319 		*stats = bp->net_stats_prev;
13320 		return;
13321 	}
13322 
13323 	bnxt_get_ring_stats(bp, stats);
13324 	bnxt_add_prev_stats(bp, stats);
13325 
13326 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13327 		u64 *rx = bp->port_stats.sw_stats;
13328 		u64 *tx = bp->port_stats.sw_stats +
13329 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13330 
13331 		stats->rx_crc_errors =
13332 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13333 		stats->rx_frame_errors =
13334 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13335 		stats->rx_length_errors =
13336 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13337 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13338 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13339 		stats->rx_errors =
13340 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13341 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13342 		stats->collisions =
13343 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13344 		stats->tx_fifo_errors =
13345 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13346 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13347 	}
13348 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13349 }
13350 
13351 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13352 					struct bnxt_total_ring_err_stats *stats,
13353 					struct bnxt_cp_ring_info *cpr)
13354 {
13355 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13356 	u64 *hw_stats = cpr->stats.sw_stats;
13357 
13358 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13359 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13360 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13361 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13362 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13363 	stats->rx_total_ring_discards +=
13364 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13365 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13366 	stats->tx_total_ring_discards +=
13367 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13368 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13369 }
13370 
13371 void bnxt_get_ring_err_stats(struct bnxt *bp,
13372 			     struct bnxt_total_ring_err_stats *stats)
13373 {
13374 	int i;
13375 
13376 	for (i = 0; i < bp->cp_nr_rings; i++)
13377 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13378 }
13379 
13380 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13381 {
13382 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13383 	struct net_device *dev = bp->dev;
13384 	struct netdev_hw_addr *ha;
13385 	u8 *haddr;
13386 	int mc_count = 0;
13387 	bool update = false;
13388 	int off = 0;
13389 
13390 	netdev_for_each_mc_addr(ha, dev) {
13391 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13392 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13393 			vnic->mc_list_count = 0;
13394 			return false;
13395 		}
13396 		haddr = ha->addr;
13397 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13398 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13399 			update = true;
13400 		}
13401 		off += ETH_ALEN;
13402 		mc_count++;
13403 	}
13404 	if (mc_count)
13405 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13406 
13407 	if (mc_count != vnic->mc_list_count) {
13408 		vnic->mc_list_count = mc_count;
13409 		update = true;
13410 	}
13411 	return update;
13412 }
13413 
13414 static bool bnxt_uc_list_updated(struct bnxt *bp)
13415 {
13416 	struct net_device *dev = bp->dev;
13417 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13418 	struct netdev_hw_addr *ha;
13419 	int off = 0;
13420 
13421 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13422 		return true;
13423 
13424 	netdev_for_each_uc_addr(ha, dev) {
13425 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13426 			return true;
13427 
13428 		off += ETH_ALEN;
13429 	}
13430 	return false;
13431 }
13432 
13433 static void bnxt_set_rx_mode(struct net_device *dev)
13434 {
13435 	struct bnxt *bp = netdev_priv(dev);
13436 	struct bnxt_vnic_info *vnic;
13437 	bool mc_update = false;
13438 	bool uc_update;
13439 	u32 mask;
13440 
13441 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13442 		return;
13443 
13444 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13445 	mask = vnic->rx_mask;
13446 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13447 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13448 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13449 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13450 
13451 	if (dev->flags & IFF_PROMISC)
13452 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13453 
13454 	uc_update = bnxt_uc_list_updated(bp);
13455 
13456 	if (dev->flags & IFF_BROADCAST)
13457 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13458 	if (dev->flags & IFF_ALLMULTI) {
13459 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13460 		vnic->mc_list_count = 0;
13461 	} else if (dev->flags & IFF_MULTICAST) {
13462 		mc_update = bnxt_mc_list_updated(bp, &mask);
13463 	}
13464 
13465 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13466 		vnic->rx_mask = mask;
13467 
13468 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13469 	}
13470 }
13471 
13472 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13473 {
13474 	struct net_device *dev = bp->dev;
13475 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13476 	struct netdev_hw_addr *ha;
13477 	int i, off = 0, rc;
13478 	bool uc_update;
13479 
13480 	netif_addr_lock_bh(dev);
13481 	uc_update = bnxt_uc_list_updated(bp);
13482 	netif_addr_unlock_bh(dev);
13483 
13484 	if (!uc_update)
13485 		goto skip_uc;
13486 
13487 	for (i = 1; i < vnic->uc_filter_count; i++) {
13488 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13489 
13490 		bnxt_hwrm_l2_filter_free(bp, fltr);
13491 		bnxt_del_l2_filter(bp, fltr);
13492 	}
13493 
13494 	vnic->uc_filter_count = 1;
13495 
13496 	netif_addr_lock_bh(dev);
13497 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13498 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13499 	} else {
13500 		netdev_for_each_uc_addr(ha, dev) {
13501 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13502 			off += ETH_ALEN;
13503 			vnic->uc_filter_count++;
13504 		}
13505 	}
13506 	netif_addr_unlock_bh(dev);
13507 
13508 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13509 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13510 		if (rc) {
13511 			if (BNXT_VF(bp) && rc == -ENODEV) {
13512 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13513 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13514 				else
13515 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13516 				rc = 0;
13517 			} else {
13518 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13519 			}
13520 			vnic->uc_filter_count = i;
13521 			return rc;
13522 		}
13523 	}
13524 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13525 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13526 
13527 skip_uc:
13528 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13529 	    !bnxt_promisc_ok(bp))
13530 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13531 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13532 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13533 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13534 			    rc);
13535 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13536 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13537 		vnic->mc_list_count = 0;
13538 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13539 	}
13540 	if (rc)
13541 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13542 			   rc);
13543 
13544 	return rc;
13545 }
13546 
13547 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13548 {
13549 #ifdef CONFIG_BNXT_SRIOV
13550 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13551 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13552 
13553 		/* No minimum rings were provisioned by the PF.  Don't
13554 		 * reserve rings by default when device is down.
13555 		 */
13556 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13557 			return true;
13558 
13559 		if (!netif_running(bp->dev))
13560 			return false;
13561 	}
13562 #endif
13563 	return true;
13564 }
13565 
13566 /* If the chip and firmware supports RFS */
13567 static bool bnxt_rfs_supported(struct bnxt *bp)
13568 {
13569 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13570 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13571 			return true;
13572 		return false;
13573 	}
13574 	/* 212 firmware is broken for aRFS */
13575 	if (BNXT_FW_MAJ(bp) == 212)
13576 		return false;
13577 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13578 		return true;
13579 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13580 		return true;
13581 	return false;
13582 }
13583 
13584 /* If runtime conditions support RFS */
13585 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13586 {
13587 	struct bnxt_hw_rings hwr = {0};
13588 	int max_vnics, max_rss_ctxs;
13589 
13590 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13591 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13592 		return bnxt_rfs_supported(bp);
13593 
13594 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13595 		return false;
13596 
13597 	hwr.grp = bp->rx_nr_rings;
13598 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13599 	if (new_rss_ctx)
13600 		hwr.vnic++;
13601 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13602 	max_vnics = bnxt_get_max_func_vnics(bp);
13603 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13604 
13605 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13606 		if (bp->rx_nr_rings > 1)
13607 			netdev_warn(bp->dev,
13608 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13609 				    min(max_rss_ctxs - 1, max_vnics - 1));
13610 		return false;
13611 	}
13612 
13613 	if (!BNXT_NEW_RM(bp))
13614 		return true;
13615 
13616 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13617 	 * issue that will mess up the default VNIC if we reduce the
13618 	 * reservations.
13619 	 */
13620 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13621 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13622 		return true;
13623 
13624 	bnxt_hwrm_reserve_rings(bp, &hwr);
13625 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13626 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13627 		return true;
13628 
13629 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13630 	hwr.vnic = 1;
13631 	hwr.rss_ctx = 0;
13632 	bnxt_hwrm_reserve_rings(bp, &hwr);
13633 	return false;
13634 }
13635 
13636 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13637 					   netdev_features_t features)
13638 {
13639 	struct bnxt *bp = netdev_priv(dev);
13640 	netdev_features_t vlan_features;
13641 
13642 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13643 		features &= ~NETIF_F_NTUPLE;
13644 
13645 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13646 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13647 
13648 	if (!(features & NETIF_F_GRO))
13649 		features &= ~NETIF_F_GRO_HW;
13650 
13651 	if (features & NETIF_F_GRO_HW)
13652 		features &= ~NETIF_F_LRO;
13653 
13654 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13655 	 * turned on or off together.
13656 	 */
13657 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13658 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13659 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13660 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13661 		else if (vlan_features)
13662 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13663 	}
13664 #ifdef CONFIG_BNXT_SRIOV
13665 	if (BNXT_VF(bp) && bp->vf.vlan)
13666 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13667 #endif
13668 	return features;
13669 }
13670 
13671 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13672 				bool link_re_init, u32 flags, bool update_tpa)
13673 {
13674 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13675 	bp->flags = flags;
13676 	if (update_tpa)
13677 		bnxt_set_ring_params(bp);
13678 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13679 }
13680 
13681 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13682 {
13683 	bool update_tpa = false, update_ntuple = false;
13684 	struct bnxt *bp = netdev_priv(dev);
13685 	u32 flags = bp->flags;
13686 	u32 changes;
13687 	int rc = 0;
13688 	bool re_init = false;
13689 
13690 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13691 	if (features & NETIF_F_GRO_HW)
13692 		flags |= BNXT_FLAG_GRO;
13693 	else if (features & NETIF_F_LRO)
13694 		flags |= BNXT_FLAG_LRO;
13695 
13696 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13697 		flags &= ~BNXT_FLAG_TPA;
13698 
13699 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13700 		flags |= BNXT_FLAG_STRIP_VLAN;
13701 
13702 	if (features & NETIF_F_NTUPLE)
13703 		flags |= BNXT_FLAG_RFS;
13704 	else
13705 		bnxt_clear_usr_fltrs(bp, true);
13706 
13707 	changes = flags ^ bp->flags;
13708 	if (changes & BNXT_FLAG_TPA) {
13709 		update_tpa = true;
13710 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13711 		    (flags & BNXT_FLAG_TPA) == 0 ||
13712 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13713 			re_init = true;
13714 	}
13715 
13716 	if (changes & ~BNXT_FLAG_TPA)
13717 		re_init = true;
13718 
13719 	if (changes & BNXT_FLAG_RFS)
13720 		update_ntuple = true;
13721 
13722 	if (flags != bp->flags) {
13723 		u32 old_flags = bp->flags;
13724 
13725 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13726 			bp->flags = flags;
13727 			if (update_tpa)
13728 				bnxt_set_ring_params(bp);
13729 			return rc;
13730 		}
13731 
13732 		if (update_ntuple)
13733 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13734 
13735 		if (re_init)
13736 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13737 
13738 		if (update_tpa) {
13739 			bp->flags = flags;
13740 			rc = bnxt_set_tpa(bp,
13741 					  (flags & BNXT_FLAG_TPA) ?
13742 					  true : false);
13743 			if (rc)
13744 				bp->flags = old_flags;
13745 		}
13746 	}
13747 	return rc;
13748 }
13749 
13750 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13751 			      u8 **nextp)
13752 {
13753 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13754 	struct hop_jumbo_hdr *jhdr;
13755 	int hdr_count = 0;
13756 	u8 *nexthdr;
13757 	int start;
13758 
13759 	/* Check that there are at most 2 IPv6 extension headers, no
13760 	 * fragment header, and each is <= 64 bytes.
13761 	 */
13762 	start = nw_off + sizeof(*ip6h);
13763 	nexthdr = &ip6h->nexthdr;
13764 	while (ipv6_ext_hdr(*nexthdr)) {
13765 		struct ipv6_opt_hdr *hp;
13766 		int hdrlen;
13767 
13768 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13769 		    *nexthdr == NEXTHDR_FRAGMENT)
13770 			return false;
13771 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13772 					  skb_headlen(skb), NULL);
13773 		if (!hp)
13774 			return false;
13775 		if (*nexthdr == NEXTHDR_AUTH)
13776 			hdrlen = ipv6_authlen(hp);
13777 		else
13778 			hdrlen = ipv6_optlen(hp);
13779 
13780 		if (hdrlen > 64)
13781 			return false;
13782 
13783 		/* The ext header may be a hop-by-hop header inserted for
13784 		 * big TCP purposes. This will be removed before sending
13785 		 * from NIC, so do not count it.
13786 		 */
13787 		if (*nexthdr == NEXTHDR_HOP) {
13788 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13789 				goto increment_hdr;
13790 
13791 			jhdr = (struct hop_jumbo_hdr *)hp;
13792 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13793 			    jhdr->nexthdr != IPPROTO_TCP)
13794 				goto increment_hdr;
13795 
13796 			goto next_hdr;
13797 		}
13798 increment_hdr:
13799 		hdr_count++;
13800 next_hdr:
13801 		nexthdr = &hp->nexthdr;
13802 		start += hdrlen;
13803 	}
13804 	if (nextp) {
13805 		/* Caller will check inner protocol */
13806 		if (skb->encapsulation) {
13807 			*nextp = nexthdr;
13808 			return true;
13809 		}
13810 		*nextp = NULL;
13811 	}
13812 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13813 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13814 }
13815 
13816 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13817 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13818 {
13819 	struct udphdr *uh = udp_hdr(skb);
13820 	__be16 udp_port = uh->dest;
13821 
13822 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13823 	    udp_port != bp->vxlan_gpe_port)
13824 		return false;
13825 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13826 		struct ethhdr *eh = inner_eth_hdr(skb);
13827 
13828 		switch (eh->h_proto) {
13829 		case htons(ETH_P_IP):
13830 			return true;
13831 		case htons(ETH_P_IPV6):
13832 			return bnxt_exthdr_check(bp, skb,
13833 						 skb_inner_network_offset(skb),
13834 						 NULL);
13835 		}
13836 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13837 		return true;
13838 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13839 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13840 					 NULL);
13841 	}
13842 	return false;
13843 }
13844 
13845 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13846 {
13847 	switch (l4_proto) {
13848 	case IPPROTO_UDP:
13849 		return bnxt_udp_tunl_check(bp, skb);
13850 	case IPPROTO_IPIP:
13851 		return true;
13852 	case IPPROTO_GRE: {
13853 		switch (skb->inner_protocol) {
13854 		default:
13855 			return false;
13856 		case htons(ETH_P_IP):
13857 			return true;
13858 		case htons(ETH_P_IPV6):
13859 			fallthrough;
13860 		}
13861 	}
13862 	case IPPROTO_IPV6:
13863 		/* Check ext headers of inner ipv6 */
13864 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13865 					 NULL);
13866 	}
13867 	return false;
13868 }
13869 
13870 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13871 					     struct net_device *dev,
13872 					     netdev_features_t features)
13873 {
13874 	struct bnxt *bp = netdev_priv(dev);
13875 	u8 *l4_proto;
13876 
13877 	features = vlan_features_check(skb, features);
13878 	switch (vlan_get_protocol(skb)) {
13879 	case htons(ETH_P_IP):
13880 		if (!skb->encapsulation)
13881 			return features;
13882 		l4_proto = &ip_hdr(skb)->protocol;
13883 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13884 			return features;
13885 		break;
13886 	case htons(ETH_P_IPV6):
13887 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13888 				       &l4_proto))
13889 			break;
13890 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13891 			return features;
13892 		break;
13893 	}
13894 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13895 }
13896 
13897 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13898 			 u32 *reg_buf)
13899 {
13900 	struct hwrm_dbg_read_direct_output *resp;
13901 	struct hwrm_dbg_read_direct_input *req;
13902 	__le32 *dbg_reg_buf;
13903 	dma_addr_t mapping;
13904 	int rc, i;
13905 
13906 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13907 	if (rc)
13908 		return rc;
13909 
13910 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13911 					 &mapping);
13912 	if (!dbg_reg_buf) {
13913 		rc = -ENOMEM;
13914 		goto dbg_rd_reg_exit;
13915 	}
13916 
13917 	req->host_dest_addr = cpu_to_le64(mapping);
13918 
13919 	resp = hwrm_req_hold(bp, req);
13920 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13921 	req->read_len32 = cpu_to_le32(num_words);
13922 
13923 	rc = hwrm_req_send(bp, req);
13924 	if (rc || resp->error_code) {
13925 		rc = -EIO;
13926 		goto dbg_rd_reg_exit;
13927 	}
13928 	for (i = 0; i < num_words; i++)
13929 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13930 
13931 dbg_rd_reg_exit:
13932 	hwrm_req_drop(bp, req);
13933 	return rc;
13934 }
13935 
13936 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13937 				       u32 ring_id, u32 *prod, u32 *cons)
13938 {
13939 	struct hwrm_dbg_ring_info_get_output *resp;
13940 	struct hwrm_dbg_ring_info_get_input *req;
13941 	int rc;
13942 
13943 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13944 	if (rc)
13945 		return rc;
13946 
13947 	req->ring_type = ring_type;
13948 	req->fw_ring_id = cpu_to_le32(ring_id);
13949 	resp = hwrm_req_hold(bp, req);
13950 	rc = hwrm_req_send(bp, req);
13951 	if (!rc) {
13952 		*prod = le32_to_cpu(resp->producer_index);
13953 		*cons = le32_to_cpu(resp->consumer_index);
13954 	}
13955 	hwrm_req_drop(bp, req);
13956 	return rc;
13957 }
13958 
13959 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13960 {
13961 	struct bnxt_tx_ring_info *txr;
13962 	int i = bnapi->index, j;
13963 
13964 	bnxt_for_each_napi_tx(j, bnapi, txr)
13965 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13966 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13967 			    txr->tx_cons);
13968 }
13969 
13970 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13971 {
13972 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13973 	int i = bnapi->index;
13974 
13975 	if (!rxr)
13976 		return;
13977 
13978 	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",
13979 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13980 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13981 		    rxr->rx_sw_agg_prod);
13982 }
13983 
13984 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13985 {
13986 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13987 	int i = bnapi->index;
13988 
13989 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13990 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13991 }
13992 
13993 static void bnxt_dbg_dump_states(struct bnxt *bp)
13994 {
13995 	int i;
13996 	struct bnxt_napi *bnapi;
13997 
13998 	for (i = 0; i < bp->cp_nr_rings; i++) {
13999 		bnapi = bp->bnapi[i];
14000 		if (netif_msg_drv(bp)) {
14001 			bnxt_dump_tx_sw_state(bnapi);
14002 			bnxt_dump_rx_sw_state(bnapi);
14003 			bnxt_dump_cp_sw_state(bnapi);
14004 		}
14005 	}
14006 }
14007 
14008 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14009 {
14010 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14011 	struct hwrm_ring_reset_input *req;
14012 	struct bnxt_napi *bnapi = rxr->bnapi;
14013 	struct bnxt_cp_ring_info *cpr;
14014 	u16 cp_ring_id;
14015 	int rc;
14016 
14017 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14018 	if (rc)
14019 		return rc;
14020 
14021 	cpr = &bnapi->cp_ring;
14022 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14023 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14024 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14025 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14026 	return hwrm_req_send_silent(bp, req);
14027 }
14028 
14029 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14030 {
14031 	if (!silent)
14032 		bnxt_dbg_dump_states(bp);
14033 	if (netif_running(bp->dev)) {
14034 		bnxt_close_nic(bp, !silent, false);
14035 		bnxt_open_nic(bp, !silent, false);
14036 	}
14037 }
14038 
14039 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14040 {
14041 	struct bnxt *bp = netdev_priv(dev);
14042 
14043 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14044 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14045 }
14046 
14047 static void bnxt_fw_health_check(struct bnxt *bp)
14048 {
14049 	struct bnxt_fw_health *fw_health = bp->fw_health;
14050 	struct pci_dev *pdev = bp->pdev;
14051 	u32 val;
14052 
14053 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14054 		return;
14055 
14056 	/* Make sure it is enabled before checking the tmr_counter. */
14057 	smp_rmb();
14058 	if (fw_health->tmr_counter) {
14059 		fw_health->tmr_counter--;
14060 		return;
14061 	}
14062 
14063 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14064 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14065 		fw_health->arrests++;
14066 		goto fw_reset;
14067 	}
14068 
14069 	fw_health->last_fw_heartbeat = val;
14070 
14071 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14072 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14073 		fw_health->discoveries++;
14074 		goto fw_reset;
14075 	}
14076 
14077 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14078 	return;
14079 
14080 fw_reset:
14081 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14082 }
14083 
14084 static void bnxt_timer(struct timer_list *t)
14085 {
14086 	struct bnxt *bp = timer_container_of(bp, t, timer);
14087 	struct net_device *dev = bp->dev;
14088 
14089 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14090 		return;
14091 
14092 	if (atomic_read(&bp->intr_sem) != 0)
14093 		goto bnxt_restart_timer;
14094 
14095 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14096 		bnxt_fw_health_check(bp);
14097 
14098 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14099 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14100 
14101 	if (bnxt_tc_flower_enabled(bp))
14102 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14103 
14104 #ifdef CONFIG_RFS_ACCEL
14105 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14106 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14107 #endif /*CONFIG_RFS_ACCEL*/
14108 
14109 	if (bp->link_info.phy_retry) {
14110 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14111 			bp->link_info.phy_retry = false;
14112 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14113 		} else {
14114 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14115 		}
14116 	}
14117 
14118 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14119 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14120 
14121 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14122 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14123 
14124 bnxt_restart_timer:
14125 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14126 }
14127 
14128 static void bnxt_lock_sp(struct bnxt *bp)
14129 {
14130 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14131 	 * set.  If the device is being closed, bnxt_close() may be holding
14132 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14133 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14134 	 * instance lock.
14135 	 */
14136 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14137 	netdev_lock(bp->dev);
14138 }
14139 
14140 static void bnxt_unlock_sp(struct bnxt *bp)
14141 {
14142 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14143 	netdev_unlock(bp->dev);
14144 }
14145 
14146 /* Only called from bnxt_sp_task() */
14147 static void bnxt_reset(struct bnxt *bp, bool silent)
14148 {
14149 	bnxt_lock_sp(bp);
14150 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14151 		bnxt_reset_task(bp, silent);
14152 	bnxt_unlock_sp(bp);
14153 }
14154 
14155 /* Only called from bnxt_sp_task() */
14156 static void bnxt_rx_ring_reset(struct bnxt *bp)
14157 {
14158 	int i;
14159 
14160 	bnxt_lock_sp(bp);
14161 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14162 		bnxt_unlock_sp(bp);
14163 		return;
14164 	}
14165 	/* Disable and flush TPA before resetting the RX ring */
14166 	if (bp->flags & BNXT_FLAG_TPA)
14167 		bnxt_set_tpa(bp, false);
14168 	for (i = 0; i < bp->rx_nr_rings; i++) {
14169 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14170 		struct bnxt_cp_ring_info *cpr;
14171 		int rc;
14172 
14173 		if (!rxr->bnapi->in_reset)
14174 			continue;
14175 
14176 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14177 		if (rc) {
14178 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14179 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14180 			else
14181 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14182 					    rc);
14183 			bnxt_reset_task(bp, true);
14184 			break;
14185 		}
14186 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14187 		rxr->rx_prod = 0;
14188 		rxr->rx_agg_prod = 0;
14189 		rxr->rx_sw_agg_prod = 0;
14190 		rxr->rx_next_cons = 0;
14191 		rxr->bnapi->in_reset = false;
14192 		bnxt_alloc_one_rx_ring(bp, i);
14193 		cpr = &rxr->bnapi->cp_ring;
14194 		cpr->sw_stats->rx.rx_resets++;
14195 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14196 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14197 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14198 	}
14199 	if (bp->flags & BNXT_FLAG_TPA)
14200 		bnxt_set_tpa(bp, true);
14201 	bnxt_unlock_sp(bp);
14202 }
14203 
14204 static void bnxt_fw_fatal_close(struct bnxt *bp)
14205 {
14206 	bnxt_tx_disable(bp);
14207 	bnxt_disable_napi(bp);
14208 	bnxt_disable_int_sync(bp);
14209 	bnxt_free_irq(bp);
14210 	bnxt_clear_int_mode(bp);
14211 	pci_disable_device(bp->pdev);
14212 }
14213 
14214 static void bnxt_fw_reset_close(struct bnxt *bp)
14215 {
14216 	/* When firmware is in fatal state, quiesce device and disable
14217 	 * bus master to prevent any potential bad DMAs before freeing
14218 	 * kernel memory.
14219 	 */
14220 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14221 		u16 val = 0;
14222 
14223 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14224 		if (val == 0xffff)
14225 			bp->fw_reset_min_dsecs = 0;
14226 		bnxt_fw_fatal_close(bp);
14227 	}
14228 	__bnxt_close_nic(bp, true, false);
14229 	bnxt_vf_reps_free(bp);
14230 	bnxt_clear_int_mode(bp);
14231 	bnxt_hwrm_func_drv_unrgtr(bp);
14232 	if (pci_is_enabled(bp->pdev))
14233 		pci_disable_device(bp->pdev);
14234 	bnxt_free_ctx_mem(bp, false);
14235 }
14236 
14237 static bool is_bnxt_fw_ok(struct bnxt *bp)
14238 {
14239 	struct bnxt_fw_health *fw_health = bp->fw_health;
14240 	bool no_heartbeat = false, has_reset = false;
14241 	u32 val;
14242 
14243 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14244 	if (val == fw_health->last_fw_heartbeat)
14245 		no_heartbeat = true;
14246 
14247 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14248 	if (val != fw_health->last_fw_reset_cnt)
14249 		has_reset = true;
14250 
14251 	if (!no_heartbeat && has_reset)
14252 		return true;
14253 
14254 	return false;
14255 }
14256 
14257 /* netdev instance lock is acquired before calling this function */
14258 static void bnxt_force_fw_reset(struct bnxt *bp)
14259 {
14260 	struct bnxt_fw_health *fw_health = bp->fw_health;
14261 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14262 	u32 wait_dsecs;
14263 
14264 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14265 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14266 		return;
14267 
14268 	/* we have to serialize with bnxt_refclk_read()*/
14269 	if (ptp) {
14270 		unsigned long flags;
14271 
14272 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14273 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14274 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14275 	} else {
14276 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14277 	}
14278 	bnxt_fw_reset_close(bp);
14279 	wait_dsecs = fw_health->master_func_wait_dsecs;
14280 	if (fw_health->primary) {
14281 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14282 			wait_dsecs = 0;
14283 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14284 	} else {
14285 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14286 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14287 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14288 	}
14289 
14290 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14291 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14292 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14293 }
14294 
14295 void bnxt_fw_exception(struct bnxt *bp)
14296 {
14297 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14298 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14299 	bnxt_ulp_stop(bp);
14300 	bnxt_lock_sp(bp);
14301 	bnxt_force_fw_reset(bp);
14302 	bnxt_unlock_sp(bp);
14303 }
14304 
14305 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14306  * < 0 on error.
14307  */
14308 static int bnxt_get_registered_vfs(struct bnxt *bp)
14309 {
14310 #ifdef CONFIG_BNXT_SRIOV
14311 	int rc;
14312 
14313 	if (!BNXT_PF(bp))
14314 		return 0;
14315 
14316 	rc = bnxt_hwrm_func_qcfg(bp);
14317 	if (rc) {
14318 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14319 		return rc;
14320 	}
14321 	if (bp->pf.registered_vfs)
14322 		return bp->pf.registered_vfs;
14323 	if (bp->sriov_cfg)
14324 		return 1;
14325 #endif
14326 	return 0;
14327 }
14328 
14329 void bnxt_fw_reset(struct bnxt *bp)
14330 {
14331 	bnxt_ulp_stop(bp);
14332 	bnxt_lock_sp(bp);
14333 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14334 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14335 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14336 		int n = 0, tmo;
14337 
14338 		/* we have to serialize with bnxt_refclk_read()*/
14339 		if (ptp) {
14340 			unsigned long flags;
14341 
14342 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14343 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14344 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14345 		} else {
14346 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14347 		}
14348 		if (bp->pf.active_vfs &&
14349 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14350 			n = bnxt_get_registered_vfs(bp);
14351 		if (n < 0) {
14352 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14353 				   n);
14354 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14355 			netif_close(bp->dev);
14356 			goto fw_reset_exit;
14357 		} else if (n > 0) {
14358 			u16 vf_tmo_dsecs = n * 10;
14359 
14360 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14361 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14362 			bp->fw_reset_state =
14363 				BNXT_FW_RESET_STATE_POLL_VF;
14364 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14365 			goto fw_reset_exit;
14366 		}
14367 		bnxt_fw_reset_close(bp);
14368 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14369 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14370 			tmo = HZ / 10;
14371 		} else {
14372 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14373 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14374 		}
14375 		bnxt_queue_fw_reset_work(bp, tmo);
14376 	}
14377 fw_reset_exit:
14378 	bnxt_unlock_sp(bp);
14379 }
14380 
14381 static void bnxt_chk_missed_irq(struct bnxt *bp)
14382 {
14383 	int i;
14384 
14385 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14386 		return;
14387 
14388 	for (i = 0; i < bp->cp_nr_rings; i++) {
14389 		struct bnxt_napi *bnapi = bp->bnapi[i];
14390 		struct bnxt_cp_ring_info *cpr;
14391 		u32 fw_ring_id;
14392 		int j;
14393 
14394 		if (!bnapi)
14395 			continue;
14396 
14397 		cpr = &bnapi->cp_ring;
14398 		for (j = 0; j < cpr->cp_ring_count; j++) {
14399 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14400 			u32 val[2];
14401 
14402 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14403 				continue;
14404 
14405 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14406 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14407 				continue;
14408 			}
14409 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14410 			bnxt_dbg_hwrm_ring_info_get(bp,
14411 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14412 				fw_ring_id, &val[0], &val[1]);
14413 			cpr->sw_stats->cmn.missed_irqs++;
14414 		}
14415 	}
14416 }
14417 
14418 static void bnxt_cfg_ntp_filters(struct bnxt *);
14419 
14420 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14421 {
14422 	struct bnxt_link_info *link_info = &bp->link_info;
14423 
14424 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14425 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14426 		if (bp->hwrm_spec_code >= 0x10201) {
14427 			if (link_info->auto_pause_setting &
14428 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14429 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14430 		} else {
14431 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14432 		}
14433 		bnxt_set_auto_speed(link_info);
14434 	} else {
14435 		bnxt_set_force_speed(link_info);
14436 		link_info->req_duplex = link_info->duplex_setting;
14437 	}
14438 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14439 		link_info->req_flow_ctrl =
14440 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14441 	else
14442 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14443 }
14444 
14445 static void bnxt_fw_echo_reply(struct bnxt *bp)
14446 {
14447 	struct bnxt_fw_health *fw_health = bp->fw_health;
14448 	struct hwrm_func_echo_response_input *req;
14449 	int rc;
14450 
14451 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14452 	if (rc)
14453 		return;
14454 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14455 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14456 	hwrm_req_send(bp, req);
14457 }
14458 
14459 static void bnxt_ulp_restart(struct bnxt *bp)
14460 {
14461 	bnxt_ulp_stop(bp);
14462 	bnxt_ulp_start(bp, 0);
14463 }
14464 
14465 static void bnxt_sp_task(struct work_struct *work)
14466 {
14467 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14468 
14469 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14470 	smp_mb__after_atomic();
14471 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14472 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14473 		return;
14474 	}
14475 
14476 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14477 		bnxt_ulp_restart(bp);
14478 		bnxt_reenable_sriov(bp);
14479 	}
14480 
14481 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14482 		bnxt_cfg_rx_mode(bp);
14483 
14484 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14485 		bnxt_cfg_ntp_filters(bp);
14486 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14487 		bnxt_hwrm_exec_fwd_req(bp);
14488 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14489 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14490 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14491 		bnxt_hwrm_port_qstats(bp, 0);
14492 		bnxt_hwrm_port_qstats_ext(bp, 0);
14493 		bnxt_accumulate_all_stats(bp);
14494 	}
14495 
14496 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14497 		int rc;
14498 
14499 		mutex_lock(&bp->link_lock);
14500 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14501 				       &bp->sp_event))
14502 			bnxt_hwrm_phy_qcaps(bp);
14503 
14504 		rc = bnxt_update_link(bp, true);
14505 		if (rc)
14506 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14507 				   rc);
14508 
14509 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14510 				       &bp->sp_event))
14511 			bnxt_init_ethtool_link_settings(bp);
14512 		mutex_unlock(&bp->link_lock);
14513 	}
14514 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14515 		int rc;
14516 
14517 		mutex_lock(&bp->link_lock);
14518 		rc = bnxt_update_phy_setting(bp);
14519 		mutex_unlock(&bp->link_lock);
14520 		if (rc) {
14521 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14522 		} else {
14523 			bp->link_info.phy_retry = false;
14524 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14525 		}
14526 	}
14527 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14528 		mutex_lock(&bp->link_lock);
14529 		bnxt_get_port_module_status(bp);
14530 		mutex_unlock(&bp->link_lock);
14531 	}
14532 
14533 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14534 		bnxt_tc_flow_stats_work(bp);
14535 
14536 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14537 		bnxt_chk_missed_irq(bp);
14538 
14539 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14540 		bnxt_fw_echo_reply(bp);
14541 
14542 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14543 		bnxt_hwmon_notify_event(bp);
14544 
14545 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14546 	 * must be the last functions to be called before exiting.
14547 	 */
14548 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14549 		bnxt_reset(bp, false);
14550 
14551 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14552 		bnxt_reset(bp, true);
14553 
14554 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14555 		bnxt_rx_ring_reset(bp);
14556 
14557 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14558 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14559 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14560 			bnxt_devlink_health_fw_report(bp);
14561 		else
14562 			bnxt_fw_reset(bp);
14563 	}
14564 
14565 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14566 		if (!is_bnxt_fw_ok(bp))
14567 			bnxt_devlink_health_fw_report(bp);
14568 	}
14569 
14570 	smp_mb__before_atomic();
14571 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14572 }
14573 
14574 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14575 				int *max_cp);
14576 
14577 /* Under netdev instance lock */
14578 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14579 		     int tx_xdp)
14580 {
14581 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14582 	struct bnxt_hw_rings hwr = {0};
14583 	int rx_rings = rx;
14584 	int rc;
14585 
14586 	if (tcs)
14587 		tx_sets = tcs;
14588 
14589 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14590 
14591 	if (max_rx < rx_rings)
14592 		return -ENOMEM;
14593 
14594 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14595 		rx_rings <<= 1;
14596 
14597 	hwr.rx = rx_rings;
14598 	hwr.tx = tx * tx_sets + tx_xdp;
14599 	if (max_tx < hwr.tx)
14600 		return -ENOMEM;
14601 
14602 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14603 
14604 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14605 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14606 	if (max_cp < hwr.cp)
14607 		return -ENOMEM;
14608 	hwr.stat = hwr.cp;
14609 	if (BNXT_NEW_RM(bp)) {
14610 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14611 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14612 		hwr.grp = rx;
14613 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14614 	}
14615 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14616 		hwr.cp_p5 = hwr.tx + rx;
14617 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14618 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14619 		if (!bnxt_ulp_registered(bp->edev)) {
14620 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14621 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14622 		}
14623 		if (hwr.cp > bp->total_irqs) {
14624 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14625 
14626 			if (total_msix < hwr.cp) {
14627 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14628 					    hwr.cp, total_msix);
14629 				rc = -ENOSPC;
14630 			}
14631 		}
14632 	}
14633 	return rc;
14634 }
14635 
14636 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14637 {
14638 	if (bp->bar2) {
14639 		pci_iounmap(pdev, bp->bar2);
14640 		bp->bar2 = NULL;
14641 	}
14642 
14643 	if (bp->bar1) {
14644 		pci_iounmap(pdev, bp->bar1);
14645 		bp->bar1 = NULL;
14646 	}
14647 
14648 	if (bp->bar0) {
14649 		pci_iounmap(pdev, bp->bar0);
14650 		bp->bar0 = NULL;
14651 	}
14652 }
14653 
14654 static void bnxt_cleanup_pci(struct bnxt *bp)
14655 {
14656 	bnxt_unmap_bars(bp, bp->pdev);
14657 	pci_release_regions(bp->pdev);
14658 	if (pci_is_enabled(bp->pdev))
14659 		pci_disable_device(bp->pdev);
14660 }
14661 
14662 static void bnxt_init_dflt_coal(struct bnxt *bp)
14663 {
14664 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14665 	struct bnxt_coal *coal;
14666 	u16 flags = 0;
14667 
14668 	if (coal_cap->cmpl_params &
14669 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14670 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14671 
14672 	/* Tick values in micro seconds.
14673 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14674 	 */
14675 	coal = &bp->rx_coal;
14676 	coal->coal_ticks = 10;
14677 	coal->coal_bufs = 30;
14678 	coal->coal_ticks_irq = 1;
14679 	coal->coal_bufs_irq = 2;
14680 	coal->idle_thresh = 50;
14681 	coal->bufs_per_record = 2;
14682 	coal->budget = 64;		/* NAPI budget */
14683 	coal->flags = flags;
14684 
14685 	coal = &bp->tx_coal;
14686 	coal->coal_ticks = 28;
14687 	coal->coal_bufs = 30;
14688 	coal->coal_ticks_irq = 2;
14689 	coal->coal_bufs_irq = 2;
14690 	coal->bufs_per_record = 1;
14691 	coal->flags = flags;
14692 
14693 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14694 }
14695 
14696 /* FW that pre-reserves 1 VNIC per function */
14697 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14698 {
14699 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14700 
14701 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14702 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14703 		return true;
14704 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14705 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14706 		return true;
14707 	return false;
14708 }
14709 
14710 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14711 {
14712 	int rc;
14713 
14714 	bp->fw_cap = 0;
14715 	rc = bnxt_hwrm_ver_get(bp);
14716 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14717 	 * so wait before continuing with recovery.
14718 	 */
14719 	if (rc)
14720 		msleep(100);
14721 	bnxt_try_map_fw_health_reg(bp);
14722 	if (rc) {
14723 		rc = bnxt_try_recover_fw(bp);
14724 		if (rc)
14725 			return rc;
14726 		rc = bnxt_hwrm_ver_get(bp);
14727 		if (rc)
14728 			return rc;
14729 	}
14730 
14731 	bnxt_nvm_cfg_ver_get(bp);
14732 
14733 	rc = bnxt_hwrm_func_reset(bp);
14734 	if (rc)
14735 		return -ENODEV;
14736 
14737 	bnxt_hwrm_fw_set_time(bp);
14738 	return 0;
14739 }
14740 
14741 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14742 {
14743 	int rc;
14744 
14745 	/* Get the MAX capabilities for this function */
14746 	rc = bnxt_hwrm_func_qcaps(bp);
14747 	if (rc) {
14748 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14749 			   rc);
14750 		return -ENODEV;
14751 	}
14752 
14753 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14754 	if (rc)
14755 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14756 			    rc);
14757 
14758 	if (bnxt_alloc_fw_health(bp)) {
14759 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14760 	} else {
14761 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14762 		if (rc)
14763 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14764 				    rc);
14765 	}
14766 
14767 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14768 	if (rc)
14769 		return -ENODEV;
14770 
14771 	rc = bnxt_alloc_crash_dump_mem(bp);
14772 	if (rc)
14773 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14774 			    rc);
14775 	if (!rc) {
14776 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14777 		if (rc) {
14778 			bnxt_free_crash_dump_mem(bp);
14779 			netdev_warn(bp->dev,
14780 				    "hwrm crash dump mem failure rc: %d\n", rc);
14781 		}
14782 	}
14783 
14784 	if (bnxt_fw_pre_resv_vnics(bp))
14785 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14786 
14787 	bnxt_hwrm_func_qcfg(bp);
14788 	bnxt_hwrm_vnic_qcaps(bp);
14789 	bnxt_hwrm_port_led_qcaps(bp);
14790 	bnxt_ethtool_init(bp);
14791 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14792 		__bnxt_hwrm_ptp_qcfg(bp);
14793 	bnxt_dcb_init(bp);
14794 	bnxt_hwmon_init(bp);
14795 	return 0;
14796 }
14797 
14798 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14799 {
14800 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14801 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14802 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14803 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14804 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14805 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14806 		bp->rss_hash_delta = bp->rss_hash_cfg;
14807 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14808 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14809 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14810 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14811 	}
14812 }
14813 
14814 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14815 {
14816 	struct net_device *dev = bp->dev;
14817 
14818 	dev->hw_features &= ~NETIF_F_NTUPLE;
14819 	dev->features &= ~NETIF_F_NTUPLE;
14820 	bp->flags &= ~BNXT_FLAG_RFS;
14821 	if (bnxt_rfs_supported(bp)) {
14822 		dev->hw_features |= NETIF_F_NTUPLE;
14823 		if (bnxt_rfs_capable(bp, false)) {
14824 			bp->flags |= BNXT_FLAG_RFS;
14825 			dev->features |= NETIF_F_NTUPLE;
14826 		}
14827 	}
14828 }
14829 
14830 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14831 {
14832 	struct pci_dev *pdev = bp->pdev;
14833 
14834 	bnxt_set_dflt_rss_hash_type(bp);
14835 	bnxt_set_dflt_rfs(bp);
14836 
14837 	bnxt_get_wol_settings(bp);
14838 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14839 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14840 	else
14841 		device_set_wakeup_capable(&pdev->dev, false);
14842 
14843 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14844 	bnxt_hwrm_coal_params_qcaps(bp);
14845 }
14846 
14847 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14848 
14849 int bnxt_fw_init_one(struct bnxt *bp)
14850 {
14851 	int rc;
14852 
14853 	rc = bnxt_fw_init_one_p1(bp);
14854 	if (rc) {
14855 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14856 		return rc;
14857 	}
14858 	rc = bnxt_fw_init_one_p2(bp);
14859 	if (rc) {
14860 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14861 		return rc;
14862 	}
14863 	rc = bnxt_probe_phy(bp, false);
14864 	if (rc)
14865 		return rc;
14866 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14867 	if (rc)
14868 		return rc;
14869 
14870 	bnxt_fw_init_one_p3(bp);
14871 	return 0;
14872 }
14873 
14874 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14875 {
14876 	struct bnxt_fw_health *fw_health = bp->fw_health;
14877 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14878 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14879 	u32 reg_type, reg_off, delay_msecs;
14880 
14881 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14882 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14883 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14884 	switch (reg_type) {
14885 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14886 		pci_write_config_dword(bp->pdev, reg_off, val);
14887 		break;
14888 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14889 		writel(reg_off & BNXT_GRC_BASE_MASK,
14890 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14891 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14892 		fallthrough;
14893 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14894 		writel(val, bp->bar0 + reg_off);
14895 		break;
14896 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14897 		writel(val, bp->bar1 + reg_off);
14898 		break;
14899 	}
14900 	if (delay_msecs) {
14901 		pci_read_config_dword(bp->pdev, 0, &val);
14902 		msleep(delay_msecs);
14903 	}
14904 }
14905 
14906 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14907 {
14908 	struct hwrm_func_qcfg_output *resp;
14909 	struct hwrm_func_qcfg_input *req;
14910 	bool result = true; /* firmware will enforce if unknown */
14911 
14912 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14913 		return result;
14914 
14915 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14916 		return result;
14917 
14918 	req->fid = cpu_to_le16(0xffff);
14919 	resp = hwrm_req_hold(bp, req);
14920 	if (!hwrm_req_send(bp, req))
14921 		result = !!(le16_to_cpu(resp->flags) &
14922 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14923 	hwrm_req_drop(bp, req);
14924 	return result;
14925 }
14926 
14927 static void bnxt_reset_all(struct bnxt *bp)
14928 {
14929 	struct bnxt_fw_health *fw_health = bp->fw_health;
14930 	int i, rc;
14931 
14932 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14933 		bnxt_fw_reset_via_optee(bp);
14934 		bp->fw_reset_timestamp = jiffies;
14935 		return;
14936 	}
14937 
14938 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14939 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14940 			bnxt_fw_reset_writel(bp, i);
14941 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14942 		struct hwrm_fw_reset_input *req;
14943 
14944 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14945 		if (!rc) {
14946 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14947 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14948 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14949 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14950 			rc = hwrm_req_send(bp, req);
14951 		}
14952 		if (rc != -ENODEV)
14953 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14954 	}
14955 	bp->fw_reset_timestamp = jiffies;
14956 }
14957 
14958 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14959 {
14960 	return time_after(jiffies, bp->fw_reset_timestamp +
14961 			  (bp->fw_reset_max_dsecs * HZ / 10));
14962 }
14963 
14964 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14965 {
14966 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14967 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14968 		bnxt_dl_health_fw_status_update(bp, false);
14969 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
14970 	netif_close(bp->dev);
14971 }
14972 
14973 static void bnxt_fw_reset_task(struct work_struct *work)
14974 {
14975 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14976 	int rc = 0;
14977 
14978 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14979 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14980 		return;
14981 	}
14982 
14983 	switch (bp->fw_reset_state) {
14984 	case BNXT_FW_RESET_STATE_POLL_VF: {
14985 		int n = bnxt_get_registered_vfs(bp);
14986 		int tmo;
14987 
14988 		if (n < 0) {
14989 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14990 				   n, jiffies_to_msecs(jiffies -
14991 				   bp->fw_reset_timestamp));
14992 			goto fw_reset_abort;
14993 		} else if (n > 0) {
14994 			if (bnxt_fw_reset_timeout(bp)) {
14995 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14996 				bp->fw_reset_state = 0;
14997 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14998 					   n);
14999 				goto ulp_start;
15000 			}
15001 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15002 			return;
15003 		}
15004 		bp->fw_reset_timestamp = jiffies;
15005 		netdev_lock(bp->dev);
15006 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15007 			bnxt_fw_reset_abort(bp, rc);
15008 			netdev_unlock(bp->dev);
15009 			goto ulp_start;
15010 		}
15011 		bnxt_fw_reset_close(bp);
15012 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15013 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15014 			tmo = HZ / 10;
15015 		} else {
15016 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15017 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15018 		}
15019 		netdev_unlock(bp->dev);
15020 		bnxt_queue_fw_reset_work(bp, tmo);
15021 		return;
15022 	}
15023 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15024 		u32 val;
15025 
15026 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15027 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15028 		    !bnxt_fw_reset_timeout(bp)) {
15029 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15030 			return;
15031 		}
15032 
15033 		if (!bp->fw_health->primary) {
15034 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15035 
15036 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15037 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15038 			return;
15039 		}
15040 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15041 	}
15042 		fallthrough;
15043 	case BNXT_FW_RESET_STATE_RESET_FW:
15044 		bnxt_reset_all(bp);
15045 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15046 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15047 		return;
15048 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15049 		bnxt_inv_fw_health_reg(bp);
15050 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15051 		    !bp->fw_reset_min_dsecs) {
15052 			u16 val;
15053 
15054 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15055 			if (val == 0xffff) {
15056 				if (bnxt_fw_reset_timeout(bp)) {
15057 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15058 					rc = -ETIMEDOUT;
15059 					goto fw_reset_abort;
15060 				}
15061 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15062 				return;
15063 			}
15064 		}
15065 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15066 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15067 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15068 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15069 			bnxt_dl_remote_reload(bp);
15070 		if (pci_enable_device(bp->pdev)) {
15071 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15072 			rc = -ENODEV;
15073 			goto fw_reset_abort;
15074 		}
15075 		pci_set_master(bp->pdev);
15076 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15077 		fallthrough;
15078 	case BNXT_FW_RESET_STATE_POLL_FW:
15079 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15080 		rc = bnxt_hwrm_poll(bp);
15081 		if (rc) {
15082 			if (bnxt_fw_reset_timeout(bp)) {
15083 				netdev_err(bp->dev, "Firmware reset aborted\n");
15084 				goto fw_reset_abort_status;
15085 			}
15086 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15087 			return;
15088 		}
15089 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15090 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15091 		fallthrough;
15092 	case BNXT_FW_RESET_STATE_OPENING:
15093 		while (!netdev_trylock(bp->dev)) {
15094 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15095 			return;
15096 		}
15097 		rc = bnxt_open(bp->dev);
15098 		if (rc) {
15099 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15100 			bnxt_fw_reset_abort(bp, rc);
15101 			netdev_unlock(bp->dev);
15102 			goto ulp_start;
15103 		}
15104 
15105 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15106 		    bp->fw_health->enabled) {
15107 			bp->fw_health->last_fw_reset_cnt =
15108 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15109 		}
15110 		bp->fw_reset_state = 0;
15111 		/* Make sure fw_reset_state is 0 before clearing the flag */
15112 		smp_mb__before_atomic();
15113 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15114 		bnxt_ptp_reapply_pps(bp);
15115 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15116 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15117 			bnxt_dl_health_fw_recovery_done(bp);
15118 			bnxt_dl_health_fw_status_update(bp, true);
15119 		}
15120 		netdev_unlock(bp->dev);
15121 		bnxt_ulp_start(bp, 0);
15122 		bnxt_reenable_sriov(bp);
15123 		netdev_lock(bp->dev);
15124 		bnxt_vf_reps_alloc(bp);
15125 		bnxt_vf_reps_open(bp);
15126 		netdev_unlock(bp->dev);
15127 		break;
15128 	}
15129 	return;
15130 
15131 fw_reset_abort_status:
15132 	if (bp->fw_health->status_reliable ||
15133 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15134 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15135 
15136 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15137 	}
15138 fw_reset_abort:
15139 	netdev_lock(bp->dev);
15140 	bnxt_fw_reset_abort(bp, rc);
15141 	netdev_unlock(bp->dev);
15142 ulp_start:
15143 	bnxt_ulp_start(bp, rc);
15144 }
15145 
15146 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15147 {
15148 	int rc;
15149 	struct bnxt *bp = netdev_priv(dev);
15150 
15151 	SET_NETDEV_DEV(dev, &pdev->dev);
15152 
15153 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15154 	rc = pci_enable_device(pdev);
15155 	if (rc) {
15156 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15157 		goto init_err;
15158 	}
15159 
15160 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15161 		dev_err(&pdev->dev,
15162 			"Cannot find PCI device base address, aborting\n");
15163 		rc = -ENODEV;
15164 		goto init_err_disable;
15165 	}
15166 
15167 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15168 	if (rc) {
15169 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15170 		goto init_err_disable;
15171 	}
15172 
15173 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15174 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15175 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15176 		rc = -EIO;
15177 		goto init_err_release;
15178 	}
15179 
15180 	pci_set_master(pdev);
15181 
15182 	bp->dev = dev;
15183 	bp->pdev = pdev;
15184 
15185 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15186 	 * determines the BAR size.
15187 	 */
15188 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15189 	if (!bp->bar0) {
15190 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15191 		rc = -ENOMEM;
15192 		goto init_err_release;
15193 	}
15194 
15195 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15196 	if (!bp->bar2) {
15197 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15198 		rc = -ENOMEM;
15199 		goto init_err_release;
15200 	}
15201 
15202 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15203 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15204 
15205 	spin_lock_init(&bp->ntp_fltr_lock);
15206 #if BITS_PER_LONG == 32
15207 	spin_lock_init(&bp->db_lock);
15208 #endif
15209 
15210 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15211 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15212 
15213 	timer_setup(&bp->timer, bnxt_timer, 0);
15214 	bp->current_interval = BNXT_TIMER_INTERVAL;
15215 
15216 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15217 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15218 
15219 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15220 	return 0;
15221 
15222 init_err_release:
15223 	bnxt_unmap_bars(bp, pdev);
15224 	pci_release_regions(pdev);
15225 
15226 init_err_disable:
15227 	pci_disable_device(pdev);
15228 
15229 init_err:
15230 	return rc;
15231 }
15232 
15233 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15234 {
15235 	struct sockaddr *addr = p;
15236 	struct bnxt *bp = netdev_priv(dev);
15237 	int rc = 0;
15238 
15239 	netdev_assert_locked(dev);
15240 
15241 	if (!is_valid_ether_addr(addr->sa_data))
15242 		return -EADDRNOTAVAIL;
15243 
15244 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15245 		return 0;
15246 
15247 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15248 	if (rc)
15249 		return rc;
15250 
15251 	eth_hw_addr_set(dev, addr->sa_data);
15252 	bnxt_clear_usr_fltrs(bp, true);
15253 	if (netif_running(dev)) {
15254 		bnxt_close_nic(bp, false, false);
15255 		rc = bnxt_open_nic(bp, false, false);
15256 	}
15257 
15258 	return rc;
15259 }
15260 
15261 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15262 {
15263 	struct bnxt *bp = netdev_priv(dev);
15264 
15265 	netdev_assert_locked(dev);
15266 
15267 	if (netif_running(dev))
15268 		bnxt_close_nic(bp, true, false);
15269 
15270 	WRITE_ONCE(dev->mtu, new_mtu);
15271 
15272 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15273 	 * program is attached.  We need to set the AGG rings settings and
15274 	 * rx_skb_func accordingly.
15275 	 */
15276 	if (READ_ONCE(bp->xdp_prog))
15277 		bnxt_set_rx_skb_mode(bp, true);
15278 
15279 	bnxt_set_ring_params(bp);
15280 
15281 	if (netif_running(dev))
15282 		return bnxt_open_nic(bp, true, false);
15283 
15284 	return 0;
15285 }
15286 
15287 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15288 {
15289 	struct bnxt *bp = netdev_priv(dev);
15290 	bool sh = false;
15291 	int rc, tx_cp;
15292 
15293 	if (tc > bp->max_tc) {
15294 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15295 			   tc, bp->max_tc);
15296 		return -EINVAL;
15297 	}
15298 
15299 	if (bp->num_tc == tc)
15300 		return 0;
15301 
15302 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15303 		sh = true;
15304 
15305 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15306 			      sh, tc, bp->tx_nr_rings_xdp);
15307 	if (rc)
15308 		return rc;
15309 
15310 	/* Needs to close the device and do hw resource re-allocations */
15311 	if (netif_running(bp->dev))
15312 		bnxt_close_nic(bp, true, false);
15313 
15314 	if (tc) {
15315 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15316 		netdev_set_num_tc(dev, tc);
15317 		bp->num_tc = tc;
15318 	} else {
15319 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15320 		netdev_reset_tc(dev);
15321 		bp->num_tc = 0;
15322 	}
15323 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15324 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15325 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15326 			       tx_cp + bp->rx_nr_rings;
15327 
15328 	if (netif_running(bp->dev))
15329 		return bnxt_open_nic(bp, true, false);
15330 
15331 	return 0;
15332 }
15333 
15334 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15335 				  void *cb_priv)
15336 {
15337 	struct bnxt *bp = cb_priv;
15338 
15339 	if (!bnxt_tc_flower_enabled(bp) ||
15340 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15341 		return -EOPNOTSUPP;
15342 
15343 	switch (type) {
15344 	case TC_SETUP_CLSFLOWER:
15345 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15346 	default:
15347 		return -EOPNOTSUPP;
15348 	}
15349 }
15350 
15351 LIST_HEAD(bnxt_block_cb_list);
15352 
15353 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15354 			 void *type_data)
15355 {
15356 	struct bnxt *bp = netdev_priv(dev);
15357 
15358 	switch (type) {
15359 	case TC_SETUP_BLOCK:
15360 		return flow_block_cb_setup_simple(type_data,
15361 						  &bnxt_block_cb_list,
15362 						  bnxt_setup_tc_block_cb,
15363 						  bp, bp, true);
15364 	case TC_SETUP_QDISC_MQPRIO: {
15365 		struct tc_mqprio_qopt *mqprio = type_data;
15366 
15367 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15368 
15369 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15370 	}
15371 	default:
15372 		return -EOPNOTSUPP;
15373 	}
15374 }
15375 
15376 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15377 			    const struct sk_buff *skb)
15378 {
15379 	struct bnxt_vnic_info *vnic;
15380 
15381 	if (skb)
15382 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15383 
15384 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15385 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15386 }
15387 
15388 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15389 			   u32 idx)
15390 {
15391 	struct hlist_head *head;
15392 	int bit_id;
15393 
15394 	spin_lock_bh(&bp->ntp_fltr_lock);
15395 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15396 	if (bit_id < 0) {
15397 		spin_unlock_bh(&bp->ntp_fltr_lock);
15398 		return -ENOMEM;
15399 	}
15400 
15401 	fltr->base.sw_id = (u16)bit_id;
15402 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15403 	fltr->base.flags |= BNXT_ACT_RING_DST;
15404 	head = &bp->ntp_fltr_hash_tbl[idx];
15405 	hlist_add_head_rcu(&fltr->base.hash, head);
15406 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15407 	bnxt_insert_usr_fltr(bp, &fltr->base);
15408 	bp->ntp_fltr_count++;
15409 	spin_unlock_bh(&bp->ntp_fltr_lock);
15410 	return 0;
15411 }
15412 
15413 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15414 			    struct bnxt_ntuple_filter *f2)
15415 {
15416 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15417 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15418 	struct flow_keys *keys1 = &f1->fkeys;
15419 	struct flow_keys *keys2 = &f2->fkeys;
15420 
15421 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15422 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15423 		return false;
15424 
15425 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15426 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15427 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15428 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15429 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15430 			return false;
15431 	} else {
15432 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15433 				     &keys2->addrs.v6addrs.src) ||
15434 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15435 				     &masks2->addrs.v6addrs.src) ||
15436 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15437 				     &keys2->addrs.v6addrs.dst) ||
15438 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15439 				     &masks2->addrs.v6addrs.dst))
15440 			return false;
15441 	}
15442 
15443 	return keys1->ports.src == keys2->ports.src &&
15444 	       masks1->ports.src == masks2->ports.src &&
15445 	       keys1->ports.dst == keys2->ports.dst &&
15446 	       masks1->ports.dst == masks2->ports.dst &&
15447 	       keys1->control.flags == keys2->control.flags &&
15448 	       f1->l2_fltr == f2->l2_fltr;
15449 }
15450 
15451 struct bnxt_ntuple_filter *
15452 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15453 				struct bnxt_ntuple_filter *fltr, u32 idx)
15454 {
15455 	struct bnxt_ntuple_filter *f;
15456 	struct hlist_head *head;
15457 
15458 	head = &bp->ntp_fltr_hash_tbl[idx];
15459 	hlist_for_each_entry_rcu(f, head, base.hash) {
15460 		if (bnxt_fltr_match(f, fltr))
15461 			return f;
15462 	}
15463 	return NULL;
15464 }
15465 
15466 #ifdef CONFIG_RFS_ACCEL
15467 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15468 			      u16 rxq_index, u32 flow_id)
15469 {
15470 	struct bnxt *bp = netdev_priv(dev);
15471 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15472 	struct flow_keys *fkeys;
15473 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15474 	struct bnxt_l2_filter *l2_fltr;
15475 	int rc = 0, idx;
15476 	u32 flags;
15477 
15478 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15479 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15480 		atomic_inc(&l2_fltr->refcnt);
15481 	} else {
15482 		struct bnxt_l2_key key;
15483 
15484 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15485 		key.vlan = 0;
15486 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15487 		if (!l2_fltr)
15488 			return -EINVAL;
15489 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15490 			bnxt_del_l2_filter(bp, l2_fltr);
15491 			return -EINVAL;
15492 		}
15493 	}
15494 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15495 	if (!new_fltr) {
15496 		bnxt_del_l2_filter(bp, l2_fltr);
15497 		return -ENOMEM;
15498 	}
15499 
15500 	fkeys = &new_fltr->fkeys;
15501 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15502 		rc = -EPROTONOSUPPORT;
15503 		goto err_free;
15504 	}
15505 
15506 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15507 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15508 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15509 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15510 		rc = -EPROTONOSUPPORT;
15511 		goto err_free;
15512 	}
15513 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15514 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15515 		if (bp->hwrm_spec_code < 0x10601) {
15516 			rc = -EPROTONOSUPPORT;
15517 			goto err_free;
15518 		}
15519 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15520 	}
15521 	flags = fkeys->control.flags;
15522 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15523 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15524 		rc = -EPROTONOSUPPORT;
15525 		goto err_free;
15526 	}
15527 	new_fltr->l2_fltr = l2_fltr;
15528 
15529 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15530 	rcu_read_lock();
15531 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15532 	if (fltr) {
15533 		rc = fltr->base.sw_id;
15534 		rcu_read_unlock();
15535 		goto err_free;
15536 	}
15537 	rcu_read_unlock();
15538 
15539 	new_fltr->flow_id = flow_id;
15540 	new_fltr->base.rxq = rxq_index;
15541 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15542 	if (!rc) {
15543 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15544 		return new_fltr->base.sw_id;
15545 	}
15546 
15547 err_free:
15548 	bnxt_del_l2_filter(bp, l2_fltr);
15549 	kfree(new_fltr);
15550 	return rc;
15551 }
15552 #endif
15553 
15554 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15555 {
15556 	spin_lock_bh(&bp->ntp_fltr_lock);
15557 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15558 		spin_unlock_bh(&bp->ntp_fltr_lock);
15559 		return;
15560 	}
15561 	hlist_del_rcu(&fltr->base.hash);
15562 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15563 	bp->ntp_fltr_count--;
15564 	spin_unlock_bh(&bp->ntp_fltr_lock);
15565 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15566 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15567 	kfree_rcu(fltr, base.rcu);
15568 }
15569 
15570 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15571 {
15572 #ifdef CONFIG_RFS_ACCEL
15573 	int i;
15574 
15575 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15576 		struct hlist_head *head;
15577 		struct hlist_node *tmp;
15578 		struct bnxt_ntuple_filter *fltr;
15579 		int rc;
15580 
15581 		head = &bp->ntp_fltr_hash_tbl[i];
15582 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15583 			bool del = false;
15584 
15585 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15586 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15587 					continue;
15588 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15589 							fltr->flow_id,
15590 							fltr->base.sw_id)) {
15591 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15592 									 fltr);
15593 					del = true;
15594 				}
15595 			} else {
15596 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15597 								       fltr);
15598 				if (rc)
15599 					del = true;
15600 				else
15601 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15602 			}
15603 
15604 			if (del)
15605 				bnxt_del_ntp_filter(bp, fltr);
15606 		}
15607 	}
15608 #endif
15609 }
15610 
15611 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15612 				    unsigned int entry, struct udp_tunnel_info *ti)
15613 {
15614 	struct bnxt *bp = netdev_priv(netdev);
15615 	unsigned int cmd;
15616 
15617 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15618 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15619 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15620 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15621 	else
15622 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15623 
15624 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15625 }
15626 
15627 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15628 				      unsigned int entry, struct udp_tunnel_info *ti)
15629 {
15630 	struct bnxt *bp = netdev_priv(netdev);
15631 	unsigned int cmd;
15632 
15633 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15634 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15635 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15636 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15637 	else
15638 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15639 
15640 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15641 }
15642 
15643 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15644 	.set_port	= bnxt_udp_tunnel_set_port,
15645 	.unset_port	= bnxt_udp_tunnel_unset_port,
15646 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15647 	.tables		= {
15648 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15649 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15650 	},
15651 }, bnxt_udp_tunnels_p7 = {
15652 	.set_port	= bnxt_udp_tunnel_set_port,
15653 	.unset_port	= bnxt_udp_tunnel_unset_port,
15654 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15655 	.tables		= {
15656 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15657 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15658 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15659 	},
15660 };
15661 
15662 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15663 			       struct net_device *dev, u32 filter_mask,
15664 			       int nlflags)
15665 {
15666 	struct bnxt *bp = netdev_priv(dev);
15667 
15668 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15669 				       nlflags, filter_mask, NULL);
15670 }
15671 
15672 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15673 			       u16 flags, struct netlink_ext_ack *extack)
15674 {
15675 	struct bnxt *bp = netdev_priv(dev);
15676 	struct nlattr *attr, *br_spec;
15677 	int rem, rc = 0;
15678 
15679 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15680 		return -EOPNOTSUPP;
15681 
15682 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15683 	if (!br_spec)
15684 		return -EINVAL;
15685 
15686 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15687 		u16 mode;
15688 
15689 		mode = nla_get_u16(attr);
15690 		if (mode == bp->br_mode)
15691 			break;
15692 
15693 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15694 		if (!rc)
15695 			bp->br_mode = mode;
15696 		break;
15697 	}
15698 	return rc;
15699 }
15700 
15701 int bnxt_get_port_parent_id(struct net_device *dev,
15702 			    struct netdev_phys_item_id *ppid)
15703 {
15704 	struct bnxt *bp = netdev_priv(dev);
15705 
15706 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15707 		return -EOPNOTSUPP;
15708 
15709 	/* The PF and it's VF-reps only support the switchdev framework */
15710 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15711 		return -EOPNOTSUPP;
15712 
15713 	ppid->id_len = sizeof(bp->dsn);
15714 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15715 
15716 	return 0;
15717 }
15718 
15719 static const struct net_device_ops bnxt_netdev_ops = {
15720 	.ndo_open		= bnxt_open,
15721 	.ndo_start_xmit		= bnxt_start_xmit,
15722 	.ndo_stop		= bnxt_close,
15723 	.ndo_get_stats64	= bnxt_get_stats64,
15724 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15725 	.ndo_eth_ioctl		= bnxt_ioctl,
15726 	.ndo_validate_addr	= eth_validate_addr,
15727 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15728 	.ndo_change_mtu		= bnxt_change_mtu,
15729 	.ndo_fix_features	= bnxt_fix_features,
15730 	.ndo_set_features	= bnxt_set_features,
15731 	.ndo_features_check	= bnxt_features_check,
15732 	.ndo_tx_timeout		= bnxt_tx_timeout,
15733 #ifdef CONFIG_BNXT_SRIOV
15734 	.ndo_get_vf_config	= bnxt_get_vf_config,
15735 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15736 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15737 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15738 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15739 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15740 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15741 #endif
15742 	.ndo_setup_tc           = bnxt_setup_tc,
15743 #ifdef CONFIG_RFS_ACCEL
15744 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15745 #endif
15746 	.ndo_bpf		= bnxt_xdp,
15747 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15748 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15749 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15750 };
15751 
15752 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15753 				    struct netdev_queue_stats_rx *stats)
15754 {
15755 	struct bnxt *bp = netdev_priv(dev);
15756 	struct bnxt_cp_ring_info *cpr;
15757 	u64 *sw;
15758 
15759 	if (!bp->bnapi)
15760 		return;
15761 
15762 	cpr = &bp->bnapi[i]->cp_ring;
15763 	sw = cpr->stats.sw_stats;
15764 
15765 	stats->packets = 0;
15766 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15767 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15768 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15769 
15770 	stats->bytes = 0;
15771 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15772 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15773 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15774 
15775 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15776 }
15777 
15778 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15779 				    struct netdev_queue_stats_tx *stats)
15780 {
15781 	struct bnxt *bp = netdev_priv(dev);
15782 	struct bnxt_napi *bnapi;
15783 	u64 *sw;
15784 
15785 	if (!bp->tx_ring)
15786 		return;
15787 
15788 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15789 	sw = bnapi->cp_ring.stats.sw_stats;
15790 
15791 	stats->packets = 0;
15792 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15793 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15794 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15795 
15796 	stats->bytes = 0;
15797 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15798 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15799 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15800 }
15801 
15802 static void bnxt_get_base_stats(struct net_device *dev,
15803 				struct netdev_queue_stats_rx *rx,
15804 				struct netdev_queue_stats_tx *tx)
15805 {
15806 	struct bnxt *bp = netdev_priv(dev);
15807 
15808 	rx->packets = bp->net_stats_prev.rx_packets;
15809 	rx->bytes = bp->net_stats_prev.rx_bytes;
15810 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15811 
15812 	tx->packets = bp->net_stats_prev.tx_packets;
15813 	tx->bytes = bp->net_stats_prev.tx_bytes;
15814 }
15815 
15816 static const struct netdev_stat_ops bnxt_stat_ops = {
15817 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15818 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15819 	.get_base_stats		= bnxt_get_base_stats,
15820 };
15821 
15822 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15823 {
15824 	struct bnxt_rx_ring_info *rxr, *clone;
15825 	struct bnxt *bp = netdev_priv(dev);
15826 	struct bnxt_ring_struct *ring;
15827 	int rc;
15828 
15829 	if (!bp->rx_ring)
15830 		return -ENETDOWN;
15831 
15832 	rxr = &bp->rx_ring[idx];
15833 	clone = qmem;
15834 	memcpy(clone, rxr, sizeof(*rxr));
15835 	bnxt_init_rx_ring_struct(bp, clone);
15836 	bnxt_reset_rx_ring_struct(bp, clone);
15837 
15838 	clone->rx_prod = 0;
15839 	clone->rx_agg_prod = 0;
15840 	clone->rx_sw_agg_prod = 0;
15841 	clone->rx_next_cons = 0;
15842 	clone->need_head_pool = false;
15843 
15844 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15845 	if (rc)
15846 		return rc;
15847 
15848 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15849 	if (rc < 0)
15850 		goto err_page_pool_destroy;
15851 
15852 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15853 					MEM_TYPE_PAGE_POOL,
15854 					clone->page_pool);
15855 	if (rc)
15856 		goto err_rxq_info_unreg;
15857 
15858 	ring = &clone->rx_ring_struct;
15859 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15860 	if (rc)
15861 		goto err_free_rx_ring;
15862 
15863 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15864 		ring = &clone->rx_agg_ring_struct;
15865 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15866 		if (rc)
15867 			goto err_free_rx_agg_ring;
15868 
15869 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15870 		if (rc)
15871 			goto err_free_rx_agg_ring;
15872 	}
15873 
15874 	if (bp->flags & BNXT_FLAG_TPA) {
15875 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15876 		if (rc)
15877 			goto err_free_tpa_info;
15878 	}
15879 
15880 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15881 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15882 
15883 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15884 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15885 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
15886 	if (bp->flags & BNXT_FLAG_TPA)
15887 		bnxt_alloc_one_tpa_info_data(bp, clone);
15888 
15889 	return 0;
15890 
15891 err_free_tpa_info:
15892 	bnxt_free_one_tpa_info(bp, clone);
15893 err_free_rx_agg_ring:
15894 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15895 err_free_rx_ring:
15896 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15897 err_rxq_info_unreg:
15898 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15899 err_page_pool_destroy:
15900 	page_pool_destroy(clone->page_pool);
15901 	if (bnxt_separate_head_pool(clone))
15902 		page_pool_destroy(clone->head_pool);
15903 	clone->page_pool = NULL;
15904 	clone->head_pool = NULL;
15905 	return rc;
15906 }
15907 
15908 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15909 {
15910 	struct bnxt_rx_ring_info *rxr = qmem;
15911 	struct bnxt *bp = netdev_priv(dev);
15912 	struct bnxt_ring_struct *ring;
15913 
15914 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15915 	bnxt_free_one_tpa_info(bp, rxr);
15916 
15917 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15918 
15919 	page_pool_destroy(rxr->page_pool);
15920 	if (bnxt_separate_head_pool(rxr))
15921 		page_pool_destroy(rxr->head_pool);
15922 	rxr->page_pool = NULL;
15923 	rxr->head_pool = NULL;
15924 
15925 	ring = &rxr->rx_ring_struct;
15926 	bnxt_free_ring(bp, &ring->ring_mem);
15927 
15928 	ring = &rxr->rx_agg_ring_struct;
15929 	bnxt_free_ring(bp, &ring->ring_mem);
15930 
15931 	kfree(rxr->rx_agg_bmap);
15932 	rxr->rx_agg_bmap = NULL;
15933 }
15934 
15935 static void bnxt_copy_rx_ring(struct bnxt *bp,
15936 			      struct bnxt_rx_ring_info *dst,
15937 			      struct bnxt_rx_ring_info *src)
15938 {
15939 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15940 	struct bnxt_ring_struct *dst_ring, *src_ring;
15941 	int i;
15942 
15943 	dst_ring = &dst->rx_ring_struct;
15944 	dst_rmem = &dst_ring->ring_mem;
15945 	src_ring = &src->rx_ring_struct;
15946 	src_rmem = &src_ring->ring_mem;
15947 
15948 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15949 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15950 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15951 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15952 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15953 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15954 
15955 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15956 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15957 	*dst_rmem->vmem = *src_rmem->vmem;
15958 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15959 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15960 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15961 	}
15962 
15963 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15964 		return;
15965 
15966 	dst_ring = &dst->rx_agg_ring_struct;
15967 	dst_rmem = &dst_ring->ring_mem;
15968 	src_ring = &src->rx_agg_ring_struct;
15969 	src_rmem = &src_ring->ring_mem;
15970 
15971 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15972 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15973 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15974 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15975 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15976 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15977 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15978 
15979 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15980 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15981 	*dst_rmem->vmem = *src_rmem->vmem;
15982 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15983 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15984 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15985 	}
15986 
15987 	dst->rx_agg_bmap = src->rx_agg_bmap;
15988 }
15989 
15990 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15991 {
15992 	struct bnxt *bp = netdev_priv(dev);
15993 	struct bnxt_rx_ring_info *rxr, *clone;
15994 	struct bnxt_cp_ring_info *cpr;
15995 	struct bnxt_vnic_info *vnic;
15996 	struct bnxt_napi *bnapi;
15997 	int i, rc;
15998 	u16 mru;
15999 
16000 	rxr = &bp->rx_ring[idx];
16001 	clone = qmem;
16002 
16003 	rxr->rx_prod = clone->rx_prod;
16004 	rxr->rx_agg_prod = clone->rx_agg_prod;
16005 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16006 	rxr->rx_next_cons = clone->rx_next_cons;
16007 	rxr->rx_tpa = clone->rx_tpa;
16008 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16009 	rxr->page_pool = clone->page_pool;
16010 	rxr->head_pool = clone->head_pool;
16011 	rxr->xdp_rxq = clone->xdp_rxq;
16012 	rxr->need_head_pool = clone->need_head_pool;
16013 
16014 	bnxt_copy_rx_ring(bp, rxr, clone);
16015 
16016 	bnapi = rxr->bnapi;
16017 	cpr = &bnapi->cp_ring;
16018 
16019 	/* All rings have been reserved and previously allocated.
16020 	 * Reallocating with the same parameters should never fail.
16021 	 */
16022 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16023 	if (rc)
16024 		goto err_reset;
16025 
16026 	if (bp->tph_mode) {
16027 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16028 		if (rc)
16029 			goto err_reset;
16030 	}
16031 
16032 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16033 	if (rc)
16034 		goto err_reset;
16035 
16036 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16037 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16038 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16039 
16040 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16041 		rc = bnxt_tx_queue_start(bp, idx);
16042 		if (rc)
16043 			goto err_reset;
16044 	}
16045 
16046 	bnxt_enable_rx_page_pool(rxr);
16047 	napi_enable_locked(&bnapi->napi);
16048 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16049 
16050 	mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
16051 	for (i = 0; i < bp->nr_vnics; i++) {
16052 		vnic = &bp->vnic_info[i];
16053 
16054 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16055 		if (rc)
16056 			return rc;
16057 	}
16058 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16059 
16060 err_reset:
16061 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16062 		   rc);
16063 	napi_enable_locked(&bnapi->napi);
16064 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16065 	bnxt_reset_task(bp, true);
16066 	return rc;
16067 }
16068 
16069 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16070 {
16071 	struct bnxt *bp = netdev_priv(dev);
16072 	struct bnxt_rx_ring_info *rxr;
16073 	struct bnxt_cp_ring_info *cpr;
16074 	struct bnxt_vnic_info *vnic;
16075 	struct bnxt_napi *bnapi;
16076 	int i;
16077 
16078 	for (i = 0; i < bp->nr_vnics; i++) {
16079 		vnic = &bp->vnic_info[i];
16080 
16081 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16082 	}
16083 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16084 	/* Make sure NAPI sees that the VNIC is disabled */
16085 	synchronize_net();
16086 	rxr = &bp->rx_ring[idx];
16087 	bnapi = rxr->bnapi;
16088 	cpr = &bnapi->cp_ring;
16089 	cancel_work_sync(&cpr->dim.work);
16090 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16091 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16092 	page_pool_disable_direct_recycling(rxr->page_pool);
16093 	if (bnxt_separate_head_pool(rxr))
16094 		page_pool_disable_direct_recycling(rxr->head_pool);
16095 
16096 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16097 		bnxt_tx_queue_stop(bp, idx);
16098 
16099 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16100 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16101 	 * after seeing the completion.
16102 	 */
16103 	napi_disable_locked(&bnapi->napi);
16104 
16105 	if (bp->tph_mode) {
16106 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16107 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16108 	}
16109 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16110 
16111 	memcpy(qmem, rxr, sizeof(*rxr));
16112 	bnxt_init_rx_ring_struct(bp, qmem);
16113 
16114 	return 0;
16115 }
16116 
16117 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16118 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16119 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16120 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16121 	.ndo_queue_start	= bnxt_queue_start,
16122 	.ndo_queue_stop		= bnxt_queue_stop,
16123 };
16124 
16125 static void bnxt_remove_one(struct pci_dev *pdev)
16126 {
16127 	struct net_device *dev = pci_get_drvdata(pdev);
16128 	struct bnxt *bp = netdev_priv(dev);
16129 
16130 	if (BNXT_PF(bp))
16131 		bnxt_sriov_disable(bp);
16132 
16133 	bnxt_rdma_aux_device_del(bp);
16134 
16135 	unregister_netdev(dev);
16136 	bnxt_ptp_clear(bp);
16137 
16138 	bnxt_rdma_aux_device_uninit(bp);
16139 
16140 	bnxt_free_l2_filters(bp, true);
16141 	bnxt_free_ntp_fltrs(bp, true);
16142 	WARN_ON(bp->num_rss_ctx);
16143 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16144 	/* Flush any pending tasks */
16145 	cancel_work_sync(&bp->sp_task);
16146 	cancel_delayed_work_sync(&bp->fw_reset_task);
16147 	bp->sp_event = 0;
16148 
16149 	bnxt_dl_fw_reporters_destroy(bp);
16150 	bnxt_dl_unregister(bp);
16151 	bnxt_shutdown_tc(bp);
16152 
16153 	bnxt_clear_int_mode(bp);
16154 	bnxt_hwrm_func_drv_unrgtr(bp);
16155 	bnxt_free_hwrm_resources(bp);
16156 	bnxt_hwmon_uninit(bp);
16157 	bnxt_ethtool_free(bp);
16158 	bnxt_dcb_free(bp);
16159 	kfree(bp->ptp_cfg);
16160 	bp->ptp_cfg = NULL;
16161 	kfree(bp->fw_health);
16162 	bp->fw_health = NULL;
16163 	bnxt_cleanup_pci(bp);
16164 	bnxt_free_ctx_mem(bp, true);
16165 	bnxt_free_crash_dump_mem(bp);
16166 	kfree(bp->rss_indir_tbl);
16167 	bp->rss_indir_tbl = NULL;
16168 	bnxt_free_port_stats(bp);
16169 	free_netdev(dev);
16170 }
16171 
16172 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16173 {
16174 	int rc = 0;
16175 	struct bnxt_link_info *link_info = &bp->link_info;
16176 
16177 	bp->phy_flags = 0;
16178 	rc = bnxt_hwrm_phy_qcaps(bp);
16179 	if (rc) {
16180 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16181 			   rc);
16182 		return rc;
16183 	}
16184 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16185 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16186 	else
16187 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16188 
16189 	bp->mac_flags = 0;
16190 	bnxt_hwrm_mac_qcaps(bp);
16191 
16192 	if (!fw_dflt)
16193 		return 0;
16194 
16195 	mutex_lock(&bp->link_lock);
16196 	rc = bnxt_update_link(bp, false);
16197 	if (rc) {
16198 		mutex_unlock(&bp->link_lock);
16199 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16200 			   rc);
16201 		return rc;
16202 	}
16203 
16204 	/* Older firmware does not have supported_auto_speeds, so assume
16205 	 * that all supported speeds can be autonegotiated.
16206 	 */
16207 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16208 		link_info->support_auto_speeds = link_info->support_speeds;
16209 
16210 	bnxt_init_ethtool_link_settings(bp);
16211 	mutex_unlock(&bp->link_lock);
16212 	return 0;
16213 }
16214 
16215 static int bnxt_get_max_irq(struct pci_dev *pdev)
16216 {
16217 	u16 ctrl;
16218 
16219 	if (!pdev->msix_cap)
16220 		return 1;
16221 
16222 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16223 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16224 }
16225 
16226 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16227 				int *max_cp)
16228 {
16229 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16230 	int max_ring_grps = 0, max_irq;
16231 
16232 	*max_tx = hw_resc->max_tx_rings;
16233 	*max_rx = hw_resc->max_rx_rings;
16234 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16235 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16236 			bnxt_get_ulp_msix_num_in_use(bp),
16237 			hw_resc->max_stat_ctxs -
16238 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16239 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16240 		*max_cp = min_t(int, *max_cp, max_irq);
16241 	max_ring_grps = hw_resc->max_hw_ring_grps;
16242 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16243 		*max_cp -= 1;
16244 		*max_rx -= 2;
16245 	}
16246 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16247 		*max_rx >>= 1;
16248 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16249 		int rc;
16250 
16251 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16252 		if (rc) {
16253 			*max_rx = 0;
16254 			*max_tx = 0;
16255 		}
16256 		/* On P5 chips, max_cp output param should be available NQs */
16257 		*max_cp = max_irq;
16258 	}
16259 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16260 }
16261 
16262 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16263 {
16264 	int rx, tx, cp;
16265 
16266 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16267 	*max_rx = rx;
16268 	*max_tx = tx;
16269 	if (!rx || !tx || !cp)
16270 		return -ENOMEM;
16271 
16272 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16273 }
16274 
16275 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16276 			       bool shared)
16277 {
16278 	int rc;
16279 
16280 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16281 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16282 		/* Not enough rings, try disabling agg rings. */
16283 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16284 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16285 		if (rc) {
16286 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16287 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16288 			return rc;
16289 		}
16290 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16291 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16292 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16293 		bnxt_set_ring_params(bp);
16294 	}
16295 
16296 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16297 		int max_cp, max_stat, max_irq;
16298 
16299 		/* Reserve minimum resources for RoCE */
16300 		max_cp = bnxt_get_max_func_cp_rings(bp);
16301 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16302 		max_irq = bnxt_get_max_func_irqs(bp);
16303 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16304 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16305 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16306 			return 0;
16307 
16308 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16309 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16310 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16311 		max_cp = min_t(int, max_cp, max_irq);
16312 		max_cp = min_t(int, max_cp, max_stat);
16313 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16314 		if (rc)
16315 			rc = 0;
16316 	}
16317 	return rc;
16318 }
16319 
16320 /* In initial default shared ring setting, each shared ring must have a
16321  * RX/TX ring pair.
16322  */
16323 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16324 {
16325 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16326 	bp->rx_nr_rings = bp->cp_nr_rings;
16327 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16328 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16329 }
16330 
16331 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16332 {
16333 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16334 	int avail_msix;
16335 
16336 	if (!bnxt_can_reserve_rings(bp))
16337 		return 0;
16338 
16339 	if (sh)
16340 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16341 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16342 	/* Reduce default rings on multi-port cards so that total default
16343 	 * rings do not exceed CPU count.
16344 	 */
16345 	if (bp->port_count > 1) {
16346 		int max_rings =
16347 			max_t(int, num_online_cpus() / bp->port_count, 1);
16348 
16349 		dflt_rings = min_t(int, dflt_rings, max_rings);
16350 	}
16351 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16352 	if (rc)
16353 		return rc;
16354 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16355 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16356 	if (sh)
16357 		bnxt_trim_dflt_sh_rings(bp);
16358 	else
16359 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16360 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16361 
16362 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16363 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16364 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16365 
16366 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16367 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16368 	}
16369 
16370 	rc = __bnxt_reserve_rings(bp);
16371 	if (rc && rc != -ENODEV)
16372 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16373 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16374 	if (sh)
16375 		bnxt_trim_dflt_sh_rings(bp);
16376 
16377 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16378 	if (bnxt_need_reserve_rings(bp)) {
16379 		rc = __bnxt_reserve_rings(bp);
16380 		if (rc && rc != -ENODEV)
16381 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16382 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16383 	}
16384 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16385 		bp->rx_nr_rings++;
16386 		bp->cp_nr_rings++;
16387 	}
16388 	if (rc) {
16389 		bp->tx_nr_rings = 0;
16390 		bp->rx_nr_rings = 0;
16391 	}
16392 	return rc;
16393 }
16394 
16395 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16396 {
16397 	int rc;
16398 
16399 	if (bp->tx_nr_rings)
16400 		return 0;
16401 
16402 	bnxt_ulp_irq_stop(bp);
16403 	bnxt_clear_int_mode(bp);
16404 	rc = bnxt_set_dflt_rings(bp, true);
16405 	if (rc) {
16406 		if (BNXT_VF(bp) && rc == -ENODEV)
16407 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16408 		else
16409 			netdev_err(bp->dev, "Not enough rings available.\n");
16410 		goto init_dflt_ring_err;
16411 	}
16412 	rc = bnxt_init_int_mode(bp);
16413 	if (rc)
16414 		goto init_dflt_ring_err;
16415 
16416 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16417 
16418 	bnxt_set_dflt_rfs(bp);
16419 
16420 init_dflt_ring_err:
16421 	bnxt_ulp_irq_restart(bp, rc);
16422 	return rc;
16423 }
16424 
16425 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16426 {
16427 	int rc;
16428 
16429 	netdev_ops_assert_locked(bp->dev);
16430 	bnxt_hwrm_func_qcaps(bp);
16431 
16432 	if (netif_running(bp->dev))
16433 		__bnxt_close_nic(bp, true, false);
16434 
16435 	bnxt_ulp_irq_stop(bp);
16436 	bnxt_clear_int_mode(bp);
16437 	rc = bnxt_init_int_mode(bp);
16438 	bnxt_ulp_irq_restart(bp, rc);
16439 
16440 	if (netif_running(bp->dev)) {
16441 		if (rc)
16442 			netif_close(bp->dev);
16443 		else
16444 			rc = bnxt_open_nic(bp, true, false);
16445 	}
16446 
16447 	return rc;
16448 }
16449 
16450 static int bnxt_init_mac_addr(struct bnxt *bp)
16451 {
16452 	int rc = 0;
16453 
16454 	if (BNXT_PF(bp)) {
16455 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16456 	} else {
16457 #ifdef CONFIG_BNXT_SRIOV
16458 		struct bnxt_vf_info *vf = &bp->vf;
16459 		bool strict_approval = true;
16460 
16461 		if (is_valid_ether_addr(vf->mac_addr)) {
16462 			/* overwrite netdev dev_addr with admin VF MAC */
16463 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16464 			/* Older PF driver or firmware may not approve this
16465 			 * correctly.
16466 			 */
16467 			strict_approval = false;
16468 		} else {
16469 			eth_hw_addr_random(bp->dev);
16470 		}
16471 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16472 #endif
16473 	}
16474 	return rc;
16475 }
16476 
16477 static void bnxt_vpd_read_info(struct bnxt *bp)
16478 {
16479 	struct pci_dev *pdev = bp->pdev;
16480 	unsigned int vpd_size, kw_len;
16481 	int pos, size;
16482 	u8 *vpd_data;
16483 
16484 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16485 	if (IS_ERR(vpd_data)) {
16486 		pci_warn(pdev, "Unable to read VPD\n");
16487 		return;
16488 	}
16489 
16490 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16491 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16492 	if (pos < 0)
16493 		goto read_sn;
16494 
16495 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16496 	memcpy(bp->board_partno, &vpd_data[pos], size);
16497 
16498 read_sn:
16499 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16500 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16501 					   &kw_len);
16502 	if (pos < 0)
16503 		goto exit;
16504 
16505 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16506 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16507 exit:
16508 	kfree(vpd_data);
16509 }
16510 
16511 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16512 {
16513 	struct pci_dev *pdev = bp->pdev;
16514 	u64 qword;
16515 
16516 	qword = pci_get_dsn(pdev);
16517 	if (!qword) {
16518 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16519 		return -EOPNOTSUPP;
16520 	}
16521 
16522 	put_unaligned_le64(qword, dsn);
16523 
16524 	bp->flags |= BNXT_FLAG_DSN_VALID;
16525 	return 0;
16526 }
16527 
16528 static int bnxt_map_db_bar(struct bnxt *bp)
16529 {
16530 	if (!bp->db_size)
16531 		return -ENODEV;
16532 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16533 	if (!bp->bar1)
16534 		return -ENOMEM;
16535 	return 0;
16536 }
16537 
16538 void bnxt_print_device_info(struct bnxt *bp)
16539 {
16540 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16541 		    board_info[bp->board_idx].name,
16542 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16543 
16544 	pcie_print_link_status(bp->pdev);
16545 }
16546 
16547 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16548 {
16549 	struct bnxt_hw_resc *hw_resc;
16550 	struct net_device *dev;
16551 	struct bnxt *bp;
16552 	int rc, max_irqs;
16553 
16554 	if (pci_is_bridge(pdev))
16555 		return -ENODEV;
16556 
16557 	if (!pdev->msix_cap) {
16558 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16559 		return -ENODEV;
16560 	}
16561 
16562 	/* Clear any pending DMA transactions from crash kernel
16563 	 * while loading driver in capture kernel.
16564 	 */
16565 	if (is_kdump_kernel()) {
16566 		pci_clear_master(pdev);
16567 		pcie_flr(pdev);
16568 	}
16569 
16570 	max_irqs = bnxt_get_max_irq(pdev);
16571 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16572 				 max_irqs);
16573 	if (!dev)
16574 		return -ENOMEM;
16575 
16576 	bp = netdev_priv(dev);
16577 	bp->board_idx = ent->driver_data;
16578 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16579 	bnxt_set_max_func_irqs(bp, max_irqs);
16580 
16581 	if (bnxt_vf_pciid(bp->board_idx))
16582 		bp->flags |= BNXT_FLAG_VF;
16583 
16584 	/* No devlink port registration in case of a VF */
16585 	if (BNXT_PF(bp))
16586 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16587 
16588 	rc = bnxt_init_board(pdev, dev);
16589 	if (rc < 0)
16590 		goto init_err_free;
16591 
16592 	dev->netdev_ops = &bnxt_netdev_ops;
16593 	dev->stat_ops = &bnxt_stat_ops;
16594 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16595 	dev->ethtool_ops = &bnxt_ethtool_ops;
16596 	pci_set_drvdata(pdev, dev);
16597 
16598 	rc = bnxt_alloc_hwrm_resources(bp);
16599 	if (rc)
16600 		goto init_err_pci_clean;
16601 
16602 	mutex_init(&bp->hwrm_cmd_lock);
16603 	mutex_init(&bp->link_lock);
16604 
16605 	rc = bnxt_fw_init_one_p1(bp);
16606 	if (rc)
16607 		goto init_err_pci_clean;
16608 
16609 	if (BNXT_PF(bp))
16610 		bnxt_vpd_read_info(bp);
16611 
16612 	if (BNXT_CHIP_P5_PLUS(bp)) {
16613 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16614 		if (BNXT_CHIP_P7(bp))
16615 			bp->flags |= BNXT_FLAG_CHIP_P7;
16616 	}
16617 
16618 	rc = bnxt_alloc_rss_indir_tbl(bp);
16619 	if (rc)
16620 		goto init_err_pci_clean;
16621 
16622 	rc = bnxt_fw_init_one_p2(bp);
16623 	if (rc)
16624 		goto init_err_pci_clean;
16625 
16626 	rc = bnxt_map_db_bar(bp);
16627 	if (rc) {
16628 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16629 			rc);
16630 		goto init_err_pci_clean;
16631 	}
16632 
16633 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16634 			   NETIF_F_TSO | NETIF_F_TSO6 |
16635 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16636 			   NETIF_F_GSO_IPXIP4 |
16637 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16638 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16639 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16640 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16641 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16642 
16643 	if (BNXT_SUPPORTS_TPA(bp))
16644 		dev->hw_features |= NETIF_F_LRO;
16645 
16646 	dev->hw_enc_features =
16647 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16648 			NETIF_F_TSO | NETIF_F_TSO6 |
16649 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16650 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16651 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16652 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16653 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16654 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16655 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16656 	else
16657 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16658 
16659 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16660 				    NETIF_F_GSO_GRE_CSUM;
16661 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16662 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16663 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16664 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16665 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16666 	if (BNXT_SUPPORTS_TPA(bp))
16667 		dev->hw_features |= NETIF_F_GRO_HW;
16668 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16669 	if (dev->features & NETIF_F_GRO_HW)
16670 		dev->features &= ~NETIF_F_LRO;
16671 	dev->priv_flags |= IFF_UNICAST_FLT;
16672 
16673 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16674 	if (bp->tso_max_segs)
16675 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16676 
16677 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16678 			    NETDEV_XDP_ACT_RX_SG;
16679 
16680 #ifdef CONFIG_BNXT_SRIOV
16681 	init_waitqueue_head(&bp->sriov_cfg_wait);
16682 #endif
16683 	if (BNXT_SUPPORTS_TPA(bp)) {
16684 		bp->gro_func = bnxt_gro_func_5730x;
16685 		if (BNXT_CHIP_P4(bp))
16686 			bp->gro_func = bnxt_gro_func_5731x;
16687 		else if (BNXT_CHIP_P5_PLUS(bp))
16688 			bp->gro_func = bnxt_gro_func_5750x;
16689 	}
16690 	if (!BNXT_CHIP_P4_PLUS(bp))
16691 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16692 
16693 	rc = bnxt_init_mac_addr(bp);
16694 	if (rc) {
16695 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16696 		rc = -EADDRNOTAVAIL;
16697 		goto init_err_pci_clean;
16698 	}
16699 
16700 	if (BNXT_PF(bp)) {
16701 		/* Read the adapter's DSN to use as the eswitch switch_id */
16702 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16703 	}
16704 
16705 	/* MTU range: 60 - FW defined max */
16706 	dev->min_mtu = ETH_ZLEN;
16707 	dev->max_mtu = bp->max_mtu;
16708 
16709 	rc = bnxt_probe_phy(bp, true);
16710 	if (rc)
16711 		goto init_err_pci_clean;
16712 
16713 	hw_resc = &bp->hw_resc;
16714 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16715 		       BNXT_L2_FLTR_MAX_FLTR;
16716 	/* Older firmware may not report these filters properly */
16717 	if (bp->max_fltr < BNXT_MAX_FLTR)
16718 		bp->max_fltr = BNXT_MAX_FLTR;
16719 	bnxt_init_l2_fltr_tbl(bp);
16720 	__bnxt_set_rx_skb_mode(bp, false);
16721 	bnxt_set_tpa_flags(bp);
16722 	bnxt_init_ring_params(bp);
16723 	bnxt_set_ring_params(bp);
16724 	bnxt_rdma_aux_device_init(bp);
16725 	rc = bnxt_set_dflt_rings(bp, true);
16726 	if (rc) {
16727 		if (BNXT_VF(bp) && rc == -ENODEV) {
16728 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16729 		} else {
16730 			netdev_err(bp->dev, "Not enough rings available.\n");
16731 			rc = -ENOMEM;
16732 		}
16733 		goto init_err_pci_clean;
16734 	}
16735 
16736 	bnxt_fw_init_one_p3(bp);
16737 
16738 	bnxt_init_dflt_coal(bp);
16739 
16740 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16741 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16742 
16743 	rc = bnxt_init_int_mode(bp);
16744 	if (rc)
16745 		goto init_err_pci_clean;
16746 
16747 	/* No TC has been set yet and rings may have been trimmed due to
16748 	 * limited MSIX, so we re-initialize the TX rings per TC.
16749 	 */
16750 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16751 
16752 	if (BNXT_PF(bp)) {
16753 		if (!bnxt_pf_wq) {
16754 			bnxt_pf_wq =
16755 				create_singlethread_workqueue("bnxt_pf_wq");
16756 			if (!bnxt_pf_wq) {
16757 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16758 				rc = -ENOMEM;
16759 				goto init_err_pci_clean;
16760 			}
16761 		}
16762 		rc = bnxt_init_tc(bp);
16763 		if (rc)
16764 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16765 				   rc);
16766 	}
16767 
16768 	bnxt_inv_fw_health_reg(bp);
16769 	rc = bnxt_dl_register(bp);
16770 	if (rc)
16771 		goto init_err_dl;
16772 
16773 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16774 
16775 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16776 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16777 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16778 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16779 	dev->request_ops_lock = true;
16780 	dev->netmem_tx = true;
16781 
16782 	rc = register_netdev(dev);
16783 	if (rc)
16784 		goto init_err_cleanup;
16785 
16786 	bnxt_dl_fw_reporters_create(bp);
16787 
16788 	bnxt_rdma_aux_device_add(bp);
16789 
16790 	bnxt_print_device_info(bp);
16791 
16792 	pci_save_state(pdev);
16793 
16794 	return 0;
16795 init_err_cleanup:
16796 	bnxt_rdma_aux_device_uninit(bp);
16797 	bnxt_dl_unregister(bp);
16798 init_err_dl:
16799 	bnxt_shutdown_tc(bp);
16800 	bnxt_clear_int_mode(bp);
16801 
16802 init_err_pci_clean:
16803 	bnxt_hwrm_func_drv_unrgtr(bp);
16804 	bnxt_free_hwrm_resources(bp);
16805 	bnxt_hwmon_uninit(bp);
16806 	bnxt_ethtool_free(bp);
16807 	bnxt_ptp_clear(bp);
16808 	kfree(bp->ptp_cfg);
16809 	bp->ptp_cfg = NULL;
16810 	kfree(bp->fw_health);
16811 	bp->fw_health = NULL;
16812 	bnxt_cleanup_pci(bp);
16813 	bnxt_free_ctx_mem(bp, true);
16814 	bnxt_free_crash_dump_mem(bp);
16815 	kfree(bp->rss_indir_tbl);
16816 	bp->rss_indir_tbl = NULL;
16817 
16818 init_err_free:
16819 	free_netdev(dev);
16820 	return rc;
16821 }
16822 
16823 static void bnxt_shutdown(struct pci_dev *pdev)
16824 {
16825 	struct net_device *dev = pci_get_drvdata(pdev);
16826 	struct bnxt *bp;
16827 
16828 	if (!dev)
16829 		return;
16830 
16831 	rtnl_lock();
16832 	netdev_lock(dev);
16833 	bp = netdev_priv(dev);
16834 	if (!bp)
16835 		goto shutdown_exit;
16836 
16837 	if (netif_running(dev))
16838 		netif_close(dev);
16839 
16840 	bnxt_ptp_clear(bp);
16841 	bnxt_clear_int_mode(bp);
16842 	pci_disable_device(pdev);
16843 
16844 	if (system_state == SYSTEM_POWER_OFF) {
16845 		pci_wake_from_d3(pdev, bp->wol);
16846 		pci_set_power_state(pdev, PCI_D3hot);
16847 	}
16848 
16849 shutdown_exit:
16850 	netdev_unlock(dev);
16851 	rtnl_unlock();
16852 }
16853 
16854 #ifdef CONFIG_PM_SLEEP
16855 static int bnxt_suspend(struct device *device)
16856 {
16857 	struct net_device *dev = dev_get_drvdata(device);
16858 	struct bnxt *bp = netdev_priv(dev);
16859 	int rc = 0;
16860 
16861 	bnxt_ulp_stop(bp);
16862 
16863 	netdev_lock(dev);
16864 	if (netif_running(dev)) {
16865 		netif_device_detach(dev);
16866 		rc = bnxt_close(dev);
16867 	}
16868 	bnxt_hwrm_func_drv_unrgtr(bp);
16869 	bnxt_ptp_clear(bp);
16870 	pci_disable_device(bp->pdev);
16871 	bnxt_free_ctx_mem(bp, false);
16872 	netdev_unlock(dev);
16873 	return rc;
16874 }
16875 
16876 static int bnxt_resume(struct device *device)
16877 {
16878 	struct net_device *dev = dev_get_drvdata(device);
16879 	struct bnxt *bp = netdev_priv(dev);
16880 	int rc = 0;
16881 
16882 	netdev_lock(dev);
16883 	rc = pci_enable_device(bp->pdev);
16884 	if (rc) {
16885 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16886 			   rc);
16887 		goto resume_exit;
16888 	}
16889 	pci_set_master(bp->pdev);
16890 	if (bnxt_hwrm_ver_get(bp)) {
16891 		rc = -ENODEV;
16892 		goto resume_exit;
16893 	}
16894 	rc = bnxt_hwrm_func_reset(bp);
16895 	if (rc) {
16896 		rc = -EBUSY;
16897 		goto resume_exit;
16898 	}
16899 
16900 	rc = bnxt_hwrm_func_qcaps(bp);
16901 	if (rc)
16902 		goto resume_exit;
16903 
16904 	bnxt_clear_reservations(bp, true);
16905 
16906 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16907 		rc = -ENODEV;
16908 		goto resume_exit;
16909 	}
16910 	if (bp->fw_crash_mem)
16911 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16912 
16913 	if (bnxt_ptp_init(bp)) {
16914 		kfree(bp->ptp_cfg);
16915 		bp->ptp_cfg = NULL;
16916 	}
16917 	bnxt_get_wol_settings(bp);
16918 	if (netif_running(dev)) {
16919 		rc = bnxt_open(dev);
16920 		if (!rc)
16921 			netif_device_attach(dev);
16922 	}
16923 
16924 resume_exit:
16925 	netdev_unlock(bp->dev);
16926 	bnxt_ulp_start(bp, rc);
16927 	if (!rc)
16928 		bnxt_reenable_sriov(bp);
16929 	return rc;
16930 }
16931 
16932 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16933 #define BNXT_PM_OPS (&bnxt_pm_ops)
16934 
16935 #else
16936 
16937 #define BNXT_PM_OPS NULL
16938 
16939 #endif /* CONFIG_PM_SLEEP */
16940 
16941 /**
16942  * bnxt_io_error_detected - called when PCI error is detected
16943  * @pdev: Pointer to PCI device
16944  * @state: The current pci connection state
16945  *
16946  * This function is called after a PCI bus error affecting
16947  * this device has been detected.
16948  */
16949 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16950 					       pci_channel_state_t state)
16951 {
16952 	struct net_device *netdev = pci_get_drvdata(pdev);
16953 	struct bnxt *bp = netdev_priv(netdev);
16954 	bool abort = false;
16955 
16956 	netdev_info(netdev, "PCI I/O error detected\n");
16957 
16958 	bnxt_ulp_stop(bp);
16959 
16960 	netdev_lock(netdev);
16961 	netif_device_detach(netdev);
16962 
16963 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16964 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16965 		abort = true;
16966 	}
16967 
16968 	if (abort || state == pci_channel_io_perm_failure) {
16969 		netdev_unlock(netdev);
16970 		return PCI_ERS_RESULT_DISCONNECT;
16971 	}
16972 
16973 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16974 	 * so we disable bus master to prevent any potential bad DMAs before
16975 	 * freeing kernel memory.
16976 	 */
16977 	if (state == pci_channel_io_frozen) {
16978 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16979 		bnxt_fw_fatal_close(bp);
16980 	}
16981 
16982 	if (netif_running(netdev))
16983 		__bnxt_close_nic(bp, true, true);
16984 
16985 	if (pci_is_enabled(pdev))
16986 		pci_disable_device(pdev);
16987 	bnxt_free_ctx_mem(bp, false);
16988 	netdev_unlock(netdev);
16989 
16990 	/* Request a slot reset. */
16991 	return PCI_ERS_RESULT_NEED_RESET;
16992 }
16993 
16994 /**
16995  * bnxt_io_slot_reset - called after the pci bus has been reset.
16996  * @pdev: Pointer to PCI device
16997  *
16998  * Restart the card from scratch, as if from a cold-boot.
16999  * At this point, the card has experienced a hard reset,
17000  * followed by fixups by BIOS, and has its config space
17001  * set up identically to what it was at cold boot.
17002  */
17003 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17004 {
17005 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17006 	struct net_device *netdev = pci_get_drvdata(pdev);
17007 	struct bnxt *bp = netdev_priv(netdev);
17008 	int retry = 0;
17009 	int err = 0;
17010 	int off;
17011 
17012 	netdev_info(bp->dev, "PCI Slot Reset\n");
17013 
17014 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17015 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17016 		msleep(900);
17017 
17018 	netdev_lock(netdev);
17019 
17020 	if (pci_enable_device(pdev)) {
17021 		dev_err(&pdev->dev,
17022 			"Cannot re-enable PCI device after reset.\n");
17023 	} else {
17024 		pci_set_master(pdev);
17025 		/* Upon fatal error, our device internal logic that latches to
17026 		 * BAR value is getting reset and will restore only upon
17027 		 * rewriting the BARs.
17028 		 *
17029 		 * As pci_restore_state() does not re-write the BARs if the
17030 		 * value is same as saved value earlier, driver needs to
17031 		 * write the BARs to 0 to force restore, in case of fatal error.
17032 		 */
17033 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17034 				       &bp->state)) {
17035 			for (off = PCI_BASE_ADDRESS_0;
17036 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17037 				pci_write_config_dword(bp->pdev, off, 0);
17038 		}
17039 		pci_restore_state(pdev);
17040 		pci_save_state(pdev);
17041 
17042 		bnxt_inv_fw_health_reg(bp);
17043 		bnxt_try_map_fw_health_reg(bp);
17044 
17045 		/* In some PCIe AER scenarios, firmware may take up to
17046 		 * 10 seconds to become ready in the worst case.
17047 		 */
17048 		do {
17049 			err = bnxt_try_recover_fw(bp);
17050 			if (!err)
17051 				break;
17052 			retry++;
17053 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17054 
17055 		if (err) {
17056 			dev_err(&pdev->dev, "Firmware not ready\n");
17057 			goto reset_exit;
17058 		}
17059 
17060 		err = bnxt_hwrm_func_reset(bp);
17061 		if (!err)
17062 			result = PCI_ERS_RESULT_RECOVERED;
17063 
17064 		/* IRQ will be initialized later in bnxt_io_resume */
17065 		bnxt_ulp_irq_stop(bp);
17066 		bnxt_clear_int_mode(bp);
17067 	}
17068 
17069 reset_exit:
17070 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17071 	bnxt_clear_reservations(bp, true);
17072 	netdev_unlock(netdev);
17073 
17074 	return result;
17075 }
17076 
17077 /**
17078  * bnxt_io_resume - called when traffic can start flowing again.
17079  * @pdev: Pointer to PCI device
17080  *
17081  * This callback is called when the error recovery driver tells
17082  * us that its OK to resume normal operation.
17083  */
17084 static void bnxt_io_resume(struct pci_dev *pdev)
17085 {
17086 	struct net_device *netdev = pci_get_drvdata(pdev);
17087 	struct bnxt *bp = netdev_priv(netdev);
17088 	int err;
17089 
17090 	netdev_info(bp->dev, "PCI Slot Resume\n");
17091 	netdev_lock(netdev);
17092 
17093 	err = bnxt_hwrm_func_qcaps(bp);
17094 	if (!err) {
17095 		if (netif_running(netdev)) {
17096 			err = bnxt_open(netdev);
17097 		} else {
17098 			err = bnxt_reserve_rings(bp, true);
17099 			if (!err)
17100 				err = bnxt_init_int_mode(bp);
17101 		}
17102 	}
17103 
17104 	if (!err)
17105 		netif_device_attach(netdev);
17106 
17107 	netdev_unlock(netdev);
17108 	bnxt_ulp_start(bp, err);
17109 	if (!err)
17110 		bnxt_reenable_sriov(bp);
17111 }
17112 
17113 static const struct pci_error_handlers bnxt_err_handler = {
17114 	.error_detected	= bnxt_io_error_detected,
17115 	.slot_reset	= bnxt_io_slot_reset,
17116 	.resume		= bnxt_io_resume
17117 };
17118 
17119 static struct pci_driver bnxt_pci_driver = {
17120 	.name		= DRV_MODULE_NAME,
17121 	.id_table	= bnxt_pci_tbl,
17122 	.probe		= bnxt_init_one,
17123 	.remove		= bnxt_remove_one,
17124 	.shutdown	= bnxt_shutdown,
17125 	.driver.pm	= BNXT_PM_OPS,
17126 	.err_handler	= &bnxt_err_handler,
17127 #if defined(CONFIG_BNXT_SRIOV)
17128 	.sriov_configure = bnxt_sriov_configure,
17129 #endif
17130 };
17131 
17132 static int __init bnxt_init(void)
17133 {
17134 	int err;
17135 
17136 	bnxt_debug_init();
17137 	err = pci_register_driver(&bnxt_pci_driver);
17138 	if (err) {
17139 		bnxt_debug_exit();
17140 		return err;
17141 	}
17142 
17143 	return 0;
17144 }
17145 
17146 static void __exit bnxt_exit(void)
17147 {
17148 	pci_unregister_driver(&bnxt_pci_driver);
17149 	if (bnxt_pf_wq)
17150 		destroy_workqueue(bnxt_pf_wq);
17151 	bnxt_debug_exit();
17152 }
17153 
17154 module_init(bnxt_init);
17155 module_exit(bnxt_exit);
17156