xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision 34f2573661e3e644efaf383178af634a2fd67828)
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 
62 #include "bnxt_hsi.h"
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 					 gfp_t gfp)
930 {
931 	netmem_ref netmem;
932 
933 	netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
934 	if (!netmem)
935 		return 0;
936 
937 	*mapping = page_pool_get_dma_addr_netmem(netmem);
938 	return netmem;
939 }
940 
941 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
942 				       struct bnxt_rx_ring_info *rxr,
943 				       gfp_t gfp)
944 {
945 	unsigned int offset;
946 	struct page *page;
947 
948 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
949 				    bp->rx_buf_size, gfp);
950 	if (!page)
951 		return NULL;
952 
953 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
954 	return page_address(page) + offset;
955 }
956 
957 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
958 		       u16 prod, gfp_t gfp)
959 {
960 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
961 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
962 	dma_addr_t mapping;
963 
964 	if (BNXT_RX_PAGE_MODE(bp)) {
965 		unsigned int offset;
966 		struct page *page =
967 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
968 
969 		if (!page)
970 			return -ENOMEM;
971 
972 		mapping += bp->rx_dma_offset;
973 		rx_buf->data = page;
974 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
975 	} else {
976 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
977 
978 		if (!data)
979 			return -ENOMEM;
980 
981 		rx_buf->data = data;
982 		rx_buf->data_ptr = data + bp->rx_offset;
983 	}
984 	rx_buf->mapping = mapping;
985 
986 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
987 	return 0;
988 }
989 
990 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
991 {
992 	u16 prod = rxr->rx_prod;
993 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
994 	struct bnxt *bp = rxr->bnapi->bp;
995 	struct rx_bd *cons_bd, *prod_bd;
996 
997 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
998 	cons_rx_buf = &rxr->rx_buf_ring[cons];
999 
1000 	prod_rx_buf->data = data;
1001 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
1002 
1003 	prod_rx_buf->mapping = cons_rx_buf->mapping;
1004 
1005 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1006 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1007 
1008 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1009 }
1010 
1011 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1012 {
1013 	u16 next, max = rxr->rx_agg_bmap_size;
1014 
1015 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1016 	if (next >= max)
1017 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1018 	return next;
1019 }
1020 
1021 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1022 				u16 prod, gfp_t gfp)
1023 {
1024 	struct rx_bd *rxbd =
1025 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1026 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1027 	u16 sw_prod = rxr->rx_sw_agg_prod;
1028 	unsigned int offset = 0;
1029 	dma_addr_t mapping;
1030 	netmem_ref netmem;
1031 
1032 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, gfp);
1033 	if (!netmem)
1034 		return -ENOMEM;
1035 
1036 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1037 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1038 
1039 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1040 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1041 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1042 
1043 	rx_agg_buf->netmem = netmem;
1044 	rx_agg_buf->offset = offset;
1045 	rx_agg_buf->mapping = mapping;
1046 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1047 	rxbd->rx_bd_opaque = sw_prod;
1048 	return 0;
1049 }
1050 
1051 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1052 				       struct bnxt_cp_ring_info *cpr,
1053 				       u16 cp_cons, u16 curr)
1054 {
1055 	struct rx_agg_cmp *agg;
1056 
1057 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1058 	agg = (struct rx_agg_cmp *)
1059 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1060 	return agg;
1061 }
1062 
1063 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1064 					      struct bnxt_rx_ring_info *rxr,
1065 					      u16 agg_id, u16 curr)
1066 {
1067 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1068 
1069 	return &tpa_info->agg_arr[curr];
1070 }
1071 
1072 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1073 				   u16 start, u32 agg_bufs, bool tpa)
1074 {
1075 	struct bnxt_napi *bnapi = cpr->bnapi;
1076 	struct bnxt *bp = bnapi->bp;
1077 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1078 	u16 prod = rxr->rx_agg_prod;
1079 	u16 sw_prod = rxr->rx_sw_agg_prod;
1080 	bool p5_tpa = false;
1081 	u32 i;
1082 
1083 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1084 		p5_tpa = true;
1085 
1086 	for (i = 0; i < agg_bufs; i++) {
1087 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1088 		struct rx_agg_cmp *agg;
1089 		struct rx_bd *prod_bd;
1090 		netmem_ref netmem;
1091 		u16 cons;
1092 
1093 		if (p5_tpa)
1094 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1095 		else
1096 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1097 		cons = agg->rx_agg_cmp_opaque;
1098 		__clear_bit(cons, rxr->rx_agg_bmap);
1099 
1100 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1101 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1102 
1103 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1104 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1105 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1106 
1107 		/* It is possible for sw_prod to be equal to cons, so
1108 		 * set cons_rx_buf->netmem to 0 first.
1109 		 */
1110 		netmem = cons_rx_buf->netmem;
1111 		cons_rx_buf->netmem = 0;
1112 		prod_rx_buf->netmem = netmem;
1113 		prod_rx_buf->offset = cons_rx_buf->offset;
1114 
1115 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1116 
1117 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1118 
1119 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1120 		prod_bd->rx_bd_opaque = sw_prod;
1121 
1122 		prod = NEXT_RX_AGG(prod);
1123 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1124 	}
1125 	rxr->rx_agg_prod = prod;
1126 	rxr->rx_sw_agg_prod = sw_prod;
1127 }
1128 
1129 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1130 					      struct bnxt_rx_ring_info *rxr,
1131 					      u16 cons, void *data, u8 *data_ptr,
1132 					      dma_addr_t dma_addr,
1133 					      unsigned int offset_and_len)
1134 {
1135 	unsigned int len = offset_and_len & 0xffff;
1136 	struct page *page = data;
1137 	u16 prod = rxr->rx_prod;
1138 	struct sk_buff *skb;
1139 	int err;
1140 
1141 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1142 	if (unlikely(err)) {
1143 		bnxt_reuse_rx_data(rxr, cons, data);
1144 		return NULL;
1145 	}
1146 	dma_addr -= bp->rx_dma_offset;
1147 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1148 				bp->rx_dir);
1149 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1150 	if (!skb) {
1151 		page_pool_recycle_direct(rxr->page_pool, page);
1152 		return NULL;
1153 	}
1154 	skb_mark_for_recycle(skb);
1155 	skb_reserve(skb, bp->rx_offset);
1156 	__skb_put(skb, len);
1157 
1158 	return skb;
1159 }
1160 
1161 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1162 					struct bnxt_rx_ring_info *rxr,
1163 					u16 cons, void *data, u8 *data_ptr,
1164 					dma_addr_t dma_addr,
1165 					unsigned int offset_and_len)
1166 {
1167 	unsigned int payload = offset_and_len >> 16;
1168 	unsigned int len = offset_and_len & 0xffff;
1169 	skb_frag_t *frag;
1170 	struct page *page = data;
1171 	u16 prod = rxr->rx_prod;
1172 	struct sk_buff *skb;
1173 	int off, err;
1174 
1175 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1176 	if (unlikely(err)) {
1177 		bnxt_reuse_rx_data(rxr, cons, data);
1178 		return NULL;
1179 	}
1180 	dma_addr -= bp->rx_dma_offset;
1181 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1182 				bp->rx_dir);
1183 
1184 	if (unlikely(!payload))
1185 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1186 
1187 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1188 	if (!skb) {
1189 		page_pool_recycle_direct(rxr->page_pool, page);
1190 		return NULL;
1191 	}
1192 
1193 	skb_mark_for_recycle(skb);
1194 	off = (void *)data_ptr - page_address(page);
1195 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1196 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1197 	       payload + NET_IP_ALIGN);
1198 
1199 	frag = &skb_shinfo(skb)->frags[0];
1200 	skb_frag_size_sub(frag, payload);
1201 	skb_frag_off_add(frag, payload);
1202 	skb->data_len -= payload;
1203 	skb->tail += payload;
1204 
1205 	return skb;
1206 }
1207 
1208 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1209 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1210 				   void *data, u8 *data_ptr,
1211 				   dma_addr_t dma_addr,
1212 				   unsigned int offset_and_len)
1213 {
1214 	u16 prod = rxr->rx_prod;
1215 	struct sk_buff *skb;
1216 	int err;
1217 
1218 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1219 	if (unlikely(err)) {
1220 		bnxt_reuse_rx_data(rxr, cons, data);
1221 		return NULL;
1222 	}
1223 
1224 	skb = napi_build_skb(data, bp->rx_buf_size);
1225 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1226 				bp->rx_dir);
1227 	if (!skb) {
1228 		page_pool_free_va(rxr->head_pool, data, true);
1229 		return NULL;
1230 	}
1231 
1232 	skb_mark_for_recycle(skb);
1233 	skb_reserve(skb, bp->rx_offset);
1234 	skb_put(skb, offset_and_len & 0xffff);
1235 	return skb;
1236 }
1237 
1238 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1239 				 struct bnxt_cp_ring_info *cpr,
1240 				 u16 idx, u32 agg_bufs, bool tpa,
1241 				 struct sk_buff *skb,
1242 				 struct xdp_buff *xdp)
1243 {
1244 	struct bnxt_napi *bnapi = cpr->bnapi;
1245 	struct skb_shared_info *shinfo;
1246 	struct bnxt_rx_ring_info *rxr;
1247 	u32 i, total_frag_len = 0;
1248 	bool p5_tpa = false;
1249 	u16 prod;
1250 
1251 	rxr = bnapi->rx_ring;
1252 	prod = rxr->rx_agg_prod;
1253 
1254 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1255 		p5_tpa = true;
1256 
1257 	if (skb)
1258 		shinfo = skb_shinfo(skb);
1259 	else
1260 		shinfo = xdp_get_shared_info_from_buff(xdp);
1261 
1262 	for (i = 0; i < agg_bufs; i++) {
1263 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1264 		struct rx_agg_cmp *agg;
1265 		u16 cons, frag_len;
1266 		netmem_ref netmem;
1267 
1268 		if (p5_tpa)
1269 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1270 		else
1271 			agg = bnxt_get_agg(bp, cpr, idx, i);
1272 		cons = agg->rx_agg_cmp_opaque;
1273 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1274 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1275 
1276 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1277 		if (skb) {
1278 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1279 					       cons_rx_buf->offset,
1280 					       frag_len, BNXT_RX_PAGE_SIZE);
1281 		} else {
1282 			skb_frag_t *frag = &shinfo->frags[i];
1283 
1284 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1285 						  cons_rx_buf->offset,
1286 						  frag_len);
1287 			shinfo->nr_frags = i + 1;
1288 		}
1289 		__clear_bit(cons, rxr->rx_agg_bmap);
1290 
1291 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1292 		 * a sw_prod index that equals the cons index, so we
1293 		 * need to clear the cons entry now.
1294 		 */
1295 		netmem = cons_rx_buf->netmem;
1296 		cons_rx_buf->netmem = 0;
1297 
1298 		if (xdp && netmem_is_pfmemalloc(netmem))
1299 			xdp_buff_set_frag_pfmemalloc(xdp);
1300 
1301 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1302 			if (skb) {
1303 				skb->len -= frag_len;
1304 				skb->data_len -= frag_len;
1305 				skb->truesize -= BNXT_RX_PAGE_SIZE;
1306 			}
1307 
1308 			--shinfo->nr_frags;
1309 			cons_rx_buf->netmem = netmem;
1310 
1311 			/* Update prod since possibly some netmems have been
1312 			 * allocated already.
1313 			 */
1314 			rxr->rx_agg_prod = prod;
1315 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1316 			return 0;
1317 		}
1318 
1319 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1320 						  BNXT_RX_PAGE_SIZE);
1321 
1322 		total_frag_len += frag_len;
1323 		prod = NEXT_RX_AGG(prod);
1324 	}
1325 	rxr->rx_agg_prod = prod;
1326 	return total_frag_len;
1327 }
1328 
1329 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1330 					       struct bnxt_cp_ring_info *cpr,
1331 					       struct sk_buff *skb, u16 idx,
1332 					       u32 agg_bufs, bool tpa)
1333 {
1334 	u32 total_frag_len = 0;
1335 
1336 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1337 					       skb, NULL);
1338 	if (!total_frag_len) {
1339 		skb_mark_for_recycle(skb);
1340 		dev_kfree_skb(skb);
1341 		return NULL;
1342 	}
1343 
1344 	return skb;
1345 }
1346 
1347 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1348 				   struct bnxt_cp_ring_info *cpr,
1349 				   struct xdp_buff *xdp, u16 idx,
1350 				   u32 agg_bufs, bool tpa)
1351 {
1352 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1353 	u32 total_frag_len = 0;
1354 
1355 	if (!xdp_buff_has_frags(xdp))
1356 		shinfo->nr_frags = 0;
1357 
1358 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1359 					       NULL, xdp);
1360 	if (total_frag_len) {
1361 		xdp_buff_set_frags_flag(xdp);
1362 		shinfo->nr_frags = agg_bufs;
1363 		shinfo->xdp_frags_size = total_frag_len;
1364 	}
1365 	return total_frag_len;
1366 }
1367 
1368 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1369 			       u8 agg_bufs, u32 *raw_cons)
1370 {
1371 	u16 last;
1372 	struct rx_agg_cmp *agg;
1373 
1374 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1375 	last = RING_CMP(*raw_cons);
1376 	agg = (struct rx_agg_cmp *)
1377 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1378 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1379 }
1380 
1381 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1382 				      unsigned int len,
1383 				      dma_addr_t mapping)
1384 {
1385 	struct bnxt *bp = bnapi->bp;
1386 	struct pci_dev *pdev = bp->pdev;
1387 	struct sk_buff *skb;
1388 
1389 	skb = napi_alloc_skb(&bnapi->napi, len);
1390 	if (!skb)
1391 		return NULL;
1392 
1393 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1394 				bp->rx_dir);
1395 
1396 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1397 	       len + NET_IP_ALIGN);
1398 
1399 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1400 				   bp->rx_dir);
1401 
1402 	skb_put(skb, len);
1403 
1404 	return skb;
1405 }
1406 
1407 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1408 				     unsigned int len,
1409 				     dma_addr_t mapping)
1410 {
1411 	return bnxt_copy_data(bnapi, data, len, mapping);
1412 }
1413 
1414 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1415 				     struct xdp_buff *xdp,
1416 				     unsigned int len,
1417 				     dma_addr_t mapping)
1418 {
1419 	unsigned int metasize = 0;
1420 	u8 *data = xdp->data;
1421 	struct sk_buff *skb;
1422 
1423 	len = xdp->data_end - xdp->data_meta;
1424 	metasize = xdp->data - xdp->data_meta;
1425 	data = xdp->data_meta;
1426 
1427 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1428 	if (!skb)
1429 		return skb;
1430 
1431 	if (metasize) {
1432 		skb_metadata_set(skb, metasize);
1433 		__skb_pull(skb, metasize);
1434 	}
1435 
1436 	return skb;
1437 }
1438 
1439 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1440 			   u32 *raw_cons, void *cmp)
1441 {
1442 	struct rx_cmp *rxcmp = cmp;
1443 	u32 tmp_raw_cons = *raw_cons;
1444 	u8 cmp_type, agg_bufs = 0;
1445 
1446 	cmp_type = RX_CMP_TYPE(rxcmp);
1447 
1448 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1449 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1450 			    RX_CMP_AGG_BUFS) >>
1451 			   RX_CMP_AGG_BUFS_SHIFT;
1452 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1453 		struct rx_tpa_end_cmp *tpa_end = cmp;
1454 
1455 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1456 			return 0;
1457 
1458 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1459 	}
1460 
1461 	if (agg_bufs) {
1462 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1463 			return -EBUSY;
1464 	}
1465 	*raw_cons = tmp_raw_cons;
1466 	return 0;
1467 }
1468 
1469 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1470 {
1471 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1472 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1473 
1474 	if (test_bit(idx, map->agg_idx_bmap))
1475 		idx = find_first_zero_bit(map->agg_idx_bmap,
1476 					  BNXT_AGG_IDX_BMAP_SIZE);
1477 	__set_bit(idx, map->agg_idx_bmap);
1478 	map->agg_id_tbl[agg_id] = idx;
1479 	return idx;
1480 }
1481 
1482 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1483 {
1484 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1485 
1486 	__clear_bit(idx, map->agg_idx_bmap);
1487 }
1488 
1489 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1490 {
1491 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1492 
1493 	return map->agg_id_tbl[agg_id];
1494 }
1495 
1496 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1497 			      struct rx_tpa_start_cmp *tpa_start,
1498 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1499 {
1500 	tpa_info->cfa_code_valid = 1;
1501 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1502 	tpa_info->vlan_valid = 0;
1503 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1504 		tpa_info->vlan_valid = 1;
1505 		tpa_info->metadata =
1506 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1507 	}
1508 }
1509 
1510 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1511 				 struct rx_tpa_start_cmp *tpa_start,
1512 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1513 {
1514 	tpa_info->vlan_valid = 0;
1515 	if (TPA_START_VLAN_VALID(tpa_start)) {
1516 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1517 		u32 vlan_proto = ETH_P_8021Q;
1518 
1519 		tpa_info->vlan_valid = 1;
1520 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1521 			vlan_proto = ETH_P_8021AD;
1522 		tpa_info->metadata = vlan_proto << 16 |
1523 				     TPA_START_METADATA0_TCI(tpa_start1);
1524 	}
1525 }
1526 
1527 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1528 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1529 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1530 {
1531 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1532 	struct bnxt_tpa_info *tpa_info;
1533 	u16 cons, prod, agg_id;
1534 	struct rx_bd *prod_bd;
1535 	dma_addr_t mapping;
1536 
1537 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1538 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1539 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1540 	} else {
1541 		agg_id = TPA_START_AGG_ID(tpa_start);
1542 	}
1543 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1544 	prod = rxr->rx_prod;
1545 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1546 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1547 	tpa_info = &rxr->rx_tpa[agg_id];
1548 
1549 	if (unlikely(cons != rxr->rx_next_cons ||
1550 		     TPA_START_ERROR(tpa_start))) {
1551 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1552 			    cons, rxr->rx_next_cons,
1553 			    TPA_START_ERROR_CODE(tpa_start1));
1554 		bnxt_sched_reset_rxr(bp, rxr);
1555 		return;
1556 	}
1557 	prod_rx_buf->data = tpa_info->data;
1558 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1559 
1560 	mapping = tpa_info->mapping;
1561 	prod_rx_buf->mapping = mapping;
1562 
1563 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1564 
1565 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1566 
1567 	tpa_info->data = cons_rx_buf->data;
1568 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1569 	cons_rx_buf->data = NULL;
1570 	tpa_info->mapping = cons_rx_buf->mapping;
1571 
1572 	tpa_info->len =
1573 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1574 				RX_TPA_START_CMP_LEN_SHIFT;
1575 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1576 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1577 		tpa_info->gso_type = SKB_GSO_TCPV4;
1578 		if (TPA_START_IS_IPV6(tpa_start1))
1579 			tpa_info->gso_type = SKB_GSO_TCPV6;
1580 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1581 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1582 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1583 			tpa_info->gso_type = SKB_GSO_TCPV6;
1584 		tpa_info->rss_hash =
1585 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1586 	} else {
1587 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1588 		tpa_info->gso_type = 0;
1589 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1590 	}
1591 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1592 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1593 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1594 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1595 	else
1596 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1597 	tpa_info->agg_count = 0;
1598 
1599 	rxr->rx_prod = NEXT_RX(prod);
1600 	cons = RING_RX(bp, NEXT_RX(cons));
1601 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1602 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1603 
1604 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1605 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1606 	cons_rx_buf->data = NULL;
1607 }
1608 
1609 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1610 {
1611 	if (agg_bufs)
1612 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1613 }
1614 
1615 #ifdef CONFIG_INET
1616 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1617 {
1618 	struct udphdr *uh = NULL;
1619 
1620 	if (ip_proto == htons(ETH_P_IP)) {
1621 		struct iphdr *iph = (struct iphdr *)skb->data;
1622 
1623 		if (iph->protocol == IPPROTO_UDP)
1624 			uh = (struct udphdr *)(iph + 1);
1625 	} else {
1626 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1627 
1628 		if (iph->nexthdr == IPPROTO_UDP)
1629 			uh = (struct udphdr *)(iph + 1);
1630 	}
1631 	if (uh) {
1632 		if (uh->check)
1633 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1634 		else
1635 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1636 	}
1637 }
1638 #endif
1639 
1640 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1641 					   int payload_off, int tcp_ts,
1642 					   struct sk_buff *skb)
1643 {
1644 #ifdef CONFIG_INET
1645 	struct tcphdr *th;
1646 	int len, nw_off;
1647 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1648 	u32 hdr_info = tpa_info->hdr_info;
1649 	bool loopback = false;
1650 
1651 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1652 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1653 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1654 
1655 	/* If the packet is an internal loopback packet, the offsets will
1656 	 * have an extra 4 bytes.
1657 	 */
1658 	if (inner_mac_off == 4) {
1659 		loopback = true;
1660 	} else if (inner_mac_off > 4) {
1661 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1662 					    ETH_HLEN - 2));
1663 
1664 		/* We only support inner iPv4/ipv6.  If we don't see the
1665 		 * correct protocol ID, it must be a loopback packet where
1666 		 * the offsets are off by 4.
1667 		 */
1668 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1669 			loopback = true;
1670 	}
1671 	if (loopback) {
1672 		/* internal loopback packet, subtract all offsets by 4 */
1673 		inner_ip_off -= 4;
1674 		inner_mac_off -= 4;
1675 		outer_ip_off -= 4;
1676 	}
1677 
1678 	nw_off = inner_ip_off - ETH_HLEN;
1679 	skb_set_network_header(skb, nw_off);
1680 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1681 		struct ipv6hdr *iph = ipv6_hdr(skb);
1682 
1683 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1684 		len = skb->len - skb_transport_offset(skb);
1685 		th = tcp_hdr(skb);
1686 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1687 	} else {
1688 		struct iphdr *iph = ip_hdr(skb);
1689 
1690 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1691 		len = skb->len - skb_transport_offset(skb);
1692 		th = tcp_hdr(skb);
1693 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1694 	}
1695 
1696 	if (inner_mac_off) { /* tunnel */
1697 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1698 					    ETH_HLEN - 2));
1699 
1700 		bnxt_gro_tunnel(skb, proto);
1701 	}
1702 #endif
1703 	return skb;
1704 }
1705 
1706 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1707 					   int payload_off, int tcp_ts,
1708 					   struct sk_buff *skb)
1709 {
1710 #ifdef CONFIG_INET
1711 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1712 	u32 hdr_info = tpa_info->hdr_info;
1713 	int iphdr_len, nw_off;
1714 
1715 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1716 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1717 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1718 
1719 	nw_off = inner_ip_off - ETH_HLEN;
1720 	skb_set_network_header(skb, nw_off);
1721 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1722 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1723 	skb_set_transport_header(skb, nw_off + iphdr_len);
1724 
1725 	if (inner_mac_off) { /* tunnel */
1726 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1727 					    ETH_HLEN - 2));
1728 
1729 		bnxt_gro_tunnel(skb, proto);
1730 	}
1731 #endif
1732 	return skb;
1733 }
1734 
1735 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1736 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1737 
1738 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1739 					   int payload_off, int tcp_ts,
1740 					   struct sk_buff *skb)
1741 {
1742 #ifdef CONFIG_INET
1743 	struct tcphdr *th;
1744 	int len, nw_off, tcp_opt_len = 0;
1745 
1746 	if (tcp_ts)
1747 		tcp_opt_len = 12;
1748 
1749 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1750 		struct iphdr *iph;
1751 
1752 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1753 			 ETH_HLEN;
1754 		skb_set_network_header(skb, nw_off);
1755 		iph = ip_hdr(skb);
1756 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1757 		len = skb->len - skb_transport_offset(skb);
1758 		th = tcp_hdr(skb);
1759 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1760 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1761 		struct ipv6hdr *iph;
1762 
1763 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1764 			 ETH_HLEN;
1765 		skb_set_network_header(skb, nw_off);
1766 		iph = ipv6_hdr(skb);
1767 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1768 		len = skb->len - skb_transport_offset(skb);
1769 		th = tcp_hdr(skb);
1770 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1771 	} else {
1772 		dev_kfree_skb_any(skb);
1773 		return NULL;
1774 	}
1775 
1776 	if (nw_off) /* tunnel */
1777 		bnxt_gro_tunnel(skb, skb->protocol);
1778 #endif
1779 	return skb;
1780 }
1781 
1782 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1783 					   struct bnxt_tpa_info *tpa_info,
1784 					   struct rx_tpa_end_cmp *tpa_end,
1785 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1786 					   struct sk_buff *skb)
1787 {
1788 #ifdef CONFIG_INET
1789 	int payload_off;
1790 	u16 segs;
1791 
1792 	segs = TPA_END_TPA_SEGS(tpa_end);
1793 	if (segs == 1)
1794 		return skb;
1795 
1796 	NAPI_GRO_CB(skb)->count = segs;
1797 	skb_shinfo(skb)->gso_size =
1798 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1799 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1800 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1801 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1802 	else
1803 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1804 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1805 	if (likely(skb))
1806 		tcp_gro_complete(skb);
1807 #endif
1808 	return skb;
1809 }
1810 
1811 /* Given the cfa_code of a received packet determine which
1812  * netdev (vf-rep or PF) the packet is destined to.
1813  */
1814 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1815 {
1816 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1817 
1818 	/* if vf-rep dev is NULL, it must belong to the PF */
1819 	return dev ? dev : bp->dev;
1820 }
1821 
1822 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1823 					   struct bnxt_cp_ring_info *cpr,
1824 					   u32 *raw_cons,
1825 					   struct rx_tpa_end_cmp *tpa_end,
1826 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1827 					   u8 *event)
1828 {
1829 	struct bnxt_napi *bnapi = cpr->bnapi;
1830 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1831 	struct net_device *dev = bp->dev;
1832 	u8 *data_ptr, agg_bufs;
1833 	unsigned int len;
1834 	struct bnxt_tpa_info *tpa_info;
1835 	dma_addr_t mapping;
1836 	struct sk_buff *skb;
1837 	u16 idx = 0, agg_id;
1838 	void *data;
1839 	bool gro;
1840 
1841 	if (unlikely(bnapi->in_reset)) {
1842 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1843 
1844 		if (rc < 0)
1845 			return ERR_PTR(-EBUSY);
1846 		return NULL;
1847 	}
1848 
1849 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1850 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1851 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1852 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1853 		tpa_info = &rxr->rx_tpa[agg_id];
1854 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1855 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1856 				    agg_bufs, tpa_info->agg_count);
1857 			agg_bufs = tpa_info->agg_count;
1858 		}
1859 		tpa_info->agg_count = 0;
1860 		*event |= BNXT_AGG_EVENT;
1861 		bnxt_free_agg_idx(rxr, agg_id);
1862 		idx = agg_id;
1863 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1864 	} else {
1865 		agg_id = TPA_END_AGG_ID(tpa_end);
1866 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1867 		tpa_info = &rxr->rx_tpa[agg_id];
1868 		idx = RING_CMP(*raw_cons);
1869 		if (agg_bufs) {
1870 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1871 				return ERR_PTR(-EBUSY);
1872 
1873 			*event |= BNXT_AGG_EVENT;
1874 			idx = NEXT_CMP(idx);
1875 		}
1876 		gro = !!TPA_END_GRO(tpa_end);
1877 	}
1878 	data = tpa_info->data;
1879 	data_ptr = tpa_info->data_ptr;
1880 	prefetch(data_ptr);
1881 	len = tpa_info->len;
1882 	mapping = tpa_info->mapping;
1883 
1884 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1885 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1886 		if (agg_bufs > MAX_SKB_FRAGS)
1887 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1888 				    agg_bufs, (int)MAX_SKB_FRAGS);
1889 		return NULL;
1890 	}
1891 
1892 	if (len <= bp->rx_copybreak) {
1893 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1894 		if (!skb) {
1895 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1896 			cpr->sw_stats->rx.rx_oom_discards += 1;
1897 			return NULL;
1898 		}
1899 	} else {
1900 		u8 *new_data;
1901 		dma_addr_t new_mapping;
1902 
1903 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1904 						GFP_ATOMIC);
1905 		if (!new_data) {
1906 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1907 			cpr->sw_stats->rx.rx_oom_discards += 1;
1908 			return NULL;
1909 		}
1910 
1911 		tpa_info->data = new_data;
1912 		tpa_info->data_ptr = new_data + bp->rx_offset;
1913 		tpa_info->mapping = new_mapping;
1914 
1915 		skb = napi_build_skb(data, bp->rx_buf_size);
1916 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1917 					bp->rx_buf_use_size, bp->rx_dir);
1918 
1919 		if (!skb) {
1920 			page_pool_free_va(rxr->head_pool, data, true);
1921 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1922 			cpr->sw_stats->rx.rx_oom_discards += 1;
1923 			return NULL;
1924 		}
1925 		skb_mark_for_recycle(skb);
1926 		skb_reserve(skb, bp->rx_offset);
1927 		skb_put(skb, len);
1928 	}
1929 
1930 	if (agg_bufs) {
1931 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1932 					      true);
1933 		if (!skb) {
1934 			/* Page reuse already handled by bnxt_rx_pages(). */
1935 			cpr->sw_stats->rx.rx_oom_discards += 1;
1936 			return NULL;
1937 		}
1938 	}
1939 
1940 	if (tpa_info->cfa_code_valid)
1941 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1942 	skb->protocol = eth_type_trans(skb, dev);
1943 
1944 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1945 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1946 
1947 	if (tpa_info->vlan_valid &&
1948 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1949 		__be16 vlan_proto = htons(tpa_info->metadata >>
1950 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1951 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1952 
1953 		if (eth_type_vlan(vlan_proto)) {
1954 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1955 		} else {
1956 			dev_kfree_skb(skb);
1957 			return NULL;
1958 		}
1959 	}
1960 
1961 	skb_checksum_none_assert(skb);
1962 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1963 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1964 		skb->csum_level =
1965 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1966 	}
1967 
1968 	if (gro)
1969 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1970 
1971 	return skb;
1972 }
1973 
1974 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1975 			 struct rx_agg_cmp *rx_agg)
1976 {
1977 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1978 	struct bnxt_tpa_info *tpa_info;
1979 
1980 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1981 	tpa_info = &rxr->rx_tpa[agg_id];
1982 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1983 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1984 }
1985 
1986 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1987 			     struct sk_buff *skb)
1988 {
1989 	skb_mark_for_recycle(skb);
1990 
1991 	if (skb->dev != bp->dev) {
1992 		/* this packet belongs to a vf-rep */
1993 		bnxt_vf_rep_rx(bp, skb);
1994 		return;
1995 	}
1996 	skb_record_rx_queue(skb, bnapi->index);
1997 	napi_gro_receive(&bnapi->napi, skb);
1998 }
1999 
2000 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
2001 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
2002 {
2003 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
2004 
2005 	if (BNXT_PTP_RX_TS_VALID(flags))
2006 		goto ts_valid;
2007 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2008 		return false;
2009 
2010 ts_valid:
2011 	*cmpl_ts = ts;
2012 	return true;
2013 }
2014 
2015 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2016 				    struct rx_cmp *rxcmp,
2017 				    struct rx_cmp_ext *rxcmp1)
2018 {
2019 	__be16 vlan_proto;
2020 	u16 vtag;
2021 
2022 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2023 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2024 		u32 meta_data;
2025 
2026 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2027 			return skb;
2028 
2029 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2030 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2031 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2032 		if (eth_type_vlan(vlan_proto))
2033 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2034 		else
2035 			goto vlan_err;
2036 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2037 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2038 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2039 
2040 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2041 				vlan_proto = htons(ETH_P_8021Q);
2042 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2043 				vlan_proto = htons(ETH_P_8021AD);
2044 			else
2045 				goto vlan_err;
2046 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2047 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2048 		}
2049 	}
2050 	return skb;
2051 vlan_err:
2052 	skb_mark_for_recycle(skb);
2053 	dev_kfree_skb(skb);
2054 	return NULL;
2055 }
2056 
2057 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2058 					   struct rx_cmp *rxcmp)
2059 {
2060 	u8 ext_op;
2061 
2062 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2063 	switch (ext_op) {
2064 	case EXT_OP_INNER_4:
2065 	case EXT_OP_OUTER_4:
2066 	case EXT_OP_INNFL_3:
2067 	case EXT_OP_OUTFL_3:
2068 		return PKT_HASH_TYPE_L4;
2069 	default:
2070 		return PKT_HASH_TYPE_L3;
2071 	}
2072 }
2073 
2074 /* returns the following:
2075  * 1       - 1 packet successfully received
2076  * 0       - successful TPA_START, packet not completed yet
2077  * -EBUSY  - completion ring does not have all the agg buffers yet
2078  * -ENOMEM - packet aborted due to out of memory
2079  * -EIO    - packet aborted due to hw error indicated in BD
2080  */
2081 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2082 		       u32 *raw_cons, u8 *event)
2083 {
2084 	struct bnxt_napi *bnapi = cpr->bnapi;
2085 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2086 	struct net_device *dev = bp->dev;
2087 	struct rx_cmp *rxcmp;
2088 	struct rx_cmp_ext *rxcmp1;
2089 	u32 tmp_raw_cons = *raw_cons;
2090 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2091 	struct skb_shared_info *sinfo;
2092 	struct bnxt_sw_rx_bd *rx_buf;
2093 	unsigned int len;
2094 	u8 *data_ptr, agg_bufs, cmp_type;
2095 	bool xdp_active = false;
2096 	dma_addr_t dma_addr;
2097 	struct sk_buff *skb;
2098 	struct xdp_buff xdp;
2099 	u32 flags, misc;
2100 	u32 cmpl_ts;
2101 	void *data;
2102 	int rc = 0;
2103 
2104 	rxcmp = (struct rx_cmp *)
2105 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2106 
2107 	cmp_type = RX_CMP_TYPE(rxcmp);
2108 
2109 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2110 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2111 		goto next_rx_no_prod_no_len;
2112 	}
2113 
2114 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2115 	cp_cons = RING_CMP(tmp_raw_cons);
2116 	rxcmp1 = (struct rx_cmp_ext *)
2117 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2118 
2119 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2120 		return -EBUSY;
2121 
2122 	/* The valid test of the entry must be done first before
2123 	 * reading any further.
2124 	 */
2125 	dma_rmb();
2126 	prod = rxr->rx_prod;
2127 
2128 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2129 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2130 		bnxt_tpa_start(bp, rxr, cmp_type,
2131 			       (struct rx_tpa_start_cmp *)rxcmp,
2132 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2133 
2134 		*event |= BNXT_RX_EVENT;
2135 		goto next_rx_no_prod_no_len;
2136 
2137 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2138 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2139 				   (struct rx_tpa_end_cmp *)rxcmp,
2140 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2141 
2142 		if (IS_ERR(skb))
2143 			return -EBUSY;
2144 
2145 		rc = -ENOMEM;
2146 		if (likely(skb)) {
2147 			bnxt_deliver_skb(bp, bnapi, skb);
2148 			rc = 1;
2149 		}
2150 		*event |= BNXT_RX_EVENT;
2151 		goto next_rx_no_prod_no_len;
2152 	}
2153 
2154 	cons = rxcmp->rx_cmp_opaque;
2155 	if (unlikely(cons != rxr->rx_next_cons)) {
2156 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2157 
2158 		/* 0xffff is forced error, don't print it */
2159 		if (rxr->rx_next_cons != 0xffff)
2160 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2161 				    cons, rxr->rx_next_cons);
2162 		bnxt_sched_reset_rxr(bp, rxr);
2163 		if (rc1)
2164 			return rc1;
2165 		goto next_rx_no_prod_no_len;
2166 	}
2167 	rx_buf = &rxr->rx_buf_ring[cons];
2168 	data = rx_buf->data;
2169 	data_ptr = rx_buf->data_ptr;
2170 	prefetch(data_ptr);
2171 
2172 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2173 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2174 
2175 	if (agg_bufs) {
2176 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2177 			return -EBUSY;
2178 
2179 		cp_cons = NEXT_CMP(cp_cons);
2180 		*event |= BNXT_AGG_EVENT;
2181 	}
2182 	*event |= BNXT_RX_EVENT;
2183 
2184 	rx_buf->data = NULL;
2185 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2186 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2187 
2188 		bnxt_reuse_rx_data(rxr, cons, data);
2189 		if (agg_bufs)
2190 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2191 					       false);
2192 
2193 		rc = -EIO;
2194 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2195 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2196 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2197 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2198 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2199 						 rx_err);
2200 				bnxt_sched_reset_rxr(bp, rxr);
2201 			}
2202 		}
2203 		goto next_rx_no_len;
2204 	}
2205 
2206 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2207 	len = flags >> RX_CMP_LEN_SHIFT;
2208 	dma_addr = rx_buf->mapping;
2209 
2210 	if (bnxt_xdp_attached(bp, rxr)) {
2211 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2212 		if (agg_bufs) {
2213 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp,
2214 							       cp_cons,
2215 							       agg_bufs,
2216 							       false);
2217 			if (!frag_len)
2218 				goto oom_next_rx;
2219 
2220 		}
2221 		xdp_active = true;
2222 	}
2223 
2224 	if (xdp_active) {
2225 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2226 			rc = 1;
2227 			goto next_rx;
2228 		}
2229 		if (xdp_buff_has_frags(&xdp)) {
2230 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2231 			agg_bufs = sinfo->nr_frags;
2232 		} else {
2233 			agg_bufs = 0;
2234 		}
2235 	}
2236 
2237 	if (len <= bp->rx_copybreak) {
2238 		if (!xdp_active)
2239 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2240 		else
2241 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2242 		bnxt_reuse_rx_data(rxr, cons, data);
2243 		if (!skb) {
2244 			if (agg_bufs) {
2245 				if (!xdp_active)
2246 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2247 							       agg_bufs, false);
2248 				else
2249 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2250 			}
2251 			goto oom_next_rx;
2252 		}
2253 	} else {
2254 		u32 payload;
2255 
2256 		if (rx_buf->data_ptr == data_ptr)
2257 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2258 		else
2259 			payload = 0;
2260 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2261 				      payload | len);
2262 		if (!skb)
2263 			goto oom_next_rx;
2264 	}
2265 
2266 	if (agg_bufs) {
2267 		if (!xdp_active) {
2268 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2269 						      agg_bufs, false);
2270 			if (!skb)
2271 				goto oom_next_rx;
2272 		} else {
2273 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2274 						 rxr->page_pool, &xdp);
2275 			if (!skb) {
2276 				/* we should be able to free the old skb here */
2277 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2278 				goto oom_next_rx;
2279 			}
2280 		}
2281 	}
2282 
2283 	if (RX_CMP_HASH_VALID(rxcmp)) {
2284 		enum pkt_hash_types type;
2285 
2286 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2287 			type = bnxt_rss_ext_op(bp, rxcmp);
2288 		} else {
2289 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2290 
2291 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2292 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2293 				type = PKT_HASH_TYPE_L4;
2294 			else
2295 				type = PKT_HASH_TYPE_L3;
2296 		}
2297 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2298 	}
2299 
2300 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2301 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2302 	skb->protocol = eth_type_trans(skb, dev);
2303 
2304 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2305 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2306 		if (!skb)
2307 			goto next_rx;
2308 	}
2309 
2310 	skb_checksum_none_assert(skb);
2311 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2312 		if (dev->features & NETIF_F_RXCSUM) {
2313 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2314 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2315 		}
2316 	} else {
2317 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2318 			if (dev->features & NETIF_F_RXCSUM)
2319 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2320 		}
2321 	}
2322 
2323 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2324 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2325 			u64 ns, ts;
2326 
2327 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2328 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2329 
2330 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2331 				memset(skb_hwtstamps(skb), 0,
2332 				       sizeof(*skb_hwtstamps(skb)));
2333 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2334 			}
2335 		}
2336 	}
2337 	bnxt_deliver_skb(bp, bnapi, skb);
2338 	rc = 1;
2339 
2340 next_rx:
2341 	cpr->rx_packets += 1;
2342 	cpr->rx_bytes += len;
2343 
2344 next_rx_no_len:
2345 	rxr->rx_prod = NEXT_RX(prod);
2346 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2347 
2348 next_rx_no_prod_no_len:
2349 	*raw_cons = tmp_raw_cons;
2350 
2351 	return rc;
2352 
2353 oom_next_rx:
2354 	cpr->sw_stats->rx.rx_oom_discards += 1;
2355 	rc = -ENOMEM;
2356 	goto next_rx;
2357 }
2358 
2359 /* In netpoll mode, if we are using a combined completion ring, we need to
2360  * discard the rx packets and recycle the buffers.
2361  */
2362 static int bnxt_force_rx_discard(struct bnxt *bp,
2363 				 struct bnxt_cp_ring_info *cpr,
2364 				 u32 *raw_cons, u8 *event)
2365 {
2366 	u32 tmp_raw_cons = *raw_cons;
2367 	struct rx_cmp_ext *rxcmp1;
2368 	struct rx_cmp *rxcmp;
2369 	u16 cp_cons;
2370 	u8 cmp_type;
2371 	int rc;
2372 
2373 	cp_cons = RING_CMP(tmp_raw_cons);
2374 	rxcmp = (struct rx_cmp *)
2375 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2376 
2377 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2378 	cp_cons = RING_CMP(tmp_raw_cons);
2379 	rxcmp1 = (struct rx_cmp_ext *)
2380 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2381 
2382 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2383 		return -EBUSY;
2384 
2385 	/* The valid test of the entry must be done first before
2386 	 * reading any further.
2387 	 */
2388 	dma_rmb();
2389 	cmp_type = RX_CMP_TYPE(rxcmp);
2390 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2391 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2392 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2393 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2394 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2395 		struct rx_tpa_end_cmp_ext *tpa_end1;
2396 
2397 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2398 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2399 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2400 	}
2401 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2402 	if (rc && rc != -EBUSY)
2403 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2404 	return rc;
2405 }
2406 
2407 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2408 {
2409 	struct bnxt_fw_health *fw_health = bp->fw_health;
2410 	u32 reg = fw_health->regs[reg_idx];
2411 	u32 reg_type, reg_off, val = 0;
2412 
2413 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2414 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2415 	switch (reg_type) {
2416 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2417 		pci_read_config_dword(bp->pdev, reg_off, &val);
2418 		break;
2419 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2420 		reg_off = fw_health->mapped_regs[reg_idx];
2421 		fallthrough;
2422 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2423 		val = readl(bp->bar0 + reg_off);
2424 		break;
2425 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2426 		val = readl(bp->bar1 + reg_off);
2427 		break;
2428 	}
2429 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2430 		val &= fw_health->fw_reset_inprog_reg_mask;
2431 	return val;
2432 }
2433 
2434 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2435 {
2436 	int i;
2437 
2438 	for (i = 0; i < bp->rx_nr_rings; i++) {
2439 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2440 		struct bnxt_ring_grp_info *grp_info;
2441 
2442 		grp_info = &bp->grp_info[grp_idx];
2443 		if (grp_info->agg_fw_ring_id == ring_id)
2444 			return grp_idx;
2445 	}
2446 	return INVALID_HW_RING_ID;
2447 }
2448 
2449 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2450 {
2451 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2452 
2453 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2454 		return link_info->force_link_speed2;
2455 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2456 		return link_info->force_pam4_link_speed;
2457 	return link_info->force_link_speed;
2458 }
2459 
2460 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2461 {
2462 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2463 
2464 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2465 		link_info->req_link_speed = link_info->force_link_speed2;
2466 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2467 		switch (link_info->req_link_speed) {
2468 		case BNXT_LINK_SPEED_50GB_PAM4:
2469 		case BNXT_LINK_SPEED_100GB_PAM4:
2470 		case BNXT_LINK_SPEED_200GB_PAM4:
2471 		case BNXT_LINK_SPEED_400GB_PAM4:
2472 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2473 			break;
2474 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2475 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2476 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2477 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2478 			break;
2479 		default:
2480 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2481 		}
2482 		return;
2483 	}
2484 	link_info->req_link_speed = link_info->force_link_speed;
2485 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2486 	if (link_info->force_pam4_link_speed) {
2487 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2488 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2489 	}
2490 }
2491 
2492 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2493 {
2494 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2495 
2496 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2497 		link_info->advertising = link_info->auto_link_speeds2;
2498 		return;
2499 	}
2500 	link_info->advertising = link_info->auto_link_speeds;
2501 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2502 }
2503 
2504 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2505 {
2506 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2507 
2508 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2509 		if (link_info->req_link_speed != link_info->force_link_speed2)
2510 			return true;
2511 		return false;
2512 	}
2513 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2514 	    link_info->req_link_speed != link_info->force_link_speed)
2515 		return true;
2516 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2517 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2518 		return true;
2519 	return false;
2520 }
2521 
2522 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2523 {
2524 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2525 
2526 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2527 		if (link_info->advertising != link_info->auto_link_speeds2)
2528 			return true;
2529 		return false;
2530 	}
2531 	if (link_info->advertising != link_info->auto_link_speeds ||
2532 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2533 		return true;
2534 	return false;
2535 }
2536 
2537 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2538 {
2539 	u32 flags = bp->ctx->ctx_arr[type].flags;
2540 
2541 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2542 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2543 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2544 }
2545 
2546 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2547 {
2548 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2549 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2550 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2551 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2552 	struct bnxt_bs_trace_info *bs_trace;
2553 	int last_pg;
2554 
2555 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2556 		return;
2557 
2558 	mem_size = ctxm->max_entries * ctxm->entry_size;
2559 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2560 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2561 
2562 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2563 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2564 
2565 	rmem = &ctx_pg[0].ring_mem;
2566 	bs_trace = &bp->bs_trace[trace_type];
2567 	bs_trace->ctx_type = ctxm->type;
2568 	bs_trace->trace_type = trace_type;
2569 	if (pages > MAX_CTX_PAGES) {
2570 		int last_pg_dir = rmem->nr_pages - 1;
2571 
2572 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2573 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2574 	} else {
2575 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2576 	}
2577 	bs_trace->magic_byte += magic_byte_offset;
2578 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2579 }
2580 
2581 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2582 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2583 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2584 
2585 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2586 	(((data2) &							\
2587 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2588 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2589 
2590 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2591 	((data2) &							\
2592 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2593 
2594 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2595 	(((data2) &							\
2596 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2597 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2598 
2599 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2600 	((data1) &							\
2601 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2602 
2603 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2604 	(((data1) &							\
2605 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2606 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2607 
2608 /* Return true if the workqueue has to be scheduled */
2609 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2610 {
2611 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2612 
2613 	switch (err_type) {
2614 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2615 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2616 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2617 		break;
2618 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2619 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2620 		break;
2621 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2622 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2623 		break;
2624 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2625 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2626 		char *threshold_type;
2627 		bool notify = false;
2628 		char *dir_str;
2629 
2630 		switch (type) {
2631 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2632 			threshold_type = "warning";
2633 			break;
2634 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2635 			threshold_type = "critical";
2636 			break;
2637 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2638 			threshold_type = "fatal";
2639 			break;
2640 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2641 			threshold_type = "shutdown";
2642 			break;
2643 		default:
2644 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2645 			return false;
2646 		}
2647 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2648 			dir_str = "above";
2649 			notify = true;
2650 		} else {
2651 			dir_str = "below";
2652 		}
2653 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2654 			    dir_str, threshold_type);
2655 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2656 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2657 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2658 		if (notify) {
2659 			bp->thermal_threshold_type = type;
2660 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2661 			return true;
2662 		}
2663 		return false;
2664 	}
2665 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2666 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2667 		break;
2668 	default:
2669 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2670 			   err_type);
2671 		break;
2672 	}
2673 	return false;
2674 }
2675 
2676 #define BNXT_GET_EVENT_PORT(data)	\
2677 	((data) &			\
2678 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2679 
2680 #define BNXT_EVENT_RING_TYPE(data2)	\
2681 	((data2) &			\
2682 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2683 
2684 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2685 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2686 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2687 
2688 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2689 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2690 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2691 
2692 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2693 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2694 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2695 
2696 #define BNXT_PHC_BITS	48
2697 
2698 static int bnxt_async_event_process(struct bnxt *bp,
2699 				    struct hwrm_async_event_cmpl *cmpl)
2700 {
2701 	u16 event_id = le16_to_cpu(cmpl->event_id);
2702 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2703 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2704 
2705 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2706 		   event_id, data1, data2);
2707 
2708 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2709 	switch (event_id) {
2710 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2711 		struct bnxt_link_info *link_info = &bp->link_info;
2712 
2713 		if (BNXT_VF(bp))
2714 			goto async_event_process_exit;
2715 
2716 		/* print unsupported speed warning in forced speed mode only */
2717 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2718 		    (data1 & 0x20000)) {
2719 			u16 fw_speed = bnxt_get_force_speed(link_info);
2720 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2721 
2722 			if (speed != SPEED_UNKNOWN)
2723 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2724 					    speed);
2725 		}
2726 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2727 	}
2728 		fallthrough;
2729 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2730 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2731 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2732 		fallthrough;
2733 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2734 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2735 		break;
2736 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2737 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2738 		break;
2739 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2740 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2741 
2742 		if (BNXT_VF(bp))
2743 			break;
2744 
2745 		if (bp->pf.port_id != port_id)
2746 			break;
2747 
2748 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2749 		break;
2750 	}
2751 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2752 		if (BNXT_PF(bp))
2753 			goto async_event_process_exit;
2754 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2755 		break;
2756 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2757 		char *type_str = "Solicited";
2758 
2759 		if (!bp->fw_health)
2760 			goto async_event_process_exit;
2761 
2762 		bp->fw_reset_timestamp = jiffies;
2763 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2764 		if (!bp->fw_reset_min_dsecs)
2765 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2766 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2767 		if (!bp->fw_reset_max_dsecs)
2768 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2769 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2770 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2771 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2772 			type_str = "Fatal";
2773 			bp->fw_health->fatalities++;
2774 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2775 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2776 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2777 			type_str = "Non-fatal";
2778 			bp->fw_health->survivals++;
2779 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2780 		}
2781 		netif_warn(bp, hw, bp->dev,
2782 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2783 			   type_str, data1, data2,
2784 			   bp->fw_reset_min_dsecs * 100,
2785 			   bp->fw_reset_max_dsecs * 100);
2786 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2787 		break;
2788 	}
2789 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2790 		struct bnxt_fw_health *fw_health = bp->fw_health;
2791 		char *status_desc = "healthy";
2792 		u32 status;
2793 
2794 		if (!fw_health)
2795 			goto async_event_process_exit;
2796 
2797 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2798 			fw_health->enabled = false;
2799 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2800 			break;
2801 		}
2802 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2803 		fw_health->tmr_multiplier =
2804 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2805 				     bp->current_interval * 10);
2806 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2807 		if (!fw_health->enabled)
2808 			fw_health->last_fw_heartbeat =
2809 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2810 		fw_health->last_fw_reset_cnt =
2811 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2812 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2813 		if (status != BNXT_FW_STATUS_HEALTHY)
2814 			status_desc = "unhealthy";
2815 		netif_info(bp, drv, bp->dev,
2816 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2817 			   fw_health->primary ? "primary" : "backup", status,
2818 			   status_desc, fw_health->last_fw_reset_cnt);
2819 		if (!fw_health->enabled) {
2820 			/* Make sure tmr_counter is set and visible to
2821 			 * bnxt_health_check() before setting enabled to true.
2822 			 */
2823 			smp_wmb();
2824 			fw_health->enabled = true;
2825 		}
2826 		goto async_event_process_exit;
2827 	}
2828 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2829 		netif_notice(bp, hw, bp->dev,
2830 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2831 			     data1, data2);
2832 		goto async_event_process_exit;
2833 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2834 		struct bnxt_rx_ring_info *rxr;
2835 		u16 grp_idx;
2836 
2837 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2838 			goto async_event_process_exit;
2839 
2840 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2841 			    BNXT_EVENT_RING_TYPE(data2), data1);
2842 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2843 			goto async_event_process_exit;
2844 
2845 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2846 		if (grp_idx == INVALID_HW_RING_ID) {
2847 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2848 				    data1);
2849 			goto async_event_process_exit;
2850 		}
2851 		rxr = bp->bnapi[grp_idx]->rx_ring;
2852 		bnxt_sched_reset_rxr(bp, rxr);
2853 		goto async_event_process_exit;
2854 	}
2855 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2856 		struct bnxt_fw_health *fw_health = bp->fw_health;
2857 
2858 		netif_notice(bp, hw, bp->dev,
2859 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2860 			     data1, data2);
2861 		if (fw_health) {
2862 			fw_health->echo_req_data1 = data1;
2863 			fw_health->echo_req_data2 = data2;
2864 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2865 			break;
2866 		}
2867 		goto async_event_process_exit;
2868 	}
2869 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2870 		bnxt_ptp_pps_event(bp, data1, data2);
2871 		goto async_event_process_exit;
2872 	}
2873 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2874 		if (bnxt_event_error_report(bp, data1, data2))
2875 			break;
2876 		goto async_event_process_exit;
2877 	}
2878 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2879 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2880 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2881 			if (BNXT_PTP_USE_RTC(bp)) {
2882 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2883 				unsigned long flags;
2884 				u64 ns;
2885 
2886 				if (!ptp)
2887 					goto async_event_process_exit;
2888 
2889 				bnxt_ptp_update_current_time(bp);
2890 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2891 				       BNXT_PHC_BITS) | ptp->current_time);
2892 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2893 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2894 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2895 			}
2896 			break;
2897 		}
2898 		goto async_event_process_exit;
2899 	}
2900 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2901 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2902 
2903 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2904 		goto async_event_process_exit;
2905 	}
2906 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2907 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2908 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2909 
2910 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2911 		goto async_event_process_exit;
2912 	}
2913 	default:
2914 		goto async_event_process_exit;
2915 	}
2916 	__bnxt_queue_sp_work(bp);
2917 async_event_process_exit:
2918 	bnxt_ulp_async_events(bp, cmpl);
2919 	return 0;
2920 }
2921 
2922 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2923 {
2924 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2925 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2926 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2927 				(struct hwrm_fwd_req_cmpl *)txcmp;
2928 
2929 	switch (cmpl_type) {
2930 	case CMPL_BASE_TYPE_HWRM_DONE:
2931 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2932 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2933 		break;
2934 
2935 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2936 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2937 
2938 		if ((vf_id < bp->pf.first_vf_id) ||
2939 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2940 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2941 				   vf_id);
2942 			return -EINVAL;
2943 		}
2944 
2945 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2946 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2947 		break;
2948 
2949 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2950 		bnxt_async_event_process(bp,
2951 					 (struct hwrm_async_event_cmpl *)txcmp);
2952 		break;
2953 
2954 	default:
2955 		break;
2956 	}
2957 
2958 	return 0;
2959 }
2960 
2961 static bool bnxt_vnic_is_active(struct bnxt *bp)
2962 {
2963 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2964 
2965 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2966 }
2967 
2968 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2969 {
2970 	struct bnxt_napi *bnapi = dev_instance;
2971 	struct bnxt *bp = bnapi->bp;
2972 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2973 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2974 
2975 	cpr->event_ctr++;
2976 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2977 	napi_schedule(&bnapi->napi);
2978 	return IRQ_HANDLED;
2979 }
2980 
2981 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2982 {
2983 	u32 raw_cons = cpr->cp_raw_cons;
2984 	u16 cons = RING_CMP(raw_cons);
2985 	struct tx_cmp *txcmp;
2986 
2987 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2988 
2989 	return TX_CMP_VALID(txcmp, raw_cons);
2990 }
2991 
2992 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2993 			    int budget)
2994 {
2995 	struct bnxt_napi *bnapi = cpr->bnapi;
2996 	u32 raw_cons = cpr->cp_raw_cons;
2997 	bool flush_xdp = false;
2998 	u32 cons;
2999 	int rx_pkts = 0;
3000 	u8 event = 0;
3001 	struct tx_cmp *txcmp;
3002 
3003 	cpr->has_more_work = 0;
3004 	cpr->had_work_done = 1;
3005 	while (1) {
3006 		u8 cmp_type;
3007 		int rc;
3008 
3009 		cons = RING_CMP(raw_cons);
3010 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3011 
3012 		if (!TX_CMP_VALID(txcmp, raw_cons))
3013 			break;
3014 
3015 		/* The valid test of the entry must be done first before
3016 		 * reading any further.
3017 		 */
3018 		dma_rmb();
3019 		cmp_type = TX_CMP_TYPE(txcmp);
3020 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3021 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3022 			u32 opaque = txcmp->tx_cmp_opaque;
3023 			struct bnxt_tx_ring_info *txr;
3024 			u16 tx_freed;
3025 
3026 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3027 			event |= BNXT_TX_CMP_EVENT;
3028 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3029 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3030 			else
3031 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3032 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3033 				   bp->tx_ring_mask;
3034 			/* return full budget so NAPI will complete. */
3035 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3036 				rx_pkts = budget;
3037 				raw_cons = NEXT_RAW_CMP(raw_cons);
3038 				if (budget)
3039 					cpr->has_more_work = 1;
3040 				break;
3041 			}
3042 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3043 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3044 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3045 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3046 			if (likely(budget))
3047 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3048 			else
3049 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3050 							   &event);
3051 			if (event & BNXT_REDIRECT_EVENT)
3052 				flush_xdp = true;
3053 			if (likely(rc >= 0))
3054 				rx_pkts += rc;
3055 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3056 			 * the NAPI budget.  Otherwise, we may potentially loop
3057 			 * here forever if we consistently cannot allocate
3058 			 * buffers.
3059 			 */
3060 			else if (rc == -ENOMEM && budget)
3061 				rx_pkts++;
3062 			else if (rc == -EBUSY)	/* partial completion */
3063 				break;
3064 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3065 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3066 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3067 			bnxt_hwrm_handler(bp, txcmp);
3068 		}
3069 		raw_cons = NEXT_RAW_CMP(raw_cons);
3070 
3071 		if (rx_pkts && rx_pkts == budget) {
3072 			cpr->has_more_work = 1;
3073 			break;
3074 		}
3075 	}
3076 
3077 	if (flush_xdp) {
3078 		xdp_do_flush();
3079 		event &= ~BNXT_REDIRECT_EVENT;
3080 	}
3081 
3082 	if (event & BNXT_TX_EVENT) {
3083 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3084 		u16 prod = txr->tx_prod;
3085 
3086 		/* Sync BD data before updating doorbell */
3087 		wmb();
3088 
3089 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3090 		event &= ~BNXT_TX_EVENT;
3091 	}
3092 
3093 	cpr->cp_raw_cons = raw_cons;
3094 	bnapi->events |= event;
3095 	return rx_pkts;
3096 }
3097 
3098 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3099 				  int budget)
3100 {
3101 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3102 		bnapi->tx_int(bp, bnapi, budget);
3103 
3104 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3105 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3106 
3107 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3108 		bnapi->events &= ~BNXT_RX_EVENT;
3109 	}
3110 	if (bnapi->events & BNXT_AGG_EVENT) {
3111 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3112 
3113 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3114 		bnapi->events &= ~BNXT_AGG_EVENT;
3115 	}
3116 }
3117 
3118 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3119 			  int budget)
3120 {
3121 	struct bnxt_napi *bnapi = cpr->bnapi;
3122 	int rx_pkts;
3123 
3124 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3125 
3126 	/* ACK completion ring before freeing tx ring and producing new
3127 	 * buffers in rx/agg rings to prevent overflowing the completion
3128 	 * ring.
3129 	 */
3130 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3131 
3132 	__bnxt_poll_work_done(bp, bnapi, budget);
3133 	return rx_pkts;
3134 }
3135 
3136 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3137 {
3138 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3139 	struct bnxt *bp = bnapi->bp;
3140 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3141 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3142 	struct tx_cmp *txcmp;
3143 	struct rx_cmp_ext *rxcmp1;
3144 	u32 cp_cons, tmp_raw_cons;
3145 	u32 raw_cons = cpr->cp_raw_cons;
3146 	bool flush_xdp = false;
3147 	u32 rx_pkts = 0;
3148 	u8 event = 0;
3149 
3150 	while (1) {
3151 		int rc;
3152 
3153 		cp_cons = RING_CMP(raw_cons);
3154 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3155 
3156 		if (!TX_CMP_VALID(txcmp, raw_cons))
3157 			break;
3158 
3159 		/* The valid test of the entry must be done first before
3160 		 * reading any further.
3161 		 */
3162 		dma_rmb();
3163 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3164 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3165 			cp_cons = RING_CMP(tmp_raw_cons);
3166 			rxcmp1 = (struct rx_cmp_ext *)
3167 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3168 
3169 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3170 				break;
3171 
3172 			/* force an error to recycle the buffer */
3173 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3174 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3175 
3176 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3177 			if (likely(rc == -EIO) && budget)
3178 				rx_pkts++;
3179 			else if (rc == -EBUSY)	/* partial completion */
3180 				break;
3181 			if (event & BNXT_REDIRECT_EVENT)
3182 				flush_xdp = true;
3183 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3184 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3185 			bnxt_hwrm_handler(bp, txcmp);
3186 		} else {
3187 			netdev_err(bp->dev,
3188 				   "Invalid completion received on special ring\n");
3189 		}
3190 		raw_cons = NEXT_RAW_CMP(raw_cons);
3191 
3192 		if (rx_pkts == budget)
3193 			break;
3194 	}
3195 
3196 	cpr->cp_raw_cons = raw_cons;
3197 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3198 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3199 
3200 	if (event & BNXT_AGG_EVENT)
3201 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3202 	if (flush_xdp)
3203 		xdp_do_flush();
3204 
3205 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3206 		napi_complete_done(napi, rx_pkts);
3207 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3208 	}
3209 	return rx_pkts;
3210 }
3211 
3212 static int bnxt_poll(struct napi_struct *napi, int budget)
3213 {
3214 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3215 	struct bnxt *bp = bnapi->bp;
3216 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3217 	int work_done = 0;
3218 
3219 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3220 		napi_complete(napi);
3221 		return 0;
3222 	}
3223 	while (1) {
3224 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3225 
3226 		if (work_done >= budget) {
3227 			if (!budget)
3228 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3229 			break;
3230 		}
3231 
3232 		if (!bnxt_has_work(bp, cpr)) {
3233 			if (napi_complete_done(napi, work_done))
3234 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3235 			break;
3236 		}
3237 	}
3238 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3239 		struct dim_sample dim_sample = {};
3240 
3241 		dim_update_sample(cpr->event_ctr,
3242 				  cpr->rx_packets,
3243 				  cpr->rx_bytes,
3244 				  &dim_sample);
3245 		net_dim(&cpr->dim, &dim_sample);
3246 	}
3247 	return work_done;
3248 }
3249 
3250 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3251 {
3252 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3253 	int i, work_done = 0;
3254 
3255 	for (i = 0; i < cpr->cp_ring_count; i++) {
3256 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3257 
3258 		if (cpr2->had_nqe_notify) {
3259 			work_done += __bnxt_poll_work(bp, cpr2,
3260 						      budget - work_done);
3261 			cpr->has_more_work |= cpr2->has_more_work;
3262 		}
3263 	}
3264 	return work_done;
3265 }
3266 
3267 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3268 				 u64 dbr_type, int budget)
3269 {
3270 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3271 	int i;
3272 
3273 	for (i = 0; i < cpr->cp_ring_count; i++) {
3274 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3275 		struct bnxt_db_info *db;
3276 
3277 		if (cpr2->had_work_done) {
3278 			u32 tgl = 0;
3279 
3280 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3281 				cpr2->had_nqe_notify = 0;
3282 				tgl = cpr2->toggle;
3283 			}
3284 			db = &cpr2->cp_db;
3285 			bnxt_writeq(bp,
3286 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3287 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3288 				    db->doorbell);
3289 			cpr2->had_work_done = 0;
3290 		}
3291 	}
3292 	__bnxt_poll_work_done(bp, bnapi, budget);
3293 }
3294 
3295 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3296 {
3297 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3298 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3299 	struct bnxt_cp_ring_info *cpr_rx;
3300 	u32 raw_cons = cpr->cp_raw_cons;
3301 	struct bnxt *bp = bnapi->bp;
3302 	struct nqe_cn *nqcmp;
3303 	int work_done = 0;
3304 	u32 cons;
3305 
3306 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3307 		napi_complete(napi);
3308 		return 0;
3309 	}
3310 	if (cpr->has_more_work) {
3311 		cpr->has_more_work = 0;
3312 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3313 	}
3314 	while (1) {
3315 		u16 type;
3316 
3317 		cons = RING_CMP(raw_cons);
3318 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3319 
3320 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3321 			if (cpr->has_more_work)
3322 				break;
3323 
3324 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3325 					     budget);
3326 			cpr->cp_raw_cons = raw_cons;
3327 			if (napi_complete_done(napi, work_done))
3328 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3329 						  cpr->cp_raw_cons);
3330 			goto poll_done;
3331 		}
3332 
3333 		/* The valid test of the entry must be done first before
3334 		 * reading any further.
3335 		 */
3336 		dma_rmb();
3337 
3338 		type = le16_to_cpu(nqcmp->type);
3339 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3340 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3341 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3342 			struct bnxt_cp_ring_info *cpr2;
3343 
3344 			/* No more budget for RX work */
3345 			if (budget && work_done >= budget &&
3346 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3347 				break;
3348 
3349 			idx = BNXT_NQ_HDL_IDX(idx);
3350 			cpr2 = &cpr->cp_ring_arr[idx];
3351 			cpr2->had_nqe_notify = 1;
3352 			cpr2->toggle = NQE_CN_TOGGLE(type);
3353 			work_done += __bnxt_poll_work(bp, cpr2,
3354 						      budget - work_done);
3355 			cpr->has_more_work |= cpr2->has_more_work;
3356 		} else {
3357 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3358 		}
3359 		raw_cons = NEXT_RAW_CMP(raw_cons);
3360 	}
3361 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3362 	if (raw_cons != cpr->cp_raw_cons) {
3363 		cpr->cp_raw_cons = raw_cons;
3364 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3365 	}
3366 poll_done:
3367 	cpr_rx = &cpr->cp_ring_arr[0];
3368 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3369 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3370 		struct dim_sample dim_sample = {};
3371 
3372 		dim_update_sample(cpr->event_ctr,
3373 				  cpr_rx->rx_packets,
3374 				  cpr_rx->rx_bytes,
3375 				  &dim_sample);
3376 		net_dim(&cpr->dim, &dim_sample);
3377 	}
3378 	return work_done;
3379 }
3380 
3381 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3382 				       struct bnxt_tx_ring_info *txr, int idx)
3383 {
3384 	int i, max_idx;
3385 	struct pci_dev *pdev = bp->pdev;
3386 
3387 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3388 
3389 	for (i = 0; i < max_idx;) {
3390 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3391 		struct sk_buff *skb;
3392 		int j, last;
3393 
3394 		if (idx  < bp->tx_nr_rings_xdp &&
3395 		    tx_buf->action == XDP_REDIRECT) {
3396 			dma_unmap_single(&pdev->dev,
3397 					 dma_unmap_addr(tx_buf, mapping),
3398 					 dma_unmap_len(tx_buf, len),
3399 					 DMA_TO_DEVICE);
3400 			xdp_return_frame(tx_buf->xdpf);
3401 			tx_buf->action = 0;
3402 			tx_buf->xdpf = NULL;
3403 			i++;
3404 			continue;
3405 		}
3406 
3407 		skb = tx_buf->skb;
3408 		if (!skb) {
3409 			i++;
3410 			continue;
3411 		}
3412 
3413 		tx_buf->skb = NULL;
3414 
3415 		if (tx_buf->is_push) {
3416 			dev_kfree_skb(skb);
3417 			i += 2;
3418 			continue;
3419 		}
3420 
3421 		dma_unmap_single(&pdev->dev,
3422 				 dma_unmap_addr(tx_buf, mapping),
3423 				 skb_headlen(skb),
3424 				 DMA_TO_DEVICE);
3425 
3426 		last = tx_buf->nr_frags;
3427 		i += 2;
3428 		for (j = 0; j < last; j++, i++) {
3429 			int ring_idx = i & bp->tx_ring_mask;
3430 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3431 
3432 			tx_buf = &txr->tx_buf_ring[ring_idx];
3433 			netmem_dma_unmap_page_attrs(&pdev->dev,
3434 						    dma_unmap_addr(tx_buf,
3435 								   mapping),
3436 						    skb_frag_size(frag),
3437 						    DMA_TO_DEVICE, 0);
3438 		}
3439 		dev_kfree_skb(skb);
3440 	}
3441 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3442 }
3443 
3444 static void bnxt_free_tx_skbs(struct bnxt *bp)
3445 {
3446 	int i;
3447 
3448 	if (!bp->tx_ring)
3449 		return;
3450 
3451 	for (i = 0; i < bp->tx_nr_rings; i++) {
3452 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3453 
3454 		if (!txr->tx_buf_ring)
3455 			continue;
3456 
3457 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3458 	}
3459 
3460 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3461 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3462 }
3463 
3464 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3465 {
3466 	int i, max_idx;
3467 
3468 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3469 
3470 	for (i = 0; i < max_idx; i++) {
3471 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3472 		void *data = rx_buf->data;
3473 
3474 		if (!data)
3475 			continue;
3476 
3477 		rx_buf->data = NULL;
3478 		if (BNXT_RX_PAGE_MODE(bp))
3479 			page_pool_recycle_direct(rxr->page_pool, data);
3480 		else
3481 			page_pool_free_va(rxr->head_pool, data, true);
3482 	}
3483 }
3484 
3485 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3486 {
3487 	int i, max_idx;
3488 
3489 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3490 
3491 	for (i = 0; i < max_idx; i++) {
3492 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3493 		netmem_ref netmem = rx_agg_buf->netmem;
3494 
3495 		if (!netmem)
3496 			continue;
3497 
3498 		rx_agg_buf->netmem = 0;
3499 		__clear_bit(i, rxr->rx_agg_bmap);
3500 
3501 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3502 	}
3503 }
3504 
3505 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3506 					struct bnxt_rx_ring_info *rxr)
3507 {
3508 	int i;
3509 
3510 	for (i = 0; i < bp->max_tpa; i++) {
3511 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3512 		u8 *data = tpa_info->data;
3513 
3514 		if (!data)
3515 			continue;
3516 
3517 		tpa_info->data = NULL;
3518 		page_pool_free_va(rxr->head_pool, data, false);
3519 	}
3520 }
3521 
3522 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3523 				       struct bnxt_rx_ring_info *rxr)
3524 {
3525 	struct bnxt_tpa_idx_map *map;
3526 
3527 	if (!rxr->rx_tpa)
3528 		goto skip_rx_tpa_free;
3529 
3530 	bnxt_free_one_tpa_info_data(bp, rxr);
3531 
3532 skip_rx_tpa_free:
3533 	if (!rxr->rx_buf_ring)
3534 		goto skip_rx_buf_free;
3535 
3536 	bnxt_free_one_rx_ring(bp, rxr);
3537 
3538 skip_rx_buf_free:
3539 	if (!rxr->rx_agg_ring)
3540 		goto skip_rx_agg_free;
3541 
3542 	bnxt_free_one_rx_agg_ring(bp, rxr);
3543 
3544 skip_rx_agg_free:
3545 	map = rxr->rx_tpa_idx_map;
3546 	if (map)
3547 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3548 }
3549 
3550 static void bnxt_free_rx_skbs(struct bnxt *bp)
3551 {
3552 	int i;
3553 
3554 	if (!bp->rx_ring)
3555 		return;
3556 
3557 	for (i = 0; i < bp->rx_nr_rings; i++)
3558 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3559 }
3560 
3561 static void bnxt_free_skbs(struct bnxt *bp)
3562 {
3563 	bnxt_free_tx_skbs(bp);
3564 	bnxt_free_rx_skbs(bp);
3565 }
3566 
3567 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3568 {
3569 	u8 init_val = ctxm->init_value;
3570 	u16 offset = ctxm->init_offset;
3571 	u8 *p2 = p;
3572 	int i;
3573 
3574 	if (!init_val)
3575 		return;
3576 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3577 		memset(p, init_val, len);
3578 		return;
3579 	}
3580 	for (i = 0; i < len; i += ctxm->entry_size)
3581 		*(p2 + i + offset) = init_val;
3582 }
3583 
3584 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3585 			       void *buf, size_t offset, size_t head,
3586 			       size_t tail)
3587 {
3588 	int i, head_page, start_idx, source_offset;
3589 	size_t len, rem_len, total_len, max_bytes;
3590 
3591 	head_page = head / rmem->page_size;
3592 	source_offset = head % rmem->page_size;
3593 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3594 	if (!total_len)
3595 		total_len = MAX_CTX_BYTES;
3596 	start_idx = head_page % MAX_CTX_PAGES;
3597 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3598 		    source_offset;
3599 	total_len = min(total_len, max_bytes);
3600 	rem_len = total_len;
3601 
3602 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3603 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3604 		if (buf)
3605 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3606 			       len);
3607 		offset += len;
3608 		rem_len -= len;
3609 	}
3610 	return total_len;
3611 }
3612 
3613 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3614 {
3615 	struct pci_dev *pdev = bp->pdev;
3616 	int i;
3617 
3618 	if (!rmem->pg_arr)
3619 		goto skip_pages;
3620 
3621 	for (i = 0; i < rmem->nr_pages; i++) {
3622 		if (!rmem->pg_arr[i])
3623 			continue;
3624 
3625 		dma_free_coherent(&pdev->dev, rmem->page_size,
3626 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3627 
3628 		rmem->pg_arr[i] = NULL;
3629 	}
3630 skip_pages:
3631 	if (rmem->pg_tbl) {
3632 		size_t pg_tbl_size = rmem->nr_pages * 8;
3633 
3634 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3635 			pg_tbl_size = rmem->page_size;
3636 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3637 				  rmem->pg_tbl, rmem->pg_tbl_map);
3638 		rmem->pg_tbl = NULL;
3639 	}
3640 	if (rmem->vmem_size && *rmem->vmem) {
3641 		vfree(*rmem->vmem);
3642 		*rmem->vmem = NULL;
3643 	}
3644 }
3645 
3646 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3647 {
3648 	struct pci_dev *pdev = bp->pdev;
3649 	u64 valid_bit = 0;
3650 	int i;
3651 
3652 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3653 		valid_bit = PTU_PTE_VALID;
3654 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3655 		size_t pg_tbl_size = rmem->nr_pages * 8;
3656 
3657 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3658 			pg_tbl_size = rmem->page_size;
3659 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3660 						  &rmem->pg_tbl_map,
3661 						  GFP_KERNEL);
3662 		if (!rmem->pg_tbl)
3663 			return -ENOMEM;
3664 	}
3665 
3666 	for (i = 0; i < rmem->nr_pages; i++) {
3667 		u64 extra_bits = valid_bit;
3668 
3669 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3670 						     rmem->page_size,
3671 						     &rmem->dma_arr[i],
3672 						     GFP_KERNEL);
3673 		if (!rmem->pg_arr[i])
3674 			return -ENOMEM;
3675 
3676 		if (rmem->ctx_mem)
3677 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3678 					  rmem->page_size);
3679 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3680 			if (i == rmem->nr_pages - 2 &&
3681 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3682 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3683 			else if (i == rmem->nr_pages - 1 &&
3684 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3685 				extra_bits |= PTU_PTE_LAST;
3686 			rmem->pg_tbl[i] =
3687 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3688 		}
3689 	}
3690 
3691 	if (rmem->vmem_size) {
3692 		*rmem->vmem = vzalloc(rmem->vmem_size);
3693 		if (!(*rmem->vmem))
3694 			return -ENOMEM;
3695 	}
3696 	return 0;
3697 }
3698 
3699 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3700 				   struct bnxt_rx_ring_info *rxr)
3701 {
3702 	int i;
3703 
3704 	kfree(rxr->rx_tpa_idx_map);
3705 	rxr->rx_tpa_idx_map = NULL;
3706 	if (rxr->rx_tpa) {
3707 		for (i = 0; i < bp->max_tpa; i++) {
3708 			kfree(rxr->rx_tpa[i].agg_arr);
3709 			rxr->rx_tpa[i].agg_arr = NULL;
3710 		}
3711 	}
3712 	kfree(rxr->rx_tpa);
3713 	rxr->rx_tpa = NULL;
3714 }
3715 
3716 static void bnxt_free_tpa_info(struct bnxt *bp)
3717 {
3718 	int i;
3719 
3720 	for (i = 0; i < bp->rx_nr_rings; i++) {
3721 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3722 
3723 		bnxt_free_one_tpa_info(bp, rxr);
3724 	}
3725 }
3726 
3727 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3728 				   struct bnxt_rx_ring_info *rxr)
3729 {
3730 	struct rx_agg_cmp *agg;
3731 	int i;
3732 
3733 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3734 			      GFP_KERNEL);
3735 	if (!rxr->rx_tpa)
3736 		return -ENOMEM;
3737 
3738 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3739 		return 0;
3740 	for (i = 0; i < bp->max_tpa; i++) {
3741 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3742 		if (!agg)
3743 			return -ENOMEM;
3744 		rxr->rx_tpa[i].agg_arr = agg;
3745 	}
3746 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3747 				      GFP_KERNEL);
3748 	if (!rxr->rx_tpa_idx_map)
3749 		return -ENOMEM;
3750 
3751 	return 0;
3752 }
3753 
3754 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3755 {
3756 	int i, rc;
3757 
3758 	bp->max_tpa = MAX_TPA;
3759 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3760 		if (!bp->max_tpa_v2)
3761 			return 0;
3762 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3763 	}
3764 
3765 	for (i = 0; i < bp->rx_nr_rings; i++) {
3766 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3767 
3768 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3769 		if (rc)
3770 			return rc;
3771 	}
3772 	return 0;
3773 }
3774 
3775 static void bnxt_free_rx_rings(struct bnxt *bp)
3776 {
3777 	int i;
3778 
3779 	if (!bp->rx_ring)
3780 		return;
3781 
3782 	bnxt_free_tpa_info(bp);
3783 	for (i = 0; i < bp->rx_nr_rings; i++) {
3784 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3785 		struct bnxt_ring_struct *ring;
3786 
3787 		if (rxr->xdp_prog)
3788 			bpf_prog_put(rxr->xdp_prog);
3789 
3790 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3791 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3792 
3793 		page_pool_destroy(rxr->page_pool);
3794 		if (bnxt_separate_head_pool(rxr))
3795 			page_pool_destroy(rxr->head_pool);
3796 		rxr->page_pool = rxr->head_pool = NULL;
3797 
3798 		kfree(rxr->rx_agg_bmap);
3799 		rxr->rx_agg_bmap = NULL;
3800 
3801 		ring = &rxr->rx_ring_struct;
3802 		bnxt_free_ring(bp, &ring->ring_mem);
3803 
3804 		ring = &rxr->rx_agg_ring_struct;
3805 		bnxt_free_ring(bp, &ring->ring_mem);
3806 	}
3807 }
3808 
3809 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3810 				   struct bnxt_rx_ring_info *rxr,
3811 				   int numa_node)
3812 {
3813 	struct page_pool_params pp = { 0 };
3814 	struct page_pool *pool;
3815 
3816 	pp.pool_size = bp->rx_agg_ring_size;
3817 	if (BNXT_RX_PAGE_MODE(bp))
3818 		pp.pool_size += bp->rx_ring_size;
3819 	pp.nid = numa_node;
3820 	pp.napi = &rxr->bnapi->napi;
3821 	pp.netdev = bp->dev;
3822 	pp.dev = &bp->pdev->dev;
3823 	pp.dma_dir = bp->rx_dir;
3824 	pp.max_len = PAGE_SIZE;
3825 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3826 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3827 	pp.queue_idx = rxr->bnapi->index;
3828 
3829 	pool = page_pool_create(&pp);
3830 	if (IS_ERR(pool))
3831 		return PTR_ERR(pool);
3832 	rxr->page_pool = pool;
3833 
3834 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3835 	if (bnxt_separate_head_pool(rxr)) {
3836 		pp.pool_size = max(bp->rx_ring_size, 1024);
3837 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3838 		pool = page_pool_create(&pp);
3839 		if (IS_ERR(pool))
3840 			goto err_destroy_pp;
3841 	}
3842 	rxr->head_pool = pool;
3843 
3844 	return 0;
3845 
3846 err_destroy_pp:
3847 	page_pool_destroy(rxr->page_pool);
3848 	rxr->page_pool = NULL;
3849 	return PTR_ERR(pool);
3850 }
3851 
3852 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3853 {
3854 	u16 mem_size;
3855 
3856 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3857 	mem_size = rxr->rx_agg_bmap_size / 8;
3858 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3859 	if (!rxr->rx_agg_bmap)
3860 		return -ENOMEM;
3861 
3862 	return 0;
3863 }
3864 
3865 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3866 {
3867 	int numa_node = dev_to_node(&bp->pdev->dev);
3868 	int i, rc = 0, agg_rings = 0, cpu;
3869 
3870 	if (!bp->rx_ring)
3871 		return -ENOMEM;
3872 
3873 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3874 		agg_rings = 1;
3875 
3876 	for (i = 0; i < bp->rx_nr_rings; i++) {
3877 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3878 		struct bnxt_ring_struct *ring;
3879 		int cpu_node;
3880 
3881 		ring = &rxr->rx_ring_struct;
3882 
3883 		cpu = cpumask_local_spread(i, numa_node);
3884 		cpu_node = cpu_to_node(cpu);
3885 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3886 			   i, cpu_node);
3887 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3888 		if (rc)
3889 			return rc;
3890 
3891 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3892 		if (rc < 0)
3893 			return rc;
3894 
3895 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3896 						MEM_TYPE_PAGE_POOL,
3897 						rxr->page_pool);
3898 		if (rc) {
3899 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3900 			return rc;
3901 		}
3902 
3903 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3904 		if (rc)
3905 			return rc;
3906 
3907 		ring->grp_idx = i;
3908 		if (agg_rings) {
3909 			ring = &rxr->rx_agg_ring_struct;
3910 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3911 			if (rc)
3912 				return rc;
3913 
3914 			ring->grp_idx = i;
3915 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3916 			if (rc)
3917 				return rc;
3918 		}
3919 	}
3920 	if (bp->flags & BNXT_FLAG_TPA)
3921 		rc = bnxt_alloc_tpa_info(bp);
3922 	return rc;
3923 }
3924 
3925 static void bnxt_free_tx_rings(struct bnxt *bp)
3926 {
3927 	int i;
3928 	struct pci_dev *pdev = bp->pdev;
3929 
3930 	if (!bp->tx_ring)
3931 		return;
3932 
3933 	for (i = 0; i < bp->tx_nr_rings; i++) {
3934 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3935 		struct bnxt_ring_struct *ring;
3936 
3937 		if (txr->tx_push) {
3938 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3939 					  txr->tx_push, txr->tx_push_mapping);
3940 			txr->tx_push = NULL;
3941 		}
3942 
3943 		ring = &txr->tx_ring_struct;
3944 
3945 		bnxt_free_ring(bp, &ring->ring_mem);
3946 	}
3947 }
3948 
3949 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3950 	((tc) * (bp)->tx_nr_rings_per_tc)
3951 
3952 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3953 	((tx) % (bp)->tx_nr_rings_per_tc)
3954 
3955 #define BNXT_RING_TO_TC(bp, tx)		\
3956 	((tx) / (bp)->tx_nr_rings_per_tc)
3957 
3958 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3959 {
3960 	int i, j, rc;
3961 	struct pci_dev *pdev = bp->pdev;
3962 
3963 	bp->tx_push_size = 0;
3964 	if (bp->tx_push_thresh) {
3965 		int push_size;
3966 
3967 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3968 					bp->tx_push_thresh);
3969 
3970 		if (push_size > 256) {
3971 			push_size = 0;
3972 			bp->tx_push_thresh = 0;
3973 		}
3974 
3975 		bp->tx_push_size = push_size;
3976 	}
3977 
3978 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3979 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3980 		struct bnxt_ring_struct *ring;
3981 		u8 qidx;
3982 
3983 		ring = &txr->tx_ring_struct;
3984 
3985 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3986 		if (rc)
3987 			return rc;
3988 
3989 		ring->grp_idx = txr->bnapi->index;
3990 		if (bp->tx_push_size) {
3991 			dma_addr_t mapping;
3992 
3993 			/* One pre-allocated DMA buffer to backup
3994 			 * TX push operation
3995 			 */
3996 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3997 						bp->tx_push_size,
3998 						&txr->tx_push_mapping,
3999 						GFP_KERNEL);
4000 
4001 			if (!txr->tx_push)
4002 				return -ENOMEM;
4003 
4004 			mapping = txr->tx_push_mapping +
4005 				sizeof(struct tx_push_bd);
4006 			txr->data_mapping = cpu_to_le64(mapping);
4007 		}
4008 		qidx = bp->tc_to_qidx[j];
4009 		ring->queue_id = bp->q_info[qidx].queue_id;
4010 		spin_lock_init(&txr->xdp_tx_lock);
4011 		if (i < bp->tx_nr_rings_xdp)
4012 			continue;
4013 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4014 			j++;
4015 	}
4016 	return 0;
4017 }
4018 
4019 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4020 {
4021 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4022 
4023 	kfree(cpr->cp_desc_ring);
4024 	cpr->cp_desc_ring = NULL;
4025 	ring->ring_mem.pg_arr = NULL;
4026 	kfree(cpr->cp_desc_mapping);
4027 	cpr->cp_desc_mapping = NULL;
4028 	ring->ring_mem.dma_arr = NULL;
4029 }
4030 
4031 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4032 {
4033 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
4034 	if (!cpr->cp_desc_ring)
4035 		return -ENOMEM;
4036 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
4037 				       GFP_KERNEL);
4038 	if (!cpr->cp_desc_mapping)
4039 		return -ENOMEM;
4040 	return 0;
4041 }
4042 
4043 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4044 {
4045 	int i;
4046 
4047 	if (!bp->bnapi)
4048 		return;
4049 	for (i = 0; i < bp->cp_nr_rings; i++) {
4050 		struct bnxt_napi *bnapi = bp->bnapi[i];
4051 
4052 		if (!bnapi)
4053 			continue;
4054 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4055 	}
4056 }
4057 
4058 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4059 {
4060 	int i, n = bp->cp_nr_pages;
4061 
4062 	for (i = 0; i < bp->cp_nr_rings; i++) {
4063 		struct bnxt_napi *bnapi = bp->bnapi[i];
4064 		int rc;
4065 
4066 		if (!bnapi)
4067 			continue;
4068 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4069 		if (rc)
4070 			return rc;
4071 	}
4072 	return 0;
4073 }
4074 
4075 static void bnxt_free_cp_rings(struct bnxt *bp)
4076 {
4077 	int i;
4078 
4079 	if (!bp->bnapi)
4080 		return;
4081 
4082 	for (i = 0; i < bp->cp_nr_rings; i++) {
4083 		struct bnxt_napi *bnapi = bp->bnapi[i];
4084 		struct bnxt_cp_ring_info *cpr;
4085 		struct bnxt_ring_struct *ring;
4086 		int j;
4087 
4088 		if (!bnapi)
4089 			continue;
4090 
4091 		cpr = &bnapi->cp_ring;
4092 		ring = &cpr->cp_ring_struct;
4093 
4094 		bnxt_free_ring(bp, &ring->ring_mem);
4095 
4096 		if (!cpr->cp_ring_arr)
4097 			continue;
4098 
4099 		for (j = 0; j < cpr->cp_ring_count; j++) {
4100 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4101 
4102 			ring = &cpr2->cp_ring_struct;
4103 			bnxt_free_ring(bp, &ring->ring_mem);
4104 			bnxt_free_cp_arrays(cpr2);
4105 		}
4106 		kfree(cpr->cp_ring_arr);
4107 		cpr->cp_ring_arr = NULL;
4108 		cpr->cp_ring_count = 0;
4109 	}
4110 }
4111 
4112 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4113 				  struct bnxt_cp_ring_info *cpr)
4114 {
4115 	struct bnxt_ring_mem_info *rmem;
4116 	struct bnxt_ring_struct *ring;
4117 	int rc;
4118 
4119 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4120 	if (rc) {
4121 		bnxt_free_cp_arrays(cpr);
4122 		return -ENOMEM;
4123 	}
4124 	ring = &cpr->cp_ring_struct;
4125 	rmem = &ring->ring_mem;
4126 	rmem->nr_pages = bp->cp_nr_pages;
4127 	rmem->page_size = HW_CMPD_RING_SIZE;
4128 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4129 	rmem->dma_arr = cpr->cp_desc_mapping;
4130 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4131 	rc = bnxt_alloc_ring(bp, rmem);
4132 	if (rc) {
4133 		bnxt_free_ring(bp, rmem);
4134 		bnxt_free_cp_arrays(cpr);
4135 	}
4136 	return rc;
4137 }
4138 
4139 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4140 {
4141 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4142 	int i, j, rc, ulp_msix;
4143 	int tcs = bp->num_tc;
4144 
4145 	if (!tcs)
4146 		tcs = 1;
4147 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4148 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4149 		struct bnxt_napi *bnapi = bp->bnapi[i];
4150 		struct bnxt_cp_ring_info *cpr, *cpr2;
4151 		struct bnxt_ring_struct *ring;
4152 		int cp_count = 0, k;
4153 		int rx = 0, tx = 0;
4154 
4155 		if (!bnapi)
4156 			continue;
4157 
4158 		cpr = &bnapi->cp_ring;
4159 		cpr->bnapi = bnapi;
4160 		ring = &cpr->cp_ring_struct;
4161 
4162 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4163 		if (rc)
4164 			return rc;
4165 
4166 		ring->map_idx = ulp_msix + i;
4167 
4168 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4169 			continue;
4170 
4171 		if (i < bp->rx_nr_rings) {
4172 			cp_count++;
4173 			rx = 1;
4174 		}
4175 		if (i < bp->tx_nr_rings_xdp) {
4176 			cp_count++;
4177 			tx = 1;
4178 		} else if ((sh && i < bp->tx_nr_rings) ||
4179 			 (!sh && i >= bp->rx_nr_rings)) {
4180 			cp_count += tcs;
4181 			tx = 1;
4182 		}
4183 
4184 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4185 					   GFP_KERNEL);
4186 		if (!cpr->cp_ring_arr)
4187 			return -ENOMEM;
4188 		cpr->cp_ring_count = cp_count;
4189 
4190 		for (k = 0; k < cp_count; k++) {
4191 			cpr2 = &cpr->cp_ring_arr[k];
4192 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4193 			if (rc)
4194 				return rc;
4195 			cpr2->bnapi = bnapi;
4196 			cpr2->sw_stats = cpr->sw_stats;
4197 			cpr2->cp_idx = k;
4198 			if (!k && rx) {
4199 				bp->rx_ring[i].rx_cpr = cpr2;
4200 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4201 			} else {
4202 				int n, tc = k - rx;
4203 
4204 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4205 				bp->tx_ring[n].tx_cpr = cpr2;
4206 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4207 			}
4208 		}
4209 		if (tx)
4210 			j++;
4211 	}
4212 	return 0;
4213 }
4214 
4215 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4216 				     struct bnxt_rx_ring_info *rxr)
4217 {
4218 	struct bnxt_ring_mem_info *rmem;
4219 	struct bnxt_ring_struct *ring;
4220 
4221 	ring = &rxr->rx_ring_struct;
4222 	rmem = &ring->ring_mem;
4223 	rmem->nr_pages = bp->rx_nr_pages;
4224 	rmem->page_size = HW_RXBD_RING_SIZE;
4225 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4226 	rmem->dma_arr = rxr->rx_desc_mapping;
4227 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4228 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4229 
4230 	ring = &rxr->rx_agg_ring_struct;
4231 	rmem = &ring->ring_mem;
4232 	rmem->nr_pages = bp->rx_agg_nr_pages;
4233 	rmem->page_size = HW_RXBD_RING_SIZE;
4234 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4235 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4236 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4237 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4238 }
4239 
4240 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4241 				      struct bnxt_rx_ring_info *rxr)
4242 {
4243 	struct bnxt_ring_mem_info *rmem;
4244 	struct bnxt_ring_struct *ring;
4245 	int i;
4246 
4247 	rxr->page_pool->p.napi = NULL;
4248 	rxr->page_pool = NULL;
4249 	rxr->head_pool->p.napi = NULL;
4250 	rxr->head_pool = NULL;
4251 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4252 
4253 	ring = &rxr->rx_ring_struct;
4254 	rmem = &ring->ring_mem;
4255 	rmem->pg_tbl = NULL;
4256 	rmem->pg_tbl_map = 0;
4257 	for (i = 0; i < rmem->nr_pages; i++) {
4258 		rmem->pg_arr[i] = NULL;
4259 		rmem->dma_arr[i] = 0;
4260 	}
4261 	*rmem->vmem = NULL;
4262 
4263 	ring = &rxr->rx_agg_ring_struct;
4264 	rmem = &ring->ring_mem;
4265 	rmem->pg_tbl = NULL;
4266 	rmem->pg_tbl_map = 0;
4267 	for (i = 0; i < rmem->nr_pages; i++) {
4268 		rmem->pg_arr[i] = NULL;
4269 		rmem->dma_arr[i] = 0;
4270 	}
4271 	*rmem->vmem = NULL;
4272 }
4273 
4274 static void bnxt_init_ring_struct(struct bnxt *bp)
4275 {
4276 	int i, j;
4277 
4278 	for (i = 0; i < bp->cp_nr_rings; i++) {
4279 		struct bnxt_napi *bnapi = bp->bnapi[i];
4280 		struct bnxt_ring_mem_info *rmem;
4281 		struct bnxt_cp_ring_info *cpr;
4282 		struct bnxt_rx_ring_info *rxr;
4283 		struct bnxt_tx_ring_info *txr;
4284 		struct bnxt_ring_struct *ring;
4285 
4286 		if (!bnapi)
4287 			continue;
4288 
4289 		cpr = &bnapi->cp_ring;
4290 		ring = &cpr->cp_ring_struct;
4291 		rmem = &ring->ring_mem;
4292 		rmem->nr_pages = bp->cp_nr_pages;
4293 		rmem->page_size = HW_CMPD_RING_SIZE;
4294 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4295 		rmem->dma_arr = cpr->cp_desc_mapping;
4296 		rmem->vmem_size = 0;
4297 
4298 		rxr = bnapi->rx_ring;
4299 		if (!rxr)
4300 			goto skip_rx;
4301 
4302 		ring = &rxr->rx_ring_struct;
4303 		rmem = &ring->ring_mem;
4304 		rmem->nr_pages = bp->rx_nr_pages;
4305 		rmem->page_size = HW_RXBD_RING_SIZE;
4306 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4307 		rmem->dma_arr = rxr->rx_desc_mapping;
4308 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4309 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4310 
4311 		ring = &rxr->rx_agg_ring_struct;
4312 		rmem = &ring->ring_mem;
4313 		rmem->nr_pages = bp->rx_agg_nr_pages;
4314 		rmem->page_size = HW_RXBD_RING_SIZE;
4315 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4316 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4317 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4318 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4319 
4320 skip_rx:
4321 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4322 			ring = &txr->tx_ring_struct;
4323 			rmem = &ring->ring_mem;
4324 			rmem->nr_pages = bp->tx_nr_pages;
4325 			rmem->page_size = HW_TXBD_RING_SIZE;
4326 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4327 			rmem->dma_arr = txr->tx_desc_mapping;
4328 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4329 			rmem->vmem = (void **)&txr->tx_buf_ring;
4330 		}
4331 	}
4332 }
4333 
4334 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4335 {
4336 	int i;
4337 	u32 prod;
4338 	struct rx_bd **rx_buf_ring;
4339 
4340 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4341 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4342 		int j;
4343 		struct rx_bd *rxbd;
4344 
4345 		rxbd = rx_buf_ring[i];
4346 		if (!rxbd)
4347 			continue;
4348 
4349 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4350 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4351 			rxbd->rx_bd_opaque = prod;
4352 		}
4353 	}
4354 }
4355 
4356 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4357 				       struct bnxt_rx_ring_info *rxr,
4358 				       int ring_nr)
4359 {
4360 	u32 prod;
4361 	int i;
4362 
4363 	prod = rxr->rx_prod;
4364 	for (i = 0; i < bp->rx_ring_size; i++) {
4365 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4366 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4367 				    ring_nr, i, bp->rx_ring_size);
4368 			break;
4369 		}
4370 		prod = NEXT_RX(prod);
4371 	}
4372 	rxr->rx_prod = prod;
4373 }
4374 
4375 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4376 					  struct bnxt_rx_ring_info *rxr,
4377 					  int ring_nr)
4378 {
4379 	u32 prod;
4380 	int i;
4381 
4382 	prod = rxr->rx_agg_prod;
4383 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4384 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4385 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4386 				    ring_nr, i, bp->rx_ring_size);
4387 			break;
4388 		}
4389 		prod = NEXT_RX_AGG(prod);
4390 	}
4391 	rxr->rx_agg_prod = prod;
4392 }
4393 
4394 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4395 					struct bnxt_rx_ring_info *rxr)
4396 {
4397 	dma_addr_t mapping;
4398 	u8 *data;
4399 	int i;
4400 
4401 	for (i = 0; i < bp->max_tpa; i++) {
4402 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4403 					    GFP_KERNEL);
4404 		if (!data)
4405 			return -ENOMEM;
4406 
4407 		rxr->rx_tpa[i].data = data;
4408 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4409 		rxr->rx_tpa[i].mapping = mapping;
4410 	}
4411 
4412 	return 0;
4413 }
4414 
4415 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4416 {
4417 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4418 	int rc;
4419 
4420 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4421 
4422 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4423 		return 0;
4424 
4425 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4426 
4427 	if (rxr->rx_tpa) {
4428 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4429 		if (rc)
4430 			return rc;
4431 	}
4432 	return 0;
4433 }
4434 
4435 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4436 				       struct bnxt_rx_ring_info *rxr)
4437 {
4438 	struct bnxt_ring_struct *ring;
4439 	u32 type;
4440 
4441 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4442 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4443 
4444 	if (NET_IP_ALIGN == 2)
4445 		type |= RX_BD_FLAGS_SOP;
4446 
4447 	ring = &rxr->rx_ring_struct;
4448 	bnxt_init_rxbd_pages(ring, type);
4449 	ring->fw_ring_id = INVALID_HW_RING_ID;
4450 }
4451 
4452 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4453 					   struct bnxt_rx_ring_info *rxr)
4454 {
4455 	struct bnxt_ring_struct *ring;
4456 	u32 type;
4457 
4458 	ring = &rxr->rx_agg_ring_struct;
4459 	ring->fw_ring_id = INVALID_HW_RING_ID;
4460 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4461 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4462 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4463 
4464 		bnxt_init_rxbd_pages(ring, type);
4465 	}
4466 }
4467 
4468 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4469 {
4470 	struct bnxt_rx_ring_info *rxr;
4471 
4472 	rxr = &bp->rx_ring[ring_nr];
4473 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4474 
4475 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4476 			     &rxr->bnapi->napi);
4477 
4478 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4479 		bpf_prog_add(bp->xdp_prog, 1);
4480 		rxr->xdp_prog = bp->xdp_prog;
4481 	}
4482 
4483 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4484 
4485 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4486 }
4487 
4488 static void bnxt_init_cp_rings(struct bnxt *bp)
4489 {
4490 	int i, j;
4491 
4492 	for (i = 0; i < bp->cp_nr_rings; i++) {
4493 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4494 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4495 
4496 		ring->fw_ring_id = INVALID_HW_RING_ID;
4497 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4498 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4499 		if (!cpr->cp_ring_arr)
4500 			continue;
4501 		for (j = 0; j < cpr->cp_ring_count; j++) {
4502 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4503 
4504 			ring = &cpr2->cp_ring_struct;
4505 			ring->fw_ring_id = INVALID_HW_RING_ID;
4506 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4507 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4508 		}
4509 	}
4510 }
4511 
4512 static int bnxt_init_rx_rings(struct bnxt *bp)
4513 {
4514 	int i, rc = 0;
4515 
4516 	if (BNXT_RX_PAGE_MODE(bp)) {
4517 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4518 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4519 	} else {
4520 		bp->rx_offset = BNXT_RX_OFFSET;
4521 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4522 	}
4523 
4524 	for (i = 0; i < bp->rx_nr_rings; i++) {
4525 		rc = bnxt_init_one_rx_ring(bp, i);
4526 		if (rc)
4527 			break;
4528 	}
4529 
4530 	return rc;
4531 }
4532 
4533 static int bnxt_init_tx_rings(struct bnxt *bp)
4534 {
4535 	u16 i;
4536 
4537 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4538 				   BNXT_MIN_TX_DESC_CNT);
4539 
4540 	for (i = 0; i < bp->tx_nr_rings; i++) {
4541 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4542 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4543 
4544 		ring->fw_ring_id = INVALID_HW_RING_ID;
4545 
4546 		if (i >= bp->tx_nr_rings_xdp)
4547 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4548 					     NETDEV_QUEUE_TYPE_TX,
4549 					     &txr->bnapi->napi);
4550 	}
4551 
4552 	return 0;
4553 }
4554 
4555 static void bnxt_free_ring_grps(struct bnxt *bp)
4556 {
4557 	kfree(bp->grp_info);
4558 	bp->grp_info = NULL;
4559 }
4560 
4561 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4562 {
4563 	int i;
4564 
4565 	if (irq_re_init) {
4566 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4567 				       sizeof(struct bnxt_ring_grp_info),
4568 				       GFP_KERNEL);
4569 		if (!bp->grp_info)
4570 			return -ENOMEM;
4571 	}
4572 	for (i = 0; i < bp->cp_nr_rings; i++) {
4573 		if (irq_re_init)
4574 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4575 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4576 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4577 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4578 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4579 	}
4580 	return 0;
4581 }
4582 
4583 static void bnxt_free_vnics(struct bnxt *bp)
4584 {
4585 	kfree(bp->vnic_info);
4586 	bp->vnic_info = NULL;
4587 	bp->nr_vnics = 0;
4588 }
4589 
4590 static int bnxt_alloc_vnics(struct bnxt *bp)
4591 {
4592 	int num_vnics = 1;
4593 
4594 #ifdef CONFIG_RFS_ACCEL
4595 	if (bp->flags & BNXT_FLAG_RFS) {
4596 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4597 			num_vnics++;
4598 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4599 			num_vnics += bp->rx_nr_rings;
4600 	}
4601 #endif
4602 
4603 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4604 		num_vnics++;
4605 
4606 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4607 				GFP_KERNEL);
4608 	if (!bp->vnic_info)
4609 		return -ENOMEM;
4610 
4611 	bp->nr_vnics = num_vnics;
4612 	return 0;
4613 }
4614 
4615 static void bnxt_init_vnics(struct bnxt *bp)
4616 {
4617 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4618 	int i;
4619 
4620 	for (i = 0; i < bp->nr_vnics; i++) {
4621 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4622 		int j;
4623 
4624 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4625 		vnic->vnic_id = i;
4626 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4627 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4628 
4629 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4630 
4631 		if (bp->vnic_info[i].rss_hash_key) {
4632 			if (i == BNXT_VNIC_DEFAULT) {
4633 				u8 *key = (void *)vnic->rss_hash_key;
4634 				int k;
4635 
4636 				if (!bp->rss_hash_key_valid &&
4637 				    !bp->rss_hash_key_updated) {
4638 					get_random_bytes(bp->rss_hash_key,
4639 							 HW_HASH_KEY_SIZE);
4640 					bp->rss_hash_key_updated = true;
4641 				}
4642 
4643 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4644 				       HW_HASH_KEY_SIZE);
4645 
4646 				if (!bp->rss_hash_key_updated)
4647 					continue;
4648 
4649 				bp->rss_hash_key_updated = false;
4650 				bp->rss_hash_key_valid = true;
4651 
4652 				bp->toeplitz_prefix = 0;
4653 				for (k = 0; k < 8; k++) {
4654 					bp->toeplitz_prefix <<= 8;
4655 					bp->toeplitz_prefix |= key[k];
4656 				}
4657 			} else {
4658 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4659 				       HW_HASH_KEY_SIZE);
4660 			}
4661 		}
4662 	}
4663 }
4664 
4665 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4666 {
4667 	int pages;
4668 
4669 	pages = ring_size / desc_per_pg;
4670 
4671 	if (!pages)
4672 		return 1;
4673 
4674 	pages++;
4675 
4676 	while (pages & (pages - 1))
4677 		pages++;
4678 
4679 	return pages;
4680 }
4681 
4682 void bnxt_set_tpa_flags(struct bnxt *bp)
4683 {
4684 	bp->flags &= ~BNXT_FLAG_TPA;
4685 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4686 		return;
4687 	if (bp->dev->features & NETIF_F_LRO)
4688 		bp->flags |= BNXT_FLAG_LRO;
4689 	else if (bp->dev->features & NETIF_F_GRO_HW)
4690 		bp->flags |= BNXT_FLAG_GRO;
4691 }
4692 
4693 static void bnxt_init_ring_params(struct bnxt *bp)
4694 {
4695 	unsigned int rx_size;
4696 
4697 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4698 	/* Try to fit 4 chunks into a 4k page */
4699 	rx_size = SZ_1K -
4700 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4701 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4702 }
4703 
4704 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4705  * be set on entry.
4706  */
4707 void bnxt_set_ring_params(struct bnxt *bp)
4708 {
4709 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4710 	u32 agg_factor = 0, agg_ring_size = 0;
4711 
4712 	/* 8 for CRC and VLAN */
4713 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4714 
4715 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4716 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4717 
4718 	ring_size = bp->rx_ring_size;
4719 	bp->rx_agg_ring_size = 0;
4720 	bp->rx_agg_nr_pages = 0;
4721 
4722 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4723 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4724 
4725 	bp->flags &= ~BNXT_FLAG_JUMBO;
4726 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4727 		u32 jumbo_factor;
4728 
4729 		bp->flags |= BNXT_FLAG_JUMBO;
4730 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4731 		if (jumbo_factor > agg_factor)
4732 			agg_factor = jumbo_factor;
4733 	}
4734 	if (agg_factor) {
4735 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4736 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4737 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4738 				    bp->rx_ring_size, ring_size);
4739 			bp->rx_ring_size = ring_size;
4740 		}
4741 		agg_ring_size = ring_size * agg_factor;
4742 
4743 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4744 							RX_DESC_CNT);
4745 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4746 			u32 tmp = agg_ring_size;
4747 
4748 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4749 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4750 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4751 				    tmp, agg_ring_size);
4752 		}
4753 		bp->rx_agg_ring_size = agg_ring_size;
4754 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4755 
4756 		if (BNXT_RX_PAGE_MODE(bp)) {
4757 			rx_space = PAGE_SIZE;
4758 			rx_size = PAGE_SIZE -
4759 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4760 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4761 		} else {
4762 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4763 				       bp->rx_copybreak,
4764 				       bp->dev->cfg_pending->hds_thresh);
4765 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4766 			rx_space = rx_size + NET_SKB_PAD +
4767 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4768 		}
4769 	}
4770 
4771 	bp->rx_buf_use_size = rx_size;
4772 	bp->rx_buf_size = rx_space;
4773 
4774 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4775 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4776 
4777 	ring_size = bp->tx_ring_size;
4778 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4779 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4780 
4781 	max_rx_cmpl = bp->rx_ring_size;
4782 	/* MAX TPA needs to be added because TPA_START completions are
4783 	 * immediately recycled, so the TPA completions are not bound by
4784 	 * the RX ring size.
4785 	 */
4786 	if (bp->flags & BNXT_FLAG_TPA)
4787 		max_rx_cmpl += bp->max_tpa;
4788 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4789 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4790 	bp->cp_ring_size = ring_size;
4791 
4792 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4793 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4794 		bp->cp_nr_pages = MAX_CP_PAGES;
4795 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4796 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4797 			    ring_size, bp->cp_ring_size);
4798 	}
4799 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4800 	bp->cp_ring_mask = bp->cp_bit - 1;
4801 }
4802 
4803 /* Changing allocation mode of RX rings.
4804  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4805  */
4806 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4807 {
4808 	struct net_device *dev = bp->dev;
4809 
4810 	if (page_mode) {
4811 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4812 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4813 
4814 		if (bp->xdp_prog->aux->xdp_has_frags)
4815 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4816 		else
4817 			dev->max_mtu =
4818 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4819 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4820 			bp->flags |= BNXT_FLAG_JUMBO;
4821 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4822 		} else {
4823 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4824 			bp->rx_skb_func = bnxt_rx_page_skb;
4825 		}
4826 		bp->rx_dir = DMA_BIDIRECTIONAL;
4827 	} else {
4828 		dev->max_mtu = bp->max_mtu;
4829 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4830 		bp->rx_dir = DMA_FROM_DEVICE;
4831 		bp->rx_skb_func = bnxt_rx_skb;
4832 	}
4833 }
4834 
4835 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4836 {
4837 	__bnxt_set_rx_skb_mode(bp, page_mode);
4838 
4839 	if (!page_mode) {
4840 		int rx, tx;
4841 
4842 		bnxt_get_max_rings(bp, &rx, &tx, true);
4843 		if (rx > 1) {
4844 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4845 			bp->dev->hw_features |= NETIF_F_LRO;
4846 		}
4847 	}
4848 
4849 	/* Update LRO and GRO_HW availability */
4850 	netdev_update_features(bp->dev);
4851 }
4852 
4853 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4854 {
4855 	int i;
4856 	struct bnxt_vnic_info *vnic;
4857 	struct pci_dev *pdev = bp->pdev;
4858 
4859 	if (!bp->vnic_info)
4860 		return;
4861 
4862 	for (i = 0; i < bp->nr_vnics; i++) {
4863 		vnic = &bp->vnic_info[i];
4864 
4865 		kfree(vnic->fw_grp_ids);
4866 		vnic->fw_grp_ids = NULL;
4867 
4868 		kfree(vnic->uc_list);
4869 		vnic->uc_list = NULL;
4870 
4871 		if (vnic->mc_list) {
4872 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4873 					  vnic->mc_list, vnic->mc_list_mapping);
4874 			vnic->mc_list = NULL;
4875 		}
4876 
4877 		if (vnic->rss_table) {
4878 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4879 					  vnic->rss_table,
4880 					  vnic->rss_table_dma_addr);
4881 			vnic->rss_table = NULL;
4882 		}
4883 
4884 		vnic->rss_hash_key = NULL;
4885 		vnic->flags = 0;
4886 	}
4887 }
4888 
4889 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4890 {
4891 	int i, rc = 0, size;
4892 	struct bnxt_vnic_info *vnic;
4893 	struct pci_dev *pdev = bp->pdev;
4894 	int max_rings;
4895 
4896 	for (i = 0; i < bp->nr_vnics; i++) {
4897 		vnic = &bp->vnic_info[i];
4898 
4899 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4900 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4901 
4902 			if (mem_size > 0) {
4903 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4904 				if (!vnic->uc_list) {
4905 					rc = -ENOMEM;
4906 					goto out;
4907 				}
4908 			}
4909 		}
4910 
4911 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4912 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4913 			vnic->mc_list =
4914 				dma_alloc_coherent(&pdev->dev,
4915 						   vnic->mc_list_size,
4916 						   &vnic->mc_list_mapping,
4917 						   GFP_KERNEL);
4918 			if (!vnic->mc_list) {
4919 				rc = -ENOMEM;
4920 				goto out;
4921 			}
4922 		}
4923 
4924 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4925 			goto vnic_skip_grps;
4926 
4927 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4928 			max_rings = bp->rx_nr_rings;
4929 		else
4930 			max_rings = 1;
4931 
4932 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4933 		if (!vnic->fw_grp_ids) {
4934 			rc = -ENOMEM;
4935 			goto out;
4936 		}
4937 vnic_skip_grps:
4938 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4939 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4940 			continue;
4941 
4942 		/* Allocate rss table and hash key */
4943 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4944 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4945 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4946 
4947 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4948 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4949 						     vnic->rss_table_size,
4950 						     &vnic->rss_table_dma_addr,
4951 						     GFP_KERNEL);
4952 		if (!vnic->rss_table) {
4953 			rc = -ENOMEM;
4954 			goto out;
4955 		}
4956 
4957 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4958 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4959 	}
4960 	return 0;
4961 
4962 out:
4963 	return rc;
4964 }
4965 
4966 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4967 {
4968 	struct bnxt_hwrm_wait_token *token;
4969 
4970 	dma_pool_destroy(bp->hwrm_dma_pool);
4971 	bp->hwrm_dma_pool = NULL;
4972 
4973 	rcu_read_lock();
4974 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4975 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4976 	rcu_read_unlock();
4977 }
4978 
4979 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4980 {
4981 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4982 					    BNXT_HWRM_DMA_SIZE,
4983 					    BNXT_HWRM_DMA_ALIGN, 0);
4984 	if (!bp->hwrm_dma_pool)
4985 		return -ENOMEM;
4986 
4987 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4988 
4989 	return 0;
4990 }
4991 
4992 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4993 {
4994 	kfree(stats->hw_masks);
4995 	stats->hw_masks = NULL;
4996 	kfree(stats->sw_stats);
4997 	stats->sw_stats = NULL;
4998 	if (stats->hw_stats) {
4999 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
5000 				  stats->hw_stats_map);
5001 		stats->hw_stats = NULL;
5002 	}
5003 }
5004 
5005 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
5006 				bool alloc_masks)
5007 {
5008 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
5009 					     &stats->hw_stats_map, GFP_KERNEL);
5010 	if (!stats->hw_stats)
5011 		return -ENOMEM;
5012 
5013 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5014 	if (!stats->sw_stats)
5015 		goto stats_mem_err;
5016 
5017 	if (alloc_masks) {
5018 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5019 		if (!stats->hw_masks)
5020 			goto stats_mem_err;
5021 	}
5022 	return 0;
5023 
5024 stats_mem_err:
5025 	bnxt_free_stats_mem(bp, stats);
5026 	return -ENOMEM;
5027 }
5028 
5029 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5030 {
5031 	int i;
5032 
5033 	for (i = 0; i < count; i++)
5034 		mask_arr[i] = mask;
5035 }
5036 
5037 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5038 {
5039 	int i;
5040 
5041 	for (i = 0; i < count; i++)
5042 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5043 }
5044 
5045 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5046 				    struct bnxt_stats_mem *stats)
5047 {
5048 	struct hwrm_func_qstats_ext_output *resp;
5049 	struct hwrm_func_qstats_ext_input *req;
5050 	__le64 *hw_masks;
5051 	int rc;
5052 
5053 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5054 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5055 		return -EOPNOTSUPP;
5056 
5057 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5058 	if (rc)
5059 		return rc;
5060 
5061 	req->fid = cpu_to_le16(0xffff);
5062 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5063 
5064 	resp = hwrm_req_hold(bp, req);
5065 	rc = hwrm_req_send(bp, req);
5066 	if (!rc) {
5067 		hw_masks = &resp->rx_ucast_pkts;
5068 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5069 	}
5070 	hwrm_req_drop(bp, req);
5071 	return rc;
5072 }
5073 
5074 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5075 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5076 
5077 static void bnxt_init_stats(struct bnxt *bp)
5078 {
5079 	struct bnxt_napi *bnapi = bp->bnapi[0];
5080 	struct bnxt_cp_ring_info *cpr;
5081 	struct bnxt_stats_mem *stats;
5082 	__le64 *rx_stats, *tx_stats;
5083 	int rc, rx_count, tx_count;
5084 	u64 *rx_masks, *tx_masks;
5085 	u64 mask;
5086 	u8 flags;
5087 
5088 	cpr = &bnapi->cp_ring;
5089 	stats = &cpr->stats;
5090 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5091 	if (rc) {
5092 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5093 			mask = (1ULL << 48) - 1;
5094 		else
5095 			mask = -1ULL;
5096 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5097 	}
5098 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5099 		stats = &bp->port_stats;
5100 		rx_stats = stats->hw_stats;
5101 		rx_masks = stats->hw_masks;
5102 		rx_count = sizeof(struct rx_port_stats) / 8;
5103 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5104 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5105 		tx_count = sizeof(struct tx_port_stats) / 8;
5106 
5107 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5108 		rc = bnxt_hwrm_port_qstats(bp, flags);
5109 		if (rc) {
5110 			mask = (1ULL << 40) - 1;
5111 
5112 			bnxt_fill_masks(rx_masks, mask, rx_count);
5113 			bnxt_fill_masks(tx_masks, mask, tx_count);
5114 		} else {
5115 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5116 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5117 			bnxt_hwrm_port_qstats(bp, 0);
5118 		}
5119 	}
5120 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5121 		stats = &bp->rx_port_stats_ext;
5122 		rx_stats = stats->hw_stats;
5123 		rx_masks = stats->hw_masks;
5124 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5125 		stats = &bp->tx_port_stats_ext;
5126 		tx_stats = stats->hw_stats;
5127 		tx_masks = stats->hw_masks;
5128 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5129 
5130 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5131 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5132 		if (rc) {
5133 			mask = (1ULL << 40) - 1;
5134 
5135 			bnxt_fill_masks(rx_masks, mask, rx_count);
5136 			if (tx_stats)
5137 				bnxt_fill_masks(tx_masks, mask, tx_count);
5138 		} else {
5139 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5140 			if (tx_stats)
5141 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5142 						   tx_count);
5143 			bnxt_hwrm_port_qstats_ext(bp, 0);
5144 		}
5145 	}
5146 }
5147 
5148 static void bnxt_free_port_stats(struct bnxt *bp)
5149 {
5150 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5151 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5152 
5153 	bnxt_free_stats_mem(bp, &bp->port_stats);
5154 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5155 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5156 }
5157 
5158 static void bnxt_free_ring_stats(struct bnxt *bp)
5159 {
5160 	int i;
5161 
5162 	if (!bp->bnapi)
5163 		return;
5164 
5165 	for (i = 0; i < bp->cp_nr_rings; i++) {
5166 		struct bnxt_napi *bnapi = bp->bnapi[i];
5167 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5168 
5169 		bnxt_free_stats_mem(bp, &cpr->stats);
5170 
5171 		kfree(cpr->sw_stats);
5172 		cpr->sw_stats = NULL;
5173 	}
5174 }
5175 
5176 static int bnxt_alloc_stats(struct bnxt *bp)
5177 {
5178 	u32 size, i;
5179 	int rc;
5180 
5181 	size = bp->hw_ring_stats_size;
5182 
5183 	for (i = 0; i < bp->cp_nr_rings; i++) {
5184 		struct bnxt_napi *bnapi = bp->bnapi[i];
5185 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5186 
5187 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5188 		if (!cpr->sw_stats)
5189 			return -ENOMEM;
5190 
5191 		cpr->stats.len = size;
5192 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5193 		if (rc)
5194 			return rc;
5195 
5196 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5197 	}
5198 
5199 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5200 		return 0;
5201 
5202 	if (bp->port_stats.hw_stats)
5203 		goto alloc_ext_stats;
5204 
5205 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5206 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5207 	if (rc)
5208 		return rc;
5209 
5210 	bp->flags |= BNXT_FLAG_PORT_STATS;
5211 
5212 alloc_ext_stats:
5213 	/* Display extended statistics only if FW supports it */
5214 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5215 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5216 			return 0;
5217 
5218 	if (bp->rx_port_stats_ext.hw_stats)
5219 		goto alloc_tx_ext_stats;
5220 
5221 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5222 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5223 	/* Extended stats are optional */
5224 	if (rc)
5225 		return 0;
5226 
5227 alloc_tx_ext_stats:
5228 	if (bp->tx_port_stats_ext.hw_stats)
5229 		return 0;
5230 
5231 	if (bp->hwrm_spec_code >= 0x10902 ||
5232 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5233 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5234 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5235 		/* Extended stats are optional */
5236 		if (rc)
5237 			return 0;
5238 	}
5239 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5240 	return 0;
5241 }
5242 
5243 static void bnxt_clear_ring_indices(struct bnxt *bp)
5244 {
5245 	int i, j;
5246 
5247 	if (!bp->bnapi)
5248 		return;
5249 
5250 	for (i = 0; i < bp->cp_nr_rings; i++) {
5251 		struct bnxt_napi *bnapi = bp->bnapi[i];
5252 		struct bnxt_cp_ring_info *cpr;
5253 		struct bnxt_rx_ring_info *rxr;
5254 		struct bnxt_tx_ring_info *txr;
5255 
5256 		if (!bnapi)
5257 			continue;
5258 
5259 		cpr = &bnapi->cp_ring;
5260 		cpr->cp_raw_cons = 0;
5261 
5262 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5263 			txr->tx_prod = 0;
5264 			txr->tx_cons = 0;
5265 			txr->tx_hw_cons = 0;
5266 		}
5267 
5268 		rxr = bnapi->rx_ring;
5269 		if (rxr) {
5270 			rxr->rx_prod = 0;
5271 			rxr->rx_agg_prod = 0;
5272 			rxr->rx_sw_agg_prod = 0;
5273 			rxr->rx_next_cons = 0;
5274 		}
5275 		bnapi->events = 0;
5276 	}
5277 }
5278 
5279 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5280 {
5281 	u8 type = fltr->type, flags = fltr->flags;
5282 
5283 	INIT_LIST_HEAD(&fltr->list);
5284 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5285 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5286 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5287 }
5288 
5289 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5290 {
5291 	if (!list_empty(&fltr->list))
5292 		list_del_init(&fltr->list);
5293 }
5294 
5295 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5296 {
5297 	struct bnxt_filter_base *usr_fltr, *tmp;
5298 
5299 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5300 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5301 			continue;
5302 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5303 	}
5304 }
5305 
5306 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5307 {
5308 	hlist_del(&fltr->hash);
5309 	bnxt_del_one_usr_fltr(bp, fltr);
5310 	if (fltr->flags) {
5311 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5312 		bp->ntp_fltr_count--;
5313 	}
5314 	kfree(fltr);
5315 }
5316 
5317 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5318 {
5319 	int i;
5320 
5321 	netdev_assert_locked(bp->dev);
5322 
5323 	/* Under netdev instance lock and all our NAPIs have been disabled.
5324 	 * It's safe to delete the hash table.
5325 	 */
5326 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5327 		struct hlist_head *head;
5328 		struct hlist_node *tmp;
5329 		struct bnxt_ntuple_filter *fltr;
5330 
5331 		head = &bp->ntp_fltr_hash_tbl[i];
5332 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5333 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5334 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5335 				     !list_empty(&fltr->base.list)))
5336 				continue;
5337 			bnxt_del_fltr(bp, &fltr->base);
5338 		}
5339 	}
5340 	if (!all)
5341 		return;
5342 
5343 	bitmap_free(bp->ntp_fltr_bmap);
5344 	bp->ntp_fltr_bmap = NULL;
5345 	bp->ntp_fltr_count = 0;
5346 }
5347 
5348 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5349 {
5350 	int i, rc = 0;
5351 
5352 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5353 		return 0;
5354 
5355 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5356 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5357 
5358 	bp->ntp_fltr_count = 0;
5359 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5360 
5361 	if (!bp->ntp_fltr_bmap)
5362 		rc = -ENOMEM;
5363 
5364 	return rc;
5365 }
5366 
5367 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5368 {
5369 	int i;
5370 
5371 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5372 		struct hlist_head *head;
5373 		struct hlist_node *tmp;
5374 		struct bnxt_l2_filter *fltr;
5375 
5376 		head = &bp->l2_fltr_hash_tbl[i];
5377 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5378 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5379 				     !list_empty(&fltr->base.list)))
5380 				continue;
5381 			bnxt_del_fltr(bp, &fltr->base);
5382 		}
5383 	}
5384 }
5385 
5386 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5387 {
5388 	int i;
5389 
5390 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5391 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5392 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5393 }
5394 
5395 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5396 {
5397 	bnxt_free_vnic_attributes(bp);
5398 	bnxt_free_tx_rings(bp);
5399 	bnxt_free_rx_rings(bp);
5400 	bnxt_free_cp_rings(bp);
5401 	bnxt_free_all_cp_arrays(bp);
5402 	bnxt_free_ntp_fltrs(bp, false);
5403 	bnxt_free_l2_filters(bp, false);
5404 	if (irq_re_init) {
5405 		bnxt_free_ring_stats(bp);
5406 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5407 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5408 			bnxt_free_port_stats(bp);
5409 		bnxt_free_ring_grps(bp);
5410 		bnxt_free_vnics(bp);
5411 		kfree(bp->tx_ring_map);
5412 		bp->tx_ring_map = NULL;
5413 		kfree(bp->tx_ring);
5414 		bp->tx_ring = NULL;
5415 		kfree(bp->rx_ring);
5416 		bp->rx_ring = NULL;
5417 		kfree(bp->bnapi);
5418 		bp->bnapi = NULL;
5419 	} else {
5420 		bnxt_clear_ring_indices(bp);
5421 	}
5422 }
5423 
5424 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5425 {
5426 	int i, j, rc, size, arr_size;
5427 	void *bnapi;
5428 
5429 	if (irq_re_init) {
5430 		/* Allocate bnapi mem pointer array and mem block for
5431 		 * all queues
5432 		 */
5433 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5434 				bp->cp_nr_rings);
5435 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5436 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5437 		if (!bnapi)
5438 			return -ENOMEM;
5439 
5440 		bp->bnapi = bnapi;
5441 		bnapi += arr_size;
5442 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5443 			bp->bnapi[i] = bnapi;
5444 			bp->bnapi[i]->index = i;
5445 			bp->bnapi[i]->bp = bp;
5446 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5447 				struct bnxt_cp_ring_info *cpr =
5448 					&bp->bnapi[i]->cp_ring;
5449 
5450 				cpr->cp_ring_struct.ring_mem.flags =
5451 					BNXT_RMEM_RING_PTE_FLAG;
5452 			}
5453 		}
5454 
5455 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5456 				      sizeof(struct bnxt_rx_ring_info),
5457 				      GFP_KERNEL);
5458 		if (!bp->rx_ring)
5459 			return -ENOMEM;
5460 
5461 		for (i = 0; i < bp->rx_nr_rings; i++) {
5462 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5463 
5464 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5465 				rxr->rx_ring_struct.ring_mem.flags =
5466 					BNXT_RMEM_RING_PTE_FLAG;
5467 				rxr->rx_agg_ring_struct.ring_mem.flags =
5468 					BNXT_RMEM_RING_PTE_FLAG;
5469 			} else {
5470 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5471 			}
5472 			rxr->bnapi = bp->bnapi[i];
5473 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5474 		}
5475 
5476 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5477 				      sizeof(struct bnxt_tx_ring_info),
5478 				      GFP_KERNEL);
5479 		if (!bp->tx_ring)
5480 			return -ENOMEM;
5481 
5482 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5483 					  GFP_KERNEL);
5484 
5485 		if (!bp->tx_ring_map)
5486 			return -ENOMEM;
5487 
5488 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5489 			j = 0;
5490 		else
5491 			j = bp->rx_nr_rings;
5492 
5493 		for (i = 0; i < bp->tx_nr_rings; i++) {
5494 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5495 			struct bnxt_napi *bnapi2;
5496 
5497 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5498 				txr->tx_ring_struct.ring_mem.flags =
5499 					BNXT_RMEM_RING_PTE_FLAG;
5500 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5501 			if (i >= bp->tx_nr_rings_xdp) {
5502 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5503 
5504 				bnapi2 = bp->bnapi[k];
5505 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5506 				txr->tx_napi_idx =
5507 					BNXT_RING_TO_TC(bp, txr->txq_index);
5508 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5509 				bnapi2->tx_int = bnxt_tx_int;
5510 			} else {
5511 				bnapi2 = bp->bnapi[j];
5512 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5513 				bnapi2->tx_ring[0] = txr;
5514 				bnapi2->tx_int = bnxt_tx_int_xdp;
5515 				j++;
5516 			}
5517 			txr->bnapi = bnapi2;
5518 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5519 				txr->tx_cpr = &bnapi2->cp_ring;
5520 		}
5521 
5522 		rc = bnxt_alloc_stats(bp);
5523 		if (rc)
5524 			goto alloc_mem_err;
5525 		bnxt_init_stats(bp);
5526 
5527 		rc = bnxt_alloc_ntp_fltrs(bp);
5528 		if (rc)
5529 			goto alloc_mem_err;
5530 
5531 		rc = bnxt_alloc_vnics(bp);
5532 		if (rc)
5533 			goto alloc_mem_err;
5534 	}
5535 
5536 	rc = bnxt_alloc_all_cp_arrays(bp);
5537 	if (rc)
5538 		goto alloc_mem_err;
5539 
5540 	bnxt_init_ring_struct(bp);
5541 
5542 	rc = bnxt_alloc_rx_rings(bp);
5543 	if (rc)
5544 		goto alloc_mem_err;
5545 
5546 	rc = bnxt_alloc_tx_rings(bp);
5547 	if (rc)
5548 		goto alloc_mem_err;
5549 
5550 	rc = bnxt_alloc_cp_rings(bp);
5551 	if (rc)
5552 		goto alloc_mem_err;
5553 
5554 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5555 						  BNXT_VNIC_MCAST_FLAG |
5556 						  BNXT_VNIC_UCAST_FLAG;
5557 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5558 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5559 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5560 
5561 	rc = bnxt_alloc_vnic_attributes(bp);
5562 	if (rc)
5563 		goto alloc_mem_err;
5564 	return 0;
5565 
5566 alloc_mem_err:
5567 	bnxt_free_mem(bp, true);
5568 	return rc;
5569 }
5570 
5571 static void bnxt_disable_int(struct bnxt *bp)
5572 {
5573 	int i;
5574 
5575 	if (!bp->bnapi)
5576 		return;
5577 
5578 	for (i = 0; i < bp->cp_nr_rings; i++) {
5579 		struct bnxt_napi *bnapi = bp->bnapi[i];
5580 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5581 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5582 
5583 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5584 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5585 	}
5586 }
5587 
5588 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5589 {
5590 	struct bnxt_napi *bnapi = bp->bnapi[n];
5591 	struct bnxt_cp_ring_info *cpr;
5592 
5593 	cpr = &bnapi->cp_ring;
5594 	return cpr->cp_ring_struct.map_idx;
5595 }
5596 
5597 static void bnxt_disable_int_sync(struct bnxt *bp)
5598 {
5599 	int i;
5600 
5601 	if (!bp->irq_tbl)
5602 		return;
5603 
5604 	atomic_inc(&bp->intr_sem);
5605 
5606 	bnxt_disable_int(bp);
5607 	for (i = 0; i < bp->cp_nr_rings; i++) {
5608 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5609 
5610 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5611 	}
5612 }
5613 
5614 static void bnxt_enable_int(struct bnxt *bp)
5615 {
5616 	int i;
5617 
5618 	atomic_set(&bp->intr_sem, 0);
5619 	for (i = 0; i < bp->cp_nr_rings; i++) {
5620 		struct bnxt_napi *bnapi = bp->bnapi[i];
5621 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5622 
5623 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5624 	}
5625 }
5626 
5627 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5628 			    bool async_only)
5629 {
5630 	DECLARE_BITMAP(async_events_bmap, 256);
5631 	u32 *events = (u32 *)async_events_bmap;
5632 	struct hwrm_func_drv_rgtr_output *resp;
5633 	struct hwrm_func_drv_rgtr_input *req;
5634 	u32 flags;
5635 	int rc, i;
5636 
5637 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5638 	if (rc)
5639 		return rc;
5640 
5641 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5642 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5643 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5644 
5645 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5646 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5647 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5648 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5649 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5650 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5651 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5652 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5653 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5654 	req->flags = cpu_to_le32(flags);
5655 	req->ver_maj_8b = DRV_VER_MAJ;
5656 	req->ver_min_8b = DRV_VER_MIN;
5657 	req->ver_upd_8b = DRV_VER_UPD;
5658 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5659 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5660 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5661 
5662 	if (BNXT_PF(bp)) {
5663 		u32 data[8];
5664 		int i;
5665 
5666 		memset(data, 0, sizeof(data));
5667 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5668 			u16 cmd = bnxt_vf_req_snif[i];
5669 			unsigned int bit, idx;
5670 
5671 			idx = cmd / 32;
5672 			bit = cmd % 32;
5673 			data[idx] |= 1 << bit;
5674 		}
5675 
5676 		for (i = 0; i < 8; i++)
5677 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5678 
5679 		req->enables |=
5680 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5681 	}
5682 
5683 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5684 		req->flags |= cpu_to_le32(
5685 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5686 
5687 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5688 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5689 		u16 event_id = bnxt_async_events_arr[i];
5690 
5691 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5692 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5693 			continue;
5694 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5695 		    !bp->ptp_cfg)
5696 			continue;
5697 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5698 	}
5699 	if (bmap && bmap_size) {
5700 		for (i = 0; i < bmap_size; i++) {
5701 			if (test_bit(i, bmap))
5702 				__set_bit(i, async_events_bmap);
5703 		}
5704 	}
5705 	for (i = 0; i < 8; i++)
5706 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5707 
5708 	if (async_only)
5709 		req->enables =
5710 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5711 
5712 	resp = hwrm_req_hold(bp, req);
5713 	rc = hwrm_req_send(bp, req);
5714 	if (!rc) {
5715 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5716 		if (resp->flags &
5717 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5718 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5719 	}
5720 	hwrm_req_drop(bp, req);
5721 	return rc;
5722 }
5723 
5724 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5725 {
5726 	struct hwrm_func_drv_unrgtr_input *req;
5727 	int rc;
5728 
5729 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5730 		return 0;
5731 
5732 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5733 	if (rc)
5734 		return rc;
5735 	return hwrm_req_send(bp, req);
5736 }
5737 
5738 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5739 
5740 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5741 {
5742 	struct hwrm_tunnel_dst_port_free_input *req;
5743 	int rc;
5744 
5745 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5746 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5747 		return 0;
5748 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5749 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5750 		return 0;
5751 
5752 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5753 	if (rc)
5754 		return rc;
5755 
5756 	req->tunnel_type = tunnel_type;
5757 
5758 	switch (tunnel_type) {
5759 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5760 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5761 		bp->vxlan_port = 0;
5762 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5763 		break;
5764 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5765 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5766 		bp->nge_port = 0;
5767 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5768 		break;
5769 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5770 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5771 		bp->vxlan_gpe_port = 0;
5772 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5773 		break;
5774 	default:
5775 		break;
5776 	}
5777 
5778 	rc = hwrm_req_send(bp, req);
5779 	if (rc)
5780 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5781 			   rc);
5782 	if (bp->flags & BNXT_FLAG_TPA)
5783 		bnxt_set_tpa(bp, true);
5784 	return rc;
5785 }
5786 
5787 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5788 					   u8 tunnel_type)
5789 {
5790 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5791 	struct hwrm_tunnel_dst_port_alloc_input *req;
5792 	int rc;
5793 
5794 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5795 	if (rc)
5796 		return rc;
5797 
5798 	req->tunnel_type = tunnel_type;
5799 	req->tunnel_dst_port_val = port;
5800 
5801 	resp = hwrm_req_hold(bp, req);
5802 	rc = hwrm_req_send(bp, req);
5803 	if (rc) {
5804 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5805 			   rc);
5806 		goto err_out;
5807 	}
5808 
5809 	switch (tunnel_type) {
5810 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5811 		bp->vxlan_port = port;
5812 		bp->vxlan_fw_dst_port_id =
5813 			le16_to_cpu(resp->tunnel_dst_port_id);
5814 		break;
5815 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5816 		bp->nge_port = port;
5817 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5818 		break;
5819 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5820 		bp->vxlan_gpe_port = port;
5821 		bp->vxlan_gpe_fw_dst_port_id =
5822 			le16_to_cpu(resp->tunnel_dst_port_id);
5823 		break;
5824 	default:
5825 		break;
5826 	}
5827 	if (bp->flags & BNXT_FLAG_TPA)
5828 		bnxt_set_tpa(bp, true);
5829 
5830 err_out:
5831 	hwrm_req_drop(bp, req);
5832 	return rc;
5833 }
5834 
5835 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5836 {
5837 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5838 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5839 	int rc;
5840 
5841 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5842 	if (rc)
5843 		return rc;
5844 
5845 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5846 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5847 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5848 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5849 	}
5850 	req->mask = cpu_to_le32(vnic->rx_mask);
5851 	return hwrm_req_send_silent(bp, req);
5852 }
5853 
5854 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5855 {
5856 	if (!atomic_dec_and_test(&fltr->refcnt))
5857 		return;
5858 	spin_lock_bh(&bp->ntp_fltr_lock);
5859 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5860 		spin_unlock_bh(&bp->ntp_fltr_lock);
5861 		return;
5862 	}
5863 	hlist_del_rcu(&fltr->base.hash);
5864 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5865 	if (fltr->base.flags) {
5866 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5867 		bp->ntp_fltr_count--;
5868 	}
5869 	spin_unlock_bh(&bp->ntp_fltr_lock);
5870 	kfree_rcu(fltr, base.rcu);
5871 }
5872 
5873 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5874 						      struct bnxt_l2_key *key,
5875 						      u32 idx)
5876 {
5877 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5878 	struct bnxt_l2_filter *fltr;
5879 
5880 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5881 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5882 
5883 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5884 		    l2_key->vlan == key->vlan)
5885 			return fltr;
5886 	}
5887 	return NULL;
5888 }
5889 
5890 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5891 						    struct bnxt_l2_key *key,
5892 						    u32 idx)
5893 {
5894 	struct bnxt_l2_filter *fltr = NULL;
5895 
5896 	rcu_read_lock();
5897 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5898 	if (fltr)
5899 		atomic_inc(&fltr->refcnt);
5900 	rcu_read_unlock();
5901 	return fltr;
5902 }
5903 
5904 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5905 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5906 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5907 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5908 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5909 
5910 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5911 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5912 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5913 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5914 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5915 
5916 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5917 {
5918 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5919 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5920 			return sizeof(fkeys->addrs.v4addrs) +
5921 			       sizeof(fkeys->ports);
5922 
5923 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5924 			return sizeof(fkeys->addrs.v4addrs);
5925 	}
5926 
5927 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5928 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5929 			return sizeof(fkeys->addrs.v6addrs) +
5930 			       sizeof(fkeys->ports);
5931 
5932 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5933 			return sizeof(fkeys->addrs.v6addrs);
5934 	}
5935 
5936 	return 0;
5937 }
5938 
5939 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5940 			 const unsigned char *key)
5941 {
5942 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5943 	struct bnxt_ipv4_tuple tuple4;
5944 	struct bnxt_ipv6_tuple tuple6;
5945 	int i, j, len = 0;
5946 	u8 *four_tuple;
5947 
5948 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5949 	if (!len)
5950 		return 0;
5951 
5952 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5953 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5954 		tuple4.ports = fkeys->ports;
5955 		four_tuple = (unsigned char *)&tuple4;
5956 	} else {
5957 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5958 		tuple6.ports = fkeys->ports;
5959 		four_tuple = (unsigned char *)&tuple6;
5960 	}
5961 
5962 	for (i = 0, j = 8; i < len; i++, j++) {
5963 		u8 byte = four_tuple[i];
5964 		int bit;
5965 
5966 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5967 			if (byte & 0x80)
5968 				hash ^= prefix;
5969 		}
5970 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5971 	}
5972 
5973 	/* The valid part of the hash is in the upper 32 bits. */
5974 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5975 }
5976 
5977 #ifdef CONFIG_RFS_ACCEL
5978 static struct bnxt_l2_filter *
5979 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5980 {
5981 	struct bnxt_l2_filter *fltr;
5982 	u32 idx;
5983 
5984 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5985 	      BNXT_L2_FLTR_HASH_MASK;
5986 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5987 	return fltr;
5988 }
5989 #endif
5990 
5991 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5992 			       struct bnxt_l2_key *key, u32 idx)
5993 {
5994 	struct hlist_head *head;
5995 
5996 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5997 	fltr->l2_key.vlan = key->vlan;
5998 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5999 	if (fltr->base.flags) {
6000 		int bit_id;
6001 
6002 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
6003 						 bp->max_fltr, 0);
6004 		if (bit_id < 0)
6005 			return -ENOMEM;
6006 		fltr->base.sw_id = (u16)bit_id;
6007 		bp->ntp_fltr_count++;
6008 	}
6009 	head = &bp->l2_fltr_hash_tbl[idx];
6010 	hlist_add_head_rcu(&fltr->base.hash, head);
6011 	bnxt_insert_usr_fltr(bp, &fltr->base);
6012 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6013 	atomic_set(&fltr->refcnt, 1);
6014 	return 0;
6015 }
6016 
6017 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6018 						   struct bnxt_l2_key *key,
6019 						   gfp_t gfp)
6020 {
6021 	struct bnxt_l2_filter *fltr;
6022 	u32 idx;
6023 	int rc;
6024 
6025 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6026 	      BNXT_L2_FLTR_HASH_MASK;
6027 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6028 	if (fltr)
6029 		return fltr;
6030 
6031 	fltr = kzalloc(sizeof(*fltr), gfp);
6032 	if (!fltr)
6033 		return ERR_PTR(-ENOMEM);
6034 	spin_lock_bh(&bp->ntp_fltr_lock);
6035 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6036 	spin_unlock_bh(&bp->ntp_fltr_lock);
6037 	if (rc) {
6038 		bnxt_del_l2_filter(bp, fltr);
6039 		fltr = ERR_PTR(rc);
6040 	}
6041 	return fltr;
6042 }
6043 
6044 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6045 						struct bnxt_l2_key *key,
6046 						u16 flags)
6047 {
6048 	struct bnxt_l2_filter *fltr;
6049 	u32 idx;
6050 	int rc;
6051 
6052 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6053 	      BNXT_L2_FLTR_HASH_MASK;
6054 	spin_lock_bh(&bp->ntp_fltr_lock);
6055 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6056 	if (fltr) {
6057 		fltr = ERR_PTR(-EEXIST);
6058 		goto l2_filter_exit;
6059 	}
6060 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
6061 	if (!fltr) {
6062 		fltr = ERR_PTR(-ENOMEM);
6063 		goto l2_filter_exit;
6064 	}
6065 	fltr->base.flags = flags;
6066 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6067 	if (rc) {
6068 		spin_unlock_bh(&bp->ntp_fltr_lock);
6069 		bnxt_del_l2_filter(bp, fltr);
6070 		return ERR_PTR(rc);
6071 	}
6072 
6073 l2_filter_exit:
6074 	spin_unlock_bh(&bp->ntp_fltr_lock);
6075 	return fltr;
6076 }
6077 
6078 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6079 {
6080 #ifdef CONFIG_BNXT_SRIOV
6081 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6082 
6083 	return vf->fw_fid;
6084 #else
6085 	return INVALID_HW_RING_ID;
6086 #endif
6087 }
6088 
6089 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6090 {
6091 	struct hwrm_cfa_l2_filter_free_input *req;
6092 	u16 target_id = 0xffff;
6093 	int rc;
6094 
6095 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6096 		struct bnxt_pf_info *pf = &bp->pf;
6097 
6098 		if (fltr->base.vf_idx >= pf->active_vfs)
6099 			return -EINVAL;
6100 
6101 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6102 		if (target_id == INVALID_HW_RING_ID)
6103 			return -EINVAL;
6104 	}
6105 
6106 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6107 	if (rc)
6108 		return rc;
6109 
6110 	req->target_id = cpu_to_le16(target_id);
6111 	req->l2_filter_id = fltr->base.filter_id;
6112 	return hwrm_req_send(bp, req);
6113 }
6114 
6115 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6116 {
6117 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6118 	struct hwrm_cfa_l2_filter_alloc_input *req;
6119 	u16 target_id = 0xffff;
6120 	int rc;
6121 
6122 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6123 		struct bnxt_pf_info *pf = &bp->pf;
6124 
6125 		if (fltr->base.vf_idx >= pf->active_vfs)
6126 			return -EINVAL;
6127 
6128 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6129 	}
6130 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6131 	if (rc)
6132 		return rc;
6133 
6134 	req->target_id = cpu_to_le16(target_id);
6135 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6136 
6137 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6138 		req->flags |=
6139 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6140 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6141 	req->enables =
6142 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6143 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6144 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6145 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6146 	eth_broadcast_addr(req->l2_addr_mask);
6147 
6148 	if (fltr->l2_key.vlan) {
6149 		req->enables |=
6150 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6151 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6152 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6153 		req->num_vlans = 1;
6154 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6155 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6156 	}
6157 
6158 	resp = hwrm_req_hold(bp, req);
6159 	rc = hwrm_req_send(bp, req);
6160 	if (!rc) {
6161 		fltr->base.filter_id = resp->l2_filter_id;
6162 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6163 	}
6164 	hwrm_req_drop(bp, req);
6165 	return rc;
6166 }
6167 
6168 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6169 				     struct bnxt_ntuple_filter *fltr)
6170 {
6171 	struct hwrm_cfa_ntuple_filter_free_input *req;
6172 	int rc;
6173 
6174 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6175 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6176 	if (rc)
6177 		return rc;
6178 
6179 	req->ntuple_filter_id = fltr->base.filter_id;
6180 	return hwrm_req_send(bp, req);
6181 }
6182 
6183 #define BNXT_NTP_FLTR_FLAGS					\
6184 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6185 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6186 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6187 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6188 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6189 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6190 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6191 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6192 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6193 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6194 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6195 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6196 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6197 
6198 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6199 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6200 
6201 void bnxt_fill_ipv6_mask(__be32 mask[4])
6202 {
6203 	int i;
6204 
6205 	for (i = 0; i < 4; i++)
6206 		mask[i] = cpu_to_be32(~0);
6207 }
6208 
6209 static void
6210 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6211 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6212 			  struct bnxt_ntuple_filter *fltr)
6213 {
6214 	u16 rxq = fltr->base.rxq;
6215 
6216 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6217 		struct ethtool_rxfh_context *ctx;
6218 		struct bnxt_rss_ctx *rss_ctx;
6219 		struct bnxt_vnic_info *vnic;
6220 
6221 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6222 			      fltr->base.fw_vnic_id);
6223 		if (ctx) {
6224 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6225 			vnic = &rss_ctx->vnic;
6226 
6227 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6228 		}
6229 		return;
6230 	}
6231 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6232 		struct bnxt_vnic_info *vnic;
6233 		u32 enables;
6234 
6235 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6236 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6237 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6238 		req->enables |= cpu_to_le32(enables);
6239 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6240 	} else {
6241 		u32 flags;
6242 
6243 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6244 		req->flags |= cpu_to_le32(flags);
6245 		req->dst_id = cpu_to_le16(rxq);
6246 	}
6247 }
6248 
6249 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6250 				      struct bnxt_ntuple_filter *fltr)
6251 {
6252 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6253 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6254 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6255 	struct flow_keys *keys = &fltr->fkeys;
6256 	struct bnxt_l2_filter *l2_fltr;
6257 	struct bnxt_vnic_info *vnic;
6258 	int rc;
6259 
6260 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6261 	if (rc)
6262 		return rc;
6263 
6264 	l2_fltr = fltr->l2_fltr;
6265 	req->l2_filter_id = l2_fltr->base.filter_id;
6266 
6267 	if (fltr->base.flags & BNXT_ACT_DROP) {
6268 		req->flags =
6269 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6270 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6271 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6272 	} else {
6273 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6274 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6275 	}
6276 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6277 
6278 	req->ethertype = htons(ETH_P_IP);
6279 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6280 	req->ip_protocol = keys->basic.ip_proto;
6281 
6282 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6283 		req->ethertype = htons(ETH_P_IPV6);
6284 		req->ip_addr_type =
6285 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6286 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6287 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6288 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6289 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6290 	} else {
6291 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6292 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6293 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6294 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6295 	}
6296 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6297 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6298 		req->tunnel_type =
6299 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6300 	}
6301 
6302 	req->src_port = keys->ports.src;
6303 	req->src_port_mask = masks->ports.src;
6304 	req->dst_port = keys->ports.dst;
6305 	req->dst_port_mask = masks->ports.dst;
6306 
6307 	resp = hwrm_req_hold(bp, req);
6308 	rc = hwrm_req_send(bp, req);
6309 	if (!rc)
6310 		fltr->base.filter_id = resp->ntuple_filter_id;
6311 	hwrm_req_drop(bp, req);
6312 	return rc;
6313 }
6314 
6315 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6316 				     const u8 *mac_addr)
6317 {
6318 	struct bnxt_l2_filter *fltr;
6319 	struct bnxt_l2_key key;
6320 	int rc;
6321 
6322 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6323 	key.vlan = 0;
6324 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6325 	if (IS_ERR(fltr))
6326 		return PTR_ERR(fltr);
6327 
6328 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6329 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6330 	if (rc)
6331 		bnxt_del_l2_filter(bp, fltr);
6332 	else
6333 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6334 	return rc;
6335 }
6336 
6337 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6338 {
6339 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6340 
6341 	/* Any associated ntuple filters will also be cleared by firmware. */
6342 	for (i = 0; i < num_of_vnics; i++) {
6343 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6344 
6345 		for (j = 0; j < vnic->uc_filter_count; j++) {
6346 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6347 
6348 			bnxt_hwrm_l2_filter_free(bp, fltr);
6349 			bnxt_del_l2_filter(bp, fltr);
6350 		}
6351 		vnic->uc_filter_count = 0;
6352 	}
6353 }
6354 
6355 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6356 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6357 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6358 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6359 
6360 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6361 					   struct hwrm_vnic_tpa_cfg_input *req)
6362 {
6363 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6364 
6365 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6366 		return;
6367 
6368 	if (bp->vxlan_port)
6369 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6370 	if (bp->vxlan_gpe_port)
6371 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6372 	if (bp->nge_port)
6373 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6374 
6375 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6376 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6377 }
6378 
6379 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6380 			   u32 tpa_flags)
6381 {
6382 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6383 	struct hwrm_vnic_tpa_cfg_input *req;
6384 	int rc;
6385 
6386 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6387 		return 0;
6388 
6389 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6390 	if (rc)
6391 		return rc;
6392 
6393 	if (tpa_flags) {
6394 		u16 mss = bp->dev->mtu - 40;
6395 		u32 nsegs, n, segs = 0, flags;
6396 
6397 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6398 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6399 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6400 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6401 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6402 		if (tpa_flags & BNXT_FLAG_GRO)
6403 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6404 
6405 		req->flags = cpu_to_le32(flags);
6406 
6407 		req->enables =
6408 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6409 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6410 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6411 
6412 		/* Number of segs are log2 units, and first packet is not
6413 		 * included as part of this units.
6414 		 */
6415 		if (mss <= BNXT_RX_PAGE_SIZE) {
6416 			n = BNXT_RX_PAGE_SIZE / mss;
6417 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6418 		} else {
6419 			n = mss / BNXT_RX_PAGE_SIZE;
6420 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6421 				n++;
6422 			nsegs = (MAX_SKB_FRAGS - n) / n;
6423 		}
6424 
6425 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6426 			segs = MAX_TPA_SEGS_P5;
6427 			max_aggs = bp->max_tpa;
6428 		} else {
6429 			segs = ilog2(nsegs);
6430 		}
6431 		req->max_agg_segs = cpu_to_le16(segs);
6432 		req->max_aggs = cpu_to_le16(max_aggs);
6433 
6434 		req->min_agg_len = cpu_to_le32(512);
6435 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6436 	}
6437 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6438 
6439 	return hwrm_req_send(bp, req);
6440 }
6441 
6442 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6443 {
6444 	struct bnxt_ring_grp_info *grp_info;
6445 
6446 	grp_info = &bp->grp_info[ring->grp_idx];
6447 	return grp_info->cp_fw_ring_id;
6448 }
6449 
6450 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6451 {
6452 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6453 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6454 	else
6455 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6456 }
6457 
6458 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6459 {
6460 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6461 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6462 	else
6463 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6464 }
6465 
6466 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6467 {
6468 	int entries;
6469 
6470 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6471 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6472 	else
6473 		entries = HW_HASH_INDEX_SIZE;
6474 
6475 	bp->rss_indir_tbl_entries = entries;
6476 	bp->rss_indir_tbl =
6477 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6478 	if (!bp->rss_indir_tbl)
6479 		return -ENOMEM;
6480 
6481 	return 0;
6482 }
6483 
6484 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6485 				 struct ethtool_rxfh_context *rss_ctx)
6486 {
6487 	u16 max_rings, max_entries, pad, i;
6488 	u32 *rss_indir_tbl;
6489 
6490 	if (!bp->rx_nr_rings)
6491 		return;
6492 
6493 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6494 		max_rings = bp->rx_nr_rings - 1;
6495 	else
6496 		max_rings = bp->rx_nr_rings;
6497 
6498 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6499 	if (rss_ctx)
6500 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6501 	else
6502 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6503 
6504 	for (i = 0; i < max_entries; i++)
6505 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6506 
6507 	pad = bp->rss_indir_tbl_entries - max_entries;
6508 	if (pad)
6509 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6510 }
6511 
6512 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6513 {
6514 	u32 i, tbl_size, max_ring = 0;
6515 
6516 	if (!bp->rss_indir_tbl)
6517 		return 0;
6518 
6519 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6520 	for (i = 0; i < tbl_size; i++)
6521 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6522 	return max_ring;
6523 }
6524 
6525 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6526 {
6527 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6528 		if (!rx_rings)
6529 			return 0;
6530 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6531 					       BNXT_RSS_TABLE_ENTRIES_P5);
6532 	}
6533 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6534 		return 2;
6535 	return 1;
6536 }
6537 
6538 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6539 {
6540 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6541 	u16 i, j;
6542 
6543 	/* Fill the RSS indirection table with ring group ids */
6544 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6545 		if (!no_rss)
6546 			j = bp->rss_indir_tbl[i];
6547 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6548 	}
6549 }
6550 
6551 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6552 				    struct bnxt_vnic_info *vnic)
6553 {
6554 	__le16 *ring_tbl = vnic->rss_table;
6555 	struct bnxt_rx_ring_info *rxr;
6556 	u16 tbl_size, i;
6557 
6558 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6559 
6560 	for (i = 0; i < tbl_size; i++) {
6561 		u16 ring_id, j;
6562 
6563 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6564 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6565 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6566 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6567 		else
6568 			j = bp->rss_indir_tbl[i];
6569 		rxr = &bp->rx_ring[j];
6570 
6571 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6572 		*ring_tbl++ = cpu_to_le16(ring_id);
6573 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6574 		*ring_tbl++ = cpu_to_le16(ring_id);
6575 	}
6576 }
6577 
6578 static void
6579 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6580 			 struct bnxt_vnic_info *vnic)
6581 {
6582 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6583 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6584 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6585 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6586 	} else {
6587 		bnxt_fill_hw_rss_tbl(bp, vnic);
6588 	}
6589 
6590 	if (bp->rss_hash_delta) {
6591 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6592 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6593 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6594 		else
6595 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6596 	} else {
6597 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6598 	}
6599 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6600 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6601 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6602 }
6603 
6604 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6605 				  bool set_rss)
6606 {
6607 	struct hwrm_vnic_rss_cfg_input *req;
6608 	int rc;
6609 
6610 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6611 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6612 		return 0;
6613 
6614 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6615 	if (rc)
6616 		return rc;
6617 
6618 	if (set_rss)
6619 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6620 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6621 	return hwrm_req_send(bp, req);
6622 }
6623 
6624 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6625 				     struct bnxt_vnic_info *vnic, bool set_rss)
6626 {
6627 	struct hwrm_vnic_rss_cfg_input *req;
6628 	dma_addr_t ring_tbl_map;
6629 	u32 i, nr_ctxs;
6630 	int rc;
6631 
6632 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6633 	if (rc)
6634 		return rc;
6635 
6636 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6637 	if (!set_rss)
6638 		return hwrm_req_send(bp, req);
6639 
6640 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6641 	ring_tbl_map = vnic->rss_table_dma_addr;
6642 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6643 
6644 	hwrm_req_hold(bp, req);
6645 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6646 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6647 		req->ring_table_pair_index = i;
6648 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6649 		rc = hwrm_req_send(bp, req);
6650 		if (rc)
6651 			goto exit;
6652 	}
6653 
6654 exit:
6655 	hwrm_req_drop(bp, req);
6656 	return rc;
6657 }
6658 
6659 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6660 {
6661 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6662 	struct hwrm_vnic_rss_qcfg_output *resp;
6663 	struct hwrm_vnic_rss_qcfg_input *req;
6664 
6665 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6666 		return;
6667 
6668 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6669 	/* all contexts configured to same hash_type, zero always exists */
6670 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6671 	resp = hwrm_req_hold(bp, req);
6672 	if (!hwrm_req_send(bp, req)) {
6673 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6674 		bp->rss_hash_delta = 0;
6675 	}
6676 	hwrm_req_drop(bp, req);
6677 }
6678 
6679 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6680 {
6681 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6682 	struct hwrm_vnic_plcmodes_cfg_input *req;
6683 	int rc;
6684 
6685 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6686 	if (rc)
6687 		return rc;
6688 
6689 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6690 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6691 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6692 
6693 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6694 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6695 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6696 		req->enables |=
6697 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6698 		req->hds_threshold = cpu_to_le16(hds_thresh);
6699 	}
6700 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6701 	return hwrm_req_send(bp, req);
6702 }
6703 
6704 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6705 					struct bnxt_vnic_info *vnic,
6706 					u16 ctx_idx)
6707 {
6708 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6709 
6710 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6711 		return;
6712 
6713 	req->rss_cos_lb_ctx_id =
6714 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6715 
6716 	hwrm_req_send(bp, req);
6717 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6718 }
6719 
6720 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6721 {
6722 	int i, j;
6723 
6724 	for (i = 0; i < bp->nr_vnics; i++) {
6725 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6726 
6727 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6728 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6729 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6730 		}
6731 	}
6732 	bp->rsscos_nr_ctxs = 0;
6733 }
6734 
6735 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6736 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6737 {
6738 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6739 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6740 	int rc;
6741 
6742 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6743 	if (rc)
6744 		return rc;
6745 
6746 	resp = hwrm_req_hold(bp, req);
6747 	rc = hwrm_req_send(bp, req);
6748 	if (!rc)
6749 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6750 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6751 	hwrm_req_drop(bp, req);
6752 
6753 	return rc;
6754 }
6755 
6756 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6757 {
6758 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6759 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6760 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6761 }
6762 
6763 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6764 {
6765 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6766 	struct hwrm_vnic_cfg_input *req;
6767 	unsigned int ring = 0, grp_idx;
6768 	u16 def_vlan = 0;
6769 	int rc;
6770 
6771 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6772 	if (rc)
6773 		return rc;
6774 
6775 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6776 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6777 
6778 		req->default_rx_ring_id =
6779 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6780 		req->default_cmpl_ring_id =
6781 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6782 		req->enables =
6783 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6784 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6785 		goto vnic_mru;
6786 	}
6787 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6788 	/* Only RSS support for now TBD: COS & LB */
6789 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6790 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6791 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6792 					   VNIC_CFG_REQ_ENABLES_MRU);
6793 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6794 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6795 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6796 					   VNIC_CFG_REQ_ENABLES_MRU);
6797 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6798 	} else {
6799 		req->rss_rule = cpu_to_le16(0xffff);
6800 	}
6801 
6802 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6803 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6804 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6805 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6806 	} else {
6807 		req->cos_rule = cpu_to_le16(0xffff);
6808 	}
6809 
6810 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6811 		ring = 0;
6812 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6813 		ring = vnic->vnic_id - 1;
6814 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6815 		ring = bp->rx_nr_rings - 1;
6816 
6817 	grp_idx = bp->rx_ring[ring].bnapi->index;
6818 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6819 	req->lb_rule = cpu_to_le16(0xffff);
6820 vnic_mru:
6821 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6822 	req->mru = cpu_to_le16(vnic->mru);
6823 
6824 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6825 #ifdef CONFIG_BNXT_SRIOV
6826 	if (BNXT_VF(bp))
6827 		def_vlan = bp->vf.vlan;
6828 #endif
6829 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6830 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6831 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6832 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6833 
6834 	return hwrm_req_send(bp, req);
6835 }
6836 
6837 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6838 				    struct bnxt_vnic_info *vnic)
6839 {
6840 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6841 		struct hwrm_vnic_free_input *req;
6842 
6843 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6844 			return;
6845 
6846 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6847 
6848 		hwrm_req_send(bp, req);
6849 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6850 	}
6851 }
6852 
6853 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6854 {
6855 	u16 i;
6856 
6857 	for (i = 0; i < bp->nr_vnics; i++)
6858 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6859 }
6860 
6861 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6862 			 unsigned int start_rx_ring_idx,
6863 			 unsigned int nr_rings)
6864 {
6865 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6866 	struct hwrm_vnic_alloc_output *resp;
6867 	struct hwrm_vnic_alloc_input *req;
6868 	int rc;
6869 
6870 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6871 	if (rc)
6872 		return rc;
6873 
6874 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6875 		goto vnic_no_ring_grps;
6876 
6877 	/* map ring groups to this vnic */
6878 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6879 		grp_idx = bp->rx_ring[i].bnapi->index;
6880 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6881 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6882 				   j, nr_rings);
6883 			break;
6884 		}
6885 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6886 	}
6887 
6888 vnic_no_ring_grps:
6889 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6890 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6891 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6892 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6893 
6894 	resp = hwrm_req_hold(bp, req);
6895 	rc = hwrm_req_send(bp, req);
6896 	if (!rc)
6897 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6898 	hwrm_req_drop(bp, req);
6899 	return rc;
6900 }
6901 
6902 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6903 {
6904 	struct hwrm_vnic_qcaps_output *resp;
6905 	struct hwrm_vnic_qcaps_input *req;
6906 	int rc;
6907 
6908 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6909 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6910 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6911 	if (bp->hwrm_spec_code < 0x10600)
6912 		return 0;
6913 
6914 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6915 	if (rc)
6916 		return rc;
6917 
6918 	resp = hwrm_req_hold(bp, req);
6919 	rc = hwrm_req_send(bp, req);
6920 	if (!rc) {
6921 		u32 flags = le32_to_cpu(resp->flags);
6922 
6923 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6924 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6925 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6926 		if (flags &
6927 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6928 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6929 
6930 		/* Older P5 fw before EXT_HW_STATS support did not set
6931 		 * VLAN_STRIP_CAP properly.
6932 		 */
6933 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6934 		    (BNXT_CHIP_P5(bp) &&
6935 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6936 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6937 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6938 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6939 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6940 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6941 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6942 		if (bp->max_tpa_v2) {
6943 			if (BNXT_CHIP_P5(bp))
6944 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6945 			else
6946 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6947 		}
6948 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6949 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6950 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6951 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6952 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6953 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6954 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6955 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6956 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6957 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6958 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6959 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6960 	}
6961 	hwrm_req_drop(bp, req);
6962 	return rc;
6963 }
6964 
6965 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6966 {
6967 	struct hwrm_ring_grp_alloc_output *resp;
6968 	struct hwrm_ring_grp_alloc_input *req;
6969 	int rc;
6970 	u16 i;
6971 
6972 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6973 		return 0;
6974 
6975 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6976 	if (rc)
6977 		return rc;
6978 
6979 	resp = hwrm_req_hold(bp, req);
6980 	for (i = 0; i < bp->rx_nr_rings; i++) {
6981 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6982 
6983 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6984 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6985 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6986 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6987 
6988 		rc = hwrm_req_send(bp, req);
6989 
6990 		if (rc)
6991 			break;
6992 
6993 		bp->grp_info[grp_idx].fw_grp_id =
6994 			le32_to_cpu(resp->ring_group_id);
6995 	}
6996 	hwrm_req_drop(bp, req);
6997 	return rc;
6998 }
6999 
7000 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
7001 {
7002 	struct hwrm_ring_grp_free_input *req;
7003 	u16 i;
7004 
7005 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7006 		return;
7007 
7008 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
7009 		return;
7010 
7011 	hwrm_req_hold(bp, req);
7012 	for (i = 0; i < bp->cp_nr_rings; i++) {
7013 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7014 			continue;
7015 		req->ring_group_id =
7016 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7017 
7018 		hwrm_req_send(bp, req);
7019 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7020 	}
7021 	hwrm_req_drop(bp, req);
7022 }
7023 
7024 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7025 				       struct hwrm_ring_alloc_input *req,
7026 				       struct bnxt_ring_struct *ring)
7027 {
7028 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7029 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7030 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7031 
7032 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7033 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7034 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7035 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7036 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7037 	} else {
7038 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7039 		if (NET_IP_ALIGN == 2)
7040 			req->flags =
7041 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7042 	}
7043 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7044 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7045 	req->enables |= cpu_to_le32(enables);
7046 }
7047 
7048 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7049 				    struct bnxt_ring_struct *ring,
7050 				    u32 ring_type, u32 map_index)
7051 {
7052 	struct hwrm_ring_alloc_output *resp;
7053 	struct hwrm_ring_alloc_input *req;
7054 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7055 	struct bnxt_ring_grp_info *grp_info;
7056 	int rc, err = 0;
7057 	u16 ring_id;
7058 
7059 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7060 	if (rc)
7061 		goto exit;
7062 
7063 	req->enables = 0;
7064 	if (rmem->nr_pages > 1) {
7065 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7066 		/* Page size is in log2 units */
7067 		req->page_size = BNXT_PAGE_SHIFT;
7068 		req->page_tbl_depth = 1;
7069 	} else {
7070 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7071 	}
7072 	req->fbo = 0;
7073 	/* Association of ring index with doorbell index and MSIX number */
7074 	req->logical_id = cpu_to_le16(map_index);
7075 
7076 	switch (ring_type) {
7077 	case HWRM_RING_ALLOC_TX: {
7078 		struct bnxt_tx_ring_info *txr;
7079 		u16 flags = 0;
7080 
7081 		txr = container_of(ring, struct bnxt_tx_ring_info,
7082 				   tx_ring_struct);
7083 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7084 		/* Association of transmit ring with completion ring */
7085 		grp_info = &bp->grp_info[ring->grp_idx];
7086 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7087 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7088 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7089 		req->queue_id = cpu_to_le16(ring->queue_id);
7090 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7091 			req->cmpl_coal_cnt =
7092 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7093 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7094 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7095 		req->flags = cpu_to_le16(flags);
7096 		break;
7097 	}
7098 	case HWRM_RING_ALLOC_RX:
7099 	case HWRM_RING_ALLOC_AGG:
7100 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7101 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7102 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7103 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7104 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7105 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7106 		break;
7107 	case HWRM_RING_ALLOC_CMPL:
7108 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7109 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7110 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7111 			/* Association of cp ring with nq */
7112 			grp_info = &bp->grp_info[map_index];
7113 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7114 			req->cq_handle = cpu_to_le64(ring->handle);
7115 			req->enables |= cpu_to_le32(
7116 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7117 		} else {
7118 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7119 		}
7120 		break;
7121 	case HWRM_RING_ALLOC_NQ:
7122 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7123 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7124 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7125 		break;
7126 	default:
7127 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7128 			   ring_type);
7129 		return -EINVAL;
7130 	}
7131 
7132 	resp = hwrm_req_hold(bp, req);
7133 	rc = hwrm_req_send(bp, req);
7134 	err = le16_to_cpu(resp->error_code);
7135 	ring_id = le16_to_cpu(resp->ring_id);
7136 	hwrm_req_drop(bp, req);
7137 
7138 exit:
7139 	if (rc || err) {
7140 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7141 			   ring_type, rc, err);
7142 		return -EIO;
7143 	}
7144 	ring->fw_ring_id = ring_id;
7145 	return rc;
7146 }
7147 
7148 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7149 {
7150 	int rc;
7151 
7152 	if (BNXT_PF(bp)) {
7153 		struct hwrm_func_cfg_input *req;
7154 
7155 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7156 		if (rc)
7157 			return rc;
7158 
7159 		req->fid = cpu_to_le16(0xffff);
7160 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7161 		req->async_event_cr = cpu_to_le16(idx);
7162 		return hwrm_req_send(bp, req);
7163 	} else {
7164 		struct hwrm_func_vf_cfg_input *req;
7165 
7166 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7167 		if (rc)
7168 			return rc;
7169 
7170 		req->enables =
7171 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7172 		req->async_event_cr = cpu_to_le16(idx);
7173 		return hwrm_req_send(bp, req);
7174 	}
7175 }
7176 
7177 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7178 			     u32 ring_type)
7179 {
7180 	switch (ring_type) {
7181 	case HWRM_RING_ALLOC_TX:
7182 		db->db_ring_mask = bp->tx_ring_mask;
7183 		break;
7184 	case HWRM_RING_ALLOC_RX:
7185 		db->db_ring_mask = bp->rx_ring_mask;
7186 		break;
7187 	case HWRM_RING_ALLOC_AGG:
7188 		db->db_ring_mask = bp->rx_agg_ring_mask;
7189 		break;
7190 	case HWRM_RING_ALLOC_CMPL:
7191 	case HWRM_RING_ALLOC_NQ:
7192 		db->db_ring_mask = bp->cp_ring_mask;
7193 		break;
7194 	}
7195 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7196 		db->db_epoch_mask = db->db_ring_mask + 1;
7197 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7198 	}
7199 }
7200 
7201 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7202 			u32 map_idx, u32 xid)
7203 {
7204 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7205 		switch (ring_type) {
7206 		case HWRM_RING_ALLOC_TX:
7207 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7208 			break;
7209 		case HWRM_RING_ALLOC_RX:
7210 		case HWRM_RING_ALLOC_AGG:
7211 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7212 			break;
7213 		case HWRM_RING_ALLOC_CMPL:
7214 			db->db_key64 = DBR_PATH_L2;
7215 			break;
7216 		case HWRM_RING_ALLOC_NQ:
7217 			db->db_key64 = DBR_PATH_L2;
7218 			break;
7219 		}
7220 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7221 
7222 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7223 			db->db_key64 |= DBR_VALID;
7224 
7225 		db->doorbell = bp->bar1 + bp->db_offset;
7226 	} else {
7227 		db->doorbell = bp->bar1 + map_idx * 0x80;
7228 		switch (ring_type) {
7229 		case HWRM_RING_ALLOC_TX:
7230 			db->db_key32 = DB_KEY_TX;
7231 			break;
7232 		case HWRM_RING_ALLOC_RX:
7233 		case HWRM_RING_ALLOC_AGG:
7234 			db->db_key32 = DB_KEY_RX;
7235 			break;
7236 		case HWRM_RING_ALLOC_CMPL:
7237 			db->db_key32 = DB_KEY_CP;
7238 			break;
7239 		}
7240 	}
7241 	bnxt_set_db_mask(bp, db, ring_type);
7242 }
7243 
7244 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7245 				   struct bnxt_rx_ring_info *rxr)
7246 {
7247 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7248 	struct bnxt_napi *bnapi = rxr->bnapi;
7249 	u32 type = HWRM_RING_ALLOC_RX;
7250 	u32 map_idx = bnapi->index;
7251 	int rc;
7252 
7253 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7254 	if (rc)
7255 		return rc;
7256 
7257 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7258 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7259 
7260 	return 0;
7261 }
7262 
7263 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7264 				       struct bnxt_rx_ring_info *rxr)
7265 {
7266 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7267 	u32 type = HWRM_RING_ALLOC_AGG;
7268 	u32 grp_idx = ring->grp_idx;
7269 	u32 map_idx;
7270 	int rc;
7271 
7272 	map_idx = grp_idx + bp->rx_nr_rings;
7273 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7274 	if (rc)
7275 		return rc;
7276 
7277 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7278 		    ring->fw_ring_id);
7279 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7280 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7281 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7282 
7283 	return 0;
7284 }
7285 
7286 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7287 				      struct bnxt_cp_ring_info *cpr)
7288 {
7289 	const u32 type = HWRM_RING_ALLOC_CMPL;
7290 	struct bnxt_napi *bnapi = cpr->bnapi;
7291 	struct bnxt_ring_struct *ring;
7292 	u32 map_idx = bnapi->index;
7293 	int rc;
7294 
7295 	ring = &cpr->cp_ring_struct;
7296 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7297 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7298 	if (rc)
7299 		return rc;
7300 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7301 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7302 	return 0;
7303 }
7304 
7305 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7306 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7307 {
7308 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7309 	const u32 type = HWRM_RING_ALLOC_TX;
7310 	int rc;
7311 
7312 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7313 	if (rc)
7314 		return rc;
7315 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7316 	return 0;
7317 }
7318 
7319 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7320 {
7321 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7322 	int i, rc = 0;
7323 	u32 type;
7324 
7325 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7326 		type = HWRM_RING_ALLOC_NQ;
7327 	else
7328 		type = HWRM_RING_ALLOC_CMPL;
7329 	for (i = 0; i < bp->cp_nr_rings; i++) {
7330 		struct bnxt_napi *bnapi = bp->bnapi[i];
7331 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7332 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7333 		u32 map_idx = ring->map_idx;
7334 		unsigned int vector;
7335 
7336 		vector = bp->irq_tbl[map_idx].vector;
7337 		disable_irq_nosync(vector);
7338 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7339 		if (rc) {
7340 			enable_irq(vector);
7341 			goto err_out;
7342 		}
7343 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7344 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7345 		enable_irq(vector);
7346 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7347 
7348 		if (!i) {
7349 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7350 			if (rc)
7351 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7352 		}
7353 	}
7354 
7355 	for (i = 0; i < bp->tx_nr_rings; i++) {
7356 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7357 
7358 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7359 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7360 			if (rc)
7361 				goto err_out;
7362 		}
7363 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7364 		if (rc)
7365 			goto err_out;
7366 	}
7367 
7368 	for (i = 0; i < bp->rx_nr_rings; i++) {
7369 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7370 
7371 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7372 		if (rc)
7373 			goto err_out;
7374 		/* If we have agg rings, post agg buffers first. */
7375 		if (!agg_rings)
7376 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7377 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7378 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7379 			if (rc)
7380 				goto err_out;
7381 		}
7382 	}
7383 
7384 	if (agg_rings) {
7385 		for (i = 0; i < bp->rx_nr_rings; i++) {
7386 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7387 			if (rc)
7388 				goto err_out;
7389 		}
7390 	}
7391 err_out:
7392 	return rc;
7393 }
7394 
7395 static void bnxt_cancel_dim(struct bnxt *bp)
7396 {
7397 	int i;
7398 
7399 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7400 	 * if NAPI is enabled.
7401 	 */
7402 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7403 		return;
7404 
7405 	/* Make sure NAPI sees that the VNIC is disabled */
7406 	synchronize_net();
7407 	for (i = 0; i < bp->rx_nr_rings; i++) {
7408 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7409 		struct bnxt_napi *bnapi = rxr->bnapi;
7410 
7411 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7412 	}
7413 }
7414 
7415 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7416 				   struct bnxt_ring_struct *ring,
7417 				   u32 ring_type, int cmpl_ring_id)
7418 {
7419 	struct hwrm_ring_free_output *resp;
7420 	struct hwrm_ring_free_input *req;
7421 	u16 error_code = 0;
7422 	int rc;
7423 
7424 	if (BNXT_NO_FW_ACCESS(bp))
7425 		return 0;
7426 
7427 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7428 	if (rc)
7429 		goto exit;
7430 
7431 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7432 	req->ring_type = ring_type;
7433 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7434 
7435 	resp = hwrm_req_hold(bp, req);
7436 	rc = hwrm_req_send(bp, req);
7437 	error_code = le16_to_cpu(resp->error_code);
7438 	hwrm_req_drop(bp, req);
7439 exit:
7440 	if (rc || error_code) {
7441 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7442 			   ring_type, rc, error_code);
7443 		return -EIO;
7444 	}
7445 	return 0;
7446 }
7447 
7448 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7449 				   struct bnxt_tx_ring_info *txr,
7450 				   bool close_path)
7451 {
7452 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7453 	u32 cmpl_ring_id;
7454 
7455 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7456 		return;
7457 
7458 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7459 		       INVALID_HW_RING_ID;
7460 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7461 				cmpl_ring_id);
7462 	ring->fw_ring_id = INVALID_HW_RING_ID;
7463 }
7464 
7465 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7466 				   struct bnxt_rx_ring_info *rxr,
7467 				   bool close_path)
7468 {
7469 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7470 	u32 grp_idx = rxr->bnapi->index;
7471 	u32 cmpl_ring_id;
7472 
7473 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7474 		return;
7475 
7476 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7477 	hwrm_ring_free_send_msg(bp, ring,
7478 				RING_FREE_REQ_RING_TYPE_RX,
7479 				close_path ? cmpl_ring_id :
7480 				INVALID_HW_RING_ID);
7481 	ring->fw_ring_id = INVALID_HW_RING_ID;
7482 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7483 }
7484 
7485 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7486 				       struct bnxt_rx_ring_info *rxr,
7487 				       bool close_path)
7488 {
7489 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7490 	u32 grp_idx = rxr->bnapi->index;
7491 	u32 type, cmpl_ring_id;
7492 
7493 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7494 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7495 	else
7496 		type = RING_FREE_REQ_RING_TYPE_RX;
7497 
7498 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7499 		return;
7500 
7501 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7502 	hwrm_ring_free_send_msg(bp, ring, type,
7503 				close_path ? cmpl_ring_id :
7504 				INVALID_HW_RING_ID);
7505 	ring->fw_ring_id = INVALID_HW_RING_ID;
7506 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7507 }
7508 
7509 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7510 				   struct bnxt_cp_ring_info *cpr)
7511 {
7512 	struct bnxt_ring_struct *ring;
7513 
7514 	ring = &cpr->cp_ring_struct;
7515 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7516 		return;
7517 
7518 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7519 				INVALID_HW_RING_ID);
7520 	ring->fw_ring_id = INVALID_HW_RING_ID;
7521 }
7522 
7523 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7524 {
7525 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7526 	int i, size = ring->ring_mem.page_size;
7527 
7528 	cpr->cp_raw_cons = 0;
7529 	cpr->toggle = 0;
7530 
7531 	for (i = 0; i < bp->cp_nr_pages; i++)
7532 		if (cpr->cp_desc_ring[i])
7533 			memset(cpr->cp_desc_ring[i], 0, size);
7534 }
7535 
7536 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7537 {
7538 	u32 type;
7539 	int i;
7540 
7541 	if (!bp->bnapi)
7542 		return;
7543 
7544 	for (i = 0; i < bp->tx_nr_rings; i++)
7545 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7546 
7547 	bnxt_cancel_dim(bp);
7548 	for (i = 0; i < bp->rx_nr_rings; i++) {
7549 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7550 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7551 	}
7552 
7553 	/* The completion rings are about to be freed.  After that the
7554 	 * IRQ doorbell will not work anymore.  So we need to disable
7555 	 * IRQ here.
7556 	 */
7557 	bnxt_disable_int_sync(bp);
7558 
7559 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7560 		type = RING_FREE_REQ_RING_TYPE_NQ;
7561 	else
7562 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7563 	for (i = 0; i < bp->cp_nr_rings; i++) {
7564 		struct bnxt_napi *bnapi = bp->bnapi[i];
7565 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7566 		struct bnxt_ring_struct *ring;
7567 		int j;
7568 
7569 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7570 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7571 
7572 		ring = &cpr->cp_ring_struct;
7573 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7574 			hwrm_ring_free_send_msg(bp, ring, type,
7575 						INVALID_HW_RING_ID);
7576 			ring->fw_ring_id = INVALID_HW_RING_ID;
7577 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7578 		}
7579 	}
7580 }
7581 
7582 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7583 			     bool shared);
7584 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7585 			   bool shared);
7586 
7587 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7588 {
7589 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7590 	struct hwrm_func_qcfg_output *resp;
7591 	struct hwrm_func_qcfg_input *req;
7592 	int rc;
7593 
7594 	if (bp->hwrm_spec_code < 0x10601)
7595 		return 0;
7596 
7597 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7598 	if (rc)
7599 		return rc;
7600 
7601 	req->fid = cpu_to_le16(0xffff);
7602 	resp = hwrm_req_hold(bp, req);
7603 	rc = hwrm_req_send(bp, req);
7604 	if (rc) {
7605 		hwrm_req_drop(bp, req);
7606 		return rc;
7607 	}
7608 
7609 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7610 	if (BNXT_NEW_RM(bp)) {
7611 		u16 cp, stats;
7612 
7613 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7614 		hw_resc->resv_hw_ring_grps =
7615 			le32_to_cpu(resp->alloc_hw_ring_grps);
7616 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7617 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7618 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7619 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7620 		hw_resc->resv_irqs = cp;
7621 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7622 			int rx = hw_resc->resv_rx_rings;
7623 			int tx = hw_resc->resv_tx_rings;
7624 
7625 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7626 				rx >>= 1;
7627 			if (cp < (rx + tx)) {
7628 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7629 				if (rc)
7630 					goto get_rings_exit;
7631 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7632 					rx <<= 1;
7633 				hw_resc->resv_rx_rings = rx;
7634 				hw_resc->resv_tx_rings = tx;
7635 			}
7636 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7637 			hw_resc->resv_hw_ring_grps = rx;
7638 		}
7639 		hw_resc->resv_cp_rings = cp;
7640 		hw_resc->resv_stat_ctxs = stats;
7641 	}
7642 get_rings_exit:
7643 	hwrm_req_drop(bp, req);
7644 	return rc;
7645 }
7646 
7647 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7648 {
7649 	struct hwrm_func_qcfg_output *resp;
7650 	struct hwrm_func_qcfg_input *req;
7651 	int rc;
7652 
7653 	if (bp->hwrm_spec_code < 0x10601)
7654 		return 0;
7655 
7656 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7657 	if (rc)
7658 		return rc;
7659 
7660 	req->fid = cpu_to_le16(fid);
7661 	resp = hwrm_req_hold(bp, req);
7662 	rc = hwrm_req_send(bp, req);
7663 	if (!rc)
7664 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7665 
7666 	hwrm_req_drop(bp, req);
7667 	return rc;
7668 }
7669 
7670 static bool bnxt_rfs_supported(struct bnxt *bp);
7671 
7672 static struct hwrm_func_cfg_input *
7673 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7674 {
7675 	struct hwrm_func_cfg_input *req;
7676 	u32 enables = 0;
7677 
7678 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7679 		return NULL;
7680 
7681 	req->fid = cpu_to_le16(0xffff);
7682 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7683 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7684 	if (BNXT_NEW_RM(bp)) {
7685 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7686 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7687 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7688 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7689 			enables |= hwr->cp_p5 ?
7690 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7691 		} else {
7692 			enables |= hwr->cp ?
7693 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7694 			enables |= hwr->grp ?
7695 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7696 		}
7697 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7698 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7699 					  0;
7700 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7701 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7702 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7703 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7704 			req->num_msix = cpu_to_le16(hwr->cp);
7705 		} else {
7706 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7707 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7708 		}
7709 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7710 		req->num_vnics = cpu_to_le16(hwr->vnic);
7711 	}
7712 	req->enables = cpu_to_le32(enables);
7713 	return req;
7714 }
7715 
7716 static struct hwrm_func_vf_cfg_input *
7717 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7718 {
7719 	struct hwrm_func_vf_cfg_input *req;
7720 	u32 enables = 0;
7721 
7722 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7723 		return NULL;
7724 
7725 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7726 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7727 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7728 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7729 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7730 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7731 		enables |= hwr->cp_p5 ?
7732 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7733 	} else {
7734 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7735 		enables |= hwr->grp ?
7736 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7737 	}
7738 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7739 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7740 
7741 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7742 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7743 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7744 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7745 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7746 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7747 	} else {
7748 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7749 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7750 	}
7751 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7752 	req->num_vnics = cpu_to_le16(hwr->vnic);
7753 
7754 	req->enables = cpu_to_le32(enables);
7755 	return req;
7756 }
7757 
7758 static int
7759 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7760 {
7761 	struct hwrm_func_cfg_input *req;
7762 	int rc;
7763 
7764 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7765 	if (!req)
7766 		return -ENOMEM;
7767 
7768 	if (!req->enables) {
7769 		hwrm_req_drop(bp, req);
7770 		return 0;
7771 	}
7772 
7773 	rc = hwrm_req_send(bp, req);
7774 	if (rc)
7775 		return rc;
7776 
7777 	if (bp->hwrm_spec_code < 0x10601)
7778 		bp->hw_resc.resv_tx_rings = hwr->tx;
7779 
7780 	return bnxt_hwrm_get_rings(bp);
7781 }
7782 
7783 static int
7784 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7785 {
7786 	struct hwrm_func_vf_cfg_input *req;
7787 	int rc;
7788 
7789 	if (!BNXT_NEW_RM(bp)) {
7790 		bp->hw_resc.resv_tx_rings = hwr->tx;
7791 		return 0;
7792 	}
7793 
7794 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7795 	if (!req)
7796 		return -ENOMEM;
7797 
7798 	rc = hwrm_req_send(bp, req);
7799 	if (rc)
7800 		return rc;
7801 
7802 	return bnxt_hwrm_get_rings(bp);
7803 }
7804 
7805 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7806 {
7807 	if (BNXT_PF(bp))
7808 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7809 	else
7810 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7811 }
7812 
7813 int bnxt_nq_rings_in_use(struct bnxt *bp)
7814 {
7815 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7816 }
7817 
7818 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7819 {
7820 	int cp;
7821 
7822 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7823 		return bnxt_nq_rings_in_use(bp);
7824 
7825 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7826 	return cp;
7827 }
7828 
7829 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7830 {
7831 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7832 }
7833 
7834 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7835 {
7836 	if (!hwr->grp)
7837 		return 0;
7838 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7839 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7840 
7841 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7842 			rss_ctx *= hwr->vnic;
7843 		return rss_ctx;
7844 	}
7845 	if (BNXT_VF(bp))
7846 		return BNXT_VF_MAX_RSS_CTX;
7847 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7848 		return hwr->grp + 1;
7849 	return 1;
7850 }
7851 
7852 /* Check if a default RSS map needs to be setup.  This function is only
7853  * used on older firmware that does not require reserving RX rings.
7854  */
7855 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7856 {
7857 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7858 
7859 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7860 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7861 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7862 		if (!netif_is_rxfh_configured(bp->dev))
7863 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7864 	}
7865 }
7866 
7867 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7868 {
7869 	if (bp->flags & BNXT_FLAG_RFS) {
7870 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7871 			return 2 + bp->num_rss_ctx;
7872 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7873 			return rx_rings + 1;
7874 	}
7875 	return 1;
7876 }
7877 
7878 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7879 {
7880 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7881 	int cp = bnxt_cp_rings_in_use(bp);
7882 	int nq = bnxt_nq_rings_in_use(bp);
7883 	int rx = bp->rx_nr_rings, stat;
7884 	int vnic, grp = rx;
7885 
7886 	/* Old firmware does not need RX ring reservations but we still
7887 	 * need to setup a default RSS map when needed.  With new firmware
7888 	 * we go through RX ring reservations first and then set up the
7889 	 * RSS map for the successfully reserved RX rings when needed.
7890 	 */
7891 	if (!BNXT_NEW_RM(bp))
7892 		bnxt_check_rss_tbl_no_rmgr(bp);
7893 
7894 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7895 	    bp->hwrm_spec_code >= 0x10601)
7896 		return true;
7897 
7898 	if (!BNXT_NEW_RM(bp))
7899 		return false;
7900 
7901 	vnic = bnxt_get_total_vnics(bp, rx);
7902 
7903 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7904 		rx <<= 1;
7905 	stat = bnxt_get_func_stat_ctxs(bp);
7906 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7907 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7908 	    (hw_resc->resv_hw_ring_grps != grp &&
7909 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7910 		return true;
7911 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7912 	    hw_resc->resv_irqs != nq)
7913 		return true;
7914 	return false;
7915 }
7916 
7917 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7918 {
7919 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7920 
7921 	hwr->tx = hw_resc->resv_tx_rings;
7922 	if (BNXT_NEW_RM(bp)) {
7923 		hwr->rx = hw_resc->resv_rx_rings;
7924 		hwr->cp = hw_resc->resv_irqs;
7925 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7926 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7927 		hwr->grp = hw_resc->resv_hw_ring_grps;
7928 		hwr->vnic = hw_resc->resv_vnics;
7929 		hwr->stat = hw_resc->resv_stat_ctxs;
7930 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7931 	}
7932 }
7933 
7934 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7935 {
7936 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7937 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7938 }
7939 
7940 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7941 
7942 static int __bnxt_reserve_rings(struct bnxt *bp)
7943 {
7944 	struct bnxt_hw_rings hwr = {0};
7945 	int rx_rings, old_rx_rings, rc;
7946 	int cp = bp->cp_nr_rings;
7947 	int ulp_msix = 0;
7948 	bool sh = false;
7949 	int tx_cp;
7950 
7951 	if (!bnxt_need_reserve_rings(bp))
7952 		return 0;
7953 
7954 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7955 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7956 		if (!ulp_msix)
7957 			bnxt_set_ulp_stat_ctxs(bp, 0);
7958 
7959 		if (ulp_msix > bp->ulp_num_msix_want)
7960 			ulp_msix = bp->ulp_num_msix_want;
7961 		hwr.cp = cp + ulp_msix;
7962 	} else {
7963 		hwr.cp = bnxt_nq_rings_in_use(bp);
7964 	}
7965 
7966 	hwr.tx = bp->tx_nr_rings;
7967 	hwr.rx = bp->rx_nr_rings;
7968 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7969 		sh = true;
7970 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7971 		hwr.cp_p5 = hwr.rx + hwr.tx;
7972 
7973 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7974 
7975 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7976 		hwr.rx <<= 1;
7977 	hwr.grp = bp->rx_nr_rings;
7978 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7979 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7980 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7981 
7982 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7983 	if (rc)
7984 		return rc;
7985 
7986 	bnxt_copy_reserved_rings(bp, &hwr);
7987 
7988 	rx_rings = hwr.rx;
7989 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7990 		if (hwr.rx >= 2) {
7991 			rx_rings = hwr.rx >> 1;
7992 		} else {
7993 			if (netif_running(bp->dev))
7994 				return -ENOMEM;
7995 
7996 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7997 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7998 			bp->dev->hw_features &= ~NETIF_F_LRO;
7999 			bp->dev->features &= ~NETIF_F_LRO;
8000 			bnxt_set_ring_params(bp);
8001 		}
8002 	}
8003 	rx_rings = min_t(int, rx_rings, hwr.grp);
8004 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
8005 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
8006 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
8007 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
8008 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
8009 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8010 		hwr.rx = rx_rings << 1;
8011 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8012 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8013 	bp->tx_nr_rings = hwr.tx;
8014 
8015 	/* If we cannot reserve all the RX rings, reset the RSS map only
8016 	 * if absolutely necessary
8017 	 */
8018 	if (rx_rings != bp->rx_nr_rings) {
8019 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8020 			    rx_rings, bp->rx_nr_rings);
8021 		if (netif_is_rxfh_configured(bp->dev) &&
8022 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8023 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8024 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8025 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8026 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8027 		}
8028 	}
8029 	bp->rx_nr_rings = rx_rings;
8030 	bp->cp_nr_rings = hwr.cp;
8031 
8032 	if (!bnxt_rings_ok(bp, &hwr))
8033 		return -ENOMEM;
8034 
8035 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8036 	    !netif_is_rxfh_configured(bp->dev))
8037 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8038 
8039 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8040 		int resv_msix, resv_ctx, ulp_ctxs;
8041 		struct bnxt_hw_resc *hw_resc;
8042 
8043 		hw_resc = &bp->hw_resc;
8044 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8045 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8046 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8047 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8048 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8049 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8050 	}
8051 
8052 	return rc;
8053 }
8054 
8055 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8056 {
8057 	struct hwrm_func_vf_cfg_input *req;
8058 	u32 flags;
8059 
8060 	if (!BNXT_NEW_RM(bp))
8061 		return 0;
8062 
8063 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8064 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8065 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8066 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8067 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8068 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8069 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8070 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8071 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8072 
8073 	req->flags = cpu_to_le32(flags);
8074 	return hwrm_req_send_silent(bp, req);
8075 }
8076 
8077 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8078 {
8079 	struct hwrm_func_cfg_input *req;
8080 	u32 flags;
8081 
8082 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8083 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8084 	if (BNXT_NEW_RM(bp)) {
8085 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8086 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8087 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8088 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8089 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8090 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8091 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8092 		else
8093 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8094 	}
8095 
8096 	req->flags = cpu_to_le32(flags);
8097 	return hwrm_req_send_silent(bp, req);
8098 }
8099 
8100 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8101 {
8102 	if (bp->hwrm_spec_code < 0x10801)
8103 		return 0;
8104 
8105 	if (BNXT_PF(bp))
8106 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8107 
8108 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8109 }
8110 
8111 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8112 {
8113 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8114 	struct hwrm_ring_aggint_qcaps_output *resp;
8115 	struct hwrm_ring_aggint_qcaps_input *req;
8116 	int rc;
8117 
8118 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8119 	coal_cap->num_cmpl_dma_aggr_max = 63;
8120 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8121 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8122 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8123 	coal_cap->int_lat_tmr_min_max = 65535;
8124 	coal_cap->int_lat_tmr_max_max = 65535;
8125 	coal_cap->num_cmpl_aggr_int_max = 65535;
8126 	coal_cap->timer_units = 80;
8127 
8128 	if (bp->hwrm_spec_code < 0x10902)
8129 		return;
8130 
8131 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8132 		return;
8133 
8134 	resp = hwrm_req_hold(bp, req);
8135 	rc = hwrm_req_send_silent(bp, req);
8136 	if (!rc) {
8137 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8138 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8139 		coal_cap->num_cmpl_dma_aggr_max =
8140 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8141 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8142 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8143 		coal_cap->cmpl_aggr_dma_tmr_max =
8144 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8145 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8146 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8147 		coal_cap->int_lat_tmr_min_max =
8148 			le16_to_cpu(resp->int_lat_tmr_min_max);
8149 		coal_cap->int_lat_tmr_max_max =
8150 			le16_to_cpu(resp->int_lat_tmr_max_max);
8151 		coal_cap->num_cmpl_aggr_int_max =
8152 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8153 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8154 	}
8155 	hwrm_req_drop(bp, req);
8156 }
8157 
8158 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8159 {
8160 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8161 
8162 	return usec * 1000 / coal_cap->timer_units;
8163 }
8164 
8165 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8166 	struct bnxt_coal *hw_coal,
8167 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8168 {
8169 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8170 	u16 val, tmr, max, flags = hw_coal->flags;
8171 	u32 cmpl_params = coal_cap->cmpl_params;
8172 
8173 	max = hw_coal->bufs_per_record * 128;
8174 	if (hw_coal->budget)
8175 		max = hw_coal->bufs_per_record * hw_coal->budget;
8176 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8177 
8178 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8179 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8180 
8181 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8182 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8183 
8184 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8185 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8186 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8187 
8188 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8189 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8190 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8191 
8192 	/* min timer set to 1/2 of interrupt timer */
8193 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8194 		val = tmr / 2;
8195 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8196 		req->int_lat_tmr_min = cpu_to_le16(val);
8197 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8198 	}
8199 
8200 	/* buf timer set to 1/4 of interrupt timer */
8201 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8202 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8203 
8204 	if (cmpl_params &
8205 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8206 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8207 		val = clamp_t(u16, tmr, 1,
8208 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8209 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8210 		req->enables |=
8211 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8212 	}
8213 
8214 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8215 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8216 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8217 	req->flags = cpu_to_le16(flags);
8218 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8219 }
8220 
8221 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8222 				   struct bnxt_coal *hw_coal)
8223 {
8224 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8225 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8226 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8227 	u32 nq_params = coal_cap->nq_params;
8228 	u16 tmr;
8229 	int rc;
8230 
8231 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8232 		return 0;
8233 
8234 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8235 	if (rc)
8236 		return rc;
8237 
8238 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8239 	req->flags =
8240 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8241 
8242 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8243 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8244 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8245 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8246 	return hwrm_req_send(bp, req);
8247 }
8248 
8249 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8250 {
8251 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8252 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8253 	struct bnxt_coal coal;
8254 	int rc;
8255 
8256 	/* Tick values in micro seconds.
8257 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8258 	 */
8259 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8260 
8261 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8262 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8263 
8264 	if (!bnapi->rx_ring)
8265 		return -ENODEV;
8266 
8267 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8268 	if (rc)
8269 		return rc;
8270 
8271 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8272 
8273 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8274 
8275 	return hwrm_req_send(bp, req_rx);
8276 }
8277 
8278 static int
8279 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8280 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8281 {
8282 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8283 
8284 	req->ring_id = cpu_to_le16(ring_id);
8285 	return hwrm_req_send(bp, req);
8286 }
8287 
8288 static int
8289 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8290 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8291 {
8292 	struct bnxt_tx_ring_info *txr;
8293 	int i, rc;
8294 
8295 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8296 		u16 ring_id;
8297 
8298 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8299 		req->ring_id = cpu_to_le16(ring_id);
8300 		rc = hwrm_req_send(bp, req);
8301 		if (rc)
8302 			return rc;
8303 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8304 			return 0;
8305 	}
8306 	return 0;
8307 }
8308 
8309 int bnxt_hwrm_set_coal(struct bnxt *bp)
8310 {
8311 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8312 	int i, rc;
8313 
8314 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8315 	if (rc)
8316 		return rc;
8317 
8318 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8319 	if (rc) {
8320 		hwrm_req_drop(bp, req_rx);
8321 		return rc;
8322 	}
8323 
8324 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8325 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8326 
8327 	hwrm_req_hold(bp, req_rx);
8328 	hwrm_req_hold(bp, req_tx);
8329 	for (i = 0; i < bp->cp_nr_rings; i++) {
8330 		struct bnxt_napi *bnapi = bp->bnapi[i];
8331 		struct bnxt_coal *hw_coal;
8332 
8333 		if (!bnapi->rx_ring)
8334 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8335 		else
8336 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8337 		if (rc)
8338 			break;
8339 
8340 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8341 			continue;
8342 
8343 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8344 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8345 			if (rc)
8346 				break;
8347 		}
8348 		if (bnapi->rx_ring)
8349 			hw_coal = &bp->rx_coal;
8350 		else
8351 			hw_coal = &bp->tx_coal;
8352 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8353 	}
8354 	hwrm_req_drop(bp, req_rx);
8355 	hwrm_req_drop(bp, req_tx);
8356 	return rc;
8357 }
8358 
8359 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8360 {
8361 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8362 	struct hwrm_stat_ctx_free_input *req;
8363 	int i;
8364 
8365 	if (!bp->bnapi)
8366 		return;
8367 
8368 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8369 		return;
8370 
8371 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8372 		return;
8373 	if (BNXT_FW_MAJ(bp) <= 20) {
8374 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8375 			hwrm_req_drop(bp, req);
8376 			return;
8377 		}
8378 		hwrm_req_hold(bp, req0);
8379 	}
8380 	hwrm_req_hold(bp, req);
8381 	for (i = 0; i < bp->cp_nr_rings; i++) {
8382 		struct bnxt_napi *bnapi = bp->bnapi[i];
8383 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8384 
8385 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8386 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8387 			if (req0) {
8388 				req0->stat_ctx_id = req->stat_ctx_id;
8389 				hwrm_req_send(bp, req0);
8390 			}
8391 			hwrm_req_send(bp, req);
8392 
8393 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8394 		}
8395 	}
8396 	hwrm_req_drop(bp, req);
8397 	if (req0)
8398 		hwrm_req_drop(bp, req0);
8399 }
8400 
8401 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8402 {
8403 	struct hwrm_stat_ctx_alloc_output *resp;
8404 	struct hwrm_stat_ctx_alloc_input *req;
8405 	int rc, i;
8406 
8407 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8408 		return 0;
8409 
8410 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8411 	if (rc)
8412 		return rc;
8413 
8414 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8415 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8416 
8417 	resp = hwrm_req_hold(bp, req);
8418 	for (i = 0; i < bp->cp_nr_rings; i++) {
8419 		struct bnxt_napi *bnapi = bp->bnapi[i];
8420 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8421 
8422 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8423 
8424 		rc = hwrm_req_send(bp, req);
8425 		if (rc)
8426 			break;
8427 
8428 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8429 
8430 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8431 	}
8432 	hwrm_req_drop(bp, req);
8433 	return rc;
8434 }
8435 
8436 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8437 {
8438 	struct hwrm_func_qcfg_output *resp;
8439 	struct hwrm_func_qcfg_input *req;
8440 	u16 flags;
8441 	int rc;
8442 
8443 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8444 	if (rc)
8445 		return rc;
8446 
8447 	req->fid = cpu_to_le16(0xffff);
8448 	resp = hwrm_req_hold(bp, req);
8449 	rc = hwrm_req_send(bp, req);
8450 	if (rc)
8451 		goto func_qcfg_exit;
8452 
8453 	flags = le16_to_cpu(resp->flags);
8454 #ifdef CONFIG_BNXT_SRIOV
8455 	if (BNXT_VF(bp)) {
8456 		struct bnxt_vf_info *vf = &bp->vf;
8457 
8458 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8459 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8460 			vf->flags |= BNXT_VF_TRUST;
8461 		else
8462 			vf->flags &= ~BNXT_VF_TRUST;
8463 	} else {
8464 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8465 	}
8466 #endif
8467 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8468 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8469 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8470 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8471 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8472 	}
8473 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8474 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8475 
8476 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8477 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8478 
8479 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8480 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8481 
8482 	switch (resp->port_partition_type) {
8483 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8484 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8485 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8486 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8487 		bp->port_partition_type = resp->port_partition_type;
8488 		break;
8489 	}
8490 	if (bp->hwrm_spec_code < 0x10707 ||
8491 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8492 		bp->br_mode = BRIDGE_MODE_VEB;
8493 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8494 		bp->br_mode = BRIDGE_MODE_VEPA;
8495 	else
8496 		bp->br_mode = BRIDGE_MODE_UNDEF;
8497 
8498 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8499 	if (!bp->max_mtu)
8500 		bp->max_mtu = BNXT_MAX_MTU;
8501 
8502 	if (bp->db_size)
8503 		goto func_qcfg_exit;
8504 
8505 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8506 	if (BNXT_CHIP_P5(bp)) {
8507 		if (BNXT_PF(bp))
8508 			bp->db_offset = DB_PF_OFFSET_P5;
8509 		else
8510 			bp->db_offset = DB_VF_OFFSET_P5;
8511 	}
8512 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8513 				 1024);
8514 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8515 	    bp->db_size <= bp->db_offset)
8516 		bp->db_size = pci_resource_len(bp->pdev, 2);
8517 
8518 func_qcfg_exit:
8519 	hwrm_req_drop(bp, req);
8520 	return rc;
8521 }
8522 
8523 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8524 				      u8 init_val, u8 init_offset,
8525 				      bool init_mask_set)
8526 {
8527 	ctxm->init_value = init_val;
8528 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8529 	if (init_mask_set)
8530 		ctxm->init_offset = init_offset * 4;
8531 	else
8532 		ctxm->init_value = 0;
8533 }
8534 
8535 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8536 {
8537 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8538 	u16 type;
8539 
8540 	for (type = 0; type < ctx_max; type++) {
8541 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8542 		int n = 1;
8543 
8544 		if (!ctxm->max_entries || ctxm->pg_info)
8545 			continue;
8546 
8547 		if (ctxm->instance_bmap)
8548 			n = hweight32(ctxm->instance_bmap);
8549 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8550 		if (!ctxm->pg_info)
8551 			return -ENOMEM;
8552 	}
8553 	return 0;
8554 }
8555 
8556 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8557 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8558 
8559 #define BNXT_CTX_INIT_VALID(flags)	\
8560 	(!!((flags) &			\
8561 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8562 
8563 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8564 {
8565 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8566 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8567 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8568 	u16 type;
8569 	int rc;
8570 
8571 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8572 	if (rc)
8573 		return rc;
8574 
8575 	if (!ctx) {
8576 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8577 		if (!ctx)
8578 			return -ENOMEM;
8579 		bp->ctx = ctx;
8580 	}
8581 
8582 	resp = hwrm_req_hold(bp, req);
8583 
8584 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8585 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8586 		u8 init_val, init_off, i;
8587 		u32 max_entries;
8588 		u16 entry_size;
8589 		__le32 *p;
8590 		u32 flags;
8591 
8592 		req->type = cpu_to_le16(type);
8593 		rc = hwrm_req_send(bp, req);
8594 		if (rc)
8595 			goto ctx_done;
8596 		flags = le32_to_cpu(resp->flags);
8597 		type = le16_to_cpu(resp->next_valid_type);
8598 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8599 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8600 			continue;
8601 		}
8602 		entry_size = le16_to_cpu(resp->entry_size);
8603 		max_entries = le32_to_cpu(resp->max_num_entries);
8604 		if (ctxm->mem_valid) {
8605 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8606 			    ctxm->entry_size != entry_size ||
8607 			    ctxm->max_entries != max_entries)
8608 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8609 			else
8610 				continue;
8611 		}
8612 		ctxm->type = le16_to_cpu(resp->type);
8613 		ctxm->entry_size = entry_size;
8614 		ctxm->flags = flags;
8615 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8616 		ctxm->entry_multiple = resp->entry_multiple;
8617 		ctxm->max_entries = max_entries;
8618 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8619 		init_val = resp->ctx_init_value;
8620 		init_off = resp->ctx_init_offset;
8621 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8622 					  BNXT_CTX_INIT_VALID(flags));
8623 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8624 					      BNXT_MAX_SPLIT_ENTRY);
8625 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8626 		     i++, p++)
8627 			ctxm->split[i] = le32_to_cpu(*p);
8628 	}
8629 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8630 
8631 ctx_done:
8632 	hwrm_req_drop(bp, req);
8633 	return rc;
8634 }
8635 
8636 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8637 {
8638 	struct hwrm_func_backing_store_qcaps_output *resp;
8639 	struct hwrm_func_backing_store_qcaps_input *req;
8640 	int rc;
8641 
8642 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8643 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8644 		return 0;
8645 
8646 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8647 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8648 
8649 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8650 	if (rc)
8651 		return rc;
8652 
8653 	resp = hwrm_req_hold(bp, req);
8654 	rc = hwrm_req_send_silent(bp, req);
8655 	if (!rc) {
8656 		struct bnxt_ctx_mem_type *ctxm;
8657 		struct bnxt_ctx_mem_info *ctx;
8658 		u8 init_val, init_idx = 0;
8659 		u16 init_mask;
8660 
8661 		ctx = bp->ctx;
8662 		if (!ctx) {
8663 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8664 			if (!ctx) {
8665 				rc = -ENOMEM;
8666 				goto ctx_err;
8667 			}
8668 			bp->ctx = ctx;
8669 		}
8670 		init_val = resp->ctx_kind_initializer;
8671 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8672 
8673 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8674 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8675 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8676 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8677 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8678 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8679 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8680 					  (init_mask & (1 << init_idx++)) != 0);
8681 
8682 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8683 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8684 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8685 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8686 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8687 					  (init_mask & (1 << init_idx++)) != 0);
8688 
8689 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8690 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8691 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8692 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8693 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8694 					  (init_mask & (1 << init_idx++)) != 0);
8695 
8696 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8697 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8698 		ctxm->max_entries = ctxm->vnic_entries +
8699 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8700 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8701 		bnxt_init_ctx_initializer(ctxm, init_val,
8702 					  resp->vnic_init_offset,
8703 					  (init_mask & (1 << init_idx++)) != 0);
8704 
8705 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8706 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8707 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8708 		bnxt_init_ctx_initializer(ctxm, init_val,
8709 					  resp->stat_init_offset,
8710 					  (init_mask & (1 << init_idx++)) != 0);
8711 
8712 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8713 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8714 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8715 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8716 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8717 		if (!ctxm->entry_multiple)
8718 			ctxm->entry_multiple = 1;
8719 
8720 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8721 
8722 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8723 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8724 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8725 		ctxm->mrav_num_entries_units =
8726 			le16_to_cpu(resp->mrav_num_entries_units);
8727 		bnxt_init_ctx_initializer(ctxm, init_val,
8728 					  resp->mrav_init_offset,
8729 					  (init_mask & (1 << init_idx++)) != 0);
8730 
8731 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8732 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8733 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8734 
8735 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8736 		if (!ctx->tqm_fp_rings_count)
8737 			ctx->tqm_fp_rings_count = bp->max_q;
8738 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8739 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8740 
8741 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8742 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8743 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8744 
8745 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8746 	} else {
8747 		rc = 0;
8748 	}
8749 ctx_err:
8750 	hwrm_req_drop(bp, req);
8751 	return rc;
8752 }
8753 
8754 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8755 				  __le64 *pg_dir)
8756 {
8757 	if (!rmem->nr_pages)
8758 		return;
8759 
8760 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8761 	if (rmem->depth >= 1) {
8762 		if (rmem->depth == 2)
8763 			*pg_attr |= 2;
8764 		else
8765 			*pg_attr |= 1;
8766 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8767 	} else {
8768 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8769 	}
8770 }
8771 
8772 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8773 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8774 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8775 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8776 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8777 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8778 
8779 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8780 {
8781 	struct hwrm_func_backing_store_cfg_input *req;
8782 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8783 	struct bnxt_ctx_pg_info *ctx_pg;
8784 	struct bnxt_ctx_mem_type *ctxm;
8785 	void **__req = (void **)&req;
8786 	u32 req_len = sizeof(*req);
8787 	__le32 *num_entries;
8788 	__le64 *pg_dir;
8789 	u32 flags = 0;
8790 	u8 *pg_attr;
8791 	u32 ena;
8792 	int rc;
8793 	int i;
8794 
8795 	if (!ctx)
8796 		return 0;
8797 
8798 	if (req_len > bp->hwrm_max_ext_req_len)
8799 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8800 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8801 	if (rc)
8802 		return rc;
8803 
8804 	req->enables = cpu_to_le32(enables);
8805 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8806 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8807 		ctx_pg = ctxm->pg_info;
8808 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8809 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8810 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8811 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8812 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8813 				      &req->qpc_pg_size_qpc_lvl,
8814 				      &req->qpc_page_dir);
8815 
8816 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8817 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8818 	}
8819 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8820 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8821 		ctx_pg = ctxm->pg_info;
8822 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8823 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8824 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8825 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8826 				      &req->srq_pg_size_srq_lvl,
8827 				      &req->srq_page_dir);
8828 	}
8829 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8830 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8831 		ctx_pg = ctxm->pg_info;
8832 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8833 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8834 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8835 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8836 				      &req->cq_pg_size_cq_lvl,
8837 				      &req->cq_page_dir);
8838 	}
8839 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8840 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8841 		ctx_pg = ctxm->pg_info;
8842 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8843 		req->vnic_num_ring_table_entries =
8844 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8845 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8846 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8847 				      &req->vnic_pg_size_vnic_lvl,
8848 				      &req->vnic_page_dir);
8849 	}
8850 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8851 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8852 		ctx_pg = ctxm->pg_info;
8853 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8854 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8855 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8856 				      &req->stat_pg_size_stat_lvl,
8857 				      &req->stat_page_dir);
8858 	}
8859 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8860 		u32 units;
8861 
8862 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8863 		ctx_pg = ctxm->pg_info;
8864 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8865 		units = ctxm->mrav_num_entries_units;
8866 		if (units) {
8867 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8868 			u32 entries;
8869 
8870 			num_mr = ctx_pg->entries - num_ah;
8871 			entries = ((num_mr / units) << 16) | (num_ah / units);
8872 			req->mrav_num_entries = cpu_to_le32(entries);
8873 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8874 		}
8875 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8876 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8877 				      &req->mrav_pg_size_mrav_lvl,
8878 				      &req->mrav_page_dir);
8879 	}
8880 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8881 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8882 		ctx_pg = ctxm->pg_info;
8883 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8884 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8885 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8886 				      &req->tim_pg_size_tim_lvl,
8887 				      &req->tim_page_dir);
8888 	}
8889 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8890 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8891 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8892 	     pg_dir = &req->tqm_sp_page_dir,
8893 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8894 	     ctx_pg = ctxm->pg_info;
8895 	     i < BNXT_MAX_TQM_RINGS;
8896 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8897 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8898 		if (!(enables & ena))
8899 			continue;
8900 
8901 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8902 		*num_entries = cpu_to_le32(ctx_pg->entries);
8903 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8904 	}
8905 	req->flags = cpu_to_le32(flags);
8906 	return hwrm_req_send(bp, req);
8907 }
8908 
8909 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8910 				  struct bnxt_ctx_pg_info *ctx_pg)
8911 {
8912 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8913 
8914 	rmem->page_size = BNXT_PAGE_SIZE;
8915 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8916 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8917 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8918 	if (rmem->depth >= 1)
8919 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8920 	return bnxt_alloc_ring(bp, rmem);
8921 }
8922 
8923 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8924 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8925 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8926 {
8927 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8928 	int rc;
8929 
8930 	if (!mem_size)
8931 		return -EINVAL;
8932 
8933 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8934 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8935 		ctx_pg->nr_pages = 0;
8936 		return -EINVAL;
8937 	}
8938 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8939 		int nr_tbls, i;
8940 
8941 		rmem->depth = 2;
8942 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8943 					     GFP_KERNEL);
8944 		if (!ctx_pg->ctx_pg_tbl)
8945 			return -ENOMEM;
8946 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8947 		rmem->nr_pages = nr_tbls;
8948 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8949 		if (rc)
8950 			return rc;
8951 		for (i = 0; i < nr_tbls; i++) {
8952 			struct bnxt_ctx_pg_info *pg_tbl;
8953 
8954 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8955 			if (!pg_tbl)
8956 				return -ENOMEM;
8957 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8958 			rmem = &pg_tbl->ring_mem;
8959 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8960 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8961 			rmem->depth = 1;
8962 			rmem->nr_pages = MAX_CTX_PAGES;
8963 			rmem->ctx_mem = ctxm;
8964 			if (i == (nr_tbls - 1)) {
8965 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8966 
8967 				if (rem)
8968 					rmem->nr_pages = rem;
8969 			}
8970 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8971 			if (rc)
8972 				break;
8973 		}
8974 	} else {
8975 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8976 		if (rmem->nr_pages > 1 || depth)
8977 			rmem->depth = 1;
8978 		rmem->ctx_mem = ctxm;
8979 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8980 	}
8981 	return rc;
8982 }
8983 
8984 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8985 				    struct bnxt_ctx_pg_info *ctx_pg,
8986 				    void *buf, size_t offset, size_t head,
8987 				    size_t tail)
8988 {
8989 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8990 	size_t nr_pages = ctx_pg->nr_pages;
8991 	int page_size = rmem->page_size;
8992 	size_t len = 0, total_len = 0;
8993 	u16 depth = rmem->depth;
8994 
8995 	tail %= nr_pages * page_size;
8996 	do {
8997 		if (depth > 1) {
8998 			int i = head / (page_size * MAX_CTX_PAGES);
8999 			struct bnxt_ctx_pg_info *pg_tbl;
9000 
9001 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9002 			rmem = &pg_tbl->ring_mem;
9003 		}
9004 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
9005 		head += len;
9006 		offset += len;
9007 		total_len += len;
9008 		if (head >= nr_pages * page_size)
9009 			head = 0;
9010 	} while (head != tail);
9011 	return total_len;
9012 }
9013 
9014 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9015 				  struct bnxt_ctx_pg_info *ctx_pg)
9016 {
9017 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9018 
9019 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9020 	    ctx_pg->ctx_pg_tbl) {
9021 		int i, nr_tbls = rmem->nr_pages;
9022 
9023 		for (i = 0; i < nr_tbls; i++) {
9024 			struct bnxt_ctx_pg_info *pg_tbl;
9025 			struct bnxt_ring_mem_info *rmem2;
9026 
9027 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9028 			if (!pg_tbl)
9029 				continue;
9030 			rmem2 = &pg_tbl->ring_mem;
9031 			bnxt_free_ring(bp, rmem2);
9032 			ctx_pg->ctx_pg_arr[i] = NULL;
9033 			kfree(pg_tbl);
9034 			ctx_pg->ctx_pg_tbl[i] = NULL;
9035 		}
9036 		kfree(ctx_pg->ctx_pg_tbl);
9037 		ctx_pg->ctx_pg_tbl = NULL;
9038 	}
9039 	bnxt_free_ring(bp, rmem);
9040 	ctx_pg->nr_pages = 0;
9041 }
9042 
9043 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9044 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9045 				   u8 pg_lvl)
9046 {
9047 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9048 	int i, rc = 0, n = 1;
9049 	u32 mem_size;
9050 
9051 	if (!ctxm->entry_size || !ctx_pg)
9052 		return -EINVAL;
9053 	if (ctxm->instance_bmap)
9054 		n = hweight32(ctxm->instance_bmap);
9055 	if (ctxm->entry_multiple)
9056 		entries = roundup(entries, ctxm->entry_multiple);
9057 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9058 	mem_size = entries * ctxm->entry_size;
9059 	for (i = 0; i < n && !rc; i++) {
9060 		ctx_pg[i].entries = entries;
9061 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9062 					    ctxm->init_value ? ctxm : NULL);
9063 	}
9064 	if (!rc)
9065 		ctxm->mem_valid = 1;
9066 	return rc;
9067 }
9068 
9069 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9070 					       struct bnxt_ctx_mem_type *ctxm,
9071 					       bool last)
9072 {
9073 	struct hwrm_func_backing_store_cfg_v2_input *req;
9074 	u32 instance_bmap = ctxm->instance_bmap;
9075 	int i, j, rc = 0, n = 1;
9076 	__le32 *p;
9077 
9078 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9079 		return 0;
9080 
9081 	if (instance_bmap)
9082 		n = hweight32(ctxm->instance_bmap);
9083 	else
9084 		instance_bmap = 1;
9085 
9086 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9087 	if (rc)
9088 		return rc;
9089 	hwrm_req_hold(bp, req);
9090 	req->type = cpu_to_le16(ctxm->type);
9091 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9092 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9093 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9094 		struct bnxt_bs_trace_info *bs_trace;
9095 		u32 enables;
9096 
9097 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9098 		req->enables = cpu_to_le32(enables);
9099 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9100 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9101 	}
9102 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9103 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9104 		p[i] = cpu_to_le32(ctxm->split[i]);
9105 	for (i = 0, j = 0; j < n && !rc; i++) {
9106 		struct bnxt_ctx_pg_info *ctx_pg;
9107 
9108 		if (!(instance_bmap & (1 << i)))
9109 			continue;
9110 		req->instance = cpu_to_le16(i);
9111 		ctx_pg = &ctxm->pg_info[j++];
9112 		if (!ctx_pg->entries)
9113 			continue;
9114 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9115 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9116 				      &req->page_size_pbl_level,
9117 				      &req->page_dir);
9118 		if (last && j == n)
9119 			req->flags =
9120 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9121 		rc = hwrm_req_send(bp, req);
9122 	}
9123 	hwrm_req_drop(bp, req);
9124 	return rc;
9125 }
9126 
9127 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9128 {
9129 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9130 	struct bnxt_ctx_mem_type *ctxm;
9131 	u16 last_type = BNXT_CTX_INV;
9132 	int rc = 0;
9133 	u16 type;
9134 
9135 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9136 		ctxm = &ctx->ctx_arr[type];
9137 		if (!bnxt_bs_trace_avail(bp, type))
9138 			continue;
9139 		if (!ctxm->mem_valid) {
9140 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9141 						     ctxm->max_entries, 1);
9142 			if (rc) {
9143 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9144 					    type);
9145 				continue;
9146 			}
9147 			bnxt_bs_trace_init(bp, ctxm);
9148 		}
9149 		last_type = type;
9150 	}
9151 
9152 	if (last_type == BNXT_CTX_INV) {
9153 		if (!ena)
9154 			return 0;
9155 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9156 			last_type = BNXT_CTX_MAX - 1;
9157 		else
9158 			last_type = BNXT_CTX_L2_MAX - 1;
9159 	}
9160 	ctx->ctx_arr[last_type].last = 1;
9161 
9162 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9163 		ctxm = &ctx->ctx_arr[type];
9164 
9165 		if (!ctxm->mem_valid)
9166 			continue;
9167 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9168 		if (rc)
9169 			return rc;
9170 	}
9171 	return 0;
9172 }
9173 
9174 /**
9175  * __bnxt_copy_ctx_mem - copy host context memory
9176  * @bp: The driver context
9177  * @ctxm: The pointer to the context memory type
9178  * @buf: The destination buffer or NULL to just obtain the length
9179  * @offset: The buffer offset to copy the data to
9180  * @head: The head offset of context memory to copy from
9181  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9182  *
9183  * This function is called for debugging purposes to dump the host context
9184  * used by the chip.
9185  *
9186  * Return: Length of memory copied
9187  */
9188 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9189 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9190 				  size_t offset, size_t head, size_t tail)
9191 {
9192 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9193 	size_t len = 0, total_len = 0;
9194 	int i, n = 1;
9195 
9196 	if (!ctx_pg)
9197 		return 0;
9198 
9199 	if (ctxm->instance_bmap)
9200 		n = hweight32(ctxm->instance_bmap);
9201 	for (i = 0; i < n; i++) {
9202 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9203 					    tail);
9204 		offset += len;
9205 		total_len += len;
9206 	}
9207 	return total_len;
9208 }
9209 
9210 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9211 			 void *buf, size_t offset)
9212 {
9213 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9214 
9215 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9216 }
9217 
9218 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9219 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9220 {
9221 	struct bnxt_ctx_pg_info *ctx_pg;
9222 	int i, n = 1;
9223 
9224 	ctxm->last = 0;
9225 
9226 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9227 		return;
9228 
9229 	ctx_pg = ctxm->pg_info;
9230 	if (ctx_pg) {
9231 		if (ctxm->instance_bmap)
9232 			n = hweight32(ctxm->instance_bmap);
9233 		for (i = 0; i < n; i++)
9234 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9235 
9236 		kfree(ctx_pg);
9237 		ctxm->pg_info = NULL;
9238 		ctxm->mem_valid = 0;
9239 	}
9240 	memset(ctxm, 0, sizeof(*ctxm));
9241 }
9242 
9243 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9244 {
9245 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9246 	u16 type;
9247 
9248 	if (!ctx)
9249 		return;
9250 
9251 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9252 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9253 
9254 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9255 	if (force) {
9256 		kfree(ctx);
9257 		bp->ctx = NULL;
9258 	}
9259 }
9260 
9261 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9262 {
9263 	struct bnxt_ctx_mem_type *ctxm;
9264 	struct bnxt_ctx_mem_info *ctx;
9265 	u32 l2_qps, qp1_qps, max_qps;
9266 	u32 ena, entries_sp, entries;
9267 	u32 srqs, max_srqs, min;
9268 	u32 num_mr, num_ah;
9269 	u32 extra_srqs = 0;
9270 	u32 extra_qps = 0;
9271 	u32 fast_qpmd_qps;
9272 	u8 pg_lvl = 1;
9273 	int i, rc;
9274 
9275 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9276 	if (rc) {
9277 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9278 			   rc);
9279 		return rc;
9280 	}
9281 	ctx = bp->ctx;
9282 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9283 		return 0;
9284 
9285 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9286 	l2_qps = ctxm->qp_l2_entries;
9287 	qp1_qps = ctxm->qp_qp1_entries;
9288 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9289 	max_qps = ctxm->max_entries;
9290 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9291 	srqs = ctxm->srq_l2_entries;
9292 	max_srqs = ctxm->max_entries;
9293 	ena = 0;
9294 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9295 		pg_lvl = 2;
9296 		if (BNXT_SW_RES_LMT(bp)) {
9297 			extra_qps = max_qps - l2_qps - qp1_qps;
9298 			extra_srqs = max_srqs - srqs;
9299 		} else {
9300 			extra_qps = min_t(u32, 65536,
9301 					  max_qps - l2_qps - qp1_qps);
9302 			/* allocate extra qps if fw supports RoCE fast qp
9303 			 * destroy feature
9304 			 */
9305 			extra_qps += fast_qpmd_qps;
9306 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9307 		}
9308 		if (fast_qpmd_qps)
9309 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9310 	}
9311 
9312 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9313 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9314 				     pg_lvl);
9315 	if (rc)
9316 		return rc;
9317 
9318 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9319 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9320 	if (rc)
9321 		return rc;
9322 
9323 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9324 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9325 				     extra_qps * 2, pg_lvl);
9326 	if (rc)
9327 		return rc;
9328 
9329 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9330 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9331 	if (rc)
9332 		return rc;
9333 
9334 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9335 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9336 	if (rc)
9337 		return rc;
9338 
9339 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9340 		goto skip_rdma;
9341 
9342 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9343 	if (BNXT_SW_RES_LMT(bp) &&
9344 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9345 		num_ah = ctxm->mrav_av_entries;
9346 		num_mr = ctxm->max_entries - num_ah;
9347 	} else {
9348 		/* 128K extra is needed to accommodate static AH context
9349 		 * allocation by f/w.
9350 		 */
9351 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9352 		num_ah = min_t(u32, num_mr, 1024 * 128);
9353 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9354 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9355 			ctxm->mrav_av_entries = num_ah;
9356 	}
9357 
9358 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9359 	if (rc)
9360 		return rc;
9361 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9362 
9363 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9364 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9365 	if (rc)
9366 		return rc;
9367 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9368 
9369 skip_rdma:
9370 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9371 	min = ctxm->min_entries;
9372 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9373 		     2 * (extra_qps + qp1_qps) + min;
9374 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9375 	if (rc)
9376 		return rc;
9377 
9378 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9379 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9380 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9381 	if (rc)
9382 		return rc;
9383 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9384 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9385 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9386 
9387 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9388 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9389 	else
9390 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9391 	if (rc) {
9392 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9393 			   rc);
9394 		return rc;
9395 	}
9396 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9397 	return 0;
9398 }
9399 
9400 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9401 {
9402 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9403 	u16 page_attr;
9404 	int rc;
9405 
9406 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9407 		return 0;
9408 
9409 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9410 	if (rc)
9411 		return rc;
9412 
9413 	if (BNXT_PAGE_SIZE == 0x2000)
9414 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9415 	else if (BNXT_PAGE_SIZE == 0x10000)
9416 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9417 	else
9418 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9419 	req->pg_size_lvl = cpu_to_le16(page_attr |
9420 				       bp->fw_crash_mem->ring_mem.depth);
9421 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9422 	req->size = cpu_to_le32(bp->fw_crash_len);
9423 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9424 	return hwrm_req_send(bp, req);
9425 }
9426 
9427 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9428 {
9429 	if (bp->fw_crash_mem) {
9430 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9431 		kfree(bp->fw_crash_mem);
9432 		bp->fw_crash_mem = NULL;
9433 	}
9434 }
9435 
9436 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9437 {
9438 	u32 mem_size = 0;
9439 	int rc;
9440 
9441 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9442 		return 0;
9443 
9444 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9445 	if (rc)
9446 		return rc;
9447 
9448 	mem_size = round_up(mem_size, 4);
9449 
9450 	/* keep and use the existing pages */
9451 	if (bp->fw_crash_mem &&
9452 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9453 		goto alloc_done;
9454 
9455 	if (bp->fw_crash_mem)
9456 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9457 	else
9458 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9459 					   GFP_KERNEL);
9460 	if (!bp->fw_crash_mem)
9461 		return -ENOMEM;
9462 
9463 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9464 	if (rc) {
9465 		bnxt_free_crash_dump_mem(bp);
9466 		return rc;
9467 	}
9468 
9469 alloc_done:
9470 	bp->fw_crash_len = mem_size;
9471 	return 0;
9472 }
9473 
9474 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9475 {
9476 	struct hwrm_func_resource_qcaps_output *resp;
9477 	struct hwrm_func_resource_qcaps_input *req;
9478 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9479 	int rc;
9480 
9481 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9482 	if (rc)
9483 		return rc;
9484 
9485 	req->fid = cpu_to_le16(0xffff);
9486 	resp = hwrm_req_hold(bp, req);
9487 	rc = hwrm_req_send_silent(bp, req);
9488 	if (rc)
9489 		goto hwrm_func_resc_qcaps_exit;
9490 
9491 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9492 	if (!all)
9493 		goto hwrm_func_resc_qcaps_exit;
9494 
9495 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9496 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9497 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9498 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9499 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9500 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9501 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9502 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9503 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9504 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9505 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9506 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9507 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9508 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9509 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9510 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9511 
9512 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9513 		u16 max_msix = le16_to_cpu(resp->max_msix);
9514 
9515 		hw_resc->max_nqs = max_msix;
9516 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9517 	}
9518 
9519 	if (BNXT_PF(bp)) {
9520 		struct bnxt_pf_info *pf = &bp->pf;
9521 
9522 		pf->vf_resv_strategy =
9523 			le16_to_cpu(resp->vf_reservation_strategy);
9524 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9525 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9526 	}
9527 hwrm_func_resc_qcaps_exit:
9528 	hwrm_req_drop(bp, req);
9529 	return rc;
9530 }
9531 
9532 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9533 {
9534 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9535 	struct hwrm_port_mac_ptp_qcfg_input *req;
9536 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9537 	u8 flags;
9538 	int rc;
9539 
9540 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9541 		rc = -ENODEV;
9542 		goto no_ptp;
9543 	}
9544 
9545 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9546 	if (rc)
9547 		goto no_ptp;
9548 
9549 	req->port_id = cpu_to_le16(bp->pf.port_id);
9550 	resp = hwrm_req_hold(bp, req);
9551 	rc = hwrm_req_send(bp, req);
9552 	if (rc)
9553 		goto exit;
9554 
9555 	flags = resp->flags;
9556 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9557 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9558 		rc = -ENODEV;
9559 		goto exit;
9560 	}
9561 	if (!ptp) {
9562 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9563 		if (!ptp) {
9564 			rc = -ENOMEM;
9565 			goto exit;
9566 		}
9567 		ptp->bp = bp;
9568 		bp->ptp_cfg = ptp;
9569 	}
9570 
9571 	if (flags &
9572 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9573 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9574 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9575 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9576 	} else if (BNXT_CHIP_P5(bp)) {
9577 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9578 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9579 	} else {
9580 		rc = -ENODEV;
9581 		goto exit;
9582 	}
9583 	ptp->rtc_configured =
9584 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9585 	rc = bnxt_ptp_init(bp);
9586 	if (rc)
9587 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9588 exit:
9589 	hwrm_req_drop(bp, req);
9590 	if (!rc)
9591 		return 0;
9592 
9593 no_ptp:
9594 	bnxt_ptp_clear(bp);
9595 	kfree(ptp);
9596 	bp->ptp_cfg = NULL;
9597 	return rc;
9598 }
9599 
9600 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9601 {
9602 	struct hwrm_func_qcaps_output *resp;
9603 	struct hwrm_func_qcaps_input *req;
9604 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9605 	u32 flags, flags_ext, flags_ext2;
9606 	int rc;
9607 
9608 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9609 	if (rc)
9610 		return rc;
9611 
9612 	req->fid = cpu_to_le16(0xffff);
9613 	resp = hwrm_req_hold(bp, req);
9614 	rc = hwrm_req_send(bp, req);
9615 	if (rc)
9616 		goto hwrm_func_qcaps_exit;
9617 
9618 	flags = le32_to_cpu(resp->flags);
9619 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9620 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9621 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9622 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9623 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9624 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9625 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9626 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9627 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9628 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9629 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9630 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9631 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9632 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9633 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9634 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9635 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9636 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9637 
9638 	flags_ext = le32_to_cpu(resp->flags_ext);
9639 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9640 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9641 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9642 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9643 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9644 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9645 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9646 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9647 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9648 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9649 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9650 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9651 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9652 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9653 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9654 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9655 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9656 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9657 
9658 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9659 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9660 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9661 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9662 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9663 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9664 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9665 	if (flags_ext2 &
9666 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9667 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9668 	if (BNXT_PF(bp) &&
9669 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9670 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9671 
9672 	bp->tx_push_thresh = 0;
9673 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9674 	    BNXT_FW_MAJ(bp) > 217)
9675 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9676 
9677 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9678 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9679 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9680 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9681 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9682 	if (!hw_resc->max_hw_ring_grps)
9683 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9684 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9685 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9686 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9687 
9688 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9689 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9690 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9691 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9692 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9693 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9694 
9695 	if (BNXT_PF(bp)) {
9696 		struct bnxt_pf_info *pf = &bp->pf;
9697 
9698 		pf->fw_fid = le16_to_cpu(resp->fid);
9699 		pf->port_id = le16_to_cpu(resp->port_id);
9700 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9701 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9702 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9703 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9704 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9705 			bp->flags |= BNXT_FLAG_WOL_CAP;
9706 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9707 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9708 		} else {
9709 			bnxt_ptp_clear(bp);
9710 			kfree(bp->ptp_cfg);
9711 			bp->ptp_cfg = NULL;
9712 		}
9713 	} else {
9714 #ifdef CONFIG_BNXT_SRIOV
9715 		struct bnxt_vf_info *vf = &bp->vf;
9716 
9717 		vf->fw_fid = le16_to_cpu(resp->fid);
9718 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9719 #endif
9720 	}
9721 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9722 
9723 hwrm_func_qcaps_exit:
9724 	hwrm_req_drop(bp, req);
9725 	return rc;
9726 }
9727 
9728 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9729 {
9730 	struct hwrm_dbg_qcaps_output *resp;
9731 	struct hwrm_dbg_qcaps_input *req;
9732 	int rc;
9733 
9734 	bp->fw_dbg_cap = 0;
9735 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9736 		return;
9737 
9738 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9739 	if (rc)
9740 		return;
9741 
9742 	req->fid = cpu_to_le16(0xffff);
9743 	resp = hwrm_req_hold(bp, req);
9744 	rc = hwrm_req_send(bp, req);
9745 	if (rc)
9746 		goto hwrm_dbg_qcaps_exit;
9747 
9748 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9749 
9750 hwrm_dbg_qcaps_exit:
9751 	hwrm_req_drop(bp, req);
9752 }
9753 
9754 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9755 
9756 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9757 {
9758 	int rc;
9759 
9760 	rc = __bnxt_hwrm_func_qcaps(bp);
9761 	if (rc)
9762 		return rc;
9763 
9764 	bnxt_hwrm_dbg_qcaps(bp);
9765 
9766 	rc = bnxt_hwrm_queue_qportcfg(bp);
9767 	if (rc) {
9768 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9769 		return rc;
9770 	}
9771 	if (bp->hwrm_spec_code >= 0x10803) {
9772 		rc = bnxt_alloc_ctx_mem(bp);
9773 		if (rc)
9774 			return rc;
9775 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9776 		if (!rc)
9777 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9778 	}
9779 	return 0;
9780 }
9781 
9782 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9783 {
9784 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9785 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9786 	u32 flags;
9787 	int rc;
9788 
9789 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9790 		return 0;
9791 
9792 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9793 	if (rc)
9794 		return rc;
9795 
9796 	resp = hwrm_req_hold(bp, req);
9797 	rc = hwrm_req_send(bp, req);
9798 	if (rc)
9799 		goto hwrm_cfa_adv_qcaps_exit;
9800 
9801 	flags = le32_to_cpu(resp->flags);
9802 	if (flags &
9803 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9804 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9805 
9806 	if (flags &
9807 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9808 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9809 
9810 	if (flags &
9811 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9812 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9813 
9814 hwrm_cfa_adv_qcaps_exit:
9815 	hwrm_req_drop(bp, req);
9816 	return rc;
9817 }
9818 
9819 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9820 {
9821 	if (bp->fw_health)
9822 		return 0;
9823 
9824 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9825 	if (!bp->fw_health)
9826 		return -ENOMEM;
9827 
9828 	mutex_init(&bp->fw_health->lock);
9829 	return 0;
9830 }
9831 
9832 static int bnxt_alloc_fw_health(struct bnxt *bp)
9833 {
9834 	int rc;
9835 
9836 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9837 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9838 		return 0;
9839 
9840 	rc = __bnxt_alloc_fw_health(bp);
9841 	if (rc) {
9842 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9843 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9844 		return rc;
9845 	}
9846 
9847 	return 0;
9848 }
9849 
9850 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9851 {
9852 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9853 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9854 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9855 }
9856 
9857 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9858 {
9859 	struct bnxt_fw_health *fw_health = bp->fw_health;
9860 	u32 reg_type;
9861 
9862 	if (!fw_health)
9863 		return;
9864 
9865 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9866 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9867 		fw_health->status_reliable = false;
9868 
9869 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9870 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9871 		fw_health->resets_reliable = false;
9872 }
9873 
9874 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9875 {
9876 	void __iomem *hs;
9877 	u32 status_loc;
9878 	u32 reg_type;
9879 	u32 sig;
9880 
9881 	if (bp->fw_health)
9882 		bp->fw_health->status_reliable = false;
9883 
9884 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9885 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9886 
9887 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9888 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9889 		if (!bp->chip_num) {
9890 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9891 			bp->chip_num = readl(bp->bar0 +
9892 					     BNXT_FW_HEALTH_WIN_BASE +
9893 					     BNXT_GRC_REG_CHIP_NUM);
9894 		}
9895 		if (!BNXT_CHIP_P5_PLUS(bp))
9896 			return;
9897 
9898 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9899 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9900 	} else {
9901 		status_loc = readl(hs + offsetof(struct hcomm_status,
9902 						 fw_status_loc));
9903 	}
9904 
9905 	if (__bnxt_alloc_fw_health(bp)) {
9906 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9907 		return;
9908 	}
9909 
9910 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9911 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9912 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9913 		__bnxt_map_fw_health_reg(bp, status_loc);
9914 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9915 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9916 	}
9917 
9918 	bp->fw_health->status_reliable = true;
9919 }
9920 
9921 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9922 {
9923 	struct bnxt_fw_health *fw_health = bp->fw_health;
9924 	u32 reg_base = 0xffffffff;
9925 	int i;
9926 
9927 	bp->fw_health->status_reliable = false;
9928 	bp->fw_health->resets_reliable = false;
9929 	/* Only pre-map the monitoring GRC registers using window 3 */
9930 	for (i = 0; i < 4; i++) {
9931 		u32 reg = fw_health->regs[i];
9932 
9933 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9934 			continue;
9935 		if (reg_base == 0xffffffff)
9936 			reg_base = reg & BNXT_GRC_BASE_MASK;
9937 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9938 			return -ERANGE;
9939 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9940 	}
9941 	bp->fw_health->status_reliable = true;
9942 	bp->fw_health->resets_reliable = true;
9943 	if (reg_base == 0xffffffff)
9944 		return 0;
9945 
9946 	__bnxt_map_fw_health_reg(bp, reg_base);
9947 	return 0;
9948 }
9949 
9950 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9951 {
9952 	if (!bp->fw_health)
9953 		return;
9954 
9955 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9956 		bp->fw_health->status_reliable = true;
9957 		bp->fw_health->resets_reliable = true;
9958 	} else {
9959 		bnxt_try_map_fw_health_reg(bp);
9960 	}
9961 }
9962 
9963 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9964 {
9965 	struct bnxt_fw_health *fw_health = bp->fw_health;
9966 	struct hwrm_error_recovery_qcfg_output *resp;
9967 	struct hwrm_error_recovery_qcfg_input *req;
9968 	int rc, i;
9969 
9970 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9971 		return 0;
9972 
9973 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9974 	if (rc)
9975 		return rc;
9976 
9977 	resp = hwrm_req_hold(bp, req);
9978 	rc = hwrm_req_send(bp, req);
9979 	if (rc)
9980 		goto err_recovery_out;
9981 	fw_health->flags = le32_to_cpu(resp->flags);
9982 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9983 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9984 		rc = -EINVAL;
9985 		goto err_recovery_out;
9986 	}
9987 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9988 	fw_health->master_func_wait_dsecs =
9989 		le32_to_cpu(resp->master_func_wait_period);
9990 	fw_health->normal_func_wait_dsecs =
9991 		le32_to_cpu(resp->normal_func_wait_period);
9992 	fw_health->post_reset_wait_dsecs =
9993 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9994 	fw_health->post_reset_max_wait_dsecs =
9995 		le32_to_cpu(resp->max_bailout_time_after_reset);
9996 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9997 		le32_to_cpu(resp->fw_health_status_reg);
9998 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9999 		le32_to_cpu(resp->fw_heartbeat_reg);
10000 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
10001 		le32_to_cpu(resp->fw_reset_cnt_reg);
10002 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
10003 		le32_to_cpu(resp->reset_inprogress_reg);
10004 	fw_health->fw_reset_inprog_reg_mask =
10005 		le32_to_cpu(resp->reset_inprogress_reg_mask);
10006 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
10007 	if (fw_health->fw_reset_seq_cnt >= 16) {
10008 		rc = -EINVAL;
10009 		goto err_recovery_out;
10010 	}
10011 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10012 		fw_health->fw_reset_seq_regs[i] =
10013 			le32_to_cpu(resp->reset_reg[i]);
10014 		fw_health->fw_reset_seq_vals[i] =
10015 			le32_to_cpu(resp->reset_reg_val[i]);
10016 		fw_health->fw_reset_seq_delay_msec[i] =
10017 			resp->delay_after_reset[i];
10018 	}
10019 err_recovery_out:
10020 	hwrm_req_drop(bp, req);
10021 	if (!rc)
10022 		rc = bnxt_map_fw_health_regs(bp);
10023 	if (rc)
10024 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10025 	return rc;
10026 }
10027 
10028 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10029 {
10030 	struct hwrm_func_reset_input *req;
10031 	int rc;
10032 
10033 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10034 	if (rc)
10035 		return rc;
10036 
10037 	req->enables = 0;
10038 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10039 	return hwrm_req_send(bp, req);
10040 }
10041 
10042 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10043 {
10044 	struct hwrm_nvm_get_dev_info_output nvm_info;
10045 
10046 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10047 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10048 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10049 			 nvm_info.nvm_cfg_ver_upd);
10050 }
10051 
10052 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10053 {
10054 	struct hwrm_queue_qportcfg_output *resp;
10055 	struct hwrm_queue_qportcfg_input *req;
10056 	u8 i, j, *qptr;
10057 	bool no_rdma;
10058 	int rc = 0;
10059 
10060 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10061 	if (rc)
10062 		return rc;
10063 
10064 	resp = hwrm_req_hold(bp, req);
10065 	rc = hwrm_req_send(bp, req);
10066 	if (rc)
10067 		goto qportcfg_exit;
10068 
10069 	if (!resp->max_configurable_queues) {
10070 		rc = -EINVAL;
10071 		goto qportcfg_exit;
10072 	}
10073 	bp->max_tc = resp->max_configurable_queues;
10074 	bp->max_lltc = resp->max_configurable_lossless_queues;
10075 	if (bp->max_tc > BNXT_MAX_QUEUE)
10076 		bp->max_tc = BNXT_MAX_QUEUE;
10077 
10078 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10079 	qptr = &resp->queue_id0;
10080 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10081 		bp->q_info[j].queue_id = *qptr;
10082 		bp->q_ids[i] = *qptr++;
10083 		bp->q_info[j].queue_profile = *qptr++;
10084 		bp->tc_to_qidx[j] = j;
10085 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10086 		    (no_rdma && BNXT_PF(bp)))
10087 			j++;
10088 	}
10089 	bp->max_q = bp->max_tc;
10090 	bp->max_tc = max_t(u8, j, 1);
10091 
10092 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10093 		bp->max_tc = 1;
10094 
10095 	if (bp->max_lltc > bp->max_tc)
10096 		bp->max_lltc = bp->max_tc;
10097 
10098 qportcfg_exit:
10099 	hwrm_req_drop(bp, req);
10100 	return rc;
10101 }
10102 
10103 static int bnxt_hwrm_poll(struct bnxt *bp)
10104 {
10105 	struct hwrm_ver_get_input *req;
10106 	int rc;
10107 
10108 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10109 	if (rc)
10110 		return rc;
10111 
10112 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10113 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10114 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10115 
10116 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10117 	rc = hwrm_req_send(bp, req);
10118 	return rc;
10119 }
10120 
10121 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10122 {
10123 	struct hwrm_ver_get_output *resp;
10124 	struct hwrm_ver_get_input *req;
10125 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10126 	u32 dev_caps_cfg, hwrm_ver;
10127 	int rc, len, max_tmo_secs;
10128 
10129 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10130 	if (rc)
10131 		return rc;
10132 
10133 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10134 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10135 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10136 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10137 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10138 
10139 	resp = hwrm_req_hold(bp, req);
10140 	rc = hwrm_req_send(bp, req);
10141 	if (rc)
10142 		goto hwrm_ver_get_exit;
10143 
10144 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10145 
10146 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10147 			     resp->hwrm_intf_min_8b << 8 |
10148 			     resp->hwrm_intf_upd_8b;
10149 	if (resp->hwrm_intf_maj_8b < 1) {
10150 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10151 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10152 			    resp->hwrm_intf_upd_8b);
10153 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10154 	}
10155 
10156 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10157 			HWRM_VERSION_UPDATE;
10158 
10159 	if (bp->hwrm_spec_code > hwrm_ver)
10160 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10161 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10162 			 HWRM_VERSION_UPDATE);
10163 	else
10164 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10165 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10166 			 resp->hwrm_intf_upd_8b);
10167 
10168 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10169 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10170 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10171 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10172 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10173 		len = FW_VER_STR_LEN;
10174 	} else {
10175 		fw_maj = resp->hwrm_fw_maj_8b;
10176 		fw_min = resp->hwrm_fw_min_8b;
10177 		fw_bld = resp->hwrm_fw_bld_8b;
10178 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10179 		len = BC_HWRM_STR_LEN;
10180 	}
10181 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10182 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10183 		 fw_rsv);
10184 
10185 	if (strlen(resp->active_pkg_name)) {
10186 		int fw_ver_len = strlen(bp->fw_ver_str);
10187 
10188 		snprintf(bp->fw_ver_str + fw_ver_len,
10189 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10190 			 resp->active_pkg_name);
10191 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10192 	}
10193 
10194 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10195 	if (!bp->hwrm_cmd_timeout)
10196 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10197 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10198 	if (!bp->hwrm_cmd_max_timeout)
10199 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10200 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10201 #ifdef CONFIG_DETECT_HUNG_TASK
10202 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10203 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10204 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10205 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10206 	}
10207 #endif
10208 
10209 	if (resp->hwrm_intf_maj_8b >= 1) {
10210 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10211 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10212 	}
10213 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10214 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10215 
10216 	bp->chip_num = le16_to_cpu(resp->chip_num);
10217 	bp->chip_rev = resp->chip_rev;
10218 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10219 	    !resp->chip_metal)
10220 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10221 
10222 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10223 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10224 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10225 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10226 
10227 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10228 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10229 
10230 	if (dev_caps_cfg &
10231 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10232 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10233 
10234 	if (dev_caps_cfg &
10235 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10236 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10237 
10238 	if (dev_caps_cfg &
10239 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10240 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10241 
10242 hwrm_ver_get_exit:
10243 	hwrm_req_drop(bp, req);
10244 	return rc;
10245 }
10246 
10247 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10248 {
10249 	struct hwrm_fw_set_time_input *req;
10250 	struct tm tm;
10251 	time64_t now = ktime_get_real_seconds();
10252 	int rc;
10253 
10254 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10255 	    bp->hwrm_spec_code < 0x10400)
10256 		return -EOPNOTSUPP;
10257 
10258 	time64_to_tm(now, 0, &tm);
10259 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10260 	if (rc)
10261 		return rc;
10262 
10263 	req->year = cpu_to_le16(1900 + tm.tm_year);
10264 	req->month = 1 + tm.tm_mon;
10265 	req->day = tm.tm_mday;
10266 	req->hour = tm.tm_hour;
10267 	req->minute = tm.tm_min;
10268 	req->second = tm.tm_sec;
10269 	return hwrm_req_send(bp, req);
10270 }
10271 
10272 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10273 {
10274 	u64 sw_tmp;
10275 
10276 	hw &= mask;
10277 	sw_tmp = (*sw & ~mask) | hw;
10278 	if (hw < (*sw & mask))
10279 		sw_tmp += mask + 1;
10280 	WRITE_ONCE(*sw, sw_tmp);
10281 }
10282 
10283 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10284 				    int count, bool ignore_zero)
10285 {
10286 	int i;
10287 
10288 	for (i = 0; i < count; i++) {
10289 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10290 
10291 		if (ignore_zero && !hw)
10292 			continue;
10293 
10294 		if (masks[i] == -1ULL)
10295 			sw_stats[i] = hw;
10296 		else
10297 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10298 	}
10299 }
10300 
10301 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10302 {
10303 	if (!stats->hw_stats)
10304 		return;
10305 
10306 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10307 				stats->hw_masks, stats->len / 8, false);
10308 }
10309 
10310 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10311 {
10312 	struct bnxt_stats_mem *ring0_stats;
10313 	bool ignore_zero = false;
10314 	int i;
10315 
10316 	/* Chip bug.  Counter intermittently becomes 0. */
10317 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10318 		ignore_zero = true;
10319 
10320 	for (i = 0; i < bp->cp_nr_rings; i++) {
10321 		struct bnxt_napi *bnapi = bp->bnapi[i];
10322 		struct bnxt_cp_ring_info *cpr;
10323 		struct bnxt_stats_mem *stats;
10324 
10325 		cpr = &bnapi->cp_ring;
10326 		stats = &cpr->stats;
10327 		if (!i)
10328 			ring0_stats = stats;
10329 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10330 					ring0_stats->hw_masks,
10331 					ring0_stats->len / 8, ignore_zero);
10332 	}
10333 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10334 		struct bnxt_stats_mem *stats = &bp->port_stats;
10335 		__le64 *hw_stats = stats->hw_stats;
10336 		u64 *sw_stats = stats->sw_stats;
10337 		u64 *masks = stats->hw_masks;
10338 		int cnt;
10339 
10340 		cnt = sizeof(struct rx_port_stats) / 8;
10341 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10342 
10343 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10344 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10345 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10346 		cnt = sizeof(struct tx_port_stats) / 8;
10347 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10348 	}
10349 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10350 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10351 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10352 	}
10353 }
10354 
10355 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10356 {
10357 	struct hwrm_port_qstats_input *req;
10358 	struct bnxt_pf_info *pf = &bp->pf;
10359 	int rc;
10360 
10361 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10362 		return 0;
10363 
10364 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10365 		return -EOPNOTSUPP;
10366 
10367 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10368 	if (rc)
10369 		return rc;
10370 
10371 	req->flags = flags;
10372 	req->port_id = cpu_to_le16(pf->port_id);
10373 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10374 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10375 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10376 	return hwrm_req_send(bp, req);
10377 }
10378 
10379 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10380 {
10381 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10382 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10383 	struct hwrm_port_qstats_ext_output *resp_qs;
10384 	struct hwrm_port_qstats_ext_input *req_qs;
10385 	struct bnxt_pf_info *pf = &bp->pf;
10386 	u32 tx_stat_size;
10387 	int rc;
10388 
10389 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10390 		return 0;
10391 
10392 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10393 		return -EOPNOTSUPP;
10394 
10395 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10396 	if (rc)
10397 		return rc;
10398 
10399 	req_qs->flags = flags;
10400 	req_qs->port_id = cpu_to_le16(pf->port_id);
10401 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10402 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10403 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10404 		       sizeof(struct tx_port_stats_ext) : 0;
10405 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10406 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10407 	resp_qs = hwrm_req_hold(bp, req_qs);
10408 	rc = hwrm_req_send(bp, req_qs);
10409 	if (!rc) {
10410 		bp->fw_rx_stats_ext_size =
10411 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10412 		if (BNXT_FW_MAJ(bp) < 220 &&
10413 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10414 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10415 
10416 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10417 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10418 	} else {
10419 		bp->fw_rx_stats_ext_size = 0;
10420 		bp->fw_tx_stats_ext_size = 0;
10421 	}
10422 	hwrm_req_drop(bp, req_qs);
10423 
10424 	if (flags)
10425 		return rc;
10426 
10427 	if (bp->fw_tx_stats_ext_size <=
10428 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10429 		bp->pri2cos_valid = 0;
10430 		return rc;
10431 	}
10432 
10433 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10434 	if (rc)
10435 		return rc;
10436 
10437 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10438 
10439 	resp_qc = hwrm_req_hold(bp, req_qc);
10440 	rc = hwrm_req_send(bp, req_qc);
10441 	if (!rc) {
10442 		u8 *pri2cos;
10443 		int i, j;
10444 
10445 		pri2cos = &resp_qc->pri0_cos_queue_id;
10446 		for (i = 0; i < 8; i++) {
10447 			u8 queue_id = pri2cos[i];
10448 			u8 queue_idx;
10449 
10450 			/* Per port queue IDs start from 0, 10, 20, etc */
10451 			queue_idx = queue_id % 10;
10452 			if (queue_idx > BNXT_MAX_QUEUE) {
10453 				bp->pri2cos_valid = false;
10454 				hwrm_req_drop(bp, req_qc);
10455 				return rc;
10456 			}
10457 			for (j = 0; j < bp->max_q; j++) {
10458 				if (bp->q_ids[j] == queue_id)
10459 					bp->pri2cos_idx[i] = queue_idx;
10460 			}
10461 		}
10462 		bp->pri2cos_valid = true;
10463 	}
10464 	hwrm_req_drop(bp, req_qc);
10465 
10466 	return rc;
10467 }
10468 
10469 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10470 {
10471 	bnxt_hwrm_tunnel_dst_port_free(bp,
10472 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10473 	bnxt_hwrm_tunnel_dst_port_free(bp,
10474 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10475 }
10476 
10477 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10478 {
10479 	int rc, i;
10480 	u32 tpa_flags = 0;
10481 
10482 	if (set_tpa)
10483 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10484 	else if (BNXT_NO_FW_ACCESS(bp))
10485 		return 0;
10486 	for (i = 0; i < bp->nr_vnics; i++) {
10487 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10488 		if (rc) {
10489 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10490 				   i, rc);
10491 			return rc;
10492 		}
10493 	}
10494 	return 0;
10495 }
10496 
10497 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10498 {
10499 	int i;
10500 
10501 	for (i = 0; i < bp->nr_vnics; i++)
10502 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10503 }
10504 
10505 static void bnxt_clear_vnic(struct bnxt *bp)
10506 {
10507 	if (!bp->vnic_info)
10508 		return;
10509 
10510 	bnxt_hwrm_clear_vnic_filter(bp);
10511 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10512 		/* clear all RSS setting before free vnic ctx */
10513 		bnxt_hwrm_clear_vnic_rss(bp);
10514 		bnxt_hwrm_vnic_ctx_free(bp);
10515 	}
10516 	/* before free the vnic, undo the vnic tpa settings */
10517 	if (bp->flags & BNXT_FLAG_TPA)
10518 		bnxt_set_tpa(bp, false);
10519 	bnxt_hwrm_vnic_free(bp);
10520 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10521 		bnxt_hwrm_vnic_ctx_free(bp);
10522 }
10523 
10524 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10525 				    bool irq_re_init)
10526 {
10527 	bnxt_clear_vnic(bp);
10528 	bnxt_hwrm_ring_free(bp, close_path);
10529 	bnxt_hwrm_ring_grp_free(bp);
10530 	if (irq_re_init) {
10531 		bnxt_hwrm_stat_ctx_free(bp);
10532 		bnxt_hwrm_free_tunnel_ports(bp);
10533 	}
10534 }
10535 
10536 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10537 {
10538 	struct hwrm_func_cfg_input *req;
10539 	u8 evb_mode;
10540 	int rc;
10541 
10542 	if (br_mode == BRIDGE_MODE_VEB)
10543 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10544 	else if (br_mode == BRIDGE_MODE_VEPA)
10545 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10546 	else
10547 		return -EINVAL;
10548 
10549 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10550 	if (rc)
10551 		return rc;
10552 
10553 	req->fid = cpu_to_le16(0xffff);
10554 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10555 	req->evb_mode = evb_mode;
10556 	return hwrm_req_send(bp, req);
10557 }
10558 
10559 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10560 {
10561 	struct hwrm_func_cfg_input *req;
10562 	int rc;
10563 
10564 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10565 		return 0;
10566 
10567 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10568 	if (rc)
10569 		return rc;
10570 
10571 	req->fid = cpu_to_le16(0xffff);
10572 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10573 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10574 	if (size == 128)
10575 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10576 
10577 	return hwrm_req_send(bp, req);
10578 }
10579 
10580 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10581 {
10582 	int rc;
10583 
10584 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10585 		goto skip_rss_ctx;
10586 
10587 	/* allocate context for vnic */
10588 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10589 	if (rc) {
10590 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10591 			   vnic->vnic_id, rc);
10592 		goto vnic_setup_err;
10593 	}
10594 	bp->rsscos_nr_ctxs++;
10595 
10596 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10597 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10598 		if (rc) {
10599 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10600 				   vnic->vnic_id, rc);
10601 			goto vnic_setup_err;
10602 		}
10603 		bp->rsscos_nr_ctxs++;
10604 	}
10605 
10606 skip_rss_ctx:
10607 	/* configure default vnic, ring grp */
10608 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10609 	if (rc) {
10610 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10611 			   vnic->vnic_id, rc);
10612 		goto vnic_setup_err;
10613 	}
10614 
10615 	/* Enable RSS hashing on vnic */
10616 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10617 	if (rc) {
10618 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10619 			   vnic->vnic_id, rc);
10620 		goto vnic_setup_err;
10621 	}
10622 
10623 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10624 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10625 		if (rc) {
10626 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10627 				   vnic->vnic_id, rc);
10628 		}
10629 	}
10630 
10631 vnic_setup_err:
10632 	return rc;
10633 }
10634 
10635 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10636 			  u8 valid)
10637 {
10638 	struct hwrm_vnic_update_input *req;
10639 	int rc;
10640 
10641 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10642 	if (rc)
10643 		return rc;
10644 
10645 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10646 
10647 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10648 		req->mru = cpu_to_le16(vnic->mru);
10649 
10650 	req->enables = cpu_to_le32(valid);
10651 
10652 	return hwrm_req_send(bp, req);
10653 }
10654 
10655 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10656 {
10657 	int rc;
10658 
10659 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10660 	if (rc) {
10661 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10662 			   vnic->vnic_id, rc);
10663 		return rc;
10664 	}
10665 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10666 	if (rc)
10667 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10668 			   vnic->vnic_id, rc);
10669 	return rc;
10670 }
10671 
10672 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10673 {
10674 	int rc, i, nr_ctxs;
10675 
10676 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10677 	for (i = 0; i < nr_ctxs; i++) {
10678 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10679 		if (rc) {
10680 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10681 				   vnic->vnic_id, i, rc);
10682 			break;
10683 		}
10684 		bp->rsscos_nr_ctxs++;
10685 	}
10686 	if (i < nr_ctxs)
10687 		return -ENOMEM;
10688 
10689 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10690 	if (rc)
10691 		return rc;
10692 
10693 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10694 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10695 		if (rc) {
10696 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10697 				   vnic->vnic_id, rc);
10698 		}
10699 	}
10700 	return rc;
10701 }
10702 
10703 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10704 {
10705 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10706 		return __bnxt_setup_vnic_p5(bp, vnic);
10707 	else
10708 		return __bnxt_setup_vnic(bp, vnic);
10709 }
10710 
10711 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10712 				     struct bnxt_vnic_info *vnic,
10713 				     u16 start_rx_ring_idx, int rx_rings)
10714 {
10715 	int rc;
10716 
10717 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10718 	if (rc) {
10719 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10720 			   vnic->vnic_id, rc);
10721 		return rc;
10722 	}
10723 	return bnxt_setup_vnic(bp, vnic);
10724 }
10725 
10726 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10727 {
10728 	struct bnxt_vnic_info *vnic;
10729 	int i, rc = 0;
10730 
10731 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10732 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10733 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10734 	}
10735 
10736 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10737 		return 0;
10738 
10739 	for (i = 0; i < bp->rx_nr_rings; i++) {
10740 		u16 vnic_id = i + 1;
10741 		u16 ring_id = i;
10742 
10743 		if (vnic_id >= bp->nr_vnics)
10744 			break;
10745 
10746 		vnic = &bp->vnic_info[vnic_id];
10747 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10748 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10749 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10750 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10751 			break;
10752 	}
10753 	return rc;
10754 }
10755 
10756 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10757 			  bool all)
10758 {
10759 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10760 	struct bnxt_filter_base *usr_fltr, *tmp;
10761 	struct bnxt_ntuple_filter *ntp_fltr;
10762 	int i;
10763 
10764 	if (netif_running(bp->dev)) {
10765 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10766 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10767 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10768 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10769 		}
10770 	}
10771 	if (!all)
10772 		return;
10773 
10774 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10775 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10776 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10777 			ntp_fltr = container_of(usr_fltr,
10778 						struct bnxt_ntuple_filter,
10779 						base);
10780 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10781 			bnxt_del_ntp_filter(bp, ntp_fltr);
10782 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10783 		}
10784 	}
10785 
10786 	if (vnic->rss_table)
10787 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10788 				  vnic->rss_table,
10789 				  vnic->rss_table_dma_addr);
10790 	bp->num_rss_ctx--;
10791 }
10792 
10793 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10794 				  int rxr_id)
10795 {
10796 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10797 	int i, vnic_rx;
10798 
10799 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10800 	 * must be updated because a future filter may use it.
10801 	 */
10802 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10803 		return true;
10804 
10805 	for (i = 0; i < tbl_size; i++) {
10806 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10807 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10808 		else
10809 			vnic_rx = bp->rss_indir_tbl[i];
10810 
10811 		if (rxr_id == vnic_rx)
10812 			return true;
10813 	}
10814 
10815 	return false;
10816 }
10817 
10818 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10819 				u16 mru, int rxr_id)
10820 {
10821 	int rc;
10822 
10823 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10824 		return 0;
10825 
10826 	if (mru) {
10827 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10828 		if (rc) {
10829 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10830 				   vnic->vnic_id, rc);
10831 			return rc;
10832 		}
10833 	}
10834 	vnic->mru = mru;
10835 	bnxt_hwrm_vnic_update(bp, vnic,
10836 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10837 
10838 	return 0;
10839 }
10840 
10841 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10842 {
10843 	struct ethtool_rxfh_context *ctx;
10844 	unsigned long context;
10845 	int rc;
10846 
10847 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10848 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10849 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10850 
10851 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10852 		if (rc)
10853 			return rc;
10854 	}
10855 
10856 	return 0;
10857 }
10858 
10859 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10860 {
10861 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10862 	struct ethtool_rxfh_context *ctx;
10863 	unsigned long context;
10864 
10865 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10866 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10867 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10868 
10869 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10870 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10871 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10872 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10873 				   rss_ctx->index);
10874 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10875 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10876 		}
10877 	}
10878 }
10879 
10880 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10881 {
10882 	struct ethtool_rxfh_context *ctx;
10883 	unsigned long context;
10884 
10885 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10886 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10887 
10888 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10889 	}
10890 }
10891 
10892 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10893 static bool bnxt_promisc_ok(struct bnxt *bp)
10894 {
10895 #ifdef CONFIG_BNXT_SRIOV
10896 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10897 		return false;
10898 #endif
10899 	return true;
10900 }
10901 
10902 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10903 {
10904 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10905 	unsigned int rc = 0;
10906 
10907 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10908 	if (rc) {
10909 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10910 			   rc);
10911 		return rc;
10912 	}
10913 
10914 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10915 	if (rc) {
10916 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10917 			   rc);
10918 		return rc;
10919 	}
10920 	return rc;
10921 }
10922 
10923 static int bnxt_cfg_rx_mode(struct bnxt *);
10924 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10925 
10926 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10927 {
10928 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10929 	int rc = 0;
10930 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10931 
10932 	if (irq_re_init) {
10933 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10934 		if (rc) {
10935 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10936 				   rc);
10937 			goto err_out;
10938 		}
10939 	}
10940 
10941 	rc = bnxt_hwrm_ring_alloc(bp);
10942 	if (rc) {
10943 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10944 		goto err_out;
10945 	}
10946 
10947 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10948 	if (rc) {
10949 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10950 		goto err_out;
10951 	}
10952 
10953 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10954 		rx_nr_rings--;
10955 
10956 	/* default vnic 0 */
10957 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10958 	if (rc) {
10959 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10960 		goto err_out;
10961 	}
10962 
10963 	if (BNXT_VF(bp))
10964 		bnxt_hwrm_func_qcfg(bp);
10965 
10966 	rc = bnxt_setup_vnic(bp, vnic);
10967 	if (rc)
10968 		goto err_out;
10969 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10970 		bnxt_hwrm_update_rss_hash_cfg(bp);
10971 
10972 	if (bp->flags & BNXT_FLAG_RFS) {
10973 		rc = bnxt_alloc_rfs_vnics(bp);
10974 		if (rc)
10975 			goto err_out;
10976 	}
10977 
10978 	if (bp->flags & BNXT_FLAG_TPA) {
10979 		rc = bnxt_set_tpa(bp, true);
10980 		if (rc)
10981 			goto err_out;
10982 	}
10983 
10984 	if (BNXT_VF(bp))
10985 		bnxt_update_vf_mac(bp);
10986 
10987 	/* Filter for default vnic 0 */
10988 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10989 	if (rc) {
10990 		if (BNXT_VF(bp) && rc == -ENODEV)
10991 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10992 		else
10993 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10994 		goto err_out;
10995 	}
10996 	vnic->uc_filter_count = 1;
10997 
10998 	vnic->rx_mask = 0;
10999 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
11000 		goto skip_rx_mask;
11001 
11002 	if (bp->dev->flags & IFF_BROADCAST)
11003 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
11004 
11005 	if (bp->dev->flags & IFF_PROMISC)
11006 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
11007 
11008 	if (bp->dev->flags & IFF_ALLMULTI) {
11009 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11010 		vnic->mc_list_count = 0;
11011 	} else if (bp->dev->flags & IFF_MULTICAST) {
11012 		u32 mask = 0;
11013 
11014 		bnxt_mc_list_updated(bp, &mask);
11015 		vnic->rx_mask |= mask;
11016 	}
11017 
11018 	rc = bnxt_cfg_rx_mode(bp);
11019 	if (rc)
11020 		goto err_out;
11021 
11022 skip_rx_mask:
11023 	rc = bnxt_hwrm_set_coal(bp);
11024 	if (rc)
11025 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11026 				rc);
11027 
11028 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11029 		rc = bnxt_setup_nitroa0_vnic(bp);
11030 		if (rc)
11031 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11032 				   rc);
11033 	}
11034 
11035 	if (BNXT_VF(bp)) {
11036 		bnxt_hwrm_func_qcfg(bp);
11037 		netdev_update_features(bp->dev);
11038 	}
11039 
11040 	return 0;
11041 
11042 err_out:
11043 	bnxt_hwrm_resource_free(bp, 0, true);
11044 
11045 	return rc;
11046 }
11047 
11048 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11049 {
11050 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11051 	return 0;
11052 }
11053 
11054 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11055 {
11056 	bnxt_init_cp_rings(bp);
11057 	bnxt_init_rx_rings(bp);
11058 	bnxt_init_tx_rings(bp);
11059 	bnxt_init_ring_grps(bp, irq_re_init);
11060 	bnxt_init_vnics(bp);
11061 
11062 	return bnxt_init_chip(bp, irq_re_init);
11063 }
11064 
11065 static int bnxt_set_real_num_queues(struct bnxt *bp)
11066 {
11067 	int rc;
11068 	struct net_device *dev = bp->dev;
11069 
11070 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11071 					  bp->tx_nr_rings_xdp);
11072 	if (rc)
11073 		return rc;
11074 
11075 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11076 	if (rc)
11077 		return rc;
11078 
11079 #ifdef CONFIG_RFS_ACCEL
11080 	if (bp->flags & BNXT_FLAG_RFS)
11081 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11082 #endif
11083 
11084 	return rc;
11085 }
11086 
11087 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11088 			     bool shared)
11089 {
11090 	int _rx = *rx, _tx = *tx;
11091 
11092 	if (shared) {
11093 		*rx = min_t(int, _rx, max);
11094 		*tx = min_t(int, _tx, max);
11095 	} else {
11096 		if (max < 2)
11097 			return -ENOMEM;
11098 
11099 		while (_rx + _tx > max) {
11100 			if (_rx > _tx && _rx > 1)
11101 				_rx--;
11102 			else if (_tx > 1)
11103 				_tx--;
11104 		}
11105 		*rx = _rx;
11106 		*tx = _tx;
11107 	}
11108 	return 0;
11109 }
11110 
11111 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11112 {
11113 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11114 }
11115 
11116 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11117 {
11118 	int tcs = bp->num_tc;
11119 
11120 	if (!tcs)
11121 		tcs = 1;
11122 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11123 }
11124 
11125 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11126 {
11127 	int tcs = bp->num_tc;
11128 
11129 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11130 	       bp->tx_nr_rings_xdp;
11131 }
11132 
11133 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11134 			   bool sh)
11135 {
11136 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11137 
11138 	if (tx_cp != *tx) {
11139 		int tx_saved = tx_cp, rc;
11140 
11141 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11142 		if (rc)
11143 			return rc;
11144 		if (tx_cp != tx_saved)
11145 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11146 		return 0;
11147 	}
11148 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11149 }
11150 
11151 static void bnxt_setup_msix(struct bnxt *bp)
11152 {
11153 	const int len = sizeof(bp->irq_tbl[0].name);
11154 	struct net_device *dev = bp->dev;
11155 	int tcs, i;
11156 
11157 	tcs = bp->num_tc;
11158 	if (tcs) {
11159 		int i, off, count;
11160 
11161 		for (i = 0; i < tcs; i++) {
11162 			count = bp->tx_nr_rings_per_tc;
11163 			off = BNXT_TC_TO_RING_BASE(bp, i);
11164 			netdev_set_tc_queue(dev, i, count, off);
11165 		}
11166 	}
11167 
11168 	for (i = 0; i < bp->cp_nr_rings; i++) {
11169 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11170 		char *attr;
11171 
11172 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11173 			attr = "TxRx";
11174 		else if (i < bp->rx_nr_rings)
11175 			attr = "rx";
11176 		else
11177 			attr = "tx";
11178 
11179 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11180 			 attr, i);
11181 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11182 	}
11183 }
11184 
11185 static int bnxt_init_int_mode(struct bnxt *bp);
11186 
11187 static int bnxt_change_msix(struct bnxt *bp, int total)
11188 {
11189 	struct msi_map map;
11190 	int i;
11191 
11192 	/* add MSIX to the end if needed */
11193 	for (i = bp->total_irqs; i < total; i++) {
11194 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11195 		if (map.index < 0)
11196 			return bp->total_irqs;
11197 		bp->irq_tbl[i].vector = map.virq;
11198 		bp->total_irqs++;
11199 	}
11200 
11201 	/* trim MSIX from the end if needed */
11202 	for (i = bp->total_irqs; i > total; i--) {
11203 		map.index = i - 1;
11204 		map.virq = bp->irq_tbl[i - 1].vector;
11205 		pci_msix_free_irq(bp->pdev, map);
11206 		bp->total_irqs--;
11207 	}
11208 	return bp->total_irqs;
11209 }
11210 
11211 static int bnxt_setup_int_mode(struct bnxt *bp)
11212 {
11213 	int rc;
11214 
11215 	if (!bp->irq_tbl) {
11216 		rc = bnxt_init_int_mode(bp);
11217 		if (rc || !bp->irq_tbl)
11218 			return rc ?: -ENODEV;
11219 	}
11220 
11221 	bnxt_setup_msix(bp);
11222 
11223 	rc = bnxt_set_real_num_queues(bp);
11224 	return rc;
11225 }
11226 
11227 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11228 {
11229 	return bp->hw_resc.max_rsscos_ctxs;
11230 }
11231 
11232 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11233 {
11234 	return bp->hw_resc.max_vnics;
11235 }
11236 
11237 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11238 {
11239 	return bp->hw_resc.max_stat_ctxs;
11240 }
11241 
11242 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11243 {
11244 	return bp->hw_resc.max_cp_rings;
11245 }
11246 
11247 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11248 {
11249 	unsigned int cp = bp->hw_resc.max_cp_rings;
11250 
11251 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11252 		cp -= bnxt_get_ulp_msix_num(bp);
11253 
11254 	return cp;
11255 }
11256 
11257 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11258 {
11259 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11260 
11261 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11262 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11263 
11264 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11265 }
11266 
11267 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11268 {
11269 	bp->hw_resc.max_irqs = max_irqs;
11270 }
11271 
11272 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11273 {
11274 	unsigned int cp;
11275 
11276 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11277 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11278 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11279 	else
11280 		return cp - bp->cp_nr_rings;
11281 }
11282 
11283 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11284 {
11285 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11286 }
11287 
11288 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11289 {
11290 	int max_irq = bnxt_get_max_func_irqs(bp);
11291 	int total_req = bp->cp_nr_rings + num;
11292 
11293 	if (max_irq < total_req) {
11294 		num = max_irq - bp->cp_nr_rings;
11295 		if (num <= 0)
11296 			return 0;
11297 	}
11298 	return num;
11299 }
11300 
11301 static int bnxt_get_num_msix(struct bnxt *bp)
11302 {
11303 	if (!BNXT_NEW_RM(bp))
11304 		return bnxt_get_max_func_irqs(bp);
11305 
11306 	return bnxt_nq_rings_in_use(bp);
11307 }
11308 
11309 static int bnxt_init_int_mode(struct bnxt *bp)
11310 {
11311 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11312 
11313 	total_vecs = bnxt_get_num_msix(bp);
11314 	max = bnxt_get_max_func_irqs(bp);
11315 	if (total_vecs > max)
11316 		total_vecs = max;
11317 
11318 	if (!total_vecs)
11319 		return 0;
11320 
11321 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11322 		min = 2;
11323 
11324 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11325 					   PCI_IRQ_MSIX);
11326 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11327 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11328 		rc = -ENODEV;
11329 		goto msix_setup_exit;
11330 	}
11331 
11332 	tbl_size = total_vecs;
11333 	if (pci_msix_can_alloc_dyn(bp->pdev))
11334 		tbl_size = max;
11335 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11336 	if (bp->irq_tbl) {
11337 		for (i = 0; i < total_vecs; i++)
11338 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11339 
11340 		bp->total_irqs = total_vecs;
11341 		/* Trim rings based upon num of vectors allocated */
11342 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11343 				     total_vecs - ulp_msix, min == 1);
11344 		if (rc)
11345 			goto msix_setup_exit;
11346 
11347 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11348 		bp->cp_nr_rings = (min == 1) ?
11349 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11350 				  tx_cp + bp->rx_nr_rings;
11351 
11352 	} else {
11353 		rc = -ENOMEM;
11354 		goto msix_setup_exit;
11355 	}
11356 	return 0;
11357 
11358 msix_setup_exit:
11359 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11360 	kfree(bp->irq_tbl);
11361 	bp->irq_tbl = NULL;
11362 	pci_free_irq_vectors(bp->pdev);
11363 	return rc;
11364 }
11365 
11366 static void bnxt_clear_int_mode(struct bnxt *bp)
11367 {
11368 	pci_free_irq_vectors(bp->pdev);
11369 
11370 	kfree(bp->irq_tbl);
11371 	bp->irq_tbl = NULL;
11372 }
11373 
11374 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11375 {
11376 	bool irq_cleared = false;
11377 	bool irq_change = false;
11378 	int tcs = bp->num_tc;
11379 	int irqs_required;
11380 	int rc;
11381 
11382 	if (!bnxt_need_reserve_rings(bp))
11383 		return 0;
11384 
11385 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11386 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11387 
11388 		if (ulp_msix > bp->ulp_num_msix_want)
11389 			ulp_msix = bp->ulp_num_msix_want;
11390 		irqs_required = ulp_msix + bp->cp_nr_rings;
11391 	} else {
11392 		irqs_required = bnxt_get_num_msix(bp);
11393 	}
11394 
11395 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11396 		irq_change = true;
11397 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11398 			bnxt_ulp_irq_stop(bp);
11399 			bnxt_clear_int_mode(bp);
11400 			irq_cleared = true;
11401 		}
11402 	}
11403 	rc = __bnxt_reserve_rings(bp);
11404 	if (irq_cleared) {
11405 		if (!rc)
11406 			rc = bnxt_init_int_mode(bp);
11407 		bnxt_ulp_irq_restart(bp, rc);
11408 	} else if (irq_change && !rc) {
11409 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11410 			rc = -ENOSPC;
11411 	}
11412 	if (rc) {
11413 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11414 		return rc;
11415 	}
11416 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11417 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11418 		netdev_err(bp->dev, "tx ring reservation failure\n");
11419 		netdev_reset_tc(bp->dev);
11420 		bp->num_tc = 0;
11421 		if (bp->tx_nr_rings_xdp)
11422 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11423 		else
11424 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11425 		return -ENOMEM;
11426 	}
11427 	return 0;
11428 }
11429 
11430 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11431 {
11432 	struct bnxt_tx_ring_info *txr;
11433 	struct netdev_queue *txq;
11434 	struct bnxt_napi *bnapi;
11435 	int i;
11436 
11437 	bnapi = bp->bnapi[idx];
11438 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11439 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11440 		synchronize_net();
11441 
11442 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11443 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11444 			if (txq) {
11445 				__netif_tx_lock_bh(txq);
11446 				netif_tx_stop_queue(txq);
11447 				__netif_tx_unlock_bh(txq);
11448 			}
11449 		}
11450 
11451 		if (!bp->tph_mode)
11452 			continue;
11453 
11454 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11455 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11456 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11457 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11458 	}
11459 }
11460 
11461 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11462 {
11463 	struct bnxt_tx_ring_info *txr;
11464 	struct netdev_queue *txq;
11465 	struct bnxt_napi *bnapi;
11466 	int rc, i;
11467 
11468 	bnapi = bp->bnapi[idx];
11469 	/* All rings have been reserved and previously allocated.
11470 	 * Reallocating with the same parameters should never fail.
11471 	 */
11472 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11473 		if (!bp->tph_mode)
11474 			goto start_tx;
11475 
11476 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11477 		if (rc)
11478 			return rc;
11479 
11480 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11481 		if (rc)
11482 			return rc;
11483 
11484 		txr->tx_prod = 0;
11485 		txr->tx_cons = 0;
11486 		txr->tx_hw_cons = 0;
11487 start_tx:
11488 		WRITE_ONCE(txr->dev_state, 0);
11489 		synchronize_net();
11490 
11491 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11492 			continue;
11493 
11494 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11495 		if (txq)
11496 			netif_tx_start_queue(txq);
11497 	}
11498 
11499 	return 0;
11500 }
11501 
11502 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11503 				     const cpumask_t *mask)
11504 {
11505 	struct bnxt_irq *irq;
11506 	u16 tag;
11507 	int err;
11508 
11509 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11510 
11511 	if (!irq->bp->tph_mode)
11512 		return;
11513 
11514 	cpumask_copy(irq->cpu_mask, mask);
11515 
11516 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11517 		return;
11518 
11519 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11520 				cpumask_first(irq->cpu_mask), &tag))
11521 		return;
11522 
11523 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11524 		return;
11525 
11526 	netdev_lock(irq->bp->dev);
11527 	if (netif_running(irq->bp->dev)) {
11528 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11529 		if (err)
11530 			netdev_err(irq->bp->dev,
11531 				   "RX queue restart failed: err=%d\n", err);
11532 	}
11533 	netdev_unlock(irq->bp->dev);
11534 }
11535 
11536 static void bnxt_irq_affinity_release(struct kref *ref)
11537 {
11538 	struct irq_affinity_notify *notify =
11539 		container_of(ref, struct irq_affinity_notify, kref);
11540 	struct bnxt_irq *irq;
11541 
11542 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11543 
11544 	if (!irq->bp->tph_mode)
11545 		return;
11546 
11547 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11548 		netdev_err(irq->bp->dev,
11549 			   "Setting ST=0 for MSIX entry %d failed\n",
11550 			   irq->msix_nr);
11551 		return;
11552 	}
11553 }
11554 
11555 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11556 {
11557 	irq_set_affinity_notifier(irq->vector, NULL);
11558 }
11559 
11560 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11561 {
11562 	struct irq_affinity_notify *notify;
11563 
11564 	irq->bp = bp;
11565 
11566 	/* Nothing to do if TPH is not enabled */
11567 	if (!bp->tph_mode)
11568 		return;
11569 
11570 	/* Register IRQ affinity notifier */
11571 	notify = &irq->affinity_notify;
11572 	notify->irq = irq->vector;
11573 	notify->notify = bnxt_irq_affinity_notify;
11574 	notify->release = bnxt_irq_affinity_release;
11575 
11576 	irq_set_affinity_notifier(irq->vector, notify);
11577 }
11578 
11579 static void bnxt_free_irq(struct bnxt *bp)
11580 {
11581 	struct bnxt_irq *irq;
11582 	int i;
11583 
11584 #ifdef CONFIG_RFS_ACCEL
11585 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11586 	bp->dev->rx_cpu_rmap = NULL;
11587 #endif
11588 	if (!bp->irq_tbl || !bp->bnapi)
11589 		return;
11590 
11591 	for (i = 0; i < bp->cp_nr_rings; i++) {
11592 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11593 
11594 		irq = &bp->irq_tbl[map_idx];
11595 		if (irq->requested) {
11596 			if (irq->have_cpumask) {
11597 				irq_update_affinity_hint(irq->vector, NULL);
11598 				free_cpumask_var(irq->cpu_mask);
11599 				irq->have_cpumask = 0;
11600 			}
11601 
11602 			bnxt_release_irq_notifier(irq);
11603 
11604 			free_irq(irq->vector, bp->bnapi[i]);
11605 		}
11606 
11607 		irq->requested = 0;
11608 	}
11609 
11610 	/* Disable TPH support */
11611 	pcie_disable_tph(bp->pdev);
11612 	bp->tph_mode = 0;
11613 }
11614 
11615 static int bnxt_request_irq(struct bnxt *bp)
11616 {
11617 	int i, j, rc = 0;
11618 	unsigned long flags = 0;
11619 #ifdef CONFIG_RFS_ACCEL
11620 	struct cpu_rmap *rmap;
11621 #endif
11622 
11623 	rc = bnxt_setup_int_mode(bp);
11624 	if (rc) {
11625 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11626 			   rc);
11627 		return rc;
11628 	}
11629 #ifdef CONFIG_RFS_ACCEL
11630 	rmap = bp->dev->rx_cpu_rmap;
11631 #endif
11632 
11633 	/* Enable TPH support as part of IRQ request */
11634 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11635 	if (!rc)
11636 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11637 
11638 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11639 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11640 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11641 
11642 #ifdef CONFIG_RFS_ACCEL
11643 		if (rmap && bp->bnapi[i]->rx_ring) {
11644 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11645 			if (rc)
11646 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11647 					    j);
11648 			j++;
11649 		}
11650 #endif
11651 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11652 				 bp->bnapi[i]);
11653 		if (rc)
11654 			break;
11655 
11656 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11657 		irq->requested = 1;
11658 
11659 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11660 			int numa_node = dev_to_node(&bp->pdev->dev);
11661 			u16 tag;
11662 
11663 			irq->have_cpumask = 1;
11664 			irq->msix_nr = map_idx;
11665 			irq->ring_nr = i;
11666 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11667 					irq->cpu_mask);
11668 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11669 			if (rc) {
11670 				netdev_warn(bp->dev,
11671 					    "Update affinity hint failed, IRQ = %d\n",
11672 					    irq->vector);
11673 				break;
11674 			}
11675 
11676 			bnxt_register_irq_notifier(bp, irq);
11677 
11678 			/* Init ST table entry */
11679 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11680 						cpumask_first(irq->cpu_mask),
11681 						&tag))
11682 				continue;
11683 
11684 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11685 		}
11686 	}
11687 	return rc;
11688 }
11689 
11690 static void bnxt_del_napi(struct bnxt *bp)
11691 {
11692 	int i;
11693 
11694 	if (!bp->bnapi)
11695 		return;
11696 
11697 	for (i = 0; i < bp->rx_nr_rings; i++)
11698 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11699 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11700 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11701 
11702 	for (i = 0; i < bp->cp_nr_rings; i++) {
11703 		struct bnxt_napi *bnapi = bp->bnapi[i];
11704 
11705 		__netif_napi_del_locked(&bnapi->napi);
11706 	}
11707 	/* We called __netif_napi_del_locked(), we need
11708 	 * to respect an RCU grace period before freeing napi structures.
11709 	 */
11710 	synchronize_net();
11711 }
11712 
11713 static void bnxt_init_napi(struct bnxt *bp)
11714 {
11715 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11716 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11717 	struct bnxt_napi *bnapi;
11718 	int i;
11719 
11720 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11721 		poll_fn = bnxt_poll_p5;
11722 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11723 		cp_nr_rings--;
11724 
11725 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11726 
11727 	for (i = 0; i < cp_nr_rings; i++) {
11728 		bnapi = bp->bnapi[i];
11729 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11730 					     bnapi->index);
11731 	}
11732 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11733 		bnapi = bp->bnapi[cp_nr_rings];
11734 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11735 	}
11736 }
11737 
11738 static void bnxt_disable_napi(struct bnxt *bp)
11739 {
11740 	int i;
11741 
11742 	if (!bp->bnapi ||
11743 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11744 		return;
11745 
11746 	for (i = 0; i < bp->cp_nr_rings; i++) {
11747 		struct bnxt_napi *bnapi = bp->bnapi[i];
11748 		struct bnxt_cp_ring_info *cpr;
11749 
11750 		cpr = &bnapi->cp_ring;
11751 		if (bnapi->tx_fault)
11752 			cpr->sw_stats->tx.tx_resets++;
11753 		if (bnapi->in_reset)
11754 			cpr->sw_stats->rx.rx_resets++;
11755 		napi_disable_locked(&bnapi->napi);
11756 	}
11757 }
11758 
11759 static void bnxt_enable_napi(struct bnxt *bp)
11760 {
11761 	int i;
11762 
11763 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11764 	for (i = 0; i < bp->cp_nr_rings; i++) {
11765 		struct bnxt_napi *bnapi = bp->bnapi[i];
11766 		struct bnxt_cp_ring_info *cpr;
11767 
11768 		bnapi->tx_fault = 0;
11769 
11770 		cpr = &bnapi->cp_ring;
11771 		bnapi->in_reset = false;
11772 
11773 		if (bnapi->rx_ring) {
11774 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11775 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11776 		}
11777 		napi_enable_locked(&bnapi->napi);
11778 	}
11779 }
11780 
11781 void bnxt_tx_disable(struct bnxt *bp)
11782 {
11783 	int i;
11784 	struct bnxt_tx_ring_info *txr;
11785 
11786 	if (bp->tx_ring) {
11787 		for (i = 0; i < bp->tx_nr_rings; i++) {
11788 			txr = &bp->tx_ring[i];
11789 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11790 		}
11791 	}
11792 	/* Make sure napi polls see @dev_state change */
11793 	synchronize_net();
11794 	/* Drop carrier first to prevent TX timeout */
11795 	netif_carrier_off(bp->dev);
11796 	/* Stop all TX queues */
11797 	netif_tx_disable(bp->dev);
11798 }
11799 
11800 void bnxt_tx_enable(struct bnxt *bp)
11801 {
11802 	int i;
11803 	struct bnxt_tx_ring_info *txr;
11804 
11805 	for (i = 0; i < bp->tx_nr_rings; i++) {
11806 		txr = &bp->tx_ring[i];
11807 		WRITE_ONCE(txr->dev_state, 0);
11808 	}
11809 	/* Make sure napi polls see @dev_state change */
11810 	synchronize_net();
11811 	netif_tx_wake_all_queues(bp->dev);
11812 	if (BNXT_LINK_IS_UP(bp))
11813 		netif_carrier_on(bp->dev);
11814 }
11815 
11816 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11817 {
11818 	u8 active_fec = link_info->active_fec_sig_mode &
11819 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11820 
11821 	switch (active_fec) {
11822 	default:
11823 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11824 		return "None";
11825 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11826 		return "Clause 74 BaseR";
11827 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11828 		return "Clause 91 RS(528,514)";
11829 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11830 		return "Clause 91 RS544_1XN";
11831 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11832 		return "Clause 91 RS(544,514)";
11833 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11834 		return "Clause 91 RS272_1XN";
11835 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11836 		return "Clause 91 RS(272,257)";
11837 	}
11838 }
11839 
11840 void bnxt_report_link(struct bnxt *bp)
11841 {
11842 	if (BNXT_LINK_IS_UP(bp)) {
11843 		const char *signal = "";
11844 		const char *flow_ctrl;
11845 		const char *duplex;
11846 		u32 speed;
11847 		u16 fec;
11848 
11849 		netif_carrier_on(bp->dev);
11850 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11851 		if (speed == SPEED_UNKNOWN) {
11852 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11853 			return;
11854 		}
11855 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11856 			duplex = "full";
11857 		else
11858 			duplex = "half";
11859 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11860 			flow_ctrl = "ON - receive & transmit";
11861 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11862 			flow_ctrl = "ON - transmit";
11863 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11864 			flow_ctrl = "ON - receive";
11865 		else
11866 			flow_ctrl = "none";
11867 		if (bp->link_info.phy_qcfg_resp.option_flags &
11868 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11869 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11870 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11871 			switch (sig_mode) {
11872 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11873 				signal = "(NRZ) ";
11874 				break;
11875 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11876 				signal = "(PAM4 56Gbps) ";
11877 				break;
11878 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11879 				signal = "(PAM4 112Gbps) ";
11880 				break;
11881 			default:
11882 				break;
11883 			}
11884 		}
11885 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11886 			    speed, signal, duplex, flow_ctrl);
11887 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11888 			netdev_info(bp->dev, "EEE is %s\n",
11889 				    bp->eee.eee_active ? "active" :
11890 							 "not active");
11891 		fec = bp->link_info.fec_cfg;
11892 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11893 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11894 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11895 				    bnxt_report_fec(&bp->link_info));
11896 	} else {
11897 		netif_carrier_off(bp->dev);
11898 		netdev_err(bp->dev, "NIC Link is Down\n");
11899 	}
11900 }
11901 
11902 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11903 {
11904 	if (!resp->supported_speeds_auto_mode &&
11905 	    !resp->supported_speeds_force_mode &&
11906 	    !resp->supported_pam4_speeds_auto_mode &&
11907 	    !resp->supported_pam4_speeds_force_mode &&
11908 	    !resp->supported_speeds2_auto_mode &&
11909 	    !resp->supported_speeds2_force_mode)
11910 		return true;
11911 	return false;
11912 }
11913 
11914 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11915 {
11916 	struct bnxt_link_info *link_info = &bp->link_info;
11917 	struct hwrm_port_phy_qcaps_output *resp;
11918 	struct hwrm_port_phy_qcaps_input *req;
11919 	int rc = 0;
11920 
11921 	if (bp->hwrm_spec_code < 0x10201)
11922 		return 0;
11923 
11924 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11925 	if (rc)
11926 		return rc;
11927 
11928 	resp = hwrm_req_hold(bp, req);
11929 	rc = hwrm_req_send(bp, req);
11930 	if (rc)
11931 		goto hwrm_phy_qcaps_exit;
11932 
11933 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11934 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11935 		struct ethtool_keee *eee = &bp->eee;
11936 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11937 
11938 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11939 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11940 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11941 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11942 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11943 	}
11944 
11945 	if (bp->hwrm_spec_code >= 0x10a01) {
11946 		if (bnxt_phy_qcaps_no_speed(resp)) {
11947 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11948 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11949 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11950 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11951 			netdev_info(bp->dev, "Ethernet link enabled\n");
11952 			/* Phy re-enabled, reprobe the speeds */
11953 			link_info->support_auto_speeds = 0;
11954 			link_info->support_pam4_auto_speeds = 0;
11955 			link_info->support_auto_speeds2 = 0;
11956 		}
11957 	}
11958 	if (resp->supported_speeds_auto_mode)
11959 		link_info->support_auto_speeds =
11960 			le16_to_cpu(resp->supported_speeds_auto_mode);
11961 	if (resp->supported_pam4_speeds_auto_mode)
11962 		link_info->support_pam4_auto_speeds =
11963 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11964 	if (resp->supported_speeds2_auto_mode)
11965 		link_info->support_auto_speeds2 =
11966 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11967 
11968 	bp->port_count = resp->port_cnt;
11969 
11970 hwrm_phy_qcaps_exit:
11971 	hwrm_req_drop(bp, req);
11972 	return rc;
11973 }
11974 
11975 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11976 {
11977 	struct hwrm_port_mac_qcaps_output *resp;
11978 	struct hwrm_port_mac_qcaps_input *req;
11979 	int rc;
11980 
11981 	if (bp->hwrm_spec_code < 0x10a03)
11982 		return;
11983 
11984 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11985 	if (rc)
11986 		return;
11987 
11988 	resp = hwrm_req_hold(bp, req);
11989 	rc = hwrm_req_send_silent(bp, req);
11990 	if (!rc)
11991 		bp->mac_flags = resp->flags;
11992 	hwrm_req_drop(bp, req);
11993 }
11994 
11995 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11996 {
11997 	u16 diff = advertising ^ supported;
11998 
11999 	return ((supported | diff) != supported);
12000 }
12001 
12002 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
12003 {
12004 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
12005 
12006 	/* Check if any advertised speeds are no longer supported. The caller
12007 	 * holds the link_lock mutex, so we can modify link_info settings.
12008 	 */
12009 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12010 		if (bnxt_support_dropped(link_info->advertising,
12011 					 link_info->support_auto_speeds2)) {
12012 			link_info->advertising = link_info->support_auto_speeds2;
12013 			return true;
12014 		}
12015 		return false;
12016 	}
12017 	if (bnxt_support_dropped(link_info->advertising,
12018 				 link_info->support_auto_speeds)) {
12019 		link_info->advertising = link_info->support_auto_speeds;
12020 		return true;
12021 	}
12022 	if (bnxt_support_dropped(link_info->advertising_pam4,
12023 				 link_info->support_pam4_auto_speeds)) {
12024 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12025 		return true;
12026 	}
12027 	return false;
12028 }
12029 
12030 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12031 {
12032 	struct bnxt_link_info *link_info = &bp->link_info;
12033 	struct hwrm_port_phy_qcfg_output *resp;
12034 	struct hwrm_port_phy_qcfg_input *req;
12035 	u8 link_state = link_info->link_state;
12036 	bool support_changed;
12037 	int rc;
12038 
12039 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12040 	if (rc)
12041 		return rc;
12042 
12043 	resp = hwrm_req_hold(bp, req);
12044 	rc = hwrm_req_send(bp, req);
12045 	if (rc) {
12046 		hwrm_req_drop(bp, req);
12047 		if (BNXT_VF(bp) && rc == -ENODEV) {
12048 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12049 			rc = 0;
12050 		}
12051 		return rc;
12052 	}
12053 
12054 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12055 	link_info->phy_link_status = resp->link;
12056 	link_info->duplex = resp->duplex_cfg;
12057 	if (bp->hwrm_spec_code >= 0x10800)
12058 		link_info->duplex = resp->duplex_state;
12059 	link_info->pause = resp->pause;
12060 	link_info->auto_mode = resp->auto_mode;
12061 	link_info->auto_pause_setting = resp->auto_pause;
12062 	link_info->lp_pause = resp->link_partner_adv_pause;
12063 	link_info->force_pause_setting = resp->force_pause;
12064 	link_info->duplex_setting = resp->duplex_cfg;
12065 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12066 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12067 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12068 			link_info->active_lanes = resp->active_lanes;
12069 	} else {
12070 		link_info->link_speed = 0;
12071 		link_info->active_lanes = 0;
12072 	}
12073 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12074 	link_info->force_pam4_link_speed =
12075 		le16_to_cpu(resp->force_pam4_link_speed);
12076 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12077 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12078 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12079 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12080 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12081 	link_info->auto_pam4_link_speeds =
12082 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12083 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12084 	link_info->lp_auto_link_speeds =
12085 		le16_to_cpu(resp->link_partner_adv_speeds);
12086 	link_info->lp_auto_pam4_link_speeds =
12087 		resp->link_partner_pam4_adv_speeds;
12088 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12089 	link_info->phy_ver[0] = resp->phy_maj;
12090 	link_info->phy_ver[1] = resp->phy_min;
12091 	link_info->phy_ver[2] = resp->phy_bld;
12092 	link_info->media_type = resp->media_type;
12093 	link_info->phy_type = resp->phy_type;
12094 	link_info->transceiver = resp->xcvr_pkg_type;
12095 	link_info->phy_addr = resp->eee_config_phy_addr &
12096 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12097 	link_info->module_status = resp->module_status;
12098 
12099 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12100 		struct ethtool_keee *eee = &bp->eee;
12101 		u16 fw_speeds;
12102 
12103 		eee->eee_active = 0;
12104 		if (resp->eee_config_phy_addr &
12105 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12106 			eee->eee_active = 1;
12107 			fw_speeds = le16_to_cpu(
12108 				resp->link_partner_adv_eee_link_speed_mask);
12109 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12110 		}
12111 
12112 		/* Pull initial EEE config */
12113 		if (!chng_link_state) {
12114 			if (resp->eee_config_phy_addr &
12115 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12116 				eee->eee_enabled = 1;
12117 
12118 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12119 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12120 
12121 			if (resp->eee_config_phy_addr &
12122 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12123 				__le32 tmr;
12124 
12125 				eee->tx_lpi_enabled = 1;
12126 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12127 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12128 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12129 			}
12130 		}
12131 	}
12132 
12133 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12134 	if (bp->hwrm_spec_code >= 0x10504) {
12135 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12136 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12137 	}
12138 	/* TODO: need to add more logic to report VF link */
12139 	if (chng_link_state) {
12140 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12141 			link_info->link_state = BNXT_LINK_STATE_UP;
12142 		else
12143 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12144 		if (link_state != link_info->link_state)
12145 			bnxt_report_link(bp);
12146 	} else {
12147 		/* always link down if not require to update link state */
12148 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12149 	}
12150 	hwrm_req_drop(bp, req);
12151 
12152 	if (!BNXT_PHY_CFG_ABLE(bp))
12153 		return 0;
12154 
12155 	support_changed = bnxt_support_speed_dropped(link_info);
12156 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12157 		bnxt_hwrm_set_link_setting(bp, true, false);
12158 	return 0;
12159 }
12160 
12161 static void bnxt_get_port_module_status(struct bnxt *bp)
12162 {
12163 	struct bnxt_link_info *link_info = &bp->link_info;
12164 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12165 	u8 module_status;
12166 
12167 	if (bnxt_update_link(bp, true))
12168 		return;
12169 
12170 	module_status = link_info->module_status;
12171 	switch (module_status) {
12172 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12173 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12174 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12175 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12176 			    bp->pf.port_id);
12177 		if (bp->hwrm_spec_code >= 0x10201) {
12178 			netdev_warn(bp->dev, "Module part number %s\n",
12179 				    resp->phy_vendor_partnumber);
12180 		}
12181 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12182 			netdev_warn(bp->dev, "TX is disabled\n");
12183 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12184 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12185 	}
12186 }
12187 
12188 static void
12189 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12190 {
12191 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12192 		if (bp->hwrm_spec_code >= 0x10201)
12193 			req->auto_pause =
12194 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12195 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12196 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12197 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12198 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12199 		req->enables |=
12200 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12201 	} else {
12202 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12203 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12204 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12205 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12206 		req->enables |=
12207 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12208 		if (bp->hwrm_spec_code >= 0x10201) {
12209 			req->auto_pause = req->force_pause;
12210 			req->enables |= cpu_to_le32(
12211 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12212 		}
12213 	}
12214 }
12215 
12216 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12217 {
12218 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12219 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12220 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12221 			req->enables |=
12222 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12223 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12224 		} else if (bp->link_info.advertising) {
12225 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12226 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12227 		}
12228 		if (bp->link_info.advertising_pam4) {
12229 			req->enables |=
12230 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12231 			req->auto_link_pam4_speed_mask =
12232 				cpu_to_le16(bp->link_info.advertising_pam4);
12233 		}
12234 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12235 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12236 	} else {
12237 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12238 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12239 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12240 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12241 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12242 				   (u32)bp->link_info.req_link_speed);
12243 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12244 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12245 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12246 		} else {
12247 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12248 		}
12249 	}
12250 
12251 	/* tell chimp that the setting takes effect immediately */
12252 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12253 }
12254 
12255 int bnxt_hwrm_set_pause(struct bnxt *bp)
12256 {
12257 	struct hwrm_port_phy_cfg_input *req;
12258 	int rc;
12259 
12260 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12261 	if (rc)
12262 		return rc;
12263 
12264 	bnxt_hwrm_set_pause_common(bp, req);
12265 
12266 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12267 	    bp->link_info.force_link_chng)
12268 		bnxt_hwrm_set_link_common(bp, req);
12269 
12270 	rc = hwrm_req_send(bp, req);
12271 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12272 		/* since changing of pause setting doesn't trigger any link
12273 		 * change event, the driver needs to update the current pause
12274 		 * result upon successfully return of the phy_cfg command
12275 		 */
12276 		bp->link_info.pause =
12277 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12278 		bp->link_info.auto_pause_setting = 0;
12279 		if (!bp->link_info.force_link_chng)
12280 			bnxt_report_link(bp);
12281 	}
12282 	bp->link_info.force_link_chng = false;
12283 	return rc;
12284 }
12285 
12286 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12287 			      struct hwrm_port_phy_cfg_input *req)
12288 {
12289 	struct ethtool_keee *eee = &bp->eee;
12290 
12291 	if (eee->eee_enabled) {
12292 		u16 eee_speeds;
12293 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12294 
12295 		if (eee->tx_lpi_enabled)
12296 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12297 		else
12298 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12299 
12300 		req->flags |= cpu_to_le32(flags);
12301 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12302 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12303 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12304 	} else {
12305 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12306 	}
12307 }
12308 
12309 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12310 {
12311 	struct hwrm_port_phy_cfg_input *req;
12312 	int rc;
12313 
12314 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12315 	if (rc)
12316 		return rc;
12317 
12318 	if (set_pause)
12319 		bnxt_hwrm_set_pause_common(bp, req);
12320 
12321 	bnxt_hwrm_set_link_common(bp, req);
12322 
12323 	if (set_eee)
12324 		bnxt_hwrm_set_eee(bp, req);
12325 	return hwrm_req_send(bp, req);
12326 }
12327 
12328 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12329 {
12330 	struct hwrm_port_phy_cfg_input *req;
12331 	int rc;
12332 
12333 	if (!BNXT_SINGLE_PF(bp))
12334 		return 0;
12335 
12336 	if (pci_num_vf(bp->pdev) &&
12337 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12338 		return 0;
12339 
12340 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12341 	if (rc)
12342 		return rc;
12343 
12344 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12345 	rc = hwrm_req_send(bp, req);
12346 	if (!rc) {
12347 		mutex_lock(&bp->link_lock);
12348 		/* Device is not obliged link down in certain scenarios, even
12349 		 * when forced. Setting the state unknown is consistent with
12350 		 * driver startup and will force link state to be reported
12351 		 * during subsequent open based on PORT_PHY_QCFG.
12352 		 */
12353 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12354 		mutex_unlock(&bp->link_lock);
12355 	}
12356 	return rc;
12357 }
12358 
12359 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12360 {
12361 #ifdef CONFIG_TEE_BNXT_FW
12362 	int rc = tee_bnxt_fw_load();
12363 
12364 	if (rc)
12365 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12366 
12367 	return rc;
12368 #else
12369 	netdev_err(bp->dev, "OP-TEE not supported\n");
12370 	return -ENODEV;
12371 #endif
12372 }
12373 
12374 static int bnxt_try_recover_fw(struct bnxt *bp)
12375 {
12376 	if (bp->fw_health && bp->fw_health->status_reliable) {
12377 		int retry = 0, rc;
12378 		u32 sts;
12379 
12380 		do {
12381 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12382 			rc = bnxt_hwrm_poll(bp);
12383 			if (!BNXT_FW_IS_BOOTING(sts) &&
12384 			    !BNXT_FW_IS_RECOVERING(sts))
12385 				break;
12386 			retry++;
12387 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12388 
12389 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12390 			netdev_err(bp->dev,
12391 				   "Firmware not responding, status: 0x%x\n",
12392 				   sts);
12393 			rc = -ENODEV;
12394 		}
12395 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12396 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12397 			return bnxt_fw_reset_via_optee(bp);
12398 		}
12399 		return rc;
12400 	}
12401 
12402 	return -ENODEV;
12403 }
12404 
12405 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12406 {
12407 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12408 
12409 	if (!BNXT_NEW_RM(bp))
12410 		return; /* no resource reservations required */
12411 
12412 	hw_resc->resv_cp_rings = 0;
12413 	hw_resc->resv_stat_ctxs = 0;
12414 	hw_resc->resv_irqs = 0;
12415 	hw_resc->resv_tx_rings = 0;
12416 	hw_resc->resv_rx_rings = 0;
12417 	hw_resc->resv_hw_ring_grps = 0;
12418 	hw_resc->resv_vnics = 0;
12419 	hw_resc->resv_rsscos_ctxs = 0;
12420 	if (!fw_reset) {
12421 		bp->tx_nr_rings = 0;
12422 		bp->rx_nr_rings = 0;
12423 	}
12424 }
12425 
12426 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12427 {
12428 	int rc;
12429 
12430 	if (!BNXT_NEW_RM(bp))
12431 		return 0; /* no resource reservations required */
12432 
12433 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12434 	if (rc)
12435 		netdev_err(bp->dev, "resc_qcaps failed\n");
12436 
12437 	bnxt_clear_reservations(bp, fw_reset);
12438 
12439 	return rc;
12440 }
12441 
12442 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12443 {
12444 	struct hwrm_func_drv_if_change_output *resp;
12445 	struct hwrm_func_drv_if_change_input *req;
12446 	bool resc_reinit = false;
12447 	bool caps_change = false;
12448 	int rc, retry = 0;
12449 	bool fw_reset;
12450 	u32 flags = 0;
12451 
12452 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12453 	bp->fw_reset_state = 0;
12454 
12455 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12456 		return 0;
12457 
12458 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12459 	if (rc)
12460 		return rc;
12461 
12462 	if (up)
12463 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12464 	resp = hwrm_req_hold(bp, req);
12465 
12466 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12467 	while (retry < BNXT_FW_IF_RETRY) {
12468 		rc = hwrm_req_send(bp, req);
12469 		if (rc != -EAGAIN)
12470 			break;
12471 
12472 		msleep(50);
12473 		retry++;
12474 	}
12475 
12476 	if (rc == -EAGAIN) {
12477 		hwrm_req_drop(bp, req);
12478 		return rc;
12479 	} else if (!rc) {
12480 		flags = le32_to_cpu(resp->flags);
12481 	} else if (up) {
12482 		rc = bnxt_try_recover_fw(bp);
12483 		fw_reset = true;
12484 	}
12485 	hwrm_req_drop(bp, req);
12486 	if (rc)
12487 		return rc;
12488 
12489 	if (!up) {
12490 		bnxt_inv_fw_health_reg(bp);
12491 		return 0;
12492 	}
12493 
12494 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12495 		resc_reinit = true;
12496 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12497 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12498 		fw_reset = true;
12499 	else
12500 		bnxt_remap_fw_health_regs(bp);
12501 
12502 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12503 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12504 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12505 		return -ENODEV;
12506 	}
12507 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12508 		caps_change = true;
12509 
12510 	if (resc_reinit || fw_reset || caps_change) {
12511 		if (fw_reset || caps_change) {
12512 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12513 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12514 				bnxt_ulp_irq_stop(bp);
12515 			bnxt_free_ctx_mem(bp, false);
12516 			bnxt_dcb_free(bp);
12517 			rc = bnxt_fw_init_one(bp);
12518 			if (rc) {
12519 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12520 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12521 				return rc;
12522 			}
12523 			/* IRQ will be initialized later in bnxt_request_irq()*/
12524 			bnxt_clear_int_mode(bp);
12525 		}
12526 		rc = bnxt_cancel_reservations(bp, fw_reset);
12527 	}
12528 	return rc;
12529 }
12530 
12531 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12532 {
12533 	struct hwrm_port_led_qcaps_output *resp;
12534 	struct hwrm_port_led_qcaps_input *req;
12535 	struct bnxt_pf_info *pf = &bp->pf;
12536 	int rc;
12537 
12538 	bp->num_leds = 0;
12539 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12540 		return 0;
12541 
12542 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12543 	if (rc)
12544 		return rc;
12545 
12546 	req->port_id = cpu_to_le16(pf->port_id);
12547 	resp = hwrm_req_hold(bp, req);
12548 	rc = hwrm_req_send(bp, req);
12549 	if (rc) {
12550 		hwrm_req_drop(bp, req);
12551 		return rc;
12552 	}
12553 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12554 		int i;
12555 
12556 		bp->num_leds = resp->num_leds;
12557 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12558 						 bp->num_leds);
12559 		for (i = 0; i < bp->num_leds; i++) {
12560 			struct bnxt_led_info *led = &bp->leds[i];
12561 			__le16 caps = led->led_state_caps;
12562 
12563 			if (!led->led_group_id ||
12564 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12565 				bp->num_leds = 0;
12566 				break;
12567 			}
12568 		}
12569 	}
12570 	hwrm_req_drop(bp, req);
12571 	return 0;
12572 }
12573 
12574 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12575 {
12576 	struct hwrm_wol_filter_alloc_output *resp;
12577 	struct hwrm_wol_filter_alloc_input *req;
12578 	int rc;
12579 
12580 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12581 	if (rc)
12582 		return rc;
12583 
12584 	req->port_id = cpu_to_le16(bp->pf.port_id);
12585 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12586 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12587 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12588 
12589 	resp = hwrm_req_hold(bp, req);
12590 	rc = hwrm_req_send(bp, req);
12591 	if (!rc)
12592 		bp->wol_filter_id = resp->wol_filter_id;
12593 	hwrm_req_drop(bp, req);
12594 	return rc;
12595 }
12596 
12597 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12598 {
12599 	struct hwrm_wol_filter_free_input *req;
12600 	int rc;
12601 
12602 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12603 	if (rc)
12604 		return rc;
12605 
12606 	req->port_id = cpu_to_le16(bp->pf.port_id);
12607 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12608 	req->wol_filter_id = bp->wol_filter_id;
12609 
12610 	return hwrm_req_send(bp, req);
12611 }
12612 
12613 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12614 {
12615 	struct hwrm_wol_filter_qcfg_output *resp;
12616 	struct hwrm_wol_filter_qcfg_input *req;
12617 	u16 next_handle = 0;
12618 	int rc;
12619 
12620 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12621 	if (rc)
12622 		return rc;
12623 
12624 	req->port_id = cpu_to_le16(bp->pf.port_id);
12625 	req->handle = cpu_to_le16(handle);
12626 	resp = hwrm_req_hold(bp, req);
12627 	rc = hwrm_req_send(bp, req);
12628 	if (!rc) {
12629 		next_handle = le16_to_cpu(resp->next_handle);
12630 		if (next_handle != 0) {
12631 			if (resp->wol_type ==
12632 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12633 				bp->wol = 1;
12634 				bp->wol_filter_id = resp->wol_filter_id;
12635 			}
12636 		}
12637 	}
12638 	hwrm_req_drop(bp, req);
12639 	return next_handle;
12640 }
12641 
12642 static void bnxt_get_wol_settings(struct bnxt *bp)
12643 {
12644 	u16 handle = 0;
12645 
12646 	bp->wol = 0;
12647 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12648 		return;
12649 
12650 	do {
12651 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12652 	} while (handle && handle != 0xffff);
12653 }
12654 
12655 static bool bnxt_eee_config_ok(struct bnxt *bp)
12656 {
12657 	struct ethtool_keee *eee = &bp->eee;
12658 	struct bnxt_link_info *link_info = &bp->link_info;
12659 
12660 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12661 		return true;
12662 
12663 	if (eee->eee_enabled) {
12664 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12665 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12666 
12667 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12668 
12669 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12670 			eee->eee_enabled = 0;
12671 			return false;
12672 		}
12673 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12674 			linkmode_and(eee->advertised, advertising,
12675 				     eee->supported);
12676 			return false;
12677 		}
12678 	}
12679 	return true;
12680 }
12681 
12682 static int bnxt_update_phy_setting(struct bnxt *bp)
12683 {
12684 	int rc;
12685 	bool update_link = false;
12686 	bool update_pause = false;
12687 	bool update_eee = false;
12688 	struct bnxt_link_info *link_info = &bp->link_info;
12689 
12690 	rc = bnxt_update_link(bp, true);
12691 	if (rc) {
12692 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12693 			   rc);
12694 		return rc;
12695 	}
12696 	if (!BNXT_SINGLE_PF(bp))
12697 		return 0;
12698 
12699 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12700 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12701 	    link_info->req_flow_ctrl)
12702 		update_pause = true;
12703 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12704 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12705 		update_pause = true;
12706 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12707 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12708 			update_link = true;
12709 		if (bnxt_force_speed_updated(link_info))
12710 			update_link = true;
12711 		if (link_info->req_duplex != link_info->duplex_setting)
12712 			update_link = true;
12713 	} else {
12714 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12715 			update_link = true;
12716 		if (bnxt_auto_speed_updated(link_info))
12717 			update_link = true;
12718 	}
12719 
12720 	/* The last close may have shutdown the link, so need to call
12721 	 * PHY_CFG to bring it back up.
12722 	 */
12723 	if (!BNXT_LINK_IS_UP(bp))
12724 		update_link = true;
12725 
12726 	if (!bnxt_eee_config_ok(bp))
12727 		update_eee = true;
12728 
12729 	if (update_link)
12730 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12731 	else if (update_pause)
12732 		rc = bnxt_hwrm_set_pause(bp);
12733 	if (rc) {
12734 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12735 			   rc);
12736 		return rc;
12737 	}
12738 
12739 	return rc;
12740 }
12741 
12742 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12743 
12744 static int bnxt_reinit_after_abort(struct bnxt *bp)
12745 {
12746 	int rc;
12747 
12748 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12749 		return -EBUSY;
12750 
12751 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12752 		return -ENODEV;
12753 
12754 	rc = bnxt_fw_init_one(bp);
12755 	if (!rc) {
12756 		bnxt_clear_int_mode(bp);
12757 		rc = bnxt_init_int_mode(bp);
12758 		if (!rc) {
12759 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12760 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12761 		}
12762 	}
12763 	return rc;
12764 }
12765 
12766 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12767 {
12768 	struct bnxt_ntuple_filter *ntp_fltr;
12769 	struct bnxt_l2_filter *l2_fltr;
12770 
12771 	if (list_empty(&fltr->list))
12772 		return;
12773 
12774 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12775 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12776 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12777 		atomic_inc(&l2_fltr->refcnt);
12778 		ntp_fltr->l2_fltr = l2_fltr;
12779 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12780 			bnxt_del_ntp_filter(bp, ntp_fltr);
12781 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12782 				   fltr->sw_id);
12783 		}
12784 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12785 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12786 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12787 			bnxt_del_l2_filter(bp, l2_fltr);
12788 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12789 				   fltr->sw_id);
12790 		}
12791 	}
12792 }
12793 
12794 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12795 {
12796 	struct bnxt_filter_base *usr_fltr, *tmp;
12797 
12798 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12799 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12800 }
12801 
12802 static int bnxt_set_xps_mapping(struct bnxt *bp)
12803 {
12804 	int numa_node = dev_to_node(&bp->pdev->dev);
12805 	unsigned int q_idx, map_idx, cpu, i;
12806 	const struct cpumask *cpu_mask_ptr;
12807 	int nr_cpus = num_online_cpus();
12808 	cpumask_t *q_map;
12809 	int rc = 0;
12810 
12811 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12812 	if (!q_map)
12813 		return -ENOMEM;
12814 
12815 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12816 	 * Each TC has the same number of TX queues. The nth TX queue for each
12817 	 * TC will have the same CPU mask.
12818 	 */
12819 	for (i = 0; i < nr_cpus; i++) {
12820 		map_idx = i % bp->tx_nr_rings_per_tc;
12821 		cpu = cpumask_local_spread(i, numa_node);
12822 		cpu_mask_ptr = get_cpu_mask(cpu);
12823 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12824 	}
12825 
12826 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12827 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12828 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12829 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12830 		if (rc) {
12831 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12832 				    q_idx);
12833 			break;
12834 		}
12835 	}
12836 
12837 	kfree(q_map);
12838 
12839 	return rc;
12840 }
12841 
12842 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12843 {
12844 	int rc = 0;
12845 
12846 	netif_carrier_off(bp->dev);
12847 	if (irq_re_init) {
12848 		/* Reserve rings now if none were reserved at driver probe. */
12849 		rc = bnxt_init_dflt_ring_mode(bp);
12850 		if (rc) {
12851 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12852 			return rc;
12853 		}
12854 	}
12855 	rc = bnxt_reserve_rings(bp, irq_re_init);
12856 	if (rc)
12857 		return rc;
12858 
12859 	rc = bnxt_alloc_mem(bp, irq_re_init);
12860 	if (rc) {
12861 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12862 		goto open_err_free_mem;
12863 	}
12864 
12865 	if (irq_re_init) {
12866 		bnxt_init_napi(bp);
12867 		rc = bnxt_request_irq(bp);
12868 		if (rc) {
12869 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12870 			goto open_err_irq;
12871 		}
12872 	}
12873 
12874 	rc = bnxt_init_nic(bp, irq_re_init);
12875 	if (rc) {
12876 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12877 		goto open_err_irq;
12878 	}
12879 
12880 	bnxt_enable_napi(bp);
12881 	bnxt_debug_dev_init(bp);
12882 
12883 	if (link_re_init) {
12884 		mutex_lock(&bp->link_lock);
12885 		rc = bnxt_update_phy_setting(bp);
12886 		mutex_unlock(&bp->link_lock);
12887 		if (rc) {
12888 			netdev_warn(bp->dev, "failed to update phy settings\n");
12889 			if (BNXT_SINGLE_PF(bp)) {
12890 				bp->link_info.phy_retry = true;
12891 				bp->link_info.phy_retry_expires =
12892 					jiffies + 5 * HZ;
12893 			}
12894 		}
12895 	}
12896 
12897 	if (irq_re_init) {
12898 		udp_tunnel_nic_reset_ntf(bp->dev);
12899 		rc = bnxt_set_xps_mapping(bp);
12900 		if (rc)
12901 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12902 	}
12903 
12904 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12905 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12906 			static_branch_enable(&bnxt_xdp_locking_key);
12907 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12908 		static_branch_disable(&bnxt_xdp_locking_key);
12909 	}
12910 	set_bit(BNXT_STATE_OPEN, &bp->state);
12911 	bnxt_enable_int(bp);
12912 	/* Enable TX queues */
12913 	bnxt_tx_enable(bp);
12914 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12915 	/* Poll link status and check for SFP+ module status */
12916 	mutex_lock(&bp->link_lock);
12917 	bnxt_get_port_module_status(bp);
12918 	mutex_unlock(&bp->link_lock);
12919 
12920 	/* VF-reps may need to be re-opened after the PF is re-opened */
12921 	if (BNXT_PF(bp))
12922 		bnxt_vf_reps_open(bp);
12923 	bnxt_ptp_init_rtc(bp, true);
12924 	bnxt_ptp_cfg_tstamp_filters(bp);
12925 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12926 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12927 	bnxt_cfg_usr_fltrs(bp);
12928 	return 0;
12929 
12930 open_err_irq:
12931 	bnxt_del_napi(bp);
12932 
12933 open_err_free_mem:
12934 	bnxt_free_skbs(bp);
12935 	bnxt_free_irq(bp);
12936 	bnxt_free_mem(bp, true);
12937 	return rc;
12938 }
12939 
12940 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12941 {
12942 	int rc = 0;
12943 
12944 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12945 		rc = -EIO;
12946 	if (!rc)
12947 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12948 	if (rc) {
12949 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12950 		netif_close(bp->dev);
12951 	}
12952 	return rc;
12953 }
12954 
12955 /* netdev instance lock held, open the NIC half way by allocating all
12956  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12957  * for offline self tests.
12958  */
12959 int bnxt_half_open_nic(struct bnxt *bp)
12960 {
12961 	int rc = 0;
12962 
12963 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12964 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12965 		rc = -ENODEV;
12966 		goto half_open_err;
12967 	}
12968 
12969 	rc = bnxt_alloc_mem(bp, true);
12970 	if (rc) {
12971 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12972 		goto half_open_err;
12973 	}
12974 	bnxt_init_napi(bp);
12975 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12976 	rc = bnxt_init_nic(bp, true);
12977 	if (rc) {
12978 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12979 		bnxt_del_napi(bp);
12980 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12981 		goto half_open_err;
12982 	}
12983 	return 0;
12984 
12985 half_open_err:
12986 	bnxt_free_skbs(bp);
12987 	bnxt_free_mem(bp, true);
12988 	netif_close(bp->dev);
12989 	return rc;
12990 }
12991 
12992 /* netdev instance lock held, this call can only be made after a previous
12993  * successful call to bnxt_half_open_nic().
12994  */
12995 void bnxt_half_close_nic(struct bnxt *bp)
12996 {
12997 	bnxt_hwrm_resource_free(bp, false, true);
12998 	bnxt_del_napi(bp);
12999 	bnxt_free_skbs(bp);
13000 	bnxt_free_mem(bp, true);
13001 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
13002 }
13003 
13004 void bnxt_reenable_sriov(struct bnxt *bp)
13005 {
13006 	if (BNXT_PF(bp)) {
13007 		struct bnxt_pf_info *pf = &bp->pf;
13008 		int n = pf->active_vfs;
13009 
13010 		if (n)
13011 			bnxt_cfg_hw_sriov(bp, &n, true);
13012 	}
13013 }
13014 
13015 static int bnxt_open(struct net_device *dev)
13016 {
13017 	struct bnxt *bp = netdev_priv(dev);
13018 	int rc;
13019 
13020 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13021 		rc = bnxt_reinit_after_abort(bp);
13022 		if (rc) {
13023 			if (rc == -EBUSY)
13024 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13025 			else
13026 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13027 			return -ENODEV;
13028 		}
13029 	}
13030 
13031 	rc = bnxt_hwrm_if_change(bp, true);
13032 	if (rc)
13033 		return rc;
13034 
13035 	rc = __bnxt_open_nic(bp, true, true);
13036 	if (rc) {
13037 		bnxt_hwrm_if_change(bp, false);
13038 	} else {
13039 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13040 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13041 				bnxt_queue_sp_work(bp,
13042 						   BNXT_RESTART_ULP_SP_EVENT);
13043 		}
13044 	}
13045 
13046 	return rc;
13047 }
13048 
13049 static bool bnxt_drv_busy(struct bnxt *bp)
13050 {
13051 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13052 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13053 }
13054 
13055 static void bnxt_get_ring_stats(struct bnxt *bp,
13056 				struct rtnl_link_stats64 *stats);
13057 
13058 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13059 			     bool link_re_init)
13060 {
13061 	/* Close the VF-reps before closing PF */
13062 	if (BNXT_PF(bp))
13063 		bnxt_vf_reps_close(bp);
13064 
13065 	/* Change device state to avoid TX queue wake up's */
13066 	bnxt_tx_disable(bp);
13067 
13068 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13069 	smp_mb__after_atomic();
13070 	while (bnxt_drv_busy(bp))
13071 		msleep(20);
13072 
13073 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13074 		bnxt_clear_rss_ctxs(bp);
13075 	/* Flush rings and disable interrupts */
13076 	bnxt_shutdown_nic(bp, irq_re_init);
13077 
13078 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13079 
13080 	bnxt_debug_dev_exit(bp);
13081 	bnxt_disable_napi(bp);
13082 	timer_delete_sync(&bp->timer);
13083 	bnxt_free_skbs(bp);
13084 
13085 	/* Save ring stats before shutdown */
13086 	if (bp->bnapi && irq_re_init) {
13087 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13088 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13089 	}
13090 	if (irq_re_init) {
13091 		bnxt_free_irq(bp);
13092 		bnxt_del_napi(bp);
13093 	}
13094 	bnxt_free_mem(bp, irq_re_init);
13095 }
13096 
13097 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13098 {
13099 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13100 		/* If we get here, it means firmware reset is in progress
13101 		 * while we are trying to close.  We can safely proceed with
13102 		 * the close because we are holding netdev instance lock.
13103 		 * Some firmware messages may fail as we proceed to close.
13104 		 * We set the ABORT_ERR flag here so that the FW reset thread
13105 		 * will later abort when it gets the netdev instance lock
13106 		 * and sees the flag.
13107 		 */
13108 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13109 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13110 	}
13111 
13112 #ifdef CONFIG_BNXT_SRIOV
13113 	if (bp->sriov_cfg) {
13114 		int rc;
13115 
13116 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13117 						      !bp->sriov_cfg,
13118 						      BNXT_SRIOV_CFG_WAIT_TMO);
13119 		if (!rc)
13120 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13121 		else if (rc < 0)
13122 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13123 	}
13124 #endif
13125 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13126 }
13127 
13128 static int bnxt_close(struct net_device *dev)
13129 {
13130 	struct bnxt *bp = netdev_priv(dev);
13131 
13132 	bnxt_close_nic(bp, true, true);
13133 	bnxt_hwrm_shutdown_link(bp);
13134 	bnxt_hwrm_if_change(bp, false);
13135 	return 0;
13136 }
13137 
13138 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13139 				   u16 *val)
13140 {
13141 	struct hwrm_port_phy_mdio_read_output *resp;
13142 	struct hwrm_port_phy_mdio_read_input *req;
13143 	int rc;
13144 
13145 	if (bp->hwrm_spec_code < 0x10a00)
13146 		return -EOPNOTSUPP;
13147 
13148 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13149 	if (rc)
13150 		return rc;
13151 
13152 	req->port_id = cpu_to_le16(bp->pf.port_id);
13153 	req->phy_addr = phy_addr;
13154 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13155 	if (mdio_phy_id_is_c45(phy_addr)) {
13156 		req->cl45_mdio = 1;
13157 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13158 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13159 		req->reg_addr = cpu_to_le16(reg);
13160 	}
13161 
13162 	resp = hwrm_req_hold(bp, req);
13163 	rc = hwrm_req_send(bp, req);
13164 	if (!rc)
13165 		*val = le16_to_cpu(resp->reg_data);
13166 	hwrm_req_drop(bp, req);
13167 	return rc;
13168 }
13169 
13170 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13171 				    u16 val)
13172 {
13173 	struct hwrm_port_phy_mdio_write_input *req;
13174 	int rc;
13175 
13176 	if (bp->hwrm_spec_code < 0x10a00)
13177 		return -EOPNOTSUPP;
13178 
13179 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13180 	if (rc)
13181 		return rc;
13182 
13183 	req->port_id = cpu_to_le16(bp->pf.port_id);
13184 	req->phy_addr = phy_addr;
13185 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13186 	if (mdio_phy_id_is_c45(phy_addr)) {
13187 		req->cl45_mdio = 1;
13188 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13189 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13190 		req->reg_addr = cpu_to_le16(reg);
13191 	}
13192 	req->reg_data = cpu_to_le16(val);
13193 
13194 	return hwrm_req_send(bp, req);
13195 }
13196 
13197 /* netdev instance lock held */
13198 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13199 {
13200 	struct mii_ioctl_data *mdio = if_mii(ifr);
13201 	struct bnxt *bp = netdev_priv(dev);
13202 	int rc;
13203 
13204 	switch (cmd) {
13205 	case SIOCGMIIPHY:
13206 		mdio->phy_id = bp->link_info.phy_addr;
13207 
13208 		fallthrough;
13209 	case SIOCGMIIREG: {
13210 		u16 mii_regval = 0;
13211 
13212 		if (!netif_running(dev))
13213 			return -EAGAIN;
13214 
13215 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13216 					     &mii_regval);
13217 		mdio->val_out = mii_regval;
13218 		return rc;
13219 	}
13220 
13221 	case SIOCSMIIREG:
13222 		if (!netif_running(dev))
13223 			return -EAGAIN;
13224 
13225 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13226 						mdio->val_in);
13227 
13228 	case SIOCSHWTSTAMP:
13229 		return bnxt_hwtstamp_set(dev, ifr);
13230 
13231 	case SIOCGHWTSTAMP:
13232 		return bnxt_hwtstamp_get(dev, ifr);
13233 
13234 	default:
13235 		/* do nothing */
13236 		break;
13237 	}
13238 	return -EOPNOTSUPP;
13239 }
13240 
13241 static void bnxt_get_ring_stats(struct bnxt *bp,
13242 				struct rtnl_link_stats64 *stats)
13243 {
13244 	int i;
13245 
13246 	for (i = 0; i < bp->cp_nr_rings; i++) {
13247 		struct bnxt_napi *bnapi = bp->bnapi[i];
13248 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13249 		u64 *sw = cpr->stats.sw_stats;
13250 
13251 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13252 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13253 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13254 
13255 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13256 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13257 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13258 
13259 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13260 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13261 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13262 
13263 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13264 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13265 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13266 
13267 		stats->rx_missed_errors +=
13268 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13269 
13270 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13271 
13272 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13273 
13274 		stats->rx_dropped +=
13275 			cpr->sw_stats->rx.rx_netpoll_discards +
13276 			cpr->sw_stats->rx.rx_oom_discards;
13277 	}
13278 }
13279 
13280 static void bnxt_add_prev_stats(struct bnxt *bp,
13281 				struct rtnl_link_stats64 *stats)
13282 {
13283 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13284 
13285 	stats->rx_packets += prev_stats->rx_packets;
13286 	stats->tx_packets += prev_stats->tx_packets;
13287 	stats->rx_bytes += prev_stats->rx_bytes;
13288 	stats->tx_bytes += prev_stats->tx_bytes;
13289 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13290 	stats->multicast += prev_stats->multicast;
13291 	stats->rx_dropped += prev_stats->rx_dropped;
13292 	stats->tx_dropped += prev_stats->tx_dropped;
13293 }
13294 
13295 static void
13296 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13297 {
13298 	struct bnxt *bp = netdev_priv(dev);
13299 
13300 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13301 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13302 	 * we check the BNXT_STATE_OPEN flag.
13303 	 */
13304 	smp_mb__after_atomic();
13305 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13306 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13307 		*stats = bp->net_stats_prev;
13308 		return;
13309 	}
13310 
13311 	bnxt_get_ring_stats(bp, stats);
13312 	bnxt_add_prev_stats(bp, stats);
13313 
13314 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13315 		u64 *rx = bp->port_stats.sw_stats;
13316 		u64 *tx = bp->port_stats.sw_stats +
13317 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13318 
13319 		stats->rx_crc_errors =
13320 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13321 		stats->rx_frame_errors =
13322 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13323 		stats->rx_length_errors =
13324 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13325 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13326 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13327 		stats->rx_errors =
13328 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13329 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13330 		stats->collisions =
13331 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13332 		stats->tx_fifo_errors =
13333 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13334 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13335 	}
13336 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13337 }
13338 
13339 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13340 					struct bnxt_total_ring_err_stats *stats,
13341 					struct bnxt_cp_ring_info *cpr)
13342 {
13343 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13344 	u64 *hw_stats = cpr->stats.sw_stats;
13345 
13346 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13347 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13348 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13349 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13350 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13351 	stats->rx_total_ring_discards +=
13352 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13353 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13354 	stats->tx_total_ring_discards +=
13355 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13356 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13357 }
13358 
13359 void bnxt_get_ring_err_stats(struct bnxt *bp,
13360 			     struct bnxt_total_ring_err_stats *stats)
13361 {
13362 	int i;
13363 
13364 	for (i = 0; i < bp->cp_nr_rings; i++)
13365 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13366 }
13367 
13368 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13369 {
13370 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13371 	struct net_device *dev = bp->dev;
13372 	struct netdev_hw_addr *ha;
13373 	u8 *haddr;
13374 	int mc_count = 0;
13375 	bool update = false;
13376 	int off = 0;
13377 
13378 	netdev_for_each_mc_addr(ha, dev) {
13379 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13380 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13381 			vnic->mc_list_count = 0;
13382 			return false;
13383 		}
13384 		haddr = ha->addr;
13385 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13386 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13387 			update = true;
13388 		}
13389 		off += ETH_ALEN;
13390 		mc_count++;
13391 	}
13392 	if (mc_count)
13393 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13394 
13395 	if (mc_count != vnic->mc_list_count) {
13396 		vnic->mc_list_count = mc_count;
13397 		update = true;
13398 	}
13399 	return update;
13400 }
13401 
13402 static bool bnxt_uc_list_updated(struct bnxt *bp)
13403 {
13404 	struct net_device *dev = bp->dev;
13405 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13406 	struct netdev_hw_addr *ha;
13407 	int off = 0;
13408 
13409 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13410 		return true;
13411 
13412 	netdev_for_each_uc_addr(ha, dev) {
13413 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13414 			return true;
13415 
13416 		off += ETH_ALEN;
13417 	}
13418 	return false;
13419 }
13420 
13421 static void bnxt_set_rx_mode(struct net_device *dev)
13422 {
13423 	struct bnxt *bp = netdev_priv(dev);
13424 	struct bnxt_vnic_info *vnic;
13425 	bool mc_update = false;
13426 	bool uc_update;
13427 	u32 mask;
13428 
13429 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13430 		return;
13431 
13432 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13433 	mask = vnic->rx_mask;
13434 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13435 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13436 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13437 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13438 
13439 	if (dev->flags & IFF_PROMISC)
13440 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13441 
13442 	uc_update = bnxt_uc_list_updated(bp);
13443 
13444 	if (dev->flags & IFF_BROADCAST)
13445 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13446 	if (dev->flags & IFF_ALLMULTI) {
13447 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13448 		vnic->mc_list_count = 0;
13449 	} else if (dev->flags & IFF_MULTICAST) {
13450 		mc_update = bnxt_mc_list_updated(bp, &mask);
13451 	}
13452 
13453 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13454 		vnic->rx_mask = mask;
13455 
13456 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13457 	}
13458 }
13459 
13460 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13461 {
13462 	struct net_device *dev = bp->dev;
13463 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13464 	struct netdev_hw_addr *ha;
13465 	int i, off = 0, rc;
13466 	bool uc_update;
13467 
13468 	netif_addr_lock_bh(dev);
13469 	uc_update = bnxt_uc_list_updated(bp);
13470 	netif_addr_unlock_bh(dev);
13471 
13472 	if (!uc_update)
13473 		goto skip_uc;
13474 
13475 	for (i = 1; i < vnic->uc_filter_count; i++) {
13476 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13477 
13478 		bnxt_hwrm_l2_filter_free(bp, fltr);
13479 		bnxt_del_l2_filter(bp, fltr);
13480 	}
13481 
13482 	vnic->uc_filter_count = 1;
13483 
13484 	netif_addr_lock_bh(dev);
13485 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13486 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13487 	} else {
13488 		netdev_for_each_uc_addr(ha, dev) {
13489 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13490 			off += ETH_ALEN;
13491 			vnic->uc_filter_count++;
13492 		}
13493 	}
13494 	netif_addr_unlock_bh(dev);
13495 
13496 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13497 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13498 		if (rc) {
13499 			if (BNXT_VF(bp) && rc == -ENODEV) {
13500 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13501 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13502 				else
13503 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13504 				rc = 0;
13505 			} else {
13506 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13507 			}
13508 			vnic->uc_filter_count = i;
13509 			return rc;
13510 		}
13511 	}
13512 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13513 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13514 
13515 skip_uc:
13516 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13517 	    !bnxt_promisc_ok(bp))
13518 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13519 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13520 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13521 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13522 			    rc);
13523 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13524 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13525 		vnic->mc_list_count = 0;
13526 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13527 	}
13528 	if (rc)
13529 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13530 			   rc);
13531 
13532 	return rc;
13533 }
13534 
13535 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13536 {
13537 #ifdef CONFIG_BNXT_SRIOV
13538 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13539 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13540 
13541 		/* No minimum rings were provisioned by the PF.  Don't
13542 		 * reserve rings by default when device is down.
13543 		 */
13544 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13545 			return true;
13546 
13547 		if (!netif_running(bp->dev))
13548 			return false;
13549 	}
13550 #endif
13551 	return true;
13552 }
13553 
13554 /* If the chip and firmware supports RFS */
13555 static bool bnxt_rfs_supported(struct bnxt *bp)
13556 {
13557 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13558 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13559 			return true;
13560 		return false;
13561 	}
13562 	/* 212 firmware is broken for aRFS */
13563 	if (BNXT_FW_MAJ(bp) == 212)
13564 		return false;
13565 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13566 		return true;
13567 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13568 		return true;
13569 	return false;
13570 }
13571 
13572 /* If runtime conditions support RFS */
13573 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13574 {
13575 	struct bnxt_hw_rings hwr = {0};
13576 	int max_vnics, max_rss_ctxs;
13577 
13578 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13579 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13580 		return bnxt_rfs_supported(bp);
13581 
13582 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13583 		return false;
13584 
13585 	hwr.grp = bp->rx_nr_rings;
13586 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13587 	if (new_rss_ctx)
13588 		hwr.vnic++;
13589 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13590 	max_vnics = bnxt_get_max_func_vnics(bp);
13591 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13592 
13593 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13594 		if (bp->rx_nr_rings > 1)
13595 			netdev_warn(bp->dev,
13596 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13597 				    min(max_rss_ctxs - 1, max_vnics - 1));
13598 		return false;
13599 	}
13600 
13601 	if (!BNXT_NEW_RM(bp))
13602 		return true;
13603 
13604 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13605 	 * issue that will mess up the default VNIC if we reduce the
13606 	 * reservations.
13607 	 */
13608 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13609 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13610 		return true;
13611 
13612 	bnxt_hwrm_reserve_rings(bp, &hwr);
13613 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13614 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13615 		return true;
13616 
13617 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13618 	hwr.vnic = 1;
13619 	hwr.rss_ctx = 0;
13620 	bnxt_hwrm_reserve_rings(bp, &hwr);
13621 	return false;
13622 }
13623 
13624 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13625 					   netdev_features_t features)
13626 {
13627 	struct bnxt *bp = netdev_priv(dev);
13628 	netdev_features_t vlan_features;
13629 
13630 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13631 		features &= ~NETIF_F_NTUPLE;
13632 
13633 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13634 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13635 
13636 	if (!(features & NETIF_F_GRO))
13637 		features &= ~NETIF_F_GRO_HW;
13638 
13639 	if (features & NETIF_F_GRO_HW)
13640 		features &= ~NETIF_F_LRO;
13641 
13642 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13643 	 * turned on or off together.
13644 	 */
13645 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13646 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13647 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13648 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13649 		else if (vlan_features)
13650 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13651 	}
13652 #ifdef CONFIG_BNXT_SRIOV
13653 	if (BNXT_VF(bp) && bp->vf.vlan)
13654 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13655 #endif
13656 	return features;
13657 }
13658 
13659 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13660 				bool link_re_init, u32 flags, bool update_tpa)
13661 {
13662 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13663 	bp->flags = flags;
13664 	if (update_tpa)
13665 		bnxt_set_ring_params(bp);
13666 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13667 }
13668 
13669 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13670 {
13671 	bool update_tpa = false, update_ntuple = false;
13672 	struct bnxt *bp = netdev_priv(dev);
13673 	u32 flags = bp->flags;
13674 	u32 changes;
13675 	int rc = 0;
13676 	bool re_init = false;
13677 
13678 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13679 	if (features & NETIF_F_GRO_HW)
13680 		flags |= BNXT_FLAG_GRO;
13681 	else if (features & NETIF_F_LRO)
13682 		flags |= BNXT_FLAG_LRO;
13683 
13684 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13685 		flags &= ~BNXT_FLAG_TPA;
13686 
13687 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13688 		flags |= BNXT_FLAG_STRIP_VLAN;
13689 
13690 	if (features & NETIF_F_NTUPLE)
13691 		flags |= BNXT_FLAG_RFS;
13692 	else
13693 		bnxt_clear_usr_fltrs(bp, true);
13694 
13695 	changes = flags ^ bp->flags;
13696 	if (changes & BNXT_FLAG_TPA) {
13697 		update_tpa = true;
13698 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13699 		    (flags & BNXT_FLAG_TPA) == 0 ||
13700 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13701 			re_init = true;
13702 	}
13703 
13704 	if (changes & ~BNXT_FLAG_TPA)
13705 		re_init = true;
13706 
13707 	if (changes & BNXT_FLAG_RFS)
13708 		update_ntuple = true;
13709 
13710 	if (flags != bp->flags) {
13711 		u32 old_flags = bp->flags;
13712 
13713 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13714 			bp->flags = flags;
13715 			if (update_tpa)
13716 				bnxt_set_ring_params(bp);
13717 			return rc;
13718 		}
13719 
13720 		if (update_ntuple)
13721 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13722 
13723 		if (re_init)
13724 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13725 
13726 		if (update_tpa) {
13727 			bp->flags = flags;
13728 			rc = bnxt_set_tpa(bp,
13729 					  (flags & BNXT_FLAG_TPA) ?
13730 					  true : false);
13731 			if (rc)
13732 				bp->flags = old_flags;
13733 		}
13734 	}
13735 	return rc;
13736 }
13737 
13738 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13739 			      u8 **nextp)
13740 {
13741 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13742 	struct hop_jumbo_hdr *jhdr;
13743 	int hdr_count = 0;
13744 	u8 *nexthdr;
13745 	int start;
13746 
13747 	/* Check that there are at most 2 IPv6 extension headers, no
13748 	 * fragment header, and each is <= 64 bytes.
13749 	 */
13750 	start = nw_off + sizeof(*ip6h);
13751 	nexthdr = &ip6h->nexthdr;
13752 	while (ipv6_ext_hdr(*nexthdr)) {
13753 		struct ipv6_opt_hdr *hp;
13754 		int hdrlen;
13755 
13756 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13757 		    *nexthdr == NEXTHDR_FRAGMENT)
13758 			return false;
13759 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13760 					  skb_headlen(skb), NULL);
13761 		if (!hp)
13762 			return false;
13763 		if (*nexthdr == NEXTHDR_AUTH)
13764 			hdrlen = ipv6_authlen(hp);
13765 		else
13766 			hdrlen = ipv6_optlen(hp);
13767 
13768 		if (hdrlen > 64)
13769 			return false;
13770 
13771 		/* The ext header may be a hop-by-hop header inserted for
13772 		 * big TCP purposes. This will be removed before sending
13773 		 * from NIC, so do not count it.
13774 		 */
13775 		if (*nexthdr == NEXTHDR_HOP) {
13776 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13777 				goto increment_hdr;
13778 
13779 			jhdr = (struct hop_jumbo_hdr *)hp;
13780 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13781 			    jhdr->nexthdr != IPPROTO_TCP)
13782 				goto increment_hdr;
13783 
13784 			goto next_hdr;
13785 		}
13786 increment_hdr:
13787 		hdr_count++;
13788 next_hdr:
13789 		nexthdr = &hp->nexthdr;
13790 		start += hdrlen;
13791 	}
13792 	if (nextp) {
13793 		/* Caller will check inner protocol */
13794 		if (skb->encapsulation) {
13795 			*nextp = nexthdr;
13796 			return true;
13797 		}
13798 		*nextp = NULL;
13799 	}
13800 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13801 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13802 }
13803 
13804 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13805 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13806 {
13807 	struct udphdr *uh = udp_hdr(skb);
13808 	__be16 udp_port = uh->dest;
13809 
13810 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13811 	    udp_port != bp->vxlan_gpe_port)
13812 		return false;
13813 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13814 		struct ethhdr *eh = inner_eth_hdr(skb);
13815 
13816 		switch (eh->h_proto) {
13817 		case htons(ETH_P_IP):
13818 			return true;
13819 		case htons(ETH_P_IPV6):
13820 			return bnxt_exthdr_check(bp, skb,
13821 						 skb_inner_network_offset(skb),
13822 						 NULL);
13823 		}
13824 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13825 		return true;
13826 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13827 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13828 					 NULL);
13829 	}
13830 	return false;
13831 }
13832 
13833 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13834 {
13835 	switch (l4_proto) {
13836 	case IPPROTO_UDP:
13837 		return bnxt_udp_tunl_check(bp, skb);
13838 	case IPPROTO_IPIP:
13839 		return true;
13840 	case IPPROTO_GRE: {
13841 		switch (skb->inner_protocol) {
13842 		default:
13843 			return false;
13844 		case htons(ETH_P_IP):
13845 			return true;
13846 		case htons(ETH_P_IPV6):
13847 			fallthrough;
13848 		}
13849 	}
13850 	case IPPROTO_IPV6:
13851 		/* Check ext headers of inner ipv6 */
13852 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13853 					 NULL);
13854 	}
13855 	return false;
13856 }
13857 
13858 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13859 					     struct net_device *dev,
13860 					     netdev_features_t features)
13861 {
13862 	struct bnxt *bp = netdev_priv(dev);
13863 	u8 *l4_proto;
13864 
13865 	features = vlan_features_check(skb, features);
13866 	switch (vlan_get_protocol(skb)) {
13867 	case htons(ETH_P_IP):
13868 		if (!skb->encapsulation)
13869 			return features;
13870 		l4_proto = &ip_hdr(skb)->protocol;
13871 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13872 			return features;
13873 		break;
13874 	case htons(ETH_P_IPV6):
13875 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13876 				       &l4_proto))
13877 			break;
13878 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13879 			return features;
13880 		break;
13881 	}
13882 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13883 }
13884 
13885 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13886 			 u32 *reg_buf)
13887 {
13888 	struct hwrm_dbg_read_direct_output *resp;
13889 	struct hwrm_dbg_read_direct_input *req;
13890 	__le32 *dbg_reg_buf;
13891 	dma_addr_t mapping;
13892 	int rc, i;
13893 
13894 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13895 	if (rc)
13896 		return rc;
13897 
13898 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13899 					 &mapping);
13900 	if (!dbg_reg_buf) {
13901 		rc = -ENOMEM;
13902 		goto dbg_rd_reg_exit;
13903 	}
13904 
13905 	req->host_dest_addr = cpu_to_le64(mapping);
13906 
13907 	resp = hwrm_req_hold(bp, req);
13908 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13909 	req->read_len32 = cpu_to_le32(num_words);
13910 
13911 	rc = hwrm_req_send(bp, req);
13912 	if (rc || resp->error_code) {
13913 		rc = -EIO;
13914 		goto dbg_rd_reg_exit;
13915 	}
13916 	for (i = 0; i < num_words; i++)
13917 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13918 
13919 dbg_rd_reg_exit:
13920 	hwrm_req_drop(bp, req);
13921 	return rc;
13922 }
13923 
13924 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13925 				       u32 ring_id, u32 *prod, u32 *cons)
13926 {
13927 	struct hwrm_dbg_ring_info_get_output *resp;
13928 	struct hwrm_dbg_ring_info_get_input *req;
13929 	int rc;
13930 
13931 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13932 	if (rc)
13933 		return rc;
13934 
13935 	req->ring_type = ring_type;
13936 	req->fw_ring_id = cpu_to_le32(ring_id);
13937 	resp = hwrm_req_hold(bp, req);
13938 	rc = hwrm_req_send(bp, req);
13939 	if (!rc) {
13940 		*prod = le32_to_cpu(resp->producer_index);
13941 		*cons = le32_to_cpu(resp->consumer_index);
13942 	}
13943 	hwrm_req_drop(bp, req);
13944 	return rc;
13945 }
13946 
13947 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13948 {
13949 	struct bnxt_tx_ring_info *txr;
13950 	int i = bnapi->index, j;
13951 
13952 	bnxt_for_each_napi_tx(j, bnapi, txr)
13953 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13954 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13955 			    txr->tx_cons);
13956 }
13957 
13958 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13959 {
13960 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13961 	int i = bnapi->index;
13962 
13963 	if (!rxr)
13964 		return;
13965 
13966 	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",
13967 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13968 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13969 		    rxr->rx_sw_agg_prod);
13970 }
13971 
13972 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13973 {
13974 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13975 	int i = bnapi->index;
13976 
13977 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13978 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13979 }
13980 
13981 static void bnxt_dbg_dump_states(struct bnxt *bp)
13982 {
13983 	int i;
13984 	struct bnxt_napi *bnapi;
13985 
13986 	for (i = 0; i < bp->cp_nr_rings; i++) {
13987 		bnapi = bp->bnapi[i];
13988 		if (netif_msg_drv(bp)) {
13989 			bnxt_dump_tx_sw_state(bnapi);
13990 			bnxt_dump_rx_sw_state(bnapi);
13991 			bnxt_dump_cp_sw_state(bnapi);
13992 		}
13993 	}
13994 }
13995 
13996 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13997 {
13998 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13999 	struct hwrm_ring_reset_input *req;
14000 	struct bnxt_napi *bnapi = rxr->bnapi;
14001 	struct bnxt_cp_ring_info *cpr;
14002 	u16 cp_ring_id;
14003 	int rc;
14004 
14005 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
14006 	if (rc)
14007 		return rc;
14008 
14009 	cpr = &bnapi->cp_ring;
14010 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14011 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14012 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14013 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14014 	return hwrm_req_send_silent(bp, req);
14015 }
14016 
14017 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14018 {
14019 	if (!silent)
14020 		bnxt_dbg_dump_states(bp);
14021 	if (netif_running(bp->dev)) {
14022 		bnxt_close_nic(bp, !silent, false);
14023 		bnxt_open_nic(bp, !silent, false);
14024 	}
14025 }
14026 
14027 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14028 {
14029 	struct bnxt *bp = netdev_priv(dev);
14030 
14031 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14032 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14033 }
14034 
14035 static void bnxt_fw_health_check(struct bnxt *bp)
14036 {
14037 	struct bnxt_fw_health *fw_health = bp->fw_health;
14038 	struct pci_dev *pdev = bp->pdev;
14039 	u32 val;
14040 
14041 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14042 		return;
14043 
14044 	/* Make sure it is enabled before checking the tmr_counter. */
14045 	smp_rmb();
14046 	if (fw_health->tmr_counter) {
14047 		fw_health->tmr_counter--;
14048 		return;
14049 	}
14050 
14051 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14052 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14053 		fw_health->arrests++;
14054 		goto fw_reset;
14055 	}
14056 
14057 	fw_health->last_fw_heartbeat = val;
14058 
14059 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14060 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14061 		fw_health->discoveries++;
14062 		goto fw_reset;
14063 	}
14064 
14065 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14066 	return;
14067 
14068 fw_reset:
14069 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14070 }
14071 
14072 static void bnxt_timer(struct timer_list *t)
14073 {
14074 	struct bnxt *bp = timer_container_of(bp, t, timer);
14075 	struct net_device *dev = bp->dev;
14076 
14077 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14078 		return;
14079 
14080 	if (atomic_read(&bp->intr_sem) != 0)
14081 		goto bnxt_restart_timer;
14082 
14083 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14084 		bnxt_fw_health_check(bp);
14085 
14086 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14087 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14088 
14089 	if (bnxt_tc_flower_enabled(bp))
14090 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14091 
14092 #ifdef CONFIG_RFS_ACCEL
14093 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14094 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14095 #endif /*CONFIG_RFS_ACCEL*/
14096 
14097 	if (bp->link_info.phy_retry) {
14098 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14099 			bp->link_info.phy_retry = false;
14100 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14101 		} else {
14102 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14103 		}
14104 	}
14105 
14106 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14107 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14108 
14109 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14110 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14111 
14112 bnxt_restart_timer:
14113 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14114 }
14115 
14116 static void bnxt_lock_sp(struct bnxt *bp)
14117 {
14118 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14119 	 * set.  If the device is being closed, bnxt_close() may be holding
14120 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14121 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14122 	 * instance lock.
14123 	 */
14124 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14125 	netdev_lock(bp->dev);
14126 }
14127 
14128 static void bnxt_unlock_sp(struct bnxt *bp)
14129 {
14130 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14131 	netdev_unlock(bp->dev);
14132 }
14133 
14134 /* Only called from bnxt_sp_task() */
14135 static void bnxt_reset(struct bnxt *bp, bool silent)
14136 {
14137 	bnxt_lock_sp(bp);
14138 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14139 		bnxt_reset_task(bp, silent);
14140 	bnxt_unlock_sp(bp);
14141 }
14142 
14143 /* Only called from bnxt_sp_task() */
14144 static void bnxt_rx_ring_reset(struct bnxt *bp)
14145 {
14146 	int i;
14147 
14148 	bnxt_lock_sp(bp);
14149 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14150 		bnxt_unlock_sp(bp);
14151 		return;
14152 	}
14153 	/* Disable and flush TPA before resetting the RX ring */
14154 	if (bp->flags & BNXT_FLAG_TPA)
14155 		bnxt_set_tpa(bp, false);
14156 	for (i = 0; i < bp->rx_nr_rings; i++) {
14157 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14158 		struct bnxt_cp_ring_info *cpr;
14159 		int rc;
14160 
14161 		if (!rxr->bnapi->in_reset)
14162 			continue;
14163 
14164 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14165 		if (rc) {
14166 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14167 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14168 			else
14169 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14170 					    rc);
14171 			bnxt_reset_task(bp, true);
14172 			break;
14173 		}
14174 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14175 		rxr->rx_prod = 0;
14176 		rxr->rx_agg_prod = 0;
14177 		rxr->rx_sw_agg_prod = 0;
14178 		rxr->rx_next_cons = 0;
14179 		rxr->bnapi->in_reset = false;
14180 		bnxt_alloc_one_rx_ring(bp, i);
14181 		cpr = &rxr->bnapi->cp_ring;
14182 		cpr->sw_stats->rx.rx_resets++;
14183 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14184 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14185 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14186 	}
14187 	if (bp->flags & BNXT_FLAG_TPA)
14188 		bnxt_set_tpa(bp, true);
14189 	bnxt_unlock_sp(bp);
14190 }
14191 
14192 static void bnxt_fw_fatal_close(struct bnxt *bp)
14193 {
14194 	bnxt_tx_disable(bp);
14195 	bnxt_disable_napi(bp);
14196 	bnxt_disable_int_sync(bp);
14197 	bnxt_free_irq(bp);
14198 	bnxt_clear_int_mode(bp);
14199 	pci_disable_device(bp->pdev);
14200 }
14201 
14202 static void bnxt_fw_reset_close(struct bnxt *bp)
14203 {
14204 	/* When firmware is in fatal state, quiesce device and disable
14205 	 * bus master to prevent any potential bad DMAs before freeing
14206 	 * kernel memory.
14207 	 */
14208 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14209 		u16 val = 0;
14210 
14211 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14212 		if (val == 0xffff)
14213 			bp->fw_reset_min_dsecs = 0;
14214 		bnxt_fw_fatal_close(bp);
14215 	}
14216 	__bnxt_close_nic(bp, true, false);
14217 	bnxt_vf_reps_free(bp);
14218 	bnxt_clear_int_mode(bp);
14219 	bnxt_hwrm_func_drv_unrgtr(bp);
14220 	if (pci_is_enabled(bp->pdev))
14221 		pci_disable_device(bp->pdev);
14222 	bnxt_free_ctx_mem(bp, false);
14223 }
14224 
14225 static bool is_bnxt_fw_ok(struct bnxt *bp)
14226 {
14227 	struct bnxt_fw_health *fw_health = bp->fw_health;
14228 	bool no_heartbeat = false, has_reset = false;
14229 	u32 val;
14230 
14231 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14232 	if (val == fw_health->last_fw_heartbeat)
14233 		no_heartbeat = true;
14234 
14235 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14236 	if (val != fw_health->last_fw_reset_cnt)
14237 		has_reset = true;
14238 
14239 	if (!no_heartbeat && has_reset)
14240 		return true;
14241 
14242 	return false;
14243 }
14244 
14245 /* netdev instance lock is acquired before calling this function */
14246 static void bnxt_force_fw_reset(struct bnxt *bp)
14247 {
14248 	struct bnxt_fw_health *fw_health = bp->fw_health;
14249 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14250 	u32 wait_dsecs;
14251 
14252 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14253 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14254 		return;
14255 
14256 	/* we have to serialize with bnxt_refclk_read()*/
14257 	if (ptp) {
14258 		unsigned long flags;
14259 
14260 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14261 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14262 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14263 	} else {
14264 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14265 	}
14266 	bnxt_fw_reset_close(bp);
14267 	wait_dsecs = fw_health->master_func_wait_dsecs;
14268 	if (fw_health->primary) {
14269 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14270 			wait_dsecs = 0;
14271 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14272 	} else {
14273 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14274 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14275 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14276 	}
14277 
14278 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14279 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14280 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14281 }
14282 
14283 void bnxt_fw_exception(struct bnxt *bp)
14284 {
14285 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14286 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14287 	bnxt_ulp_stop(bp);
14288 	bnxt_lock_sp(bp);
14289 	bnxt_force_fw_reset(bp);
14290 	bnxt_unlock_sp(bp);
14291 }
14292 
14293 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14294  * < 0 on error.
14295  */
14296 static int bnxt_get_registered_vfs(struct bnxt *bp)
14297 {
14298 #ifdef CONFIG_BNXT_SRIOV
14299 	int rc;
14300 
14301 	if (!BNXT_PF(bp))
14302 		return 0;
14303 
14304 	rc = bnxt_hwrm_func_qcfg(bp);
14305 	if (rc) {
14306 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14307 		return rc;
14308 	}
14309 	if (bp->pf.registered_vfs)
14310 		return bp->pf.registered_vfs;
14311 	if (bp->sriov_cfg)
14312 		return 1;
14313 #endif
14314 	return 0;
14315 }
14316 
14317 void bnxt_fw_reset(struct bnxt *bp)
14318 {
14319 	bnxt_ulp_stop(bp);
14320 	bnxt_lock_sp(bp);
14321 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14322 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14323 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14324 		int n = 0, tmo;
14325 
14326 		/* we have to serialize with bnxt_refclk_read()*/
14327 		if (ptp) {
14328 			unsigned long flags;
14329 
14330 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14331 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14332 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14333 		} else {
14334 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14335 		}
14336 		if (bp->pf.active_vfs &&
14337 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14338 			n = bnxt_get_registered_vfs(bp);
14339 		if (n < 0) {
14340 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14341 				   n);
14342 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14343 			netif_close(bp->dev);
14344 			goto fw_reset_exit;
14345 		} else if (n > 0) {
14346 			u16 vf_tmo_dsecs = n * 10;
14347 
14348 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14349 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14350 			bp->fw_reset_state =
14351 				BNXT_FW_RESET_STATE_POLL_VF;
14352 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14353 			goto fw_reset_exit;
14354 		}
14355 		bnxt_fw_reset_close(bp);
14356 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14357 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14358 			tmo = HZ / 10;
14359 		} else {
14360 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14361 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14362 		}
14363 		bnxt_queue_fw_reset_work(bp, tmo);
14364 	}
14365 fw_reset_exit:
14366 	bnxt_unlock_sp(bp);
14367 }
14368 
14369 static void bnxt_chk_missed_irq(struct bnxt *bp)
14370 {
14371 	int i;
14372 
14373 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14374 		return;
14375 
14376 	for (i = 0; i < bp->cp_nr_rings; i++) {
14377 		struct bnxt_napi *bnapi = bp->bnapi[i];
14378 		struct bnxt_cp_ring_info *cpr;
14379 		u32 fw_ring_id;
14380 		int j;
14381 
14382 		if (!bnapi)
14383 			continue;
14384 
14385 		cpr = &bnapi->cp_ring;
14386 		for (j = 0; j < cpr->cp_ring_count; j++) {
14387 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14388 			u32 val[2];
14389 
14390 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14391 				continue;
14392 
14393 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14394 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14395 				continue;
14396 			}
14397 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14398 			bnxt_dbg_hwrm_ring_info_get(bp,
14399 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14400 				fw_ring_id, &val[0], &val[1]);
14401 			cpr->sw_stats->cmn.missed_irqs++;
14402 		}
14403 	}
14404 }
14405 
14406 static void bnxt_cfg_ntp_filters(struct bnxt *);
14407 
14408 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14409 {
14410 	struct bnxt_link_info *link_info = &bp->link_info;
14411 
14412 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14413 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14414 		if (bp->hwrm_spec_code >= 0x10201) {
14415 			if (link_info->auto_pause_setting &
14416 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14417 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14418 		} else {
14419 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14420 		}
14421 		bnxt_set_auto_speed(link_info);
14422 	} else {
14423 		bnxt_set_force_speed(link_info);
14424 		link_info->req_duplex = link_info->duplex_setting;
14425 	}
14426 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14427 		link_info->req_flow_ctrl =
14428 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14429 	else
14430 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14431 }
14432 
14433 static void bnxt_fw_echo_reply(struct bnxt *bp)
14434 {
14435 	struct bnxt_fw_health *fw_health = bp->fw_health;
14436 	struct hwrm_func_echo_response_input *req;
14437 	int rc;
14438 
14439 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14440 	if (rc)
14441 		return;
14442 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14443 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14444 	hwrm_req_send(bp, req);
14445 }
14446 
14447 static void bnxt_ulp_restart(struct bnxt *bp)
14448 {
14449 	bnxt_ulp_stop(bp);
14450 	bnxt_ulp_start(bp, 0);
14451 }
14452 
14453 static void bnxt_sp_task(struct work_struct *work)
14454 {
14455 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14456 
14457 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14458 	smp_mb__after_atomic();
14459 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14460 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14461 		return;
14462 	}
14463 
14464 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14465 		bnxt_ulp_restart(bp);
14466 		bnxt_reenable_sriov(bp);
14467 	}
14468 
14469 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14470 		bnxt_cfg_rx_mode(bp);
14471 
14472 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14473 		bnxt_cfg_ntp_filters(bp);
14474 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14475 		bnxt_hwrm_exec_fwd_req(bp);
14476 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14477 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14478 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14479 		bnxt_hwrm_port_qstats(bp, 0);
14480 		bnxt_hwrm_port_qstats_ext(bp, 0);
14481 		bnxt_accumulate_all_stats(bp);
14482 	}
14483 
14484 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14485 		int rc;
14486 
14487 		mutex_lock(&bp->link_lock);
14488 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14489 				       &bp->sp_event))
14490 			bnxt_hwrm_phy_qcaps(bp);
14491 
14492 		rc = bnxt_update_link(bp, true);
14493 		if (rc)
14494 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14495 				   rc);
14496 
14497 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14498 				       &bp->sp_event))
14499 			bnxt_init_ethtool_link_settings(bp);
14500 		mutex_unlock(&bp->link_lock);
14501 	}
14502 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14503 		int rc;
14504 
14505 		mutex_lock(&bp->link_lock);
14506 		rc = bnxt_update_phy_setting(bp);
14507 		mutex_unlock(&bp->link_lock);
14508 		if (rc) {
14509 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14510 		} else {
14511 			bp->link_info.phy_retry = false;
14512 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14513 		}
14514 	}
14515 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14516 		mutex_lock(&bp->link_lock);
14517 		bnxt_get_port_module_status(bp);
14518 		mutex_unlock(&bp->link_lock);
14519 	}
14520 
14521 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14522 		bnxt_tc_flow_stats_work(bp);
14523 
14524 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14525 		bnxt_chk_missed_irq(bp);
14526 
14527 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14528 		bnxt_fw_echo_reply(bp);
14529 
14530 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14531 		bnxt_hwmon_notify_event(bp);
14532 
14533 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14534 	 * must be the last functions to be called before exiting.
14535 	 */
14536 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14537 		bnxt_reset(bp, false);
14538 
14539 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14540 		bnxt_reset(bp, true);
14541 
14542 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14543 		bnxt_rx_ring_reset(bp);
14544 
14545 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14546 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14547 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14548 			bnxt_devlink_health_fw_report(bp);
14549 		else
14550 			bnxt_fw_reset(bp);
14551 	}
14552 
14553 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14554 		if (!is_bnxt_fw_ok(bp))
14555 			bnxt_devlink_health_fw_report(bp);
14556 	}
14557 
14558 	smp_mb__before_atomic();
14559 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14560 }
14561 
14562 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14563 				int *max_cp);
14564 
14565 /* Under netdev instance lock */
14566 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14567 		     int tx_xdp)
14568 {
14569 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14570 	struct bnxt_hw_rings hwr = {0};
14571 	int rx_rings = rx;
14572 	int rc;
14573 
14574 	if (tcs)
14575 		tx_sets = tcs;
14576 
14577 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14578 
14579 	if (max_rx < rx_rings)
14580 		return -ENOMEM;
14581 
14582 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14583 		rx_rings <<= 1;
14584 
14585 	hwr.rx = rx_rings;
14586 	hwr.tx = tx * tx_sets + tx_xdp;
14587 	if (max_tx < hwr.tx)
14588 		return -ENOMEM;
14589 
14590 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14591 
14592 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14593 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14594 	if (max_cp < hwr.cp)
14595 		return -ENOMEM;
14596 	hwr.stat = hwr.cp;
14597 	if (BNXT_NEW_RM(bp)) {
14598 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14599 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14600 		hwr.grp = rx;
14601 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14602 	}
14603 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14604 		hwr.cp_p5 = hwr.tx + rx;
14605 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14606 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14607 		if (!bnxt_ulp_registered(bp->edev)) {
14608 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14609 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14610 		}
14611 		if (hwr.cp > bp->total_irqs) {
14612 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14613 
14614 			if (total_msix < hwr.cp) {
14615 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14616 					    hwr.cp, total_msix);
14617 				rc = -ENOSPC;
14618 			}
14619 		}
14620 	}
14621 	return rc;
14622 }
14623 
14624 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14625 {
14626 	if (bp->bar2) {
14627 		pci_iounmap(pdev, bp->bar2);
14628 		bp->bar2 = NULL;
14629 	}
14630 
14631 	if (bp->bar1) {
14632 		pci_iounmap(pdev, bp->bar1);
14633 		bp->bar1 = NULL;
14634 	}
14635 
14636 	if (bp->bar0) {
14637 		pci_iounmap(pdev, bp->bar0);
14638 		bp->bar0 = NULL;
14639 	}
14640 }
14641 
14642 static void bnxt_cleanup_pci(struct bnxt *bp)
14643 {
14644 	bnxt_unmap_bars(bp, bp->pdev);
14645 	pci_release_regions(bp->pdev);
14646 	if (pci_is_enabled(bp->pdev))
14647 		pci_disable_device(bp->pdev);
14648 }
14649 
14650 static void bnxt_init_dflt_coal(struct bnxt *bp)
14651 {
14652 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14653 	struct bnxt_coal *coal;
14654 	u16 flags = 0;
14655 
14656 	if (coal_cap->cmpl_params &
14657 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14658 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14659 
14660 	/* Tick values in micro seconds.
14661 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14662 	 */
14663 	coal = &bp->rx_coal;
14664 	coal->coal_ticks = 10;
14665 	coal->coal_bufs = 30;
14666 	coal->coal_ticks_irq = 1;
14667 	coal->coal_bufs_irq = 2;
14668 	coal->idle_thresh = 50;
14669 	coal->bufs_per_record = 2;
14670 	coal->budget = 64;		/* NAPI budget */
14671 	coal->flags = flags;
14672 
14673 	coal = &bp->tx_coal;
14674 	coal->coal_ticks = 28;
14675 	coal->coal_bufs = 30;
14676 	coal->coal_ticks_irq = 2;
14677 	coal->coal_bufs_irq = 2;
14678 	coal->bufs_per_record = 1;
14679 	coal->flags = flags;
14680 
14681 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14682 }
14683 
14684 /* FW that pre-reserves 1 VNIC per function */
14685 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14686 {
14687 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14688 
14689 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14690 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14691 		return true;
14692 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14693 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14694 		return true;
14695 	return false;
14696 }
14697 
14698 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14699 {
14700 	int rc;
14701 
14702 	bp->fw_cap = 0;
14703 	rc = bnxt_hwrm_ver_get(bp);
14704 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14705 	 * so wait before continuing with recovery.
14706 	 */
14707 	if (rc)
14708 		msleep(100);
14709 	bnxt_try_map_fw_health_reg(bp);
14710 	if (rc) {
14711 		rc = bnxt_try_recover_fw(bp);
14712 		if (rc)
14713 			return rc;
14714 		rc = bnxt_hwrm_ver_get(bp);
14715 		if (rc)
14716 			return rc;
14717 	}
14718 
14719 	bnxt_nvm_cfg_ver_get(bp);
14720 
14721 	rc = bnxt_hwrm_func_reset(bp);
14722 	if (rc)
14723 		return -ENODEV;
14724 
14725 	bnxt_hwrm_fw_set_time(bp);
14726 	return 0;
14727 }
14728 
14729 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14730 {
14731 	int rc;
14732 
14733 	/* Get the MAX capabilities for this function */
14734 	rc = bnxt_hwrm_func_qcaps(bp);
14735 	if (rc) {
14736 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14737 			   rc);
14738 		return -ENODEV;
14739 	}
14740 
14741 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14742 	if (rc)
14743 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14744 			    rc);
14745 
14746 	if (bnxt_alloc_fw_health(bp)) {
14747 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14748 	} else {
14749 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14750 		if (rc)
14751 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14752 				    rc);
14753 	}
14754 
14755 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14756 	if (rc)
14757 		return -ENODEV;
14758 
14759 	rc = bnxt_alloc_crash_dump_mem(bp);
14760 	if (rc)
14761 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14762 			    rc);
14763 	if (!rc) {
14764 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14765 		if (rc) {
14766 			bnxt_free_crash_dump_mem(bp);
14767 			netdev_warn(bp->dev,
14768 				    "hwrm crash dump mem failure rc: %d\n", rc);
14769 		}
14770 	}
14771 
14772 	if (bnxt_fw_pre_resv_vnics(bp))
14773 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14774 
14775 	bnxt_hwrm_func_qcfg(bp);
14776 	bnxt_hwrm_vnic_qcaps(bp);
14777 	bnxt_hwrm_port_led_qcaps(bp);
14778 	bnxt_ethtool_init(bp);
14779 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14780 		__bnxt_hwrm_ptp_qcfg(bp);
14781 	bnxt_dcb_init(bp);
14782 	bnxt_hwmon_init(bp);
14783 	return 0;
14784 }
14785 
14786 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14787 {
14788 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14789 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14790 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14791 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14792 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14793 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14794 		bp->rss_hash_delta = bp->rss_hash_cfg;
14795 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14796 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14797 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14798 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14799 	}
14800 }
14801 
14802 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14803 {
14804 	struct net_device *dev = bp->dev;
14805 
14806 	dev->hw_features &= ~NETIF_F_NTUPLE;
14807 	dev->features &= ~NETIF_F_NTUPLE;
14808 	bp->flags &= ~BNXT_FLAG_RFS;
14809 	if (bnxt_rfs_supported(bp)) {
14810 		dev->hw_features |= NETIF_F_NTUPLE;
14811 		if (bnxt_rfs_capable(bp, false)) {
14812 			bp->flags |= BNXT_FLAG_RFS;
14813 			dev->features |= NETIF_F_NTUPLE;
14814 		}
14815 	}
14816 }
14817 
14818 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14819 {
14820 	struct pci_dev *pdev = bp->pdev;
14821 
14822 	bnxt_set_dflt_rss_hash_type(bp);
14823 	bnxt_set_dflt_rfs(bp);
14824 
14825 	bnxt_get_wol_settings(bp);
14826 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14827 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14828 	else
14829 		device_set_wakeup_capable(&pdev->dev, false);
14830 
14831 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14832 	bnxt_hwrm_coal_params_qcaps(bp);
14833 }
14834 
14835 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14836 
14837 int bnxt_fw_init_one(struct bnxt *bp)
14838 {
14839 	int rc;
14840 
14841 	rc = bnxt_fw_init_one_p1(bp);
14842 	if (rc) {
14843 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14844 		return rc;
14845 	}
14846 	rc = bnxt_fw_init_one_p2(bp);
14847 	if (rc) {
14848 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14849 		return rc;
14850 	}
14851 	rc = bnxt_probe_phy(bp, false);
14852 	if (rc)
14853 		return rc;
14854 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14855 	if (rc)
14856 		return rc;
14857 
14858 	bnxt_fw_init_one_p3(bp);
14859 	return 0;
14860 }
14861 
14862 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14863 {
14864 	struct bnxt_fw_health *fw_health = bp->fw_health;
14865 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14866 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14867 	u32 reg_type, reg_off, delay_msecs;
14868 
14869 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14870 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14871 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14872 	switch (reg_type) {
14873 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14874 		pci_write_config_dword(bp->pdev, reg_off, val);
14875 		break;
14876 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14877 		writel(reg_off & BNXT_GRC_BASE_MASK,
14878 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14879 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14880 		fallthrough;
14881 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14882 		writel(val, bp->bar0 + reg_off);
14883 		break;
14884 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14885 		writel(val, bp->bar1 + reg_off);
14886 		break;
14887 	}
14888 	if (delay_msecs) {
14889 		pci_read_config_dword(bp->pdev, 0, &val);
14890 		msleep(delay_msecs);
14891 	}
14892 }
14893 
14894 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14895 {
14896 	struct hwrm_func_qcfg_output *resp;
14897 	struct hwrm_func_qcfg_input *req;
14898 	bool result = true; /* firmware will enforce if unknown */
14899 
14900 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14901 		return result;
14902 
14903 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14904 		return result;
14905 
14906 	req->fid = cpu_to_le16(0xffff);
14907 	resp = hwrm_req_hold(bp, req);
14908 	if (!hwrm_req_send(bp, req))
14909 		result = !!(le16_to_cpu(resp->flags) &
14910 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14911 	hwrm_req_drop(bp, req);
14912 	return result;
14913 }
14914 
14915 static void bnxt_reset_all(struct bnxt *bp)
14916 {
14917 	struct bnxt_fw_health *fw_health = bp->fw_health;
14918 	int i, rc;
14919 
14920 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14921 		bnxt_fw_reset_via_optee(bp);
14922 		bp->fw_reset_timestamp = jiffies;
14923 		return;
14924 	}
14925 
14926 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14927 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14928 			bnxt_fw_reset_writel(bp, i);
14929 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14930 		struct hwrm_fw_reset_input *req;
14931 
14932 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14933 		if (!rc) {
14934 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14935 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14936 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14937 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14938 			rc = hwrm_req_send(bp, req);
14939 		}
14940 		if (rc != -ENODEV)
14941 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14942 	}
14943 	bp->fw_reset_timestamp = jiffies;
14944 }
14945 
14946 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14947 {
14948 	return time_after(jiffies, bp->fw_reset_timestamp +
14949 			  (bp->fw_reset_max_dsecs * HZ / 10));
14950 }
14951 
14952 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14953 {
14954 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14955 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14956 		bnxt_dl_health_fw_status_update(bp, false);
14957 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
14958 	netif_close(bp->dev);
14959 }
14960 
14961 static void bnxt_fw_reset_task(struct work_struct *work)
14962 {
14963 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14964 	int rc = 0;
14965 
14966 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14967 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14968 		return;
14969 	}
14970 
14971 	switch (bp->fw_reset_state) {
14972 	case BNXT_FW_RESET_STATE_POLL_VF: {
14973 		int n = bnxt_get_registered_vfs(bp);
14974 		int tmo;
14975 
14976 		if (n < 0) {
14977 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14978 				   n, jiffies_to_msecs(jiffies -
14979 				   bp->fw_reset_timestamp));
14980 			goto fw_reset_abort;
14981 		} else if (n > 0) {
14982 			if (bnxt_fw_reset_timeout(bp)) {
14983 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14984 				bp->fw_reset_state = 0;
14985 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14986 					   n);
14987 				goto ulp_start;
14988 			}
14989 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14990 			return;
14991 		}
14992 		bp->fw_reset_timestamp = jiffies;
14993 		netdev_lock(bp->dev);
14994 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14995 			bnxt_fw_reset_abort(bp, rc);
14996 			netdev_unlock(bp->dev);
14997 			goto ulp_start;
14998 		}
14999 		bnxt_fw_reset_close(bp);
15000 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15001 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15002 			tmo = HZ / 10;
15003 		} else {
15004 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15005 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15006 		}
15007 		netdev_unlock(bp->dev);
15008 		bnxt_queue_fw_reset_work(bp, tmo);
15009 		return;
15010 	}
15011 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15012 		u32 val;
15013 
15014 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15015 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15016 		    !bnxt_fw_reset_timeout(bp)) {
15017 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15018 			return;
15019 		}
15020 
15021 		if (!bp->fw_health->primary) {
15022 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15023 
15024 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15025 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15026 			return;
15027 		}
15028 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15029 	}
15030 		fallthrough;
15031 	case BNXT_FW_RESET_STATE_RESET_FW:
15032 		bnxt_reset_all(bp);
15033 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15034 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15035 		return;
15036 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15037 		bnxt_inv_fw_health_reg(bp);
15038 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15039 		    !bp->fw_reset_min_dsecs) {
15040 			u16 val;
15041 
15042 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15043 			if (val == 0xffff) {
15044 				if (bnxt_fw_reset_timeout(bp)) {
15045 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15046 					rc = -ETIMEDOUT;
15047 					goto fw_reset_abort;
15048 				}
15049 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15050 				return;
15051 			}
15052 		}
15053 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15054 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15055 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15056 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15057 			bnxt_dl_remote_reload(bp);
15058 		if (pci_enable_device(bp->pdev)) {
15059 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15060 			rc = -ENODEV;
15061 			goto fw_reset_abort;
15062 		}
15063 		pci_set_master(bp->pdev);
15064 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15065 		fallthrough;
15066 	case BNXT_FW_RESET_STATE_POLL_FW:
15067 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15068 		rc = bnxt_hwrm_poll(bp);
15069 		if (rc) {
15070 			if (bnxt_fw_reset_timeout(bp)) {
15071 				netdev_err(bp->dev, "Firmware reset aborted\n");
15072 				goto fw_reset_abort_status;
15073 			}
15074 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15075 			return;
15076 		}
15077 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15078 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15079 		fallthrough;
15080 	case BNXT_FW_RESET_STATE_OPENING:
15081 		while (!netdev_trylock(bp->dev)) {
15082 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15083 			return;
15084 		}
15085 		rc = bnxt_open(bp->dev);
15086 		if (rc) {
15087 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15088 			bnxt_fw_reset_abort(bp, rc);
15089 			netdev_unlock(bp->dev);
15090 			goto ulp_start;
15091 		}
15092 
15093 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15094 		    bp->fw_health->enabled) {
15095 			bp->fw_health->last_fw_reset_cnt =
15096 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15097 		}
15098 		bp->fw_reset_state = 0;
15099 		/* Make sure fw_reset_state is 0 before clearing the flag */
15100 		smp_mb__before_atomic();
15101 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15102 		bnxt_ptp_reapply_pps(bp);
15103 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15104 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15105 			bnxt_dl_health_fw_recovery_done(bp);
15106 			bnxt_dl_health_fw_status_update(bp, true);
15107 		}
15108 		netdev_unlock(bp->dev);
15109 		bnxt_ulp_start(bp, 0);
15110 		bnxt_reenable_sriov(bp);
15111 		netdev_lock(bp->dev);
15112 		bnxt_vf_reps_alloc(bp);
15113 		bnxt_vf_reps_open(bp);
15114 		netdev_unlock(bp->dev);
15115 		break;
15116 	}
15117 	return;
15118 
15119 fw_reset_abort_status:
15120 	if (bp->fw_health->status_reliable ||
15121 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15122 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15123 
15124 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15125 	}
15126 fw_reset_abort:
15127 	netdev_lock(bp->dev);
15128 	bnxt_fw_reset_abort(bp, rc);
15129 	netdev_unlock(bp->dev);
15130 ulp_start:
15131 	bnxt_ulp_start(bp, rc);
15132 }
15133 
15134 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15135 {
15136 	int rc;
15137 	struct bnxt *bp = netdev_priv(dev);
15138 
15139 	SET_NETDEV_DEV(dev, &pdev->dev);
15140 
15141 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15142 	rc = pci_enable_device(pdev);
15143 	if (rc) {
15144 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15145 		goto init_err;
15146 	}
15147 
15148 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15149 		dev_err(&pdev->dev,
15150 			"Cannot find PCI device base address, aborting\n");
15151 		rc = -ENODEV;
15152 		goto init_err_disable;
15153 	}
15154 
15155 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15156 	if (rc) {
15157 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15158 		goto init_err_disable;
15159 	}
15160 
15161 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15162 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15163 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15164 		rc = -EIO;
15165 		goto init_err_release;
15166 	}
15167 
15168 	pci_set_master(pdev);
15169 
15170 	bp->dev = dev;
15171 	bp->pdev = pdev;
15172 
15173 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15174 	 * determines the BAR size.
15175 	 */
15176 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15177 	if (!bp->bar0) {
15178 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15179 		rc = -ENOMEM;
15180 		goto init_err_release;
15181 	}
15182 
15183 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15184 	if (!bp->bar2) {
15185 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15186 		rc = -ENOMEM;
15187 		goto init_err_release;
15188 	}
15189 
15190 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15191 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15192 
15193 	spin_lock_init(&bp->ntp_fltr_lock);
15194 #if BITS_PER_LONG == 32
15195 	spin_lock_init(&bp->db_lock);
15196 #endif
15197 
15198 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15199 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15200 
15201 	timer_setup(&bp->timer, bnxt_timer, 0);
15202 	bp->current_interval = BNXT_TIMER_INTERVAL;
15203 
15204 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15205 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15206 
15207 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15208 	return 0;
15209 
15210 init_err_release:
15211 	bnxt_unmap_bars(bp, pdev);
15212 	pci_release_regions(pdev);
15213 
15214 init_err_disable:
15215 	pci_disable_device(pdev);
15216 
15217 init_err:
15218 	return rc;
15219 }
15220 
15221 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15222 {
15223 	struct sockaddr *addr = p;
15224 	struct bnxt *bp = netdev_priv(dev);
15225 	int rc = 0;
15226 
15227 	netdev_assert_locked(dev);
15228 
15229 	if (!is_valid_ether_addr(addr->sa_data))
15230 		return -EADDRNOTAVAIL;
15231 
15232 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15233 		return 0;
15234 
15235 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15236 	if (rc)
15237 		return rc;
15238 
15239 	eth_hw_addr_set(dev, addr->sa_data);
15240 	bnxt_clear_usr_fltrs(bp, true);
15241 	if (netif_running(dev)) {
15242 		bnxt_close_nic(bp, false, false);
15243 		rc = bnxt_open_nic(bp, false, false);
15244 	}
15245 
15246 	return rc;
15247 }
15248 
15249 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15250 {
15251 	struct bnxt *bp = netdev_priv(dev);
15252 
15253 	netdev_assert_locked(dev);
15254 
15255 	if (netif_running(dev))
15256 		bnxt_close_nic(bp, true, false);
15257 
15258 	WRITE_ONCE(dev->mtu, new_mtu);
15259 
15260 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15261 	 * program is attached.  We need to set the AGG rings settings and
15262 	 * rx_skb_func accordingly.
15263 	 */
15264 	if (READ_ONCE(bp->xdp_prog))
15265 		bnxt_set_rx_skb_mode(bp, true);
15266 
15267 	bnxt_set_ring_params(bp);
15268 
15269 	if (netif_running(dev))
15270 		return bnxt_open_nic(bp, true, false);
15271 
15272 	return 0;
15273 }
15274 
15275 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15276 {
15277 	struct bnxt *bp = netdev_priv(dev);
15278 	bool sh = false;
15279 	int rc, tx_cp;
15280 
15281 	if (tc > bp->max_tc) {
15282 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15283 			   tc, bp->max_tc);
15284 		return -EINVAL;
15285 	}
15286 
15287 	if (bp->num_tc == tc)
15288 		return 0;
15289 
15290 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15291 		sh = true;
15292 
15293 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15294 			      sh, tc, bp->tx_nr_rings_xdp);
15295 	if (rc)
15296 		return rc;
15297 
15298 	/* Needs to close the device and do hw resource re-allocations */
15299 	if (netif_running(bp->dev))
15300 		bnxt_close_nic(bp, true, false);
15301 
15302 	if (tc) {
15303 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15304 		netdev_set_num_tc(dev, tc);
15305 		bp->num_tc = tc;
15306 	} else {
15307 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15308 		netdev_reset_tc(dev);
15309 		bp->num_tc = 0;
15310 	}
15311 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15312 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15313 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15314 			       tx_cp + bp->rx_nr_rings;
15315 
15316 	if (netif_running(bp->dev))
15317 		return bnxt_open_nic(bp, true, false);
15318 
15319 	return 0;
15320 }
15321 
15322 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15323 				  void *cb_priv)
15324 {
15325 	struct bnxt *bp = cb_priv;
15326 
15327 	if (!bnxt_tc_flower_enabled(bp) ||
15328 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15329 		return -EOPNOTSUPP;
15330 
15331 	switch (type) {
15332 	case TC_SETUP_CLSFLOWER:
15333 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15334 	default:
15335 		return -EOPNOTSUPP;
15336 	}
15337 }
15338 
15339 LIST_HEAD(bnxt_block_cb_list);
15340 
15341 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15342 			 void *type_data)
15343 {
15344 	struct bnxt *bp = netdev_priv(dev);
15345 
15346 	switch (type) {
15347 	case TC_SETUP_BLOCK:
15348 		return flow_block_cb_setup_simple(type_data,
15349 						  &bnxt_block_cb_list,
15350 						  bnxt_setup_tc_block_cb,
15351 						  bp, bp, true);
15352 	case TC_SETUP_QDISC_MQPRIO: {
15353 		struct tc_mqprio_qopt *mqprio = type_data;
15354 
15355 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15356 
15357 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15358 	}
15359 	default:
15360 		return -EOPNOTSUPP;
15361 	}
15362 }
15363 
15364 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15365 			    const struct sk_buff *skb)
15366 {
15367 	struct bnxt_vnic_info *vnic;
15368 
15369 	if (skb)
15370 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15371 
15372 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15373 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15374 }
15375 
15376 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15377 			   u32 idx)
15378 {
15379 	struct hlist_head *head;
15380 	int bit_id;
15381 
15382 	spin_lock_bh(&bp->ntp_fltr_lock);
15383 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15384 	if (bit_id < 0) {
15385 		spin_unlock_bh(&bp->ntp_fltr_lock);
15386 		return -ENOMEM;
15387 	}
15388 
15389 	fltr->base.sw_id = (u16)bit_id;
15390 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15391 	fltr->base.flags |= BNXT_ACT_RING_DST;
15392 	head = &bp->ntp_fltr_hash_tbl[idx];
15393 	hlist_add_head_rcu(&fltr->base.hash, head);
15394 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15395 	bnxt_insert_usr_fltr(bp, &fltr->base);
15396 	bp->ntp_fltr_count++;
15397 	spin_unlock_bh(&bp->ntp_fltr_lock);
15398 	return 0;
15399 }
15400 
15401 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15402 			    struct bnxt_ntuple_filter *f2)
15403 {
15404 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15405 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15406 	struct flow_keys *keys1 = &f1->fkeys;
15407 	struct flow_keys *keys2 = &f2->fkeys;
15408 
15409 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15410 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15411 		return false;
15412 
15413 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15414 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15415 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15416 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15417 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15418 			return false;
15419 	} else {
15420 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15421 				     &keys2->addrs.v6addrs.src) ||
15422 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15423 				     &masks2->addrs.v6addrs.src) ||
15424 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15425 				     &keys2->addrs.v6addrs.dst) ||
15426 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15427 				     &masks2->addrs.v6addrs.dst))
15428 			return false;
15429 	}
15430 
15431 	return keys1->ports.src == keys2->ports.src &&
15432 	       masks1->ports.src == masks2->ports.src &&
15433 	       keys1->ports.dst == keys2->ports.dst &&
15434 	       masks1->ports.dst == masks2->ports.dst &&
15435 	       keys1->control.flags == keys2->control.flags &&
15436 	       f1->l2_fltr == f2->l2_fltr;
15437 }
15438 
15439 struct bnxt_ntuple_filter *
15440 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15441 				struct bnxt_ntuple_filter *fltr, u32 idx)
15442 {
15443 	struct bnxt_ntuple_filter *f;
15444 	struct hlist_head *head;
15445 
15446 	head = &bp->ntp_fltr_hash_tbl[idx];
15447 	hlist_for_each_entry_rcu(f, head, base.hash) {
15448 		if (bnxt_fltr_match(f, fltr))
15449 			return f;
15450 	}
15451 	return NULL;
15452 }
15453 
15454 #ifdef CONFIG_RFS_ACCEL
15455 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15456 			      u16 rxq_index, u32 flow_id)
15457 {
15458 	struct bnxt *bp = netdev_priv(dev);
15459 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15460 	struct flow_keys *fkeys;
15461 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15462 	struct bnxt_l2_filter *l2_fltr;
15463 	int rc = 0, idx;
15464 	u32 flags;
15465 
15466 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15467 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15468 		atomic_inc(&l2_fltr->refcnt);
15469 	} else {
15470 		struct bnxt_l2_key key;
15471 
15472 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15473 		key.vlan = 0;
15474 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15475 		if (!l2_fltr)
15476 			return -EINVAL;
15477 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15478 			bnxt_del_l2_filter(bp, l2_fltr);
15479 			return -EINVAL;
15480 		}
15481 	}
15482 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15483 	if (!new_fltr) {
15484 		bnxt_del_l2_filter(bp, l2_fltr);
15485 		return -ENOMEM;
15486 	}
15487 
15488 	fkeys = &new_fltr->fkeys;
15489 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15490 		rc = -EPROTONOSUPPORT;
15491 		goto err_free;
15492 	}
15493 
15494 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15495 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15496 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15497 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15498 		rc = -EPROTONOSUPPORT;
15499 		goto err_free;
15500 	}
15501 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15502 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15503 		if (bp->hwrm_spec_code < 0x10601) {
15504 			rc = -EPROTONOSUPPORT;
15505 			goto err_free;
15506 		}
15507 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15508 	}
15509 	flags = fkeys->control.flags;
15510 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15511 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15512 		rc = -EPROTONOSUPPORT;
15513 		goto err_free;
15514 	}
15515 	new_fltr->l2_fltr = l2_fltr;
15516 
15517 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15518 	rcu_read_lock();
15519 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15520 	if (fltr) {
15521 		rc = fltr->base.sw_id;
15522 		rcu_read_unlock();
15523 		goto err_free;
15524 	}
15525 	rcu_read_unlock();
15526 
15527 	new_fltr->flow_id = flow_id;
15528 	new_fltr->base.rxq = rxq_index;
15529 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15530 	if (!rc) {
15531 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15532 		return new_fltr->base.sw_id;
15533 	}
15534 
15535 err_free:
15536 	bnxt_del_l2_filter(bp, l2_fltr);
15537 	kfree(new_fltr);
15538 	return rc;
15539 }
15540 #endif
15541 
15542 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15543 {
15544 	spin_lock_bh(&bp->ntp_fltr_lock);
15545 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15546 		spin_unlock_bh(&bp->ntp_fltr_lock);
15547 		return;
15548 	}
15549 	hlist_del_rcu(&fltr->base.hash);
15550 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15551 	bp->ntp_fltr_count--;
15552 	spin_unlock_bh(&bp->ntp_fltr_lock);
15553 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15554 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15555 	kfree_rcu(fltr, base.rcu);
15556 }
15557 
15558 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15559 {
15560 #ifdef CONFIG_RFS_ACCEL
15561 	int i;
15562 
15563 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15564 		struct hlist_head *head;
15565 		struct hlist_node *tmp;
15566 		struct bnxt_ntuple_filter *fltr;
15567 		int rc;
15568 
15569 		head = &bp->ntp_fltr_hash_tbl[i];
15570 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15571 			bool del = false;
15572 
15573 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15574 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15575 					continue;
15576 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15577 							fltr->flow_id,
15578 							fltr->base.sw_id)) {
15579 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15580 									 fltr);
15581 					del = true;
15582 				}
15583 			} else {
15584 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15585 								       fltr);
15586 				if (rc)
15587 					del = true;
15588 				else
15589 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15590 			}
15591 
15592 			if (del)
15593 				bnxt_del_ntp_filter(bp, fltr);
15594 		}
15595 	}
15596 #endif
15597 }
15598 
15599 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15600 				    unsigned int entry, struct udp_tunnel_info *ti)
15601 {
15602 	struct bnxt *bp = netdev_priv(netdev);
15603 	unsigned int cmd;
15604 
15605 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15606 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15607 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15608 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15609 	else
15610 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15611 
15612 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15613 }
15614 
15615 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15616 				      unsigned int entry, struct udp_tunnel_info *ti)
15617 {
15618 	struct bnxt *bp = netdev_priv(netdev);
15619 	unsigned int cmd;
15620 
15621 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15622 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15623 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15624 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15625 	else
15626 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15627 
15628 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15629 }
15630 
15631 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15632 	.set_port	= bnxt_udp_tunnel_set_port,
15633 	.unset_port	= bnxt_udp_tunnel_unset_port,
15634 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15635 	.tables		= {
15636 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15637 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15638 	},
15639 }, bnxt_udp_tunnels_p7 = {
15640 	.set_port	= bnxt_udp_tunnel_set_port,
15641 	.unset_port	= bnxt_udp_tunnel_unset_port,
15642 	.flags		= UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15643 	.tables		= {
15644 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15645 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15646 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15647 	},
15648 };
15649 
15650 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15651 			       struct net_device *dev, u32 filter_mask,
15652 			       int nlflags)
15653 {
15654 	struct bnxt *bp = netdev_priv(dev);
15655 
15656 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15657 				       nlflags, filter_mask, NULL);
15658 }
15659 
15660 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15661 			       u16 flags, struct netlink_ext_ack *extack)
15662 {
15663 	struct bnxt *bp = netdev_priv(dev);
15664 	struct nlattr *attr, *br_spec;
15665 	int rem, rc = 0;
15666 
15667 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15668 		return -EOPNOTSUPP;
15669 
15670 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15671 	if (!br_spec)
15672 		return -EINVAL;
15673 
15674 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15675 		u16 mode;
15676 
15677 		mode = nla_get_u16(attr);
15678 		if (mode == bp->br_mode)
15679 			break;
15680 
15681 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15682 		if (!rc)
15683 			bp->br_mode = mode;
15684 		break;
15685 	}
15686 	return rc;
15687 }
15688 
15689 int bnxt_get_port_parent_id(struct net_device *dev,
15690 			    struct netdev_phys_item_id *ppid)
15691 {
15692 	struct bnxt *bp = netdev_priv(dev);
15693 
15694 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15695 		return -EOPNOTSUPP;
15696 
15697 	/* The PF and it's VF-reps only support the switchdev framework */
15698 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15699 		return -EOPNOTSUPP;
15700 
15701 	ppid->id_len = sizeof(bp->dsn);
15702 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15703 
15704 	return 0;
15705 }
15706 
15707 static const struct net_device_ops bnxt_netdev_ops = {
15708 	.ndo_open		= bnxt_open,
15709 	.ndo_start_xmit		= bnxt_start_xmit,
15710 	.ndo_stop		= bnxt_close,
15711 	.ndo_get_stats64	= bnxt_get_stats64,
15712 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15713 	.ndo_eth_ioctl		= bnxt_ioctl,
15714 	.ndo_validate_addr	= eth_validate_addr,
15715 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15716 	.ndo_change_mtu		= bnxt_change_mtu,
15717 	.ndo_fix_features	= bnxt_fix_features,
15718 	.ndo_set_features	= bnxt_set_features,
15719 	.ndo_features_check	= bnxt_features_check,
15720 	.ndo_tx_timeout		= bnxt_tx_timeout,
15721 #ifdef CONFIG_BNXT_SRIOV
15722 	.ndo_get_vf_config	= bnxt_get_vf_config,
15723 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15724 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15725 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15726 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15727 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15728 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15729 #endif
15730 	.ndo_setup_tc           = bnxt_setup_tc,
15731 #ifdef CONFIG_RFS_ACCEL
15732 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15733 #endif
15734 	.ndo_bpf		= bnxt_xdp,
15735 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15736 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15737 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15738 };
15739 
15740 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15741 				    struct netdev_queue_stats_rx *stats)
15742 {
15743 	struct bnxt *bp = netdev_priv(dev);
15744 	struct bnxt_cp_ring_info *cpr;
15745 	u64 *sw;
15746 
15747 	if (!bp->bnapi)
15748 		return;
15749 
15750 	cpr = &bp->bnapi[i]->cp_ring;
15751 	sw = cpr->stats.sw_stats;
15752 
15753 	stats->packets = 0;
15754 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15755 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15756 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15757 
15758 	stats->bytes = 0;
15759 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15760 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15761 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15762 
15763 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15764 }
15765 
15766 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15767 				    struct netdev_queue_stats_tx *stats)
15768 {
15769 	struct bnxt *bp = netdev_priv(dev);
15770 	struct bnxt_napi *bnapi;
15771 	u64 *sw;
15772 
15773 	if (!bp->tx_ring)
15774 		return;
15775 
15776 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15777 	sw = bnapi->cp_ring.stats.sw_stats;
15778 
15779 	stats->packets = 0;
15780 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15781 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15782 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15783 
15784 	stats->bytes = 0;
15785 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15786 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15787 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15788 }
15789 
15790 static void bnxt_get_base_stats(struct net_device *dev,
15791 				struct netdev_queue_stats_rx *rx,
15792 				struct netdev_queue_stats_tx *tx)
15793 {
15794 	struct bnxt *bp = netdev_priv(dev);
15795 
15796 	rx->packets = bp->net_stats_prev.rx_packets;
15797 	rx->bytes = bp->net_stats_prev.rx_bytes;
15798 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15799 
15800 	tx->packets = bp->net_stats_prev.tx_packets;
15801 	tx->bytes = bp->net_stats_prev.tx_bytes;
15802 }
15803 
15804 static const struct netdev_stat_ops bnxt_stat_ops = {
15805 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15806 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15807 	.get_base_stats		= bnxt_get_base_stats,
15808 };
15809 
15810 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15811 {
15812 	struct bnxt_rx_ring_info *rxr, *clone;
15813 	struct bnxt *bp = netdev_priv(dev);
15814 	struct bnxt_ring_struct *ring;
15815 	int rc;
15816 
15817 	if (!bp->rx_ring)
15818 		return -ENETDOWN;
15819 
15820 	rxr = &bp->rx_ring[idx];
15821 	clone = qmem;
15822 	memcpy(clone, rxr, sizeof(*rxr));
15823 	bnxt_init_rx_ring_struct(bp, clone);
15824 	bnxt_reset_rx_ring_struct(bp, clone);
15825 
15826 	clone->rx_prod = 0;
15827 	clone->rx_agg_prod = 0;
15828 	clone->rx_sw_agg_prod = 0;
15829 	clone->rx_next_cons = 0;
15830 	clone->need_head_pool = false;
15831 
15832 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15833 	if (rc)
15834 		return rc;
15835 
15836 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15837 	if (rc < 0)
15838 		goto err_page_pool_destroy;
15839 
15840 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15841 					MEM_TYPE_PAGE_POOL,
15842 					clone->page_pool);
15843 	if (rc)
15844 		goto err_rxq_info_unreg;
15845 
15846 	ring = &clone->rx_ring_struct;
15847 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15848 	if (rc)
15849 		goto err_free_rx_ring;
15850 
15851 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15852 		ring = &clone->rx_agg_ring_struct;
15853 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15854 		if (rc)
15855 			goto err_free_rx_agg_ring;
15856 
15857 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15858 		if (rc)
15859 			goto err_free_rx_agg_ring;
15860 	}
15861 
15862 	if (bp->flags & BNXT_FLAG_TPA) {
15863 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15864 		if (rc)
15865 			goto err_free_tpa_info;
15866 	}
15867 
15868 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15869 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15870 
15871 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15872 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15873 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
15874 	if (bp->flags & BNXT_FLAG_TPA)
15875 		bnxt_alloc_one_tpa_info_data(bp, clone);
15876 
15877 	return 0;
15878 
15879 err_free_tpa_info:
15880 	bnxt_free_one_tpa_info(bp, clone);
15881 err_free_rx_agg_ring:
15882 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15883 err_free_rx_ring:
15884 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15885 err_rxq_info_unreg:
15886 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15887 err_page_pool_destroy:
15888 	page_pool_destroy(clone->page_pool);
15889 	if (bnxt_separate_head_pool(clone))
15890 		page_pool_destroy(clone->head_pool);
15891 	clone->page_pool = NULL;
15892 	clone->head_pool = NULL;
15893 	return rc;
15894 }
15895 
15896 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15897 {
15898 	struct bnxt_rx_ring_info *rxr = qmem;
15899 	struct bnxt *bp = netdev_priv(dev);
15900 	struct bnxt_ring_struct *ring;
15901 
15902 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15903 	bnxt_free_one_tpa_info(bp, rxr);
15904 
15905 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15906 
15907 	page_pool_destroy(rxr->page_pool);
15908 	if (bnxt_separate_head_pool(rxr))
15909 		page_pool_destroy(rxr->head_pool);
15910 	rxr->page_pool = NULL;
15911 	rxr->head_pool = NULL;
15912 
15913 	ring = &rxr->rx_ring_struct;
15914 	bnxt_free_ring(bp, &ring->ring_mem);
15915 
15916 	ring = &rxr->rx_agg_ring_struct;
15917 	bnxt_free_ring(bp, &ring->ring_mem);
15918 
15919 	kfree(rxr->rx_agg_bmap);
15920 	rxr->rx_agg_bmap = NULL;
15921 }
15922 
15923 static void bnxt_copy_rx_ring(struct bnxt *bp,
15924 			      struct bnxt_rx_ring_info *dst,
15925 			      struct bnxt_rx_ring_info *src)
15926 {
15927 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15928 	struct bnxt_ring_struct *dst_ring, *src_ring;
15929 	int i;
15930 
15931 	dst_ring = &dst->rx_ring_struct;
15932 	dst_rmem = &dst_ring->ring_mem;
15933 	src_ring = &src->rx_ring_struct;
15934 	src_rmem = &src_ring->ring_mem;
15935 
15936 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15937 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15938 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15939 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15940 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15941 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15942 
15943 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15944 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15945 	*dst_rmem->vmem = *src_rmem->vmem;
15946 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15947 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15948 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15949 	}
15950 
15951 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15952 		return;
15953 
15954 	dst_ring = &dst->rx_agg_ring_struct;
15955 	dst_rmem = &dst_ring->ring_mem;
15956 	src_ring = &src->rx_agg_ring_struct;
15957 	src_rmem = &src_ring->ring_mem;
15958 
15959 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15960 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15961 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15962 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15963 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15964 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15965 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15966 
15967 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15968 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15969 	*dst_rmem->vmem = *src_rmem->vmem;
15970 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15971 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15972 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15973 	}
15974 
15975 	dst->rx_agg_bmap = src->rx_agg_bmap;
15976 }
15977 
15978 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15979 {
15980 	struct bnxt *bp = netdev_priv(dev);
15981 	struct bnxt_rx_ring_info *rxr, *clone;
15982 	struct bnxt_cp_ring_info *cpr;
15983 	struct bnxt_vnic_info *vnic;
15984 	struct bnxt_napi *bnapi;
15985 	int i, rc;
15986 	u16 mru;
15987 
15988 	rxr = &bp->rx_ring[idx];
15989 	clone = qmem;
15990 
15991 	rxr->rx_prod = clone->rx_prod;
15992 	rxr->rx_agg_prod = clone->rx_agg_prod;
15993 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15994 	rxr->rx_next_cons = clone->rx_next_cons;
15995 	rxr->rx_tpa = clone->rx_tpa;
15996 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15997 	rxr->page_pool = clone->page_pool;
15998 	rxr->head_pool = clone->head_pool;
15999 	rxr->xdp_rxq = clone->xdp_rxq;
16000 	rxr->need_head_pool = clone->need_head_pool;
16001 
16002 	bnxt_copy_rx_ring(bp, rxr, clone);
16003 
16004 	bnapi = rxr->bnapi;
16005 	cpr = &bnapi->cp_ring;
16006 
16007 	/* All rings have been reserved and previously allocated.
16008 	 * Reallocating with the same parameters should never fail.
16009 	 */
16010 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16011 	if (rc)
16012 		goto err_reset;
16013 
16014 	if (bp->tph_mode) {
16015 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16016 		if (rc)
16017 			goto err_reset;
16018 	}
16019 
16020 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16021 	if (rc)
16022 		goto err_reset;
16023 
16024 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16025 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16026 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16027 
16028 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16029 		rc = bnxt_tx_queue_start(bp, idx);
16030 		if (rc)
16031 			goto err_reset;
16032 	}
16033 
16034 	napi_enable_locked(&bnapi->napi);
16035 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16036 
16037 	mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
16038 	for (i = 0; i < bp->nr_vnics; i++) {
16039 		vnic = &bp->vnic_info[i];
16040 
16041 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16042 		if (rc)
16043 			return rc;
16044 	}
16045 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16046 
16047 err_reset:
16048 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16049 		   rc);
16050 	napi_enable_locked(&bnapi->napi);
16051 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16052 	bnxt_reset_task(bp, true);
16053 	return rc;
16054 }
16055 
16056 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16057 {
16058 	struct bnxt *bp = netdev_priv(dev);
16059 	struct bnxt_rx_ring_info *rxr;
16060 	struct bnxt_cp_ring_info *cpr;
16061 	struct bnxt_vnic_info *vnic;
16062 	struct bnxt_napi *bnapi;
16063 	int i;
16064 
16065 	for (i = 0; i < bp->nr_vnics; i++) {
16066 		vnic = &bp->vnic_info[i];
16067 
16068 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16069 	}
16070 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16071 	/* Make sure NAPI sees that the VNIC is disabled */
16072 	synchronize_net();
16073 	rxr = &bp->rx_ring[idx];
16074 	bnapi = rxr->bnapi;
16075 	cpr = &bnapi->cp_ring;
16076 	cancel_work_sync(&cpr->dim.work);
16077 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16078 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16079 	page_pool_disable_direct_recycling(rxr->page_pool);
16080 	if (bnxt_separate_head_pool(rxr))
16081 		page_pool_disable_direct_recycling(rxr->head_pool);
16082 
16083 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16084 		bnxt_tx_queue_stop(bp, idx);
16085 
16086 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16087 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16088 	 * after seeing the completion.
16089 	 */
16090 	napi_disable_locked(&bnapi->napi);
16091 
16092 	if (bp->tph_mode) {
16093 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16094 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16095 	}
16096 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16097 
16098 	memcpy(qmem, rxr, sizeof(*rxr));
16099 	bnxt_init_rx_ring_struct(bp, qmem);
16100 
16101 	return 0;
16102 }
16103 
16104 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16105 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16106 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16107 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16108 	.ndo_queue_start	= bnxt_queue_start,
16109 	.ndo_queue_stop		= bnxt_queue_stop,
16110 };
16111 
16112 static void bnxt_remove_one(struct pci_dev *pdev)
16113 {
16114 	struct net_device *dev = pci_get_drvdata(pdev);
16115 	struct bnxt *bp = netdev_priv(dev);
16116 
16117 	if (BNXT_PF(bp))
16118 		bnxt_sriov_disable(bp);
16119 
16120 	bnxt_rdma_aux_device_del(bp);
16121 
16122 	unregister_netdev(dev);
16123 	bnxt_ptp_clear(bp);
16124 
16125 	bnxt_rdma_aux_device_uninit(bp);
16126 
16127 	bnxt_free_l2_filters(bp, true);
16128 	bnxt_free_ntp_fltrs(bp, true);
16129 	WARN_ON(bp->num_rss_ctx);
16130 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16131 	/* Flush any pending tasks */
16132 	cancel_work_sync(&bp->sp_task);
16133 	cancel_delayed_work_sync(&bp->fw_reset_task);
16134 	bp->sp_event = 0;
16135 
16136 	bnxt_dl_fw_reporters_destroy(bp);
16137 	bnxt_dl_unregister(bp);
16138 	bnxt_shutdown_tc(bp);
16139 
16140 	bnxt_clear_int_mode(bp);
16141 	bnxt_hwrm_func_drv_unrgtr(bp);
16142 	bnxt_free_hwrm_resources(bp);
16143 	bnxt_hwmon_uninit(bp);
16144 	bnxt_ethtool_free(bp);
16145 	bnxt_dcb_free(bp);
16146 	kfree(bp->ptp_cfg);
16147 	bp->ptp_cfg = NULL;
16148 	kfree(bp->fw_health);
16149 	bp->fw_health = NULL;
16150 	bnxt_cleanup_pci(bp);
16151 	bnxt_free_ctx_mem(bp, true);
16152 	bnxt_free_crash_dump_mem(bp);
16153 	kfree(bp->rss_indir_tbl);
16154 	bp->rss_indir_tbl = NULL;
16155 	bnxt_free_port_stats(bp);
16156 	free_netdev(dev);
16157 }
16158 
16159 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16160 {
16161 	int rc = 0;
16162 	struct bnxt_link_info *link_info = &bp->link_info;
16163 
16164 	bp->phy_flags = 0;
16165 	rc = bnxt_hwrm_phy_qcaps(bp);
16166 	if (rc) {
16167 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16168 			   rc);
16169 		return rc;
16170 	}
16171 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16172 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16173 	else
16174 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16175 
16176 	bp->mac_flags = 0;
16177 	bnxt_hwrm_mac_qcaps(bp);
16178 
16179 	if (!fw_dflt)
16180 		return 0;
16181 
16182 	mutex_lock(&bp->link_lock);
16183 	rc = bnxt_update_link(bp, false);
16184 	if (rc) {
16185 		mutex_unlock(&bp->link_lock);
16186 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16187 			   rc);
16188 		return rc;
16189 	}
16190 
16191 	/* Older firmware does not have supported_auto_speeds, so assume
16192 	 * that all supported speeds can be autonegotiated.
16193 	 */
16194 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16195 		link_info->support_auto_speeds = link_info->support_speeds;
16196 
16197 	bnxt_init_ethtool_link_settings(bp);
16198 	mutex_unlock(&bp->link_lock);
16199 	return 0;
16200 }
16201 
16202 static int bnxt_get_max_irq(struct pci_dev *pdev)
16203 {
16204 	u16 ctrl;
16205 
16206 	if (!pdev->msix_cap)
16207 		return 1;
16208 
16209 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16210 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16211 }
16212 
16213 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16214 				int *max_cp)
16215 {
16216 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16217 	int max_ring_grps = 0, max_irq;
16218 
16219 	*max_tx = hw_resc->max_tx_rings;
16220 	*max_rx = hw_resc->max_rx_rings;
16221 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16222 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16223 			bnxt_get_ulp_msix_num_in_use(bp),
16224 			hw_resc->max_stat_ctxs -
16225 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16226 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16227 		*max_cp = min_t(int, *max_cp, max_irq);
16228 	max_ring_grps = hw_resc->max_hw_ring_grps;
16229 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16230 		*max_cp -= 1;
16231 		*max_rx -= 2;
16232 	}
16233 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16234 		*max_rx >>= 1;
16235 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16236 		int rc;
16237 
16238 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16239 		if (rc) {
16240 			*max_rx = 0;
16241 			*max_tx = 0;
16242 		}
16243 		/* On P5 chips, max_cp output param should be available NQs */
16244 		*max_cp = max_irq;
16245 	}
16246 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16247 }
16248 
16249 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16250 {
16251 	int rx, tx, cp;
16252 
16253 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16254 	*max_rx = rx;
16255 	*max_tx = tx;
16256 	if (!rx || !tx || !cp)
16257 		return -ENOMEM;
16258 
16259 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16260 }
16261 
16262 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16263 			       bool shared)
16264 {
16265 	int rc;
16266 
16267 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16268 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16269 		/* Not enough rings, try disabling agg rings. */
16270 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16271 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16272 		if (rc) {
16273 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16274 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16275 			return rc;
16276 		}
16277 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16278 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16279 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16280 		bnxt_set_ring_params(bp);
16281 	}
16282 
16283 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16284 		int max_cp, max_stat, max_irq;
16285 
16286 		/* Reserve minimum resources for RoCE */
16287 		max_cp = bnxt_get_max_func_cp_rings(bp);
16288 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16289 		max_irq = bnxt_get_max_func_irqs(bp);
16290 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16291 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16292 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16293 			return 0;
16294 
16295 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16296 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16297 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16298 		max_cp = min_t(int, max_cp, max_irq);
16299 		max_cp = min_t(int, max_cp, max_stat);
16300 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16301 		if (rc)
16302 			rc = 0;
16303 	}
16304 	return rc;
16305 }
16306 
16307 /* In initial default shared ring setting, each shared ring must have a
16308  * RX/TX ring pair.
16309  */
16310 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16311 {
16312 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16313 	bp->rx_nr_rings = bp->cp_nr_rings;
16314 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16315 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16316 }
16317 
16318 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16319 {
16320 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16321 	int avail_msix;
16322 
16323 	if (!bnxt_can_reserve_rings(bp))
16324 		return 0;
16325 
16326 	if (sh)
16327 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16328 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16329 	/* Reduce default rings on multi-port cards so that total default
16330 	 * rings do not exceed CPU count.
16331 	 */
16332 	if (bp->port_count > 1) {
16333 		int max_rings =
16334 			max_t(int, num_online_cpus() / bp->port_count, 1);
16335 
16336 		dflt_rings = min_t(int, dflt_rings, max_rings);
16337 	}
16338 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16339 	if (rc)
16340 		return rc;
16341 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16342 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16343 	if (sh)
16344 		bnxt_trim_dflt_sh_rings(bp);
16345 	else
16346 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16347 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16348 
16349 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16350 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16351 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16352 
16353 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16354 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16355 	}
16356 
16357 	rc = __bnxt_reserve_rings(bp);
16358 	if (rc && rc != -ENODEV)
16359 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16360 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16361 	if (sh)
16362 		bnxt_trim_dflt_sh_rings(bp);
16363 
16364 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16365 	if (bnxt_need_reserve_rings(bp)) {
16366 		rc = __bnxt_reserve_rings(bp);
16367 		if (rc && rc != -ENODEV)
16368 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16369 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16370 	}
16371 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16372 		bp->rx_nr_rings++;
16373 		bp->cp_nr_rings++;
16374 	}
16375 	if (rc) {
16376 		bp->tx_nr_rings = 0;
16377 		bp->rx_nr_rings = 0;
16378 	}
16379 	return rc;
16380 }
16381 
16382 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16383 {
16384 	int rc;
16385 
16386 	if (bp->tx_nr_rings)
16387 		return 0;
16388 
16389 	bnxt_ulp_irq_stop(bp);
16390 	bnxt_clear_int_mode(bp);
16391 	rc = bnxt_set_dflt_rings(bp, true);
16392 	if (rc) {
16393 		if (BNXT_VF(bp) && rc == -ENODEV)
16394 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16395 		else
16396 			netdev_err(bp->dev, "Not enough rings available.\n");
16397 		goto init_dflt_ring_err;
16398 	}
16399 	rc = bnxt_init_int_mode(bp);
16400 	if (rc)
16401 		goto init_dflt_ring_err;
16402 
16403 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16404 
16405 	bnxt_set_dflt_rfs(bp);
16406 
16407 init_dflt_ring_err:
16408 	bnxt_ulp_irq_restart(bp, rc);
16409 	return rc;
16410 }
16411 
16412 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16413 {
16414 	int rc;
16415 
16416 	netdev_ops_assert_locked(bp->dev);
16417 	bnxt_hwrm_func_qcaps(bp);
16418 
16419 	if (netif_running(bp->dev))
16420 		__bnxt_close_nic(bp, true, false);
16421 
16422 	bnxt_ulp_irq_stop(bp);
16423 	bnxt_clear_int_mode(bp);
16424 	rc = bnxt_init_int_mode(bp);
16425 	bnxt_ulp_irq_restart(bp, rc);
16426 
16427 	if (netif_running(bp->dev)) {
16428 		if (rc)
16429 			netif_close(bp->dev);
16430 		else
16431 			rc = bnxt_open_nic(bp, true, false);
16432 	}
16433 
16434 	return rc;
16435 }
16436 
16437 static int bnxt_init_mac_addr(struct bnxt *bp)
16438 {
16439 	int rc = 0;
16440 
16441 	if (BNXT_PF(bp)) {
16442 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16443 	} else {
16444 #ifdef CONFIG_BNXT_SRIOV
16445 		struct bnxt_vf_info *vf = &bp->vf;
16446 		bool strict_approval = true;
16447 
16448 		if (is_valid_ether_addr(vf->mac_addr)) {
16449 			/* overwrite netdev dev_addr with admin VF MAC */
16450 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16451 			/* Older PF driver or firmware may not approve this
16452 			 * correctly.
16453 			 */
16454 			strict_approval = false;
16455 		} else {
16456 			eth_hw_addr_random(bp->dev);
16457 		}
16458 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16459 #endif
16460 	}
16461 	return rc;
16462 }
16463 
16464 static void bnxt_vpd_read_info(struct bnxt *bp)
16465 {
16466 	struct pci_dev *pdev = bp->pdev;
16467 	unsigned int vpd_size, kw_len;
16468 	int pos, size;
16469 	u8 *vpd_data;
16470 
16471 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16472 	if (IS_ERR(vpd_data)) {
16473 		pci_warn(pdev, "Unable to read VPD\n");
16474 		return;
16475 	}
16476 
16477 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16478 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16479 	if (pos < 0)
16480 		goto read_sn;
16481 
16482 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16483 	memcpy(bp->board_partno, &vpd_data[pos], size);
16484 
16485 read_sn:
16486 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16487 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16488 					   &kw_len);
16489 	if (pos < 0)
16490 		goto exit;
16491 
16492 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16493 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16494 exit:
16495 	kfree(vpd_data);
16496 }
16497 
16498 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16499 {
16500 	struct pci_dev *pdev = bp->pdev;
16501 	u64 qword;
16502 
16503 	qword = pci_get_dsn(pdev);
16504 	if (!qword) {
16505 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16506 		return -EOPNOTSUPP;
16507 	}
16508 
16509 	put_unaligned_le64(qword, dsn);
16510 
16511 	bp->flags |= BNXT_FLAG_DSN_VALID;
16512 	return 0;
16513 }
16514 
16515 static int bnxt_map_db_bar(struct bnxt *bp)
16516 {
16517 	if (!bp->db_size)
16518 		return -ENODEV;
16519 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16520 	if (!bp->bar1)
16521 		return -ENOMEM;
16522 	return 0;
16523 }
16524 
16525 void bnxt_print_device_info(struct bnxt *bp)
16526 {
16527 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16528 		    board_info[bp->board_idx].name,
16529 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16530 
16531 	pcie_print_link_status(bp->pdev);
16532 }
16533 
16534 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16535 {
16536 	struct bnxt_hw_resc *hw_resc;
16537 	struct net_device *dev;
16538 	struct bnxt *bp;
16539 	int rc, max_irqs;
16540 
16541 	if (pci_is_bridge(pdev))
16542 		return -ENODEV;
16543 
16544 	if (!pdev->msix_cap) {
16545 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16546 		return -ENODEV;
16547 	}
16548 
16549 	/* Clear any pending DMA transactions from crash kernel
16550 	 * while loading driver in capture kernel.
16551 	 */
16552 	if (is_kdump_kernel()) {
16553 		pci_clear_master(pdev);
16554 		pcie_flr(pdev);
16555 	}
16556 
16557 	max_irqs = bnxt_get_max_irq(pdev);
16558 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16559 				 max_irqs);
16560 	if (!dev)
16561 		return -ENOMEM;
16562 
16563 	bp = netdev_priv(dev);
16564 	bp->board_idx = ent->driver_data;
16565 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16566 	bnxt_set_max_func_irqs(bp, max_irqs);
16567 
16568 	if (bnxt_vf_pciid(bp->board_idx))
16569 		bp->flags |= BNXT_FLAG_VF;
16570 
16571 	/* No devlink port registration in case of a VF */
16572 	if (BNXT_PF(bp))
16573 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16574 
16575 	rc = bnxt_init_board(pdev, dev);
16576 	if (rc < 0)
16577 		goto init_err_free;
16578 
16579 	dev->netdev_ops = &bnxt_netdev_ops;
16580 	dev->stat_ops = &bnxt_stat_ops;
16581 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16582 	dev->ethtool_ops = &bnxt_ethtool_ops;
16583 	pci_set_drvdata(pdev, dev);
16584 
16585 	rc = bnxt_alloc_hwrm_resources(bp);
16586 	if (rc)
16587 		goto init_err_pci_clean;
16588 
16589 	mutex_init(&bp->hwrm_cmd_lock);
16590 	mutex_init(&bp->link_lock);
16591 
16592 	rc = bnxt_fw_init_one_p1(bp);
16593 	if (rc)
16594 		goto init_err_pci_clean;
16595 
16596 	if (BNXT_PF(bp))
16597 		bnxt_vpd_read_info(bp);
16598 
16599 	if (BNXT_CHIP_P5_PLUS(bp)) {
16600 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16601 		if (BNXT_CHIP_P7(bp))
16602 			bp->flags |= BNXT_FLAG_CHIP_P7;
16603 	}
16604 
16605 	rc = bnxt_alloc_rss_indir_tbl(bp);
16606 	if (rc)
16607 		goto init_err_pci_clean;
16608 
16609 	rc = bnxt_fw_init_one_p2(bp);
16610 	if (rc)
16611 		goto init_err_pci_clean;
16612 
16613 	rc = bnxt_map_db_bar(bp);
16614 	if (rc) {
16615 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16616 			rc);
16617 		goto init_err_pci_clean;
16618 	}
16619 
16620 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16621 			   NETIF_F_TSO | NETIF_F_TSO6 |
16622 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16623 			   NETIF_F_GSO_IPXIP4 |
16624 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16625 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16626 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16627 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16628 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16629 
16630 	if (BNXT_SUPPORTS_TPA(bp))
16631 		dev->hw_features |= NETIF_F_LRO;
16632 
16633 	dev->hw_enc_features =
16634 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16635 			NETIF_F_TSO | NETIF_F_TSO6 |
16636 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16637 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16638 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16639 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16640 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16641 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16642 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16643 	else
16644 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16645 
16646 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16647 				    NETIF_F_GSO_GRE_CSUM;
16648 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16649 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16650 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16651 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16652 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16653 	if (BNXT_SUPPORTS_TPA(bp))
16654 		dev->hw_features |= NETIF_F_GRO_HW;
16655 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16656 	if (dev->features & NETIF_F_GRO_HW)
16657 		dev->features &= ~NETIF_F_LRO;
16658 	dev->priv_flags |= IFF_UNICAST_FLT;
16659 
16660 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16661 	if (bp->tso_max_segs)
16662 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16663 
16664 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16665 			    NETDEV_XDP_ACT_RX_SG;
16666 
16667 #ifdef CONFIG_BNXT_SRIOV
16668 	init_waitqueue_head(&bp->sriov_cfg_wait);
16669 #endif
16670 	if (BNXT_SUPPORTS_TPA(bp)) {
16671 		bp->gro_func = bnxt_gro_func_5730x;
16672 		if (BNXT_CHIP_P4(bp))
16673 			bp->gro_func = bnxt_gro_func_5731x;
16674 		else if (BNXT_CHIP_P5_PLUS(bp))
16675 			bp->gro_func = bnxt_gro_func_5750x;
16676 	}
16677 	if (!BNXT_CHIP_P4_PLUS(bp))
16678 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16679 
16680 	rc = bnxt_init_mac_addr(bp);
16681 	if (rc) {
16682 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16683 		rc = -EADDRNOTAVAIL;
16684 		goto init_err_pci_clean;
16685 	}
16686 
16687 	if (BNXT_PF(bp)) {
16688 		/* Read the adapter's DSN to use as the eswitch switch_id */
16689 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16690 	}
16691 
16692 	/* MTU range: 60 - FW defined max */
16693 	dev->min_mtu = ETH_ZLEN;
16694 	dev->max_mtu = bp->max_mtu;
16695 
16696 	rc = bnxt_probe_phy(bp, true);
16697 	if (rc)
16698 		goto init_err_pci_clean;
16699 
16700 	hw_resc = &bp->hw_resc;
16701 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16702 		       BNXT_L2_FLTR_MAX_FLTR;
16703 	/* Older firmware may not report these filters properly */
16704 	if (bp->max_fltr < BNXT_MAX_FLTR)
16705 		bp->max_fltr = BNXT_MAX_FLTR;
16706 	bnxt_init_l2_fltr_tbl(bp);
16707 	__bnxt_set_rx_skb_mode(bp, false);
16708 	bnxt_set_tpa_flags(bp);
16709 	bnxt_init_ring_params(bp);
16710 	bnxt_set_ring_params(bp);
16711 	bnxt_rdma_aux_device_init(bp);
16712 	rc = bnxt_set_dflt_rings(bp, true);
16713 	if (rc) {
16714 		if (BNXT_VF(bp) && rc == -ENODEV) {
16715 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16716 		} else {
16717 			netdev_err(bp->dev, "Not enough rings available.\n");
16718 			rc = -ENOMEM;
16719 		}
16720 		goto init_err_pci_clean;
16721 	}
16722 
16723 	bnxt_fw_init_one_p3(bp);
16724 
16725 	bnxt_init_dflt_coal(bp);
16726 
16727 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16728 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16729 
16730 	rc = bnxt_init_int_mode(bp);
16731 	if (rc)
16732 		goto init_err_pci_clean;
16733 
16734 	/* No TC has been set yet and rings may have been trimmed due to
16735 	 * limited MSIX, so we re-initialize the TX rings per TC.
16736 	 */
16737 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16738 
16739 	if (BNXT_PF(bp)) {
16740 		if (!bnxt_pf_wq) {
16741 			bnxt_pf_wq =
16742 				create_singlethread_workqueue("bnxt_pf_wq");
16743 			if (!bnxt_pf_wq) {
16744 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16745 				rc = -ENOMEM;
16746 				goto init_err_pci_clean;
16747 			}
16748 		}
16749 		rc = bnxt_init_tc(bp);
16750 		if (rc)
16751 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16752 				   rc);
16753 	}
16754 
16755 	bnxt_inv_fw_health_reg(bp);
16756 	rc = bnxt_dl_register(bp);
16757 	if (rc)
16758 		goto init_err_dl;
16759 
16760 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16761 
16762 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16763 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16764 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16765 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16766 	dev->request_ops_lock = true;
16767 	dev->netmem_tx = true;
16768 
16769 	rc = register_netdev(dev);
16770 	if (rc)
16771 		goto init_err_cleanup;
16772 
16773 	bnxt_dl_fw_reporters_create(bp);
16774 
16775 	bnxt_rdma_aux_device_add(bp);
16776 
16777 	bnxt_print_device_info(bp);
16778 
16779 	pci_save_state(pdev);
16780 
16781 	return 0;
16782 init_err_cleanup:
16783 	bnxt_rdma_aux_device_uninit(bp);
16784 	bnxt_dl_unregister(bp);
16785 init_err_dl:
16786 	bnxt_shutdown_tc(bp);
16787 	bnxt_clear_int_mode(bp);
16788 
16789 init_err_pci_clean:
16790 	bnxt_hwrm_func_drv_unrgtr(bp);
16791 	bnxt_free_hwrm_resources(bp);
16792 	bnxt_hwmon_uninit(bp);
16793 	bnxt_ethtool_free(bp);
16794 	bnxt_ptp_clear(bp);
16795 	kfree(bp->ptp_cfg);
16796 	bp->ptp_cfg = NULL;
16797 	kfree(bp->fw_health);
16798 	bp->fw_health = NULL;
16799 	bnxt_cleanup_pci(bp);
16800 	bnxt_free_ctx_mem(bp, true);
16801 	bnxt_free_crash_dump_mem(bp);
16802 	kfree(bp->rss_indir_tbl);
16803 	bp->rss_indir_tbl = NULL;
16804 
16805 init_err_free:
16806 	free_netdev(dev);
16807 	return rc;
16808 }
16809 
16810 static void bnxt_shutdown(struct pci_dev *pdev)
16811 {
16812 	struct net_device *dev = pci_get_drvdata(pdev);
16813 	struct bnxt *bp;
16814 
16815 	if (!dev)
16816 		return;
16817 
16818 	rtnl_lock();
16819 	netdev_lock(dev);
16820 	bp = netdev_priv(dev);
16821 	if (!bp)
16822 		goto shutdown_exit;
16823 
16824 	if (netif_running(dev))
16825 		netif_close(dev);
16826 
16827 	bnxt_ptp_clear(bp);
16828 	bnxt_clear_int_mode(bp);
16829 	pci_disable_device(pdev);
16830 
16831 	if (system_state == SYSTEM_POWER_OFF) {
16832 		pci_wake_from_d3(pdev, bp->wol);
16833 		pci_set_power_state(pdev, PCI_D3hot);
16834 	}
16835 
16836 shutdown_exit:
16837 	netdev_unlock(dev);
16838 	rtnl_unlock();
16839 }
16840 
16841 #ifdef CONFIG_PM_SLEEP
16842 static int bnxt_suspend(struct device *device)
16843 {
16844 	struct net_device *dev = dev_get_drvdata(device);
16845 	struct bnxt *bp = netdev_priv(dev);
16846 	int rc = 0;
16847 
16848 	bnxt_ulp_stop(bp);
16849 
16850 	netdev_lock(dev);
16851 	if (netif_running(dev)) {
16852 		netif_device_detach(dev);
16853 		rc = bnxt_close(dev);
16854 	}
16855 	bnxt_hwrm_func_drv_unrgtr(bp);
16856 	bnxt_ptp_clear(bp);
16857 	pci_disable_device(bp->pdev);
16858 	bnxt_free_ctx_mem(bp, false);
16859 	netdev_unlock(dev);
16860 	return rc;
16861 }
16862 
16863 static int bnxt_resume(struct device *device)
16864 {
16865 	struct net_device *dev = dev_get_drvdata(device);
16866 	struct bnxt *bp = netdev_priv(dev);
16867 	int rc = 0;
16868 
16869 	netdev_lock(dev);
16870 	rc = pci_enable_device(bp->pdev);
16871 	if (rc) {
16872 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16873 			   rc);
16874 		goto resume_exit;
16875 	}
16876 	pci_set_master(bp->pdev);
16877 	if (bnxt_hwrm_ver_get(bp)) {
16878 		rc = -ENODEV;
16879 		goto resume_exit;
16880 	}
16881 	rc = bnxt_hwrm_func_reset(bp);
16882 	if (rc) {
16883 		rc = -EBUSY;
16884 		goto resume_exit;
16885 	}
16886 
16887 	rc = bnxt_hwrm_func_qcaps(bp);
16888 	if (rc)
16889 		goto resume_exit;
16890 
16891 	bnxt_clear_reservations(bp, true);
16892 
16893 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16894 		rc = -ENODEV;
16895 		goto resume_exit;
16896 	}
16897 	if (bp->fw_crash_mem)
16898 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16899 
16900 	if (bnxt_ptp_init(bp)) {
16901 		kfree(bp->ptp_cfg);
16902 		bp->ptp_cfg = NULL;
16903 	}
16904 	bnxt_get_wol_settings(bp);
16905 	if (netif_running(dev)) {
16906 		rc = bnxt_open(dev);
16907 		if (!rc)
16908 			netif_device_attach(dev);
16909 	}
16910 
16911 resume_exit:
16912 	netdev_unlock(bp->dev);
16913 	bnxt_ulp_start(bp, rc);
16914 	if (!rc)
16915 		bnxt_reenable_sriov(bp);
16916 	return rc;
16917 }
16918 
16919 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16920 #define BNXT_PM_OPS (&bnxt_pm_ops)
16921 
16922 #else
16923 
16924 #define BNXT_PM_OPS NULL
16925 
16926 #endif /* CONFIG_PM_SLEEP */
16927 
16928 /**
16929  * bnxt_io_error_detected - called when PCI error is detected
16930  * @pdev: Pointer to PCI device
16931  * @state: The current pci connection state
16932  *
16933  * This function is called after a PCI bus error affecting
16934  * this device has been detected.
16935  */
16936 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16937 					       pci_channel_state_t state)
16938 {
16939 	struct net_device *netdev = pci_get_drvdata(pdev);
16940 	struct bnxt *bp = netdev_priv(netdev);
16941 	bool abort = false;
16942 
16943 	netdev_info(netdev, "PCI I/O error detected\n");
16944 
16945 	bnxt_ulp_stop(bp);
16946 
16947 	netdev_lock(netdev);
16948 	netif_device_detach(netdev);
16949 
16950 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16951 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16952 		abort = true;
16953 	}
16954 
16955 	if (abort || state == pci_channel_io_perm_failure) {
16956 		netdev_unlock(netdev);
16957 		return PCI_ERS_RESULT_DISCONNECT;
16958 	}
16959 
16960 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16961 	 * so we disable bus master to prevent any potential bad DMAs before
16962 	 * freeing kernel memory.
16963 	 */
16964 	if (state == pci_channel_io_frozen) {
16965 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16966 		bnxt_fw_fatal_close(bp);
16967 	}
16968 
16969 	if (netif_running(netdev))
16970 		__bnxt_close_nic(bp, true, true);
16971 
16972 	if (pci_is_enabled(pdev))
16973 		pci_disable_device(pdev);
16974 	bnxt_free_ctx_mem(bp, false);
16975 	netdev_unlock(netdev);
16976 
16977 	/* Request a slot slot reset. */
16978 	return PCI_ERS_RESULT_NEED_RESET;
16979 }
16980 
16981 /**
16982  * bnxt_io_slot_reset - called after the pci bus has been reset.
16983  * @pdev: Pointer to PCI device
16984  *
16985  * Restart the card from scratch, as if from a cold-boot.
16986  * At this point, the card has experienced a hard reset,
16987  * followed by fixups by BIOS, and has its config space
16988  * set up identically to what it was at cold boot.
16989  */
16990 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16991 {
16992 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16993 	struct net_device *netdev = pci_get_drvdata(pdev);
16994 	struct bnxt *bp = netdev_priv(netdev);
16995 	int retry = 0;
16996 	int err = 0;
16997 	int off;
16998 
16999 	netdev_info(bp->dev, "PCI Slot Reset\n");
17000 
17001 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17002 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17003 		msleep(900);
17004 
17005 	netdev_lock(netdev);
17006 
17007 	if (pci_enable_device(pdev)) {
17008 		dev_err(&pdev->dev,
17009 			"Cannot re-enable PCI device after reset.\n");
17010 	} else {
17011 		pci_set_master(pdev);
17012 		/* Upon fatal error, our device internal logic that latches to
17013 		 * BAR value is getting reset and will restore only upon
17014 		 * rewriting the BARs.
17015 		 *
17016 		 * As pci_restore_state() does not re-write the BARs if the
17017 		 * value is same as saved value earlier, driver needs to
17018 		 * write the BARs to 0 to force restore, in case of fatal error.
17019 		 */
17020 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17021 				       &bp->state)) {
17022 			for (off = PCI_BASE_ADDRESS_0;
17023 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17024 				pci_write_config_dword(bp->pdev, off, 0);
17025 		}
17026 		pci_restore_state(pdev);
17027 		pci_save_state(pdev);
17028 
17029 		bnxt_inv_fw_health_reg(bp);
17030 		bnxt_try_map_fw_health_reg(bp);
17031 
17032 		/* In some PCIe AER scenarios, firmware may take up to
17033 		 * 10 seconds to become ready in the worst case.
17034 		 */
17035 		do {
17036 			err = bnxt_try_recover_fw(bp);
17037 			if (!err)
17038 				break;
17039 			retry++;
17040 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17041 
17042 		if (err) {
17043 			dev_err(&pdev->dev, "Firmware not ready\n");
17044 			goto reset_exit;
17045 		}
17046 
17047 		err = bnxt_hwrm_func_reset(bp);
17048 		if (!err)
17049 			result = PCI_ERS_RESULT_RECOVERED;
17050 
17051 		/* IRQ will be initialized later in bnxt_io_resume */
17052 		bnxt_ulp_irq_stop(bp);
17053 		bnxt_clear_int_mode(bp);
17054 	}
17055 
17056 reset_exit:
17057 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17058 	bnxt_clear_reservations(bp, true);
17059 	netdev_unlock(netdev);
17060 
17061 	return result;
17062 }
17063 
17064 /**
17065  * bnxt_io_resume - called when traffic can start flowing again.
17066  * @pdev: Pointer to PCI device
17067  *
17068  * This callback is called when the error recovery driver tells
17069  * us that its OK to resume normal operation.
17070  */
17071 static void bnxt_io_resume(struct pci_dev *pdev)
17072 {
17073 	struct net_device *netdev = pci_get_drvdata(pdev);
17074 	struct bnxt *bp = netdev_priv(netdev);
17075 	int err;
17076 
17077 	netdev_info(bp->dev, "PCI Slot Resume\n");
17078 	netdev_lock(netdev);
17079 
17080 	err = bnxt_hwrm_func_qcaps(bp);
17081 	if (!err) {
17082 		if (netif_running(netdev)) {
17083 			err = bnxt_open(netdev);
17084 		} else {
17085 			err = bnxt_reserve_rings(bp, true);
17086 			if (!err)
17087 				err = bnxt_init_int_mode(bp);
17088 		}
17089 	}
17090 
17091 	if (!err)
17092 		netif_device_attach(netdev);
17093 
17094 	netdev_unlock(netdev);
17095 	bnxt_ulp_start(bp, err);
17096 	if (!err)
17097 		bnxt_reenable_sriov(bp);
17098 }
17099 
17100 static const struct pci_error_handlers bnxt_err_handler = {
17101 	.error_detected	= bnxt_io_error_detected,
17102 	.slot_reset	= bnxt_io_slot_reset,
17103 	.resume		= bnxt_io_resume
17104 };
17105 
17106 static struct pci_driver bnxt_pci_driver = {
17107 	.name		= DRV_MODULE_NAME,
17108 	.id_table	= bnxt_pci_tbl,
17109 	.probe		= bnxt_init_one,
17110 	.remove		= bnxt_remove_one,
17111 	.shutdown	= bnxt_shutdown,
17112 	.driver.pm	= BNXT_PM_OPS,
17113 	.err_handler	= &bnxt_err_handler,
17114 #if defined(CONFIG_BNXT_SRIOV)
17115 	.sriov_configure = bnxt_sriov_configure,
17116 #endif
17117 };
17118 
17119 static int __init bnxt_init(void)
17120 {
17121 	int err;
17122 
17123 	bnxt_debug_init();
17124 	err = pci_register_driver(&bnxt_pci_driver);
17125 	if (err) {
17126 		bnxt_debug_exit();
17127 		return err;
17128 	}
17129 
17130 	return 0;
17131 }
17132 
17133 static void __exit bnxt_exit(void)
17134 {
17135 	pci_unregister_driver(&bnxt_pci_driver);
17136 	if (bnxt_pf_wq)
17137 		destroy_workqueue(bnxt_pf_wq);
17138 	bnxt_debug_exit();
17139 }
17140 
17141 module_init(bnxt_init);
17142 module_exit(bnxt_exit);
17143