xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision df9c299371054cb725eef730fd0f1d0fe2ed6bb0)
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 
481 	i = skb_get_queue_mapping(skb);
482 	if (unlikely(i >= bp->tx_nr_rings)) {
483 		dev_kfree_skb_any(skb);
484 		dev_core_stats_tx_dropped_inc(dev);
485 		return NETDEV_TX_OK;
486 	}
487 
488 	txq = netdev_get_tx_queue(dev, i);
489 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
490 	prod = txr->tx_prod;
491 
492 #if (MAX_SKB_FRAGS > TX_MAX_FRAGS)
493 	if (skb_shinfo(skb)->nr_frags > TX_MAX_FRAGS) {
494 		netdev_warn_once(dev, "SKB has too many (%d) fragments, max supported is %d.  SKB will be linearized.\n",
495 				 skb_shinfo(skb)->nr_frags, TX_MAX_FRAGS);
496 		if (skb_linearize(skb)) {
497 			dev_kfree_skb_any(skb);
498 			dev_core_stats_tx_dropped_inc(dev);
499 			return NETDEV_TX_OK;
500 		}
501 	}
502 #endif
503 	free_size = bnxt_tx_avail(bp, txr);
504 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
505 		/* We must have raced with NAPI cleanup */
506 		if (net_ratelimit() && txr->kick_pending)
507 			netif_warn(bp, tx_err, dev,
508 				   "bnxt: ring busy w/ flush pending!\n");
509 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
510 					bp->tx_wake_thresh))
511 			return NETDEV_TX_BUSY;
512 	}
513 
514 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
515 		goto tx_free;
516 
517 	length = skb->len;
518 	len = skb_headlen(skb);
519 	last_frag = skb_shinfo(skb)->nr_frags;
520 
521 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
522 
523 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
524 	tx_buf->skb = skb;
525 	tx_buf->nr_frags = last_frag;
526 
527 	vlan_tag_flags = 0;
528 	cfa_action = bnxt_xmit_get_cfa_action(skb);
529 	if (skb_vlan_tag_present(skb)) {
530 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
531 				 skb_vlan_tag_get(skb);
532 		/* Currently supports 8021Q, 8021AD vlan offloads
533 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
534 		 */
535 		if (skb->vlan_proto == htons(ETH_P_8021Q))
536 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
537 	}
538 
539 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
540 	    ptp->tx_tstamp_en) {
541 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
542 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
543 			tx_buf->is_ts_pkt = 1;
544 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
545 		} else if (!skb_is_gso(skb)) {
546 			u16 seq_id, hdr_off;
547 
548 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
549 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
550 				if (vlan_tag_flags)
551 					hdr_off += VLAN_HLEN;
552 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
553 				tx_buf->is_ts_pkt = 1;
554 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
555 
556 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
557 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
558 				tx_buf->txts_prod = txts_prod;
559 			}
560 		}
561 	}
562 	if (unlikely(skb->no_fcs))
563 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
564 
565 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
566 	    !lflags) {
567 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
568 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
569 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
570 		void __iomem *db = txr->tx_db.doorbell;
571 		void *pdata = tx_push_buf->data;
572 		u64 *end;
573 		int j, push_len;
574 
575 		/* Set COAL_NOW to be ready quickly for the next push */
576 		tx_push->tx_bd_len_flags_type =
577 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
578 					TX_BD_TYPE_LONG_TX_BD |
579 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
580 					TX_BD_FLAGS_COAL_NOW |
581 					TX_BD_FLAGS_PACKET_END |
582 					TX_BD_CNT(2));
583 
584 		if (skb->ip_summed == CHECKSUM_PARTIAL)
585 			tx_push1->tx_bd_hsize_lflags =
586 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
587 		else
588 			tx_push1->tx_bd_hsize_lflags = 0;
589 
590 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
591 		tx_push1->tx_bd_cfa_action =
592 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
593 
594 		end = pdata + length;
595 		end = PTR_ALIGN(end, 8) - 1;
596 		*end = 0;
597 
598 		skb_copy_from_linear_data(skb, pdata, len);
599 		pdata += len;
600 		for (j = 0; j < last_frag; j++) {
601 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
602 			void *fptr;
603 
604 			fptr = skb_frag_address_safe(frag);
605 			if (!fptr)
606 				goto normal_tx;
607 
608 			memcpy(pdata, fptr, skb_frag_size(frag));
609 			pdata += skb_frag_size(frag);
610 		}
611 
612 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
613 		txbd->tx_bd_haddr = txr->data_mapping;
614 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
615 		prod = NEXT_TX(prod);
616 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
617 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
618 		memcpy(txbd, tx_push1, sizeof(*txbd));
619 		prod = NEXT_TX(prod);
620 		tx_push->doorbell =
621 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
622 				    DB_RING_IDX(&txr->tx_db, prod));
623 		WRITE_ONCE(txr->tx_prod, prod);
624 
625 		tx_buf->is_push = 1;
626 		netdev_tx_sent_queue(txq, skb->len);
627 		wmb();	/* Sync is_push and byte queue before pushing data */
628 
629 		push_len = (length + sizeof(*tx_push) + 7) / 8;
630 		if (push_len > 16) {
631 			__iowrite64_copy(db, tx_push_buf, 16);
632 			__iowrite32_copy(db + 4, tx_push_buf + 1,
633 					 (push_len - 16) << 1);
634 		} else {
635 			__iowrite64_copy(db, tx_push_buf, push_len);
636 		}
637 
638 		goto tx_done;
639 	}
640 
641 normal_tx:
642 	if (length < BNXT_MIN_PKT_SIZE) {
643 		pad = BNXT_MIN_PKT_SIZE - length;
644 		if (skb_pad(skb, pad))
645 			/* SKB already freed. */
646 			goto tx_kick_pending;
647 		length = BNXT_MIN_PKT_SIZE;
648 	}
649 
650 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
651 
652 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
653 		goto tx_free;
654 
655 	dma_unmap_addr_set(tx_buf, mapping, mapping);
656 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
657 		TX_BD_CNT(last_frag + 2);
658 
659 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
660 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
661 
662 	prod = NEXT_TX(prod);
663 	txbd1 = (struct tx_bd_ext *)
664 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
665 
666 	txbd1->tx_bd_hsize_lflags = lflags;
667 	if (skb_is_gso(skb)) {
668 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
669 		u32 hdr_len;
670 
671 		if (skb->encapsulation) {
672 			if (udp_gso)
673 				hdr_len = skb_inner_transport_offset(skb) +
674 					  sizeof(struct udphdr);
675 			else
676 				hdr_len = skb_inner_tcp_all_headers(skb);
677 		} else if (udp_gso) {
678 			hdr_len = skb_transport_offset(skb) +
679 				  sizeof(struct udphdr);
680 		} else {
681 			hdr_len = skb_tcp_all_headers(skb);
682 		}
683 
684 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
685 					TX_BD_FLAGS_T_IPID |
686 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
687 		length = skb_shinfo(skb)->gso_size;
688 		txbd1->tx_bd_mss = cpu_to_le32(length);
689 		length += hdr_len;
690 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
691 		txbd1->tx_bd_hsize_lflags |=
692 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
693 		txbd1->tx_bd_mss = 0;
694 	}
695 
696 	length >>= 9;
697 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
698 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
699 				     skb->len);
700 		i = 0;
701 		goto tx_dma_error;
702 	}
703 	flags |= bnxt_lhint_arr[length];
704 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
705 
706 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
707 	txbd1->tx_bd_cfa_action =
708 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
709 	txbd0 = txbd;
710 	for (i = 0; i < last_frag; i++) {
711 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
712 
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 		dma_unmap_addr_set(tx_buf, mapping, mapping);
725 
726 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
727 
728 		flags = len << TX_BD_LEN_SHIFT;
729 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
730 	}
731 
732 	flags &= ~TX_BD_LEN;
733 	txbd->tx_bd_len_flags_type =
734 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
735 			    TX_BD_FLAGS_PACKET_END);
736 
737 	netdev_tx_sent_queue(txq, skb->len);
738 
739 	skb_tx_timestamp(skb);
740 
741 	prod = NEXT_TX(prod);
742 	WRITE_ONCE(txr->tx_prod, prod);
743 
744 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
745 		bnxt_txr_db_kick(bp, txr, prod);
746 	} else {
747 		if (free_size >= bp->tx_wake_thresh)
748 			txbd0->tx_bd_len_flags_type |=
749 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
750 		txr->kick_pending = 1;
751 	}
752 
753 tx_done:
754 
755 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
756 		if (netdev_xmit_more() && !tx_buf->is_push) {
757 			txbd0->tx_bd_len_flags_type &=
758 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
759 			bnxt_txr_db_kick(bp, txr, prod);
760 		}
761 
762 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
763 				   bp->tx_wake_thresh);
764 	}
765 	return NETDEV_TX_OK;
766 
767 tx_dma_error:
768 	last_frag = i;
769 
770 	/* start back at beginning and unmap skb */
771 	prod = txr->tx_prod;
772 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
773 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
774 			 skb_headlen(skb), DMA_TO_DEVICE);
775 	prod = NEXT_TX(prod);
776 
777 	/* unmap remaining mapped pages */
778 	for (i = 0; i < last_frag; i++) {
779 		prod = NEXT_TX(prod);
780 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
781 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
782 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
783 			       DMA_TO_DEVICE);
784 	}
785 
786 tx_free:
787 	dev_kfree_skb_any(skb);
788 tx_kick_pending:
789 	if (BNXT_TX_PTP_IS_SET(lflags)) {
790 		txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].is_ts_pkt = 0;
791 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
792 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
793 			/* set SKB to err so PTP worker will clean up */
794 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
795 	}
796 	if (txr->kick_pending)
797 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
798 	txr->tx_buf_ring[RING_TX(bp, txr->tx_prod)].skb = NULL;
799 	dev_core_stats_tx_dropped_inc(dev);
800 	return NETDEV_TX_OK;
801 }
802 
803 /* Returns true if some remaining TX packets not processed. */
804 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
805 			  int budget)
806 {
807 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
808 	struct pci_dev *pdev = bp->pdev;
809 	u16 hw_cons = txr->tx_hw_cons;
810 	unsigned int tx_bytes = 0;
811 	u16 cons = txr->tx_cons;
812 	int tx_pkts = 0;
813 	bool rc = false;
814 
815 	while (RING_TX(bp, cons) != hw_cons) {
816 		struct bnxt_sw_tx_bd *tx_buf;
817 		struct sk_buff *skb;
818 		bool is_ts_pkt;
819 		int j, last;
820 
821 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
822 		skb = tx_buf->skb;
823 
824 		if (unlikely(!skb)) {
825 			bnxt_sched_reset_txr(bp, txr, cons);
826 			return rc;
827 		}
828 
829 		is_ts_pkt = tx_buf->is_ts_pkt;
830 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
831 			rc = true;
832 			break;
833 		}
834 
835 		cons = NEXT_TX(cons);
836 		tx_pkts++;
837 		tx_bytes += skb->len;
838 		tx_buf->skb = NULL;
839 		tx_buf->is_ts_pkt = 0;
840 
841 		if (tx_buf->is_push) {
842 			tx_buf->is_push = 0;
843 			goto next_tx_int;
844 		}
845 
846 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
847 				 skb_headlen(skb), DMA_TO_DEVICE);
848 		last = tx_buf->nr_frags;
849 
850 		for (j = 0; j < last; j++) {
851 			cons = NEXT_TX(cons);
852 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
853 			dma_unmap_page(
854 				&pdev->dev,
855 				dma_unmap_addr(tx_buf, mapping),
856 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
857 				DMA_TO_DEVICE);
858 		}
859 		if (unlikely(is_ts_pkt)) {
860 			if (BNXT_CHIP_P5(bp)) {
861 				/* PTP worker takes ownership of the skb */
862 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
863 				skb = NULL;
864 			}
865 		}
866 
867 next_tx_int:
868 		cons = NEXT_TX(cons);
869 
870 		dev_consume_skb_any(skb);
871 	}
872 
873 	WRITE_ONCE(txr->tx_cons, cons);
874 
875 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
876 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
877 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
878 
879 	return rc;
880 }
881 
882 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
883 {
884 	struct bnxt_tx_ring_info *txr;
885 	bool more = false;
886 	int i;
887 
888 	bnxt_for_each_napi_tx(i, bnapi, txr) {
889 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
890 			more |= __bnxt_tx_int(bp, txr, budget);
891 	}
892 	if (!more)
893 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
894 }
895 
896 static bool bnxt_separate_head_pool(struct bnxt_rx_ring_info *rxr)
897 {
898 	return rxr->need_head_pool || PAGE_SIZE > BNXT_RX_PAGE_SIZE;
899 }
900 
901 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
902 					 struct bnxt_rx_ring_info *rxr,
903 					 unsigned int *offset,
904 					 gfp_t gfp)
905 {
906 	struct page *page;
907 
908 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
909 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
910 						BNXT_RX_PAGE_SIZE);
911 	} else {
912 		page = page_pool_dev_alloc_pages(rxr->page_pool);
913 		*offset = 0;
914 	}
915 	if (!page)
916 		return NULL;
917 
918 	*mapping = page_pool_get_dma_addr(page) + *offset;
919 	return page;
920 }
921 
922 static netmem_ref __bnxt_alloc_rx_netmem(struct bnxt *bp, dma_addr_t *mapping,
923 					 struct bnxt_rx_ring_info *rxr,
924 					 gfp_t gfp)
925 {
926 	netmem_ref netmem;
927 
928 	netmem = page_pool_alloc_netmems(rxr->page_pool, gfp);
929 	if (!netmem)
930 		return 0;
931 
932 	*mapping = page_pool_get_dma_addr_netmem(netmem);
933 	return netmem;
934 }
935 
936 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
937 				       struct bnxt_rx_ring_info *rxr,
938 				       gfp_t gfp)
939 {
940 	unsigned int offset;
941 	struct page *page;
942 
943 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
944 				    bp->rx_buf_size, gfp);
945 	if (!page)
946 		return NULL;
947 
948 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
949 	return page_address(page) + offset;
950 }
951 
952 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
953 		       u16 prod, gfp_t gfp)
954 {
955 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
956 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
957 	dma_addr_t mapping;
958 
959 	if (BNXT_RX_PAGE_MODE(bp)) {
960 		unsigned int offset;
961 		struct page *page =
962 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
963 
964 		if (!page)
965 			return -ENOMEM;
966 
967 		mapping += bp->rx_dma_offset;
968 		rx_buf->data = page;
969 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
970 	} else {
971 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
972 
973 		if (!data)
974 			return -ENOMEM;
975 
976 		rx_buf->data = data;
977 		rx_buf->data_ptr = data + bp->rx_offset;
978 	}
979 	rx_buf->mapping = mapping;
980 
981 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
982 	return 0;
983 }
984 
985 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
986 {
987 	u16 prod = rxr->rx_prod;
988 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
989 	struct bnxt *bp = rxr->bnapi->bp;
990 	struct rx_bd *cons_bd, *prod_bd;
991 
992 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
993 	cons_rx_buf = &rxr->rx_buf_ring[cons];
994 
995 	prod_rx_buf->data = data;
996 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
997 
998 	prod_rx_buf->mapping = cons_rx_buf->mapping;
999 
1000 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1001 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
1002 
1003 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
1004 }
1005 
1006 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1007 {
1008 	u16 next, max = rxr->rx_agg_bmap_size;
1009 
1010 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
1011 	if (next >= max)
1012 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
1013 	return next;
1014 }
1015 
1016 static int bnxt_alloc_rx_netmem(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1017 				u16 prod, gfp_t gfp)
1018 {
1019 	struct rx_bd *rxbd =
1020 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1021 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
1022 	u16 sw_prod = rxr->rx_sw_agg_prod;
1023 	unsigned int offset = 0;
1024 	dma_addr_t mapping;
1025 	netmem_ref netmem;
1026 
1027 	netmem = __bnxt_alloc_rx_netmem(bp, &mapping, rxr, gfp);
1028 	if (!netmem)
1029 		return -ENOMEM;
1030 
1031 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1032 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1033 
1034 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1035 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1036 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1037 
1038 	rx_agg_buf->netmem = netmem;
1039 	rx_agg_buf->offset = offset;
1040 	rx_agg_buf->mapping = mapping;
1041 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1042 	rxbd->rx_bd_opaque = sw_prod;
1043 	return 0;
1044 }
1045 
1046 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1047 				       struct bnxt_cp_ring_info *cpr,
1048 				       u16 cp_cons, u16 curr)
1049 {
1050 	struct rx_agg_cmp *agg;
1051 
1052 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1053 	agg = (struct rx_agg_cmp *)
1054 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1055 	return agg;
1056 }
1057 
1058 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1059 					      struct bnxt_rx_ring_info *rxr,
1060 					      u16 agg_id, u16 curr)
1061 {
1062 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1063 
1064 	return &tpa_info->agg_arr[curr];
1065 }
1066 
1067 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1068 				   u16 start, u32 agg_bufs, bool tpa)
1069 {
1070 	struct bnxt_napi *bnapi = cpr->bnapi;
1071 	struct bnxt *bp = bnapi->bp;
1072 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1073 	u16 prod = rxr->rx_agg_prod;
1074 	u16 sw_prod = rxr->rx_sw_agg_prod;
1075 	bool p5_tpa = false;
1076 	u32 i;
1077 
1078 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1079 		p5_tpa = true;
1080 
1081 	for (i = 0; i < agg_bufs; i++) {
1082 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1083 		struct rx_agg_cmp *agg;
1084 		struct rx_bd *prod_bd;
1085 		netmem_ref netmem;
1086 		u16 cons;
1087 
1088 		if (p5_tpa)
1089 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1090 		else
1091 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1092 		cons = agg->rx_agg_cmp_opaque;
1093 		__clear_bit(cons, rxr->rx_agg_bmap);
1094 
1095 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1096 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1097 
1098 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1099 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1100 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1101 
1102 		/* It is possible for sw_prod to be equal to cons, so
1103 		 * set cons_rx_buf->netmem to 0 first.
1104 		 */
1105 		netmem = cons_rx_buf->netmem;
1106 		cons_rx_buf->netmem = 0;
1107 		prod_rx_buf->netmem = netmem;
1108 		prod_rx_buf->offset = cons_rx_buf->offset;
1109 
1110 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1111 
1112 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1113 
1114 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1115 		prod_bd->rx_bd_opaque = sw_prod;
1116 
1117 		prod = NEXT_RX_AGG(prod);
1118 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1119 	}
1120 	rxr->rx_agg_prod = prod;
1121 	rxr->rx_sw_agg_prod = sw_prod;
1122 }
1123 
1124 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1125 					      struct bnxt_rx_ring_info *rxr,
1126 					      u16 cons, void *data, u8 *data_ptr,
1127 					      dma_addr_t dma_addr,
1128 					      unsigned int offset_and_len)
1129 {
1130 	unsigned int len = offset_and_len & 0xffff;
1131 	struct page *page = data;
1132 	u16 prod = rxr->rx_prod;
1133 	struct sk_buff *skb;
1134 	int err;
1135 
1136 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1137 	if (unlikely(err)) {
1138 		bnxt_reuse_rx_data(rxr, cons, data);
1139 		return NULL;
1140 	}
1141 	dma_addr -= bp->rx_dma_offset;
1142 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1143 				bp->rx_dir);
1144 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1145 	if (!skb) {
1146 		page_pool_recycle_direct(rxr->page_pool, page);
1147 		return NULL;
1148 	}
1149 	skb_mark_for_recycle(skb);
1150 	skb_reserve(skb, bp->rx_offset);
1151 	__skb_put(skb, len);
1152 
1153 	return skb;
1154 }
1155 
1156 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1157 					struct bnxt_rx_ring_info *rxr,
1158 					u16 cons, void *data, u8 *data_ptr,
1159 					dma_addr_t dma_addr,
1160 					unsigned int offset_and_len)
1161 {
1162 	unsigned int payload = offset_and_len >> 16;
1163 	unsigned int len = offset_and_len & 0xffff;
1164 	skb_frag_t *frag;
1165 	struct page *page = data;
1166 	u16 prod = rxr->rx_prod;
1167 	struct sk_buff *skb;
1168 	int off, err;
1169 
1170 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1171 	if (unlikely(err)) {
1172 		bnxt_reuse_rx_data(rxr, cons, data);
1173 		return NULL;
1174 	}
1175 	dma_addr -= bp->rx_dma_offset;
1176 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1177 				bp->rx_dir);
1178 
1179 	if (unlikely(!payload))
1180 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1181 
1182 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1183 	if (!skb) {
1184 		page_pool_recycle_direct(rxr->page_pool, page);
1185 		return NULL;
1186 	}
1187 
1188 	skb_mark_for_recycle(skb);
1189 	off = (void *)data_ptr - page_address(page);
1190 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1191 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1192 	       payload + NET_IP_ALIGN);
1193 
1194 	frag = &skb_shinfo(skb)->frags[0];
1195 	skb_frag_size_sub(frag, payload);
1196 	skb_frag_off_add(frag, payload);
1197 	skb->data_len -= payload;
1198 	skb->tail += payload;
1199 
1200 	return skb;
1201 }
1202 
1203 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1204 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1205 				   void *data, u8 *data_ptr,
1206 				   dma_addr_t dma_addr,
1207 				   unsigned int offset_and_len)
1208 {
1209 	u16 prod = rxr->rx_prod;
1210 	struct sk_buff *skb;
1211 	int err;
1212 
1213 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1214 	if (unlikely(err)) {
1215 		bnxt_reuse_rx_data(rxr, cons, data);
1216 		return NULL;
1217 	}
1218 
1219 	skb = napi_build_skb(data, bp->rx_buf_size);
1220 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1221 				bp->rx_dir);
1222 	if (!skb) {
1223 		page_pool_free_va(rxr->head_pool, data, true);
1224 		return NULL;
1225 	}
1226 
1227 	skb_mark_for_recycle(skb);
1228 	skb_reserve(skb, bp->rx_offset);
1229 	skb_put(skb, offset_and_len & 0xffff);
1230 	return skb;
1231 }
1232 
1233 static u32 __bnxt_rx_agg_netmems(struct bnxt *bp,
1234 				 struct bnxt_cp_ring_info *cpr,
1235 				 u16 idx, u32 agg_bufs, bool tpa,
1236 				 struct sk_buff *skb,
1237 				 struct xdp_buff *xdp)
1238 {
1239 	struct bnxt_napi *bnapi = cpr->bnapi;
1240 	struct skb_shared_info *shinfo;
1241 	struct bnxt_rx_ring_info *rxr;
1242 	u32 i, total_frag_len = 0;
1243 	bool p5_tpa = false;
1244 	u16 prod;
1245 
1246 	rxr = bnapi->rx_ring;
1247 	prod = rxr->rx_agg_prod;
1248 
1249 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1250 		p5_tpa = true;
1251 
1252 	if (skb)
1253 		shinfo = skb_shinfo(skb);
1254 	else
1255 		shinfo = xdp_get_shared_info_from_buff(xdp);
1256 
1257 	for (i = 0; i < agg_bufs; i++) {
1258 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1259 		struct rx_agg_cmp *agg;
1260 		u16 cons, frag_len;
1261 		netmem_ref netmem;
1262 
1263 		if (p5_tpa)
1264 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1265 		else
1266 			agg = bnxt_get_agg(bp, cpr, idx, i);
1267 		cons = agg->rx_agg_cmp_opaque;
1268 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1269 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1270 
1271 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1272 		if (skb) {
1273 			skb_add_rx_frag_netmem(skb, i, cons_rx_buf->netmem,
1274 					       cons_rx_buf->offset,
1275 					       frag_len, BNXT_RX_PAGE_SIZE);
1276 		} else {
1277 			skb_frag_t *frag = &shinfo->frags[i];
1278 
1279 			skb_frag_fill_netmem_desc(frag, cons_rx_buf->netmem,
1280 						  cons_rx_buf->offset,
1281 						  frag_len);
1282 			shinfo->nr_frags = i + 1;
1283 		}
1284 		__clear_bit(cons, rxr->rx_agg_bmap);
1285 
1286 		/* It is possible for bnxt_alloc_rx_netmem() to allocate
1287 		 * a sw_prod index that equals the cons index, so we
1288 		 * need to clear the cons entry now.
1289 		 */
1290 		netmem = cons_rx_buf->netmem;
1291 		cons_rx_buf->netmem = 0;
1292 
1293 		if (xdp && netmem_is_pfmemalloc(netmem))
1294 			xdp_buff_set_frag_pfmemalloc(xdp);
1295 
1296 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_ATOMIC) != 0) {
1297 			if (skb) {
1298 				skb->len -= frag_len;
1299 				skb->data_len -= frag_len;
1300 				skb->truesize -= BNXT_RX_PAGE_SIZE;
1301 			}
1302 
1303 			--shinfo->nr_frags;
1304 			cons_rx_buf->netmem = netmem;
1305 
1306 			/* Update prod since possibly some netmems have been
1307 			 * allocated already.
1308 			 */
1309 			rxr->rx_agg_prod = prod;
1310 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1311 			return 0;
1312 		}
1313 
1314 		page_pool_dma_sync_netmem_for_cpu(rxr->page_pool, netmem, 0,
1315 						  BNXT_RX_PAGE_SIZE);
1316 
1317 		total_frag_len += frag_len;
1318 		prod = NEXT_RX_AGG(prod);
1319 	}
1320 	rxr->rx_agg_prod = prod;
1321 	return total_frag_len;
1322 }
1323 
1324 static struct sk_buff *bnxt_rx_agg_netmems_skb(struct bnxt *bp,
1325 					       struct bnxt_cp_ring_info *cpr,
1326 					       struct sk_buff *skb, u16 idx,
1327 					       u32 agg_bufs, bool tpa)
1328 {
1329 	u32 total_frag_len = 0;
1330 
1331 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1332 					       skb, NULL);
1333 	if (!total_frag_len) {
1334 		skb_mark_for_recycle(skb);
1335 		dev_kfree_skb(skb);
1336 		return NULL;
1337 	}
1338 
1339 	return skb;
1340 }
1341 
1342 static u32 bnxt_rx_agg_netmems_xdp(struct bnxt *bp,
1343 				   struct bnxt_cp_ring_info *cpr,
1344 				   struct xdp_buff *xdp, u16 idx,
1345 				   u32 agg_bufs, bool tpa)
1346 {
1347 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1348 	u32 total_frag_len = 0;
1349 
1350 	if (!xdp_buff_has_frags(xdp))
1351 		shinfo->nr_frags = 0;
1352 
1353 	total_frag_len = __bnxt_rx_agg_netmems(bp, cpr, idx, agg_bufs, tpa,
1354 					       NULL, xdp);
1355 	if (total_frag_len) {
1356 		xdp_buff_set_frags_flag(xdp);
1357 		shinfo->nr_frags = agg_bufs;
1358 		shinfo->xdp_frags_size = total_frag_len;
1359 	}
1360 	return total_frag_len;
1361 }
1362 
1363 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1364 			       u8 agg_bufs, u32 *raw_cons)
1365 {
1366 	u16 last;
1367 	struct rx_agg_cmp *agg;
1368 
1369 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1370 	last = RING_CMP(*raw_cons);
1371 	agg = (struct rx_agg_cmp *)
1372 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1373 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1374 }
1375 
1376 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1377 				      unsigned int len,
1378 				      dma_addr_t mapping)
1379 {
1380 	struct bnxt *bp = bnapi->bp;
1381 	struct pci_dev *pdev = bp->pdev;
1382 	struct sk_buff *skb;
1383 
1384 	skb = napi_alloc_skb(&bnapi->napi, len);
1385 	if (!skb)
1386 		return NULL;
1387 
1388 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1389 				bp->rx_dir);
1390 
1391 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1392 	       len + NET_IP_ALIGN);
1393 
1394 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1395 				   bp->rx_dir);
1396 
1397 	skb_put(skb, len);
1398 
1399 	return skb;
1400 }
1401 
1402 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1403 				     unsigned int len,
1404 				     dma_addr_t mapping)
1405 {
1406 	return bnxt_copy_data(bnapi, data, len, mapping);
1407 }
1408 
1409 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1410 				     struct xdp_buff *xdp,
1411 				     unsigned int len,
1412 				     dma_addr_t mapping)
1413 {
1414 	unsigned int metasize = 0;
1415 	u8 *data = xdp->data;
1416 	struct sk_buff *skb;
1417 
1418 	len = xdp->data_end - xdp->data_meta;
1419 	metasize = xdp->data - xdp->data_meta;
1420 	data = xdp->data_meta;
1421 
1422 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1423 	if (!skb)
1424 		return skb;
1425 
1426 	if (metasize) {
1427 		skb_metadata_set(skb, metasize);
1428 		__skb_pull(skb, metasize);
1429 	}
1430 
1431 	return skb;
1432 }
1433 
1434 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1435 			   u32 *raw_cons, void *cmp)
1436 {
1437 	struct rx_cmp *rxcmp = cmp;
1438 	u32 tmp_raw_cons = *raw_cons;
1439 	u8 cmp_type, agg_bufs = 0;
1440 
1441 	cmp_type = RX_CMP_TYPE(rxcmp);
1442 
1443 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1444 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1445 			    RX_CMP_AGG_BUFS) >>
1446 			   RX_CMP_AGG_BUFS_SHIFT;
1447 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1448 		struct rx_tpa_end_cmp *tpa_end = cmp;
1449 
1450 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1451 			return 0;
1452 
1453 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1454 	}
1455 
1456 	if (agg_bufs) {
1457 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1458 			return -EBUSY;
1459 	}
1460 	*raw_cons = tmp_raw_cons;
1461 	return 0;
1462 }
1463 
1464 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1465 {
1466 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1467 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1468 
1469 	if (test_bit(idx, map->agg_idx_bmap))
1470 		idx = find_first_zero_bit(map->agg_idx_bmap,
1471 					  BNXT_AGG_IDX_BMAP_SIZE);
1472 	__set_bit(idx, map->agg_idx_bmap);
1473 	map->agg_id_tbl[agg_id] = idx;
1474 	return idx;
1475 }
1476 
1477 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1478 {
1479 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1480 
1481 	__clear_bit(idx, map->agg_idx_bmap);
1482 }
1483 
1484 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1485 {
1486 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1487 
1488 	return map->agg_id_tbl[agg_id];
1489 }
1490 
1491 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1492 			      struct rx_tpa_start_cmp *tpa_start,
1493 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1494 {
1495 	tpa_info->cfa_code_valid = 1;
1496 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1497 	tpa_info->vlan_valid = 0;
1498 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1499 		tpa_info->vlan_valid = 1;
1500 		tpa_info->metadata =
1501 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1502 	}
1503 }
1504 
1505 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1506 				 struct rx_tpa_start_cmp *tpa_start,
1507 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1508 {
1509 	tpa_info->vlan_valid = 0;
1510 	if (TPA_START_VLAN_VALID(tpa_start)) {
1511 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1512 		u32 vlan_proto = ETH_P_8021Q;
1513 
1514 		tpa_info->vlan_valid = 1;
1515 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1516 			vlan_proto = ETH_P_8021AD;
1517 		tpa_info->metadata = vlan_proto << 16 |
1518 				     TPA_START_METADATA0_TCI(tpa_start1);
1519 	}
1520 }
1521 
1522 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1523 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1524 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1525 {
1526 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1527 	struct bnxt_tpa_info *tpa_info;
1528 	u16 cons, prod, agg_id;
1529 	struct rx_bd *prod_bd;
1530 	dma_addr_t mapping;
1531 
1532 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1533 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1534 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1535 	} else {
1536 		agg_id = TPA_START_AGG_ID(tpa_start);
1537 	}
1538 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1539 	prod = rxr->rx_prod;
1540 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1541 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1542 	tpa_info = &rxr->rx_tpa[agg_id];
1543 
1544 	if (unlikely(cons != rxr->rx_next_cons ||
1545 		     TPA_START_ERROR(tpa_start))) {
1546 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1547 			    cons, rxr->rx_next_cons,
1548 			    TPA_START_ERROR_CODE(tpa_start1));
1549 		bnxt_sched_reset_rxr(bp, rxr);
1550 		return;
1551 	}
1552 	prod_rx_buf->data = tpa_info->data;
1553 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1554 
1555 	mapping = tpa_info->mapping;
1556 	prod_rx_buf->mapping = mapping;
1557 
1558 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1559 
1560 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1561 
1562 	tpa_info->data = cons_rx_buf->data;
1563 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1564 	cons_rx_buf->data = NULL;
1565 	tpa_info->mapping = cons_rx_buf->mapping;
1566 
1567 	tpa_info->len =
1568 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1569 				RX_TPA_START_CMP_LEN_SHIFT;
1570 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1571 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1572 		tpa_info->gso_type = SKB_GSO_TCPV4;
1573 		if (TPA_START_IS_IPV6(tpa_start1))
1574 			tpa_info->gso_type = SKB_GSO_TCPV6;
1575 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1576 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1577 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1578 			tpa_info->gso_type = SKB_GSO_TCPV6;
1579 		tpa_info->rss_hash =
1580 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1581 	} else {
1582 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1583 		tpa_info->gso_type = 0;
1584 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1585 	}
1586 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1587 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1588 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1589 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1590 	else
1591 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1592 	tpa_info->agg_count = 0;
1593 
1594 	rxr->rx_prod = NEXT_RX(prod);
1595 	cons = RING_RX(bp, NEXT_RX(cons));
1596 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1597 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1598 
1599 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1600 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1601 	cons_rx_buf->data = NULL;
1602 }
1603 
1604 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1605 {
1606 	if (agg_bufs)
1607 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1608 }
1609 
1610 #ifdef CONFIG_INET
1611 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1612 {
1613 	struct udphdr *uh = NULL;
1614 
1615 	if (ip_proto == htons(ETH_P_IP)) {
1616 		struct iphdr *iph = (struct iphdr *)skb->data;
1617 
1618 		if (iph->protocol == IPPROTO_UDP)
1619 			uh = (struct udphdr *)(iph + 1);
1620 	} else {
1621 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1622 
1623 		if (iph->nexthdr == IPPROTO_UDP)
1624 			uh = (struct udphdr *)(iph + 1);
1625 	}
1626 	if (uh) {
1627 		if (uh->check)
1628 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1629 		else
1630 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1631 	}
1632 }
1633 #endif
1634 
1635 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1636 					   int payload_off, int tcp_ts,
1637 					   struct sk_buff *skb)
1638 {
1639 #ifdef CONFIG_INET
1640 	struct tcphdr *th;
1641 	int len, nw_off;
1642 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1643 	u32 hdr_info = tpa_info->hdr_info;
1644 	bool loopback = false;
1645 
1646 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1647 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1648 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1649 
1650 	/* If the packet is an internal loopback packet, the offsets will
1651 	 * have an extra 4 bytes.
1652 	 */
1653 	if (inner_mac_off == 4) {
1654 		loopback = true;
1655 	} else if (inner_mac_off > 4) {
1656 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1657 					    ETH_HLEN - 2));
1658 
1659 		/* We only support inner iPv4/ipv6.  If we don't see the
1660 		 * correct protocol ID, it must be a loopback packet where
1661 		 * the offsets are off by 4.
1662 		 */
1663 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1664 			loopback = true;
1665 	}
1666 	if (loopback) {
1667 		/* internal loopback packet, subtract all offsets by 4 */
1668 		inner_ip_off -= 4;
1669 		inner_mac_off -= 4;
1670 		outer_ip_off -= 4;
1671 	}
1672 
1673 	nw_off = inner_ip_off - ETH_HLEN;
1674 	skb_set_network_header(skb, nw_off);
1675 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1676 		struct ipv6hdr *iph = ipv6_hdr(skb);
1677 
1678 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1679 		len = skb->len - skb_transport_offset(skb);
1680 		th = tcp_hdr(skb);
1681 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1682 	} else {
1683 		struct iphdr *iph = ip_hdr(skb);
1684 
1685 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1686 		len = skb->len - skb_transport_offset(skb);
1687 		th = tcp_hdr(skb);
1688 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1689 	}
1690 
1691 	if (inner_mac_off) { /* tunnel */
1692 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1693 					    ETH_HLEN - 2));
1694 
1695 		bnxt_gro_tunnel(skb, proto);
1696 	}
1697 #endif
1698 	return skb;
1699 }
1700 
1701 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1702 					   int payload_off, int tcp_ts,
1703 					   struct sk_buff *skb)
1704 {
1705 #ifdef CONFIG_INET
1706 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1707 	u32 hdr_info = tpa_info->hdr_info;
1708 	int iphdr_len, nw_off;
1709 
1710 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1711 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1712 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1713 
1714 	nw_off = inner_ip_off - ETH_HLEN;
1715 	skb_set_network_header(skb, nw_off);
1716 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1717 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1718 	skb_set_transport_header(skb, nw_off + iphdr_len);
1719 
1720 	if (inner_mac_off) { /* tunnel */
1721 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1722 					    ETH_HLEN - 2));
1723 
1724 		bnxt_gro_tunnel(skb, proto);
1725 	}
1726 #endif
1727 	return skb;
1728 }
1729 
1730 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1731 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1732 
1733 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1734 					   int payload_off, int tcp_ts,
1735 					   struct sk_buff *skb)
1736 {
1737 #ifdef CONFIG_INET
1738 	struct tcphdr *th;
1739 	int len, nw_off, tcp_opt_len = 0;
1740 
1741 	if (tcp_ts)
1742 		tcp_opt_len = 12;
1743 
1744 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1745 		struct iphdr *iph;
1746 
1747 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1748 			 ETH_HLEN;
1749 		skb_set_network_header(skb, nw_off);
1750 		iph = ip_hdr(skb);
1751 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1752 		len = skb->len - skb_transport_offset(skb);
1753 		th = tcp_hdr(skb);
1754 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1755 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1756 		struct ipv6hdr *iph;
1757 
1758 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1759 			 ETH_HLEN;
1760 		skb_set_network_header(skb, nw_off);
1761 		iph = ipv6_hdr(skb);
1762 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1763 		len = skb->len - skb_transport_offset(skb);
1764 		th = tcp_hdr(skb);
1765 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1766 	} else {
1767 		dev_kfree_skb_any(skb);
1768 		return NULL;
1769 	}
1770 
1771 	if (nw_off) /* tunnel */
1772 		bnxt_gro_tunnel(skb, skb->protocol);
1773 #endif
1774 	return skb;
1775 }
1776 
1777 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1778 					   struct bnxt_tpa_info *tpa_info,
1779 					   struct rx_tpa_end_cmp *tpa_end,
1780 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1781 					   struct sk_buff *skb)
1782 {
1783 #ifdef CONFIG_INET
1784 	int payload_off;
1785 	u16 segs;
1786 
1787 	segs = TPA_END_TPA_SEGS(tpa_end);
1788 	if (segs == 1)
1789 		return skb;
1790 
1791 	NAPI_GRO_CB(skb)->count = segs;
1792 	skb_shinfo(skb)->gso_size =
1793 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1794 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1795 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1796 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1797 	else
1798 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1799 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1800 	if (likely(skb))
1801 		tcp_gro_complete(skb);
1802 #endif
1803 	return skb;
1804 }
1805 
1806 /* Given the cfa_code of a received packet determine which
1807  * netdev (vf-rep or PF) the packet is destined to.
1808  */
1809 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1810 {
1811 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1812 
1813 	/* if vf-rep dev is NULL, the must belongs to the PF */
1814 	return dev ? dev : bp->dev;
1815 }
1816 
1817 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1818 					   struct bnxt_cp_ring_info *cpr,
1819 					   u32 *raw_cons,
1820 					   struct rx_tpa_end_cmp *tpa_end,
1821 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1822 					   u8 *event)
1823 {
1824 	struct bnxt_napi *bnapi = cpr->bnapi;
1825 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1826 	struct net_device *dev = bp->dev;
1827 	u8 *data_ptr, agg_bufs;
1828 	unsigned int len;
1829 	struct bnxt_tpa_info *tpa_info;
1830 	dma_addr_t mapping;
1831 	struct sk_buff *skb;
1832 	u16 idx = 0, agg_id;
1833 	void *data;
1834 	bool gro;
1835 
1836 	if (unlikely(bnapi->in_reset)) {
1837 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1838 
1839 		if (rc < 0)
1840 			return ERR_PTR(-EBUSY);
1841 		return NULL;
1842 	}
1843 
1844 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1845 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1846 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1847 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1848 		tpa_info = &rxr->rx_tpa[agg_id];
1849 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1850 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1851 				    agg_bufs, tpa_info->agg_count);
1852 			agg_bufs = tpa_info->agg_count;
1853 		}
1854 		tpa_info->agg_count = 0;
1855 		*event |= BNXT_AGG_EVENT;
1856 		bnxt_free_agg_idx(rxr, agg_id);
1857 		idx = agg_id;
1858 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1859 	} else {
1860 		agg_id = TPA_END_AGG_ID(tpa_end);
1861 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1862 		tpa_info = &rxr->rx_tpa[agg_id];
1863 		idx = RING_CMP(*raw_cons);
1864 		if (agg_bufs) {
1865 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1866 				return ERR_PTR(-EBUSY);
1867 
1868 			*event |= BNXT_AGG_EVENT;
1869 			idx = NEXT_CMP(idx);
1870 		}
1871 		gro = !!TPA_END_GRO(tpa_end);
1872 	}
1873 	data = tpa_info->data;
1874 	data_ptr = tpa_info->data_ptr;
1875 	prefetch(data_ptr);
1876 	len = tpa_info->len;
1877 	mapping = tpa_info->mapping;
1878 
1879 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1880 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1881 		if (agg_bufs > MAX_SKB_FRAGS)
1882 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1883 				    agg_bufs, (int)MAX_SKB_FRAGS);
1884 		return NULL;
1885 	}
1886 
1887 	if (len <= bp->rx_copybreak) {
1888 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1889 		if (!skb) {
1890 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1891 			cpr->sw_stats->rx.rx_oom_discards += 1;
1892 			return NULL;
1893 		}
1894 	} else {
1895 		u8 *new_data;
1896 		dma_addr_t new_mapping;
1897 
1898 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1899 						GFP_ATOMIC);
1900 		if (!new_data) {
1901 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1902 			cpr->sw_stats->rx.rx_oom_discards += 1;
1903 			return NULL;
1904 		}
1905 
1906 		tpa_info->data = new_data;
1907 		tpa_info->data_ptr = new_data + bp->rx_offset;
1908 		tpa_info->mapping = new_mapping;
1909 
1910 		skb = napi_build_skb(data, bp->rx_buf_size);
1911 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1912 					bp->rx_buf_use_size, bp->rx_dir);
1913 
1914 		if (!skb) {
1915 			page_pool_free_va(rxr->head_pool, data, true);
1916 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1917 			cpr->sw_stats->rx.rx_oom_discards += 1;
1918 			return NULL;
1919 		}
1920 		skb_mark_for_recycle(skb);
1921 		skb_reserve(skb, bp->rx_offset);
1922 		skb_put(skb, len);
1923 	}
1924 
1925 	if (agg_bufs) {
1926 		skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, idx, agg_bufs,
1927 					      true);
1928 		if (!skb) {
1929 			/* Page reuse already handled by bnxt_rx_pages(). */
1930 			cpr->sw_stats->rx.rx_oom_discards += 1;
1931 			return NULL;
1932 		}
1933 	}
1934 
1935 	if (tpa_info->cfa_code_valid)
1936 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1937 	skb->protocol = eth_type_trans(skb, dev);
1938 
1939 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1940 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1941 
1942 	if (tpa_info->vlan_valid &&
1943 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1944 		__be16 vlan_proto = htons(tpa_info->metadata >>
1945 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1946 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1947 
1948 		if (eth_type_vlan(vlan_proto)) {
1949 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1950 		} else {
1951 			dev_kfree_skb(skb);
1952 			return NULL;
1953 		}
1954 	}
1955 
1956 	skb_checksum_none_assert(skb);
1957 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1958 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1959 		skb->csum_level =
1960 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1961 	}
1962 
1963 	if (gro)
1964 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1965 
1966 	return skb;
1967 }
1968 
1969 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1970 			 struct rx_agg_cmp *rx_agg)
1971 {
1972 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1973 	struct bnxt_tpa_info *tpa_info;
1974 
1975 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1976 	tpa_info = &rxr->rx_tpa[agg_id];
1977 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1978 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1979 }
1980 
1981 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1982 			     struct sk_buff *skb)
1983 {
1984 	skb_mark_for_recycle(skb);
1985 
1986 	if (skb->dev != bp->dev) {
1987 		/* this packet belongs to a vf-rep */
1988 		bnxt_vf_rep_rx(bp, skb);
1989 		return;
1990 	}
1991 	skb_record_rx_queue(skb, bnapi->index);
1992 	napi_gro_receive(&bnapi->napi, skb);
1993 }
1994 
1995 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1996 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1997 {
1998 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1999 
2000 	if (BNXT_PTP_RX_TS_VALID(flags))
2001 		goto ts_valid;
2002 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
2003 		return false;
2004 
2005 ts_valid:
2006 	*cmpl_ts = ts;
2007 	return true;
2008 }
2009 
2010 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
2011 				    struct rx_cmp *rxcmp,
2012 				    struct rx_cmp_ext *rxcmp1)
2013 {
2014 	__be16 vlan_proto;
2015 	u16 vtag;
2016 
2017 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
2018 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
2019 		u32 meta_data;
2020 
2021 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
2022 			return skb;
2023 
2024 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
2025 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
2026 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
2027 		if (eth_type_vlan(vlan_proto))
2028 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2029 		else
2030 			goto vlan_err;
2031 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2032 		if (RX_CMP_VLAN_VALID(rxcmp)) {
2033 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
2034 
2035 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
2036 				vlan_proto = htons(ETH_P_8021Q);
2037 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
2038 				vlan_proto = htons(ETH_P_8021AD);
2039 			else
2040 				goto vlan_err;
2041 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
2042 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2043 		}
2044 	}
2045 	return skb;
2046 vlan_err:
2047 	skb_mark_for_recycle(skb);
2048 	dev_kfree_skb(skb);
2049 	return NULL;
2050 }
2051 
2052 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2053 					   struct rx_cmp *rxcmp)
2054 {
2055 	u8 ext_op;
2056 
2057 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2058 	switch (ext_op) {
2059 	case EXT_OP_INNER_4:
2060 	case EXT_OP_OUTER_4:
2061 	case EXT_OP_INNFL_3:
2062 	case EXT_OP_OUTFL_3:
2063 		return PKT_HASH_TYPE_L4;
2064 	default:
2065 		return PKT_HASH_TYPE_L3;
2066 	}
2067 }
2068 
2069 /* returns the following:
2070  * 1       - 1 packet successfully received
2071  * 0       - successful TPA_START, packet not completed yet
2072  * -EBUSY  - completion ring does not have all the agg buffers yet
2073  * -ENOMEM - packet aborted due to out of memory
2074  * -EIO    - packet aborted due to hw error indicated in BD
2075  */
2076 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2077 		       u32 *raw_cons, u8 *event)
2078 {
2079 	struct bnxt_napi *bnapi = cpr->bnapi;
2080 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2081 	struct net_device *dev = bp->dev;
2082 	struct rx_cmp *rxcmp;
2083 	struct rx_cmp_ext *rxcmp1;
2084 	u32 tmp_raw_cons = *raw_cons;
2085 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2086 	struct skb_shared_info *sinfo;
2087 	struct bnxt_sw_rx_bd *rx_buf;
2088 	unsigned int len;
2089 	u8 *data_ptr, agg_bufs, cmp_type;
2090 	bool xdp_active = false;
2091 	dma_addr_t dma_addr;
2092 	struct sk_buff *skb;
2093 	struct xdp_buff xdp;
2094 	u32 flags, misc;
2095 	u32 cmpl_ts;
2096 	void *data;
2097 	int rc = 0;
2098 
2099 	rxcmp = (struct rx_cmp *)
2100 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2101 
2102 	cmp_type = RX_CMP_TYPE(rxcmp);
2103 
2104 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2105 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2106 		goto next_rx_no_prod_no_len;
2107 	}
2108 
2109 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2110 	cp_cons = RING_CMP(tmp_raw_cons);
2111 	rxcmp1 = (struct rx_cmp_ext *)
2112 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2113 
2114 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2115 		return -EBUSY;
2116 
2117 	/* The valid test of the entry must be done first before
2118 	 * reading any further.
2119 	 */
2120 	dma_rmb();
2121 	prod = rxr->rx_prod;
2122 
2123 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2124 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2125 		bnxt_tpa_start(bp, rxr, cmp_type,
2126 			       (struct rx_tpa_start_cmp *)rxcmp,
2127 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2128 
2129 		*event |= BNXT_RX_EVENT;
2130 		goto next_rx_no_prod_no_len;
2131 
2132 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2133 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2134 				   (struct rx_tpa_end_cmp *)rxcmp,
2135 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2136 
2137 		if (IS_ERR(skb))
2138 			return -EBUSY;
2139 
2140 		rc = -ENOMEM;
2141 		if (likely(skb)) {
2142 			bnxt_deliver_skb(bp, bnapi, skb);
2143 			rc = 1;
2144 		}
2145 		*event |= BNXT_RX_EVENT;
2146 		goto next_rx_no_prod_no_len;
2147 	}
2148 
2149 	cons = rxcmp->rx_cmp_opaque;
2150 	if (unlikely(cons != rxr->rx_next_cons)) {
2151 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2152 
2153 		/* 0xffff is forced error, don't print it */
2154 		if (rxr->rx_next_cons != 0xffff)
2155 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2156 				    cons, rxr->rx_next_cons);
2157 		bnxt_sched_reset_rxr(bp, rxr);
2158 		if (rc1)
2159 			return rc1;
2160 		goto next_rx_no_prod_no_len;
2161 	}
2162 	rx_buf = &rxr->rx_buf_ring[cons];
2163 	data = rx_buf->data;
2164 	data_ptr = rx_buf->data_ptr;
2165 	prefetch(data_ptr);
2166 
2167 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2168 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2169 
2170 	if (agg_bufs) {
2171 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2172 			return -EBUSY;
2173 
2174 		cp_cons = NEXT_CMP(cp_cons);
2175 		*event |= BNXT_AGG_EVENT;
2176 	}
2177 	*event |= BNXT_RX_EVENT;
2178 
2179 	rx_buf->data = NULL;
2180 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2181 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2182 
2183 		bnxt_reuse_rx_data(rxr, cons, data);
2184 		if (agg_bufs)
2185 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2186 					       false);
2187 
2188 		rc = -EIO;
2189 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2190 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2191 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2192 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2193 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2194 						 rx_err);
2195 				bnxt_sched_reset_rxr(bp, rxr);
2196 			}
2197 		}
2198 		goto next_rx_no_len;
2199 	}
2200 
2201 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2202 	len = flags >> RX_CMP_LEN_SHIFT;
2203 	dma_addr = rx_buf->mapping;
2204 
2205 	if (bnxt_xdp_attached(bp, rxr)) {
2206 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2207 		if (agg_bufs) {
2208 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp,
2209 							       cp_cons,
2210 							       agg_bufs,
2211 							       false);
2212 			if (!frag_len)
2213 				goto oom_next_rx;
2214 
2215 		}
2216 		xdp_active = true;
2217 	}
2218 
2219 	if (xdp_active) {
2220 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2221 			rc = 1;
2222 			goto next_rx;
2223 		}
2224 		if (xdp_buff_has_frags(&xdp)) {
2225 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2226 			agg_bufs = sinfo->nr_frags;
2227 		} else {
2228 			agg_bufs = 0;
2229 		}
2230 	}
2231 
2232 	if (len <= bp->rx_copybreak) {
2233 		if (!xdp_active)
2234 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2235 		else
2236 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2237 		bnxt_reuse_rx_data(rxr, cons, data);
2238 		if (!skb) {
2239 			if (agg_bufs) {
2240 				if (!xdp_active)
2241 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2242 							       agg_bufs, false);
2243 				else
2244 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2245 			}
2246 			goto oom_next_rx;
2247 		}
2248 	} else {
2249 		u32 payload;
2250 
2251 		if (rx_buf->data_ptr == data_ptr)
2252 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2253 		else
2254 			payload = 0;
2255 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2256 				      payload | len);
2257 		if (!skb)
2258 			goto oom_next_rx;
2259 	}
2260 
2261 	if (agg_bufs) {
2262 		if (!xdp_active) {
2263 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2264 						      agg_bufs, false);
2265 			if (!skb)
2266 				goto oom_next_rx;
2267 		} else {
2268 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2269 						 rxr->page_pool, &xdp);
2270 			if (!skb) {
2271 				/* we should be able to free the old skb here */
2272 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2273 				goto oom_next_rx;
2274 			}
2275 		}
2276 	}
2277 
2278 	if (RX_CMP_HASH_VALID(rxcmp)) {
2279 		enum pkt_hash_types type;
2280 
2281 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2282 			type = bnxt_rss_ext_op(bp, rxcmp);
2283 		} else {
2284 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2285 
2286 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2287 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2288 				type = PKT_HASH_TYPE_L4;
2289 			else
2290 				type = PKT_HASH_TYPE_L3;
2291 		}
2292 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2293 	}
2294 
2295 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2296 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2297 	skb->protocol = eth_type_trans(skb, dev);
2298 
2299 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2300 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2301 		if (!skb)
2302 			goto next_rx;
2303 	}
2304 
2305 	skb_checksum_none_assert(skb);
2306 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2307 		if (dev->features & NETIF_F_RXCSUM) {
2308 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2309 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2310 		}
2311 	} else {
2312 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2313 			if (dev->features & NETIF_F_RXCSUM)
2314 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2315 		}
2316 	}
2317 
2318 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2319 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2320 			u64 ns, ts;
2321 
2322 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2323 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2324 
2325 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2326 				memset(skb_hwtstamps(skb), 0,
2327 				       sizeof(*skb_hwtstamps(skb)));
2328 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2329 			}
2330 		}
2331 	}
2332 	bnxt_deliver_skb(bp, bnapi, skb);
2333 	rc = 1;
2334 
2335 next_rx:
2336 	cpr->rx_packets += 1;
2337 	cpr->rx_bytes += len;
2338 
2339 next_rx_no_len:
2340 	rxr->rx_prod = NEXT_RX(prod);
2341 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2342 
2343 next_rx_no_prod_no_len:
2344 	*raw_cons = tmp_raw_cons;
2345 
2346 	return rc;
2347 
2348 oom_next_rx:
2349 	cpr->sw_stats->rx.rx_oom_discards += 1;
2350 	rc = -ENOMEM;
2351 	goto next_rx;
2352 }
2353 
2354 /* In netpoll mode, if we are using a combined completion ring, we need to
2355  * discard the rx packets and recycle the buffers.
2356  */
2357 static int bnxt_force_rx_discard(struct bnxt *bp,
2358 				 struct bnxt_cp_ring_info *cpr,
2359 				 u32 *raw_cons, u8 *event)
2360 {
2361 	u32 tmp_raw_cons = *raw_cons;
2362 	struct rx_cmp_ext *rxcmp1;
2363 	struct rx_cmp *rxcmp;
2364 	u16 cp_cons;
2365 	u8 cmp_type;
2366 	int rc;
2367 
2368 	cp_cons = RING_CMP(tmp_raw_cons);
2369 	rxcmp = (struct rx_cmp *)
2370 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2371 
2372 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2373 	cp_cons = RING_CMP(tmp_raw_cons);
2374 	rxcmp1 = (struct rx_cmp_ext *)
2375 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2376 
2377 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2378 		return -EBUSY;
2379 
2380 	/* The valid test of the entry must be done first before
2381 	 * reading any further.
2382 	 */
2383 	dma_rmb();
2384 	cmp_type = RX_CMP_TYPE(rxcmp);
2385 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2386 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2387 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2388 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2389 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2390 		struct rx_tpa_end_cmp_ext *tpa_end1;
2391 
2392 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2393 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2394 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2395 	}
2396 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2397 	if (rc && rc != -EBUSY)
2398 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2399 	return rc;
2400 }
2401 
2402 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2403 {
2404 	struct bnxt_fw_health *fw_health = bp->fw_health;
2405 	u32 reg = fw_health->regs[reg_idx];
2406 	u32 reg_type, reg_off, val = 0;
2407 
2408 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2409 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2410 	switch (reg_type) {
2411 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2412 		pci_read_config_dword(bp->pdev, reg_off, &val);
2413 		break;
2414 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2415 		reg_off = fw_health->mapped_regs[reg_idx];
2416 		fallthrough;
2417 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2418 		val = readl(bp->bar0 + reg_off);
2419 		break;
2420 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2421 		val = readl(bp->bar1 + reg_off);
2422 		break;
2423 	}
2424 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2425 		val &= fw_health->fw_reset_inprog_reg_mask;
2426 	return val;
2427 }
2428 
2429 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2430 {
2431 	int i;
2432 
2433 	for (i = 0; i < bp->rx_nr_rings; i++) {
2434 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2435 		struct bnxt_ring_grp_info *grp_info;
2436 
2437 		grp_info = &bp->grp_info[grp_idx];
2438 		if (grp_info->agg_fw_ring_id == ring_id)
2439 			return grp_idx;
2440 	}
2441 	return INVALID_HW_RING_ID;
2442 }
2443 
2444 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2445 {
2446 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2447 
2448 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2449 		return link_info->force_link_speed2;
2450 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2451 		return link_info->force_pam4_link_speed;
2452 	return link_info->force_link_speed;
2453 }
2454 
2455 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2456 {
2457 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2458 
2459 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2460 		link_info->req_link_speed = link_info->force_link_speed2;
2461 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2462 		switch (link_info->req_link_speed) {
2463 		case BNXT_LINK_SPEED_50GB_PAM4:
2464 		case BNXT_LINK_SPEED_100GB_PAM4:
2465 		case BNXT_LINK_SPEED_200GB_PAM4:
2466 		case BNXT_LINK_SPEED_400GB_PAM4:
2467 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2468 			break;
2469 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2470 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2471 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2472 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2473 			break;
2474 		default:
2475 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2476 		}
2477 		return;
2478 	}
2479 	link_info->req_link_speed = link_info->force_link_speed;
2480 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2481 	if (link_info->force_pam4_link_speed) {
2482 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2483 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2484 	}
2485 }
2486 
2487 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2488 {
2489 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2490 
2491 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2492 		link_info->advertising = link_info->auto_link_speeds2;
2493 		return;
2494 	}
2495 	link_info->advertising = link_info->auto_link_speeds;
2496 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2497 }
2498 
2499 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2500 {
2501 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2502 
2503 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2504 		if (link_info->req_link_speed != link_info->force_link_speed2)
2505 			return true;
2506 		return false;
2507 	}
2508 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2509 	    link_info->req_link_speed != link_info->force_link_speed)
2510 		return true;
2511 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2512 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2513 		return true;
2514 	return false;
2515 }
2516 
2517 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2518 {
2519 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2520 
2521 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2522 		if (link_info->advertising != link_info->auto_link_speeds2)
2523 			return true;
2524 		return false;
2525 	}
2526 	if (link_info->advertising != link_info->auto_link_speeds ||
2527 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2528 		return true;
2529 	return false;
2530 }
2531 
2532 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2533 {
2534 	u32 flags = bp->ctx->ctx_arr[type].flags;
2535 
2536 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2537 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2538 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2539 }
2540 
2541 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2542 {
2543 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2544 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2545 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2546 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2547 	struct bnxt_bs_trace_info *bs_trace;
2548 	int last_pg;
2549 
2550 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2551 		return;
2552 
2553 	mem_size = ctxm->max_entries * ctxm->entry_size;
2554 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2555 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2556 
2557 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2558 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2559 
2560 	rmem = &ctx_pg[0].ring_mem;
2561 	bs_trace = &bp->bs_trace[trace_type];
2562 	bs_trace->ctx_type = ctxm->type;
2563 	bs_trace->trace_type = trace_type;
2564 	if (pages > MAX_CTX_PAGES) {
2565 		int last_pg_dir = rmem->nr_pages - 1;
2566 
2567 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2568 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2569 	} else {
2570 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2571 	}
2572 	bs_trace->magic_byte += magic_byte_offset;
2573 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2574 }
2575 
2576 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2577 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2578 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2579 
2580 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2581 	(((data2) &							\
2582 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2583 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2584 
2585 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2586 	((data2) &							\
2587 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2588 
2589 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2590 	(((data2) &							\
2591 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2592 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2593 
2594 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2595 	((data1) &							\
2596 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2597 
2598 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2599 	(((data1) &							\
2600 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2601 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2602 
2603 /* Return true if the workqueue has to be scheduled */
2604 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2605 {
2606 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2607 
2608 	switch (err_type) {
2609 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2610 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2611 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2612 		break;
2613 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2614 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2615 		break;
2616 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2617 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2618 		break;
2619 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2620 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2621 		char *threshold_type;
2622 		bool notify = false;
2623 		char *dir_str;
2624 
2625 		switch (type) {
2626 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2627 			threshold_type = "warning";
2628 			break;
2629 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2630 			threshold_type = "critical";
2631 			break;
2632 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2633 			threshold_type = "fatal";
2634 			break;
2635 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2636 			threshold_type = "shutdown";
2637 			break;
2638 		default:
2639 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2640 			return false;
2641 		}
2642 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2643 			dir_str = "above";
2644 			notify = true;
2645 		} else {
2646 			dir_str = "below";
2647 		}
2648 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2649 			    dir_str, threshold_type);
2650 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2651 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2652 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2653 		if (notify) {
2654 			bp->thermal_threshold_type = type;
2655 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2656 			return true;
2657 		}
2658 		return false;
2659 	}
2660 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2661 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2662 		break;
2663 	default:
2664 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2665 			   err_type);
2666 		break;
2667 	}
2668 	return false;
2669 }
2670 
2671 #define BNXT_GET_EVENT_PORT(data)	\
2672 	((data) &			\
2673 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2674 
2675 #define BNXT_EVENT_RING_TYPE(data2)	\
2676 	((data2) &			\
2677 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2678 
2679 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2680 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2681 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2682 
2683 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2684 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2685 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2686 
2687 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2688 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2689 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2690 
2691 #define BNXT_PHC_BITS	48
2692 
2693 static int bnxt_async_event_process(struct bnxt *bp,
2694 				    struct hwrm_async_event_cmpl *cmpl)
2695 {
2696 	u16 event_id = le16_to_cpu(cmpl->event_id);
2697 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2698 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2699 
2700 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2701 		   event_id, data1, data2);
2702 
2703 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2704 	switch (event_id) {
2705 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2706 		struct bnxt_link_info *link_info = &bp->link_info;
2707 
2708 		if (BNXT_VF(bp))
2709 			goto async_event_process_exit;
2710 
2711 		/* print unsupported speed warning in forced speed mode only */
2712 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2713 		    (data1 & 0x20000)) {
2714 			u16 fw_speed = bnxt_get_force_speed(link_info);
2715 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2716 
2717 			if (speed != SPEED_UNKNOWN)
2718 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2719 					    speed);
2720 		}
2721 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2722 	}
2723 		fallthrough;
2724 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2725 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2726 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2727 		fallthrough;
2728 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2729 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2730 		break;
2731 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2732 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2733 		break;
2734 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2735 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2736 
2737 		if (BNXT_VF(bp))
2738 			break;
2739 
2740 		if (bp->pf.port_id != port_id)
2741 			break;
2742 
2743 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2744 		break;
2745 	}
2746 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2747 		if (BNXT_PF(bp))
2748 			goto async_event_process_exit;
2749 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2750 		break;
2751 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2752 		char *type_str = "Solicited";
2753 
2754 		if (!bp->fw_health)
2755 			goto async_event_process_exit;
2756 
2757 		bp->fw_reset_timestamp = jiffies;
2758 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2759 		if (!bp->fw_reset_min_dsecs)
2760 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2761 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2762 		if (!bp->fw_reset_max_dsecs)
2763 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2764 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2765 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2766 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2767 			type_str = "Fatal";
2768 			bp->fw_health->fatalities++;
2769 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2770 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2771 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2772 			type_str = "Non-fatal";
2773 			bp->fw_health->survivals++;
2774 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2775 		}
2776 		netif_warn(bp, hw, bp->dev,
2777 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2778 			   type_str, data1, data2,
2779 			   bp->fw_reset_min_dsecs * 100,
2780 			   bp->fw_reset_max_dsecs * 100);
2781 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2782 		break;
2783 	}
2784 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2785 		struct bnxt_fw_health *fw_health = bp->fw_health;
2786 		char *status_desc = "healthy";
2787 		u32 status;
2788 
2789 		if (!fw_health)
2790 			goto async_event_process_exit;
2791 
2792 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2793 			fw_health->enabled = false;
2794 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2795 			break;
2796 		}
2797 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2798 		fw_health->tmr_multiplier =
2799 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2800 				     bp->current_interval * 10);
2801 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2802 		if (!fw_health->enabled)
2803 			fw_health->last_fw_heartbeat =
2804 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2805 		fw_health->last_fw_reset_cnt =
2806 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2807 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2808 		if (status != BNXT_FW_STATUS_HEALTHY)
2809 			status_desc = "unhealthy";
2810 		netif_info(bp, drv, bp->dev,
2811 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2812 			   fw_health->primary ? "primary" : "backup", status,
2813 			   status_desc, fw_health->last_fw_reset_cnt);
2814 		if (!fw_health->enabled) {
2815 			/* Make sure tmr_counter is set and visible to
2816 			 * bnxt_health_check() before setting enabled to true.
2817 			 */
2818 			smp_wmb();
2819 			fw_health->enabled = true;
2820 		}
2821 		goto async_event_process_exit;
2822 	}
2823 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2824 		netif_notice(bp, hw, bp->dev,
2825 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2826 			     data1, data2);
2827 		goto async_event_process_exit;
2828 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2829 		struct bnxt_rx_ring_info *rxr;
2830 		u16 grp_idx;
2831 
2832 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2833 			goto async_event_process_exit;
2834 
2835 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2836 			    BNXT_EVENT_RING_TYPE(data2), data1);
2837 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2838 			goto async_event_process_exit;
2839 
2840 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2841 		if (grp_idx == INVALID_HW_RING_ID) {
2842 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2843 				    data1);
2844 			goto async_event_process_exit;
2845 		}
2846 		rxr = bp->bnapi[grp_idx]->rx_ring;
2847 		bnxt_sched_reset_rxr(bp, rxr);
2848 		goto async_event_process_exit;
2849 	}
2850 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2851 		struct bnxt_fw_health *fw_health = bp->fw_health;
2852 
2853 		netif_notice(bp, hw, bp->dev,
2854 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2855 			     data1, data2);
2856 		if (fw_health) {
2857 			fw_health->echo_req_data1 = data1;
2858 			fw_health->echo_req_data2 = data2;
2859 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2860 			break;
2861 		}
2862 		goto async_event_process_exit;
2863 	}
2864 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2865 		bnxt_ptp_pps_event(bp, data1, data2);
2866 		goto async_event_process_exit;
2867 	}
2868 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2869 		if (bnxt_event_error_report(bp, data1, data2))
2870 			break;
2871 		goto async_event_process_exit;
2872 	}
2873 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2874 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2875 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2876 			if (BNXT_PTP_USE_RTC(bp)) {
2877 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2878 				unsigned long flags;
2879 				u64 ns;
2880 
2881 				if (!ptp)
2882 					goto async_event_process_exit;
2883 
2884 				bnxt_ptp_update_current_time(bp);
2885 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2886 				       BNXT_PHC_BITS) | ptp->current_time);
2887 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2888 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2889 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2890 			}
2891 			break;
2892 		}
2893 		goto async_event_process_exit;
2894 	}
2895 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2896 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2897 
2898 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2899 		goto async_event_process_exit;
2900 	}
2901 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2902 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2903 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2904 
2905 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2906 		goto async_event_process_exit;
2907 	}
2908 	default:
2909 		goto async_event_process_exit;
2910 	}
2911 	__bnxt_queue_sp_work(bp);
2912 async_event_process_exit:
2913 	bnxt_ulp_async_events(bp, cmpl);
2914 	return 0;
2915 }
2916 
2917 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2918 {
2919 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2920 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2921 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2922 				(struct hwrm_fwd_req_cmpl *)txcmp;
2923 
2924 	switch (cmpl_type) {
2925 	case CMPL_BASE_TYPE_HWRM_DONE:
2926 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2927 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2928 		break;
2929 
2930 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2931 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2932 
2933 		if ((vf_id < bp->pf.first_vf_id) ||
2934 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2935 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2936 				   vf_id);
2937 			return -EINVAL;
2938 		}
2939 
2940 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2941 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2942 		break;
2943 
2944 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2945 		bnxt_async_event_process(bp,
2946 					 (struct hwrm_async_event_cmpl *)txcmp);
2947 		break;
2948 
2949 	default:
2950 		break;
2951 	}
2952 
2953 	return 0;
2954 }
2955 
2956 static bool bnxt_vnic_is_active(struct bnxt *bp)
2957 {
2958 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2959 
2960 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2961 }
2962 
2963 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2964 {
2965 	struct bnxt_napi *bnapi = dev_instance;
2966 	struct bnxt *bp = bnapi->bp;
2967 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2968 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2969 
2970 	cpr->event_ctr++;
2971 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2972 	napi_schedule(&bnapi->napi);
2973 	return IRQ_HANDLED;
2974 }
2975 
2976 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2977 {
2978 	u32 raw_cons = cpr->cp_raw_cons;
2979 	u16 cons = RING_CMP(raw_cons);
2980 	struct tx_cmp *txcmp;
2981 
2982 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2983 
2984 	return TX_CMP_VALID(txcmp, raw_cons);
2985 }
2986 
2987 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2988 			    int budget)
2989 {
2990 	struct bnxt_napi *bnapi = cpr->bnapi;
2991 	u32 raw_cons = cpr->cp_raw_cons;
2992 	u32 cons;
2993 	int rx_pkts = 0;
2994 	u8 event = 0;
2995 	struct tx_cmp *txcmp;
2996 
2997 	cpr->has_more_work = 0;
2998 	cpr->had_work_done = 1;
2999 	while (1) {
3000 		u8 cmp_type;
3001 		int rc;
3002 
3003 		cons = RING_CMP(raw_cons);
3004 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3005 
3006 		if (!TX_CMP_VALID(txcmp, raw_cons))
3007 			break;
3008 
3009 		/* The valid test of the entry must be done first before
3010 		 * reading any further.
3011 		 */
3012 		dma_rmb();
3013 		cmp_type = TX_CMP_TYPE(txcmp);
3014 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3015 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3016 			u32 opaque = txcmp->tx_cmp_opaque;
3017 			struct bnxt_tx_ring_info *txr;
3018 			u16 tx_freed;
3019 
3020 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3021 			event |= BNXT_TX_CMP_EVENT;
3022 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3023 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3024 			else
3025 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3026 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3027 				   bp->tx_ring_mask;
3028 			/* return full budget so NAPI will complete. */
3029 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3030 				rx_pkts = budget;
3031 				raw_cons = NEXT_RAW_CMP(raw_cons);
3032 				if (budget)
3033 					cpr->has_more_work = 1;
3034 				break;
3035 			}
3036 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3037 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3038 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3039 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3040 			if (likely(budget))
3041 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3042 			else
3043 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3044 							   &event);
3045 			if (likely(rc >= 0))
3046 				rx_pkts += rc;
3047 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3048 			 * the NAPI budget.  Otherwise, we may potentially loop
3049 			 * here forever if we consistently cannot allocate
3050 			 * buffers.
3051 			 */
3052 			else if (rc == -ENOMEM && budget)
3053 				rx_pkts++;
3054 			else if (rc == -EBUSY)	/* partial completion */
3055 				break;
3056 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3057 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3058 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3059 			bnxt_hwrm_handler(bp, txcmp);
3060 		}
3061 		raw_cons = NEXT_RAW_CMP(raw_cons);
3062 
3063 		if (rx_pkts && rx_pkts == budget) {
3064 			cpr->has_more_work = 1;
3065 			break;
3066 		}
3067 	}
3068 
3069 	if (event & BNXT_REDIRECT_EVENT) {
3070 		xdp_do_flush();
3071 		event &= ~BNXT_REDIRECT_EVENT;
3072 	}
3073 
3074 	if (event & BNXT_TX_EVENT) {
3075 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3076 		u16 prod = txr->tx_prod;
3077 
3078 		/* Sync BD data before updating doorbell */
3079 		wmb();
3080 
3081 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3082 		event &= ~BNXT_TX_EVENT;
3083 	}
3084 
3085 	cpr->cp_raw_cons = raw_cons;
3086 	bnapi->events |= event;
3087 	return rx_pkts;
3088 }
3089 
3090 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3091 				  int budget)
3092 {
3093 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3094 		bnapi->tx_int(bp, bnapi, budget);
3095 
3096 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3097 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3098 
3099 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3100 		bnapi->events &= ~BNXT_RX_EVENT;
3101 	}
3102 	if (bnapi->events & BNXT_AGG_EVENT) {
3103 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3104 
3105 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3106 		bnapi->events &= ~BNXT_AGG_EVENT;
3107 	}
3108 }
3109 
3110 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3111 			  int budget)
3112 {
3113 	struct bnxt_napi *bnapi = cpr->bnapi;
3114 	int rx_pkts;
3115 
3116 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3117 
3118 	/* ACK completion ring before freeing tx ring and producing new
3119 	 * buffers in rx/agg rings to prevent overflowing the completion
3120 	 * ring.
3121 	 */
3122 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3123 
3124 	__bnxt_poll_work_done(bp, bnapi, budget);
3125 	return rx_pkts;
3126 }
3127 
3128 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3129 {
3130 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3131 	struct bnxt *bp = bnapi->bp;
3132 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3133 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3134 	struct tx_cmp *txcmp;
3135 	struct rx_cmp_ext *rxcmp1;
3136 	u32 cp_cons, tmp_raw_cons;
3137 	u32 raw_cons = cpr->cp_raw_cons;
3138 	bool flush_xdp = false;
3139 	u32 rx_pkts = 0;
3140 	u8 event = 0;
3141 
3142 	while (1) {
3143 		int rc;
3144 
3145 		cp_cons = RING_CMP(raw_cons);
3146 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3147 
3148 		if (!TX_CMP_VALID(txcmp, raw_cons))
3149 			break;
3150 
3151 		/* The valid test of the entry must be done first before
3152 		 * reading any further.
3153 		 */
3154 		dma_rmb();
3155 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3156 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3157 			cp_cons = RING_CMP(tmp_raw_cons);
3158 			rxcmp1 = (struct rx_cmp_ext *)
3159 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3160 
3161 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3162 				break;
3163 
3164 			/* force an error to recycle the buffer */
3165 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3166 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3167 
3168 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3169 			if (likely(rc == -EIO) && budget)
3170 				rx_pkts++;
3171 			else if (rc == -EBUSY)	/* partial completion */
3172 				break;
3173 			if (event & BNXT_REDIRECT_EVENT)
3174 				flush_xdp = true;
3175 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3176 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3177 			bnxt_hwrm_handler(bp, txcmp);
3178 		} else {
3179 			netdev_err(bp->dev,
3180 				   "Invalid completion received on special ring\n");
3181 		}
3182 		raw_cons = NEXT_RAW_CMP(raw_cons);
3183 
3184 		if (rx_pkts == budget)
3185 			break;
3186 	}
3187 
3188 	cpr->cp_raw_cons = raw_cons;
3189 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3190 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3191 
3192 	if (event & BNXT_AGG_EVENT)
3193 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3194 	if (flush_xdp)
3195 		xdp_do_flush();
3196 
3197 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3198 		napi_complete_done(napi, rx_pkts);
3199 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3200 	}
3201 	return rx_pkts;
3202 }
3203 
3204 static int bnxt_poll(struct napi_struct *napi, int budget)
3205 {
3206 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3207 	struct bnxt *bp = bnapi->bp;
3208 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3209 	int work_done = 0;
3210 
3211 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3212 		napi_complete(napi);
3213 		return 0;
3214 	}
3215 	while (1) {
3216 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3217 
3218 		if (work_done >= budget) {
3219 			if (!budget)
3220 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3221 			break;
3222 		}
3223 
3224 		if (!bnxt_has_work(bp, cpr)) {
3225 			if (napi_complete_done(napi, work_done))
3226 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3227 			break;
3228 		}
3229 	}
3230 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3231 		struct dim_sample dim_sample = {};
3232 
3233 		dim_update_sample(cpr->event_ctr,
3234 				  cpr->rx_packets,
3235 				  cpr->rx_bytes,
3236 				  &dim_sample);
3237 		net_dim(&cpr->dim, &dim_sample);
3238 	}
3239 	return work_done;
3240 }
3241 
3242 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3243 {
3244 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3245 	int i, work_done = 0;
3246 
3247 	for (i = 0; i < cpr->cp_ring_count; i++) {
3248 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3249 
3250 		if (cpr2->had_nqe_notify) {
3251 			work_done += __bnxt_poll_work(bp, cpr2,
3252 						      budget - work_done);
3253 			cpr->has_more_work |= cpr2->has_more_work;
3254 		}
3255 	}
3256 	return work_done;
3257 }
3258 
3259 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3260 				 u64 dbr_type, int budget)
3261 {
3262 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3263 	int i;
3264 
3265 	for (i = 0; i < cpr->cp_ring_count; i++) {
3266 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3267 		struct bnxt_db_info *db;
3268 
3269 		if (cpr2->had_work_done) {
3270 			u32 tgl = 0;
3271 
3272 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3273 				cpr2->had_nqe_notify = 0;
3274 				tgl = cpr2->toggle;
3275 			}
3276 			db = &cpr2->cp_db;
3277 			bnxt_writeq(bp,
3278 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3279 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3280 				    db->doorbell);
3281 			cpr2->had_work_done = 0;
3282 		}
3283 	}
3284 	__bnxt_poll_work_done(bp, bnapi, budget);
3285 }
3286 
3287 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3288 {
3289 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3290 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3291 	struct bnxt_cp_ring_info *cpr_rx;
3292 	u32 raw_cons = cpr->cp_raw_cons;
3293 	struct bnxt *bp = bnapi->bp;
3294 	struct nqe_cn *nqcmp;
3295 	int work_done = 0;
3296 	u32 cons;
3297 
3298 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3299 		napi_complete(napi);
3300 		return 0;
3301 	}
3302 	if (cpr->has_more_work) {
3303 		cpr->has_more_work = 0;
3304 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3305 	}
3306 	while (1) {
3307 		u16 type;
3308 
3309 		cons = RING_CMP(raw_cons);
3310 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3311 
3312 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3313 			if (cpr->has_more_work)
3314 				break;
3315 
3316 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3317 					     budget);
3318 			cpr->cp_raw_cons = raw_cons;
3319 			if (napi_complete_done(napi, work_done))
3320 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3321 						  cpr->cp_raw_cons);
3322 			goto poll_done;
3323 		}
3324 
3325 		/* The valid test of the entry must be done first before
3326 		 * reading any further.
3327 		 */
3328 		dma_rmb();
3329 
3330 		type = le16_to_cpu(nqcmp->type);
3331 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3332 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3333 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3334 			struct bnxt_cp_ring_info *cpr2;
3335 
3336 			/* No more budget for RX work */
3337 			if (budget && work_done >= budget &&
3338 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3339 				break;
3340 
3341 			idx = BNXT_NQ_HDL_IDX(idx);
3342 			cpr2 = &cpr->cp_ring_arr[idx];
3343 			cpr2->had_nqe_notify = 1;
3344 			cpr2->toggle = NQE_CN_TOGGLE(type);
3345 			work_done += __bnxt_poll_work(bp, cpr2,
3346 						      budget - work_done);
3347 			cpr->has_more_work |= cpr2->has_more_work;
3348 		} else {
3349 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3350 		}
3351 		raw_cons = NEXT_RAW_CMP(raw_cons);
3352 	}
3353 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3354 	if (raw_cons != cpr->cp_raw_cons) {
3355 		cpr->cp_raw_cons = raw_cons;
3356 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3357 	}
3358 poll_done:
3359 	cpr_rx = &cpr->cp_ring_arr[0];
3360 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3361 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3362 		struct dim_sample dim_sample = {};
3363 
3364 		dim_update_sample(cpr->event_ctr,
3365 				  cpr_rx->rx_packets,
3366 				  cpr_rx->rx_bytes,
3367 				  &dim_sample);
3368 		net_dim(&cpr->dim, &dim_sample);
3369 	}
3370 	return work_done;
3371 }
3372 
3373 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3374 				       struct bnxt_tx_ring_info *txr, int idx)
3375 {
3376 	int i, max_idx;
3377 	struct pci_dev *pdev = bp->pdev;
3378 
3379 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3380 
3381 	for (i = 0; i < max_idx;) {
3382 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3383 		struct sk_buff *skb;
3384 		int j, last;
3385 
3386 		if (idx  < bp->tx_nr_rings_xdp &&
3387 		    tx_buf->action == XDP_REDIRECT) {
3388 			dma_unmap_single(&pdev->dev,
3389 					 dma_unmap_addr(tx_buf, mapping),
3390 					 dma_unmap_len(tx_buf, len),
3391 					 DMA_TO_DEVICE);
3392 			xdp_return_frame(tx_buf->xdpf);
3393 			tx_buf->action = 0;
3394 			tx_buf->xdpf = NULL;
3395 			i++;
3396 			continue;
3397 		}
3398 
3399 		skb = tx_buf->skb;
3400 		if (!skb) {
3401 			i++;
3402 			continue;
3403 		}
3404 
3405 		tx_buf->skb = NULL;
3406 
3407 		if (tx_buf->is_push) {
3408 			dev_kfree_skb(skb);
3409 			i += 2;
3410 			continue;
3411 		}
3412 
3413 		dma_unmap_single(&pdev->dev,
3414 				 dma_unmap_addr(tx_buf, mapping),
3415 				 skb_headlen(skb),
3416 				 DMA_TO_DEVICE);
3417 
3418 		last = tx_buf->nr_frags;
3419 		i += 2;
3420 		for (j = 0; j < last; j++, i++) {
3421 			int ring_idx = i & bp->tx_ring_mask;
3422 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3423 
3424 			tx_buf = &txr->tx_buf_ring[ring_idx];
3425 			dma_unmap_page(&pdev->dev,
3426 				       dma_unmap_addr(tx_buf, mapping),
3427 				       skb_frag_size(frag), DMA_TO_DEVICE);
3428 		}
3429 		dev_kfree_skb(skb);
3430 	}
3431 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3432 }
3433 
3434 static void bnxt_free_tx_skbs(struct bnxt *bp)
3435 {
3436 	int i;
3437 
3438 	if (!bp->tx_ring)
3439 		return;
3440 
3441 	for (i = 0; i < bp->tx_nr_rings; i++) {
3442 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3443 
3444 		if (!txr->tx_buf_ring)
3445 			continue;
3446 
3447 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3448 	}
3449 
3450 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
3451 		bnxt_ptp_free_txts_skbs(bp->ptp_cfg);
3452 }
3453 
3454 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3455 {
3456 	int i, max_idx;
3457 
3458 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3459 
3460 	for (i = 0; i < max_idx; i++) {
3461 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3462 		void *data = rx_buf->data;
3463 
3464 		if (!data)
3465 			continue;
3466 
3467 		rx_buf->data = NULL;
3468 		if (BNXT_RX_PAGE_MODE(bp))
3469 			page_pool_recycle_direct(rxr->page_pool, data);
3470 		else
3471 			page_pool_free_va(rxr->head_pool, data, true);
3472 	}
3473 }
3474 
3475 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3476 {
3477 	int i, max_idx;
3478 
3479 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3480 
3481 	for (i = 0; i < max_idx; i++) {
3482 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3483 		netmem_ref netmem = rx_agg_buf->netmem;
3484 
3485 		if (!netmem)
3486 			continue;
3487 
3488 		rx_agg_buf->netmem = 0;
3489 		__clear_bit(i, rxr->rx_agg_bmap);
3490 
3491 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3492 	}
3493 }
3494 
3495 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3496 					struct bnxt_rx_ring_info *rxr)
3497 {
3498 	int i;
3499 
3500 	for (i = 0; i < bp->max_tpa; i++) {
3501 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3502 		u8 *data = tpa_info->data;
3503 
3504 		if (!data)
3505 			continue;
3506 
3507 		tpa_info->data = NULL;
3508 		page_pool_free_va(rxr->head_pool, data, false);
3509 	}
3510 }
3511 
3512 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3513 				       struct bnxt_rx_ring_info *rxr)
3514 {
3515 	struct bnxt_tpa_idx_map *map;
3516 
3517 	if (!rxr->rx_tpa)
3518 		goto skip_rx_tpa_free;
3519 
3520 	bnxt_free_one_tpa_info_data(bp, rxr);
3521 
3522 skip_rx_tpa_free:
3523 	if (!rxr->rx_buf_ring)
3524 		goto skip_rx_buf_free;
3525 
3526 	bnxt_free_one_rx_ring(bp, rxr);
3527 
3528 skip_rx_buf_free:
3529 	if (!rxr->rx_agg_ring)
3530 		goto skip_rx_agg_free;
3531 
3532 	bnxt_free_one_rx_agg_ring(bp, rxr);
3533 
3534 skip_rx_agg_free:
3535 	map = rxr->rx_tpa_idx_map;
3536 	if (map)
3537 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3538 }
3539 
3540 static void bnxt_free_rx_skbs(struct bnxt *bp)
3541 {
3542 	int i;
3543 
3544 	if (!bp->rx_ring)
3545 		return;
3546 
3547 	for (i = 0; i < bp->rx_nr_rings; i++)
3548 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3549 }
3550 
3551 static void bnxt_free_skbs(struct bnxt *bp)
3552 {
3553 	bnxt_free_tx_skbs(bp);
3554 	bnxt_free_rx_skbs(bp);
3555 }
3556 
3557 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3558 {
3559 	u8 init_val = ctxm->init_value;
3560 	u16 offset = ctxm->init_offset;
3561 	u8 *p2 = p;
3562 	int i;
3563 
3564 	if (!init_val)
3565 		return;
3566 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3567 		memset(p, init_val, len);
3568 		return;
3569 	}
3570 	for (i = 0; i < len; i += ctxm->entry_size)
3571 		*(p2 + i + offset) = init_val;
3572 }
3573 
3574 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3575 			       void *buf, size_t offset, size_t head,
3576 			       size_t tail)
3577 {
3578 	int i, head_page, start_idx, source_offset;
3579 	size_t len, rem_len, total_len, max_bytes;
3580 
3581 	head_page = head / rmem->page_size;
3582 	source_offset = head % rmem->page_size;
3583 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3584 	if (!total_len)
3585 		total_len = MAX_CTX_BYTES;
3586 	start_idx = head_page % MAX_CTX_PAGES;
3587 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3588 		    source_offset;
3589 	total_len = min(total_len, max_bytes);
3590 	rem_len = total_len;
3591 
3592 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3593 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3594 		if (buf)
3595 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3596 			       len);
3597 		offset += len;
3598 		rem_len -= len;
3599 	}
3600 	return total_len;
3601 }
3602 
3603 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3604 {
3605 	struct pci_dev *pdev = bp->pdev;
3606 	int i;
3607 
3608 	if (!rmem->pg_arr)
3609 		goto skip_pages;
3610 
3611 	for (i = 0; i < rmem->nr_pages; i++) {
3612 		if (!rmem->pg_arr[i])
3613 			continue;
3614 
3615 		dma_free_coherent(&pdev->dev, rmem->page_size,
3616 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3617 
3618 		rmem->pg_arr[i] = NULL;
3619 	}
3620 skip_pages:
3621 	if (rmem->pg_tbl) {
3622 		size_t pg_tbl_size = rmem->nr_pages * 8;
3623 
3624 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3625 			pg_tbl_size = rmem->page_size;
3626 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3627 				  rmem->pg_tbl, rmem->pg_tbl_map);
3628 		rmem->pg_tbl = NULL;
3629 	}
3630 	if (rmem->vmem_size && *rmem->vmem) {
3631 		vfree(*rmem->vmem);
3632 		*rmem->vmem = NULL;
3633 	}
3634 }
3635 
3636 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3637 {
3638 	struct pci_dev *pdev = bp->pdev;
3639 	u64 valid_bit = 0;
3640 	int i;
3641 
3642 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3643 		valid_bit = PTU_PTE_VALID;
3644 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3645 		size_t pg_tbl_size = rmem->nr_pages * 8;
3646 
3647 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3648 			pg_tbl_size = rmem->page_size;
3649 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3650 						  &rmem->pg_tbl_map,
3651 						  GFP_KERNEL);
3652 		if (!rmem->pg_tbl)
3653 			return -ENOMEM;
3654 	}
3655 
3656 	for (i = 0; i < rmem->nr_pages; i++) {
3657 		u64 extra_bits = valid_bit;
3658 
3659 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3660 						     rmem->page_size,
3661 						     &rmem->dma_arr[i],
3662 						     GFP_KERNEL);
3663 		if (!rmem->pg_arr[i])
3664 			return -ENOMEM;
3665 
3666 		if (rmem->ctx_mem)
3667 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3668 					  rmem->page_size);
3669 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3670 			if (i == rmem->nr_pages - 2 &&
3671 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3672 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3673 			else if (i == rmem->nr_pages - 1 &&
3674 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3675 				extra_bits |= PTU_PTE_LAST;
3676 			rmem->pg_tbl[i] =
3677 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3678 		}
3679 	}
3680 
3681 	if (rmem->vmem_size) {
3682 		*rmem->vmem = vzalloc(rmem->vmem_size);
3683 		if (!(*rmem->vmem))
3684 			return -ENOMEM;
3685 	}
3686 	return 0;
3687 }
3688 
3689 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3690 				   struct bnxt_rx_ring_info *rxr)
3691 {
3692 	int i;
3693 
3694 	kfree(rxr->rx_tpa_idx_map);
3695 	rxr->rx_tpa_idx_map = NULL;
3696 	if (rxr->rx_tpa) {
3697 		for (i = 0; i < bp->max_tpa; i++) {
3698 			kfree(rxr->rx_tpa[i].agg_arr);
3699 			rxr->rx_tpa[i].agg_arr = NULL;
3700 		}
3701 	}
3702 	kfree(rxr->rx_tpa);
3703 	rxr->rx_tpa = NULL;
3704 }
3705 
3706 static void bnxt_free_tpa_info(struct bnxt *bp)
3707 {
3708 	int i;
3709 
3710 	for (i = 0; i < bp->rx_nr_rings; i++) {
3711 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3712 
3713 		bnxt_free_one_tpa_info(bp, rxr);
3714 	}
3715 }
3716 
3717 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3718 				   struct bnxt_rx_ring_info *rxr)
3719 {
3720 	struct rx_agg_cmp *agg;
3721 	int i;
3722 
3723 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3724 			      GFP_KERNEL);
3725 	if (!rxr->rx_tpa)
3726 		return -ENOMEM;
3727 
3728 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3729 		return 0;
3730 	for (i = 0; i < bp->max_tpa; i++) {
3731 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3732 		if (!agg)
3733 			return -ENOMEM;
3734 		rxr->rx_tpa[i].agg_arr = agg;
3735 	}
3736 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3737 				      GFP_KERNEL);
3738 	if (!rxr->rx_tpa_idx_map)
3739 		return -ENOMEM;
3740 
3741 	return 0;
3742 }
3743 
3744 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3745 {
3746 	int i, rc;
3747 
3748 	bp->max_tpa = MAX_TPA;
3749 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3750 		if (!bp->max_tpa_v2)
3751 			return 0;
3752 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3753 	}
3754 
3755 	for (i = 0; i < bp->rx_nr_rings; i++) {
3756 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3757 
3758 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3759 		if (rc)
3760 			return rc;
3761 	}
3762 	return 0;
3763 }
3764 
3765 static void bnxt_free_rx_rings(struct bnxt *bp)
3766 {
3767 	int i;
3768 
3769 	if (!bp->rx_ring)
3770 		return;
3771 
3772 	bnxt_free_tpa_info(bp);
3773 	for (i = 0; i < bp->rx_nr_rings; i++) {
3774 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3775 		struct bnxt_ring_struct *ring;
3776 
3777 		if (rxr->xdp_prog)
3778 			bpf_prog_put(rxr->xdp_prog);
3779 
3780 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3781 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3782 
3783 		page_pool_destroy(rxr->page_pool);
3784 		if (bnxt_separate_head_pool(rxr))
3785 			page_pool_destroy(rxr->head_pool);
3786 		rxr->page_pool = rxr->head_pool = NULL;
3787 
3788 		kfree(rxr->rx_agg_bmap);
3789 		rxr->rx_agg_bmap = NULL;
3790 
3791 		ring = &rxr->rx_ring_struct;
3792 		bnxt_free_ring(bp, &ring->ring_mem);
3793 
3794 		ring = &rxr->rx_agg_ring_struct;
3795 		bnxt_free_ring(bp, &ring->ring_mem);
3796 	}
3797 }
3798 
3799 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3800 				   struct bnxt_rx_ring_info *rxr,
3801 				   int numa_node)
3802 {
3803 	struct page_pool_params pp = { 0 };
3804 	struct page_pool *pool;
3805 
3806 	pp.pool_size = bp->rx_agg_ring_size;
3807 	if (BNXT_RX_PAGE_MODE(bp))
3808 		pp.pool_size += bp->rx_ring_size;
3809 	pp.nid = numa_node;
3810 	pp.napi = &rxr->bnapi->napi;
3811 	pp.netdev = bp->dev;
3812 	pp.dev = &bp->pdev->dev;
3813 	pp.dma_dir = bp->rx_dir;
3814 	pp.max_len = PAGE_SIZE;
3815 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3816 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3817 	pp.queue_idx = rxr->bnapi->index;
3818 
3819 	pool = page_pool_create(&pp);
3820 	if (IS_ERR(pool))
3821 		return PTR_ERR(pool);
3822 	rxr->page_pool = pool;
3823 
3824 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3825 	if (bnxt_separate_head_pool(rxr)) {
3826 		pp.pool_size = max(bp->rx_ring_size, 1024);
3827 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3828 		pool = page_pool_create(&pp);
3829 		if (IS_ERR(pool))
3830 			goto err_destroy_pp;
3831 	}
3832 	rxr->head_pool = pool;
3833 
3834 	return 0;
3835 
3836 err_destroy_pp:
3837 	page_pool_destroy(rxr->page_pool);
3838 	rxr->page_pool = NULL;
3839 	return PTR_ERR(pool);
3840 }
3841 
3842 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3843 {
3844 	u16 mem_size;
3845 
3846 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3847 	mem_size = rxr->rx_agg_bmap_size / 8;
3848 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3849 	if (!rxr->rx_agg_bmap)
3850 		return -ENOMEM;
3851 
3852 	return 0;
3853 }
3854 
3855 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3856 {
3857 	int numa_node = dev_to_node(&bp->pdev->dev);
3858 	int i, rc = 0, agg_rings = 0, cpu;
3859 
3860 	if (!bp->rx_ring)
3861 		return -ENOMEM;
3862 
3863 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3864 		agg_rings = 1;
3865 
3866 	for (i = 0; i < bp->rx_nr_rings; i++) {
3867 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3868 		struct bnxt_ring_struct *ring;
3869 		int cpu_node;
3870 
3871 		ring = &rxr->rx_ring_struct;
3872 
3873 		cpu = cpumask_local_spread(i, numa_node);
3874 		cpu_node = cpu_to_node(cpu);
3875 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3876 			   i, cpu_node);
3877 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3878 		if (rc)
3879 			return rc;
3880 
3881 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3882 		if (rc < 0)
3883 			return rc;
3884 
3885 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3886 						MEM_TYPE_PAGE_POOL,
3887 						rxr->page_pool);
3888 		if (rc) {
3889 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3890 			return rc;
3891 		}
3892 
3893 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3894 		if (rc)
3895 			return rc;
3896 
3897 		ring->grp_idx = i;
3898 		if (agg_rings) {
3899 			ring = &rxr->rx_agg_ring_struct;
3900 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3901 			if (rc)
3902 				return rc;
3903 
3904 			ring->grp_idx = i;
3905 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3906 			if (rc)
3907 				return rc;
3908 		}
3909 	}
3910 	if (bp->flags & BNXT_FLAG_TPA)
3911 		rc = bnxt_alloc_tpa_info(bp);
3912 	return rc;
3913 }
3914 
3915 static void bnxt_free_tx_rings(struct bnxt *bp)
3916 {
3917 	int i;
3918 	struct pci_dev *pdev = bp->pdev;
3919 
3920 	if (!bp->tx_ring)
3921 		return;
3922 
3923 	for (i = 0; i < bp->tx_nr_rings; i++) {
3924 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3925 		struct bnxt_ring_struct *ring;
3926 
3927 		if (txr->tx_push) {
3928 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3929 					  txr->tx_push, txr->tx_push_mapping);
3930 			txr->tx_push = NULL;
3931 		}
3932 
3933 		ring = &txr->tx_ring_struct;
3934 
3935 		bnxt_free_ring(bp, &ring->ring_mem);
3936 	}
3937 }
3938 
3939 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3940 	((tc) * (bp)->tx_nr_rings_per_tc)
3941 
3942 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3943 	((tx) % (bp)->tx_nr_rings_per_tc)
3944 
3945 #define BNXT_RING_TO_TC(bp, tx)		\
3946 	((tx) / (bp)->tx_nr_rings_per_tc)
3947 
3948 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3949 {
3950 	int i, j, rc;
3951 	struct pci_dev *pdev = bp->pdev;
3952 
3953 	bp->tx_push_size = 0;
3954 	if (bp->tx_push_thresh) {
3955 		int push_size;
3956 
3957 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3958 					bp->tx_push_thresh);
3959 
3960 		if (push_size > 256) {
3961 			push_size = 0;
3962 			bp->tx_push_thresh = 0;
3963 		}
3964 
3965 		bp->tx_push_size = push_size;
3966 	}
3967 
3968 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3969 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3970 		struct bnxt_ring_struct *ring;
3971 		u8 qidx;
3972 
3973 		ring = &txr->tx_ring_struct;
3974 
3975 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3976 		if (rc)
3977 			return rc;
3978 
3979 		ring->grp_idx = txr->bnapi->index;
3980 		if (bp->tx_push_size) {
3981 			dma_addr_t mapping;
3982 
3983 			/* One pre-allocated DMA buffer to backup
3984 			 * TX push operation
3985 			 */
3986 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3987 						bp->tx_push_size,
3988 						&txr->tx_push_mapping,
3989 						GFP_KERNEL);
3990 
3991 			if (!txr->tx_push)
3992 				return -ENOMEM;
3993 
3994 			mapping = txr->tx_push_mapping +
3995 				sizeof(struct tx_push_bd);
3996 			txr->data_mapping = cpu_to_le64(mapping);
3997 		}
3998 		qidx = bp->tc_to_qidx[j];
3999 		ring->queue_id = bp->q_info[qidx].queue_id;
4000 		spin_lock_init(&txr->xdp_tx_lock);
4001 		if (i < bp->tx_nr_rings_xdp)
4002 			continue;
4003 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4004 			j++;
4005 	}
4006 	return 0;
4007 }
4008 
4009 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4010 {
4011 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4012 
4013 	kfree(cpr->cp_desc_ring);
4014 	cpr->cp_desc_ring = NULL;
4015 	ring->ring_mem.pg_arr = NULL;
4016 	kfree(cpr->cp_desc_mapping);
4017 	cpr->cp_desc_mapping = NULL;
4018 	ring->ring_mem.dma_arr = NULL;
4019 }
4020 
4021 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4022 {
4023 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
4024 	if (!cpr->cp_desc_ring)
4025 		return -ENOMEM;
4026 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
4027 				       GFP_KERNEL);
4028 	if (!cpr->cp_desc_mapping)
4029 		return -ENOMEM;
4030 	return 0;
4031 }
4032 
4033 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4034 {
4035 	int i;
4036 
4037 	if (!bp->bnapi)
4038 		return;
4039 	for (i = 0; i < bp->cp_nr_rings; i++) {
4040 		struct bnxt_napi *bnapi = bp->bnapi[i];
4041 
4042 		if (!bnapi)
4043 			continue;
4044 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4045 	}
4046 }
4047 
4048 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4049 {
4050 	int i, n = bp->cp_nr_pages;
4051 
4052 	for (i = 0; i < bp->cp_nr_rings; i++) {
4053 		struct bnxt_napi *bnapi = bp->bnapi[i];
4054 		int rc;
4055 
4056 		if (!bnapi)
4057 			continue;
4058 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4059 		if (rc)
4060 			return rc;
4061 	}
4062 	return 0;
4063 }
4064 
4065 static void bnxt_free_cp_rings(struct bnxt *bp)
4066 {
4067 	int i;
4068 
4069 	if (!bp->bnapi)
4070 		return;
4071 
4072 	for (i = 0; i < bp->cp_nr_rings; i++) {
4073 		struct bnxt_napi *bnapi = bp->bnapi[i];
4074 		struct bnxt_cp_ring_info *cpr;
4075 		struct bnxt_ring_struct *ring;
4076 		int j;
4077 
4078 		if (!bnapi)
4079 			continue;
4080 
4081 		cpr = &bnapi->cp_ring;
4082 		ring = &cpr->cp_ring_struct;
4083 
4084 		bnxt_free_ring(bp, &ring->ring_mem);
4085 
4086 		if (!cpr->cp_ring_arr)
4087 			continue;
4088 
4089 		for (j = 0; j < cpr->cp_ring_count; j++) {
4090 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4091 
4092 			ring = &cpr2->cp_ring_struct;
4093 			bnxt_free_ring(bp, &ring->ring_mem);
4094 			bnxt_free_cp_arrays(cpr2);
4095 		}
4096 		kfree(cpr->cp_ring_arr);
4097 		cpr->cp_ring_arr = NULL;
4098 		cpr->cp_ring_count = 0;
4099 	}
4100 }
4101 
4102 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4103 				  struct bnxt_cp_ring_info *cpr)
4104 {
4105 	struct bnxt_ring_mem_info *rmem;
4106 	struct bnxt_ring_struct *ring;
4107 	int rc;
4108 
4109 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4110 	if (rc) {
4111 		bnxt_free_cp_arrays(cpr);
4112 		return -ENOMEM;
4113 	}
4114 	ring = &cpr->cp_ring_struct;
4115 	rmem = &ring->ring_mem;
4116 	rmem->nr_pages = bp->cp_nr_pages;
4117 	rmem->page_size = HW_CMPD_RING_SIZE;
4118 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4119 	rmem->dma_arr = cpr->cp_desc_mapping;
4120 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4121 	rc = bnxt_alloc_ring(bp, rmem);
4122 	if (rc) {
4123 		bnxt_free_ring(bp, rmem);
4124 		bnxt_free_cp_arrays(cpr);
4125 	}
4126 	return rc;
4127 }
4128 
4129 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4130 {
4131 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4132 	int i, j, rc, ulp_msix;
4133 	int tcs = bp->num_tc;
4134 
4135 	if (!tcs)
4136 		tcs = 1;
4137 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4138 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4139 		struct bnxt_napi *bnapi = bp->bnapi[i];
4140 		struct bnxt_cp_ring_info *cpr, *cpr2;
4141 		struct bnxt_ring_struct *ring;
4142 		int cp_count = 0, k;
4143 		int rx = 0, tx = 0;
4144 
4145 		if (!bnapi)
4146 			continue;
4147 
4148 		cpr = &bnapi->cp_ring;
4149 		cpr->bnapi = bnapi;
4150 		ring = &cpr->cp_ring_struct;
4151 
4152 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4153 		if (rc)
4154 			return rc;
4155 
4156 		ring->map_idx = ulp_msix + i;
4157 
4158 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4159 			continue;
4160 
4161 		if (i < bp->rx_nr_rings) {
4162 			cp_count++;
4163 			rx = 1;
4164 		}
4165 		if (i < bp->tx_nr_rings_xdp) {
4166 			cp_count++;
4167 			tx = 1;
4168 		} else if ((sh && i < bp->tx_nr_rings) ||
4169 			 (!sh && i >= bp->rx_nr_rings)) {
4170 			cp_count += tcs;
4171 			tx = 1;
4172 		}
4173 
4174 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4175 					   GFP_KERNEL);
4176 		if (!cpr->cp_ring_arr)
4177 			return -ENOMEM;
4178 		cpr->cp_ring_count = cp_count;
4179 
4180 		for (k = 0; k < cp_count; k++) {
4181 			cpr2 = &cpr->cp_ring_arr[k];
4182 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4183 			if (rc)
4184 				return rc;
4185 			cpr2->bnapi = bnapi;
4186 			cpr2->sw_stats = cpr->sw_stats;
4187 			cpr2->cp_idx = k;
4188 			if (!k && rx) {
4189 				bp->rx_ring[i].rx_cpr = cpr2;
4190 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4191 			} else {
4192 				int n, tc = k - rx;
4193 
4194 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4195 				bp->tx_ring[n].tx_cpr = cpr2;
4196 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4197 			}
4198 		}
4199 		if (tx)
4200 			j++;
4201 	}
4202 	return 0;
4203 }
4204 
4205 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4206 				     struct bnxt_rx_ring_info *rxr)
4207 {
4208 	struct bnxt_ring_mem_info *rmem;
4209 	struct bnxt_ring_struct *ring;
4210 
4211 	ring = &rxr->rx_ring_struct;
4212 	rmem = &ring->ring_mem;
4213 	rmem->nr_pages = bp->rx_nr_pages;
4214 	rmem->page_size = HW_RXBD_RING_SIZE;
4215 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4216 	rmem->dma_arr = rxr->rx_desc_mapping;
4217 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4218 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4219 
4220 	ring = &rxr->rx_agg_ring_struct;
4221 	rmem = &ring->ring_mem;
4222 	rmem->nr_pages = bp->rx_agg_nr_pages;
4223 	rmem->page_size = HW_RXBD_RING_SIZE;
4224 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4225 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4226 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4227 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4228 }
4229 
4230 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4231 				      struct bnxt_rx_ring_info *rxr)
4232 {
4233 	struct bnxt_ring_mem_info *rmem;
4234 	struct bnxt_ring_struct *ring;
4235 	int i;
4236 
4237 	rxr->page_pool->p.napi = NULL;
4238 	rxr->page_pool = NULL;
4239 	rxr->head_pool->p.napi = NULL;
4240 	rxr->head_pool = NULL;
4241 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4242 
4243 	ring = &rxr->rx_ring_struct;
4244 	rmem = &ring->ring_mem;
4245 	rmem->pg_tbl = NULL;
4246 	rmem->pg_tbl_map = 0;
4247 	for (i = 0; i < rmem->nr_pages; i++) {
4248 		rmem->pg_arr[i] = NULL;
4249 		rmem->dma_arr[i] = 0;
4250 	}
4251 	*rmem->vmem = NULL;
4252 
4253 	ring = &rxr->rx_agg_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 
4264 static void bnxt_init_ring_struct(struct bnxt *bp)
4265 {
4266 	int i, j;
4267 
4268 	for (i = 0; i < bp->cp_nr_rings; i++) {
4269 		struct bnxt_napi *bnapi = bp->bnapi[i];
4270 		struct bnxt_ring_mem_info *rmem;
4271 		struct bnxt_cp_ring_info *cpr;
4272 		struct bnxt_rx_ring_info *rxr;
4273 		struct bnxt_tx_ring_info *txr;
4274 		struct bnxt_ring_struct *ring;
4275 
4276 		if (!bnapi)
4277 			continue;
4278 
4279 		cpr = &bnapi->cp_ring;
4280 		ring = &cpr->cp_ring_struct;
4281 		rmem = &ring->ring_mem;
4282 		rmem->nr_pages = bp->cp_nr_pages;
4283 		rmem->page_size = HW_CMPD_RING_SIZE;
4284 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4285 		rmem->dma_arr = cpr->cp_desc_mapping;
4286 		rmem->vmem_size = 0;
4287 
4288 		rxr = bnapi->rx_ring;
4289 		if (!rxr)
4290 			goto skip_rx;
4291 
4292 		ring = &rxr->rx_ring_struct;
4293 		rmem = &ring->ring_mem;
4294 		rmem->nr_pages = bp->rx_nr_pages;
4295 		rmem->page_size = HW_RXBD_RING_SIZE;
4296 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4297 		rmem->dma_arr = rxr->rx_desc_mapping;
4298 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4299 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4300 
4301 		ring = &rxr->rx_agg_ring_struct;
4302 		rmem = &ring->ring_mem;
4303 		rmem->nr_pages = bp->rx_agg_nr_pages;
4304 		rmem->page_size = HW_RXBD_RING_SIZE;
4305 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4306 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4307 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4308 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4309 
4310 skip_rx:
4311 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4312 			ring = &txr->tx_ring_struct;
4313 			rmem = &ring->ring_mem;
4314 			rmem->nr_pages = bp->tx_nr_pages;
4315 			rmem->page_size = HW_TXBD_RING_SIZE;
4316 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4317 			rmem->dma_arr = txr->tx_desc_mapping;
4318 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4319 			rmem->vmem = (void **)&txr->tx_buf_ring;
4320 		}
4321 	}
4322 }
4323 
4324 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4325 {
4326 	int i;
4327 	u32 prod;
4328 	struct rx_bd **rx_buf_ring;
4329 
4330 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4331 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4332 		int j;
4333 		struct rx_bd *rxbd;
4334 
4335 		rxbd = rx_buf_ring[i];
4336 		if (!rxbd)
4337 			continue;
4338 
4339 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4340 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4341 			rxbd->rx_bd_opaque = prod;
4342 		}
4343 	}
4344 }
4345 
4346 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4347 				       struct bnxt_rx_ring_info *rxr,
4348 				       int ring_nr)
4349 {
4350 	u32 prod;
4351 	int i;
4352 
4353 	prod = rxr->rx_prod;
4354 	for (i = 0; i < bp->rx_ring_size; i++) {
4355 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4356 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4357 				    ring_nr, i, bp->rx_ring_size);
4358 			break;
4359 		}
4360 		prod = NEXT_RX(prod);
4361 	}
4362 	rxr->rx_prod = prod;
4363 }
4364 
4365 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4366 					  struct bnxt_rx_ring_info *rxr,
4367 					  int ring_nr)
4368 {
4369 	u32 prod;
4370 	int i;
4371 
4372 	prod = rxr->rx_agg_prod;
4373 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4374 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4375 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4376 				    ring_nr, i, bp->rx_ring_size);
4377 			break;
4378 		}
4379 		prod = NEXT_RX_AGG(prod);
4380 	}
4381 	rxr->rx_agg_prod = prod;
4382 }
4383 
4384 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4385 					struct bnxt_rx_ring_info *rxr)
4386 {
4387 	dma_addr_t mapping;
4388 	u8 *data;
4389 	int i;
4390 
4391 	for (i = 0; i < bp->max_tpa; i++) {
4392 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4393 					    GFP_KERNEL);
4394 		if (!data)
4395 			return -ENOMEM;
4396 
4397 		rxr->rx_tpa[i].data = data;
4398 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4399 		rxr->rx_tpa[i].mapping = mapping;
4400 	}
4401 
4402 	return 0;
4403 }
4404 
4405 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4406 {
4407 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4408 	int rc;
4409 
4410 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4411 
4412 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4413 		return 0;
4414 
4415 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4416 
4417 	if (rxr->rx_tpa) {
4418 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4419 		if (rc)
4420 			return rc;
4421 	}
4422 	return 0;
4423 }
4424 
4425 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4426 				       struct bnxt_rx_ring_info *rxr)
4427 {
4428 	struct bnxt_ring_struct *ring;
4429 	u32 type;
4430 
4431 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4432 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4433 
4434 	if (NET_IP_ALIGN == 2)
4435 		type |= RX_BD_FLAGS_SOP;
4436 
4437 	ring = &rxr->rx_ring_struct;
4438 	bnxt_init_rxbd_pages(ring, type);
4439 	ring->fw_ring_id = INVALID_HW_RING_ID;
4440 }
4441 
4442 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4443 					   struct bnxt_rx_ring_info *rxr)
4444 {
4445 	struct bnxt_ring_struct *ring;
4446 	u32 type;
4447 
4448 	ring = &rxr->rx_agg_ring_struct;
4449 	ring->fw_ring_id = INVALID_HW_RING_ID;
4450 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4451 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4452 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4453 
4454 		bnxt_init_rxbd_pages(ring, type);
4455 	}
4456 }
4457 
4458 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4459 {
4460 	struct bnxt_rx_ring_info *rxr;
4461 
4462 	rxr = &bp->rx_ring[ring_nr];
4463 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4464 
4465 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4466 			     &rxr->bnapi->napi);
4467 
4468 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4469 		bpf_prog_add(bp->xdp_prog, 1);
4470 		rxr->xdp_prog = bp->xdp_prog;
4471 	}
4472 
4473 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4474 
4475 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4476 }
4477 
4478 static void bnxt_init_cp_rings(struct bnxt *bp)
4479 {
4480 	int i, j;
4481 
4482 	for (i = 0; i < bp->cp_nr_rings; i++) {
4483 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4484 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4485 
4486 		ring->fw_ring_id = INVALID_HW_RING_ID;
4487 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4488 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4489 		if (!cpr->cp_ring_arr)
4490 			continue;
4491 		for (j = 0; j < cpr->cp_ring_count; j++) {
4492 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4493 
4494 			ring = &cpr2->cp_ring_struct;
4495 			ring->fw_ring_id = INVALID_HW_RING_ID;
4496 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4497 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4498 		}
4499 	}
4500 }
4501 
4502 static int bnxt_init_rx_rings(struct bnxt *bp)
4503 {
4504 	int i, rc = 0;
4505 
4506 	if (BNXT_RX_PAGE_MODE(bp)) {
4507 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4508 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4509 	} else {
4510 		bp->rx_offset = BNXT_RX_OFFSET;
4511 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4512 	}
4513 
4514 	for (i = 0; i < bp->rx_nr_rings; i++) {
4515 		rc = bnxt_init_one_rx_ring(bp, i);
4516 		if (rc)
4517 			break;
4518 	}
4519 
4520 	return rc;
4521 }
4522 
4523 static int bnxt_init_tx_rings(struct bnxt *bp)
4524 {
4525 	u16 i;
4526 
4527 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4528 				   BNXT_MIN_TX_DESC_CNT);
4529 
4530 	for (i = 0; i < bp->tx_nr_rings; i++) {
4531 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4532 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4533 
4534 		ring->fw_ring_id = INVALID_HW_RING_ID;
4535 
4536 		if (i >= bp->tx_nr_rings_xdp)
4537 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4538 					     NETDEV_QUEUE_TYPE_TX,
4539 					     &txr->bnapi->napi);
4540 	}
4541 
4542 	return 0;
4543 }
4544 
4545 static void bnxt_free_ring_grps(struct bnxt *bp)
4546 {
4547 	kfree(bp->grp_info);
4548 	bp->grp_info = NULL;
4549 }
4550 
4551 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4552 {
4553 	int i;
4554 
4555 	if (irq_re_init) {
4556 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4557 				       sizeof(struct bnxt_ring_grp_info),
4558 				       GFP_KERNEL);
4559 		if (!bp->grp_info)
4560 			return -ENOMEM;
4561 	}
4562 	for (i = 0; i < bp->cp_nr_rings; i++) {
4563 		if (irq_re_init)
4564 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4565 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4566 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4567 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4568 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4569 	}
4570 	return 0;
4571 }
4572 
4573 static void bnxt_free_vnics(struct bnxt *bp)
4574 {
4575 	kfree(bp->vnic_info);
4576 	bp->vnic_info = NULL;
4577 	bp->nr_vnics = 0;
4578 }
4579 
4580 static int bnxt_alloc_vnics(struct bnxt *bp)
4581 {
4582 	int num_vnics = 1;
4583 
4584 #ifdef CONFIG_RFS_ACCEL
4585 	if (bp->flags & BNXT_FLAG_RFS) {
4586 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4587 			num_vnics++;
4588 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4589 			num_vnics += bp->rx_nr_rings;
4590 	}
4591 #endif
4592 
4593 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4594 		num_vnics++;
4595 
4596 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4597 				GFP_KERNEL);
4598 	if (!bp->vnic_info)
4599 		return -ENOMEM;
4600 
4601 	bp->nr_vnics = num_vnics;
4602 	return 0;
4603 }
4604 
4605 static void bnxt_init_vnics(struct bnxt *bp)
4606 {
4607 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4608 	int i;
4609 
4610 	for (i = 0; i < bp->nr_vnics; i++) {
4611 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4612 		int j;
4613 
4614 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4615 		vnic->vnic_id = i;
4616 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4617 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4618 
4619 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4620 
4621 		if (bp->vnic_info[i].rss_hash_key) {
4622 			if (i == BNXT_VNIC_DEFAULT) {
4623 				u8 *key = (void *)vnic->rss_hash_key;
4624 				int k;
4625 
4626 				if (!bp->rss_hash_key_valid &&
4627 				    !bp->rss_hash_key_updated) {
4628 					get_random_bytes(bp->rss_hash_key,
4629 							 HW_HASH_KEY_SIZE);
4630 					bp->rss_hash_key_updated = true;
4631 				}
4632 
4633 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4634 				       HW_HASH_KEY_SIZE);
4635 
4636 				if (!bp->rss_hash_key_updated)
4637 					continue;
4638 
4639 				bp->rss_hash_key_updated = false;
4640 				bp->rss_hash_key_valid = true;
4641 
4642 				bp->toeplitz_prefix = 0;
4643 				for (k = 0; k < 8; k++) {
4644 					bp->toeplitz_prefix <<= 8;
4645 					bp->toeplitz_prefix |= key[k];
4646 				}
4647 			} else {
4648 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4649 				       HW_HASH_KEY_SIZE);
4650 			}
4651 		}
4652 	}
4653 }
4654 
4655 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4656 {
4657 	int pages;
4658 
4659 	pages = ring_size / desc_per_pg;
4660 
4661 	if (!pages)
4662 		return 1;
4663 
4664 	pages++;
4665 
4666 	while (pages & (pages - 1))
4667 		pages++;
4668 
4669 	return pages;
4670 }
4671 
4672 void bnxt_set_tpa_flags(struct bnxt *bp)
4673 {
4674 	bp->flags &= ~BNXT_FLAG_TPA;
4675 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4676 		return;
4677 	if (bp->dev->features & NETIF_F_LRO)
4678 		bp->flags |= BNXT_FLAG_LRO;
4679 	else if (bp->dev->features & NETIF_F_GRO_HW)
4680 		bp->flags |= BNXT_FLAG_GRO;
4681 }
4682 
4683 static void bnxt_init_ring_params(struct bnxt *bp)
4684 {
4685 	unsigned int rx_size;
4686 
4687 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4688 	/* Try to fit 4 chunks into a 4k page */
4689 	rx_size = SZ_1K -
4690 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4691 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4692 }
4693 
4694 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4695  * be set on entry.
4696  */
4697 void bnxt_set_ring_params(struct bnxt *bp)
4698 {
4699 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4700 	u32 agg_factor = 0, agg_ring_size = 0;
4701 
4702 	/* 8 for CRC and VLAN */
4703 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4704 
4705 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4706 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4707 
4708 	ring_size = bp->rx_ring_size;
4709 	bp->rx_agg_ring_size = 0;
4710 	bp->rx_agg_nr_pages = 0;
4711 
4712 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4713 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4714 
4715 	bp->flags &= ~BNXT_FLAG_JUMBO;
4716 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4717 		u32 jumbo_factor;
4718 
4719 		bp->flags |= BNXT_FLAG_JUMBO;
4720 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4721 		if (jumbo_factor > agg_factor)
4722 			agg_factor = jumbo_factor;
4723 	}
4724 	if (agg_factor) {
4725 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4726 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4727 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4728 				    bp->rx_ring_size, ring_size);
4729 			bp->rx_ring_size = ring_size;
4730 		}
4731 		agg_ring_size = ring_size * agg_factor;
4732 
4733 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4734 							RX_DESC_CNT);
4735 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4736 			u32 tmp = agg_ring_size;
4737 
4738 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4739 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4740 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4741 				    tmp, agg_ring_size);
4742 		}
4743 		bp->rx_agg_ring_size = agg_ring_size;
4744 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4745 
4746 		if (BNXT_RX_PAGE_MODE(bp)) {
4747 			rx_space = PAGE_SIZE;
4748 			rx_size = PAGE_SIZE -
4749 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4750 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4751 		} else {
4752 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4753 				       bp->rx_copybreak,
4754 				       bp->dev->cfg_pending->hds_thresh);
4755 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4756 			rx_space = rx_size + NET_SKB_PAD +
4757 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4758 		}
4759 	}
4760 
4761 	bp->rx_buf_use_size = rx_size;
4762 	bp->rx_buf_size = rx_space;
4763 
4764 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4765 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4766 
4767 	ring_size = bp->tx_ring_size;
4768 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4769 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4770 
4771 	max_rx_cmpl = bp->rx_ring_size;
4772 	/* MAX TPA needs to be added because TPA_START completions are
4773 	 * immediately recycled, so the TPA completions are not bound by
4774 	 * the RX ring size.
4775 	 */
4776 	if (bp->flags & BNXT_FLAG_TPA)
4777 		max_rx_cmpl += bp->max_tpa;
4778 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4779 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4780 	bp->cp_ring_size = ring_size;
4781 
4782 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4783 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4784 		bp->cp_nr_pages = MAX_CP_PAGES;
4785 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4786 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4787 			    ring_size, bp->cp_ring_size);
4788 	}
4789 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4790 	bp->cp_ring_mask = bp->cp_bit - 1;
4791 }
4792 
4793 /* Changing allocation mode of RX rings.
4794  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4795  */
4796 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4797 {
4798 	struct net_device *dev = bp->dev;
4799 
4800 	if (page_mode) {
4801 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4802 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4803 
4804 		if (bp->xdp_prog->aux->xdp_has_frags)
4805 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4806 		else
4807 			dev->max_mtu =
4808 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4809 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4810 			bp->flags |= BNXT_FLAG_JUMBO;
4811 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4812 		} else {
4813 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4814 			bp->rx_skb_func = bnxt_rx_page_skb;
4815 		}
4816 		bp->rx_dir = DMA_BIDIRECTIONAL;
4817 	} else {
4818 		dev->max_mtu = bp->max_mtu;
4819 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4820 		bp->rx_dir = DMA_FROM_DEVICE;
4821 		bp->rx_skb_func = bnxt_rx_skb;
4822 	}
4823 }
4824 
4825 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4826 {
4827 	__bnxt_set_rx_skb_mode(bp, page_mode);
4828 
4829 	if (!page_mode) {
4830 		int rx, tx;
4831 
4832 		bnxt_get_max_rings(bp, &rx, &tx, true);
4833 		if (rx > 1) {
4834 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4835 			bp->dev->hw_features |= NETIF_F_LRO;
4836 		}
4837 	}
4838 
4839 	/* Update LRO and GRO_HW availability */
4840 	netdev_update_features(bp->dev);
4841 }
4842 
4843 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4844 {
4845 	int i;
4846 	struct bnxt_vnic_info *vnic;
4847 	struct pci_dev *pdev = bp->pdev;
4848 
4849 	if (!bp->vnic_info)
4850 		return;
4851 
4852 	for (i = 0; i < bp->nr_vnics; i++) {
4853 		vnic = &bp->vnic_info[i];
4854 
4855 		kfree(vnic->fw_grp_ids);
4856 		vnic->fw_grp_ids = NULL;
4857 
4858 		kfree(vnic->uc_list);
4859 		vnic->uc_list = NULL;
4860 
4861 		if (vnic->mc_list) {
4862 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4863 					  vnic->mc_list, vnic->mc_list_mapping);
4864 			vnic->mc_list = NULL;
4865 		}
4866 
4867 		if (vnic->rss_table) {
4868 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4869 					  vnic->rss_table,
4870 					  vnic->rss_table_dma_addr);
4871 			vnic->rss_table = NULL;
4872 		}
4873 
4874 		vnic->rss_hash_key = NULL;
4875 		vnic->flags = 0;
4876 	}
4877 }
4878 
4879 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4880 {
4881 	int i, rc = 0, size;
4882 	struct bnxt_vnic_info *vnic;
4883 	struct pci_dev *pdev = bp->pdev;
4884 	int max_rings;
4885 
4886 	for (i = 0; i < bp->nr_vnics; i++) {
4887 		vnic = &bp->vnic_info[i];
4888 
4889 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4890 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4891 
4892 			if (mem_size > 0) {
4893 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4894 				if (!vnic->uc_list) {
4895 					rc = -ENOMEM;
4896 					goto out;
4897 				}
4898 			}
4899 		}
4900 
4901 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4902 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4903 			vnic->mc_list =
4904 				dma_alloc_coherent(&pdev->dev,
4905 						   vnic->mc_list_size,
4906 						   &vnic->mc_list_mapping,
4907 						   GFP_KERNEL);
4908 			if (!vnic->mc_list) {
4909 				rc = -ENOMEM;
4910 				goto out;
4911 			}
4912 		}
4913 
4914 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4915 			goto vnic_skip_grps;
4916 
4917 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4918 			max_rings = bp->rx_nr_rings;
4919 		else
4920 			max_rings = 1;
4921 
4922 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4923 		if (!vnic->fw_grp_ids) {
4924 			rc = -ENOMEM;
4925 			goto out;
4926 		}
4927 vnic_skip_grps:
4928 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4929 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4930 			continue;
4931 
4932 		/* Allocate rss table and hash key */
4933 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4934 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4935 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4936 
4937 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4938 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4939 						     vnic->rss_table_size,
4940 						     &vnic->rss_table_dma_addr,
4941 						     GFP_KERNEL);
4942 		if (!vnic->rss_table) {
4943 			rc = -ENOMEM;
4944 			goto out;
4945 		}
4946 
4947 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4948 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4949 	}
4950 	return 0;
4951 
4952 out:
4953 	return rc;
4954 }
4955 
4956 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4957 {
4958 	struct bnxt_hwrm_wait_token *token;
4959 
4960 	dma_pool_destroy(bp->hwrm_dma_pool);
4961 	bp->hwrm_dma_pool = NULL;
4962 
4963 	rcu_read_lock();
4964 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4965 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4966 	rcu_read_unlock();
4967 }
4968 
4969 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4970 {
4971 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4972 					    BNXT_HWRM_DMA_SIZE,
4973 					    BNXT_HWRM_DMA_ALIGN, 0);
4974 	if (!bp->hwrm_dma_pool)
4975 		return -ENOMEM;
4976 
4977 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4978 
4979 	return 0;
4980 }
4981 
4982 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4983 {
4984 	kfree(stats->hw_masks);
4985 	stats->hw_masks = NULL;
4986 	kfree(stats->sw_stats);
4987 	stats->sw_stats = NULL;
4988 	if (stats->hw_stats) {
4989 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4990 				  stats->hw_stats_map);
4991 		stats->hw_stats = NULL;
4992 	}
4993 }
4994 
4995 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4996 				bool alloc_masks)
4997 {
4998 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4999 					     &stats->hw_stats_map, GFP_KERNEL);
5000 	if (!stats->hw_stats)
5001 		return -ENOMEM;
5002 
5003 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5004 	if (!stats->sw_stats)
5005 		goto stats_mem_err;
5006 
5007 	if (alloc_masks) {
5008 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5009 		if (!stats->hw_masks)
5010 			goto stats_mem_err;
5011 	}
5012 	return 0;
5013 
5014 stats_mem_err:
5015 	bnxt_free_stats_mem(bp, stats);
5016 	return -ENOMEM;
5017 }
5018 
5019 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5020 {
5021 	int i;
5022 
5023 	for (i = 0; i < count; i++)
5024 		mask_arr[i] = mask;
5025 }
5026 
5027 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5028 {
5029 	int i;
5030 
5031 	for (i = 0; i < count; i++)
5032 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5033 }
5034 
5035 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5036 				    struct bnxt_stats_mem *stats)
5037 {
5038 	struct hwrm_func_qstats_ext_output *resp;
5039 	struct hwrm_func_qstats_ext_input *req;
5040 	__le64 *hw_masks;
5041 	int rc;
5042 
5043 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5044 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5045 		return -EOPNOTSUPP;
5046 
5047 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5048 	if (rc)
5049 		return rc;
5050 
5051 	req->fid = cpu_to_le16(0xffff);
5052 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5053 
5054 	resp = hwrm_req_hold(bp, req);
5055 	rc = hwrm_req_send(bp, req);
5056 	if (!rc) {
5057 		hw_masks = &resp->rx_ucast_pkts;
5058 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5059 	}
5060 	hwrm_req_drop(bp, req);
5061 	return rc;
5062 }
5063 
5064 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5065 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5066 
5067 static void bnxt_init_stats(struct bnxt *bp)
5068 {
5069 	struct bnxt_napi *bnapi = bp->bnapi[0];
5070 	struct bnxt_cp_ring_info *cpr;
5071 	struct bnxt_stats_mem *stats;
5072 	__le64 *rx_stats, *tx_stats;
5073 	int rc, rx_count, tx_count;
5074 	u64 *rx_masks, *tx_masks;
5075 	u64 mask;
5076 	u8 flags;
5077 
5078 	cpr = &bnapi->cp_ring;
5079 	stats = &cpr->stats;
5080 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5081 	if (rc) {
5082 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5083 			mask = (1ULL << 48) - 1;
5084 		else
5085 			mask = -1ULL;
5086 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5087 	}
5088 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5089 		stats = &bp->port_stats;
5090 		rx_stats = stats->hw_stats;
5091 		rx_masks = stats->hw_masks;
5092 		rx_count = sizeof(struct rx_port_stats) / 8;
5093 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5094 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5095 		tx_count = sizeof(struct tx_port_stats) / 8;
5096 
5097 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5098 		rc = bnxt_hwrm_port_qstats(bp, flags);
5099 		if (rc) {
5100 			mask = (1ULL << 40) - 1;
5101 
5102 			bnxt_fill_masks(rx_masks, mask, rx_count);
5103 			bnxt_fill_masks(tx_masks, mask, tx_count);
5104 		} else {
5105 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5106 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5107 			bnxt_hwrm_port_qstats(bp, 0);
5108 		}
5109 	}
5110 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5111 		stats = &bp->rx_port_stats_ext;
5112 		rx_stats = stats->hw_stats;
5113 		rx_masks = stats->hw_masks;
5114 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5115 		stats = &bp->tx_port_stats_ext;
5116 		tx_stats = stats->hw_stats;
5117 		tx_masks = stats->hw_masks;
5118 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5119 
5120 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5121 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5122 		if (rc) {
5123 			mask = (1ULL << 40) - 1;
5124 
5125 			bnxt_fill_masks(rx_masks, mask, rx_count);
5126 			if (tx_stats)
5127 				bnxt_fill_masks(tx_masks, mask, tx_count);
5128 		} else {
5129 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5130 			if (tx_stats)
5131 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5132 						   tx_count);
5133 			bnxt_hwrm_port_qstats_ext(bp, 0);
5134 		}
5135 	}
5136 }
5137 
5138 static void bnxt_free_port_stats(struct bnxt *bp)
5139 {
5140 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5141 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5142 
5143 	bnxt_free_stats_mem(bp, &bp->port_stats);
5144 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5145 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5146 }
5147 
5148 static void bnxt_free_ring_stats(struct bnxt *bp)
5149 {
5150 	int i;
5151 
5152 	if (!bp->bnapi)
5153 		return;
5154 
5155 	for (i = 0; i < bp->cp_nr_rings; i++) {
5156 		struct bnxt_napi *bnapi = bp->bnapi[i];
5157 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5158 
5159 		bnxt_free_stats_mem(bp, &cpr->stats);
5160 
5161 		kfree(cpr->sw_stats);
5162 		cpr->sw_stats = NULL;
5163 	}
5164 }
5165 
5166 static int bnxt_alloc_stats(struct bnxt *bp)
5167 {
5168 	u32 size, i;
5169 	int rc;
5170 
5171 	size = bp->hw_ring_stats_size;
5172 
5173 	for (i = 0; i < bp->cp_nr_rings; i++) {
5174 		struct bnxt_napi *bnapi = bp->bnapi[i];
5175 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5176 
5177 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5178 		if (!cpr->sw_stats)
5179 			return -ENOMEM;
5180 
5181 		cpr->stats.len = size;
5182 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5183 		if (rc)
5184 			return rc;
5185 
5186 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5187 	}
5188 
5189 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5190 		return 0;
5191 
5192 	if (bp->port_stats.hw_stats)
5193 		goto alloc_ext_stats;
5194 
5195 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5196 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5197 	if (rc)
5198 		return rc;
5199 
5200 	bp->flags |= BNXT_FLAG_PORT_STATS;
5201 
5202 alloc_ext_stats:
5203 	/* Display extended statistics only if FW supports it */
5204 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5205 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5206 			return 0;
5207 
5208 	if (bp->rx_port_stats_ext.hw_stats)
5209 		goto alloc_tx_ext_stats;
5210 
5211 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5212 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5213 	/* Extended stats are optional */
5214 	if (rc)
5215 		return 0;
5216 
5217 alloc_tx_ext_stats:
5218 	if (bp->tx_port_stats_ext.hw_stats)
5219 		return 0;
5220 
5221 	if (bp->hwrm_spec_code >= 0x10902 ||
5222 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5223 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5224 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5225 		/* Extended stats are optional */
5226 		if (rc)
5227 			return 0;
5228 	}
5229 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5230 	return 0;
5231 }
5232 
5233 static void bnxt_clear_ring_indices(struct bnxt *bp)
5234 {
5235 	int i, j;
5236 
5237 	if (!bp->bnapi)
5238 		return;
5239 
5240 	for (i = 0; i < bp->cp_nr_rings; i++) {
5241 		struct bnxt_napi *bnapi = bp->bnapi[i];
5242 		struct bnxt_cp_ring_info *cpr;
5243 		struct bnxt_rx_ring_info *rxr;
5244 		struct bnxt_tx_ring_info *txr;
5245 
5246 		if (!bnapi)
5247 			continue;
5248 
5249 		cpr = &bnapi->cp_ring;
5250 		cpr->cp_raw_cons = 0;
5251 
5252 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5253 			txr->tx_prod = 0;
5254 			txr->tx_cons = 0;
5255 			txr->tx_hw_cons = 0;
5256 		}
5257 
5258 		rxr = bnapi->rx_ring;
5259 		if (rxr) {
5260 			rxr->rx_prod = 0;
5261 			rxr->rx_agg_prod = 0;
5262 			rxr->rx_sw_agg_prod = 0;
5263 			rxr->rx_next_cons = 0;
5264 		}
5265 		bnapi->events = 0;
5266 	}
5267 }
5268 
5269 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5270 {
5271 	u8 type = fltr->type, flags = fltr->flags;
5272 
5273 	INIT_LIST_HEAD(&fltr->list);
5274 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5275 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5276 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5277 }
5278 
5279 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5280 {
5281 	if (!list_empty(&fltr->list))
5282 		list_del_init(&fltr->list);
5283 }
5284 
5285 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5286 {
5287 	struct bnxt_filter_base *usr_fltr, *tmp;
5288 
5289 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5290 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5291 			continue;
5292 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5293 	}
5294 }
5295 
5296 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5297 {
5298 	hlist_del(&fltr->hash);
5299 	bnxt_del_one_usr_fltr(bp, fltr);
5300 	if (fltr->flags) {
5301 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5302 		bp->ntp_fltr_count--;
5303 	}
5304 	kfree(fltr);
5305 }
5306 
5307 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5308 {
5309 	int i;
5310 
5311 	netdev_assert_locked(bp->dev);
5312 
5313 	/* Under netdev instance lock and all our NAPIs have been disabled.
5314 	 * It's safe to delete the hash table.
5315 	 */
5316 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5317 		struct hlist_head *head;
5318 		struct hlist_node *tmp;
5319 		struct bnxt_ntuple_filter *fltr;
5320 
5321 		head = &bp->ntp_fltr_hash_tbl[i];
5322 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5323 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5324 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5325 				     !list_empty(&fltr->base.list)))
5326 				continue;
5327 			bnxt_del_fltr(bp, &fltr->base);
5328 		}
5329 	}
5330 	if (!all)
5331 		return;
5332 
5333 	bitmap_free(bp->ntp_fltr_bmap);
5334 	bp->ntp_fltr_bmap = NULL;
5335 	bp->ntp_fltr_count = 0;
5336 }
5337 
5338 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5339 {
5340 	int i, rc = 0;
5341 
5342 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5343 		return 0;
5344 
5345 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5346 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5347 
5348 	bp->ntp_fltr_count = 0;
5349 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5350 
5351 	if (!bp->ntp_fltr_bmap)
5352 		rc = -ENOMEM;
5353 
5354 	return rc;
5355 }
5356 
5357 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5358 {
5359 	int i;
5360 
5361 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5362 		struct hlist_head *head;
5363 		struct hlist_node *tmp;
5364 		struct bnxt_l2_filter *fltr;
5365 
5366 		head = &bp->l2_fltr_hash_tbl[i];
5367 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5368 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5369 				     !list_empty(&fltr->base.list)))
5370 				continue;
5371 			bnxt_del_fltr(bp, &fltr->base);
5372 		}
5373 	}
5374 }
5375 
5376 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5377 {
5378 	int i;
5379 
5380 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5381 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5382 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5383 }
5384 
5385 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5386 {
5387 	bnxt_free_vnic_attributes(bp);
5388 	bnxt_free_tx_rings(bp);
5389 	bnxt_free_rx_rings(bp);
5390 	bnxt_free_cp_rings(bp);
5391 	bnxt_free_all_cp_arrays(bp);
5392 	bnxt_free_ntp_fltrs(bp, false);
5393 	bnxt_free_l2_filters(bp, false);
5394 	if (irq_re_init) {
5395 		bnxt_free_ring_stats(bp);
5396 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5397 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5398 			bnxt_free_port_stats(bp);
5399 		bnxt_free_ring_grps(bp);
5400 		bnxt_free_vnics(bp);
5401 		kfree(bp->tx_ring_map);
5402 		bp->tx_ring_map = NULL;
5403 		kfree(bp->tx_ring);
5404 		bp->tx_ring = NULL;
5405 		kfree(bp->rx_ring);
5406 		bp->rx_ring = NULL;
5407 		kfree(bp->bnapi);
5408 		bp->bnapi = NULL;
5409 	} else {
5410 		bnxt_clear_ring_indices(bp);
5411 	}
5412 }
5413 
5414 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5415 {
5416 	int i, j, rc, size, arr_size;
5417 	void *bnapi;
5418 
5419 	if (irq_re_init) {
5420 		/* Allocate bnapi mem pointer array and mem block for
5421 		 * all queues
5422 		 */
5423 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5424 				bp->cp_nr_rings);
5425 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5426 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5427 		if (!bnapi)
5428 			return -ENOMEM;
5429 
5430 		bp->bnapi = bnapi;
5431 		bnapi += arr_size;
5432 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5433 			bp->bnapi[i] = bnapi;
5434 			bp->bnapi[i]->index = i;
5435 			bp->bnapi[i]->bp = bp;
5436 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5437 				struct bnxt_cp_ring_info *cpr =
5438 					&bp->bnapi[i]->cp_ring;
5439 
5440 				cpr->cp_ring_struct.ring_mem.flags =
5441 					BNXT_RMEM_RING_PTE_FLAG;
5442 			}
5443 		}
5444 
5445 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5446 				      sizeof(struct bnxt_rx_ring_info),
5447 				      GFP_KERNEL);
5448 		if (!bp->rx_ring)
5449 			return -ENOMEM;
5450 
5451 		for (i = 0; i < bp->rx_nr_rings; i++) {
5452 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5453 
5454 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5455 				rxr->rx_ring_struct.ring_mem.flags =
5456 					BNXT_RMEM_RING_PTE_FLAG;
5457 				rxr->rx_agg_ring_struct.ring_mem.flags =
5458 					BNXT_RMEM_RING_PTE_FLAG;
5459 			} else {
5460 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5461 			}
5462 			rxr->bnapi = bp->bnapi[i];
5463 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5464 		}
5465 
5466 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5467 				      sizeof(struct bnxt_tx_ring_info),
5468 				      GFP_KERNEL);
5469 		if (!bp->tx_ring)
5470 			return -ENOMEM;
5471 
5472 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5473 					  GFP_KERNEL);
5474 
5475 		if (!bp->tx_ring_map)
5476 			return -ENOMEM;
5477 
5478 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5479 			j = 0;
5480 		else
5481 			j = bp->rx_nr_rings;
5482 
5483 		for (i = 0; i < bp->tx_nr_rings; i++) {
5484 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5485 			struct bnxt_napi *bnapi2;
5486 
5487 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5488 				txr->tx_ring_struct.ring_mem.flags =
5489 					BNXT_RMEM_RING_PTE_FLAG;
5490 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5491 			if (i >= bp->tx_nr_rings_xdp) {
5492 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5493 
5494 				bnapi2 = bp->bnapi[k];
5495 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5496 				txr->tx_napi_idx =
5497 					BNXT_RING_TO_TC(bp, txr->txq_index);
5498 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5499 				bnapi2->tx_int = bnxt_tx_int;
5500 			} else {
5501 				bnapi2 = bp->bnapi[j];
5502 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5503 				bnapi2->tx_ring[0] = txr;
5504 				bnapi2->tx_int = bnxt_tx_int_xdp;
5505 				j++;
5506 			}
5507 			txr->bnapi = bnapi2;
5508 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5509 				txr->tx_cpr = &bnapi2->cp_ring;
5510 		}
5511 
5512 		rc = bnxt_alloc_stats(bp);
5513 		if (rc)
5514 			goto alloc_mem_err;
5515 		bnxt_init_stats(bp);
5516 
5517 		rc = bnxt_alloc_ntp_fltrs(bp);
5518 		if (rc)
5519 			goto alloc_mem_err;
5520 
5521 		rc = bnxt_alloc_vnics(bp);
5522 		if (rc)
5523 			goto alloc_mem_err;
5524 	}
5525 
5526 	rc = bnxt_alloc_all_cp_arrays(bp);
5527 	if (rc)
5528 		goto alloc_mem_err;
5529 
5530 	bnxt_init_ring_struct(bp);
5531 
5532 	rc = bnxt_alloc_rx_rings(bp);
5533 	if (rc)
5534 		goto alloc_mem_err;
5535 
5536 	rc = bnxt_alloc_tx_rings(bp);
5537 	if (rc)
5538 		goto alloc_mem_err;
5539 
5540 	rc = bnxt_alloc_cp_rings(bp);
5541 	if (rc)
5542 		goto alloc_mem_err;
5543 
5544 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5545 						  BNXT_VNIC_MCAST_FLAG |
5546 						  BNXT_VNIC_UCAST_FLAG;
5547 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5548 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5549 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5550 
5551 	rc = bnxt_alloc_vnic_attributes(bp);
5552 	if (rc)
5553 		goto alloc_mem_err;
5554 	return 0;
5555 
5556 alloc_mem_err:
5557 	bnxt_free_mem(bp, true);
5558 	return rc;
5559 }
5560 
5561 static void bnxt_disable_int(struct bnxt *bp)
5562 {
5563 	int i;
5564 
5565 	if (!bp->bnapi)
5566 		return;
5567 
5568 	for (i = 0; i < bp->cp_nr_rings; i++) {
5569 		struct bnxt_napi *bnapi = bp->bnapi[i];
5570 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5571 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5572 
5573 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5574 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5575 	}
5576 }
5577 
5578 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5579 {
5580 	struct bnxt_napi *bnapi = bp->bnapi[n];
5581 	struct bnxt_cp_ring_info *cpr;
5582 
5583 	cpr = &bnapi->cp_ring;
5584 	return cpr->cp_ring_struct.map_idx;
5585 }
5586 
5587 static void bnxt_disable_int_sync(struct bnxt *bp)
5588 {
5589 	int i;
5590 
5591 	if (!bp->irq_tbl)
5592 		return;
5593 
5594 	atomic_inc(&bp->intr_sem);
5595 
5596 	bnxt_disable_int(bp);
5597 	for (i = 0; i < bp->cp_nr_rings; i++) {
5598 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5599 
5600 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5601 	}
5602 }
5603 
5604 static void bnxt_enable_int(struct bnxt *bp)
5605 {
5606 	int i;
5607 
5608 	atomic_set(&bp->intr_sem, 0);
5609 	for (i = 0; i < bp->cp_nr_rings; i++) {
5610 		struct bnxt_napi *bnapi = bp->bnapi[i];
5611 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5612 
5613 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5614 	}
5615 }
5616 
5617 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5618 			    bool async_only)
5619 {
5620 	DECLARE_BITMAP(async_events_bmap, 256);
5621 	u32 *events = (u32 *)async_events_bmap;
5622 	struct hwrm_func_drv_rgtr_output *resp;
5623 	struct hwrm_func_drv_rgtr_input *req;
5624 	u32 flags;
5625 	int rc, i;
5626 
5627 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5628 	if (rc)
5629 		return rc;
5630 
5631 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5632 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5633 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5634 
5635 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5636 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5637 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5638 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5639 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5640 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5641 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5642 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5643 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5644 	req->flags = cpu_to_le32(flags);
5645 	req->ver_maj_8b = DRV_VER_MAJ;
5646 	req->ver_min_8b = DRV_VER_MIN;
5647 	req->ver_upd_8b = DRV_VER_UPD;
5648 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5649 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5650 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5651 
5652 	if (BNXT_PF(bp)) {
5653 		u32 data[8];
5654 		int i;
5655 
5656 		memset(data, 0, sizeof(data));
5657 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5658 			u16 cmd = bnxt_vf_req_snif[i];
5659 			unsigned int bit, idx;
5660 
5661 			idx = cmd / 32;
5662 			bit = cmd % 32;
5663 			data[idx] |= 1 << bit;
5664 		}
5665 
5666 		for (i = 0; i < 8; i++)
5667 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5668 
5669 		req->enables |=
5670 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5671 	}
5672 
5673 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5674 		req->flags |= cpu_to_le32(
5675 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5676 
5677 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5678 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5679 		u16 event_id = bnxt_async_events_arr[i];
5680 
5681 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5682 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5683 			continue;
5684 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5685 		    !bp->ptp_cfg)
5686 			continue;
5687 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5688 	}
5689 	if (bmap && bmap_size) {
5690 		for (i = 0; i < bmap_size; i++) {
5691 			if (test_bit(i, bmap))
5692 				__set_bit(i, async_events_bmap);
5693 		}
5694 	}
5695 	for (i = 0; i < 8; i++)
5696 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5697 
5698 	if (async_only)
5699 		req->enables =
5700 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5701 
5702 	resp = hwrm_req_hold(bp, req);
5703 	rc = hwrm_req_send(bp, req);
5704 	if (!rc) {
5705 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5706 		if (resp->flags &
5707 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5708 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5709 	}
5710 	hwrm_req_drop(bp, req);
5711 	return rc;
5712 }
5713 
5714 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5715 {
5716 	struct hwrm_func_drv_unrgtr_input *req;
5717 	int rc;
5718 
5719 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5720 		return 0;
5721 
5722 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5723 	if (rc)
5724 		return rc;
5725 	return hwrm_req_send(bp, req);
5726 }
5727 
5728 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5729 
5730 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5731 {
5732 	struct hwrm_tunnel_dst_port_free_input *req;
5733 	int rc;
5734 
5735 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5736 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5737 		return 0;
5738 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5739 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5740 		return 0;
5741 
5742 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5743 	if (rc)
5744 		return rc;
5745 
5746 	req->tunnel_type = tunnel_type;
5747 
5748 	switch (tunnel_type) {
5749 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5750 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5751 		bp->vxlan_port = 0;
5752 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5753 		break;
5754 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5755 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5756 		bp->nge_port = 0;
5757 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5758 		break;
5759 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5760 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5761 		bp->vxlan_gpe_port = 0;
5762 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5763 		break;
5764 	default:
5765 		break;
5766 	}
5767 
5768 	rc = hwrm_req_send(bp, req);
5769 	if (rc)
5770 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5771 			   rc);
5772 	if (bp->flags & BNXT_FLAG_TPA)
5773 		bnxt_set_tpa(bp, true);
5774 	return rc;
5775 }
5776 
5777 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5778 					   u8 tunnel_type)
5779 {
5780 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5781 	struct hwrm_tunnel_dst_port_alloc_input *req;
5782 	int rc;
5783 
5784 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5785 	if (rc)
5786 		return rc;
5787 
5788 	req->tunnel_type = tunnel_type;
5789 	req->tunnel_dst_port_val = port;
5790 
5791 	resp = hwrm_req_hold(bp, req);
5792 	rc = hwrm_req_send(bp, req);
5793 	if (rc) {
5794 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5795 			   rc);
5796 		goto err_out;
5797 	}
5798 
5799 	switch (tunnel_type) {
5800 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5801 		bp->vxlan_port = port;
5802 		bp->vxlan_fw_dst_port_id =
5803 			le16_to_cpu(resp->tunnel_dst_port_id);
5804 		break;
5805 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5806 		bp->nge_port = port;
5807 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5808 		break;
5809 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5810 		bp->vxlan_gpe_port = port;
5811 		bp->vxlan_gpe_fw_dst_port_id =
5812 			le16_to_cpu(resp->tunnel_dst_port_id);
5813 		break;
5814 	default:
5815 		break;
5816 	}
5817 	if (bp->flags & BNXT_FLAG_TPA)
5818 		bnxt_set_tpa(bp, true);
5819 
5820 err_out:
5821 	hwrm_req_drop(bp, req);
5822 	return rc;
5823 }
5824 
5825 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5826 {
5827 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5828 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5829 	int rc;
5830 
5831 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5832 	if (rc)
5833 		return rc;
5834 
5835 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5836 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5837 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5838 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5839 	}
5840 	req->mask = cpu_to_le32(vnic->rx_mask);
5841 	return hwrm_req_send_silent(bp, req);
5842 }
5843 
5844 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5845 {
5846 	if (!atomic_dec_and_test(&fltr->refcnt))
5847 		return;
5848 	spin_lock_bh(&bp->ntp_fltr_lock);
5849 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5850 		spin_unlock_bh(&bp->ntp_fltr_lock);
5851 		return;
5852 	}
5853 	hlist_del_rcu(&fltr->base.hash);
5854 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5855 	if (fltr->base.flags) {
5856 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5857 		bp->ntp_fltr_count--;
5858 	}
5859 	spin_unlock_bh(&bp->ntp_fltr_lock);
5860 	kfree_rcu(fltr, base.rcu);
5861 }
5862 
5863 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5864 						      struct bnxt_l2_key *key,
5865 						      u32 idx)
5866 {
5867 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5868 	struct bnxt_l2_filter *fltr;
5869 
5870 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5871 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5872 
5873 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5874 		    l2_key->vlan == key->vlan)
5875 			return fltr;
5876 	}
5877 	return NULL;
5878 }
5879 
5880 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5881 						    struct bnxt_l2_key *key,
5882 						    u32 idx)
5883 {
5884 	struct bnxt_l2_filter *fltr = NULL;
5885 
5886 	rcu_read_lock();
5887 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5888 	if (fltr)
5889 		atomic_inc(&fltr->refcnt);
5890 	rcu_read_unlock();
5891 	return fltr;
5892 }
5893 
5894 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5895 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5896 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5897 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5898 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5899 
5900 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5901 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5902 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5903 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5904 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5905 
5906 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5907 {
5908 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5909 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5910 			return sizeof(fkeys->addrs.v4addrs) +
5911 			       sizeof(fkeys->ports);
5912 
5913 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5914 			return sizeof(fkeys->addrs.v4addrs);
5915 	}
5916 
5917 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5918 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5919 			return sizeof(fkeys->addrs.v6addrs) +
5920 			       sizeof(fkeys->ports);
5921 
5922 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5923 			return sizeof(fkeys->addrs.v6addrs);
5924 	}
5925 
5926 	return 0;
5927 }
5928 
5929 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5930 			 const unsigned char *key)
5931 {
5932 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5933 	struct bnxt_ipv4_tuple tuple4;
5934 	struct bnxt_ipv6_tuple tuple6;
5935 	int i, j, len = 0;
5936 	u8 *four_tuple;
5937 
5938 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5939 	if (!len)
5940 		return 0;
5941 
5942 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5943 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5944 		tuple4.ports = fkeys->ports;
5945 		four_tuple = (unsigned char *)&tuple4;
5946 	} else {
5947 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5948 		tuple6.ports = fkeys->ports;
5949 		four_tuple = (unsigned char *)&tuple6;
5950 	}
5951 
5952 	for (i = 0, j = 8; i < len; i++, j++) {
5953 		u8 byte = four_tuple[i];
5954 		int bit;
5955 
5956 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5957 			if (byte & 0x80)
5958 				hash ^= prefix;
5959 		}
5960 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5961 	}
5962 
5963 	/* The valid part of the hash is in the upper 32 bits. */
5964 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5965 }
5966 
5967 #ifdef CONFIG_RFS_ACCEL
5968 static struct bnxt_l2_filter *
5969 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5970 {
5971 	struct bnxt_l2_filter *fltr;
5972 	u32 idx;
5973 
5974 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5975 	      BNXT_L2_FLTR_HASH_MASK;
5976 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5977 	return fltr;
5978 }
5979 #endif
5980 
5981 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5982 			       struct bnxt_l2_key *key, u32 idx)
5983 {
5984 	struct hlist_head *head;
5985 
5986 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5987 	fltr->l2_key.vlan = key->vlan;
5988 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5989 	if (fltr->base.flags) {
5990 		int bit_id;
5991 
5992 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5993 						 bp->max_fltr, 0);
5994 		if (bit_id < 0)
5995 			return -ENOMEM;
5996 		fltr->base.sw_id = (u16)bit_id;
5997 		bp->ntp_fltr_count++;
5998 	}
5999 	head = &bp->l2_fltr_hash_tbl[idx];
6000 	hlist_add_head_rcu(&fltr->base.hash, head);
6001 	bnxt_insert_usr_fltr(bp, &fltr->base);
6002 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
6003 	atomic_set(&fltr->refcnt, 1);
6004 	return 0;
6005 }
6006 
6007 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6008 						   struct bnxt_l2_key *key,
6009 						   gfp_t gfp)
6010 {
6011 	struct bnxt_l2_filter *fltr;
6012 	u32 idx;
6013 	int rc;
6014 
6015 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6016 	      BNXT_L2_FLTR_HASH_MASK;
6017 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6018 	if (fltr)
6019 		return fltr;
6020 
6021 	fltr = kzalloc(sizeof(*fltr), gfp);
6022 	if (!fltr)
6023 		return ERR_PTR(-ENOMEM);
6024 	spin_lock_bh(&bp->ntp_fltr_lock);
6025 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6026 	spin_unlock_bh(&bp->ntp_fltr_lock);
6027 	if (rc) {
6028 		bnxt_del_l2_filter(bp, fltr);
6029 		fltr = ERR_PTR(rc);
6030 	}
6031 	return fltr;
6032 }
6033 
6034 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6035 						struct bnxt_l2_key *key,
6036 						u16 flags)
6037 {
6038 	struct bnxt_l2_filter *fltr;
6039 	u32 idx;
6040 	int rc;
6041 
6042 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6043 	      BNXT_L2_FLTR_HASH_MASK;
6044 	spin_lock_bh(&bp->ntp_fltr_lock);
6045 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6046 	if (fltr) {
6047 		fltr = ERR_PTR(-EEXIST);
6048 		goto l2_filter_exit;
6049 	}
6050 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
6051 	if (!fltr) {
6052 		fltr = ERR_PTR(-ENOMEM);
6053 		goto l2_filter_exit;
6054 	}
6055 	fltr->base.flags = flags;
6056 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6057 	if (rc) {
6058 		spin_unlock_bh(&bp->ntp_fltr_lock);
6059 		bnxt_del_l2_filter(bp, fltr);
6060 		return ERR_PTR(rc);
6061 	}
6062 
6063 l2_filter_exit:
6064 	spin_unlock_bh(&bp->ntp_fltr_lock);
6065 	return fltr;
6066 }
6067 
6068 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6069 {
6070 #ifdef CONFIG_BNXT_SRIOV
6071 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6072 
6073 	return vf->fw_fid;
6074 #else
6075 	return INVALID_HW_RING_ID;
6076 #endif
6077 }
6078 
6079 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6080 {
6081 	struct hwrm_cfa_l2_filter_free_input *req;
6082 	u16 target_id = 0xffff;
6083 	int rc;
6084 
6085 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6086 		struct bnxt_pf_info *pf = &bp->pf;
6087 
6088 		if (fltr->base.vf_idx >= pf->active_vfs)
6089 			return -EINVAL;
6090 
6091 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6092 		if (target_id == INVALID_HW_RING_ID)
6093 			return -EINVAL;
6094 	}
6095 
6096 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6097 	if (rc)
6098 		return rc;
6099 
6100 	req->target_id = cpu_to_le16(target_id);
6101 	req->l2_filter_id = fltr->base.filter_id;
6102 	return hwrm_req_send(bp, req);
6103 }
6104 
6105 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6106 {
6107 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6108 	struct hwrm_cfa_l2_filter_alloc_input *req;
6109 	u16 target_id = 0xffff;
6110 	int rc;
6111 
6112 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6113 		struct bnxt_pf_info *pf = &bp->pf;
6114 
6115 		if (fltr->base.vf_idx >= pf->active_vfs)
6116 			return -EINVAL;
6117 
6118 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6119 	}
6120 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6121 	if (rc)
6122 		return rc;
6123 
6124 	req->target_id = cpu_to_le16(target_id);
6125 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6126 
6127 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6128 		req->flags |=
6129 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6130 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6131 	req->enables =
6132 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6133 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6134 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6135 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6136 	eth_broadcast_addr(req->l2_addr_mask);
6137 
6138 	if (fltr->l2_key.vlan) {
6139 		req->enables |=
6140 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6141 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6142 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6143 		req->num_vlans = 1;
6144 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6145 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6146 	}
6147 
6148 	resp = hwrm_req_hold(bp, req);
6149 	rc = hwrm_req_send(bp, req);
6150 	if (!rc) {
6151 		fltr->base.filter_id = resp->l2_filter_id;
6152 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6153 	}
6154 	hwrm_req_drop(bp, req);
6155 	return rc;
6156 }
6157 
6158 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6159 				     struct bnxt_ntuple_filter *fltr)
6160 {
6161 	struct hwrm_cfa_ntuple_filter_free_input *req;
6162 	int rc;
6163 
6164 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6165 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6166 	if (rc)
6167 		return rc;
6168 
6169 	req->ntuple_filter_id = fltr->base.filter_id;
6170 	return hwrm_req_send(bp, req);
6171 }
6172 
6173 #define BNXT_NTP_FLTR_FLAGS					\
6174 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6175 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6176 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6177 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6178 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6179 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6180 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6181 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6182 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6183 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6184 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6185 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6186 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6187 
6188 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6189 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6190 
6191 void bnxt_fill_ipv6_mask(__be32 mask[4])
6192 {
6193 	int i;
6194 
6195 	for (i = 0; i < 4; i++)
6196 		mask[i] = cpu_to_be32(~0);
6197 }
6198 
6199 static void
6200 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6201 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6202 			  struct bnxt_ntuple_filter *fltr)
6203 {
6204 	u16 rxq = fltr->base.rxq;
6205 
6206 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6207 		struct ethtool_rxfh_context *ctx;
6208 		struct bnxt_rss_ctx *rss_ctx;
6209 		struct bnxt_vnic_info *vnic;
6210 
6211 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6212 			      fltr->base.fw_vnic_id);
6213 		if (ctx) {
6214 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6215 			vnic = &rss_ctx->vnic;
6216 
6217 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6218 		}
6219 		return;
6220 	}
6221 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6222 		struct bnxt_vnic_info *vnic;
6223 		u32 enables;
6224 
6225 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6226 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6227 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6228 		req->enables |= cpu_to_le32(enables);
6229 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6230 	} else {
6231 		u32 flags;
6232 
6233 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6234 		req->flags |= cpu_to_le32(flags);
6235 		req->dst_id = cpu_to_le16(rxq);
6236 	}
6237 }
6238 
6239 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6240 				      struct bnxt_ntuple_filter *fltr)
6241 {
6242 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6243 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6244 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6245 	struct flow_keys *keys = &fltr->fkeys;
6246 	struct bnxt_l2_filter *l2_fltr;
6247 	struct bnxt_vnic_info *vnic;
6248 	int rc;
6249 
6250 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6251 	if (rc)
6252 		return rc;
6253 
6254 	l2_fltr = fltr->l2_fltr;
6255 	req->l2_filter_id = l2_fltr->base.filter_id;
6256 
6257 	if (fltr->base.flags & BNXT_ACT_DROP) {
6258 		req->flags =
6259 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6260 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6261 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6262 	} else {
6263 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6264 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6265 	}
6266 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6267 
6268 	req->ethertype = htons(ETH_P_IP);
6269 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6270 	req->ip_protocol = keys->basic.ip_proto;
6271 
6272 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6273 		req->ethertype = htons(ETH_P_IPV6);
6274 		req->ip_addr_type =
6275 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6276 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6277 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6278 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6279 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6280 	} else {
6281 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6282 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6283 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6284 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6285 	}
6286 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6287 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6288 		req->tunnel_type =
6289 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6290 	}
6291 
6292 	req->src_port = keys->ports.src;
6293 	req->src_port_mask = masks->ports.src;
6294 	req->dst_port = keys->ports.dst;
6295 	req->dst_port_mask = masks->ports.dst;
6296 
6297 	resp = hwrm_req_hold(bp, req);
6298 	rc = hwrm_req_send(bp, req);
6299 	if (!rc)
6300 		fltr->base.filter_id = resp->ntuple_filter_id;
6301 	hwrm_req_drop(bp, req);
6302 	return rc;
6303 }
6304 
6305 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6306 				     const u8 *mac_addr)
6307 {
6308 	struct bnxt_l2_filter *fltr;
6309 	struct bnxt_l2_key key;
6310 	int rc;
6311 
6312 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6313 	key.vlan = 0;
6314 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6315 	if (IS_ERR(fltr))
6316 		return PTR_ERR(fltr);
6317 
6318 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6319 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6320 	if (rc)
6321 		bnxt_del_l2_filter(bp, fltr);
6322 	else
6323 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6324 	return rc;
6325 }
6326 
6327 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6328 {
6329 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6330 
6331 	/* Any associated ntuple filters will also be cleared by firmware. */
6332 	for (i = 0; i < num_of_vnics; i++) {
6333 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6334 
6335 		for (j = 0; j < vnic->uc_filter_count; j++) {
6336 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6337 
6338 			bnxt_hwrm_l2_filter_free(bp, fltr);
6339 			bnxt_del_l2_filter(bp, fltr);
6340 		}
6341 		vnic->uc_filter_count = 0;
6342 	}
6343 }
6344 
6345 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6346 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6347 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6348 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6349 
6350 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6351 					   struct hwrm_vnic_tpa_cfg_input *req)
6352 {
6353 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6354 
6355 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6356 		return;
6357 
6358 	if (bp->vxlan_port)
6359 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6360 	if (bp->vxlan_gpe_port)
6361 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6362 	if (bp->nge_port)
6363 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6364 
6365 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6366 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6367 }
6368 
6369 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6370 			   u32 tpa_flags)
6371 {
6372 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6373 	struct hwrm_vnic_tpa_cfg_input *req;
6374 	int rc;
6375 
6376 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6377 		return 0;
6378 
6379 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6380 	if (rc)
6381 		return rc;
6382 
6383 	if (tpa_flags) {
6384 		u16 mss = bp->dev->mtu - 40;
6385 		u32 nsegs, n, segs = 0, flags;
6386 
6387 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6388 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6389 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6390 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6391 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6392 		if (tpa_flags & BNXT_FLAG_GRO)
6393 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6394 
6395 		req->flags = cpu_to_le32(flags);
6396 
6397 		req->enables =
6398 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6399 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6400 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6401 
6402 		/* Number of segs are log2 units, and first packet is not
6403 		 * included as part of this units.
6404 		 */
6405 		if (mss <= BNXT_RX_PAGE_SIZE) {
6406 			n = BNXT_RX_PAGE_SIZE / mss;
6407 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6408 		} else {
6409 			n = mss / BNXT_RX_PAGE_SIZE;
6410 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6411 				n++;
6412 			nsegs = (MAX_SKB_FRAGS - n) / n;
6413 		}
6414 
6415 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6416 			segs = MAX_TPA_SEGS_P5;
6417 			max_aggs = bp->max_tpa;
6418 		} else {
6419 			segs = ilog2(nsegs);
6420 		}
6421 		req->max_agg_segs = cpu_to_le16(segs);
6422 		req->max_aggs = cpu_to_le16(max_aggs);
6423 
6424 		req->min_agg_len = cpu_to_le32(512);
6425 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6426 	}
6427 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6428 
6429 	return hwrm_req_send(bp, req);
6430 }
6431 
6432 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6433 {
6434 	struct bnxt_ring_grp_info *grp_info;
6435 
6436 	grp_info = &bp->grp_info[ring->grp_idx];
6437 	return grp_info->cp_fw_ring_id;
6438 }
6439 
6440 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6441 {
6442 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6443 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6444 	else
6445 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6446 }
6447 
6448 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6449 {
6450 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6451 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6452 	else
6453 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6454 }
6455 
6456 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6457 {
6458 	int entries;
6459 
6460 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6461 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6462 	else
6463 		entries = HW_HASH_INDEX_SIZE;
6464 
6465 	bp->rss_indir_tbl_entries = entries;
6466 	bp->rss_indir_tbl =
6467 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6468 	if (!bp->rss_indir_tbl)
6469 		return -ENOMEM;
6470 
6471 	return 0;
6472 }
6473 
6474 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6475 				 struct ethtool_rxfh_context *rss_ctx)
6476 {
6477 	u16 max_rings, max_entries, pad, i;
6478 	u32 *rss_indir_tbl;
6479 
6480 	if (!bp->rx_nr_rings)
6481 		return;
6482 
6483 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6484 		max_rings = bp->rx_nr_rings - 1;
6485 	else
6486 		max_rings = bp->rx_nr_rings;
6487 
6488 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6489 	if (rss_ctx)
6490 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6491 	else
6492 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6493 
6494 	for (i = 0; i < max_entries; i++)
6495 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6496 
6497 	pad = bp->rss_indir_tbl_entries - max_entries;
6498 	if (pad)
6499 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6500 }
6501 
6502 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6503 {
6504 	u32 i, tbl_size, max_ring = 0;
6505 
6506 	if (!bp->rss_indir_tbl)
6507 		return 0;
6508 
6509 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6510 	for (i = 0; i < tbl_size; i++)
6511 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6512 	return max_ring;
6513 }
6514 
6515 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6516 {
6517 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6518 		if (!rx_rings)
6519 			return 0;
6520 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6521 					       BNXT_RSS_TABLE_ENTRIES_P5);
6522 	}
6523 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6524 		return 2;
6525 	return 1;
6526 }
6527 
6528 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6529 {
6530 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6531 	u16 i, j;
6532 
6533 	/* Fill the RSS indirection table with ring group ids */
6534 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6535 		if (!no_rss)
6536 			j = bp->rss_indir_tbl[i];
6537 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6538 	}
6539 }
6540 
6541 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6542 				    struct bnxt_vnic_info *vnic)
6543 {
6544 	__le16 *ring_tbl = vnic->rss_table;
6545 	struct bnxt_rx_ring_info *rxr;
6546 	u16 tbl_size, i;
6547 
6548 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6549 
6550 	for (i = 0; i < tbl_size; i++) {
6551 		u16 ring_id, j;
6552 
6553 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6554 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6555 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6556 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6557 		else
6558 			j = bp->rss_indir_tbl[i];
6559 		rxr = &bp->rx_ring[j];
6560 
6561 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6562 		*ring_tbl++ = cpu_to_le16(ring_id);
6563 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6564 		*ring_tbl++ = cpu_to_le16(ring_id);
6565 	}
6566 }
6567 
6568 static void
6569 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6570 			 struct bnxt_vnic_info *vnic)
6571 {
6572 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6573 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6574 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6575 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6576 	} else {
6577 		bnxt_fill_hw_rss_tbl(bp, vnic);
6578 	}
6579 
6580 	if (bp->rss_hash_delta) {
6581 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6582 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6583 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6584 		else
6585 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6586 	} else {
6587 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6588 	}
6589 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6590 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6591 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6592 }
6593 
6594 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6595 				  bool set_rss)
6596 {
6597 	struct hwrm_vnic_rss_cfg_input *req;
6598 	int rc;
6599 
6600 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6601 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6602 		return 0;
6603 
6604 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6605 	if (rc)
6606 		return rc;
6607 
6608 	if (set_rss)
6609 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6610 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6611 	return hwrm_req_send(bp, req);
6612 }
6613 
6614 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6615 				     struct bnxt_vnic_info *vnic, bool set_rss)
6616 {
6617 	struct hwrm_vnic_rss_cfg_input *req;
6618 	dma_addr_t ring_tbl_map;
6619 	u32 i, nr_ctxs;
6620 	int rc;
6621 
6622 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6623 	if (rc)
6624 		return rc;
6625 
6626 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6627 	if (!set_rss)
6628 		return hwrm_req_send(bp, req);
6629 
6630 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6631 	ring_tbl_map = vnic->rss_table_dma_addr;
6632 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6633 
6634 	hwrm_req_hold(bp, req);
6635 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6636 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6637 		req->ring_table_pair_index = i;
6638 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6639 		rc = hwrm_req_send(bp, req);
6640 		if (rc)
6641 			goto exit;
6642 	}
6643 
6644 exit:
6645 	hwrm_req_drop(bp, req);
6646 	return rc;
6647 }
6648 
6649 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6650 {
6651 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6652 	struct hwrm_vnic_rss_qcfg_output *resp;
6653 	struct hwrm_vnic_rss_qcfg_input *req;
6654 
6655 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6656 		return;
6657 
6658 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6659 	/* all contexts configured to same hash_type, zero always exists */
6660 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6661 	resp = hwrm_req_hold(bp, req);
6662 	if (!hwrm_req_send(bp, req)) {
6663 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6664 		bp->rss_hash_delta = 0;
6665 	}
6666 	hwrm_req_drop(bp, req);
6667 }
6668 
6669 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6670 {
6671 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6672 	struct hwrm_vnic_plcmodes_cfg_input *req;
6673 	int rc;
6674 
6675 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6676 	if (rc)
6677 		return rc;
6678 
6679 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6680 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6681 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6682 
6683 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6684 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6685 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6686 		req->enables |=
6687 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6688 		req->hds_threshold = cpu_to_le16(hds_thresh);
6689 	}
6690 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6691 	return hwrm_req_send(bp, req);
6692 }
6693 
6694 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6695 					struct bnxt_vnic_info *vnic,
6696 					u16 ctx_idx)
6697 {
6698 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6699 
6700 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6701 		return;
6702 
6703 	req->rss_cos_lb_ctx_id =
6704 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6705 
6706 	hwrm_req_send(bp, req);
6707 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6708 }
6709 
6710 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6711 {
6712 	int i, j;
6713 
6714 	for (i = 0; i < bp->nr_vnics; i++) {
6715 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6716 
6717 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6718 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6719 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6720 		}
6721 	}
6722 	bp->rsscos_nr_ctxs = 0;
6723 }
6724 
6725 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6726 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6727 {
6728 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6729 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6730 	int rc;
6731 
6732 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6733 	if (rc)
6734 		return rc;
6735 
6736 	resp = hwrm_req_hold(bp, req);
6737 	rc = hwrm_req_send(bp, req);
6738 	if (!rc)
6739 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6740 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6741 	hwrm_req_drop(bp, req);
6742 
6743 	return rc;
6744 }
6745 
6746 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6747 {
6748 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6749 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6750 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6751 }
6752 
6753 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6754 {
6755 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6756 	struct hwrm_vnic_cfg_input *req;
6757 	unsigned int ring = 0, grp_idx;
6758 	u16 def_vlan = 0;
6759 	int rc;
6760 
6761 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6762 	if (rc)
6763 		return rc;
6764 
6765 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6766 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6767 
6768 		req->default_rx_ring_id =
6769 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6770 		req->default_cmpl_ring_id =
6771 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6772 		req->enables =
6773 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6774 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6775 		goto vnic_mru;
6776 	}
6777 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6778 	/* Only RSS support for now TBD: COS & LB */
6779 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6780 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6781 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6782 					   VNIC_CFG_REQ_ENABLES_MRU);
6783 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6784 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6785 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6786 					   VNIC_CFG_REQ_ENABLES_MRU);
6787 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6788 	} else {
6789 		req->rss_rule = cpu_to_le16(0xffff);
6790 	}
6791 
6792 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6793 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6794 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6795 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6796 	} else {
6797 		req->cos_rule = cpu_to_le16(0xffff);
6798 	}
6799 
6800 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6801 		ring = 0;
6802 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6803 		ring = vnic->vnic_id - 1;
6804 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6805 		ring = bp->rx_nr_rings - 1;
6806 
6807 	grp_idx = bp->rx_ring[ring].bnapi->index;
6808 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6809 	req->lb_rule = cpu_to_le16(0xffff);
6810 vnic_mru:
6811 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6812 	req->mru = cpu_to_le16(vnic->mru);
6813 
6814 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6815 #ifdef CONFIG_BNXT_SRIOV
6816 	if (BNXT_VF(bp))
6817 		def_vlan = bp->vf.vlan;
6818 #endif
6819 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6820 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6821 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6822 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6823 
6824 	return hwrm_req_send(bp, req);
6825 }
6826 
6827 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6828 				    struct bnxt_vnic_info *vnic)
6829 {
6830 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6831 		struct hwrm_vnic_free_input *req;
6832 
6833 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6834 			return;
6835 
6836 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6837 
6838 		hwrm_req_send(bp, req);
6839 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6840 	}
6841 }
6842 
6843 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6844 {
6845 	u16 i;
6846 
6847 	for (i = 0; i < bp->nr_vnics; i++)
6848 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6849 }
6850 
6851 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6852 			 unsigned int start_rx_ring_idx,
6853 			 unsigned int nr_rings)
6854 {
6855 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6856 	struct hwrm_vnic_alloc_output *resp;
6857 	struct hwrm_vnic_alloc_input *req;
6858 	int rc;
6859 
6860 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6861 	if (rc)
6862 		return rc;
6863 
6864 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6865 		goto vnic_no_ring_grps;
6866 
6867 	/* map ring groups to this vnic */
6868 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6869 		grp_idx = bp->rx_ring[i].bnapi->index;
6870 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6871 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6872 				   j, nr_rings);
6873 			break;
6874 		}
6875 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6876 	}
6877 
6878 vnic_no_ring_grps:
6879 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6880 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6881 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6882 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6883 
6884 	resp = hwrm_req_hold(bp, req);
6885 	rc = hwrm_req_send(bp, req);
6886 	if (!rc)
6887 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6888 	hwrm_req_drop(bp, req);
6889 	return rc;
6890 }
6891 
6892 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6893 {
6894 	struct hwrm_vnic_qcaps_output *resp;
6895 	struct hwrm_vnic_qcaps_input *req;
6896 	int rc;
6897 
6898 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6899 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6900 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6901 	if (bp->hwrm_spec_code < 0x10600)
6902 		return 0;
6903 
6904 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6905 	if (rc)
6906 		return rc;
6907 
6908 	resp = hwrm_req_hold(bp, req);
6909 	rc = hwrm_req_send(bp, req);
6910 	if (!rc) {
6911 		u32 flags = le32_to_cpu(resp->flags);
6912 
6913 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6914 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6915 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6916 		if (flags &
6917 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6918 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6919 
6920 		/* Older P5 fw before EXT_HW_STATS support did not set
6921 		 * VLAN_STRIP_CAP properly.
6922 		 */
6923 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6924 		    (BNXT_CHIP_P5(bp) &&
6925 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6926 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6927 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6928 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6929 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6930 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6931 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6932 		if (bp->max_tpa_v2) {
6933 			if (BNXT_CHIP_P5(bp))
6934 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6935 			else
6936 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6937 		}
6938 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6939 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6940 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6941 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6942 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6943 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6944 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6945 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6946 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6947 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6948 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6949 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6950 	}
6951 	hwrm_req_drop(bp, req);
6952 	return rc;
6953 }
6954 
6955 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6956 {
6957 	struct hwrm_ring_grp_alloc_output *resp;
6958 	struct hwrm_ring_grp_alloc_input *req;
6959 	int rc;
6960 	u16 i;
6961 
6962 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6963 		return 0;
6964 
6965 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6966 	if (rc)
6967 		return rc;
6968 
6969 	resp = hwrm_req_hold(bp, req);
6970 	for (i = 0; i < bp->rx_nr_rings; i++) {
6971 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6972 
6973 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6974 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6975 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6976 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6977 
6978 		rc = hwrm_req_send(bp, req);
6979 
6980 		if (rc)
6981 			break;
6982 
6983 		bp->grp_info[grp_idx].fw_grp_id =
6984 			le32_to_cpu(resp->ring_group_id);
6985 	}
6986 	hwrm_req_drop(bp, req);
6987 	return rc;
6988 }
6989 
6990 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6991 {
6992 	struct hwrm_ring_grp_free_input *req;
6993 	u16 i;
6994 
6995 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6996 		return;
6997 
6998 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6999 		return;
7000 
7001 	hwrm_req_hold(bp, req);
7002 	for (i = 0; i < bp->cp_nr_rings; i++) {
7003 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7004 			continue;
7005 		req->ring_group_id =
7006 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7007 
7008 		hwrm_req_send(bp, req);
7009 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7010 	}
7011 	hwrm_req_drop(bp, req);
7012 }
7013 
7014 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7015 				       struct hwrm_ring_alloc_input *req,
7016 				       struct bnxt_ring_struct *ring)
7017 {
7018 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7019 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7020 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7021 
7022 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7023 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7024 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7025 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7026 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7027 	} else {
7028 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7029 		if (NET_IP_ALIGN == 2)
7030 			req->flags =
7031 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7032 	}
7033 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7034 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7035 	req->enables |= cpu_to_le32(enables);
7036 }
7037 
7038 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7039 				    struct bnxt_ring_struct *ring,
7040 				    u32 ring_type, u32 map_index)
7041 {
7042 	struct hwrm_ring_alloc_output *resp;
7043 	struct hwrm_ring_alloc_input *req;
7044 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7045 	struct bnxt_ring_grp_info *grp_info;
7046 	int rc, err = 0;
7047 	u16 ring_id;
7048 
7049 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7050 	if (rc)
7051 		goto exit;
7052 
7053 	req->enables = 0;
7054 	if (rmem->nr_pages > 1) {
7055 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7056 		/* Page size is in log2 units */
7057 		req->page_size = BNXT_PAGE_SHIFT;
7058 		req->page_tbl_depth = 1;
7059 	} else {
7060 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7061 	}
7062 	req->fbo = 0;
7063 	/* Association of ring index with doorbell index and MSIX number */
7064 	req->logical_id = cpu_to_le16(map_index);
7065 
7066 	switch (ring_type) {
7067 	case HWRM_RING_ALLOC_TX: {
7068 		struct bnxt_tx_ring_info *txr;
7069 		u16 flags = 0;
7070 
7071 		txr = container_of(ring, struct bnxt_tx_ring_info,
7072 				   tx_ring_struct);
7073 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7074 		/* Association of transmit ring with completion ring */
7075 		grp_info = &bp->grp_info[ring->grp_idx];
7076 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7077 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7078 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7079 		req->queue_id = cpu_to_le16(ring->queue_id);
7080 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7081 			req->cmpl_coal_cnt =
7082 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7083 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7084 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7085 		req->flags = cpu_to_le16(flags);
7086 		break;
7087 	}
7088 	case HWRM_RING_ALLOC_RX:
7089 	case HWRM_RING_ALLOC_AGG:
7090 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7091 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7092 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7093 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7094 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7095 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7096 		break;
7097 	case HWRM_RING_ALLOC_CMPL:
7098 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7099 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7100 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7101 			/* Association of cp ring with nq */
7102 			grp_info = &bp->grp_info[map_index];
7103 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7104 			req->cq_handle = cpu_to_le64(ring->handle);
7105 			req->enables |= cpu_to_le32(
7106 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7107 		} else {
7108 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7109 		}
7110 		break;
7111 	case HWRM_RING_ALLOC_NQ:
7112 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7113 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7114 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7115 		break;
7116 	default:
7117 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7118 			   ring_type);
7119 		return -1;
7120 	}
7121 
7122 	resp = hwrm_req_hold(bp, req);
7123 	rc = hwrm_req_send(bp, req);
7124 	err = le16_to_cpu(resp->error_code);
7125 	ring_id = le16_to_cpu(resp->ring_id);
7126 	hwrm_req_drop(bp, req);
7127 
7128 exit:
7129 	if (rc || err) {
7130 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7131 			   ring_type, rc, err);
7132 		return -EIO;
7133 	}
7134 	ring->fw_ring_id = ring_id;
7135 	return rc;
7136 }
7137 
7138 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7139 {
7140 	int rc;
7141 
7142 	if (BNXT_PF(bp)) {
7143 		struct hwrm_func_cfg_input *req;
7144 
7145 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7146 		if (rc)
7147 			return rc;
7148 
7149 		req->fid = cpu_to_le16(0xffff);
7150 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7151 		req->async_event_cr = cpu_to_le16(idx);
7152 		return hwrm_req_send(bp, req);
7153 	} else {
7154 		struct hwrm_func_vf_cfg_input *req;
7155 
7156 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7157 		if (rc)
7158 			return rc;
7159 
7160 		req->enables =
7161 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7162 		req->async_event_cr = cpu_to_le16(idx);
7163 		return hwrm_req_send(bp, req);
7164 	}
7165 }
7166 
7167 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7168 			     u32 ring_type)
7169 {
7170 	switch (ring_type) {
7171 	case HWRM_RING_ALLOC_TX:
7172 		db->db_ring_mask = bp->tx_ring_mask;
7173 		break;
7174 	case HWRM_RING_ALLOC_RX:
7175 		db->db_ring_mask = bp->rx_ring_mask;
7176 		break;
7177 	case HWRM_RING_ALLOC_AGG:
7178 		db->db_ring_mask = bp->rx_agg_ring_mask;
7179 		break;
7180 	case HWRM_RING_ALLOC_CMPL:
7181 	case HWRM_RING_ALLOC_NQ:
7182 		db->db_ring_mask = bp->cp_ring_mask;
7183 		break;
7184 	}
7185 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7186 		db->db_epoch_mask = db->db_ring_mask + 1;
7187 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7188 	}
7189 }
7190 
7191 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7192 			u32 map_idx, u32 xid)
7193 {
7194 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7195 		switch (ring_type) {
7196 		case HWRM_RING_ALLOC_TX:
7197 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7198 			break;
7199 		case HWRM_RING_ALLOC_RX:
7200 		case HWRM_RING_ALLOC_AGG:
7201 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7202 			break;
7203 		case HWRM_RING_ALLOC_CMPL:
7204 			db->db_key64 = DBR_PATH_L2;
7205 			break;
7206 		case HWRM_RING_ALLOC_NQ:
7207 			db->db_key64 = DBR_PATH_L2;
7208 			break;
7209 		}
7210 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7211 
7212 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7213 			db->db_key64 |= DBR_VALID;
7214 
7215 		db->doorbell = bp->bar1 + bp->db_offset;
7216 	} else {
7217 		db->doorbell = bp->bar1 + map_idx * 0x80;
7218 		switch (ring_type) {
7219 		case HWRM_RING_ALLOC_TX:
7220 			db->db_key32 = DB_KEY_TX;
7221 			break;
7222 		case HWRM_RING_ALLOC_RX:
7223 		case HWRM_RING_ALLOC_AGG:
7224 			db->db_key32 = DB_KEY_RX;
7225 			break;
7226 		case HWRM_RING_ALLOC_CMPL:
7227 			db->db_key32 = DB_KEY_CP;
7228 			break;
7229 		}
7230 	}
7231 	bnxt_set_db_mask(bp, db, ring_type);
7232 }
7233 
7234 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7235 				   struct bnxt_rx_ring_info *rxr)
7236 {
7237 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7238 	struct bnxt_napi *bnapi = rxr->bnapi;
7239 	u32 type = HWRM_RING_ALLOC_RX;
7240 	u32 map_idx = bnapi->index;
7241 	int rc;
7242 
7243 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7244 	if (rc)
7245 		return rc;
7246 
7247 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7248 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7249 
7250 	return 0;
7251 }
7252 
7253 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7254 				       struct bnxt_rx_ring_info *rxr)
7255 {
7256 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7257 	u32 type = HWRM_RING_ALLOC_AGG;
7258 	u32 grp_idx = ring->grp_idx;
7259 	u32 map_idx;
7260 	int rc;
7261 
7262 	map_idx = grp_idx + bp->rx_nr_rings;
7263 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7264 	if (rc)
7265 		return rc;
7266 
7267 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7268 		    ring->fw_ring_id);
7269 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7270 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7271 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7272 
7273 	return 0;
7274 }
7275 
7276 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7277 				      struct bnxt_cp_ring_info *cpr)
7278 {
7279 	const u32 type = HWRM_RING_ALLOC_CMPL;
7280 	struct bnxt_napi *bnapi = cpr->bnapi;
7281 	struct bnxt_ring_struct *ring;
7282 	u32 map_idx = bnapi->index;
7283 	int rc;
7284 
7285 	ring = &cpr->cp_ring_struct;
7286 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7287 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7288 	if (rc)
7289 		return rc;
7290 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7291 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7292 	return 0;
7293 }
7294 
7295 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7296 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7297 {
7298 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7299 	const u32 type = HWRM_RING_ALLOC_TX;
7300 	int rc;
7301 
7302 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7303 	if (rc)
7304 		return rc;
7305 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7306 	return 0;
7307 }
7308 
7309 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7310 {
7311 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7312 	int i, rc = 0;
7313 	u32 type;
7314 
7315 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7316 		type = HWRM_RING_ALLOC_NQ;
7317 	else
7318 		type = HWRM_RING_ALLOC_CMPL;
7319 	for (i = 0; i < bp->cp_nr_rings; i++) {
7320 		struct bnxt_napi *bnapi = bp->bnapi[i];
7321 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7322 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7323 		u32 map_idx = ring->map_idx;
7324 		unsigned int vector;
7325 
7326 		vector = bp->irq_tbl[map_idx].vector;
7327 		disable_irq_nosync(vector);
7328 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7329 		if (rc) {
7330 			enable_irq(vector);
7331 			goto err_out;
7332 		}
7333 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7334 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7335 		enable_irq(vector);
7336 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7337 
7338 		if (!i) {
7339 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7340 			if (rc)
7341 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7342 		}
7343 	}
7344 
7345 	for (i = 0; i < bp->tx_nr_rings; i++) {
7346 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7347 
7348 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7349 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7350 			if (rc)
7351 				goto err_out;
7352 		}
7353 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7354 		if (rc)
7355 			goto err_out;
7356 	}
7357 
7358 	for (i = 0; i < bp->rx_nr_rings; i++) {
7359 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7360 
7361 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7362 		if (rc)
7363 			goto err_out;
7364 		/* If we have agg rings, post agg buffers first. */
7365 		if (!agg_rings)
7366 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7367 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7368 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7369 			if (rc)
7370 				goto err_out;
7371 		}
7372 	}
7373 
7374 	if (agg_rings) {
7375 		for (i = 0; i < bp->rx_nr_rings; i++) {
7376 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7377 			if (rc)
7378 				goto err_out;
7379 		}
7380 	}
7381 err_out:
7382 	return rc;
7383 }
7384 
7385 static void bnxt_cancel_dim(struct bnxt *bp)
7386 {
7387 	int i;
7388 
7389 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7390 	 * if NAPI is enabled.
7391 	 */
7392 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7393 		return;
7394 
7395 	/* Make sure NAPI sees that the VNIC is disabled */
7396 	synchronize_net();
7397 	for (i = 0; i < bp->rx_nr_rings; i++) {
7398 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7399 		struct bnxt_napi *bnapi = rxr->bnapi;
7400 
7401 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7402 	}
7403 }
7404 
7405 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7406 				   struct bnxt_ring_struct *ring,
7407 				   u32 ring_type, int cmpl_ring_id)
7408 {
7409 	struct hwrm_ring_free_output *resp;
7410 	struct hwrm_ring_free_input *req;
7411 	u16 error_code = 0;
7412 	int rc;
7413 
7414 	if (BNXT_NO_FW_ACCESS(bp))
7415 		return 0;
7416 
7417 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7418 	if (rc)
7419 		goto exit;
7420 
7421 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7422 	req->ring_type = ring_type;
7423 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7424 
7425 	resp = hwrm_req_hold(bp, req);
7426 	rc = hwrm_req_send(bp, req);
7427 	error_code = le16_to_cpu(resp->error_code);
7428 	hwrm_req_drop(bp, req);
7429 exit:
7430 	if (rc || error_code) {
7431 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7432 			   ring_type, rc, error_code);
7433 		return -EIO;
7434 	}
7435 	return 0;
7436 }
7437 
7438 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7439 				   struct bnxt_tx_ring_info *txr,
7440 				   bool close_path)
7441 {
7442 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7443 	u32 cmpl_ring_id;
7444 
7445 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7446 		return;
7447 
7448 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7449 		       INVALID_HW_RING_ID;
7450 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7451 				cmpl_ring_id);
7452 	ring->fw_ring_id = INVALID_HW_RING_ID;
7453 }
7454 
7455 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7456 				   struct bnxt_rx_ring_info *rxr,
7457 				   bool close_path)
7458 {
7459 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7460 	u32 grp_idx = rxr->bnapi->index;
7461 	u32 cmpl_ring_id;
7462 
7463 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7464 		return;
7465 
7466 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7467 	hwrm_ring_free_send_msg(bp, ring,
7468 				RING_FREE_REQ_RING_TYPE_RX,
7469 				close_path ? cmpl_ring_id :
7470 				INVALID_HW_RING_ID);
7471 	ring->fw_ring_id = INVALID_HW_RING_ID;
7472 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7473 }
7474 
7475 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7476 				       struct bnxt_rx_ring_info *rxr,
7477 				       bool close_path)
7478 {
7479 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7480 	u32 grp_idx = rxr->bnapi->index;
7481 	u32 type, cmpl_ring_id;
7482 
7483 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7484 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7485 	else
7486 		type = RING_FREE_REQ_RING_TYPE_RX;
7487 
7488 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7489 		return;
7490 
7491 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7492 	hwrm_ring_free_send_msg(bp, ring, type,
7493 				close_path ? cmpl_ring_id :
7494 				INVALID_HW_RING_ID);
7495 	ring->fw_ring_id = INVALID_HW_RING_ID;
7496 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7497 }
7498 
7499 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7500 				   struct bnxt_cp_ring_info *cpr)
7501 {
7502 	struct bnxt_ring_struct *ring;
7503 
7504 	ring = &cpr->cp_ring_struct;
7505 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7506 		return;
7507 
7508 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7509 				INVALID_HW_RING_ID);
7510 	ring->fw_ring_id = INVALID_HW_RING_ID;
7511 }
7512 
7513 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7514 {
7515 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7516 	int i, size = ring->ring_mem.page_size;
7517 
7518 	cpr->cp_raw_cons = 0;
7519 	cpr->toggle = 0;
7520 
7521 	for (i = 0; i < bp->cp_nr_pages; i++)
7522 		if (cpr->cp_desc_ring[i])
7523 			memset(cpr->cp_desc_ring[i], 0, size);
7524 }
7525 
7526 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7527 {
7528 	u32 type;
7529 	int i;
7530 
7531 	if (!bp->bnapi)
7532 		return;
7533 
7534 	for (i = 0; i < bp->tx_nr_rings; i++)
7535 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7536 
7537 	bnxt_cancel_dim(bp);
7538 	for (i = 0; i < bp->rx_nr_rings; i++) {
7539 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7540 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7541 	}
7542 
7543 	/* The completion rings are about to be freed.  After that the
7544 	 * IRQ doorbell will not work anymore.  So we need to disable
7545 	 * IRQ here.
7546 	 */
7547 	bnxt_disable_int_sync(bp);
7548 
7549 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7550 		type = RING_FREE_REQ_RING_TYPE_NQ;
7551 	else
7552 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7553 	for (i = 0; i < bp->cp_nr_rings; i++) {
7554 		struct bnxt_napi *bnapi = bp->bnapi[i];
7555 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7556 		struct bnxt_ring_struct *ring;
7557 		int j;
7558 
7559 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7560 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7561 
7562 		ring = &cpr->cp_ring_struct;
7563 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7564 			hwrm_ring_free_send_msg(bp, ring, type,
7565 						INVALID_HW_RING_ID);
7566 			ring->fw_ring_id = INVALID_HW_RING_ID;
7567 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7568 		}
7569 	}
7570 }
7571 
7572 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7573 			     bool shared);
7574 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7575 			   bool shared);
7576 
7577 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7578 {
7579 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7580 	struct hwrm_func_qcfg_output *resp;
7581 	struct hwrm_func_qcfg_input *req;
7582 	int rc;
7583 
7584 	if (bp->hwrm_spec_code < 0x10601)
7585 		return 0;
7586 
7587 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7588 	if (rc)
7589 		return rc;
7590 
7591 	req->fid = cpu_to_le16(0xffff);
7592 	resp = hwrm_req_hold(bp, req);
7593 	rc = hwrm_req_send(bp, req);
7594 	if (rc) {
7595 		hwrm_req_drop(bp, req);
7596 		return rc;
7597 	}
7598 
7599 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7600 	if (BNXT_NEW_RM(bp)) {
7601 		u16 cp, stats;
7602 
7603 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7604 		hw_resc->resv_hw_ring_grps =
7605 			le32_to_cpu(resp->alloc_hw_ring_grps);
7606 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7607 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7608 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7609 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7610 		hw_resc->resv_irqs = cp;
7611 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7612 			int rx = hw_resc->resv_rx_rings;
7613 			int tx = hw_resc->resv_tx_rings;
7614 
7615 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7616 				rx >>= 1;
7617 			if (cp < (rx + tx)) {
7618 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7619 				if (rc)
7620 					goto get_rings_exit;
7621 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7622 					rx <<= 1;
7623 				hw_resc->resv_rx_rings = rx;
7624 				hw_resc->resv_tx_rings = tx;
7625 			}
7626 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7627 			hw_resc->resv_hw_ring_grps = rx;
7628 		}
7629 		hw_resc->resv_cp_rings = cp;
7630 		hw_resc->resv_stat_ctxs = stats;
7631 	}
7632 get_rings_exit:
7633 	hwrm_req_drop(bp, req);
7634 	return rc;
7635 }
7636 
7637 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7638 {
7639 	struct hwrm_func_qcfg_output *resp;
7640 	struct hwrm_func_qcfg_input *req;
7641 	int rc;
7642 
7643 	if (bp->hwrm_spec_code < 0x10601)
7644 		return 0;
7645 
7646 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7647 	if (rc)
7648 		return rc;
7649 
7650 	req->fid = cpu_to_le16(fid);
7651 	resp = hwrm_req_hold(bp, req);
7652 	rc = hwrm_req_send(bp, req);
7653 	if (!rc)
7654 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7655 
7656 	hwrm_req_drop(bp, req);
7657 	return rc;
7658 }
7659 
7660 static bool bnxt_rfs_supported(struct bnxt *bp);
7661 
7662 static struct hwrm_func_cfg_input *
7663 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7664 {
7665 	struct hwrm_func_cfg_input *req;
7666 	u32 enables = 0;
7667 
7668 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7669 		return NULL;
7670 
7671 	req->fid = cpu_to_le16(0xffff);
7672 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7673 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7674 	if (BNXT_NEW_RM(bp)) {
7675 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7676 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7677 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7678 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7679 			enables |= hwr->cp_p5 ?
7680 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7681 		} else {
7682 			enables |= hwr->cp ?
7683 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7684 			enables |= hwr->grp ?
7685 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7686 		}
7687 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7688 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7689 					  0;
7690 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7691 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7692 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7693 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7694 			req->num_msix = cpu_to_le16(hwr->cp);
7695 		} else {
7696 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7697 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7698 		}
7699 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7700 		req->num_vnics = cpu_to_le16(hwr->vnic);
7701 	}
7702 	req->enables = cpu_to_le32(enables);
7703 	return req;
7704 }
7705 
7706 static struct hwrm_func_vf_cfg_input *
7707 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7708 {
7709 	struct hwrm_func_vf_cfg_input *req;
7710 	u32 enables = 0;
7711 
7712 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7713 		return NULL;
7714 
7715 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7716 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7717 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7718 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7719 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7720 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7721 		enables |= hwr->cp_p5 ?
7722 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7723 	} else {
7724 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7725 		enables |= hwr->grp ?
7726 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7727 	}
7728 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7729 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7730 
7731 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7732 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7733 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7734 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7735 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7736 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7737 	} else {
7738 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7739 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7740 	}
7741 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7742 	req->num_vnics = cpu_to_le16(hwr->vnic);
7743 
7744 	req->enables = cpu_to_le32(enables);
7745 	return req;
7746 }
7747 
7748 static int
7749 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7750 {
7751 	struct hwrm_func_cfg_input *req;
7752 	int rc;
7753 
7754 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7755 	if (!req)
7756 		return -ENOMEM;
7757 
7758 	if (!req->enables) {
7759 		hwrm_req_drop(bp, req);
7760 		return 0;
7761 	}
7762 
7763 	rc = hwrm_req_send(bp, req);
7764 	if (rc)
7765 		return rc;
7766 
7767 	if (bp->hwrm_spec_code < 0x10601)
7768 		bp->hw_resc.resv_tx_rings = hwr->tx;
7769 
7770 	return bnxt_hwrm_get_rings(bp);
7771 }
7772 
7773 static int
7774 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7775 {
7776 	struct hwrm_func_vf_cfg_input *req;
7777 	int rc;
7778 
7779 	if (!BNXT_NEW_RM(bp)) {
7780 		bp->hw_resc.resv_tx_rings = hwr->tx;
7781 		return 0;
7782 	}
7783 
7784 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7785 	if (!req)
7786 		return -ENOMEM;
7787 
7788 	rc = hwrm_req_send(bp, req);
7789 	if (rc)
7790 		return rc;
7791 
7792 	return bnxt_hwrm_get_rings(bp);
7793 }
7794 
7795 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7796 {
7797 	if (BNXT_PF(bp))
7798 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7799 	else
7800 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7801 }
7802 
7803 int bnxt_nq_rings_in_use(struct bnxt *bp)
7804 {
7805 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7806 }
7807 
7808 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7809 {
7810 	int cp;
7811 
7812 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7813 		return bnxt_nq_rings_in_use(bp);
7814 
7815 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7816 	return cp;
7817 }
7818 
7819 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7820 {
7821 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7822 }
7823 
7824 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7825 {
7826 	if (!hwr->grp)
7827 		return 0;
7828 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7829 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7830 
7831 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7832 			rss_ctx *= hwr->vnic;
7833 		return rss_ctx;
7834 	}
7835 	if (BNXT_VF(bp))
7836 		return BNXT_VF_MAX_RSS_CTX;
7837 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7838 		return hwr->grp + 1;
7839 	return 1;
7840 }
7841 
7842 /* Check if a default RSS map needs to be setup.  This function is only
7843  * used on older firmware that does not require reserving RX rings.
7844  */
7845 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7846 {
7847 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7848 
7849 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7850 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7851 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7852 		if (!netif_is_rxfh_configured(bp->dev))
7853 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7854 	}
7855 }
7856 
7857 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7858 {
7859 	if (bp->flags & BNXT_FLAG_RFS) {
7860 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7861 			return 2 + bp->num_rss_ctx;
7862 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7863 			return rx_rings + 1;
7864 	}
7865 	return 1;
7866 }
7867 
7868 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7869 {
7870 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7871 	int cp = bnxt_cp_rings_in_use(bp);
7872 	int nq = bnxt_nq_rings_in_use(bp);
7873 	int rx = bp->rx_nr_rings, stat;
7874 	int vnic, grp = rx;
7875 
7876 	/* Old firmware does not need RX ring reservations but we still
7877 	 * need to setup a default RSS map when needed.  With new firmware
7878 	 * we go through RX ring reservations first and then set up the
7879 	 * RSS map for the successfully reserved RX rings when needed.
7880 	 */
7881 	if (!BNXT_NEW_RM(bp))
7882 		bnxt_check_rss_tbl_no_rmgr(bp);
7883 
7884 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7885 	    bp->hwrm_spec_code >= 0x10601)
7886 		return true;
7887 
7888 	if (!BNXT_NEW_RM(bp))
7889 		return false;
7890 
7891 	vnic = bnxt_get_total_vnics(bp, rx);
7892 
7893 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7894 		rx <<= 1;
7895 	stat = bnxt_get_func_stat_ctxs(bp);
7896 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7897 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7898 	    (hw_resc->resv_hw_ring_grps != grp &&
7899 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7900 		return true;
7901 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7902 	    hw_resc->resv_irqs != nq)
7903 		return true;
7904 	return false;
7905 }
7906 
7907 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7908 {
7909 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7910 
7911 	hwr->tx = hw_resc->resv_tx_rings;
7912 	if (BNXT_NEW_RM(bp)) {
7913 		hwr->rx = hw_resc->resv_rx_rings;
7914 		hwr->cp = hw_resc->resv_irqs;
7915 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7916 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7917 		hwr->grp = hw_resc->resv_hw_ring_grps;
7918 		hwr->vnic = hw_resc->resv_vnics;
7919 		hwr->stat = hw_resc->resv_stat_ctxs;
7920 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7921 	}
7922 }
7923 
7924 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7925 {
7926 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7927 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7928 }
7929 
7930 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7931 
7932 static int __bnxt_reserve_rings(struct bnxt *bp)
7933 {
7934 	struct bnxt_hw_rings hwr = {0};
7935 	int rx_rings, old_rx_rings, rc;
7936 	int cp = bp->cp_nr_rings;
7937 	int ulp_msix = 0;
7938 	bool sh = false;
7939 	int tx_cp;
7940 
7941 	if (!bnxt_need_reserve_rings(bp))
7942 		return 0;
7943 
7944 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7945 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7946 		if (!ulp_msix)
7947 			bnxt_set_ulp_stat_ctxs(bp, 0);
7948 
7949 		if (ulp_msix > bp->ulp_num_msix_want)
7950 			ulp_msix = bp->ulp_num_msix_want;
7951 		hwr.cp = cp + ulp_msix;
7952 	} else {
7953 		hwr.cp = bnxt_nq_rings_in_use(bp);
7954 	}
7955 
7956 	hwr.tx = bp->tx_nr_rings;
7957 	hwr.rx = bp->rx_nr_rings;
7958 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7959 		sh = true;
7960 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7961 		hwr.cp_p5 = hwr.rx + hwr.tx;
7962 
7963 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7964 
7965 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7966 		hwr.rx <<= 1;
7967 	hwr.grp = bp->rx_nr_rings;
7968 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7969 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7970 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7971 
7972 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7973 	if (rc)
7974 		return rc;
7975 
7976 	bnxt_copy_reserved_rings(bp, &hwr);
7977 
7978 	rx_rings = hwr.rx;
7979 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7980 		if (hwr.rx >= 2) {
7981 			rx_rings = hwr.rx >> 1;
7982 		} else {
7983 			if (netif_running(bp->dev))
7984 				return -ENOMEM;
7985 
7986 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7987 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7988 			bp->dev->hw_features &= ~NETIF_F_LRO;
7989 			bp->dev->features &= ~NETIF_F_LRO;
7990 			bnxt_set_ring_params(bp);
7991 		}
7992 	}
7993 	rx_rings = min_t(int, rx_rings, hwr.grp);
7994 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7995 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7996 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7997 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7998 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7999 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
8000 		hwr.rx = rx_rings << 1;
8001 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
8002 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
8003 	bp->tx_nr_rings = hwr.tx;
8004 
8005 	/* If we cannot reserve all the RX rings, reset the RSS map only
8006 	 * if absolutely necessary
8007 	 */
8008 	if (rx_rings != bp->rx_nr_rings) {
8009 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8010 			    rx_rings, bp->rx_nr_rings);
8011 		if (netif_is_rxfh_configured(bp->dev) &&
8012 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8013 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8014 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8015 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8016 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8017 		}
8018 	}
8019 	bp->rx_nr_rings = rx_rings;
8020 	bp->cp_nr_rings = hwr.cp;
8021 
8022 	if (!bnxt_rings_ok(bp, &hwr))
8023 		return -ENOMEM;
8024 
8025 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8026 	    !netif_is_rxfh_configured(bp->dev))
8027 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8028 
8029 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8030 		int resv_msix, resv_ctx, ulp_ctxs;
8031 		struct bnxt_hw_resc *hw_resc;
8032 
8033 		hw_resc = &bp->hw_resc;
8034 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8035 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8036 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8037 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8038 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8039 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8040 	}
8041 
8042 	return rc;
8043 }
8044 
8045 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8046 {
8047 	struct hwrm_func_vf_cfg_input *req;
8048 	u32 flags;
8049 
8050 	if (!BNXT_NEW_RM(bp))
8051 		return 0;
8052 
8053 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8054 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8055 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8056 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8057 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8058 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8059 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8060 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8061 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8062 
8063 	req->flags = cpu_to_le32(flags);
8064 	return hwrm_req_send_silent(bp, req);
8065 }
8066 
8067 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8068 {
8069 	struct hwrm_func_cfg_input *req;
8070 	u32 flags;
8071 
8072 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8073 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8074 	if (BNXT_NEW_RM(bp)) {
8075 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8076 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8077 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8078 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8079 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8080 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8081 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8082 		else
8083 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8084 	}
8085 
8086 	req->flags = cpu_to_le32(flags);
8087 	return hwrm_req_send_silent(bp, req);
8088 }
8089 
8090 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8091 {
8092 	if (bp->hwrm_spec_code < 0x10801)
8093 		return 0;
8094 
8095 	if (BNXT_PF(bp))
8096 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8097 
8098 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8099 }
8100 
8101 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8102 {
8103 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8104 	struct hwrm_ring_aggint_qcaps_output *resp;
8105 	struct hwrm_ring_aggint_qcaps_input *req;
8106 	int rc;
8107 
8108 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8109 	coal_cap->num_cmpl_dma_aggr_max = 63;
8110 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8111 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8112 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8113 	coal_cap->int_lat_tmr_min_max = 65535;
8114 	coal_cap->int_lat_tmr_max_max = 65535;
8115 	coal_cap->num_cmpl_aggr_int_max = 65535;
8116 	coal_cap->timer_units = 80;
8117 
8118 	if (bp->hwrm_spec_code < 0x10902)
8119 		return;
8120 
8121 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8122 		return;
8123 
8124 	resp = hwrm_req_hold(bp, req);
8125 	rc = hwrm_req_send_silent(bp, req);
8126 	if (!rc) {
8127 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8128 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8129 		coal_cap->num_cmpl_dma_aggr_max =
8130 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8131 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8132 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8133 		coal_cap->cmpl_aggr_dma_tmr_max =
8134 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8135 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8136 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8137 		coal_cap->int_lat_tmr_min_max =
8138 			le16_to_cpu(resp->int_lat_tmr_min_max);
8139 		coal_cap->int_lat_tmr_max_max =
8140 			le16_to_cpu(resp->int_lat_tmr_max_max);
8141 		coal_cap->num_cmpl_aggr_int_max =
8142 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8143 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8144 	}
8145 	hwrm_req_drop(bp, req);
8146 }
8147 
8148 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8149 {
8150 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8151 
8152 	return usec * 1000 / coal_cap->timer_units;
8153 }
8154 
8155 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8156 	struct bnxt_coal *hw_coal,
8157 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8158 {
8159 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8160 	u16 val, tmr, max, flags = hw_coal->flags;
8161 	u32 cmpl_params = coal_cap->cmpl_params;
8162 
8163 	max = hw_coal->bufs_per_record * 128;
8164 	if (hw_coal->budget)
8165 		max = hw_coal->bufs_per_record * hw_coal->budget;
8166 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8167 
8168 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8169 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8170 
8171 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8172 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8173 
8174 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8175 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8176 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8177 
8178 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8179 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8180 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8181 
8182 	/* min timer set to 1/2 of interrupt timer */
8183 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8184 		val = tmr / 2;
8185 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8186 		req->int_lat_tmr_min = cpu_to_le16(val);
8187 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8188 	}
8189 
8190 	/* buf timer set to 1/4 of interrupt timer */
8191 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8192 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8193 
8194 	if (cmpl_params &
8195 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8196 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8197 		val = clamp_t(u16, tmr, 1,
8198 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8199 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8200 		req->enables |=
8201 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8202 	}
8203 
8204 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8205 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8206 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8207 	req->flags = cpu_to_le16(flags);
8208 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8209 }
8210 
8211 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8212 				   struct bnxt_coal *hw_coal)
8213 {
8214 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8215 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8216 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8217 	u32 nq_params = coal_cap->nq_params;
8218 	u16 tmr;
8219 	int rc;
8220 
8221 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8222 		return 0;
8223 
8224 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8225 	if (rc)
8226 		return rc;
8227 
8228 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8229 	req->flags =
8230 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8231 
8232 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8233 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8234 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8235 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8236 	return hwrm_req_send(bp, req);
8237 }
8238 
8239 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8240 {
8241 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8242 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8243 	struct bnxt_coal coal;
8244 	int rc;
8245 
8246 	/* Tick values in micro seconds.
8247 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8248 	 */
8249 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8250 
8251 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8252 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8253 
8254 	if (!bnapi->rx_ring)
8255 		return -ENODEV;
8256 
8257 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8258 	if (rc)
8259 		return rc;
8260 
8261 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8262 
8263 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8264 
8265 	return hwrm_req_send(bp, req_rx);
8266 }
8267 
8268 static int
8269 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8270 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8271 {
8272 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8273 
8274 	req->ring_id = cpu_to_le16(ring_id);
8275 	return hwrm_req_send(bp, req);
8276 }
8277 
8278 static int
8279 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8280 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8281 {
8282 	struct bnxt_tx_ring_info *txr;
8283 	int i, rc;
8284 
8285 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8286 		u16 ring_id;
8287 
8288 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8289 		req->ring_id = cpu_to_le16(ring_id);
8290 		rc = hwrm_req_send(bp, req);
8291 		if (rc)
8292 			return rc;
8293 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8294 			return 0;
8295 	}
8296 	return 0;
8297 }
8298 
8299 int bnxt_hwrm_set_coal(struct bnxt *bp)
8300 {
8301 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8302 	int i, rc;
8303 
8304 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8305 	if (rc)
8306 		return rc;
8307 
8308 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8309 	if (rc) {
8310 		hwrm_req_drop(bp, req_rx);
8311 		return rc;
8312 	}
8313 
8314 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8315 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8316 
8317 	hwrm_req_hold(bp, req_rx);
8318 	hwrm_req_hold(bp, req_tx);
8319 	for (i = 0; i < bp->cp_nr_rings; i++) {
8320 		struct bnxt_napi *bnapi = bp->bnapi[i];
8321 		struct bnxt_coal *hw_coal;
8322 
8323 		if (!bnapi->rx_ring)
8324 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8325 		else
8326 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8327 		if (rc)
8328 			break;
8329 
8330 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8331 			continue;
8332 
8333 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8334 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8335 			if (rc)
8336 				break;
8337 		}
8338 		if (bnapi->rx_ring)
8339 			hw_coal = &bp->rx_coal;
8340 		else
8341 			hw_coal = &bp->tx_coal;
8342 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8343 	}
8344 	hwrm_req_drop(bp, req_rx);
8345 	hwrm_req_drop(bp, req_tx);
8346 	return rc;
8347 }
8348 
8349 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8350 {
8351 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8352 	struct hwrm_stat_ctx_free_input *req;
8353 	int i;
8354 
8355 	if (!bp->bnapi)
8356 		return;
8357 
8358 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8359 		return;
8360 
8361 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8362 		return;
8363 	if (BNXT_FW_MAJ(bp) <= 20) {
8364 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8365 			hwrm_req_drop(bp, req);
8366 			return;
8367 		}
8368 		hwrm_req_hold(bp, req0);
8369 	}
8370 	hwrm_req_hold(bp, req);
8371 	for (i = 0; i < bp->cp_nr_rings; i++) {
8372 		struct bnxt_napi *bnapi = bp->bnapi[i];
8373 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8374 
8375 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8376 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8377 			if (req0) {
8378 				req0->stat_ctx_id = req->stat_ctx_id;
8379 				hwrm_req_send(bp, req0);
8380 			}
8381 			hwrm_req_send(bp, req);
8382 
8383 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8384 		}
8385 	}
8386 	hwrm_req_drop(bp, req);
8387 	if (req0)
8388 		hwrm_req_drop(bp, req0);
8389 }
8390 
8391 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8392 {
8393 	struct hwrm_stat_ctx_alloc_output *resp;
8394 	struct hwrm_stat_ctx_alloc_input *req;
8395 	int rc, i;
8396 
8397 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8398 		return 0;
8399 
8400 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8401 	if (rc)
8402 		return rc;
8403 
8404 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8405 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8406 
8407 	resp = hwrm_req_hold(bp, req);
8408 	for (i = 0; i < bp->cp_nr_rings; i++) {
8409 		struct bnxt_napi *bnapi = bp->bnapi[i];
8410 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8411 
8412 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8413 
8414 		rc = hwrm_req_send(bp, req);
8415 		if (rc)
8416 			break;
8417 
8418 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8419 
8420 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8421 	}
8422 	hwrm_req_drop(bp, req);
8423 	return rc;
8424 }
8425 
8426 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8427 {
8428 	struct hwrm_func_qcfg_output *resp;
8429 	struct hwrm_func_qcfg_input *req;
8430 	u16 flags;
8431 	int rc;
8432 
8433 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8434 	if (rc)
8435 		return rc;
8436 
8437 	req->fid = cpu_to_le16(0xffff);
8438 	resp = hwrm_req_hold(bp, req);
8439 	rc = hwrm_req_send(bp, req);
8440 	if (rc)
8441 		goto func_qcfg_exit;
8442 
8443 	flags = le16_to_cpu(resp->flags);
8444 #ifdef CONFIG_BNXT_SRIOV
8445 	if (BNXT_VF(bp)) {
8446 		struct bnxt_vf_info *vf = &bp->vf;
8447 
8448 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8449 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8450 			vf->flags |= BNXT_VF_TRUST;
8451 		else
8452 			vf->flags &= ~BNXT_VF_TRUST;
8453 	} else {
8454 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8455 	}
8456 #endif
8457 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8458 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8459 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8460 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8461 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8462 	}
8463 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8464 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8465 
8466 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8467 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8468 
8469 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8470 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8471 
8472 	switch (resp->port_partition_type) {
8473 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8474 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8475 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8476 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8477 		bp->port_partition_type = resp->port_partition_type;
8478 		break;
8479 	}
8480 	if (bp->hwrm_spec_code < 0x10707 ||
8481 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8482 		bp->br_mode = BRIDGE_MODE_VEB;
8483 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8484 		bp->br_mode = BRIDGE_MODE_VEPA;
8485 	else
8486 		bp->br_mode = BRIDGE_MODE_UNDEF;
8487 
8488 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8489 	if (!bp->max_mtu)
8490 		bp->max_mtu = BNXT_MAX_MTU;
8491 
8492 	if (bp->db_size)
8493 		goto func_qcfg_exit;
8494 
8495 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8496 	if (BNXT_CHIP_P5(bp)) {
8497 		if (BNXT_PF(bp))
8498 			bp->db_offset = DB_PF_OFFSET_P5;
8499 		else
8500 			bp->db_offset = DB_VF_OFFSET_P5;
8501 	}
8502 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8503 				 1024);
8504 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8505 	    bp->db_size <= bp->db_offset)
8506 		bp->db_size = pci_resource_len(bp->pdev, 2);
8507 
8508 func_qcfg_exit:
8509 	hwrm_req_drop(bp, req);
8510 	return rc;
8511 }
8512 
8513 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8514 				      u8 init_val, u8 init_offset,
8515 				      bool init_mask_set)
8516 {
8517 	ctxm->init_value = init_val;
8518 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8519 	if (init_mask_set)
8520 		ctxm->init_offset = init_offset * 4;
8521 	else
8522 		ctxm->init_value = 0;
8523 }
8524 
8525 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8526 {
8527 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8528 	u16 type;
8529 
8530 	for (type = 0; type < ctx_max; type++) {
8531 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8532 		int n = 1;
8533 
8534 		if (!ctxm->max_entries || ctxm->pg_info)
8535 			continue;
8536 
8537 		if (ctxm->instance_bmap)
8538 			n = hweight32(ctxm->instance_bmap);
8539 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8540 		if (!ctxm->pg_info)
8541 			return -ENOMEM;
8542 	}
8543 	return 0;
8544 }
8545 
8546 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8547 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8548 
8549 #define BNXT_CTX_INIT_VALID(flags)	\
8550 	(!!((flags) &			\
8551 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8552 
8553 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8554 {
8555 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8556 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8557 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8558 	u16 type;
8559 	int rc;
8560 
8561 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8562 	if (rc)
8563 		return rc;
8564 
8565 	if (!ctx) {
8566 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8567 		if (!ctx)
8568 			return -ENOMEM;
8569 		bp->ctx = ctx;
8570 	}
8571 
8572 	resp = hwrm_req_hold(bp, req);
8573 
8574 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8575 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8576 		u8 init_val, init_off, i;
8577 		u32 max_entries;
8578 		u16 entry_size;
8579 		__le32 *p;
8580 		u32 flags;
8581 
8582 		req->type = cpu_to_le16(type);
8583 		rc = hwrm_req_send(bp, req);
8584 		if (rc)
8585 			goto ctx_done;
8586 		flags = le32_to_cpu(resp->flags);
8587 		type = le16_to_cpu(resp->next_valid_type);
8588 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8589 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8590 			continue;
8591 		}
8592 		entry_size = le16_to_cpu(resp->entry_size);
8593 		max_entries = le32_to_cpu(resp->max_num_entries);
8594 		if (ctxm->mem_valid) {
8595 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8596 			    ctxm->entry_size != entry_size ||
8597 			    ctxm->max_entries != max_entries)
8598 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8599 			else
8600 				continue;
8601 		}
8602 		ctxm->type = le16_to_cpu(resp->type);
8603 		ctxm->entry_size = entry_size;
8604 		ctxm->flags = flags;
8605 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8606 		ctxm->entry_multiple = resp->entry_multiple;
8607 		ctxm->max_entries = max_entries;
8608 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8609 		init_val = resp->ctx_init_value;
8610 		init_off = resp->ctx_init_offset;
8611 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8612 					  BNXT_CTX_INIT_VALID(flags));
8613 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8614 					      BNXT_MAX_SPLIT_ENTRY);
8615 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8616 		     i++, p++)
8617 			ctxm->split[i] = le32_to_cpu(*p);
8618 	}
8619 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8620 
8621 ctx_done:
8622 	hwrm_req_drop(bp, req);
8623 	return rc;
8624 }
8625 
8626 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8627 {
8628 	struct hwrm_func_backing_store_qcaps_output *resp;
8629 	struct hwrm_func_backing_store_qcaps_input *req;
8630 	int rc;
8631 
8632 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8633 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8634 		return 0;
8635 
8636 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8637 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8638 
8639 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8640 	if (rc)
8641 		return rc;
8642 
8643 	resp = hwrm_req_hold(bp, req);
8644 	rc = hwrm_req_send_silent(bp, req);
8645 	if (!rc) {
8646 		struct bnxt_ctx_mem_type *ctxm;
8647 		struct bnxt_ctx_mem_info *ctx;
8648 		u8 init_val, init_idx = 0;
8649 		u16 init_mask;
8650 
8651 		ctx = bp->ctx;
8652 		if (!ctx) {
8653 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8654 			if (!ctx) {
8655 				rc = -ENOMEM;
8656 				goto ctx_err;
8657 			}
8658 			bp->ctx = ctx;
8659 		}
8660 		init_val = resp->ctx_kind_initializer;
8661 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8662 
8663 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8664 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8665 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8666 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8667 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8668 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8669 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8670 					  (init_mask & (1 << init_idx++)) != 0);
8671 
8672 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8673 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8674 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8675 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8676 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8677 					  (init_mask & (1 << init_idx++)) != 0);
8678 
8679 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8680 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8681 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8682 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8683 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8684 					  (init_mask & (1 << init_idx++)) != 0);
8685 
8686 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8687 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8688 		ctxm->max_entries = ctxm->vnic_entries +
8689 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8690 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8691 		bnxt_init_ctx_initializer(ctxm, init_val,
8692 					  resp->vnic_init_offset,
8693 					  (init_mask & (1 << init_idx++)) != 0);
8694 
8695 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8696 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8697 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8698 		bnxt_init_ctx_initializer(ctxm, init_val,
8699 					  resp->stat_init_offset,
8700 					  (init_mask & (1 << init_idx++)) != 0);
8701 
8702 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8703 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8704 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8705 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8706 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8707 		if (!ctxm->entry_multiple)
8708 			ctxm->entry_multiple = 1;
8709 
8710 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8711 
8712 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8713 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8714 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8715 		ctxm->mrav_num_entries_units =
8716 			le16_to_cpu(resp->mrav_num_entries_units);
8717 		bnxt_init_ctx_initializer(ctxm, init_val,
8718 					  resp->mrav_init_offset,
8719 					  (init_mask & (1 << init_idx++)) != 0);
8720 
8721 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8722 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8723 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8724 
8725 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8726 		if (!ctx->tqm_fp_rings_count)
8727 			ctx->tqm_fp_rings_count = bp->max_q;
8728 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8729 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8730 
8731 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8732 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8733 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8734 
8735 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8736 	} else {
8737 		rc = 0;
8738 	}
8739 ctx_err:
8740 	hwrm_req_drop(bp, req);
8741 	return rc;
8742 }
8743 
8744 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8745 				  __le64 *pg_dir)
8746 {
8747 	if (!rmem->nr_pages)
8748 		return;
8749 
8750 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8751 	if (rmem->depth >= 1) {
8752 		if (rmem->depth == 2)
8753 			*pg_attr |= 2;
8754 		else
8755 			*pg_attr |= 1;
8756 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8757 	} else {
8758 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8759 	}
8760 }
8761 
8762 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8763 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8764 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8765 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8766 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8767 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8768 
8769 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8770 {
8771 	struct hwrm_func_backing_store_cfg_input *req;
8772 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8773 	struct bnxt_ctx_pg_info *ctx_pg;
8774 	struct bnxt_ctx_mem_type *ctxm;
8775 	void **__req = (void **)&req;
8776 	u32 req_len = sizeof(*req);
8777 	__le32 *num_entries;
8778 	__le64 *pg_dir;
8779 	u32 flags = 0;
8780 	u8 *pg_attr;
8781 	u32 ena;
8782 	int rc;
8783 	int i;
8784 
8785 	if (!ctx)
8786 		return 0;
8787 
8788 	if (req_len > bp->hwrm_max_ext_req_len)
8789 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8790 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8791 	if (rc)
8792 		return rc;
8793 
8794 	req->enables = cpu_to_le32(enables);
8795 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8796 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8797 		ctx_pg = ctxm->pg_info;
8798 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8799 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8800 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8801 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8802 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8803 				      &req->qpc_pg_size_qpc_lvl,
8804 				      &req->qpc_page_dir);
8805 
8806 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8807 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8808 	}
8809 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8810 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8811 		ctx_pg = ctxm->pg_info;
8812 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8813 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8814 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8815 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8816 				      &req->srq_pg_size_srq_lvl,
8817 				      &req->srq_page_dir);
8818 	}
8819 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8820 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8821 		ctx_pg = ctxm->pg_info;
8822 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8823 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8824 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8825 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8826 				      &req->cq_pg_size_cq_lvl,
8827 				      &req->cq_page_dir);
8828 	}
8829 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8830 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8831 		ctx_pg = ctxm->pg_info;
8832 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8833 		req->vnic_num_ring_table_entries =
8834 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8835 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8836 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8837 				      &req->vnic_pg_size_vnic_lvl,
8838 				      &req->vnic_page_dir);
8839 	}
8840 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8841 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8842 		ctx_pg = ctxm->pg_info;
8843 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8844 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8845 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8846 				      &req->stat_pg_size_stat_lvl,
8847 				      &req->stat_page_dir);
8848 	}
8849 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8850 		u32 units;
8851 
8852 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8853 		ctx_pg = ctxm->pg_info;
8854 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8855 		units = ctxm->mrav_num_entries_units;
8856 		if (units) {
8857 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8858 			u32 entries;
8859 
8860 			num_mr = ctx_pg->entries - num_ah;
8861 			entries = ((num_mr / units) << 16) | (num_ah / units);
8862 			req->mrav_num_entries = cpu_to_le32(entries);
8863 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8864 		}
8865 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8866 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8867 				      &req->mrav_pg_size_mrav_lvl,
8868 				      &req->mrav_page_dir);
8869 	}
8870 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8871 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8872 		ctx_pg = ctxm->pg_info;
8873 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8874 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8875 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8876 				      &req->tim_pg_size_tim_lvl,
8877 				      &req->tim_page_dir);
8878 	}
8879 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8880 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8881 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8882 	     pg_dir = &req->tqm_sp_page_dir,
8883 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8884 	     ctx_pg = ctxm->pg_info;
8885 	     i < BNXT_MAX_TQM_RINGS;
8886 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8887 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8888 		if (!(enables & ena))
8889 			continue;
8890 
8891 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8892 		*num_entries = cpu_to_le32(ctx_pg->entries);
8893 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8894 	}
8895 	req->flags = cpu_to_le32(flags);
8896 	return hwrm_req_send(bp, req);
8897 }
8898 
8899 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8900 				  struct bnxt_ctx_pg_info *ctx_pg)
8901 {
8902 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8903 
8904 	rmem->page_size = BNXT_PAGE_SIZE;
8905 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8906 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8907 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8908 	if (rmem->depth >= 1)
8909 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8910 	return bnxt_alloc_ring(bp, rmem);
8911 }
8912 
8913 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8914 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8915 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8916 {
8917 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8918 	int rc;
8919 
8920 	if (!mem_size)
8921 		return -EINVAL;
8922 
8923 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8924 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8925 		ctx_pg->nr_pages = 0;
8926 		return -EINVAL;
8927 	}
8928 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8929 		int nr_tbls, i;
8930 
8931 		rmem->depth = 2;
8932 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8933 					     GFP_KERNEL);
8934 		if (!ctx_pg->ctx_pg_tbl)
8935 			return -ENOMEM;
8936 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8937 		rmem->nr_pages = nr_tbls;
8938 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8939 		if (rc)
8940 			return rc;
8941 		for (i = 0; i < nr_tbls; i++) {
8942 			struct bnxt_ctx_pg_info *pg_tbl;
8943 
8944 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8945 			if (!pg_tbl)
8946 				return -ENOMEM;
8947 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8948 			rmem = &pg_tbl->ring_mem;
8949 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8950 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8951 			rmem->depth = 1;
8952 			rmem->nr_pages = MAX_CTX_PAGES;
8953 			rmem->ctx_mem = ctxm;
8954 			if (i == (nr_tbls - 1)) {
8955 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8956 
8957 				if (rem)
8958 					rmem->nr_pages = rem;
8959 			}
8960 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8961 			if (rc)
8962 				break;
8963 		}
8964 	} else {
8965 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8966 		if (rmem->nr_pages > 1 || depth)
8967 			rmem->depth = 1;
8968 		rmem->ctx_mem = ctxm;
8969 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8970 	}
8971 	return rc;
8972 }
8973 
8974 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8975 				    struct bnxt_ctx_pg_info *ctx_pg,
8976 				    void *buf, size_t offset, size_t head,
8977 				    size_t tail)
8978 {
8979 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8980 	size_t nr_pages = ctx_pg->nr_pages;
8981 	int page_size = rmem->page_size;
8982 	size_t len = 0, total_len = 0;
8983 	u16 depth = rmem->depth;
8984 
8985 	tail %= nr_pages * page_size;
8986 	do {
8987 		if (depth > 1) {
8988 			int i = head / (page_size * MAX_CTX_PAGES);
8989 			struct bnxt_ctx_pg_info *pg_tbl;
8990 
8991 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8992 			rmem = &pg_tbl->ring_mem;
8993 		}
8994 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8995 		head += len;
8996 		offset += len;
8997 		total_len += len;
8998 		if (head >= nr_pages * page_size)
8999 			head = 0;
9000 	} while (head != tail);
9001 	return total_len;
9002 }
9003 
9004 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9005 				  struct bnxt_ctx_pg_info *ctx_pg)
9006 {
9007 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9008 
9009 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9010 	    ctx_pg->ctx_pg_tbl) {
9011 		int i, nr_tbls = rmem->nr_pages;
9012 
9013 		for (i = 0; i < nr_tbls; i++) {
9014 			struct bnxt_ctx_pg_info *pg_tbl;
9015 			struct bnxt_ring_mem_info *rmem2;
9016 
9017 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9018 			if (!pg_tbl)
9019 				continue;
9020 			rmem2 = &pg_tbl->ring_mem;
9021 			bnxt_free_ring(bp, rmem2);
9022 			ctx_pg->ctx_pg_arr[i] = NULL;
9023 			kfree(pg_tbl);
9024 			ctx_pg->ctx_pg_tbl[i] = NULL;
9025 		}
9026 		kfree(ctx_pg->ctx_pg_tbl);
9027 		ctx_pg->ctx_pg_tbl = NULL;
9028 	}
9029 	bnxt_free_ring(bp, rmem);
9030 	ctx_pg->nr_pages = 0;
9031 }
9032 
9033 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9034 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9035 				   u8 pg_lvl)
9036 {
9037 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9038 	int i, rc = 0, n = 1;
9039 	u32 mem_size;
9040 
9041 	if (!ctxm->entry_size || !ctx_pg)
9042 		return -EINVAL;
9043 	if (ctxm->instance_bmap)
9044 		n = hweight32(ctxm->instance_bmap);
9045 	if (ctxm->entry_multiple)
9046 		entries = roundup(entries, ctxm->entry_multiple);
9047 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9048 	mem_size = entries * ctxm->entry_size;
9049 	for (i = 0; i < n && !rc; i++) {
9050 		ctx_pg[i].entries = entries;
9051 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9052 					    ctxm->init_value ? ctxm : NULL);
9053 	}
9054 	if (!rc)
9055 		ctxm->mem_valid = 1;
9056 	return rc;
9057 }
9058 
9059 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9060 					       struct bnxt_ctx_mem_type *ctxm,
9061 					       bool last)
9062 {
9063 	struct hwrm_func_backing_store_cfg_v2_input *req;
9064 	u32 instance_bmap = ctxm->instance_bmap;
9065 	int i, j, rc = 0, n = 1;
9066 	__le32 *p;
9067 
9068 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9069 		return 0;
9070 
9071 	if (instance_bmap)
9072 		n = hweight32(ctxm->instance_bmap);
9073 	else
9074 		instance_bmap = 1;
9075 
9076 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9077 	if (rc)
9078 		return rc;
9079 	hwrm_req_hold(bp, req);
9080 	req->type = cpu_to_le16(ctxm->type);
9081 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9082 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9083 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9084 		struct bnxt_bs_trace_info *bs_trace;
9085 		u32 enables;
9086 
9087 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9088 		req->enables = cpu_to_le32(enables);
9089 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9090 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9091 	}
9092 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9093 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9094 		p[i] = cpu_to_le32(ctxm->split[i]);
9095 	for (i = 0, j = 0; j < n && !rc; i++) {
9096 		struct bnxt_ctx_pg_info *ctx_pg;
9097 
9098 		if (!(instance_bmap & (1 << i)))
9099 			continue;
9100 		req->instance = cpu_to_le16(i);
9101 		ctx_pg = &ctxm->pg_info[j++];
9102 		if (!ctx_pg->entries)
9103 			continue;
9104 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9105 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9106 				      &req->page_size_pbl_level,
9107 				      &req->page_dir);
9108 		if (last && j == n)
9109 			req->flags =
9110 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9111 		rc = hwrm_req_send(bp, req);
9112 	}
9113 	hwrm_req_drop(bp, req);
9114 	return rc;
9115 }
9116 
9117 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9118 {
9119 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9120 	struct bnxt_ctx_mem_type *ctxm;
9121 	u16 last_type = BNXT_CTX_INV;
9122 	int rc = 0;
9123 	u16 type;
9124 
9125 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9126 		ctxm = &ctx->ctx_arr[type];
9127 		if (!bnxt_bs_trace_avail(bp, type))
9128 			continue;
9129 		if (!ctxm->mem_valid) {
9130 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9131 						     ctxm->max_entries, 1);
9132 			if (rc) {
9133 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9134 					    type);
9135 				continue;
9136 			}
9137 			bnxt_bs_trace_init(bp, ctxm);
9138 		}
9139 		last_type = type;
9140 	}
9141 
9142 	if (last_type == BNXT_CTX_INV) {
9143 		if (!ena)
9144 			return 0;
9145 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9146 			last_type = BNXT_CTX_MAX - 1;
9147 		else
9148 			last_type = BNXT_CTX_L2_MAX - 1;
9149 	}
9150 	ctx->ctx_arr[last_type].last = 1;
9151 
9152 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9153 		ctxm = &ctx->ctx_arr[type];
9154 
9155 		if (!ctxm->mem_valid)
9156 			continue;
9157 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9158 		if (rc)
9159 			return rc;
9160 	}
9161 	return 0;
9162 }
9163 
9164 /**
9165  * __bnxt_copy_ctx_mem - copy host context memory
9166  * @bp: The driver context
9167  * @ctxm: The pointer to the context memory type
9168  * @buf: The destination buffer or NULL to just obtain the length
9169  * @offset: The buffer offset to copy the data to
9170  * @head: The head offset of context memory to copy from
9171  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9172  *
9173  * This function is called for debugging purposes to dump the host context
9174  * used by the chip.
9175  *
9176  * Return: Length of memory copied
9177  */
9178 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9179 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9180 				  size_t offset, size_t head, size_t tail)
9181 {
9182 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9183 	size_t len = 0, total_len = 0;
9184 	int i, n = 1;
9185 
9186 	if (!ctx_pg)
9187 		return 0;
9188 
9189 	if (ctxm->instance_bmap)
9190 		n = hweight32(ctxm->instance_bmap);
9191 	for (i = 0; i < n; i++) {
9192 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9193 					    tail);
9194 		offset += len;
9195 		total_len += len;
9196 	}
9197 	return total_len;
9198 }
9199 
9200 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9201 			 void *buf, size_t offset)
9202 {
9203 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9204 
9205 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9206 }
9207 
9208 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9209 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9210 {
9211 	struct bnxt_ctx_pg_info *ctx_pg;
9212 	int i, n = 1;
9213 
9214 	ctxm->last = 0;
9215 
9216 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9217 		return;
9218 
9219 	ctx_pg = ctxm->pg_info;
9220 	if (ctx_pg) {
9221 		if (ctxm->instance_bmap)
9222 			n = hweight32(ctxm->instance_bmap);
9223 		for (i = 0; i < n; i++)
9224 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9225 
9226 		kfree(ctx_pg);
9227 		ctxm->pg_info = NULL;
9228 		ctxm->mem_valid = 0;
9229 	}
9230 	memset(ctxm, 0, sizeof(*ctxm));
9231 }
9232 
9233 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9234 {
9235 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9236 	u16 type;
9237 
9238 	if (!ctx)
9239 		return;
9240 
9241 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9242 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9243 
9244 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9245 	if (force) {
9246 		kfree(ctx);
9247 		bp->ctx = NULL;
9248 	}
9249 }
9250 
9251 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9252 {
9253 	struct bnxt_ctx_mem_type *ctxm;
9254 	struct bnxt_ctx_mem_info *ctx;
9255 	u32 l2_qps, qp1_qps, max_qps;
9256 	u32 ena, entries_sp, entries;
9257 	u32 srqs, max_srqs, min;
9258 	u32 num_mr, num_ah;
9259 	u32 extra_srqs = 0;
9260 	u32 extra_qps = 0;
9261 	u32 fast_qpmd_qps;
9262 	u8 pg_lvl = 1;
9263 	int i, rc;
9264 
9265 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9266 	if (rc) {
9267 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9268 			   rc);
9269 		return rc;
9270 	}
9271 	ctx = bp->ctx;
9272 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9273 		return 0;
9274 
9275 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9276 	l2_qps = ctxm->qp_l2_entries;
9277 	qp1_qps = ctxm->qp_qp1_entries;
9278 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9279 	max_qps = ctxm->max_entries;
9280 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9281 	srqs = ctxm->srq_l2_entries;
9282 	max_srqs = ctxm->max_entries;
9283 	ena = 0;
9284 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9285 		pg_lvl = 2;
9286 		if (BNXT_SW_RES_LMT(bp)) {
9287 			extra_qps = max_qps - l2_qps - qp1_qps;
9288 			extra_srqs = max_srqs - srqs;
9289 		} else {
9290 			extra_qps = min_t(u32, 65536,
9291 					  max_qps - l2_qps - qp1_qps);
9292 			/* allocate extra qps if fw supports RoCE fast qp
9293 			 * destroy feature
9294 			 */
9295 			extra_qps += fast_qpmd_qps;
9296 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9297 		}
9298 		if (fast_qpmd_qps)
9299 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9300 	}
9301 
9302 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9303 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9304 				     pg_lvl);
9305 	if (rc)
9306 		return rc;
9307 
9308 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9309 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9310 	if (rc)
9311 		return rc;
9312 
9313 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9314 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9315 				     extra_qps * 2, pg_lvl);
9316 	if (rc)
9317 		return rc;
9318 
9319 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9320 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9321 	if (rc)
9322 		return rc;
9323 
9324 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9325 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9326 	if (rc)
9327 		return rc;
9328 
9329 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9330 		goto skip_rdma;
9331 
9332 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9333 	if (BNXT_SW_RES_LMT(bp) &&
9334 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9335 		num_ah = ctxm->mrav_av_entries;
9336 		num_mr = ctxm->max_entries - num_ah;
9337 	} else {
9338 		/* 128K extra is needed to accommodate static AH context
9339 		 * allocation by f/w.
9340 		 */
9341 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9342 		num_ah = min_t(u32, num_mr, 1024 * 128);
9343 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9344 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9345 			ctxm->mrav_av_entries = num_ah;
9346 	}
9347 
9348 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9349 	if (rc)
9350 		return rc;
9351 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9352 
9353 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9354 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9355 	if (rc)
9356 		return rc;
9357 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9358 
9359 skip_rdma:
9360 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9361 	min = ctxm->min_entries;
9362 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9363 		     2 * (extra_qps + qp1_qps) + min;
9364 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9365 	if (rc)
9366 		return rc;
9367 
9368 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9369 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9370 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9371 	if (rc)
9372 		return rc;
9373 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9374 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9375 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9376 
9377 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9378 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9379 	else
9380 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9381 	if (rc) {
9382 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9383 			   rc);
9384 		return rc;
9385 	}
9386 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9387 	return 0;
9388 }
9389 
9390 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9391 {
9392 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9393 	u16 page_attr;
9394 	int rc;
9395 
9396 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9397 		return 0;
9398 
9399 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9400 	if (rc)
9401 		return rc;
9402 
9403 	if (BNXT_PAGE_SIZE == 0x2000)
9404 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9405 	else if (BNXT_PAGE_SIZE == 0x10000)
9406 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9407 	else
9408 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9409 	req->pg_size_lvl = cpu_to_le16(page_attr |
9410 				       bp->fw_crash_mem->ring_mem.depth);
9411 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9412 	req->size = cpu_to_le32(bp->fw_crash_len);
9413 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9414 	return hwrm_req_send(bp, req);
9415 }
9416 
9417 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9418 {
9419 	if (bp->fw_crash_mem) {
9420 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9421 		kfree(bp->fw_crash_mem);
9422 		bp->fw_crash_mem = NULL;
9423 	}
9424 }
9425 
9426 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9427 {
9428 	u32 mem_size = 0;
9429 	int rc;
9430 
9431 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9432 		return 0;
9433 
9434 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9435 	if (rc)
9436 		return rc;
9437 
9438 	mem_size = round_up(mem_size, 4);
9439 
9440 	/* keep and use the existing pages */
9441 	if (bp->fw_crash_mem &&
9442 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9443 		goto alloc_done;
9444 
9445 	if (bp->fw_crash_mem)
9446 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9447 	else
9448 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9449 					   GFP_KERNEL);
9450 	if (!bp->fw_crash_mem)
9451 		return -ENOMEM;
9452 
9453 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9454 	if (rc) {
9455 		bnxt_free_crash_dump_mem(bp);
9456 		return rc;
9457 	}
9458 
9459 alloc_done:
9460 	bp->fw_crash_len = mem_size;
9461 	return 0;
9462 }
9463 
9464 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9465 {
9466 	struct hwrm_func_resource_qcaps_output *resp;
9467 	struct hwrm_func_resource_qcaps_input *req;
9468 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9469 	int rc;
9470 
9471 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9472 	if (rc)
9473 		return rc;
9474 
9475 	req->fid = cpu_to_le16(0xffff);
9476 	resp = hwrm_req_hold(bp, req);
9477 	rc = hwrm_req_send_silent(bp, req);
9478 	if (rc)
9479 		goto hwrm_func_resc_qcaps_exit;
9480 
9481 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9482 	if (!all)
9483 		goto hwrm_func_resc_qcaps_exit;
9484 
9485 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9486 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9487 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9488 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9489 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9490 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9491 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9492 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9493 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9494 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9495 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9496 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9497 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9498 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9499 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9500 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9501 
9502 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9503 		u16 max_msix = le16_to_cpu(resp->max_msix);
9504 
9505 		hw_resc->max_nqs = max_msix;
9506 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9507 	}
9508 
9509 	if (BNXT_PF(bp)) {
9510 		struct bnxt_pf_info *pf = &bp->pf;
9511 
9512 		pf->vf_resv_strategy =
9513 			le16_to_cpu(resp->vf_reservation_strategy);
9514 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9515 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9516 	}
9517 hwrm_func_resc_qcaps_exit:
9518 	hwrm_req_drop(bp, req);
9519 	return rc;
9520 }
9521 
9522 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9523 {
9524 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9525 	struct hwrm_port_mac_ptp_qcfg_input *req;
9526 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9527 	u8 flags;
9528 	int rc;
9529 
9530 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9531 		rc = -ENODEV;
9532 		goto no_ptp;
9533 	}
9534 
9535 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9536 	if (rc)
9537 		goto no_ptp;
9538 
9539 	req->port_id = cpu_to_le16(bp->pf.port_id);
9540 	resp = hwrm_req_hold(bp, req);
9541 	rc = hwrm_req_send(bp, req);
9542 	if (rc)
9543 		goto exit;
9544 
9545 	flags = resp->flags;
9546 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9547 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9548 		rc = -ENODEV;
9549 		goto exit;
9550 	}
9551 	if (!ptp) {
9552 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9553 		if (!ptp) {
9554 			rc = -ENOMEM;
9555 			goto exit;
9556 		}
9557 		ptp->bp = bp;
9558 		bp->ptp_cfg = ptp;
9559 	}
9560 
9561 	if (flags &
9562 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9563 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9564 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9565 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9566 	} else if (BNXT_CHIP_P5(bp)) {
9567 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9568 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9569 	} else {
9570 		rc = -ENODEV;
9571 		goto exit;
9572 	}
9573 	ptp->rtc_configured =
9574 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9575 	rc = bnxt_ptp_init(bp);
9576 	if (rc)
9577 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9578 exit:
9579 	hwrm_req_drop(bp, req);
9580 	if (!rc)
9581 		return 0;
9582 
9583 no_ptp:
9584 	bnxt_ptp_clear(bp);
9585 	kfree(ptp);
9586 	bp->ptp_cfg = NULL;
9587 	return rc;
9588 }
9589 
9590 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9591 {
9592 	struct hwrm_func_qcaps_output *resp;
9593 	struct hwrm_func_qcaps_input *req;
9594 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9595 	u32 flags, flags_ext, flags_ext2;
9596 	int rc;
9597 
9598 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9599 	if (rc)
9600 		return rc;
9601 
9602 	req->fid = cpu_to_le16(0xffff);
9603 	resp = hwrm_req_hold(bp, req);
9604 	rc = hwrm_req_send(bp, req);
9605 	if (rc)
9606 		goto hwrm_func_qcaps_exit;
9607 
9608 	flags = le32_to_cpu(resp->flags);
9609 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9610 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9611 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9612 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9613 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9614 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9615 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9616 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9617 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9618 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9619 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9620 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9621 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9622 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9623 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9624 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9625 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9626 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9627 
9628 	flags_ext = le32_to_cpu(resp->flags_ext);
9629 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9630 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9631 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9632 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9633 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9634 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9635 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9636 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9637 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9638 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9639 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9640 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9641 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9642 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9643 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9644 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9645 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9646 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9647 
9648 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9649 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9650 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9651 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9652 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9653 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9654 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9655 	if (flags_ext2 &
9656 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9657 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9658 	if (BNXT_PF(bp) &&
9659 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9660 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9661 
9662 	bp->tx_push_thresh = 0;
9663 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9664 	    BNXT_FW_MAJ(bp) > 217)
9665 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9666 
9667 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9668 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9669 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9670 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9671 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9672 	if (!hw_resc->max_hw_ring_grps)
9673 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9674 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9675 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9676 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9677 
9678 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9679 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9680 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9681 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9682 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9683 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9684 
9685 	if (BNXT_PF(bp)) {
9686 		struct bnxt_pf_info *pf = &bp->pf;
9687 
9688 		pf->fw_fid = le16_to_cpu(resp->fid);
9689 		pf->port_id = le16_to_cpu(resp->port_id);
9690 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9691 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9692 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9693 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9694 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9695 			bp->flags |= BNXT_FLAG_WOL_CAP;
9696 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9697 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9698 		} else {
9699 			bnxt_ptp_clear(bp);
9700 			kfree(bp->ptp_cfg);
9701 			bp->ptp_cfg = NULL;
9702 		}
9703 	} else {
9704 #ifdef CONFIG_BNXT_SRIOV
9705 		struct bnxt_vf_info *vf = &bp->vf;
9706 
9707 		vf->fw_fid = le16_to_cpu(resp->fid);
9708 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9709 #endif
9710 	}
9711 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9712 
9713 hwrm_func_qcaps_exit:
9714 	hwrm_req_drop(bp, req);
9715 	return rc;
9716 }
9717 
9718 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9719 {
9720 	struct hwrm_dbg_qcaps_output *resp;
9721 	struct hwrm_dbg_qcaps_input *req;
9722 	int rc;
9723 
9724 	bp->fw_dbg_cap = 0;
9725 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9726 		return;
9727 
9728 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9729 	if (rc)
9730 		return;
9731 
9732 	req->fid = cpu_to_le16(0xffff);
9733 	resp = hwrm_req_hold(bp, req);
9734 	rc = hwrm_req_send(bp, req);
9735 	if (rc)
9736 		goto hwrm_dbg_qcaps_exit;
9737 
9738 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9739 
9740 hwrm_dbg_qcaps_exit:
9741 	hwrm_req_drop(bp, req);
9742 }
9743 
9744 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9745 
9746 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9747 {
9748 	int rc;
9749 
9750 	rc = __bnxt_hwrm_func_qcaps(bp);
9751 	if (rc)
9752 		return rc;
9753 
9754 	bnxt_hwrm_dbg_qcaps(bp);
9755 
9756 	rc = bnxt_hwrm_queue_qportcfg(bp);
9757 	if (rc) {
9758 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9759 		return rc;
9760 	}
9761 	if (bp->hwrm_spec_code >= 0x10803) {
9762 		rc = bnxt_alloc_ctx_mem(bp);
9763 		if (rc)
9764 			return rc;
9765 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9766 		if (!rc)
9767 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9768 	}
9769 	return 0;
9770 }
9771 
9772 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9773 {
9774 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9775 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9776 	u32 flags;
9777 	int rc;
9778 
9779 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9780 		return 0;
9781 
9782 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9783 	if (rc)
9784 		return rc;
9785 
9786 	resp = hwrm_req_hold(bp, req);
9787 	rc = hwrm_req_send(bp, req);
9788 	if (rc)
9789 		goto hwrm_cfa_adv_qcaps_exit;
9790 
9791 	flags = le32_to_cpu(resp->flags);
9792 	if (flags &
9793 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9794 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9795 
9796 	if (flags &
9797 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9798 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9799 
9800 	if (flags &
9801 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9802 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9803 
9804 hwrm_cfa_adv_qcaps_exit:
9805 	hwrm_req_drop(bp, req);
9806 	return rc;
9807 }
9808 
9809 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9810 {
9811 	if (bp->fw_health)
9812 		return 0;
9813 
9814 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9815 	if (!bp->fw_health)
9816 		return -ENOMEM;
9817 
9818 	mutex_init(&bp->fw_health->lock);
9819 	return 0;
9820 }
9821 
9822 static int bnxt_alloc_fw_health(struct bnxt *bp)
9823 {
9824 	int rc;
9825 
9826 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9827 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9828 		return 0;
9829 
9830 	rc = __bnxt_alloc_fw_health(bp);
9831 	if (rc) {
9832 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9833 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9834 		return rc;
9835 	}
9836 
9837 	return 0;
9838 }
9839 
9840 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9841 {
9842 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9843 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9844 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9845 }
9846 
9847 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9848 {
9849 	struct bnxt_fw_health *fw_health = bp->fw_health;
9850 	u32 reg_type;
9851 
9852 	if (!fw_health)
9853 		return;
9854 
9855 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9856 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9857 		fw_health->status_reliable = false;
9858 
9859 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9860 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9861 		fw_health->resets_reliable = false;
9862 }
9863 
9864 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9865 {
9866 	void __iomem *hs;
9867 	u32 status_loc;
9868 	u32 reg_type;
9869 	u32 sig;
9870 
9871 	if (bp->fw_health)
9872 		bp->fw_health->status_reliable = false;
9873 
9874 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9875 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9876 
9877 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9878 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9879 		if (!bp->chip_num) {
9880 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9881 			bp->chip_num = readl(bp->bar0 +
9882 					     BNXT_FW_HEALTH_WIN_BASE +
9883 					     BNXT_GRC_REG_CHIP_NUM);
9884 		}
9885 		if (!BNXT_CHIP_P5_PLUS(bp))
9886 			return;
9887 
9888 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9889 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9890 	} else {
9891 		status_loc = readl(hs + offsetof(struct hcomm_status,
9892 						 fw_status_loc));
9893 	}
9894 
9895 	if (__bnxt_alloc_fw_health(bp)) {
9896 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9897 		return;
9898 	}
9899 
9900 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9901 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9902 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9903 		__bnxt_map_fw_health_reg(bp, status_loc);
9904 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9905 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9906 	}
9907 
9908 	bp->fw_health->status_reliable = true;
9909 }
9910 
9911 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9912 {
9913 	struct bnxt_fw_health *fw_health = bp->fw_health;
9914 	u32 reg_base = 0xffffffff;
9915 	int i;
9916 
9917 	bp->fw_health->status_reliable = false;
9918 	bp->fw_health->resets_reliable = false;
9919 	/* Only pre-map the monitoring GRC registers using window 3 */
9920 	for (i = 0; i < 4; i++) {
9921 		u32 reg = fw_health->regs[i];
9922 
9923 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9924 			continue;
9925 		if (reg_base == 0xffffffff)
9926 			reg_base = reg & BNXT_GRC_BASE_MASK;
9927 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9928 			return -ERANGE;
9929 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9930 	}
9931 	bp->fw_health->status_reliable = true;
9932 	bp->fw_health->resets_reliable = true;
9933 	if (reg_base == 0xffffffff)
9934 		return 0;
9935 
9936 	__bnxt_map_fw_health_reg(bp, reg_base);
9937 	return 0;
9938 }
9939 
9940 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9941 {
9942 	if (!bp->fw_health)
9943 		return;
9944 
9945 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9946 		bp->fw_health->status_reliable = true;
9947 		bp->fw_health->resets_reliable = true;
9948 	} else {
9949 		bnxt_try_map_fw_health_reg(bp);
9950 	}
9951 }
9952 
9953 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9954 {
9955 	struct bnxt_fw_health *fw_health = bp->fw_health;
9956 	struct hwrm_error_recovery_qcfg_output *resp;
9957 	struct hwrm_error_recovery_qcfg_input *req;
9958 	int rc, i;
9959 
9960 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9961 		return 0;
9962 
9963 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9964 	if (rc)
9965 		return rc;
9966 
9967 	resp = hwrm_req_hold(bp, req);
9968 	rc = hwrm_req_send(bp, req);
9969 	if (rc)
9970 		goto err_recovery_out;
9971 	fw_health->flags = le32_to_cpu(resp->flags);
9972 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9973 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9974 		rc = -EINVAL;
9975 		goto err_recovery_out;
9976 	}
9977 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9978 	fw_health->master_func_wait_dsecs =
9979 		le32_to_cpu(resp->master_func_wait_period);
9980 	fw_health->normal_func_wait_dsecs =
9981 		le32_to_cpu(resp->normal_func_wait_period);
9982 	fw_health->post_reset_wait_dsecs =
9983 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9984 	fw_health->post_reset_max_wait_dsecs =
9985 		le32_to_cpu(resp->max_bailout_time_after_reset);
9986 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9987 		le32_to_cpu(resp->fw_health_status_reg);
9988 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9989 		le32_to_cpu(resp->fw_heartbeat_reg);
9990 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9991 		le32_to_cpu(resp->fw_reset_cnt_reg);
9992 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9993 		le32_to_cpu(resp->reset_inprogress_reg);
9994 	fw_health->fw_reset_inprog_reg_mask =
9995 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9996 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9997 	if (fw_health->fw_reset_seq_cnt >= 16) {
9998 		rc = -EINVAL;
9999 		goto err_recovery_out;
10000 	}
10001 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
10002 		fw_health->fw_reset_seq_regs[i] =
10003 			le32_to_cpu(resp->reset_reg[i]);
10004 		fw_health->fw_reset_seq_vals[i] =
10005 			le32_to_cpu(resp->reset_reg_val[i]);
10006 		fw_health->fw_reset_seq_delay_msec[i] =
10007 			resp->delay_after_reset[i];
10008 	}
10009 err_recovery_out:
10010 	hwrm_req_drop(bp, req);
10011 	if (!rc)
10012 		rc = bnxt_map_fw_health_regs(bp);
10013 	if (rc)
10014 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10015 	return rc;
10016 }
10017 
10018 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10019 {
10020 	struct hwrm_func_reset_input *req;
10021 	int rc;
10022 
10023 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10024 	if (rc)
10025 		return rc;
10026 
10027 	req->enables = 0;
10028 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10029 	return hwrm_req_send(bp, req);
10030 }
10031 
10032 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10033 {
10034 	struct hwrm_nvm_get_dev_info_output nvm_info;
10035 
10036 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10037 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10038 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10039 			 nvm_info.nvm_cfg_ver_upd);
10040 }
10041 
10042 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10043 {
10044 	struct hwrm_queue_qportcfg_output *resp;
10045 	struct hwrm_queue_qportcfg_input *req;
10046 	u8 i, j, *qptr;
10047 	bool no_rdma;
10048 	int rc = 0;
10049 
10050 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10051 	if (rc)
10052 		return rc;
10053 
10054 	resp = hwrm_req_hold(bp, req);
10055 	rc = hwrm_req_send(bp, req);
10056 	if (rc)
10057 		goto qportcfg_exit;
10058 
10059 	if (!resp->max_configurable_queues) {
10060 		rc = -EINVAL;
10061 		goto qportcfg_exit;
10062 	}
10063 	bp->max_tc = resp->max_configurable_queues;
10064 	bp->max_lltc = resp->max_configurable_lossless_queues;
10065 	if (bp->max_tc > BNXT_MAX_QUEUE)
10066 		bp->max_tc = BNXT_MAX_QUEUE;
10067 
10068 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10069 	qptr = &resp->queue_id0;
10070 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10071 		bp->q_info[j].queue_id = *qptr;
10072 		bp->q_ids[i] = *qptr++;
10073 		bp->q_info[j].queue_profile = *qptr++;
10074 		bp->tc_to_qidx[j] = j;
10075 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10076 		    (no_rdma && BNXT_PF(bp)))
10077 			j++;
10078 	}
10079 	bp->max_q = bp->max_tc;
10080 	bp->max_tc = max_t(u8, j, 1);
10081 
10082 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10083 		bp->max_tc = 1;
10084 
10085 	if (bp->max_lltc > bp->max_tc)
10086 		bp->max_lltc = bp->max_tc;
10087 
10088 qportcfg_exit:
10089 	hwrm_req_drop(bp, req);
10090 	return rc;
10091 }
10092 
10093 static int bnxt_hwrm_poll(struct bnxt *bp)
10094 {
10095 	struct hwrm_ver_get_input *req;
10096 	int rc;
10097 
10098 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10099 	if (rc)
10100 		return rc;
10101 
10102 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10103 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10104 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10105 
10106 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10107 	rc = hwrm_req_send(bp, req);
10108 	return rc;
10109 }
10110 
10111 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10112 {
10113 	struct hwrm_ver_get_output *resp;
10114 	struct hwrm_ver_get_input *req;
10115 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10116 	u32 dev_caps_cfg, hwrm_ver;
10117 	int rc, len, max_tmo_secs;
10118 
10119 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10120 	if (rc)
10121 		return rc;
10122 
10123 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10124 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10125 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10126 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10127 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10128 
10129 	resp = hwrm_req_hold(bp, req);
10130 	rc = hwrm_req_send(bp, req);
10131 	if (rc)
10132 		goto hwrm_ver_get_exit;
10133 
10134 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10135 
10136 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10137 			     resp->hwrm_intf_min_8b << 8 |
10138 			     resp->hwrm_intf_upd_8b;
10139 	if (resp->hwrm_intf_maj_8b < 1) {
10140 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10141 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10142 			    resp->hwrm_intf_upd_8b);
10143 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10144 	}
10145 
10146 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10147 			HWRM_VERSION_UPDATE;
10148 
10149 	if (bp->hwrm_spec_code > hwrm_ver)
10150 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10151 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10152 			 HWRM_VERSION_UPDATE);
10153 	else
10154 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10155 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10156 			 resp->hwrm_intf_upd_8b);
10157 
10158 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10159 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10160 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10161 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10162 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10163 		len = FW_VER_STR_LEN;
10164 	} else {
10165 		fw_maj = resp->hwrm_fw_maj_8b;
10166 		fw_min = resp->hwrm_fw_min_8b;
10167 		fw_bld = resp->hwrm_fw_bld_8b;
10168 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10169 		len = BC_HWRM_STR_LEN;
10170 	}
10171 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10172 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10173 		 fw_rsv);
10174 
10175 	if (strlen(resp->active_pkg_name)) {
10176 		int fw_ver_len = strlen(bp->fw_ver_str);
10177 
10178 		snprintf(bp->fw_ver_str + fw_ver_len,
10179 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10180 			 resp->active_pkg_name);
10181 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10182 	}
10183 
10184 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10185 	if (!bp->hwrm_cmd_timeout)
10186 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10187 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10188 	if (!bp->hwrm_cmd_max_timeout)
10189 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10190 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10191 #ifdef CONFIG_DETECT_HUNG_TASK
10192 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10193 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10194 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10195 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10196 	}
10197 #endif
10198 
10199 	if (resp->hwrm_intf_maj_8b >= 1) {
10200 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10201 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10202 	}
10203 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10204 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10205 
10206 	bp->chip_num = le16_to_cpu(resp->chip_num);
10207 	bp->chip_rev = resp->chip_rev;
10208 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10209 	    !resp->chip_metal)
10210 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10211 
10212 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10213 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10214 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10215 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10216 
10217 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10218 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10219 
10220 	if (dev_caps_cfg &
10221 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10222 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10223 
10224 	if (dev_caps_cfg &
10225 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10226 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10227 
10228 	if (dev_caps_cfg &
10229 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10230 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10231 
10232 hwrm_ver_get_exit:
10233 	hwrm_req_drop(bp, req);
10234 	return rc;
10235 }
10236 
10237 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10238 {
10239 	struct hwrm_fw_set_time_input *req;
10240 	struct tm tm;
10241 	time64_t now = ktime_get_real_seconds();
10242 	int rc;
10243 
10244 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10245 	    bp->hwrm_spec_code < 0x10400)
10246 		return -EOPNOTSUPP;
10247 
10248 	time64_to_tm(now, 0, &tm);
10249 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10250 	if (rc)
10251 		return rc;
10252 
10253 	req->year = cpu_to_le16(1900 + tm.tm_year);
10254 	req->month = 1 + tm.tm_mon;
10255 	req->day = tm.tm_mday;
10256 	req->hour = tm.tm_hour;
10257 	req->minute = tm.tm_min;
10258 	req->second = tm.tm_sec;
10259 	return hwrm_req_send(bp, req);
10260 }
10261 
10262 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10263 {
10264 	u64 sw_tmp;
10265 
10266 	hw &= mask;
10267 	sw_tmp = (*sw & ~mask) | hw;
10268 	if (hw < (*sw & mask))
10269 		sw_tmp += mask + 1;
10270 	WRITE_ONCE(*sw, sw_tmp);
10271 }
10272 
10273 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10274 				    int count, bool ignore_zero)
10275 {
10276 	int i;
10277 
10278 	for (i = 0; i < count; i++) {
10279 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10280 
10281 		if (ignore_zero && !hw)
10282 			continue;
10283 
10284 		if (masks[i] == -1ULL)
10285 			sw_stats[i] = hw;
10286 		else
10287 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10288 	}
10289 }
10290 
10291 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10292 {
10293 	if (!stats->hw_stats)
10294 		return;
10295 
10296 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10297 				stats->hw_masks, stats->len / 8, false);
10298 }
10299 
10300 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10301 {
10302 	struct bnxt_stats_mem *ring0_stats;
10303 	bool ignore_zero = false;
10304 	int i;
10305 
10306 	/* Chip bug.  Counter intermittently becomes 0. */
10307 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10308 		ignore_zero = true;
10309 
10310 	for (i = 0; i < bp->cp_nr_rings; i++) {
10311 		struct bnxt_napi *bnapi = bp->bnapi[i];
10312 		struct bnxt_cp_ring_info *cpr;
10313 		struct bnxt_stats_mem *stats;
10314 
10315 		cpr = &bnapi->cp_ring;
10316 		stats = &cpr->stats;
10317 		if (!i)
10318 			ring0_stats = stats;
10319 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10320 					ring0_stats->hw_masks,
10321 					ring0_stats->len / 8, ignore_zero);
10322 	}
10323 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10324 		struct bnxt_stats_mem *stats = &bp->port_stats;
10325 		__le64 *hw_stats = stats->hw_stats;
10326 		u64 *sw_stats = stats->sw_stats;
10327 		u64 *masks = stats->hw_masks;
10328 		int cnt;
10329 
10330 		cnt = sizeof(struct rx_port_stats) / 8;
10331 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10332 
10333 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10334 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10335 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10336 		cnt = sizeof(struct tx_port_stats) / 8;
10337 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10338 	}
10339 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10340 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10341 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10342 	}
10343 }
10344 
10345 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10346 {
10347 	struct hwrm_port_qstats_input *req;
10348 	struct bnxt_pf_info *pf = &bp->pf;
10349 	int rc;
10350 
10351 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10352 		return 0;
10353 
10354 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10355 		return -EOPNOTSUPP;
10356 
10357 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10358 	if (rc)
10359 		return rc;
10360 
10361 	req->flags = flags;
10362 	req->port_id = cpu_to_le16(pf->port_id);
10363 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10364 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10365 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10366 	return hwrm_req_send(bp, req);
10367 }
10368 
10369 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10370 {
10371 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10372 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10373 	struct hwrm_port_qstats_ext_output *resp_qs;
10374 	struct hwrm_port_qstats_ext_input *req_qs;
10375 	struct bnxt_pf_info *pf = &bp->pf;
10376 	u32 tx_stat_size;
10377 	int rc;
10378 
10379 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10380 		return 0;
10381 
10382 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10383 		return -EOPNOTSUPP;
10384 
10385 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10386 	if (rc)
10387 		return rc;
10388 
10389 	req_qs->flags = flags;
10390 	req_qs->port_id = cpu_to_le16(pf->port_id);
10391 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10392 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10393 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10394 		       sizeof(struct tx_port_stats_ext) : 0;
10395 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10396 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10397 	resp_qs = hwrm_req_hold(bp, req_qs);
10398 	rc = hwrm_req_send(bp, req_qs);
10399 	if (!rc) {
10400 		bp->fw_rx_stats_ext_size =
10401 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10402 		if (BNXT_FW_MAJ(bp) < 220 &&
10403 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10404 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10405 
10406 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10407 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10408 	} else {
10409 		bp->fw_rx_stats_ext_size = 0;
10410 		bp->fw_tx_stats_ext_size = 0;
10411 	}
10412 	hwrm_req_drop(bp, req_qs);
10413 
10414 	if (flags)
10415 		return rc;
10416 
10417 	if (bp->fw_tx_stats_ext_size <=
10418 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10419 		bp->pri2cos_valid = 0;
10420 		return rc;
10421 	}
10422 
10423 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10424 	if (rc)
10425 		return rc;
10426 
10427 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10428 
10429 	resp_qc = hwrm_req_hold(bp, req_qc);
10430 	rc = hwrm_req_send(bp, req_qc);
10431 	if (!rc) {
10432 		u8 *pri2cos;
10433 		int i, j;
10434 
10435 		pri2cos = &resp_qc->pri0_cos_queue_id;
10436 		for (i = 0; i < 8; i++) {
10437 			u8 queue_id = pri2cos[i];
10438 			u8 queue_idx;
10439 
10440 			/* Per port queue IDs start from 0, 10, 20, etc */
10441 			queue_idx = queue_id % 10;
10442 			if (queue_idx > BNXT_MAX_QUEUE) {
10443 				bp->pri2cos_valid = false;
10444 				hwrm_req_drop(bp, req_qc);
10445 				return rc;
10446 			}
10447 			for (j = 0; j < bp->max_q; j++) {
10448 				if (bp->q_ids[j] == queue_id)
10449 					bp->pri2cos_idx[i] = queue_idx;
10450 			}
10451 		}
10452 		bp->pri2cos_valid = true;
10453 	}
10454 	hwrm_req_drop(bp, req_qc);
10455 
10456 	return rc;
10457 }
10458 
10459 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10460 {
10461 	bnxt_hwrm_tunnel_dst_port_free(bp,
10462 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10463 	bnxt_hwrm_tunnel_dst_port_free(bp,
10464 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10465 }
10466 
10467 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10468 {
10469 	int rc, i;
10470 	u32 tpa_flags = 0;
10471 
10472 	if (set_tpa)
10473 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10474 	else if (BNXT_NO_FW_ACCESS(bp))
10475 		return 0;
10476 	for (i = 0; i < bp->nr_vnics; i++) {
10477 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10478 		if (rc) {
10479 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10480 				   i, rc);
10481 			return rc;
10482 		}
10483 	}
10484 	return 0;
10485 }
10486 
10487 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10488 {
10489 	int i;
10490 
10491 	for (i = 0; i < bp->nr_vnics; i++)
10492 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10493 }
10494 
10495 static void bnxt_clear_vnic(struct bnxt *bp)
10496 {
10497 	if (!bp->vnic_info)
10498 		return;
10499 
10500 	bnxt_hwrm_clear_vnic_filter(bp);
10501 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10502 		/* clear all RSS setting before free vnic ctx */
10503 		bnxt_hwrm_clear_vnic_rss(bp);
10504 		bnxt_hwrm_vnic_ctx_free(bp);
10505 	}
10506 	/* before free the vnic, undo the vnic tpa settings */
10507 	if (bp->flags & BNXT_FLAG_TPA)
10508 		bnxt_set_tpa(bp, false);
10509 	bnxt_hwrm_vnic_free(bp);
10510 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10511 		bnxt_hwrm_vnic_ctx_free(bp);
10512 }
10513 
10514 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10515 				    bool irq_re_init)
10516 {
10517 	bnxt_clear_vnic(bp);
10518 	bnxt_hwrm_ring_free(bp, close_path);
10519 	bnxt_hwrm_ring_grp_free(bp);
10520 	if (irq_re_init) {
10521 		bnxt_hwrm_stat_ctx_free(bp);
10522 		bnxt_hwrm_free_tunnel_ports(bp);
10523 	}
10524 }
10525 
10526 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10527 {
10528 	struct hwrm_func_cfg_input *req;
10529 	u8 evb_mode;
10530 	int rc;
10531 
10532 	if (br_mode == BRIDGE_MODE_VEB)
10533 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10534 	else if (br_mode == BRIDGE_MODE_VEPA)
10535 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10536 	else
10537 		return -EINVAL;
10538 
10539 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10540 	if (rc)
10541 		return rc;
10542 
10543 	req->fid = cpu_to_le16(0xffff);
10544 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10545 	req->evb_mode = evb_mode;
10546 	return hwrm_req_send(bp, req);
10547 }
10548 
10549 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10550 {
10551 	struct hwrm_func_cfg_input *req;
10552 	int rc;
10553 
10554 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10555 		return 0;
10556 
10557 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10558 	if (rc)
10559 		return rc;
10560 
10561 	req->fid = cpu_to_le16(0xffff);
10562 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10563 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10564 	if (size == 128)
10565 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10566 
10567 	return hwrm_req_send(bp, req);
10568 }
10569 
10570 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10571 {
10572 	int rc;
10573 
10574 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10575 		goto skip_rss_ctx;
10576 
10577 	/* allocate context for vnic */
10578 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10579 	if (rc) {
10580 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10581 			   vnic->vnic_id, rc);
10582 		goto vnic_setup_err;
10583 	}
10584 	bp->rsscos_nr_ctxs++;
10585 
10586 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10587 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10588 		if (rc) {
10589 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10590 				   vnic->vnic_id, rc);
10591 			goto vnic_setup_err;
10592 		}
10593 		bp->rsscos_nr_ctxs++;
10594 	}
10595 
10596 skip_rss_ctx:
10597 	/* configure default vnic, ring grp */
10598 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10599 	if (rc) {
10600 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10601 			   vnic->vnic_id, rc);
10602 		goto vnic_setup_err;
10603 	}
10604 
10605 	/* Enable RSS hashing on vnic */
10606 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10607 	if (rc) {
10608 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10609 			   vnic->vnic_id, rc);
10610 		goto vnic_setup_err;
10611 	}
10612 
10613 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10614 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10615 		if (rc) {
10616 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10617 				   vnic->vnic_id, rc);
10618 		}
10619 	}
10620 
10621 vnic_setup_err:
10622 	return rc;
10623 }
10624 
10625 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10626 			  u8 valid)
10627 {
10628 	struct hwrm_vnic_update_input *req;
10629 	int rc;
10630 
10631 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10632 	if (rc)
10633 		return rc;
10634 
10635 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10636 
10637 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10638 		req->mru = cpu_to_le16(vnic->mru);
10639 
10640 	req->enables = cpu_to_le32(valid);
10641 
10642 	return hwrm_req_send(bp, req);
10643 }
10644 
10645 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10646 {
10647 	int rc;
10648 
10649 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10650 	if (rc) {
10651 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10652 			   vnic->vnic_id, rc);
10653 		return rc;
10654 	}
10655 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10656 	if (rc)
10657 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10658 			   vnic->vnic_id, rc);
10659 	return rc;
10660 }
10661 
10662 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10663 {
10664 	int rc, i, nr_ctxs;
10665 
10666 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10667 	for (i = 0; i < nr_ctxs; i++) {
10668 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10669 		if (rc) {
10670 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10671 				   vnic->vnic_id, i, rc);
10672 			break;
10673 		}
10674 		bp->rsscos_nr_ctxs++;
10675 	}
10676 	if (i < nr_ctxs)
10677 		return -ENOMEM;
10678 
10679 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10680 	if (rc)
10681 		return rc;
10682 
10683 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10684 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10685 		if (rc) {
10686 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10687 				   vnic->vnic_id, rc);
10688 		}
10689 	}
10690 	return rc;
10691 }
10692 
10693 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10694 {
10695 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10696 		return __bnxt_setup_vnic_p5(bp, vnic);
10697 	else
10698 		return __bnxt_setup_vnic(bp, vnic);
10699 }
10700 
10701 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10702 				     struct bnxt_vnic_info *vnic,
10703 				     u16 start_rx_ring_idx, int rx_rings)
10704 {
10705 	int rc;
10706 
10707 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10708 	if (rc) {
10709 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10710 			   vnic->vnic_id, rc);
10711 		return rc;
10712 	}
10713 	return bnxt_setup_vnic(bp, vnic);
10714 }
10715 
10716 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10717 {
10718 	struct bnxt_vnic_info *vnic;
10719 	int i, rc = 0;
10720 
10721 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10722 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10723 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10724 	}
10725 
10726 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10727 		return 0;
10728 
10729 	for (i = 0; i < bp->rx_nr_rings; i++) {
10730 		u16 vnic_id = i + 1;
10731 		u16 ring_id = i;
10732 
10733 		if (vnic_id >= bp->nr_vnics)
10734 			break;
10735 
10736 		vnic = &bp->vnic_info[vnic_id];
10737 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10738 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10739 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10740 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10741 			break;
10742 	}
10743 	return rc;
10744 }
10745 
10746 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10747 			  bool all)
10748 {
10749 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10750 	struct bnxt_filter_base *usr_fltr, *tmp;
10751 	struct bnxt_ntuple_filter *ntp_fltr;
10752 	int i;
10753 
10754 	if (netif_running(bp->dev)) {
10755 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10756 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10757 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10758 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10759 		}
10760 	}
10761 	if (!all)
10762 		return;
10763 
10764 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10765 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10766 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10767 			ntp_fltr = container_of(usr_fltr,
10768 						struct bnxt_ntuple_filter,
10769 						base);
10770 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10771 			bnxt_del_ntp_filter(bp, ntp_fltr);
10772 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10773 		}
10774 	}
10775 
10776 	if (vnic->rss_table)
10777 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10778 				  vnic->rss_table,
10779 				  vnic->rss_table_dma_addr);
10780 	bp->num_rss_ctx--;
10781 }
10782 
10783 static bool bnxt_vnic_has_rx_ring(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10784 				  int rxr_id)
10785 {
10786 	u16 tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
10787 	int i, vnic_rx;
10788 
10789 	/* Ntuple VNIC always has all the rx rings. Any change of ring id
10790 	 * must be updated because a future filter may use it.
10791 	 */
10792 	if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
10793 		return true;
10794 
10795 	for (i = 0; i < tbl_size; i++) {
10796 		if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
10797 			vnic_rx = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
10798 		else
10799 			vnic_rx = bp->rss_indir_tbl[i];
10800 
10801 		if (rxr_id == vnic_rx)
10802 			return true;
10803 	}
10804 
10805 	return false;
10806 }
10807 
10808 static int bnxt_set_vnic_mru_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10809 				u16 mru, int rxr_id)
10810 {
10811 	int rc;
10812 
10813 	if (!bnxt_vnic_has_rx_ring(bp, vnic, rxr_id))
10814 		return 0;
10815 
10816 	if (mru) {
10817 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10818 		if (rc) {
10819 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10820 				   vnic->vnic_id, rc);
10821 			return rc;
10822 		}
10823 	}
10824 	vnic->mru = mru;
10825 	bnxt_hwrm_vnic_update(bp, vnic,
10826 			      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
10827 
10828 	return 0;
10829 }
10830 
10831 static int bnxt_set_rss_ctx_vnic_mru(struct bnxt *bp, u16 mru, int rxr_id)
10832 {
10833 	struct ethtool_rxfh_context *ctx;
10834 	unsigned long context;
10835 	int rc;
10836 
10837 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10838 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10839 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10840 
10841 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, rxr_id);
10842 		if (rc)
10843 			return rc;
10844 	}
10845 
10846 	return 0;
10847 }
10848 
10849 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10850 {
10851 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10852 	struct ethtool_rxfh_context *ctx;
10853 	unsigned long context;
10854 
10855 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10856 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10857 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10858 
10859 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10860 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10861 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10862 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10863 				   rss_ctx->index);
10864 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10865 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10866 		}
10867 	}
10868 }
10869 
10870 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10871 {
10872 	struct ethtool_rxfh_context *ctx;
10873 	unsigned long context;
10874 
10875 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10876 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10877 
10878 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10879 	}
10880 }
10881 
10882 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10883 static bool bnxt_promisc_ok(struct bnxt *bp)
10884 {
10885 #ifdef CONFIG_BNXT_SRIOV
10886 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10887 		return false;
10888 #endif
10889 	return true;
10890 }
10891 
10892 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10893 {
10894 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10895 	unsigned int rc = 0;
10896 
10897 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10898 	if (rc) {
10899 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10900 			   rc);
10901 		return rc;
10902 	}
10903 
10904 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10905 	if (rc) {
10906 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10907 			   rc);
10908 		return rc;
10909 	}
10910 	return rc;
10911 }
10912 
10913 static int bnxt_cfg_rx_mode(struct bnxt *);
10914 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10915 
10916 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10917 {
10918 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10919 	int rc = 0;
10920 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10921 
10922 	if (irq_re_init) {
10923 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10924 		if (rc) {
10925 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10926 				   rc);
10927 			goto err_out;
10928 		}
10929 	}
10930 
10931 	rc = bnxt_hwrm_ring_alloc(bp);
10932 	if (rc) {
10933 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10934 		goto err_out;
10935 	}
10936 
10937 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10938 	if (rc) {
10939 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10940 		goto err_out;
10941 	}
10942 
10943 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10944 		rx_nr_rings--;
10945 
10946 	/* default vnic 0 */
10947 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10948 	if (rc) {
10949 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10950 		goto err_out;
10951 	}
10952 
10953 	if (BNXT_VF(bp))
10954 		bnxt_hwrm_func_qcfg(bp);
10955 
10956 	rc = bnxt_setup_vnic(bp, vnic);
10957 	if (rc)
10958 		goto err_out;
10959 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10960 		bnxt_hwrm_update_rss_hash_cfg(bp);
10961 
10962 	if (bp->flags & BNXT_FLAG_RFS) {
10963 		rc = bnxt_alloc_rfs_vnics(bp);
10964 		if (rc)
10965 			goto err_out;
10966 	}
10967 
10968 	if (bp->flags & BNXT_FLAG_TPA) {
10969 		rc = bnxt_set_tpa(bp, true);
10970 		if (rc)
10971 			goto err_out;
10972 	}
10973 
10974 	if (BNXT_VF(bp))
10975 		bnxt_update_vf_mac(bp);
10976 
10977 	/* Filter for default vnic 0 */
10978 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10979 	if (rc) {
10980 		if (BNXT_VF(bp) && rc == -ENODEV)
10981 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10982 		else
10983 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10984 		goto err_out;
10985 	}
10986 	vnic->uc_filter_count = 1;
10987 
10988 	vnic->rx_mask = 0;
10989 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10990 		goto skip_rx_mask;
10991 
10992 	if (bp->dev->flags & IFF_BROADCAST)
10993 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10994 
10995 	if (bp->dev->flags & IFF_PROMISC)
10996 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10997 
10998 	if (bp->dev->flags & IFF_ALLMULTI) {
10999 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
11000 		vnic->mc_list_count = 0;
11001 	} else if (bp->dev->flags & IFF_MULTICAST) {
11002 		u32 mask = 0;
11003 
11004 		bnxt_mc_list_updated(bp, &mask);
11005 		vnic->rx_mask |= mask;
11006 	}
11007 
11008 	rc = bnxt_cfg_rx_mode(bp);
11009 	if (rc)
11010 		goto err_out;
11011 
11012 skip_rx_mask:
11013 	rc = bnxt_hwrm_set_coal(bp);
11014 	if (rc)
11015 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
11016 				rc);
11017 
11018 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11019 		rc = bnxt_setup_nitroa0_vnic(bp);
11020 		if (rc)
11021 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
11022 				   rc);
11023 	}
11024 
11025 	if (BNXT_VF(bp)) {
11026 		bnxt_hwrm_func_qcfg(bp);
11027 		netdev_update_features(bp->dev);
11028 	}
11029 
11030 	return 0;
11031 
11032 err_out:
11033 	bnxt_hwrm_resource_free(bp, 0, true);
11034 
11035 	return rc;
11036 }
11037 
11038 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
11039 {
11040 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
11041 	return 0;
11042 }
11043 
11044 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
11045 {
11046 	bnxt_init_cp_rings(bp);
11047 	bnxt_init_rx_rings(bp);
11048 	bnxt_init_tx_rings(bp);
11049 	bnxt_init_ring_grps(bp, irq_re_init);
11050 	bnxt_init_vnics(bp);
11051 
11052 	return bnxt_init_chip(bp, irq_re_init);
11053 }
11054 
11055 static int bnxt_set_real_num_queues(struct bnxt *bp)
11056 {
11057 	int rc;
11058 	struct net_device *dev = bp->dev;
11059 
11060 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
11061 					  bp->tx_nr_rings_xdp);
11062 	if (rc)
11063 		return rc;
11064 
11065 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
11066 	if (rc)
11067 		return rc;
11068 
11069 #ifdef CONFIG_RFS_ACCEL
11070 	if (bp->flags & BNXT_FLAG_RFS)
11071 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11072 #endif
11073 
11074 	return rc;
11075 }
11076 
11077 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11078 			     bool shared)
11079 {
11080 	int _rx = *rx, _tx = *tx;
11081 
11082 	if (shared) {
11083 		*rx = min_t(int, _rx, max);
11084 		*tx = min_t(int, _tx, max);
11085 	} else {
11086 		if (max < 2)
11087 			return -ENOMEM;
11088 
11089 		while (_rx + _tx > max) {
11090 			if (_rx > _tx && _rx > 1)
11091 				_rx--;
11092 			else if (_tx > 1)
11093 				_tx--;
11094 		}
11095 		*rx = _rx;
11096 		*tx = _tx;
11097 	}
11098 	return 0;
11099 }
11100 
11101 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11102 {
11103 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11104 }
11105 
11106 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11107 {
11108 	int tcs = bp->num_tc;
11109 
11110 	if (!tcs)
11111 		tcs = 1;
11112 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11113 }
11114 
11115 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11116 {
11117 	int tcs = bp->num_tc;
11118 
11119 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11120 	       bp->tx_nr_rings_xdp;
11121 }
11122 
11123 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11124 			   bool sh)
11125 {
11126 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11127 
11128 	if (tx_cp != *tx) {
11129 		int tx_saved = tx_cp, rc;
11130 
11131 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11132 		if (rc)
11133 			return rc;
11134 		if (tx_cp != tx_saved)
11135 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11136 		return 0;
11137 	}
11138 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11139 }
11140 
11141 static void bnxt_setup_msix(struct bnxt *bp)
11142 {
11143 	const int len = sizeof(bp->irq_tbl[0].name);
11144 	struct net_device *dev = bp->dev;
11145 	int tcs, i;
11146 
11147 	tcs = bp->num_tc;
11148 	if (tcs) {
11149 		int i, off, count;
11150 
11151 		for (i = 0; i < tcs; i++) {
11152 			count = bp->tx_nr_rings_per_tc;
11153 			off = BNXT_TC_TO_RING_BASE(bp, i);
11154 			netdev_set_tc_queue(dev, i, count, off);
11155 		}
11156 	}
11157 
11158 	for (i = 0; i < bp->cp_nr_rings; i++) {
11159 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11160 		char *attr;
11161 
11162 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11163 			attr = "TxRx";
11164 		else if (i < bp->rx_nr_rings)
11165 			attr = "rx";
11166 		else
11167 			attr = "tx";
11168 
11169 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11170 			 attr, i);
11171 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11172 	}
11173 }
11174 
11175 static int bnxt_init_int_mode(struct bnxt *bp);
11176 
11177 static int bnxt_change_msix(struct bnxt *bp, int total)
11178 {
11179 	struct msi_map map;
11180 	int i;
11181 
11182 	/* add MSIX to the end if needed */
11183 	for (i = bp->total_irqs; i < total; i++) {
11184 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11185 		if (map.index < 0)
11186 			return bp->total_irqs;
11187 		bp->irq_tbl[i].vector = map.virq;
11188 		bp->total_irqs++;
11189 	}
11190 
11191 	/* trim MSIX from the end if needed */
11192 	for (i = bp->total_irqs; i > total; i--) {
11193 		map.index = i - 1;
11194 		map.virq = bp->irq_tbl[i - 1].vector;
11195 		pci_msix_free_irq(bp->pdev, map);
11196 		bp->total_irqs--;
11197 	}
11198 	return bp->total_irqs;
11199 }
11200 
11201 static int bnxt_setup_int_mode(struct bnxt *bp)
11202 {
11203 	int rc;
11204 
11205 	if (!bp->irq_tbl) {
11206 		rc = bnxt_init_int_mode(bp);
11207 		if (rc || !bp->irq_tbl)
11208 			return rc ?: -ENODEV;
11209 	}
11210 
11211 	bnxt_setup_msix(bp);
11212 
11213 	rc = bnxt_set_real_num_queues(bp);
11214 	return rc;
11215 }
11216 
11217 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11218 {
11219 	return bp->hw_resc.max_rsscos_ctxs;
11220 }
11221 
11222 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11223 {
11224 	return bp->hw_resc.max_vnics;
11225 }
11226 
11227 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11228 {
11229 	return bp->hw_resc.max_stat_ctxs;
11230 }
11231 
11232 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11233 {
11234 	return bp->hw_resc.max_cp_rings;
11235 }
11236 
11237 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11238 {
11239 	unsigned int cp = bp->hw_resc.max_cp_rings;
11240 
11241 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11242 		cp -= bnxt_get_ulp_msix_num(bp);
11243 
11244 	return cp;
11245 }
11246 
11247 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11248 {
11249 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11250 
11251 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11252 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11253 
11254 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11255 }
11256 
11257 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11258 {
11259 	bp->hw_resc.max_irqs = max_irqs;
11260 }
11261 
11262 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11263 {
11264 	unsigned int cp;
11265 
11266 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11267 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11268 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11269 	else
11270 		return cp - bp->cp_nr_rings;
11271 }
11272 
11273 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11274 {
11275 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11276 }
11277 
11278 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11279 {
11280 	int max_irq = bnxt_get_max_func_irqs(bp);
11281 	int total_req = bp->cp_nr_rings + num;
11282 
11283 	if (max_irq < total_req) {
11284 		num = max_irq - bp->cp_nr_rings;
11285 		if (num <= 0)
11286 			return 0;
11287 	}
11288 	return num;
11289 }
11290 
11291 static int bnxt_get_num_msix(struct bnxt *bp)
11292 {
11293 	if (!BNXT_NEW_RM(bp))
11294 		return bnxt_get_max_func_irqs(bp);
11295 
11296 	return bnxt_nq_rings_in_use(bp);
11297 }
11298 
11299 static int bnxt_init_int_mode(struct bnxt *bp)
11300 {
11301 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11302 
11303 	total_vecs = bnxt_get_num_msix(bp);
11304 	max = bnxt_get_max_func_irqs(bp);
11305 	if (total_vecs > max)
11306 		total_vecs = max;
11307 
11308 	if (!total_vecs)
11309 		return 0;
11310 
11311 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11312 		min = 2;
11313 
11314 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11315 					   PCI_IRQ_MSIX);
11316 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11317 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11318 		rc = -ENODEV;
11319 		goto msix_setup_exit;
11320 	}
11321 
11322 	tbl_size = total_vecs;
11323 	if (pci_msix_can_alloc_dyn(bp->pdev))
11324 		tbl_size = max;
11325 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11326 	if (bp->irq_tbl) {
11327 		for (i = 0; i < total_vecs; i++)
11328 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11329 
11330 		bp->total_irqs = total_vecs;
11331 		/* Trim rings based upon num of vectors allocated */
11332 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11333 				     total_vecs - ulp_msix, min == 1);
11334 		if (rc)
11335 			goto msix_setup_exit;
11336 
11337 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11338 		bp->cp_nr_rings = (min == 1) ?
11339 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11340 				  tx_cp + bp->rx_nr_rings;
11341 
11342 	} else {
11343 		rc = -ENOMEM;
11344 		goto msix_setup_exit;
11345 	}
11346 	return 0;
11347 
11348 msix_setup_exit:
11349 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11350 	kfree(bp->irq_tbl);
11351 	bp->irq_tbl = NULL;
11352 	pci_free_irq_vectors(bp->pdev);
11353 	return rc;
11354 }
11355 
11356 static void bnxt_clear_int_mode(struct bnxt *bp)
11357 {
11358 	pci_free_irq_vectors(bp->pdev);
11359 
11360 	kfree(bp->irq_tbl);
11361 	bp->irq_tbl = NULL;
11362 }
11363 
11364 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11365 {
11366 	bool irq_cleared = false;
11367 	bool irq_change = false;
11368 	int tcs = bp->num_tc;
11369 	int irqs_required;
11370 	int rc;
11371 
11372 	if (!bnxt_need_reserve_rings(bp))
11373 		return 0;
11374 
11375 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11376 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11377 
11378 		if (ulp_msix > bp->ulp_num_msix_want)
11379 			ulp_msix = bp->ulp_num_msix_want;
11380 		irqs_required = ulp_msix + bp->cp_nr_rings;
11381 	} else {
11382 		irqs_required = bnxt_get_num_msix(bp);
11383 	}
11384 
11385 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11386 		irq_change = true;
11387 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11388 			bnxt_ulp_irq_stop(bp);
11389 			bnxt_clear_int_mode(bp);
11390 			irq_cleared = true;
11391 		}
11392 	}
11393 	rc = __bnxt_reserve_rings(bp);
11394 	if (irq_cleared) {
11395 		if (!rc)
11396 			rc = bnxt_init_int_mode(bp);
11397 		bnxt_ulp_irq_restart(bp, rc);
11398 	} else if (irq_change && !rc) {
11399 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11400 			rc = -ENOSPC;
11401 	}
11402 	if (rc) {
11403 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11404 		return rc;
11405 	}
11406 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11407 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11408 		netdev_err(bp->dev, "tx ring reservation failure\n");
11409 		netdev_reset_tc(bp->dev);
11410 		bp->num_tc = 0;
11411 		if (bp->tx_nr_rings_xdp)
11412 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11413 		else
11414 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11415 		return -ENOMEM;
11416 	}
11417 	return 0;
11418 }
11419 
11420 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11421 {
11422 	struct bnxt_tx_ring_info *txr;
11423 	struct netdev_queue *txq;
11424 	struct bnxt_napi *bnapi;
11425 	int i;
11426 
11427 	bnapi = bp->bnapi[idx];
11428 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11429 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11430 		synchronize_net();
11431 
11432 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11433 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11434 			if (txq) {
11435 				__netif_tx_lock_bh(txq);
11436 				netif_tx_stop_queue(txq);
11437 				__netif_tx_unlock_bh(txq);
11438 			}
11439 		}
11440 
11441 		if (!bp->tph_mode)
11442 			continue;
11443 
11444 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11445 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11446 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11447 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11448 	}
11449 }
11450 
11451 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11452 {
11453 	struct bnxt_tx_ring_info *txr;
11454 	struct netdev_queue *txq;
11455 	struct bnxt_napi *bnapi;
11456 	int rc, i;
11457 
11458 	bnapi = bp->bnapi[idx];
11459 	/* All rings have been reserved and previously allocated.
11460 	 * Reallocating with the same parameters should never fail.
11461 	 */
11462 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11463 		if (!bp->tph_mode)
11464 			goto start_tx;
11465 
11466 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11467 		if (rc)
11468 			return rc;
11469 
11470 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11471 		if (rc)
11472 			return rc;
11473 
11474 		txr->tx_prod = 0;
11475 		txr->tx_cons = 0;
11476 		txr->tx_hw_cons = 0;
11477 start_tx:
11478 		WRITE_ONCE(txr->dev_state, 0);
11479 		synchronize_net();
11480 
11481 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11482 			continue;
11483 
11484 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11485 		if (txq)
11486 			netif_tx_start_queue(txq);
11487 	}
11488 
11489 	return 0;
11490 }
11491 
11492 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11493 				     const cpumask_t *mask)
11494 {
11495 	struct bnxt_irq *irq;
11496 	u16 tag;
11497 	int err;
11498 
11499 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11500 
11501 	if (!irq->bp->tph_mode)
11502 		return;
11503 
11504 	cpumask_copy(irq->cpu_mask, mask);
11505 
11506 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11507 		return;
11508 
11509 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11510 				cpumask_first(irq->cpu_mask), &tag))
11511 		return;
11512 
11513 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11514 		return;
11515 
11516 	netdev_lock(irq->bp->dev);
11517 	if (netif_running(irq->bp->dev)) {
11518 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11519 		if (err)
11520 			netdev_err(irq->bp->dev,
11521 				   "RX queue restart failed: err=%d\n", err);
11522 	}
11523 	netdev_unlock(irq->bp->dev);
11524 }
11525 
11526 static void bnxt_irq_affinity_release(struct kref *ref)
11527 {
11528 	struct irq_affinity_notify *notify =
11529 		container_of(ref, struct irq_affinity_notify, kref);
11530 	struct bnxt_irq *irq;
11531 
11532 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11533 
11534 	if (!irq->bp->tph_mode)
11535 		return;
11536 
11537 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11538 		netdev_err(irq->bp->dev,
11539 			   "Setting ST=0 for MSIX entry %d failed\n",
11540 			   irq->msix_nr);
11541 		return;
11542 	}
11543 }
11544 
11545 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11546 {
11547 	irq_set_affinity_notifier(irq->vector, NULL);
11548 }
11549 
11550 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11551 {
11552 	struct irq_affinity_notify *notify;
11553 
11554 	irq->bp = bp;
11555 
11556 	/* Nothing to do if TPH is not enabled */
11557 	if (!bp->tph_mode)
11558 		return;
11559 
11560 	/* Register IRQ affinity notifier */
11561 	notify = &irq->affinity_notify;
11562 	notify->irq = irq->vector;
11563 	notify->notify = bnxt_irq_affinity_notify;
11564 	notify->release = bnxt_irq_affinity_release;
11565 
11566 	irq_set_affinity_notifier(irq->vector, notify);
11567 }
11568 
11569 static void bnxt_free_irq(struct bnxt *bp)
11570 {
11571 	struct bnxt_irq *irq;
11572 	int i;
11573 
11574 #ifdef CONFIG_RFS_ACCEL
11575 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11576 	bp->dev->rx_cpu_rmap = NULL;
11577 #endif
11578 	if (!bp->irq_tbl || !bp->bnapi)
11579 		return;
11580 
11581 	for (i = 0; i < bp->cp_nr_rings; i++) {
11582 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11583 
11584 		irq = &bp->irq_tbl[map_idx];
11585 		if (irq->requested) {
11586 			if (irq->have_cpumask) {
11587 				irq_update_affinity_hint(irq->vector, NULL);
11588 				free_cpumask_var(irq->cpu_mask);
11589 				irq->have_cpumask = 0;
11590 			}
11591 
11592 			bnxt_release_irq_notifier(irq);
11593 
11594 			free_irq(irq->vector, bp->bnapi[i]);
11595 		}
11596 
11597 		irq->requested = 0;
11598 	}
11599 
11600 	/* Disable TPH support */
11601 	pcie_disable_tph(bp->pdev);
11602 	bp->tph_mode = 0;
11603 }
11604 
11605 static int bnxt_request_irq(struct bnxt *bp)
11606 {
11607 	int i, j, rc = 0;
11608 	unsigned long flags = 0;
11609 #ifdef CONFIG_RFS_ACCEL
11610 	struct cpu_rmap *rmap;
11611 #endif
11612 
11613 	rc = bnxt_setup_int_mode(bp);
11614 	if (rc) {
11615 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11616 			   rc);
11617 		return rc;
11618 	}
11619 #ifdef CONFIG_RFS_ACCEL
11620 	rmap = bp->dev->rx_cpu_rmap;
11621 #endif
11622 
11623 	/* Enable TPH support as part of IRQ request */
11624 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11625 	if (!rc)
11626 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11627 
11628 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11629 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11630 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11631 
11632 #ifdef CONFIG_RFS_ACCEL
11633 		if (rmap && bp->bnapi[i]->rx_ring) {
11634 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11635 			if (rc)
11636 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11637 					    j);
11638 			j++;
11639 		}
11640 #endif
11641 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11642 				 bp->bnapi[i]);
11643 		if (rc)
11644 			break;
11645 
11646 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11647 		irq->requested = 1;
11648 
11649 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11650 			int numa_node = dev_to_node(&bp->pdev->dev);
11651 			u16 tag;
11652 
11653 			irq->have_cpumask = 1;
11654 			irq->msix_nr = map_idx;
11655 			irq->ring_nr = i;
11656 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11657 					irq->cpu_mask);
11658 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11659 			if (rc) {
11660 				netdev_warn(bp->dev,
11661 					    "Update affinity hint failed, IRQ = %d\n",
11662 					    irq->vector);
11663 				break;
11664 			}
11665 
11666 			bnxt_register_irq_notifier(bp, irq);
11667 
11668 			/* Init ST table entry */
11669 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11670 						cpumask_first(irq->cpu_mask),
11671 						&tag))
11672 				continue;
11673 
11674 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11675 		}
11676 	}
11677 	return rc;
11678 }
11679 
11680 static void bnxt_del_napi(struct bnxt *bp)
11681 {
11682 	int i;
11683 
11684 	if (!bp->bnapi)
11685 		return;
11686 
11687 	for (i = 0; i < bp->rx_nr_rings; i++)
11688 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11689 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11690 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11691 
11692 	for (i = 0; i < bp->cp_nr_rings; i++) {
11693 		struct bnxt_napi *bnapi = bp->bnapi[i];
11694 
11695 		__netif_napi_del_locked(&bnapi->napi);
11696 	}
11697 	/* We called __netif_napi_del_locked(), we need
11698 	 * to respect an RCU grace period before freeing napi structures.
11699 	 */
11700 	synchronize_net();
11701 }
11702 
11703 static void bnxt_init_napi(struct bnxt *bp)
11704 {
11705 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11706 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11707 	struct bnxt_napi *bnapi;
11708 	int i;
11709 
11710 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11711 		poll_fn = bnxt_poll_p5;
11712 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11713 		cp_nr_rings--;
11714 
11715 	set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11716 
11717 	for (i = 0; i < cp_nr_rings; i++) {
11718 		bnapi = bp->bnapi[i];
11719 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11720 					     bnapi->index);
11721 	}
11722 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11723 		bnapi = bp->bnapi[cp_nr_rings];
11724 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11725 	}
11726 }
11727 
11728 static void bnxt_disable_napi(struct bnxt *bp)
11729 {
11730 	int i;
11731 
11732 	if (!bp->bnapi ||
11733 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11734 		return;
11735 
11736 	for (i = 0; i < bp->cp_nr_rings; i++) {
11737 		struct bnxt_napi *bnapi = bp->bnapi[i];
11738 		struct bnxt_cp_ring_info *cpr;
11739 
11740 		cpr = &bnapi->cp_ring;
11741 		if (bnapi->tx_fault)
11742 			cpr->sw_stats->tx.tx_resets++;
11743 		if (bnapi->in_reset)
11744 			cpr->sw_stats->rx.rx_resets++;
11745 		napi_disable_locked(&bnapi->napi);
11746 	}
11747 }
11748 
11749 static void bnxt_enable_napi(struct bnxt *bp)
11750 {
11751 	int i;
11752 
11753 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11754 	for (i = 0; i < bp->cp_nr_rings; i++) {
11755 		struct bnxt_napi *bnapi = bp->bnapi[i];
11756 		struct bnxt_cp_ring_info *cpr;
11757 
11758 		bnapi->tx_fault = 0;
11759 
11760 		cpr = &bnapi->cp_ring;
11761 		bnapi->in_reset = false;
11762 
11763 		if (bnapi->rx_ring) {
11764 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11765 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11766 		}
11767 		napi_enable_locked(&bnapi->napi);
11768 	}
11769 }
11770 
11771 void bnxt_tx_disable(struct bnxt *bp)
11772 {
11773 	int i;
11774 	struct bnxt_tx_ring_info *txr;
11775 
11776 	if (bp->tx_ring) {
11777 		for (i = 0; i < bp->tx_nr_rings; i++) {
11778 			txr = &bp->tx_ring[i];
11779 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11780 		}
11781 	}
11782 	/* Make sure napi polls see @dev_state change */
11783 	synchronize_net();
11784 	/* Drop carrier first to prevent TX timeout */
11785 	netif_carrier_off(bp->dev);
11786 	/* Stop all TX queues */
11787 	netif_tx_disable(bp->dev);
11788 }
11789 
11790 void bnxt_tx_enable(struct bnxt *bp)
11791 {
11792 	int i;
11793 	struct bnxt_tx_ring_info *txr;
11794 
11795 	for (i = 0; i < bp->tx_nr_rings; i++) {
11796 		txr = &bp->tx_ring[i];
11797 		WRITE_ONCE(txr->dev_state, 0);
11798 	}
11799 	/* Make sure napi polls see @dev_state change */
11800 	synchronize_net();
11801 	netif_tx_wake_all_queues(bp->dev);
11802 	if (BNXT_LINK_IS_UP(bp))
11803 		netif_carrier_on(bp->dev);
11804 }
11805 
11806 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11807 {
11808 	u8 active_fec = link_info->active_fec_sig_mode &
11809 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11810 
11811 	switch (active_fec) {
11812 	default:
11813 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11814 		return "None";
11815 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11816 		return "Clause 74 BaseR";
11817 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11818 		return "Clause 91 RS(528,514)";
11819 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11820 		return "Clause 91 RS544_1XN";
11821 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11822 		return "Clause 91 RS(544,514)";
11823 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11824 		return "Clause 91 RS272_1XN";
11825 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11826 		return "Clause 91 RS(272,257)";
11827 	}
11828 }
11829 
11830 void bnxt_report_link(struct bnxt *bp)
11831 {
11832 	if (BNXT_LINK_IS_UP(bp)) {
11833 		const char *signal = "";
11834 		const char *flow_ctrl;
11835 		const char *duplex;
11836 		u32 speed;
11837 		u16 fec;
11838 
11839 		netif_carrier_on(bp->dev);
11840 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11841 		if (speed == SPEED_UNKNOWN) {
11842 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11843 			return;
11844 		}
11845 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11846 			duplex = "full";
11847 		else
11848 			duplex = "half";
11849 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11850 			flow_ctrl = "ON - receive & transmit";
11851 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11852 			flow_ctrl = "ON - transmit";
11853 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11854 			flow_ctrl = "ON - receive";
11855 		else
11856 			flow_ctrl = "none";
11857 		if (bp->link_info.phy_qcfg_resp.option_flags &
11858 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11859 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11860 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11861 			switch (sig_mode) {
11862 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11863 				signal = "(NRZ) ";
11864 				break;
11865 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11866 				signal = "(PAM4 56Gbps) ";
11867 				break;
11868 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11869 				signal = "(PAM4 112Gbps) ";
11870 				break;
11871 			default:
11872 				break;
11873 			}
11874 		}
11875 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11876 			    speed, signal, duplex, flow_ctrl);
11877 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11878 			netdev_info(bp->dev, "EEE is %s\n",
11879 				    bp->eee.eee_active ? "active" :
11880 							 "not active");
11881 		fec = bp->link_info.fec_cfg;
11882 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11883 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11884 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11885 				    bnxt_report_fec(&bp->link_info));
11886 	} else {
11887 		netif_carrier_off(bp->dev);
11888 		netdev_err(bp->dev, "NIC Link is Down\n");
11889 	}
11890 }
11891 
11892 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11893 {
11894 	if (!resp->supported_speeds_auto_mode &&
11895 	    !resp->supported_speeds_force_mode &&
11896 	    !resp->supported_pam4_speeds_auto_mode &&
11897 	    !resp->supported_pam4_speeds_force_mode &&
11898 	    !resp->supported_speeds2_auto_mode &&
11899 	    !resp->supported_speeds2_force_mode)
11900 		return true;
11901 	return false;
11902 }
11903 
11904 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11905 {
11906 	struct bnxt_link_info *link_info = &bp->link_info;
11907 	struct hwrm_port_phy_qcaps_output *resp;
11908 	struct hwrm_port_phy_qcaps_input *req;
11909 	int rc = 0;
11910 
11911 	if (bp->hwrm_spec_code < 0x10201)
11912 		return 0;
11913 
11914 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11915 	if (rc)
11916 		return rc;
11917 
11918 	resp = hwrm_req_hold(bp, req);
11919 	rc = hwrm_req_send(bp, req);
11920 	if (rc)
11921 		goto hwrm_phy_qcaps_exit;
11922 
11923 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11924 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11925 		struct ethtool_keee *eee = &bp->eee;
11926 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11927 
11928 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11929 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11930 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11931 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11932 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11933 	}
11934 
11935 	if (bp->hwrm_spec_code >= 0x10a01) {
11936 		if (bnxt_phy_qcaps_no_speed(resp)) {
11937 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11938 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11939 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11940 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11941 			netdev_info(bp->dev, "Ethernet link enabled\n");
11942 			/* Phy re-enabled, reprobe the speeds */
11943 			link_info->support_auto_speeds = 0;
11944 			link_info->support_pam4_auto_speeds = 0;
11945 			link_info->support_auto_speeds2 = 0;
11946 		}
11947 	}
11948 	if (resp->supported_speeds_auto_mode)
11949 		link_info->support_auto_speeds =
11950 			le16_to_cpu(resp->supported_speeds_auto_mode);
11951 	if (resp->supported_pam4_speeds_auto_mode)
11952 		link_info->support_pam4_auto_speeds =
11953 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11954 	if (resp->supported_speeds2_auto_mode)
11955 		link_info->support_auto_speeds2 =
11956 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11957 
11958 	bp->port_count = resp->port_cnt;
11959 
11960 hwrm_phy_qcaps_exit:
11961 	hwrm_req_drop(bp, req);
11962 	return rc;
11963 }
11964 
11965 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11966 {
11967 	struct hwrm_port_mac_qcaps_output *resp;
11968 	struct hwrm_port_mac_qcaps_input *req;
11969 	int rc;
11970 
11971 	if (bp->hwrm_spec_code < 0x10a03)
11972 		return;
11973 
11974 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11975 	if (rc)
11976 		return;
11977 
11978 	resp = hwrm_req_hold(bp, req);
11979 	rc = hwrm_req_send_silent(bp, req);
11980 	if (!rc)
11981 		bp->mac_flags = resp->flags;
11982 	hwrm_req_drop(bp, req);
11983 }
11984 
11985 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11986 {
11987 	u16 diff = advertising ^ supported;
11988 
11989 	return ((supported | diff) != supported);
11990 }
11991 
11992 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11993 {
11994 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11995 
11996 	/* Check if any advertised speeds are no longer supported. The caller
11997 	 * holds the link_lock mutex, so we can modify link_info settings.
11998 	 */
11999 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12000 		if (bnxt_support_dropped(link_info->advertising,
12001 					 link_info->support_auto_speeds2)) {
12002 			link_info->advertising = link_info->support_auto_speeds2;
12003 			return true;
12004 		}
12005 		return false;
12006 	}
12007 	if (bnxt_support_dropped(link_info->advertising,
12008 				 link_info->support_auto_speeds)) {
12009 		link_info->advertising = link_info->support_auto_speeds;
12010 		return true;
12011 	}
12012 	if (bnxt_support_dropped(link_info->advertising_pam4,
12013 				 link_info->support_pam4_auto_speeds)) {
12014 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
12015 		return true;
12016 	}
12017 	return false;
12018 }
12019 
12020 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
12021 {
12022 	struct bnxt_link_info *link_info = &bp->link_info;
12023 	struct hwrm_port_phy_qcfg_output *resp;
12024 	struct hwrm_port_phy_qcfg_input *req;
12025 	u8 link_state = link_info->link_state;
12026 	bool support_changed;
12027 	int rc;
12028 
12029 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
12030 	if (rc)
12031 		return rc;
12032 
12033 	resp = hwrm_req_hold(bp, req);
12034 	rc = hwrm_req_send(bp, req);
12035 	if (rc) {
12036 		hwrm_req_drop(bp, req);
12037 		if (BNXT_VF(bp) && rc == -ENODEV) {
12038 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
12039 			rc = 0;
12040 		}
12041 		return rc;
12042 	}
12043 
12044 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
12045 	link_info->phy_link_status = resp->link;
12046 	link_info->duplex = resp->duplex_cfg;
12047 	if (bp->hwrm_spec_code >= 0x10800)
12048 		link_info->duplex = resp->duplex_state;
12049 	link_info->pause = resp->pause;
12050 	link_info->auto_mode = resp->auto_mode;
12051 	link_info->auto_pause_setting = resp->auto_pause;
12052 	link_info->lp_pause = resp->link_partner_adv_pause;
12053 	link_info->force_pause_setting = resp->force_pause;
12054 	link_info->duplex_setting = resp->duplex_cfg;
12055 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
12056 		link_info->link_speed = le16_to_cpu(resp->link_speed);
12057 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
12058 			link_info->active_lanes = resp->active_lanes;
12059 	} else {
12060 		link_info->link_speed = 0;
12061 		link_info->active_lanes = 0;
12062 	}
12063 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
12064 	link_info->force_pam4_link_speed =
12065 		le16_to_cpu(resp->force_pam4_link_speed);
12066 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
12067 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
12068 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
12069 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
12070 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
12071 	link_info->auto_pam4_link_speeds =
12072 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12073 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12074 	link_info->lp_auto_link_speeds =
12075 		le16_to_cpu(resp->link_partner_adv_speeds);
12076 	link_info->lp_auto_pam4_link_speeds =
12077 		resp->link_partner_pam4_adv_speeds;
12078 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12079 	link_info->phy_ver[0] = resp->phy_maj;
12080 	link_info->phy_ver[1] = resp->phy_min;
12081 	link_info->phy_ver[2] = resp->phy_bld;
12082 	link_info->media_type = resp->media_type;
12083 	link_info->phy_type = resp->phy_type;
12084 	link_info->transceiver = resp->xcvr_pkg_type;
12085 	link_info->phy_addr = resp->eee_config_phy_addr &
12086 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12087 	link_info->module_status = resp->module_status;
12088 
12089 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12090 		struct ethtool_keee *eee = &bp->eee;
12091 		u16 fw_speeds;
12092 
12093 		eee->eee_active = 0;
12094 		if (resp->eee_config_phy_addr &
12095 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12096 			eee->eee_active = 1;
12097 			fw_speeds = le16_to_cpu(
12098 				resp->link_partner_adv_eee_link_speed_mask);
12099 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12100 		}
12101 
12102 		/* Pull initial EEE config */
12103 		if (!chng_link_state) {
12104 			if (resp->eee_config_phy_addr &
12105 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12106 				eee->eee_enabled = 1;
12107 
12108 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12109 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12110 
12111 			if (resp->eee_config_phy_addr &
12112 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12113 				__le32 tmr;
12114 
12115 				eee->tx_lpi_enabled = 1;
12116 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12117 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12118 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12119 			}
12120 		}
12121 	}
12122 
12123 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12124 	if (bp->hwrm_spec_code >= 0x10504) {
12125 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12126 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12127 	}
12128 	/* TODO: need to add more logic to report VF link */
12129 	if (chng_link_state) {
12130 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12131 			link_info->link_state = BNXT_LINK_STATE_UP;
12132 		else
12133 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12134 		if (link_state != link_info->link_state)
12135 			bnxt_report_link(bp);
12136 	} else {
12137 		/* always link down if not require to update link state */
12138 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12139 	}
12140 	hwrm_req_drop(bp, req);
12141 
12142 	if (!BNXT_PHY_CFG_ABLE(bp))
12143 		return 0;
12144 
12145 	support_changed = bnxt_support_speed_dropped(link_info);
12146 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12147 		bnxt_hwrm_set_link_setting(bp, true, false);
12148 	return 0;
12149 }
12150 
12151 static void bnxt_get_port_module_status(struct bnxt *bp)
12152 {
12153 	struct bnxt_link_info *link_info = &bp->link_info;
12154 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12155 	u8 module_status;
12156 
12157 	if (bnxt_update_link(bp, true))
12158 		return;
12159 
12160 	module_status = link_info->module_status;
12161 	switch (module_status) {
12162 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12163 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12164 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12165 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12166 			    bp->pf.port_id);
12167 		if (bp->hwrm_spec_code >= 0x10201) {
12168 			netdev_warn(bp->dev, "Module part number %s\n",
12169 				    resp->phy_vendor_partnumber);
12170 		}
12171 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12172 			netdev_warn(bp->dev, "TX is disabled\n");
12173 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12174 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12175 	}
12176 }
12177 
12178 static void
12179 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12180 {
12181 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12182 		if (bp->hwrm_spec_code >= 0x10201)
12183 			req->auto_pause =
12184 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12185 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12186 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12187 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12188 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12189 		req->enables |=
12190 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12191 	} else {
12192 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12193 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12194 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12195 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12196 		req->enables |=
12197 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12198 		if (bp->hwrm_spec_code >= 0x10201) {
12199 			req->auto_pause = req->force_pause;
12200 			req->enables |= cpu_to_le32(
12201 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12202 		}
12203 	}
12204 }
12205 
12206 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12207 {
12208 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12209 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12210 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12211 			req->enables |=
12212 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12213 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12214 		} else if (bp->link_info.advertising) {
12215 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12216 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12217 		}
12218 		if (bp->link_info.advertising_pam4) {
12219 			req->enables |=
12220 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12221 			req->auto_link_pam4_speed_mask =
12222 				cpu_to_le16(bp->link_info.advertising_pam4);
12223 		}
12224 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12225 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12226 	} else {
12227 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12228 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12229 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12230 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12231 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12232 				   (u32)bp->link_info.req_link_speed);
12233 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12234 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12235 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12236 		} else {
12237 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12238 		}
12239 	}
12240 
12241 	/* tell chimp that the setting takes effect immediately */
12242 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12243 }
12244 
12245 int bnxt_hwrm_set_pause(struct bnxt *bp)
12246 {
12247 	struct hwrm_port_phy_cfg_input *req;
12248 	int rc;
12249 
12250 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12251 	if (rc)
12252 		return rc;
12253 
12254 	bnxt_hwrm_set_pause_common(bp, req);
12255 
12256 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12257 	    bp->link_info.force_link_chng)
12258 		bnxt_hwrm_set_link_common(bp, req);
12259 
12260 	rc = hwrm_req_send(bp, req);
12261 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12262 		/* since changing of pause setting doesn't trigger any link
12263 		 * change event, the driver needs to update the current pause
12264 		 * result upon successfully return of the phy_cfg command
12265 		 */
12266 		bp->link_info.pause =
12267 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12268 		bp->link_info.auto_pause_setting = 0;
12269 		if (!bp->link_info.force_link_chng)
12270 			bnxt_report_link(bp);
12271 	}
12272 	bp->link_info.force_link_chng = false;
12273 	return rc;
12274 }
12275 
12276 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12277 			      struct hwrm_port_phy_cfg_input *req)
12278 {
12279 	struct ethtool_keee *eee = &bp->eee;
12280 
12281 	if (eee->eee_enabled) {
12282 		u16 eee_speeds;
12283 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12284 
12285 		if (eee->tx_lpi_enabled)
12286 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12287 		else
12288 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12289 
12290 		req->flags |= cpu_to_le32(flags);
12291 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12292 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12293 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12294 	} else {
12295 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12296 	}
12297 }
12298 
12299 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12300 {
12301 	struct hwrm_port_phy_cfg_input *req;
12302 	int rc;
12303 
12304 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12305 	if (rc)
12306 		return rc;
12307 
12308 	if (set_pause)
12309 		bnxt_hwrm_set_pause_common(bp, req);
12310 
12311 	bnxt_hwrm_set_link_common(bp, req);
12312 
12313 	if (set_eee)
12314 		bnxt_hwrm_set_eee(bp, req);
12315 	return hwrm_req_send(bp, req);
12316 }
12317 
12318 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12319 {
12320 	struct hwrm_port_phy_cfg_input *req;
12321 	int rc;
12322 
12323 	if (!BNXT_SINGLE_PF(bp))
12324 		return 0;
12325 
12326 	if (pci_num_vf(bp->pdev) &&
12327 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12328 		return 0;
12329 
12330 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12331 	if (rc)
12332 		return rc;
12333 
12334 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12335 	rc = hwrm_req_send(bp, req);
12336 	if (!rc) {
12337 		mutex_lock(&bp->link_lock);
12338 		/* Device is not obliged link down in certain scenarios, even
12339 		 * when forced. Setting the state unknown is consistent with
12340 		 * driver startup and will force link state to be reported
12341 		 * during subsequent open based on PORT_PHY_QCFG.
12342 		 */
12343 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12344 		mutex_unlock(&bp->link_lock);
12345 	}
12346 	return rc;
12347 }
12348 
12349 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12350 {
12351 #ifdef CONFIG_TEE_BNXT_FW
12352 	int rc = tee_bnxt_fw_load();
12353 
12354 	if (rc)
12355 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12356 
12357 	return rc;
12358 #else
12359 	netdev_err(bp->dev, "OP-TEE not supported\n");
12360 	return -ENODEV;
12361 #endif
12362 }
12363 
12364 static int bnxt_try_recover_fw(struct bnxt *bp)
12365 {
12366 	if (bp->fw_health && bp->fw_health->status_reliable) {
12367 		int retry = 0, rc;
12368 		u32 sts;
12369 
12370 		do {
12371 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12372 			rc = bnxt_hwrm_poll(bp);
12373 			if (!BNXT_FW_IS_BOOTING(sts) &&
12374 			    !BNXT_FW_IS_RECOVERING(sts))
12375 				break;
12376 			retry++;
12377 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12378 
12379 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12380 			netdev_err(bp->dev,
12381 				   "Firmware not responding, status: 0x%x\n",
12382 				   sts);
12383 			rc = -ENODEV;
12384 		}
12385 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12386 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12387 			return bnxt_fw_reset_via_optee(bp);
12388 		}
12389 		return rc;
12390 	}
12391 
12392 	return -ENODEV;
12393 }
12394 
12395 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12396 {
12397 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12398 
12399 	if (!BNXT_NEW_RM(bp))
12400 		return; /* no resource reservations required */
12401 
12402 	hw_resc->resv_cp_rings = 0;
12403 	hw_resc->resv_stat_ctxs = 0;
12404 	hw_resc->resv_irqs = 0;
12405 	hw_resc->resv_tx_rings = 0;
12406 	hw_resc->resv_rx_rings = 0;
12407 	hw_resc->resv_hw_ring_grps = 0;
12408 	hw_resc->resv_vnics = 0;
12409 	hw_resc->resv_rsscos_ctxs = 0;
12410 	if (!fw_reset) {
12411 		bp->tx_nr_rings = 0;
12412 		bp->rx_nr_rings = 0;
12413 	}
12414 }
12415 
12416 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12417 {
12418 	int rc;
12419 
12420 	if (!BNXT_NEW_RM(bp))
12421 		return 0; /* no resource reservations required */
12422 
12423 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12424 	if (rc)
12425 		netdev_err(bp->dev, "resc_qcaps failed\n");
12426 
12427 	bnxt_clear_reservations(bp, fw_reset);
12428 
12429 	return rc;
12430 }
12431 
12432 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12433 {
12434 	struct hwrm_func_drv_if_change_output *resp;
12435 	struct hwrm_func_drv_if_change_input *req;
12436 	bool resc_reinit = false;
12437 	bool caps_change = false;
12438 	int rc, retry = 0;
12439 	bool fw_reset;
12440 	u32 flags = 0;
12441 
12442 	fw_reset = (bp->fw_reset_state == BNXT_FW_RESET_STATE_ABORT);
12443 	bp->fw_reset_state = 0;
12444 
12445 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12446 		return 0;
12447 
12448 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12449 	if (rc)
12450 		return rc;
12451 
12452 	if (up)
12453 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12454 	resp = hwrm_req_hold(bp, req);
12455 
12456 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12457 	while (retry < BNXT_FW_IF_RETRY) {
12458 		rc = hwrm_req_send(bp, req);
12459 		if (rc != -EAGAIN)
12460 			break;
12461 
12462 		msleep(50);
12463 		retry++;
12464 	}
12465 
12466 	if (rc == -EAGAIN) {
12467 		hwrm_req_drop(bp, req);
12468 		return rc;
12469 	} else if (!rc) {
12470 		flags = le32_to_cpu(resp->flags);
12471 	} else if (up) {
12472 		rc = bnxt_try_recover_fw(bp);
12473 		fw_reset = true;
12474 	}
12475 	hwrm_req_drop(bp, req);
12476 	if (rc)
12477 		return rc;
12478 
12479 	if (!up) {
12480 		bnxt_inv_fw_health_reg(bp);
12481 		return 0;
12482 	}
12483 
12484 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12485 		resc_reinit = true;
12486 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12487 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12488 		fw_reset = true;
12489 	else
12490 		bnxt_remap_fw_health_regs(bp);
12491 
12492 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12493 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12494 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12495 		return -ENODEV;
12496 	}
12497 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12498 		caps_change = true;
12499 
12500 	if (resc_reinit || fw_reset || caps_change) {
12501 		if (fw_reset || caps_change) {
12502 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12503 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12504 				bnxt_ulp_irq_stop(bp);
12505 			bnxt_free_ctx_mem(bp, false);
12506 			bnxt_dcb_free(bp);
12507 			rc = bnxt_fw_init_one(bp);
12508 			if (rc) {
12509 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12510 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12511 				return rc;
12512 			}
12513 			/* IRQ will be initialized later in bnxt_request_irq()*/
12514 			bnxt_clear_int_mode(bp);
12515 		}
12516 		rc = bnxt_cancel_reservations(bp, fw_reset);
12517 	}
12518 	return rc;
12519 }
12520 
12521 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12522 {
12523 	struct hwrm_port_led_qcaps_output *resp;
12524 	struct hwrm_port_led_qcaps_input *req;
12525 	struct bnxt_pf_info *pf = &bp->pf;
12526 	int rc;
12527 
12528 	bp->num_leds = 0;
12529 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12530 		return 0;
12531 
12532 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12533 	if (rc)
12534 		return rc;
12535 
12536 	req->port_id = cpu_to_le16(pf->port_id);
12537 	resp = hwrm_req_hold(bp, req);
12538 	rc = hwrm_req_send(bp, req);
12539 	if (rc) {
12540 		hwrm_req_drop(bp, req);
12541 		return rc;
12542 	}
12543 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12544 		int i;
12545 
12546 		bp->num_leds = resp->num_leds;
12547 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12548 						 bp->num_leds);
12549 		for (i = 0; i < bp->num_leds; i++) {
12550 			struct bnxt_led_info *led = &bp->leds[i];
12551 			__le16 caps = led->led_state_caps;
12552 
12553 			if (!led->led_group_id ||
12554 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12555 				bp->num_leds = 0;
12556 				break;
12557 			}
12558 		}
12559 	}
12560 	hwrm_req_drop(bp, req);
12561 	return 0;
12562 }
12563 
12564 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12565 {
12566 	struct hwrm_wol_filter_alloc_output *resp;
12567 	struct hwrm_wol_filter_alloc_input *req;
12568 	int rc;
12569 
12570 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12571 	if (rc)
12572 		return rc;
12573 
12574 	req->port_id = cpu_to_le16(bp->pf.port_id);
12575 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12576 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12577 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12578 
12579 	resp = hwrm_req_hold(bp, req);
12580 	rc = hwrm_req_send(bp, req);
12581 	if (!rc)
12582 		bp->wol_filter_id = resp->wol_filter_id;
12583 	hwrm_req_drop(bp, req);
12584 	return rc;
12585 }
12586 
12587 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12588 {
12589 	struct hwrm_wol_filter_free_input *req;
12590 	int rc;
12591 
12592 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12593 	if (rc)
12594 		return rc;
12595 
12596 	req->port_id = cpu_to_le16(bp->pf.port_id);
12597 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12598 	req->wol_filter_id = bp->wol_filter_id;
12599 
12600 	return hwrm_req_send(bp, req);
12601 }
12602 
12603 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12604 {
12605 	struct hwrm_wol_filter_qcfg_output *resp;
12606 	struct hwrm_wol_filter_qcfg_input *req;
12607 	u16 next_handle = 0;
12608 	int rc;
12609 
12610 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12611 	if (rc)
12612 		return rc;
12613 
12614 	req->port_id = cpu_to_le16(bp->pf.port_id);
12615 	req->handle = cpu_to_le16(handle);
12616 	resp = hwrm_req_hold(bp, req);
12617 	rc = hwrm_req_send(bp, req);
12618 	if (!rc) {
12619 		next_handle = le16_to_cpu(resp->next_handle);
12620 		if (next_handle != 0) {
12621 			if (resp->wol_type ==
12622 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12623 				bp->wol = 1;
12624 				bp->wol_filter_id = resp->wol_filter_id;
12625 			}
12626 		}
12627 	}
12628 	hwrm_req_drop(bp, req);
12629 	return next_handle;
12630 }
12631 
12632 static void bnxt_get_wol_settings(struct bnxt *bp)
12633 {
12634 	u16 handle = 0;
12635 
12636 	bp->wol = 0;
12637 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12638 		return;
12639 
12640 	do {
12641 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12642 	} while (handle && handle != 0xffff);
12643 }
12644 
12645 static bool bnxt_eee_config_ok(struct bnxt *bp)
12646 {
12647 	struct ethtool_keee *eee = &bp->eee;
12648 	struct bnxt_link_info *link_info = &bp->link_info;
12649 
12650 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12651 		return true;
12652 
12653 	if (eee->eee_enabled) {
12654 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12655 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12656 
12657 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12658 
12659 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12660 			eee->eee_enabled = 0;
12661 			return false;
12662 		}
12663 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12664 			linkmode_and(eee->advertised, advertising,
12665 				     eee->supported);
12666 			return false;
12667 		}
12668 	}
12669 	return true;
12670 }
12671 
12672 static int bnxt_update_phy_setting(struct bnxt *bp)
12673 {
12674 	int rc;
12675 	bool update_link = false;
12676 	bool update_pause = false;
12677 	bool update_eee = false;
12678 	struct bnxt_link_info *link_info = &bp->link_info;
12679 
12680 	rc = bnxt_update_link(bp, true);
12681 	if (rc) {
12682 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12683 			   rc);
12684 		return rc;
12685 	}
12686 	if (!BNXT_SINGLE_PF(bp))
12687 		return 0;
12688 
12689 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12690 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12691 	    link_info->req_flow_ctrl)
12692 		update_pause = true;
12693 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12694 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12695 		update_pause = true;
12696 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12697 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12698 			update_link = true;
12699 		if (bnxt_force_speed_updated(link_info))
12700 			update_link = true;
12701 		if (link_info->req_duplex != link_info->duplex_setting)
12702 			update_link = true;
12703 	} else {
12704 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12705 			update_link = true;
12706 		if (bnxt_auto_speed_updated(link_info))
12707 			update_link = true;
12708 	}
12709 
12710 	/* The last close may have shutdown the link, so need to call
12711 	 * PHY_CFG to bring it back up.
12712 	 */
12713 	if (!BNXT_LINK_IS_UP(bp))
12714 		update_link = true;
12715 
12716 	if (!bnxt_eee_config_ok(bp))
12717 		update_eee = true;
12718 
12719 	if (update_link)
12720 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12721 	else if (update_pause)
12722 		rc = bnxt_hwrm_set_pause(bp);
12723 	if (rc) {
12724 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12725 			   rc);
12726 		return rc;
12727 	}
12728 
12729 	return rc;
12730 }
12731 
12732 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12733 
12734 static int bnxt_reinit_after_abort(struct bnxt *bp)
12735 {
12736 	int rc;
12737 
12738 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12739 		return -EBUSY;
12740 
12741 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12742 		return -ENODEV;
12743 
12744 	rc = bnxt_fw_init_one(bp);
12745 	if (!rc) {
12746 		bnxt_clear_int_mode(bp);
12747 		rc = bnxt_init_int_mode(bp);
12748 		if (!rc) {
12749 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12750 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12751 		}
12752 	}
12753 	return rc;
12754 }
12755 
12756 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12757 {
12758 	struct bnxt_ntuple_filter *ntp_fltr;
12759 	struct bnxt_l2_filter *l2_fltr;
12760 
12761 	if (list_empty(&fltr->list))
12762 		return;
12763 
12764 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12765 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12766 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12767 		atomic_inc(&l2_fltr->refcnt);
12768 		ntp_fltr->l2_fltr = l2_fltr;
12769 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12770 			bnxt_del_ntp_filter(bp, ntp_fltr);
12771 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12772 				   fltr->sw_id);
12773 		}
12774 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12775 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12776 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12777 			bnxt_del_l2_filter(bp, l2_fltr);
12778 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12779 				   fltr->sw_id);
12780 		}
12781 	}
12782 }
12783 
12784 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12785 {
12786 	struct bnxt_filter_base *usr_fltr, *tmp;
12787 
12788 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12789 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12790 }
12791 
12792 static int bnxt_set_xps_mapping(struct bnxt *bp)
12793 {
12794 	int numa_node = dev_to_node(&bp->pdev->dev);
12795 	unsigned int q_idx, map_idx, cpu, i;
12796 	const struct cpumask *cpu_mask_ptr;
12797 	int nr_cpus = num_online_cpus();
12798 	cpumask_t *q_map;
12799 	int rc = 0;
12800 
12801 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12802 	if (!q_map)
12803 		return -ENOMEM;
12804 
12805 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12806 	 * Each TC has the same number of TX queues. The nth TX queue for each
12807 	 * TC will have the same CPU mask.
12808 	 */
12809 	for (i = 0; i < nr_cpus; i++) {
12810 		map_idx = i % bp->tx_nr_rings_per_tc;
12811 		cpu = cpumask_local_spread(i, numa_node);
12812 		cpu_mask_ptr = get_cpu_mask(cpu);
12813 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12814 	}
12815 
12816 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12817 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12818 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12819 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12820 		if (rc) {
12821 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12822 				    q_idx);
12823 			break;
12824 		}
12825 	}
12826 
12827 	kfree(q_map);
12828 
12829 	return rc;
12830 }
12831 
12832 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12833 {
12834 	int rc = 0;
12835 
12836 	netif_carrier_off(bp->dev);
12837 	if (irq_re_init) {
12838 		/* Reserve rings now if none were reserved at driver probe. */
12839 		rc = bnxt_init_dflt_ring_mode(bp);
12840 		if (rc) {
12841 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12842 			return rc;
12843 		}
12844 	}
12845 	rc = bnxt_reserve_rings(bp, irq_re_init);
12846 	if (rc)
12847 		return rc;
12848 
12849 	rc = bnxt_alloc_mem(bp, irq_re_init);
12850 	if (rc) {
12851 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12852 		goto open_err_free_mem;
12853 	}
12854 
12855 	if (irq_re_init) {
12856 		bnxt_init_napi(bp);
12857 		rc = bnxt_request_irq(bp);
12858 		if (rc) {
12859 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12860 			goto open_err_irq;
12861 		}
12862 	}
12863 
12864 	rc = bnxt_init_nic(bp, irq_re_init);
12865 	if (rc) {
12866 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12867 		goto open_err_irq;
12868 	}
12869 
12870 	bnxt_enable_napi(bp);
12871 	bnxt_debug_dev_init(bp);
12872 
12873 	if (link_re_init) {
12874 		mutex_lock(&bp->link_lock);
12875 		rc = bnxt_update_phy_setting(bp);
12876 		mutex_unlock(&bp->link_lock);
12877 		if (rc) {
12878 			netdev_warn(bp->dev, "failed to update phy settings\n");
12879 			if (BNXT_SINGLE_PF(bp)) {
12880 				bp->link_info.phy_retry = true;
12881 				bp->link_info.phy_retry_expires =
12882 					jiffies + 5 * HZ;
12883 			}
12884 		}
12885 	}
12886 
12887 	if (irq_re_init) {
12888 		udp_tunnel_nic_reset_ntf(bp->dev);
12889 		rc = bnxt_set_xps_mapping(bp);
12890 		if (rc)
12891 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12892 	}
12893 
12894 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12895 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12896 			static_branch_enable(&bnxt_xdp_locking_key);
12897 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12898 		static_branch_disable(&bnxt_xdp_locking_key);
12899 	}
12900 	set_bit(BNXT_STATE_OPEN, &bp->state);
12901 	bnxt_enable_int(bp);
12902 	/* Enable TX queues */
12903 	bnxt_tx_enable(bp);
12904 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12905 	/* Poll link status and check for SFP+ module status */
12906 	mutex_lock(&bp->link_lock);
12907 	bnxt_get_port_module_status(bp);
12908 	mutex_unlock(&bp->link_lock);
12909 
12910 	/* VF-reps may need to be re-opened after the PF is re-opened */
12911 	if (BNXT_PF(bp))
12912 		bnxt_vf_reps_open(bp);
12913 	bnxt_ptp_init_rtc(bp, true);
12914 	bnxt_ptp_cfg_tstamp_filters(bp);
12915 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12916 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12917 	bnxt_cfg_usr_fltrs(bp);
12918 	return 0;
12919 
12920 open_err_irq:
12921 	bnxt_del_napi(bp);
12922 
12923 open_err_free_mem:
12924 	bnxt_free_skbs(bp);
12925 	bnxt_free_irq(bp);
12926 	bnxt_free_mem(bp, true);
12927 	return rc;
12928 }
12929 
12930 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12931 {
12932 	int rc = 0;
12933 
12934 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12935 		rc = -EIO;
12936 	if (!rc)
12937 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12938 	if (rc) {
12939 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12940 		netif_close(bp->dev);
12941 	}
12942 	return rc;
12943 }
12944 
12945 /* netdev instance lock held, open the NIC half way by allocating all
12946  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12947  * for offline self tests.
12948  */
12949 int bnxt_half_open_nic(struct bnxt *bp)
12950 {
12951 	int rc = 0;
12952 
12953 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12954 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12955 		rc = -ENODEV;
12956 		goto half_open_err;
12957 	}
12958 
12959 	rc = bnxt_alloc_mem(bp, true);
12960 	if (rc) {
12961 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12962 		goto half_open_err;
12963 	}
12964 	bnxt_init_napi(bp);
12965 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12966 	rc = bnxt_init_nic(bp, true);
12967 	if (rc) {
12968 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12969 		bnxt_del_napi(bp);
12970 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12971 		goto half_open_err;
12972 	}
12973 	return 0;
12974 
12975 half_open_err:
12976 	bnxt_free_skbs(bp);
12977 	bnxt_free_mem(bp, true);
12978 	netif_close(bp->dev);
12979 	return rc;
12980 }
12981 
12982 /* netdev instance lock held, this call can only be made after a previous
12983  * successful call to bnxt_half_open_nic().
12984  */
12985 void bnxt_half_close_nic(struct bnxt *bp)
12986 {
12987 	bnxt_hwrm_resource_free(bp, false, true);
12988 	bnxt_del_napi(bp);
12989 	bnxt_free_skbs(bp);
12990 	bnxt_free_mem(bp, true);
12991 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12992 }
12993 
12994 void bnxt_reenable_sriov(struct bnxt *bp)
12995 {
12996 	if (BNXT_PF(bp)) {
12997 		struct bnxt_pf_info *pf = &bp->pf;
12998 		int n = pf->active_vfs;
12999 
13000 		if (n)
13001 			bnxt_cfg_hw_sriov(bp, &n, true);
13002 	}
13003 }
13004 
13005 static int bnxt_open(struct net_device *dev)
13006 {
13007 	struct bnxt *bp = netdev_priv(dev);
13008 	int rc;
13009 
13010 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
13011 		rc = bnxt_reinit_after_abort(bp);
13012 		if (rc) {
13013 			if (rc == -EBUSY)
13014 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
13015 			else
13016 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
13017 			return -ENODEV;
13018 		}
13019 	}
13020 
13021 	rc = bnxt_hwrm_if_change(bp, true);
13022 	if (rc)
13023 		return rc;
13024 
13025 	rc = __bnxt_open_nic(bp, true, true);
13026 	if (rc) {
13027 		bnxt_hwrm_if_change(bp, false);
13028 	} else {
13029 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
13030 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13031 				bnxt_queue_sp_work(bp,
13032 						   BNXT_RESTART_ULP_SP_EVENT);
13033 		}
13034 	}
13035 
13036 	return rc;
13037 }
13038 
13039 static bool bnxt_drv_busy(struct bnxt *bp)
13040 {
13041 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
13042 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
13043 }
13044 
13045 static void bnxt_get_ring_stats(struct bnxt *bp,
13046 				struct rtnl_link_stats64 *stats);
13047 
13048 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
13049 			     bool link_re_init)
13050 {
13051 	/* Close the VF-reps before closing PF */
13052 	if (BNXT_PF(bp))
13053 		bnxt_vf_reps_close(bp);
13054 
13055 	/* Change device state to avoid TX queue wake up's */
13056 	bnxt_tx_disable(bp);
13057 
13058 	clear_bit(BNXT_STATE_OPEN, &bp->state);
13059 	smp_mb__after_atomic();
13060 	while (bnxt_drv_busy(bp))
13061 		msleep(20);
13062 
13063 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
13064 		bnxt_clear_rss_ctxs(bp);
13065 	/* Flush rings and disable interrupts */
13066 	bnxt_shutdown_nic(bp, irq_re_init);
13067 
13068 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13069 
13070 	bnxt_debug_dev_exit(bp);
13071 	bnxt_disable_napi(bp);
13072 	timer_delete_sync(&bp->timer);
13073 	bnxt_free_skbs(bp);
13074 
13075 	/* Save ring stats before shutdown */
13076 	if (bp->bnapi && irq_re_init) {
13077 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13078 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13079 	}
13080 	if (irq_re_init) {
13081 		bnxt_free_irq(bp);
13082 		bnxt_del_napi(bp);
13083 	}
13084 	bnxt_free_mem(bp, irq_re_init);
13085 }
13086 
13087 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13088 {
13089 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13090 		/* If we get here, it means firmware reset is in progress
13091 		 * while we are trying to close.  We can safely proceed with
13092 		 * the close because we are holding netdev instance lock.
13093 		 * Some firmware messages may fail as we proceed to close.
13094 		 * We set the ABORT_ERR flag here so that the FW reset thread
13095 		 * will later abort when it gets the netdev instance lock
13096 		 * and sees the flag.
13097 		 */
13098 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13099 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13100 	}
13101 
13102 #ifdef CONFIG_BNXT_SRIOV
13103 	if (bp->sriov_cfg) {
13104 		int rc;
13105 
13106 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13107 						      !bp->sriov_cfg,
13108 						      BNXT_SRIOV_CFG_WAIT_TMO);
13109 		if (!rc)
13110 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13111 		else if (rc < 0)
13112 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13113 	}
13114 #endif
13115 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13116 }
13117 
13118 static int bnxt_close(struct net_device *dev)
13119 {
13120 	struct bnxt *bp = netdev_priv(dev);
13121 
13122 	bnxt_close_nic(bp, true, true);
13123 	bnxt_hwrm_shutdown_link(bp);
13124 	bnxt_hwrm_if_change(bp, false);
13125 	return 0;
13126 }
13127 
13128 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13129 				   u16 *val)
13130 {
13131 	struct hwrm_port_phy_mdio_read_output *resp;
13132 	struct hwrm_port_phy_mdio_read_input *req;
13133 	int rc;
13134 
13135 	if (bp->hwrm_spec_code < 0x10a00)
13136 		return -EOPNOTSUPP;
13137 
13138 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13139 	if (rc)
13140 		return rc;
13141 
13142 	req->port_id = cpu_to_le16(bp->pf.port_id);
13143 	req->phy_addr = phy_addr;
13144 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13145 	if (mdio_phy_id_is_c45(phy_addr)) {
13146 		req->cl45_mdio = 1;
13147 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13148 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13149 		req->reg_addr = cpu_to_le16(reg);
13150 	}
13151 
13152 	resp = hwrm_req_hold(bp, req);
13153 	rc = hwrm_req_send(bp, req);
13154 	if (!rc)
13155 		*val = le16_to_cpu(resp->reg_data);
13156 	hwrm_req_drop(bp, req);
13157 	return rc;
13158 }
13159 
13160 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13161 				    u16 val)
13162 {
13163 	struct hwrm_port_phy_mdio_write_input *req;
13164 	int rc;
13165 
13166 	if (bp->hwrm_spec_code < 0x10a00)
13167 		return -EOPNOTSUPP;
13168 
13169 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13170 	if (rc)
13171 		return rc;
13172 
13173 	req->port_id = cpu_to_le16(bp->pf.port_id);
13174 	req->phy_addr = phy_addr;
13175 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13176 	if (mdio_phy_id_is_c45(phy_addr)) {
13177 		req->cl45_mdio = 1;
13178 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13179 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13180 		req->reg_addr = cpu_to_le16(reg);
13181 	}
13182 	req->reg_data = cpu_to_le16(val);
13183 
13184 	return hwrm_req_send(bp, req);
13185 }
13186 
13187 /* netdev instance lock held */
13188 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13189 {
13190 	struct mii_ioctl_data *mdio = if_mii(ifr);
13191 	struct bnxt *bp = netdev_priv(dev);
13192 	int rc;
13193 
13194 	switch (cmd) {
13195 	case SIOCGMIIPHY:
13196 		mdio->phy_id = bp->link_info.phy_addr;
13197 
13198 		fallthrough;
13199 	case SIOCGMIIREG: {
13200 		u16 mii_regval = 0;
13201 
13202 		if (!netif_running(dev))
13203 			return -EAGAIN;
13204 
13205 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13206 					     &mii_regval);
13207 		mdio->val_out = mii_regval;
13208 		return rc;
13209 	}
13210 
13211 	case SIOCSMIIREG:
13212 		if (!netif_running(dev))
13213 			return -EAGAIN;
13214 
13215 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13216 						mdio->val_in);
13217 
13218 	case SIOCSHWTSTAMP:
13219 		return bnxt_hwtstamp_set(dev, ifr);
13220 
13221 	case SIOCGHWTSTAMP:
13222 		return bnxt_hwtstamp_get(dev, ifr);
13223 
13224 	default:
13225 		/* do nothing */
13226 		break;
13227 	}
13228 	return -EOPNOTSUPP;
13229 }
13230 
13231 static void bnxt_get_ring_stats(struct bnxt *bp,
13232 				struct rtnl_link_stats64 *stats)
13233 {
13234 	int i;
13235 
13236 	for (i = 0; i < bp->cp_nr_rings; i++) {
13237 		struct bnxt_napi *bnapi = bp->bnapi[i];
13238 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13239 		u64 *sw = cpr->stats.sw_stats;
13240 
13241 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13242 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13243 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13244 
13245 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13246 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13247 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13248 
13249 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13250 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13251 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13252 
13253 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13254 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13255 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13256 
13257 		stats->rx_missed_errors +=
13258 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13259 
13260 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13261 
13262 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13263 
13264 		stats->rx_dropped +=
13265 			cpr->sw_stats->rx.rx_netpoll_discards +
13266 			cpr->sw_stats->rx.rx_oom_discards;
13267 	}
13268 }
13269 
13270 static void bnxt_add_prev_stats(struct bnxt *bp,
13271 				struct rtnl_link_stats64 *stats)
13272 {
13273 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13274 
13275 	stats->rx_packets += prev_stats->rx_packets;
13276 	stats->tx_packets += prev_stats->tx_packets;
13277 	stats->rx_bytes += prev_stats->rx_bytes;
13278 	stats->tx_bytes += prev_stats->tx_bytes;
13279 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13280 	stats->multicast += prev_stats->multicast;
13281 	stats->rx_dropped += prev_stats->rx_dropped;
13282 	stats->tx_dropped += prev_stats->tx_dropped;
13283 }
13284 
13285 static void
13286 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13287 {
13288 	struct bnxt *bp = netdev_priv(dev);
13289 
13290 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13291 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13292 	 * we check the BNXT_STATE_OPEN flag.
13293 	 */
13294 	smp_mb__after_atomic();
13295 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13296 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13297 		*stats = bp->net_stats_prev;
13298 		return;
13299 	}
13300 
13301 	bnxt_get_ring_stats(bp, stats);
13302 	bnxt_add_prev_stats(bp, stats);
13303 
13304 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13305 		u64 *rx = bp->port_stats.sw_stats;
13306 		u64 *tx = bp->port_stats.sw_stats +
13307 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13308 
13309 		stats->rx_crc_errors =
13310 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13311 		stats->rx_frame_errors =
13312 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13313 		stats->rx_length_errors =
13314 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13315 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13316 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13317 		stats->rx_errors =
13318 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13319 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13320 		stats->collisions =
13321 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13322 		stats->tx_fifo_errors =
13323 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13324 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13325 	}
13326 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13327 }
13328 
13329 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13330 					struct bnxt_total_ring_err_stats *stats,
13331 					struct bnxt_cp_ring_info *cpr)
13332 {
13333 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13334 	u64 *hw_stats = cpr->stats.sw_stats;
13335 
13336 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13337 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13338 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13339 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13340 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13341 	stats->rx_total_ring_discards +=
13342 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13343 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13344 	stats->tx_total_ring_discards +=
13345 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13346 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13347 }
13348 
13349 void bnxt_get_ring_err_stats(struct bnxt *bp,
13350 			     struct bnxt_total_ring_err_stats *stats)
13351 {
13352 	int i;
13353 
13354 	for (i = 0; i < bp->cp_nr_rings; i++)
13355 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13356 }
13357 
13358 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13359 {
13360 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13361 	struct net_device *dev = bp->dev;
13362 	struct netdev_hw_addr *ha;
13363 	u8 *haddr;
13364 	int mc_count = 0;
13365 	bool update = false;
13366 	int off = 0;
13367 
13368 	netdev_for_each_mc_addr(ha, dev) {
13369 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13370 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13371 			vnic->mc_list_count = 0;
13372 			return false;
13373 		}
13374 		haddr = ha->addr;
13375 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13376 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13377 			update = true;
13378 		}
13379 		off += ETH_ALEN;
13380 		mc_count++;
13381 	}
13382 	if (mc_count)
13383 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13384 
13385 	if (mc_count != vnic->mc_list_count) {
13386 		vnic->mc_list_count = mc_count;
13387 		update = true;
13388 	}
13389 	return update;
13390 }
13391 
13392 static bool bnxt_uc_list_updated(struct bnxt *bp)
13393 {
13394 	struct net_device *dev = bp->dev;
13395 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13396 	struct netdev_hw_addr *ha;
13397 	int off = 0;
13398 
13399 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13400 		return true;
13401 
13402 	netdev_for_each_uc_addr(ha, dev) {
13403 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13404 			return true;
13405 
13406 		off += ETH_ALEN;
13407 	}
13408 	return false;
13409 }
13410 
13411 static void bnxt_set_rx_mode(struct net_device *dev)
13412 {
13413 	struct bnxt *bp = netdev_priv(dev);
13414 	struct bnxt_vnic_info *vnic;
13415 	bool mc_update = false;
13416 	bool uc_update;
13417 	u32 mask;
13418 
13419 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13420 		return;
13421 
13422 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13423 	mask = vnic->rx_mask;
13424 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13425 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13426 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13427 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13428 
13429 	if (dev->flags & IFF_PROMISC)
13430 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13431 
13432 	uc_update = bnxt_uc_list_updated(bp);
13433 
13434 	if (dev->flags & IFF_BROADCAST)
13435 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13436 	if (dev->flags & IFF_ALLMULTI) {
13437 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13438 		vnic->mc_list_count = 0;
13439 	} else if (dev->flags & IFF_MULTICAST) {
13440 		mc_update = bnxt_mc_list_updated(bp, &mask);
13441 	}
13442 
13443 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13444 		vnic->rx_mask = mask;
13445 
13446 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13447 	}
13448 }
13449 
13450 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13451 {
13452 	struct net_device *dev = bp->dev;
13453 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13454 	struct netdev_hw_addr *ha;
13455 	int i, off = 0, rc;
13456 	bool uc_update;
13457 
13458 	netif_addr_lock_bh(dev);
13459 	uc_update = bnxt_uc_list_updated(bp);
13460 	netif_addr_unlock_bh(dev);
13461 
13462 	if (!uc_update)
13463 		goto skip_uc;
13464 
13465 	for (i = 1; i < vnic->uc_filter_count; i++) {
13466 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13467 
13468 		bnxt_hwrm_l2_filter_free(bp, fltr);
13469 		bnxt_del_l2_filter(bp, fltr);
13470 	}
13471 
13472 	vnic->uc_filter_count = 1;
13473 
13474 	netif_addr_lock_bh(dev);
13475 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13476 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13477 	} else {
13478 		netdev_for_each_uc_addr(ha, dev) {
13479 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13480 			off += ETH_ALEN;
13481 			vnic->uc_filter_count++;
13482 		}
13483 	}
13484 	netif_addr_unlock_bh(dev);
13485 
13486 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13487 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13488 		if (rc) {
13489 			if (BNXT_VF(bp) && rc == -ENODEV) {
13490 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13491 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13492 				else
13493 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13494 				rc = 0;
13495 			} else {
13496 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13497 			}
13498 			vnic->uc_filter_count = i;
13499 			return rc;
13500 		}
13501 	}
13502 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13503 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13504 
13505 skip_uc:
13506 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13507 	    !bnxt_promisc_ok(bp))
13508 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13509 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13510 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13511 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13512 			    rc);
13513 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13514 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13515 		vnic->mc_list_count = 0;
13516 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13517 	}
13518 	if (rc)
13519 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13520 			   rc);
13521 
13522 	return rc;
13523 }
13524 
13525 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13526 {
13527 #ifdef CONFIG_BNXT_SRIOV
13528 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13529 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13530 
13531 		/* No minimum rings were provisioned by the PF.  Don't
13532 		 * reserve rings by default when device is down.
13533 		 */
13534 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13535 			return true;
13536 
13537 		if (!netif_running(bp->dev))
13538 			return false;
13539 	}
13540 #endif
13541 	return true;
13542 }
13543 
13544 /* If the chip and firmware supports RFS */
13545 static bool bnxt_rfs_supported(struct bnxt *bp)
13546 {
13547 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13548 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13549 			return true;
13550 		return false;
13551 	}
13552 	/* 212 firmware is broken for aRFS */
13553 	if (BNXT_FW_MAJ(bp) == 212)
13554 		return false;
13555 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13556 		return true;
13557 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13558 		return true;
13559 	return false;
13560 }
13561 
13562 /* If runtime conditions support RFS */
13563 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13564 {
13565 	struct bnxt_hw_rings hwr = {0};
13566 	int max_vnics, max_rss_ctxs;
13567 
13568 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13569 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13570 		return bnxt_rfs_supported(bp);
13571 
13572 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13573 		return false;
13574 
13575 	hwr.grp = bp->rx_nr_rings;
13576 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13577 	if (new_rss_ctx)
13578 		hwr.vnic++;
13579 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13580 	max_vnics = bnxt_get_max_func_vnics(bp);
13581 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13582 
13583 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13584 		if (bp->rx_nr_rings > 1)
13585 			netdev_warn(bp->dev,
13586 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13587 				    min(max_rss_ctxs - 1, max_vnics - 1));
13588 		return false;
13589 	}
13590 
13591 	if (!BNXT_NEW_RM(bp))
13592 		return true;
13593 
13594 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13595 	 * issue that will mess up the default VNIC if we reduce the
13596 	 * reservations.
13597 	 */
13598 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13599 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13600 		return true;
13601 
13602 	bnxt_hwrm_reserve_rings(bp, &hwr);
13603 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13604 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13605 		return true;
13606 
13607 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13608 	hwr.vnic = 1;
13609 	hwr.rss_ctx = 0;
13610 	bnxt_hwrm_reserve_rings(bp, &hwr);
13611 	return false;
13612 }
13613 
13614 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13615 					   netdev_features_t features)
13616 {
13617 	struct bnxt *bp = netdev_priv(dev);
13618 	netdev_features_t vlan_features;
13619 
13620 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13621 		features &= ~NETIF_F_NTUPLE;
13622 
13623 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13624 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13625 
13626 	if (!(features & NETIF_F_GRO))
13627 		features &= ~NETIF_F_GRO_HW;
13628 
13629 	if (features & NETIF_F_GRO_HW)
13630 		features &= ~NETIF_F_LRO;
13631 
13632 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13633 	 * turned on or off together.
13634 	 */
13635 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13636 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13637 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13638 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13639 		else if (vlan_features)
13640 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13641 	}
13642 #ifdef CONFIG_BNXT_SRIOV
13643 	if (BNXT_VF(bp) && bp->vf.vlan)
13644 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13645 #endif
13646 	return features;
13647 }
13648 
13649 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13650 				bool link_re_init, u32 flags, bool update_tpa)
13651 {
13652 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13653 	bp->flags = flags;
13654 	if (update_tpa)
13655 		bnxt_set_ring_params(bp);
13656 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13657 }
13658 
13659 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13660 {
13661 	bool update_tpa = false, update_ntuple = false;
13662 	struct bnxt *bp = netdev_priv(dev);
13663 	u32 flags = bp->flags;
13664 	u32 changes;
13665 	int rc = 0;
13666 	bool re_init = false;
13667 
13668 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13669 	if (features & NETIF_F_GRO_HW)
13670 		flags |= BNXT_FLAG_GRO;
13671 	else if (features & NETIF_F_LRO)
13672 		flags |= BNXT_FLAG_LRO;
13673 
13674 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13675 		flags &= ~BNXT_FLAG_TPA;
13676 
13677 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13678 		flags |= BNXT_FLAG_STRIP_VLAN;
13679 
13680 	if (features & NETIF_F_NTUPLE)
13681 		flags |= BNXT_FLAG_RFS;
13682 	else
13683 		bnxt_clear_usr_fltrs(bp, true);
13684 
13685 	changes = flags ^ bp->flags;
13686 	if (changes & BNXT_FLAG_TPA) {
13687 		update_tpa = true;
13688 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13689 		    (flags & BNXT_FLAG_TPA) == 0 ||
13690 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13691 			re_init = true;
13692 	}
13693 
13694 	if (changes & ~BNXT_FLAG_TPA)
13695 		re_init = true;
13696 
13697 	if (changes & BNXT_FLAG_RFS)
13698 		update_ntuple = true;
13699 
13700 	if (flags != bp->flags) {
13701 		u32 old_flags = bp->flags;
13702 
13703 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13704 			bp->flags = flags;
13705 			if (update_tpa)
13706 				bnxt_set_ring_params(bp);
13707 			return rc;
13708 		}
13709 
13710 		if (update_ntuple)
13711 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13712 
13713 		if (re_init)
13714 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13715 
13716 		if (update_tpa) {
13717 			bp->flags = flags;
13718 			rc = bnxt_set_tpa(bp,
13719 					  (flags & BNXT_FLAG_TPA) ?
13720 					  true : false);
13721 			if (rc)
13722 				bp->flags = old_flags;
13723 		}
13724 	}
13725 	return rc;
13726 }
13727 
13728 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13729 			      u8 **nextp)
13730 {
13731 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13732 	struct hop_jumbo_hdr *jhdr;
13733 	int hdr_count = 0;
13734 	u8 *nexthdr;
13735 	int start;
13736 
13737 	/* Check that there are at most 2 IPv6 extension headers, no
13738 	 * fragment header, and each is <= 64 bytes.
13739 	 */
13740 	start = nw_off + sizeof(*ip6h);
13741 	nexthdr = &ip6h->nexthdr;
13742 	while (ipv6_ext_hdr(*nexthdr)) {
13743 		struct ipv6_opt_hdr *hp;
13744 		int hdrlen;
13745 
13746 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13747 		    *nexthdr == NEXTHDR_FRAGMENT)
13748 			return false;
13749 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13750 					  skb_headlen(skb), NULL);
13751 		if (!hp)
13752 			return false;
13753 		if (*nexthdr == NEXTHDR_AUTH)
13754 			hdrlen = ipv6_authlen(hp);
13755 		else
13756 			hdrlen = ipv6_optlen(hp);
13757 
13758 		if (hdrlen > 64)
13759 			return false;
13760 
13761 		/* The ext header may be a hop-by-hop header inserted for
13762 		 * big TCP purposes. This will be removed before sending
13763 		 * from NIC, so do not count it.
13764 		 */
13765 		if (*nexthdr == NEXTHDR_HOP) {
13766 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13767 				goto increment_hdr;
13768 
13769 			jhdr = (struct hop_jumbo_hdr *)hp;
13770 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13771 			    jhdr->nexthdr != IPPROTO_TCP)
13772 				goto increment_hdr;
13773 
13774 			goto next_hdr;
13775 		}
13776 increment_hdr:
13777 		hdr_count++;
13778 next_hdr:
13779 		nexthdr = &hp->nexthdr;
13780 		start += hdrlen;
13781 	}
13782 	if (nextp) {
13783 		/* Caller will check inner protocol */
13784 		if (skb->encapsulation) {
13785 			*nextp = nexthdr;
13786 			return true;
13787 		}
13788 		*nextp = NULL;
13789 	}
13790 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13791 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13792 }
13793 
13794 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13795 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13796 {
13797 	struct udphdr *uh = udp_hdr(skb);
13798 	__be16 udp_port = uh->dest;
13799 
13800 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13801 	    udp_port != bp->vxlan_gpe_port)
13802 		return false;
13803 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13804 		struct ethhdr *eh = inner_eth_hdr(skb);
13805 
13806 		switch (eh->h_proto) {
13807 		case htons(ETH_P_IP):
13808 			return true;
13809 		case htons(ETH_P_IPV6):
13810 			return bnxt_exthdr_check(bp, skb,
13811 						 skb_inner_network_offset(skb),
13812 						 NULL);
13813 		}
13814 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13815 		return true;
13816 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13817 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13818 					 NULL);
13819 	}
13820 	return false;
13821 }
13822 
13823 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13824 {
13825 	switch (l4_proto) {
13826 	case IPPROTO_UDP:
13827 		return bnxt_udp_tunl_check(bp, skb);
13828 	case IPPROTO_IPIP:
13829 		return true;
13830 	case IPPROTO_GRE: {
13831 		switch (skb->inner_protocol) {
13832 		default:
13833 			return false;
13834 		case htons(ETH_P_IP):
13835 			return true;
13836 		case htons(ETH_P_IPV6):
13837 			fallthrough;
13838 		}
13839 	}
13840 	case IPPROTO_IPV6:
13841 		/* Check ext headers of inner ipv6 */
13842 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13843 					 NULL);
13844 	}
13845 	return false;
13846 }
13847 
13848 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13849 					     struct net_device *dev,
13850 					     netdev_features_t features)
13851 {
13852 	struct bnxt *bp = netdev_priv(dev);
13853 	u8 *l4_proto;
13854 
13855 	features = vlan_features_check(skb, features);
13856 	switch (vlan_get_protocol(skb)) {
13857 	case htons(ETH_P_IP):
13858 		if (!skb->encapsulation)
13859 			return features;
13860 		l4_proto = &ip_hdr(skb)->protocol;
13861 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13862 			return features;
13863 		break;
13864 	case htons(ETH_P_IPV6):
13865 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13866 				       &l4_proto))
13867 			break;
13868 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13869 			return features;
13870 		break;
13871 	}
13872 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13873 }
13874 
13875 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13876 			 u32 *reg_buf)
13877 {
13878 	struct hwrm_dbg_read_direct_output *resp;
13879 	struct hwrm_dbg_read_direct_input *req;
13880 	__le32 *dbg_reg_buf;
13881 	dma_addr_t mapping;
13882 	int rc, i;
13883 
13884 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13885 	if (rc)
13886 		return rc;
13887 
13888 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13889 					 &mapping);
13890 	if (!dbg_reg_buf) {
13891 		rc = -ENOMEM;
13892 		goto dbg_rd_reg_exit;
13893 	}
13894 
13895 	req->host_dest_addr = cpu_to_le64(mapping);
13896 
13897 	resp = hwrm_req_hold(bp, req);
13898 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13899 	req->read_len32 = cpu_to_le32(num_words);
13900 
13901 	rc = hwrm_req_send(bp, req);
13902 	if (rc || resp->error_code) {
13903 		rc = -EIO;
13904 		goto dbg_rd_reg_exit;
13905 	}
13906 	for (i = 0; i < num_words; i++)
13907 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13908 
13909 dbg_rd_reg_exit:
13910 	hwrm_req_drop(bp, req);
13911 	return rc;
13912 }
13913 
13914 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13915 				       u32 ring_id, u32 *prod, u32 *cons)
13916 {
13917 	struct hwrm_dbg_ring_info_get_output *resp;
13918 	struct hwrm_dbg_ring_info_get_input *req;
13919 	int rc;
13920 
13921 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13922 	if (rc)
13923 		return rc;
13924 
13925 	req->ring_type = ring_type;
13926 	req->fw_ring_id = cpu_to_le32(ring_id);
13927 	resp = hwrm_req_hold(bp, req);
13928 	rc = hwrm_req_send(bp, req);
13929 	if (!rc) {
13930 		*prod = le32_to_cpu(resp->producer_index);
13931 		*cons = le32_to_cpu(resp->consumer_index);
13932 	}
13933 	hwrm_req_drop(bp, req);
13934 	return rc;
13935 }
13936 
13937 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13938 {
13939 	struct bnxt_tx_ring_info *txr;
13940 	int i = bnapi->index, j;
13941 
13942 	bnxt_for_each_napi_tx(j, bnapi, txr)
13943 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13944 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13945 			    txr->tx_cons);
13946 }
13947 
13948 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13949 {
13950 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13951 	int i = bnapi->index;
13952 
13953 	if (!rxr)
13954 		return;
13955 
13956 	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",
13957 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13958 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13959 		    rxr->rx_sw_agg_prod);
13960 }
13961 
13962 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13963 {
13964 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13965 	int i = bnapi->index;
13966 
13967 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13968 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13969 }
13970 
13971 static void bnxt_dbg_dump_states(struct bnxt *bp)
13972 {
13973 	int i;
13974 	struct bnxt_napi *bnapi;
13975 
13976 	for (i = 0; i < bp->cp_nr_rings; i++) {
13977 		bnapi = bp->bnapi[i];
13978 		if (netif_msg_drv(bp)) {
13979 			bnxt_dump_tx_sw_state(bnapi);
13980 			bnxt_dump_rx_sw_state(bnapi);
13981 			bnxt_dump_cp_sw_state(bnapi);
13982 		}
13983 	}
13984 }
13985 
13986 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13987 {
13988 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13989 	struct hwrm_ring_reset_input *req;
13990 	struct bnxt_napi *bnapi = rxr->bnapi;
13991 	struct bnxt_cp_ring_info *cpr;
13992 	u16 cp_ring_id;
13993 	int rc;
13994 
13995 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13996 	if (rc)
13997 		return rc;
13998 
13999 	cpr = &bnapi->cp_ring;
14000 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
14001 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
14002 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
14003 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
14004 	return hwrm_req_send_silent(bp, req);
14005 }
14006 
14007 static void bnxt_reset_task(struct bnxt *bp, bool silent)
14008 {
14009 	if (!silent)
14010 		bnxt_dbg_dump_states(bp);
14011 	if (netif_running(bp->dev)) {
14012 		bnxt_close_nic(bp, !silent, false);
14013 		bnxt_open_nic(bp, !silent, false);
14014 	}
14015 }
14016 
14017 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
14018 {
14019 	struct bnxt *bp = netdev_priv(dev);
14020 
14021 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
14022 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
14023 }
14024 
14025 static void bnxt_fw_health_check(struct bnxt *bp)
14026 {
14027 	struct bnxt_fw_health *fw_health = bp->fw_health;
14028 	struct pci_dev *pdev = bp->pdev;
14029 	u32 val;
14030 
14031 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14032 		return;
14033 
14034 	/* Make sure it is enabled before checking the tmr_counter. */
14035 	smp_rmb();
14036 	if (fw_health->tmr_counter) {
14037 		fw_health->tmr_counter--;
14038 		return;
14039 	}
14040 
14041 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14042 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
14043 		fw_health->arrests++;
14044 		goto fw_reset;
14045 	}
14046 
14047 	fw_health->last_fw_heartbeat = val;
14048 
14049 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14050 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
14051 		fw_health->discoveries++;
14052 		goto fw_reset;
14053 	}
14054 
14055 	fw_health->tmr_counter = fw_health->tmr_multiplier;
14056 	return;
14057 
14058 fw_reset:
14059 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
14060 }
14061 
14062 static void bnxt_timer(struct timer_list *t)
14063 {
14064 	struct bnxt *bp = timer_container_of(bp, t, timer);
14065 	struct net_device *dev = bp->dev;
14066 
14067 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
14068 		return;
14069 
14070 	if (atomic_read(&bp->intr_sem) != 0)
14071 		goto bnxt_restart_timer;
14072 
14073 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14074 		bnxt_fw_health_check(bp);
14075 
14076 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14077 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14078 
14079 	if (bnxt_tc_flower_enabled(bp))
14080 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14081 
14082 #ifdef CONFIG_RFS_ACCEL
14083 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14084 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14085 #endif /*CONFIG_RFS_ACCEL*/
14086 
14087 	if (bp->link_info.phy_retry) {
14088 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14089 			bp->link_info.phy_retry = false;
14090 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14091 		} else {
14092 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14093 		}
14094 	}
14095 
14096 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14097 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14098 
14099 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14100 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14101 
14102 bnxt_restart_timer:
14103 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14104 }
14105 
14106 static void bnxt_lock_sp(struct bnxt *bp)
14107 {
14108 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14109 	 * set.  If the device is being closed, bnxt_close() may be holding
14110 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14111 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14112 	 * instance lock.
14113 	 */
14114 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14115 	netdev_lock(bp->dev);
14116 }
14117 
14118 static void bnxt_unlock_sp(struct bnxt *bp)
14119 {
14120 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14121 	netdev_unlock(bp->dev);
14122 }
14123 
14124 /* Same as bnxt_lock_sp() with additional rtnl_lock */
14125 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
14126 {
14127 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14128 	rtnl_lock();
14129 	netdev_lock(bp->dev);
14130 }
14131 
14132 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
14133 {
14134 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14135 	netdev_unlock(bp->dev);
14136 	rtnl_unlock();
14137 }
14138 
14139 /* Only called from bnxt_sp_task() */
14140 static void bnxt_reset(struct bnxt *bp, bool silent)
14141 {
14142 	bnxt_rtnl_lock_sp(bp);
14143 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14144 		bnxt_reset_task(bp, silent);
14145 	bnxt_rtnl_unlock_sp(bp);
14146 }
14147 
14148 /* Only called from bnxt_sp_task() */
14149 static void bnxt_rx_ring_reset(struct bnxt *bp)
14150 {
14151 	int i;
14152 
14153 	bnxt_rtnl_lock_sp(bp);
14154 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14155 		bnxt_rtnl_unlock_sp(bp);
14156 		return;
14157 	}
14158 	/* Disable and flush TPA before resetting the RX ring */
14159 	if (bp->flags & BNXT_FLAG_TPA)
14160 		bnxt_set_tpa(bp, false);
14161 	for (i = 0; i < bp->rx_nr_rings; i++) {
14162 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14163 		struct bnxt_cp_ring_info *cpr;
14164 		int rc;
14165 
14166 		if (!rxr->bnapi->in_reset)
14167 			continue;
14168 
14169 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14170 		if (rc) {
14171 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14172 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14173 			else
14174 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14175 					    rc);
14176 			bnxt_reset_task(bp, true);
14177 			break;
14178 		}
14179 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14180 		rxr->rx_prod = 0;
14181 		rxr->rx_agg_prod = 0;
14182 		rxr->rx_sw_agg_prod = 0;
14183 		rxr->rx_next_cons = 0;
14184 		rxr->bnapi->in_reset = false;
14185 		bnxt_alloc_one_rx_ring(bp, i);
14186 		cpr = &rxr->bnapi->cp_ring;
14187 		cpr->sw_stats->rx.rx_resets++;
14188 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14189 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14190 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14191 	}
14192 	if (bp->flags & BNXT_FLAG_TPA)
14193 		bnxt_set_tpa(bp, true);
14194 	bnxt_rtnl_unlock_sp(bp);
14195 }
14196 
14197 static void bnxt_fw_fatal_close(struct bnxt *bp)
14198 {
14199 	bnxt_tx_disable(bp);
14200 	bnxt_disable_napi(bp);
14201 	bnxt_disable_int_sync(bp);
14202 	bnxt_free_irq(bp);
14203 	bnxt_clear_int_mode(bp);
14204 	pci_disable_device(bp->pdev);
14205 }
14206 
14207 static void bnxt_fw_reset_close(struct bnxt *bp)
14208 {
14209 	/* When firmware is in fatal state, quiesce device and disable
14210 	 * bus master to prevent any potential bad DMAs before freeing
14211 	 * kernel memory.
14212 	 */
14213 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14214 		u16 val = 0;
14215 
14216 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14217 		if (val == 0xffff)
14218 			bp->fw_reset_min_dsecs = 0;
14219 		bnxt_fw_fatal_close(bp);
14220 	}
14221 	__bnxt_close_nic(bp, true, false);
14222 	bnxt_vf_reps_free(bp);
14223 	bnxt_clear_int_mode(bp);
14224 	bnxt_hwrm_func_drv_unrgtr(bp);
14225 	if (pci_is_enabled(bp->pdev))
14226 		pci_disable_device(bp->pdev);
14227 	bnxt_free_ctx_mem(bp, false);
14228 }
14229 
14230 static bool is_bnxt_fw_ok(struct bnxt *bp)
14231 {
14232 	struct bnxt_fw_health *fw_health = bp->fw_health;
14233 	bool no_heartbeat = false, has_reset = false;
14234 	u32 val;
14235 
14236 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14237 	if (val == fw_health->last_fw_heartbeat)
14238 		no_heartbeat = true;
14239 
14240 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14241 	if (val != fw_health->last_fw_reset_cnt)
14242 		has_reset = true;
14243 
14244 	if (!no_heartbeat && has_reset)
14245 		return true;
14246 
14247 	return false;
14248 }
14249 
14250 /* netdev instance lock is acquired before calling this function */
14251 static void bnxt_force_fw_reset(struct bnxt *bp)
14252 {
14253 	struct bnxt_fw_health *fw_health = bp->fw_health;
14254 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14255 	u32 wait_dsecs;
14256 
14257 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14258 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14259 		return;
14260 
14261 	/* we have to serialize with bnxt_refclk_read()*/
14262 	if (ptp) {
14263 		unsigned long flags;
14264 
14265 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14266 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14267 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14268 	} else {
14269 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14270 	}
14271 	bnxt_fw_reset_close(bp);
14272 	wait_dsecs = fw_health->master_func_wait_dsecs;
14273 	if (fw_health->primary) {
14274 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14275 			wait_dsecs = 0;
14276 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14277 	} else {
14278 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14279 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14280 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14281 	}
14282 
14283 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14284 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14285 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14286 }
14287 
14288 void bnxt_fw_exception(struct bnxt *bp)
14289 {
14290 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14291 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14292 	bnxt_ulp_stop(bp);
14293 	bnxt_lock_sp(bp);
14294 	bnxt_force_fw_reset(bp);
14295 	bnxt_unlock_sp(bp);
14296 }
14297 
14298 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14299  * < 0 on error.
14300  */
14301 static int bnxt_get_registered_vfs(struct bnxt *bp)
14302 {
14303 #ifdef CONFIG_BNXT_SRIOV
14304 	int rc;
14305 
14306 	if (!BNXT_PF(bp))
14307 		return 0;
14308 
14309 	rc = bnxt_hwrm_func_qcfg(bp);
14310 	if (rc) {
14311 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14312 		return rc;
14313 	}
14314 	if (bp->pf.registered_vfs)
14315 		return bp->pf.registered_vfs;
14316 	if (bp->sriov_cfg)
14317 		return 1;
14318 #endif
14319 	return 0;
14320 }
14321 
14322 void bnxt_fw_reset(struct bnxt *bp)
14323 {
14324 	bnxt_ulp_stop(bp);
14325 	bnxt_lock_sp(bp);
14326 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14327 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14328 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14329 		int n = 0, tmo;
14330 
14331 		/* we have to serialize with bnxt_refclk_read()*/
14332 		if (ptp) {
14333 			unsigned long flags;
14334 
14335 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14336 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14337 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14338 		} else {
14339 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14340 		}
14341 		if (bp->pf.active_vfs &&
14342 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14343 			n = bnxt_get_registered_vfs(bp);
14344 		if (n < 0) {
14345 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14346 				   n);
14347 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14348 			netif_close(bp->dev);
14349 			goto fw_reset_exit;
14350 		} else if (n > 0) {
14351 			u16 vf_tmo_dsecs = n * 10;
14352 
14353 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14354 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14355 			bp->fw_reset_state =
14356 				BNXT_FW_RESET_STATE_POLL_VF;
14357 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14358 			goto fw_reset_exit;
14359 		}
14360 		bnxt_fw_reset_close(bp);
14361 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14362 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14363 			tmo = HZ / 10;
14364 		} else {
14365 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14366 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14367 		}
14368 		bnxt_queue_fw_reset_work(bp, tmo);
14369 	}
14370 fw_reset_exit:
14371 	bnxt_unlock_sp(bp);
14372 }
14373 
14374 static void bnxt_chk_missed_irq(struct bnxt *bp)
14375 {
14376 	int i;
14377 
14378 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14379 		return;
14380 
14381 	for (i = 0; i < bp->cp_nr_rings; i++) {
14382 		struct bnxt_napi *bnapi = bp->bnapi[i];
14383 		struct bnxt_cp_ring_info *cpr;
14384 		u32 fw_ring_id;
14385 		int j;
14386 
14387 		if (!bnapi)
14388 			continue;
14389 
14390 		cpr = &bnapi->cp_ring;
14391 		for (j = 0; j < cpr->cp_ring_count; j++) {
14392 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14393 			u32 val[2];
14394 
14395 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14396 				continue;
14397 
14398 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14399 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14400 				continue;
14401 			}
14402 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14403 			bnxt_dbg_hwrm_ring_info_get(bp,
14404 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14405 				fw_ring_id, &val[0], &val[1]);
14406 			cpr->sw_stats->cmn.missed_irqs++;
14407 		}
14408 	}
14409 }
14410 
14411 static void bnxt_cfg_ntp_filters(struct bnxt *);
14412 
14413 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14414 {
14415 	struct bnxt_link_info *link_info = &bp->link_info;
14416 
14417 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14418 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14419 		if (bp->hwrm_spec_code >= 0x10201) {
14420 			if (link_info->auto_pause_setting &
14421 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14422 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14423 		} else {
14424 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14425 		}
14426 		bnxt_set_auto_speed(link_info);
14427 	} else {
14428 		bnxt_set_force_speed(link_info);
14429 		link_info->req_duplex = link_info->duplex_setting;
14430 	}
14431 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14432 		link_info->req_flow_ctrl =
14433 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14434 	else
14435 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14436 }
14437 
14438 static void bnxt_fw_echo_reply(struct bnxt *bp)
14439 {
14440 	struct bnxt_fw_health *fw_health = bp->fw_health;
14441 	struct hwrm_func_echo_response_input *req;
14442 	int rc;
14443 
14444 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14445 	if (rc)
14446 		return;
14447 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14448 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14449 	hwrm_req_send(bp, req);
14450 }
14451 
14452 static void bnxt_ulp_restart(struct bnxt *bp)
14453 {
14454 	bnxt_ulp_stop(bp);
14455 	bnxt_ulp_start(bp, 0);
14456 }
14457 
14458 static void bnxt_sp_task(struct work_struct *work)
14459 {
14460 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14461 
14462 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14463 	smp_mb__after_atomic();
14464 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14465 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14466 		return;
14467 	}
14468 
14469 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14470 		bnxt_ulp_restart(bp);
14471 		bnxt_reenable_sriov(bp);
14472 	}
14473 
14474 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14475 		bnxt_cfg_rx_mode(bp);
14476 
14477 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14478 		bnxt_cfg_ntp_filters(bp);
14479 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14480 		bnxt_hwrm_exec_fwd_req(bp);
14481 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14482 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14483 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14484 		bnxt_hwrm_port_qstats(bp, 0);
14485 		bnxt_hwrm_port_qstats_ext(bp, 0);
14486 		bnxt_accumulate_all_stats(bp);
14487 	}
14488 
14489 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14490 		int rc;
14491 
14492 		mutex_lock(&bp->link_lock);
14493 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14494 				       &bp->sp_event))
14495 			bnxt_hwrm_phy_qcaps(bp);
14496 
14497 		rc = bnxt_update_link(bp, true);
14498 		if (rc)
14499 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14500 				   rc);
14501 
14502 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14503 				       &bp->sp_event))
14504 			bnxt_init_ethtool_link_settings(bp);
14505 		mutex_unlock(&bp->link_lock);
14506 	}
14507 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14508 		int rc;
14509 
14510 		mutex_lock(&bp->link_lock);
14511 		rc = bnxt_update_phy_setting(bp);
14512 		mutex_unlock(&bp->link_lock);
14513 		if (rc) {
14514 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14515 		} else {
14516 			bp->link_info.phy_retry = false;
14517 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14518 		}
14519 	}
14520 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14521 		mutex_lock(&bp->link_lock);
14522 		bnxt_get_port_module_status(bp);
14523 		mutex_unlock(&bp->link_lock);
14524 	}
14525 
14526 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14527 		bnxt_tc_flow_stats_work(bp);
14528 
14529 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14530 		bnxt_chk_missed_irq(bp);
14531 
14532 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14533 		bnxt_fw_echo_reply(bp);
14534 
14535 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14536 		bnxt_hwmon_notify_event(bp);
14537 
14538 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14539 	 * must be the last functions to be called before exiting.
14540 	 */
14541 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14542 		bnxt_reset(bp, false);
14543 
14544 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14545 		bnxt_reset(bp, true);
14546 
14547 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14548 		bnxt_rx_ring_reset(bp);
14549 
14550 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14551 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14552 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14553 			bnxt_devlink_health_fw_report(bp);
14554 		else
14555 			bnxt_fw_reset(bp);
14556 	}
14557 
14558 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14559 		if (!is_bnxt_fw_ok(bp))
14560 			bnxt_devlink_health_fw_report(bp);
14561 	}
14562 
14563 	smp_mb__before_atomic();
14564 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14565 }
14566 
14567 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14568 				int *max_cp);
14569 
14570 /* Under netdev instance lock */
14571 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14572 		     int tx_xdp)
14573 {
14574 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14575 	struct bnxt_hw_rings hwr = {0};
14576 	int rx_rings = rx;
14577 	int rc;
14578 
14579 	if (tcs)
14580 		tx_sets = tcs;
14581 
14582 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14583 
14584 	if (max_rx < rx_rings)
14585 		return -ENOMEM;
14586 
14587 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14588 		rx_rings <<= 1;
14589 
14590 	hwr.rx = rx_rings;
14591 	hwr.tx = tx * tx_sets + tx_xdp;
14592 	if (max_tx < hwr.tx)
14593 		return -ENOMEM;
14594 
14595 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14596 
14597 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14598 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14599 	if (max_cp < hwr.cp)
14600 		return -ENOMEM;
14601 	hwr.stat = hwr.cp;
14602 	if (BNXT_NEW_RM(bp)) {
14603 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14604 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14605 		hwr.grp = rx;
14606 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14607 	}
14608 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14609 		hwr.cp_p5 = hwr.tx + rx;
14610 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14611 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14612 		if (!bnxt_ulp_registered(bp->edev)) {
14613 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14614 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14615 		}
14616 		if (hwr.cp > bp->total_irqs) {
14617 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14618 
14619 			if (total_msix < hwr.cp) {
14620 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14621 					    hwr.cp, total_msix);
14622 				rc = -ENOSPC;
14623 			}
14624 		}
14625 	}
14626 	return rc;
14627 }
14628 
14629 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14630 {
14631 	if (bp->bar2) {
14632 		pci_iounmap(pdev, bp->bar2);
14633 		bp->bar2 = NULL;
14634 	}
14635 
14636 	if (bp->bar1) {
14637 		pci_iounmap(pdev, bp->bar1);
14638 		bp->bar1 = NULL;
14639 	}
14640 
14641 	if (bp->bar0) {
14642 		pci_iounmap(pdev, bp->bar0);
14643 		bp->bar0 = NULL;
14644 	}
14645 }
14646 
14647 static void bnxt_cleanup_pci(struct bnxt *bp)
14648 {
14649 	bnxt_unmap_bars(bp, bp->pdev);
14650 	pci_release_regions(bp->pdev);
14651 	if (pci_is_enabled(bp->pdev))
14652 		pci_disable_device(bp->pdev);
14653 }
14654 
14655 static void bnxt_init_dflt_coal(struct bnxt *bp)
14656 {
14657 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14658 	struct bnxt_coal *coal;
14659 	u16 flags = 0;
14660 
14661 	if (coal_cap->cmpl_params &
14662 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14663 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14664 
14665 	/* Tick values in micro seconds.
14666 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14667 	 */
14668 	coal = &bp->rx_coal;
14669 	coal->coal_ticks = 10;
14670 	coal->coal_bufs = 30;
14671 	coal->coal_ticks_irq = 1;
14672 	coal->coal_bufs_irq = 2;
14673 	coal->idle_thresh = 50;
14674 	coal->bufs_per_record = 2;
14675 	coal->budget = 64;		/* NAPI budget */
14676 	coal->flags = flags;
14677 
14678 	coal = &bp->tx_coal;
14679 	coal->coal_ticks = 28;
14680 	coal->coal_bufs = 30;
14681 	coal->coal_ticks_irq = 2;
14682 	coal->coal_bufs_irq = 2;
14683 	coal->bufs_per_record = 1;
14684 	coal->flags = flags;
14685 
14686 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14687 }
14688 
14689 /* FW that pre-reserves 1 VNIC per function */
14690 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14691 {
14692 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14693 
14694 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14695 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14696 		return true;
14697 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14698 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14699 		return true;
14700 	return false;
14701 }
14702 
14703 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14704 {
14705 	int rc;
14706 
14707 	bp->fw_cap = 0;
14708 	rc = bnxt_hwrm_ver_get(bp);
14709 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14710 	 * so wait before continuing with recovery.
14711 	 */
14712 	if (rc)
14713 		msleep(100);
14714 	bnxt_try_map_fw_health_reg(bp);
14715 	if (rc) {
14716 		rc = bnxt_try_recover_fw(bp);
14717 		if (rc)
14718 			return rc;
14719 		rc = bnxt_hwrm_ver_get(bp);
14720 		if (rc)
14721 			return rc;
14722 	}
14723 
14724 	bnxt_nvm_cfg_ver_get(bp);
14725 
14726 	rc = bnxt_hwrm_func_reset(bp);
14727 	if (rc)
14728 		return -ENODEV;
14729 
14730 	bnxt_hwrm_fw_set_time(bp);
14731 	return 0;
14732 }
14733 
14734 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14735 {
14736 	int rc;
14737 
14738 	/* Get the MAX capabilities for this function */
14739 	rc = bnxt_hwrm_func_qcaps(bp);
14740 	if (rc) {
14741 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14742 			   rc);
14743 		return -ENODEV;
14744 	}
14745 
14746 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14747 	if (rc)
14748 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14749 			    rc);
14750 
14751 	if (bnxt_alloc_fw_health(bp)) {
14752 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14753 	} else {
14754 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14755 		if (rc)
14756 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14757 				    rc);
14758 	}
14759 
14760 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14761 	if (rc)
14762 		return -ENODEV;
14763 
14764 	rc = bnxt_alloc_crash_dump_mem(bp);
14765 	if (rc)
14766 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14767 			    rc);
14768 	if (!rc) {
14769 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14770 		if (rc) {
14771 			bnxt_free_crash_dump_mem(bp);
14772 			netdev_warn(bp->dev,
14773 				    "hwrm crash dump mem failure rc: %d\n", rc);
14774 		}
14775 	}
14776 
14777 	if (bnxt_fw_pre_resv_vnics(bp))
14778 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14779 
14780 	bnxt_hwrm_func_qcfg(bp);
14781 	bnxt_hwrm_vnic_qcaps(bp);
14782 	bnxt_hwrm_port_led_qcaps(bp);
14783 	bnxt_ethtool_init(bp);
14784 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14785 		__bnxt_hwrm_ptp_qcfg(bp);
14786 	bnxt_dcb_init(bp);
14787 	bnxt_hwmon_init(bp);
14788 	return 0;
14789 }
14790 
14791 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14792 {
14793 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14794 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14795 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14796 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14797 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14798 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14799 		bp->rss_hash_delta = bp->rss_hash_cfg;
14800 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14801 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14802 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14803 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14804 	}
14805 }
14806 
14807 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14808 {
14809 	struct net_device *dev = bp->dev;
14810 
14811 	dev->hw_features &= ~NETIF_F_NTUPLE;
14812 	dev->features &= ~NETIF_F_NTUPLE;
14813 	bp->flags &= ~BNXT_FLAG_RFS;
14814 	if (bnxt_rfs_supported(bp)) {
14815 		dev->hw_features |= NETIF_F_NTUPLE;
14816 		if (bnxt_rfs_capable(bp, false)) {
14817 			bp->flags |= BNXT_FLAG_RFS;
14818 			dev->features |= NETIF_F_NTUPLE;
14819 		}
14820 	}
14821 }
14822 
14823 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14824 {
14825 	struct pci_dev *pdev = bp->pdev;
14826 
14827 	bnxt_set_dflt_rss_hash_type(bp);
14828 	bnxt_set_dflt_rfs(bp);
14829 
14830 	bnxt_get_wol_settings(bp);
14831 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14832 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14833 	else
14834 		device_set_wakeup_capable(&pdev->dev, false);
14835 
14836 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14837 	bnxt_hwrm_coal_params_qcaps(bp);
14838 }
14839 
14840 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14841 
14842 int bnxt_fw_init_one(struct bnxt *bp)
14843 {
14844 	int rc;
14845 
14846 	rc = bnxt_fw_init_one_p1(bp);
14847 	if (rc) {
14848 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14849 		return rc;
14850 	}
14851 	rc = bnxt_fw_init_one_p2(bp);
14852 	if (rc) {
14853 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14854 		return rc;
14855 	}
14856 	rc = bnxt_probe_phy(bp, false);
14857 	if (rc)
14858 		return rc;
14859 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14860 	if (rc)
14861 		return rc;
14862 
14863 	bnxt_fw_init_one_p3(bp);
14864 	return 0;
14865 }
14866 
14867 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14868 {
14869 	struct bnxt_fw_health *fw_health = bp->fw_health;
14870 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14871 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14872 	u32 reg_type, reg_off, delay_msecs;
14873 
14874 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14875 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14876 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14877 	switch (reg_type) {
14878 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14879 		pci_write_config_dword(bp->pdev, reg_off, val);
14880 		break;
14881 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14882 		writel(reg_off & BNXT_GRC_BASE_MASK,
14883 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14884 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14885 		fallthrough;
14886 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14887 		writel(val, bp->bar0 + reg_off);
14888 		break;
14889 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14890 		writel(val, bp->bar1 + reg_off);
14891 		break;
14892 	}
14893 	if (delay_msecs) {
14894 		pci_read_config_dword(bp->pdev, 0, &val);
14895 		msleep(delay_msecs);
14896 	}
14897 }
14898 
14899 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14900 {
14901 	struct hwrm_func_qcfg_output *resp;
14902 	struct hwrm_func_qcfg_input *req;
14903 	bool result = true; /* firmware will enforce if unknown */
14904 
14905 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14906 		return result;
14907 
14908 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14909 		return result;
14910 
14911 	req->fid = cpu_to_le16(0xffff);
14912 	resp = hwrm_req_hold(bp, req);
14913 	if (!hwrm_req_send(bp, req))
14914 		result = !!(le16_to_cpu(resp->flags) &
14915 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14916 	hwrm_req_drop(bp, req);
14917 	return result;
14918 }
14919 
14920 static void bnxt_reset_all(struct bnxt *bp)
14921 {
14922 	struct bnxt_fw_health *fw_health = bp->fw_health;
14923 	int i, rc;
14924 
14925 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14926 		bnxt_fw_reset_via_optee(bp);
14927 		bp->fw_reset_timestamp = jiffies;
14928 		return;
14929 	}
14930 
14931 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14932 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14933 			bnxt_fw_reset_writel(bp, i);
14934 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14935 		struct hwrm_fw_reset_input *req;
14936 
14937 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14938 		if (!rc) {
14939 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14940 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14941 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14942 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14943 			rc = hwrm_req_send(bp, req);
14944 		}
14945 		if (rc != -ENODEV)
14946 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14947 	}
14948 	bp->fw_reset_timestamp = jiffies;
14949 }
14950 
14951 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14952 {
14953 	return time_after(jiffies, bp->fw_reset_timestamp +
14954 			  (bp->fw_reset_max_dsecs * HZ / 10));
14955 }
14956 
14957 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14958 {
14959 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14960 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14961 		bnxt_dl_health_fw_status_update(bp, false);
14962 	bp->fw_reset_state = BNXT_FW_RESET_STATE_ABORT;
14963 	netif_close(bp->dev);
14964 }
14965 
14966 static void bnxt_fw_reset_task(struct work_struct *work)
14967 {
14968 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14969 	int rc = 0;
14970 
14971 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14972 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14973 		return;
14974 	}
14975 
14976 	switch (bp->fw_reset_state) {
14977 	case BNXT_FW_RESET_STATE_POLL_VF: {
14978 		int n = bnxt_get_registered_vfs(bp);
14979 		int tmo;
14980 
14981 		if (n < 0) {
14982 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14983 				   n, jiffies_to_msecs(jiffies -
14984 				   bp->fw_reset_timestamp));
14985 			goto fw_reset_abort;
14986 		} else if (n > 0) {
14987 			if (bnxt_fw_reset_timeout(bp)) {
14988 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14989 				bp->fw_reset_state = 0;
14990 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14991 					   n);
14992 				goto ulp_start;
14993 			}
14994 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14995 			return;
14996 		}
14997 		bp->fw_reset_timestamp = jiffies;
14998 		netdev_lock(bp->dev);
14999 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
15000 			bnxt_fw_reset_abort(bp, rc);
15001 			netdev_unlock(bp->dev);
15002 			goto ulp_start;
15003 		}
15004 		bnxt_fw_reset_close(bp);
15005 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
15006 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
15007 			tmo = HZ / 10;
15008 		} else {
15009 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15010 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
15011 		}
15012 		netdev_unlock(bp->dev);
15013 		bnxt_queue_fw_reset_work(bp, tmo);
15014 		return;
15015 	}
15016 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
15017 		u32 val;
15018 
15019 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15020 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
15021 		    !bnxt_fw_reset_timeout(bp)) {
15022 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15023 			return;
15024 		}
15025 
15026 		if (!bp->fw_health->primary) {
15027 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
15028 
15029 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15030 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
15031 			return;
15032 		}
15033 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
15034 	}
15035 		fallthrough;
15036 	case BNXT_FW_RESET_STATE_RESET_FW:
15037 		bnxt_reset_all(bp);
15038 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
15039 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
15040 		return;
15041 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
15042 		bnxt_inv_fw_health_reg(bp);
15043 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
15044 		    !bp->fw_reset_min_dsecs) {
15045 			u16 val;
15046 
15047 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
15048 			if (val == 0xffff) {
15049 				if (bnxt_fw_reset_timeout(bp)) {
15050 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
15051 					rc = -ETIMEDOUT;
15052 					goto fw_reset_abort;
15053 				}
15054 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
15055 				return;
15056 			}
15057 		}
15058 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
15059 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
15060 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
15061 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
15062 			bnxt_dl_remote_reload(bp);
15063 		if (pci_enable_device(bp->pdev)) {
15064 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
15065 			rc = -ENODEV;
15066 			goto fw_reset_abort;
15067 		}
15068 		pci_set_master(bp->pdev);
15069 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
15070 		fallthrough;
15071 	case BNXT_FW_RESET_STATE_POLL_FW:
15072 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
15073 		rc = bnxt_hwrm_poll(bp);
15074 		if (rc) {
15075 			if (bnxt_fw_reset_timeout(bp)) {
15076 				netdev_err(bp->dev, "Firmware reset aborted\n");
15077 				goto fw_reset_abort_status;
15078 			}
15079 			bnxt_queue_fw_reset_work(bp, HZ / 5);
15080 			return;
15081 		}
15082 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
15083 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15084 		fallthrough;
15085 	case BNXT_FW_RESET_STATE_OPENING:
15086 		while (!rtnl_trylock()) {
15087 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15088 			return;
15089 		}
15090 		netdev_lock(bp->dev);
15091 		rc = bnxt_open(bp->dev);
15092 		if (rc) {
15093 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15094 			bnxt_fw_reset_abort(bp, rc);
15095 			netdev_unlock(bp->dev);
15096 			rtnl_unlock();
15097 			goto ulp_start;
15098 		}
15099 
15100 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15101 		    bp->fw_health->enabled) {
15102 			bp->fw_health->last_fw_reset_cnt =
15103 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15104 		}
15105 		bp->fw_reset_state = 0;
15106 		/* Make sure fw_reset_state is 0 before clearing the flag */
15107 		smp_mb__before_atomic();
15108 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15109 		bnxt_ptp_reapply_pps(bp);
15110 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15111 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15112 			bnxt_dl_health_fw_recovery_done(bp);
15113 			bnxt_dl_health_fw_status_update(bp, true);
15114 		}
15115 		netdev_unlock(bp->dev);
15116 		rtnl_unlock();
15117 		bnxt_ulp_start(bp, 0);
15118 		bnxt_reenable_sriov(bp);
15119 		netdev_lock(bp->dev);
15120 		bnxt_vf_reps_alloc(bp);
15121 		bnxt_vf_reps_open(bp);
15122 		netdev_unlock(bp->dev);
15123 		break;
15124 	}
15125 	return;
15126 
15127 fw_reset_abort_status:
15128 	if (bp->fw_health->status_reliable ||
15129 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15130 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15131 
15132 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15133 	}
15134 fw_reset_abort:
15135 	netdev_lock(bp->dev);
15136 	bnxt_fw_reset_abort(bp, rc);
15137 	netdev_unlock(bp->dev);
15138 ulp_start:
15139 	bnxt_ulp_start(bp, rc);
15140 }
15141 
15142 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15143 {
15144 	int rc;
15145 	struct bnxt *bp = netdev_priv(dev);
15146 
15147 	SET_NETDEV_DEV(dev, &pdev->dev);
15148 
15149 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15150 	rc = pci_enable_device(pdev);
15151 	if (rc) {
15152 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15153 		goto init_err;
15154 	}
15155 
15156 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15157 		dev_err(&pdev->dev,
15158 			"Cannot find PCI device base address, aborting\n");
15159 		rc = -ENODEV;
15160 		goto init_err_disable;
15161 	}
15162 
15163 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15164 	if (rc) {
15165 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15166 		goto init_err_disable;
15167 	}
15168 
15169 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15170 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15171 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15172 		rc = -EIO;
15173 		goto init_err_release;
15174 	}
15175 
15176 	pci_set_master(pdev);
15177 
15178 	bp->dev = dev;
15179 	bp->pdev = pdev;
15180 
15181 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15182 	 * determines the BAR size.
15183 	 */
15184 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15185 	if (!bp->bar0) {
15186 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15187 		rc = -ENOMEM;
15188 		goto init_err_release;
15189 	}
15190 
15191 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15192 	if (!bp->bar2) {
15193 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15194 		rc = -ENOMEM;
15195 		goto init_err_release;
15196 	}
15197 
15198 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15199 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15200 
15201 	spin_lock_init(&bp->ntp_fltr_lock);
15202 #if BITS_PER_LONG == 32
15203 	spin_lock_init(&bp->db_lock);
15204 #endif
15205 
15206 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15207 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15208 
15209 	timer_setup(&bp->timer, bnxt_timer, 0);
15210 	bp->current_interval = BNXT_TIMER_INTERVAL;
15211 
15212 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15213 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15214 
15215 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15216 	return 0;
15217 
15218 init_err_release:
15219 	bnxt_unmap_bars(bp, pdev);
15220 	pci_release_regions(pdev);
15221 
15222 init_err_disable:
15223 	pci_disable_device(pdev);
15224 
15225 init_err:
15226 	return rc;
15227 }
15228 
15229 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15230 {
15231 	struct sockaddr *addr = p;
15232 	struct bnxt *bp = netdev_priv(dev);
15233 	int rc = 0;
15234 
15235 	netdev_assert_locked(dev);
15236 
15237 	if (!is_valid_ether_addr(addr->sa_data))
15238 		return -EADDRNOTAVAIL;
15239 
15240 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15241 		return 0;
15242 
15243 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15244 	if (rc)
15245 		return rc;
15246 
15247 	eth_hw_addr_set(dev, addr->sa_data);
15248 	bnxt_clear_usr_fltrs(bp, true);
15249 	if (netif_running(dev)) {
15250 		bnxt_close_nic(bp, false, false);
15251 		rc = bnxt_open_nic(bp, false, false);
15252 	}
15253 
15254 	return rc;
15255 }
15256 
15257 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15258 {
15259 	struct bnxt *bp = netdev_priv(dev);
15260 
15261 	netdev_assert_locked(dev);
15262 
15263 	if (netif_running(dev))
15264 		bnxt_close_nic(bp, true, false);
15265 
15266 	WRITE_ONCE(dev->mtu, new_mtu);
15267 
15268 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15269 	 * program is attached.  We need to set the AGG rings settings and
15270 	 * rx_skb_func accordingly.
15271 	 */
15272 	if (READ_ONCE(bp->xdp_prog))
15273 		bnxt_set_rx_skb_mode(bp, true);
15274 
15275 	bnxt_set_ring_params(bp);
15276 
15277 	if (netif_running(dev))
15278 		return bnxt_open_nic(bp, true, false);
15279 
15280 	return 0;
15281 }
15282 
15283 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15284 {
15285 	struct bnxt *bp = netdev_priv(dev);
15286 	bool sh = false;
15287 	int rc, tx_cp;
15288 
15289 	if (tc > bp->max_tc) {
15290 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15291 			   tc, bp->max_tc);
15292 		return -EINVAL;
15293 	}
15294 
15295 	if (bp->num_tc == tc)
15296 		return 0;
15297 
15298 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15299 		sh = true;
15300 
15301 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15302 			      sh, tc, bp->tx_nr_rings_xdp);
15303 	if (rc)
15304 		return rc;
15305 
15306 	/* Needs to close the device and do hw resource re-allocations */
15307 	if (netif_running(bp->dev))
15308 		bnxt_close_nic(bp, true, false);
15309 
15310 	if (tc) {
15311 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15312 		netdev_set_num_tc(dev, tc);
15313 		bp->num_tc = tc;
15314 	} else {
15315 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15316 		netdev_reset_tc(dev);
15317 		bp->num_tc = 0;
15318 	}
15319 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15320 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15321 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15322 			       tx_cp + bp->rx_nr_rings;
15323 
15324 	if (netif_running(bp->dev))
15325 		return bnxt_open_nic(bp, true, false);
15326 
15327 	return 0;
15328 }
15329 
15330 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15331 				  void *cb_priv)
15332 {
15333 	struct bnxt *bp = cb_priv;
15334 
15335 	if (!bnxt_tc_flower_enabled(bp) ||
15336 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15337 		return -EOPNOTSUPP;
15338 
15339 	switch (type) {
15340 	case TC_SETUP_CLSFLOWER:
15341 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15342 	default:
15343 		return -EOPNOTSUPP;
15344 	}
15345 }
15346 
15347 LIST_HEAD(bnxt_block_cb_list);
15348 
15349 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15350 			 void *type_data)
15351 {
15352 	struct bnxt *bp = netdev_priv(dev);
15353 
15354 	switch (type) {
15355 	case TC_SETUP_BLOCK:
15356 		return flow_block_cb_setup_simple(type_data,
15357 						  &bnxt_block_cb_list,
15358 						  bnxt_setup_tc_block_cb,
15359 						  bp, bp, true);
15360 	case TC_SETUP_QDISC_MQPRIO: {
15361 		struct tc_mqprio_qopt *mqprio = type_data;
15362 
15363 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15364 
15365 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15366 	}
15367 	default:
15368 		return -EOPNOTSUPP;
15369 	}
15370 }
15371 
15372 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15373 			    const struct sk_buff *skb)
15374 {
15375 	struct bnxt_vnic_info *vnic;
15376 
15377 	if (skb)
15378 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15379 
15380 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15381 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15382 }
15383 
15384 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15385 			   u32 idx)
15386 {
15387 	struct hlist_head *head;
15388 	int bit_id;
15389 
15390 	spin_lock_bh(&bp->ntp_fltr_lock);
15391 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15392 	if (bit_id < 0) {
15393 		spin_unlock_bh(&bp->ntp_fltr_lock);
15394 		return -ENOMEM;
15395 	}
15396 
15397 	fltr->base.sw_id = (u16)bit_id;
15398 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15399 	fltr->base.flags |= BNXT_ACT_RING_DST;
15400 	head = &bp->ntp_fltr_hash_tbl[idx];
15401 	hlist_add_head_rcu(&fltr->base.hash, head);
15402 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15403 	bnxt_insert_usr_fltr(bp, &fltr->base);
15404 	bp->ntp_fltr_count++;
15405 	spin_unlock_bh(&bp->ntp_fltr_lock);
15406 	return 0;
15407 }
15408 
15409 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15410 			    struct bnxt_ntuple_filter *f2)
15411 {
15412 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15413 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15414 	struct flow_keys *keys1 = &f1->fkeys;
15415 	struct flow_keys *keys2 = &f2->fkeys;
15416 
15417 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15418 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15419 		return false;
15420 
15421 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15422 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15423 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15424 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15425 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15426 			return false;
15427 	} else {
15428 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15429 				     &keys2->addrs.v6addrs.src) ||
15430 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15431 				     &masks2->addrs.v6addrs.src) ||
15432 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15433 				     &keys2->addrs.v6addrs.dst) ||
15434 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15435 				     &masks2->addrs.v6addrs.dst))
15436 			return false;
15437 	}
15438 
15439 	return keys1->ports.src == keys2->ports.src &&
15440 	       masks1->ports.src == masks2->ports.src &&
15441 	       keys1->ports.dst == keys2->ports.dst &&
15442 	       masks1->ports.dst == masks2->ports.dst &&
15443 	       keys1->control.flags == keys2->control.flags &&
15444 	       f1->l2_fltr == f2->l2_fltr;
15445 }
15446 
15447 struct bnxt_ntuple_filter *
15448 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15449 				struct bnxt_ntuple_filter *fltr, u32 idx)
15450 {
15451 	struct bnxt_ntuple_filter *f;
15452 	struct hlist_head *head;
15453 
15454 	head = &bp->ntp_fltr_hash_tbl[idx];
15455 	hlist_for_each_entry_rcu(f, head, base.hash) {
15456 		if (bnxt_fltr_match(f, fltr))
15457 			return f;
15458 	}
15459 	return NULL;
15460 }
15461 
15462 #ifdef CONFIG_RFS_ACCEL
15463 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15464 			      u16 rxq_index, u32 flow_id)
15465 {
15466 	struct bnxt *bp = netdev_priv(dev);
15467 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15468 	struct flow_keys *fkeys;
15469 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15470 	struct bnxt_l2_filter *l2_fltr;
15471 	int rc = 0, idx;
15472 	u32 flags;
15473 
15474 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15475 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15476 		atomic_inc(&l2_fltr->refcnt);
15477 	} else {
15478 		struct bnxt_l2_key key;
15479 
15480 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15481 		key.vlan = 0;
15482 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15483 		if (!l2_fltr)
15484 			return -EINVAL;
15485 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15486 			bnxt_del_l2_filter(bp, l2_fltr);
15487 			return -EINVAL;
15488 		}
15489 	}
15490 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15491 	if (!new_fltr) {
15492 		bnxt_del_l2_filter(bp, l2_fltr);
15493 		return -ENOMEM;
15494 	}
15495 
15496 	fkeys = &new_fltr->fkeys;
15497 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15498 		rc = -EPROTONOSUPPORT;
15499 		goto err_free;
15500 	}
15501 
15502 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15503 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15504 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15505 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15506 		rc = -EPROTONOSUPPORT;
15507 		goto err_free;
15508 	}
15509 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15510 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15511 		if (bp->hwrm_spec_code < 0x10601) {
15512 			rc = -EPROTONOSUPPORT;
15513 			goto err_free;
15514 		}
15515 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15516 	}
15517 	flags = fkeys->control.flags;
15518 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15519 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15520 		rc = -EPROTONOSUPPORT;
15521 		goto err_free;
15522 	}
15523 	new_fltr->l2_fltr = l2_fltr;
15524 
15525 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15526 	rcu_read_lock();
15527 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15528 	if (fltr) {
15529 		rc = fltr->base.sw_id;
15530 		rcu_read_unlock();
15531 		goto err_free;
15532 	}
15533 	rcu_read_unlock();
15534 
15535 	new_fltr->flow_id = flow_id;
15536 	new_fltr->base.rxq = rxq_index;
15537 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15538 	if (!rc) {
15539 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15540 		return new_fltr->base.sw_id;
15541 	}
15542 
15543 err_free:
15544 	bnxt_del_l2_filter(bp, l2_fltr);
15545 	kfree(new_fltr);
15546 	return rc;
15547 }
15548 #endif
15549 
15550 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15551 {
15552 	spin_lock_bh(&bp->ntp_fltr_lock);
15553 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15554 		spin_unlock_bh(&bp->ntp_fltr_lock);
15555 		return;
15556 	}
15557 	hlist_del_rcu(&fltr->base.hash);
15558 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15559 	bp->ntp_fltr_count--;
15560 	spin_unlock_bh(&bp->ntp_fltr_lock);
15561 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15562 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15563 	kfree_rcu(fltr, base.rcu);
15564 }
15565 
15566 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15567 {
15568 #ifdef CONFIG_RFS_ACCEL
15569 	int i;
15570 
15571 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15572 		struct hlist_head *head;
15573 		struct hlist_node *tmp;
15574 		struct bnxt_ntuple_filter *fltr;
15575 		int rc;
15576 
15577 		head = &bp->ntp_fltr_hash_tbl[i];
15578 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15579 			bool del = false;
15580 
15581 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15582 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15583 					continue;
15584 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15585 							fltr->flow_id,
15586 							fltr->base.sw_id)) {
15587 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15588 									 fltr);
15589 					del = true;
15590 				}
15591 			} else {
15592 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15593 								       fltr);
15594 				if (rc)
15595 					del = true;
15596 				else
15597 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15598 			}
15599 
15600 			if (del)
15601 				bnxt_del_ntp_filter(bp, fltr);
15602 		}
15603 	}
15604 #endif
15605 }
15606 
15607 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15608 				    unsigned int entry, struct udp_tunnel_info *ti)
15609 {
15610 	struct bnxt *bp = netdev_priv(netdev);
15611 	unsigned int cmd;
15612 
15613 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15614 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15615 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15616 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15617 	else
15618 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15619 
15620 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15621 }
15622 
15623 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15624 				      unsigned int entry, struct udp_tunnel_info *ti)
15625 {
15626 	struct bnxt *bp = netdev_priv(netdev);
15627 	unsigned int cmd;
15628 
15629 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15630 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15631 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15632 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15633 	else
15634 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15635 
15636 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15637 }
15638 
15639 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15640 	.set_port	= bnxt_udp_tunnel_set_port,
15641 	.unset_port	= bnxt_udp_tunnel_unset_port,
15642 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15643 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15644 	.tables		= {
15645 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15646 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15647 	},
15648 }, bnxt_udp_tunnels_p7 = {
15649 	.set_port	= bnxt_udp_tunnel_set_port,
15650 	.unset_port	= bnxt_udp_tunnel_unset_port,
15651 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15652 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15653 	.tables		= {
15654 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15655 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15656 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15657 	},
15658 };
15659 
15660 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15661 			       struct net_device *dev, u32 filter_mask,
15662 			       int nlflags)
15663 {
15664 	struct bnxt *bp = netdev_priv(dev);
15665 
15666 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15667 				       nlflags, filter_mask, NULL);
15668 }
15669 
15670 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15671 			       u16 flags, struct netlink_ext_ack *extack)
15672 {
15673 	struct bnxt *bp = netdev_priv(dev);
15674 	struct nlattr *attr, *br_spec;
15675 	int rem, rc = 0;
15676 
15677 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15678 		return -EOPNOTSUPP;
15679 
15680 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15681 	if (!br_spec)
15682 		return -EINVAL;
15683 
15684 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15685 		u16 mode;
15686 
15687 		mode = nla_get_u16(attr);
15688 		if (mode == bp->br_mode)
15689 			break;
15690 
15691 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15692 		if (!rc)
15693 			bp->br_mode = mode;
15694 		break;
15695 	}
15696 	return rc;
15697 }
15698 
15699 int bnxt_get_port_parent_id(struct net_device *dev,
15700 			    struct netdev_phys_item_id *ppid)
15701 {
15702 	struct bnxt *bp = netdev_priv(dev);
15703 
15704 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15705 		return -EOPNOTSUPP;
15706 
15707 	/* The PF and it's VF-reps only support the switchdev framework */
15708 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15709 		return -EOPNOTSUPP;
15710 
15711 	ppid->id_len = sizeof(bp->dsn);
15712 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15713 
15714 	return 0;
15715 }
15716 
15717 static const struct net_device_ops bnxt_netdev_ops = {
15718 	.ndo_open		= bnxt_open,
15719 	.ndo_start_xmit		= bnxt_start_xmit,
15720 	.ndo_stop		= bnxt_close,
15721 	.ndo_get_stats64	= bnxt_get_stats64,
15722 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15723 	.ndo_eth_ioctl		= bnxt_ioctl,
15724 	.ndo_validate_addr	= eth_validate_addr,
15725 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15726 	.ndo_change_mtu		= bnxt_change_mtu,
15727 	.ndo_fix_features	= bnxt_fix_features,
15728 	.ndo_set_features	= bnxt_set_features,
15729 	.ndo_features_check	= bnxt_features_check,
15730 	.ndo_tx_timeout		= bnxt_tx_timeout,
15731 #ifdef CONFIG_BNXT_SRIOV
15732 	.ndo_get_vf_config	= bnxt_get_vf_config,
15733 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15734 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15735 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15736 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15737 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15738 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15739 #endif
15740 	.ndo_setup_tc           = bnxt_setup_tc,
15741 #ifdef CONFIG_RFS_ACCEL
15742 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15743 #endif
15744 	.ndo_bpf		= bnxt_xdp,
15745 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15746 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15747 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15748 };
15749 
15750 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15751 				    struct netdev_queue_stats_rx *stats)
15752 {
15753 	struct bnxt *bp = netdev_priv(dev);
15754 	struct bnxt_cp_ring_info *cpr;
15755 	u64 *sw;
15756 
15757 	if (!bp->bnapi)
15758 		return;
15759 
15760 	cpr = &bp->bnapi[i]->cp_ring;
15761 	sw = cpr->stats.sw_stats;
15762 
15763 	stats->packets = 0;
15764 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15765 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15766 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15767 
15768 	stats->bytes = 0;
15769 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15770 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15771 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15772 
15773 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15774 }
15775 
15776 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15777 				    struct netdev_queue_stats_tx *stats)
15778 {
15779 	struct bnxt *bp = netdev_priv(dev);
15780 	struct bnxt_napi *bnapi;
15781 	u64 *sw;
15782 
15783 	if (!bp->tx_ring)
15784 		return;
15785 
15786 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15787 	sw = bnapi->cp_ring.stats.sw_stats;
15788 
15789 	stats->packets = 0;
15790 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15791 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15792 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15793 
15794 	stats->bytes = 0;
15795 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15796 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15797 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15798 }
15799 
15800 static void bnxt_get_base_stats(struct net_device *dev,
15801 				struct netdev_queue_stats_rx *rx,
15802 				struct netdev_queue_stats_tx *tx)
15803 {
15804 	struct bnxt *bp = netdev_priv(dev);
15805 
15806 	rx->packets = bp->net_stats_prev.rx_packets;
15807 	rx->bytes = bp->net_stats_prev.rx_bytes;
15808 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15809 
15810 	tx->packets = bp->net_stats_prev.tx_packets;
15811 	tx->bytes = bp->net_stats_prev.tx_bytes;
15812 }
15813 
15814 static const struct netdev_stat_ops bnxt_stat_ops = {
15815 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15816 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15817 	.get_base_stats		= bnxt_get_base_stats,
15818 };
15819 
15820 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15821 {
15822 	struct bnxt_rx_ring_info *rxr, *clone;
15823 	struct bnxt *bp = netdev_priv(dev);
15824 	struct bnxt_ring_struct *ring;
15825 	int rc;
15826 
15827 	if (!bp->rx_ring)
15828 		return -ENETDOWN;
15829 
15830 	rxr = &bp->rx_ring[idx];
15831 	clone = qmem;
15832 	memcpy(clone, rxr, sizeof(*rxr));
15833 	bnxt_init_rx_ring_struct(bp, clone);
15834 	bnxt_reset_rx_ring_struct(bp, clone);
15835 
15836 	clone->rx_prod = 0;
15837 	clone->rx_agg_prod = 0;
15838 	clone->rx_sw_agg_prod = 0;
15839 	clone->rx_next_cons = 0;
15840 	clone->need_head_pool = false;
15841 
15842 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15843 	if (rc)
15844 		return rc;
15845 
15846 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15847 	if (rc < 0)
15848 		goto err_page_pool_destroy;
15849 
15850 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15851 					MEM_TYPE_PAGE_POOL,
15852 					clone->page_pool);
15853 	if (rc)
15854 		goto err_rxq_info_unreg;
15855 
15856 	ring = &clone->rx_ring_struct;
15857 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15858 	if (rc)
15859 		goto err_free_rx_ring;
15860 
15861 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15862 		ring = &clone->rx_agg_ring_struct;
15863 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15864 		if (rc)
15865 			goto err_free_rx_agg_ring;
15866 
15867 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15868 		if (rc)
15869 			goto err_free_rx_agg_ring;
15870 	}
15871 
15872 	if (bp->flags & BNXT_FLAG_TPA) {
15873 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15874 		if (rc)
15875 			goto err_free_tpa_info;
15876 	}
15877 
15878 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15879 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15880 
15881 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15882 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15883 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
15884 	if (bp->flags & BNXT_FLAG_TPA)
15885 		bnxt_alloc_one_tpa_info_data(bp, clone);
15886 
15887 	return 0;
15888 
15889 err_free_tpa_info:
15890 	bnxt_free_one_tpa_info(bp, clone);
15891 err_free_rx_agg_ring:
15892 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15893 err_free_rx_ring:
15894 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15895 err_rxq_info_unreg:
15896 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15897 err_page_pool_destroy:
15898 	page_pool_destroy(clone->page_pool);
15899 	if (bnxt_separate_head_pool(clone))
15900 		page_pool_destroy(clone->head_pool);
15901 	clone->page_pool = NULL;
15902 	clone->head_pool = NULL;
15903 	return rc;
15904 }
15905 
15906 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15907 {
15908 	struct bnxt_rx_ring_info *rxr = qmem;
15909 	struct bnxt *bp = netdev_priv(dev);
15910 	struct bnxt_ring_struct *ring;
15911 
15912 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15913 	bnxt_free_one_tpa_info(bp, rxr);
15914 
15915 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15916 
15917 	page_pool_destroy(rxr->page_pool);
15918 	if (bnxt_separate_head_pool(rxr))
15919 		page_pool_destroy(rxr->head_pool);
15920 	rxr->page_pool = NULL;
15921 	rxr->head_pool = NULL;
15922 
15923 	ring = &rxr->rx_ring_struct;
15924 	bnxt_free_ring(bp, &ring->ring_mem);
15925 
15926 	ring = &rxr->rx_agg_ring_struct;
15927 	bnxt_free_ring(bp, &ring->ring_mem);
15928 
15929 	kfree(rxr->rx_agg_bmap);
15930 	rxr->rx_agg_bmap = NULL;
15931 }
15932 
15933 static void bnxt_copy_rx_ring(struct bnxt *bp,
15934 			      struct bnxt_rx_ring_info *dst,
15935 			      struct bnxt_rx_ring_info *src)
15936 {
15937 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15938 	struct bnxt_ring_struct *dst_ring, *src_ring;
15939 	int i;
15940 
15941 	dst_ring = &dst->rx_ring_struct;
15942 	dst_rmem = &dst_ring->ring_mem;
15943 	src_ring = &src->rx_ring_struct;
15944 	src_rmem = &src_ring->ring_mem;
15945 
15946 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15947 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15948 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15949 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15950 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15951 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15952 
15953 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15954 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15955 	*dst_rmem->vmem = *src_rmem->vmem;
15956 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15957 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15958 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15959 	}
15960 
15961 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15962 		return;
15963 
15964 	dst_ring = &dst->rx_agg_ring_struct;
15965 	dst_rmem = &dst_ring->ring_mem;
15966 	src_ring = &src->rx_agg_ring_struct;
15967 	src_rmem = &src_ring->ring_mem;
15968 
15969 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15970 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15971 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15972 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15973 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15974 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15975 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15976 
15977 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15978 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15979 	*dst_rmem->vmem = *src_rmem->vmem;
15980 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15981 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15982 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15983 	}
15984 
15985 	dst->rx_agg_bmap = src->rx_agg_bmap;
15986 }
15987 
15988 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15989 {
15990 	struct bnxt *bp = netdev_priv(dev);
15991 	struct bnxt_rx_ring_info *rxr, *clone;
15992 	struct bnxt_cp_ring_info *cpr;
15993 	struct bnxt_vnic_info *vnic;
15994 	struct bnxt_napi *bnapi;
15995 	int i, rc;
15996 	u16 mru;
15997 
15998 	rxr = &bp->rx_ring[idx];
15999 	clone = qmem;
16000 
16001 	rxr->rx_prod = clone->rx_prod;
16002 	rxr->rx_agg_prod = clone->rx_agg_prod;
16003 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
16004 	rxr->rx_next_cons = clone->rx_next_cons;
16005 	rxr->rx_tpa = clone->rx_tpa;
16006 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
16007 	rxr->page_pool = clone->page_pool;
16008 	rxr->head_pool = clone->head_pool;
16009 	rxr->xdp_rxq = clone->xdp_rxq;
16010 	rxr->need_head_pool = clone->need_head_pool;
16011 
16012 	bnxt_copy_rx_ring(bp, rxr, clone);
16013 
16014 	bnapi = rxr->bnapi;
16015 	cpr = &bnapi->cp_ring;
16016 
16017 	/* All rings have been reserved and previously allocated.
16018 	 * Reallocating with the same parameters should never fail.
16019 	 */
16020 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
16021 	if (rc)
16022 		goto err_reset;
16023 
16024 	if (bp->tph_mode) {
16025 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
16026 		if (rc)
16027 			goto err_reset;
16028 	}
16029 
16030 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
16031 	if (rc)
16032 		goto err_reset;
16033 
16034 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
16035 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16036 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
16037 
16038 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
16039 		rc = bnxt_tx_queue_start(bp, idx);
16040 		if (rc)
16041 			goto err_reset;
16042 	}
16043 
16044 	napi_enable_locked(&bnapi->napi);
16045 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16046 
16047 	mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
16048 	for (i = 0; i < bp->nr_vnics; i++) {
16049 		vnic = &bp->vnic_info[i];
16050 
16051 		rc = bnxt_set_vnic_mru_p5(bp, vnic, mru, idx);
16052 		if (rc)
16053 			return rc;
16054 	}
16055 	return bnxt_set_rss_ctx_vnic_mru(bp, mru, idx);
16056 
16057 err_reset:
16058 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
16059 		   rc);
16060 	napi_enable_locked(&bnapi->napi);
16061 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
16062 	netif_close(dev);
16063 	return rc;
16064 }
16065 
16066 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
16067 {
16068 	struct bnxt *bp = netdev_priv(dev);
16069 	struct bnxt_rx_ring_info *rxr;
16070 	struct bnxt_cp_ring_info *cpr;
16071 	struct bnxt_vnic_info *vnic;
16072 	struct bnxt_napi *bnapi;
16073 	int i;
16074 
16075 	for (i = 0; i < bp->nr_vnics; i++) {
16076 		vnic = &bp->vnic_info[i];
16077 
16078 		bnxt_set_vnic_mru_p5(bp, vnic, 0, idx);
16079 	}
16080 	bnxt_set_rss_ctx_vnic_mru(bp, 0, idx);
16081 	/* Make sure NAPI sees that the VNIC is disabled */
16082 	synchronize_net();
16083 	rxr = &bp->rx_ring[idx];
16084 	bnapi = rxr->bnapi;
16085 	cpr = &bnapi->cp_ring;
16086 	cancel_work_sync(&cpr->dim.work);
16087 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16088 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16089 	page_pool_disable_direct_recycling(rxr->page_pool);
16090 	if (bnxt_separate_head_pool(rxr))
16091 		page_pool_disable_direct_recycling(rxr->head_pool);
16092 
16093 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16094 		bnxt_tx_queue_stop(bp, idx);
16095 
16096 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16097 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16098 	 * after seeing the completion.
16099 	 */
16100 	napi_disable_locked(&bnapi->napi);
16101 
16102 	if (bp->tph_mode) {
16103 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16104 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16105 	}
16106 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16107 
16108 	memcpy(qmem, rxr, sizeof(*rxr));
16109 	bnxt_init_rx_ring_struct(bp, qmem);
16110 
16111 	return 0;
16112 }
16113 
16114 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16115 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16116 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16117 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16118 	.ndo_queue_start	= bnxt_queue_start,
16119 	.ndo_queue_stop		= bnxt_queue_stop,
16120 };
16121 
16122 static void bnxt_remove_one(struct pci_dev *pdev)
16123 {
16124 	struct net_device *dev = pci_get_drvdata(pdev);
16125 	struct bnxt *bp = netdev_priv(dev);
16126 
16127 	if (BNXT_PF(bp))
16128 		bnxt_sriov_disable(bp);
16129 
16130 	bnxt_rdma_aux_device_del(bp);
16131 
16132 	unregister_netdev(dev);
16133 	bnxt_ptp_clear(bp);
16134 
16135 	bnxt_rdma_aux_device_uninit(bp);
16136 
16137 	bnxt_free_l2_filters(bp, true);
16138 	bnxt_free_ntp_fltrs(bp, true);
16139 	WARN_ON(bp->num_rss_ctx);
16140 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16141 	/* Flush any pending tasks */
16142 	cancel_work_sync(&bp->sp_task);
16143 	cancel_delayed_work_sync(&bp->fw_reset_task);
16144 	bp->sp_event = 0;
16145 
16146 	bnxt_dl_fw_reporters_destroy(bp);
16147 	bnxt_dl_unregister(bp);
16148 	bnxt_shutdown_tc(bp);
16149 
16150 	bnxt_clear_int_mode(bp);
16151 	bnxt_hwrm_func_drv_unrgtr(bp);
16152 	bnxt_free_hwrm_resources(bp);
16153 	bnxt_hwmon_uninit(bp);
16154 	bnxt_ethtool_free(bp);
16155 	bnxt_dcb_free(bp);
16156 	kfree(bp->ptp_cfg);
16157 	bp->ptp_cfg = NULL;
16158 	kfree(bp->fw_health);
16159 	bp->fw_health = NULL;
16160 	bnxt_cleanup_pci(bp);
16161 	bnxt_free_ctx_mem(bp, true);
16162 	bnxt_free_crash_dump_mem(bp);
16163 	kfree(bp->rss_indir_tbl);
16164 	bp->rss_indir_tbl = NULL;
16165 	bnxt_free_port_stats(bp);
16166 	free_netdev(dev);
16167 }
16168 
16169 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16170 {
16171 	int rc = 0;
16172 	struct bnxt_link_info *link_info = &bp->link_info;
16173 
16174 	bp->phy_flags = 0;
16175 	rc = bnxt_hwrm_phy_qcaps(bp);
16176 	if (rc) {
16177 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16178 			   rc);
16179 		return rc;
16180 	}
16181 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16182 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16183 	else
16184 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16185 
16186 	bp->mac_flags = 0;
16187 	bnxt_hwrm_mac_qcaps(bp);
16188 
16189 	if (!fw_dflt)
16190 		return 0;
16191 
16192 	mutex_lock(&bp->link_lock);
16193 	rc = bnxt_update_link(bp, false);
16194 	if (rc) {
16195 		mutex_unlock(&bp->link_lock);
16196 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16197 			   rc);
16198 		return rc;
16199 	}
16200 
16201 	/* Older firmware does not have supported_auto_speeds, so assume
16202 	 * that all supported speeds can be autonegotiated.
16203 	 */
16204 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16205 		link_info->support_auto_speeds = link_info->support_speeds;
16206 
16207 	bnxt_init_ethtool_link_settings(bp);
16208 	mutex_unlock(&bp->link_lock);
16209 	return 0;
16210 }
16211 
16212 static int bnxt_get_max_irq(struct pci_dev *pdev)
16213 {
16214 	u16 ctrl;
16215 
16216 	if (!pdev->msix_cap)
16217 		return 1;
16218 
16219 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16220 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16221 }
16222 
16223 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16224 				int *max_cp)
16225 {
16226 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16227 	int max_ring_grps = 0, max_irq;
16228 
16229 	*max_tx = hw_resc->max_tx_rings;
16230 	*max_rx = hw_resc->max_rx_rings;
16231 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16232 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16233 			bnxt_get_ulp_msix_num_in_use(bp),
16234 			hw_resc->max_stat_ctxs -
16235 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16236 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16237 		*max_cp = min_t(int, *max_cp, max_irq);
16238 	max_ring_grps = hw_resc->max_hw_ring_grps;
16239 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16240 		*max_cp -= 1;
16241 		*max_rx -= 2;
16242 	}
16243 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16244 		*max_rx >>= 1;
16245 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16246 		int rc;
16247 
16248 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16249 		if (rc) {
16250 			*max_rx = 0;
16251 			*max_tx = 0;
16252 		}
16253 		/* On P5 chips, max_cp output param should be available NQs */
16254 		*max_cp = max_irq;
16255 	}
16256 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16257 }
16258 
16259 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16260 {
16261 	int rx, tx, cp;
16262 
16263 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16264 	*max_rx = rx;
16265 	*max_tx = tx;
16266 	if (!rx || !tx || !cp)
16267 		return -ENOMEM;
16268 
16269 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16270 }
16271 
16272 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16273 			       bool shared)
16274 {
16275 	int rc;
16276 
16277 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16278 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16279 		/* Not enough rings, try disabling agg rings. */
16280 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16281 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16282 		if (rc) {
16283 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16284 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16285 			return rc;
16286 		}
16287 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16288 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16289 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16290 		bnxt_set_ring_params(bp);
16291 	}
16292 
16293 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16294 		int max_cp, max_stat, max_irq;
16295 
16296 		/* Reserve minimum resources for RoCE */
16297 		max_cp = bnxt_get_max_func_cp_rings(bp);
16298 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16299 		max_irq = bnxt_get_max_func_irqs(bp);
16300 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16301 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16302 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16303 			return 0;
16304 
16305 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16306 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16307 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16308 		max_cp = min_t(int, max_cp, max_irq);
16309 		max_cp = min_t(int, max_cp, max_stat);
16310 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16311 		if (rc)
16312 			rc = 0;
16313 	}
16314 	return rc;
16315 }
16316 
16317 /* In initial default shared ring setting, each shared ring must have a
16318  * RX/TX ring pair.
16319  */
16320 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16321 {
16322 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16323 	bp->rx_nr_rings = bp->cp_nr_rings;
16324 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16325 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16326 }
16327 
16328 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16329 {
16330 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16331 	int avail_msix;
16332 
16333 	if (!bnxt_can_reserve_rings(bp))
16334 		return 0;
16335 
16336 	if (sh)
16337 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16338 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16339 	/* Reduce default rings on multi-port cards so that total default
16340 	 * rings do not exceed CPU count.
16341 	 */
16342 	if (bp->port_count > 1) {
16343 		int max_rings =
16344 			max_t(int, num_online_cpus() / bp->port_count, 1);
16345 
16346 		dflt_rings = min_t(int, dflt_rings, max_rings);
16347 	}
16348 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16349 	if (rc)
16350 		return rc;
16351 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16352 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16353 	if (sh)
16354 		bnxt_trim_dflt_sh_rings(bp);
16355 	else
16356 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16357 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16358 
16359 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16360 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16361 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16362 
16363 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16364 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16365 	}
16366 
16367 	rc = __bnxt_reserve_rings(bp);
16368 	if (rc && rc != -ENODEV)
16369 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16370 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16371 	if (sh)
16372 		bnxt_trim_dflt_sh_rings(bp);
16373 
16374 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16375 	if (bnxt_need_reserve_rings(bp)) {
16376 		rc = __bnxt_reserve_rings(bp);
16377 		if (rc && rc != -ENODEV)
16378 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16379 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16380 	}
16381 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16382 		bp->rx_nr_rings++;
16383 		bp->cp_nr_rings++;
16384 	}
16385 	if (rc) {
16386 		bp->tx_nr_rings = 0;
16387 		bp->rx_nr_rings = 0;
16388 	}
16389 	return rc;
16390 }
16391 
16392 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16393 {
16394 	int rc;
16395 
16396 	if (bp->tx_nr_rings)
16397 		return 0;
16398 
16399 	bnxt_ulp_irq_stop(bp);
16400 	bnxt_clear_int_mode(bp);
16401 	rc = bnxt_set_dflt_rings(bp, true);
16402 	if (rc) {
16403 		if (BNXT_VF(bp) && rc == -ENODEV)
16404 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16405 		else
16406 			netdev_err(bp->dev, "Not enough rings available.\n");
16407 		goto init_dflt_ring_err;
16408 	}
16409 	rc = bnxt_init_int_mode(bp);
16410 	if (rc)
16411 		goto init_dflt_ring_err;
16412 
16413 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16414 
16415 	bnxt_set_dflt_rfs(bp);
16416 
16417 init_dflt_ring_err:
16418 	bnxt_ulp_irq_restart(bp, rc);
16419 	return rc;
16420 }
16421 
16422 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16423 {
16424 	int rc;
16425 
16426 	netdev_ops_assert_locked(bp->dev);
16427 	bnxt_hwrm_func_qcaps(bp);
16428 
16429 	if (netif_running(bp->dev))
16430 		__bnxt_close_nic(bp, true, false);
16431 
16432 	bnxt_ulp_irq_stop(bp);
16433 	bnxt_clear_int_mode(bp);
16434 	rc = bnxt_init_int_mode(bp);
16435 	bnxt_ulp_irq_restart(bp, rc);
16436 
16437 	if (netif_running(bp->dev)) {
16438 		if (rc)
16439 			netif_close(bp->dev);
16440 		else
16441 			rc = bnxt_open_nic(bp, true, false);
16442 	}
16443 
16444 	return rc;
16445 }
16446 
16447 static int bnxt_init_mac_addr(struct bnxt *bp)
16448 {
16449 	int rc = 0;
16450 
16451 	if (BNXT_PF(bp)) {
16452 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16453 	} else {
16454 #ifdef CONFIG_BNXT_SRIOV
16455 		struct bnxt_vf_info *vf = &bp->vf;
16456 		bool strict_approval = true;
16457 
16458 		if (is_valid_ether_addr(vf->mac_addr)) {
16459 			/* overwrite netdev dev_addr with admin VF MAC */
16460 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16461 			/* Older PF driver or firmware may not approve this
16462 			 * correctly.
16463 			 */
16464 			strict_approval = false;
16465 		} else {
16466 			eth_hw_addr_random(bp->dev);
16467 		}
16468 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16469 #endif
16470 	}
16471 	return rc;
16472 }
16473 
16474 static void bnxt_vpd_read_info(struct bnxt *bp)
16475 {
16476 	struct pci_dev *pdev = bp->pdev;
16477 	unsigned int vpd_size, kw_len;
16478 	int pos, size;
16479 	u8 *vpd_data;
16480 
16481 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16482 	if (IS_ERR(vpd_data)) {
16483 		pci_warn(pdev, "Unable to read VPD\n");
16484 		return;
16485 	}
16486 
16487 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16488 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16489 	if (pos < 0)
16490 		goto read_sn;
16491 
16492 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16493 	memcpy(bp->board_partno, &vpd_data[pos], size);
16494 
16495 read_sn:
16496 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16497 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16498 					   &kw_len);
16499 	if (pos < 0)
16500 		goto exit;
16501 
16502 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16503 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16504 exit:
16505 	kfree(vpd_data);
16506 }
16507 
16508 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16509 {
16510 	struct pci_dev *pdev = bp->pdev;
16511 	u64 qword;
16512 
16513 	qword = pci_get_dsn(pdev);
16514 	if (!qword) {
16515 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16516 		return -EOPNOTSUPP;
16517 	}
16518 
16519 	put_unaligned_le64(qword, dsn);
16520 
16521 	bp->flags |= BNXT_FLAG_DSN_VALID;
16522 	return 0;
16523 }
16524 
16525 static int bnxt_map_db_bar(struct bnxt *bp)
16526 {
16527 	if (!bp->db_size)
16528 		return -ENODEV;
16529 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16530 	if (!bp->bar1)
16531 		return -ENOMEM;
16532 	return 0;
16533 }
16534 
16535 void bnxt_print_device_info(struct bnxt *bp)
16536 {
16537 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16538 		    board_info[bp->board_idx].name,
16539 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16540 
16541 	pcie_print_link_status(bp->pdev);
16542 }
16543 
16544 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16545 {
16546 	struct bnxt_hw_resc *hw_resc;
16547 	struct net_device *dev;
16548 	struct bnxt *bp;
16549 	int rc, max_irqs;
16550 
16551 	if (pci_is_bridge(pdev))
16552 		return -ENODEV;
16553 
16554 	if (!pdev->msix_cap) {
16555 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16556 		return -ENODEV;
16557 	}
16558 
16559 	/* Clear any pending DMA transactions from crash kernel
16560 	 * while loading driver in capture kernel.
16561 	 */
16562 	if (is_kdump_kernel()) {
16563 		pci_clear_master(pdev);
16564 		pcie_flr(pdev);
16565 	}
16566 
16567 	max_irqs = bnxt_get_max_irq(pdev);
16568 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16569 				 max_irqs);
16570 	if (!dev)
16571 		return -ENOMEM;
16572 
16573 	bp = netdev_priv(dev);
16574 	bp->board_idx = ent->driver_data;
16575 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16576 	bnxt_set_max_func_irqs(bp, max_irqs);
16577 
16578 	if (bnxt_vf_pciid(bp->board_idx))
16579 		bp->flags |= BNXT_FLAG_VF;
16580 
16581 	/* No devlink port registration in case of a VF */
16582 	if (BNXT_PF(bp))
16583 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16584 
16585 	rc = bnxt_init_board(pdev, dev);
16586 	if (rc < 0)
16587 		goto init_err_free;
16588 
16589 	dev->netdev_ops = &bnxt_netdev_ops;
16590 	dev->stat_ops = &bnxt_stat_ops;
16591 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16592 	dev->ethtool_ops = &bnxt_ethtool_ops;
16593 	pci_set_drvdata(pdev, dev);
16594 
16595 	rc = bnxt_alloc_hwrm_resources(bp);
16596 	if (rc)
16597 		goto init_err_pci_clean;
16598 
16599 	mutex_init(&bp->hwrm_cmd_lock);
16600 	mutex_init(&bp->link_lock);
16601 
16602 	rc = bnxt_fw_init_one_p1(bp);
16603 	if (rc)
16604 		goto init_err_pci_clean;
16605 
16606 	if (BNXT_PF(bp))
16607 		bnxt_vpd_read_info(bp);
16608 
16609 	if (BNXT_CHIP_P5_PLUS(bp)) {
16610 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16611 		if (BNXT_CHIP_P7(bp))
16612 			bp->flags |= BNXT_FLAG_CHIP_P7;
16613 	}
16614 
16615 	rc = bnxt_alloc_rss_indir_tbl(bp);
16616 	if (rc)
16617 		goto init_err_pci_clean;
16618 
16619 	rc = bnxt_fw_init_one_p2(bp);
16620 	if (rc)
16621 		goto init_err_pci_clean;
16622 
16623 	rc = bnxt_map_db_bar(bp);
16624 	if (rc) {
16625 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16626 			rc);
16627 		goto init_err_pci_clean;
16628 	}
16629 
16630 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16631 			   NETIF_F_TSO | NETIF_F_TSO6 |
16632 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16633 			   NETIF_F_GSO_IPXIP4 |
16634 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16635 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16636 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16637 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16638 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16639 
16640 	if (BNXT_SUPPORTS_TPA(bp))
16641 		dev->hw_features |= NETIF_F_LRO;
16642 
16643 	dev->hw_enc_features =
16644 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16645 			NETIF_F_TSO | NETIF_F_TSO6 |
16646 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16647 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16648 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16649 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16650 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16651 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16652 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16653 	else
16654 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16655 
16656 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16657 				    NETIF_F_GSO_GRE_CSUM;
16658 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16659 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16660 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16661 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16662 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16663 	if (BNXT_SUPPORTS_TPA(bp))
16664 		dev->hw_features |= NETIF_F_GRO_HW;
16665 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16666 	if (dev->features & NETIF_F_GRO_HW)
16667 		dev->features &= ~NETIF_F_LRO;
16668 	dev->priv_flags |= IFF_UNICAST_FLT;
16669 
16670 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16671 	if (bp->tso_max_segs)
16672 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16673 
16674 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16675 			    NETDEV_XDP_ACT_RX_SG;
16676 
16677 #ifdef CONFIG_BNXT_SRIOV
16678 	init_waitqueue_head(&bp->sriov_cfg_wait);
16679 #endif
16680 	if (BNXT_SUPPORTS_TPA(bp)) {
16681 		bp->gro_func = bnxt_gro_func_5730x;
16682 		if (BNXT_CHIP_P4(bp))
16683 			bp->gro_func = bnxt_gro_func_5731x;
16684 		else if (BNXT_CHIP_P5_PLUS(bp))
16685 			bp->gro_func = bnxt_gro_func_5750x;
16686 	}
16687 	if (!BNXT_CHIP_P4_PLUS(bp))
16688 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16689 
16690 	rc = bnxt_init_mac_addr(bp);
16691 	if (rc) {
16692 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16693 		rc = -EADDRNOTAVAIL;
16694 		goto init_err_pci_clean;
16695 	}
16696 
16697 	if (BNXT_PF(bp)) {
16698 		/* Read the adapter's DSN to use as the eswitch switch_id */
16699 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16700 	}
16701 
16702 	/* MTU range: 60 - FW defined max */
16703 	dev->min_mtu = ETH_ZLEN;
16704 	dev->max_mtu = bp->max_mtu;
16705 
16706 	rc = bnxt_probe_phy(bp, true);
16707 	if (rc)
16708 		goto init_err_pci_clean;
16709 
16710 	hw_resc = &bp->hw_resc;
16711 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16712 		       BNXT_L2_FLTR_MAX_FLTR;
16713 	/* Older firmware may not report these filters properly */
16714 	if (bp->max_fltr < BNXT_MAX_FLTR)
16715 		bp->max_fltr = BNXT_MAX_FLTR;
16716 	bnxt_init_l2_fltr_tbl(bp);
16717 	__bnxt_set_rx_skb_mode(bp, false);
16718 	bnxt_set_tpa_flags(bp);
16719 	bnxt_init_ring_params(bp);
16720 	bnxt_set_ring_params(bp);
16721 	bnxt_rdma_aux_device_init(bp);
16722 	rc = bnxt_set_dflt_rings(bp, true);
16723 	if (rc) {
16724 		if (BNXT_VF(bp) && rc == -ENODEV) {
16725 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16726 		} else {
16727 			netdev_err(bp->dev, "Not enough rings available.\n");
16728 			rc = -ENOMEM;
16729 		}
16730 		goto init_err_pci_clean;
16731 	}
16732 
16733 	bnxt_fw_init_one_p3(bp);
16734 
16735 	bnxt_init_dflt_coal(bp);
16736 
16737 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16738 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16739 
16740 	rc = bnxt_init_int_mode(bp);
16741 	if (rc)
16742 		goto init_err_pci_clean;
16743 
16744 	/* No TC has been set yet and rings may have been trimmed due to
16745 	 * limited MSIX, so we re-initialize the TX rings per TC.
16746 	 */
16747 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16748 
16749 	if (BNXT_PF(bp)) {
16750 		if (!bnxt_pf_wq) {
16751 			bnxt_pf_wq =
16752 				create_singlethread_workqueue("bnxt_pf_wq");
16753 			if (!bnxt_pf_wq) {
16754 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16755 				rc = -ENOMEM;
16756 				goto init_err_pci_clean;
16757 			}
16758 		}
16759 		rc = bnxt_init_tc(bp);
16760 		if (rc)
16761 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16762 				   rc);
16763 	}
16764 
16765 	bnxt_inv_fw_health_reg(bp);
16766 	rc = bnxt_dl_register(bp);
16767 	if (rc)
16768 		goto init_err_dl;
16769 
16770 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16771 
16772 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16773 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16774 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16775 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16776 	dev->request_ops_lock = true;
16777 
16778 	rc = register_netdev(dev);
16779 	if (rc)
16780 		goto init_err_cleanup;
16781 
16782 	bnxt_dl_fw_reporters_create(bp);
16783 
16784 	bnxt_rdma_aux_device_add(bp);
16785 
16786 	bnxt_print_device_info(bp);
16787 
16788 	pci_save_state(pdev);
16789 
16790 	return 0;
16791 init_err_cleanup:
16792 	bnxt_rdma_aux_device_uninit(bp);
16793 	bnxt_dl_unregister(bp);
16794 init_err_dl:
16795 	bnxt_shutdown_tc(bp);
16796 	bnxt_clear_int_mode(bp);
16797 
16798 init_err_pci_clean:
16799 	bnxt_hwrm_func_drv_unrgtr(bp);
16800 	bnxt_free_hwrm_resources(bp);
16801 	bnxt_hwmon_uninit(bp);
16802 	bnxt_ethtool_free(bp);
16803 	bnxt_ptp_clear(bp);
16804 	kfree(bp->ptp_cfg);
16805 	bp->ptp_cfg = NULL;
16806 	kfree(bp->fw_health);
16807 	bp->fw_health = NULL;
16808 	bnxt_cleanup_pci(bp);
16809 	bnxt_free_ctx_mem(bp, true);
16810 	bnxt_free_crash_dump_mem(bp);
16811 	kfree(bp->rss_indir_tbl);
16812 	bp->rss_indir_tbl = NULL;
16813 
16814 init_err_free:
16815 	free_netdev(dev);
16816 	return rc;
16817 }
16818 
16819 static void bnxt_shutdown(struct pci_dev *pdev)
16820 {
16821 	struct net_device *dev = pci_get_drvdata(pdev);
16822 	struct bnxt *bp;
16823 
16824 	if (!dev)
16825 		return;
16826 
16827 	rtnl_lock();
16828 	netdev_lock(dev);
16829 	bp = netdev_priv(dev);
16830 	if (!bp)
16831 		goto shutdown_exit;
16832 
16833 	if (netif_running(dev))
16834 		netif_close(dev);
16835 
16836 	bnxt_ptp_clear(bp);
16837 	bnxt_clear_int_mode(bp);
16838 	pci_disable_device(pdev);
16839 
16840 	if (system_state == SYSTEM_POWER_OFF) {
16841 		pci_wake_from_d3(pdev, bp->wol);
16842 		pci_set_power_state(pdev, PCI_D3hot);
16843 	}
16844 
16845 shutdown_exit:
16846 	netdev_unlock(dev);
16847 	rtnl_unlock();
16848 }
16849 
16850 #ifdef CONFIG_PM_SLEEP
16851 static int bnxt_suspend(struct device *device)
16852 {
16853 	struct net_device *dev = dev_get_drvdata(device);
16854 	struct bnxt *bp = netdev_priv(dev);
16855 	int rc = 0;
16856 
16857 	bnxt_ulp_stop(bp);
16858 
16859 	netdev_lock(dev);
16860 	if (netif_running(dev)) {
16861 		netif_device_detach(dev);
16862 		rc = bnxt_close(dev);
16863 	}
16864 	bnxt_hwrm_func_drv_unrgtr(bp);
16865 	bnxt_ptp_clear(bp);
16866 	pci_disable_device(bp->pdev);
16867 	bnxt_free_ctx_mem(bp, false);
16868 	netdev_unlock(dev);
16869 	return rc;
16870 }
16871 
16872 static int bnxt_resume(struct device *device)
16873 {
16874 	struct net_device *dev = dev_get_drvdata(device);
16875 	struct bnxt *bp = netdev_priv(dev);
16876 	int rc = 0;
16877 
16878 	rtnl_lock();
16879 	netdev_lock(dev);
16880 	rc = pci_enable_device(bp->pdev);
16881 	if (rc) {
16882 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16883 			   rc);
16884 		goto resume_exit;
16885 	}
16886 	pci_set_master(bp->pdev);
16887 	if (bnxt_hwrm_ver_get(bp)) {
16888 		rc = -ENODEV;
16889 		goto resume_exit;
16890 	}
16891 	rc = bnxt_hwrm_func_reset(bp);
16892 	if (rc) {
16893 		rc = -EBUSY;
16894 		goto resume_exit;
16895 	}
16896 
16897 	rc = bnxt_hwrm_func_qcaps(bp);
16898 	if (rc)
16899 		goto resume_exit;
16900 
16901 	bnxt_clear_reservations(bp, true);
16902 
16903 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16904 		rc = -ENODEV;
16905 		goto resume_exit;
16906 	}
16907 	if (bp->fw_crash_mem)
16908 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16909 
16910 	if (bnxt_ptp_init(bp)) {
16911 		kfree(bp->ptp_cfg);
16912 		bp->ptp_cfg = NULL;
16913 	}
16914 	bnxt_get_wol_settings(bp);
16915 	if (netif_running(dev)) {
16916 		rc = bnxt_open(dev);
16917 		if (!rc)
16918 			netif_device_attach(dev);
16919 	}
16920 
16921 resume_exit:
16922 	netdev_unlock(bp->dev);
16923 	rtnl_unlock();
16924 	bnxt_ulp_start(bp, rc);
16925 	if (!rc)
16926 		bnxt_reenable_sriov(bp);
16927 	return rc;
16928 }
16929 
16930 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16931 #define BNXT_PM_OPS (&bnxt_pm_ops)
16932 
16933 #else
16934 
16935 #define BNXT_PM_OPS NULL
16936 
16937 #endif /* CONFIG_PM_SLEEP */
16938 
16939 /**
16940  * bnxt_io_error_detected - called when PCI error is detected
16941  * @pdev: Pointer to PCI device
16942  * @state: The current pci connection state
16943  *
16944  * This function is called after a PCI bus error affecting
16945  * this device has been detected.
16946  */
16947 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16948 					       pci_channel_state_t state)
16949 {
16950 	struct net_device *netdev = pci_get_drvdata(pdev);
16951 	struct bnxt *bp = netdev_priv(netdev);
16952 	bool abort = false;
16953 
16954 	netdev_info(netdev, "PCI I/O error detected\n");
16955 
16956 	bnxt_ulp_stop(bp);
16957 
16958 	netdev_lock(netdev);
16959 	netif_device_detach(netdev);
16960 
16961 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16962 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16963 		abort = true;
16964 	}
16965 
16966 	if (abort || state == pci_channel_io_perm_failure) {
16967 		netdev_unlock(netdev);
16968 		return PCI_ERS_RESULT_DISCONNECT;
16969 	}
16970 
16971 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16972 	 * so we disable bus master to prevent any potential bad DMAs before
16973 	 * freeing kernel memory.
16974 	 */
16975 	if (state == pci_channel_io_frozen) {
16976 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16977 		bnxt_fw_fatal_close(bp);
16978 	}
16979 
16980 	if (netif_running(netdev))
16981 		__bnxt_close_nic(bp, true, true);
16982 
16983 	if (pci_is_enabled(pdev))
16984 		pci_disable_device(pdev);
16985 	bnxt_free_ctx_mem(bp, false);
16986 	netdev_unlock(netdev);
16987 
16988 	/* Request a slot slot reset. */
16989 	return PCI_ERS_RESULT_NEED_RESET;
16990 }
16991 
16992 /**
16993  * bnxt_io_slot_reset - called after the pci bus has been reset.
16994  * @pdev: Pointer to PCI device
16995  *
16996  * Restart the card from scratch, as if from a cold-boot.
16997  * At this point, the card has experienced a hard reset,
16998  * followed by fixups by BIOS, and has its config space
16999  * set up identically to what it was at cold boot.
17000  */
17001 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
17002 {
17003 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
17004 	struct net_device *netdev = pci_get_drvdata(pdev);
17005 	struct bnxt *bp = netdev_priv(netdev);
17006 	int retry = 0;
17007 	int err = 0;
17008 	int off;
17009 
17010 	netdev_info(bp->dev, "PCI Slot Reset\n");
17011 
17012 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
17013 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
17014 		msleep(900);
17015 
17016 	netdev_lock(netdev);
17017 
17018 	if (pci_enable_device(pdev)) {
17019 		dev_err(&pdev->dev,
17020 			"Cannot re-enable PCI device after reset.\n");
17021 	} else {
17022 		pci_set_master(pdev);
17023 		/* Upon fatal error, our device internal logic that latches to
17024 		 * BAR value is getting reset and will restore only upon
17025 		 * rewriting the BARs.
17026 		 *
17027 		 * As pci_restore_state() does not re-write the BARs if the
17028 		 * value is same as saved value earlier, driver needs to
17029 		 * write the BARs to 0 to force restore, in case of fatal error.
17030 		 */
17031 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
17032 				       &bp->state)) {
17033 			for (off = PCI_BASE_ADDRESS_0;
17034 			     off <= PCI_BASE_ADDRESS_5; off += 4)
17035 				pci_write_config_dword(bp->pdev, off, 0);
17036 		}
17037 		pci_restore_state(pdev);
17038 		pci_save_state(pdev);
17039 
17040 		bnxt_inv_fw_health_reg(bp);
17041 		bnxt_try_map_fw_health_reg(bp);
17042 
17043 		/* In some PCIe AER scenarios, firmware may take up to
17044 		 * 10 seconds to become ready in the worst case.
17045 		 */
17046 		do {
17047 			err = bnxt_try_recover_fw(bp);
17048 			if (!err)
17049 				break;
17050 			retry++;
17051 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
17052 
17053 		if (err) {
17054 			dev_err(&pdev->dev, "Firmware not ready\n");
17055 			goto reset_exit;
17056 		}
17057 
17058 		err = bnxt_hwrm_func_reset(bp);
17059 		if (!err)
17060 			result = PCI_ERS_RESULT_RECOVERED;
17061 
17062 		/* IRQ will be initialized later in bnxt_io_resume */
17063 		bnxt_ulp_irq_stop(bp);
17064 		bnxt_clear_int_mode(bp);
17065 	}
17066 
17067 reset_exit:
17068 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
17069 	bnxt_clear_reservations(bp, true);
17070 	netdev_unlock(netdev);
17071 
17072 	return result;
17073 }
17074 
17075 /**
17076  * bnxt_io_resume - called when traffic can start flowing again.
17077  * @pdev: Pointer to PCI device
17078  *
17079  * This callback is called when the error recovery driver tells
17080  * us that its OK to resume normal operation.
17081  */
17082 static void bnxt_io_resume(struct pci_dev *pdev)
17083 {
17084 	struct net_device *netdev = pci_get_drvdata(pdev);
17085 	struct bnxt *bp = netdev_priv(netdev);
17086 	int err;
17087 
17088 	netdev_info(bp->dev, "PCI Slot Resume\n");
17089 	rtnl_lock();
17090 	netdev_lock(netdev);
17091 
17092 	err = bnxt_hwrm_func_qcaps(bp);
17093 	if (!err) {
17094 		if (netif_running(netdev)) {
17095 			err = bnxt_open(netdev);
17096 		} else {
17097 			err = bnxt_reserve_rings(bp, true);
17098 			if (!err)
17099 				err = bnxt_init_int_mode(bp);
17100 		}
17101 	}
17102 
17103 	if (!err)
17104 		netif_device_attach(netdev);
17105 
17106 	netdev_unlock(netdev);
17107 	rtnl_unlock();
17108 	bnxt_ulp_start(bp, err);
17109 	if (!err)
17110 		bnxt_reenable_sriov(bp);
17111 }
17112 
17113 static const struct pci_error_handlers bnxt_err_handler = {
17114 	.error_detected	= bnxt_io_error_detected,
17115 	.slot_reset	= bnxt_io_slot_reset,
17116 	.resume		= bnxt_io_resume
17117 };
17118 
17119 static struct pci_driver bnxt_pci_driver = {
17120 	.name		= DRV_MODULE_NAME,
17121 	.id_table	= bnxt_pci_tbl,
17122 	.probe		= bnxt_init_one,
17123 	.remove		= bnxt_remove_one,
17124 	.shutdown	= bnxt_shutdown,
17125 	.driver.pm	= BNXT_PM_OPS,
17126 	.err_handler	= &bnxt_err_handler,
17127 #if defined(CONFIG_BNXT_SRIOV)
17128 	.sriov_configure = bnxt_sriov_configure,
17129 #endif
17130 };
17131 
17132 static int __init bnxt_init(void)
17133 {
17134 	int err;
17135 
17136 	bnxt_debug_init();
17137 	err = pci_register_driver(&bnxt_pci_driver);
17138 	if (err) {
17139 		bnxt_debug_exit();
17140 		return err;
17141 	}
17142 
17143 	return 0;
17144 }
17145 
17146 static void __exit bnxt_exit(void)
17147 {
17148 	pci_unregister_driver(&bnxt_pci_driver);
17149 	if (bnxt_pf_wq)
17150 		destroy_workqueue(bnxt_pf_wq);
17151 	bnxt_debug_exit();
17152 }
17153 
17154 module_init(bnxt_init);
17155 module_exit(bnxt_exit);
17156