xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision f09fc24dd9a5ec989dfdde7090624924ede6ddc7)
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_RSS_IPV6_FLOW_LABEL_CAP)
6973 			bp->rss_cap |= BNXT_RSS_CAP_IPV6_FLOW_LABEL_RSS_CAP;
6974 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6975 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6976 	}
6977 	hwrm_req_drop(bp, req);
6978 	return rc;
6979 }
6980 
6981 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6982 {
6983 	struct hwrm_ring_grp_alloc_output *resp;
6984 	struct hwrm_ring_grp_alloc_input *req;
6985 	int rc;
6986 	u16 i;
6987 
6988 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6989 		return 0;
6990 
6991 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6992 	if (rc)
6993 		return rc;
6994 
6995 	resp = hwrm_req_hold(bp, req);
6996 	for (i = 0; i < bp->rx_nr_rings; i++) {
6997 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6998 
6999 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
7000 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
7001 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
7002 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
7003 
7004 		rc = hwrm_req_send(bp, req);
7005 
7006 		if (rc)
7007 			break;
7008 
7009 		bp->grp_info[grp_idx].fw_grp_id =
7010 			le32_to_cpu(resp->ring_group_id);
7011 	}
7012 	hwrm_req_drop(bp, req);
7013 	return rc;
7014 }
7015 
7016 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7017 {
7018 	struct hwrm_ring_grp_free_input *req;
7019 	u16 i;
7020 
7021 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7022 		return;
7023 
7024 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7025 		return;
7026 
7027 	hwrm_req_hold(bp, req);
7028 	for (i = 0; i < bp->cp_nr_rings; i++) {
7029 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7030 			continue;
7031 		req->ring_group_id =
7032 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7033 
7034 		hwrm_req_send(bp, req);
7035 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7036 	}
7037 	hwrm_req_drop(bp, req);
7038 }
7039 
7040 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7041 				       struct hwrm_ring_alloc_input *req,
7042 				       struct bnxt_ring_struct *ring)
7043 {
7044 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7045 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7046 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7047 
7048 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7049 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7050 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7051 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7052 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7053 	} else {
7054 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7055 		if (NET_IP_ALIGN == 2)
7056 			req->flags =
7057 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7058 	}
7059 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7060 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7061 	req->enables |= cpu_to_le32(enables);
7062 }
7063 
7064 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7065 				    struct bnxt_ring_struct *ring,
7066 				    u32 ring_type, u32 map_index)
7067 {
7068 	struct hwrm_ring_alloc_output *resp;
7069 	struct hwrm_ring_alloc_input *req;
7070 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7071 	struct bnxt_ring_grp_info *grp_info;
7072 	int rc, err = 0;
7073 	u16 ring_id;
7074 
7075 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7076 	if (rc)
7077 		goto exit;
7078 
7079 	req->enables = 0;
7080 	if (rmem->nr_pages > 1) {
7081 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7082 		/* Page size is in log2 units */
7083 		req->page_size = BNXT_PAGE_SHIFT;
7084 		req->page_tbl_depth = 1;
7085 	} else {
7086 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7087 	}
7088 	req->fbo = 0;
7089 	/* Association of ring index with doorbell index and MSIX number */
7090 	req->logical_id = cpu_to_le16(map_index);
7091 
7092 	switch (ring_type) {
7093 	case HWRM_RING_ALLOC_TX: {
7094 		struct bnxt_tx_ring_info *txr;
7095 		u16 flags = 0;
7096 
7097 		txr = container_of(ring, struct bnxt_tx_ring_info,
7098 				   tx_ring_struct);
7099 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7100 		/* Association of transmit ring with completion ring */
7101 		grp_info = &bp->grp_info[ring->grp_idx];
7102 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7103 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7104 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7105 		req->queue_id = cpu_to_le16(ring->queue_id);
7106 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7107 			req->cmpl_coal_cnt =
7108 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7109 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7110 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7111 		req->flags = cpu_to_le16(flags);
7112 		break;
7113 	}
7114 	case HWRM_RING_ALLOC_RX:
7115 	case HWRM_RING_ALLOC_AGG:
7116 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7117 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7118 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7119 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7120 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7121 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7122 		break;
7123 	case HWRM_RING_ALLOC_CMPL:
7124 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7125 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7126 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7127 			/* Association of cp ring with nq */
7128 			grp_info = &bp->grp_info[map_index];
7129 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7130 			req->cq_handle = cpu_to_le64(ring->handle);
7131 			req->enables |= cpu_to_le32(
7132 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7133 		} else {
7134 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7135 		}
7136 		break;
7137 	case HWRM_RING_ALLOC_NQ:
7138 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7139 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7140 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7141 		break;
7142 	default:
7143 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7144 			   ring_type);
7145 		return -EINVAL;
7146 	}
7147 
7148 	resp = hwrm_req_hold(bp, req);
7149 	rc = hwrm_req_send(bp, req);
7150 	err = le16_to_cpu(resp->error_code);
7151 	ring_id = le16_to_cpu(resp->ring_id);
7152 	hwrm_req_drop(bp, req);
7153 
7154 exit:
7155 	if (rc || err) {
7156 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7157 			   ring_type, rc, err);
7158 		return -EIO;
7159 	}
7160 	ring->fw_ring_id = ring_id;
7161 	return rc;
7162 }
7163 
7164 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7165 {
7166 	int rc;
7167 
7168 	if (BNXT_PF(bp)) {
7169 		struct hwrm_func_cfg_input *req;
7170 
7171 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7172 		if (rc)
7173 			return rc;
7174 
7175 		req->fid = cpu_to_le16(0xffff);
7176 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7177 		req->async_event_cr = cpu_to_le16(idx);
7178 		return hwrm_req_send(bp, req);
7179 	} else {
7180 		struct hwrm_func_vf_cfg_input *req;
7181 
7182 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7183 		if (rc)
7184 			return rc;
7185 
7186 		req->enables =
7187 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7188 		req->async_event_cr = cpu_to_le16(idx);
7189 		return hwrm_req_send(bp, req);
7190 	}
7191 }
7192 
7193 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7194 			     u32 ring_type)
7195 {
7196 	switch (ring_type) {
7197 	case HWRM_RING_ALLOC_TX:
7198 		db->db_ring_mask = bp->tx_ring_mask;
7199 		break;
7200 	case HWRM_RING_ALLOC_RX:
7201 		db->db_ring_mask = bp->rx_ring_mask;
7202 		break;
7203 	case HWRM_RING_ALLOC_AGG:
7204 		db->db_ring_mask = bp->rx_agg_ring_mask;
7205 		break;
7206 	case HWRM_RING_ALLOC_CMPL:
7207 	case HWRM_RING_ALLOC_NQ:
7208 		db->db_ring_mask = bp->cp_ring_mask;
7209 		break;
7210 	}
7211 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7212 		db->db_epoch_mask = db->db_ring_mask + 1;
7213 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7214 	}
7215 }
7216 
7217 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7218 			u32 map_idx, u32 xid)
7219 {
7220 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7221 		switch (ring_type) {
7222 		case HWRM_RING_ALLOC_TX:
7223 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7224 			break;
7225 		case HWRM_RING_ALLOC_RX:
7226 		case HWRM_RING_ALLOC_AGG:
7227 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7228 			break;
7229 		case HWRM_RING_ALLOC_CMPL:
7230 			db->db_key64 = DBR_PATH_L2;
7231 			break;
7232 		case HWRM_RING_ALLOC_NQ:
7233 			db->db_key64 = DBR_PATH_L2;
7234 			break;
7235 		}
7236 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7237 
7238 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7239 			db->db_key64 |= DBR_VALID;
7240 
7241 		db->doorbell = bp->bar1 + bp->db_offset;
7242 	} else {
7243 		db->doorbell = bp->bar1 + map_idx * 0x80;
7244 		switch (ring_type) {
7245 		case HWRM_RING_ALLOC_TX:
7246 			db->db_key32 = DB_KEY_TX;
7247 			break;
7248 		case HWRM_RING_ALLOC_RX:
7249 		case HWRM_RING_ALLOC_AGG:
7250 			db->db_key32 = DB_KEY_RX;
7251 			break;
7252 		case HWRM_RING_ALLOC_CMPL:
7253 			db->db_key32 = DB_KEY_CP;
7254 			break;
7255 		}
7256 	}
7257 	bnxt_set_db_mask(bp, db, ring_type);
7258 }
7259 
7260 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7261 				   struct bnxt_rx_ring_info *rxr)
7262 {
7263 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7264 	struct bnxt_napi *bnapi = rxr->bnapi;
7265 	u32 type = HWRM_RING_ALLOC_RX;
7266 	u32 map_idx = bnapi->index;
7267 	int rc;
7268 
7269 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7270 	if (rc)
7271 		return rc;
7272 
7273 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7274 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7275 
7276 	return 0;
7277 }
7278 
7279 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7280 				       struct bnxt_rx_ring_info *rxr)
7281 {
7282 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7283 	u32 type = HWRM_RING_ALLOC_AGG;
7284 	u32 grp_idx = ring->grp_idx;
7285 	u32 map_idx;
7286 	int rc;
7287 
7288 	map_idx = grp_idx + bp->rx_nr_rings;
7289 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7290 	if (rc)
7291 		return rc;
7292 
7293 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7294 		    ring->fw_ring_id);
7295 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7296 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7297 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7298 
7299 	return 0;
7300 }
7301 
7302 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7303 				      struct bnxt_cp_ring_info *cpr)
7304 {
7305 	const u32 type = HWRM_RING_ALLOC_CMPL;
7306 	struct bnxt_napi *bnapi = cpr->bnapi;
7307 	struct bnxt_ring_struct *ring;
7308 	u32 map_idx = bnapi->index;
7309 	int rc;
7310 
7311 	ring = &cpr->cp_ring_struct;
7312 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7313 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7314 	if (rc)
7315 		return rc;
7316 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7317 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7318 	return 0;
7319 }
7320 
7321 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7322 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7323 {
7324 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7325 	const u32 type = HWRM_RING_ALLOC_TX;
7326 	int rc;
7327 
7328 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7329 	if (rc)
7330 		return rc;
7331 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7332 	return 0;
7333 }
7334 
7335 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7336 {
7337 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7338 	int i, rc = 0;
7339 	u32 type;
7340 
7341 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7342 		type = HWRM_RING_ALLOC_NQ;
7343 	else
7344 		type = HWRM_RING_ALLOC_CMPL;
7345 	for (i = 0; i < bp->cp_nr_rings; i++) {
7346 		struct bnxt_napi *bnapi = bp->bnapi[i];
7347 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7348 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7349 		u32 map_idx = ring->map_idx;
7350 		unsigned int vector;
7351 
7352 		vector = bp->irq_tbl[map_idx].vector;
7353 		disable_irq_nosync(vector);
7354 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7355 		if (rc) {
7356 			enable_irq(vector);
7357 			goto err_out;
7358 		}
7359 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7360 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7361 		enable_irq(vector);
7362 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7363 
7364 		if (!i) {
7365 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7366 			if (rc)
7367 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7368 		}
7369 	}
7370 
7371 	for (i = 0; i < bp->tx_nr_rings; i++) {
7372 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7373 
7374 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7375 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7376 			if (rc)
7377 				goto err_out;
7378 		}
7379 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7380 		if (rc)
7381 			goto err_out;
7382 	}
7383 
7384 	for (i = 0; i < bp->rx_nr_rings; i++) {
7385 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7386 
7387 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7388 		if (rc)
7389 			goto err_out;
7390 		/* If we have agg rings, post agg buffers first. */
7391 		if (!agg_rings)
7392 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7393 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7394 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7395 			if (rc)
7396 				goto err_out;
7397 		}
7398 	}
7399 
7400 	if (agg_rings) {
7401 		for (i = 0; i < bp->rx_nr_rings; i++) {
7402 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7403 			if (rc)
7404 				goto err_out;
7405 		}
7406 	}
7407 err_out:
7408 	return rc;
7409 }
7410 
7411 static void bnxt_cancel_dim(struct bnxt *bp)
7412 {
7413 	int i;
7414 
7415 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7416 	 * if NAPI is enabled.
7417 	 */
7418 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7419 		return;
7420 
7421 	/* Make sure NAPI sees that the VNIC is disabled */
7422 	synchronize_net();
7423 	for (i = 0; i < bp->rx_nr_rings; i++) {
7424 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7425 		struct bnxt_napi *bnapi = rxr->bnapi;
7426 
7427 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7428 	}
7429 }
7430 
7431 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7432 				   struct bnxt_ring_struct *ring,
7433 				   u32 ring_type, int cmpl_ring_id)
7434 {
7435 	struct hwrm_ring_free_output *resp;
7436 	struct hwrm_ring_free_input *req;
7437 	u16 error_code = 0;
7438 	int rc;
7439 
7440 	if (BNXT_NO_FW_ACCESS(bp))
7441 		return 0;
7442 
7443 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7444 	if (rc)
7445 		goto exit;
7446 
7447 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7448 	req->ring_type = ring_type;
7449 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7450 
7451 	resp = hwrm_req_hold(bp, req);
7452 	rc = hwrm_req_send(bp, req);
7453 	error_code = le16_to_cpu(resp->error_code);
7454 	hwrm_req_drop(bp, req);
7455 exit:
7456 	if (rc || error_code) {
7457 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7458 			   ring_type, rc, error_code);
7459 		return -EIO;
7460 	}
7461 	return 0;
7462 }
7463 
7464 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7465 				   struct bnxt_tx_ring_info *txr,
7466 				   bool close_path)
7467 {
7468 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7469 	u32 cmpl_ring_id;
7470 
7471 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7472 		return;
7473 
7474 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7475 		       INVALID_HW_RING_ID;
7476 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7477 				cmpl_ring_id);
7478 	ring->fw_ring_id = INVALID_HW_RING_ID;
7479 }
7480 
7481 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7482 				   struct bnxt_rx_ring_info *rxr,
7483 				   bool close_path)
7484 {
7485 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7486 	u32 grp_idx = rxr->bnapi->index;
7487 	u32 cmpl_ring_id;
7488 
7489 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7490 		return;
7491 
7492 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7493 	hwrm_ring_free_send_msg(bp, ring,
7494 				RING_FREE_REQ_RING_TYPE_RX,
7495 				close_path ? cmpl_ring_id :
7496 				INVALID_HW_RING_ID);
7497 	ring->fw_ring_id = INVALID_HW_RING_ID;
7498 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7499 }
7500 
7501 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7502 				       struct bnxt_rx_ring_info *rxr,
7503 				       bool close_path)
7504 {
7505 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7506 	u32 grp_idx = rxr->bnapi->index;
7507 	u32 type, cmpl_ring_id;
7508 
7509 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7510 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7511 	else
7512 		type = RING_FREE_REQ_RING_TYPE_RX;
7513 
7514 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7515 		return;
7516 
7517 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7518 	hwrm_ring_free_send_msg(bp, ring, type,
7519 				close_path ? cmpl_ring_id :
7520 				INVALID_HW_RING_ID);
7521 	ring->fw_ring_id = INVALID_HW_RING_ID;
7522 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7523 }
7524 
7525 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7526 				   struct bnxt_cp_ring_info *cpr)
7527 {
7528 	struct bnxt_ring_struct *ring;
7529 
7530 	ring = &cpr->cp_ring_struct;
7531 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7532 		return;
7533 
7534 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7535 				INVALID_HW_RING_ID);
7536 	ring->fw_ring_id = INVALID_HW_RING_ID;
7537 }
7538 
7539 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7540 {
7541 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7542 	int i, size = ring->ring_mem.page_size;
7543 
7544 	cpr->cp_raw_cons = 0;
7545 	cpr->toggle = 0;
7546 
7547 	for (i = 0; i < bp->cp_nr_pages; i++)
7548 		if (cpr->cp_desc_ring[i])
7549 			memset(cpr->cp_desc_ring[i], 0, size);
7550 }
7551 
7552 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7553 {
7554 	u32 type;
7555 	int i;
7556 
7557 	if (!bp->bnapi)
7558 		return;
7559 
7560 	for (i = 0; i < bp->tx_nr_rings; i++)
7561 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7562 
7563 	bnxt_cancel_dim(bp);
7564 	for (i = 0; i < bp->rx_nr_rings; i++) {
7565 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7566 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7567 	}
7568 
7569 	/* The completion rings are about to be freed.  After that the
7570 	 * IRQ doorbell will not work anymore.  So we need to disable
7571 	 * IRQ here.
7572 	 */
7573 	bnxt_disable_int_sync(bp);
7574 
7575 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7576 		type = RING_FREE_REQ_RING_TYPE_NQ;
7577 	else
7578 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7579 	for (i = 0; i < bp->cp_nr_rings; i++) {
7580 		struct bnxt_napi *bnapi = bp->bnapi[i];
7581 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7582 		struct bnxt_ring_struct *ring;
7583 		int j;
7584 
7585 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7586 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7587 
7588 		ring = &cpr->cp_ring_struct;
7589 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7590 			hwrm_ring_free_send_msg(bp, ring, type,
7591 						INVALID_HW_RING_ID);
7592 			ring->fw_ring_id = INVALID_HW_RING_ID;
7593 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7594 		}
7595 	}
7596 }
7597 
7598 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7599 			     bool shared);
7600 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7601 			   bool shared);
7602 
7603 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7604 {
7605 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7606 	struct hwrm_func_qcfg_output *resp;
7607 	struct hwrm_func_qcfg_input *req;
7608 	int rc;
7609 
7610 	if (bp->hwrm_spec_code < 0x10601)
7611 		return 0;
7612 
7613 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7614 	if (rc)
7615 		return rc;
7616 
7617 	req->fid = cpu_to_le16(0xffff);
7618 	resp = hwrm_req_hold(bp, req);
7619 	rc = hwrm_req_send(bp, req);
7620 	if (rc) {
7621 		hwrm_req_drop(bp, req);
7622 		return rc;
7623 	}
7624 
7625 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7626 	if (BNXT_NEW_RM(bp)) {
7627 		u16 cp, stats;
7628 
7629 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7630 		hw_resc->resv_hw_ring_grps =
7631 			le32_to_cpu(resp->alloc_hw_ring_grps);
7632 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7633 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7634 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7635 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7636 		hw_resc->resv_irqs = cp;
7637 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7638 			int rx = hw_resc->resv_rx_rings;
7639 			int tx = hw_resc->resv_tx_rings;
7640 
7641 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7642 				rx >>= 1;
7643 			if (cp < (rx + tx)) {
7644 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7645 				if (rc)
7646 					goto get_rings_exit;
7647 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7648 					rx <<= 1;
7649 				hw_resc->resv_rx_rings = rx;
7650 				hw_resc->resv_tx_rings = tx;
7651 			}
7652 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7653 			hw_resc->resv_hw_ring_grps = rx;
7654 		}
7655 		hw_resc->resv_cp_rings = cp;
7656 		hw_resc->resv_stat_ctxs = stats;
7657 	}
7658 get_rings_exit:
7659 	hwrm_req_drop(bp, req);
7660 	return rc;
7661 }
7662 
7663 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7664 {
7665 	struct hwrm_func_qcfg_output *resp;
7666 	struct hwrm_func_qcfg_input *req;
7667 	int rc;
7668 
7669 	if (bp->hwrm_spec_code < 0x10601)
7670 		return 0;
7671 
7672 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7673 	if (rc)
7674 		return rc;
7675 
7676 	req->fid = cpu_to_le16(fid);
7677 	resp = hwrm_req_hold(bp, req);
7678 	rc = hwrm_req_send(bp, req);
7679 	if (!rc)
7680 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7681 
7682 	hwrm_req_drop(bp, req);
7683 	return rc;
7684 }
7685 
7686 static bool bnxt_rfs_supported(struct bnxt *bp);
7687 
7688 static struct hwrm_func_cfg_input *
7689 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7690 {
7691 	struct hwrm_func_cfg_input *req;
7692 	u32 enables = 0;
7693 
7694 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7695 		return NULL;
7696 
7697 	req->fid = cpu_to_le16(0xffff);
7698 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7699 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7700 	if (BNXT_NEW_RM(bp)) {
7701 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7702 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7703 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7704 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7705 			enables |= hwr->cp_p5 ?
7706 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7707 		} else {
7708 			enables |= hwr->cp ?
7709 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7710 			enables |= hwr->grp ?
7711 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7712 		}
7713 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7714 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7715 					  0;
7716 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7717 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7718 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7719 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7720 			req->num_msix = cpu_to_le16(hwr->cp);
7721 		} else {
7722 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7723 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7724 		}
7725 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7726 		req->num_vnics = cpu_to_le16(hwr->vnic);
7727 	}
7728 	req->enables = cpu_to_le32(enables);
7729 	return req;
7730 }
7731 
7732 static struct hwrm_func_vf_cfg_input *
7733 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7734 {
7735 	struct hwrm_func_vf_cfg_input *req;
7736 	u32 enables = 0;
7737 
7738 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7739 		return NULL;
7740 
7741 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7742 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7743 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7744 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7745 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7746 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7747 		enables |= hwr->cp_p5 ?
7748 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7749 	} else {
7750 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7751 		enables |= hwr->grp ?
7752 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7753 	}
7754 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7755 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7756 
7757 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7758 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7759 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7760 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7761 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7762 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7763 	} else {
7764 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7765 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7766 	}
7767 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7768 	req->num_vnics = cpu_to_le16(hwr->vnic);
7769 
7770 	req->enables = cpu_to_le32(enables);
7771 	return req;
7772 }
7773 
7774 static int
7775 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7776 {
7777 	struct hwrm_func_cfg_input *req;
7778 	int rc;
7779 
7780 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7781 	if (!req)
7782 		return -ENOMEM;
7783 
7784 	if (!req->enables) {
7785 		hwrm_req_drop(bp, req);
7786 		return 0;
7787 	}
7788 
7789 	rc = hwrm_req_send(bp, req);
7790 	if (rc)
7791 		return rc;
7792 
7793 	if (bp->hwrm_spec_code < 0x10601)
7794 		bp->hw_resc.resv_tx_rings = hwr->tx;
7795 
7796 	return bnxt_hwrm_get_rings(bp);
7797 }
7798 
7799 static int
7800 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7801 {
7802 	struct hwrm_func_vf_cfg_input *req;
7803 	int rc;
7804 
7805 	if (!BNXT_NEW_RM(bp)) {
7806 		bp->hw_resc.resv_tx_rings = hwr->tx;
7807 		return 0;
7808 	}
7809 
7810 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7811 	if (!req)
7812 		return -ENOMEM;
7813 
7814 	rc = hwrm_req_send(bp, req);
7815 	if (rc)
7816 		return rc;
7817 
7818 	return bnxt_hwrm_get_rings(bp);
7819 }
7820 
7821 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7822 {
7823 	if (BNXT_PF(bp))
7824 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7825 	else
7826 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7827 }
7828 
7829 int bnxt_nq_rings_in_use(struct bnxt *bp)
7830 {
7831 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7832 }
7833 
7834 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7835 {
7836 	int cp;
7837 
7838 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7839 		return bnxt_nq_rings_in_use(bp);
7840 
7841 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7842 	return cp;
7843 }
7844 
7845 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7846 {
7847 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7848 }
7849 
7850 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7851 {
7852 	if (!hwr->grp)
7853 		return 0;
7854 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7855 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7856 
7857 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7858 			rss_ctx *= hwr->vnic;
7859 		return rss_ctx;
7860 	}
7861 	if (BNXT_VF(bp))
7862 		return BNXT_VF_MAX_RSS_CTX;
7863 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7864 		return hwr->grp + 1;
7865 	return 1;
7866 }
7867 
7868 /* Check if a default RSS map needs to be setup.  This function is only
7869  * used on older firmware that does not require reserving RX rings.
7870  */
7871 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7872 {
7873 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7874 
7875 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7876 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7877 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7878 		if (!netif_is_rxfh_configured(bp->dev))
7879 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7880 	}
7881 }
7882 
7883 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7884 {
7885 	if (bp->flags & BNXT_FLAG_RFS) {
7886 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7887 			return 2 + bp->num_rss_ctx;
7888 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7889 			return rx_rings + 1;
7890 	}
7891 	return 1;
7892 }
7893 
7894 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7895 {
7896 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7897 	int cp = bnxt_cp_rings_in_use(bp);
7898 	int nq = bnxt_nq_rings_in_use(bp);
7899 	int rx = bp->rx_nr_rings, stat;
7900 	int vnic, grp = rx;
7901 
7902 	/* Old firmware does not need RX ring reservations but we still
7903 	 * need to setup a default RSS map when needed.  With new firmware
7904 	 * we go through RX ring reservations first and then set up the
7905 	 * RSS map for the successfully reserved RX rings when needed.
7906 	 */
7907 	if (!BNXT_NEW_RM(bp))
7908 		bnxt_check_rss_tbl_no_rmgr(bp);
7909 
7910 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7911 	    bp->hwrm_spec_code >= 0x10601)
7912 		return true;
7913 
7914 	if (!BNXT_NEW_RM(bp))
7915 		return false;
7916 
7917 	vnic = bnxt_get_total_vnics(bp, rx);
7918 
7919 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7920 		rx <<= 1;
7921 	stat = bnxt_get_func_stat_ctxs(bp);
7922 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7923 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7924 	    (hw_resc->resv_hw_ring_grps != grp &&
7925 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7926 		return true;
7927 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7928 	    hw_resc->resv_irqs != nq)
7929 		return true;
7930 	return false;
7931 }
7932 
7933 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7934 {
7935 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7936 
7937 	hwr->tx = hw_resc->resv_tx_rings;
7938 	if (BNXT_NEW_RM(bp)) {
7939 		hwr->rx = hw_resc->resv_rx_rings;
7940 		hwr->cp = hw_resc->resv_irqs;
7941 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7942 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7943 		hwr->grp = hw_resc->resv_hw_ring_grps;
7944 		hwr->vnic = hw_resc->resv_vnics;
7945 		hwr->stat = hw_resc->resv_stat_ctxs;
7946 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7947 	}
7948 }
7949 
7950 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7951 {
7952 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7953 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7954 }
7955 
7956 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7957 
7958 static int __bnxt_reserve_rings(struct bnxt *bp)
7959 {
7960 	struct bnxt_hw_rings hwr = {0};
7961 	int rx_rings, old_rx_rings, rc;
7962 	int cp = bp->cp_nr_rings;
7963 	int ulp_msix = 0;
7964 	bool sh = false;
7965 	int tx_cp;
7966 
7967 	if (!bnxt_need_reserve_rings(bp))
7968 		return 0;
7969 
7970 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7971 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7972 		if (!ulp_msix)
7973 			bnxt_set_ulp_stat_ctxs(bp, 0);
7974 
7975 		if (ulp_msix > bp->ulp_num_msix_want)
7976 			ulp_msix = bp->ulp_num_msix_want;
7977 		hwr.cp = cp + ulp_msix;
7978 	} else {
7979 		hwr.cp = bnxt_nq_rings_in_use(bp);
7980 	}
7981 
7982 	hwr.tx = bp->tx_nr_rings;
7983 	hwr.rx = bp->rx_nr_rings;
7984 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7985 		sh = true;
7986 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7987 		hwr.cp_p5 = hwr.rx + hwr.tx;
7988 
7989 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7990 
7991 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7992 		hwr.rx <<= 1;
7993 	hwr.grp = bp->rx_nr_rings;
7994 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7995 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7996 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7997 
7998 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7999 	if (rc)
8000 		return rc;
8001 
8002 	bnxt_copy_reserved_rings(bp, &hwr);
8003 
8004 	rx_rings = hwr.rx;
8005 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
8006 		if (hwr.rx >= 2) {
8007 			rx_rings = hwr.rx >> 1;
8008 		} else {
8009 			if (netif_running(bp->dev))
8010 				return -ENOMEM;
8011 
8012 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
8013 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
8014 			bp->dev->hw_features &= ~NETIF_F_LRO;
8015 			bp->dev->features &= ~NETIF_F_LRO;
8016 			bnxt_set_ring_params(bp);
8017 		}
8018 	}
8019 	rx_rings = min_t(int, rx_rings, hwr.grp);
8020 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8021 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8022 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8023 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8024 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8025 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8026 		hwr.rx = rx_rings << 1;
8027 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8028 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8029 	bp->tx_nr_rings = hwr.tx;
8030 
8031 	/* If we cannot reserve all the RX rings, reset the RSS map only
8032 	 * if absolutely necessary
8033 	 */
8034 	if (rx_rings != bp->rx_nr_rings) {
8035 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8036 			    rx_rings, bp->rx_nr_rings);
8037 		if (netif_is_rxfh_configured(bp->dev) &&
8038 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8039 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8040 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8041 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8042 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8043 		}
8044 	}
8045 	bp->rx_nr_rings = rx_rings;
8046 	bp->cp_nr_rings = hwr.cp;
8047 
8048 	if (!bnxt_rings_ok(bp, &hwr))
8049 		return -ENOMEM;
8050 
8051 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8052 	    !netif_is_rxfh_configured(bp->dev))
8053 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8054 
8055 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8056 		int resv_msix, resv_ctx, ulp_ctxs;
8057 		struct bnxt_hw_resc *hw_resc;
8058 
8059 		hw_resc = &bp->hw_resc;
8060 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8061 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8062 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8063 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8064 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8065 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8066 	}
8067 
8068 	return rc;
8069 }
8070 
8071 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8072 {
8073 	struct hwrm_func_vf_cfg_input *req;
8074 	u32 flags;
8075 
8076 	if (!BNXT_NEW_RM(bp))
8077 		return 0;
8078 
8079 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8080 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8081 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8082 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8083 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8084 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8085 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8086 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8087 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8088 
8089 	req->flags = cpu_to_le32(flags);
8090 	return hwrm_req_send_silent(bp, req);
8091 }
8092 
8093 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8094 {
8095 	struct hwrm_func_cfg_input *req;
8096 	u32 flags;
8097 
8098 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8099 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8100 	if (BNXT_NEW_RM(bp)) {
8101 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8102 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8103 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8104 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8105 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8106 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8107 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8108 		else
8109 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8110 	}
8111 
8112 	req->flags = cpu_to_le32(flags);
8113 	return hwrm_req_send_silent(bp, req);
8114 }
8115 
8116 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8117 {
8118 	if (bp->hwrm_spec_code < 0x10801)
8119 		return 0;
8120 
8121 	if (BNXT_PF(bp))
8122 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8123 
8124 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8125 }
8126 
8127 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8128 {
8129 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8130 	struct hwrm_ring_aggint_qcaps_output *resp;
8131 	struct hwrm_ring_aggint_qcaps_input *req;
8132 	int rc;
8133 
8134 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8135 	coal_cap->num_cmpl_dma_aggr_max = 63;
8136 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8137 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8138 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8139 	coal_cap->int_lat_tmr_min_max = 65535;
8140 	coal_cap->int_lat_tmr_max_max = 65535;
8141 	coal_cap->num_cmpl_aggr_int_max = 65535;
8142 	coal_cap->timer_units = 80;
8143 
8144 	if (bp->hwrm_spec_code < 0x10902)
8145 		return;
8146 
8147 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8148 		return;
8149 
8150 	resp = hwrm_req_hold(bp, req);
8151 	rc = hwrm_req_send_silent(bp, req);
8152 	if (!rc) {
8153 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8154 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8155 		coal_cap->num_cmpl_dma_aggr_max =
8156 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8157 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8158 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8159 		coal_cap->cmpl_aggr_dma_tmr_max =
8160 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8161 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8162 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8163 		coal_cap->int_lat_tmr_min_max =
8164 			le16_to_cpu(resp->int_lat_tmr_min_max);
8165 		coal_cap->int_lat_tmr_max_max =
8166 			le16_to_cpu(resp->int_lat_tmr_max_max);
8167 		coal_cap->num_cmpl_aggr_int_max =
8168 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8169 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8170 	}
8171 	hwrm_req_drop(bp, req);
8172 }
8173 
8174 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8175 {
8176 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8177 
8178 	return usec * 1000 / coal_cap->timer_units;
8179 }
8180 
8181 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8182 	struct bnxt_coal *hw_coal,
8183 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8184 {
8185 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8186 	u16 val, tmr, max, flags = hw_coal->flags;
8187 	u32 cmpl_params = coal_cap->cmpl_params;
8188 
8189 	max = hw_coal->bufs_per_record * 128;
8190 	if (hw_coal->budget)
8191 		max = hw_coal->bufs_per_record * hw_coal->budget;
8192 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8193 
8194 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8195 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8196 
8197 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8198 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8199 
8200 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8201 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8202 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8203 
8204 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8205 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8206 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8207 
8208 	/* min timer set to 1/2 of interrupt timer */
8209 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8210 		val = tmr / 2;
8211 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8212 		req->int_lat_tmr_min = cpu_to_le16(val);
8213 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8214 	}
8215 
8216 	/* buf timer set to 1/4 of interrupt timer */
8217 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8218 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8219 
8220 	if (cmpl_params &
8221 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8222 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8223 		val = clamp_t(u16, tmr, 1,
8224 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8225 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8226 		req->enables |=
8227 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8228 	}
8229 
8230 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8231 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8232 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8233 	req->flags = cpu_to_le16(flags);
8234 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8235 }
8236 
8237 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8238 				   struct bnxt_coal *hw_coal)
8239 {
8240 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8241 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8242 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8243 	u32 nq_params = coal_cap->nq_params;
8244 	u16 tmr;
8245 	int rc;
8246 
8247 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8248 		return 0;
8249 
8250 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8251 	if (rc)
8252 		return rc;
8253 
8254 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8255 	req->flags =
8256 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8257 
8258 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8259 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8260 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8261 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8262 	return hwrm_req_send(bp, req);
8263 }
8264 
8265 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8266 {
8267 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8268 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8269 	struct bnxt_coal coal;
8270 	int rc;
8271 
8272 	/* Tick values in micro seconds.
8273 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8274 	 */
8275 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8276 
8277 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8278 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8279 
8280 	if (!bnapi->rx_ring)
8281 		return -ENODEV;
8282 
8283 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8284 	if (rc)
8285 		return rc;
8286 
8287 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8288 
8289 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8290 
8291 	return hwrm_req_send(bp, req_rx);
8292 }
8293 
8294 static int
8295 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8296 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8297 {
8298 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8299 
8300 	req->ring_id = cpu_to_le16(ring_id);
8301 	return hwrm_req_send(bp, req);
8302 }
8303 
8304 static int
8305 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8306 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8307 {
8308 	struct bnxt_tx_ring_info *txr;
8309 	int i, rc;
8310 
8311 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8312 		u16 ring_id;
8313 
8314 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8315 		req->ring_id = cpu_to_le16(ring_id);
8316 		rc = hwrm_req_send(bp, req);
8317 		if (rc)
8318 			return rc;
8319 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8320 			return 0;
8321 	}
8322 	return 0;
8323 }
8324 
8325 int bnxt_hwrm_set_coal(struct bnxt *bp)
8326 {
8327 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8328 	int i, rc;
8329 
8330 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8331 	if (rc)
8332 		return rc;
8333 
8334 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8335 	if (rc) {
8336 		hwrm_req_drop(bp, req_rx);
8337 		return rc;
8338 	}
8339 
8340 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8341 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8342 
8343 	hwrm_req_hold(bp, req_rx);
8344 	hwrm_req_hold(bp, req_tx);
8345 	for (i = 0; i < bp->cp_nr_rings; i++) {
8346 		struct bnxt_napi *bnapi = bp->bnapi[i];
8347 		struct bnxt_coal *hw_coal;
8348 
8349 		if (!bnapi->rx_ring)
8350 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8351 		else
8352 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8353 		if (rc)
8354 			break;
8355 
8356 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8357 			continue;
8358 
8359 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8360 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8361 			if (rc)
8362 				break;
8363 		}
8364 		if (bnapi->rx_ring)
8365 			hw_coal = &bp->rx_coal;
8366 		else
8367 			hw_coal = &bp->tx_coal;
8368 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8369 	}
8370 	hwrm_req_drop(bp, req_rx);
8371 	hwrm_req_drop(bp, req_tx);
8372 	return rc;
8373 }
8374 
8375 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8376 {
8377 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8378 	struct hwrm_stat_ctx_free_input *req;
8379 	int i;
8380 
8381 	if (!bp->bnapi)
8382 		return;
8383 
8384 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8385 		return;
8386 
8387 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8388 		return;
8389 	if (BNXT_FW_MAJ(bp) <= 20) {
8390 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8391 			hwrm_req_drop(bp, req);
8392 			return;
8393 		}
8394 		hwrm_req_hold(bp, req0);
8395 	}
8396 	hwrm_req_hold(bp, req);
8397 	for (i = 0; i < bp->cp_nr_rings; i++) {
8398 		struct bnxt_napi *bnapi = bp->bnapi[i];
8399 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8400 
8401 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8402 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8403 			if (req0) {
8404 				req0->stat_ctx_id = req->stat_ctx_id;
8405 				hwrm_req_send(bp, req0);
8406 			}
8407 			hwrm_req_send(bp, req);
8408 
8409 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8410 		}
8411 	}
8412 	hwrm_req_drop(bp, req);
8413 	if (req0)
8414 		hwrm_req_drop(bp, req0);
8415 }
8416 
8417 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8418 {
8419 	struct hwrm_stat_ctx_alloc_output *resp;
8420 	struct hwrm_stat_ctx_alloc_input *req;
8421 	int rc, i;
8422 
8423 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8424 		return 0;
8425 
8426 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8427 	if (rc)
8428 		return rc;
8429 
8430 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8431 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8432 
8433 	resp = hwrm_req_hold(bp, req);
8434 	for (i = 0; i < bp->cp_nr_rings; i++) {
8435 		struct bnxt_napi *bnapi = bp->bnapi[i];
8436 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8437 
8438 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8439 
8440 		rc = hwrm_req_send(bp, req);
8441 		if (rc)
8442 			break;
8443 
8444 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8445 
8446 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8447 	}
8448 	hwrm_req_drop(bp, req);
8449 	return rc;
8450 }
8451 
8452 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8453 {
8454 	struct hwrm_func_qcfg_output *resp;
8455 	struct hwrm_func_qcfg_input *req;
8456 	u16 flags;
8457 	int rc;
8458 
8459 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8460 	if (rc)
8461 		return rc;
8462 
8463 	req->fid = cpu_to_le16(0xffff);
8464 	resp = hwrm_req_hold(bp, req);
8465 	rc = hwrm_req_send(bp, req);
8466 	if (rc)
8467 		goto func_qcfg_exit;
8468 
8469 	flags = le16_to_cpu(resp->flags);
8470 #ifdef CONFIG_BNXT_SRIOV
8471 	if (BNXT_VF(bp)) {
8472 		struct bnxt_vf_info *vf = &bp->vf;
8473 
8474 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8475 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8476 			vf->flags |= BNXT_VF_TRUST;
8477 		else
8478 			vf->flags &= ~BNXT_VF_TRUST;
8479 	} else {
8480 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8481 	}
8482 #endif
8483 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8484 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8485 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8486 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8487 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8488 	}
8489 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8490 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8491 
8492 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8493 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8494 
8495 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8496 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8497 
8498 	switch (resp->port_partition_type) {
8499 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8500 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8501 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8502 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8503 		bp->port_partition_type = resp->port_partition_type;
8504 		break;
8505 	}
8506 	if (bp->hwrm_spec_code < 0x10707 ||
8507 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8508 		bp->br_mode = BRIDGE_MODE_VEB;
8509 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8510 		bp->br_mode = BRIDGE_MODE_VEPA;
8511 	else
8512 		bp->br_mode = BRIDGE_MODE_UNDEF;
8513 
8514 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8515 	if (!bp->max_mtu)
8516 		bp->max_mtu = BNXT_MAX_MTU;
8517 
8518 	if (bp->db_size)
8519 		goto func_qcfg_exit;
8520 
8521 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8522 	if (BNXT_CHIP_P5(bp)) {
8523 		if (BNXT_PF(bp))
8524 			bp->db_offset = DB_PF_OFFSET_P5;
8525 		else
8526 			bp->db_offset = DB_VF_OFFSET_P5;
8527 	}
8528 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8529 				 1024);
8530 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8531 	    bp->db_size <= bp->db_offset)
8532 		bp->db_size = pci_resource_len(bp->pdev, 2);
8533 
8534 func_qcfg_exit:
8535 	hwrm_req_drop(bp, req);
8536 	return rc;
8537 }
8538 
8539 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8540 				      u8 init_val, u8 init_offset,
8541 				      bool init_mask_set)
8542 {
8543 	ctxm->init_value = init_val;
8544 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8545 	if (init_mask_set)
8546 		ctxm->init_offset = init_offset * 4;
8547 	else
8548 		ctxm->init_value = 0;
8549 }
8550 
8551 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8552 {
8553 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8554 	u16 type;
8555 
8556 	for (type = 0; type < ctx_max; type++) {
8557 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8558 		int n = 1;
8559 
8560 		if (!ctxm->max_entries || ctxm->pg_info)
8561 			continue;
8562 
8563 		if (ctxm->instance_bmap)
8564 			n = hweight32(ctxm->instance_bmap);
8565 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8566 		if (!ctxm->pg_info)
8567 			return -ENOMEM;
8568 	}
8569 	return 0;
8570 }
8571 
8572 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8573 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8574 
8575 #define BNXT_CTX_INIT_VALID(flags)	\
8576 	(!!((flags) &			\
8577 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8578 
8579 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8580 {
8581 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8582 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8583 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8584 	u16 type;
8585 	int rc;
8586 
8587 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8588 	if (rc)
8589 		return rc;
8590 
8591 	if (!ctx) {
8592 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8593 		if (!ctx)
8594 			return -ENOMEM;
8595 		bp->ctx = ctx;
8596 	}
8597 
8598 	resp = hwrm_req_hold(bp, req);
8599 
8600 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8601 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8602 		u8 init_val, init_off, i;
8603 		u32 max_entries;
8604 		u16 entry_size;
8605 		__le32 *p;
8606 		u32 flags;
8607 
8608 		req->type = cpu_to_le16(type);
8609 		rc = hwrm_req_send(bp, req);
8610 		if (rc)
8611 			goto ctx_done;
8612 		flags = le32_to_cpu(resp->flags);
8613 		type = le16_to_cpu(resp->next_valid_type);
8614 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8615 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8616 			continue;
8617 		}
8618 		entry_size = le16_to_cpu(resp->entry_size);
8619 		max_entries = le32_to_cpu(resp->max_num_entries);
8620 		if (ctxm->mem_valid) {
8621 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8622 			    ctxm->entry_size != entry_size ||
8623 			    ctxm->max_entries != max_entries)
8624 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8625 			else
8626 				continue;
8627 		}
8628 		ctxm->type = le16_to_cpu(resp->type);
8629 		ctxm->entry_size = entry_size;
8630 		ctxm->flags = flags;
8631 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8632 		ctxm->entry_multiple = resp->entry_multiple;
8633 		ctxm->max_entries = max_entries;
8634 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8635 		init_val = resp->ctx_init_value;
8636 		init_off = resp->ctx_init_offset;
8637 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8638 					  BNXT_CTX_INIT_VALID(flags));
8639 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8640 					      BNXT_MAX_SPLIT_ENTRY);
8641 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8642 		     i++, p++)
8643 			ctxm->split[i] = le32_to_cpu(*p);
8644 	}
8645 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8646 
8647 ctx_done:
8648 	hwrm_req_drop(bp, req);
8649 	return rc;
8650 }
8651 
8652 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8653 {
8654 	struct hwrm_func_backing_store_qcaps_output *resp;
8655 	struct hwrm_func_backing_store_qcaps_input *req;
8656 	int rc;
8657 
8658 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8659 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8660 		return 0;
8661 
8662 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8663 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8664 
8665 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8666 	if (rc)
8667 		return rc;
8668 
8669 	resp = hwrm_req_hold(bp, req);
8670 	rc = hwrm_req_send_silent(bp, req);
8671 	if (!rc) {
8672 		struct bnxt_ctx_mem_type *ctxm;
8673 		struct bnxt_ctx_mem_info *ctx;
8674 		u8 init_val, init_idx = 0;
8675 		u16 init_mask;
8676 
8677 		ctx = bp->ctx;
8678 		if (!ctx) {
8679 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8680 			if (!ctx) {
8681 				rc = -ENOMEM;
8682 				goto ctx_err;
8683 			}
8684 			bp->ctx = ctx;
8685 		}
8686 		init_val = resp->ctx_kind_initializer;
8687 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8688 
8689 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8690 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8691 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8692 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8693 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8694 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8695 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8696 					  (init_mask & (1 << init_idx++)) != 0);
8697 
8698 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8699 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8700 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8701 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8702 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8703 					  (init_mask & (1 << init_idx++)) != 0);
8704 
8705 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8706 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8707 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8708 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8709 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8710 					  (init_mask & (1 << init_idx++)) != 0);
8711 
8712 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8713 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8714 		ctxm->max_entries = ctxm->vnic_entries +
8715 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8716 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8717 		bnxt_init_ctx_initializer(ctxm, init_val,
8718 					  resp->vnic_init_offset,
8719 					  (init_mask & (1 << init_idx++)) != 0);
8720 
8721 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8722 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8723 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8724 		bnxt_init_ctx_initializer(ctxm, init_val,
8725 					  resp->stat_init_offset,
8726 					  (init_mask & (1 << init_idx++)) != 0);
8727 
8728 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8729 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8730 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8731 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8732 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8733 		if (!ctxm->entry_multiple)
8734 			ctxm->entry_multiple = 1;
8735 
8736 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8737 
8738 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8739 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8740 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8741 		ctxm->mrav_num_entries_units =
8742 			le16_to_cpu(resp->mrav_num_entries_units);
8743 		bnxt_init_ctx_initializer(ctxm, init_val,
8744 					  resp->mrav_init_offset,
8745 					  (init_mask & (1 << init_idx++)) != 0);
8746 
8747 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8748 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8749 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8750 
8751 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8752 		if (!ctx->tqm_fp_rings_count)
8753 			ctx->tqm_fp_rings_count = bp->max_q;
8754 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8755 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8756 
8757 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8758 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8759 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8760 
8761 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8762 	} else {
8763 		rc = 0;
8764 	}
8765 ctx_err:
8766 	hwrm_req_drop(bp, req);
8767 	return rc;
8768 }
8769 
8770 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8771 				  __le64 *pg_dir)
8772 {
8773 	if (!rmem->nr_pages)
8774 		return;
8775 
8776 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8777 	if (rmem->depth >= 1) {
8778 		if (rmem->depth == 2)
8779 			*pg_attr |= 2;
8780 		else
8781 			*pg_attr |= 1;
8782 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8783 	} else {
8784 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8785 	}
8786 }
8787 
8788 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8789 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8790 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8791 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8792 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8793 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8794 
8795 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8796 {
8797 	struct hwrm_func_backing_store_cfg_input *req;
8798 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8799 	struct bnxt_ctx_pg_info *ctx_pg;
8800 	struct bnxt_ctx_mem_type *ctxm;
8801 	void **__req = (void **)&req;
8802 	u32 req_len = sizeof(*req);
8803 	__le32 *num_entries;
8804 	__le64 *pg_dir;
8805 	u32 flags = 0;
8806 	u8 *pg_attr;
8807 	u32 ena;
8808 	int rc;
8809 	int i;
8810 
8811 	if (!ctx)
8812 		return 0;
8813 
8814 	if (req_len > bp->hwrm_max_ext_req_len)
8815 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8816 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8817 	if (rc)
8818 		return rc;
8819 
8820 	req->enables = cpu_to_le32(enables);
8821 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8822 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8823 		ctx_pg = ctxm->pg_info;
8824 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8825 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8826 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8827 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8828 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8829 				      &req->qpc_pg_size_qpc_lvl,
8830 				      &req->qpc_page_dir);
8831 
8832 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8833 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8834 	}
8835 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8836 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8837 		ctx_pg = ctxm->pg_info;
8838 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8839 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8840 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8841 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8842 				      &req->srq_pg_size_srq_lvl,
8843 				      &req->srq_page_dir);
8844 	}
8845 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8846 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8847 		ctx_pg = ctxm->pg_info;
8848 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8849 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8850 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8851 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8852 				      &req->cq_pg_size_cq_lvl,
8853 				      &req->cq_page_dir);
8854 	}
8855 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8856 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8857 		ctx_pg = ctxm->pg_info;
8858 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8859 		req->vnic_num_ring_table_entries =
8860 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8861 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8862 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8863 				      &req->vnic_pg_size_vnic_lvl,
8864 				      &req->vnic_page_dir);
8865 	}
8866 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8867 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8868 		ctx_pg = ctxm->pg_info;
8869 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8870 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8871 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8872 				      &req->stat_pg_size_stat_lvl,
8873 				      &req->stat_page_dir);
8874 	}
8875 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8876 		u32 units;
8877 
8878 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8879 		ctx_pg = ctxm->pg_info;
8880 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8881 		units = ctxm->mrav_num_entries_units;
8882 		if (units) {
8883 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8884 			u32 entries;
8885 
8886 			num_mr = ctx_pg->entries - num_ah;
8887 			entries = ((num_mr / units) << 16) | (num_ah / units);
8888 			req->mrav_num_entries = cpu_to_le32(entries);
8889 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8890 		}
8891 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8892 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8893 				      &req->mrav_pg_size_mrav_lvl,
8894 				      &req->mrav_page_dir);
8895 	}
8896 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8897 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8898 		ctx_pg = ctxm->pg_info;
8899 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8900 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8901 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8902 				      &req->tim_pg_size_tim_lvl,
8903 				      &req->tim_page_dir);
8904 	}
8905 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8906 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8907 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8908 	     pg_dir = &req->tqm_sp_page_dir,
8909 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8910 	     ctx_pg = ctxm->pg_info;
8911 	     i < BNXT_MAX_TQM_RINGS;
8912 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8913 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8914 		if (!(enables & ena))
8915 			continue;
8916 
8917 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8918 		*num_entries = cpu_to_le32(ctx_pg->entries);
8919 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8920 	}
8921 	req->flags = cpu_to_le32(flags);
8922 	return hwrm_req_send(bp, req);
8923 }
8924 
8925 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8926 				  struct bnxt_ctx_pg_info *ctx_pg)
8927 {
8928 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8929 
8930 	rmem->page_size = BNXT_PAGE_SIZE;
8931 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8932 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8933 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8934 	if (rmem->depth >= 1)
8935 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8936 	return bnxt_alloc_ring(bp, rmem);
8937 }
8938 
8939 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8940 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8941 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8942 {
8943 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8944 	int rc;
8945 
8946 	if (!mem_size)
8947 		return -EINVAL;
8948 
8949 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8950 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8951 		ctx_pg->nr_pages = 0;
8952 		return -EINVAL;
8953 	}
8954 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8955 		int nr_tbls, i;
8956 
8957 		rmem->depth = 2;
8958 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8959 					     GFP_KERNEL);
8960 		if (!ctx_pg->ctx_pg_tbl)
8961 			return -ENOMEM;
8962 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8963 		rmem->nr_pages = nr_tbls;
8964 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8965 		if (rc)
8966 			return rc;
8967 		for (i = 0; i < nr_tbls; i++) {
8968 			struct bnxt_ctx_pg_info *pg_tbl;
8969 
8970 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8971 			if (!pg_tbl)
8972 				return -ENOMEM;
8973 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8974 			rmem = &pg_tbl->ring_mem;
8975 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8976 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8977 			rmem->depth = 1;
8978 			rmem->nr_pages = MAX_CTX_PAGES;
8979 			rmem->ctx_mem = ctxm;
8980 			if (i == (nr_tbls - 1)) {
8981 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8982 
8983 				if (rem)
8984 					rmem->nr_pages = rem;
8985 			}
8986 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8987 			if (rc)
8988 				break;
8989 		}
8990 	} else {
8991 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8992 		if (rmem->nr_pages > 1 || depth)
8993 			rmem->depth = 1;
8994 		rmem->ctx_mem = ctxm;
8995 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8996 	}
8997 	return rc;
8998 }
8999 
9000 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
9001 				    struct bnxt_ctx_pg_info *ctx_pg,
9002 				    void *buf, size_t offset, size_t head,
9003 				    size_t tail)
9004 {
9005 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9006 	size_t nr_pages = ctx_pg->nr_pages;
9007 	int page_size = rmem->page_size;
9008 	size_t len = 0, total_len = 0;
9009 	u16 depth = rmem->depth;
9010 
9011 	tail %= nr_pages * page_size;
9012 	do {
9013 		if (depth > 1) {
9014 			int i = head / (page_size * MAX_CTX_PAGES);
9015 			struct bnxt_ctx_pg_info *pg_tbl;
9016 
9017 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9018 			rmem = &pg_tbl->ring_mem;
9019 		}
9020 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9021 		head += len;
9022 		offset += len;
9023 		total_len += len;
9024 		if (head >= nr_pages * page_size)
9025 			head = 0;
9026 	} while (head != tail);
9027 	return total_len;
9028 }
9029 
9030 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9031 				  struct bnxt_ctx_pg_info *ctx_pg)
9032 {
9033 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9034 
9035 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9036 	    ctx_pg->ctx_pg_tbl) {
9037 		int i, nr_tbls = rmem->nr_pages;
9038 
9039 		for (i = 0; i < nr_tbls; i++) {
9040 			struct bnxt_ctx_pg_info *pg_tbl;
9041 			struct bnxt_ring_mem_info *rmem2;
9042 
9043 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9044 			if (!pg_tbl)
9045 				continue;
9046 			rmem2 = &pg_tbl->ring_mem;
9047 			bnxt_free_ring(bp, rmem2);
9048 			ctx_pg->ctx_pg_arr[i] = NULL;
9049 			kfree(pg_tbl);
9050 			ctx_pg->ctx_pg_tbl[i] = NULL;
9051 		}
9052 		kfree(ctx_pg->ctx_pg_tbl);
9053 		ctx_pg->ctx_pg_tbl = NULL;
9054 	}
9055 	bnxt_free_ring(bp, rmem);
9056 	ctx_pg->nr_pages = 0;
9057 }
9058 
9059 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9060 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9061 				   u8 pg_lvl)
9062 {
9063 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9064 	int i, rc = 0, n = 1;
9065 	u32 mem_size;
9066 
9067 	if (!ctxm->entry_size || !ctx_pg)
9068 		return -EINVAL;
9069 	if (ctxm->instance_bmap)
9070 		n = hweight32(ctxm->instance_bmap);
9071 	if (ctxm->entry_multiple)
9072 		entries = roundup(entries, ctxm->entry_multiple);
9073 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9074 	mem_size = entries * ctxm->entry_size;
9075 	for (i = 0; i < n && !rc; i++) {
9076 		ctx_pg[i].entries = entries;
9077 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9078 					    ctxm->init_value ? ctxm : NULL);
9079 	}
9080 	if (!rc)
9081 		ctxm->mem_valid = 1;
9082 	return rc;
9083 }
9084 
9085 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9086 					       struct bnxt_ctx_mem_type *ctxm,
9087 					       bool last)
9088 {
9089 	struct hwrm_func_backing_store_cfg_v2_input *req;
9090 	u32 instance_bmap = ctxm->instance_bmap;
9091 	int i, j, rc = 0, n = 1;
9092 	__le32 *p;
9093 
9094 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9095 		return 0;
9096 
9097 	if (instance_bmap)
9098 		n = hweight32(ctxm->instance_bmap);
9099 	else
9100 		instance_bmap = 1;
9101 
9102 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9103 	if (rc)
9104 		return rc;
9105 	hwrm_req_hold(bp, req);
9106 	req->type = cpu_to_le16(ctxm->type);
9107 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9108 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9109 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9110 		struct bnxt_bs_trace_info *bs_trace;
9111 		u32 enables;
9112 
9113 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9114 		req->enables = cpu_to_le32(enables);
9115 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9116 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9117 	}
9118 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9119 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9120 		p[i] = cpu_to_le32(ctxm->split[i]);
9121 	for (i = 0, j = 0; j < n && !rc; i++) {
9122 		struct bnxt_ctx_pg_info *ctx_pg;
9123 
9124 		if (!(instance_bmap & (1 << i)))
9125 			continue;
9126 		req->instance = cpu_to_le16(i);
9127 		ctx_pg = &ctxm->pg_info[j++];
9128 		if (!ctx_pg->entries)
9129 			continue;
9130 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9131 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9132 				      &req->page_size_pbl_level,
9133 				      &req->page_dir);
9134 		if (last && j == n)
9135 			req->flags =
9136 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9137 		rc = hwrm_req_send(bp, req);
9138 	}
9139 	hwrm_req_drop(bp, req);
9140 	return rc;
9141 }
9142 
9143 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9144 {
9145 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9146 	struct bnxt_ctx_mem_type *ctxm;
9147 	u16 last_type = BNXT_CTX_INV;
9148 	int rc = 0;
9149 	u16 type;
9150 
9151 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9152 		ctxm = &ctx->ctx_arr[type];
9153 		if (!bnxt_bs_trace_avail(bp, type))
9154 			continue;
9155 		if (!ctxm->mem_valid) {
9156 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9157 						     ctxm->max_entries, 1);
9158 			if (rc) {
9159 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9160 					    type);
9161 				continue;
9162 			}
9163 			bnxt_bs_trace_init(bp, ctxm);
9164 		}
9165 		last_type = type;
9166 	}
9167 
9168 	if (last_type == BNXT_CTX_INV) {
9169 		if (!ena)
9170 			return 0;
9171 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9172 			last_type = BNXT_CTX_MAX - 1;
9173 		else
9174 			last_type = BNXT_CTX_L2_MAX - 1;
9175 	}
9176 	ctx->ctx_arr[last_type].last = 1;
9177 
9178 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9179 		ctxm = &ctx->ctx_arr[type];
9180 
9181 		if (!ctxm->mem_valid)
9182 			continue;
9183 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9184 		if (rc)
9185 			return rc;
9186 	}
9187 	return 0;
9188 }
9189 
9190 /**
9191  * __bnxt_copy_ctx_mem - copy host context memory
9192  * @bp: The driver context
9193  * @ctxm: The pointer to the context memory type
9194  * @buf: The destination buffer or NULL to just obtain the length
9195  * @offset: The buffer offset to copy the data to
9196  * @head: The head offset of context memory to copy from
9197  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9198  *
9199  * This function is called for debugging purposes to dump the host context
9200  * used by the chip.
9201  *
9202  * Return: Length of memory copied
9203  */
9204 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9205 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9206 				  size_t offset, size_t head, size_t tail)
9207 {
9208 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9209 	size_t len = 0, total_len = 0;
9210 	int i, n = 1;
9211 
9212 	if (!ctx_pg)
9213 		return 0;
9214 
9215 	if (ctxm->instance_bmap)
9216 		n = hweight32(ctxm->instance_bmap);
9217 	for (i = 0; i < n; i++) {
9218 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9219 					    tail);
9220 		offset += len;
9221 		total_len += len;
9222 	}
9223 	return total_len;
9224 }
9225 
9226 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9227 			 void *buf, size_t offset)
9228 {
9229 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9230 
9231 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9232 }
9233 
9234 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9235 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9236 {
9237 	struct bnxt_ctx_pg_info *ctx_pg;
9238 	int i, n = 1;
9239 
9240 	ctxm->last = 0;
9241 
9242 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9243 		return;
9244 
9245 	ctx_pg = ctxm->pg_info;
9246 	if (ctx_pg) {
9247 		if (ctxm->instance_bmap)
9248 			n = hweight32(ctxm->instance_bmap);
9249 		for (i = 0; i < n; i++)
9250 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9251 
9252 		kfree(ctx_pg);
9253 		ctxm->pg_info = NULL;
9254 		ctxm->mem_valid = 0;
9255 	}
9256 	memset(ctxm, 0, sizeof(*ctxm));
9257 }
9258 
9259 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9260 {
9261 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9262 	u16 type;
9263 
9264 	if (!ctx)
9265 		return;
9266 
9267 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9268 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9269 
9270 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9271 	if (force) {
9272 		kfree(ctx);
9273 		bp->ctx = NULL;
9274 	}
9275 }
9276 
9277 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9278 {
9279 	struct bnxt_ctx_mem_type *ctxm;
9280 	struct bnxt_ctx_mem_info *ctx;
9281 	u32 l2_qps, qp1_qps, max_qps;
9282 	u32 ena, entries_sp, entries;
9283 	u32 srqs, max_srqs, min;
9284 	u32 num_mr, num_ah;
9285 	u32 extra_srqs = 0;
9286 	u32 extra_qps = 0;
9287 	u32 fast_qpmd_qps;
9288 	u8 pg_lvl = 1;
9289 	int i, rc;
9290 
9291 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9292 	if (rc) {
9293 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9294 			   rc);
9295 		return rc;
9296 	}
9297 	ctx = bp->ctx;
9298 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9299 		return 0;
9300 
9301 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9302 	l2_qps = ctxm->qp_l2_entries;
9303 	qp1_qps = ctxm->qp_qp1_entries;
9304 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9305 	max_qps = ctxm->max_entries;
9306 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9307 	srqs = ctxm->srq_l2_entries;
9308 	max_srqs = ctxm->max_entries;
9309 	ena = 0;
9310 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9311 		pg_lvl = 2;
9312 		if (BNXT_SW_RES_LMT(bp)) {
9313 			extra_qps = max_qps - l2_qps - qp1_qps;
9314 			extra_srqs = max_srqs - srqs;
9315 		} else {
9316 			extra_qps = min_t(u32, 65536,
9317 					  max_qps - l2_qps - qp1_qps);
9318 			/* allocate extra qps if fw supports RoCE fast qp
9319 			 * destroy feature
9320 			 */
9321 			extra_qps += fast_qpmd_qps;
9322 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9323 		}
9324 		if (fast_qpmd_qps)
9325 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9326 	}
9327 
9328 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9329 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9330 				     pg_lvl);
9331 	if (rc)
9332 		return rc;
9333 
9334 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9335 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9336 	if (rc)
9337 		return rc;
9338 
9339 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9340 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9341 				     extra_qps * 2, pg_lvl);
9342 	if (rc)
9343 		return rc;
9344 
9345 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9346 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9347 	if (rc)
9348 		return rc;
9349 
9350 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9351 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9352 	if (rc)
9353 		return rc;
9354 
9355 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9356 		goto skip_rdma;
9357 
9358 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9359 	if (BNXT_SW_RES_LMT(bp) &&
9360 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9361 		num_ah = ctxm->mrav_av_entries;
9362 		num_mr = ctxm->max_entries - num_ah;
9363 	} else {
9364 		/* 128K extra is needed to accommodate static AH context
9365 		 * allocation by f/w.
9366 		 */
9367 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9368 		num_ah = min_t(u32, num_mr, 1024 * 128);
9369 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9370 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9371 			ctxm->mrav_av_entries = num_ah;
9372 	}
9373 
9374 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9375 	if (rc)
9376 		return rc;
9377 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9378 
9379 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9380 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9381 	if (rc)
9382 		return rc;
9383 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9384 
9385 skip_rdma:
9386 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9387 	min = ctxm->min_entries;
9388 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9389 		     2 * (extra_qps + qp1_qps) + min;
9390 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9391 	if (rc)
9392 		return rc;
9393 
9394 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9395 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9396 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9397 	if (rc)
9398 		return rc;
9399 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9400 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9401 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9402 
9403 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9404 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9405 	else
9406 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9407 	if (rc) {
9408 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9409 			   rc);
9410 		return rc;
9411 	}
9412 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9413 	return 0;
9414 }
9415 
9416 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9417 {
9418 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9419 	u16 page_attr;
9420 	int rc;
9421 
9422 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9423 		return 0;
9424 
9425 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9426 	if (rc)
9427 		return rc;
9428 
9429 	if (BNXT_PAGE_SIZE == 0x2000)
9430 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9431 	else if (BNXT_PAGE_SIZE == 0x10000)
9432 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9433 	else
9434 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9435 	req->pg_size_lvl = cpu_to_le16(page_attr |
9436 				       bp->fw_crash_mem->ring_mem.depth);
9437 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9438 	req->size = cpu_to_le32(bp->fw_crash_len);
9439 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9440 	return hwrm_req_send(bp, req);
9441 }
9442 
9443 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9444 {
9445 	if (bp->fw_crash_mem) {
9446 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9447 		kfree(bp->fw_crash_mem);
9448 		bp->fw_crash_mem = NULL;
9449 	}
9450 }
9451 
9452 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9453 {
9454 	u32 mem_size = 0;
9455 	int rc;
9456 
9457 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9458 		return 0;
9459 
9460 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9461 	if (rc)
9462 		return rc;
9463 
9464 	mem_size = round_up(mem_size, 4);
9465 
9466 	/* keep and use the existing pages */
9467 	if (bp->fw_crash_mem &&
9468 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9469 		goto alloc_done;
9470 
9471 	if (bp->fw_crash_mem)
9472 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9473 	else
9474 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9475 					   GFP_KERNEL);
9476 	if (!bp->fw_crash_mem)
9477 		return -ENOMEM;
9478 
9479 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9480 	if (rc) {
9481 		bnxt_free_crash_dump_mem(bp);
9482 		return rc;
9483 	}
9484 
9485 alloc_done:
9486 	bp->fw_crash_len = mem_size;
9487 	return 0;
9488 }
9489 
9490 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9491 {
9492 	struct hwrm_func_resource_qcaps_output *resp;
9493 	struct hwrm_func_resource_qcaps_input *req;
9494 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9495 	int rc;
9496 
9497 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9498 	if (rc)
9499 		return rc;
9500 
9501 	req->fid = cpu_to_le16(0xffff);
9502 	resp = hwrm_req_hold(bp, req);
9503 	rc = hwrm_req_send_silent(bp, req);
9504 	if (rc)
9505 		goto hwrm_func_resc_qcaps_exit;
9506 
9507 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9508 	if (!all)
9509 		goto hwrm_func_resc_qcaps_exit;
9510 
9511 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9512 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9513 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9514 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9515 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9516 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9517 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9518 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9519 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9520 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9521 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9522 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9523 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9524 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9525 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9526 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9527 
9528 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9529 		u16 max_msix = le16_to_cpu(resp->max_msix);
9530 
9531 		hw_resc->max_nqs = max_msix;
9532 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9533 	}
9534 
9535 	if (BNXT_PF(bp)) {
9536 		struct bnxt_pf_info *pf = &bp->pf;
9537 
9538 		pf->vf_resv_strategy =
9539 			le16_to_cpu(resp->vf_reservation_strategy);
9540 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9541 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9542 	}
9543 hwrm_func_resc_qcaps_exit:
9544 	hwrm_req_drop(bp, req);
9545 	return rc;
9546 }
9547 
9548 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9549 {
9550 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9551 	struct hwrm_port_mac_ptp_qcfg_input *req;
9552 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9553 	u8 flags;
9554 	int rc;
9555 
9556 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9557 		rc = -ENODEV;
9558 		goto no_ptp;
9559 	}
9560 
9561 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9562 	if (rc)
9563 		goto no_ptp;
9564 
9565 	req->port_id = cpu_to_le16(bp->pf.port_id);
9566 	resp = hwrm_req_hold(bp, req);
9567 	rc = hwrm_req_send(bp, req);
9568 	if (rc)
9569 		goto exit;
9570 
9571 	flags = resp->flags;
9572 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9573 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9574 		rc = -ENODEV;
9575 		goto exit;
9576 	}
9577 	if (!ptp) {
9578 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9579 		if (!ptp) {
9580 			rc = -ENOMEM;
9581 			goto exit;
9582 		}
9583 		ptp->bp = bp;
9584 		bp->ptp_cfg = ptp;
9585 	}
9586 
9587 	if (flags &
9588 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9589 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9590 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9591 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9592 	} else if (BNXT_CHIP_P5(bp)) {
9593 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9594 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9595 	} else {
9596 		rc = -ENODEV;
9597 		goto exit;
9598 	}
9599 	ptp->rtc_configured =
9600 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9601 	rc = bnxt_ptp_init(bp);
9602 	if (rc)
9603 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9604 exit:
9605 	hwrm_req_drop(bp, req);
9606 	if (!rc)
9607 		return 0;
9608 
9609 no_ptp:
9610 	bnxt_ptp_clear(bp);
9611 	kfree(ptp);
9612 	bp->ptp_cfg = NULL;
9613 	return rc;
9614 }
9615 
9616 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9617 {
9618 	struct hwrm_func_qcaps_output *resp;
9619 	struct hwrm_func_qcaps_input *req;
9620 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9621 	u32 flags, flags_ext, flags_ext2;
9622 	int rc;
9623 
9624 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9625 	if (rc)
9626 		return rc;
9627 
9628 	req->fid = cpu_to_le16(0xffff);
9629 	resp = hwrm_req_hold(bp, req);
9630 	rc = hwrm_req_send(bp, req);
9631 	if (rc)
9632 		goto hwrm_func_qcaps_exit;
9633 
9634 	flags = le32_to_cpu(resp->flags);
9635 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9636 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9637 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9638 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9639 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9640 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9641 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9642 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9643 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9644 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9645 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9646 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9647 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9648 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9649 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9650 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9651 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9652 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9653 
9654 	flags_ext = le32_to_cpu(resp->flags_ext);
9655 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9656 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9657 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9658 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9659 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9660 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9661 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9662 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9663 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9664 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9665 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9666 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9667 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9668 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9669 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9670 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9671 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9672 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9673 
9674 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9675 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9676 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9677 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9678 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9679 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9680 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9681 	if (flags_ext2 &
9682 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9683 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9684 	if (BNXT_PF(bp) &&
9685 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9686 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9687 
9688 	bp->tx_push_thresh = 0;
9689 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9690 	    BNXT_FW_MAJ(bp) > 217)
9691 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9692 
9693 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9694 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9695 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9696 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9697 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9698 	if (!hw_resc->max_hw_ring_grps)
9699 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9700 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9701 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9702 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9703 
9704 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9705 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9706 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9707 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9708 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9709 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9710 
9711 	if (BNXT_PF(bp)) {
9712 		struct bnxt_pf_info *pf = &bp->pf;
9713 
9714 		pf->fw_fid = le16_to_cpu(resp->fid);
9715 		pf->port_id = le16_to_cpu(resp->port_id);
9716 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9717 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9718 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9719 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9720 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9721 			bp->flags |= BNXT_FLAG_WOL_CAP;
9722 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9723 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9724 		} else {
9725 			bnxt_ptp_clear(bp);
9726 			kfree(bp->ptp_cfg);
9727 			bp->ptp_cfg = NULL;
9728 		}
9729 	} else {
9730 #ifdef CONFIG_BNXT_SRIOV
9731 		struct bnxt_vf_info *vf = &bp->vf;
9732 
9733 		vf->fw_fid = le16_to_cpu(resp->fid);
9734 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9735 #endif
9736 	}
9737 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9738 
9739 hwrm_func_qcaps_exit:
9740 	hwrm_req_drop(bp, req);
9741 	return rc;
9742 }
9743 
9744 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9745 {
9746 	struct hwrm_dbg_qcaps_output *resp;
9747 	struct hwrm_dbg_qcaps_input *req;
9748 	int rc;
9749 
9750 	bp->fw_dbg_cap = 0;
9751 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9752 		return;
9753 
9754 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9755 	if (rc)
9756 		return;
9757 
9758 	req->fid = cpu_to_le16(0xffff);
9759 	resp = hwrm_req_hold(bp, req);
9760 	rc = hwrm_req_send(bp, req);
9761 	if (rc)
9762 		goto hwrm_dbg_qcaps_exit;
9763 
9764 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9765 
9766 hwrm_dbg_qcaps_exit:
9767 	hwrm_req_drop(bp, req);
9768 }
9769 
9770 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9771 
9772 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9773 {
9774 	int rc;
9775 
9776 	rc = __bnxt_hwrm_func_qcaps(bp);
9777 	if (rc)
9778 		return rc;
9779 
9780 	bnxt_hwrm_dbg_qcaps(bp);
9781 
9782 	rc = bnxt_hwrm_queue_qportcfg(bp);
9783 	if (rc) {
9784 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9785 		return rc;
9786 	}
9787 	if (bp->hwrm_spec_code >= 0x10803) {
9788 		rc = bnxt_alloc_ctx_mem(bp);
9789 		if (rc)
9790 			return rc;
9791 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9792 		if (!rc)
9793 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9794 	}
9795 	return 0;
9796 }
9797 
9798 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9799 {
9800 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9801 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9802 	u32 flags;
9803 	int rc;
9804 
9805 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9806 		return 0;
9807 
9808 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9809 	if (rc)
9810 		return rc;
9811 
9812 	resp = hwrm_req_hold(bp, req);
9813 	rc = hwrm_req_send(bp, req);
9814 	if (rc)
9815 		goto hwrm_cfa_adv_qcaps_exit;
9816 
9817 	flags = le32_to_cpu(resp->flags);
9818 	if (flags &
9819 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9820 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9821 
9822 	if (flags &
9823 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9824 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9825 
9826 	if (flags &
9827 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9828 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9829 
9830 hwrm_cfa_adv_qcaps_exit:
9831 	hwrm_req_drop(bp, req);
9832 	return rc;
9833 }
9834 
9835 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9836 {
9837 	if (bp->fw_health)
9838 		return 0;
9839 
9840 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9841 	if (!bp->fw_health)
9842 		return -ENOMEM;
9843 
9844 	mutex_init(&bp->fw_health->lock);
9845 	return 0;
9846 }
9847 
9848 static int bnxt_alloc_fw_health(struct bnxt *bp)
9849 {
9850 	int rc;
9851 
9852 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9853 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9854 		return 0;
9855 
9856 	rc = __bnxt_alloc_fw_health(bp);
9857 	if (rc) {
9858 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9859 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9860 		return rc;
9861 	}
9862 
9863 	return 0;
9864 }
9865 
9866 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9867 {
9868 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9869 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9870 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9871 }
9872 
9873 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9874 {
9875 	struct bnxt_fw_health *fw_health = bp->fw_health;
9876 	u32 reg_type;
9877 
9878 	if (!fw_health)
9879 		return;
9880 
9881 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9882 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9883 		fw_health->status_reliable = false;
9884 
9885 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9886 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9887 		fw_health->resets_reliable = false;
9888 }
9889 
9890 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9891 {
9892 	void __iomem *hs;
9893 	u32 status_loc;
9894 	u32 reg_type;
9895 	u32 sig;
9896 
9897 	if (bp->fw_health)
9898 		bp->fw_health->status_reliable = false;
9899 
9900 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9901 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9902 
9903 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9904 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9905 		if (!bp->chip_num) {
9906 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9907 			bp->chip_num = readl(bp->bar0 +
9908 					     BNXT_FW_HEALTH_WIN_BASE +
9909 					     BNXT_GRC_REG_CHIP_NUM);
9910 		}
9911 		if (!BNXT_CHIP_P5_PLUS(bp))
9912 			return;
9913 
9914 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9915 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9916 	} else {
9917 		status_loc = readl(hs + offsetof(struct hcomm_status,
9918 						 fw_status_loc));
9919 	}
9920 
9921 	if (__bnxt_alloc_fw_health(bp)) {
9922 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9923 		return;
9924 	}
9925 
9926 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9927 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9928 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9929 		__bnxt_map_fw_health_reg(bp, status_loc);
9930 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9931 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9932 	}
9933 
9934 	bp->fw_health->status_reliable = true;
9935 }
9936 
9937 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9938 {
9939 	struct bnxt_fw_health *fw_health = bp->fw_health;
9940 	u32 reg_base = 0xffffffff;
9941 	int i;
9942 
9943 	bp->fw_health->status_reliable = false;
9944 	bp->fw_health->resets_reliable = false;
9945 	/* Only pre-map the monitoring GRC registers using window 3 */
9946 	for (i = 0; i < 4; i++) {
9947 		u32 reg = fw_health->regs[i];
9948 
9949 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9950 			continue;
9951 		if (reg_base == 0xffffffff)
9952 			reg_base = reg & BNXT_GRC_BASE_MASK;
9953 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9954 			return -ERANGE;
9955 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9956 	}
9957 	bp->fw_health->status_reliable = true;
9958 	bp->fw_health->resets_reliable = true;
9959 	if (reg_base == 0xffffffff)
9960 		return 0;
9961 
9962 	__bnxt_map_fw_health_reg(bp, reg_base);
9963 	return 0;
9964 }
9965 
9966 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9967 {
9968 	if (!bp->fw_health)
9969 		return;
9970 
9971 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9972 		bp->fw_health->status_reliable = true;
9973 		bp->fw_health->resets_reliable = true;
9974 	} else {
9975 		bnxt_try_map_fw_health_reg(bp);
9976 	}
9977 }
9978 
9979 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9980 {
9981 	struct bnxt_fw_health *fw_health = bp->fw_health;
9982 	struct hwrm_error_recovery_qcfg_output *resp;
9983 	struct hwrm_error_recovery_qcfg_input *req;
9984 	int rc, i;
9985 
9986 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9987 		return 0;
9988 
9989 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9990 	if (rc)
9991 		return rc;
9992 
9993 	resp = hwrm_req_hold(bp, req);
9994 	rc = hwrm_req_send(bp, req);
9995 	if (rc)
9996 		goto err_recovery_out;
9997 	fw_health->flags = le32_to_cpu(resp->flags);
9998 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9999 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
10000 		rc = -EINVAL;
10001 		goto err_recovery_out;
10002 	}
10003 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
10004 	fw_health->master_func_wait_dsecs =
10005 		le32_to_cpu(resp->master_func_wait_period);
10006 	fw_health->normal_func_wait_dsecs =
10007 		le32_to_cpu(resp->normal_func_wait_period);
10008 	fw_health->post_reset_wait_dsecs =
10009 		le32_to_cpu(resp->master_func_wait_period_after_reset);
10010 	fw_health->post_reset_max_wait_dsecs =
10011 		le32_to_cpu(resp->max_bailout_time_after_reset);
10012 	fw_health->regs[BNXT_FW_HEALTH_REG] =
10013 		le32_to_cpu(resp->fw_health_status_reg);
10014 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
10015 		le32_to_cpu(resp->fw_heartbeat_reg);
10016 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10017 		le32_to_cpu(resp->fw_reset_cnt_reg);
10018 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10019 		le32_to_cpu(resp->reset_inprogress_reg);
10020 	fw_health->fw_reset_inprog_reg_mask =
10021 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10022 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10023 	if (fw_health->fw_reset_seq_cnt >= 16) {
10024 		rc = -EINVAL;
10025 		goto err_recovery_out;
10026 	}
10027 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10028 		fw_health->fw_reset_seq_regs[i] =
10029 			le32_to_cpu(resp->reset_reg[i]);
10030 		fw_health->fw_reset_seq_vals[i] =
10031 			le32_to_cpu(resp->reset_reg_val[i]);
10032 		fw_health->fw_reset_seq_delay_msec[i] =
10033 			resp->delay_after_reset[i];
10034 	}
10035 err_recovery_out:
10036 	hwrm_req_drop(bp, req);
10037 	if (!rc)
10038 		rc = bnxt_map_fw_health_regs(bp);
10039 	if (rc)
10040 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10041 	return rc;
10042 }
10043 
10044 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10045 {
10046 	struct hwrm_func_reset_input *req;
10047 	int rc;
10048 
10049 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10050 	if (rc)
10051 		return rc;
10052 
10053 	req->enables = 0;
10054 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10055 	return hwrm_req_send(bp, req);
10056 }
10057 
10058 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10059 {
10060 	struct hwrm_nvm_get_dev_info_output nvm_info;
10061 
10062 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10063 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10064 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10065 			 nvm_info.nvm_cfg_ver_upd);
10066 }
10067 
10068 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10069 {
10070 	struct hwrm_queue_qportcfg_output *resp;
10071 	struct hwrm_queue_qportcfg_input *req;
10072 	u8 i, j, *qptr;
10073 	bool no_rdma;
10074 	int rc = 0;
10075 
10076 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10077 	if (rc)
10078 		return rc;
10079 
10080 	resp = hwrm_req_hold(bp, req);
10081 	rc = hwrm_req_send(bp, req);
10082 	if (rc)
10083 		goto qportcfg_exit;
10084 
10085 	if (!resp->max_configurable_queues) {
10086 		rc = -EINVAL;
10087 		goto qportcfg_exit;
10088 	}
10089 	bp->max_tc = resp->max_configurable_queues;
10090 	bp->max_lltc = resp->max_configurable_lossless_queues;
10091 	if (bp->max_tc > BNXT_MAX_QUEUE)
10092 		bp->max_tc = BNXT_MAX_QUEUE;
10093 
10094 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10095 	qptr = &resp->queue_id0;
10096 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10097 		bp->q_info[j].queue_id = *qptr;
10098 		bp->q_ids[i] = *qptr++;
10099 		bp->q_info[j].queue_profile = *qptr++;
10100 		bp->tc_to_qidx[j] = j;
10101 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10102 		    (no_rdma && BNXT_PF(bp)))
10103 			j++;
10104 	}
10105 	bp->max_q = bp->max_tc;
10106 	bp->max_tc = max_t(u8, j, 1);
10107 
10108 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10109 		bp->max_tc = 1;
10110 
10111 	if (bp->max_lltc > bp->max_tc)
10112 		bp->max_lltc = bp->max_tc;
10113 
10114 qportcfg_exit:
10115 	hwrm_req_drop(bp, req);
10116 	return rc;
10117 }
10118 
10119 static int bnxt_hwrm_poll(struct bnxt *bp)
10120 {
10121 	struct hwrm_ver_get_input *req;
10122 	int rc;
10123 
10124 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10125 	if (rc)
10126 		return rc;
10127 
10128 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10129 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10130 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10131 
10132 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10133 	rc = hwrm_req_send(bp, req);
10134 	return rc;
10135 }
10136 
10137 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10138 {
10139 	struct hwrm_ver_get_output *resp;
10140 	struct hwrm_ver_get_input *req;
10141 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10142 	u32 dev_caps_cfg, hwrm_ver;
10143 	int rc, len, max_tmo_secs;
10144 
10145 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10146 	if (rc)
10147 		return rc;
10148 
10149 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10150 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10151 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10152 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10153 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10154 
10155 	resp = hwrm_req_hold(bp, req);
10156 	rc = hwrm_req_send(bp, req);
10157 	if (rc)
10158 		goto hwrm_ver_get_exit;
10159 
10160 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10161 
10162 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10163 			     resp->hwrm_intf_min_8b << 8 |
10164 			     resp->hwrm_intf_upd_8b;
10165 	if (resp->hwrm_intf_maj_8b < 1) {
10166 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10167 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10168 			    resp->hwrm_intf_upd_8b);
10169 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10170 	}
10171 
10172 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10173 			HWRM_VERSION_UPDATE;
10174 
10175 	if (bp->hwrm_spec_code > hwrm_ver)
10176 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10177 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10178 			 HWRM_VERSION_UPDATE);
10179 	else
10180 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10181 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10182 			 resp->hwrm_intf_upd_8b);
10183 
10184 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10185 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10186 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10187 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10188 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10189 		len = FW_VER_STR_LEN;
10190 	} else {
10191 		fw_maj = resp->hwrm_fw_maj_8b;
10192 		fw_min = resp->hwrm_fw_min_8b;
10193 		fw_bld = resp->hwrm_fw_bld_8b;
10194 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10195 		len = BC_HWRM_STR_LEN;
10196 	}
10197 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10198 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10199 		 fw_rsv);
10200 
10201 	if (strlen(resp->active_pkg_name)) {
10202 		int fw_ver_len = strlen(bp->fw_ver_str);
10203 
10204 		snprintf(bp->fw_ver_str + fw_ver_len,
10205 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10206 			 resp->active_pkg_name);
10207 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10208 	}
10209 
10210 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10211 	if (!bp->hwrm_cmd_timeout)
10212 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10213 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10214 	if (!bp->hwrm_cmd_max_timeout)
10215 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10216 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10217 #ifdef CONFIG_DETECT_HUNG_TASK
10218 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10219 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10220 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10221 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10222 	}
10223 #endif
10224 
10225 	if (resp->hwrm_intf_maj_8b >= 1) {
10226 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10227 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10228 	}
10229 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10230 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10231 
10232 	bp->chip_num = le16_to_cpu(resp->chip_num);
10233 	bp->chip_rev = resp->chip_rev;
10234 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10235 	    !resp->chip_metal)
10236 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10237 
10238 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10239 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10240 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10241 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10242 
10243 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10244 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10245 
10246 	if (dev_caps_cfg &
10247 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10248 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10249 
10250 	if (dev_caps_cfg &
10251 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10252 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10253 
10254 	if (dev_caps_cfg &
10255 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10256 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10257 
10258 hwrm_ver_get_exit:
10259 	hwrm_req_drop(bp, req);
10260 	return rc;
10261 }
10262 
10263 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10264 {
10265 	struct hwrm_fw_set_time_input *req;
10266 	struct tm tm;
10267 	time64_t now = ktime_get_real_seconds();
10268 	int rc;
10269 
10270 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10271 	    bp->hwrm_spec_code < 0x10400)
10272 		return -EOPNOTSUPP;
10273 
10274 	time64_to_tm(now, 0, &tm);
10275 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10276 	if (rc)
10277 		return rc;
10278 
10279 	req->year = cpu_to_le16(1900 + tm.tm_year);
10280 	req->month = 1 + tm.tm_mon;
10281 	req->day = tm.tm_mday;
10282 	req->hour = tm.tm_hour;
10283 	req->minute = tm.tm_min;
10284 	req->second = tm.tm_sec;
10285 	return hwrm_req_send(bp, req);
10286 }
10287 
10288 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10289 {
10290 	u64 sw_tmp;
10291 
10292 	hw &= mask;
10293 	sw_tmp = (*sw & ~mask) | hw;
10294 	if (hw < (*sw & mask))
10295 		sw_tmp += mask + 1;
10296 	WRITE_ONCE(*sw, sw_tmp);
10297 }
10298 
10299 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10300 				    int count, bool ignore_zero)
10301 {
10302 	int i;
10303 
10304 	for (i = 0; i < count; i++) {
10305 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10306 
10307 		if (ignore_zero && !hw)
10308 			continue;
10309 
10310 		if (masks[i] == -1ULL)
10311 			sw_stats[i] = hw;
10312 		else
10313 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10314 	}
10315 }
10316 
10317 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10318 {
10319 	if (!stats->hw_stats)
10320 		return;
10321 
10322 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10323 				stats->hw_masks, stats->len / 8, false);
10324 }
10325 
10326 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10327 {
10328 	struct bnxt_stats_mem *ring0_stats;
10329 	bool ignore_zero = false;
10330 	int i;
10331 
10332 	/* Chip bug.  Counter intermittently becomes 0. */
10333 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10334 		ignore_zero = true;
10335 
10336 	for (i = 0; i < bp->cp_nr_rings; i++) {
10337 		struct bnxt_napi *bnapi = bp->bnapi[i];
10338 		struct bnxt_cp_ring_info *cpr;
10339 		struct bnxt_stats_mem *stats;
10340 
10341 		cpr = &bnapi->cp_ring;
10342 		stats = &cpr->stats;
10343 		if (!i)
10344 			ring0_stats = stats;
10345 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10346 					ring0_stats->hw_masks,
10347 					ring0_stats->len / 8, ignore_zero);
10348 	}
10349 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10350 		struct bnxt_stats_mem *stats = &bp->port_stats;
10351 		__le64 *hw_stats = stats->hw_stats;
10352 		u64 *sw_stats = stats->sw_stats;
10353 		u64 *masks = stats->hw_masks;
10354 		int cnt;
10355 
10356 		cnt = sizeof(struct rx_port_stats) / 8;
10357 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10358 
10359 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10360 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10361 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10362 		cnt = sizeof(struct tx_port_stats) / 8;
10363 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10364 	}
10365 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10366 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10367 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10368 	}
10369 }
10370 
10371 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10372 {
10373 	struct hwrm_port_qstats_input *req;
10374 	struct bnxt_pf_info *pf = &bp->pf;
10375 	int rc;
10376 
10377 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10378 		return 0;
10379 
10380 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10381 		return -EOPNOTSUPP;
10382 
10383 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10384 	if (rc)
10385 		return rc;
10386 
10387 	req->flags = flags;
10388 	req->port_id = cpu_to_le16(pf->port_id);
10389 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10390 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10391 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10392 	return hwrm_req_send(bp, req);
10393 }
10394 
10395 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10396 {
10397 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10398 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10399 	struct hwrm_port_qstats_ext_output *resp_qs;
10400 	struct hwrm_port_qstats_ext_input *req_qs;
10401 	struct bnxt_pf_info *pf = &bp->pf;
10402 	u32 tx_stat_size;
10403 	int rc;
10404 
10405 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10406 		return 0;
10407 
10408 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10409 		return -EOPNOTSUPP;
10410 
10411 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10412 	if (rc)
10413 		return rc;
10414 
10415 	req_qs->flags = flags;
10416 	req_qs->port_id = cpu_to_le16(pf->port_id);
10417 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10418 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10419 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10420 		       sizeof(struct tx_port_stats_ext) : 0;
10421 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10422 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10423 	resp_qs = hwrm_req_hold(bp, req_qs);
10424 	rc = hwrm_req_send(bp, req_qs);
10425 	if (!rc) {
10426 		bp->fw_rx_stats_ext_size =
10427 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10428 		if (BNXT_FW_MAJ(bp) < 220 &&
10429 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10430 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10431 
10432 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10433 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10434 	} else {
10435 		bp->fw_rx_stats_ext_size = 0;
10436 		bp->fw_tx_stats_ext_size = 0;
10437 	}
10438 	hwrm_req_drop(bp, req_qs);
10439 
10440 	if (flags)
10441 		return rc;
10442 
10443 	if (bp->fw_tx_stats_ext_size <=
10444 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10445 		bp->pri2cos_valid = 0;
10446 		return rc;
10447 	}
10448 
10449 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10450 	if (rc)
10451 		return rc;
10452 
10453 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10454 
10455 	resp_qc = hwrm_req_hold(bp, req_qc);
10456 	rc = hwrm_req_send(bp, req_qc);
10457 	if (!rc) {
10458 		u8 *pri2cos;
10459 		int i, j;
10460 
10461 		pri2cos = &resp_qc->pri0_cos_queue_id;
10462 		for (i = 0; i < 8; i++) {
10463 			u8 queue_id = pri2cos[i];
10464 			u8 queue_idx;
10465 
10466 			/* Per port queue IDs start from 0, 10, 20, etc */
10467 			queue_idx = queue_id % 10;
10468 			if (queue_idx > BNXT_MAX_QUEUE) {
10469 				bp->pri2cos_valid = false;
10470 				hwrm_req_drop(bp, req_qc);
10471 				return rc;
10472 			}
10473 			for (j = 0; j < bp->max_q; j++) {
10474 				if (bp->q_ids[j] == queue_id)
10475 					bp->pri2cos_idx[i] = queue_idx;
10476 			}
10477 		}
10478 		bp->pri2cos_valid = true;
10479 	}
10480 	hwrm_req_drop(bp, req_qc);
10481 
10482 	return rc;
10483 }
10484 
10485 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10486 {
10487 	bnxt_hwrm_tunnel_dst_port_free(bp,
10488 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10489 	bnxt_hwrm_tunnel_dst_port_free(bp,
10490 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10491 }
10492 
10493 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10494 {
10495 	int rc, i;
10496 	u32 tpa_flags = 0;
10497 
10498 	if (set_tpa)
10499 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10500 	else if (BNXT_NO_FW_ACCESS(bp))
10501 		return 0;
10502 	for (i = 0; i < bp->nr_vnics; i++) {
10503 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10504 		if (rc) {
10505 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10506 				   i, rc);
10507 			return rc;
10508 		}
10509 	}
10510 	return 0;
10511 }
10512 
10513 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10514 {
10515 	int i;
10516 
10517 	for (i = 0; i < bp->nr_vnics; i++)
10518 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10519 }
10520 
10521 static void bnxt_clear_vnic(struct bnxt *bp)
10522 {
10523 	if (!bp->vnic_info)
10524 		return;
10525 
10526 	bnxt_hwrm_clear_vnic_filter(bp);
10527 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10528 		/* clear all RSS setting before free vnic ctx */
10529 		bnxt_hwrm_clear_vnic_rss(bp);
10530 		bnxt_hwrm_vnic_ctx_free(bp);
10531 	}
10532 	/* before free the vnic, undo the vnic tpa settings */
10533 	if (bp->flags & BNXT_FLAG_TPA)
10534 		bnxt_set_tpa(bp, false);
10535 	bnxt_hwrm_vnic_free(bp);
10536 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10537 		bnxt_hwrm_vnic_ctx_free(bp);
10538 }
10539 
10540 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10541 				    bool irq_re_init)
10542 {
10543 	bnxt_clear_vnic(bp);
10544 	bnxt_hwrm_ring_free(bp, close_path);
10545 	bnxt_hwrm_ring_grp_free(bp);
10546 	if (irq_re_init) {
10547 		bnxt_hwrm_stat_ctx_free(bp);
10548 		bnxt_hwrm_free_tunnel_ports(bp);
10549 	}
10550 }
10551 
10552 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10553 {
10554 	struct hwrm_func_cfg_input *req;
10555 	u8 evb_mode;
10556 	int rc;
10557 
10558 	if (br_mode == BRIDGE_MODE_VEB)
10559 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10560 	else if (br_mode == BRIDGE_MODE_VEPA)
10561 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10562 	else
10563 		return -EINVAL;
10564 
10565 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10566 	if (rc)
10567 		return rc;
10568 
10569 	req->fid = cpu_to_le16(0xffff);
10570 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10571 	req->evb_mode = evb_mode;
10572 	return hwrm_req_send(bp, req);
10573 }
10574 
10575 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10576 {
10577 	struct hwrm_func_cfg_input *req;
10578 	int rc;
10579 
10580 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10581 		return 0;
10582 
10583 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10584 	if (rc)
10585 		return rc;
10586 
10587 	req->fid = cpu_to_le16(0xffff);
10588 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10589 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10590 	if (size == 128)
10591 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10592 
10593 	return hwrm_req_send(bp, req);
10594 }
10595 
10596 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10597 {
10598 	int rc;
10599 
10600 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10601 		goto skip_rss_ctx;
10602 
10603 	/* allocate context for vnic */
10604 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10605 	if (rc) {
10606 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10607 			   vnic->vnic_id, rc);
10608 		goto vnic_setup_err;
10609 	}
10610 	bp->rsscos_nr_ctxs++;
10611 
10612 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10613 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10614 		if (rc) {
10615 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10616 				   vnic->vnic_id, rc);
10617 			goto vnic_setup_err;
10618 		}
10619 		bp->rsscos_nr_ctxs++;
10620 	}
10621 
10622 skip_rss_ctx:
10623 	/* configure default vnic, ring grp */
10624 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10625 	if (rc) {
10626 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10627 			   vnic->vnic_id, rc);
10628 		goto vnic_setup_err;
10629 	}
10630 
10631 	/* Enable RSS hashing on vnic */
10632 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10633 	if (rc) {
10634 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10635 			   vnic->vnic_id, rc);
10636 		goto vnic_setup_err;
10637 	}
10638 
10639 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10640 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10641 		if (rc) {
10642 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10643 				   vnic->vnic_id, rc);
10644 		}
10645 	}
10646 
10647 vnic_setup_err:
10648 	return rc;
10649 }
10650 
10651 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10652 			  u8 valid)
10653 {
10654 	struct hwrm_vnic_update_input *req;
10655 	int rc;
10656 
10657 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10658 	if (rc)
10659 		return rc;
10660 
10661 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10662 
10663 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10664 		req->mru = cpu_to_le16(vnic->mru);
10665 
10666 	req->enables = cpu_to_le32(valid);
10667 
10668 	return hwrm_req_send(bp, req);
10669 }
10670 
10671 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10672 {
10673 	int rc;
10674 
10675 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10676 	if (rc) {
10677 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10678 			   vnic->vnic_id, rc);
10679 		return rc;
10680 	}
10681 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10682 	if (rc)
10683 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10684 			   vnic->vnic_id, rc);
10685 	return rc;
10686 }
10687 
10688 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10689 {
10690 	int rc, i, nr_ctxs;
10691 
10692 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10693 	for (i = 0; i < nr_ctxs; i++) {
10694 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10695 		if (rc) {
10696 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10697 				   vnic->vnic_id, i, rc);
10698 			break;
10699 		}
10700 		bp->rsscos_nr_ctxs++;
10701 	}
10702 	if (i < nr_ctxs)
10703 		return -ENOMEM;
10704 
10705 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10706 	if (rc)
10707 		return rc;
10708 
10709 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10710 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10711 		if (rc) {
10712 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10713 				   vnic->vnic_id, rc);
10714 		}
10715 	}
10716 	return rc;
10717 }
10718 
10719 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10720 {
10721 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10722 		return __bnxt_setup_vnic_p5(bp, vnic);
10723 	else
10724 		return __bnxt_setup_vnic(bp, vnic);
10725 }
10726 
10727 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10728 				     struct bnxt_vnic_info *vnic,
10729 				     u16 start_rx_ring_idx, int rx_rings)
10730 {
10731 	int rc;
10732 
10733 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10734 	if (rc) {
10735 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10736 			   vnic->vnic_id, rc);
10737 		return rc;
10738 	}
10739 	return bnxt_setup_vnic(bp, vnic);
10740 }
10741 
10742 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10743 {
10744 	struct bnxt_vnic_info *vnic;
10745 	int i, rc = 0;
10746 
10747 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10748 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10749 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10750 	}
10751 
10752 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10753 		return 0;
10754 
10755 	for (i = 0; i < bp->rx_nr_rings; i++) {
10756 		u16 vnic_id = i + 1;
10757 		u16 ring_id = i;
10758 
10759 		if (vnic_id >= bp->nr_vnics)
10760 			break;
10761 
10762 		vnic = &bp->vnic_info[vnic_id];
10763 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10764 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10765 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10766 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10767 			break;
10768 	}
10769 	return rc;
10770 }
10771 
10772 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10773 			  bool all)
10774 {
10775 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10776 	struct bnxt_filter_base *usr_fltr, *tmp;
10777 	struct bnxt_ntuple_filter *ntp_fltr;
10778 	int i;
10779 
10780 	if (netif_running(bp->dev)) {
10781 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10782 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10783 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10784 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10785 		}
10786 	}
10787 	if (!all)
10788 		return;
10789 
10790 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10791 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10792 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10793 			ntp_fltr = container_of(usr_fltr,
10794 						struct bnxt_ntuple_filter,
10795 						base);
10796 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10797 			bnxt_del_ntp_filter(bp, ntp_fltr);
10798 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10799 		}
10800 	}
10801 
10802 	if (vnic->rss_table)
10803 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10804 				  vnic->rss_table,
10805 				  vnic->rss_table_dma_addr);
10806 	bp->num_rss_ctx--;
10807 }
10808 
10809 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10810 				  int rxr_id)
10811 {
10812 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10813 	int i, vnic_rx;
10814 
10815 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10816 	 * must be updated because a future filter may use it.
10817 	 */
10818 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10819 		return true;
10820 
10821 	for (i = 0; i < tbl_size; i++) {
10822 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10823 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10824 		else
10825 			vnic_rx = bp->rss_indir_tbl[i];
10826 
10827 		if (rxr_id == vnic_rx)
10828 			return true;
10829 	}
10830 
10831 	return false;
10832 }
10833 
10834 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10835 				u16 mru, int rxr_id)
10836 {
10837 	int rc;
10838 
10839 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10840 		return 0;
10841 
10842 	if (mru) {
10843 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10844 		if (rc) {
10845 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10846 				   vnic->vnic_id, rc);
10847 			return rc;
10848 		}
10849 	}
10850 	vnic->mru = mru;
10851 	bnxt_hwrm_vnic_update(bp, vnic,
10852 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10853 
10854 	return 0;
10855 }
10856 
10857 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10858 {
10859 	struct ethtool_rxfh_context *ctx;
10860 	unsigned long context;
10861 	int rc;
10862 
10863 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10864 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10865 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10866 
10867 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10868 		if (rc)
10869 			return rc;
10870 	}
10871 
10872 	return 0;
10873 }
10874 
10875 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10876 {
10877 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10878 	struct ethtool_rxfh_context *ctx;
10879 	unsigned long context;
10880 
10881 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10882 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10883 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10884 
10885 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10886 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10887 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10888 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10889 				   rss_ctx->index);
10890 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10891 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10892 		}
10893 	}
10894 }
10895 
10896 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10897 {
10898 	struct ethtool_rxfh_context *ctx;
10899 	unsigned long context;
10900 
10901 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10902 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10903 
10904 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10905 	}
10906 }
10907 
10908 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10909 static bool bnxt_promisc_ok(struct bnxt *bp)
10910 {
10911 #ifdef CONFIG_BNXT_SRIOV
10912 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10913 		return false;
10914 #endif
10915 	return true;
10916 }
10917 
10918 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10919 {
10920 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10921 	unsigned int rc = 0;
10922 
10923 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10924 	if (rc) {
10925 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10926 			   rc);
10927 		return rc;
10928 	}
10929 
10930 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10931 	if (rc) {
10932 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10933 			   rc);
10934 		return rc;
10935 	}
10936 	return rc;
10937 }
10938 
10939 static int bnxt_cfg_rx_mode(struct bnxt *);
10940 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10941 
10942 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10943 {
10944 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10945 	int rc = 0;
10946 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10947 
10948 	if (irq_re_init) {
10949 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10950 		if (rc) {
10951 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10952 				   rc);
10953 			goto err_out;
10954 		}
10955 	}
10956 
10957 	rc = bnxt_hwrm_ring_alloc(bp);
10958 	if (rc) {
10959 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10960 		goto err_out;
10961 	}
10962 
10963 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10964 	if (rc) {
10965 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10966 		goto err_out;
10967 	}
10968 
10969 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10970 		rx_nr_rings--;
10971 
10972 	/* default vnic 0 */
10973 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10974 	if (rc) {
10975 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10976 		goto err_out;
10977 	}
10978 
10979 	if (BNXT_VF(bp))
10980 		bnxt_hwrm_func_qcfg(bp);
10981 
10982 	rc = bnxt_setup_vnic(bp, vnic);
10983 	if (rc)
10984 		goto err_out;
10985 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10986 		bnxt_hwrm_update_rss_hash_cfg(bp);
10987 
10988 	if (bp->flags & BNXT_FLAG_RFS) {
10989 		rc = bnxt_alloc_rfs_vnics(bp);
10990 		if (rc)
10991 			goto err_out;
10992 	}
10993 
10994 	if (bp->flags & BNXT_FLAG_TPA) {
10995 		rc = bnxt_set_tpa(bp, true);
10996 		if (rc)
10997 			goto err_out;
10998 	}
10999 
11000 	if (BNXT_VF(bp))
11001 		bnxt_update_vf_mac(bp);
11002 
11003 	/* Filter for default vnic 0 */
11004 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
11005 	if (rc) {
11006 		if (BNXT_VF(bp) && rc == -ENODEV)
11007 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
11008 		else
11009 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
11010 		goto err_out;
11011 	}
11012 	vnic->uc_filter_count = 1;
11013 
11014 	vnic->rx_mask = 0;
11015 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11016 		goto skip_rx_mask;
11017 
11018 	if (bp->dev->flags & IFF_BROADCAST)
11019 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11020 
11021 	if (bp->dev->flags & IFF_PROMISC)
11022 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11023 
11024 	if (bp->dev->flags & IFF_ALLMULTI) {
11025 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11026 		vnic->mc_list_count = 0;
11027 	} else if (bp->dev->flags & IFF_MULTICAST) {
11028 		u32 mask = 0;
11029 
11030 		bnxt_mc_list_updated(bp, &mask);
11031 		vnic->rx_mask |= mask;
11032 	}
11033 
11034 	rc = bnxt_cfg_rx_mode(bp);
11035 	if (rc)
11036 		goto err_out;
11037 
11038 skip_rx_mask:
11039 	rc = bnxt_hwrm_set_coal(bp);
11040 	if (rc)
11041 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11042 				rc);
11043 
11044 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11045 		rc = bnxt_setup_nitroa0_vnic(bp);
11046 		if (rc)
11047 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11048 				   rc);
11049 	}
11050 
11051 	if (BNXT_VF(bp)) {
11052 		bnxt_hwrm_func_qcfg(bp);
11053 		netdev_update_features(bp->dev);
11054 	}
11055 
11056 	return 0;
11057 
11058 err_out:
11059 	bnxt_hwrm_resource_free(bp, 0, true);
11060 
11061 	return rc;
11062 }
11063 
11064 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11065 {
11066 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11067 	return 0;
11068 }
11069 
11070 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11071 {
11072 	bnxt_init_cp_rings(bp);
11073 	bnxt_init_rx_rings(bp);
11074 	bnxt_init_tx_rings(bp);
11075 	bnxt_init_ring_grps(bp, irq_re_init);
11076 	bnxt_init_vnics(bp);
11077 
11078 	return bnxt_init_chip(bp, irq_re_init);
11079 }
11080 
11081 static int bnxt_set_real_num_queues(struct bnxt *bp)
11082 {
11083 	int rc;
11084 	struct net_device *dev = bp->dev;
11085 
11086 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11087 					  bp->tx_nr_rings_xdp);
11088 	if (rc)
11089 		return rc;
11090 
11091 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11092 	if (rc)
11093 		return rc;
11094 
11095 #ifdef CONFIG_RFS_ACCEL
11096 	if (bp->flags & BNXT_FLAG_RFS)
11097 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11098 #endif
11099 
11100 	return rc;
11101 }
11102 
11103 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11104 			     bool shared)
11105 {
11106 	int _rx = *rx, _tx = *tx;
11107 
11108 	if (shared) {
11109 		*rx = min_t(int, _rx, max);
11110 		*tx = min_t(int, _tx, max);
11111 	} else {
11112 		if (max < 2)
11113 			return -ENOMEM;
11114 
11115 		while (_rx + _tx > max) {
11116 			if (_rx > _tx && _rx > 1)
11117 				_rx--;
11118 			else if (_tx > 1)
11119 				_tx--;
11120 		}
11121 		*rx = _rx;
11122 		*tx = _tx;
11123 	}
11124 	return 0;
11125 }
11126 
11127 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11128 {
11129 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11130 }
11131 
11132 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11133 {
11134 	int tcs = bp->num_tc;
11135 
11136 	if (!tcs)
11137 		tcs = 1;
11138 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11139 }
11140 
11141 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11142 {
11143 	int tcs = bp->num_tc;
11144 
11145 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11146 	       bp->tx_nr_rings_xdp;
11147 }
11148 
11149 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11150 			   bool sh)
11151 {
11152 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11153 
11154 	if (tx_cp != *tx) {
11155 		int tx_saved = tx_cp, rc;
11156 
11157 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11158 		if (rc)
11159 			return rc;
11160 		if (tx_cp != tx_saved)
11161 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11162 		return 0;
11163 	}
11164 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11165 }
11166 
11167 static void bnxt_setup_msix(struct bnxt *bp)
11168 {
11169 	const int len = sizeof(bp->irq_tbl[0].name);
11170 	struct net_device *dev = bp->dev;
11171 	int tcs, i;
11172 
11173 	tcs = bp->num_tc;
11174 	if (tcs) {
11175 		int i, off, count;
11176 
11177 		for (i = 0; i < tcs; i++) {
11178 			count = bp->tx_nr_rings_per_tc;
11179 			off = BNXT_TC_TO_RING_BASE(bp, i);
11180 			netdev_set_tc_queue(dev, i, count, off);
11181 		}
11182 	}
11183 
11184 	for (i = 0; i < bp->cp_nr_rings; i++) {
11185 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11186 		char *attr;
11187 
11188 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11189 			attr = "TxRx";
11190 		else if (i < bp->rx_nr_rings)
11191 			attr = "rx";
11192 		else
11193 			attr = "tx";
11194 
11195 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11196 			 attr, i);
11197 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11198 	}
11199 }
11200 
11201 static int bnxt_init_int_mode(struct bnxt *bp);
11202 
11203 static int bnxt_change_msix(struct bnxt *bp, int total)
11204 {
11205 	struct msi_map map;
11206 	int i;
11207 
11208 	/* add MSIX to the end if needed */
11209 	for (i = bp->total_irqs; i < total; i++) {
11210 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11211 		if (map.index < 0)
11212 			return bp->total_irqs;
11213 		bp->irq_tbl[i].vector = map.virq;
11214 		bp->total_irqs++;
11215 	}
11216 
11217 	/* trim MSIX from the end if needed */
11218 	for (i = bp->total_irqs; i > total; i--) {
11219 		map.index = i - 1;
11220 		map.virq = bp->irq_tbl[i - 1].vector;
11221 		pci_msix_free_irq(bp->pdev, map);
11222 		bp->total_irqs--;
11223 	}
11224 	return bp->total_irqs;
11225 }
11226 
11227 static int bnxt_setup_int_mode(struct bnxt *bp)
11228 {
11229 	int rc;
11230 
11231 	if (!bp->irq_tbl) {
11232 		rc = bnxt_init_int_mode(bp);
11233 		if (rc || !bp->irq_tbl)
11234 			return rc ?: -ENODEV;
11235 	}
11236 
11237 	bnxt_setup_msix(bp);
11238 
11239 	rc = bnxt_set_real_num_queues(bp);
11240 	return rc;
11241 }
11242 
11243 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11244 {
11245 	return bp->hw_resc.max_rsscos_ctxs;
11246 }
11247 
11248 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11249 {
11250 	return bp->hw_resc.max_vnics;
11251 }
11252 
11253 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11254 {
11255 	return bp->hw_resc.max_stat_ctxs;
11256 }
11257 
11258 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11259 {
11260 	return bp->hw_resc.max_cp_rings;
11261 }
11262 
11263 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11264 {
11265 	unsigned int cp = bp->hw_resc.max_cp_rings;
11266 
11267 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11268 		cp -= bnxt_get_ulp_msix_num(bp);
11269 
11270 	return cp;
11271 }
11272 
11273 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11274 {
11275 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11276 
11277 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11278 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11279 
11280 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11281 }
11282 
11283 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11284 {
11285 	bp->hw_resc.max_irqs = max_irqs;
11286 }
11287 
11288 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11289 {
11290 	unsigned int cp;
11291 
11292 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11293 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11294 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11295 	else
11296 		return cp - bp->cp_nr_rings;
11297 }
11298 
11299 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11300 {
11301 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11302 }
11303 
11304 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11305 {
11306 	int max_irq = bnxt_get_max_func_irqs(bp);
11307 	int total_req = bp->cp_nr_rings + num;
11308 
11309 	if (max_irq < total_req) {
11310 		num = max_irq - bp->cp_nr_rings;
11311 		if (num <= 0)
11312 			return 0;
11313 	}
11314 	return num;
11315 }
11316 
11317 static int bnxt_get_num_msix(struct bnxt *bp)
11318 {
11319 	if (!BNXT_NEW_RM(bp))
11320 		return bnxt_get_max_func_irqs(bp);
11321 
11322 	return bnxt_nq_rings_in_use(bp);
11323 }
11324 
11325 static int bnxt_init_int_mode(struct bnxt *bp)
11326 {
11327 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11328 
11329 	total_vecs = bnxt_get_num_msix(bp);
11330 	max = bnxt_get_max_func_irqs(bp);
11331 	if (total_vecs > max)
11332 		total_vecs = max;
11333 
11334 	if (!total_vecs)
11335 		return 0;
11336 
11337 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11338 		min = 2;
11339 
11340 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11341 					   PCI_IRQ_MSIX);
11342 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11343 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11344 		rc = -ENODEV;
11345 		goto msix_setup_exit;
11346 	}
11347 
11348 	tbl_size = total_vecs;
11349 	if (pci_msix_can_alloc_dyn(bp->pdev))
11350 		tbl_size = max;
11351 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11352 	if (bp->irq_tbl) {
11353 		for (i = 0; i < total_vecs; i++)
11354 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11355 
11356 		bp->total_irqs = total_vecs;
11357 		/* Trim rings based upon num of vectors allocated */
11358 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11359 				     total_vecs - ulp_msix, min == 1);
11360 		if (rc)
11361 			goto msix_setup_exit;
11362 
11363 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11364 		bp->cp_nr_rings = (min == 1) ?
11365 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11366 				  tx_cp + bp->rx_nr_rings;
11367 
11368 	} else {
11369 		rc = -ENOMEM;
11370 		goto msix_setup_exit;
11371 	}
11372 	return 0;
11373 
11374 msix_setup_exit:
11375 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11376 	kfree(bp->irq_tbl);
11377 	bp->irq_tbl = NULL;
11378 	pci_free_irq_vectors(bp->pdev);
11379 	return rc;
11380 }
11381 
11382 static void bnxt_clear_int_mode(struct bnxt *bp)
11383 {
11384 	pci_free_irq_vectors(bp->pdev);
11385 
11386 	kfree(bp->irq_tbl);
11387 	bp->irq_tbl = NULL;
11388 }
11389 
11390 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11391 {
11392 	bool irq_cleared = false;
11393 	bool irq_change = false;
11394 	int tcs = bp->num_tc;
11395 	int irqs_required;
11396 	int rc;
11397 
11398 	if (!bnxt_need_reserve_rings(bp))
11399 		return 0;
11400 
11401 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11402 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11403 
11404 		if (ulp_msix > bp->ulp_num_msix_want)
11405 			ulp_msix = bp->ulp_num_msix_want;
11406 		irqs_required = ulp_msix + bp->cp_nr_rings;
11407 	} else {
11408 		irqs_required = bnxt_get_num_msix(bp);
11409 	}
11410 
11411 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11412 		irq_change = true;
11413 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11414 			bnxt_ulp_irq_stop(bp);
11415 			bnxt_clear_int_mode(bp);
11416 			irq_cleared = true;
11417 		}
11418 	}
11419 	rc = __bnxt_reserve_rings(bp);
11420 	if (irq_cleared) {
11421 		if (!rc)
11422 			rc = bnxt_init_int_mode(bp);
11423 		bnxt_ulp_irq_restart(bp, rc);
11424 	} else if (irq_change && !rc) {
11425 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11426 			rc = -ENOSPC;
11427 	}
11428 	if (rc) {
11429 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11430 		return rc;
11431 	}
11432 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11433 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11434 		netdev_err(bp->dev, "tx ring reservation failure\n");
11435 		netdev_reset_tc(bp->dev);
11436 		bp->num_tc = 0;
11437 		if (bp->tx_nr_rings_xdp)
11438 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11439 		else
11440 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11441 		return -ENOMEM;
11442 	}
11443 	return 0;
11444 }
11445 
11446 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11447 {
11448 	struct bnxt_tx_ring_info *txr;
11449 	struct netdev_queue *txq;
11450 	struct bnxt_napi *bnapi;
11451 	int i;
11452 
11453 	bnapi = bp->bnapi[idx];
11454 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11455 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11456 		synchronize_net();
11457 
11458 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11459 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11460 			if (txq) {
11461 				__netif_tx_lock_bh(txq);
11462 				netif_tx_stop_queue(txq);
11463 				__netif_tx_unlock_bh(txq);
11464 			}
11465 		}
11466 
11467 		if (!bp->tph_mode)
11468 			continue;
11469 
11470 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11471 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11472 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11473 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11474 	}
11475 }
11476 
11477 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11478 {
11479 	struct bnxt_tx_ring_info *txr;
11480 	struct netdev_queue *txq;
11481 	struct bnxt_napi *bnapi;
11482 	int rc, i;
11483 
11484 	bnapi = bp->bnapi[idx];
11485 	/* All rings have been reserved and previously allocated.
11486 	 * Reallocating with the same parameters should never fail.
11487 	 */
11488 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11489 		if (!bp->tph_mode)
11490 			goto start_tx;
11491 
11492 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11493 		if (rc)
11494 			return rc;
11495 
11496 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11497 		if (rc)
11498 			return rc;
11499 
11500 		txr->tx_prod = 0;
11501 		txr->tx_cons = 0;
11502 		txr->tx_hw_cons = 0;
11503 start_tx:
11504 		WRITE_ONCE(txr->dev_state, 0);
11505 		synchronize_net();
11506 
11507 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11508 			continue;
11509 
11510 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11511 		if (txq)
11512 			netif_tx_start_queue(txq);
11513 	}
11514 
11515 	return 0;
11516 }
11517 
11518 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11519 				     const cpumask_t *mask)
11520 {
11521 	struct bnxt_irq *irq;
11522 	u16 tag;
11523 	int err;
11524 
11525 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11526 
11527 	if (!irq->bp->tph_mode)
11528 		return;
11529 
11530 	cpumask_copy(irq->cpu_mask, mask);
11531 
11532 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11533 		return;
11534 
11535 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11536 				cpumask_first(irq->cpu_mask), &tag))
11537 		return;
11538 
11539 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11540 		return;
11541 
11542 	netdev_lock(irq->bp->dev);
11543 	if (netif_running(irq->bp->dev)) {
11544 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11545 		if (err)
11546 			netdev_err(irq->bp->dev,
11547 				   "RX queue restart failed: err=%d\n", err);
11548 	}
11549 	netdev_unlock(irq->bp->dev);
11550 }
11551 
11552 static void bnxt_irq_affinity_release(struct kref *ref)
11553 {
11554 	struct irq_affinity_notify *notify =
11555 		container_of(ref, struct irq_affinity_notify, kref);
11556 	struct bnxt_irq *irq;
11557 
11558 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11559 
11560 	if (!irq->bp->tph_mode)
11561 		return;
11562 
11563 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11564 		netdev_err(irq->bp->dev,
11565 			   "Setting ST=0 for MSIX entry %d failed\n",
11566 			   irq->msix_nr);
11567 		return;
11568 	}
11569 }
11570 
11571 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11572 {
11573 	irq_set_affinity_notifier(irq->vector, NULL);
11574 }
11575 
11576 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11577 {
11578 	struct irq_affinity_notify *notify;
11579 
11580 	irq->bp = bp;
11581 
11582 	/* Nothing to do if TPH is not enabled */
11583 	if (!bp->tph_mode)
11584 		return;
11585 
11586 	/* Register IRQ affinity notifier */
11587 	notify = &irq->affinity_notify;
11588 	notify->irq = irq->vector;
11589 	notify->notify = bnxt_irq_affinity_notify;
11590 	notify->release = bnxt_irq_affinity_release;
11591 
11592 	irq_set_affinity_notifier(irq->vector, notify);
11593 }
11594 
11595 static void bnxt_free_irq(struct bnxt *bp)
11596 {
11597 	struct bnxt_irq *irq;
11598 	int i;
11599 
11600 #ifdef CONFIG_RFS_ACCEL
11601 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11602 	bp->dev->rx_cpu_rmap = NULL;
11603 #endif
11604 	if (!bp->irq_tbl || !bp->bnapi)
11605 		return;
11606 
11607 	for (i = 0; i < bp->cp_nr_rings; i++) {
11608 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11609 
11610 		irq = &bp->irq_tbl[map_idx];
11611 		if (irq->requested) {
11612 			if (irq->have_cpumask) {
11613 				irq_update_affinity_hint(irq->vector, NULL);
11614 				free_cpumask_var(irq->cpu_mask);
11615 				irq->have_cpumask = 0;
11616 			}
11617 
11618 			bnxt_release_irq_notifier(irq);
11619 
11620 			free_irq(irq->vector, bp->bnapi[i]);
11621 		}
11622 
11623 		irq->requested = 0;
11624 	}
11625 
11626 	/* Disable TPH support */
11627 	pcie_disable_tph(bp->pdev);
11628 	bp->tph_mode = 0;
11629 }
11630 
11631 static int bnxt_request_irq(struct bnxt *bp)
11632 {
11633 	struct cpu_rmap *rmap = NULL;
11634 	int i, j, rc = 0;
11635 	unsigned long flags = 0;
11636 
11637 	rc = bnxt_setup_int_mode(bp);
11638 	if (rc) {
11639 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11640 			   rc);
11641 		return rc;
11642 	}
11643 #ifdef CONFIG_RFS_ACCEL
11644 	rmap = bp->dev->rx_cpu_rmap;
11645 #endif
11646 
11647 	/* Enable TPH support as part of IRQ request */
11648 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11649 	if (!rc)
11650 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11651 
11652 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11653 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11654 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11655 
11656 		if (IS_ENABLED(CONFIG_RFS_ACCEL) &&
11657 		    rmap && bp->bnapi[i]->rx_ring) {
11658 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11659 			if (rc)
11660 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11661 					    j);
11662 			j++;
11663 		}
11664 
11665 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11666 				 bp->bnapi[i]);
11667 		if (rc)
11668 			break;
11669 
11670 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11671 		irq->requested = 1;
11672 
11673 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11674 			int numa_node = dev_to_node(&bp->pdev->dev);
11675 			u16 tag;
11676 
11677 			irq->have_cpumask = 1;
11678 			irq->msix_nr = map_idx;
11679 			irq->ring_nr = i;
11680 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11681 					irq->cpu_mask);
11682 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11683 			if (rc) {
11684 				netdev_warn(bp->dev,
11685 					    "Update affinity hint failed, IRQ = %d\n",
11686 					    irq->vector);
11687 				break;
11688 			}
11689 
11690 			bnxt_register_irq_notifier(bp, irq);
11691 
11692 			/* Init ST table entry */
11693 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11694 						cpumask_first(irq->cpu_mask),
11695 						&tag))
11696 				continue;
11697 
11698 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11699 		}
11700 	}
11701 	return rc;
11702 }
11703 
11704 static void bnxt_del_napi(struct bnxt *bp)
11705 {
11706 	int i;
11707 
11708 	if (!bp->bnapi)
11709 		return;
11710 
11711 	for (i = 0; i < bp->rx_nr_rings; i++)
11712 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11713 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11714 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11715 
11716 	for (i = 0; i < bp->cp_nr_rings; i++) {
11717 		struct bnxt_napi *bnapi = bp->bnapi[i];
11718 
11719 		__netif_napi_del_locked(&bnapi->napi);
11720 	}
11721 	/* We called __netif_napi_del_locked(), we need
11722 	 * to respect an RCU grace period before freeing napi structures.
11723 	 */
11724 	synchronize_net();
11725 }
11726 
11727 static void bnxt_init_napi(struct bnxt *bp)
11728 {
11729 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11730 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11731 	struct bnxt_napi *bnapi;
11732 	int i;
11733 
11734 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11735 		poll_fn = bnxt_poll_p5;
11736 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11737 		cp_nr_rings--;
11738 
11739 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11740 
11741 	for (i = 0; i < cp_nr_rings; i++) {
11742 		bnapi = bp->bnapi[i];
11743 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11744 					     bnapi->index);
11745 	}
11746 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11747 		bnapi = bp->bnapi[cp_nr_rings];
11748 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11749 	}
11750 }
11751 
11752 static void bnxt_disable_napi(struct bnxt *bp)
11753 {
11754 	int i;
11755 
11756 	if (!bp->bnapi ||
11757 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11758 		return;
11759 
11760 	for (i = 0; i < bp->cp_nr_rings; i++) {
11761 		struct bnxt_napi *bnapi = bp->bnapi[i];
11762 		struct bnxt_cp_ring_info *cpr;
11763 
11764 		cpr = &bnapi->cp_ring;
11765 		if (bnapi->tx_fault)
11766 			cpr->sw_stats->tx.tx_resets++;
11767 		if (bnapi->in_reset)
11768 			cpr->sw_stats->rx.rx_resets++;
11769 		napi_disable_locked(&bnapi->napi);
11770 	}
11771 }
11772 
11773 static void bnxt_enable_napi(struct bnxt *bp)
11774 {
11775 	int i;
11776 
11777 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11778 	for (i = 0; i < bp->cp_nr_rings; i++) {
11779 		struct bnxt_napi *bnapi = bp->bnapi[i];
11780 		struct bnxt_cp_ring_info *cpr;
11781 
11782 		bnapi->tx_fault = 0;
11783 
11784 		cpr = &bnapi->cp_ring;
11785 		bnapi->in_reset = false;
11786 
11787 		if (bnapi->rx_ring) {
11788 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11789 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11790 		}
11791 		napi_enable_locked(&bnapi->napi);
11792 	}
11793 }
11794 
11795 void bnxt_tx_disable(struct bnxt *bp)
11796 {
11797 	int i;
11798 	struct bnxt_tx_ring_info *txr;
11799 
11800 	if (bp->tx_ring) {
11801 		for (i = 0; i < bp->tx_nr_rings; i++) {
11802 			txr = &bp->tx_ring[i];
11803 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11804 		}
11805 	}
11806 	/* Make sure napi polls see @dev_state change */
11807 	synchronize_net();
11808 	/* Drop carrier first to prevent TX timeout */
11809 	netif_carrier_off(bp->dev);
11810 	/* Stop all TX queues */
11811 	netif_tx_disable(bp->dev);
11812 }
11813 
11814 void bnxt_tx_enable(struct bnxt *bp)
11815 {
11816 	int i;
11817 	struct bnxt_tx_ring_info *txr;
11818 
11819 	for (i = 0; i < bp->tx_nr_rings; i++) {
11820 		txr = &bp->tx_ring[i];
11821 		WRITE_ONCE(txr->dev_state, 0);
11822 	}
11823 	/* Make sure napi polls see @dev_state change */
11824 	synchronize_net();
11825 	netif_tx_wake_all_queues(bp->dev);
11826 	if (BNXT_LINK_IS_UP(bp))
11827 		netif_carrier_on(bp->dev);
11828 }
11829 
11830 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11831 {
11832 	u8 active_fec = link_info->active_fec_sig_mode &
11833 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11834 
11835 	switch (active_fec) {
11836 	default:
11837 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11838 		return "None";
11839 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11840 		return "Clause 74 BaseR";
11841 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11842 		return "Clause 91 RS(528,514)";
11843 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11844 		return "Clause 91 RS544_1XN";
11845 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11846 		return "Clause 91 RS(544,514)";
11847 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11848 		return "Clause 91 RS272_1XN";
11849 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11850 		return "Clause 91 RS(272,257)";
11851 	}
11852 }
11853 
11854 void bnxt_report_link(struct bnxt *bp)
11855 {
11856 	if (BNXT_LINK_IS_UP(bp)) {
11857 		const char *signal = "";
11858 		const char *flow_ctrl;
11859 		const char *duplex;
11860 		u32 speed;
11861 		u16 fec;
11862 
11863 		netif_carrier_on(bp->dev);
11864 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11865 		if (speed == SPEED_UNKNOWN) {
11866 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11867 			return;
11868 		}
11869 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11870 			duplex = "full";
11871 		else
11872 			duplex = "half";
11873 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11874 			flow_ctrl = "ON - receive & transmit";
11875 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11876 			flow_ctrl = "ON - transmit";
11877 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11878 			flow_ctrl = "ON - receive";
11879 		else
11880 			flow_ctrl = "none";
11881 		if (bp->link_info.phy_qcfg_resp.option_flags &
11882 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11883 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11884 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11885 			switch (sig_mode) {
11886 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11887 				signal = "(NRZ) ";
11888 				break;
11889 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11890 				signal = "(PAM4 56Gbps) ";
11891 				break;
11892 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11893 				signal = "(PAM4 112Gbps) ";
11894 				break;
11895 			default:
11896 				break;
11897 			}
11898 		}
11899 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11900 			    speed, signal, duplex, flow_ctrl);
11901 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11902 			netdev_info(bp->dev, "EEE is %s\n",
11903 				    bp->eee.eee_active ? "active" :
11904 							 "not active");
11905 		fec = bp->link_info.fec_cfg;
11906 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11907 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11908 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11909 				    bnxt_report_fec(&bp->link_info));
11910 	} else {
11911 		netif_carrier_off(bp->dev);
11912 		netdev_err(bp->dev, "NIC Link is Down\n");
11913 	}
11914 }
11915 
11916 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11917 {
11918 	if (!resp->supported_speeds_auto_mode &&
11919 	    !resp->supported_speeds_force_mode &&
11920 	    !resp->supported_pam4_speeds_auto_mode &&
11921 	    !resp->supported_pam4_speeds_force_mode &&
11922 	    !resp->supported_speeds2_auto_mode &&
11923 	    !resp->supported_speeds2_force_mode)
11924 		return true;
11925 	return false;
11926 }
11927 
11928 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11929 {
11930 	struct bnxt_link_info *link_info = &bp->link_info;
11931 	struct hwrm_port_phy_qcaps_output *resp;
11932 	struct hwrm_port_phy_qcaps_input *req;
11933 	int rc = 0;
11934 
11935 	if (bp->hwrm_spec_code < 0x10201)
11936 		return 0;
11937 
11938 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11939 	if (rc)
11940 		return rc;
11941 
11942 	resp = hwrm_req_hold(bp, req);
11943 	rc = hwrm_req_send(bp, req);
11944 	if (rc)
11945 		goto hwrm_phy_qcaps_exit;
11946 
11947 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11948 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11949 		struct ethtool_keee *eee = &bp->eee;
11950 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11951 
11952 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11953 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11954 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11955 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11956 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11957 	}
11958 
11959 	if (bp->hwrm_spec_code >= 0x10a01) {
11960 		if (bnxt_phy_qcaps_no_speed(resp)) {
11961 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11962 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11963 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11964 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11965 			netdev_info(bp->dev, "Ethernet link enabled\n");
11966 			/* Phy re-enabled, reprobe the speeds */
11967 			link_info->support_auto_speeds = 0;
11968 			link_info->support_pam4_auto_speeds = 0;
11969 			link_info->support_auto_speeds2 = 0;
11970 		}
11971 	}
11972 	if (resp->supported_speeds_auto_mode)
11973 		link_info->support_auto_speeds =
11974 			le16_to_cpu(resp->supported_speeds_auto_mode);
11975 	if (resp->supported_pam4_speeds_auto_mode)
11976 		link_info->support_pam4_auto_speeds =
11977 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11978 	if (resp->supported_speeds2_auto_mode)
11979 		link_info->support_auto_speeds2 =
11980 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11981 
11982 	bp->port_count = resp->port_cnt;
11983 
11984 hwrm_phy_qcaps_exit:
11985 	hwrm_req_drop(bp, req);
11986 	return rc;
11987 }
11988 
11989 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11990 {
11991 	struct hwrm_port_mac_qcaps_output *resp;
11992 	struct hwrm_port_mac_qcaps_input *req;
11993 	int rc;
11994 
11995 	if (bp->hwrm_spec_code < 0x10a03)
11996 		return;
11997 
11998 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11999 	if (rc)
12000 		return;
12001 
12002 	resp = hwrm_req_hold(bp, req);
12003 	rc = hwrm_req_send_silent(bp, req);
12004 	if (!rc)
12005 		bp->mac_flags = resp->flags;
12006 	hwrm_req_drop(bp, req);
12007 }
12008 
12009 static bool bnxt_support_dropped(u16 advertising, u16 supported)
12010 {
12011 	u16 diff = advertising ^ supported;
12012 
12013 	return ((supported | diff) != supported);
12014 }
12015 
12016 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12017 {
12018 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12019 
12020 	/* Check if any advertised speeds are no longer supported. The caller
12021 	 * holds the link_lock mutex, so we can modify link_info settings.
12022 	 */
12023 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12024 		if (bnxt_support_dropped(link_info->advertising,
12025 					 link_info->support_auto_speeds2)) {
12026 			link_info->advertising = link_info->support_auto_speeds2;
12027 			return true;
12028 		}
12029 		return false;
12030 	}
12031 	if (bnxt_support_dropped(link_info->advertising,
12032 				 link_info->support_auto_speeds)) {
12033 		link_info->advertising = link_info->support_auto_speeds;
12034 		return true;
12035 	}
12036 	if (bnxt_support_dropped(link_info->advertising_pam4,
12037 				 link_info->support_pam4_auto_speeds)) {
12038 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12039 		return true;
12040 	}
12041 	return false;
12042 }
12043 
12044 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12045 {
12046 	struct bnxt_link_info *link_info = &bp->link_info;
12047 	struct hwrm_port_phy_qcfg_output *resp;
12048 	struct hwrm_port_phy_qcfg_input *req;
12049 	u8 link_state = link_info->link_state;
12050 	bool support_changed;
12051 	int rc;
12052 
12053 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12054 	if (rc)
12055 		return rc;
12056 
12057 	resp = hwrm_req_hold(bp, req);
12058 	rc = hwrm_req_send(bp, req);
12059 	if (rc) {
12060 		hwrm_req_drop(bp, req);
12061 		if (BNXT_VF(bp) && rc == -ENODEV) {
12062 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12063 			rc = 0;
12064 		}
12065 		return rc;
12066 	}
12067 
12068 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12069 	link_info->phy_link_status = resp->link;
12070 	link_info->duplex = resp->duplex_cfg;
12071 	if (bp->hwrm_spec_code >= 0x10800)
12072 		link_info->duplex = resp->duplex_state;
12073 	link_info->pause = resp->pause;
12074 	link_info->auto_mode = resp->auto_mode;
12075 	link_info->auto_pause_setting = resp->auto_pause;
12076 	link_info->lp_pause = resp->link_partner_adv_pause;
12077 	link_info->force_pause_setting = resp->force_pause;
12078 	link_info->duplex_setting = resp->duplex_cfg;
12079 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12080 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12081 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12082 			link_info->active_lanes = resp->active_lanes;
12083 	} else {
12084 		link_info->link_speed = 0;
12085 		link_info->active_lanes = 0;
12086 	}
12087 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12088 	link_info->force_pam4_link_speed =
12089 		le16_to_cpu(resp->force_pam4_link_speed);
12090 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12091 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12092 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12093 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12094 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12095 	link_info->auto_pam4_link_speeds =
12096 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12097 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12098 	link_info->lp_auto_link_speeds =
12099 		le16_to_cpu(resp->link_partner_adv_speeds);
12100 	link_info->lp_auto_pam4_link_speeds =
12101 		resp->link_partner_pam4_adv_speeds;
12102 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12103 	link_info->phy_ver[0] = resp->phy_maj;
12104 	link_info->phy_ver[1] = resp->phy_min;
12105 	link_info->phy_ver[2] = resp->phy_bld;
12106 	link_info->media_type = resp->media_type;
12107 	link_info->phy_type = resp->phy_type;
12108 	link_info->transceiver = resp->xcvr_pkg_type;
12109 	link_info->phy_addr = resp->eee_config_phy_addr &
12110 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12111 	link_info->module_status = resp->module_status;
12112 
12113 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12114 		struct ethtool_keee *eee = &bp->eee;
12115 		u16 fw_speeds;
12116 
12117 		eee->eee_active = 0;
12118 		if (resp->eee_config_phy_addr &
12119 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12120 			eee->eee_active = 1;
12121 			fw_speeds = le16_to_cpu(
12122 				resp->link_partner_adv_eee_link_speed_mask);
12123 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12124 		}
12125 
12126 		/* Pull initial EEE config */
12127 		if (!chng_link_state) {
12128 			if (resp->eee_config_phy_addr &
12129 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12130 				eee->eee_enabled = 1;
12131 
12132 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12133 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12134 
12135 			if (resp->eee_config_phy_addr &
12136 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12137 				__le32 tmr;
12138 
12139 				eee->tx_lpi_enabled = 1;
12140 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12141 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12142 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12143 			}
12144 		}
12145 	}
12146 
12147 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12148 	if (bp->hwrm_spec_code >= 0x10504) {
12149 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12150 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12151 	}
12152 	/* TODO: need to add more logic to report VF link */
12153 	if (chng_link_state) {
12154 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12155 			link_info->link_state = BNXT_LINK_STATE_UP;
12156 		else
12157 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12158 		if (link_state != link_info->link_state)
12159 			bnxt_report_link(bp);
12160 	} else {
12161 		/* always link down if not require to update link state */
12162 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12163 	}
12164 	hwrm_req_drop(bp, req);
12165 
12166 	if (!BNXT_PHY_CFG_ABLE(bp))
12167 		return 0;
12168 
12169 	support_changed = bnxt_support_speed_dropped(link_info);
12170 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12171 		bnxt_hwrm_set_link_setting(bp, true, false);
12172 	return 0;
12173 }
12174 
12175 static void bnxt_get_port_module_status(struct bnxt *bp)
12176 {
12177 	struct bnxt_link_info *link_info = &bp->link_info;
12178 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12179 	u8 module_status;
12180 
12181 	if (bnxt_update_link(bp, true))
12182 		return;
12183 
12184 	module_status = link_info->module_status;
12185 	switch (module_status) {
12186 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12187 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12188 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12189 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12190 			    bp->pf.port_id);
12191 		if (bp->hwrm_spec_code >= 0x10201) {
12192 			netdev_warn(bp->dev, "Module part number %s\n",
12193 				    resp->phy_vendor_partnumber);
12194 		}
12195 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12196 			netdev_warn(bp->dev, "TX is disabled\n");
12197 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12198 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12199 	}
12200 }
12201 
12202 static void
12203 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12204 {
12205 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12206 		if (bp->hwrm_spec_code >= 0x10201)
12207 			req->auto_pause =
12208 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12209 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12210 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12211 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12212 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12213 		req->enables |=
12214 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12215 	} else {
12216 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12217 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12218 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12219 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12220 		req->enables |=
12221 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12222 		if (bp->hwrm_spec_code >= 0x10201) {
12223 			req->auto_pause = req->force_pause;
12224 			req->enables |= cpu_to_le32(
12225 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12226 		}
12227 	}
12228 }
12229 
12230 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12231 {
12232 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12233 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12234 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12235 			req->enables |=
12236 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12237 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12238 		} else if (bp->link_info.advertising) {
12239 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12240 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12241 		}
12242 		if (bp->link_info.advertising_pam4) {
12243 			req->enables |=
12244 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12245 			req->auto_link_pam4_speed_mask =
12246 				cpu_to_le16(bp->link_info.advertising_pam4);
12247 		}
12248 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12249 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12250 	} else {
12251 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12252 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12253 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12254 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12255 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12256 				   (u32)bp->link_info.req_link_speed);
12257 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12258 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12259 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12260 		} else {
12261 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12262 		}
12263 	}
12264 
12265 	/* tell chimp that the setting takes effect immediately */
12266 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12267 }
12268 
12269 int bnxt_hwrm_set_pause(struct bnxt *bp)
12270 {
12271 	struct hwrm_port_phy_cfg_input *req;
12272 	int rc;
12273 
12274 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12275 	if (rc)
12276 		return rc;
12277 
12278 	bnxt_hwrm_set_pause_common(bp, req);
12279 
12280 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12281 	    bp->link_info.force_link_chng)
12282 		bnxt_hwrm_set_link_common(bp, req);
12283 
12284 	rc = hwrm_req_send(bp, req);
12285 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12286 		/* since changing of pause setting doesn't trigger any link
12287 		 * change event, the driver needs to update the current pause
12288 		 * result upon successfully return of the phy_cfg command
12289 		 */
12290 		bp->link_info.pause =
12291 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12292 		bp->link_info.auto_pause_setting = 0;
12293 		if (!bp->link_info.force_link_chng)
12294 			bnxt_report_link(bp);
12295 	}
12296 	bp->link_info.force_link_chng = false;
12297 	return rc;
12298 }
12299 
12300 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12301 			      struct hwrm_port_phy_cfg_input *req)
12302 {
12303 	struct ethtool_keee *eee = &bp->eee;
12304 
12305 	if (eee->eee_enabled) {
12306 		u16 eee_speeds;
12307 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12308 
12309 		if (eee->tx_lpi_enabled)
12310 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12311 		else
12312 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12313 
12314 		req->flags |= cpu_to_le32(flags);
12315 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12316 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12317 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12318 	} else {
12319 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12320 	}
12321 }
12322 
12323 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12324 {
12325 	struct hwrm_port_phy_cfg_input *req;
12326 	int rc;
12327 
12328 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12329 	if (rc)
12330 		return rc;
12331 
12332 	if (set_pause)
12333 		bnxt_hwrm_set_pause_common(bp, req);
12334 
12335 	bnxt_hwrm_set_link_common(bp, req);
12336 
12337 	if (set_eee)
12338 		bnxt_hwrm_set_eee(bp, req);
12339 	return hwrm_req_send(bp, req);
12340 }
12341 
12342 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12343 {
12344 	struct hwrm_port_phy_cfg_input *req;
12345 	int rc;
12346 
12347 	if (!BNXT_SINGLE_PF(bp))
12348 		return 0;
12349 
12350 	if (pci_num_vf(bp->pdev) &&
12351 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12352 		return 0;
12353 
12354 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12355 	if (rc)
12356 		return rc;
12357 
12358 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12359 	rc = hwrm_req_send(bp, req);
12360 	if (!rc) {
12361 		mutex_lock(&bp->link_lock);
12362 		/* Device is not obliged link down in certain scenarios, even
12363 		 * when forced. Setting the state unknown is consistent with
12364 		 * driver startup and will force link state to be reported
12365 		 * during subsequent open based on PORT_PHY_QCFG.
12366 		 */
12367 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12368 		mutex_unlock(&bp->link_lock);
12369 	}
12370 	return rc;
12371 }
12372 
12373 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12374 {
12375 #ifdef CONFIG_TEE_BNXT_FW
12376 	int rc = tee_bnxt_fw_load();
12377 
12378 	if (rc)
12379 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12380 
12381 	return rc;
12382 #else
12383 	netdev_err(bp->dev, "OP-TEE not supported\n");
12384 	return -ENODEV;
12385 #endif
12386 }
12387 
12388 static int bnxt_try_recover_fw(struct bnxt *bp)
12389 {
12390 	if (bp->fw_health && bp->fw_health->status_reliable) {
12391 		int retry = 0, rc;
12392 		u32 sts;
12393 
12394 		do {
12395 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12396 			rc = bnxt_hwrm_poll(bp);
12397 			if (!BNXT_FW_IS_BOOTING(sts) &&
12398 			    !BNXT_FW_IS_RECOVERING(sts))
12399 				break;
12400 			retry++;
12401 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12402 
12403 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12404 			netdev_err(bp->dev,
12405 				   "Firmware not responding, status: 0x%x\n",
12406 				   sts);
12407 			rc = -ENODEV;
12408 		}
12409 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12410 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12411 			return bnxt_fw_reset_via_optee(bp);
12412 		}
12413 		return rc;
12414 	}
12415 
12416 	return -ENODEV;
12417 }
12418 
12419 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12420 {
12421 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12422 
12423 	if (!BNXT_NEW_RM(bp))
12424 		return; /* no resource reservations required */
12425 
12426 	hw_resc->resv_cp_rings = 0;
12427 	hw_resc->resv_stat_ctxs = 0;
12428 	hw_resc->resv_irqs = 0;
12429 	hw_resc->resv_tx_rings = 0;
12430 	hw_resc->resv_rx_rings = 0;
12431 	hw_resc->resv_hw_ring_grps = 0;
12432 	hw_resc->resv_vnics = 0;
12433 	hw_resc->resv_rsscos_ctxs = 0;
12434 	if (!fw_reset) {
12435 		bp->tx_nr_rings = 0;
12436 		bp->rx_nr_rings = 0;
12437 	}
12438 }
12439 
12440 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12441 {
12442 	int rc;
12443 
12444 	if (!BNXT_NEW_RM(bp))
12445 		return 0; /* no resource reservations required */
12446 
12447 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12448 	if (rc)
12449 		netdev_err(bp->dev, "resc_qcaps failed\n");
12450 
12451 	bnxt_clear_reservations(bp, fw_reset);
12452 
12453 	return rc;
12454 }
12455 
12456 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12457 {
12458 	struct hwrm_func_drv_if_change_output *resp;
12459 	struct hwrm_func_drv_if_change_input *req;
12460 	bool resc_reinit = false;
12461 	bool caps_change = false;
12462 	int rc, retry = 0;
12463 	bool fw_reset;
12464 	u32 flags = 0;
12465 
12466 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12467 	bp->fw_reset_state = 0;
12468 
12469 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12470 		return 0;
12471 
12472 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12473 	if (rc)
12474 		return rc;
12475 
12476 	if (up)
12477 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12478 	resp = hwrm_req_hold(bp, req);
12479 
12480 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12481 	while (retry < BNXT_FW_IF_RETRY) {
12482 		rc = hwrm_req_send(bp, req);
12483 		if (rc != -EAGAIN)
12484 			break;
12485 
12486 		msleep(50);
12487 		retry++;
12488 	}
12489 
12490 	if (rc == -EAGAIN) {
12491 		hwrm_req_drop(bp, req);
12492 		return rc;
12493 	} else if (!rc) {
12494 		flags = le32_to_cpu(resp->flags);
12495 	} else if (up) {
12496 		rc = bnxt_try_recover_fw(bp);
12497 		fw_reset = true;
12498 	}
12499 	hwrm_req_drop(bp, req);
12500 	if (rc)
12501 		return rc;
12502 
12503 	if (!up) {
12504 		bnxt_inv_fw_health_reg(bp);
12505 		return 0;
12506 	}
12507 
12508 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12509 		resc_reinit = true;
12510 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12511 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12512 		fw_reset = true;
12513 	else
12514 		bnxt_remap_fw_health_regs(bp);
12515 
12516 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12517 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12518 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12519 		return -ENODEV;
12520 	}
12521 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12522 		caps_change = true;
12523 
12524 	if (resc_reinit || fw_reset || caps_change) {
12525 		if (fw_reset || caps_change) {
12526 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12527 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12528 				bnxt_ulp_irq_stop(bp);
12529 			bnxt_free_ctx_mem(bp, false);
12530 			bnxt_dcb_free(bp);
12531 			rc = bnxt_fw_init_one(bp);
12532 			if (rc) {
12533 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12534 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12535 				return rc;
12536 			}
12537 			/* IRQ will be initialized later in bnxt_request_irq()*/
12538 			bnxt_clear_int_mode(bp);
12539 		}
12540 		rc = bnxt_cancel_reservations(bp, fw_reset);
12541 	}
12542 	return rc;
12543 }
12544 
12545 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12546 {
12547 	struct hwrm_port_led_qcaps_output *resp;
12548 	struct hwrm_port_led_qcaps_input *req;
12549 	struct bnxt_pf_info *pf = &bp->pf;
12550 	int rc;
12551 
12552 	bp->num_leds = 0;
12553 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12554 		return 0;
12555 
12556 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12557 	if (rc)
12558 		return rc;
12559 
12560 	req->port_id = cpu_to_le16(pf->port_id);
12561 	resp = hwrm_req_hold(bp, req);
12562 	rc = hwrm_req_send(bp, req);
12563 	if (rc) {
12564 		hwrm_req_drop(bp, req);
12565 		return rc;
12566 	}
12567 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12568 		int i;
12569 
12570 		bp->num_leds = resp->num_leds;
12571 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12572 						 bp->num_leds);
12573 		for (i = 0; i < bp->num_leds; i++) {
12574 			struct bnxt_led_info *led = &bp->leds[i];
12575 			__le16 caps = led->led_state_caps;
12576 
12577 			if (!led->led_group_id ||
12578 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12579 				bp->num_leds = 0;
12580 				break;
12581 			}
12582 		}
12583 	}
12584 	hwrm_req_drop(bp, req);
12585 	return 0;
12586 }
12587 
12588 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12589 {
12590 	struct hwrm_wol_filter_alloc_output *resp;
12591 	struct hwrm_wol_filter_alloc_input *req;
12592 	int rc;
12593 
12594 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12595 	if (rc)
12596 		return rc;
12597 
12598 	req->port_id = cpu_to_le16(bp->pf.port_id);
12599 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12600 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12601 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12602 
12603 	resp = hwrm_req_hold(bp, req);
12604 	rc = hwrm_req_send(bp, req);
12605 	if (!rc)
12606 		bp->wol_filter_id = resp->wol_filter_id;
12607 	hwrm_req_drop(bp, req);
12608 	return rc;
12609 }
12610 
12611 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12612 {
12613 	struct hwrm_wol_filter_free_input *req;
12614 	int rc;
12615 
12616 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12617 	if (rc)
12618 		return rc;
12619 
12620 	req->port_id = cpu_to_le16(bp->pf.port_id);
12621 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12622 	req->wol_filter_id = bp->wol_filter_id;
12623 
12624 	return hwrm_req_send(bp, req);
12625 }
12626 
12627 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12628 {
12629 	struct hwrm_wol_filter_qcfg_output *resp;
12630 	struct hwrm_wol_filter_qcfg_input *req;
12631 	u16 next_handle = 0;
12632 	int rc;
12633 
12634 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12635 	if (rc)
12636 		return rc;
12637 
12638 	req->port_id = cpu_to_le16(bp->pf.port_id);
12639 	req->handle = cpu_to_le16(handle);
12640 	resp = hwrm_req_hold(bp, req);
12641 	rc = hwrm_req_send(bp, req);
12642 	if (!rc) {
12643 		next_handle = le16_to_cpu(resp->next_handle);
12644 		if (next_handle != 0) {
12645 			if (resp->wol_type ==
12646 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12647 				bp->wol = 1;
12648 				bp->wol_filter_id = resp->wol_filter_id;
12649 			}
12650 		}
12651 	}
12652 	hwrm_req_drop(bp, req);
12653 	return next_handle;
12654 }
12655 
12656 static void bnxt_get_wol_settings(struct bnxt *bp)
12657 {
12658 	u16 handle = 0;
12659 
12660 	bp->wol = 0;
12661 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12662 		return;
12663 
12664 	do {
12665 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12666 	} while (handle && handle != 0xffff);
12667 }
12668 
12669 static bool bnxt_eee_config_ok(struct bnxt *bp)
12670 {
12671 	struct ethtool_keee *eee = &bp->eee;
12672 	struct bnxt_link_info *link_info = &bp->link_info;
12673 
12674 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12675 		return true;
12676 
12677 	if (eee->eee_enabled) {
12678 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12679 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12680 
12681 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12682 
12683 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12684 			eee->eee_enabled = 0;
12685 			return false;
12686 		}
12687 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12688 			linkmode_and(eee->advertised, advertising,
12689 				     eee->supported);
12690 			return false;
12691 		}
12692 	}
12693 	return true;
12694 }
12695 
12696 static int bnxt_update_phy_setting(struct bnxt *bp)
12697 {
12698 	int rc;
12699 	bool update_link = false;
12700 	bool update_pause = false;
12701 	bool update_eee = false;
12702 	struct bnxt_link_info *link_info = &bp->link_info;
12703 
12704 	rc = bnxt_update_link(bp, true);
12705 	if (rc) {
12706 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12707 			   rc);
12708 		return rc;
12709 	}
12710 	if (!BNXT_SINGLE_PF(bp))
12711 		return 0;
12712 
12713 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12714 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12715 	    link_info->req_flow_ctrl)
12716 		update_pause = true;
12717 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12718 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12719 		update_pause = true;
12720 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12721 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12722 			update_link = true;
12723 		if (bnxt_force_speed_updated(link_info))
12724 			update_link = true;
12725 		if (link_info->req_duplex != link_info->duplex_setting)
12726 			update_link = true;
12727 	} else {
12728 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12729 			update_link = true;
12730 		if (bnxt_auto_speed_updated(link_info))
12731 			update_link = true;
12732 	}
12733 
12734 	/* The last close may have shutdown the link, so need to call
12735 	 * PHY_CFG to bring it back up.
12736 	 */
12737 	if (!BNXT_LINK_IS_UP(bp))
12738 		update_link = true;
12739 
12740 	if (!bnxt_eee_config_ok(bp))
12741 		update_eee = true;
12742 
12743 	if (update_link)
12744 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12745 	else if (update_pause)
12746 		rc = bnxt_hwrm_set_pause(bp);
12747 	if (rc) {
12748 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12749 			   rc);
12750 		return rc;
12751 	}
12752 
12753 	return rc;
12754 }
12755 
12756 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12757 
12758 static int bnxt_reinit_after_abort(struct bnxt *bp)
12759 {
12760 	int rc;
12761 
12762 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12763 		return -EBUSY;
12764 
12765 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12766 		return -ENODEV;
12767 
12768 	rc = bnxt_fw_init_one(bp);
12769 	if (!rc) {
12770 		bnxt_clear_int_mode(bp);
12771 		rc = bnxt_init_int_mode(bp);
12772 		if (!rc) {
12773 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12774 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12775 		}
12776 	}
12777 	return rc;
12778 }
12779 
12780 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12781 {
12782 	struct bnxt_ntuple_filter *ntp_fltr;
12783 	struct bnxt_l2_filter *l2_fltr;
12784 
12785 	if (list_empty(&fltr->list))
12786 		return;
12787 
12788 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12789 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12790 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12791 		atomic_inc(&l2_fltr->refcnt);
12792 		ntp_fltr->l2_fltr = l2_fltr;
12793 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12794 			bnxt_del_ntp_filter(bp, ntp_fltr);
12795 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12796 				   fltr->sw_id);
12797 		}
12798 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12799 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12800 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12801 			bnxt_del_l2_filter(bp, l2_fltr);
12802 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12803 				   fltr->sw_id);
12804 		}
12805 	}
12806 }
12807 
12808 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12809 {
12810 	struct bnxt_filter_base *usr_fltr, *tmp;
12811 
12812 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12813 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12814 }
12815 
12816 static int bnxt_set_xps_mapping(struct bnxt *bp)
12817 {
12818 	int numa_node = dev_to_node(&bp->pdev->dev);
12819 	unsigned int q_idx, map_idx, cpu, i;
12820 	const struct cpumask *cpu_mask_ptr;
12821 	int nr_cpus = num_online_cpus();
12822 	cpumask_t *q_map;
12823 	int rc = 0;
12824 
12825 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12826 	if (!q_map)
12827 		return -ENOMEM;
12828 
12829 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12830 	 * Each TC has the same number of TX queues. The nth TX queue for each
12831 	 * TC will have the same CPU mask.
12832 	 */
12833 	for (i = 0; i < nr_cpus; i++) {
12834 		map_idx = i % bp->tx_nr_rings_per_tc;
12835 		cpu = cpumask_local_spread(i, numa_node);
12836 		cpu_mask_ptr = get_cpu_mask(cpu);
12837 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12838 	}
12839 
12840 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12841 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12842 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12843 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12844 		if (rc) {
12845 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12846 				    q_idx);
12847 			break;
12848 		}
12849 	}
12850 
12851 	kfree(q_map);
12852 
12853 	return rc;
12854 }
12855 
12856 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12857 {
12858 	int rc = 0;
12859 
12860 	netif_carrier_off(bp->dev);
12861 	if (irq_re_init) {
12862 		/* Reserve rings now if none were reserved at driver probe. */
12863 		rc = bnxt_init_dflt_ring_mode(bp);
12864 		if (rc) {
12865 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12866 			return rc;
12867 		}
12868 	}
12869 	rc = bnxt_reserve_rings(bp, irq_re_init);
12870 	if (rc)
12871 		return rc;
12872 
12873 	rc = bnxt_alloc_mem(bp, irq_re_init);
12874 	if (rc) {
12875 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12876 		goto open_err_free_mem;
12877 	}
12878 
12879 	if (irq_re_init) {
12880 		bnxt_init_napi(bp);
12881 		rc = bnxt_request_irq(bp);
12882 		if (rc) {
12883 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12884 			goto open_err_irq;
12885 		}
12886 	}
12887 
12888 	rc = bnxt_init_nic(bp, irq_re_init);
12889 	if (rc) {
12890 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12891 		goto open_err_irq;
12892 	}
12893 
12894 	bnxt_enable_napi(bp);
12895 	bnxt_debug_dev_init(bp);
12896 
12897 	if (link_re_init) {
12898 		mutex_lock(&bp->link_lock);
12899 		rc = bnxt_update_phy_setting(bp);
12900 		mutex_unlock(&bp->link_lock);
12901 		if (rc) {
12902 			netdev_warn(bp->dev, "failed to update phy settings\n");
12903 			if (BNXT_SINGLE_PF(bp)) {
12904 				bp->link_info.phy_retry = true;
12905 				bp->link_info.phy_retry_expires =
12906 					jiffies + 5 * HZ;
12907 			}
12908 		}
12909 	}
12910 
12911 	if (irq_re_init) {
12912 		udp_tunnel_nic_reset_ntf(bp->dev);
12913 		rc = bnxt_set_xps_mapping(bp);
12914 		if (rc)
12915 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12916 	}
12917 
12918 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12919 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12920 			static_branch_enable(&bnxt_xdp_locking_key);
12921 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12922 		static_branch_disable(&bnxt_xdp_locking_key);
12923 	}
12924 	set_bit(BNXT_STATE_OPEN, &bp->state);
12925 	bnxt_enable_int(bp);
12926 	/* Enable TX queues */
12927 	bnxt_tx_enable(bp);
12928 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12929 	/* Poll link status and check for SFP+ module status */
12930 	mutex_lock(&bp->link_lock);
12931 	bnxt_get_port_module_status(bp);
12932 	mutex_unlock(&bp->link_lock);
12933 
12934 	/* VF-reps may need to be re-opened after the PF is re-opened */
12935 	if (BNXT_PF(bp))
12936 		bnxt_vf_reps_open(bp);
12937 	bnxt_ptp_init_rtc(bp, true);
12938 	bnxt_ptp_cfg_tstamp_filters(bp);
12939 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12940 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12941 	bnxt_cfg_usr_fltrs(bp);
12942 	return 0;
12943 
12944 open_err_irq:
12945 	bnxt_del_napi(bp);
12946 
12947 open_err_free_mem:
12948 	bnxt_free_skbs(bp);
12949 	bnxt_free_irq(bp);
12950 	bnxt_free_mem(bp, true);
12951 	return rc;
12952 }
12953 
12954 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12955 {
12956 	int rc = 0;
12957 
12958 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12959 		rc = -EIO;
12960 	if (!rc)
12961 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12962 	if (rc) {
12963 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12964 		netif_close(bp->dev);
12965 	}
12966 	return rc;
12967 }
12968 
12969 /* netdev instance lock held, open the NIC half way by allocating all
12970  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12971  * for offline self tests.
12972  */
12973 int bnxt_half_open_nic(struct bnxt *bp)
12974 {
12975 	int rc = 0;
12976 
12977 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12978 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12979 		rc = -ENODEV;
12980 		goto half_open_err;
12981 	}
12982 
12983 	rc = bnxt_alloc_mem(bp, true);
12984 	if (rc) {
12985 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12986 		goto half_open_err;
12987 	}
12988 	bnxt_init_napi(bp);
12989 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12990 	rc = bnxt_init_nic(bp, true);
12991 	if (rc) {
12992 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12993 		bnxt_del_napi(bp);
12994 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12995 		goto half_open_err;
12996 	}
12997 	return 0;
12998 
12999 half_open_err:
13000 	bnxt_free_skbs(bp);
13001 	bnxt_free_mem(bp, true);
13002 	netif_close(bp->dev);
13003 	return rc;
13004 }
13005 
13006 /* netdev instance lock held, this call can only be made after a previous
13007  * successful call to bnxt_half_open_nic().
13008  */
13009 void bnxt_half_close_nic(struct bnxt *bp)
13010 {
13011 	bnxt_hwrm_resource_free(bp, false, true);
13012 	bnxt_del_napi(bp);
13013 	bnxt_free_skbs(bp);
13014 	bnxt_free_mem(bp, true);
13015 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13016 }
13017 
13018 void bnxt_reenable_sriov(struct bnxt *bp)
13019 {
13020 	if (BNXT_PF(bp)) {
13021 		struct bnxt_pf_info *pf = &bp->pf;
13022 		int n = pf->active_vfs;
13023 
13024 		if (n)
13025 			bnxt_cfg_hw_sriov(bp, &n, true);
13026 	}
13027 }
13028 
13029 static int bnxt_open(struct net_device *dev)
13030 {
13031 	struct bnxt *bp = netdev_priv(dev);
13032 	int rc;
13033 
13034 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13035 		rc = bnxt_reinit_after_abort(bp);
13036 		if (rc) {
13037 			if (rc == -EBUSY)
13038 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13039 			else
13040 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13041 			return -ENODEV;
13042 		}
13043 	}
13044 
13045 	rc = bnxt_hwrm_if_change(bp, true);
13046 	if (rc)
13047 		return rc;
13048 
13049 	rc = __bnxt_open_nic(bp, true, true);
13050 	if (rc) {
13051 		bnxt_hwrm_if_change(bp, false);
13052 	} else {
13053 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13054 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13055 				bnxt_queue_sp_work(bp,
13056 						   BNXT_RESTART_ULP_SP_EVENT);
13057 		}
13058 	}
13059 
13060 	return rc;
13061 }
13062 
13063 static bool bnxt_drv_busy(struct bnxt *bp)
13064 {
13065 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13066 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13067 }
13068 
13069 static void bnxt_get_ring_stats(struct bnxt *bp,
13070 				struct rtnl_link_stats64 *stats);
13071 
13072 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13073 			     bool link_re_init)
13074 {
13075 	/* Close the VF-reps before closing PF */
13076 	if (BNXT_PF(bp))
13077 		bnxt_vf_reps_close(bp);
13078 
13079 	/* Change device state to avoid TX queue wake up's */
13080 	bnxt_tx_disable(bp);
13081 
13082 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13083 	smp_mb__after_atomic();
13084 	while (bnxt_drv_busy(bp))
13085 		msleep(20);
13086 
13087 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13088 		bnxt_clear_rss_ctxs(bp);
13089 	/* Flush rings and disable interrupts */
13090 	bnxt_shutdown_nic(bp, irq_re_init);
13091 
13092 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13093 
13094 	bnxt_debug_dev_exit(bp);
13095 	bnxt_disable_napi(bp);
13096 	timer_delete_sync(&bp->timer);
13097 	bnxt_free_skbs(bp);
13098 
13099 	/* Save ring stats before shutdown */
13100 	if (bp->bnapi && irq_re_init) {
13101 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13102 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13103 	}
13104 	if (irq_re_init) {
13105 		bnxt_free_irq(bp);
13106 		bnxt_del_napi(bp);
13107 	}
13108 	bnxt_free_mem(bp, irq_re_init);
13109 }
13110 
13111 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13112 {
13113 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13114 		/* If we get here, it means firmware reset is in progress
13115 		 * while we are trying to close.  We can safely proceed with
13116 		 * the close because we are holding netdev instance lock.
13117 		 * Some firmware messages may fail as we proceed to close.
13118 		 * We set the ABORT_ERR flag here so that the FW reset thread
13119 		 * will later abort when it gets the netdev instance lock
13120 		 * and sees the flag.
13121 		 */
13122 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13123 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13124 	}
13125 
13126 #ifdef CONFIG_BNXT_SRIOV
13127 	if (bp->sriov_cfg) {
13128 		int rc;
13129 
13130 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13131 						      !bp->sriov_cfg,
13132 						      BNXT_SRIOV_CFG_WAIT_TMO);
13133 		if (!rc)
13134 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13135 		else if (rc < 0)
13136 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13137 	}
13138 #endif
13139 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13140 }
13141 
13142 static int bnxt_close(struct net_device *dev)
13143 {
13144 	struct bnxt *bp = netdev_priv(dev);
13145 
13146 	bnxt_close_nic(bp, true, true);
13147 	bnxt_hwrm_shutdown_link(bp);
13148 	bnxt_hwrm_if_change(bp, false);
13149 	return 0;
13150 }
13151 
13152 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13153 				   u16 *val)
13154 {
13155 	struct hwrm_port_phy_mdio_read_output *resp;
13156 	struct hwrm_port_phy_mdio_read_input *req;
13157 	int rc;
13158 
13159 	if (bp->hwrm_spec_code < 0x10a00)
13160 		return -EOPNOTSUPP;
13161 
13162 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13163 	if (rc)
13164 		return rc;
13165 
13166 	req->port_id = cpu_to_le16(bp->pf.port_id);
13167 	req->phy_addr = phy_addr;
13168 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13169 	if (mdio_phy_id_is_c45(phy_addr)) {
13170 		req->cl45_mdio = 1;
13171 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13172 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13173 		req->reg_addr = cpu_to_le16(reg);
13174 	}
13175 
13176 	resp = hwrm_req_hold(bp, req);
13177 	rc = hwrm_req_send(bp, req);
13178 	if (!rc)
13179 		*val = le16_to_cpu(resp->reg_data);
13180 	hwrm_req_drop(bp, req);
13181 	return rc;
13182 }
13183 
13184 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13185 				    u16 val)
13186 {
13187 	struct hwrm_port_phy_mdio_write_input *req;
13188 	int rc;
13189 
13190 	if (bp->hwrm_spec_code < 0x10a00)
13191 		return -EOPNOTSUPP;
13192 
13193 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13194 	if (rc)
13195 		return rc;
13196 
13197 	req->port_id = cpu_to_le16(bp->pf.port_id);
13198 	req->phy_addr = phy_addr;
13199 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13200 	if (mdio_phy_id_is_c45(phy_addr)) {
13201 		req->cl45_mdio = 1;
13202 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13203 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13204 		req->reg_addr = cpu_to_le16(reg);
13205 	}
13206 	req->reg_data = cpu_to_le16(val);
13207 
13208 	return hwrm_req_send(bp, req);
13209 }
13210 
13211 /* netdev instance lock held */
13212 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13213 {
13214 	struct mii_ioctl_data *mdio = if_mii(ifr);
13215 	struct bnxt *bp = netdev_priv(dev);
13216 	int rc;
13217 
13218 	switch (cmd) {
13219 	case SIOCGMIIPHY:
13220 		mdio->phy_id = bp->link_info.phy_addr;
13221 
13222 		fallthrough;
13223 	case SIOCGMIIREG: {
13224 		u16 mii_regval = 0;
13225 
13226 		if (!netif_running(dev))
13227 			return -EAGAIN;
13228 
13229 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13230 					     &mii_regval);
13231 		mdio->val_out = mii_regval;
13232 		return rc;
13233 	}
13234 
13235 	case SIOCSMIIREG:
13236 		if (!netif_running(dev))
13237 			return -EAGAIN;
13238 
13239 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13240 						mdio->val_in);
13241 
13242 	case SIOCSHWTSTAMP:
13243 		return bnxt_hwtstamp_set(dev, ifr);
13244 
13245 	case SIOCGHWTSTAMP:
13246 		return bnxt_hwtstamp_get(dev, ifr);
13247 
13248 	default:
13249 		/* do nothing */
13250 		break;
13251 	}
13252 	return -EOPNOTSUPP;
13253 }
13254 
13255 static void bnxt_get_ring_stats(struct bnxt *bp,
13256 				struct rtnl_link_stats64 *stats)
13257 {
13258 	int i;
13259 
13260 	for (i = 0; i < bp->cp_nr_rings; i++) {
13261 		struct bnxt_napi *bnapi = bp->bnapi[i];
13262 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13263 		u64 *sw = cpr->stats.sw_stats;
13264 
13265 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13266 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13267 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13268 
13269 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13270 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13271 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13272 
13273 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13274 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13275 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13276 
13277 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13278 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13279 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13280 
13281 		stats->rx_missed_errors +=
13282 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13283 
13284 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13285 
13286 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13287 
13288 		stats->rx_dropped +=
13289 			cpr->sw_stats->rx.rx_netpoll_discards +
13290 			cpr->sw_stats->rx.rx_oom_discards;
13291 	}
13292 }
13293 
13294 static void bnxt_add_prev_stats(struct bnxt *bp,
13295 				struct rtnl_link_stats64 *stats)
13296 {
13297 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13298 
13299 	stats->rx_packets += prev_stats->rx_packets;
13300 	stats->tx_packets += prev_stats->tx_packets;
13301 	stats->rx_bytes += prev_stats->rx_bytes;
13302 	stats->tx_bytes += prev_stats->tx_bytes;
13303 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13304 	stats->multicast += prev_stats->multicast;
13305 	stats->rx_dropped += prev_stats->rx_dropped;
13306 	stats->tx_dropped += prev_stats->tx_dropped;
13307 }
13308 
13309 static void
13310 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13311 {
13312 	struct bnxt *bp = netdev_priv(dev);
13313 
13314 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13315 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13316 	 * we check the BNXT_STATE_OPEN flag.
13317 	 */
13318 	smp_mb__after_atomic();
13319 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13320 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13321 		*stats = bp->net_stats_prev;
13322 		return;
13323 	}
13324 
13325 	bnxt_get_ring_stats(bp, stats);
13326 	bnxt_add_prev_stats(bp, stats);
13327 
13328 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13329 		u64 *rx = bp->port_stats.sw_stats;
13330 		u64 *tx = bp->port_stats.sw_stats +
13331 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13332 
13333 		stats->rx_crc_errors =
13334 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13335 		stats->rx_frame_errors =
13336 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13337 		stats->rx_length_errors =
13338 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13339 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13340 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13341 		stats->rx_errors =
13342 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13343 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13344 		stats->collisions =
13345 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13346 		stats->tx_fifo_errors =
13347 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13348 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13349 	}
13350 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13351 }
13352 
13353 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13354 					struct bnxt_total_ring_err_stats *stats,
13355 					struct bnxt_cp_ring_info *cpr)
13356 {
13357 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13358 	u64 *hw_stats = cpr->stats.sw_stats;
13359 
13360 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13361 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13362 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13363 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13364 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13365 	stats->rx_total_ring_discards +=
13366 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13367 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13368 	stats->tx_total_ring_discards +=
13369 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13370 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13371 }
13372 
13373 void bnxt_get_ring_err_stats(struct bnxt *bp,
13374 			     struct bnxt_total_ring_err_stats *stats)
13375 {
13376 	int i;
13377 
13378 	for (i = 0; i < bp->cp_nr_rings; i++)
13379 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13380 }
13381 
13382 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13383 {
13384 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13385 	struct net_device *dev = bp->dev;
13386 	struct netdev_hw_addr *ha;
13387 	u8 *haddr;
13388 	int mc_count = 0;
13389 	bool update = false;
13390 	int off = 0;
13391 
13392 	netdev_for_each_mc_addr(ha, dev) {
13393 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13394 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13395 			vnic->mc_list_count = 0;
13396 			return false;
13397 		}
13398 		haddr = ha->addr;
13399 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13400 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13401 			update = true;
13402 		}
13403 		off += ETH_ALEN;
13404 		mc_count++;
13405 	}
13406 	if (mc_count)
13407 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13408 
13409 	if (mc_count != vnic->mc_list_count) {
13410 		vnic->mc_list_count = mc_count;
13411 		update = true;
13412 	}
13413 	return update;
13414 }
13415 
13416 static bool bnxt_uc_list_updated(struct bnxt *bp)
13417 {
13418 	struct net_device *dev = bp->dev;
13419 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13420 	struct netdev_hw_addr *ha;
13421 	int off = 0;
13422 
13423 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13424 		return true;
13425 
13426 	netdev_for_each_uc_addr(ha, dev) {
13427 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13428 			return true;
13429 
13430 		off += ETH_ALEN;
13431 	}
13432 	return false;
13433 }
13434 
13435 static void bnxt_set_rx_mode(struct net_device *dev)
13436 {
13437 	struct bnxt *bp = netdev_priv(dev);
13438 	struct bnxt_vnic_info *vnic;
13439 	bool mc_update = false;
13440 	bool uc_update;
13441 	u32 mask;
13442 
13443 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13444 		return;
13445 
13446 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13447 	mask = vnic->rx_mask;
13448 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13449 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13450 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13451 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13452 
13453 	if (dev->flags & IFF_PROMISC)
13454 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13455 
13456 	uc_update = bnxt_uc_list_updated(bp);
13457 
13458 	if (dev->flags & IFF_BROADCAST)
13459 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13460 	if (dev->flags & IFF_ALLMULTI) {
13461 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13462 		vnic->mc_list_count = 0;
13463 	} else if (dev->flags & IFF_MULTICAST) {
13464 		mc_update = bnxt_mc_list_updated(bp, &mask);
13465 	}
13466 
13467 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13468 		vnic->rx_mask = mask;
13469 
13470 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13471 	}
13472 }
13473 
13474 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13475 {
13476 	struct net_device *dev = bp->dev;
13477 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13478 	struct netdev_hw_addr *ha;
13479 	int i, off = 0, rc;
13480 	bool uc_update;
13481 
13482 	netif_addr_lock_bh(dev);
13483 	uc_update = bnxt_uc_list_updated(bp);
13484 	netif_addr_unlock_bh(dev);
13485 
13486 	if (!uc_update)
13487 		goto skip_uc;
13488 
13489 	for (i = 1; i < vnic->uc_filter_count; i++) {
13490 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13491 
13492 		bnxt_hwrm_l2_filter_free(bp, fltr);
13493 		bnxt_del_l2_filter(bp, fltr);
13494 	}
13495 
13496 	vnic->uc_filter_count = 1;
13497 
13498 	netif_addr_lock_bh(dev);
13499 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13500 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13501 	} else {
13502 		netdev_for_each_uc_addr(ha, dev) {
13503 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13504 			off += ETH_ALEN;
13505 			vnic->uc_filter_count++;
13506 		}
13507 	}
13508 	netif_addr_unlock_bh(dev);
13509 
13510 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13511 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13512 		if (rc) {
13513 			if (BNXT_VF(bp) && rc == -ENODEV) {
13514 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13515 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13516 				else
13517 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13518 				rc = 0;
13519 			} else {
13520 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13521 			}
13522 			vnic->uc_filter_count = i;
13523 			return rc;
13524 		}
13525 	}
13526 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13527 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13528 
13529 skip_uc:
13530 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13531 	    !bnxt_promisc_ok(bp))
13532 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13533 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13534 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13535 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13536 			    rc);
13537 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13538 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13539 		vnic->mc_list_count = 0;
13540 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13541 	}
13542 	if (rc)
13543 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13544 			   rc);
13545 
13546 	return rc;
13547 }
13548 
13549 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13550 {
13551 #ifdef CONFIG_BNXT_SRIOV
13552 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13553 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13554 
13555 		/* No minimum rings were provisioned by the PF.  Don't
13556 		 * reserve rings by default when device is down.
13557 		 */
13558 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13559 			return true;
13560 
13561 		if (!netif_running(bp->dev))
13562 			return false;
13563 	}
13564 #endif
13565 	return true;
13566 }
13567 
13568 /* If the chip and firmware supports RFS */
13569 static bool bnxt_rfs_supported(struct bnxt *bp)
13570 {
13571 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13572 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13573 			return true;
13574 		return false;
13575 	}
13576 	/* 212 firmware is broken for aRFS */
13577 	if (BNXT_FW_MAJ(bp) == 212)
13578 		return false;
13579 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13580 		return true;
13581 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13582 		return true;
13583 	return false;
13584 }
13585 
13586 /* If runtime conditions support RFS */
13587 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13588 {
13589 	struct bnxt_hw_rings hwr = {0};
13590 	int max_vnics, max_rss_ctxs;
13591 
13592 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13593 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13594 		return bnxt_rfs_supported(bp);
13595 
13596 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13597 		return false;
13598 
13599 	hwr.grp = bp->rx_nr_rings;
13600 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13601 	if (new_rss_ctx)
13602 		hwr.vnic++;
13603 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13604 	max_vnics = bnxt_get_max_func_vnics(bp);
13605 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13606 
13607 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13608 		if (bp->rx_nr_rings > 1)
13609 			netdev_warn(bp->dev,
13610 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13611 				    min(max_rss_ctxs - 1, max_vnics - 1));
13612 		return false;
13613 	}
13614 
13615 	if (!BNXT_NEW_RM(bp))
13616 		return true;
13617 
13618 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13619 	 * issue that will mess up the default VNIC if we reduce the
13620 	 * reservations.
13621 	 */
13622 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13623 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13624 		return true;
13625 
13626 	bnxt_hwrm_reserve_rings(bp, &hwr);
13627 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13628 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13629 		return true;
13630 
13631 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13632 	hwr.vnic = 1;
13633 	hwr.rss_ctx = 0;
13634 	bnxt_hwrm_reserve_rings(bp, &hwr);
13635 	return false;
13636 }
13637 
13638 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13639 					   netdev_features_t features)
13640 {
13641 	struct bnxt *bp = netdev_priv(dev);
13642 	netdev_features_t vlan_features;
13643 
13644 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13645 		features &= ~NETIF_F_NTUPLE;
13646 
13647 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13648 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13649 
13650 	if (!(features & NETIF_F_GRO))
13651 		features &= ~NETIF_F_GRO_HW;
13652 
13653 	if (features & NETIF_F_GRO_HW)
13654 		features &= ~NETIF_F_LRO;
13655 
13656 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13657 	 * turned on or off together.
13658 	 */
13659 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13660 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13661 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13662 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13663 		else if (vlan_features)
13664 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13665 	}
13666 #ifdef CONFIG_BNXT_SRIOV
13667 	if (BNXT_VF(bp) && bp->vf.vlan)
13668 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13669 #endif
13670 	return features;
13671 }
13672 
13673 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13674 				bool link_re_init, u32 flags, bool update_tpa)
13675 {
13676 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13677 	bp->flags = flags;
13678 	if (update_tpa)
13679 		bnxt_set_ring_params(bp);
13680 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13681 }
13682 
13683 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13684 {
13685 	bool update_tpa = false, update_ntuple = false;
13686 	struct bnxt *bp = netdev_priv(dev);
13687 	u32 flags = bp->flags;
13688 	u32 changes;
13689 	int rc = 0;
13690 	bool re_init = false;
13691 
13692 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13693 	if (features & NETIF_F_GRO_HW)
13694 		flags |= BNXT_FLAG_GRO;
13695 	else if (features & NETIF_F_LRO)
13696 		flags |= BNXT_FLAG_LRO;
13697 
13698 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13699 		flags &= ~BNXT_FLAG_TPA;
13700 
13701 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13702 		flags |= BNXT_FLAG_STRIP_VLAN;
13703 
13704 	if (features & NETIF_F_NTUPLE)
13705 		flags |= BNXT_FLAG_RFS;
13706 	else
13707 		bnxt_clear_usr_fltrs(bp, true);
13708 
13709 	changes = flags ^ bp->flags;
13710 	if (changes & BNXT_FLAG_TPA) {
13711 		update_tpa = true;
13712 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13713 		    (flags & BNXT_FLAG_TPA) == 0 ||
13714 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13715 			re_init = true;
13716 	}
13717 
13718 	if (changes & ~BNXT_FLAG_TPA)
13719 		re_init = true;
13720 
13721 	if (changes & BNXT_FLAG_RFS)
13722 		update_ntuple = true;
13723 
13724 	if (flags != bp->flags) {
13725 		u32 old_flags = bp->flags;
13726 
13727 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13728 			bp->flags = flags;
13729 			if (update_tpa)
13730 				bnxt_set_ring_params(bp);
13731 			return rc;
13732 		}
13733 
13734 		if (update_ntuple)
13735 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13736 
13737 		if (re_init)
13738 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13739 
13740 		if (update_tpa) {
13741 			bp->flags = flags;
13742 			rc = bnxt_set_tpa(bp,
13743 					  (flags & BNXT_FLAG_TPA) ?
13744 					  true : false);
13745 			if (rc)
13746 				bp->flags = old_flags;
13747 		}
13748 	}
13749 	return rc;
13750 }
13751 
13752 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13753 			      u8 **nextp)
13754 {
13755 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13756 	struct hop_jumbo_hdr *jhdr;
13757 	int hdr_count = 0;
13758 	u8 *nexthdr;
13759 	int start;
13760 
13761 	/* Check that there are at most 2 IPv6 extension headers, no
13762 	 * fragment header, and each is <= 64 bytes.
13763 	 */
13764 	start = nw_off + sizeof(*ip6h);
13765 	nexthdr = &ip6h->nexthdr;
13766 	while (ipv6_ext_hdr(*nexthdr)) {
13767 		struct ipv6_opt_hdr *hp;
13768 		int hdrlen;
13769 
13770 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13771 		    *nexthdr == NEXTHDR_FRAGMENT)
13772 			return false;
13773 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13774 					  skb_headlen(skb), NULL);
13775 		if (!hp)
13776 			return false;
13777 		if (*nexthdr == NEXTHDR_AUTH)
13778 			hdrlen = ipv6_authlen(hp);
13779 		else
13780 			hdrlen = ipv6_optlen(hp);
13781 
13782 		if (hdrlen > 64)
13783 			return false;
13784 
13785 		/* The ext header may be a hop-by-hop header inserted for
13786 		 * big TCP purposes. This will be removed before sending
13787 		 * from NIC, so do not count it.
13788 		 */
13789 		if (*nexthdr == NEXTHDR_HOP) {
13790 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13791 				goto increment_hdr;
13792 
13793 			jhdr = (struct hop_jumbo_hdr *)hp;
13794 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13795 			    jhdr->nexthdr != IPPROTO_TCP)
13796 				goto increment_hdr;
13797 
13798 			goto next_hdr;
13799 		}
13800 increment_hdr:
13801 		hdr_count++;
13802 next_hdr:
13803 		nexthdr = &hp->nexthdr;
13804 		start += hdrlen;
13805 	}
13806 	if (nextp) {
13807 		/* Caller will check inner protocol */
13808 		if (skb->encapsulation) {
13809 			*nextp = nexthdr;
13810 			return true;
13811 		}
13812 		*nextp = NULL;
13813 	}
13814 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13815 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13816 }
13817 
13818 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13819 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13820 {
13821 	struct udphdr *uh = udp_hdr(skb);
13822 	__be16 udp_port = uh->dest;
13823 
13824 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13825 	    udp_port != bp->vxlan_gpe_port)
13826 		return false;
13827 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13828 		struct ethhdr *eh = inner_eth_hdr(skb);
13829 
13830 		switch (eh->h_proto) {
13831 		case htons(ETH_P_IP):
13832 			return true;
13833 		case htons(ETH_P_IPV6):
13834 			return bnxt_exthdr_check(bp, skb,
13835 						 skb_inner_network_offset(skb),
13836 						 NULL);
13837 		}
13838 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13839 		return true;
13840 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13841 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13842 					 NULL);
13843 	}
13844 	return false;
13845 }
13846 
13847 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13848 {
13849 	switch (l4_proto) {
13850 	case IPPROTO_UDP:
13851 		return bnxt_udp_tunl_check(bp, skb);
13852 	case IPPROTO_IPIP:
13853 		return true;
13854 	case IPPROTO_GRE: {
13855 		switch (skb->inner_protocol) {
13856 		default:
13857 			return false;
13858 		case htons(ETH_P_IP):
13859 			return true;
13860 		case htons(ETH_P_IPV6):
13861 			fallthrough;
13862 		}
13863 	}
13864 	case IPPROTO_IPV6:
13865 		/* Check ext headers of inner ipv6 */
13866 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13867 					 NULL);
13868 	}
13869 	return false;
13870 }
13871 
13872 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13873 					     struct net_device *dev,
13874 					     netdev_features_t features)
13875 {
13876 	struct bnxt *bp = netdev_priv(dev);
13877 	u8 *l4_proto;
13878 
13879 	features = vlan_features_check(skb, features);
13880 	switch (vlan_get_protocol(skb)) {
13881 	case htons(ETH_P_IP):
13882 		if (!skb->encapsulation)
13883 			return features;
13884 		l4_proto = &ip_hdr(skb)->protocol;
13885 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13886 			return features;
13887 		break;
13888 	case htons(ETH_P_IPV6):
13889 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13890 				       &l4_proto))
13891 			break;
13892 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13893 			return features;
13894 		break;
13895 	}
13896 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13897 }
13898 
13899 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13900 			 u32 *reg_buf)
13901 {
13902 	struct hwrm_dbg_read_direct_output *resp;
13903 	struct hwrm_dbg_read_direct_input *req;
13904 	__le32 *dbg_reg_buf;
13905 	dma_addr_t mapping;
13906 	int rc, i;
13907 
13908 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13909 	if (rc)
13910 		return rc;
13911 
13912 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13913 					 &mapping);
13914 	if (!dbg_reg_buf) {
13915 		rc = -ENOMEM;
13916 		goto dbg_rd_reg_exit;
13917 	}
13918 
13919 	req->host_dest_addr = cpu_to_le64(mapping);
13920 
13921 	resp = hwrm_req_hold(bp, req);
13922 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13923 	req->read_len32 = cpu_to_le32(num_words);
13924 
13925 	rc = hwrm_req_send(bp, req);
13926 	if (rc || resp->error_code) {
13927 		rc = -EIO;
13928 		goto dbg_rd_reg_exit;
13929 	}
13930 	for (i = 0; i < num_words; i++)
13931 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13932 
13933 dbg_rd_reg_exit:
13934 	hwrm_req_drop(bp, req);
13935 	return rc;
13936 }
13937 
13938 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13939 				       u32 ring_id, u32 *prod, u32 *cons)
13940 {
13941 	struct hwrm_dbg_ring_info_get_output *resp;
13942 	struct hwrm_dbg_ring_info_get_input *req;
13943 	int rc;
13944 
13945 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13946 	if (rc)
13947 		return rc;
13948 
13949 	req->ring_type = ring_type;
13950 	req->fw_ring_id = cpu_to_le32(ring_id);
13951 	resp = hwrm_req_hold(bp, req);
13952 	rc = hwrm_req_send(bp, req);
13953 	if (!rc) {
13954 		*prod = le32_to_cpu(resp->producer_index);
13955 		*cons = le32_to_cpu(resp->consumer_index);
13956 	}
13957 	hwrm_req_drop(bp, req);
13958 	return rc;
13959 }
13960 
13961 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13962 {
13963 	struct bnxt_tx_ring_info *txr;
13964 	int i = bnapi->index, j;
13965 
13966 	bnxt_for_each_napi_tx(j, bnapi, txr)
13967 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13968 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13969 			    txr->tx_cons);
13970 }
13971 
13972 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13973 {
13974 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13975 	int i = bnapi->index;
13976 
13977 	if (!rxr)
13978 		return;
13979 
13980 	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",
13981 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13982 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13983 		    rxr->rx_sw_agg_prod);
13984 }
13985 
13986 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13987 {
13988 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13989 	int i = bnapi->index;
13990 
13991 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13992 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13993 }
13994 
13995 static void bnxt_dbg_dump_states(struct bnxt *bp)
13996 {
13997 	int i;
13998 	struct bnxt_napi *bnapi;
13999 
14000 	for (i = 0; i < bp->cp_nr_rings; i++) {
14001 		bnapi = bp->bnapi[i];
14002 		if (netif_msg_drv(bp)) {
14003 			bnxt_dump_tx_sw_state(bnapi);
14004 			bnxt_dump_rx_sw_state(bnapi);
14005 			bnxt_dump_cp_sw_state(bnapi);
14006 		}
14007 	}
14008 }
14009 
14010 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
14011 {
14012 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
14013 	struct hwrm_ring_reset_input *req;
14014 	struct bnxt_napi *bnapi = rxr->bnapi;
14015 	struct bnxt_cp_ring_info *cpr;
14016 	u16 cp_ring_id;
14017 	int rc;
14018 
14019 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14020 	if (rc)
14021 		return rc;
14022 
14023 	cpr = &bnapi->cp_ring;
14024 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14025 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14026 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14027 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14028 	return hwrm_req_send_silent(bp, req);
14029 }
14030 
14031 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14032 {
14033 	if (!silent)
14034 		bnxt_dbg_dump_states(bp);
14035 	if (netif_running(bp->dev)) {
14036 		bnxt_close_nic(bp, !silent, false);
14037 		bnxt_open_nic(bp, !silent, false);
14038 	}
14039 }
14040 
14041 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14042 {
14043 	struct bnxt *bp = netdev_priv(dev);
14044 
14045 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14046 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14047 }
14048 
14049 static void bnxt_fw_health_check(struct bnxt *bp)
14050 {
14051 	struct bnxt_fw_health *fw_health = bp->fw_health;
14052 	struct pci_dev *pdev = bp->pdev;
14053 	u32 val;
14054 
14055 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14056 		return;
14057 
14058 	/* Make sure it is enabled before checking the tmr_counter. */
14059 	smp_rmb();
14060 	if (fw_health->tmr_counter) {
14061 		fw_health->tmr_counter--;
14062 		return;
14063 	}
14064 
14065 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14066 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14067 		fw_health->arrests++;
14068 		goto fw_reset;
14069 	}
14070 
14071 	fw_health->last_fw_heartbeat = val;
14072 
14073 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14074 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14075 		fw_health->discoveries++;
14076 		goto fw_reset;
14077 	}
14078 
14079 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14080 	return;
14081 
14082 fw_reset:
14083 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14084 }
14085 
14086 static void bnxt_timer(struct timer_list *t)
14087 {
14088 	struct bnxt *bp = timer_container_of(bp, t, timer);
14089 	struct net_device *dev = bp->dev;
14090 
14091 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14092 		return;
14093 
14094 	if (atomic_read(&bp->intr_sem) != 0)
14095 		goto bnxt_restart_timer;
14096 
14097 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14098 		bnxt_fw_health_check(bp);
14099 
14100 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14101 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14102 
14103 	if (bnxt_tc_flower_enabled(bp))
14104 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14105 
14106 #ifdef CONFIG_RFS_ACCEL
14107 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14108 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14109 #endif /*CONFIG_RFS_ACCEL*/
14110 
14111 	if (bp->link_info.phy_retry) {
14112 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14113 			bp->link_info.phy_retry = false;
14114 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14115 		} else {
14116 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14117 		}
14118 	}
14119 
14120 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14121 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14122 
14123 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14124 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14125 
14126 bnxt_restart_timer:
14127 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14128 }
14129 
14130 static void bnxt_lock_sp(struct bnxt *bp)
14131 {
14132 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14133 	 * set.  If the device is being closed, bnxt_close() may be holding
14134 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14135 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14136 	 * instance lock.
14137 	 */
14138 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14139 	netdev_lock(bp->dev);
14140 }
14141 
14142 static void bnxt_unlock_sp(struct bnxt *bp)
14143 {
14144 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14145 	netdev_unlock(bp->dev);
14146 }
14147 
14148 /* Only called from bnxt_sp_task() */
14149 static void bnxt_reset(struct bnxt *bp, bool silent)
14150 {
14151 	bnxt_lock_sp(bp);
14152 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14153 		bnxt_reset_task(bp, silent);
14154 	bnxt_unlock_sp(bp);
14155 }
14156 
14157 /* Only called from bnxt_sp_task() */
14158 static void bnxt_rx_ring_reset(struct bnxt *bp)
14159 {
14160 	int i;
14161 
14162 	bnxt_lock_sp(bp);
14163 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14164 		bnxt_unlock_sp(bp);
14165 		return;
14166 	}
14167 	/* Disable and flush TPA before resetting the RX ring */
14168 	if (bp->flags & BNXT_FLAG_TPA)
14169 		bnxt_set_tpa(bp, false);
14170 	for (i = 0; i < bp->rx_nr_rings; i++) {
14171 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14172 		struct bnxt_cp_ring_info *cpr;
14173 		int rc;
14174 
14175 		if (!rxr->bnapi->in_reset)
14176 			continue;
14177 
14178 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14179 		if (rc) {
14180 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14181 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14182 			else
14183 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14184 					    rc);
14185 			bnxt_reset_task(bp, true);
14186 			break;
14187 		}
14188 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14189 		rxr->rx_prod = 0;
14190 		rxr->rx_agg_prod = 0;
14191 		rxr->rx_sw_agg_prod = 0;
14192 		rxr->rx_next_cons = 0;
14193 		rxr->bnapi->in_reset = false;
14194 		bnxt_alloc_one_rx_ring(bp, i);
14195 		cpr = &rxr->bnapi->cp_ring;
14196 		cpr->sw_stats->rx.rx_resets++;
14197 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14198 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14199 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14200 	}
14201 	if (bp->flags & BNXT_FLAG_TPA)
14202 		bnxt_set_tpa(bp, true);
14203 	bnxt_unlock_sp(bp);
14204 }
14205 
14206 static void bnxt_fw_fatal_close(struct bnxt *bp)
14207 {
14208 	bnxt_tx_disable(bp);
14209 	bnxt_disable_napi(bp);
14210 	bnxt_disable_int_sync(bp);
14211 	bnxt_free_irq(bp);
14212 	bnxt_clear_int_mode(bp);
14213 	pci_disable_device(bp->pdev);
14214 }
14215 
14216 static void bnxt_fw_reset_close(struct bnxt *bp)
14217 {
14218 	/* When firmware is in fatal state, quiesce device and disable
14219 	 * bus master to prevent any potential bad DMAs before freeing
14220 	 * kernel memory.
14221 	 */
14222 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14223 		u16 val = 0;
14224 
14225 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14226 		if (val == 0xffff)
14227 			bp->fw_reset_min_dsecs = 0;
14228 		bnxt_fw_fatal_close(bp);
14229 	}
14230 	__bnxt_close_nic(bp, true, false);
14231 	bnxt_vf_reps_free(bp);
14232 	bnxt_clear_int_mode(bp);
14233 	bnxt_hwrm_func_drv_unrgtr(bp);
14234 	if (pci_is_enabled(bp->pdev))
14235 		pci_disable_device(bp->pdev);
14236 	bnxt_free_ctx_mem(bp, false);
14237 }
14238 
14239 static bool is_bnxt_fw_ok(struct bnxt *bp)
14240 {
14241 	struct bnxt_fw_health *fw_health = bp->fw_health;
14242 	bool no_heartbeat = false, has_reset = false;
14243 	u32 val;
14244 
14245 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14246 	if (val == fw_health->last_fw_heartbeat)
14247 		no_heartbeat = true;
14248 
14249 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14250 	if (val != fw_health->last_fw_reset_cnt)
14251 		has_reset = true;
14252 
14253 	if (!no_heartbeat && has_reset)
14254 		return true;
14255 
14256 	return false;
14257 }
14258 
14259 /* netdev instance lock is acquired before calling this function */
14260 static void bnxt_force_fw_reset(struct bnxt *bp)
14261 {
14262 	struct bnxt_fw_health *fw_health = bp->fw_health;
14263 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14264 	u32 wait_dsecs;
14265 
14266 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14267 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14268 		return;
14269 
14270 	/* we have to serialize with bnxt_refclk_read()*/
14271 	if (ptp) {
14272 		unsigned long flags;
14273 
14274 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14275 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14276 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14277 	} else {
14278 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14279 	}
14280 	bnxt_fw_reset_close(bp);
14281 	wait_dsecs = fw_health->master_func_wait_dsecs;
14282 	if (fw_health->primary) {
14283 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14284 			wait_dsecs = 0;
14285 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14286 	} else {
14287 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14288 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14289 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14290 	}
14291 
14292 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14293 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14294 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14295 }
14296 
14297 void bnxt_fw_exception(struct bnxt *bp)
14298 {
14299 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14300 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14301 	bnxt_ulp_stop(bp);
14302 	bnxt_lock_sp(bp);
14303 	bnxt_force_fw_reset(bp);
14304 	bnxt_unlock_sp(bp);
14305 }
14306 
14307 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14308  * < 0 on error.
14309  */
14310 static int bnxt_get_registered_vfs(struct bnxt *bp)
14311 {
14312 #ifdef CONFIG_BNXT_SRIOV
14313 	int rc;
14314 
14315 	if (!BNXT_PF(bp))
14316 		return 0;
14317 
14318 	rc = bnxt_hwrm_func_qcfg(bp);
14319 	if (rc) {
14320 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14321 		return rc;
14322 	}
14323 	if (bp->pf.registered_vfs)
14324 		return bp->pf.registered_vfs;
14325 	if (bp->sriov_cfg)
14326 		return 1;
14327 #endif
14328 	return 0;
14329 }
14330 
14331 void bnxt_fw_reset(struct bnxt *bp)
14332 {
14333 	bnxt_ulp_stop(bp);
14334 	bnxt_lock_sp(bp);
14335 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14336 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14337 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14338 		int n = 0, tmo;
14339 
14340 		/* we have to serialize with bnxt_refclk_read()*/
14341 		if (ptp) {
14342 			unsigned long flags;
14343 
14344 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14345 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14346 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14347 		} else {
14348 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14349 		}
14350 		if (bp->pf.active_vfs &&
14351 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14352 			n = bnxt_get_registered_vfs(bp);
14353 		if (n < 0) {
14354 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14355 				   n);
14356 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14357 			netif_close(bp->dev);
14358 			goto fw_reset_exit;
14359 		} else if (n > 0) {
14360 			u16 vf_tmo_dsecs = n * 10;
14361 
14362 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14363 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14364 			bp->fw_reset_state =
14365 				BNXT_FW_RESET_STATE_POLL_VF;
14366 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14367 			goto fw_reset_exit;
14368 		}
14369 		bnxt_fw_reset_close(bp);
14370 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14371 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14372 			tmo = HZ / 10;
14373 		} else {
14374 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14375 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14376 		}
14377 		bnxt_queue_fw_reset_work(bp, tmo);
14378 	}
14379 fw_reset_exit:
14380 	bnxt_unlock_sp(bp);
14381 }
14382 
14383 static void bnxt_chk_missed_irq(struct bnxt *bp)
14384 {
14385 	int i;
14386 
14387 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14388 		return;
14389 
14390 	for (i = 0; i < bp->cp_nr_rings; i++) {
14391 		struct bnxt_napi *bnapi = bp->bnapi[i];
14392 		struct bnxt_cp_ring_info *cpr;
14393 		u32 fw_ring_id;
14394 		int j;
14395 
14396 		if (!bnapi)
14397 			continue;
14398 
14399 		cpr = &bnapi->cp_ring;
14400 		for (j = 0; j < cpr->cp_ring_count; j++) {
14401 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14402 			u32 val[2];
14403 
14404 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14405 				continue;
14406 
14407 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14408 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14409 				continue;
14410 			}
14411 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14412 			bnxt_dbg_hwrm_ring_info_get(bp,
14413 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14414 				fw_ring_id, &val[0], &val[1]);
14415 			cpr->sw_stats->cmn.missed_irqs++;
14416 		}
14417 	}
14418 }
14419 
14420 static void bnxt_cfg_ntp_filters(struct bnxt *);
14421 
14422 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14423 {
14424 	struct bnxt_link_info *link_info = &bp->link_info;
14425 
14426 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14427 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14428 		if (bp->hwrm_spec_code >= 0x10201) {
14429 			if (link_info->auto_pause_setting &
14430 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14431 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14432 		} else {
14433 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14434 		}
14435 		bnxt_set_auto_speed(link_info);
14436 	} else {
14437 		bnxt_set_force_speed(link_info);
14438 		link_info->req_duplex = link_info->duplex_setting;
14439 	}
14440 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14441 		link_info->req_flow_ctrl =
14442 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14443 	else
14444 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14445 }
14446 
14447 static void bnxt_fw_echo_reply(struct bnxt *bp)
14448 {
14449 	struct bnxt_fw_health *fw_health = bp->fw_health;
14450 	struct hwrm_func_echo_response_input *req;
14451 	int rc;
14452 
14453 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14454 	if (rc)
14455 		return;
14456 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14457 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14458 	hwrm_req_send(bp, req);
14459 }
14460 
14461 static void bnxt_ulp_restart(struct bnxt *bp)
14462 {
14463 	bnxt_ulp_stop(bp);
14464 	bnxt_ulp_start(bp, 0);
14465 }
14466 
14467 static void bnxt_sp_task(struct work_struct *work)
14468 {
14469 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14470 
14471 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14472 	smp_mb__after_atomic();
14473 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14474 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14475 		return;
14476 	}
14477 
14478 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14479 		bnxt_ulp_restart(bp);
14480 		bnxt_reenable_sriov(bp);
14481 	}
14482 
14483 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14484 		bnxt_cfg_rx_mode(bp);
14485 
14486 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14487 		bnxt_cfg_ntp_filters(bp);
14488 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14489 		bnxt_hwrm_exec_fwd_req(bp);
14490 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14491 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14492 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14493 		bnxt_hwrm_port_qstats(bp, 0);
14494 		bnxt_hwrm_port_qstats_ext(bp, 0);
14495 		bnxt_accumulate_all_stats(bp);
14496 	}
14497 
14498 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14499 		int rc;
14500 
14501 		mutex_lock(&bp->link_lock);
14502 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14503 				       &bp->sp_event))
14504 			bnxt_hwrm_phy_qcaps(bp);
14505 
14506 		rc = bnxt_update_link(bp, true);
14507 		if (rc)
14508 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14509 				   rc);
14510 
14511 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14512 				       &bp->sp_event))
14513 			bnxt_init_ethtool_link_settings(bp);
14514 		mutex_unlock(&bp->link_lock);
14515 	}
14516 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14517 		int rc;
14518 
14519 		mutex_lock(&bp->link_lock);
14520 		rc = bnxt_update_phy_setting(bp);
14521 		mutex_unlock(&bp->link_lock);
14522 		if (rc) {
14523 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14524 		} else {
14525 			bp->link_info.phy_retry = false;
14526 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14527 		}
14528 	}
14529 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14530 		mutex_lock(&bp->link_lock);
14531 		bnxt_get_port_module_status(bp);
14532 		mutex_unlock(&bp->link_lock);
14533 	}
14534 
14535 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14536 		bnxt_tc_flow_stats_work(bp);
14537 
14538 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14539 		bnxt_chk_missed_irq(bp);
14540 
14541 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14542 		bnxt_fw_echo_reply(bp);
14543 
14544 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14545 		bnxt_hwmon_notify_event(bp);
14546 
14547 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14548 	 * must be the last functions to be called before exiting.
14549 	 */
14550 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14551 		bnxt_reset(bp, false);
14552 
14553 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14554 		bnxt_reset(bp, true);
14555 
14556 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14557 		bnxt_rx_ring_reset(bp);
14558 
14559 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14560 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14561 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14562 			bnxt_devlink_health_fw_report(bp);
14563 		else
14564 			bnxt_fw_reset(bp);
14565 	}
14566 
14567 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14568 		if (!is_bnxt_fw_ok(bp))
14569 			bnxt_devlink_health_fw_report(bp);
14570 	}
14571 
14572 	smp_mb__before_atomic();
14573 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14574 }
14575 
14576 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14577 				int *max_cp);
14578 
14579 /* Under netdev instance lock */
14580 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14581 		     int tx_xdp)
14582 {
14583 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14584 	struct bnxt_hw_rings hwr = {0};
14585 	int rx_rings = rx;
14586 	int rc;
14587 
14588 	if (tcs)
14589 		tx_sets = tcs;
14590 
14591 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14592 
14593 	if (max_rx < rx_rings)
14594 		return -ENOMEM;
14595 
14596 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14597 		rx_rings <<= 1;
14598 
14599 	hwr.rx = rx_rings;
14600 	hwr.tx = tx * tx_sets + tx_xdp;
14601 	if (max_tx < hwr.tx)
14602 		return -ENOMEM;
14603 
14604 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14605 
14606 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14607 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14608 	if (max_cp < hwr.cp)
14609 		return -ENOMEM;
14610 	hwr.stat = hwr.cp;
14611 	if (BNXT_NEW_RM(bp)) {
14612 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14613 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14614 		hwr.grp = rx;
14615 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14616 	}
14617 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14618 		hwr.cp_p5 = hwr.tx + rx;
14619 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14620 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14621 		if (!bnxt_ulp_registered(bp->edev)) {
14622 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14623 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14624 		}
14625 		if (hwr.cp > bp->total_irqs) {
14626 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14627 
14628 			if (total_msix < hwr.cp) {
14629 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14630 					    hwr.cp, total_msix);
14631 				rc = -ENOSPC;
14632 			}
14633 		}
14634 	}
14635 	return rc;
14636 }
14637 
14638 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14639 {
14640 	if (bp->bar2) {
14641 		pci_iounmap(pdev, bp->bar2);
14642 		bp->bar2 = NULL;
14643 	}
14644 
14645 	if (bp->bar1) {
14646 		pci_iounmap(pdev, bp->bar1);
14647 		bp->bar1 = NULL;
14648 	}
14649 
14650 	if (bp->bar0) {
14651 		pci_iounmap(pdev, bp->bar0);
14652 		bp->bar0 = NULL;
14653 	}
14654 }
14655 
14656 static void bnxt_cleanup_pci(struct bnxt *bp)
14657 {
14658 	bnxt_unmap_bars(bp, bp->pdev);
14659 	pci_release_regions(bp->pdev);
14660 	if (pci_is_enabled(bp->pdev))
14661 		pci_disable_device(bp->pdev);
14662 }
14663 
14664 static void bnxt_init_dflt_coal(struct bnxt *bp)
14665 {
14666 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14667 	struct bnxt_coal *coal;
14668 	u16 flags = 0;
14669 
14670 	if (coal_cap->cmpl_params &
14671 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14672 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14673 
14674 	/* Tick values in micro seconds.
14675 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14676 	 */
14677 	coal = &bp->rx_coal;
14678 	coal->coal_ticks = 10;
14679 	coal->coal_bufs = 30;
14680 	coal->coal_ticks_irq = 1;
14681 	coal->coal_bufs_irq = 2;
14682 	coal->idle_thresh = 50;
14683 	coal->bufs_per_record = 2;
14684 	coal->budget = 64;		/* NAPI budget */
14685 	coal->flags = flags;
14686 
14687 	coal = &bp->tx_coal;
14688 	coal->coal_ticks = 28;
14689 	coal->coal_bufs = 30;
14690 	coal->coal_ticks_irq = 2;
14691 	coal->coal_bufs_irq = 2;
14692 	coal->bufs_per_record = 1;
14693 	coal->flags = flags;
14694 
14695 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14696 }
14697 
14698 /* FW that pre-reserves 1 VNIC per function */
14699 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14700 {
14701 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14702 
14703 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14704 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14705 		return true;
14706 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14707 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14708 		return true;
14709 	return false;
14710 }
14711 
14712 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14713 {
14714 	int rc;
14715 
14716 	bp->fw_cap = 0;
14717 	rc = bnxt_hwrm_ver_get(bp);
14718 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14719 	 * so wait before continuing with recovery.
14720 	 */
14721 	if (rc)
14722 		msleep(100);
14723 	bnxt_try_map_fw_health_reg(bp);
14724 	if (rc) {
14725 		rc = bnxt_try_recover_fw(bp);
14726 		if (rc)
14727 			return rc;
14728 		rc = bnxt_hwrm_ver_get(bp);
14729 		if (rc)
14730 			return rc;
14731 	}
14732 
14733 	bnxt_nvm_cfg_ver_get(bp);
14734 
14735 	rc = bnxt_hwrm_func_reset(bp);
14736 	if (rc)
14737 		return -ENODEV;
14738 
14739 	bnxt_hwrm_fw_set_time(bp);
14740 	return 0;
14741 }
14742 
14743 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14744 {
14745 	int rc;
14746 
14747 	/* Get the MAX capabilities for this function */
14748 	rc = bnxt_hwrm_func_qcaps(bp);
14749 	if (rc) {
14750 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14751 			   rc);
14752 		return -ENODEV;
14753 	}
14754 
14755 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14756 	if (rc)
14757 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14758 			    rc);
14759 
14760 	if (bnxt_alloc_fw_health(bp)) {
14761 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14762 	} else {
14763 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14764 		if (rc)
14765 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14766 				    rc);
14767 	}
14768 
14769 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14770 	if (rc)
14771 		return -ENODEV;
14772 
14773 	rc = bnxt_alloc_crash_dump_mem(bp);
14774 	if (rc)
14775 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14776 			    rc);
14777 	if (!rc) {
14778 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14779 		if (rc) {
14780 			bnxt_free_crash_dump_mem(bp);
14781 			netdev_warn(bp->dev,
14782 				    "hwrm crash dump mem failure rc: %d\n", rc);
14783 		}
14784 	}
14785 
14786 	if (bnxt_fw_pre_resv_vnics(bp))
14787 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14788 
14789 	bnxt_hwrm_func_qcfg(bp);
14790 	bnxt_hwrm_vnic_qcaps(bp);
14791 	bnxt_hwrm_port_led_qcaps(bp);
14792 	bnxt_ethtool_init(bp);
14793 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14794 		__bnxt_hwrm_ptp_qcfg(bp);
14795 	bnxt_dcb_init(bp);
14796 	bnxt_hwmon_init(bp);
14797 	return 0;
14798 }
14799 
14800 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14801 {
14802 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14803 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14804 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14805 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14806 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14807 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14808 		bp->rss_hash_delta = bp->rss_hash_cfg;
14809 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14810 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14811 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14812 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14813 	}
14814 }
14815 
14816 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14817 {
14818 	struct net_device *dev = bp->dev;
14819 
14820 	dev->hw_features &= ~NETIF_F_NTUPLE;
14821 	dev->features &= ~NETIF_F_NTUPLE;
14822 	bp->flags &= ~BNXT_FLAG_RFS;
14823 	if (bnxt_rfs_supported(bp)) {
14824 		dev->hw_features |= NETIF_F_NTUPLE;
14825 		if (bnxt_rfs_capable(bp, false)) {
14826 			bp->flags |= BNXT_FLAG_RFS;
14827 			dev->features |= NETIF_F_NTUPLE;
14828 		}
14829 	}
14830 }
14831 
14832 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14833 {
14834 	struct pci_dev *pdev = bp->pdev;
14835 
14836 	bnxt_set_dflt_rss_hash_type(bp);
14837 	bnxt_set_dflt_rfs(bp);
14838 
14839 	bnxt_get_wol_settings(bp);
14840 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14841 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14842 	else
14843 		device_set_wakeup_capable(&pdev->dev, false);
14844 
14845 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14846 	bnxt_hwrm_coal_params_qcaps(bp);
14847 }
14848 
14849 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14850 
14851 int bnxt_fw_init_one(struct bnxt *bp)
14852 {
14853 	int rc;
14854 
14855 	rc = bnxt_fw_init_one_p1(bp);
14856 	if (rc) {
14857 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14858 		return rc;
14859 	}
14860 	rc = bnxt_fw_init_one_p2(bp);
14861 	if (rc) {
14862 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14863 		return rc;
14864 	}
14865 	rc = bnxt_probe_phy(bp, false);
14866 	if (rc)
14867 		return rc;
14868 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14869 	if (rc)
14870 		return rc;
14871 
14872 	bnxt_fw_init_one_p3(bp);
14873 	return 0;
14874 }
14875 
14876 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14877 {
14878 	struct bnxt_fw_health *fw_health = bp->fw_health;
14879 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14880 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14881 	u32 reg_type, reg_off, delay_msecs;
14882 
14883 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14884 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14885 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14886 	switch (reg_type) {
14887 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14888 		pci_write_config_dword(bp->pdev, reg_off, val);
14889 		break;
14890 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14891 		writel(reg_off & BNXT_GRC_BASE_MASK,
14892 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14893 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14894 		fallthrough;
14895 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14896 		writel(val, bp->bar0 + reg_off);
14897 		break;
14898 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14899 		writel(val, bp->bar1 + reg_off);
14900 		break;
14901 	}
14902 	if (delay_msecs) {
14903 		pci_read_config_dword(bp->pdev, 0, &val);
14904 		msleep(delay_msecs);
14905 	}
14906 }
14907 
14908 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14909 {
14910 	struct hwrm_func_qcfg_output *resp;
14911 	struct hwrm_func_qcfg_input *req;
14912 	bool result = true; /* firmware will enforce if unknown */
14913 
14914 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14915 		return result;
14916 
14917 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14918 		return result;
14919 
14920 	req->fid = cpu_to_le16(0xffff);
14921 	resp = hwrm_req_hold(bp, req);
14922 	if (!hwrm_req_send(bp, req))
14923 		result = !!(le16_to_cpu(resp->flags) &
14924 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14925 	hwrm_req_drop(bp, req);
14926 	return result;
14927 }
14928 
14929 static void bnxt_reset_all(struct bnxt *bp)
14930 {
14931 	struct bnxt_fw_health *fw_health = bp->fw_health;
14932 	int i, rc;
14933 
14934 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14935 		bnxt_fw_reset_via_optee(bp);
14936 		bp->fw_reset_timestamp = jiffies;
14937 		return;
14938 	}
14939 
14940 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14941 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14942 			bnxt_fw_reset_writel(bp, i);
14943 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14944 		struct hwrm_fw_reset_input *req;
14945 
14946 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14947 		if (!rc) {
14948 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14949 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14950 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14951 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14952 			rc = hwrm_req_send(bp, req);
14953 		}
14954 		if (rc != -ENODEV)
14955 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14956 	}
14957 	bp->fw_reset_timestamp = jiffies;
14958 }
14959 
14960 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14961 {
14962 	return time_after(jiffies, bp->fw_reset_timestamp +
14963 			  (bp->fw_reset_max_dsecs * HZ / 10));
14964 }
14965 
14966 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14967 {
14968 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14969 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14970 		bnxt_dl_health_fw_status_update(bp, false);
14971 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
14972 	netif_close(bp->dev);
14973 }
14974 
14975 static void bnxt_fw_reset_task(struct work_struct *work)
14976 {
14977 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14978 	int rc = 0;
14979 
14980 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14981 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14982 		return;
14983 	}
14984 
14985 	switch (bp->fw_reset_state) {
14986 	case BNXT_FW_RESET_STATE_POLL_VF: {
14987 		int n = bnxt_get_registered_vfs(bp);
14988 		int tmo;
14989 
14990 		if (n < 0) {
14991 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14992 				   n, jiffies_to_msecs(jiffies -
14993 				   bp->fw_reset_timestamp));
14994 			goto fw_reset_abort;
14995 		} else if (n > 0) {
14996 			if (bnxt_fw_reset_timeout(bp)) {
14997 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14998 				bp->fw_reset_state = 0;
14999 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
15000 					   n);
15001 				goto ulp_start;
15002 			}
15003 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15004 			return;
15005 		}
15006 		bp->fw_reset_timestamp = jiffies;
15007 		netdev_lock(bp->dev);
15008 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15009 			bnxt_fw_reset_abort(bp, rc);
15010 			netdev_unlock(bp->dev);
15011 			goto ulp_start;
15012 		}
15013 		bnxt_fw_reset_close(bp);
15014 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15015 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15016 			tmo = HZ / 10;
15017 		} else {
15018 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15019 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15020 		}
15021 		netdev_unlock(bp->dev);
15022 		bnxt_queue_fw_reset_work(bp, tmo);
15023 		return;
15024 	}
15025 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15026 		u32 val;
15027 
15028 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15029 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15030 		    !bnxt_fw_reset_timeout(bp)) {
15031 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15032 			return;
15033 		}
15034 
15035 		if (!bp->fw_health->primary) {
15036 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15037 
15038 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15039 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15040 			return;
15041 		}
15042 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15043 	}
15044 		fallthrough;
15045 	case BNXT_FW_RESET_STATE_RESET_FW:
15046 		bnxt_reset_all(bp);
15047 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15048 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15049 		return;
15050 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15051 		bnxt_inv_fw_health_reg(bp);
15052 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15053 		    !bp->fw_reset_min_dsecs) {
15054 			u16 val;
15055 
15056 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15057 			if (val == 0xffff) {
15058 				if (bnxt_fw_reset_timeout(bp)) {
15059 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15060 					rc = -ETIMEDOUT;
15061 					goto fw_reset_abort;
15062 				}
15063 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15064 				return;
15065 			}
15066 		}
15067 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15068 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15069 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15070 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15071 			bnxt_dl_remote_reload(bp);
15072 		if (pci_enable_device(bp->pdev)) {
15073 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15074 			rc = -ENODEV;
15075 			goto fw_reset_abort;
15076 		}
15077 		pci_set_master(bp->pdev);
15078 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15079 		fallthrough;
15080 	case BNXT_FW_RESET_STATE_POLL_FW:
15081 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15082 		rc = bnxt_hwrm_poll(bp);
15083 		if (rc) {
15084 			if (bnxt_fw_reset_timeout(bp)) {
15085 				netdev_err(bp->dev, "Firmware reset aborted\n");
15086 				goto fw_reset_abort_status;
15087 			}
15088 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15089 			return;
15090 		}
15091 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15092 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15093 		fallthrough;
15094 	case BNXT_FW_RESET_STATE_OPENING:
15095 		while (!netdev_trylock(bp->dev)) {
15096 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15097 			return;
15098 		}
15099 		rc = bnxt_open(bp->dev);
15100 		if (rc) {
15101 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15102 			bnxt_fw_reset_abort(bp, rc);
15103 			netdev_unlock(bp->dev);
15104 			goto ulp_start;
15105 		}
15106 
15107 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15108 		    bp->fw_health->enabled) {
15109 			bp->fw_health->last_fw_reset_cnt =
15110 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15111 		}
15112 		bp->fw_reset_state = 0;
15113 		/* Make sure fw_reset_state is 0 before clearing the flag */
15114 		smp_mb__before_atomic();
15115 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15116 		bnxt_ptp_reapply_pps(bp);
15117 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15118 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15119 			bnxt_dl_health_fw_recovery_done(bp);
15120 			bnxt_dl_health_fw_status_update(bp, true);
15121 		}
15122 		netdev_unlock(bp->dev);
15123 		bnxt_ulp_start(bp, 0);
15124 		bnxt_reenable_sriov(bp);
15125 		netdev_lock(bp->dev);
15126 		bnxt_vf_reps_alloc(bp);
15127 		bnxt_vf_reps_open(bp);
15128 		netdev_unlock(bp->dev);
15129 		break;
15130 	}
15131 	return;
15132 
15133 fw_reset_abort_status:
15134 	if (bp->fw_health->status_reliable ||
15135 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15136 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15137 
15138 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15139 	}
15140 fw_reset_abort:
15141 	netdev_lock(bp->dev);
15142 	bnxt_fw_reset_abort(bp, rc);
15143 	netdev_unlock(bp->dev);
15144 ulp_start:
15145 	bnxt_ulp_start(bp, rc);
15146 }
15147 
15148 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15149 {
15150 	int rc;
15151 	struct bnxt *bp = netdev_priv(dev);
15152 
15153 	SET_NETDEV_DEV(dev, &pdev->dev);
15154 
15155 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15156 	rc = pci_enable_device(pdev);
15157 	if (rc) {
15158 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15159 		goto init_err;
15160 	}
15161 
15162 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15163 		dev_err(&pdev->dev,
15164 			"Cannot find PCI device base address, aborting\n");
15165 		rc = -ENODEV;
15166 		goto init_err_disable;
15167 	}
15168 
15169 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15170 	if (rc) {
15171 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15172 		goto init_err_disable;
15173 	}
15174 
15175 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15176 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15177 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15178 		rc = -EIO;
15179 		goto init_err_release;
15180 	}
15181 
15182 	pci_set_master(pdev);
15183 
15184 	bp->dev = dev;
15185 	bp->pdev = pdev;
15186 
15187 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15188 	 * determines the BAR size.
15189 	 */
15190 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15191 	if (!bp->bar0) {
15192 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15193 		rc = -ENOMEM;
15194 		goto init_err_release;
15195 	}
15196 
15197 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15198 	if (!bp->bar2) {
15199 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15200 		rc = -ENOMEM;
15201 		goto init_err_release;
15202 	}
15203 
15204 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15205 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15206 
15207 	spin_lock_init(&bp->ntp_fltr_lock);
15208 #if BITS_PER_LONG == 32
15209 	spin_lock_init(&bp->db_lock);
15210 #endif
15211 
15212 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15213 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15214 
15215 	timer_setup(&bp->timer, bnxt_timer, 0);
15216 	bp->current_interval = BNXT_TIMER_INTERVAL;
15217 
15218 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15219 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15220 
15221 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15222 	return 0;
15223 
15224 init_err_release:
15225 	bnxt_unmap_bars(bp, pdev);
15226 	pci_release_regions(pdev);
15227 
15228 init_err_disable:
15229 	pci_disable_device(pdev);
15230 
15231 init_err:
15232 	return rc;
15233 }
15234 
15235 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15236 {
15237 	struct sockaddr *addr = p;
15238 	struct bnxt *bp = netdev_priv(dev);
15239 	int rc = 0;
15240 
15241 	netdev_assert_locked(dev);
15242 
15243 	if (!is_valid_ether_addr(addr->sa_data))
15244 		return -EADDRNOTAVAIL;
15245 
15246 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15247 		return 0;
15248 
15249 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15250 	if (rc)
15251 		return rc;
15252 
15253 	eth_hw_addr_set(dev, addr->sa_data);
15254 	bnxt_clear_usr_fltrs(bp, true);
15255 	if (netif_running(dev)) {
15256 		bnxt_close_nic(bp, false, false);
15257 		rc = bnxt_open_nic(bp, false, false);
15258 	}
15259 
15260 	return rc;
15261 }
15262 
15263 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15264 {
15265 	struct bnxt *bp = netdev_priv(dev);
15266 
15267 	netdev_assert_locked(dev);
15268 
15269 	if (netif_running(dev))
15270 		bnxt_close_nic(bp, true, false);
15271 
15272 	WRITE_ONCE(dev->mtu, new_mtu);
15273 
15274 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15275 	 * program is attached.  We need to set the AGG rings settings and
15276 	 * rx_skb_func accordingly.
15277 	 */
15278 	if (READ_ONCE(bp->xdp_prog))
15279 		bnxt_set_rx_skb_mode(bp, true);
15280 
15281 	bnxt_set_ring_params(bp);
15282 
15283 	if (netif_running(dev))
15284 		return bnxt_open_nic(bp, true, false);
15285 
15286 	return 0;
15287 }
15288 
15289 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15290 {
15291 	struct bnxt *bp = netdev_priv(dev);
15292 	bool sh = false;
15293 	int rc, tx_cp;
15294 
15295 	if (tc > bp->max_tc) {
15296 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15297 			   tc, bp->max_tc);
15298 		return -EINVAL;
15299 	}
15300 
15301 	if (bp->num_tc == tc)
15302 		return 0;
15303 
15304 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15305 		sh = true;
15306 
15307 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15308 			      sh, tc, bp->tx_nr_rings_xdp);
15309 	if (rc)
15310 		return rc;
15311 
15312 	/* Needs to close the device and do hw resource re-allocations */
15313 	if (netif_running(bp->dev))
15314 		bnxt_close_nic(bp, true, false);
15315 
15316 	if (tc) {
15317 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15318 		netdev_set_num_tc(dev, tc);
15319 		bp->num_tc = tc;
15320 	} else {
15321 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15322 		netdev_reset_tc(dev);
15323 		bp->num_tc = 0;
15324 	}
15325 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15326 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15327 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15328 			       tx_cp + bp->rx_nr_rings;
15329 
15330 	if (netif_running(bp->dev))
15331 		return bnxt_open_nic(bp, true, false);
15332 
15333 	return 0;
15334 }
15335 
15336 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15337 				  void *cb_priv)
15338 {
15339 	struct bnxt *bp = cb_priv;
15340 
15341 	if (!bnxt_tc_flower_enabled(bp) ||
15342 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15343 		return -EOPNOTSUPP;
15344 
15345 	switch (type) {
15346 	case TC_SETUP_CLSFLOWER:
15347 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15348 	default:
15349 		return -EOPNOTSUPP;
15350 	}
15351 }
15352 
15353 LIST_HEAD(bnxt_block_cb_list);
15354 
15355 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15356 			 void *type_data)
15357 {
15358 	struct bnxt *bp = netdev_priv(dev);
15359 
15360 	switch (type) {
15361 	case TC_SETUP_BLOCK:
15362 		return flow_block_cb_setup_simple(type_data,
15363 						  &bnxt_block_cb_list,
15364 						  bnxt_setup_tc_block_cb,
15365 						  bp, bp, true);
15366 	case TC_SETUP_QDISC_MQPRIO: {
15367 		struct tc_mqprio_qopt *mqprio = type_data;
15368 
15369 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15370 
15371 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15372 	}
15373 	default:
15374 		return -EOPNOTSUPP;
15375 	}
15376 }
15377 
15378 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15379 			    const struct sk_buff *skb)
15380 {
15381 	struct bnxt_vnic_info *vnic;
15382 
15383 	if (skb)
15384 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15385 
15386 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15387 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15388 }
15389 
15390 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15391 			   u32 idx)
15392 {
15393 	struct hlist_head *head;
15394 	int bit_id;
15395 
15396 	spin_lock_bh(&bp->ntp_fltr_lock);
15397 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15398 	if (bit_id < 0) {
15399 		spin_unlock_bh(&bp->ntp_fltr_lock);
15400 		return -ENOMEM;
15401 	}
15402 
15403 	fltr->base.sw_id = (u16)bit_id;
15404 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15405 	fltr->base.flags |= BNXT_ACT_RING_DST;
15406 	head = &bp->ntp_fltr_hash_tbl[idx];
15407 	hlist_add_head_rcu(&fltr->base.hash, head);
15408 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15409 	bnxt_insert_usr_fltr(bp, &fltr->base);
15410 	bp->ntp_fltr_count++;
15411 	spin_unlock_bh(&bp->ntp_fltr_lock);
15412 	return 0;
15413 }
15414 
15415 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15416 			    struct bnxt_ntuple_filter *f2)
15417 {
15418 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15419 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15420 	struct flow_keys *keys1 = &f1->fkeys;
15421 	struct flow_keys *keys2 = &f2->fkeys;
15422 
15423 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15424 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15425 		return false;
15426 
15427 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15428 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15429 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15430 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15431 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15432 			return false;
15433 	} else {
15434 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15435 				     &keys2->addrs.v6addrs.src) ||
15436 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15437 				     &masks2->addrs.v6addrs.src) ||
15438 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15439 				     &keys2->addrs.v6addrs.dst) ||
15440 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15441 				     &masks2->addrs.v6addrs.dst))
15442 			return false;
15443 	}
15444 
15445 	return keys1->ports.src == keys2->ports.src &&
15446 	       masks1->ports.src == masks2->ports.src &&
15447 	       keys1->ports.dst == keys2->ports.dst &&
15448 	       masks1->ports.dst == masks2->ports.dst &&
15449 	       keys1->control.flags == keys2->control.flags &&
15450 	       f1->l2_fltr == f2->l2_fltr;
15451 }
15452 
15453 struct bnxt_ntuple_filter *
15454 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15455 				struct bnxt_ntuple_filter *fltr, u32 idx)
15456 {
15457 	struct bnxt_ntuple_filter *f;
15458 	struct hlist_head *head;
15459 
15460 	head = &bp->ntp_fltr_hash_tbl[idx];
15461 	hlist_for_each_entry_rcu(f, head, base.hash) {
15462 		if (bnxt_fltr_match(f, fltr))
15463 			return f;
15464 	}
15465 	return NULL;
15466 }
15467 
15468 #ifdef CONFIG_RFS_ACCEL
15469 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15470 			      u16 rxq_index, u32 flow_id)
15471 {
15472 	struct bnxt *bp = netdev_priv(dev);
15473 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15474 	struct flow_keys *fkeys;
15475 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15476 	struct bnxt_l2_filter *l2_fltr;
15477 	int rc = 0, idx;
15478 	u32 flags;
15479 
15480 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15481 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15482 		atomic_inc(&l2_fltr->refcnt);
15483 	} else {
15484 		struct bnxt_l2_key key;
15485 
15486 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15487 		key.vlan = 0;
15488 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15489 		if (!l2_fltr)
15490 			return -EINVAL;
15491 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15492 			bnxt_del_l2_filter(bp, l2_fltr);
15493 			return -EINVAL;
15494 		}
15495 	}
15496 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15497 	if (!new_fltr) {
15498 		bnxt_del_l2_filter(bp, l2_fltr);
15499 		return -ENOMEM;
15500 	}
15501 
15502 	fkeys = &new_fltr->fkeys;
15503 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15504 		rc = -EPROTONOSUPPORT;
15505 		goto err_free;
15506 	}
15507 
15508 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15509 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15510 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15511 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15512 		rc = -EPROTONOSUPPORT;
15513 		goto err_free;
15514 	}
15515 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15516 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15517 		if (bp->hwrm_spec_code < 0x10601) {
15518 			rc = -EPROTONOSUPPORT;
15519 			goto err_free;
15520 		}
15521 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15522 	}
15523 	flags = fkeys->control.flags;
15524 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15525 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15526 		rc = -EPROTONOSUPPORT;
15527 		goto err_free;
15528 	}
15529 	new_fltr->l2_fltr = l2_fltr;
15530 
15531 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15532 	rcu_read_lock();
15533 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15534 	if (fltr) {
15535 		rc = fltr->base.sw_id;
15536 		rcu_read_unlock();
15537 		goto err_free;
15538 	}
15539 	rcu_read_unlock();
15540 
15541 	new_fltr->flow_id = flow_id;
15542 	new_fltr->base.rxq = rxq_index;
15543 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15544 	if (!rc) {
15545 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15546 		return new_fltr->base.sw_id;
15547 	}
15548 
15549 err_free:
15550 	bnxt_del_l2_filter(bp, l2_fltr);
15551 	kfree(new_fltr);
15552 	return rc;
15553 }
15554 #endif
15555 
15556 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15557 {
15558 	spin_lock_bh(&bp->ntp_fltr_lock);
15559 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15560 		spin_unlock_bh(&bp->ntp_fltr_lock);
15561 		return;
15562 	}
15563 	hlist_del_rcu(&fltr->base.hash);
15564 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15565 	bp->ntp_fltr_count--;
15566 	spin_unlock_bh(&bp->ntp_fltr_lock);
15567 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15568 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15569 	kfree_rcu(fltr, base.rcu);
15570 }
15571 
15572 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15573 {
15574 #ifdef CONFIG_RFS_ACCEL
15575 	int i;
15576 
15577 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15578 		struct hlist_head *head;
15579 		struct hlist_node *tmp;
15580 		struct bnxt_ntuple_filter *fltr;
15581 		int rc;
15582 
15583 		head = &bp->ntp_fltr_hash_tbl[i];
15584 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15585 			bool del = false;
15586 
15587 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15588 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15589 					continue;
15590 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15591 							fltr->flow_id,
15592 							fltr->base.sw_id)) {
15593 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15594 									 fltr);
15595 					del = true;
15596 				}
15597 			} else {
15598 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15599 								       fltr);
15600 				if (rc)
15601 					del = true;
15602 				else
15603 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15604 			}
15605 
15606 			if (del)
15607 				bnxt_del_ntp_filter(bp, fltr);
15608 		}
15609 	}
15610 #endif
15611 }
15612 
15613 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15614 				    unsigned int entry, struct udp_tunnel_info *ti)
15615 {
15616 	struct bnxt *bp = netdev_priv(netdev);
15617 	unsigned int cmd;
15618 
15619 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15620 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15621 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15622 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15623 	else
15624 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15625 
15626 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15627 }
15628 
15629 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15630 				      unsigned int entry, struct udp_tunnel_info *ti)
15631 {
15632 	struct bnxt *bp = netdev_priv(netdev);
15633 	unsigned int cmd;
15634 
15635 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15636 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15637 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15638 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15639 	else
15640 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15641 
15642 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15643 }
15644 
15645 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15646 	.set_port	= bnxt_udp_tunnel_set_port,
15647 	.unset_port	= bnxt_udp_tunnel_unset_port,
15648 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15649 	.tables		= {
15650 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15651 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15652 	},
15653 }, bnxt_udp_tunnels_p7 = {
15654 	.set_port	= bnxt_udp_tunnel_set_port,
15655 	.unset_port	= bnxt_udp_tunnel_unset_port,
15656 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15657 	.tables		= {
15658 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15659 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15660 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15661 	},
15662 };
15663 
15664 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15665 			       struct net_device *dev, u32 filter_mask,
15666 			       int nlflags)
15667 {
15668 	struct bnxt *bp = netdev_priv(dev);
15669 
15670 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15671 				       nlflags, filter_mask, NULL);
15672 }
15673 
15674 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15675 			       u16 flags, struct netlink_ext_ack *extack)
15676 {
15677 	struct bnxt *bp = netdev_priv(dev);
15678 	struct nlattr *attr, *br_spec;
15679 	int rem, rc = 0;
15680 
15681 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15682 		return -EOPNOTSUPP;
15683 
15684 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15685 	if (!br_spec)
15686 		return -EINVAL;
15687 
15688 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15689 		u16 mode;
15690 
15691 		mode = nla_get_u16(attr);
15692 		if (mode == bp->br_mode)
15693 			break;
15694 
15695 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15696 		if (!rc)
15697 			bp->br_mode = mode;
15698 		break;
15699 	}
15700 	return rc;
15701 }
15702 
15703 int bnxt_get_port_parent_id(struct net_device *dev,
15704 			    struct netdev_phys_item_id *ppid)
15705 {
15706 	struct bnxt *bp = netdev_priv(dev);
15707 
15708 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15709 		return -EOPNOTSUPP;
15710 
15711 	/* The PF and it's VF-reps only support the switchdev framework */
15712 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15713 		return -EOPNOTSUPP;
15714 
15715 	ppid->id_len = sizeof(bp->dsn);
15716 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15717 
15718 	return 0;
15719 }
15720 
15721 static const struct net_device_ops bnxt_netdev_ops = {
15722 	.ndo_open		= bnxt_open,
15723 	.ndo_start_xmit		= bnxt_start_xmit,
15724 	.ndo_stop		= bnxt_close,
15725 	.ndo_get_stats64	= bnxt_get_stats64,
15726 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15727 	.ndo_eth_ioctl		= bnxt_ioctl,
15728 	.ndo_validate_addr	= eth_validate_addr,
15729 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15730 	.ndo_change_mtu		= bnxt_change_mtu,
15731 	.ndo_fix_features	= bnxt_fix_features,
15732 	.ndo_set_features	= bnxt_set_features,
15733 	.ndo_features_check	= bnxt_features_check,
15734 	.ndo_tx_timeout		= bnxt_tx_timeout,
15735 #ifdef CONFIG_BNXT_SRIOV
15736 	.ndo_get_vf_config	= bnxt_get_vf_config,
15737 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15738 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15739 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15740 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15741 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15742 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15743 #endif
15744 	.ndo_setup_tc           = bnxt_setup_tc,
15745 #ifdef CONFIG_RFS_ACCEL
15746 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15747 #endif
15748 	.ndo_bpf		= bnxt_xdp,
15749 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15750 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15751 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15752 };
15753 
15754 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15755 				    struct netdev_queue_stats_rx *stats)
15756 {
15757 	struct bnxt *bp = netdev_priv(dev);
15758 	struct bnxt_cp_ring_info *cpr;
15759 	u64 *sw;
15760 
15761 	if (!bp->bnapi)
15762 		return;
15763 
15764 	cpr = &bp->bnapi[i]->cp_ring;
15765 	sw = cpr->stats.sw_stats;
15766 
15767 	stats->packets = 0;
15768 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15769 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15770 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15771 
15772 	stats->bytes = 0;
15773 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15774 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15775 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15776 
15777 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15778 }
15779 
15780 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15781 				    struct netdev_queue_stats_tx *stats)
15782 {
15783 	struct bnxt *bp = netdev_priv(dev);
15784 	struct bnxt_napi *bnapi;
15785 	u64 *sw;
15786 
15787 	if (!bp->tx_ring)
15788 		return;
15789 
15790 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15791 	sw = bnapi->cp_ring.stats.sw_stats;
15792 
15793 	stats->packets = 0;
15794 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15795 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15796 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15797 
15798 	stats->bytes = 0;
15799 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15800 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15801 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15802 }
15803 
15804 static void bnxt_get_base_stats(struct net_device *dev,
15805 				struct netdev_queue_stats_rx *rx,
15806 				struct netdev_queue_stats_tx *tx)
15807 {
15808 	struct bnxt *bp = netdev_priv(dev);
15809 
15810 	rx->packets = bp->net_stats_prev.rx_packets;
15811 	rx->bytes = bp->net_stats_prev.rx_bytes;
15812 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15813 
15814 	tx->packets = bp->net_stats_prev.tx_packets;
15815 	tx->bytes = bp->net_stats_prev.tx_bytes;
15816 }
15817 
15818 static const struct netdev_stat_ops bnxt_stat_ops = {
15819 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15820 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15821 	.get_base_stats		= bnxt_get_base_stats,
15822 };
15823 
15824 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15825 {
15826 	struct bnxt_rx_ring_info *rxr, *clone;
15827 	struct bnxt *bp = netdev_priv(dev);
15828 	struct bnxt_ring_struct *ring;
15829 	int rc;
15830 
15831 	if (!bp->rx_ring)
15832 		return -ENETDOWN;
15833 
15834 	rxr = &bp->rx_ring[idx];
15835 	clone = qmem;
15836 	memcpy(clone, rxr, sizeof(*rxr));
15837 	bnxt_init_rx_ring_struct(bp, clone);
15838 	bnxt_reset_rx_ring_struct(bp, clone);
15839 
15840 	clone->rx_prod = 0;
15841 	clone->rx_agg_prod = 0;
15842 	clone->rx_sw_agg_prod = 0;
15843 	clone->rx_next_cons = 0;
15844 	clone->need_head_pool = false;
15845 
15846 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15847 	if (rc)
15848 		return rc;
15849 
15850 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15851 	if (rc < 0)
15852 		goto err_page_pool_destroy;
15853 
15854 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15855 					MEM_TYPE_PAGE_POOL,
15856 					clone->page_pool);
15857 	if (rc)
15858 		goto err_rxq_info_unreg;
15859 
15860 	ring = &clone->rx_ring_struct;
15861 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15862 	if (rc)
15863 		goto err_free_rx_ring;
15864 
15865 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15866 		ring = &clone->rx_agg_ring_struct;
15867 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15868 		if (rc)
15869 			goto err_free_rx_agg_ring;
15870 
15871 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15872 		if (rc)
15873 			goto err_free_rx_agg_ring;
15874 	}
15875 
15876 	if (bp->flags & BNXT_FLAG_TPA) {
15877 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15878 		if (rc)
15879 			goto err_free_tpa_info;
15880 	}
15881 
15882 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15883 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15884 
15885 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15886 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15887 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
15888 	if (bp->flags & BNXT_FLAG_TPA)
15889 		bnxt_alloc_one_tpa_info_data(bp, clone);
15890 
15891 	return 0;
15892 
15893 err_free_tpa_info:
15894 	bnxt_free_one_tpa_info(bp, clone);
15895 err_free_rx_agg_ring:
15896 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15897 err_free_rx_ring:
15898 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15899 err_rxq_info_unreg:
15900 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15901 err_page_pool_destroy:
15902 	page_pool_destroy(clone->page_pool);
15903 	if (bnxt_separate_head_pool(clone))
15904 		page_pool_destroy(clone->head_pool);
15905 	clone->page_pool = NULL;
15906 	clone->head_pool = NULL;
15907 	return rc;
15908 }
15909 
15910 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15911 {
15912 	struct bnxt_rx_ring_info *rxr = qmem;
15913 	struct bnxt *bp = netdev_priv(dev);
15914 	struct bnxt_ring_struct *ring;
15915 
15916 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15917 	bnxt_free_one_tpa_info(bp, rxr);
15918 
15919 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15920 
15921 	page_pool_destroy(rxr->page_pool);
15922 	if (bnxt_separate_head_pool(rxr))
15923 		page_pool_destroy(rxr->head_pool);
15924 	rxr->page_pool = NULL;
15925 	rxr->head_pool = NULL;
15926 
15927 	ring = &rxr->rx_ring_struct;
15928 	bnxt_free_ring(bp, &ring->ring_mem);
15929 
15930 	ring = &rxr->rx_agg_ring_struct;
15931 	bnxt_free_ring(bp, &ring->ring_mem);
15932 
15933 	kfree(rxr->rx_agg_bmap);
15934 	rxr->rx_agg_bmap = NULL;
15935 }
15936 
15937 static void bnxt_copy_rx_ring(struct bnxt *bp,
15938 			      struct bnxt_rx_ring_info *dst,
15939 			      struct bnxt_rx_ring_info *src)
15940 {
15941 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15942 	struct bnxt_ring_struct *dst_ring, *src_ring;
15943 	int i;
15944 
15945 	dst_ring = &dst->rx_ring_struct;
15946 	dst_rmem = &dst_ring->ring_mem;
15947 	src_ring = &src->rx_ring_struct;
15948 	src_rmem = &src_ring->ring_mem;
15949 
15950 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15951 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15952 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15953 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15954 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15955 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15956 
15957 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15958 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15959 	*dst_rmem->vmem = *src_rmem->vmem;
15960 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15961 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15962 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15963 	}
15964 
15965 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15966 		return;
15967 
15968 	dst_ring = &dst->rx_agg_ring_struct;
15969 	dst_rmem = &dst_ring->ring_mem;
15970 	src_ring = &src->rx_agg_ring_struct;
15971 	src_rmem = &src_ring->ring_mem;
15972 
15973 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15974 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15975 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15976 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15977 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15978 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15979 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15980 
15981 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15982 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15983 	*dst_rmem->vmem = *src_rmem->vmem;
15984 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15985 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15986 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15987 	}
15988 
15989 	dst->rx_agg_bmap = src->rx_agg_bmap;
15990 }
15991 
15992 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15993 {
15994 	struct bnxt *bp = netdev_priv(dev);
15995 	struct bnxt_rx_ring_info *rxr, *clone;
15996 	struct bnxt_cp_ring_info *cpr;
15997 	struct bnxt_vnic_info *vnic;
15998 	struct bnxt_napi *bnapi;
15999 	int i, rc;
16000 	u16 mru;
16001 
16002 	rxr = &bp->rx_ring[idx];
16003 	clone = qmem;
16004 
16005 	rxr->rx_prod = clone->rx_prod;
16006 	rxr->rx_agg_prod = clone->rx_agg_prod;
16007 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16008 	rxr->rx_next_cons = clone->rx_next_cons;
16009 	rxr->rx_tpa = clone->rx_tpa;
16010 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16011 	rxr->page_pool = clone->page_pool;
16012 	rxr->head_pool = clone->head_pool;
16013 	rxr->xdp_rxq = clone->xdp_rxq;
16014 	rxr->need_head_pool = clone->need_head_pool;
16015 
16016 	bnxt_copy_rx_ring(bp, rxr, clone);
16017 
16018 	bnapi = rxr->bnapi;
16019 	cpr = &bnapi->cp_ring;
16020 
16021 	/* All rings have been reserved and previously allocated.
16022 	 * Reallocating with the same parameters should never fail.
16023 	 */
16024 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16025 	if (rc)
16026 		goto err_reset;
16027 
16028 	if (bp->tph_mode) {
16029 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16030 		if (rc)
16031 			goto err_reset;
16032 	}
16033 
16034 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16035 	if (rc)
16036 		goto err_reset;
16037 
16038 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16039 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16040 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16041 
16042 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16043 		rc = bnxt_tx_queue_start(bp, idx);
16044 		if (rc)
16045 			goto err_reset;
16046 	}
16047 
16048 	bnxt_enable_rx_page_pool(rxr);
16049 	napi_enable_locked(&bnapi->napi);
16050 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16051 
16052 	mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
16053 	for (i = 0; i < bp->nr_vnics; i++) {
16054 		vnic = &bp->vnic_info[i];
16055 
16056 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16057 		if (rc)
16058 			return rc;
16059 	}
16060 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16061 
16062 err_reset:
16063 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16064 		   rc);
16065 	napi_enable_locked(&bnapi->napi);
16066 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16067 	bnxt_reset_task(bp, true);
16068 	return rc;
16069 }
16070 
16071 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16072 {
16073 	struct bnxt *bp = netdev_priv(dev);
16074 	struct bnxt_rx_ring_info *rxr;
16075 	struct bnxt_cp_ring_info *cpr;
16076 	struct bnxt_vnic_info *vnic;
16077 	struct bnxt_napi *bnapi;
16078 	int i;
16079 
16080 	for (i = 0; i < bp->nr_vnics; i++) {
16081 		vnic = &bp->vnic_info[i];
16082 
16083 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16084 	}
16085 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16086 	/* Make sure NAPI sees that the VNIC is disabled */
16087 	synchronize_net();
16088 	rxr = &bp->rx_ring[idx];
16089 	bnapi = rxr->bnapi;
16090 	cpr = &bnapi->cp_ring;
16091 	cancel_work_sync(&cpr->dim.work);
16092 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16093 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16094 	page_pool_disable_direct_recycling(rxr->page_pool);
16095 	if (bnxt_separate_head_pool(rxr))
16096 		page_pool_disable_direct_recycling(rxr->head_pool);
16097 
16098 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16099 		bnxt_tx_queue_stop(bp, idx);
16100 
16101 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16102 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16103 	 * after seeing the completion.
16104 	 */
16105 	napi_disable_locked(&bnapi->napi);
16106 
16107 	if (bp->tph_mode) {
16108 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16109 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16110 	}
16111 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16112 
16113 	memcpy(qmem, rxr, sizeof(*rxr));
16114 	bnxt_init_rx_ring_struct(bp, qmem);
16115 
16116 	return 0;
16117 }
16118 
16119 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16120 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16121 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16122 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16123 	.ndo_queue_start	= bnxt_queue_start,
16124 	.ndo_queue_stop		= bnxt_queue_stop,
16125 };
16126 
16127 static void bnxt_remove_one(struct pci_dev *pdev)
16128 {
16129 	struct net_device *dev = pci_get_drvdata(pdev);
16130 	struct bnxt *bp = netdev_priv(dev);
16131 
16132 	if (BNXT_PF(bp))
16133 		bnxt_sriov_disable(bp);
16134 
16135 	bnxt_rdma_aux_device_del(bp);
16136 
16137 	unregister_netdev(dev);
16138 	bnxt_ptp_clear(bp);
16139 
16140 	bnxt_rdma_aux_device_uninit(bp);
16141 
16142 	bnxt_free_l2_filters(bp, true);
16143 	bnxt_free_ntp_fltrs(bp, true);
16144 	WARN_ON(bp->num_rss_ctx);
16145 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16146 	/* Flush any pending tasks */
16147 	cancel_work_sync(&bp->sp_task);
16148 	cancel_delayed_work_sync(&bp->fw_reset_task);
16149 	bp->sp_event = 0;
16150 
16151 	bnxt_dl_fw_reporters_destroy(bp);
16152 	bnxt_dl_unregister(bp);
16153 	bnxt_shutdown_tc(bp);
16154 
16155 	bnxt_clear_int_mode(bp);
16156 	bnxt_hwrm_func_drv_unrgtr(bp);
16157 	bnxt_free_hwrm_resources(bp);
16158 	bnxt_hwmon_uninit(bp);
16159 	bnxt_ethtool_free(bp);
16160 	bnxt_dcb_free(bp);
16161 	kfree(bp->ptp_cfg);
16162 	bp->ptp_cfg = NULL;
16163 	kfree(bp->fw_health);
16164 	bp->fw_health = NULL;
16165 	bnxt_cleanup_pci(bp);
16166 	bnxt_free_ctx_mem(bp, true);
16167 	bnxt_free_crash_dump_mem(bp);
16168 	kfree(bp->rss_indir_tbl);
16169 	bp->rss_indir_tbl = NULL;
16170 	bnxt_free_port_stats(bp);
16171 	free_netdev(dev);
16172 }
16173 
16174 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16175 {
16176 	int rc = 0;
16177 	struct bnxt_link_info *link_info = &bp->link_info;
16178 
16179 	bp->phy_flags = 0;
16180 	rc = bnxt_hwrm_phy_qcaps(bp);
16181 	if (rc) {
16182 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16183 			   rc);
16184 		return rc;
16185 	}
16186 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16187 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16188 	else
16189 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16190 
16191 	bp->mac_flags = 0;
16192 	bnxt_hwrm_mac_qcaps(bp);
16193 
16194 	if (!fw_dflt)
16195 		return 0;
16196 
16197 	mutex_lock(&bp->link_lock);
16198 	rc = bnxt_update_link(bp, false);
16199 	if (rc) {
16200 		mutex_unlock(&bp->link_lock);
16201 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16202 			   rc);
16203 		return rc;
16204 	}
16205 
16206 	/* Older firmware does not have supported_auto_speeds, so assume
16207 	 * that all supported speeds can be autonegotiated.
16208 	 */
16209 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16210 		link_info->support_auto_speeds = link_info->support_speeds;
16211 
16212 	bnxt_init_ethtool_link_settings(bp);
16213 	mutex_unlock(&bp->link_lock);
16214 	return 0;
16215 }
16216 
16217 static int bnxt_get_max_irq(struct pci_dev *pdev)
16218 {
16219 	u16 ctrl;
16220 
16221 	if (!pdev->msix_cap)
16222 		return 1;
16223 
16224 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16225 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16226 }
16227 
16228 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16229 				int *max_cp)
16230 {
16231 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16232 	int max_ring_grps = 0, max_irq;
16233 
16234 	*max_tx = hw_resc->max_tx_rings;
16235 	*max_rx = hw_resc->max_rx_rings;
16236 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16237 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16238 			bnxt_get_ulp_msix_num_in_use(bp),
16239 			hw_resc->max_stat_ctxs -
16240 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16241 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16242 		*max_cp = min_t(int, *max_cp, max_irq);
16243 	max_ring_grps = hw_resc->max_hw_ring_grps;
16244 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16245 		*max_cp -= 1;
16246 		*max_rx -= 2;
16247 	}
16248 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16249 		*max_rx >>= 1;
16250 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16251 		int rc;
16252 
16253 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16254 		if (rc) {
16255 			*max_rx = 0;
16256 			*max_tx = 0;
16257 		}
16258 		/* On P5 chips, max_cp output param should be available NQs */
16259 		*max_cp = max_irq;
16260 	}
16261 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16262 }
16263 
16264 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16265 {
16266 	int rx, tx, cp;
16267 
16268 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16269 	*max_rx = rx;
16270 	*max_tx = tx;
16271 	if (!rx || !tx || !cp)
16272 		return -ENOMEM;
16273 
16274 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16275 }
16276 
16277 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16278 			       bool shared)
16279 {
16280 	int rc;
16281 
16282 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16283 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16284 		/* Not enough rings, try disabling agg rings. */
16285 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16286 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16287 		if (rc) {
16288 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16289 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16290 			return rc;
16291 		}
16292 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16293 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16294 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16295 		bnxt_set_ring_params(bp);
16296 	}
16297 
16298 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16299 		int max_cp, max_stat, max_irq;
16300 
16301 		/* Reserve minimum resources for RoCE */
16302 		max_cp = bnxt_get_max_func_cp_rings(bp);
16303 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16304 		max_irq = bnxt_get_max_func_irqs(bp);
16305 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16306 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16307 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16308 			return 0;
16309 
16310 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16311 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16312 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16313 		max_cp = min_t(int, max_cp, max_irq);
16314 		max_cp = min_t(int, max_cp, max_stat);
16315 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16316 		if (rc)
16317 			rc = 0;
16318 	}
16319 	return rc;
16320 }
16321 
16322 /* In initial default shared ring setting, each shared ring must have a
16323  * RX/TX ring pair.
16324  */
16325 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16326 {
16327 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16328 	bp->rx_nr_rings = bp->cp_nr_rings;
16329 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16330 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16331 }
16332 
16333 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16334 {
16335 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16336 	int avail_msix;
16337 
16338 	if (!bnxt_can_reserve_rings(bp))
16339 		return 0;
16340 
16341 	if (sh)
16342 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16343 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16344 	/* Reduce default rings on multi-port cards so that total default
16345 	 * rings do not exceed CPU count.
16346 	 */
16347 	if (bp->port_count > 1) {
16348 		int max_rings =
16349 			max_t(int, num_online_cpus() / bp->port_count, 1);
16350 
16351 		dflt_rings = min_t(int, dflt_rings, max_rings);
16352 	}
16353 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16354 	if (rc)
16355 		return rc;
16356 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16357 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16358 	if (sh)
16359 		bnxt_trim_dflt_sh_rings(bp);
16360 	else
16361 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16362 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16363 
16364 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16365 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16366 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16367 
16368 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16369 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16370 	}
16371 
16372 	rc = __bnxt_reserve_rings(bp);
16373 	if (rc && rc != -ENODEV)
16374 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16375 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16376 	if (sh)
16377 		bnxt_trim_dflt_sh_rings(bp);
16378 
16379 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16380 	if (bnxt_need_reserve_rings(bp)) {
16381 		rc = __bnxt_reserve_rings(bp);
16382 		if (rc && rc != -ENODEV)
16383 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16384 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16385 	}
16386 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16387 		bp->rx_nr_rings++;
16388 		bp->cp_nr_rings++;
16389 	}
16390 	if (rc) {
16391 		bp->tx_nr_rings = 0;
16392 		bp->rx_nr_rings = 0;
16393 	}
16394 	return rc;
16395 }
16396 
16397 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16398 {
16399 	int rc;
16400 
16401 	if (bp->tx_nr_rings)
16402 		return 0;
16403 
16404 	bnxt_ulp_irq_stop(bp);
16405 	bnxt_clear_int_mode(bp);
16406 	rc = bnxt_set_dflt_rings(bp, true);
16407 	if (rc) {
16408 		if (BNXT_VF(bp) && rc == -ENODEV)
16409 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16410 		else
16411 			netdev_err(bp->dev, "Not enough rings available.\n");
16412 		goto init_dflt_ring_err;
16413 	}
16414 	rc = bnxt_init_int_mode(bp);
16415 	if (rc)
16416 		goto init_dflt_ring_err;
16417 
16418 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16419 
16420 	bnxt_set_dflt_rfs(bp);
16421 
16422 init_dflt_ring_err:
16423 	bnxt_ulp_irq_restart(bp, rc);
16424 	return rc;
16425 }
16426 
16427 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16428 {
16429 	int rc;
16430 
16431 	netdev_ops_assert_locked(bp->dev);
16432 	bnxt_hwrm_func_qcaps(bp);
16433 
16434 	if (netif_running(bp->dev))
16435 		__bnxt_close_nic(bp, true, false);
16436 
16437 	bnxt_ulp_irq_stop(bp);
16438 	bnxt_clear_int_mode(bp);
16439 	rc = bnxt_init_int_mode(bp);
16440 	bnxt_ulp_irq_restart(bp, rc);
16441 
16442 	if (netif_running(bp->dev)) {
16443 		if (rc)
16444 			netif_close(bp->dev);
16445 		else
16446 			rc = bnxt_open_nic(bp, true, false);
16447 	}
16448 
16449 	return rc;
16450 }
16451 
16452 static int bnxt_init_mac_addr(struct bnxt *bp)
16453 {
16454 	int rc = 0;
16455 
16456 	if (BNXT_PF(bp)) {
16457 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16458 	} else {
16459 #ifdef CONFIG_BNXT_SRIOV
16460 		struct bnxt_vf_info *vf = &bp->vf;
16461 		bool strict_approval = true;
16462 
16463 		if (is_valid_ether_addr(vf->mac_addr)) {
16464 			/* overwrite netdev dev_addr with admin VF MAC */
16465 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16466 			/* Older PF driver or firmware may not approve this
16467 			 * correctly.
16468 			 */
16469 			strict_approval = false;
16470 		} else {
16471 			eth_hw_addr_random(bp->dev);
16472 		}
16473 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16474 #endif
16475 	}
16476 	return rc;
16477 }
16478 
16479 static void bnxt_vpd_read_info(struct bnxt *bp)
16480 {
16481 	struct pci_dev *pdev = bp->pdev;
16482 	unsigned int vpd_size, kw_len;
16483 	int pos, size;
16484 	u8 *vpd_data;
16485 
16486 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16487 	if (IS_ERR(vpd_data)) {
16488 		pci_warn(pdev, "Unable to read VPD\n");
16489 		return;
16490 	}
16491 
16492 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16493 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16494 	if (pos < 0)
16495 		goto read_sn;
16496 
16497 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16498 	memcpy(bp->board_partno, &vpd_data[pos], size);
16499 
16500 read_sn:
16501 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16502 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16503 					   &kw_len);
16504 	if (pos < 0)
16505 		goto exit;
16506 
16507 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16508 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16509 exit:
16510 	kfree(vpd_data);
16511 }
16512 
16513 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16514 {
16515 	struct pci_dev *pdev = bp->pdev;
16516 	u64 qword;
16517 
16518 	qword = pci_get_dsn(pdev);
16519 	if (!qword) {
16520 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16521 		return -EOPNOTSUPP;
16522 	}
16523 
16524 	put_unaligned_le64(qword, dsn);
16525 
16526 	bp->flags |= BNXT_FLAG_DSN_VALID;
16527 	return 0;
16528 }
16529 
16530 static int bnxt_map_db_bar(struct bnxt *bp)
16531 {
16532 	if (!bp->db_size)
16533 		return -ENODEV;
16534 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16535 	if (!bp->bar1)
16536 		return -ENOMEM;
16537 	return 0;
16538 }
16539 
16540 void bnxt_print_device_info(struct bnxt *bp)
16541 {
16542 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16543 		    board_info[bp->board_idx].name,
16544 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16545 
16546 	pcie_print_link_status(bp->pdev);
16547 }
16548 
16549 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16550 {
16551 	struct bnxt_hw_resc *hw_resc;
16552 	struct net_device *dev;
16553 	struct bnxt *bp;
16554 	int rc, max_irqs;
16555 
16556 	if (pci_is_bridge(pdev))
16557 		return -ENODEV;
16558 
16559 	if (!pdev->msix_cap) {
16560 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16561 		return -ENODEV;
16562 	}
16563 
16564 	/* Clear any pending DMA transactions from crash kernel
16565 	 * while loading driver in capture kernel.
16566 	 */
16567 	if (is_kdump_kernel()) {
16568 		pci_clear_master(pdev);
16569 		pcie_flr(pdev);
16570 	}
16571 
16572 	max_irqs = bnxt_get_max_irq(pdev);
16573 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16574 				 max_irqs);
16575 	if (!dev)
16576 		return -ENOMEM;
16577 
16578 	bp = netdev_priv(dev);
16579 	bp->board_idx = ent->driver_data;
16580 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16581 	bnxt_set_max_func_irqs(bp, max_irqs);
16582 
16583 	if (bnxt_vf_pciid(bp->board_idx))
16584 		bp->flags |= BNXT_FLAG_VF;
16585 
16586 	/* No devlink port registration in case of a VF */
16587 	if (BNXT_PF(bp))
16588 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16589 
16590 	rc = bnxt_init_board(pdev, dev);
16591 	if (rc < 0)
16592 		goto init_err_free;
16593 
16594 	dev->netdev_ops = &bnxt_netdev_ops;
16595 	dev->stat_ops = &bnxt_stat_ops;
16596 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16597 	dev->ethtool_ops = &bnxt_ethtool_ops;
16598 	pci_set_drvdata(pdev, dev);
16599 
16600 	rc = bnxt_alloc_hwrm_resources(bp);
16601 	if (rc)
16602 		goto init_err_pci_clean;
16603 
16604 	mutex_init(&bp->hwrm_cmd_lock);
16605 	mutex_init(&bp->link_lock);
16606 
16607 	rc = bnxt_fw_init_one_p1(bp);
16608 	if (rc)
16609 		goto init_err_pci_clean;
16610 
16611 	if (BNXT_PF(bp))
16612 		bnxt_vpd_read_info(bp);
16613 
16614 	if (BNXT_CHIP_P5_PLUS(bp)) {
16615 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16616 		if (BNXT_CHIP_P7(bp))
16617 			bp->flags |= BNXT_FLAG_CHIP_P7;
16618 	}
16619 
16620 	rc = bnxt_alloc_rss_indir_tbl(bp);
16621 	if (rc)
16622 		goto init_err_pci_clean;
16623 
16624 	rc = bnxt_fw_init_one_p2(bp);
16625 	if (rc)
16626 		goto init_err_pci_clean;
16627 
16628 	rc = bnxt_map_db_bar(bp);
16629 	if (rc) {
16630 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16631 			rc);
16632 		goto init_err_pci_clean;
16633 	}
16634 
16635 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16636 			   NETIF_F_TSO | NETIF_F_TSO6 |
16637 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16638 			   NETIF_F_GSO_IPXIP4 |
16639 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16640 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16641 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16642 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16643 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16644 
16645 	if (BNXT_SUPPORTS_TPA(bp))
16646 		dev->hw_features |= NETIF_F_LRO;
16647 
16648 	dev->hw_enc_features =
16649 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16650 			NETIF_F_TSO | NETIF_F_TSO6 |
16651 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16652 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16653 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16654 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16655 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16656 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16657 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16658 	else
16659 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16660 
16661 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16662 				    NETIF_F_GSO_GRE_CSUM;
16663 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16664 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16665 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16666 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16667 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16668 	if (BNXT_SUPPORTS_TPA(bp))
16669 		dev->hw_features |= NETIF_F_GRO_HW;
16670 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16671 	if (dev->features & NETIF_F_GRO_HW)
16672 		dev->features &= ~NETIF_F_LRO;
16673 	dev->priv_flags |= IFF_UNICAST_FLT;
16674 
16675 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16676 	if (bp->tso_max_segs)
16677 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16678 
16679 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16680 			    NETDEV_XDP_ACT_RX_SG;
16681 
16682 #ifdef CONFIG_BNXT_SRIOV
16683 	init_waitqueue_head(&bp->sriov_cfg_wait);
16684 #endif
16685 	if (BNXT_SUPPORTS_TPA(bp)) {
16686 		bp->gro_func = bnxt_gro_func_5730x;
16687 		if (BNXT_CHIP_P4(bp))
16688 			bp->gro_func = bnxt_gro_func_5731x;
16689 		else if (BNXT_CHIP_P5_PLUS(bp))
16690 			bp->gro_func = bnxt_gro_func_5750x;
16691 	}
16692 	if (!BNXT_CHIP_P4_PLUS(bp))
16693 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16694 
16695 	rc = bnxt_init_mac_addr(bp);
16696 	if (rc) {
16697 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16698 		rc = -EADDRNOTAVAIL;
16699 		goto init_err_pci_clean;
16700 	}
16701 
16702 	if (BNXT_PF(bp)) {
16703 		/* Read the adapter's DSN to use as the eswitch switch_id */
16704 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16705 	}
16706 
16707 	/* MTU range: 60 - FW defined max */
16708 	dev->min_mtu = ETH_ZLEN;
16709 	dev->max_mtu = bp->max_mtu;
16710 
16711 	rc = bnxt_probe_phy(bp, true);
16712 	if (rc)
16713 		goto init_err_pci_clean;
16714 
16715 	hw_resc = &bp->hw_resc;
16716 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16717 		       BNXT_L2_FLTR_MAX_FLTR;
16718 	/* Older firmware may not report these filters properly */
16719 	if (bp->max_fltr < BNXT_MAX_FLTR)
16720 		bp->max_fltr = BNXT_MAX_FLTR;
16721 	bnxt_init_l2_fltr_tbl(bp);
16722 	__bnxt_set_rx_skb_mode(bp, false);
16723 	bnxt_set_tpa_flags(bp);
16724 	bnxt_init_ring_params(bp);
16725 	bnxt_set_ring_params(bp);
16726 	bnxt_rdma_aux_device_init(bp);
16727 	rc = bnxt_set_dflt_rings(bp, true);
16728 	if (rc) {
16729 		if (BNXT_VF(bp) && rc == -ENODEV) {
16730 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16731 		} else {
16732 			netdev_err(bp->dev, "Not enough rings available.\n");
16733 			rc = -ENOMEM;
16734 		}
16735 		goto init_err_pci_clean;
16736 	}
16737 
16738 	bnxt_fw_init_one_p3(bp);
16739 
16740 	bnxt_init_dflt_coal(bp);
16741 
16742 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16743 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16744 
16745 	rc = bnxt_init_int_mode(bp);
16746 	if (rc)
16747 		goto init_err_pci_clean;
16748 
16749 	/* No TC has been set yet and rings may have been trimmed due to
16750 	 * limited MSIX, so we re-initialize the TX rings per TC.
16751 	 */
16752 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16753 
16754 	if (BNXT_PF(bp)) {
16755 		if (!bnxt_pf_wq) {
16756 			bnxt_pf_wq =
16757 				create_singlethread_workqueue("bnxt_pf_wq");
16758 			if (!bnxt_pf_wq) {
16759 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16760 				rc = -ENOMEM;
16761 				goto init_err_pci_clean;
16762 			}
16763 		}
16764 		rc = bnxt_init_tc(bp);
16765 		if (rc)
16766 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16767 				   rc);
16768 	}
16769 
16770 	bnxt_inv_fw_health_reg(bp);
16771 	rc = bnxt_dl_register(bp);
16772 	if (rc)
16773 		goto init_err_dl;
16774 
16775 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16776 
16777 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16778 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16779 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16780 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16781 	dev->request_ops_lock = true;
16782 	dev->netmem_tx = true;
16783 
16784 	rc = register_netdev(dev);
16785 	if (rc)
16786 		goto init_err_cleanup;
16787 
16788 	bnxt_dl_fw_reporters_create(bp);
16789 
16790 	bnxt_rdma_aux_device_add(bp);
16791 
16792 	bnxt_print_device_info(bp);
16793 
16794 	pci_save_state(pdev);
16795 
16796 	return 0;
16797 init_err_cleanup:
16798 	bnxt_rdma_aux_device_uninit(bp);
16799 	bnxt_dl_unregister(bp);
16800 init_err_dl:
16801 	bnxt_shutdown_tc(bp);
16802 	bnxt_clear_int_mode(bp);
16803 
16804 init_err_pci_clean:
16805 	bnxt_hwrm_func_drv_unrgtr(bp);
16806 	bnxt_free_hwrm_resources(bp);
16807 	bnxt_hwmon_uninit(bp);
16808 	bnxt_ethtool_free(bp);
16809 	bnxt_ptp_clear(bp);
16810 	kfree(bp->ptp_cfg);
16811 	bp->ptp_cfg = NULL;
16812 	kfree(bp->fw_health);
16813 	bp->fw_health = NULL;
16814 	bnxt_cleanup_pci(bp);
16815 	bnxt_free_ctx_mem(bp, true);
16816 	bnxt_free_crash_dump_mem(bp);
16817 	kfree(bp->rss_indir_tbl);
16818 	bp->rss_indir_tbl = NULL;
16819 
16820 init_err_free:
16821 	free_netdev(dev);
16822 	return rc;
16823 }
16824 
16825 static void bnxt_shutdown(struct pci_dev *pdev)
16826 {
16827 	struct net_device *dev = pci_get_drvdata(pdev);
16828 	struct bnxt *bp;
16829 
16830 	if (!dev)
16831 		return;
16832 
16833 	rtnl_lock();
16834 	netdev_lock(dev);
16835 	bp = netdev_priv(dev);
16836 	if (!bp)
16837 		goto shutdown_exit;
16838 
16839 	if (netif_running(dev))
16840 		netif_close(dev);
16841 
16842 	bnxt_ptp_clear(bp);
16843 	bnxt_clear_int_mode(bp);
16844 	pci_disable_device(pdev);
16845 
16846 	if (system_state == SYSTEM_POWER_OFF) {
16847 		pci_wake_from_d3(pdev, bp->wol);
16848 		pci_set_power_state(pdev, PCI_D3hot);
16849 	}
16850 
16851 shutdown_exit:
16852 	netdev_unlock(dev);
16853 	rtnl_unlock();
16854 }
16855 
16856 #ifdef CONFIG_PM_SLEEP
16857 static int bnxt_suspend(struct device *device)
16858 {
16859 	struct net_device *dev = dev_get_drvdata(device);
16860 	struct bnxt *bp = netdev_priv(dev);
16861 	int rc = 0;
16862 
16863 	bnxt_ulp_stop(bp);
16864 
16865 	netdev_lock(dev);
16866 	if (netif_running(dev)) {
16867 		netif_device_detach(dev);
16868 		rc = bnxt_close(dev);
16869 	}
16870 	bnxt_hwrm_func_drv_unrgtr(bp);
16871 	bnxt_ptp_clear(bp);
16872 	pci_disable_device(bp->pdev);
16873 	bnxt_free_ctx_mem(bp, false);
16874 	netdev_unlock(dev);
16875 	return rc;
16876 }
16877 
16878 static int bnxt_resume(struct device *device)
16879 {
16880 	struct net_device *dev = dev_get_drvdata(device);
16881 	struct bnxt *bp = netdev_priv(dev);
16882 	int rc = 0;
16883 
16884 	netdev_lock(dev);
16885 	rc = pci_enable_device(bp->pdev);
16886 	if (rc) {
16887 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16888 			   rc);
16889 		goto resume_exit;
16890 	}
16891 	pci_set_master(bp->pdev);
16892 	if (bnxt_hwrm_ver_get(bp)) {
16893 		rc = -ENODEV;
16894 		goto resume_exit;
16895 	}
16896 	rc = bnxt_hwrm_func_reset(bp);
16897 	if (rc) {
16898 		rc = -EBUSY;
16899 		goto resume_exit;
16900 	}
16901 
16902 	rc = bnxt_hwrm_func_qcaps(bp);
16903 	if (rc)
16904 		goto resume_exit;
16905 
16906 	bnxt_clear_reservations(bp, true);
16907 
16908 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16909 		rc = -ENODEV;
16910 		goto resume_exit;
16911 	}
16912 	if (bp->fw_crash_mem)
16913 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16914 
16915 	if (bnxt_ptp_init(bp)) {
16916 		kfree(bp->ptp_cfg);
16917 		bp->ptp_cfg = NULL;
16918 	}
16919 	bnxt_get_wol_settings(bp);
16920 	if (netif_running(dev)) {
16921 		rc = bnxt_open(dev);
16922 		if (!rc)
16923 			netif_device_attach(dev);
16924 	}
16925 
16926 resume_exit:
16927 	netdev_unlock(bp->dev);
16928 	bnxt_ulp_start(bp, rc);
16929 	if (!rc)
16930 		bnxt_reenable_sriov(bp);
16931 	return rc;
16932 }
16933 
16934 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16935 #define BNXT_PM_OPS (&bnxt_pm_ops)
16936 
16937 #else
16938 
16939 #define BNXT_PM_OPS NULL
16940 
16941 #endif /* CONFIG_PM_SLEEP */
16942 
16943 /**
16944  * bnxt_io_error_detected - called when PCI error is detected
16945  * @pdev: Pointer to PCI device
16946  * @state: The current pci connection state
16947  *
16948  * This function is called after a PCI bus error affecting
16949  * this device has been detected.
16950  */
16951 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16952 					       pci_channel_state_t state)
16953 {
16954 	struct net_device *netdev = pci_get_drvdata(pdev);
16955 	struct bnxt *bp = netdev_priv(netdev);
16956 	bool abort = false;
16957 
16958 	netdev_info(netdev, "PCI I/O error detected\n");
16959 
16960 	bnxt_ulp_stop(bp);
16961 
16962 	netdev_lock(netdev);
16963 	netif_device_detach(netdev);
16964 
16965 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16966 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16967 		abort = true;
16968 	}
16969 
16970 	if (abort || state == pci_channel_io_perm_failure) {
16971 		netdev_unlock(netdev);
16972 		return PCI_ERS_RESULT_DISCONNECT;
16973 	}
16974 
16975 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16976 	 * so we disable bus master to prevent any potential bad DMAs before
16977 	 * freeing kernel memory.
16978 	 */
16979 	if (state == pci_channel_io_frozen) {
16980 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16981 		bnxt_fw_fatal_close(bp);
16982 	}
16983 
16984 	if (netif_running(netdev))
16985 		__bnxt_close_nic(bp, true, true);
16986 
16987 	if (pci_is_enabled(pdev))
16988 		pci_disable_device(pdev);
16989 	bnxt_free_ctx_mem(bp, false);
16990 	netdev_unlock(netdev);
16991 
16992 	/* Request a slot reset. */
16993 	return PCI_ERS_RESULT_NEED_RESET;
16994 }
16995 
16996 /**
16997  * bnxt_io_slot_reset - called after the pci bus has been reset.
16998  * @pdev: Pointer to PCI device
16999  *
17000  * Restart the card from scratch, as if from a cold-boot.
17001  * At this point, the card has experienced a hard reset,
17002  * followed by fixups by BIOS, and has its config space
17003  * set up identically to what it was at cold boot.
17004  */
17005 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17006 {
17007 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17008 	struct net_device *netdev = pci_get_drvdata(pdev);
17009 	struct bnxt *bp = netdev_priv(netdev);
17010 	int retry = 0;
17011 	int err = 0;
17012 	int off;
17013 
17014 	netdev_info(bp->dev, "PCI Slot Reset\n");
17015 
17016 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17017 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17018 		msleep(900);
17019 
17020 	netdev_lock(netdev);
17021 
17022 	if (pci_enable_device(pdev)) {
17023 		dev_err(&pdev->dev,
17024 			"Cannot re-enable PCI device after reset.\n");
17025 	} else {
17026 		pci_set_master(pdev);
17027 		/* Upon fatal error, our device internal logic that latches to
17028 		 * BAR value is getting reset and will restore only upon
17029 		 * rewriting the BARs.
17030 		 *
17031 		 * As pci_restore_state() does not re-write the BARs if the
17032 		 * value is same as saved value earlier, driver needs to
17033 		 * write the BARs to 0 to force restore, in case of fatal error.
17034 		 */
17035 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17036 				       &bp->state)) {
17037 			for (off = PCI_BASE_ADDRESS_0;
17038 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17039 				pci_write_config_dword(bp->pdev, off, 0);
17040 		}
17041 		pci_restore_state(pdev);
17042 		pci_save_state(pdev);
17043 
17044 		bnxt_inv_fw_health_reg(bp);
17045 		bnxt_try_map_fw_health_reg(bp);
17046 
17047 		/* In some PCIe AER scenarios, firmware may take up to
17048 		 * 10 seconds to become ready in the worst case.
17049 		 */
17050 		do {
17051 			err = bnxt_try_recover_fw(bp);
17052 			if (!err)
17053 				break;
17054 			retry++;
17055 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17056 
17057 		if (err) {
17058 			dev_err(&pdev->dev, "Firmware not ready\n");
17059 			goto reset_exit;
17060 		}
17061 
17062 		err = bnxt_hwrm_func_reset(bp);
17063 		if (!err)
17064 			result = PCI_ERS_RESULT_RECOVERED;
17065 
17066 		/* IRQ will be initialized later in bnxt_io_resume */
17067 		bnxt_ulp_irq_stop(bp);
17068 		bnxt_clear_int_mode(bp);
17069 	}
17070 
17071 reset_exit:
17072 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17073 	bnxt_clear_reservations(bp, true);
17074 	netdev_unlock(netdev);
17075 
17076 	return result;
17077 }
17078 
17079 /**
17080  * bnxt_io_resume - called when traffic can start flowing again.
17081  * @pdev: Pointer to PCI device
17082  *
17083  * This callback is called when the error recovery driver tells
17084  * us that its OK to resume normal operation.
17085  */
17086 static void bnxt_io_resume(struct pci_dev *pdev)
17087 {
17088 	struct net_device *netdev = pci_get_drvdata(pdev);
17089 	struct bnxt *bp = netdev_priv(netdev);
17090 	int err;
17091 
17092 	netdev_info(bp->dev, "PCI Slot Resume\n");
17093 	netdev_lock(netdev);
17094 
17095 	err = bnxt_hwrm_func_qcaps(bp);
17096 	if (!err) {
17097 		if (netif_running(netdev)) {
17098 			err = bnxt_open(netdev);
17099 		} else {
17100 			err = bnxt_reserve_rings(bp, true);
17101 			if (!err)
17102 				err = bnxt_init_int_mode(bp);
17103 		}
17104 	}
17105 
17106 	if (!err)
17107 		netif_device_attach(netdev);
17108 
17109 	netdev_unlock(netdev);
17110 	bnxt_ulp_start(bp, err);
17111 	if (!err)
17112 		bnxt_reenable_sriov(bp);
17113 }
17114 
17115 static const struct pci_error_handlers bnxt_err_handler = {
17116 	.error_detected	= bnxt_io_error_detected,
17117 	.slot_reset	= bnxt_io_slot_reset,
17118 	.resume		= bnxt_io_resume
17119 };
17120 
17121 static struct pci_driver bnxt_pci_driver = {
17122 	.name		= DRV_MODULE_NAME,
17123 	.id_table	= bnxt_pci_tbl,
17124 	.probe		= bnxt_init_one,
17125 	.remove		= bnxt_remove_one,
17126 	.shutdown	= bnxt_shutdown,
17127 	.driver.pm	= BNXT_PM_OPS,
17128 	.err_handler	= &bnxt_err_handler,
17129 #if defined(CONFIG_BNXT_SRIOV)
17130 	.sriov_configure = bnxt_sriov_configure,
17131 #endif
17132 };
17133 
17134 static int __init bnxt_init(void)
17135 {
17136 	int err;
17137 
17138 	bnxt_debug_init();
17139 	err = pci_register_driver(&bnxt_pci_driver);
17140 	if (err) {
17141 		bnxt_debug_exit();
17142 		return err;
17143 	}
17144 
17145 	return 0;
17146 }
17147 
17148 static void __exit bnxt_exit(void)
17149 {
17150 	pci_unregister_driver(&bnxt_pci_driver);
17151 	if (bnxt_pf_wq)
17152 		destroy_workqueue(bnxt_pf_wq);
17153 	bnxt_debug_exit();
17154 }
17155 
17156 module_init(bnxt_init);
17157 module_exit(bnxt_exit);
17158