xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision fe259a1bb26ec78842c975d992331705b0c2c2e8)
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 	dev_kfree_skb(skb);
2048 	return NULL;
2049 }
2050 
2051 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2052 					   struct rx_cmp *rxcmp)
2053 {
2054 	u8 ext_op;
2055 
2056 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2057 	switch (ext_op) {
2058 	case EXT_OP_INNER_4:
2059 	case EXT_OP_OUTER_4:
2060 	case EXT_OP_INNFL_3:
2061 	case EXT_OP_OUTFL_3:
2062 		return PKT_HASH_TYPE_L4;
2063 	default:
2064 		return PKT_HASH_TYPE_L3;
2065 	}
2066 }
2067 
2068 /* returns the following:
2069  * 1       - 1 packet successfully received
2070  * 0       - successful TPA_START, packet not completed yet
2071  * -EBUSY  - completion ring does not have all the agg buffers yet
2072  * -ENOMEM - packet aborted due to out of memory
2073  * -EIO    - packet aborted due to hw error indicated in BD
2074  */
2075 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2076 		       u32 *raw_cons, u8 *event)
2077 {
2078 	struct bnxt_napi *bnapi = cpr->bnapi;
2079 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2080 	struct net_device *dev = bp->dev;
2081 	struct rx_cmp *rxcmp;
2082 	struct rx_cmp_ext *rxcmp1;
2083 	u32 tmp_raw_cons = *raw_cons;
2084 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2085 	struct skb_shared_info *sinfo;
2086 	struct bnxt_sw_rx_bd *rx_buf;
2087 	unsigned int len;
2088 	u8 *data_ptr, agg_bufs, cmp_type;
2089 	bool xdp_active = false;
2090 	dma_addr_t dma_addr;
2091 	struct sk_buff *skb;
2092 	struct xdp_buff xdp;
2093 	u32 flags, misc;
2094 	u32 cmpl_ts;
2095 	void *data;
2096 	int rc = 0;
2097 
2098 	rxcmp = (struct rx_cmp *)
2099 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2100 
2101 	cmp_type = RX_CMP_TYPE(rxcmp);
2102 
2103 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2104 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2105 		goto next_rx_no_prod_no_len;
2106 	}
2107 
2108 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2109 	cp_cons = RING_CMP(tmp_raw_cons);
2110 	rxcmp1 = (struct rx_cmp_ext *)
2111 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2112 
2113 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2114 		return -EBUSY;
2115 
2116 	/* The valid test of the entry must be done first before
2117 	 * reading any further.
2118 	 */
2119 	dma_rmb();
2120 	prod = rxr->rx_prod;
2121 
2122 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2123 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2124 		bnxt_tpa_start(bp, rxr, cmp_type,
2125 			       (struct rx_tpa_start_cmp *)rxcmp,
2126 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2127 
2128 		*event |= BNXT_RX_EVENT;
2129 		goto next_rx_no_prod_no_len;
2130 
2131 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2132 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2133 				   (struct rx_tpa_end_cmp *)rxcmp,
2134 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2135 
2136 		if (IS_ERR(skb))
2137 			return -EBUSY;
2138 
2139 		rc = -ENOMEM;
2140 		if (likely(skb)) {
2141 			bnxt_deliver_skb(bp, bnapi, skb);
2142 			rc = 1;
2143 		}
2144 		*event |= BNXT_RX_EVENT;
2145 		goto next_rx_no_prod_no_len;
2146 	}
2147 
2148 	cons = rxcmp->rx_cmp_opaque;
2149 	if (unlikely(cons != rxr->rx_next_cons)) {
2150 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2151 
2152 		/* 0xffff is forced error, don't print it */
2153 		if (rxr->rx_next_cons != 0xffff)
2154 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2155 				    cons, rxr->rx_next_cons);
2156 		bnxt_sched_reset_rxr(bp, rxr);
2157 		if (rc1)
2158 			return rc1;
2159 		goto next_rx_no_prod_no_len;
2160 	}
2161 	rx_buf = &rxr->rx_buf_ring[cons];
2162 	data = rx_buf->data;
2163 	data_ptr = rx_buf->data_ptr;
2164 	prefetch(data_ptr);
2165 
2166 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2167 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2168 
2169 	if (agg_bufs) {
2170 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2171 			return -EBUSY;
2172 
2173 		cp_cons = NEXT_CMP(cp_cons);
2174 		*event |= BNXT_AGG_EVENT;
2175 	}
2176 	*event |= BNXT_RX_EVENT;
2177 
2178 	rx_buf->data = NULL;
2179 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2180 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2181 
2182 		bnxt_reuse_rx_data(rxr, cons, data);
2183 		if (agg_bufs)
2184 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2185 					       false);
2186 
2187 		rc = -EIO;
2188 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2189 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2190 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2191 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2192 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2193 						 rx_err);
2194 				bnxt_sched_reset_rxr(bp, rxr);
2195 			}
2196 		}
2197 		goto next_rx_no_len;
2198 	}
2199 
2200 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2201 	len = flags >> RX_CMP_LEN_SHIFT;
2202 	dma_addr = rx_buf->mapping;
2203 
2204 	if (bnxt_xdp_attached(bp, rxr)) {
2205 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2206 		if (agg_bufs) {
2207 			u32 frag_len = bnxt_rx_agg_netmems_xdp(bp, cpr, &xdp,
2208 							       cp_cons,
2209 							       agg_bufs,
2210 							       false);
2211 			if (!frag_len)
2212 				goto oom_next_rx;
2213 
2214 		}
2215 		xdp_active = true;
2216 	}
2217 
2218 	if (xdp_active) {
2219 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2220 			rc = 1;
2221 			goto next_rx;
2222 		}
2223 		if (xdp_buff_has_frags(&xdp)) {
2224 			sinfo = xdp_get_shared_info_from_buff(&xdp);
2225 			agg_bufs = sinfo->nr_frags;
2226 		} else {
2227 			agg_bufs = 0;
2228 		}
2229 	}
2230 
2231 	if (len <= bp->rx_copybreak) {
2232 		if (!xdp_active)
2233 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2234 		else
2235 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2236 		bnxt_reuse_rx_data(rxr, cons, data);
2237 		if (!skb) {
2238 			if (agg_bufs) {
2239 				if (!xdp_active)
2240 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2241 							       agg_bufs, false);
2242 				else
2243 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2244 			}
2245 			goto oom_next_rx;
2246 		}
2247 	} else {
2248 		u32 payload;
2249 
2250 		if (rx_buf->data_ptr == data_ptr)
2251 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2252 		else
2253 			payload = 0;
2254 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2255 				      payload | len);
2256 		if (!skb)
2257 			goto oom_next_rx;
2258 	}
2259 
2260 	if (agg_bufs) {
2261 		if (!xdp_active) {
2262 			skb = bnxt_rx_agg_netmems_skb(bp, cpr, skb, cp_cons,
2263 						      agg_bufs, false);
2264 			if (!skb)
2265 				goto oom_next_rx;
2266 		} else {
2267 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs,
2268 						 rxr->page_pool, &xdp);
2269 			if (!skb) {
2270 				/* we should be able to free the old skb here */
2271 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2272 				goto oom_next_rx;
2273 			}
2274 		}
2275 	}
2276 
2277 	if (RX_CMP_HASH_VALID(rxcmp)) {
2278 		enum pkt_hash_types type;
2279 
2280 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2281 			type = bnxt_rss_ext_op(bp, rxcmp);
2282 		} else {
2283 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2284 
2285 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2286 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2287 				type = PKT_HASH_TYPE_L4;
2288 			else
2289 				type = PKT_HASH_TYPE_L3;
2290 		}
2291 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2292 	}
2293 
2294 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2295 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2296 	skb->protocol = eth_type_trans(skb, dev);
2297 
2298 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2299 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2300 		if (!skb)
2301 			goto next_rx;
2302 	}
2303 
2304 	skb_checksum_none_assert(skb);
2305 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2306 		if (dev->features & NETIF_F_RXCSUM) {
2307 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2308 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2309 		}
2310 	} else {
2311 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2312 			if (dev->features & NETIF_F_RXCSUM)
2313 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2314 		}
2315 	}
2316 
2317 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2318 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2319 			u64 ns, ts;
2320 
2321 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2322 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2323 
2324 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2325 				memset(skb_hwtstamps(skb), 0,
2326 				       sizeof(*skb_hwtstamps(skb)));
2327 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2328 			}
2329 		}
2330 	}
2331 	bnxt_deliver_skb(bp, bnapi, skb);
2332 	rc = 1;
2333 
2334 next_rx:
2335 	cpr->rx_packets += 1;
2336 	cpr->rx_bytes += len;
2337 
2338 next_rx_no_len:
2339 	rxr->rx_prod = NEXT_RX(prod);
2340 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2341 
2342 next_rx_no_prod_no_len:
2343 	*raw_cons = tmp_raw_cons;
2344 
2345 	return rc;
2346 
2347 oom_next_rx:
2348 	cpr->sw_stats->rx.rx_oom_discards += 1;
2349 	rc = -ENOMEM;
2350 	goto next_rx;
2351 }
2352 
2353 /* In netpoll mode, if we are using a combined completion ring, we need to
2354  * discard the rx packets and recycle the buffers.
2355  */
2356 static int bnxt_force_rx_discard(struct bnxt *bp,
2357 				 struct bnxt_cp_ring_info *cpr,
2358 				 u32 *raw_cons, u8 *event)
2359 {
2360 	u32 tmp_raw_cons = *raw_cons;
2361 	struct rx_cmp_ext *rxcmp1;
2362 	struct rx_cmp *rxcmp;
2363 	u16 cp_cons;
2364 	u8 cmp_type;
2365 	int rc;
2366 
2367 	cp_cons = RING_CMP(tmp_raw_cons);
2368 	rxcmp = (struct rx_cmp *)
2369 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2370 
2371 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2372 	cp_cons = RING_CMP(tmp_raw_cons);
2373 	rxcmp1 = (struct rx_cmp_ext *)
2374 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2375 
2376 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2377 		return -EBUSY;
2378 
2379 	/* The valid test of the entry must be done first before
2380 	 * reading any further.
2381 	 */
2382 	dma_rmb();
2383 	cmp_type = RX_CMP_TYPE(rxcmp);
2384 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2385 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2386 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2387 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2388 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2389 		struct rx_tpa_end_cmp_ext *tpa_end1;
2390 
2391 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2392 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2393 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2394 	}
2395 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2396 	if (rc && rc != -EBUSY)
2397 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2398 	return rc;
2399 }
2400 
2401 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2402 {
2403 	struct bnxt_fw_health *fw_health = bp->fw_health;
2404 	u32 reg = fw_health->regs[reg_idx];
2405 	u32 reg_type, reg_off, val = 0;
2406 
2407 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2408 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2409 	switch (reg_type) {
2410 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2411 		pci_read_config_dword(bp->pdev, reg_off, &val);
2412 		break;
2413 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2414 		reg_off = fw_health->mapped_regs[reg_idx];
2415 		fallthrough;
2416 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2417 		val = readl(bp->bar0 + reg_off);
2418 		break;
2419 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2420 		val = readl(bp->bar1 + reg_off);
2421 		break;
2422 	}
2423 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2424 		val &= fw_health->fw_reset_inprog_reg_mask;
2425 	return val;
2426 }
2427 
2428 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2429 {
2430 	int i;
2431 
2432 	for (i = 0; i < bp->rx_nr_rings; i++) {
2433 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2434 		struct bnxt_ring_grp_info *grp_info;
2435 
2436 		grp_info = &bp->grp_info[grp_idx];
2437 		if (grp_info->agg_fw_ring_id == ring_id)
2438 			return grp_idx;
2439 	}
2440 	return INVALID_HW_RING_ID;
2441 }
2442 
2443 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2444 {
2445 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2446 
2447 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2448 		return link_info->force_link_speed2;
2449 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2450 		return link_info->force_pam4_link_speed;
2451 	return link_info->force_link_speed;
2452 }
2453 
2454 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2455 {
2456 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2457 
2458 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2459 		link_info->req_link_speed = link_info->force_link_speed2;
2460 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2461 		switch (link_info->req_link_speed) {
2462 		case BNXT_LINK_SPEED_50GB_PAM4:
2463 		case BNXT_LINK_SPEED_100GB_PAM4:
2464 		case BNXT_LINK_SPEED_200GB_PAM4:
2465 		case BNXT_LINK_SPEED_400GB_PAM4:
2466 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2467 			break;
2468 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2469 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2470 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2471 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2472 			break;
2473 		default:
2474 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2475 		}
2476 		return;
2477 	}
2478 	link_info->req_link_speed = link_info->force_link_speed;
2479 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2480 	if (link_info->force_pam4_link_speed) {
2481 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2482 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2483 	}
2484 }
2485 
2486 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2487 {
2488 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2489 
2490 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2491 		link_info->advertising = link_info->auto_link_speeds2;
2492 		return;
2493 	}
2494 	link_info->advertising = link_info->auto_link_speeds;
2495 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2496 }
2497 
2498 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2499 {
2500 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2501 
2502 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2503 		if (link_info->req_link_speed != link_info->force_link_speed2)
2504 			return true;
2505 		return false;
2506 	}
2507 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2508 	    link_info->req_link_speed != link_info->force_link_speed)
2509 		return true;
2510 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2511 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2512 		return true;
2513 	return false;
2514 }
2515 
2516 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2517 {
2518 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2519 
2520 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2521 		if (link_info->advertising != link_info->auto_link_speeds2)
2522 			return true;
2523 		return false;
2524 	}
2525 	if (link_info->advertising != link_info->auto_link_speeds ||
2526 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2527 		return true;
2528 	return false;
2529 }
2530 
2531 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2532 {
2533 	u32 flags = bp->ctx->ctx_arr[type].flags;
2534 
2535 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2536 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2537 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2538 }
2539 
2540 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2541 {
2542 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2543 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2544 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2545 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2546 	struct bnxt_bs_trace_info *bs_trace;
2547 	int last_pg;
2548 
2549 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2550 		return;
2551 
2552 	mem_size = ctxm->max_entries * ctxm->entry_size;
2553 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2554 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2555 
2556 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2557 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2558 
2559 	rmem = &ctx_pg[0].ring_mem;
2560 	bs_trace = &bp->bs_trace[trace_type];
2561 	bs_trace->ctx_type = ctxm->type;
2562 	bs_trace->trace_type = trace_type;
2563 	if (pages > MAX_CTX_PAGES) {
2564 		int last_pg_dir = rmem->nr_pages - 1;
2565 
2566 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2567 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2568 	} else {
2569 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2570 	}
2571 	bs_trace->magic_byte += magic_byte_offset;
2572 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2573 }
2574 
2575 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2576 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2577 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2578 
2579 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2580 	(((data2) &							\
2581 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2582 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2583 
2584 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2585 	((data2) &							\
2586 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2587 
2588 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2589 	(((data2) &							\
2590 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2591 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2592 
2593 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2594 	((data1) &							\
2595 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2596 
2597 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2598 	(((data1) &							\
2599 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2600 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2601 
2602 /* Return true if the workqueue has to be scheduled */
2603 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2604 {
2605 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2606 
2607 	switch (err_type) {
2608 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2609 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2610 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2611 		break;
2612 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2613 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2614 		break;
2615 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2616 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2617 		break;
2618 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2619 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2620 		char *threshold_type;
2621 		bool notify = false;
2622 		char *dir_str;
2623 
2624 		switch (type) {
2625 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2626 			threshold_type = "warning";
2627 			break;
2628 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2629 			threshold_type = "critical";
2630 			break;
2631 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2632 			threshold_type = "fatal";
2633 			break;
2634 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2635 			threshold_type = "shutdown";
2636 			break;
2637 		default:
2638 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2639 			return false;
2640 		}
2641 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2642 			dir_str = "above";
2643 			notify = true;
2644 		} else {
2645 			dir_str = "below";
2646 		}
2647 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2648 			    dir_str, threshold_type);
2649 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2650 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2651 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2652 		if (notify) {
2653 			bp->thermal_threshold_type = type;
2654 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2655 			return true;
2656 		}
2657 		return false;
2658 	}
2659 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2660 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2661 		break;
2662 	default:
2663 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2664 			   err_type);
2665 		break;
2666 	}
2667 	return false;
2668 }
2669 
2670 #define BNXT_GET_EVENT_PORT(data)	\
2671 	((data) &			\
2672 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2673 
2674 #define BNXT_EVENT_RING_TYPE(data2)	\
2675 	((data2) &			\
2676 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2677 
2678 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2679 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2680 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2681 
2682 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2683 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2684 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2685 
2686 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2687 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2688 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2689 
2690 #define BNXT_PHC_BITS	48
2691 
2692 static int bnxt_async_event_process(struct bnxt *bp,
2693 				    struct hwrm_async_event_cmpl *cmpl)
2694 {
2695 	u16 event_id = le16_to_cpu(cmpl->event_id);
2696 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2697 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2698 
2699 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2700 		   event_id, data1, data2);
2701 
2702 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2703 	switch (event_id) {
2704 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2705 		struct bnxt_link_info *link_info = &bp->link_info;
2706 
2707 		if (BNXT_VF(bp))
2708 			goto async_event_process_exit;
2709 
2710 		/* print unsupported speed warning in forced speed mode only */
2711 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2712 		    (data1 & 0x20000)) {
2713 			u16 fw_speed = bnxt_get_force_speed(link_info);
2714 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2715 
2716 			if (speed != SPEED_UNKNOWN)
2717 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2718 					    speed);
2719 		}
2720 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2721 	}
2722 		fallthrough;
2723 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2724 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2725 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2726 		fallthrough;
2727 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2728 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2729 		break;
2730 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2731 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2732 		break;
2733 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2734 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2735 
2736 		if (BNXT_VF(bp))
2737 			break;
2738 
2739 		if (bp->pf.port_id != port_id)
2740 			break;
2741 
2742 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2743 		break;
2744 	}
2745 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2746 		if (BNXT_PF(bp))
2747 			goto async_event_process_exit;
2748 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2749 		break;
2750 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2751 		char *type_str = "Solicited";
2752 
2753 		if (!bp->fw_health)
2754 			goto async_event_process_exit;
2755 
2756 		bp->fw_reset_timestamp = jiffies;
2757 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2758 		if (!bp->fw_reset_min_dsecs)
2759 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2760 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2761 		if (!bp->fw_reset_max_dsecs)
2762 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2763 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2764 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2765 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2766 			type_str = "Fatal";
2767 			bp->fw_health->fatalities++;
2768 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2769 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2770 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2771 			type_str = "Non-fatal";
2772 			bp->fw_health->survivals++;
2773 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2774 		}
2775 		netif_warn(bp, hw, bp->dev,
2776 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2777 			   type_str, data1, data2,
2778 			   bp->fw_reset_min_dsecs * 100,
2779 			   bp->fw_reset_max_dsecs * 100);
2780 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2781 		break;
2782 	}
2783 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2784 		struct bnxt_fw_health *fw_health = bp->fw_health;
2785 		char *status_desc = "healthy";
2786 		u32 status;
2787 
2788 		if (!fw_health)
2789 			goto async_event_process_exit;
2790 
2791 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2792 			fw_health->enabled = false;
2793 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2794 			break;
2795 		}
2796 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2797 		fw_health->tmr_multiplier =
2798 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2799 				     bp->current_interval * 10);
2800 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2801 		if (!fw_health->enabled)
2802 			fw_health->last_fw_heartbeat =
2803 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2804 		fw_health->last_fw_reset_cnt =
2805 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2806 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2807 		if (status != BNXT_FW_STATUS_HEALTHY)
2808 			status_desc = "unhealthy";
2809 		netif_info(bp, drv, bp->dev,
2810 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2811 			   fw_health->primary ? "primary" : "backup", status,
2812 			   status_desc, fw_health->last_fw_reset_cnt);
2813 		if (!fw_health->enabled) {
2814 			/* Make sure tmr_counter is set and visible to
2815 			 * bnxt_health_check() before setting enabled to true.
2816 			 */
2817 			smp_wmb();
2818 			fw_health->enabled = true;
2819 		}
2820 		goto async_event_process_exit;
2821 	}
2822 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2823 		netif_notice(bp, hw, bp->dev,
2824 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2825 			     data1, data2);
2826 		goto async_event_process_exit;
2827 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2828 		struct bnxt_rx_ring_info *rxr;
2829 		u16 grp_idx;
2830 
2831 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2832 			goto async_event_process_exit;
2833 
2834 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2835 			    BNXT_EVENT_RING_TYPE(data2), data1);
2836 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2837 			goto async_event_process_exit;
2838 
2839 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2840 		if (grp_idx == INVALID_HW_RING_ID) {
2841 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2842 				    data1);
2843 			goto async_event_process_exit;
2844 		}
2845 		rxr = bp->bnapi[grp_idx]->rx_ring;
2846 		bnxt_sched_reset_rxr(bp, rxr);
2847 		goto async_event_process_exit;
2848 	}
2849 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2850 		struct bnxt_fw_health *fw_health = bp->fw_health;
2851 
2852 		netif_notice(bp, hw, bp->dev,
2853 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2854 			     data1, data2);
2855 		if (fw_health) {
2856 			fw_health->echo_req_data1 = data1;
2857 			fw_health->echo_req_data2 = data2;
2858 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2859 			break;
2860 		}
2861 		goto async_event_process_exit;
2862 	}
2863 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2864 		bnxt_ptp_pps_event(bp, data1, data2);
2865 		goto async_event_process_exit;
2866 	}
2867 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2868 		if (bnxt_event_error_report(bp, data1, data2))
2869 			break;
2870 		goto async_event_process_exit;
2871 	}
2872 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2873 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2874 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2875 			if (BNXT_PTP_USE_RTC(bp)) {
2876 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2877 				unsigned long flags;
2878 				u64 ns;
2879 
2880 				if (!ptp)
2881 					goto async_event_process_exit;
2882 
2883 				bnxt_ptp_update_current_time(bp);
2884 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2885 				       BNXT_PHC_BITS) | ptp->current_time);
2886 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2887 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2888 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2889 			}
2890 			break;
2891 		}
2892 		goto async_event_process_exit;
2893 	}
2894 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2895 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2896 
2897 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2898 		goto async_event_process_exit;
2899 	}
2900 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2901 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2902 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2903 
2904 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2905 		goto async_event_process_exit;
2906 	}
2907 	default:
2908 		goto async_event_process_exit;
2909 	}
2910 	__bnxt_queue_sp_work(bp);
2911 async_event_process_exit:
2912 	bnxt_ulp_async_events(bp, cmpl);
2913 	return 0;
2914 }
2915 
2916 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2917 {
2918 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2919 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2920 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2921 				(struct hwrm_fwd_req_cmpl *)txcmp;
2922 
2923 	switch (cmpl_type) {
2924 	case CMPL_BASE_TYPE_HWRM_DONE:
2925 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2926 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2927 		break;
2928 
2929 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2930 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2931 
2932 		if ((vf_id < bp->pf.first_vf_id) ||
2933 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2934 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2935 				   vf_id);
2936 			return -EINVAL;
2937 		}
2938 
2939 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2940 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2941 		break;
2942 
2943 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2944 		bnxt_async_event_process(bp,
2945 					 (struct hwrm_async_event_cmpl *)txcmp);
2946 		break;
2947 
2948 	default:
2949 		break;
2950 	}
2951 
2952 	return 0;
2953 }
2954 
2955 static bool bnxt_vnic_is_active(struct bnxt *bp)
2956 {
2957 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2958 
2959 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2960 }
2961 
2962 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2963 {
2964 	struct bnxt_napi *bnapi = dev_instance;
2965 	struct bnxt *bp = bnapi->bp;
2966 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2967 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2968 
2969 	cpr->event_ctr++;
2970 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2971 	napi_schedule(&bnapi->napi);
2972 	return IRQ_HANDLED;
2973 }
2974 
2975 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2976 {
2977 	u32 raw_cons = cpr->cp_raw_cons;
2978 	u16 cons = RING_CMP(raw_cons);
2979 	struct tx_cmp *txcmp;
2980 
2981 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2982 
2983 	return TX_CMP_VALID(txcmp, raw_cons);
2984 }
2985 
2986 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2987 			    int budget)
2988 {
2989 	struct bnxt_napi *bnapi = cpr->bnapi;
2990 	u32 raw_cons = cpr->cp_raw_cons;
2991 	u32 cons;
2992 	int rx_pkts = 0;
2993 	u8 event = 0;
2994 	struct tx_cmp *txcmp;
2995 
2996 	cpr->has_more_work = 0;
2997 	cpr->had_work_done = 1;
2998 	while (1) {
2999 		u8 cmp_type;
3000 		int rc;
3001 
3002 		cons = RING_CMP(raw_cons);
3003 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3004 
3005 		if (!TX_CMP_VALID(txcmp, raw_cons))
3006 			break;
3007 
3008 		/* The valid test of the entry must be done first before
3009 		 * reading any further.
3010 		 */
3011 		dma_rmb();
3012 		cmp_type = TX_CMP_TYPE(txcmp);
3013 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
3014 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
3015 			u32 opaque = txcmp->tx_cmp_opaque;
3016 			struct bnxt_tx_ring_info *txr;
3017 			u16 tx_freed;
3018 
3019 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
3020 			event |= BNXT_TX_CMP_EVENT;
3021 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
3022 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
3023 			else
3024 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
3025 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
3026 				   bp->tx_ring_mask;
3027 			/* return full budget so NAPI will complete. */
3028 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
3029 				rx_pkts = budget;
3030 				raw_cons = NEXT_RAW_CMP(raw_cons);
3031 				if (budget)
3032 					cpr->has_more_work = 1;
3033 				break;
3034 			}
3035 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
3036 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
3037 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
3038 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
3039 			if (likely(budget))
3040 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3041 			else
3042 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
3043 							   &event);
3044 			if (likely(rc >= 0))
3045 				rx_pkts += rc;
3046 			/* Increment rx_pkts when rc is -ENOMEM to count towards
3047 			 * the NAPI budget.  Otherwise, we may potentially loop
3048 			 * here forever if we consistently cannot allocate
3049 			 * buffers.
3050 			 */
3051 			else if (rc == -ENOMEM && budget)
3052 				rx_pkts++;
3053 			else if (rc == -EBUSY)	/* partial completion */
3054 				break;
3055 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3056 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3057 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3058 			bnxt_hwrm_handler(bp, txcmp);
3059 		}
3060 		raw_cons = NEXT_RAW_CMP(raw_cons);
3061 
3062 		if (rx_pkts && rx_pkts == budget) {
3063 			cpr->has_more_work = 1;
3064 			break;
3065 		}
3066 	}
3067 
3068 	if (event & BNXT_REDIRECT_EVENT) {
3069 		xdp_do_flush();
3070 		event &= ~BNXT_REDIRECT_EVENT;
3071 	}
3072 
3073 	if (event & BNXT_TX_EVENT) {
3074 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3075 		u16 prod = txr->tx_prod;
3076 
3077 		/* Sync BD data before updating doorbell */
3078 		wmb();
3079 
3080 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3081 		event &= ~BNXT_TX_EVENT;
3082 	}
3083 
3084 	cpr->cp_raw_cons = raw_cons;
3085 	bnapi->events |= event;
3086 	return rx_pkts;
3087 }
3088 
3089 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3090 				  int budget)
3091 {
3092 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3093 		bnapi->tx_int(bp, bnapi, budget);
3094 
3095 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3096 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3097 
3098 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3099 		bnapi->events &= ~BNXT_RX_EVENT;
3100 	}
3101 	if (bnapi->events & BNXT_AGG_EVENT) {
3102 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3103 
3104 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3105 		bnapi->events &= ~BNXT_AGG_EVENT;
3106 	}
3107 }
3108 
3109 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3110 			  int budget)
3111 {
3112 	struct bnxt_napi *bnapi = cpr->bnapi;
3113 	int rx_pkts;
3114 
3115 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3116 
3117 	/* ACK completion ring before freeing tx ring and producing new
3118 	 * buffers in rx/agg rings to prevent overflowing the completion
3119 	 * ring.
3120 	 */
3121 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3122 
3123 	__bnxt_poll_work_done(bp, bnapi, budget);
3124 	return rx_pkts;
3125 }
3126 
3127 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3128 {
3129 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3130 	struct bnxt *bp = bnapi->bp;
3131 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3132 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3133 	struct tx_cmp *txcmp;
3134 	struct rx_cmp_ext *rxcmp1;
3135 	u32 cp_cons, tmp_raw_cons;
3136 	u32 raw_cons = cpr->cp_raw_cons;
3137 	bool flush_xdp = false;
3138 	u32 rx_pkts = 0;
3139 	u8 event = 0;
3140 
3141 	while (1) {
3142 		int rc;
3143 
3144 		cp_cons = RING_CMP(raw_cons);
3145 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3146 
3147 		if (!TX_CMP_VALID(txcmp, raw_cons))
3148 			break;
3149 
3150 		/* The valid test of the entry must be done first before
3151 		 * reading any further.
3152 		 */
3153 		dma_rmb();
3154 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3155 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3156 			cp_cons = RING_CMP(tmp_raw_cons);
3157 			rxcmp1 = (struct rx_cmp_ext *)
3158 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3159 
3160 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3161 				break;
3162 
3163 			/* force an error to recycle the buffer */
3164 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3165 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3166 
3167 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3168 			if (likely(rc == -EIO) && budget)
3169 				rx_pkts++;
3170 			else if (rc == -EBUSY)	/* partial completion */
3171 				break;
3172 			if (event & BNXT_REDIRECT_EVENT)
3173 				flush_xdp = true;
3174 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3175 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3176 			bnxt_hwrm_handler(bp, txcmp);
3177 		} else {
3178 			netdev_err(bp->dev,
3179 				   "Invalid completion received on special ring\n");
3180 		}
3181 		raw_cons = NEXT_RAW_CMP(raw_cons);
3182 
3183 		if (rx_pkts == budget)
3184 			break;
3185 	}
3186 
3187 	cpr->cp_raw_cons = raw_cons;
3188 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3189 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3190 
3191 	if (event & BNXT_AGG_EVENT)
3192 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3193 	if (flush_xdp)
3194 		xdp_do_flush();
3195 
3196 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3197 		napi_complete_done(napi, rx_pkts);
3198 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3199 	}
3200 	return rx_pkts;
3201 }
3202 
3203 static int bnxt_poll(struct napi_struct *napi, int budget)
3204 {
3205 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3206 	struct bnxt *bp = bnapi->bp;
3207 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3208 	int work_done = 0;
3209 
3210 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3211 		napi_complete(napi);
3212 		return 0;
3213 	}
3214 	while (1) {
3215 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3216 
3217 		if (work_done >= budget) {
3218 			if (!budget)
3219 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3220 			break;
3221 		}
3222 
3223 		if (!bnxt_has_work(bp, cpr)) {
3224 			if (napi_complete_done(napi, work_done))
3225 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3226 			break;
3227 		}
3228 	}
3229 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3230 		struct dim_sample dim_sample = {};
3231 
3232 		dim_update_sample(cpr->event_ctr,
3233 				  cpr->rx_packets,
3234 				  cpr->rx_bytes,
3235 				  &dim_sample);
3236 		net_dim(&cpr->dim, &dim_sample);
3237 	}
3238 	return work_done;
3239 }
3240 
3241 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3242 {
3243 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3244 	int i, work_done = 0;
3245 
3246 	for (i = 0; i < cpr->cp_ring_count; i++) {
3247 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3248 
3249 		if (cpr2->had_nqe_notify) {
3250 			work_done += __bnxt_poll_work(bp, cpr2,
3251 						      budget - work_done);
3252 			cpr->has_more_work |= cpr2->has_more_work;
3253 		}
3254 	}
3255 	return work_done;
3256 }
3257 
3258 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3259 				 u64 dbr_type, int budget)
3260 {
3261 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3262 	int i;
3263 
3264 	for (i = 0; i < cpr->cp_ring_count; i++) {
3265 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3266 		struct bnxt_db_info *db;
3267 
3268 		if (cpr2->had_work_done) {
3269 			u32 tgl = 0;
3270 
3271 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3272 				cpr2->had_nqe_notify = 0;
3273 				tgl = cpr2->toggle;
3274 			}
3275 			db = &cpr2->cp_db;
3276 			bnxt_writeq(bp,
3277 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3278 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3279 				    db->doorbell);
3280 			cpr2->had_work_done = 0;
3281 		}
3282 	}
3283 	__bnxt_poll_work_done(bp, bnapi, budget);
3284 }
3285 
3286 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3287 {
3288 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3289 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3290 	struct bnxt_cp_ring_info *cpr_rx;
3291 	u32 raw_cons = cpr->cp_raw_cons;
3292 	struct bnxt *bp = bnapi->bp;
3293 	struct nqe_cn *nqcmp;
3294 	int work_done = 0;
3295 	u32 cons;
3296 
3297 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3298 		napi_complete(napi);
3299 		return 0;
3300 	}
3301 	if (cpr->has_more_work) {
3302 		cpr->has_more_work = 0;
3303 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3304 	}
3305 	while (1) {
3306 		u16 type;
3307 
3308 		cons = RING_CMP(raw_cons);
3309 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3310 
3311 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3312 			if (cpr->has_more_work)
3313 				break;
3314 
3315 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3316 					     budget);
3317 			cpr->cp_raw_cons = raw_cons;
3318 			if (napi_complete_done(napi, work_done))
3319 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3320 						  cpr->cp_raw_cons);
3321 			goto poll_done;
3322 		}
3323 
3324 		/* The valid test of the entry must be done first before
3325 		 * reading any further.
3326 		 */
3327 		dma_rmb();
3328 
3329 		type = le16_to_cpu(nqcmp->type);
3330 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3331 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3332 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3333 			struct bnxt_cp_ring_info *cpr2;
3334 
3335 			/* No more budget for RX work */
3336 			if (budget && work_done >= budget &&
3337 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3338 				break;
3339 
3340 			idx = BNXT_NQ_HDL_IDX(idx);
3341 			cpr2 = &cpr->cp_ring_arr[idx];
3342 			cpr2->had_nqe_notify = 1;
3343 			cpr2->toggle = NQE_CN_TOGGLE(type);
3344 			work_done += __bnxt_poll_work(bp, cpr2,
3345 						      budget - work_done);
3346 			cpr->has_more_work |= cpr2->has_more_work;
3347 		} else {
3348 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3349 		}
3350 		raw_cons = NEXT_RAW_CMP(raw_cons);
3351 	}
3352 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3353 	if (raw_cons != cpr->cp_raw_cons) {
3354 		cpr->cp_raw_cons = raw_cons;
3355 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3356 	}
3357 poll_done:
3358 	cpr_rx = &cpr->cp_ring_arr[0];
3359 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3360 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3361 		struct dim_sample dim_sample = {};
3362 
3363 		dim_update_sample(cpr->event_ctr,
3364 				  cpr_rx->rx_packets,
3365 				  cpr_rx->rx_bytes,
3366 				  &dim_sample);
3367 		net_dim(&cpr->dim, &dim_sample);
3368 	}
3369 	return work_done;
3370 }
3371 
3372 static void bnxt_free_one_tx_ring_skbs(struct bnxt *bp,
3373 				       struct bnxt_tx_ring_info *txr, int idx)
3374 {
3375 	int i, max_idx;
3376 	struct pci_dev *pdev = bp->pdev;
3377 
3378 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3379 
3380 	for (i = 0; i < max_idx;) {
3381 		struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[i];
3382 		struct sk_buff *skb;
3383 		int j, last;
3384 
3385 		if (idx  < bp->tx_nr_rings_xdp &&
3386 		    tx_buf->action == XDP_REDIRECT) {
3387 			dma_unmap_single(&pdev->dev,
3388 					 dma_unmap_addr(tx_buf, mapping),
3389 					 dma_unmap_len(tx_buf, len),
3390 					 DMA_TO_DEVICE);
3391 			xdp_return_frame(tx_buf->xdpf);
3392 			tx_buf->action = 0;
3393 			tx_buf->xdpf = NULL;
3394 			i++;
3395 			continue;
3396 		}
3397 
3398 		skb = tx_buf->skb;
3399 		if (!skb) {
3400 			i++;
3401 			continue;
3402 		}
3403 
3404 		tx_buf->skb = NULL;
3405 
3406 		if (tx_buf->is_push) {
3407 			dev_kfree_skb(skb);
3408 			i += 2;
3409 			continue;
3410 		}
3411 
3412 		dma_unmap_single(&pdev->dev,
3413 				 dma_unmap_addr(tx_buf, mapping),
3414 				 skb_headlen(skb),
3415 				 DMA_TO_DEVICE);
3416 
3417 		last = tx_buf->nr_frags;
3418 		i += 2;
3419 		for (j = 0; j < last; j++, i++) {
3420 			int ring_idx = i & bp->tx_ring_mask;
3421 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
3422 
3423 			tx_buf = &txr->tx_buf_ring[ring_idx];
3424 			dma_unmap_page(&pdev->dev,
3425 				       dma_unmap_addr(tx_buf, mapping),
3426 				       skb_frag_size(frag), DMA_TO_DEVICE);
3427 		}
3428 		dev_kfree_skb(skb);
3429 	}
3430 	netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, idx));
3431 }
3432 
3433 static void bnxt_free_tx_skbs(struct bnxt *bp)
3434 {
3435 	int i;
3436 
3437 	if (!bp->tx_ring)
3438 		return;
3439 
3440 	for (i = 0; i < bp->tx_nr_rings; i++) {
3441 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3442 
3443 		if (!txr->tx_buf_ring)
3444 			continue;
3445 
3446 		bnxt_free_one_tx_ring_skbs(bp, txr, i);
3447 	}
3448 }
3449 
3450 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3451 {
3452 	int i, max_idx;
3453 
3454 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3455 
3456 	for (i = 0; i < max_idx; i++) {
3457 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3458 		void *data = rx_buf->data;
3459 
3460 		if (!data)
3461 			continue;
3462 
3463 		rx_buf->data = NULL;
3464 		if (BNXT_RX_PAGE_MODE(bp))
3465 			page_pool_recycle_direct(rxr->page_pool, data);
3466 		else
3467 			page_pool_free_va(rxr->head_pool, data, true);
3468 	}
3469 }
3470 
3471 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3472 {
3473 	int i, max_idx;
3474 
3475 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3476 
3477 	for (i = 0; i < max_idx; i++) {
3478 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3479 		netmem_ref netmem = rx_agg_buf->netmem;
3480 
3481 		if (!netmem)
3482 			continue;
3483 
3484 		rx_agg_buf->netmem = 0;
3485 		__clear_bit(i, rxr->rx_agg_bmap);
3486 
3487 		page_pool_recycle_direct_netmem(rxr->page_pool, netmem);
3488 	}
3489 }
3490 
3491 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3492 					struct bnxt_rx_ring_info *rxr)
3493 {
3494 	int i;
3495 
3496 	for (i = 0; i < bp->max_tpa; i++) {
3497 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3498 		u8 *data = tpa_info->data;
3499 
3500 		if (!data)
3501 			continue;
3502 
3503 		tpa_info->data = NULL;
3504 		page_pool_free_va(rxr->head_pool, data, false);
3505 	}
3506 }
3507 
3508 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3509 				       struct bnxt_rx_ring_info *rxr)
3510 {
3511 	struct bnxt_tpa_idx_map *map;
3512 
3513 	if (!rxr->rx_tpa)
3514 		goto skip_rx_tpa_free;
3515 
3516 	bnxt_free_one_tpa_info_data(bp, rxr);
3517 
3518 skip_rx_tpa_free:
3519 	if (!rxr->rx_buf_ring)
3520 		goto skip_rx_buf_free;
3521 
3522 	bnxt_free_one_rx_ring(bp, rxr);
3523 
3524 skip_rx_buf_free:
3525 	if (!rxr->rx_agg_ring)
3526 		goto skip_rx_agg_free;
3527 
3528 	bnxt_free_one_rx_agg_ring(bp, rxr);
3529 
3530 skip_rx_agg_free:
3531 	map = rxr->rx_tpa_idx_map;
3532 	if (map)
3533 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3534 }
3535 
3536 static void bnxt_free_rx_skbs(struct bnxt *bp)
3537 {
3538 	int i;
3539 
3540 	if (!bp->rx_ring)
3541 		return;
3542 
3543 	for (i = 0; i < bp->rx_nr_rings; i++)
3544 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3545 }
3546 
3547 static void bnxt_free_skbs(struct bnxt *bp)
3548 {
3549 	bnxt_free_tx_skbs(bp);
3550 	bnxt_free_rx_skbs(bp);
3551 }
3552 
3553 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3554 {
3555 	u8 init_val = ctxm->init_value;
3556 	u16 offset = ctxm->init_offset;
3557 	u8 *p2 = p;
3558 	int i;
3559 
3560 	if (!init_val)
3561 		return;
3562 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3563 		memset(p, init_val, len);
3564 		return;
3565 	}
3566 	for (i = 0; i < len; i += ctxm->entry_size)
3567 		*(p2 + i + offset) = init_val;
3568 }
3569 
3570 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3571 			       void *buf, size_t offset, size_t head,
3572 			       size_t tail)
3573 {
3574 	int i, head_page, start_idx, source_offset;
3575 	size_t len, rem_len, total_len, max_bytes;
3576 
3577 	head_page = head / rmem->page_size;
3578 	source_offset = head % rmem->page_size;
3579 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3580 	if (!total_len)
3581 		total_len = MAX_CTX_BYTES;
3582 	start_idx = head_page % MAX_CTX_PAGES;
3583 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3584 		    source_offset;
3585 	total_len = min(total_len, max_bytes);
3586 	rem_len = total_len;
3587 
3588 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3589 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3590 		if (buf)
3591 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3592 			       len);
3593 		offset += len;
3594 		rem_len -= len;
3595 	}
3596 	return total_len;
3597 }
3598 
3599 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3600 {
3601 	struct pci_dev *pdev = bp->pdev;
3602 	int i;
3603 
3604 	if (!rmem->pg_arr)
3605 		goto skip_pages;
3606 
3607 	for (i = 0; i < rmem->nr_pages; i++) {
3608 		if (!rmem->pg_arr[i])
3609 			continue;
3610 
3611 		dma_free_coherent(&pdev->dev, rmem->page_size,
3612 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3613 
3614 		rmem->pg_arr[i] = NULL;
3615 	}
3616 skip_pages:
3617 	if (rmem->pg_tbl) {
3618 		size_t pg_tbl_size = rmem->nr_pages * 8;
3619 
3620 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3621 			pg_tbl_size = rmem->page_size;
3622 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3623 				  rmem->pg_tbl, rmem->pg_tbl_map);
3624 		rmem->pg_tbl = NULL;
3625 	}
3626 	if (rmem->vmem_size && *rmem->vmem) {
3627 		vfree(*rmem->vmem);
3628 		*rmem->vmem = NULL;
3629 	}
3630 }
3631 
3632 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3633 {
3634 	struct pci_dev *pdev = bp->pdev;
3635 	u64 valid_bit = 0;
3636 	int i;
3637 
3638 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3639 		valid_bit = PTU_PTE_VALID;
3640 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3641 		size_t pg_tbl_size = rmem->nr_pages * 8;
3642 
3643 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3644 			pg_tbl_size = rmem->page_size;
3645 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3646 						  &rmem->pg_tbl_map,
3647 						  GFP_KERNEL);
3648 		if (!rmem->pg_tbl)
3649 			return -ENOMEM;
3650 	}
3651 
3652 	for (i = 0; i < rmem->nr_pages; i++) {
3653 		u64 extra_bits = valid_bit;
3654 
3655 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3656 						     rmem->page_size,
3657 						     &rmem->dma_arr[i],
3658 						     GFP_KERNEL);
3659 		if (!rmem->pg_arr[i])
3660 			return -ENOMEM;
3661 
3662 		if (rmem->ctx_mem)
3663 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3664 					  rmem->page_size);
3665 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3666 			if (i == rmem->nr_pages - 2 &&
3667 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3668 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3669 			else if (i == rmem->nr_pages - 1 &&
3670 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3671 				extra_bits |= PTU_PTE_LAST;
3672 			rmem->pg_tbl[i] =
3673 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3674 		}
3675 	}
3676 
3677 	if (rmem->vmem_size) {
3678 		*rmem->vmem = vzalloc(rmem->vmem_size);
3679 		if (!(*rmem->vmem))
3680 			return -ENOMEM;
3681 	}
3682 	return 0;
3683 }
3684 
3685 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3686 				   struct bnxt_rx_ring_info *rxr)
3687 {
3688 	int i;
3689 
3690 	kfree(rxr->rx_tpa_idx_map);
3691 	rxr->rx_tpa_idx_map = NULL;
3692 	if (rxr->rx_tpa) {
3693 		for (i = 0; i < bp->max_tpa; i++) {
3694 			kfree(rxr->rx_tpa[i].agg_arr);
3695 			rxr->rx_tpa[i].agg_arr = NULL;
3696 		}
3697 	}
3698 	kfree(rxr->rx_tpa);
3699 	rxr->rx_tpa = NULL;
3700 }
3701 
3702 static void bnxt_free_tpa_info(struct bnxt *bp)
3703 {
3704 	int i;
3705 
3706 	for (i = 0; i < bp->rx_nr_rings; i++) {
3707 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3708 
3709 		bnxt_free_one_tpa_info(bp, rxr);
3710 	}
3711 }
3712 
3713 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3714 				   struct bnxt_rx_ring_info *rxr)
3715 {
3716 	struct rx_agg_cmp *agg;
3717 	int i;
3718 
3719 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3720 			      GFP_KERNEL);
3721 	if (!rxr->rx_tpa)
3722 		return -ENOMEM;
3723 
3724 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3725 		return 0;
3726 	for (i = 0; i < bp->max_tpa; i++) {
3727 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3728 		if (!agg)
3729 			return -ENOMEM;
3730 		rxr->rx_tpa[i].agg_arr = agg;
3731 	}
3732 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3733 				      GFP_KERNEL);
3734 	if (!rxr->rx_tpa_idx_map)
3735 		return -ENOMEM;
3736 
3737 	return 0;
3738 }
3739 
3740 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3741 {
3742 	int i, rc;
3743 
3744 	bp->max_tpa = MAX_TPA;
3745 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3746 		if (!bp->max_tpa_v2)
3747 			return 0;
3748 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3749 	}
3750 
3751 	for (i = 0; i < bp->rx_nr_rings; i++) {
3752 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3753 
3754 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3755 		if (rc)
3756 			return rc;
3757 	}
3758 	return 0;
3759 }
3760 
3761 static void bnxt_free_rx_rings(struct bnxt *bp)
3762 {
3763 	int i;
3764 
3765 	if (!bp->rx_ring)
3766 		return;
3767 
3768 	bnxt_free_tpa_info(bp);
3769 	for (i = 0; i < bp->rx_nr_rings; i++) {
3770 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3771 		struct bnxt_ring_struct *ring;
3772 
3773 		if (rxr->xdp_prog)
3774 			bpf_prog_put(rxr->xdp_prog);
3775 
3776 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3777 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3778 
3779 		page_pool_destroy(rxr->page_pool);
3780 		if (bnxt_separate_head_pool(rxr))
3781 			page_pool_destroy(rxr->head_pool);
3782 		rxr->page_pool = rxr->head_pool = NULL;
3783 
3784 		kfree(rxr->rx_agg_bmap);
3785 		rxr->rx_agg_bmap = NULL;
3786 
3787 		ring = &rxr->rx_ring_struct;
3788 		bnxt_free_ring(bp, &ring->ring_mem);
3789 
3790 		ring = &rxr->rx_agg_ring_struct;
3791 		bnxt_free_ring(bp, &ring->ring_mem);
3792 	}
3793 }
3794 
3795 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3796 				   struct bnxt_rx_ring_info *rxr,
3797 				   int numa_node)
3798 {
3799 	struct page_pool_params pp = { 0 };
3800 	struct page_pool *pool;
3801 
3802 	pp.pool_size = bp->rx_agg_ring_size;
3803 	if (BNXT_RX_PAGE_MODE(bp))
3804 		pp.pool_size += bp->rx_ring_size;
3805 	pp.nid = numa_node;
3806 	pp.napi = &rxr->bnapi->napi;
3807 	pp.netdev = bp->dev;
3808 	pp.dev = &bp->pdev->dev;
3809 	pp.dma_dir = bp->rx_dir;
3810 	pp.max_len = PAGE_SIZE;
3811 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV |
3812 		   PP_FLAG_ALLOW_UNREADABLE_NETMEM;
3813 	pp.queue_idx = rxr->bnapi->index;
3814 
3815 	pool = page_pool_create(&pp);
3816 	if (IS_ERR(pool))
3817 		return PTR_ERR(pool);
3818 	rxr->page_pool = pool;
3819 
3820 	rxr->need_head_pool = page_pool_is_unreadable(pool);
3821 	if (bnxt_separate_head_pool(rxr)) {
3822 		pp.pool_size = max(bp->rx_ring_size, 1024);
3823 		pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3824 		pool = page_pool_create(&pp);
3825 		if (IS_ERR(pool))
3826 			goto err_destroy_pp;
3827 	}
3828 	rxr->head_pool = pool;
3829 
3830 	return 0;
3831 
3832 err_destroy_pp:
3833 	page_pool_destroy(rxr->page_pool);
3834 	rxr->page_pool = NULL;
3835 	return PTR_ERR(pool);
3836 }
3837 
3838 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3839 {
3840 	u16 mem_size;
3841 
3842 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3843 	mem_size = rxr->rx_agg_bmap_size / 8;
3844 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3845 	if (!rxr->rx_agg_bmap)
3846 		return -ENOMEM;
3847 
3848 	return 0;
3849 }
3850 
3851 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3852 {
3853 	int numa_node = dev_to_node(&bp->pdev->dev);
3854 	int i, rc = 0, agg_rings = 0, cpu;
3855 
3856 	if (!bp->rx_ring)
3857 		return -ENOMEM;
3858 
3859 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3860 		agg_rings = 1;
3861 
3862 	for (i = 0; i < bp->rx_nr_rings; i++) {
3863 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3864 		struct bnxt_ring_struct *ring;
3865 		int cpu_node;
3866 
3867 		ring = &rxr->rx_ring_struct;
3868 
3869 		cpu = cpumask_local_spread(i, numa_node);
3870 		cpu_node = cpu_to_node(cpu);
3871 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3872 			   i, cpu_node);
3873 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3874 		if (rc)
3875 			return rc;
3876 
3877 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3878 		if (rc < 0)
3879 			return rc;
3880 
3881 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3882 						MEM_TYPE_PAGE_POOL,
3883 						rxr->page_pool);
3884 		if (rc) {
3885 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3886 			return rc;
3887 		}
3888 
3889 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3890 		if (rc)
3891 			return rc;
3892 
3893 		ring->grp_idx = i;
3894 		if (agg_rings) {
3895 			ring = &rxr->rx_agg_ring_struct;
3896 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3897 			if (rc)
3898 				return rc;
3899 
3900 			ring->grp_idx = i;
3901 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3902 			if (rc)
3903 				return rc;
3904 		}
3905 	}
3906 	if (bp->flags & BNXT_FLAG_TPA)
3907 		rc = bnxt_alloc_tpa_info(bp);
3908 	return rc;
3909 }
3910 
3911 static void bnxt_free_tx_rings(struct bnxt *bp)
3912 {
3913 	int i;
3914 	struct pci_dev *pdev = bp->pdev;
3915 
3916 	if (!bp->tx_ring)
3917 		return;
3918 
3919 	for (i = 0; i < bp->tx_nr_rings; i++) {
3920 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3921 		struct bnxt_ring_struct *ring;
3922 
3923 		if (txr->tx_push) {
3924 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3925 					  txr->tx_push, txr->tx_push_mapping);
3926 			txr->tx_push = NULL;
3927 		}
3928 
3929 		ring = &txr->tx_ring_struct;
3930 
3931 		bnxt_free_ring(bp, &ring->ring_mem);
3932 	}
3933 }
3934 
3935 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3936 	((tc) * (bp)->tx_nr_rings_per_tc)
3937 
3938 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3939 	((tx) % (bp)->tx_nr_rings_per_tc)
3940 
3941 #define BNXT_RING_TO_TC(bp, tx)		\
3942 	((tx) / (bp)->tx_nr_rings_per_tc)
3943 
3944 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3945 {
3946 	int i, j, rc;
3947 	struct pci_dev *pdev = bp->pdev;
3948 
3949 	bp->tx_push_size = 0;
3950 	if (bp->tx_push_thresh) {
3951 		int push_size;
3952 
3953 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3954 					bp->tx_push_thresh);
3955 
3956 		if (push_size > 256) {
3957 			push_size = 0;
3958 			bp->tx_push_thresh = 0;
3959 		}
3960 
3961 		bp->tx_push_size = push_size;
3962 	}
3963 
3964 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3965 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3966 		struct bnxt_ring_struct *ring;
3967 		u8 qidx;
3968 
3969 		ring = &txr->tx_ring_struct;
3970 
3971 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3972 		if (rc)
3973 			return rc;
3974 
3975 		ring->grp_idx = txr->bnapi->index;
3976 		if (bp->tx_push_size) {
3977 			dma_addr_t mapping;
3978 
3979 			/* One pre-allocated DMA buffer to backup
3980 			 * TX push operation
3981 			 */
3982 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3983 						bp->tx_push_size,
3984 						&txr->tx_push_mapping,
3985 						GFP_KERNEL);
3986 
3987 			if (!txr->tx_push)
3988 				return -ENOMEM;
3989 
3990 			mapping = txr->tx_push_mapping +
3991 				sizeof(struct tx_push_bd);
3992 			txr->data_mapping = cpu_to_le64(mapping);
3993 		}
3994 		qidx = bp->tc_to_qidx[j];
3995 		ring->queue_id = bp->q_info[qidx].queue_id;
3996 		spin_lock_init(&txr->xdp_tx_lock);
3997 		if (i < bp->tx_nr_rings_xdp)
3998 			continue;
3999 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
4000 			j++;
4001 	}
4002 	return 0;
4003 }
4004 
4005 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
4006 {
4007 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4008 
4009 	kfree(cpr->cp_desc_ring);
4010 	cpr->cp_desc_ring = NULL;
4011 	ring->ring_mem.pg_arr = NULL;
4012 	kfree(cpr->cp_desc_mapping);
4013 	cpr->cp_desc_mapping = NULL;
4014 	ring->ring_mem.dma_arr = NULL;
4015 }
4016 
4017 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
4018 {
4019 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
4020 	if (!cpr->cp_desc_ring)
4021 		return -ENOMEM;
4022 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
4023 				       GFP_KERNEL);
4024 	if (!cpr->cp_desc_mapping)
4025 		return -ENOMEM;
4026 	return 0;
4027 }
4028 
4029 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
4030 {
4031 	int i;
4032 
4033 	if (!bp->bnapi)
4034 		return;
4035 	for (i = 0; i < bp->cp_nr_rings; i++) {
4036 		struct bnxt_napi *bnapi = bp->bnapi[i];
4037 
4038 		if (!bnapi)
4039 			continue;
4040 		bnxt_free_cp_arrays(&bnapi->cp_ring);
4041 	}
4042 }
4043 
4044 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
4045 {
4046 	int i, n = bp->cp_nr_pages;
4047 
4048 	for (i = 0; i < bp->cp_nr_rings; i++) {
4049 		struct bnxt_napi *bnapi = bp->bnapi[i];
4050 		int rc;
4051 
4052 		if (!bnapi)
4053 			continue;
4054 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
4055 		if (rc)
4056 			return rc;
4057 	}
4058 	return 0;
4059 }
4060 
4061 static void bnxt_free_cp_rings(struct bnxt *bp)
4062 {
4063 	int i;
4064 
4065 	if (!bp->bnapi)
4066 		return;
4067 
4068 	for (i = 0; i < bp->cp_nr_rings; i++) {
4069 		struct bnxt_napi *bnapi = bp->bnapi[i];
4070 		struct bnxt_cp_ring_info *cpr;
4071 		struct bnxt_ring_struct *ring;
4072 		int j;
4073 
4074 		if (!bnapi)
4075 			continue;
4076 
4077 		cpr = &bnapi->cp_ring;
4078 		ring = &cpr->cp_ring_struct;
4079 
4080 		bnxt_free_ring(bp, &ring->ring_mem);
4081 
4082 		if (!cpr->cp_ring_arr)
4083 			continue;
4084 
4085 		for (j = 0; j < cpr->cp_ring_count; j++) {
4086 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4087 
4088 			ring = &cpr2->cp_ring_struct;
4089 			bnxt_free_ring(bp, &ring->ring_mem);
4090 			bnxt_free_cp_arrays(cpr2);
4091 		}
4092 		kfree(cpr->cp_ring_arr);
4093 		cpr->cp_ring_arr = NULL;
4094 		cpr->cp_ring_count = 0;
4095 	}
4096 }
4097 
4098 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4099 				  struct bnxt_cp_ring_info *cpr)
4100 {
4101 	struct bnxt_ring_mem_info *rmem;
4102 	struct bnxt_ring_struct *ring;
4103 	int rc;
4104 
4105 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4106 	if (rc) {
4107 		bnxt_free_cp_arrays(cpr);
4108 		return -ENOMEM;
4109 	}
4110 	ring = &cpr->cp_ring_struct;
4111 	rmem = &ring->ring_mem;
4112 	rmem->nr_pages = bp->cp_nr_pages;
4113 	rmem->page_size = HW_CMPD_RING_SIZE;
4114 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4115 	rmem->dma_arr = cpr->cp_desc_mapping;
4116 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4117 	rc = bnxt_alloc_ring(bp, rmem);
4118 	if (rc) {
4119 		bnxt_free_ring(bp, rmem);
4120 		bnxt_free_cp_arrays(cpr);
4121 	}
4122 	return rc;
4123 }
4124 
4125 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4126 {
4127 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4128 	int i, j, rc, ulp_msix;
4129 	int tcs = bp->num_tc;
4130 
4131 	if (!tcs)
4132 		tcs = 1;
4133 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4134 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4135 		struct bnxt_napi *bnapi = bp->bnapi[i];
4136 		struct bnxt_cp_ring_info *cpr, *cpr2;
4137 		struct bnxt_ring_struct *ring;
4138 		int cp_count = 0, k;
4139 		int rx = 0, tx = 0;
4140 
4141 		if (!bnapi)
4142 			continue;
4143 
4144 		cpr = &bnapi->cp_ring;
4145 		cpr->bnapi = bnapi;
4146 		ring = &cpr->cp_ring_struct;
4147 
4148 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4149 		if (rc)
4150 			return rc;
4151 
4152 		ring->map_idx = ulp_msix + i;
4153 
4154 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4155 			continue;
4156 
4157 		if (i < bp->rx_nr_rings) {
4158 			cp_count++;
4159 			rx = 1;
4160 		}
4161 		if (i < bp->tx_nr_rings_xdp) {
4162 			cp_count++;
4163 			tx = 1;
4164 		} else if ((sh && i < bp->tx_nr_rings) ||
4165 			 (!sh && i >= bp->rx_nr_rings)) {
4166 			cp_count += tcs;
4167 			tx = 1;
4168 		}
4169 
4170 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4171 					   GFP_KERNEL);
4172 		if (!cpr->cp_ring_arr)
4173 			return -ENOMEM;
4174 		cpr->cp_ring_count = cp_count;
4175 
4176 		for (k = 0; k < cp_count; k++) {
4177 			cpr2 = &cpr->cp_ring_arr[k];
4178 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4179 			if (rc)
4180 				return rc;
4181 			cpr2->bnapi = bnapi;
4182 			cpr2->sw_stats = cpr->sw_stats;
4183 			cpr2->cp_idx = k;
4184 			if (!k && rx) {
4185 				bp->rx_ring[i].rx_cpr = cpr2;
4186 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4187 			} else {
4188 				int n, tc = k - rx;
4189 
4190 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4191 				bp->tx_ring[n].tx_cpr = cpr2;
4192 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4193 			}
4194 		}
4195 		if (tx)
4196 			j++;
4197 	}
4198 	return 0;
4199 }
4200 
4201 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4202 				     struct bnxt_rx_ring_info *rxr)
4203 {
4204 	struct bnxt_ring_mem_info *rmem;
4205 	struct bnxt_ring_struct *ring;
4206 
4207 	ring = &rxr->rx_ring_struct;
4208 	rmem = &ring->ring_mem;
4209 	rmem->nr_pages = bp->rx_nr_pages;
4210 	rmem->page_size = HW_RXBD_RING_SIZE;
4211 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4212 	rmem->dma_arr = rxr->rx_desc_mapping;
4213 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4214 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4215 
4216 	ring = &rxr->rx_agg_ring_struct;
4217 	rmem = &ring->ring_mem;
4218 	rmem->nr_pages = bp->rx_agg_nr_pages;
4219 	rmem->page_size = HW_RXBD_RING_SIZE;
4220 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4221 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4222 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4223 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4224 }
4225 
4226 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4227 				      struct bnxt_rx_ring_info *rxr)
4228 {
4229 	struct bnxt_ring_mem_info *rmem;
4230 	struct bnxt_ring_struct *ring;
4231 	int i;
4232 
4233 	rxr->page_pool->p.napi = NULL;
4234 	rxr->page_pool = NULL;
4235 	rxr->head_pool->p.napi = NULL;
4236 	rxr->head_pool = NULL;
4237 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4238 
4239 	ring = &rxr->rx_ring_struct;
4240 	rmem = &ring->ring_mem;
4241 	rmem->pg_tbl = NULL;
4242 	rmem->pg_tbl_map = 0;
4243 	for (i = 0; i < rmem->nr_pages; i++) {
4244 		rmem->pg_arr[i] = NULL;
4245 		rmem->dma_arr[i] = 0;
4246 	}
4247 	*rmem->vmem = NULL;
4248 
4249 	ring = &rxr->rx_agg_ring_struct;
4250 	rmem = &ring->ring_mem;
4251 	rmem->pg_tbl = NULL;
4252 	rmem->pg_tbl_map = 0;
4253 	for (i = 0; i < rmem->nr_pages; i++) {
4254 		rmem->pg_arr[i] = NULL;
4255 		rmem->dma_arr[i] = 0;
4256 	}
4257 	*rmem->vmem = NULL;
4258 }
4259 
4260 static void bnxt_init_ring_struct(struct bnxt *bp)
4261 {
4262 	int i, j;
4263 
4264 	for (i = 0; i < bp->cp_nr_rings; i++) {
4265 		struct bnxt_napi *bnapi = bp->bnapi[i];
4266 		struct bnxt_ring_mem_info *rmem;
4267 		struct bnxt_cp_ring_info *cpr;
4268 		struct bnxt_rx_ring_info *rxr;
4269 		struct bnxt_tx_ring_info *txr;
4270 		struct bnxt_ring_struct *ring;
4271 
4272 		if (!bnapi)
4273 			continue;
4274 
4275 		cpr = &bnapi->cp_ring;
4276 		ring = &cpr->cp_ring_struct;
4277 		rmem = &ring->ring_mem;
4278 		rmem->nr_pages = bp->cp_nr_pages;
4279 		rmem->page_size = HW_CMPD_RING_SIZE;
4280 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4281 		rmem->dma_arr = cpr->cp_desc_mapping;
4282 		rmem->vmem_size = 0;
4283 
4284 		rxr = bnapi->rx_ring;
4285 		if (!rxr)
4286 			goto skip_rx;
4287 
4288 		ring = &rxr->rx_ring_struct;
4289 		rmem = &ring->ring_mem;
4290 		rmem->nr_pages = bp->rx_nr_pages;
4291 		rmem->page_size = HW_RXBD_RING_SIZE;
4292 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4293 		rmem->dma_arr = rxr->rx_desc_mapping;
4294 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4295 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4296 
4297 		ring = &rxr->rx_agg_ring_struct;
4298 		rmem = &ring->ring_mem;
4299 		rmem->nr_pages = bp->rx_agg_nr_pages;
4300 		rmem->page_size = HW_RXBD_RING_SIZE;
4301 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4302 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4303 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4304 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4305 
4306 skip_rx:
4307 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4308 			ring = &txr->tx_ring_struct;
4309 			rmem = &ring->ring_mem;
4310 			rmem->nr_pages = bp->tx_nr_pages;
4311 			rmem->page_size = HW_TXBD_RING_SIZE;
4312 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4313 			rmem->dma_arr = txr->tx_desc_mapping;
4314 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4315 			rmem->vmem = (void **)&txr->tx_buf_ring;
4316 		}
4317 	}
4318 }
4319 
4320 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4321 {
4322 	int i;
4323 	u32 prod;
4324 	struct rx_bd **rx_buf_ring;
4325 
4326 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4327 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4328 		int j;
4329 		struct rx_bd *rxbd;
4330 
4331 		rxbd = rx_buf_ring[i];
4332 		if (!rxbd)
4333 			continue;
4334 
4335 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4336 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4337 			rxbd->rx_bd_opaque = prod;
4338 		}
4339 	}
4340 }
4341 
4342 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4343 				       struct bnxt_rx_ring_info *rxr,
4344 				       int ring_nr)
4345 {
4346 	u32 prod;
4347 	int i;
4348 
4349 	prod = rxr->rx_prod;
4350 	for (i = 0; i < bp->rx_ring_size; i++) {
4351 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4352 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4353 				    ring_nr, i, bp->rx_ring_size);
4354 			break;
4355 		}
4356 		prod = NEXT_RX(prod);
4357 	}
4358 	rxr->rx_prod = prod;
4359 }
4360 
4361 static void bnxt_alloc_one_rx_ring_netmem(struct bnxt *bp,
4362 					  struct bnxt_rx_ring_info *rxr,
4363 					  int ring_nr)
4364 {
4365 	u32 prod;
4366 	int i;
4367 
4368 	prod = rxr->rx_agg_prod;
4369 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4370 		if (bnxt_alloc_rx_netmem(bp, rxr, prod, GFP_KERNEL)) {
4371 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4372 				    ring_nr, i, bp->rx_ring_size);
4373 			break;
4374 		}
4375 		prod = NEXT_RX_AGG(prod);
4376 	}
4377 	rxr->rx_agg_prod = prod;
4378 }
4379 
4380 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4381 					struct bnxt_rx_ring_info *rxr)
4382 {
4383 	dma_addr_t mapping;
4384 	u8 *data;
4385 	int i;
4386 
4387 	for (i = 0; i < bp->max_tpa; i++) {
4388 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4389 					    GFP_KERNEL);
4390 		if (!data)
4391 			return -ENOMEM;
4392 
4393 		rxr->rx_tpa[i].data = data;
4394 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4395 		rxr->rx_tpa[i].mapping = mapping;
4396 	}
4397 
4398 	return 0;
4399 }
4400 
4401 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4402 {
4403 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4404 	int rc;
4405 
4406 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4407 
4408 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4409 		return 0;
4410 
4411 	bnxt_alloc_one_rx_ring_netmem(bp, rxr, ring_nr);
4412 
4413 	if (rxr->rx_tpa) {
4414 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4415 		if (rc)
4416 			return rc;
4417 	}
4418 	return 0;
4419 }
4420 
4421 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4422 				       struct bnxt_rx_ring_info *rxr)
4423 {
4424 	struct bnxt_ring_struct *ring;
4425 	u32 type;
4426 
4427 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4428 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4429 
4430 	if (NET_IP_ALIGN == 2)
4431 		type |= RX_BD_FLAGS_SOP;
4432 
4433 	ring = &rxr->rx_ring_struct;
4434 	bnxt_init_rxbd_pages(ring, type);
4435 	ring->fw_ring_id = INVALID_HW_RING_ID;
4436 }
4437 
4438 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4439 					   struct bnxt_rx_ring_info *rxr)
4440 {
4441 	struct bnxt_ring_struct *ring;
4442 	u32 type;
4443 
4444 	ring = &rxr->rx_agg_ring_struct;
4445 	ring->fw_ring_id = INVALID_HW_RING_ID;
4446 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4447 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4448 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4449 
4450 		bnxt_init_rxbd_pages(ring, type);
4451 	}
4452 }
4453 
4454 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4455 {
4456 	struct bnxt_rx_ring_info *rxr;
4457 
4458 	rxr = &bp->rx_ring[ring_nr];
4459 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4460 
4461 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4462 			     &rxr->bnapi->napi);
4463 
4464 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4465 		bpf_prog_add(bp->xdp_prog, 1);
4466 		rxr->xdp_prog = bp->xdp_prog;
4467 	}
4468 
4469 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4470 
4471 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4472 }
4473 
4474 static void bnxt_init_cp_rings(struct bnxt *bp)
4475 {
4476 	int i, j;
4477 
4478 	for (i = 0; i < bp->cp_nr_rings; i++) {
4479 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4480 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4481 
4482 		ring->fw_ring_id = INVALID_HW_RING_ID;
4483 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4484 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4485 		if (!cpr->cp_ring_arr)
4486 			continue;
4487 		for (j = 0; j < cpr->cp_ring_count; j++) {
4488 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4489 
4490 			ring = &cpr2->cp_ring_struct;
4491 			ring->fw_ring_id = INVALID_HW_RING_ID;
4492 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4493 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4494 		}
4495 	}
4496 }
4497 
4498 static int bnxt_init_rx_rings(struct bnxt *bp)
4499 {
4500 	int i, rc = 0;
4501 
4502 	if (BNXT_RX_PAGE_MODE(bp)) {
4503 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4504 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4505 	} else {
4506 		bp->rx_offset = BNXT_RX_OFFSET;
4507 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4508 	}
4509 
4510 	for (i = 0; i < bp->rx_nr_rings; i++) {
4511 		rc = bnxt_init_one_rx_ring(bp, i);
4512 		if (rc)
4513 			break;
4514 	}
4515 
4516 	return rc;
4517 }
4518 
4519 static int bnxt_init_tx_rings(struct bnxt *bp)
4520 {
4521 	u16 i;
4522 
4523 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4524 				   BNXT_MIN_TX_DESC_CNT);
4525 
4526 	for (i = 0; i < bp->tx_nr_rings; i++) {
4527 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4528 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4529 
4530 		ring->fw_ring_id = INVALID_HW_RING_ID;
4531 
4532 		if (i >= bp->tx_nr_rings_xdp)
4533 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4534 					     NETDEV_QUEUE_TYPE_TX,
4535 					     &txr->bnapi->napi);
4536 	}
4537 
4538 	return 0;
4539 }
4540 
4541 static void bnxt_free_ring_grps(struct bnxt *bp)
4542 {
4543 	kfree(bp->grp_info);
4544 	bp->grp_info = NULL;
4545 }
4546 
4547 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4548 {
4549 	int i;
4550 
4551 	if (irq_re_init) {
4552 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4553 				       sizeof(struct bnxt_ring_grp_info),
4554 				       GFP_KERNEL);
4555 		if (!bp->grp_info)
4556 			return -ENOMEM;
4557 	}
4558 	for (i = 0; i < bp->cp_nr_rings; i++) {
4559 		if (irq_re_init)
4560 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4561 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4562 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4563 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4564 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4565 	}
4566 	return 0;
4567 }
4568 
4569 static void bnxt_free_vnics(struct bnxt *bp)
4570 {
4571 	kfree(bp->vnic_info);
4572 	bp->vnic_info = NULL;
4573 	bp->nr_vnics = 0;
4574 }
4575 
4576 static int bnxt_alloc_vnics(struct bnxt *bp)
4577 {
4578 	int num_vnics = 1;
4579 
4580 #ifdef CONFIG_RFS_ACCEL
4581 	if (bp->flags & BNXT_FLAG_RFS) {
4582 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4583 			num_vnics++;
4584 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4585 			num_vnics += bp->rx_nr_rings;
4586 	}
4587 #endif
4588 
4589 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4590 		num_vnics++;
4591 
4592 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4593 				GFP_KERNEL);
4594 	if (!bp->vnic_info)
4595 		return -ENOMEM;
4596 
4597 	bp->nr_vnics = num_vnics;
4598 	return 0;
4599 }
4600 
4601 static void bnxt_init_vnics(struct bnxt *bp)
4602 {
4603 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4604 	int i;
4605 
4606 	for (i = 0; i < bp->nr_vnics; i++) {
4607 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4608 		int j;
4609 
4610 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4611 		vnic->vnic_id = i;
4612 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4613 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4614 
4615 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4616 
4617 		if (bp->vnic_info[i].rss_hash_key) {
4618 			if (i == BNXT_VNIC_DEFAULT) {
4619 				u8 *key = (void *)vnic->rss_hash_key;
4620 				int k;
4621 
4622 				if (!bp->rss_hash_key_valid &&
4623 				    !bp->rss_hash_key_updated) {
4624 					get_random_bytes(bp->rss_hash_key,
4625 							 HW_HASH_KEY_SIZE);
4626 					bp->rss_hash_key_updated = true;
4627 				}
4628 
4629 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4630 				       HW_HASH_KEY_SIZE);
4631 
4632 				if (!bp->rss_hash_key_updated)
4633 					continue;
4634 
4635 				bp->rss_hash_key_updated = false;
4636 				bp->rss_hash_key_valid = true;
4637 
4638 				bp->toeplitz_prefix = 0;
4639 				for (k = 0; k < 8; k++) {
4640 					bp->toeplitz_prefix <<= 8;
4641 					bp->toeplitz_prefix |= key[k];
4642 				}
4643 			} else {
4644 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4645 				       HW_HASH_KEY_SIZE);
4646 			}
4647 		}
4648 	}
4649 }
4650 
4651 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4652 {
4653 	int pages;
4654 
4655 	pages = ring_size / desc_per_pg;
4656 
4657 	if (!pages)
4658 		return 1;
4659 
4660 	pages++;
4661 
4662 	while (pages & (pages - 1))
4663 		pages++;
4664 
4665 	return pages;
4666 }
4667 
4668 void bnxt_set_tpa_flags(struct bnxt *bp)
4669 {
4670 	bp->flags &= ~BNXT_FLAG_TPA;
4671 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4672 		return;
4673 	if (bp->dev->features & NETIF_F_LRO)
4674 		bp->flags |= BNXT_FLAG_LRO;
4675 	else if (bp->dev->features & NETIF_F_GRO_HW)
4676 		bp->flags |= BNXT_FLAG_GRO;
4677 }
4678 
4679 static void bnxt_init_ring_params(struct bnxt *bp)
4680 {
4681 	unsigned int rx_size;
4682 
4683 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4684 	/* Try to fit 4 chunks into a 4k page */
4685 	rx_size = SZ_1K -
4686 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4687 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4688 }
4689 
4690 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4691  * be set on entry.
4692  */
4693 void bnxt_set_ring_params(struct bnxt *bp)
4694 {
4695 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4696 	u32 agg_factor = 0, agg_ring_size = 0;
4697 
4698 	/* 8 for CRC and VLAN */
4699 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4700 
4701 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4702 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4703 
4704 	ring_size = bp->rx_ring_size;
4705 	bp->rx_agg_ring_size = 0;
4706 	bp->rx_agg_nr_pages = 0;
4707 
4708 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4709 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4710 
4711 	bp->flags &= ~BNXT_FLAG_JUMBO;
4712 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4713 		u32 jumbo_factor;
4714 
4715 		bp->flags |= BNXT_FLAG_JUMBO;
4716 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4717 		if (jumbo_factor > agg_factor)
4718 			agg_factor = jumbo_factor;
4719 	}
4720 	if (agg_factor) {
4721 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4722 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4723 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4724 				    bp->rx_ring_size, ring_size);
4725 			bp->rx_ring_size = ring_size;
4726 		}
4727 		agg_ring_size = ring_size * agg_factor;
4728 
4729 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4730 							RX_DESC_CNT);
4731 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4732 			u32 tmp = agg_ring_size;
4733 
4734 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4735 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4736 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4737 				    tmp, agg_ring_size);
4738 		}
4739 		bp->rx_agg_ring_size = agg_ring_size;
4740 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4741 
4742 		if (BNXT_RX_PAGE_MODE(bp)) {
4743 			rx_space = PAGE_SIZE;
4744 			rx_size = PAGE_SIZE -
4745 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4746 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4747 		} else {
4748 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4749 				       bp->rx_copybreak,
4750 				       bp->dev->cfg_pending->hds_thresh);
4751 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4752 			rx_space = rx_size + NET_SKB_PAD +
4753 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4754 		}
4755 	}
4756 
4757 	bp->rx_buf_use_size = rx_size;
4758 	bp->rx_buf_size = rx_space;
4759 
4760 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4761 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4762 
4763 	ring_size = bp->tx_ring_size;
4764 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4765 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4766 
4767 	max_rx_cmpl = bp->rx_ring_size;
4768 	/* MAX TPA needs to be added because TPA_START completions are
4769 	 * immediately recycled, so the TPA completions are not bound by
4770 	 * the RX ring size.
4771 	 */
4772 	if (bp->flags & BNXT_FLAG_TPA)
4773 		max_rx_cmpl += bp->max_tpa;
4774 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4775 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4776 	bp->cp_ring_size = ring_size;
4777 
4778 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4779 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4780 		bp->cp_nr_pages = MAX_CP_PAGES;
4781 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4782 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4783 			    ring_size, bp->cp_ring_size);
4784 	}
4785 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4786 	bp->cp_ring_mask = bp->cp_bit - 1;
4787 }
4788 
4789 /* Changing allocation mode of RX rings.
4790  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4791  */
4792 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4793 {
4794 	struct net_device *dev = bp->dev;
4795 
4796 	if (page_mode) {
4797 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4798 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4799 
4800 		if (bp->xdp_prog->aux->xdp_has_frags)
4801 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4802 		else
4803 			dev->max_mtu =
4804 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4805 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4806 			bp->flags |= BNXT_FLAG_JUMBO;
4807 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4808 		} else {
4809 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4810 			bp->rx_skb_func = bnxt_rx_page_skb;
4811 		}
4812 		bp->rx_dir = DMA_BIDIRECTIONAL;
4813 	} else {
4814 		dev->max_mtu = bp->max_mtu;
4815 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4816 		bp->rx_dir = DMA_FROM_DEVICE;
4817 		bp->rx_skb_func = bnxt_rx_skb;
4818 	}
4819 }
4820 
4821 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4822 {
4823 	__bnxt_set_rx_skb_mode(bp, page_mode);
4824 
4825 	if (!page_mode) {
4826 		int rx, tx;
4827 
4828 		bnxt_get_max_rings(bp, &rx, &tx, true);
4829 		if (rx > 1) {
4830 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4831 			bp->dev->hw_features |= NETIF_F_LRO;
4832 		}
4833 	}
4834 
4835 	/* Update LRO and GRO_HW availability */
4836 	netdev_update_features(bp->dev);
4837 }
4838 
4839 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4840 {
4841 	int i;
4842 	struct bnxt_vnic_info *vnic;
4843 	struct pci_dev *pdev = bp->pdev;
4844 
4845 	if (!bp->vnic_info)
4846 		return;
4847 
4848 	for (i = 0; i < bp->nr_vnics; i++) {
4849 		vnic = &bp->vnic_info[i];
4850 
4851 		kfree(vnic->fw_grp_ids);
4852 		vnic->fw_grp_ids = NULL;
4853 
4854 		kfree(vnic->uc_list);
4855 		vnic->uc_list = NULL;
4856 
4857 		if (vnic->mc_list) {
4858 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4859 					  vnic->mc_list, vnic->mc_list_mapping);
4860 			vnic->mc_list = NULL;
4861 		}
4862 
4863 		if (vnic->rss_table) {
4864 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4865 					  vnic->rss_table,
4866 					  vnic->rss_table_dma_addr);
4867 			vnic->rss_table = NULL;
4868 		}
4869 
4870 		vnic->rss_hash_key = NULL;
4871 		vnic->flags = 0;
4872 	}
4873 }
4874 
4875 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4876 {
4877 	int i, rc = 0, size;
4878 	struct bnxt_vnic_info *vnic;
4879 	struct pci_dev *pdev = bp->pdev;
4880 	int max_rings;
4881 
4882 	for (i = 0; i < bp->nr_vnics; i++) {
4883 		vnic = &bp->vnic_info[i];
4884 
4885 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4886 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4887 
4888 			if (mem_size > 0) {
4889 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4890 				if (!vnic->uc_list) {
4891 					rc = -ENOMEM;
4892 					goto out;
4893 				}
4894 			}
4895 		}
4896 
4897 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4898 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4899 			vnic->mc_list =
4900 				dma_alloc_coherent(&pdev->dev,
4901 						   vnic->mc_list_size,
4902 						   &vnic->mc_list_mapping,
4903 						   GFP_KERNEL);
4904 			if (!vnic->mc_list) {
4905 				rc = -ENOMEM;
4906 				goto out;
4907 			}
4908 		}
4909 
4910 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4911 			goto vnic_skip_grps;
4912 
4913 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4914 			max_rings = bp->rx_nr_rings;
4915 		else
4916 			max_rings = 1;
4917 
4918 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4919 		if (!vnic->fw_grp_ids) {
4920 			rc = -ENOMEM;
4921 			goto out;
4922 		}
4923 vnic_skip_grps:
4924 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4925 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4926 			continue;
4927 
4928 		/* Allocate rss table and hash key */
4929 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4930 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4931 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4932 
4933 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4934 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4935 						     vnic->rss_table_size,
4936 						     &vnic->rss_table_dma_addr,
4937 						     GFP_KERNEL);
4938 		if (!vnic->rss_table) {
4939 			rc = -ENOMEM;
4940 			goto out;
4941 		}
4942 
4943 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4944 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4945 	}
4946 	return 0;
4947 
4948 out:
4949 	return rc;
4950 }
4951 
4952 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4953 {
4954 	struct bnxt_hwrm_wait_token *token;
4955 
4956 	dma_pool_destroy(bp->hwrm_dma_pool);
4957 	bp->hwrm_dma_pool = NULL;
4958 
4959 	rcu_read_lock();
4960 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4961 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4962 	rcu_read_unlock();
4963 }
4964 
4965 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4966 {
4967 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4968 					    BNXT_HWRM_DMA_SIZE,
4969 					    BNXT_HWRM_DMA_ALIGN, 0);
4970 	if (!bp->hwrm_dma_pool)
4971 		return -ENOMEM;
4972 
4973 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4974 
4975 	return 0;
4976 }
4977 
4978 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4979 {
4980 	kfree(stats->hw_masks);
4981 	stats->hw_masks = NULL;
4982 	kfree(stats->sw_stats);
4983 	stats->sw_stats = NULL;
4984 	if (stats->hw_stats) {
4985 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4986 				  stats->hw_stats_map);
4987 		stats->hw_stats = NULL;
4988 	}
4989 }
4990 
4991 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4992 				bool alloc_masks)
4993 {
4994 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4995 					     &stats->hw_stats_map, GFP_KERNEL);
4996 	if (!stats->hw_stats)
4997 		return -ENOMEM;
4998 
4999 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
5000 	if (!stats->sw_stats)
5001 		goto stats_mem_err;
5002 
5003 	if (alloc_masks) {
5004 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
5005 		if (!stats->hw_masks)
5006 			goto stats_mem_err;
5007 	}
5008 	return 0;
5009 
5010 stats_mem_err:
5011 	bnxt_free_stats_mem(bp, stats);
5012 	return -ENOMEM;
5013 }
5014 
5015 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
5016 {
5017 	int i;
5018 
5019 	for (i = 0; i < count; i++)
5020 		mask_arr[i] = mask;
5021 }
5022 
5023 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
5024 {
5025 	int i;
5026 
5027 	for (i = 0; i < count; i++)
5028 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
5029 }
5030 
5031 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
5032 				    struct bnxt_stats_mem *stats)
5033 {
5034 	struct hwrm_func_qstats_ext_output *resp;
5035 	struct hwrm_func_qstats_ext_input *req;
5036 	__le64 *hw_masks;
5037 	int rc;
5038 
5039 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
5040 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5041 		return -EOPNOTSUPP;
5042 
5043 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
5044 	if (rc)
5045 		return rc;
5046 
5047 	req->fid = cpu_to_le16(0xffff);
5048 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5049 
5050 	resp = hwrm_req_hold(bp, req);
5051 	rc = hwrm_req_send(bp, req);
5052 	if (!rc) {
5053 		hw_masks = &resp->rx_ucast_pkts;
5054 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
5055 	}
5056 	hwrm_req_drop(bp, req);
5057 	return rc;
5058 }
5059 
5060 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
5061 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
5062 
5063 static void bnxt_init_stats(struct bnxt *bp)
5064 {
5065 	struct bnxt_napi *bnapi = bp->bnapi[0];
5066 	struct bnxt_cp_ring_info *cpr;
5067 	struct bnxt_stats_mem *stats;
5068 	__le64 *rx_stats, *tx_stats;
5069 	int rc, rx_count, tx_count;
5070 	u64 *rx_masks, *tx_masks;
5071 	u64 mask;
5072 	u8 flags;
5073 
5074 	cpr = &bnapi->cp_ring;
5075 	stats = &cpr->stats;
5076 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5077 	if (rc) {
5078 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5079 			mask = (1ULL << 48) - 1;
5080 		else
5081 			mask = -1ULL;
5082 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5083 	}
5084 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5085 		stats = &bp->port_stats;
5086 		rx_stats = stats->hw_stats;
5087 		rx_masks = stats->hw_masks;
5088 		rx_count = sizeof(struct rx_port_stats) / 8;
5089 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5090 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5091 		tx_count = sizeof(struct tx_port_stats) / 8;
5092 
5093 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5094 		rc = bnxt_hwrm_port_qstats(bp, flags);
5095 		if (rc) {
5096 			mask = (1ULL << 40) - 1;
5097 
5098 			bnxt_fill_masks(rx_masks, mask, rx_count);
5099 			bnxt_fill_masks(tx_masks, mask, tx_count);
5100 		} else {
5101 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5102 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5103 			bnxt_hwrm_port_qstats(bp, 0);
5104 		}
5105 	}
5106 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5107 		stats = &bp->rx_port_stats_ext;
5108 		rx_stats = stats->hw_stats;
5109 		rx_masks = stats->hw_masks;
5110 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5111 		stats = &bp->tx_port_stats_ext;
5112 		tx_stats = stats->hw_stats;
5113 		tx_masks = stats->hw_masks;
5114 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5115 
5116 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5117 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5118 		if (rc) {
5119 			mask = (1ULL << 40) - 1;
5120 
5121 			bnxt_fill_masks(rx_masks, mask, rx_count);
5122 			if (tx_stats)
5123 				bnxt_fill_masks(tx_masks, mask, tx_count);
5124 		} else {
5125 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5126 			if (tx_stats)
5127 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5128 						   tx_count);
5129 			bnxt_hwrm_port_qstats_ext(bp, 0);
5130 		}
5131 	}
5132 }
5133 
5134 static void bnxt_free_port_stats(struct bnxt *bp)
5135 {
5136 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5137 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5138 
5139 	bnxt_free_stats_mem(bp, &bp->port_stats);
5140 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5141 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5142 }
5143 
5144 static void bnxt_free_ring_stats(struct bnxt *bp)
5145 {
5146 	int i;
5147 
5148 	if (!bp->bnapi)
5149 		return;
5150 
5151 	for (i = 0; i < bp->cp_nr_rings; i++) {
5152 		struct bnxt_napi *bnapi = bp->bnapi[i];
5153 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5154 
5155 		bnxt_free_stats_mem(bp, &cpr->stats);
5156 
5157 		kfree(cpr->sw_stats);
5158 		cpr->sw_stats = NULL;
5159 	}
5160 }
5161 
5162 static int bnxt_alloc_stats(struct bnxt *bp)
5163 {
5164 	u32 size, i;
5165 	int rc;
5166 
5167 	size = bp->hw_ring_stats_size;
5168 
5169 	for (i = 0; i < bp->cp_nr_rings; i++) {
5170 		struct bnxt_napi *bnapi = bp->bnapi[i];
5171 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5172 
5173 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5174 		if (!cpr->sw_stats)
5175 			return -ENOMEM;
5176 
5177 		cpr->stats.len = size;
5178 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5179 		if (rc)
5180 			return rc;
5181 
5182 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5183 	}
5184 
5185 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5186 		return 0;
5187 
5188 	if (bp->port_stats.hw_stats)
5189 		goto alloc_ext_stats;
5190 
5191 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5192 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5193 	if (rc)
5194 		return rc;
5195 
5196 	bp->flags |= BNXT_FLAG_PORT_STATS;
5197 
5198 alloc_ext_stats:
5199 	/* Display extended statistics only if FW supports it */
5200 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5201 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5202 			return 0;
5203 
5204 	if (bp->rx_port_stats_ext.hw_stats)
5205 		goto alloc_tx_ext_stats;
5206 
5207 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5208 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5209 	/* Extended stats are optional */
5210 	if (rc)
5211 		return 0;
5212 
5213 alloc_tx_ext_stats:
5214 	if (bp->tx_port_stats_ext.hw_stats)
5215 		return 0;
5216 
5217 	if (bp->hwrm_spec_code >= 0x10902 ||
5218 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5219 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5220 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5221 		/* Extended stats are optional */
5222 		if (rc)
5223 			return 0;
5224 	}
5225 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5226 	return 0;
5227 }
5228 
5229 static void bnxt_clear_ring_indices(struct bnxt *bp)
5230 {
5231 	int i, j;
5232 
5233 	if (!bp->bnapi)
5234 		return;
5235 
5236 	for (i = 0; i < bp->cp_nr_rings; i++) {
5237 		struct bnxt_napi *bnapi = bp->bnapi[i];
5238 		struct bnxt_cp_ring_info *cpr;
5239 		struct bnxt_rx_ring_info *rxr;
5240 		struct bnxt_tx_ring_info *txr;
5241 
5242 		if (!bnapi)
5243 			continue;
5244 
5245 		cpr = &bnapi->cp_ring;
5246 		cpr->cp_raw_cons = 0;
5247 
5248 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5249 			txr->tx_prod = 0;
5250 			txr->tx_cons = 0;
5251 			txr->tx_hw_cons = 0;
5252 		}
5253 
5254 		rxr = bnapi->rx_ring;
5255 		if (rxr) {
5256 			rxr->rx_prod = 0;
5257 			rxr->rx_agg_prod = 0;
5258 			rxr->rx_sw_agg_prod = 0;
5259 			rxr->rx_next_cons = 0;
5260 		}
5261 		bnapi->events = 0;
5262 	}
5263 }
5264 
5265 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5266 {
5267 	u8 type = fltr->type, flags = fltr->flags;
5268 
5269 	INIT_LIST_HEAD(&fltr->list);
5270 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5271 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5272 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5273 }
5274 
5275 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5276 {
5277 	if (!list_empty(&fltr->list))
5278 		list_del_init(&fltr->list);
5279 }
5280 
5281 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5282 {
5283 	struct bnxt_filter_base *usr_fltr, *tmp;
5284 
5285 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5286 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5287 			continue;
5288 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5289 	}
5290 }
5291 
5292 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5293 {
5294 	hlist_del(&fltr->hash);
5295 	bnxt_del_one_usr_fltr(bp, fltr);
5296 	if (fltr->flags) {
5297 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5298 		bp->ntp_fltr_count--;
5299 	}
5300 	kfree(fltr);
5301 }
5302 
5303 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5304 {
5305 	int i;
5306 
5307 	netdev_assert_locked(bp->dev);
5308 
5309 	/* Under netdev instance lock and all our NAPIs have been disabled.
5310 	 * It's safe to delete the hash table.
5311 	 */
5312 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5313 		struct hlist_head *head;
5314 		struct hlist_node *tmp;
5315 		struct bnxt_ntuple_filter *fltr;
5316 
5317 		head = &bp->ntp_fltr_hash_tbl[i];
5318 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5319 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5320 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5321 				     !list_empty(&fltr->base.list)))
5322 				continue;
5323 			bnxt_del_fltr(bp, &fltr->base);
5324 		}
5325 	}
5326 	if (!all)
5327 		return;
5328 
5329 	bitmap_free(bp->ntp_fltr_bmap);
5330 	bp->ntp_fltr_bmap = NULL;
5331 	bp->ntp_fltr_count = 0;
5332 }
5333 
5334 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5335 {
5336 	int i, rc = 0;
5337 
5338 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5339 		return 0;
5340 
5341 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5342 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5343 
5344 	bp->ntp_fltr_count = 0;
5345 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5346 
5347 	if (!bp->ntp_fltr_bmap)
5348 		rc = -ENOMEM;
5349 
5350 	return rc;
5351 }
5352 
5353 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5354 {
5355 	int i;
5356 
5357 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5358 		struct hlist_head *head;
5359 		struct hlist_node *tmp;
5360 		struct bnxt_l2_filter *fltr;
5361 
5362 		head = &bp->l2_fltr_hash_tbl[i];
5363 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5364 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5365 				     !list_empty(&fltr->base.list)))
5366 				continue;
5367 			bnxt_del_fltr(bp, &fltr->base);
5368 		}
5369 	}
5370 }
5371 
5372 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5373 {
5374 	int i;
5375 
5376 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5377 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5378 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5379 }
5380 
5381 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5382 {
5383 	bnxt_free_vnic_attributes(bp);
5384 	bnxt_free_tx_rings(bp);
5385 	bnxt_free_rx_rings(bp);
5386 	bnxt_free_cp_rings(bp);
5387 	bnxt_free_all_cp_arrays(bp);
5388 	bnxt_free_ntp_fltrs(bp, false);
5389 	bnxt_free_l2_filters(bp, false);
5390 	if (irq_re_init) {
5391 		bnxt_free_ring_stats(bp);
5392 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5393 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5394 			bnxt_free_port_stats(bp);
5395 		bnxt_free_ring_grps(bp);
5396 		bnxt_free_vnics(bp);
5397 		kfree(bp->tx_ring_map);
5398 		bp->tx_ring_map = NULL;
5399 		kfree(bp->tx_ring);
5400 		bp->tx_ring = NULL;
5401 		kfree(bp->rx_ring);
5402 		bp->rx_ring = NULL;
5403 		kfree(bp->bnapi);
5404 		bp->bnapi = NULL;
5405 	} else {
5406 		bnxt_clear_ring_indices(bp);
5407 	}
5408 }
5409 
5410 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5411 {
5412 	int i, j, rc, size, arr_size;
5413 	void *bnapi;
5414 
5415 	if (irq_re_init) {
5416 		/* Allocate bnapi mem pointer array and mem block for
5417 		 * all queues
5418 		 */
5419 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5420 				bp->cp_nr_rings);
5421 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5422 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5423 		if (!bnapi)
5424 			return -ENOMEM;
5425 
5426 		bp->bnapi = bnapi;
5427 		bnapi += arr_size;
5428 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5429 			bp->bnapi[i] = bnapi;
5430 			bp->bnapi[i]->index = i;
5431 			bp->bnapi[i]->bp = bp;
5432 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5433 				struct bnxt_cp_ring_info *cpr =
5434 					&bp->bnapi[i]->cp_ring;
5435 
5436 				cpr->cp_ring_struct.ring_mem.flags =
5437 					BNXT_RMEM_RING_PTE_FLAG;
5438 			}
5439 		}
5440 
5441 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5442 				      sizeof(struct bnxt_rx_ring_info),
5443 				      GFP_KERNEL);
5444 		if (!bp->rx_ring)
5445 			return -ENOMEM;
5446 
5447 		for (i = 0; i < bp->rx_nr_rings; i++) {
5448 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5449 
5450 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5451 				rxr->rx_ring_struct.ring_mem.flags =
5452 					BNXT_RMEM_RING_PTE_FLAG;
5453 				rxr->rx_agg_ring_struct.ring_mem.flags =
5454 					BNXT_RMEM_RING_PTE_FLAG;
5455 			} else {
5456 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5457 			}
5458 			rxr->bnapi = bp->bnapi[i];
5459 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5460 		}
5461 
5462 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5463 				      sizeof(struct bnxt_tx_ring_info),
5464 				      GFP_KERNEL);
5465 		if (!bp->tx_ring)
5466 			return -ENOMEM;
5467 
5468 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5469 					  GFP_KERNEL);
5470 
5471 		if (!bp->tx_ring_map)
5472 			return -ENOMEM;
5473 
5474 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5475 			j = 0;
5476 		else
5477 			j = bp->rx_nr_rings;
5478 
5479 		for (i = 0; i < bp->tx_nr_rings; i++) {
5480 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5481 			struct bnxt_napi *bnapi2;
5482 
5483 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5484 				txr->tx_ring_struct.ring_mem.flags =
5485 					BNXT_RMEM_RING_PTE_FLAG;
5486 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5487 			if (i >= bp->tx_nr_rings_xdp) {
5488 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5489 
5490 				bnapi2 = bp->bnapi[k];
5491 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5492 				txr->tx_napi_idx =
5493 					BNXT_RING_TO_TC(bp, txr->txq_index);
5494 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5495 				bnapi2->tx_int = bnxt_tx_int;
5496 			} else {
5497 				bnapi2 = bp->bnapi[j];
5498 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5499 				bnapi2->tx_ring[0] = txr;
5500 				bnapi2->tx_int = bnxt_tx_int_xdp;
5501 				j++;
5502 			}
5503 			txr->bnapi = bnapi2;
5504 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5505 				txr->tx_cpr = &bnapi2->cp_ring;
5506 		}
5507 
5508 		rc = bnxt_alloc_stats(bp);
5509 		if (rc)
5510 			goto alloc_mem_err;
5511 		bnxt_init_stats(bp);
5512 
5513 		rc = bnxt_alloc_ntp_fltrs(bp);
5514 		if (rc)
5515 			goto alloc_mem_err;
5516 
5517 		rc = bnxt_alloc_vnics(bp);
5518 		if (rc)
5519 			goto alloc_mem_err;
5520 	}
5521 
5522 	rc = bnxt_alloc_all_cp_arrays(bp);
5523 	if (rc)
5524 		goto alloc_mem_err;
5525 
5526 	bnxt_init_ring_struct(bp);
5527 
5528 	rc = bnxt_alloc_rx_rings(bp);
5529 	if (rc)
5530 		goto alloc_mem_err;
5531 
5532 	rc = bnxt_alloc_tx_rings(bp);
5533 	if (rc)
5534 		goto alloc_mem_err;
5535 
5536 	rc = bnxt_alloc_cp_rings(bp);
5537 	if (rc)
5538 		goto alloc_mem_err;
5539 
5540 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5541 						  BNXT_VNIC_MCAST_FLAG |
5542 						  BNXT_VNIC_UCAST_FLAG;
5543 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5544 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5545 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5546 
5547 	rc = bnxt_alloc_vnic_attributes(bp);
5548 	if (rc)
5549 		goto alloc_mem_err;
5550 	return 0;
5551 
5552 alloc_mem_err:
5553 	bnxt_free_mem(bp, true);
5554 	return rc;
5555 }
5556 
5557 static void bnxt_disable_int(struct bnxt *bp)
5558 {
5559 	int i;
5560 
5561 	if (!bp->bnapi)
5562 		return;
5563 
5564 	for (i = 0; i < bp->cp_nr_rings; i++) {
5565 		struct bnxt_napi *bnapi = bp->bnapi[i];
5566 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5567 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5568 
5569 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5570 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5571 	}
5572 }
5573 
5574 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5575 {
5576 	struct bnxt_napi *bnapi = bp->bnapi[n];
5577 	struct bnxt_cp_ring_info *cpr;
5578 
5579 	cpr = &bnapi->cp_ring;
5580 	return cpr->cp_ring_struct.map_idx;
5581 }
5582 
5583 static void bnxt_disable_int_sync(struct bnxt *bp)
5584 {
5585 	int i;
5586 
5587 	if (!bp->irq_tbl)
5588 		return;
5589 
5590 	atomic_inc(&bp->intr_sem);
5591 
5592 	bnxt_disable_int(bp);
5593 	for (i = 0; i < bp->cp_nr_rings; i++) {
5594 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5595 
5596 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5597 	}
5598 }
5599 
5600 static void bnxt_enable_int(struct bnxt *bp)
5601 {
5602 	int i;
5603 
5604 	atomic_set(&bp->intr_sem, 0);
5605 	for (i = 0; i < bp->cp_nr_rings; i++) {
5606 		struct bnxt_napi *bnapi = bp->bnapi[i];
5607 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5608 
5609 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5610 	}
5611 }
5612 
5613 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5614 			    bool async_only)
5615 {
5616 	DECLARE_BITMAP(async_events_bmap, 256);
5617 	u32 *events = (u32 *)async_events_bmap;
5618 	struct hwrm_func_drv_rgtr_output *resp;
5619 	struct hwrm_func_drv_rgtr_input *req;
5620 	u32 flags;
5621 	int rc, i;
5622 
5623 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5624 	if (rc)
5625 		return rc;
5626 
5627 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5628 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5629 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5630 
5631 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5632 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5633 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5634 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5635 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5636 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5637 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5638 	if (bp->fw_cap & BNXT_FW_CAP_NPAR_1_2)
5639 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_NPAR_1_2_SUPPORT;
5640 	req->flags = cpu_to_le32(flags);
5641 	req->ver_maj_8b = DRV_VER_MAJ;
5642 	req->ver_min_8b = DRV_VER_MIN;
5643 	req->ver_upd_8b = DRV_VER_UPD;
5644 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5645 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5646 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5647 
5648 	if (BNXT_PF(bp)) {
5649 		u32 data[8];
5650 		int i;
5651 
5652 		memset(data, 0, sizeof(data));
5653 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5654 			u16 cmd = bnxt_vf_req_snif[i];
5655 			unsigned int bit, idx;
5656 
5657 			idx = cmd / 32;
5658 			bit = cmd % 32;
5659 			data[idx] |= 1 << bit;
5660 		}
5661 
5662 		for (i = 0; i < 8; i++)
5663 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5664 
5665 		req->enables |=
5666 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5667 	}
5668 
5669 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5670 		req->flags |= cpu_to_le32(
5671 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5672 
5673 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5674 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5675 		u16 event_id = bnxt_async_events_arr[i];
5676 
5677 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5678 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5679 			continue;
5680 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5681 		    !bp->ptp_cfg)
5682 			continue;
5683 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5684 	}
5685 	if (bmap && bmap_size) {
5686 		for (i = 0; i < bmap_size; i++) {
5687 			if (test_bit(i, bmap))
5688 				__set_bit(i, async_events_bmap);
5689 		}
5690 	}
5691 	for (i = 0; i < 8; i++)
5692 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5693 
5694 	if (async_only)
5695 		req->enables =
5696 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5697 
5698 	resp = hwrm_req_hold(bp, req);
5699 	rc = hwrm_req_send(bp, req);
5700 	if (!rc) {
5701 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5702 		if (resp->flags &
5703 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5704 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5705 	}
5706 	hwrm_req_drop(bp, req);
5707 	return rc;
5708 }
5709 
5710 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5711 {
5712 	struct hwrm_func_drv_unrgtr_input *req;
5713 	int rc;
5714 
5715 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5716 		return 0;
5717 
5718 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5719 	if (rc)
5720 		return rc;
5721 	return hwrm_req_send(bp, req);
5722 }
5723 
5724 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5725 
5726 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5727 {
5728 	struct hwrm_tunnel_dst_port_free_input *req;
5729 	int rc;
5730 
5731 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5732 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5733 		return 0;
5734 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5735 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5736 		return 0;
5737 
5738 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5739 	if (rc)
5740 		return rc;
5741 
5742 	req->tunnel_type = tunnel_type;
5743 
5744 	switch (tunnel_type) {
5745 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5746 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5747 		bp->vxlan_port = 0;
5748 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5749 		break;
5750 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5751 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5752 		bp->nge_port = 0;
5753 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5754 		break;
5755 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5756 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5757 		bp->vxlan_gpe_port = 0;
5758 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5759 		break;
5760 	default:
5761 		break;
5762 	}
5763 
5764 	rc = hwrm_req_send(bp, req);
5765 	if (rc)
5766 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5767 			   rc);
5768 	if (bp->flags & BNXT_FLAG_TPA)
5769 		bnxt_set_tpa(bp, true);
5770 	return rc;
5771 }
5772 
5773 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5774 					   u8 tunnel_type)
5775 {
5776 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5777 	struct hwrm_tunnel_dst_port_alloc_input *req;
5778 	int rc;
5779 
5780 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5781 	if (rc)
5782 		return rc;
5783 
5784 	req->tunnel_type = tunnel_type;
5785 	req->tunnel_dst_port_val = port;
5786 
5787 	resp = hwrm_req_hold(bp, req);
5788 	rc = hwrm_req_send(bp, req);
5789 	if (rc) {
5790 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5791 			   rc);
5792 		goto err_out;
5793 	}
5794 
5795 	switch (tunnel_type) {
5796 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5797 		bp->vxlan_port = port;
5798 		bp->vxlan_fw_dst_port_id =
5799 			le16_to_cpu(resp->tunnel_dst_port_id);
5800 		break;
5801 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5802 		bp->nge_port = port;
5803 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5804 		break;
5805 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5806 		bp->vxlan_gpe_port = port;
5807 		bp->vxlan_gpe_fw_dst_port_id =
5808 			le16_to_cpu(resp->tunnel_dst_port_id);
5809 		break;
5810 	default:
5811 		break;
5812 	}
5813 	if (bp->flags & BNXT_FLAG_TPA)
5814 		bnxt_set_tpa(bp, true);
5815 
5816 err_out:
5817 	hwrm_req_drop(bp, req);
5818 	return rc;
5819 }
5820 
5821 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5822 {
5823 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5824 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5825 	int rc;
5826 
5827 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5828 	if (rc)
5829 		return rc;
5830 
5831 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5832 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5833 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5834 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5835 	}
5836 	req->mask = cpu_to_le32(vnic->rx_mask);
5837 	return hwrm_req_send_silent(bp, req);
5838 }
5839 
5840 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5841 {
5842 	if (!atomic_dec_and_test(&fltr->refcnt))
5843 		return;
5844 	spin_lock_bh(&bp->ntp_fltr_lock);
5845 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5846 		spin_unlock_bh(&bp->ntp_fltr_lock);
5847 		return;
5848 	}
5849 	hlist_del_rcu(&fltr->base.hash);
5850 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5851 	if (fltr->base.flags) {
5852 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5853 		bp->ntp_fltr_count--;
5854 	}
5855 	spin_unlock_bh(&bp->ntp_fltr_lock);
5856 	kfree_rcu(fltr, base.rcu);
5857 }
5858 
5859 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5860 						      struct bnxt_l2_key *key,
5861 						      u32 idx)
5862 {
5863 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5864 	struct bnxt_l2_filter *fltr;
5865 
5866 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5867 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5868 
5869 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5870 		    l2_key->vlan == key->vlan)
5871 			return fltr;
5872 	}
5873 	return NULL;
5874 }
5875 
5876 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5877 						    struct bnxt_l2_key *key,
5878 						    u32 idx)
5879 {
5880 	struct bnxt_l2_filter *fltr = NULL;
5881 
5882 	rcu_read_lock();
5883 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5884 	if (fltr)
5885 		atomic_inc(&fltr->refcnt);
5886 	rcu_read_unlock();
5887 	return fltr;
5888 }
5889 
5890 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5891 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5892 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5893 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5894 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5895 
5896 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5897 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5898 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5899 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5900 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5901 
5902 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5903 {
5904 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5905 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5906 			return sizeof(fkeys->addrs.v4addrs) +
5907 			       sizeof(fkeys->ports);
5908 
5909 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5910 			return sizeof(fkeys->addrs.v4addrs);
5911 	}
5912 
5913 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5914 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5915 			return sizeof(fkeys->addrs.v6addrs) +
5916 			       sizeof(fkeys->ports);
5917 
5918 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5919 			return sizeof(fkeys->addrs.v6addrs);
5920 	}
5921 
5922 	return 0;
5923 }
5924 
5925 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5926 			 const unsigned char *key)
5927 {
5928 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5929 	struct bnxt_ipv4_tuple tuple4;
5930 	struct bnxt_ipv6_tuple tuple6;
5931 	int i, j, len = 0;
5932 	u8 *four_tuple;
5933 
5934 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5935 	if (!len)
5936 		return 0;
5937 
5938 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5939 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5940 		tuple4.ports = fkeys->ports;
5941 		four_tuple = (unsigned char *)&tuple4;
5942 	} else {
5943 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5944 		tuple6.ports = fkeys->ports;
5945 		four_tuple = (unsigned char *)&tuple6;
5946 	}
5947 
5948 	for (i = 0, j = 8; i < len; i++, j++) {
5949 		u8 byte = four_tuple[i];
5950 		int bit;
5951 
5952 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5953 			if (byte & 0x80)
5954 				hash ^= prefix;
5955 		}
5956 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5957 	}
5958 
5959 	/* The valid part of the hash is in the upper 32 bits. */
5960 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5961 }
5962 
5963 #ifdef CONFIG_RFS_ACCEL
5964 static struct bnxt_l2_filter *
5965 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5966 {
5967 	struct bnxt_l2_filter *fltr;
5968 	u32 idx;
5969 
5970 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5971 	      BNXT_L2_FLTR_HASH_MASK;
5972 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5973 	return fltr;
5974 }
5975 #endif
5976 
5977 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5978 			       struct bnxt_l2_key *key, u32 idx)
5979 {
5980 	struct hlist_head *head;
5981 
5982 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5983 	fltr->l2_key.vlan = key->vlan;
5984 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5985 	if (fltr->base.flags) {
5986 		int bit_id;
5987 
5988 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5989 						 bp->max_fltr, 0);
5990 		if (bit_id < 0)
5991 			return -ENOMEM;
5992 		fltr->base.sw_id = (u16)bit_id;
5993 		bp->ntp_fltr_count++;
5994 	}
5995 	head = &bp->l2_fltr_hash_tbl[idx];
5996 	hlist_add_head_rcu(&fltr->base.hash, head);
5997 	bnxt_insert_usr_fltr(bp, &fltr->base);
5998 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5999 	atomic_set(&fltr->refcnt, 1);
6000 	return 0;
6001 }
6002 
6003 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
6004 						   struct bnxt_l2_key *key,
6005 						   gfp_t gfp)
6006 {
6007 	struct bnxt_l2_filter *fltr;
6008 	u32 idx;
6009 	int rc;
6010 
6011 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6012 	      BNXT_L2_FLTR_HASH_MASK;
6013 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
6014 	if (fltr)
6015 		return fltr;
6016 
6017 	fltr = kzalloc(sizeof(*fltr), gfp);
6018 	if (!fltr)
6019 		return ERR_PTR(-ENOMEM);
6020 	spin_lock_bh(&bp->ntp_fltr_lock);
6021 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6022 	spin_unlock_bh(&bp->ntp_fltr_lock);
6023 	if (rc) {
6024 		bnxt_del_l2_filter(bp, fltr);
6025 		fltr = ERR_PTR(rc);
6026 	}
6027 	return fltr;
6028 }
6029 
6030 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
6031 						struct bnxt_l2_key *key,
6032 						u16 flags)
6033 {
6034 	struct bnxt_l2_filter *fltr;
6035 	u32 idx;
6036 	int rc;
6037 
6038 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
6039 	      BNXT_L2_FLTR_HASH_MASK;
6040 	spin_lock_bh(&bp->ntp_fltr_lock);
6041 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
6042 	if (fltr) {
6043 		fltr = ERR_PTR(-EEXIST);
6044 		goto l2_filter_exit;
6045 	}
6046 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
6047 	if (!fltr) {
6048 		fltr = ERR_PTR(-ENOMEM);
6049 		goto l2_filter_exit;
6050 	}
6051 	fltr->base.flags = flags;
6052 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
6053 	if (rc) {
6054 		spin_unlock_bh(&bp->ntp_fltr_lock);
6055 		bnxt_del_l2_filter(bp, fltr);
6056 		return ERR_PTR(rc);
6057 	}
6058 
6059 l2_filter_exit:
6060 	spin_unlock_bh(&bp->ntp_fltr_lock);
6061 	return fltr;
6062 }
6063 
6064 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
6065 {
6066 #ifdef CONFIG_BNXT_SRIOV
6067 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
6068 
6069 	return vf->fw_fid;
6070 #else
6071 	return INVALID_HW_RING_ID;
6072 #endif
6073 }
6074 
6075 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6076 {
6077 	struct hwrm_cfa_l2_filter_free_input *req;
6078 	u16 target_id = 0xffff;
6079 	int rc;
6080 
6081 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6082 		struct bnxt_pf_info *pf = &bp->pf;
6083 
6084 		if (fltr->base.vf_idx >= pf->active_vfs)
6085 			return -EINVAL;
6086 
6087 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6088 		if (target_id == INVALID_HW_RING_ID)
6089 			return -EINVAL;
6090 	}
6091 
6092 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6093 	if (rc)
6094 		return rc;
6095 
6096 	req->target_id = cpu_to_le16(target_id);
6097 	req->l2_filter_id = fltr->base.filter_id;
6098 	return hwrm_req_send(bp, req);
6099 }
6100 
6101 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6102 {
6103 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6104 	struct hwrm_cfa_l2_filter_alloc_input *req;
6105 	u16 target_id = 0xffff;
6106 	int rc;
6107 
6108 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6109 		struct bnxt_pf_info *pf = &bp->pf;
6110 
6111 		if (fltr->base.vf_idx >= pf->active_vfs)
6112 			return -EINVAL;
6113 
6114 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6115 	}
6116 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6117 	if (rc)
6118 		return rc;
6119 
6120 	req->target_id = cpu_to_le16(target_id);
6121 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6122 
6123 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6124 		req->flags |=
6125 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6126 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6127 	req->enables =
6128 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6129 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6130 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6131 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6132 	eth_broadcast_addr(req->l2_addr_mask);
6133 
6134 	if (fltr->l2_key.vlan) {
6135 		req->enables |=
6136 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6137 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6138 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6139 		req->num_vlans = 1;
6140 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6141 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6142 	}
6143 
6144 	resp = hwrm_req_hold(bp, req);
6145 	rc = hwrm_req_send(bp, req);
6146 	if (!rc) {
6147 		fltr->base.filter_id = resp->l2_filter_id;
6148 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6149 	}
6150 	hwrm_req_drop(bp, req);
6151 	return rc;
6152 }
6153 
6154 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6155 				     struct bnxt_ntuple_filter *fltr)
6156 {
6157 	struct hwrm_cfa_ntuple_filter_free_input *req;
6158 	int rc;
6159 
6160 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6161 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6162 	if (rc)
6163 		return rc;
6164 
6165 	req->ntuple_filter_id = fltr->base.filter_id;
6166 	return hwrm_req_send(bp, req);
6167 }
6168 
6169 #define BNXT_NTP_FLTR_FLAGS					\
6170 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6171 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6172 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6173 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6174 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6175 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6176 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6177 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6178 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6179 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6180 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6181 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6182 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6183 
6184 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6185 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6186 
6187 void bnxt_fill_ipv6_mask(__be32 mask[4])
6188 {
6189 	int i;
6190 
6191 	for (i = 0; i < 4; i++)
6192 		mask[i] = cpu_to_be32(~0);
6193 }
6194 
6195 static void
6196 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6197 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6198 			  struct bnxt_ntuple_filter *fltr)
6199 {
6200 	u16 rxq = fltr->base.rxq;
6201 
6202 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6203 		struct ethtool_rxfh_context *ctx;
6204 		struct bnxt_rss_ctx *rss_ctx;
6205 		struct bnxt_vnic_info *vnic;
6206 
6207 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6208 			      fltr->base.fw_vnic_id);
6209 		if (ctx) {
6210 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6211 			vnic = &rss_ctx->vnic;
6212 
6213 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6214 		}
6215 		return;
6216 	}
6217 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6218 		struct bnxt_vnic_info *vnic;
6219 		u32 enables;
6220 
6221 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6222 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6223 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6224 		req->enables |= cpu_to_le32(enables);
6225 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6226 	} else {
6227 		u32 flags;
6228 
6229 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6230 		req->flags |= cpu_to_le32(flags);
6231 		req->dst_id = cpu_to_le16(rxq);
6232 	}
6233 }
6234 
6235 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6236 				      struct bnxt_ntuple_filter *fltr)
6237 {
6238 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6239 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6240 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6241 	struct flow_keys *keys = &fltr->fkeys;
6242 	struct bnxt_l2_filter *l2_fltr;
6243 	struct bnxt_vnic_info *vnic;
6244 	int rc;
6245 
6246 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6247 	if (rc)
6248 		return rc;
6249 
6250 	l2_fltr = fltr->l2_fltr;
6251 	req->l2_filter_id = l2_fltr->base.filter_id;
6252 
6253 	if (fltr->base.flags & BNXT_ACT_DROP) {
6254 		req->flags =
6255 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6256 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6257 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6258 	} else {
6259 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6260 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6261 	}
6262 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6263 
6264 	req->ethertype = htons(ETH_P_IP);
6265 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6266 	req->ip_protocol = keys->basic.ip_proto;
6267 
6268 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6269 		req->ethertype = htons(ETH_P_IPV6);
6270 		req->ip_addr_type =
6271 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6272 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6273 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6274 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6275 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6276 	} else {
6277 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6278 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6279 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6280 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6281 	}
6282 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6283 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6284 		req->tunnel_type =
6285 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6286 	}
6287 
6288 	req->src_port = keys->ports.src;
6289 	req->src_port_mask = masks->ports.src;
6290 	req->dst_port = keys->ports.dst;
6291 	req->dst_port_mask = masks->ports.dst;
6292 
6293 	resp = hwrm_req_hold(bp, req);
6294 	rc = hwrm_req_send(bp, req);
6295 	if (!rc)
6296 		fltr->base.filter_id = resp->ntuple_filter_id;
6297 	hwrm_req_drop(bp, req);
6298 	return rc;
6299 }
6300 
6301 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6302 				     const u8 *mac_addr)
6303 {
6304 	struct bnxt_l2_filter *fltr;
6305 	struct bnxt_l2_key key;
6306 	int rc;
6307 
6308 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6309 	key.vlan = 0;
6310 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6311 	if (IS_ERR(fltr))
6312 		return PTR_ERR(fltr);
6313 
6314 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6315 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6316 	if (rc)
6317 		bnxt_del_l2_filter(bp, fltr);
6318 	else
6319 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6320 	return rc;
6321 }
6322 
6323 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6324 {
6325 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6326 
6327 	/* Any associated ntuple filters will also be cleared by firmware. */
6328 	for (i = 0; i < num_of_vnics; i++) {
6329 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6330 
6331 		for (j = 0; j < vnic->uc_filter_count; j++) {
6332 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6333 
6334 			bnxt_hwrm_l2_filter_free(bp, fltr);
6335 			bnxt_del_l2_filter(bp, fltr);
6336 		}
6337 		vnic->uc_filter_count = 0;
6338 	}
6339 }
6340 
6341 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6342 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6343 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6344 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6345 
6346 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6347 					   struct hwrm_vnic_tpa_cfg_input *req)
6348 {
6349 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6350 
6351 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6352 		return;
6353 
6354 	if (bp->vxlan_port)
6355 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6356 	if (bp->vxlan_gpe_port)
6357 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6358 	if (bp->nge_port)
6359 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6360 
6361 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6362 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6363 }
6364 
6365 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6366 			   u32 tpa_flags)
6367 {
6368 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6369 	struct hwrm_vnic_tpa_cfg_input *req;
6370 	int rc;
6371 
6372 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6373 		return 0;
6374 
6375 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6376 	if (rc)
6377 		return rc;
6378 
6379 	if (tpa_flags) {
6380 		u16 mss = bp->dev->mtu - 40;
6381 		u32 nsegs, n, segs = 0, flags;
6382 
6383 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6384 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6385 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6386 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6387 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6388 		if (tpa_flags & BNXT_FLAG_GRO)
6389 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6390 
6391 		req->flags = cpu_to_le32(flags);
6392 
6393 		req->enables =
6394 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6395 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6396 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6397 
6398 		/* Number of segs are log2 units, and first packet is not
6399 		 * included as part of this units.
6400 		 */
6401 		if (mss <= BNXT_RX_PAGE_SIZE) {
6402 			n = BNXT_RX_PAGE_SIZE / mss;
6403 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6404 		} else {
6405 			n = mss / BNXT_RX_PAGE_SIZE;
6406 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6407 				n++;
6408 			nsegs = (MAX_SKB_FRAGS - n) / n;
6409 		}
6410 
6411 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6412 			segs = MAX_TPA_SEGS_P5;
6413 			max_aggs = bp->max_tpa;
6414 		} else {
6415 			segs = ilog2(nsegs);
6416 		}
6417 		req->max_agg_segs = cpu_to_le16(segs);
6418 		req->max_aggs = cpu_to_le16(max_aggs);
6419 
6420 		req->min_agg_len = cpu_to_le32(512);
6421 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6422 	}
6423 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6424 
6425 	return hwrm_req_send(bp, req);
6426 }
6427 
6428 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6429 {
6430 	struct bnxt_ring_grp_info *grp_info;
6431 
6432 	grp_info = &bp->grp_info[ring->grp_idx];
6433 	return grp_info->cp_fw_ring_id;
6434 }
6435 
6436 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6437 {
6438 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6439 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6440 	else
6441 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6442 }
6443 
6444 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6445 {
6446 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6447 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6448 	else
6449 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6450 }
6451 
6452 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6453 {
6454 	int entries;
6455 
6456 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6457 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6458 	else
6459 		entries = HW_HASH_INDEX_SIZE;
6460 
6461 	bp->rss_indir_tbl_entries = entries;
6462 	bp->rss_indir_tbl =
6463 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6464 	if (!bp->rss_indir_tbl)
6465 		return -ENOMEM;
6466 
6467 	return 0;
6468 }
6469 
6470 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6471 				 struct ethtool_rxfh_context *rss_ctx)
6472 {
6473 	u16 max_rings, max_entries, pad, i;
6474 	u32 *rss_indir_tbl;
6475 
6476 	if (!bp->rx_nr_rings)
6477 		return;
6478 
6479 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6480 		max_rings = bp->rx_nr_rings - 1;
6481 	else
6482 		max_rings = bp->rx_nr_rings;
6483 
6484 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6485 	if (rss_ctx)
6486 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6487 	else
6488 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6489 
6490 	for (i = 0; i < max_entries; i++)
6491 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6492 
6493 	pad = bp->rss_indir_tbl_entries - max_entries;
6494 	if (pad)
6495 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6496 }
6497 
6498 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6499 {
6500 	u32 i, tbl_size, max_ring = 0;
6501 
6502 	if (!bp->rss_indir_tbl)
6503 		return 0;
6504 
6505 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6506 	for (i = 0; i < tbl_size; i++)
6507 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6508 	return max_ring;
6509 }
6510 
6511 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6512 {
6513 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6514 		if (!rx_rings)
6515 			return 0;
6516 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6517 					       BNXT_RSS_TABLE_ENTRIES_P5);
6518 	}
6519 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6520 		return 2;
6521 	return 1;
6522 }
6523 
6524 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6525 {
6526 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6527 	u16 i, j;
6528 
6529 	/* Fill the RSS indirection table with ring group ids */
6530 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6531 		if (!no_rss)
6532 			j = bp->rss_indir_tbl[i];
6533 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6534 	}
6535 }
6536 
6537 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6538 				    struct bnxt_vnic_info *vnic)
6539 {
6540 	__le16 *ring_tbl = vnic->rss_table;
6541 	struct bnxt_rx_ring_info *rxr;
6542 	u16 tbl_size, i;
6543 
6544 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6545 
6546 	for (i = 0; i < tbl_size; i++) {
6547 		u16 ring_id, j;
6548 
6549 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6550 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6551 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6552 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6553 		else
6554 			j = bp->rss_indir_tbl[i];
6555 		rxr = &bp->rx_ring[j];
6556 
6557 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6558 		*ring_tbl++ = cpu_to_le16(ring_id);
6559 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6560 		*ring_tbl++ = cpu_to_le16(ring_id);
6561 	}
6562 }
6563 
6564 static void
6565 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6566 			 struct bnxt_vnic_info *vnic)
6567 {
6568 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6569 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6570 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6571 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6572 	} else {
6573 		bnxt_fill_hw_rss_tbl(bp, vnic);
6574 	}
6575 
6576 	if (bp->rss_hash_delta) {
6577 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6578 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6579 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6580 		else
6581 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6582 	} else {
6583 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6584 	}
6585 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6586 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6587 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6588 }
6589 
6590 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6591 				  bool set_rss)
6592 {
6593 	struct hwrm_vnic_rss_cfg_input *req;
6594 	int rc;
6595 
6596 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6597 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6598 		return 0;
6599 
6600 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6601 	if (rc)
6602 		return rc;
6603 
6604 	if (set_rss)
6605 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6606 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6607 	return hwrm_req_send(bp, req);
6608 }
6609 
6610 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6611 				     struct bnxt_vnic_info *vnic, bool set_rss)
6612 {
6613 	struct hwrm_vnic_rss_cfg_input *req;
6614 	dma_addr_t ring_tbl_map;
6615 	u32 i, nr_ctxs;
6616 	int rc;
6617 
6618 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6619 	if (rc)
6620 		return rc;
6621 
6622 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6623 	if (!set_rss)
6624 		return hwrm_req_send(bp, req);
6625 
6626 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6627 	ring_tbl_map = vnic->rss_table_dma_addr;
6628 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6629 
6630 	hwrm_req_hold(bp, req);
6631 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6632 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6633 		req->ring_table_pair_index = i;
6634 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6635 		rc = hwrm_req_send(bp, req);
6636 		if (rc)
6637 			goto exit;
6638 	}
6639 
6640 exit:
6641 	hwrm_req_drop(bp, req);
6642 	return rc;
6643 }
6644 
6645 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6646 {
6647 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6648 	struct hwrm_vnic_rss_qcfg_output *resp;
6649 	struct hwrm_vnic_rss_qcfg_input *req;
6650 
6651 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6652 		return;
6653 
6654 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6655 	/* all contexts configured to same hash_type, zero always exists */
6656 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6657 	resp = hwrm_req_hold(bp, req);
6658 	if (!hwrm_req_send(bp, req)) {
6659 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6660 		bp->rss_hash_delta = 0;
6661 	}
6662 	hwrm_req_drop(bp, req);
6663 }
6664 
6665 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6666 {
6667 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6668 	struct hwrm_vnic_plcmodes_cfg_input *req;
6669 	int rc;
6670 
6671 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6672 	if (rc)
6673 		return rc;
6674 
6675 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6676 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6677 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6678 
6679 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6680 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6681 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6682 		req->enables |=
6683 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6684 		req->hds_threshold = cpu_to_le16(hds_thresh);
6685 	}
6686 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6687 	return hwrm_req_send(bp, req);
6688 }
6689 
6690 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6691 					struct bnxt_vnic_info *vnic,
6692 					u16 ctx_idx)
6693 {
6694 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6695 
6696 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6697 		return;
6698 
6699 	req->rss_cos_lb_ctx_id =
6700 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6701 
6702 	hwrm_req_send(bp, req);
6703 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6704 }
6705 
6706 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6707 {
6708 	int i, j;
6709 
6710 	for (i = 0; i < bp->nr_vnics; i++) {
6711 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6712 
6713 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6714 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6715 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6716 		}
6717 	}
6718 	bp->rsscos_nr_ctxs = 0;
6719 }
6720 
6721 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6722 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6723 {
6724 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6725 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6726 	int rc;
6727 
6728 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6729 	if (rc)
6730 		return rc;
6731 
6732 	resp = hwrm_req_hold(bp, req);
6733 	rc = hwrm_req_send(bp, req);
6734 	if (!rc)
6735 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6736 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6737 	hwrm_req_drop(bp, req);
6738 
6739 	return rc;
6740 }
6741 
6742 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6743 {
6744 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6745 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6746 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6747 }
6748 
6749 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6750 {
6751 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6752 	struct hwrm_vnic_cfg_input *req;
6753 	unsigned int ring = 0, grp_idx;
6754 	u16 def_vlan = 0;
6755 	int rc;
6756 
6757 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6758 	if (rc)
6759 		return rc;
6760 
6761 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6762 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6763 
6764 		req->default_rx_ring_id =
6765 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6766 		req->default_cmpl_ring_id =
6767 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6768 		req->enables =
6769 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6770 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6771 		goto vnic_mru;
6772 	}
6773 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6774 	/* Only RSS support for now TBD: COS & LB */
6775 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6776 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6777 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6778 					   VNIC_CFG_REQ_ENABLES_MRU);
6779 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6780 		req->rss_rule = cpu_to_le16(vnic0->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 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6784 	} else {
6785 		req->rss_rule = cpu_to_le16(0xffff);
6786 	}
6787 
6788 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6789 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6790 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6791 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6792 	} else {
6793 		req->cos_rule = cpu_to_le16(0xffff);
6794 	}
6795 
6796 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6797 		ring = 0;
6798 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6799 		ring = vnic->vnic_id - 1;
6800 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6801 		ring = bp->rx_nr_rings - 1;
6802 
6803 	grp_idx = bp->rx_ring[ring].bnapi->index;
6804 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6805 	req->lb_rule = cpu_to_le16(0xffff);
6806 vnic_mru:
6807 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6808 	req->mru = cpu_to_le16(vnic->mru);
6809 
6810 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6811 #ifdef CONFIG_BNXT_SRIOV
6812 	if (BNXT_VF(bp))
6813 		def_vlan = bp->vf.vlan;
6814 #endif
6815 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6816 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6817 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6818 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6819 
6820 	return hwrm_req_send(bp, req);
6821 }
6822 
6823 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6824 				    struct bnxt_vnic_info *vnic)
6825 {
6826 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6827 		struct hwrm_vnic_free_input *req;
6828 
6829 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6830 			return;
6831 
6832 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6833 
6834 		hwrm_req_send(bp, req);
6835 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6836 	}
6837 }
6838 
6839 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6840 {
6841 	u16 i;
6842 
6843 	for (i = 0; i < bp->nr_vnics; i++)
6844 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6845 }
6846 
6847 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6848 			 unsigned int start_rx_ring_idx,
6849 			 unsigned int nr_rings)
6850 {
6851 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6852 	struct hwrm_vnic_alloc_output *resp;
6853 	struct hwrm_vnic_alloc_input *req;
6854 	int rc;
6855 
6856 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6857 	if (rc)
6858 		return rc;
6859 
6860 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6861 		goto vnic_no_ring_grps;
6862 
6863 	/* map ring groups to this vnic */
6864 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6865 		grp_idx = bp->rx_ring[i].bnapi->index;
6866 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6867 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6868 				   j, nr_rings);
6869 			break;
6870 		}
6871 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6872 	}
6873 
6874 vnic_no_ring_grps:
6875 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6876 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6877 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6878 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6879 
6880 	resp = hwrm_req_hold(bp, req);
6881 	rc = hwrm_req_send(bp, req);
6882 	if (!rc)
6883 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6884 	hwrm_req_drop(bp, req);
6885 	return rc;
6886 }
6887 
6888 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6889 {
6890 	struct hwrm_vnic_qcaps_output *resp;
6891 	struct hwrm_vnic_qcaps_input *req;
6892 	int rc;
6893 
6894 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6895 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6896 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6897 	if (bp->hwrm_spec_code < 0x10600)
6898 		return 0;
6899 
6900 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6901 	if (rc)
6902 		return rc;
6903 
6904 	resp = hwrm_req_hold(bp, req);
6905 	rc = hwrm_req_send(bp, req);
6906 	if (!rc) {
6907 		u32 flags = le32_to_cpu(resp->flags);
6908 
6909 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6910 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6911 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6912 		if (flags &
6913 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6914 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6915 
6916 		/* Older P5 fw before EXT_HW_STATS support did not set
6917 		 * VLAN_STRIP_CAP properly.
6918 		 */
6919 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6920 		    (BNXT_CHIP_P5(bp) &&
6921 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6922 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6923 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6924 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6925 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6926 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6927 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6928 		if (bp->max_tpa_v2) {
6929 			if (BNXT_CHIP_P5(bp))
6930 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6931 			else
6932 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6933 		}
6934 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6935 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6936 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6937 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6938 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6939 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6940 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6941 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6942 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6943 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6944 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6945 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6946 	}
6947 	hwrm_req_drop(bp, req);
6948 	return rc;
6949 }
6950 
6951 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6952 {
6953 	struct hwrm_ring_grp_alloc_output *resp;
6954 	struct hwrm_ring_grp_alloc_input *req;
6955 	int rc;
6956 	u16 i;
6957 
6958 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6959 		return 0;
6960 
6961 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6962 	if (rc)
6963 		return rc;
6964 
6965 	resp = hwrm_req_hold(bp, req);
6966 	for (i = 0; i < bp->rx_nr_rings; i++) {
6967 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6968 
6969 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6970 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6971 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6972 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6973 
6974 		rc = hwrm_req_send(bp, req);
6975 
6976 		if (rc)
6977 			break;
6978 
6979 		bp->grp_info[grp_idx].fw_grp_id =
6980 			le32_to_cpu(resp->ring_group_id);
6981 	}
6982 	hwrm_req_drop(bp, req);
6983 	return rc;
6984 }
6985 
6986 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6987 {
6988 	struct hwrm_ring_grp_free_input *req;
6989 	u16 i;
6990 
6991 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6992 		return;
6993 
6994 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6995 		return;
6996 
6997 	hwrm_req_hold(bp, req);
6998 	for (i = 0; i < bp->cp_nr_rings; i++) {
6999 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
7000 			continue;
7001 		req->ring_group_id =
7002 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
7003 
7004 		hwrm_req_send(bp, req);
7005 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
7006 	}
7007 	hwrm_req_drop(bp, req);
7008 }
7009 
7010 static void bnxt_set_rx_ring_params_p5(struct bnxt *bp, u32 ring_type,
7011 				       struct hwrm_ring_alloc_input *req,
7012 				       struct bnxt_ring_struct *ring)
7013 {
7014 	struct bnxt_ring_grp_info *grp_info = &bp->grp_info[ring->grp_idx];
7015 	u32 enables = RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID |
7016 		      RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID;
7017 
7018 	if (ring_type == HWRM_RING_ALLOC_AGG) {
7019 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7020 		req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7021 		req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7022 		enables |= RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID;
7023 	} else {
7024 		req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
7025 		if (NET_IP_ALIGN == 2)
7026 			req->flags =
7027 				cpu_to_le16(RING_ALLOC_REQ_FLAGS_RX_SOP_PAD);
7028 	}
7029 	req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7030 	req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7031 	req->enables |= cpu_to_le32(enables);
7032 }
7033 
7034 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
7035 				    struct bnxt_ring_struct *ring,
7036 				    u32 ring_type, u32 map_index)
7037 {
7038 	struct hwrm_ring_alloc_output *resp;
7039 	struct hwrm_ring_alloc_input *req;
7040 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
7041 	struct bnxt_ring_grp_info *grp_info;
7042 	int rc, err = 0;
7043 	u16 ring_id;
7044 
7045 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
7046 	if (rc)
7047 		goto exit;
7048 
7049 	req->enables = 0;
7050 	if (rmem->nr_pages > 1) {
7051 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
7052 		/* Page size is in log2 units */
7053 		req->page_size = BNXT_PAGE_SHIFT;
7054 		req->page_tbl_depth = 1;
7055 	} else {
7056 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
7057 	}
7058 	req->fbo = 0;
7059 	/* Association of ring index with doorbell index and MSIX number */
7060 	req->logical_id = cpu_to_le16(map_index);
7061 
7062 	switch (ring_type) {
7063 	case HWRM_RING_ALLOC_TX: {
7064 		struct bnxt_tx_ring_info *txr;
7065 		u16 flags = 0;
7066 
7067 		txr = container_of(ring, struct bnxt_tx_ring_info,
7068 				   tx_ring_struct);
7069 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
7070 		/* Association of transmit ring with completion ring */
7071 		grp_info = &bp->grp_info[ring->grp_idx];
7072 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
7073 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
7074 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7075 		req->queue_id = cpu_to_le16(ring->queue_id);
7076 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
7077 			req->cmpl_coal_cnt =
7078 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
7079 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
7080 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
7081 		req->flags = cpu_to_le16(flags);
7082 		break;
7083 	}
7084 	case HWRM_RING_ALLOC_RX:
7085 	case HWRM_RING_ALLOC_AGG:
7086 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7087 		req->length = (ring_type == HWRM_RING_ALLOC_RX) ?
7088 			      cpu_to_le32(bp->rx_ring_mask + 1) :
7089 			      cpu_to_le32(bp->rx_agg_ring_mask + 1);
7090 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7091 			bnxt_set_rx_ring_params_p5(bp, ring_type, req, ring);
7092 		break;
7093 	case HWRM_RING_ALLOC_CMPL:
7094 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7095 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7096 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7097 			/* Association of cp ring with nq */
7098 			grp_info = &bp->grp_info[map_index];
7099 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7100 			req->cq_handle = cpu_to_le64(ring->handle);
7101 			req->enables |= cpu_to_le32(
7102 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7103 		} else {
7104 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7105 		}
7106 		break;
7107 	case HWRM_RING_ALLOC_NQ:
7108 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7109 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7110 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7111 		break;
7112 	default:
7113 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7114 			   ring_type);
7115 		return -1;
7116 	}
7117 
7118 	resp = hwrm_req_hold(bp, req);
7119 	rc = hwrm_req_send(bp, req);
7120 	err = le16_to_cpu(resp->error_code);
7121 	ring_id = le16_to_cpu(resp->ring_id);
7122 	hwrm_req_drop(bp, req);
7123 
7124 exit:
7125 	if (rc || err) {
7126 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7127 			   ring_type, rc, err);
7128 		return -EIO;
7129 	}
7130 	ring->fw_ring_id = ring_id;
7131 	return rc;
7132 }
7133 
7134 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7135 {
7136 	int rc;
7137 
7138 	if (BNXT_PF(bp)) {
7139 		struct hwrm_func_cfg_input *req;
7140 
7141 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7142 		if (rc)
7143 			return rc;
7144 
7145 		req->fid = cpu_to_le16(0xffff);
7146 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7147 		req->async_event_cr = cpu_to_le16(idx);
7148 		return hwrm_req_send(bp, req);
7149 	} else {
7150 		struct hwrm_func_vf_cfg_input *req;
7151 
7152 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7153 		if (rc)
7154 			return rc;
7155 
7156 		req->enables =
7157 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7158 		req->async_event_cr = cpu_to_le16(idx);
7159 		return hwrm_req_send(bp, req);
7160 	}
7161 }
7162 
7163 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7164 			     u32 ring_type)
7165 {
7166 	switch (ring_type) {
7167 	case HWRM_RING_ALLOC_TX:
7168 		db->db_ring_mask = bp->tx_ring_mask;
7169 		break;
7170 	case HWRM_RING_ALLOC_RX:
7171 		db->db_ring_mask = bp->rx_ring_mask;
7172 		break;
7173 	case HWRM_RING_ALLOC_AGG:
7174 		db->db_ring_mask = bp->rx_agg_ring_mask;
7175 		break;
7176 	case HWRM_RING_ALLOC_CMPL:
7177 	case HWRM_RING_ALLOC_NQ:
7178 		db->db_ring_mask = bp->cp_ring_mask;
7179 		break;
7180 	}
7181 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7182 		db->db_epoch_mask = db->db_ring_mask + 1;
7183 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7184 	}
7185 }
7186 
7187 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7188 			u32 map_idx, u32 xid)
7189 {
7190 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7191 		switch (ring_type) {
7192 		case HWRM_RING_ALLOC_TX:
7193 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7194 			break;
7195 		case HWRM_RING_ALLOC_RX:
7196 		case HWRM_RING_ALLOC_AGG:
7197 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7198 			break;
7199 		case HWRM_RING_ALLOC_CMPL:
7200 			db->db_key64 = DBR_PATH_L2;
7201 			break;
7202 		case HWRM_RING_ALLOC_NQ:
7203 			db->db_key64 = DBR_PATH_L2;
7204 			break;
7205 		}
7206 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7207 
7208 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7209 			db->db_key64 |= DBR_VALID;
7210 
7211 		db->doorbell = bp->bar1 + bp->db_offset;
7212 	} else {
7213 		db->doorbell = bp->bar1 + map_idx * 0x80;
7214 		switch (ring_type) {
7215 		case HWRM_RING_ALLOC_TX:
7216 			db->db_key32 = DB_KEY_TX;
7217 			break;
7218 		case HWRM_RING_ALLOC_RX:
7219 		case HWRM_RING_ALLOC_AGG:
7220 			db->db_key32 = DB_KEY_RX;
7221 			break;
7222 		case HWRM_RING_ALLOC_CMPL:
7223 			db->db_key32 = DB_KEY_CP;
7224 			break;
7225 		}
7226 	}
7227 	bnxt_set_db_mask(bp, db, ring_type);
7228 }
7229 
7230 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7231 				   struct bnxt_rx_ring_info *rxr)
7232 {
7233 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7234 	struct bnxt_napi *bnapi = rxr->bnapi;
7235 	u32 type = HWRM_RING_ALLOC_RX;
7236 	u32 map_idx = bnapi->index;
7237 	int rc;
7238 
7239 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7240 	if (rc)
7241 		return rc;
7242 
7243 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7244 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7245 
7246 	return 0;
7247 }
7248 
7249 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7250 				       struct bnxt_rx_ring_info *rxr)
7251 {
7252 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7253 	u32 type = HWRM_RING_ALLOC_AGG;
7254 	u32 grp_idx = ring->grp_idx;
7255 	u32 map_idx;
7256 	int rc;
7257 
7258 	map_idx = grp_idx + bp->rx_nr_rings;
7259 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7260 	if (rc)
7261 		return rc;
7262 
7263 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7264 		    ring->fw_ring_id);
7265 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7266 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7267 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7268 
7269 	return 0;
7270 }
7271 
7272 static int bnxt_hwrm_cp_ring_alloc_p5(struct bnxt *bp,
7273 				      struct bnxt_cp_ring_info *cpr)
7274 {
7275 	const u32 type = HWRM_RING_ALLOC_CMPL;
7276 	struct bnxt_napi *bnapi = cpr->bnapi;
7277 	struct bnxt_ring_struct *ring;
7278 	u32 map_idx = bnapi->index;
7279 	int rc;
7280 
7281 	ring = &cpr->cp_ring_struct;
7282 	ring->handle = BNXT_SET_NQ_HDL(cpr);
7283 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7284 	if (rc)
7285 		return rc;
7286 	bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7287 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7288 	return 0;
7289 }
7290 
7291 static int bnxt_hwrm_tx_ring_alloc(struct bnxt *bp,
7292 				   struct bnxt_tx_ring_info *txr, u32 tx_idx)
7293 {
7294 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7295 	const u32 type = HWRM_RING_ALLOC_TX;
7296 	int rc;
7297 
7298 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, tx_idx);
7299 	if (rc)
7300 		return rc;
7301 	bnxt_set_db(bp, &txr->tx_db, type, tx_idx, ring->fw_ring_id);
7302 	return 0;
7303 }
7304 
7305 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7306 {
7307 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7308 	int i, rc = 0;
7309 	u32 type;
7310 
7311 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7312 		type = HWRM_RING_ALLOC_NQ;
7313 	else
7314 		type = HWRM_RING_ALLOC_CMPL;
7315 	for (i = 0; i < bp->cp_nr_rings; i++) {
7316 		struct bnxt_napi *bnapi = bp->bnapi[i];
7317 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7318 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7319 		u32 map_idx = ring->map_idx;
7320 		unsigned int vector;
7321 
7322 		vector = bp->irq_tbl[map_idx].vector;
7323 		disable_irq_nosync(vector);
7324 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7325 		if (rc) {
7326 			enable_irq(vector);
7327 			goto err_out;
7328 		}
7329 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7330 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7331 		enable_irq(vector);
7332 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7333 
7334 		if (!i) {
7335 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7336 			if (rc)
7337 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7338 		}
7339 	}
7340 
7341 	for (i = 0; i < bp->tx_nr_rings; i++) {
7342 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7343 
7344 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7345 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
7346 			if (rc)
7347 				goto err_out;
7348 		}
7349 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, i);
7350 		if (rc)
7351 			goto err_out;
7352 	}
7353 
7354 	for (i = 0; i < bp->rx_nr_rings; i++) {
7355 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7356 
7357 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7358 		if (rc)
7359 			goto err_out;
7360 		/* If we have agg rings, post agg buffers first. */
7361 		if (!agg_rings)
7362 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7363 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7364 			rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
7365 			if (rc)
7366 				goto err_out;
7367 		}
7368 	}
7369 
7370 	if (agg_rings) {
7371 		for (i = 0; i < bp->rx_nr_rings; i++) {
7372 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7373 			if (rc)
7374 				goto err_out;
7375 		}
7376 	}
7377 err_out:
7378 	return rc;
7379 }
7380 
7381 static void bnxt_cancel_dim(struct bnxt *bp)
7382 {
7383 	int i;
7384 
7385 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7386 	 * if NAPI is enabled.
7387 	 */
7388 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7389 		return;
7390 
7391 	/* Make sure NAPI sees that the VNIC is disabled */
7392 	synchronize_net();
7393 	for (i = 0; i < bp->rx_nr_rings; i++) {
7394 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7395 		struct bnxt_napi *bnapi = rxr->bnapi;
7396 
7397 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7398 	}
7399 }
7400 
7401 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7402 				   struct bnxt_ring_struct *ring,
7403 				   u32 ring_type, int cmpl_ring_id)
7404 {
7405 	struct hwrm_ring_free_output *resp;
7406 	struct hwrm_ring_free_input *req;
7407 	u16 error_code = 0;
7408 	int rc;
7409 
7410 	if (BNXT_NO_FW_ACCESS(bp))
7411 		return 0;
7412 
7413 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7414 	if (rc)
7415 		goto exit;
7416 
7417 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7418 	req->ring_type = ring_type;
7419 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7420 
7421 	resp = hwrm_req_hold(bp, req);
7422 	rc = hwrm_req_send(bp, req);
7423 	error_code = le16_to_cpu(resp->error_code);
7424 	hwrm_req_drop(bp, req);
7425 exit:
7426 	if (rc || error_code) {
7427 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7428 			   ring_type, rc, error_code);
7429 		return -EIO;
7430 	}
7431 	return 0;
7432 }
7433 
7434 static void bnxt_hwrm_tx_ring_free(struct bnxt *bp,
7435 				   struct bnxt_tx_ring_info *txr,
7436 				   bool close_path)
7437 {
7438 	struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7439 	u32 cmpl_ring_id;
7440 
7441 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7442 		return;
7443 
7444 	cmpl_ring_id = close_path ? bnxt_cp_ring_for_tx(bp, txr) :
7445 		       INVALID_HW_RING_ID;
7446 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_TX,
7447 				cmpl_ring_id);
7448 	ring->fw_ring_id = INVALID_HW_RING_ID;
7449 }
7450 
7451 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7452 				   struct bnxt_rx_ring_info *rxr,
7453 				   bool close_path)
7454 {
7455 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7456 	u32 grp_idx = rxr->bnapi->index;
7457 	u32 cmpl_ring_id;
7458 
7459 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7460 		return;
7461 
7462 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7463 	hwrm_ring_free_send_msg(bp, ring,
7464 				RING_FREE_REQ_RING_TYPE_RX,
7465 				close_path ? cmpl_ring_id :
7466 				INVALID_HW_RING_ID);
7467 	ring->fw_ring_id = INVALID_HW_RING_ID;
7468 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7469 }
7470 
7471 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7472 				       struct bnxt_rx_ring_info *rxr,
7473 				       bool close_path)
7474 {
7475 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7476 	u32 grp_idx = rxr->bnapi->index;
7477 	u32 type, cmpl_ring_id;
7478 
7479 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7480 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7481 	else
7482 		type = RING_FREE_REQ_RING_TYPE_RX;
7483 
7484 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7485 		return;
7486 
7487 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7488 	hwrm_ring_free_send_msg(bp, ring, type,
7489 				close_path ? cmpl_ring_id :
7490 				INVALID_HW_RING_ID);
7491 	ring->fw_ring_id = INVALID_HW_RING_ID;
7492 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7493 }
7494 
7495 static void bnxt_hwrm_cp_ring_free(struct bnxt *bp,
7496 				   struct bnxt_cp_ring_info *cpr)
7497 {
7498 	struct bnxt_ring_struct *ring;
7499 
7500 	ring = &cpr->cp_ring_struct;
7501 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7502 		return;
7503 
7504 	hwrm_ring_free_send_msg(bp, ring, RING_FREE_REQ_RING_TYPE_L2_CMPL,
7505 				INVALID_HW_RING_ID);
7506 	ring->fw_ring_id = INVALID_HW_RING_ID;
7507 }
7508 
7509 static void bnxt_clear_one_cp_ring(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
7510 {
7511 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7512 	int i, size = ring->ring_mem.page_size;
7513 
7514 	cpr->cp_raw_cons = 0;
7515 	cpr->toggle = 0;
7516 
7517 	for (i = 0; i < bp->cp_nr_pages; i++)
7518 		if (cpr->cp_desc_ring[i])
7519 			memset(cpr->cp_desc_ring[i], 0, size);
7520 }
7521 
7522 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7523 {
7524 	u32 type;
7525 	int i;
7526 
7527 	if (!bp->bnapi)
7528 		return;
7529 
7530 	for (i = 0; i < bp->tx_nr_rings; i++)
7531 		bnxt_hwrm_tx_ring_free(bp, &bp->tx_ring[i], close_path);
7532 
7533 	bnxt_cancel_dim(bp);
7534 	for (i = 0; i < bp->rx_nr_rings; i++) {
7535 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7536 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7537 	}
7538 
7539 	/* The completion rings are about to be freed.  After that the
7540 	 * IRQ doorbell will not work anymore.  So we need to disable
7541 	 * IRQ here.
7542 	 */
7543 	bnxt_disable_int_sync(bp);
7544 
7545 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7546 		type = RING_FREE_REQ_RING_TYPE_NQ;
7547 	else
7548 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7549 	for (i = 0; i < bp->cp_nr_rings; i++) {
7550 		struct bnxt_napi *bnapi = bp->bnapi[i];
7551 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7552 		struct bnxt_ring_struct *ring;
7553 		int j;
7554 
7555 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++)
7556 			bnxt_hwrm_cp_ring_free(bp, &cpr->cp_ring_arr[j]);
7557 
7558 		ring = &cpr->cp_ring_struct;
7559 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7560 			hwrm_ring_free_send_msg(bp, ring, type,
7561 						INVALID_HW_RING_ID);
7562 			ring->fw_ring_id = INVALID_HW_RING_ID;
7563 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7564 		}
7565 	}
7566 }
7567 
7568 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7569 			     bool shared);
7570 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7571 			   bool shared);
7572 
7573 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7574 {
7575 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7576 	struct hwrm_func_qcfg_output *resp;
7577 	struct hwrm_func_qcfg_input *req;
7578 	int rc;
7579 
7580 	if (bp->hwrm_spec_code < 0x10601)
7581 		return 0;
7582 
7583 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7584 	if (rc)
7585 		return rc;
7586 
7587 	req->fid = cpu_to_le16(0xffff);
7588 	resp = hwrm_req_hold(bp, req);
7589 	rc = hwrm_req_send(bp, req);
7590 	if (rc) {
7591 		hwrm_req_drop(bp, req);
7592 		return rc;
7593 	}
7594 
7595 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7596 	if (BNXT_NEW_RM(bp)) {
7597 		u16 cp, stats;
7598 
7599 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7600 		hw_resc->resv_hw_ring_grps =
7601 			le32_to_cpu(resp->alloc_hw_ring_grps);
7602 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7603 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7604 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7605 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7606 		hw_resc->resv_irqs = cp;
7607 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7608 			int rx = hw_resc->resv_rx_rings;
7609 			int tx = hw_resc->resv_tx_rings;
7610 
7611 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7612 				rx >>= 1;
7613 			if (cp < (rx + tx)) {
7614 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7615 				if (rc)
7616 					goto get_rings_exit;
7617 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7618 					rx <<= 1;
7619 				hw_resc->resv_rx_rings = rx;
7620 				hw_resc->resv_tx_rings = tx;
7621 			}
7622 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7623 			hw_resc->resv_hw_ring_grps = rx;
7624 		}
7625 		hw_resc->resv_cp_rings = cp;
7626 		hw_resc->resv_stat_ctxs = stats;
7627 	}
7628 get_rings_exit:
7629 	hwrm_req_drop(bp, req);
7630 	return rc;
7631 }
7632 
7633 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7634 {
7635 	struct hwrm_func_qcfg_output *resp;
7636 	struct hwrm_func_qcfg_input *req;
7637 	int rc;
7638 
7639 	if (bp->hwrm_spec_code < 0x10601)
7640 		return 0;
7641 
7642 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7643 	if (rc)
7644 		return rc;
7645 
7646 	req->fid = cpu_to_le16(fid);
7647 	resp = hwrm_req_hold(bp, req);
7648 	rc = hwrm_req_send(bp, req);
7649 	if (!rc)
7650 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7651 
7652 	hwrm_req_drop(bp, req);
7653 	return rc;
7654 }
7655 
7656 static bool bnxt_rfs_supported(struct bnxt *bp);
7657 
7658 static struct hwrm_func_cfg_input *
7659 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7660 {
7661 	struct hwrm_func_cfg_input *req;
7662 	u32 enables = 0;
7663 
7664 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7665 		return NULL;
7666 
7667 	req->fid = cpu_to_le16(0xffff);
7668 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7669 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7670 	if (BNXT_NEW_RM(bp)) {
7671 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7672 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7673 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7674 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7675 			enables |= hwr->cp_p5 ?
7676 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7677 		} else {
7678 			enables |= hwr->cp ?
7679 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7680 			enables |= hwr->grp ?
7681 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7682 		}
7683 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7684 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7685 					  0;
7686 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7687 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7688 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7689 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7690 			req->num_msix = cpu_to_le16(hwr->cp);
7691 		} else {
7692 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7693 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7694 		}
7695 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7696 		req->num_vnics = cpu_to_le16(hwr->vnic);
7697 	}
7698 	req->enables = cpu_to_le32(enables);
7699 	return req;
7700 }
7701 
7702 static struct hwrm_func_vf_cfg_input *
7703 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7704 {
7705 	struct hwrm_func_vf_cfg_input *req;
7706 	u32 enables = 0;
7707 
7708 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7709 		return NULL;
7710 
7711 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7712 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7713 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7714 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7715 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7716 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7717 		enables |= hwr->cp_p5 ?
7718 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7719 	} else {
7720 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7721 		enables |= hwr->grp ?
7722 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7723 	}
7724 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7725 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7726 
7727 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7728 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7729 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7730 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7731 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7732 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7733 	} else {
7734 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7735 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7736 	}
7737 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7738 	req->num_vnics = cpu_to_le16(hwr->vnic);
7739 
7740 	req->enables = cpu_to_le32(enables);
7741 	return req;
7742 }
7743 
7744 static int
7745 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7746 {
7747 	struct hwrm_func_cfg_input *req;
7748 	int rc;
7749 
7750 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7751 	if (!req)
7752 		return -ENOMEM;
7753 
7754 	if (!req->enables) {
7755 		hwrm_req_drop(bp, req);
7756 		return 0;
7757 	}
7758 
7759 	rc = hwrm_req_send(bp, req);
7760 	if (rc)
7761 		return rc;
7762 
7763 	if (bp->hwrm_spec_code < 0x10601)
7764 		bp->hw_resc.resv_tx_rings = hwr->tx;
7765 
7766 	return bnxt_hwrm_get_rings(bp);
7767 }
7768 
7769 static int
7770 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7771 {
7772 	struct hwrm_func_vf_cfg_input *req;
7773 	int rc;
7774 
7775 	if (!BNXT_NEW_RM(bp)) {
7776 		bp->hw_resc.resv_tx_rings = hwr->tx;
7777 		return 0;
7778 	}
7779 
7780 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7781 	if (!req)
7782 		return -ENOMEM;
7783 
7784 	rc = hwrm_req_send(bp, req);
7785 	if (rc)
7786 		return rc;
7787 
7788 	return bnxt_hwrm_get_rings(bp);
7789 }
7790 
7791 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7792 {
7793 	if (BNXT_PF(bp))
7794 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7795 	else
7796 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7797 }
7798 
7799 int bnxt_nq_rings_in_use(struct bnxt *bp)
7800 {
7801 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7802 }
7803 
7804 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7805 {
7806 	int cp;
7807 
7808 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7809 		return bnxt_nq_rings_in_use(bp);
7810 
7811 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7812 	return cp;
7813 }
7814 
7815 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7816 {
7817 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7818 }
7819 
7820 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7821 {
7822 	if (!hwr->grp)
7823 		return 0;
7824 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7825 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7826 
7827 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7828 			rss_ctx *= hwr->vnic;
7829 		return rss_ctx;
7830 	}
7831 	if (BNXT_VF(bp))
7832 		return BNXT_VF_MAX_RSS_CTX;
7833 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7834 		return hwr->grp + 1;
7835 	return 1;
7836 }
7837 
7838 /* Check if a default RSS map needs to be setup.  This function is only
7839  * used on older firmware that does not require reserving RX rings.
7840  */
7841 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7842 {
7843 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7844 
7845 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7846 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7847 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7848 		if (!netif_is_rxfh_configured(bp->dev))
7849 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7850 	}
7851 }
7852 
7853 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7854 {
7855 	if (bp->flags & BNXT_FLAG_RFS) {
7856 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7857 			return 2 + bp->num_rss_ctx;
7858 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7859 			return rx_rings + 1;
7860 	}
7861 	return 1;
7862 }
7863 
7864 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7865 {
7866 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7867 	int cp = bnxt_cp_rings_in_use(bp);
7868 	int nq = bnxt_nq_rings_in_use(bp);
7869 	int rx = bp->rx_nr_rings, stat;
7870 	int vnic, grp = rx;
7871 
7872 	/* Old firmware does not need RX ring reservations but we still
7873 	 * need to setup a default RSS map when needed.  With new firmware
7874 	 * we go through RX ring reservations first and then set up the
7875 	 * RSS map for the successfully reserved RX rings when needed.
7876 	 */
7877 	if (!BNXT_NEW_RM(bp))
7878 		bnxt_check_rss_tbl_no_rmgr(bp);
7879 
7880 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7881 	    bp->hwrm_spec_code >= 0x10601)
7882 		return true;
7883 
7884 	if (!BNXT_NEW_RM(bp))
7885 		return false;
7886 
7887 	vnic = bnxt_get_total_vnics(bp, rx);
7888 
7889 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7890 		rx <<= 1;
7891 	stat = bnxt_get_func_stat_ctxs(bp);
7892 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7893 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7894 	    (hw_resc->resv_hw_ring_grps != grp &&
7895 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7896 		return true;
7897 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7898 	    hw_resc->resv_irqs != nq)
7899 		return true;
7900 	return false;
7901 }
7902 
7903 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7904 {
7905 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7906 
7907 	hwr->tx = hw_resc->resv_tx_rings;
7908 	if (BNXT_NEW_RM(bp)) {
7909 		hwr->rx = hw_resc->resv_rx_rings;
7910 		hwr->cp = hw_resc->resv_irqs;
7911 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7912 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7913 		hwr->grp = hw_resc->resv_hw_ring_grps;
7914 		hwr->vnic = hw_resc->resv_vnics;
7915 		hwr->stat = hw_resc->resv_stat_ctxs;
7916 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7917 	}
7918 }
7919 
7920 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7921 {
7922 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7923 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7924 }
7925 
7926 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7927 
7928 static int __bnxt_reserve_rings(struct bnxt *bp)
7929 {
7930 	struct bnxt_hw_rings hwr = {0};
7931 	int rx_rings, old_rx_rings, rc;
7932 	int cp = bp->cp_nr_rings;
7933 	int ulp_msix = 0;
7934 	bool sh = false;
7935 	int tx_cp;
7936 
7937 	if (!bnxt_need_reserve_rings(bp))
7938 		return 0;
7939 
7940 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7941 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7942 		if (!ulp_msix)
7943 			bnxt_set_ulp_stat_ctxs(bp, 0);
7944 
7945 		if (ulp_msix > bp->ulp_num_msix_want)
7946 			ulp_msix = bp->ulp_num_msix_want;
7947 		hwr.cp = cp + ulp_msix;
7948 	} else {
7949 		hwr.cp = bnxt_nq_rings_in_use(bp);
7950 	}
7951 
7952 	hwr.tx = bp->tx_nr_rings;
7953 	hwr.rx = bp->rx_nr_rings;
7954 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7955 		sh = true;
7956 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7957 		hwr.cp_p5 = hwr.rx + hwr.tx;
7958 
7959 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7960 
7961 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7962 		hwr.rx <<= 1;
7963 	hwr.grp = bp->rx_nr_rings;
7964 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7965 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7966 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7967 
7968 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7969 	if (rc)
7970 		return rc;
7971 
7972 	bnxt_copy_reserved_rings(bp, &hwr);
7973 
7974 	rx_rings = hwr.rx;
7975 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7976 		if (hwr.rx >= 2) {
7977 			rx_rings = hwr.rx >> 1;
7978 		} else {
7979 			if (netif_running(bp->dev))
7980 				return -ENOMEM;
7981 
7982 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7983 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7984 			bp->dev->hw_features &= ~NETIF_F_LRO;
7985 			bp->dev->features &= ~NETIF_F_LRO;
7986 			bnxt_set_ring_params(bp);
7987 		}
7988 	}
7989 	rx_rings = min_t(int, rx_rings, hwr.grp);
7990 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7991 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7992 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7993 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7994 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7995 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7996 		hwr.rx = rx_rings << 1;
7997 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7998 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7999 	bp->tx_nr_rings = hwr.tx;
8000 
8001 	/* If we cannot reserve all the RX rings, reset the RSS map only
8002 	 * if absolutely necessary
8003 	 */
8004 	if (rx_rings != bp->rx_nr_rings) {
8005 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
8006 			    rx_rings, bp->rx_nr_rings);
8007 		if (netif_is_rxfh_configured(bp->dev) &&
8008 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
8009 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
8010 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
8011 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
8012 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
8013 		}
8014 	}
8015 	bp->rx_nr_rings = rx_rings;
8016 	bp->cp_nr_rings = hwr.cp;
8017 
8018 	if (!bnxt_rings_ok(bp, &hwr))
8019 		return -ENOMEM;
8020 
8021 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
8022 	    !netif_is_rxfh_configured(bp->dev))
8023 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
8024 
8025 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
8026 		int resv_msix, resv_ctx, ulp_ctxs;
8027 		struct bnxt_hw_resc *hw_resc;
8028 
8029 		hw_resc = &bp->hw_resc;
8030 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
8031 		ulp_msix = min_t(int, resv_msix, ulp_msix);
8032 		bnxt_set_ulp_msix_num(bp, ulp_msix);
8033 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
8034 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
8035 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
8036 	}
8037 
8038 	return rc;
8039 }
8040 
8041 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8042 {
8043 	struct hwrm_func_vf_cfg_input *req;
8044 	u32 flags;
8045 
8046 	if (!BNXT_NEW_RM(bp))
8047 		return 0;
8048 
8049 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
8050 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
8051 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8052 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8053 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8054 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
8055 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
8056 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8057 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8058 
8059 	req->flags = cpu_to_le32(flags);
8060 	return hwrm_req_send_silent(bp, req);
8061 }
8062 
8063 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8064 {
8065 	struct hwrm_func_cfg_input *req;
8066 	u32 flags;
8067 
8068 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
8069 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
8070 	if (BNXT_NEW_RM(bp)) {
8071 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
8072 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
8073 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
8074 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
8075 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
8076 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
8077 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
8078 		else
8079 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
8080 	}
8081 
8082 	req->flags = cpu_to_le32(flags);
8083 	return hwrm_req_send_silent(bp, req);
8084 }
8085 
8086 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
8087 {
8088 	if (bp->hwrm_spec_code < 0x10801)
8089 		return 0;
8090 
8091 	if (BNXT_PF(bp))
8092 		return bnxt_hwrm_check_pf_rings(bp, hwr);
8093 
8094 	return bnxt_hwrm_check_vf_rings(bp, hwr);
8095 }
8096 
8097 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
8098 {
8099 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8100 	struct hwrm_ring_aggint_qcaps_output *resp;
8101 	struct hwrm_ring_aggint_qcaps_input *req;
8102 	int rc;
8103 
8104 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8105 	coal_cap->num_cmpl_dma_aggr_max = 63;
8106 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8107 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8108 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8109 	coal_cap->int_lat_tmr_min_max = 65535;
8110 	coal_cap->int_lat_tmr_max_max = 65535;
8111 	coal_cap->num_cmpl_aggr_int_max = 65535;
8112 	coal_cap->timer_units = 80;
8113 
8114 	if (bp->hwrm_spec_code < 0x10902)
8115 		return;
8116 
8117 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8118 		return;
8119 
8120 	resp = hwrm_req_hold(bp, req);
8121 	rc = hwrm_req_send_silent(bp, req);
8122 	if (!rc) {
8123 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8124 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8125 		coal_cap->num_cmpl_dma_aggr_max =
8126 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8127 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8128 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8129 		coal_cap->cmpl_aggr_dma_tmr_max =
8130 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8131 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8132 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8133 		coal_cap->int_lat_tmr_min_max =
8134 			le16_to_cpu(resp->int_lat_tmr_min_max);
8135 		coal_cap->int_lat_tmr_max_max =
8136 			le16_to_cpu(resp->int_lat_tmr_max_max);
8137 		coal_cap->num_cmpl_aggr_int_max =
8138 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8139 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8140 	}
8141 	hwrm_req_drop(bp, req);
8142 }
8143 
8144 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8145 {
8146 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8147 
8148 	return usec * 1000 / coal_cap->timer_units;
8149 }
8150 
8151 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8152 	struct bnxt_coal *hw_coal,
8153 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8154 {
8155 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8156 	u16 val, tmr, max, flags = hw_coal->flags;
8157 	u32 cmpl_params = coal_cap->cmpl_params;
8158 
8159 	max = hw_coal->bufs_per_record * 128;
8160 	if (hw_coal->budget)
8161 		max = hw_coal->bufs_per_record * hw_coal->budget;
8162 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8163 
8164 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8165 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8166 
8167 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8168 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8169 
8170 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8171 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8172 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8173 
8174 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8175 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8176 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8177 
8178 	/* min timer set to 1/2 of interrupt timer */
8179 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8180 		val = tmr / 2;
8181 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8182 		req->int_lat_tmr_min = cpu_to_le16(val);
8183 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8184 	}
8185 
8186 	/* buf timer set to 1/4 of interrupt timer */
8187 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8188 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8189 
8190 	if (cmpl_params &
8191 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8192 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8193 		val = clamp_t(u16, tmr, 1,
8194 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8195 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8196 		req->enables |=
8197 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8198 	}
8199 
8200 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8201 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8202 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8203 	req->flags = cpu_to_le16(flags);
8204 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8205 }
8206 
8207 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8208 				   struct bnxt_coal *hw_coal)
8209 {
8210 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8211 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8212 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8213 	u32 nq_params = coal_cap->nq_params;
8214 	u16 tmr;
8215 	int rc;
8216 
8217 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8218 		return 0;
8219 
8220 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8221 	if (rc)
8222 		return rc;
8223 
8224 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8225 	req->flags =
8226 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8227 
8228 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8229 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8230 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8231 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8232 	return hwrm_req_send(bp, req);
8233 }
8234 
8235 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8236 {
8237 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8238 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8239 	struct bnxt_coal coal;
8240 	int rc;
8241 
8242 	/* Tick values in micro seconds.
8243 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8244 	 */
8245 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8246 
8247 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8248 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8249 
8250 	if (!bnapi->rx_ring)
8251 		return -ENODEV;
8252 
8253 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8254 	if (rc)
8255 		return rc;
8256 
8257 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8258 
8259 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8260 
8261 	return hwrm_req_send(bp, req_rx);
8262 }
8263 
8264 static int
8265 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8266 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8267 {
8268 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8269 
8270 	req->ring_id = cpu_to_le16(ring_id);
8271 	return hwrm_req_send(bp, req);
8272 }
8273 
8274 static int
8275 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8276 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8277 {
8278 	struct bnxt_tx_ring_info *txr;
8279 	int i, rc;
8280 
8281 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8282 		u16 ring_id;
8283 
8284 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8285 		req->ring_id = cpu_to_le16(ring_id);
8286 		rc = hwrm_req_send(bp, req);
8287 		if (rc)
8288 			return rc;
8289 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8290 			return 0;
8291 	}
8292 	return 0;
8293 }
8294 
8295 int bnxt_hwrm_set_coal(struct bnxt *bp)
8296 {
8297 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8298 	int i, rc;
8299 
8300 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8301 	if (rc)
8302 		return rc;
8303 
8304 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8305 	if (rc) {
8306 		hwrm_req_drop(bp, req_rx);
8307 		return rc;
8308 	}
8309 
8310 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8311 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8312 
8313 	hwrm_req_hold(bp, req_rx);
8314 	hwrm_req_hold(bp, req_tx);
8315 	for (i = 0; i < bp->cp_nr_rings; i++) {
8316 		struct bnxt_napi *bnapi = bp->bnapi[i];
8317 		struct bnxt_coal *hw_coal;
8318 
8319 		if (!bnapi->rx_ring)
8320 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8321 		else
8322 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8323 		if (rc)
8324 			break;
8325 
8326 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8327 			continue;
8328 
8329 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8330 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8331 			if (rc)
8332 				break;
8333 		}
8334 		if (bnapi->rx_ring)
8335 			hw_coal = &bp->rx_coal;
8336 		else
8337 			hw_coal = &bp->tx_coal;
8338 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8339 	}
8340 	hwrm_req_drop(bp, req_rx);
8341 	hwrm_req_drop(bp, req_tx);
8342 	return rc;
8343 }
8344 
8345 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8346 {
8347 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8348 	struct hwrm_stat_ctx_free_input *req;
8349 	int i;
8350 
8351 	if (!bp->bnapi)
8352 		return;
8353 
8354 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8355 		return;
8356 
8357 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8358 		return;
8359 	if (BNXT_FW_MAJ(bp) <= 20) {
8360 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8361 			hwrm_req_drop(bp, req);
8362 			return;
8363 		}
8364 		hwrm_req_hold(bp, req0);
8365 	}
8366 	hwrm_req_hold(bp, req);
8367 	for (i = 0; i < bp->cp_nr_rings; i++) {
8368 		struct bnxt_napi *bnapi = bp->bnapi[i];
8369 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8370 
8371 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8372 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8373 			if (req0) {
8374 				req0->stat_ctx_id = req->stat_ctx_id;
8375 				hwrm_req_send(bp, req0);
8376 			}
8377 			hwrm_req_send(bp, req);
8378 
8379 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8380 		}
8381 	}
8382 	hwrm_req_drop(bp, req);
8383 	if (req0)
8384 		hwrm_req_drop(bp, req0);
8385 }
8386 
8387 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8388 {
8389 	struct hwrm_stat_ctx_alloc_output *resp;
8390 	struct hwrm_stat_ctx_alloc_input *req;
8391 	int rc, i;
8392 
8393 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8394 		return 0;
8395 
8396 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8397 	if (rc)
8398 		return rc;
8399 
8400 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8401 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8402 
8403 	resp = hwrm_req_hold(bp, req);
8404 	for (i = 0; i < bp->cp_nr_rings; i++) {
8405 		struct bnxt_napi *bnapi = bp->bnapi[i];
8406 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8407 
8408 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8409 
8410 		rc = hwrm_req_send(bp, req);
8411 		if (rc)
8412 			break;
8413 
8414 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8415 
8416 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8417 	}
8418 	hwrm_req_drop(bp, req);
8419 	return rc;
8420 }
8421 
8422 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8423 {
8424 	struct hwrm_func_qcfg_output *resp;
8425 	struct hwrm_func_qcfg_input *req;
8426 	u16 flags;
8427 	int rc;
8428 
8429 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8430 	if (rc)
8431 		return rc;
8432 
8433 	req->fid = cpu_to_le16(0xffff);
8434 	resp = hwrm_req_hold(bp, req);
8435 	rc = hwrm_req_send(bp, req);
8436 	if (rc)
8437 		goto func_qcfg_exit;
8438 
8439 	flags = le16_to_cpu(resp->flags);
8440 #ifdef CONFIG_BNXT_SRIOV
8441 	if (BNXT_VF(bp)) {
8442 		struct bnxt_vf_info *vf = &bp->vf;
8443 
8444 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8445 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8446 			vf->flags |= BNXT_VF_TRUST;
8447 		else
8448 			vf->flags &= ~BNXT_VF_TRUST;
8449 	} else {
8450 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8451 	}
8452 #endif
8453 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8454 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8455 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8456 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8457 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8458 	}
8459 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8460 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8461 
8462 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8463 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8464 
8465 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8466 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8467 
8468 	switch (resp->port_partition_type) {
8469 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8470 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_2:
8471 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8472 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8473 		bp->port_partition_type = resp->port_partition_type;
8474 		break;
8475 	}
8476 	if (bp->hwrm_spec_code < 0x10707 ||
8477 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8478 		bp->br_mode = BRIDGE_MODE_VEB;
8479 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8480 		bp->br_mode = BRIDGE_MODE_VEPA;
8481 	else
8482 		bp->br_mode = BRIDGE_MODE_UNDEF;
8483 
8484 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8485 	if (!bp->max_mtu)
8486 		bp->max_mtu = BNXT_MAX_MTU;
8487 
8488 	if (bp->db_size)
8489 		goto func_qcfg_exit;
8490 
8491 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8492 	if (BNXT_CHIP_P5(bp)) {
8493 		if (BNXT_PF(bp))
8494 			bp->db_offset = DB_PF_OFFSET_P5;
8495 		else
8496 			bp->db_offset = DB_VF_OFFSET_P5;
8497 	}
8498 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8499 				 1024);
8500 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8501 	    bp->db_size <= bp->db_offset)
8502 		bp->db_size = pci_resource_len(bp->pdev, 2);
8503 
8504 func_qcfg_exit:
8505 	hwrm_req_drop(bp, req);
8506 	return rc;
8507 }
8508 
8509 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8510 				      u8 init_val, u8 init_offset,
8511 				      bool init_mask_set)
8512 {
8513 	ctxm->init_value = init_val;
8514 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8515 	if (init_mask_set)
8516 		ctxm->init_offset = init_offset * 4;
8517 	else
8518 		ctxm->init_value = 0;
8519 }
8520 
8521 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8522 {
8523 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8524 	u16 type;
8525 
8526 	for (type = 0; type < ctx_max; type++) {
8527 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8528 		int n = 1;
8529 
8530 		if (!ctxm->max_entries || ctxm->pg_info)
8531 			continue;
8532 
8533 		if (ctxm->instance_bmap)
8534 			n = hweight32(ctxm->instance_bmap);
8535 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8536 		if (!ctxm->pg_info)
8537 			return -ENOMEM;
8538 	}
8539 	return 0;
8540 }
8541 
8542 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8543 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8544 
8545 #define BNXT_CTX_INIT_VALID(flags)	\
8546 	(!!((flags) &			\
8547 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8548 
8549 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8550 {
8551 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8552 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8553 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8554 	u16 type;
8555 	int rc;
8556 
8557 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8558 	if (rc)
8559 		return rc;
8560 
8561 	if (!ctx) {
8562 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8563 		if (!ctx)
8564 			return -ENOMEM;
8565 		bp->ctx = ctx;
8566 	}
8567 
8568 	resp = hwrm_req_hold(bp, req);
8569 
8570 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8571 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8572 		u8 init_val, init_off, i;
8573 		u32 max_entries;
8574 		u16 entry_size;
8575 		__le32 *p;
8576 		u32 flags;
8577 
8578 		req->type = cpu_to_le16(type);
8579 		rc = hwrm_req_send(bp, req);
8580 		if (rc)
8581 			goto ctx_done;
8582 		flags = le32_to_cpu(resp->flags);
8583 		type = le16_to_cpu(resp->next_valid_type);
8584 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8585 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8586 			continue;
8587 		}
8588 		entry_size = le16_to_cpu(resp->entry_size);
8589 		max_entries = le32_to_cpu(resp->max_num_entries);
8590 		if (ctxm->mem_valid) {
8591 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8592 			    ctxm->entry_size != entry_size ||
8593 			    ctxm->max_entries != max_entries)
8594 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8595 			else
8596 				continue;
8597 		}
8598 		ctxm->type = le16_to_cpu(resp->type);
8599 		ctxm->entry_size = entry_size;
8600 		ctxm->flags = flags;
8601 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8602 		ctxm->entry_multiple = resp->entry_multiple;
8603 		ctxm->max_entries = max_entries;
8604 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8605 		init_val = resp->ctx_init_value;
8606 		init_off = resp->ctx_init_offset;
8607 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8608 					  BNXT_CTX_INIT_VALID(flags));
8609 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8610 					      BNXT_MAX_SPLIT_ENTRY);
8611 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8612 		     i++, p++)
8613 			ctxm->split[i] = le32_to_cpu(*p);
8614 	}
8615 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8616 
8617 ctx_done:
8618 	hwrm_req_drop(bp, req);
8619 	return rc;
8620 }
8621 
8622 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8623 {
8624 	struct hwrm_func_backing_store_qcaps_output *resp;
8625 	struct hwrm_func_backing_store_qcaps_input *req;
8626 	int rc;
8627 
8628 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8629 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8630 		return 0;
8631 
8632 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8633 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8634 
8635 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8636 	if (rc)
8637 		return rc;
8638 
8639 	resp = hwrm_req_hold(bp, req);
8640 	rc = hwrm_req_send_silent(bp, req);
8641 	if (!rc) {
8642 		struct bnxt_ctx_mem_type *ctxm;
8643 		struct bnxt_ctx_mem_info *ctx;
8644 		u8 init_val, init_idx = 0;
8645 		u16 init_mask;
8646 
8647 		ctx = bp->ctx;
8648 		if (!ctx) {
8649 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8650 			if (!ctx) {
8651 				rc = -ENOMEM;
8652 				goto ctx_err;
8653 			}
8654 			bp->ctx = ctx;
8655 		}
8656 		init_val = resp->ctx_kind_initializer;
8657 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8658 
8659 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8660 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8661 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8662 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8663 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8664 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8665 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8666 					  (init_mask & (1 << init_idx++)) != 0);
8667 
8668 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8669 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8670 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8671 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8672 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8673 					  (init_mask & (1 << init_idx++)) != 0);
8674 
8675 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8676 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8677 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8678 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8679 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8680 					  (init_mask & (1 << init_idx++)) != 0);
8681 
8682 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8683 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8684 		ctxm->max_entries = ctxm->vnic_entries +
8685 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8686 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8687 		bnxt_init_ctx_initializer(ctxm, init_val,
8688 					  resp->vnic_init_offset,
8689 					  (init_mask & (1 << init_idx++)) != 0);
8690 
8691 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8692 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8693 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8694 		bnxt_init_ctx_initializer(ctxm, init_val,
8695 					  resp->stat_init_offset,
8696 					  (init_mask & (1 << init_idx++)) != 0);
8697 
8698 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8699 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8700 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8701 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8702 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8703 		if (!ctxm->entry_multiple)
8704 			ctxm->entry_multiple = 1;
8705 
8706 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8707 
8708 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8709 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8710 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8711 		ctxm->mrav_num_entries_units =
8712 			le16_to_cpu(resp->mrav_num_entries_units);
8713 		bnxt_init_ctx_initializer(ctxm, init_val,
8714 					  resp->mrav_init_offset,
8715 					  (init_mask & (1 << init_idx++)) != 0);
8716 
8717 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8718 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8719 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8720 
8721 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8722 		if (!ctx->tqm_fp_rings_count)
8723 			ctx->tqm_fp_rings_count = bp->max_q;
8724 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8725 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8726 
8727 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8728 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8729 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8730 
8731 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8732 	} else {
8733 		rc = 0;
8734 	}
8735 ctx_err:
8736 	hwrm_req_drop(bp, req);
8737 	return rc;
8738 }
8739 
8740 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8741 				  __le64 *pg_dir)
8742 {
8743 	if (!rmem->nr_pages)
8744 		return;
8745 
8746 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8747 	if (rmem->depth >= 1) {
8748 		if (rmem->depth == 2)
8749 			*pg_attr |= 2;
8750 		else
8751 			*pg_attr |= 1;
8752 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8753 	} else {
8754 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8755 	}
8756 }
8757 
8758 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8759 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8760 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8761 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8762 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8763 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8764 
8765 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8766 {
8767 	struct hwrm_func_backing_store_cfg_input *req;
8768 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8769 	struct bnxt_ctx_pg_info *ctx_pg;
8770 	struct bnxt_ctx_mem_type *ctxm;
8771 	void **__req = (void **)&req;
8772 	u32 req_len = sizeof(*req);
8773 	__le32 *num_entries;
8774 	__le64 *pg_dir;
8775 	u32 flags = 0;
8776 	u8 *pg_attr;
8777 	u32 ena;
8778 	int rc;
8779 	int i;
8780 
8781 	if (!ctx)
8782 		return 0;
8783 
8784 	if (req_len > bp->hwrm_max_ext_req_len)
8785 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8786 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8787 	if (rc)
8788 		return rc;
8789 
8790 	req->enables = cpu_to_le32(enables);
8791 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8792 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8793 		ctx_pg = ctxm->pg_info;
8794 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8795 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8796 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8797 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8798 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8799 				      &req->qpc_pg_size_qpc_lvl,
8800 				      &req->qpc_page_dir);
8801 
8802 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8803 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8804 	}
8805 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8806 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8807 		ctx_pg = ctxm->pg_info;
8808 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8809 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8810 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8811 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8812 				      &req->srq_pg_size_srq_lvl,
8813 				      &req->srq_page_dir);
8814 	}
8815 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8816 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8817 		ctx_pg = ctxm->pg_info;
8818 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8819 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8820 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8821 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8822 				      &req->cq_pg_size_cq_lvl,
8823 				      &req->cq_page_dir);
8824 	}
8825 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8826 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8827 		ctx_pg = ctxm->pg_info;
8828 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8829 		req->vnic_num_ring_table_entries =
8830 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8831 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8832 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8833 				      &req->vnic_pg_size_vnic_lvl,
8834 				      &req->vnic_page_dir);
8835 	}
8836 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8837 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8838 		ctx_pg = ctxm->pg_info;
8839 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8840 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8841 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8842 				      &req->stat_pg_size_stat_lvl,
8843 				      &req->stat_page_dir);
8844 	}
8845 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8846 		u32 units;
8847 
8848 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8849 		ctx_pg = ctxm->pg_info;
8850 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8851 		units = ctxm->mrav_num_entries_units;
8852 		if (units) {
8853 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8854 			u32 entries;
8855 
8856 			num_mr = ctx_pg->entries - num_ah;
8857 			entries = ((num_mr / units) << 16) | (num_ah / units);
8858 			req->mrav_num_entries = cpu_to_le32(entries);
8859 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8860 		}
8861 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8862 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8863 				      &req->mrav_pg_size_mrav_lvl,
8864 				      &req->mrav_page_dir);
8865 	}
8866 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8867 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8868 		ctx_pg = ctxm->pg_info;
8869 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8870 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8871 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8872 				      &req->tim_pg_size_tim_lvl,
8873 				      &req->tim_page_dir);
8874 	}
8875 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8876 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8877 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8878 	     pg_dir = &req->tqm_sp_page_dir,
8879 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8880 	     ctx_pg = ctxm->pg_info;
8881 	     i < BNXT_MAX_TQM_RINGS;
8882 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8883 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8884 		if (!(enables & ena))
8885 			continue;
8886 
8887 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8888 		*num_entries = cpu_to_le32(ctx_pg->entries);
8889 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8890 	}
8891 	req->flags = cpu_to_le32(flags);
8892 	return hwrm_req_send(bp, req);
8893 }
8894 
8895 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8896 				  struct bnxt_ctx_pg_info *ctx_pg)
8897 {
8898 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8899 
8900 	rmem->page_size = BNXT_PAGE_SIZE;
8901 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8902 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8903 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8904 	if (rmem->depth >= 1)
8905 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8906 	return bnxt_alloc_ring(bp, rmem);
8907 }
8908 
8909 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8910 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8911 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8912 {
8913 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8914 	int rc;
8915 
8916 	if (!mem_size)
8917 		return -EINVAL;
8918 
8919 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8920 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8921 		ctx_pg->nr_pages = 0;
8922 		return -EINVAL;
8923 	}
8924 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8925 		int nr_tbls, i;
8926 
8927 		rmem->depth = 2;
8928 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8929 					     GFP_KERNEL);
8930 		if (!ctx_pg->ctx_pg_tbl)
8931 			return -ENOMEM;
8932 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8933 		rmem->nr_pages = nr_tbls;
8934 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8935 		if (rc)
8936 			return rc;
8937 		for (i = 0; i < nr_tbls; i++) {
8938 			struct bnxt_ctx_pg_info *pg_tbl;
8939 
8940 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8941 			if (!pg_tbl)
8942 				return -ENOMEM;
8943 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8944 			rmem = &pg_tbl->ring_mem;
8945 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8946 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8947 			rmem->depth = 1;
8948 			rmem->nr_pages = MAX_CTX_PAGES;
8949 			rmem->ctx_mem = ctxm;
8950 			if (i == (nr_tbls - 1)) {
8951 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8952 
8953 				if (rem)
8954 					rmem->nr_pages = rem;
8955 			}
8956 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8957 			if (rc)
8958 				break;
8959 		}
8960 	} else {
8961 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8962 		if (rmem->nr_pages > 1 || depth)
8963 			rmem->depth = 1;
8964 		rmem->ctx_mem = ctxm;
8965 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8966 	}
8967 	return rc;
8968 }
8969 
8970 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8971 				    struct bnxt_ctx_pg_info *ctx_pg,
8972 				    void *buf, size_t offset, size_t head,
8973 				    size_t tail)
8974 {
8975 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8976 	size_t nr_pages = ctx_pg->nr_pages;
8977 	int page_size = rmem->page_size;
8978 	size_t len = 0, total_len = 0;
8979 	u16 depth = rmem->depth;
8980 
8981 	tail %= nr_pages * page_size;
8982 	do {
8983 		if (depth > 1) {
8984 			int i = head / (page_size * MAX_CTX_PAGES);
8985 			struct bnxt_ctx_pg_info *pg_tbl;
8986 
8987 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8988 			rmem = &pg_tbl->ring_mem;
8989 		}
8990 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8991 		head += len;
8992 		offset += len;
8993 		total_len += len;
8994 		if (head >= nr_pages * page_size)
8995 			head = 0;
8996 	} while (head != tail);
8997 	return total_len;
8998 }
8999 
9000 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
9001 				  struct bnxt_ctx_pg_info *ctx_pg)
9002 {
9003 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
9004 
9005 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
9006 	    ctx_pg->ctx_pg_tbl) {
9007 		int i, nr_tbls = rmem->nr_pages;
9008 
9009 		for (i = 0; i < nr_tbls; i++) {
9010 			struct bnxt_ctx_pg_info *pg_tbl;
9011 			struct bnxt_ring_mem_info *rmem2;
9012 
9013 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
9014 			if (!pg_tbl)
9015 				continue;
9016 			rmem2 = &pg_tbl->ring_mem;
9017 			bnxt_free_ring(bp, rmem2);
9018 			ctx_pg->ctx_pg_arr[i] = NULL;
9019 			kfree(pg_tbl);
9020 			ctx_pg->ctx_pg_tbl[i] = NULL;
9021 		}
9022 		kfree(ctx_pg->ctx_pg_tbl);
9023 		ctx_pg->ctx_pg_tbl = NULL;
9024 	}
9025 	bnxt_free_ring(bp, rmem);
9026 	ctx_pg->nr_pages = 0;
9027 }
9028 
9029 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
9030 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
9031 				   u8 pg_lvl)
9032 {
9033 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9034 	int i, rc = 0, n = 1;
9035 	u32 mem_size;
9036 
9037 	if (!ctxm->entry_size || !ctx_pg)
9038 		return -EINVAL;
9039 	if (ctxm->instance_bmap)
9040 		n = hweight32(ctxm->instance_bmap);
9041 	if (ctxm->entry_multiple)
9042 		entries = roundup(entries, ctxm->entry_multiple);
9043 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
9044 	mem_size = entries * ctxm->entry_size;
9045 	for (i = 0; i < n && !rc; i++) {
9046 		ctx_pg[i].entries = entries;
9047 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
9048 					    ctxm->init_value ? ctxm : NULL);
9049 	}
9050 	if (!rc)
9051 		ctxm->mem_valid = 1;
9052 	return rc;
9053 }
9054 
9055 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
9056 					       struct bnxt_ctx_mem_type *ctxm,
9057 					       bool last)
9058 {
9059 	struct hwrm_func_backing_store_cfg_v2_input *req;
9060 	u32 instance_bmap = ctxm->instance_bmap;
9061 	int i, j, rc = 0, n = 1;
9062 	__le32 *p;
9063 
9064 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
9065 		return 0;
9066 
9067 	if (instance_bmap)
9068 		n = hweight32(ctxm->instance_bmap);
9069 	else
9070 		instance_bmap = 1;
9071 
9072 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
9073 	if (rc)
9074 		return rc;
9075 	hwrm_req_hold(bp, req);
9076 	req->type = cpu_to_le16(ctxm->type);
9077 	req->entry_size = cpu_to_le16(ctxm->entry_size);
9078 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
9079 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
9080 		struct bnxt_bs_trace_info *bs_trace;
9081 		u32 enables;
9082 
9083 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
9084 		req->enables = cpu_to_le32(enables);
9085 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
9086 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
9087 	}
9088 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
9089 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
9090 		p[i] = cpu_to_le32(ctxm->split[i]);
9091 	for (i = 0, j = 0; j < n && !rc; i++) {
9092 		struct bnxt_ctx_pg_info *ctx_pg;
9093 
9094 		if (!(instance_bmap & (1 << i)))
9095 			continue;
9096 		req->instance = cpu_to_le16(i);
9097 		ctx_pg = &ctxm->pg_info[j++];
9098 		if (!ctx_pg->entries)
9099 			continue;
9100 		req->num_entries = cpu_to_le32(ctx_pg->entries);
9101 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
9102 				      &req->page_size_pbl_level,
9103 				      &req->page_dir);
9104 		if (last && j == n)
9105 			req->flags =
9106 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9107 		rc = hwrm_req_send(bp, req);
9108 	}
9109 	hwrm_req_drop(bp, req);
9110 	return rc;
9111 }
9112 
9113 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9114 {
9115 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9116 	struct bnxt_ctx_mem_type *ctxm;
9117 	u16 last_type = BNXT_CTX_INV;
9118 	int rc = 0;
9119 	u16 type;
9120 
9121 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9122 		ctxm = &ctx->ctx_arr[type];
9123 		if (!bnxt_bs_trace_avail(bp, type))
9124 			continue;
9125 		if (!ctxm->mem_valid) {
9126 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9127 						     ctxm->max_entries, 1);
9128 			if (rc) {
9129 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9130 					    type);
9131 				continue;
9132 			}
9133 			bnxt_bs_trace_init(bp, ctxm);
9134 		}
9135 		last_type = type;
9136 	}
9137 
9138 	if (last_type == BNXT_CTX_INV) {
9139 		if (!ena)
9140 			return 0;
9141 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9142 			last_type = BNXT_CTX_MAX - 1;
9143 		else
9144 			last_type = BNXT_CTX_L2_MAX - 1;
9145 	}
9146 	ctx->ctx_arr[last_type].last = 1;
9147 
9148 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9149 		ctxm = &ctx->ctx_arr[type];
9150 
9151 		if (!ctxm->mem_valid)
9152 			continue;
9153 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9154 		if (rc)
9155 			return rc;
9156 	}
9157 	return 0;
9158 }
9159 
9160 /**
9161  * __bnxt_copy_ctx_mem - copy host context memory
9162  * @bp: The driver context
9163  * @ctxm: The pointer to the context memory type
9164  * @buf: The destination buffer or NULL to just obtain the length
9165  * @offset: The buffer offset to copy the data to
9166  * @head: The head offset of context memory to copy from
9167  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9168  *
9169  * This function is called for debugging purposes to dump the host context
9170  * used by the chip.
9171  *
9172  * Return: Length of memory copied
9173  */
9174 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9175 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9176 				  size_t offset, size_t head, size_t tail)
9177 {
9178 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9179 	size_t len = 0, total_len = 0;
9180 	int i, n = 1;
9181 
9182 	if (!ctx_pg)
9183 		return 0;
9184 
9185 	if (ctxm->instance_bmap)
9186 		n = hweight32(ctxm->instance_bmap);
9187 	for (i = 0; i < n; i++) {
9188 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9189 					    tail);
9190 		offset += len;
9191 		total_len += len;
9192 	}
9193 	return total_len;
9194 }
9195 
9196 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9197 			 void *buf, size_t offset)
9198 {
9199 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9200 
9201 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9202 }
9203 
9204 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9205 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9206 {
9207 	struct bnxt_ctx_pg_info *ctx_pg;
9208 	int i, n = 1;
9209 
9210 	ctxm->last = 0;
9211 
9212 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9213 		return;
9214 
9215 	ctx_pg = ctxm->pg_info;
9216 	if (ctx_pg) {
9217 		if (ctxm->instance_bmap)
9218 			n = hweight32(ctxm->instance_bmap);
9219 		for (i = 0; i < n; i++)
9220 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9221 
9222 		kfree(ctx_pg);
9223 		ctxm->pg_info = NULL;
9224 		ctxm->mem_valid = 0;
9225 	}
9226 	memset(ctxm, 0, sizeof(*ctxm));
9227 }
9228 
9229 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9230 {
9231 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9232 	u16 type;
9233 
9234 	if (!ctx)
9235 		return;
9236 
9237 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9238 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9239 
9240 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9241 	if (force) {
9242 		kfree(ctx);
9243 		bp->ctx = NULL;
9244 	}
9245 }
9246 
9247 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9248 {
9249 	struct bnxt_ctx_mem_type *ctxm;
9250 	struct bnxt_ctx_mem_info *ctx;
9251 	u32 l2_qps, qp1_qps, max_qps;
9252 	u32 ena, entries_sp, entries;
9253 	u32 srqs, max_srqs, min;
9254 	u32 num_mr, num_ah;
9255 	u32 extra_srqs = 0;
9256 	u32 extra_qps = 0;
9257 	u32 fast_qpmd_qps;
9258 	u8 pg_lvl = 1;
9259 	int i, rc;
9260 
9261 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9262 	if (rc) {
9263 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9264 			   rc);
9265 		return rc;
9266 	}
9267 	ctx = bp->ctx;
9268 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9269 		return 0;
9270 
9271 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9272 	l2_qps = ctxm->qp_l2_entries;
9273 	qp1_qps = ctxm->qp_qp1_entries;
9274 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9275 	max_qps = ctxm->max_entries;
9276 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9277 	srqs = ctxm->srq_l2_entries;
9278 	max_srqs = ctxm->max_entries;
9279 	ena = 0;
9280 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9281 		pg_lvl = 2;
9282 		if (BNXT_SW_RES_LMT(bp)) {
9283 			extra_qps = max_qps - l2_qps - qp1_qps;
9284 			extra_srqs = max_srqs - srqs;
9285 		} else {
9286 			extra_qps = min_t(u32, 65536,
9287 					  max_qps - l2_qps - qp1_qps);
9288 			/* allocate extra qps if fw supports RoCE fast qp
9289 			 * destroy feature
9290 			 */
9291 			extra_qps += fast_qpmd_qps;
9292 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9293 		}
9294 		if (fast_qpmd_qps)
9295 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9296 	}
9297 
9298 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9299 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9300 				     pg_lvl);
9301 	if (rc)
9302 		return rc;
9303 
9304 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9305 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9306 	if (rc)
9307 		return rc;
9308 
9309 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9310 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9311 				     extra_qps * 2, pg_lvl);
9312 	if (rc)
9313 		return rc;
9314 
9315 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9316 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9317 	if (rc)
9318 		return rc;
9319 
9320 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9321 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9322 	if (rc)
9323 		return rc;
9324 
9325 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9326 		goto skip_rdma;
9327 
9328 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9329 	if (BNXT_SW_RES_LMT(bp) &&
9330 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9331 		num_ah = ctxm->mrav_av_entries;
9332 		num_mr = ctxm->max_entries - num_ah;
9333 	} else {
9334 		/* 128K extra is needed to accommodate static AH context
9335 		 * allocation by f/w.
9336 		 */
9337 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9338 		num_ah = min_t(u32, num_mr, 1024 * 128);
9339 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9340 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9341 			ctxm->mrav_av_entries = num_ah;
9342 	}
9343 
9344 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9345 	if (rc)
9346 		return rc;
9347 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9348 
9349 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9350 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9351 	if (rc)
9352 		return rc;
9353 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9354 
9355 skip_rdma:
9356 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9357 	min = ctxm->min_entries;
9358 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9359 		     2 * (extra_qps + qp1_qps) + min;
9360 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9361 	if (rc)
9362 		return rc;
9363 
9364 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9365 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9366 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9367 	if (rc)
9368 		return rc;
9369 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9370 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9371 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9372 
9373 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9374 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9375 	else
9376 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9377 	if (rc) {
9378 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9379 			   rc);
9380 		return rc;
9381 	}
9382 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9383 	return 0;
9384 }
9385 
9386 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9387 {
9388 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9389 	u16 page_attr;
9390 	int rc;
9391 
9392 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9393 		return 0;
9394 
9395 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9396 	if (rc)
9397 		return rc;
9398 
9399 	if (BNXT_PAGE_SIZE == 0x2000)
9400 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9401 	else if (BNXT_PAGE_SIZE == 0x10000)
9402 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9403 	else
9404 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9405 	req->pg_size_lvl = cpu_to_le16(page_attr |
9406 				       bp->fw_crash_mem->ring_mem.depth);
9407 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9408 	req->size = cpu_to_le32(bp->fw_crash_len);
9409 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9410 	return hwrm_req_send(bp, req);
9411 }
9412 
9413 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9414 {
9415 	if (bp->fw_crash_mem) {
9416 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9417 		kfree(bp->fw_crash_mem);
9418 		bp->fw_crash_mem = NULL;
9419 	}
9420 }
9421 
9422 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9423 {
9424 	u32 mem_size = 0;
9425 	int rc;
9426 
9427 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9428 		return 0;
9429 
9430 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9431 	if (rc)
9432 		return rc;
9433 
9434 	mem_size = round_up(mem_size, 4);
9435 
9436 	/* keep and use the existing pages */
9437 	if (bp->fw_crash_mem &&
9438 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9439 		goto alloc_done;
9440 
9441 	if (bp->fw_crash_mem)
9442 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9443 	else
9444 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9445 					   GFP_KERNEL);
9446 	if (!bp->fw_crash_mem)
9447 		return -ENOMEM;
9448 
9449 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9450 	if (rc) {
9451 		bnxt_free_crash_dump_mem(bp);
9452 		return rc;
9453 	}
9454 
9455 alloc_done:
9456 	bp->fw_crash_len = mem_size;
9457 	return 0;
9458 }
9459 
9460 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9461 {
9462 	struct hwrm_func_resource_qcaps_output *resp;
9463 	struct hwrm_func_resource_qcaps_input *req;
9464 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9465 	int rc;
9466 
9467 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9468 	if (rc)
9469 		return rc;
9470 
9471 	req->fid = cpu_to_le16(0xffff);
9472 	resp = hwrm_req_hold(bp, req);
9473 	rc = hwrm_req_send_silent(bp, req);
9474 	if (rc)
9475 		goto hwrm_func_resc_qcaps_exit;
9476 
9477 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9478 	if (!all)
9479 		goto hwrm_func_resc_qcaps_exit;
9480 
9481 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9482 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9483 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9484 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9485 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9486 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9487 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9488 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9489 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9490 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9491 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9492 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9493 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9494 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9495 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9496 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9497 
9498 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9499 		u16 max_msix = le16_to_cpu(resp->max_msix);
9500 
9501 		hw_resc->max_nqs = max_msix;
9502 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9503 	}
9504 
9505 	if (BNXT_PF(bp)) {
9506 		struct bnxt_pf_info *pf = &bp->pf;
9507 
9508 		pf->vf_resv_strategy =
9509 			le16_to_cpu(resp->vf_reservation_strategy);
9510 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9511 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9512 	}
9513 hwrm_func_resc_qcaps_exit:
9514 	hwrm_req_drop(bp, req);
9515 	return rc;
9516 }
9517 
9518 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9519 {
9520 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9521 	struct hwrm_port_mac_ptp_qcfg_input *req;
9522 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9523 	u8 flags;
9524 	int rc;
9525 
9526 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9527 		rc = -ENODEV;
9528 		goto no_ptp;
9529 	}
9530 
9531 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9532 	if (rc)
9533 		goto no_ptp;
9534 
9535 	req->port_id = cpu_to_le16(bp->pf.port_id);
9536 	resp = hwrm_req_hold(bp, req);
9537 	rc = hwrm_req_send(bp, req);
9538 	if (rc)
9539 		goto exit;
9540 
9541 	flags = resp->flags;
9542 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9543 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9544 		rc = -ENODEV;
9545 		goto exit;
9546 	}
9547 	if (!ptp) {
9548 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9549 		if (!ptp) {
9550 			rc = -ENOMEM;
9551 			goto exit;
9552 		}
9553 		ptp->bp = bp;
9554 		bp->ptp_cfg = ptp;
9555 	}
9556 
9557 	if (flags &
9558 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9559 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9560 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9561 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9562 	} else if (BNXT_CHIP_P5(bp)) {
9563 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9564 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9565 	} else {
9566 		rc = -ENODEV;
9567 		goto exit;
9568 	}
9569 	ptp->rtc_configured =
9570 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9571 	rc = bnxt_ptp_init(bp);
9572 	if (rc)
9573 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9574 exit:
9575 	hwrm_req_drop(bp, req);
9576 	if (!rc)
9577 		return 0;
9578 
9579 no_ptp:
9580 	bnxt_ptp_clear(bp);
9581 	kfree(ptp);
9582 	bp->ptp_cfg = NULL;
9583 	return rc;
9584 }
9585 
9586 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9587 {
9588 	struct hwrm_func_qcaps_output *resp;
9589 	struct hwrm_func_qcaps_input *req;
9590 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9591 	u32 flags, flags_ext, flags_ext2;
9592 	int rc;
9593 
9594 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9595 	if (rc)
9596 		return rc;
9597 
9598 	req->fid = cpu_to_le16(0xffff);
9599 	resp = hwrm_req_hold(bp, req);
9600 	rc = hwrm_req_send(bp, req);
9601 	if (rc)
9602 		goto hwrm_func_qcaps_exit;
9603 
9604 	flags = le32_to_cpu(resp->flags);
9605 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9606 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9607 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9608 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9609 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9610 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9611 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9612 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9613 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9614 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9615 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9616 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9617 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9618 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9619 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9620 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9621 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9622 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9623 
9624 	flags_ext = le32_to_cpu(resp->flags_ext);
9625 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9626 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9627 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9628 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9629 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9630 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9631 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9632 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9633 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9634 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9635 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_NPAR_1_2_SUPPORTED)
9636 		bp->fw_cap |= BNXT_FW_CAP_NPAR_1_2;
9637 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9638 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9639 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9640 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9641 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9642 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9643 
9644 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9645 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9646 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9647 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9648 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9649 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9650 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9651 	if (flags_ext2 &
9652 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9653 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9654 	if (BNXT_PF(bp) &&
9655 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9656 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9657 
9658 	bp->tx_push_thresh = 0;
9659 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9660 	    BNXT_FW_MAJ(bp) > 217)
9661 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9662 
9663 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9664 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9665 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9666 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9667 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9668 	if (!hw_resc->max_hw_ring_grps)
9669 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9670 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9671 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9672 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9673 
9674 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9675 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9676 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9677 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9678 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9679 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9680 
9681 	if (BNXT_PF(bp)) {
9682 		struct bnxt_pf_info *pf = &bp->pf;
9683 
9684 		pf->fw_fid = le16_to_cpu(resp->fid);
9685 		pf->port_id = le16_to_cpu(resp->port_id);
9686 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9687 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9688 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9689 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9690 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9691 			bp->flags |= BNXT_FLAG_WOL_CAP;
9692 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9693 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9694 		} else {
9695 			bnxt_ptp_clear(bp);
9696 			kfree(bp->ptp_cfg);
9697 			bp->ptp_cfg = NULL;
9698 		}
9699 	} else {
9700 #ifdef CONFIG_BNXT_SRIOV
9701 		struct bnxt_vf_info *vf = &bp->vf;
9702 
9703 		vf->fw_fid = le16_to_cpu(resp->fid);
9704 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9705 #endif
9706 	}
9707 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9708 
9709 hwrm_func_qcaps_exit:
9710 	hwrm_req_drop(bp, req);
9711 	return rc;
9712 }
9713 
9714 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9715 {
9716 	struct hwrm_dbg_qcaps_output *resp;
9717 	struct hwrm_dbg_qcaps_input *req;
9718 	int rc;
9719 
9720 	bp->fw_dbg_cap = 0;
9721 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9722 		return;
9723 
9724 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9725 	if (rc)
9726 		return;
9727 
9728 	req->fid = cpu_to_le16(0xffff);
9729 	resp = hwrm_req_hold(bp, req);
9730 	rc = hwrm_req_send(bp, req);
9731 	if (rc)
9732 		goto hwrm_dbg_qcaps_exit;
9733 
9734 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9735 
9736 hwrm_dbg_qcaps_exit:
9737 	hwrm_req_drop(bp, req);
9738 }
9739 
9740 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9741 
9742 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9743 {
9744 	int rc;
9745 
9746 	rc = __bnxt_hwrm_func_qcaps(bp);
9747 	if (rc)
9748 		return rc;
9749 
9750 	bnxt_hwrm_dbg_qcaps(bp);
9751 
9752 	rc = bnxt_hwrm_queue_qportcfg(bp);
9753 	if (rc) {
9754 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9755 		return rc;
9756 	}
9757 	if (bp->hwrm_spec_code >= 0x10803) {
9758 		rc = bnxt_alloc_ctx_mem(bp);
9759 		if (rc)
9760 			return rc;
9761 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9762 		if (!rc)
9763 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9764 	}
9765 	return 0;
9766 }
9767 
9768 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9769 {
9770 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9771 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9772 	u32 flags;
9773 	int rc;
9774 
9775 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9776 		return 0;
9777 
9778 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9779 	if (rc)
9780 		return rc;
9781 
9782 	resp = hwrm_req_hold(bp, req);
9783 	rc = hwrm_req_send(bp, req);
9784 	if (rc)
9785 		goto hwrm_cfa_adv_qcaps_exit;
9786 
9787 	flags = le32_to_cpu(resp->flags);
9788 	if (flags &
9789 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9790 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9791 
9792 	if (flags &
9793 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9794 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9795 
9796 	if (flags &
9797 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9798 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9799 
9800 hwrm_cfa_adv_qcaps_exit:
9801 	hwrm_req_drop(bp, req);
9802 	return rc;
9803 }
9804 
9805 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9806 {
9807 	if (bp->fw_health)
9808 		return 0;
9809 
9810 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9811 	if (!bp->fw_health)
9812 		return -ENOMEM;
9813 
9814 	mutex_init(&bp->fw_health->lock);
9815 	return 0;
9816 }
9817 
9818 static int bnxt_alloc_fw_health(struct bnxt *bp)
9819 {
9820 	int rc;
9821 
9822 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9823 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9824 		return 0;
9825 
9826 	rc = __bnxt_alloc_fw_health(bp);
9827 	if (rc) {
9828 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9829 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9830 		return rc;
9831 	}
9832 
9833 	return 0;
9834 }
9835 
9836 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9837 {
9838 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9839 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9840 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9841 }
9842 
9843 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9844 {
9845 	struct bnxt_fw_health *fw_health = bp->fw_health;
9846 	u32 reg_type;
9847 
9848 	if (!fw_health)
9849 		return;
9850 
9851 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9852 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9853 		fw_health->status_reliable = false;
9854 
9855 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9856 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9857 		fw_health->resets_reliable = false;
9858 }
9859 
9860 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9861 {
9862 	void __iomem *hs;
9863 	u32 status_loc;
9864 	u32 reg_type;
9865 	u32 sig;
9866 
9867 	if (bp->fw_health)
9868 		bp->fw_health->status_reliable = false;
9869 
9870 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9871 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9872 
9873 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9874 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9875 		if (!bp->chip_num) {
9876 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9877 			bp->chip_num = readl(bp->bar0 +
9878 					     BNXT_FW_HEALTH_WIN_BASE +
9879 					     BNXT_GRC_REG_CHIP_NUM);
9880 		}
9881 		if (!BNXT_CHIP_P5_PLUS(bp))
9882 			return;
9883 
9884 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9885 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9886 	} else {
9887 		status_loc = readl(hs + offsetof(struct hcomm_status,
9888 						 fw_status_loc));
9889 	}
9890 
9891 	if (__bnxt_alloc_fw_health(bp)) {
9892 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9893 		return;
9894 	}
9895 
9896 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9897 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9898 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9899 		__bnxt_map_fw_health_reg(bp, status_loc);
9900 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9901 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9902 	}
9903 
9904 	bp->fw_health->status_reliable = true;
9905 }
9906 
9907 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9908 {
9909 	struct bnxt_fw_health *fw_health = bp->fw_health;
9910 	u32 reg_base = 0xffffffff;
9911 	int i;
9912 
9913 	bp->fw_health->status_reliable = false;
9914 	bp->fw_health->resets_reliable = false;
9915 	/* Only pre-map the monitoring GRC registers using window 3 */
9916 	for (i = 0; i < 4; i++) {
9917 		u32 reg = fw_health->regs[i];
9918 
9919 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9920 			continue;
9921 		if (reg_base == 0xffffffff)
9922 			reg_base = reg & BNXT_GRC_BASE_MASK;
9923 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9924 			return -ERANGE;
9925 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9926 	}
9927 	bp->fw_health->status_reliable = true;
9928 	bp->fw_health->resets_reliable = true;
9929 	if (reg_base == 0xffffffff)
9930 		return 0;
9931 
9932 	__bnxt_map_fw_health_reg(bp, reg_base);
9933 	return 0;
9934 }
9935 
9936 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9937 {
9938 	if (!bp->fw_health)
9939 		return;
9940 
9941 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9942 		bp->fw_health->status_reliable = true;
9943 		bp->fw_health->resets_reliable = true;
9944 	} else {
9945 		bnxt_try_map_fw_health_reg(bp);
9946 	}
9947 }
9948 
9949 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9950 {
9951 	struct bnxt_fw_health *fw_health = bp->fw_health;
9952 	struct hwrm_error_recovery_qcfg_output *resp;
9953 	struct hwrm_error_recovery_qcfg_input *req;
9954 	int rc, i;
9955 
9956 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9957 		return 0;
9958 
9959 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9960 	if (rc)
9961 		return rc;
9962 
9963 	resp = hwrm_req_hold(bp, req);
9964 	rc = hwrm_req_send(bp, req);
9965 	if (rc)
9966 		goto err_recovery_out;
9967 	fw_health->flags = le32_to_cpu(resp->flags);
9968 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9969 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9970 		rc = -EINVAL;
9971 		goto err_recovery_out;
9972 	}
9973 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9974 	fw_health->master_func_wait_dsecs =
9975 		le32_to_cpu(resp->master_func_wait_period);
9976 	fw_health->normal_func_wait_dsecs =
9977 		le32_to_cpu(resp->normal_func_wait_period);
9978 	fw_health->post_reset_wait_dsecs =
9979 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9980 	fw_health->post_reset_max_wait_dsecs =
9981 		le32_to_cpu(resp->max_bailout_time_after_reset);
9982 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9983 		le32_to_cpu(resp->fw_health_status_reg);
9984 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9985 		le32_to_cpu(resp->fw_heartbeat_reg);
9986 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9987 		le32_to_cpu(resp->fw_reset_cnt_reg);
9988 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9989 		le32_to_cpu(resp->reset_inprogress_reg);
9990 	fw_health->fw_reset_inprog_reg_mask =
9991 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9992 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9993 	if (fw_health->fw_reset_seq_cnt >= 16) {
9994 		rc = -EINVAL;
9995 		goto err_recovery_out;
9996 	}
9997 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9998 		fw_health->fw_reset_seq_regs[i] =
9999 			le32_to_cpu(resp->reset_reg[i]);
10000 		fw_health->fw_reset_seq_vals[i] =
10001 			le32_to_cpu(resp->reset_reg_val[i]);
10002 		fw_health->fw_reset_seq_delay_msec[i] =
10003 			resp->delay_after_reset[i];
10004 	}
10005 err_recovery_out:
10006 	hwrm_req_drop(bp, req);
10007 	if (!rc)
10008 		rc = bnxt_map_fw_health_regs(bp);
10009 	if (rc)
10010 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
10011 	return rc;
10012 }
10013 
10014 static int bnxt_hwrm_func_reset(struct bnxt *bp)
10015 {
10016 	struct hwrm_func_reset_input *req;
10017 	int rc;
10018 
10019 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
10020 	if (rc)
10021 		return rc;
10022 
10023 	req->enables = 0;
10024 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
10025 	return hwrm_req_send(bp, req);
10026 }
10027 
10028 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
10029 {
10030 	struct hwrm_nvm_get_dev_info_output nvm_info;
10031 
10032 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
10033 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
10034 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
10035 			 nvm_info.nvm_cfg_ver_upd);
10036 }
10037 
10038 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
10039 {
10040 	struct hwrm_queue_qportcfg_output *resp;
10041 	struct hwrm_queue_qportcfg_input *req;
10042 	u8 i, j, *qptr;
10043 	bool no_rdma;
10044 	int rc = 0;
10045 
10046 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
10047 	if (rc)
10048 		return rc;
10049 
10050 	resp = hwrm_req_hold(bp, req);
10051 	rc = hwrm_req_send(bp, req);
10052 	if (rc)
10053 		goto qportcfg_exit;
10054 
10055 	if (!resp->max_configurable_queues) {
10056 		rc = -EINVAL;
10057 		goto qportcfg_exit;
10058 	}
10059 	bp->max_tc = resp->max_configurable_queues;
10060 	bp->max_lltc = resp->max_configurable_lossless_queues;
10061 	if (bp->max_tc > BNXT_MAX_QUEUE)
10062 		bp->max_tc = BNXT_MAX_QUEUE;
10063 
10064 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
10065 	qptr = &resp->queue_id0;
10066 	for (i = 0, j = 0; i < bp->max_tc; i++) {
10067 		bp->q_info[j].queue_id = *qptr;
10068 		bp->q_ids[i] = *qptr++;
10069 		bp->q_info[j].queue_profile = *qptr++;
10070 		bp->tc_to_qidx[j] = j;
10071 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
10072 		    (no_rdma && BNXT_PF(bp)))
10073 			j++;
10074 	}
10075 	bp->max_q = bp->max_tc;
10076 	bp->max_tc = max_t(u8, j, 1);
10077 
10078 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
10079 		bp->max_tc = 1;
10080 
10081 	if (bp->max_lltc > bp->max_tc)
10082 		bp->max_lltc = bp->max_tc;
10083 
10084 qportcfg_exit:
10085 	hwrm_req_drop(bp, req);
10086 	return rc;
10087 }
10088 
10089 static int bnxt_hwrm_poll(struct bnxt *bp)
10090 {
10091 	struct hwrm_ver_get_input *req;
10092 	int rc;
10093 
10094 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10095 	if (rc)
10096 		return rc;
10097 
10098 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10099 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10100 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10101 
10102 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
10103 	rc = hwrm_req_send(bp, req);
10104 	return rc;
10105 }
10106 
10107 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10108 {
10109 	struct hwrm_ver_get_output *resp;
10110 	struct hwrm_ver_get_input *req;
10111 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10112 	u32 dev_caps_cfg, hwrm_ver;
10113 	int rc, len, max_tmo_secs;
10114 
10115 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10116 	if (rc)
10117 		return rc;
10118 
10119 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10120 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10121 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10122 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10123 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10124 
10125 	resp = hwrm_req_hold(bp, req);
10126 	rc = hwrm_req_send(bp, req);
10127 	if (rc)
10128 		goto hwrm_ver_get_exit;
10129 
10130 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10131 
10132 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10133 			     resp->hwrm_intf_min_8b << 8 |
10134 			     resp->hwrm_intf_upd_8b;
10135 	if (resp->hwrm_intf_maj_8b < 1) {
10136 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10137 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10138 			    resp->hwrm_intf_upd_8b);
10139 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10140 	}
10141 
10142 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10143 			HWRM_VERSION_UPDATE;
10144 
10145 	if (bp->hwrm_spec_code > hwrm_ver)
10146 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10147 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10148 			 HWRM_VERSION_UPDATE);
10149 	else
10150 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10151 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10152 			 resp->hwrm_intf_upd_8b);
10153 
10154 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10155 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10156 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10157 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10158 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10159 		len = FW_VER_STR_LEN;
10160 	} else {
10161 		fw_maj = resp->hwrm_fw_maj_8b;
10162 		fw_min = resp->hwrm_fw_min_8b;
10163 		fw_bld = resp->hwrm_fw_bld_8b;
10164 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10165 		len = BC_HWRM_STR_LEN;
10166 	}
10167 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10168 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10169 		 fw_rsv);
10170 
10171 	if (strlen(resp->active_pkg_name)) {
10172 		int fw_ver_len = strlen(bp->fw_ver_str);
10173 
10174 		snprintf(bp->fw_ver_str + fw_ver_len,
10175 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10176 			 resp->active_pkg_name);
10177 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10178 	}
10179 
10180 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10181 	if (!bp->hwrm_cmd_timeout)
10182 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10183 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10184 	if (!bp->hwrm_cmd_max_timeout)
10185 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10186 	max_tmo_secs = bp->hwrm_cmd_max_timeout / 1000;
10187 #ifdef CONFIG_DETECT_HUNG_TASK
10188 	if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT ||
10189 	    max_tmo_secs > CONFIG_DEFAULT_HUNG_TASK_TIMEOUT) {
10190 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog (kernel default %ds)\n",
10191 			    max_tmo_secs, CONFIG_DEFAULT_HUNG_TASK_TIMEOUT);
10192 	}
10193 #endif
10194 
10195 	if (resp->hwrm_intf_maj_8b >= 1) {
10196 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10197 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10198 	}
10199 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10200 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10201 
10202 	bp->chip_num = le16_to_cpu(resp->chip_num);
10203 	bp->chip_rev = resp->chip_rev;
10204 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10205 	    !resp->chip_metal)
10206 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10207 
10208 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10209 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10210 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10211 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10212 
10213 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10214 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10215 
10216 	if (dev_caps_cfg &
10217 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10218 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10219 
10220 	if (dev_caps_cfg &
10221 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10222 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10223 
10224 	if (dev_caps_cfg &
10225 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10226 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10227 
10228 hwrm_ver_get_exit:
10229 	hwrm_req_drop(bp, req);
10230 	return rc;
10231 }
10232 
10233 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10234 {
10235 	struct hwrm_fw_set_time_input *req;
10236 	struct tm tm;
10237 	time64_t now = ktime_get_real_seconds();
10238 	int rc;
10239 
10240 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10241 	    bp->hwrm_spec_code < 0x10400)
10242 		return -EOPNOTSUPP;
10243 
10244 	time64_to_tm(now, 0, &tm);
10245 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10246 	if (rc)
10247 		return rc;
10248 
10249 	req->year = cpu_to_le16(1900 + tm.tm_year);
10250 	req->month = 1 + tm.tm_mon;
10251 	req->day = tm.tm_mday;
10252 	req->hour = tm.tm_hour;
10253 	req->minute = tm.tm_min;
10254 	req->second = tm.tm_sec;
10255 	return hwrm_req_send(bp, req);
10256 }
10257 
10258 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10259 {
10260 	u64 sw_tmp;
10261 
10262 	hw &= mask;
10263 	sw_tmp = (*sw & ~mask) | hw;
10264 	if (hw < (*sw & mask))
10265 		sw_tmp += mask + 1;
10266 	WRITE_ONCE(*sw, sw_tmp);
10267 }
10268 
10269 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10270 				    int count, bool ignore_zero)
10271 {
10272 	int i;
10273 
10274 	for (i = 0; i < count; i++) {
10275 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10276 
10277 		if (ignore_zero && !hw)
10278 			continue;
10279 
10280 		if (masks[i] == -1ULL)
10281 			sw_stats[i] = hw;
10282 		else
10283 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10284 	}
10285 }
10286 
10287 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10288 {
10289 	if (!stats->hw_stats)
10290 		return;
10291 
10292 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10293 				stats->hw_masks, stats->len / 8, false);
10294 }
10295 
10296 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10297 {
10298 	struct bnxt_stats_mem *ring0_stats;
10299 	bool ignore_zero = false;
10300 	int i;
10301 
10302 	/* Chip bug.  Counter intermittently becomes 0. */
10303 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10304 		ignore_zero = true;
10305 
10306 	for (i = 0; i < bp->cp_nr_rings; i++) {
10307 		struct bnxt_napi *bnapi = bp->bnapi[i];
10308 		struct bnxt_cp_ring_info *cpr;
10309 		struct bnxt_stats_mem *stats;
10310 
10311 		cpr = &bnapi->cp_ring;
10312 		stats = &cpr->stats;
10313 		if (!i)
10314 			ring0_stats = stats;
10315 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10316 					ring0_stats->hw_masks,
10317 					ring0_stats->len / 8, ignore_zero);
10318 	}
10319 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10320 		struct bnxt_stats_mem *stats = &bp->port_stats;
10321 		__le64 *hw_stats = stats->hw_stats;
10322 		u64 *sw_stats = stats->sw_stats;
10323 		u64 *masks = stats->hw_masks;
10324 		int cnt;
10325 
10326 		cnt = sizeof(struct rx_port_stats) / 8;
10327 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10328 
10329 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10330 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10331 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10332 		cnt = sizeof(struct tx_port_stats) / 8;
10333 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10334 	}
10335 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10336 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10337 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10338 	}
10339 }
10340 
10341 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10342 {
10343 	struct hwrm_port_qstats_input *req;
10344 	struct bnxt_pf_info *pf = &bp->pf;
10345 	int rc;
10346 
10347 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10348 		return 0;
10349 
10350 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10351 		return -EOPNOTSUPP;
10352 
10353 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10354 	if (rc)
10355 		return rc;
10356 
10357 	req->flags = flags;
10358 	req->port_id = cpu_to_le16(pf->port_id);
10359 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10360 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10361 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10362 	return hwrm_req_send(bp, req);
10363 }
10364 
10365 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10366 {
10367 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10368 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10369 	struct hwrm_port_qstats_ext_output *resp_qs;
10370 	struct hwrm_port_qstats_ext_input *req_qs;
10371 	struct bnxt_pf_info *pf = &bp->pf;
10372 	u32 tx_stat_size;
10373 	int rc;
10374 
10375 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10376 		return 0;
10377 
10378 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10379 		return -EOPNOTSUPP;
10380 
10381 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10382 	if (rc)
10383 		return rc;
10384 
10385 	req_qs->flags = flags;
10386 	req_qs->port_id = cpu_to_le16(pf->port_id);
10387 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10388 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10389 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10390 		       sizeof(struct tx_port_stats_ext) : 0;
10391 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10392 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10393 	resp_qs = hwrm_req_hold(bp, req_qs);
10394 	rc = hwrm_req_send(bp, req_qs);
10395 	if (!rc) {
10396 		bp->fw_rx_stats_ext_size =
10397 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10398 		if (BNXT_FW_MAJ(bp) < 220 &&
10399 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10400 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10401 
10402 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10403 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10404 	} else {
10405 		bp->fw_rx_stats_ext_size = 0;
10406 		bp->fw_tx_stats_ext_size = 0;
10407 	}
10408 	hwrm_req_drop(bp, req_qs);
10409 
10410 	if (flags)
10411 		return rc;
10412 
10413 	if (bp->fw_tx_stats_ext_size <=
10414 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10415 		bp->pri2cos_valid = 0;
10416 		return rc;
10417 	}
10418 
10419 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10420 	if (rc)
10421 		return rc;
10422 
10423 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10424 
10425 	resp_qc = hwrm_req_hold(bp, req_qc);
10426 	rc = hwrm_req_send(bp, req_qc);
10427 	if (!rc) {
10428 		u8 *pri2cos;
10429 		int i, j;
10430 
10431 		pri2cos = &resp_qc->pri0_cos_queue_id;
10432 		for (i = 0; i < 8; i++) {
10433 			u8 queue_id = pri2cos[i];
10434 			u8 queue_idx;
10435 
10436 			/* Per port queue IDs start from 0, 10, 20, etc */
10437 			queue_idx = queue_id % 10;
10438 			if (queue_idx > BNXT_MAX_QUEUE) {
10439 				bp->pri2cos_valid = false;
10440 				hwrm_req_drop(bp, req_qc);
10441 				return rc;
10442 			}
10443 			for (j = 0; j < bp->max_q; j++) {
10444 				if (bp->q_ids[j] == queue_id)
10445 					bp->pri2cos_idx[i] = queue_idx;
10446 			}
10447 		}
10448 		bp->pri2cos_valid = true;
10449 	}
10450 	hwrm_req_drop(bp, req_qc);
10451 
10452 	return rc;
10453 }
10454 
10455 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10456 {
10457 	bnxt_hwrm_tunnel_dst_port_free(bp,
10458 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10459 	bnxt_hwrm_tunnel_dst_port_free(bp,
10460 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10461 }
10462 
10463 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10464 {
10465 	int rc, i;
10466 	u32 tpa_flags = 0;
10467 
10468 	if (set_tpa)
10469 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10470 	else if (BNXT_NO_FW_ACCESS(bp))
10471 		return 0;
10472 	for (i = 0; i < bp->nr_vnics; i++) {
10473 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10474 		if (rc) {
10475 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10476 				   i, rc);
10477 			return rc;
10478 		}
10479 	}
10480 	return 0;
10481 }
10482 
10483 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10484 {
10485 	int i;
10486 
10487 	for (i = 0; i < bp->nr_vnics; i++)
10488 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10489 }
10490 
10491 static void bnxt_clear_vnic(struct bnxt *bp)
10492 {
10493 	if (!bp->vnic_info)
10494 		return;
10495 
10496 	bnxt_hwrm_clear_vnic_filter(bp);
10497 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10498 		/* clear all RSS setting before free vnic ctx */
10499 		bnxt_hwrm_clear_vnic_rss(bp);
10500 		bnxt_hwrm_vnic_ctx_free(bp);
10501 	}
10502 	/* before free the vnic, undo the vnic tpa settings */
10503 	if (bp->flags & BNXT_FLAG_TPA)
10504 		bnxt_set_tpa(bp, false);
10505 	bnxt_hwrm_vnic_free(bp);
10506 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10507 		bnxt_hwrm_vnic_ctx_free(bp);
10508 }
10509 
10510 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10511 				    bool irq_re_init)
10512 {
10513 	bnxt_clear_vnic(bp);
10514 	bnxt_hwrm_ring_free(bp, close_path);
10515 	bnxt_hwrm_ring_grp_free(bp);
10516 	if (irq_re_init) {
10517 		bnxt_hwrm_stat_ctx_free(bp);
10518 		bnxt_hwrm_free_tunnel_ports(bp);
10519 	}
10520 }
10521 
10522 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10523 {
10524 	struct hwrm_func_cfg_input *req;
10525 	u8 evb_mode;
10526 	int rc;
10527 
10528 	if (br_mode == BRIDGE_MODE_VEB)
10529 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10530 	else if (br_mode == BRIDGE_MODE_VEPA)
10531 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10532 	else
10533 		return -EINVAL;
10534 
10535 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10536 	if (rc)
10537 		return rc;
10538 
10539 	req->fid = cpu_to_le16(0xffff);
10540 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10541 	req->evb_mode = evb_mode;
10542 	return hwrm_req_send(bp, req);
10543 }
10544 
10545 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10546 {
10547 	struct hwrm_func_cfg_input *req;
10548 	int rc;
10549 
10550 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10551 		return 0;
10552 
10553 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10554 	if (rc)
10555 		return rc;
10556 
10557 	req->fid = cpu_to_le16(0xffff);
10558 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10559 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10560 	if (size == 128)
10561 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10562 
10563 	return hwrm_req_send(bp, req);
10564 }
10565 
10566 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10567 {
10568 	int rc;
10569 
10570 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10571 		goto skip_rss_ctx;
10572 
10573 	/* allocate context for vnic */
10574 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10575 	if (rc) {
10576 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10577 			   vnic->vnic_id, rc);
10578 		goto vnic_setup_err;
10579 	}
10580 	bp->rsscos_nr_ctxs++;
10581 
10582 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10583 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10584 		if (rc) {
10585 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10586 				   vnic->vnic_id, rc);
10587 			goto vnic_setup_err;
10588 		}
10589 		bp->rsscos_nr_ctxs++;
10590 	}
10591 
10592 skip_rss_ctx:
10593 	/* configure default vnic, ring grp */
10594 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10595 	if (rc) {
10596 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10597 			   vnic->vnic_id, rc);
10598 		goto vnic_setup_err;
10599 	}
10600 
10601 	/* Enable RSS hashing on vnic */
10602 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10603 	if (rc) {
10604 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10605 			   vnic->vnic_id, rc);
10606 		goto vnic_setup_err;
10607 	}
10608 
10609 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10610 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10611 		if (rc) {
10612 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10613 				   vnic->vnic_id, rc);
10614 		}
10615 	}
10616 
10617 vnic_setup_err:
10618 	return rc;
10619 }
10620 
10621 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10622 			  u8 valid)
10623 {
10624 	struct hwrm_vnic_update_input *req;
10625 	int rc;
10626 
10627 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10628 	if (rc)
10629 		return rc;
10630 
10631 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10632 
10633 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10634 		req->mru = cpu_to_le16(vnic->mru);
10635 
10636 	req->enables = cpu_to_le32(valid);
10637 
10638 	return hwrm_req_send(bp, req);
10639 }
10640 
10641 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10642 {
10643 	int rc;
10644 
10645 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10646 	if (rc) {
10647 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10648 			   vnic->vnic_id, rc);
10649 		return rc;
10650 	}
10651 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10652 	if (rc)
10653 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10654 			   vnic->vnic_id, rc);
10655 	return rc;
10656 }
10657 
10658 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10659 {
10660 	int rc, i, nr_ctxs;
10661 
10662 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10663 	for (i = 0; i < nr_ctxs; i++) {
10664 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10665 		if (rc) {
10666 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10667 				   vnic->vnic_id, i, rc);
10668 			break;
10669 		}
10670 		bp->rsscos_nr_ctxs++;
10671 	}
10672 	if (i < nr_ctxs)
10673 		return -ENOMEM;
10674 
10675 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10676 	if (rc)
10677 		return rc;
10678 
10679 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10680 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10681 		if (rc) {
10682 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10683 				   vnic->vnic_id, rc);
10684 		}
10685 	}
10686 	return rc;
10687 }
10688 
10689 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10690 {
10691 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10692 		return __bnxt_setup_vnic_p5(bp, vnic);
10693 	else
10694 		return __bnxt_setup_vnic(bp, vnic);
10695 }
10696 
10697 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10698 				     struct bnxt_vnic_info *vnic,
10699 				     u16 start_rx_ring_idx, int rx_rings)
10700 {
10701 	int rc;
10702 
10703 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10704 	if (rc) {
10705 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10706 			   vnic->vnic_id, rc);
10707 		return rc;
10708 	}
10709 	return bnxt_setup_vnic(bp, vnic);
10710 }
10711 
10712 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10713 {
10714 	struct bnxt_vnic_info *vnic;
10715 	int i, rc = 0;
10716 
10717 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10718 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10719 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10720 	}
10721 
10722 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10723 		return 0;
10724 
10725 	for (i = 0; i < bp->rx_nr_rings; i++) {
10726 		u16 vnic_id = i + 1;
10727 		u16 ring_id = i;
10728 
10729 		if (vnic_id >= bp->nr_vnics)
10730 			break;
10731 
10732 		vnic = &bp->vnic_info[vnic_id];
10733 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10734 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10735 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10736 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10737 			break;
10738 	}
10739 	return rc;
10740 }
10741 
10742 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10743 			  bool all)
10744 {
10745 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10746 	struct bnxt_filter_base *usr_fltr, *tmp;
10747 	struct bnxt_ntuple_filter *ntp_fltr;
10748 	int i;
10749 
10750 	if (netif_running(bp->dev)) {
10751 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10752 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10753 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10754 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10755 		}
10756 	}
10757 	if (!all)
10758 		return;
10759 
10760 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10761 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10762 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10763 			ntp_fltr = container_of(usr_fltr,
10764 						struct bnxt_ntuple_filter,
10765 						base);
10766 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10767 			bnxt_del_ntp_filter(bp, ntp_fltr);
10768 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10769 		}
10770 	}
10771 
10772 	if (vnic->rss_table)
10773 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10774 				  vnic->rss_table,
10775 				  vnic->rss_table_dma_addr);
10776 	bp->num_rss_ctx--;
10777 }
10778 
10779 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10780 {
10781 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10782 	struct ethtool_rxfh_context *ctx;
10783 	unsigned long context;
10784 
10785 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10786 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10787 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10788 
10789 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10790 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10791 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10792 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10793 				   rss_ctx->index);
10794 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10795 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10796 		}
10797 	}
10798 }
10799 
10800 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10801 {
10802 	struct ethtool_rxfh_context *ctx;
10803 	unsigned long context;
10804 
10805 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10806 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10807 
10808 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10809 	}
10810 }
10811 
10812 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10813 static bool bnxt_promisc_ok(struct bnxt *bp)
10814 {
10815 #ifdef CONFIG_BNXT_SRIOV
10816 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10817 		return false;
10818 #endif
10819 	return true;
10820 }
10821 
10822 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10823 {
10824 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10825 	unsigned int rc = 0;
10826 
10827 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10828 	if (rc) {
10829 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10830 			   rc);
10831 		return rc;
10832 	}
10833 
10834 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10835 	if (rc) {
10836 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10837 			   rc);
10838 		return rc;
10839 	}
10840 	return rc;
10841 }
10842 
10843 static int bnxt_cfg_rx_mode(struct bnxt *);
10844 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10845 
10846 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10847 {
10848 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10849 	int rc = 0;
10850 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10851 
10852 	if (irq_re_init) {
10853 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10854 		if (rc) {
10855 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10856 				   rc);
10857 			goto err_out;
10858 		}
10859 	}
10860 
10861 	rc = bnxt_hwrm_ring_alloc(bp);
10862 	if (rc) {
10863 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10864 		goto err_out;
10865 	}
10866 
10867 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10868 	if (rc) {
10869 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10870 		goto err_out;
10871 	}
10872 
10873 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10874 		rx_nr_rings--;
10875 
10876 	/* default vnic 0 */
10877 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10878 	if (rc) {
10879 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10880 		goto err_out;
10881 	}
10882 
10883 	if (BNXT_VF(bp))
10884 		bnxt_hwrm_func_qcfg(bp);
10885 
10886 	rc = bnxt_setup_vnic(bp, vnic);
10887 	if (rc)
10888 		goto err_out;
10889 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10890 		bnxt_hwrm_update_rss_hash_cfg(bp);
10891 
10892 	if (bp->flags & BNXT_FLAG_RFS) {
10893 		rc = bnxt_alloc_rfs_vnics(bp);
10894 		if (rc)
10895 			goto err_out;
10896 	}
10897 
10898 	if (bp->flags & BNXT_FLAG_TPA) {
10899 		rc = bnxt_set_tpa(bp, true);
10900 		if (rc)
10901 			goto err_out;
10902 	}
10903 
10904 	if (BNXT_VF(bp))
10905 		bnxt_update_vf_mac(bp);
10906 
10907 	/* Filter for default vnic 0 */
10908 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10909 	if (rc) {
10910 		if (BNXT_VF(bp) && rc == -ENODEV)
10911 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10912 		else
10913 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10914 		goto err_out;
10915 	}
10916 	vnic->uc_filter_count = 1;
10917 
10918 	vnic->rx_mask = 0;
10919 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10920 		goto skip_rx_mask;
10921 
10922 	if (bp->dev->flags & IFF_BROADCAST)
10923 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10924 
10925 	if (bp->dev->flags & IFF_PROMISC)
10926 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10927 
10928 	if (bp->dev->flags & IFF_ALLMULTI) {
10929 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10930 		vnic->mc_list_count = 0;
10931 	} else if (bp->dev->flags & IFF_MULTICAST) {
10932 		u32 mask = 0;
10933 
10934 		bnxt_mc_list_updated(bp, &mask);
10935 		vnic->rx_mask |= mask;
10936 	}
10937 
10938 	rc = bnxt_cfg_rx_mode(bp);
10939 	if (rc)
10940 		goto err_out;
10941 
10942 skip_rx_mask:
10943 	rc = bnxt_hwrm_set_coal(bp);
10944 	if (rc)
10945 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10946 				rc);
10947 
10948 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10949 		rc = bnxt_setup_nitroa0_vnic(bp);
10950 		if (rc)
10951 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10952 				   rc);
10953 	}
10954 
10955 	if (BNXT_VF(bp)) {
10956 		bnxt_hwrm_func_qcfg(bp);
10957 		netdev_update_features(bp->dev);
10958 	}
10959 
10960 	return 0;
10961 
10962 err_out:
10963 	bnxt_hwrm_resource_free(bp, 0, true);
10964 
10965 	return rc;
10966 }
10967 
10968 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10969 {
10970 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10971 	return 0;
10972 }
10973 
10974 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10975 {
10976 	bnxt_init_cp_rings(bp);
10977 	bnxt_init_rx_rings(bp);
10978 	bnxt_init_tx_rings(bp);
10979 	bnxt_init_ring_grps(bp, irq_re_init);
10980 	bnxt_init_vnics(bp);
10981 
10982 	return bnxt_init_chip(bp, irq_re_init);
10983 }
10984 
10985 static int bnxt_set_real_num_queues(struct bnxt *bp)
10986 {
10987 	int rc;
10988 	struct net_device *dev = bp->dev;
10989 
10990 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10991 					  bp->tx_nr_rings_xdp);
10992 	if (rc)
10993 		return rc;
10994 
10995 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10996 	if (rc)
10997 		return rc;
10998 
10999 #ifdef CONFIG_RFS_ACCEL
11000 	if (bp->flags & BNXT_FLAG_RFS)
11001 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
11002 #endif
11003 
11004 	return rc;
11005 }
11006 
11007 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11008 			     bool shared)
11009 {
11010 	int _rx = *rx, _tx = *tx;
11011 
11012 	if (shared) {
11013 		*rx = min_t(int, _rx, max);
11014 		*tx = min_t(int, _tx, max);
11015 	} else {
11016 		if (max < 2)
11017 			return -ENOMEM;
11018 
11019 		while (_rx + _tx > max) {
11020 			if (_rx > _tx && _rx > 1)
11021 				_rx--;
11022 			else if (_tx > 1)
11023 				_tx--;
11024 		}
11025 		*rx = _rx;
11026 		*tx = _tx;
11027 	}
11028 	return 0;
11029 }
11030 
11031 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
11032 {
11033 	return (tx - tx_xdp) / tx_sets + tx_xdp;
11034 }
11035 
11036 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
11037 {
11038 	int tcs = bp->num_tc;
11039 
11040 	if (!tcs)
11041 		tcs = 1;
11042 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
11043 }
11044 
11045 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
11046 {
11047 	int tcs = bp->num_tc;
11048 
11049 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
11050 	       bp->tx_nr_rings_xdp;
11051 }
11052 
11053 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
11054 			   bool sh)
11055 {
11056 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
11057 
11058 	if (tx_cp != *tx) {
11059 		int tx_saved = tx_cp, rc;
11060 
11061 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
11062 		if (rc)
11063 			return rc;
11064 		if (tx_cp != tx_saved)
11065 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
11066 		return 0;
11067 	}
11068 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
11069 }
11070 
11071 static void bnxt_setup_msix(struct bnxt *bp)
11072 {
11073 	const int len = sizeof(bp->irq_tbl[0].name);
11074 	struct net_device *dev = bp->dev;
11075 	int tcs, i;
11076 
11077 	tcs = bp->num_tc;
11078 	if (tcs) {
11079 		int i, off, count;
11080 
11081 		for (i = 0; i < tcs; i++) {
11082 			count = bp->tx_nr_rings_per_tc;
11083 			off = BNXT_TC_TO_RING_BASE(bp, i);
11084 			netdev_set_tc_queue(dev, i, count, off);
11085 		}
11086 	}
11087 
11088 	for (i = 0; i < bp->cp_nr_rings; i++) {
11089 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11090 		char *attr;
11091 
11092 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
11093 			attr = "TxRx";
11094 		else if (i < bp->rx_nr_rings)
11095 			attr = "rx";
11096 		else
11097 			attr = "tx";
11098 
11099 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
11100 			 attr, i);
11101 		bp->irq_tbl[map_idx].handler = bnxt_msix;
11102 	}
11103 }
11104 
11105 static int bnxt_init_int_mode(struct bnxt *bp);
11106 
11107 static int bnxt_change_msix(struct bnxt *bp, int total)
11108 {
11109 	struct msi_map map;
11110 	int i;
11111 
11112 	/* add MSIX to the end if needed */
11113 	for (i = bp->total_irqs; i < total; i++) {
11114 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11115 		if (map.index < 0)
11116 			return bp->total_irqs;
11117 		bp->irq_tbl[i].vector = map.virq;
11118 		bp->total_irqs++;
11119 	}
11120 
11121 	/* trim MSIX from the end if needed */
11122 	for (i = bp->total_irqs; i > total; i--) {
11123 		map.index = i - 1;
11124 		map.virq = bp->irq_tbl[i - 1].vector;
11125 		pci_msix_free_irq(bp->pdev, map);
11126 		bp->total_irqs--;
11127 	}
11128 	return bp->total_irqs;
11129 }
11130 
11131 static int bnxt_setup_int_mode(struct bnxt *bp)
11132 {
11133 	int rc;
11134 
11135 	if (!bp->irq_tbl) {
11136 		rc = bnxt_init_int_mode(bp);
11137 		if (rc || !bp->irq_tbl)
11138 			return rc ?: -ENODEV;
11139 	}
11140 
11141 	bnxt_setup_msix(bp);
11142 
11143 	rc = bnxt_set_real_num_queues(bp);
11144 	return rc;
11145 }
11146 
11147 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11148 {
11149 	return bp->hw_resc.max_rsscos_ctxs;
11150 }
11151 
11152 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11153 {
11154 	return bp->hw_resc.max_vnics;
11155 }
11156 
11157 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11158 {
11159 	return bp->hw_resc.max_stat_ctxs;
11160 }
11161 
11162 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11163 {
11164 	return bp->hw_resc.max_cp_rings;
11165 }
11166 
11167 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11168 {
11169 	unsigned int cp = bp->hw_resc.max_cp_rings;
11170 
11171 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11172 		cp -= bnxt_get_ulp_msix_num(bp);
11173 
11174 	return cp;
11175 }
11176 
11177 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11178 {
11179 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11180 
11181 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11182 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11183 
11184 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11185 }
11186 
11187 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11188 {
11189 	bp->hw_resc.max_irqs = max_irqs;
11190 }
11191 
11192 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11193 {
11194 	unsigned int cp;
11195 
11196 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11197 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11198 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11199 	else
11200 		return cp - bp->cp_nr_rings;
11201 }
11202 
11203 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11204 {
11205 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11206 }
11207 
11208 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11209 {
11210 	int max_irq = bnxt_get_max_func_irqs(bp);
11211 	int total_req = bp->cp_nr_rings + num;
11212 
11213 	if (max_irq < total_req) {
11214 		num = max_irq - bp->cp_nr_rings;
11215 		if (num <= 0)
11216 			return 0;
11217 	}
11218 	return num;
11219 }
11220 
11221 static int bnxt_get_num_msix(struct bnxt *bp)
11222 {
11223 	if (!BNXT_NEW_RM(bp))
11224 		return bnxt_get_max_func_irqs(bp);
11225 
11226 	return bnxt_nq_rings_in_use(bp);
11227 }
11228 
11229 static int bnxt_init_int_mode(struct bnxt *bp)
11230 {
11231 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11232 
11233 	total_vecs = bnxt_get_num_msix(bp);
11234 	max = bnxt_get_max_func_irqs(bp);
11235 	if (total_vecs > max)
11236 		total_vecs = max;
11237 
11238 	if (!total_vecs)
11239 		return 0;
11240 
11241 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11242 		min = 2;
11243 
11244 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11245 					   PCI_IRQ_MSIX);
11246 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11247 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11248 		rc = -ENODEV;
11249 		goto msix_setup_exit;
11250 	}
11251 
11252 	tbl_size = total_vecs;
11253 	if (pci_msix_can_alloc_dyn(bp->pdev))
11254 		tbl_size = max;
11255 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11256 	if (bp->irq_tbl) {
11257 		for (i = 0; i < total_vecs; i++)
11258 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11259 
11260 		bp->total_irqs = total_vecs;
11261 		/* Trim rings based upon num of vectors allocated */
11262 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11263 				     total_vecs - ulp_msix, min == 1);
11264 		if (rc)
11265 			goto msix_setup_exit;
11266 
11267 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11268 		bp->cp_nr_rings = (min == 1) ?
11269 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11270 				  tx_cp + bp->rx_nr_rings;
11271 
11272 	} else {
11273 		rc = -ENOMEM;
11274 		goto msix_setup_exit;
11275 	}
11276 	return 0;
11277 
11278 msix_setup_exit:
11279 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11280 	kfree(bp->irq_tbl);
11281 	bp->irq_tbl = NULL;
11282 	pci_free_irq_vectors(bp->pdev);
11283 	return rc;
11284 }
11285 
11286 static void bnxt_clear_int_mode(struct bnxt *bp)
11287 {
11288 	pci_free_irq_vectors(bp->pdev);
11289 
11290 	kfree(bp->irq_tbl);
11291 	bp->irq_tbl = NULL;
11292 }
11293 
11294 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11295 {
11296 	bool irq_cleared = false;
11297 	bool irq_change = false;
11298 	int tcs = bp->num_tc;
11299 	int irqs_required;
11300 	int rc;
11301 
11302 	if (!bnxt_need_reserve_rings(bp))
11303 		return 0;
11304 
11305 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11306 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11307 
11308 		if (ulp_msix > bp->ulp_num_msix_want)
11309 			ulp_msix = bp->ulp_num_msix_want;
11310 		irqs_required = ulp_msix + bp->cp_nr_rings;
11311 	} else {
11312 		irqs_required = bnxt_get_num_msix(bp);
11313 	}
11314 
11315 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11316 		irq_change = true;
11317 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11318 			bnxt_ulp_irq_stop(bp);
11319 			bnxt_clear_int_mode(bp);
11320 			irq_cleared = true;
11321 		}
11322 	}
11323 	rc = __bnxt_reserve_rings(bp);
11324 	if (irq_cleared) {
11325 		if (!rc)
11326 			rc = bnxt_init_int_mode(bp);
11327 		bnxt_ulp_irq_restart(bp, rc);
11328 	} else if (irq_change && !rc) {
11329 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11330 			rc = -ENOSPC;
11331 	}
11332 	if (rc) {
11333 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11334 		return rc;
11335 	}
11336 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11337 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11338 		netdev_err(bp->dev, "tx ring reservation failure\n");
11339 		netdev_reset_tc(bp->dev);
11340 		bp->num_tc = 0;
11341 		if (bp->tx_nr_rings_xdp)
11342 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11343 		else
11344 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11345 		return -ENOMEM;
11346 	}
11347 	return 0;
11348 }
11349 
11350 static void bnxt_tx_queue_stop(struct bnxt *bp, int idx)
11351 {
11352 	struct bnxt_tx_ring_info *txr;
11353 	struct netdev_queue *txq;
11354 	struct bnxt_napi *bnapi;
11355 	int i;
11356 
11357 	bnapi = bp->bnapi[idx];
11358 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11359 		WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11360 		synchronize_net();
11361 
11362 		if (!(bnapi->flags & BNXT_NAPI_FLAG_XDP)) {
11363 			txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11364 			if (txq) {
11365 				__netif_tx_lock_bh(txq);
11366 				netif_tx_stop_queue(txq);
11367 				__netif_tx_unlock_bh(txq);
11368 			}
11369 		}
11370 
11371 		if (!bp->tph_mode)
11372 			continue;
11373 
11374 		bnxt_hwrm_tx_ring_free(bp, txr, true);
11375 		bnxt_hwrm_cp_ring_free(bp, txr->tx_cpr);
11376 		bnxt_free_one_tx_ring_skbs(bp, txr, txr->txq_index);
11377 		bnxt_clear_one_cp_ring(bp, txr->tx_cpr);
11378 	}
11379 }
11380 
11381 static int bnxt_tx_queue_start(struct bnxt *bp, int idx)
11382 {
11383 	struct bnxt_tx_ring_info *txr;
11384 	struct netdev_queue *txq;
11385 	struct bnxt_napi *bnapi;
11386 	int rc, i;
11387 
11388 	bnapi = bp->bnapi[idx];
11389 	/* All rings have been reserved and previously allocated.
11390 	 * Reallocating with the same parameters should never fail.
11391 	 */
11392 	bnxt_for_each_napi_tx(i, bnapi, txr) {
11393 		if (!bp->tph_mode)
11394 			goto start_tx;
11395 
11396 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, txr->tx_cpr);
11397 		if (rc)
11398 			return rc;
11399 
11400 		rc = bnxt_hwrm_tx_ring_alloc(bp, txr, false);
11401 		if (rc)
11402 			return rc;
11403 
11404 		txr->tx_prod = 0;
11405 		txr->tx_cons = 0;
11406 		txr->tx_hw_cons = 0;
11407 start_tx:
11408 		WRITE_ONCE(txr->dev_state, 0);
11409 		synchronize_net();
11410 
11411 		if (bnapi->flags & BNXT_NAPI_FLAG_XDP)
11412 			continue;
11413 
11414 		txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
11415 		if (txq)
11416 			netif_tx_start_queue(txq);
11417 	}
11418 
11419 	return 0;
11420 }
11421 
11422 static void bnxt_irq_affinity_notify(struct irq_affinity_notify *notify,
11423 				     const cpumask_t *mask)
11424 {
11425 	struct bnxt_irq *irq;
11426 	u16 tag;
11427 	int err;
11428 
11429 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11430 
11431 	if (!irq->bp->tph_mode)
11432 		return;
11433 
11434 	cpumask_copy(irq->cpu_mask, mask);
11435 
11436 	if (irq->ring_nr >= irq->bp->rx_nr_rings)
11437 		return;
11438 
11439 	if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11440 				cpumask_first(irq->cpu_mask), &tag))
11441 		return;
11442 
11443 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag))
11444 		return;
11445 
11446 	netdev_lock(irq->bp->dev);
11447 	if (netif_running(irq->bp->dev)) {
11448 		err = netdev_rx_queue_restart(irq->bp->dev, irq->ring_nr);
11449 		if (err)
11450 			netdev_err(irq->bp->dev,
11451 				   "RX queue restart failed: err=%d\n", err);
11452 	}
11453 	netdev_unlock(irq->bp->dev);
11454 }
11455 
11456 static void bnxt_irq_affinity_release(struct kref *ref)
11457 {
11458 	struct irq_affinity_notify *notify =
11459 		container_of(ref, struct irq_affinity_notify, kref);
11460 	struct bnxt_irq *irq;
11461 
11462 	irq = container_of(notify, struct bnxt_irq, affinity_notify);
11463 
11464 	if (!irq->bp->tph_mode)
11465 		return;
11466 
11467 	if (pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, 0)) {
11468 		netdev_err(irq->bp->dev,
11469 			   "Setting ST=0 for MSIX entry %d failed\n",
11470 			   irq->msix_nr);
11471 		return;
11472 	}
11473 }
11474 
11475 static void bnxt_release_irq_notifier(struct bnxt_irq *irq)
11476 {
11477 	irq_set_affinity_notifier(irq->vector, NULL);
11478 }
11479 
11480 static void bnxt_register_irq_notifier(struct bnxt *bp, struct bnxt_irq *irq)
11481 {
11482 	struct irq_affinity_notify *notify;
11483 
11484 	irq->bp = bp;
11485 
11486 	/* Nothing to do if TPH is not enabled */
11487 	if (!bp->tph_mode)
11488 		return;
11489 
11490 	/* Register IRQ affinity notifier */
11491 	notify = &irq->affinity_notify;
11492 	notify->irq = irq->vector;
11493 	notify->notify = bnxt_irq_affinity_notify;
11494 	notify->release = bnxt_irq_affinity_release;
11495 
11496 	irq_set_affinity_notifier(irq->vector, notify);
11497 }
11498 
11499 static void bnxt_free_irq(struct bnxt *bp)
11500 {
11501 	struct bnxt_irq *irq;
11502 	int i;
11503 
11504 #ifdef CONFIG_RFS_ACCEL
11505 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11506 	bp->dev->rx_cpu_rmap = NULL;
11507 #endif
11508 	if (!bp->irq_tbl || !bp->bnapi)
11509 		return;
11510 
11511 	for (i = 0; i < bp->cp_nr_rings; i++) {
11512 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11513 
11514 		irq = &bp->irq_tbl[map_idx];
11515 		if (irq->requested) {
11516 			if (irq->have_cpumask) {
11517 				irq_update_affinity_hint(irq->vector, NULL);
11518 				free_cpumask_var(irq->cpu_mask);
11519 				irq->have_cpumask = 0;
11520 			}
11521 
11522 			bnxt_release_irq_notifier(irq);
11523 
11524 			free_irq(irq->vector, bp->bnapi[i]);
11525 		}
11526 
11527 		irq->requested = 0;
11528 	}
11529 
11530 	/* Disable TPH support */
11531 	pcie_disable_tph(bp->pdev);
11532 	bp->tph_mode = 0;
11533 }
11534 
11535 static int bnxt_request_irq(struct bnxt *bp)
11536 {
11537 	int i, j, rc = 0;
11538 	unsigned long flags = 0;
11539 #ifdef CONFIG_RFS_ACCEL
11540 	struct cpu_rmap *rmap;
11541 #endif
11542 
11543 	rc = bnxt_setup_int_mode(bp);
11544 	if (rc) {
11545 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11546 			   rc);
11547 		return rc;
11548 	}
11549 #ifdef CONFIG_RFS_ACCEL
11550 	rmap = bp->dev->rx_cpu_rmap;
11551 #endif
11552 
11553 	/* Enable TPH support as part of IRQ request */
11554 	rc = pcie_enable_tph(bp->pdev, PCI_TPH_ST_IV_MODE);
11555 	if (!rc)
11556 		bp->tph_mode = PCI_TPH_ST_IV_MODE;
11557 
11558 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11559 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11560 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11561 
11562 #ifdef CONFIG_RFS_ACCEL
11563 		if (rmap && bp->bnapi[i]->rx_ring) {
11564 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11565 			if (rc)
11566 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11567 					    j);
11568 			j++;
11569 		}
11570 #endif
11571 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11572 				 bp->bnapi[i]);
11573 		if (rc)
11574 			break;
11575 
11576 		netif_napi_set_irq_locked(&bp->bnapi[i]->napi, irq->vector);
11577 		irq->requested = 1;
11578 
11579 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11580 			int numa_node = dev_to_node(&bp->pdev->dev);
11581 			u16 tag;
11582 
11583 			irq->have_cpumask = 1;
11584 			irq->msix_nr = map_idx;
11585 			irq->ring_nr = i;
11586 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11587 					irq->cpu_mask);
11588 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11589 			if (rc) {
11590 				netdev_warn(bp->dev,
11591 					    "Update affinity hint failed, IRQ = %d\n",
11592 					    irq->vector);
11593 				break;
11594 			}
11595 
11596 			bnxt_register_irq_notifier(bp, irq);
11597 
11598 			/* Init ST table entry */
11599 			if (pcie_tph_get_cpu_st(irq->bp->pdev, TPH_MEM_TYPE_VM,
11600 						cpumask_first(irq->cpu_mask),
11601 						&tag))
11602 				continue;
11603 
11604 			pcie_tph_set_st_entry(irq->bp->pdev, irq->msix_nr, tag);
11605 		}
11606 	}
11607 	return rc;
11608 }
11609 
11610 static void bnxt_del_napi(struct bnxt *bp)
11611 {
11612 	int i;
11613 
11614 	if (!bp->bnapi)
11615 		return;
11616 
11617 	for (i = 0; i < bp->rx_nr_rings; i++)
11618 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11619 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11620 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11621 
11622 	for (i = 0; i < bp->cp_nr_rings; i++) {
11623 		struct bnxt_napi *bnapi = bp->bnapi[i];
11624 
11625 		__netif_napi_del_locked(&bnapi->napi);
11626 	}
11627 	/* We called __netif_napi_del_locked(), we need
11628 	 * to respect an RCU grace period before freeing napi structures.
11629 	 */
11630 	synchronize_net();
11631 }
11632 
11633 static void bnxt_init_napi(struct bnxt *bp)
11634 {
11635 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11636 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11637 	struct bnxt_napi *bnapi;
11638 	int i;
11639 
11640 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11641 		poll_fn = bnxt_poll_p5;
11642 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11643 		cp_nr_rings--;
11644 	for (i = 0; i < cp_nr_rings; i++) {
11645 		bnapi = bp->bnapi[i];
11646 		netif_napi_add_config_locked(bp->dev, &bnapi->napi, poll_fn,
11647 					     bnapi->index);
11648 	}
11649 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11650 		bnapi = bp->bnapi[cp_nr_rings];
11651 		netif_napi_add_locked(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11652 	}
11653 }
11654 
11655 static void bnxt_disable_napi(struct bnxt *bp)
11656 {
11657 	int i;
11658 
11659 	if (!bp->bnapi ||
11660 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11661 		return;
11662 
11663 	for (i = 0; i < bp->cp_nr_rings; i++) {
11664 		struct bnxt_napi *bnapi = bp->bnapi[i];
11665 		struct bnxt_cp_ring_info *cpr;
11666 
11667 		cpr = &bnapi->cp_ring;
11668 		if (bnapi->tx_fault)
11669 			cpr->sw_stats->tx.tx_resets++;
11670 		if (bnapi->in_reset)
11671 			cpr->sw_stats->rx.rx_resets++;
11672 		napi_disable_locked(&bnapi->napi);
11673 	}
11674 }
11675 
11676 static void bnxt_enable_napi(struct bnxt *bp)
11677 {
11678 	int i;
11679 
11680 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11681 	for (i = 0; i < bp->cp_nr_rings; i++) {
11682 		struct bnxt_napi *bnapi = bp->bnapi[i];
11683 		struct bnxt_cp_ring_info *cpr;
11684 
11685 		bnapi->tx_fault = 0;
11686 
11687 		cpr = &bnapi->cp_ring;
11688 		bnapi->in_reset = false;
11689 
11690 		if (bnapi->rx_ring) {
11691 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11692 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11693 		}
11694 		napi_enable_locked(&bnapi->napi);
11695 	}
11696 }
11697 
11698 void bnxt_tx_disable(struct bnxt *bp)
11699 {
11700 	int i;
11701 	struct bnxt_tx_ring_info *txr;
11702 
11703 	if (bp->tx_ring) {
11704 		for (i = 0; i < bp->tx_nr_rings; i++) {
11705 			txr = &bp->tx_ring[i];
11706 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11707 		}
11708 	}
11709 	/* Make sure napi polls see @dev_state change */
11710 	synchronize_net();
11711 	/* Drop carrier first to prevent TX timeout */
11712 	netif_carrier_off(bp->dev);
11713 	/* Stop all TX queues */
11714 	netif_tx_disable(bp->dev);
11715 }
11716 
11717 void bnxt_tx_enable(struct bnxt *bp)
11718 {
11719 	int i;
11720 	struct bnxt_tx_ring_info *txr;
11721 
11722 	for (i = 0; i < bp->tx_nr_rings; i++) {
11723 		txr = &bp->tx_ring[i];
11724 		WRITE_ONCE(txr->dev_state, 0);
11725 	}
11726 	/* Make sure napi polls see @dev_state change */
11727 	synchronize_net();
11728 	netif_tx_wake_all_queues(bp->dev);
11729 	if (BNXT_LINK_IS_UP(bp))
11730 		netif_carrier_on(bp->dev);
11731 }
11732 
11733 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11734 {
11735 	u8 active_fec = link_info->active_fec_sig_mode &
11736 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11737 
11738 	switch (active_fec) {
11739 	default:
11740 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11741 		return "None";
11742 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11743 		return "Clause 74 BaseR";
11744 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11745 		return "Clause 91 RS(528,514)";
11746 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11747 		return "Clause 91 RS544_1XN";
11748 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11749 		return "Clause 91 RS(544,514)";
11750 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11751 		return "Clause 91 RS272_1XN";
11752 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11753 		return "Clause 91 RS(272,257)";
11754 	}
11755 }
11756 
11757 void bnxt_report_link(struct bnxt *bp)
11758 {
11759 	if (BNXT_LINK_IS_UP(bp)) {
11760 		const char *signal = "";
11761 		const char *flow_ctrl;
11762 		const char *duplex;
11763 		u32 speed;
11764 		u16 fec;
11765 
11766 		netif_carrier_on(bp->dev);
11767 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11768 		if (speed == SPEED_UNKNOWN) {
11769 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11770 			return;
11771 		}
11772 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11773 			duplex = "full";
11774 		else
11775 			duplex = "half";
11776 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11777 			flow_ctrl = "ON - receive & transmit";
11778 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11779 			flow_ctrl = "ON - transmit";
11780 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11781 			flow_ctrl = "ON - receive";
11782 		else
11783 			flow_ctrl = "none";
11784 		if (bp->link_info.phy_qcfg_resp.option_flags &
11785 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11786 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11787 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11788 			switch (sig_mode) {
11789 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11790 				signal = "(NRZ) ";
11791 				break;
11792 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11793 				signal = "(PAM4 56Gbps) ";
11794 				break;
11795 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11796 				signal = "(PAM4 112Gbps) ";
11797 				break;
11798 			default:
11799 				break;
11800 			}
11801 		}
11802 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11803 			    speed, signal, duplex, flow_ctrl);
11804 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11805 			netdev_info(bp->dev, "EEE is %s\n",
11806 				    bp->eee.eee_active ? "active" :
11807 							 "not active");
11808 		fec = bp->link_info.fec_cfg;
11809 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11810 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11811 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11812 				    bnxt_report_fec(&bp->link_info));
11813 	} else {
11814 		netif_carrier_off(bp->dev);
11815 		netdev_err(bp->dev, "NIC Link is Down\n");
11816 	}
11817 }
11818 
11819 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11820 {
11821 	if (!resp->supported_speeds_auto_mode &&
11822 	    !resp->supported_speeds_force_mode &&
11823 	    !resp->supported_pam4_speeds_auto_mode &&
11824 	    !resp->supported_pam4_speeds_force_mode &&
11825 	    !resp->supported_speeds2_auto_mode &&
11826 	    !resp->supported_speeds2_force_mode)
11827 		return true;
11828 	return false;
11829 }
11830 
11831 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11832 {
11833 	struct bnxt_link_info *link_info = &bp->link_info;
11834 	struct hwrm_port_phy_qcaps_output *resp;
11835 	struct hwrm_port_phy_qcaps_input *req;
11836 	int rc = 0;
11837 
11838 	if (bp->hwrm_spec_code < 0x10201)
11839 		return 0;
11840 
11841 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11842 	if (rc)
11843 		return rc;
11844 
11845 	resp = hwrm_req_hold(bp, req);
11846 	rc = hwrm_req_send(bp, req);
11847 	if (rc)
11848 		goto hwrm_phy_qcaps_exit;
11849 
11850 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11851 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11852 		struct ethtool_keee *eee = &bp->eee;
11853 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11854 
11855 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11856 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11857 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11858 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11859 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11860 	}
11861 
11862 	if (bp->hwrm_spec_code >= 0x10a01) {
11863 		if (bnxt_phy_qcaps_no_speed(resp)) {
11864 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11865 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11866 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11867 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11868 			netdev_info(bp->dev, "Ethernet link enabled\n");
11869 			/* Phy re-enabled, reprobe the speeds */
11870 			link_info->support_auto_speeds = 0;
11871 			link_info->support_pam4_auto_speeds = 0;
11872 			link_info->support_auto_speeds2 = 0;
11873 		}
11874 	}
11875 	if (resp->supported_speeds_auto_mode)
11876 		link_info->support_auto_speeds =
11877 			le16_to_cpu(resp->supported_speeds_auto_mode);
11878 	if (resp->supported_pam4_speeds_auto_mode)
11879 		link_info->support_pam4_auto_speeds =
11880 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11881 	if (resp->supported_speeds2_auto_mode)
11882 		link_info->support_auto_speeds2 =
11883 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11884 
11885 	bp->port_count = resp->port_cnt;
11886 
11887 hwrm_phy_qcaps_exit:
11888 	hwrm_req_drop(bp, req);
11889 	return rc;
11890 }
11891 
11892 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11893 {
11894 	struct hwrm_port_mac_qcaps_output *resp;
11895 	struct hwrm_port_mac_qcaps_input *req;
11896 	int rc;
11897 
11898 	if (bp->hwrm_spec_code < 0x10a03)
11899 		return;
11900 
11901 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11902 	if (rc)
11903 		return;
11904 
11905 	resp = hwrm_req_hold(bp, req);
11906 	rc = hwrm_req_send_silent(bp, req);
11907 	if (!rc)
11908 		bp->mac_flags = resp->flags;
11909 	hwrm_req_drop(bp, req);
11910 }
11911 
11912 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11913 {
11914 	u16 diff = advertising ^ supported;
11915 
11916 	return ((supported | diff) != supported);
11917 }
11918 
11919 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11920 {
11921 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11922 
11923 	/* Check if any advertised speeds are no longer supported. The caller
11924 	 * holds the link_lock mutex, so we can modify link_info settings.
11925 	 */
11926 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11927 		if (bnxt_support_dropped(link_info->advertising,
11928 					 link_info->support_auto_speeds2)) {
11929 			link_info->advertising = link_info->support_auto_speeds2;
11930 			return true;
11931 		}
11932 		return false;
11933 	}
11934 	if (bnxt_support_dropped(link_info->advertising,
11935 				 link_info->support_auto_speeds)) {
11936 		link_info->advertising = link_info->support_auto_speeds;
11937 		return true;
11938 	}
11939 	if (bnxt_support_dropped(link_info->advertising_pam4,
11940 				 link_info->support_pam4_auto_speeds)) {
11941 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11942 		return true;
11943 	}
11944 	return false;
11945 }
11946 
11947 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11948 {
11949 	struct bnxt_link_info *link_info = &bp->link_info;
11950 	struct hwrm_port_phy_qcfg_output *resp;
11951 	struct hwrm_port_phy_qcfg_input *req;
11952 	u8 link_state = link_info->link_state;
11953 	bool support_changed;
11954 	int rc;
11955 
11956 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11957 	if (rc)
11958 		return rc;
11959 
11960 	resp = hwrm_req_hold(bp, req);
11961 	rc = hwrm_req_send(bp, req);
11962 	if (rc) {
11963 		hwrm_req_drop(bp, req);
11964 		if (BNXT_VF(bp) && rc == -ENODEV) {
11965 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11966 			rc = 0;
11967 		}
11968 		return rc;
11969 	}
11970 
11971 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11972 	link_info->phy_link_status = resp->link;
11973 	link_info->duplex = resp->duplex_cfg;
11974 	if (bp->hwrm_spec_code >= 0x10800)
11975 		link_info->duplex = resp->duplex_state;
11976 	link_info->pause = resp->pause;
11977 	link_info->auto_mode = resp->auto_mode;
11978 	link_info->auto_pause_setting = resp->auto_pause;
11979 	link_info->lp_pause = resp->link_partner_adv_pause;
11980 	link_info->force_pause_setting = resp->force_pause;
11981 	link_info->duplex_setting = resp->duplex_cfg;
11982 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11983 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11984 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11985 			link_info->active_lanes = resp->active_lanes;
11986 	} else {
11987 		link_info->link_speed = 0;
11988 		link_info->active_lanes = 0;
11989 	}
11990 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11991 	link_info->force_pam4_link_speed =
11992 		le16_to_cpu(resp->force_pam4_link_speed);
11993 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11994 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11995 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11996 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11997 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11998 	link_info->auto_pam4_link_speeds =
11999 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
12000 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
12001 	link_info->lp_auto_link_speeds =
12002 		le16_to_cpu(resp->link_partner_adv_speeds);
12003 	link_info->lp_auto_pam4_link_speeds =
12004 		resp->link_partner_pam4_adv_speeds;
12005 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
12006 	link_info->phy_ver[0] = resp->phy_maj;
12007 	link_info->phy_ver[1] = resp->phy_min;
12008 	link_info->phy_ver[2] = resp->phy_bld;
12009 	link_info->media_type = resp->media_type;
12010 	link_info->phy_type = resp->phy_type;
12011 	link_info->transceiver = resp->xcvr_pkg_type;
12012 	link_info->phy_addr = resp->eee_config_phy_addr &
12013 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
12014 	link_info->module_status = resp->module_status;
12015 
12016 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
12017 		struct ethtool_keee *eee = &bp->eee;
12018 		u16 fw_speeds;
12019 
12020 		eee->eee_active = 0;
12021 		if (resp->eee_config_phy_addr &
12022 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
12023 			eee->eee_active = 1;
12024 			fw_speeds = le16_to_cpu(
12025 				resp->link_partner_adv_eee_link_speed_mask);
12026 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
12027 		}
12028 
12029 		/* Pull initial EEE config */
12030 		if (!chng_link_state) {
12031 			if (resp->eee_config_phy_addr &
12032 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
12033 				eee->eee_enabled = 1;
12034 
12035 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
12036 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
12037 
12038 			if (resp->eee_config_phy_addr &
12039 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
12040 				__le32 tmr;
12041 
12042 				eee->tx_lpi_enabled = 1;
12043 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
12044 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
12045 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
12046 			}
12047 		}
12048 	}
12049 
12050 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
12051 	if (bp->hwrm_spec_code >= 0x10504) {
12052 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
12053 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
12054 	}
12055 	/* TODO: need to add more logic to report VF link */
12056 	if (chng_link_state) {
12057 		if (link_info->phy_link_status == BNXT_LINK_LINK)
12058 			link_info->link_state = BNXT_LINK_STATE_UP;
12059 		else
12060 			link_info->link_state = BNXT_LINK_STATE_DOWN;
12061 		if (link_state != link_info->link_state)
12062 			bnxt_report_link(bp);
12063 	} else {
12064 		/* always link down if not require to update link state */
12065 		link_info->link_state = BNXT_LINK_STATE_DOWN;
12066 	}
12067 	hwrm_req_drop(bp, req);
12068 
12069 	if (!BNXT_PHY_CFG_ABLE(bp))
12070 		return 0;
12071 
12072 	support_changed = bnxt_support_speed_dropped(link_info);
12073 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
12074 		bnxt_hwrm_set_link_setting(bp, true, false);
12075 	return 0;
12076 }
12077 
12078 static void bnxt_get_port_module_status(struct bnxt *bp)
12079 {
12080 	struct bnxt_link_info *link_info = &bp->link_info;
12081 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
12082 	u8 module_status;
12083 
12084 	if (bnxt_update_link(bp, true))
12085 		return;
12086 
12087 	module_status = link_info->module_status;
12088 	switch (module_status) {
12089 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
12090 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
12091 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
12092 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
12093 			    bp->pf.port_id);
12094 		if (bp->hwrm_spec_code >= 0x10201) {
12095 			netdev_warn(bp->dev, "Module part number %s\n",
12096 				    resp->phy_vendor_partnumber);
12097 		}
12098 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
12099 			netdev_warn(bp->dev, "TX is disabled\n");
12100 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
12101 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
12102 	}
12103 }
12104 
12105 static void
12106 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12107 {
12108 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
12109 		if (bp->hwrm_spec_code >= 0x10201)
12110 			req->auto_pause =
12111 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
12112 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12113 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
12114 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12115 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
12116 		req->enables |=
12117 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12118 	} else {
12119 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
12120 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
12121 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
12122 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
12123 		req->enables |=
12124 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
12125 		if (bp->hwrm_spec_code >= 0x10201) {
12126 			req->auto_pause = req->force_pause;
12127 			req->enables |= cpu_to_le32(
12128 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
12129 		}
12130 	}
12131 }
12132 
12133 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
12134 {
12135 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
12136 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
12137 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12138 			req->enables |=
12139 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
12140 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
12141 		} else if (bp->link_info.advertising) {
12142 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
12143 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
12144 		}
12145 		if (bp->link_info.advertising_pam4) {
12146 			req->enables |=
12147 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
12148 			req->auto_link_pam4_speed_mask =
12149 				cpu_to_le16(bp->link_info.advertising_pam4);
12150 		}
12151 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
12152 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
12153 	} else {
12154 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
12155 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
12156 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
12157 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
12158 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
12159 				   (u32)bp->link_info.req_link_speed);
12160 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
12161 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12162 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
12163 		} else {
12164 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
12165 		}
12166 	}
12167 
12168 	/* tell chimp that the setting takes effect immediately */
12169 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
12170 }
12171 
12172 int bnxt_hwrm_set_pause(struct bnxt *bp)
12173 {
12174 	struct hwrm_port_phy_cfg_input *req;
12175 	int rc;
12176 
12177 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12178 	if (rc)
12179 		return rc;
12180 
12181 	bnxt_hwrm_set_pause_common(bp, req);
12182 
12183 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
12184 	    bp->link_info.force_link_chng)
12185 		bnxt_hwrm_set_link_common(bp, req);
12186 
12187 	rc = hwrm_req_send(bp, req);
12188 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
12189 		/* since changing of pause setting doesn't trigger any link
12190 		 * change event, the driver needs to update the current pause
12191 		 * result upon successfully return of the phy_cfg command
12192 		 */
12193 		bp->link_info.pause =
12194 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
12195 		bp->link_info.auto_pause_setting = 0;
12196 		if (!bp->link_info.force_link_chng)
12197 			bnxt_report_link(bp);
12198 	}
12199 	bp->link_info.force_link_chng = false;
12200 	return rc;
12201 }
12202 
12203 static void bnxt_hwrm_set_eee(struct bnxt *bp,
12204 			      struct hwrm_port_phy_cfg_input *req)
12205 {
12206 	struct ethtool_keee *eee = &bp->eee;
12207 
12208 	if (eee->eee_enabled) {
12209 		u16 eee_speeds;
12210 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
12211 
12212 		if (eee->tx_lpi_enabled)
12213 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
12214 		else
12215 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
12216 
12217 		req->flags |= cpu_to_le32(flags);
12218 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
12219 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
12220 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
12221 	} else {
12222 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
12223 	}
12224 }
12225 
12226 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
12227 {
12228 	struct hwrm_port_phy_cfg_input *req;
12229 	int rc;
12230 
12231 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12232 	if (rc)
12233 		return rc;
12234 
12235 	if (set_pause)
12236 		bnxt_hwrm_set_pause_common(bp, req);
12237 
12238 	bnxt_hwrm_set_link_common(bp, req);
12239 
12240 	if (set_eee)
12241 		bnxt_hwrm_set_eee(bp, req);
12242 	return hwrm_req_send(bp, req);
12243 }
12244 
12245 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
12246 {
12247 	struct hwrm_port_phy_cfg_input *req;
12248 	int rc;
12249 
12250 	if (!BNXT_SINGLE_PF(bp))
12251 		return 0;
12252 
12253 	if (pci_num_vf(bp->pdev) &&
12254 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
12255 		return 0;
12256 
12257 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
12258 	if (rc)
12259 		return rc;
12260 
12261 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
12262 	rc = hwrm_req_send(bp, req);
12263 	if (!rc) {
12264 		mutex_lock(&bp->link_lock);
12265 		/* Device is not obliged link down in certain scenarios, even
12266 		 * when forced. Setting the state unknown is consistent with
12267 		 * driver startup and will force link state to be reported
12268 		 * during subsequent open based on PORT_PHY_QCFG.
12269 		 */
12270 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
12271 		mutex_unlock(&bp->link_lock);
12272 	}
12273 	return rc;
12274 }
12275 
12276 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
12277 {
12278 #ifdef CONFIG_TEE_BNXT_FW
12279 	int rc = tee_bnxt_fw_load();
12280 
12281 	if (rc)
12282 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
12283 
12284 	return rc;
12285 #else
12286 	netdev_err(bp->dev, "OP-TEE not supported\n");
12287 	return -ENODEV;
12288 #endif
12289 }
12290 
12291 static int bnxt_try_recover_fw(struct bnxt *bp)
12292 {
12293 	if (bp->fw_health && bp->fw_health->status_reliable) {
12294 		int retry = 0, rc;
12295 		u32 sts;
12296 
12297 		do {
12298 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12299 			rc = bnxt_hwrm_poll(bp);
12300 			if (!BNXT_FW_IS_BOOTING(sts) &&
12301 			    !BNXT_FW_IS_RECOVERING(sts))
12302 				break;
12303 			retry++;
12304 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12305 
12306 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12307 			netdev_err(bp->dev,
12308 				   "Firmware not responding, status: 0x%x\n",
12309 				   sts);
12310 			rc = -ENODEV;
12311 		}
12312 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12313 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12314 			return bnxt_fw_reset_via_optee(bp);
12315 		}
12316 		return rc;
12317 	}
12318 
12319 	return -ENODEV;
12320 }
12321 
12322 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12323 {
12324 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12325 
12326 	if (!BNXT_NEW_RM(bp))
12327 		return; /* no resource reservations required */
12328 
12329 	hw_resc->resv_cp_rings = 0;
12330 	hw_resc->resv_stat_ctxs = 0;
12331 	hw_resc->resv_irqs = 0;
12332 	hw_resc->resv_tx_rings = 0;
12333 	hw_resc->resv_rx_rings = 0;
12334 	hw_resc->resv_hw_ring_grps = 0;
12335 	hw_resc->resv_vnics = 0;
12336 	hw_resc->resv_rsscos_ctxs = 0;
12337 	if (!fw_reset) {
12338 		bp->tx_nr_rings = 0;
12339 		bp->rx_nr_rings = 0;
12340 	}
12341 }
12342 
12343 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12344 {
12345 	int rc;
12346 
12347 	if (!BNXT_NEW_RM(bp))
12348 		return 0; /* no resource reservations required */
12349 
12350 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12351 	if (rc)
12352 		netdev_err(bp->dev, "resc_qcaps failed\n");
12353 
12354 	bnxt_clear_reservations(bp, fw_reset);
12355 
12356 	return rc;
12357 }
12358 
12359 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12360 {
12361 	struct hwrm_func_drv_if_change_output *resp;
12362 	struct hwrm_func_drv_if_change_input *req;
12363 	bool fw_reset = !bp->irq_tbl;
12364 	bool resc_reinit = false;
12365 	bool caps_change = false;
12366 	int rc, retry = 0;
12367 	u32 flags = 0;
12368 
12369 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12370 		return 0;
12371 
12372 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12373 	if (rc)
12374 		return rc;
12375 
12376 	if (up)
12377 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12378 	resp = hwrm_req_hold(bp, req);
12379 
12380 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12381 	while (retry < BNXT_FW_IF_RETRY) {
12382 		rc = hwrm_req_send(bp, req);
12383 		if (rc != -EAGAIN)
12384 			break;
12385 
12386 		msleep(50);
12387 		retry++;
12388 	}
12389 
12390 	if (rc == -EAGAIN) {
12391 		hwrm_req_drop(bp, req);
12392 		return rc;
12393 	} else if (!rc) {
12394 		flags = le32_to_cpu(resp->flags);
12395 	} else if (up) {
12396 		rc = bnxt_try_recover_fw(bp);
12397 		fw_reset = true;
12398 	}
12399 	hwrm_req_drop(bp, req);
12400 	if (rc)
12401 		return rc;
12402 
12403 	if (!up) {
12404 		bnxt_inv_fw_health_reg(bp);
12405 		return 0;
12406 	}
12407 
12408 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12409 		resc_reinit = true;
12410 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12411 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12412 		fw_reset = true;
12413 	else
12414 		bnxt_remap_fw_health_regs(bp);
12415 
12416 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12417 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12418 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12419 		return -ENODEV;
12420 	}
12421 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_CAPS_CHANGE)
12422 		caps_change = true;
12423 
12424 	if (resc_reinit || fw_reset || caps_change) {
12425 		if (fw_reset || caps_change) {
12426 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12427 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12428 				bnxt_ulp_irq_stop(bp);
12429 			bnxt_free_ctx_mem(bp, false);
12430 			bnxt_dcb_free(bp);
12431 			rc = bnxt_fw_init_one(bp);
12432 			if (rc) {
12433 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12434 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12435 				return rc;
12436 			}
12437 			bnxt_clear_int_mode(bp);
12438 			rc = bnxt_init_int_mode(bp);
12439 			if (rc) {
12440 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12441 				netdev_err(bp->dev, "init int mode failed\n");
12442 				return rc;
12443 			}
12444 		}
12445 		rc = bnxt_cancel_reservations(bp, fw_reset);
12446 	}
12447 	return rc;
12448 }
12449 
12450 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12451 {
12452 	struct hwrm_port_led_qcaps_output *resp;
12453 	struct hwrm_port_led_qcaps_input *req;
12454 	struct bnxt_pf_info *pf = &bp->pf;
12455 	int rc;
12456 
12457 	bp->num_leds = 0;
12458 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12459 		return 0;
12460 
12461 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12462 	if (rc)
12463 		return rc;
12464 
12465 	req->port_id = cpu_to_le16(pf->port_id);
12466 	resp = hwrm_req_hold(bp, req);
12467 	rc = hwrm_req_send(bp, req);
12468 	if (rc) {
12469 		hwrm_req_drop(bp, req);
12470 		return rc;
12471 	}
12472 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12473 		int i;
12474 
12475 		bp->num_leds = resp->num_leds;
12476 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12477 						 bp->num_leds);
12478 		for (i = 0; i < bp->num_leds; i++) {
12479 			struct bnxt_led_info *led = &bp->leds[i];
12480 			__le16 caps = led->led_state_caps;
12481 
12482 			if (!led->led_group_id ||
12483 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12484 				bp->num_leds = 0;
12485 				break;
12486 			}
12487 		}
12488 	}
12489 	hwrm_req_drop(bp, req);
12490 	return 0;
12491 }
12492 
12493 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12494 {
12495 	struct hwrm_wol_filter_alloc_output *resp;
12496 	struct hwrm_wol_filter_alloc_input *req;
12497 	int rc;
12498 
12499 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12500 	if (rc)
12501 		return rc;
12502 
12503 	req->port_id = cpu_to_le16(bp->pf.port_id);
12504 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12505 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12506 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12507 
12508 	resp = hwrm_req_hold(bp, req);
12509 	rc = hwrm_req_send(bp, req);
12510 	if (!rc)
12511 		bp->wol_filter_id = resp->wol_filter_id;
12512 	hwrm_req_drop(bp, req);
12513 	return rc;
12514 }
12515 
12516 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12517 {
12518 	struct hwrm_wol_filter_free_input *req;
12519 	int rc;
12520 
12521 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12522 	if (rc)
12523 		return rc;
12524 
12525 	req->port_id = cpu_to_le16(bp->pf.port_id);
12526 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12527 	req->wol_filter_id = bp->wol_filter_id;
12528 
12529 	return hwrm_req_send(bp, req);
12530 }
12531 
12532 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12533 {
12534 	struct hwrm_wol_filter_qcfg_output *resp;
12535 	struct hwrm_wol_filter_qcfg_input *req;
12536 	u16 next_handle = 0;
12537 	int rc;
12538 
12539 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12540 	if (rc)
12541 		return rc;
12542 
12543 	req->port_id = cpu_to_le16(bp->pf.port_id);
12544 	req->handle = cpu_to_le16(handle);
12545 	resp = hwrm_req_hold(bp, req);
12546 	rc = hwrm_req_send(bp, req);
12547 	if (!rc) {
12548 		next_handle = le16_to_cpu(resp->next_handle);
12549 		if (next_handle != 0) {
12550 			if (resp->wol_type ==
12551 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12552 				bp->wol = 1;
12553 				bp->wol_filter_id = resp->wol_filter_id;
12554 			}
12555 		}
12556 	}
12557 	hwrm_req_drop(bp, req);
12558 	return next_handle;
12559 }
12560 
12561 static void bnxt_get_wol_settings(struct bnxt *bp)
12562 {
12563 	u16 handle = 0;
12564 
12565 	bp->wol = 0;
12566 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12567 		return;
12568 
12569 	do {
12570 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12571 	} while (handle && handle != 0xffff);
12572 }
12573 
12574 static bool bnxt_eee_config_ok(struct bnxt *bp)
12575 {
12576 	struct ethtool_keee *eee = &bp->eee;
12577 	struct bnxt_link_info *link_info = &bp->link_info;
12578 
12579 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12580 		return true;
12581 
12582 	if (eee->eee_enabled) {
12583 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12584 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12585 
12586 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12587 
12588 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12589 			eee->eee_enabled = 0;
12590 			return false;
12591 		}
12592 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12593 			linkmode_and(eee->advertised, advertising,
12594 				     eee->supported);
12595 			return false;
12596 		}
12597 	}
12598 	return true;
12599 }
12600 
12601 static int bnxt_update_phy_setting(struct bnxt *bp)
12602 {
12603 	int rc;
12604 	bool update_link = false;
12605 	bool update_pause = false;
12606 	bool update_eee = false;
12607 	struct bnxt_link_info *link_info = &bp->link_info;
12608 
12609 	rc = bnxt_update_link(bp, true);
12610 	if (rc) {
12611 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12612 			   rc);
12613 		return rc;
12614 	}
12615 	if (!BNXT_SINGLE_PF(bp))
12616 		return 0;
12617 
12618 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12619 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12620 	    link_info->req_flow_ctrl)
12621 		update_pause = true;
12622 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12623 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12624 		update_pause = true;
12625 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12626 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12627 			update_link = true;
12628 		if (bnxt_force_speed_updated(link_info))
12629 			update_link = true;
12630 		if (link_info->req_duplex != link_info->duplex_setting)
12631 			update_link = true;
12632 	} else {
12633 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12634 			update_link = true;
12635 		if (bnxt_auto_speed_updated(link_info))
12636 			update_link = true;
12637 	}
12638 
12639 	/* The last close may have shutdown the link, so need to call
12640 	 * PHY_CFG to bring it back up.
12641 	 */
12642 	if (!BNXT_LINK_IS_UP(bp))
12643 		update_link = true;
12644 
12645 	if (!bnxt_eee_config_ok(bp))
12646 		update_eee = true;
12647 
12648 	if (update_link)
12649 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12650 	else if (update_pause)
12651 		rc = bnxt_hwrm_set_pause(bp);
12652 	if (rc) {
12653 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12654 			   rc);
12655 		return rc;
12656 	}
12657 
12658 	return rc;
12659 }
12660 
12661 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12662 
12663 static int bnxt_reinit_after_abort(struct bnxt *bp)
12664 {
12665 	int rc;
12666 
12667 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12668 		return -EBUSY;
12669 
12670 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12671 		return -ENODEV;
12672 
12673 	rc = bnxt_fw_init_one(bp);
12674 	if (!rc) {
12675 		bnxt_clear_int_mode(bp);
12676 		rc = bnxt_init_int_mode(bp);
12677 		if (!rc) {
12678 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12679 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12680 		}
12681 	}
12682 	return rc;
12683 }
12684 
12685 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12686 {
12687 	struct bnxt_ntuple_filter *ntp_fltr;
12688 	struct bnxt_l2_filter *l2_fltr;
12689 
12690 	if (list_empty(&fltr->list))
12691 		return;
12692 
12693 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12694 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12695 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12696 		atomic_inc(&l2_fltr->refcnt);
12697 		ntp_fltr->l2_fltr = l2_fltr;
12698 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12699 			bnxt_del_ntp_filter(bp, ntp_fltr);
12700 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12701 				   fltr->sw_id);
12702 		}
12703 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12704 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12705 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12706 			bnxt_del_l2_filter(bp, l2_fltr);
12707 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12708 				   fltr->sw_id);
12709 		}
12710 	}
12711 }
12712 
12713 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12714 {
12715 	struct bnxt_filter_base *usr_fltr, *tmp;
12716 
12717 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12718 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12719 }
12720 
12721 static int bnxt_set_xps_mapping(struct bnxt *bp)
12722 {
12723 	int numa_node = dev_to_node(&bp->pdev->dev);
12724 	unsigned int q_idx, map_idx, cpu, i;
12725 	const struct cpumask *cpu_mask_ptr;
12726 	int nr_cpus = num_online_cpus();
12727 	cpumask_t *q_map;
12728 	int rc = 0;
12729 
12730 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12731 	if (!q_map)
12732 		return -ENOMEM;
12733 
12734 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12735 	 * Each TC has the same number of TX queues. The nth TX queue for each
12736 	 * TC will have the same CPU mask.
12737 	 */
12738 	for (i = 0; i < nr_cpus; i++) {
12739 		map_idx = i % bp->tx_nr_rings_per_tc;
12740 		cpu = cpumask_local_spread(i, numa_node);
12741 		cpu_mask_ptr = get_cpu_mask(cpu);
12742 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12743 	}
12744 
12745 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12746 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12747 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12748 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12749 		if (rc) {
12750 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12751 				    q_idx);
12752 			break;
12753 		}
12754 	}
12755 
12756 	kfree(q_map);
12757 
12758 	return rc;
12759 }
12760 
12761 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12762 {
12763 	int rc = 0;
12764 
12765 	netif_carrier_off(bp->dev);
12766 	if (irq_re_init) {
12767 		/* Reserve rings now if none were reserved at driver probe. */
12768 		rc = bnxt_init_dflt_ring_mode(bp);
12769 		if (rc) {
12770 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12771 			return rc;
12772 		}
12773 	}
12774 	rc = bnxt_reserve_rings(bp, irq_re_init);
12775 	if (rc)
12776 		return rc;
12777 
12778 	rc = bnxt_alloc_mem(bp, irq_re_init);
12779 	if (rc) {
12780 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12781 		goto open_err_free_mem;
12782 	}
12783 
12784 	if (irq_re_init) {
12785 		bnxt_init_napi(bp);
12786 		rc = bnxt_request_irq(bp);
12787 		if (rc) {
12788 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12789 			goto open_err_irq;
12790 		}
12791 	}
12792 
12793 	rc = bnxt_init_nic(bp, irq_re_init);
12794 	if (rc) {
12795 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12796 		goto open_err_irq;
12797 	}
12798 
12799 	bnxt_enable_napi(bp);
12800 	bnxt_debug_dev_init(bp);
12801 
12802 	if (link_re_init) {
12803 		mutex_lock(&bp->link_lock);
12804 		rc = bnxt_update_phy_setting(bp);
12805 		mutex_unlock(&bp->link_lock);
12806 		if (rc) {
12807 			netdev_warn(bp->dev, "failed to update phy settings\n");
12808 			if (BNXT_SINGLE_PF(bp)) {
12809 				bp->link_info.phy_retry = true;
12810 				bp->link_info.phy_retry_expires =
12811 					jiffies + 5 * HZ;
12812 			}
12813 		}
12814 	}
12815 
12816 	if (irq_re_init) {
12817 		udp_tunnel_nic_reset_ntf(bp->dev);
12818 		rc = bnxt_set_xps_mapping(bp);
12819 		if (rc)
12820 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12821 	}
12822 
12823 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12824 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12825 			static_branch_enable(&bnxt_xdp_locking_key);
12826 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12827 		static_branch_disable(&bnxt_xdp_locking_key);
12828 	}
12829 	set_bit(BNXT_STATE_OPEN, &bp->state);
12830 	bnxt_enable_int(bp);
12831 	/* Enable TX queues */
12832 	bnxt_tx_enable(bp);
12833 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12834 	/* Poll link status and check for SFP+ module status */
12835 	mutex_lock(&bp->link_lock);
12836 	bnxt_get_port_module_status(bp);
12837 	mutex_unlock(&bp->link_lock);
12838 
12839 	/* VF-reps may need to be re-opened after the PF is re-opened */
12840 	if (BNXT_PF(bp))
12841 		bnxt_vf_reps_open(bp);
12842 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12843 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12844 	bnxt_ptp_init_rtc(bp, true);
12845 	bnxt_ptp_cfg_tstamp_filters(bp);
12846 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12847 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12848 	bnxt_cfg_usr_fltrs(bp);
12849 	return 0;
12850 
12851 open_err_irq:
12852 	bnxt_del_napi(bp);
12853 
12854 open_err_free_mem:
12855 	bnxt_free_skbs(bp);
12856 	bnxt_free_irq(bp);
12857 	bnxt_free_mem(bp, true);
12858 	return rc;
12859 }
12860 
12861 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12862 {
12863 	int rc = 0;
12864 
12865 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12866 		rc = -EIO;
12867 	if (!rc)
12868 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12869 	if (rc) {
12870 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12871 		netif_close(bp->dev);
12872 	}
12873 	return rc;
12874 }
12875 
12876 /* netdev instance lock held, open the NIC half way by allocating all
12877  * resources, but NAPI, IRQ, and TX are not enabled.  This is mainly used
12878  * for offline self tests.
12879  */
12880 int bnxt_half_open_nic(struct bnxt *bp)
12881 {
12882 	int rc = 0;
12883 
12884 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12885 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12886 		rc = -ENODEV;
12887 		goto half_open_err;
12888 	}
12889 
12890 	rc = bnxt_alloc_mem(bp, true);
12891 	if (rc) {
12892 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12893 		goto half_open_err;
12894 	}
12895 	bnxt_init_napi(bp);
12896 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12897 	rc = bnxt_init_nic(bp, true);
12898 	if (rc) {
12899 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12900 		bnxt_del_napi(bp);
12901 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12902 		goto half_open_err;
12903 	}
12904 	return 0;
12905 
12906 half_open_err:
12907 	bnxt_free_skbs(bp);
12908 	bnxt_free_mem(bp, true);
12909 	netif_close(bp->dev);
12910 	return rc;
12911 }
12912 
12913 /* netdev instance lock held, this call can only be made after a previous
12914  * successful call to bnxt_half_open_nic().
12915  */
12916 void bnxt_half_close_nic(struct bnxt *bp)
12917 {
12918 	bnxt_hwrm_resource_free(bp, false, true);
12919 	bnxt_del_napi(bp);
12920 	bnxt_free_skbs(bp);
12921 	bnxt_free_mem(bp, true);
12922 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12923 }
12924 
12925 void bnxt_reenable_sriov(struct bnxt *bp)
12926 {
12927 	if (BNXT_PF(bp)) {
12928 		struct bnxt_pf_info *pf = &bp->pf;
12929 		int n = pf->active_vfs;
12930 
12931 		if (n)
12932 			bnxt_cfg_hw_sriov(bp, &n, true);
12933 	}
12934 }
12935 
12936 static int bnxt_open(struct net_device *dev)
12937 {
12938 	struct bnxt *bp = netdev_priv(dev);
12939 	int rc;
12940 
12941 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12942 		rc = bnxt_reinit_after_abort(bp);
12943 		if (rc) {
12944 			if (rc == -EBUSY)
12945 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12946 			else
12947 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12948 			return -ENODEV;
12949 		}
12950 	}
12951 
12952 	rc = bnxt_hwrm_if_change(bp, true);
12953 	if (rc)
12954 		return rc;
12955 
12956 	rc = __bnxt_open_nic(bp, true, true);
12957 	if (rc) {
12958 		bnxt_hwrm_if_change(bp, false);
12959 	} else {
12960 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12961 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12962 				bnxt_queue_sp_work(bp,
12963 						   BNXT_RESTART_ULP_SP_EVENT);
12964 		}
12965 	}
12966 
12967 	return rc;
12968 }
12969 
12970 static bool bnxt_drv_busy(struct bnxt *bp)
12971 {
12972 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12973 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12974 }
12975 
12976 static void bnxt_get_ring_stats(struct bnxt *bp,
12977 				struct rtnl_link_stats64 *stats);
12978 
12979 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12980 			     bool link_re_init)
12981 {
12982 	/* Close the VF-reps before closing PF */
12983 	if (BNXT_PF(bp))
12984 		bnxt_vf_reps_close(bp);
12985 
12986 	/* Change device state to avoid TX queue wake up's */
12987 	bnxt_tx_disable(bp);
12988 
12989 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12990 	smp_mb__after_atomic();
12991 	while (bnxt_drv_busy(bp))
12992 		msleep(20);
12993 
12994 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12995 		bnxt_clear_rss_ctxs(bp);
12996 	/* Flush rings and disable interrupts */
12997 	bnxt_shutdown_nic(bp, irq_re_init);
12998 
12999 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
13000 
13001 	bnxt_debug_dev_exit(bp);
13002 	bnxt_disable_napi(bp);
13003 	timer_delete_sync(&bp->timer);
13004 	bnxt_free_skbs(bp);
13005 
13006 	/* Save ring stats before shutdown */
13007 	if (bp->bnapi && irq_re_init) {
13008 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
13009 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
13010 	}
13011 	if (irq_re_init) {
13012 		bnxt_free_irq(bp);
13013 		bnxt_del_napi(bp);
13014 	}
13015 	bnxt_free_mem(bp, irq_re_init);
13016 }
13017 
13018 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
13019 {
13020 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13021 		/* If we get here, it means firmware reset is in progress
13022 		 * while we are trying to close.  We can safely proceed with
13023 		 * the close because we are holding netdev instance lock.
13024 		 * Some firmware messages may fail as we proceed to close.
13025 		 * We set the ABORT_ERR flag here so that the FW reset thread
13026 		 * will later abort when it gets the netdev instance lock
13027 		 * and sees the flag.
13028 		 */
13029 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
13030 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
13031 	}
13032 
13033 #ifdef CONFIG_BNXT_SRIOV
13034 	if (bp->sriov_cfg) {
13035 		int rc;
13036 
13037 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
13038 						      !bp->sriov_cfg,
13039 						      BNXT_SRIOV_CFG_WAIT_TMO);
13040 		if (!rc)
13041 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
13042 		else if (rc < 0)
13043 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
13044 	}
13045 #endif
13046 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
13047 }
13048 
13049 static int bnxt_close(struct net_device *dev)
13050 {
13051 	struct bnxt *bp = netdev_priv(dev);
13052 
13053 	bnxt_close_nic(bp, true, true);
13054 	bnxt_hwrm_shutdown_link(bp);
13055 	bnxt_hwrm_if_change(bp, false);
13056 	return 0;
13057 }
13058 
13059 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
13060 				   u16 *val)
13061 {
13062 	struct hwrm_port_phy_mdio_read_output *resp;
13063 	struct hwrm_port_phy_mdio_read_input *req;
13064 	int rc;
13065 
13066 	if (bp->hwrm_spec_code < 0x10a00)
13067 		return -EOPNOTSUPP;
13068 
13069 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
13070 	if (rc)
13071 		return rc;
13072 
13073 	req->port_id = cpu_to_le16(bp->pf.port_id);
13074 	req->phy_addr = phy_addr;
13075 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13076 	if (mdio_phy_id_is_c45(phy_addr)) {
13077 		req->cl45_mdio = 1;
13078 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13079 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13080 		req->reg_addr = cpu_to_le16(reg);
13081 	}
13082 
13083 	resp = hwrm_req_hold(bp, req);
13084 	rc = hwrm_req_send(bp, req);
13085 	if (!rc)
13086 		*val = le16_to_cpu(resp->reg_data);
13087 	hwrm_req_drop(bp, req);
13088 	return rc;
13089 }
13090 
13091 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
13092 				    u16 val)
13093 {
13094 	struct hwrm_port_phy_mdio_write_input *req;
13095 	int rc;
13096 
13097 	if (bp->hwrm_spec_code < 0x10a00)
13098 		return -EOPNOTSUPP;
13099 
13100 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
13101 	if (rc)
13102 		return rc;
13103 
13104 	req->port_id = cpu_to_le16(bp->pf.port_id);
13105 	req->phy_addr = phy_addr;
13106 	req->reg_addr = cpu_to_le16(reg & 0x1f);
13107 	if (mdio_phy_id_is_c45(phy_addr)) {
13108 		req->cl45_mdio = 1;
13109 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
13110 		req->dev_addr = mdio_phy_id_devad(phy_addr);
13111 		req->reg_addr = cpu_to_le16(reg);
13112 	}
13113 	req->reg_data = cpu_to_le16(val);
13114 
13115 	return hwrm_req_send(bp, req);
13116 }
13117 
13118 /* netdev instance lock held */
13119 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
13120 {
13121 	struct mii_ioctl_data *mdio = if_mii(ifr);
13122 	struct bnxt *bp = netdev_priv(dev);
13123 	int rc;
13124 
13125 	switch (cmd) {
13126 	case SIOCGMIIPHY:
13127 		mdio->phy_id = bp->link_info.phy_addr;
13128 
13129 		fallthrough;
13130 	case SIOCGMIIREG: {
13131 		u16 mii_regval = 0;
13132 
13133 		if (!netif_running(dev))
13134 			return -EAGAIN;
13135 
13136 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
13137 					     &mii_regval);
13138 		mdio->val_out = mii_regval;
13139 		return rc;
13140 	}
13141 
13142 	case SIOCSMIIREG:
13143 		if (!netif_running(dev))
13144 			return -EAGAIN;
13145 
13146 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
13147 						mdio->val_in);
13148 
13149 	case SIOCSHWTSTAMP:
13150 		return bnxt_hwtstamp_set(dev, ifr);
13151 
13152 	case SIOCGHWTSTAMP:
13153 		return bnxt_hwtstamp_get(dev, ifr);
13154 
13155 	default:
13156 		/* do nothing */
13157 		break;
13158 	}
13159 	return -EOPNOTSUPP;
13160 }
13161 
13162 static void bnxt_get_ring_stats(struct bnxt *bp,
13163 				struct rtnl_link_stats64 *stats)
13164 {
13165 	int i;
13166 
13167 	for (i = 0; i < bp->cp_nr_rings; i++) {
13168 		struct bnxt_napi *bnapi = bp->bnapi[i];
13169 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13170 		u64 *sw = cpr->stats.sw_stats;
13171 
13172 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
13173 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13174 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
13175 
13176 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
13177 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
13178 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
13179 
13180 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
13181 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
13182 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
13183 
13184 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
13185 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
13186 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
13187 
13188 		stats->rx_missed_errors +=
13189 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
13190 
13191 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
13192 
13193 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
13194 
13195 		stats->rx_dropped +=
13196 			cpr->sw_stats->rx.rx_netpoll_discards +
13197 			cpr->sw_stats->rx.rx_oom_discards;
13198 	}
13199 }
13200 
13201 static void bnxt_add_prev_stats(struct bnxt *bp,
13202 				struct rtnl_link_stats64 *stats)
13203 {
13204 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
13205 
13206 	stats->rx_packets += prev_stats->rx_packets;
13207 	stats->tx_packets += prev_stats->tx_packets;
13208 	stats->rx_bytes += prev_stats->rx_bytes;
13209 	stats->tx_bytes += prev_stats->tx_bytes;
13210 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
13211 	stats->multicast += prev_stats->multicast;
13212 	stats->rx_dropped += prev_stats->rx_dropped;
13213 	stats->tx_dropped += prev_stats->tx_dropped;
13214 }
13215 
13216 static void
13217 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
13218 {
13219 	struct bnxt *bp = netdev_priv(dev);
13220 
13221 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
13222 	/* Make sure bnxt_close_nic() sees that we are reading stats before
13223 	 * we check the BNXT_STATE_OPEN flag.
13224 	 */
13225 	smp_mb__after_atomic();
13226 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13227 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13228 		*stats = bp->net_stats_prev;
13229 		return;
13230 	}
13231 
13232 	bnxt_get_ring_stats(bp, stats);
13233 	bnxt_add_prev_stats(bp, stats);
13234 
13235 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
13236 		u64 *rx = bp->port_stats.sw_stats;
13237 		u64 *tx = bp->port_stats.sw_stats +
13238 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
13239 
13240 		stats->rx_crc_errors =
13241 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
13242 		stats->rx_frame_errors =
13243 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
13244 		stats->rx_length_errors =
13245 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
13246 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
13247 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
13248 		stats->rx_errors =
13249 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
13250 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
13251 		stats->collisions =
13252 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
13253 		stats->tx_fifo_errors =
13254 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
13255 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
13256 	}
13257 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
13258 }
13259 
13260 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
13261 					struct bnxt_total_ring_err_stats *stats,
13262 					struct bnxt_cp_ring_info *cpr)
13263 {
13264 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
13265 	u64 *hw_stats = cpr->stats.sw_stats;
13266 
13267 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
13268 	stats->rx_total_resets += sw_stats->rx.rx_resets;
13269 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
13270 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
13271 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
13272 	stats->rx_total_ring_discards +=
13273 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
13274 	stats->tx_total_resets += sw_stats->tx.tx_resets;
13275 	stats->tx_total_ring_discards +=
13276 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
13277 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
13278 }
13279 
13280 void bnxt_get_ring_err_stats(struct bnxt *bp,
13281 			     struct bnxt_total_ring_err_stats *stats)
13282 {
13283 	int i;
13284 
13285 	for (i = 0; i < bp->cp_nr_rings; i++)
13286 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
13287 }
13288 
13289 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13290 {
13291 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13292 	struct net_device *dev = bp->dev;
13293 	struct netdev_hw_addr *ha;
13294 	u8 *haddr;
13295 	int mc_count = 0;
13296 	bool update = false;
13297 	int off = 0;
13298 
13299 	netdev_for_each_mc_addr(ha, dev) {
13300 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13301 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13302 			vnic->mc_list_count = 0;
13303 			return false;
13304 		}
13305 		haddr = ha->addr;
13306 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13307 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13308 			update = true;
13309 		}
13310 		off += ETH_ALEN;
13311 		mc_count++;
13312 	}
13313 	if (mc_count)
13314 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13315 
13316 	if (mc_count != vnic->mc_list_count) {
13317 		vnic->mc_list_count = mc_count;
13318 		update = true;
13319 	}
13320 	return update;
13321 }
13322 
13323 static bool bnxt_uc_list_updated(struct bnxt *bp)
13324 {
13325 	struct net_device *dev = bp->dev;
13326 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13327 	struct netdev_hw_addr *ha;
13328 	int off = 0;
13329 
13330 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13331 		return true;
13332 
13333 	netdev_for_each_uc_addr(ha, dev) {
13334 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13335 			return true;
13336 
13337 		off += ETH_ALEN;
13338 	}
13339 	return false;
13340 }
13341 
13342 static void bnxt_set_rx_mode(struct net_device *dev)
13343 {
13344 	struct bnxt *bp = netdev_priv(dev);
13345 	struct bnxt_vnic_info *vnic;
13346 	bool mc_update = false;
13347 	bool uc_update;
13348 	u32 mask;
13349 
13350 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13351 		return;
13352 
13353 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13354 	mask = vnic->rx_mask;
13355 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13356 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13357 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13358 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13359 
13360 	if (dev->flags & IFF_PROMISC)
13361 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13362 
13363 	uc_update = bnxt_uc_list_updated(bp);
13364 
13365 	if (dev->flags & IFF_BROADCAST)
13366 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13367 	if (dev->flags & IFF_ALLMULTI) {
13368 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13369 		vnic->mc_list_count = 0;
13370 	} else if (dev->flags & IFF_MULTICAST) {
13371 		mc_update = bnxt_mc_list_updated(bp, &mask);
13372 	}
13373 
13374 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13375 		vnic->rx_mask = mask;
13376 
13377 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13378 	}
13379 }
13380 
13381 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13382 {
13383 	struct net_device *dev = bp->dev;
13384 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13385 	struct netdev_hw_addr *ha;
13386 	int i, off = 0, rc;
13387 	bool uc_update;
13388 
13389 	netif_addr_lock_bh(dev);
13390 	uc_update = bnxt_uc_list_updated(bp);
13391 	netif_addr_unlock_bh(dev);
13392 
13393 	if (!uc_update)
13394 		goto skip_uc;
13395 
13396 	for (i = 1; i < vnic->uc_filter_count; i++) {
13397 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13398 
13399 		bnxt_hwrm_l2_filter_free(bp, fltr);
13400 		bnxt_del_l2_filter(bp, fltr);
13401 	}
13402 
13403 	vnic->uc_filter_count = 1;
13404 
13405 	netif_addr_lock_bh(dev);
13406 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13407 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13408 	} else {
13409 		netdev_for_each_uc_addr(ha, dev) {
13410 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13411 			off += ETH_ALEN;
13412 			vnic->uc_filter_count++;
13413 		}
13414 	}
13415 	netif_addr_unlock_bh(dev);
13416 
13417 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13418 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13419 		if (rc) {
13420 			if (BNXT_VF(bp) && rc == -ENODEV) {
13421 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13422 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13423 				else
13424 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13425 				rc = 0;
13426 			} else {
13427 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13428 			}
13429 			vnic->uc_filter_count = i;
13430 			return rc;
13431 		}
13432 	}
13433 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13434 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13435 
13436 skip_uc:
13437 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13438 	    !bnxt_promisc_ok(bp))
13439 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13440 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13441 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13442 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13443 			    rc);
13444 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13445 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13446 		vnic->mc_list_count = 0;
13447 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13448 	}
13449 	if (rc)
13450 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13451 			   rc);
13452 
13453 	return rc;
13454 }
13455 
13456 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13457 {
13458 #ifdef CONFIG_BNXT_SRIOV
13459 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13460 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13461 
13462 		/* No minimum rings were provisioned by the PF.  Don't
13463 		 * reserve rings by default when device is down.
13464 		 */
13465 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13466 			return true;
13467 
13468 		if (!netif_running(bp->dev))
13469 			return false;
13470 	}
13471 #endif
13472 	return true;
13473 }
13474 
13475 /* If the chip and firmware supports RFS */
13476 static bool bnxt_rfs_supported(struct bnxt *bp)
13477 {
13478 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13479 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13480 			return true;
13481 		return false;
13482 	}
13483 	/* 212 firmware is broken for aRFS */
13484 	if (BNXT_FW_MAJ(bp) == 212)
13485 		return false;
13486 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13487 		return true;
13488 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13489 		return true;
13490 	return false;
13491 }
13492 
13493 /* If runtime conditions support RFS */
13494 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13495 {
13496 	struct bnxt_hw_rings hwr = {0};
13497 	int max_vnics, max_rss_ctxs;
13498 
13499 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13500 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13501 		return bnxt_rfs_supported(bp);
13502 
13503 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13504 		return false;
13505 
13506 	hwr.grp = bp->rx_nr_rings;
13507 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13508 	if (new_rss_ctx)
13509 		hwr.vnic++;
13510 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13511 	max_vnics = bnxt_get_max_func_vnics(bp);
13512 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13513 
13514 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13515 		if (bp->rx_nr_rings > 1)
13516 			netdev_warn(bp->dev,
13517 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13518 				    min(max_rss_ctxs - 1, max_vnics - 1));
13519 		return false;
13520 	}
13521 
13522 	if (!BNXT_NEW_RM(bp))
13523 		return true;
13524 
13525 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13526 	 * issue that will mess up the default VNIC if we reduce the
13527 	 * reservations.
13528 	 */
13529 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13530 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13531 		return true;
13532 
13533 	bnxt_hwrm_reserve_rings(bp, &hwr);
13534 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13535 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13536 		return true;
13537 
13538 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13539 	hwr.vnic = 1;
13540 	hwr.rss_ctx = 0;
13541 	bnxt_hwrm_reserve_rings(bp, &hwr);
13542 	return false;
13543 }
13544 
13545 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13546 					   netdev_features_t features)
13547 {
13548 	struct bnxt *bp = netdev_priv(dev);
13549 	netdev_features_t vlan_features;
13550 
13551 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13552 		features &= ~NETIF_F_NTUPLE;
13553 
13554 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13555 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13556 
13557 	if (!(features & NETIF_F_GRO))
13558 		features &= ~NETIF_F_GRO_HW;
13559 
13560 	if (features & NETIF_F_GRO_HW)
13561 		features &= ~NETIF_F_LRO;
13562 
13563 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13564 	 * turned on or off together.
13565 	 */
13566 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13567 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13568 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13569 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13570 		else if (vlan_features)
13571 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13572 	}
13573 #ifdef CONFIG_BNXT_SRIOV
13574 	if (BNXT_VF(bp) && bp->vf.vlan)
13575 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13576 #endif
13577 	return features;
13578 }
13579 
13580 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13581 				bool link_re_init, u32 flags, bool update_tpa)
13582 {
13583 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13584 	bp->flags = flags;
13585 	if (update_tpa)
13586 		bnxt_set_ring_params(bp);
13587 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13588 }
13589 
13590 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13591 {
13592 	bool update_tpa = false, update_ntuple = false;
13593 	struct bnxt *bp = netdev_priv(dev);
13594 	u32 flags = bp->flags;
13595 	u32 changes;
13596 	int rc = 0;
13597 	bool re_init = false;
13598 
13599 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13600 	if (features & NETIF_F_GRO_HW)
13601 		flags |= BNXT_FLAG_GRO;
13602 	else if (features & NETIF_F_LRO)
13603 		flags |= BNXT_FLAG_LRO;
13604 
13605 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13606 		flags &= ~BNXT_FLAG_TPA;
13607 
13608 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13609 		flags |= BNXT_FLAG_STRIP_VLAN;
13610 
13611 	if (features & NETIF_F_NTUPLE)
13612 		flags |= BNXT_FLAG_RFS;
13613 	else
13614 		bnxt_clear_usr_fltrs(bp, true);
13615 
13616 	changes = flags ^ bp->flags;
13617 	if (changes & BNXT_FLAG_TPA) {
13618 		update_tpa = true;
13619 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13620 		    (flags & BNXT_FLAG_TPA) == 0 ||
13621 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13622 			re_init = true;
13623 	}
13624 
13625 	if (changes & ~BNXT_FLAG_TPA)
13626 		re_init = true;
13627 
13628 	if (changes & BNXT_FLAG_RFS)
13629 		update_ntuple = true;
13630 
13631 	if (flags != bp->flags) {
13632 		u32 old_flags = bp->flags;
13633 
13634 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13635 			bp->flags = flags;
13636 			if (update_tpa)
13637 				bnxt_set_ring_params(bp);
13638 			return rc;
13639 		}
13640 
13641 		if (update_ntuple)
13642 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13643 
13644 		if (re_init)
13645 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13646 
13647 		if (update_tpa) {
13648 			bp->flags = flags;
13649 			rc = bnxt_set_tpa(bp,
13650 					  (flags & BNXT_FLAG_TPA) ?
13651 					  true : false);
13652 			if (rc)
13653 				bp->flags = old_flags;
13654 		}
13655 	}
13656 	return rc;
13657 }
13658 
13659 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13660 			      u8 **nextp)
13661 {
13662 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13663 	struct hop_jumbo_hdr *jhdr;
13664 	int hdr_count = 0;
13665 	u8 *nexthdr;
13666 	int start;
13667 
13668 	/* Check that there are at most 2 IPv6 extension headers, no
13669 	 * fragment header, and each is <= 64 bytes.
13670 	 */
13671 	start = nw_off + sizeof(*ip6h);
13672 	nexthdr = &ip6h->nexthdr;
13673 	while (ipv6_ext_hdr(*nexthdr)) {
13674 		struct ipv6_opt_hdr *hp;
13675 		int hdrlen;
13676 
13677 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13678 		    *nexthdr == NEXTHDR_FRAGMENT)
13679 			return false;
13680 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13681 					  skb_headlen(skb), NULL);
13682 		if (!hp)
13683 			return false;
13684 		if (*nexthdr == NEXTHDR_AUTH)
13685 			hdrlen = ipv6_authlen(hp);
13686 		else
13687 			hdrlen = ipv6_optlen(hp);
13688 
13689 		if (hdrlen > 64)
13690 			return false;
13691 
13692 		/* The ext header may be a hop-by-hop header inserted for
13693 		 * big TCP purposes. This will be removed before sending
13694 		 * from NIC, so do not count it.
13695 		 */
13696 		if (*nexthdr == NEXTHDR_HOP) {
13697 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13698 				goto increment_hdr;
13699 
13700 			jhdr = (struct hop_jumbo_hdr *)hp;
13701 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13702 			    jhdr->nexthdr != IPPROTO_TCP)
13703 				goto increment_hdr;
13704 
13705 			goto next_hdr;
13706 		}
13707 increment_hdr:
13708 		hdr_count++;
13709 next_hdr:
13710 		nexthdr = &hp->nexthdr;
13711 		start += hdrlen;
13712 	}
13713 	if (nextp) {
13714 		/* Caller will check inner protocol */
13715 		if (skb->encapsulation) {
13716 			*nextp = nexthdr;
13717 			return true;
13718 		}
13719 		*nextp = NULL;
13720 	}
13721 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13722 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13723 }
13724 
13725 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13726 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13727 {
13728 	struct udphdr *uh = udp_hdr(skb);
13729 	__be16 udp_port = uh->dest;
13730 
13731 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13732 	    udp_port != bp->vxlan_gpe_port)
13733 		return false;
13734 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13735 		struct ethhdr *eh = inner_eth_hdr(skb);
13736 
13737 		switch (eh->h_proto) {
13738 		case htons(ETH_P_IP):
13739 			return true;
13740 		case htons(ETH_P_IPV6):
13741 			return bnxt_exthdr_check(bp, skb,
13742 						 skb_inner_network_offset(skb),
13743 						 NULL);
13744 		}
13745 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13746 		return true;
13747 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13748 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13749 					 NULL);
13750 	}
13751 	return false;
13752 }
13753 
13754 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13755 {
13756 	switch (l4_proto) {
13757 	case IPPROTO_UDP:
13758 		return bnxt_udp_tunl_check(bp, skb);
13759 	case IPPROTO_IPIP:
13760 		return true;
13761 	case IPPROTO_GRE: {
13762 		switch (skb->inner_protocol) {
13763 		default:
13764 			return false;
13765 		case htons(ETH_P_IP):
13766 			return true;
13767 		case htons(ETH_P_IPV6):
13768 			fallthrough;
13769 		}
13770 	}
13771 	case IPPROTO_IPV6:
13772 		/* Check ext headers of inner ipv6 */
13773 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13774 					 NULL);
13775 	}
13776 	return false;
13777 }
13778 
13779 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13780 					     struct net_device *dev,
13781 					     netdev_features_t features)
13782 {
13783 	struct bnxt *bp = netdev_priv(dev);
13784 	u8 *l4_proto;
13785 
13786 	features = vlan_features_check(skb, features);
13787 	switch (vlan_get_protocol(skb)) {
13788 	case htons(ETH_P_IP):
13789 		if (!skb->encapsulation)
13790 			return features;
13791 		l4_proto = &ip_hdr(skb)->protocol;
13792 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13793 			return features;
13794 		break;
13795 	case htons(ETH_P_IPV6):
13796 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13797 				       &l4_proto))
13798 			break;
13799 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13800 			return features;
13801 		break;
13802 	}
13803 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13804 }
13805 
13806 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13807 			 u32 *reg_buf)
13808 {
13809 	struct hwrm_dbg_read_direct_output *resp;
13810 	struct hwrm_dbg_read_direct_input *req;
13811 	__le32 *dbg_reg_buf;
13812 	dma_addr_t mapping;
13813 	int rc, i;
13814 
13815 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13816 	if (rc)
13817 		return rc;
13818 
13819 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13820 					 &mapping);
13821 	if (!dbg_reg_buf) {
13822 		rc = -ENOMEM;
13823 		goto dbg_rd_reg_exit;
13824 	}
13825 
13826 	req->host_dest_addr = cpu_to_le64(mapping);
13827 
13828 	resp = hwrm_req_hold(bp, req);
13829 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13830 	req->read_len32 = cpu_to_le32(num_words);
13831 
13832 	rc = hwrm_req_send(bp, req);
13833 	if (rc || resp->error_code) {
13834 		rc = -EIO;
13835 		goto dbg_rd_reg_exit;
13836 	}
13837 	for (i = 0; i < num_words; i++)
13838 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13839 
13840 dbg_rd_reg_exit:
13841 	hwrm_req_drop(bp, req);
13842 	return rc;
13843 }
13844 
13845 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13846 				       u32 ring_id, u32 *prod, u32 *cons)
13847 {
13848 	struct hwrm_dbg_ring_info_get_output *resp;
13849 	struct hwrm_dbg_ring_info_get_input *req;
13850 	int rc;
13851 
13852 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13853 	if (rc)
13854 		return rc;
13855 
13856 	req->ring_type = ring_type;
13857 	req->fw_ring_id = cpu_to_le32(ring_id);
13858 	resp = hwrm_req_hold(bp, req);
13859 	rc = hwrm_req_send(bp, req);
13860 	if (!rc) {
13861 		*prod = le32_to_cpu(resp->producer_index);
13862 		*cons = le32_to_cpu(resp->consumer_index);
13863 	}
13864 	hwrm_req_drop(bp, req);
13865 	return rc;
13866 }
13867 
13868 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13869 {
13870 	struct bnxt_tx_ring_info *txr;
13871 	int i = bnapi->index, j;
13872 
13873 	bnxt_for_each_napi_tx(j, bnapi, txr)
13874 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13875 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13876 			    txr->tx_cons);
13877 }
13878 
13879 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13880 {
13881 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13882 	int i = bnapi->index;
13883 
13884 	if (!rxr)
13885 		return;
13886 
13887 	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",
13888 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13889 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13890 		    rxr->rx_sw_agg_prod);
13891 }
13892 
13893 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13894 {
13895 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13896 	int i = bnapi->index;
13897 
13898 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13899 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13900 }
13901 
13902 static void bnxt_dbg_dump_states(struct bnxt *bp)
13903 {
13904 	int i;
13905 	struct bnxt_napi *bnapi;
13906 
13907 	for (i = 0; i < bp->cp_nr_rings; i++) {
13908 		bnapi = bp->bnapi[i];
13909 		if (netif_msg_drv(bp)) {
13910 			bnxt_dump_tx_sw_state(bnapi);
13911 			bnxt_dump_rx_sw_state(bnapi);
13912 			bnxt_dump_cp_sw_state(bnapi);
13913 		}
13914 	}
13915 }
13916 
13917 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13918 {
13919 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13920 	struct hwrm_ring_reset_input *req;
13921 	struct bnxt_napi *bnapi = rxr->bnapi;
13922 	struct bnxt_cp_ring_info *cpr;
13923 	u16 cp_ring_id;
13924 	int rc;
13925 
13926 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13927 	if (rc)
13928 		return rc;
13929 
13930 	cpr = &bnapi->cp_ring;
13931 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13932 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13933 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13934 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13935 	return hwrm_req_send_silent(bp, req);
13936 }
13937 
13938 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13939 {
13940 	if (!silent)
13941 		bnxt_dbg_dump_states(bp);
13942 	if (netif_running(bp->dev)) {
13943 		bnxt_close_nic(bp, !silent, false);
13944 		bnxt_open_nic(bp, !silent, false);
13945 	}
13946 }
13947 
13948 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13949 {
13950 	struct bnxt *bp = netdev_priv(dev);
13951 
13952 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13953 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13954 }
13955 
13956 static void bnxt_fw_health_check(struct bnxt *bp)
13957 {
13958 	struct bnxt_fw_health *fw_health = bp->fw_health;
13959 	struct pci_dev *pdev = bp->pdev;
13960 	u32 val;
13961 
13962 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13963 		return;
13964 
13965 	/* Make sure it is enabled before checking the tmr_counter. */
13966 	smp_rmb();
13967 	if (fw_health->tmr_counter) {
13968 		fw_health->tmr_counter--;
13969 		return;
13970 	}
13971 
13972 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13973 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13974 		fw_health->arrests++;
13975 		goto fw_reset;
13976 	}
13977 
13978 	fw_health->last_fw_heartbeat = val;
13979 
13980 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13981 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13982 		fw_health->discoveries++;
13983 		goto fw_reset;
13984 	}
13985 
13986 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13987 	return;
13988 
13989 fw_reset:
13990 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13991 }
13992 
13993 static void bnxt_timer(struct timer_list *t)
13994 {
13995 	struct bnxt *bp = from_timer(bp, t, timer);
13996 	struct net_device *dev = bp->dev;
13997 
13998 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13999 		return;
14000 
14001 	if (atomic_read(&bp->intr_sem) != 0)
14002 		goto bnxt_restart_timer;
14003 
14004 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
14005 		bnxt_fw_health_check(bp);
14006 
14007 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
14008 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
14009 
14010 	if (bnxt_tc_flower_enabled(bp))
14011 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
14012 
14013 #ifdef CONFIG_RFS_ACCEL
14014 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
14015 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
14016 #endif /*CONFIG_RFS_ACCEL*/
14017 
14018 	if (bp->link_info.phy_retry) {
14019 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
14020 			bp->link_info.phy_retry = false;
14021 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
14022 		} else {
14023 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
14024 		}
14025 	}
14026 
14027 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
14028 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
14029 
14030 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
14031 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
14032 
14033 bnxt_restart_timer:
14034 	mod_timer(&bp->timer, jiffies + bp->current_interval);
14035 }
14036 
14037 static void bnxt_lock_sp(struct bnxt *bp)
14038 {
14039 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
14040 	 * set.  If the device is being closed, bnxt_close() may be holding
14041 	 * netdev instance lock and waiting for BNXT_STATE_IN_SP_TASK to clear.
14042 	 * So we must clear BNXT_STATE_IN_SP_TASK before holding netdev
14043 	 * instance lock.
14044 	 */
14045 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14046 	netdev_lock(bp->dev);
14047 }
14048 
14049 static void bnxt_unlock_sp(struct bnxt *bp)
14050 {
14051 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14052 	netdev_unlock(bp->dev);
14053 }
14054 
14055 /* Only called from bnxt_sp_task() */
14056 static void bnxt_reset(struct bnxt *bp, bool silent)
14057 {
14058 	bnxt_lock_sp(bp);
14059 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
14060 		bnxt_reset_task(bp, silent);
14061 	bnxt_unlock_sp(bp);
14062 }
14063 
14064 /* Only called from bnxt_sp_task() */
14065 static void bnxt_rx_ring_reset(struct bnxt *bp)
14066 {
14067 	int i;
14068 
14069 	bnxt_lock_sp(bp);
14070 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14071 		bnxt_unlock_sp(bp);
14072 		return;
14073 	}
14074 	/* Disable and flush TPA before resetting the RX ring */
14075 	if (bp->flags & BNXT_FLAG_TPA)
14076 		bnxt_set_tpa(bp, false);
14077 	for (i = 0; i < bp->rx_nr_rings; i++) {
14078 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
14079 		struct bnxt_cp_ring_info *cpr;
14080 		int rc;
14081 
14082 		if (!rxr->bnapi->in_reset)
14083 			continue;
14084 
14085 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
14086 		if (rc) {
14087 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
14088 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
14089 			else
14090 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
14091 					    rc);
14092 			bnxt_reset_task(bp, true);
14093 			break;
14094 		}
14095 		bnxt_free_one_rx_ring_skbs(bp, rxr);
14096 		rxr->rx_prod = 0;
14097 		rxr->rx_agg_prod = 0;
14098 		rxr->rx_sw_agg_prod = 0;
14099 		rxr->rx_next_cons = 0;
14100 		rxr->bnapi->in_reset = false;
14101 		bnxt_alloc_one_rx_ring(bp, i);
14102 		cpr = &rxr->bnapi->cp_ring;
14103 		cpr->sw_stats->rx.rx_resets++;
14104 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
14105 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
14106 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
14107 	}
14108 	if (bp->flags & BNXT_FLAG_TPA)
14109 		bnxt_set_tpa(bp, true);
14110 	bnxt_unlock_sp(bp);
14111 }
14112 
14113 static void bnxt_fw_fatal_close(struct bnxt *bp)
14114 {
14115 	bnxt_tx_disable(bp);
14116 	bnxt_disable_napi(bp);
14117 	bnxt_disable_int_sync(bp);
14118 	bnxt_free_irq(bp);
14119 	bnxt_clear_int_mode(bp);
14120 	pci_disable_device(bp->pdev);
14121 }
14122 
14123 static void bnxt_fw_reset_close(struct bnxt *bp)
14124 {
14125 	/* When firmware is in fatal state, quiesce device and disable
14126 	 * bus master to prevent any potential bad DMAs before freeing
14127 	 * kernel memory.
14128 	 */
14129 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
14130 		u16 val = 0;
14131 
14132 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14133 		if (val == 0xffff)
14134 			bp->fw_reset_min_dsecs = 0;
14135 		bnxt_fw_fatal_close(bp);
14136 	}
14137 	__bnxt_close_nic(bp, true, false);
14138 	bnxt_vf_reps_free(bp);
14139 	bnxt_clear_int_mode(bp);
14140 	bnxt_hwrm_func_drv_unrgtr(bp);
14141 	if (pci_is_enabled(bp->pdev))
14142 		pci_disable_device(bp->pdev);
14143 	bnxt_free_ctx_mem(bp, false);
14144 }
14145 
14146 static bool is_bnxt_fw_ok(struct bnxt *bp)
14147 {
14148 	struct bnxt_fw_health *fw_health = bp->fw_health;
14149 	bool no_heartbeat = false, has_reset = false;
14150 	u32 val;
14151 
14152 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
14153 	if (val == fw_health->last_fw_heartbeat)
14154 		no_heartbeat = true;
14155 
14156 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14157 	if (val != fw_health->last_fw_reset_cnt)
14158 		has_reset = true;
14159 
14160 	if (!no_heartbeat && has_reset)
14161 		return true;
14162 
14163 	return false;
14164 }
14165 
14166 /* netdev instance lock is acquired before calling this function */
14167 static void bnxt_force_fw_reset(struct bnxt *bp)
14168 {
14169 	struct bnxt_fw_health *fw_health = bp->fw_health;
14170 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14171 	u32 wait_dsecs;
14172 
14173 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
14174 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
14175 		return;
14176 
14177 	/* we have to serialize with bnxt_refclk_read()*/
14178 	if (ptp) {
14179 		unsigned long flags;
14180 
14181 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
14182 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14183 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14184 	} else {
14185 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14186 	}
14187 	bnxt_fw_reset_close(bp);
14188 	wait_dsecs = fw_health->master_func_wait_dsecs;
14189 	if (fw_health->primary) {
14190 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
14191 			wait_dsecs = 0;
14192 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14193 	} else {
14194 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
14195 		wait_dsecs = fw_health->normal_func_wait_dsecs;
14196 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14197 	}
14198 
14199 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
14200 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
14201 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14202 }
14203 
14204 void bnxt_fw_exception(struct bnxt *bp)
14205 {
14206 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
14207 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14208 	bnxt_ulp_stop(bp);
14209 	bnxt_lock_sp(bp);
14210 	bnxt_force_fw_reset(bp);
14211 	bnxt_unlock_sp(bp);
14212 }
14213 
14214 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
14215  * < 0 on error.
14216  */
14217 static int bnxt_get_registered_vfs(struct bnxt *bp)
14218 {
14219 #ifdef CONFIG_BNXT_SRIOV
14220 	int rc;
14221 
14222 	if (!BNXT_PF(bp))
14223 		return 0;
14224 
14225 	rc = bnxt_hwrm_func_qcfg(bp);
14226 	if (rc) {
14227 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
14228 		return rc;
14229 	}
14230 	if (bp->pf.registered_vfs)
14231 		return bp->pf.registered_vfs;
14232 	if (bp->sriov_cfg)
14233 		return 1;
14234 #endif
14235 	return 0;
14236 }
14237 
14238 void bnxt_fw_reset(struct bnxt *bp)
14239 {
14240 	bnxt_ulp_stop(bp);
14241 	bnxt_lock_sp(bp);
14242 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
14243 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14244 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
14245 		int n = 0, tmo;
14246 
14247 		/* we have to serialize with bnxt_refclk_read()*/
14248 		if (ptp) {
14249 			unsigned long flags;
14250 
14251 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
14252 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14253 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
14254 		} else {
14255 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14256 		}
14257 		if (bp->pf.active_vfs &&
14258 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
14259 			n = bnxt_get_registered_vfs(bp);
14260 		if (n < 0) {
14261 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
14262 				   n);
14263 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14264 			netif_close(bp->dev);
14265 			goto fw_reset_exit;
14266 		} else if (n > 0) {
14267 			u16 vf_tmo_dsecs = n * 10;
14268 
14269 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
14270 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
14271 			bp->fw_reset_state =
14272 				BNXT_FW_RESET_STATE_POLL_VF;
14273 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14274 			goto fw_reset_exit;
14275 		}
14276 		bnxt_fw_reset_close(bp);
14277 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14278 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14279 			tmo = HZ / 10;
14280 		} else {
14281 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14282 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14283 		}
14284 		bnxt_queue_fw_reset_work(bp, tmo);
14285 	}
14286 fw_reset_exit:
14287 	bnxt_unlock_sp(bp);
14288 }
14289 
14290 static void bnxt_chk_missed_irq(struct bnxt *bp)
14291 {
14292 	int i;
14293 
14294 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14295 		return;
14296 
14297 	for (i = 0; i < bp->cp_nr_rings; i++) {
14298 		struct bnxt_napi *bnapi = bp->bnapi[i];
14299 		struct bnxt_cp_ring_info *cpr;
14300 		u32 fw_ring_id;
14301 		int j;
14302 
14303 		if (!bnapi)
14304 			continue;
14305 
14306 		cpr = &bnapi->cp_ring;
14307 		for (j = 0; j < cpr->cp_ring_count; j++) {
14308 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14309 			u32 val[2];
14310 
14311 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14312 				continue;
14313 
14314 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14315 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14316 				continue;
14317 			}
14318 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14319 			bnxt_dbg_hwrm_ring_info_get(bp,
14320 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14321 				fw_ring_id, &val[0], &val[1]);
14322 			cpr->sw_stats->cmn.missed_irqs++;
14323 		}
14324 	}
14325 }
14326 
14327 static void bnxt_cfg_ntp_filters(struct bnxt *);
14328 
14329 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14330 {
14331 	struct bnxt_link_info *link_info = &bp->link_info;
14332 
14333 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14334 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14335 		if (bp->hwrm_spec_code >= 0x10201) {
14336 			if (link_info->auto_pause_setting &
14337 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14338 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14339 		} else {
14340 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14341 		}
14342 		bnxt_set_auto_speed(link_info);
14343 	} else {
14344 		bnxt_set_force_speed(link_info);
14345 		link_info->req_duplex = link_info->duplex_setting;
14346 	}
14347 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14348 		link_info->req_flow_ctrl =
14349 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14350 	else
14351 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14352 }
14353 
14354 static void bnxt_fw_echo_reply(struct bnxt *bp)
14355 {
14356 	struct bnxt_fw_health *fw_health = bp->fw_health;
14357 	struct hwrm_func_echo_response_input *req;
14358 	int rc;
14359 
14360 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14361 	if (rc)
14362 		return;
14363 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14364 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14365 	hwrm_req_send(bp, req);
14366 }
14367 
14368 static void bnxt_ulp_restart(struct bnxt *bp)
14369 {
14370 	bnxt_ulp_stop(bp);
14371 	bnxt_ulp_start(bp, 0);
14372 }
14373 
14374 static void bnxt_sp_task(struct work_struct *work)
14375 {
14376 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14377 
14378 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14379 	smp_mb__after_atomic();
14380 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14381 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14382 		return;
14383 	}
14384 
14385 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14386 		bnxt_ulp_restart(bp);
14387 		bnxt_reenable_sriov(bp);
14388 	}
14389 
14390 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14391 		bnxt_cfg_rx_mode(bp);
14392 
14393 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14394 		bnxt_cfg_ntp_filters(bp);
14395 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14396 		bnxt_hwrm_exec_fwd_req(bp);
14397 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14398 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14399 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14400 		bnxt_hwrm_port_qstats(bp, 0);
14401 		bnxt_hwrm_port_qstats_ext(bp, 0);
14402 		bnxt_accumulate_all_stats(bp);
14403 	}
14404 
14405 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14406 		int rc;
14407 
14408 		mutex_lock(&bp->link_lock);
14409 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14410 				       &bp->sp_event))
14411 			bnxt_hwrm_phy_qcaps(bp);
14412 
14413 		rc = bnxt_update_link(bp, true);
14414 		if (rc)
14415 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14416 				   rc);
14417 
14418 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14419 				       &bp->sp_event))
14420 			bnxt_init_ethtool_link_settings(bp);
14421 		mutex_unlock(&bp->link_lock);
14422 	}
14423 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14424 		int rc;
14425 
14426 		mutex_lock(&bp->link_lock);
14427 		rc = bnxt_update_phy_setting(bp);
14428 		mutex_unlock(&bp->link_lock);
14429 		if (rc) {
14430 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14431 		} else {
14432 			bp->link_info.phy_retry = false;
14433 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14434 		}
14435 	}
14436 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14437 		mutex_lock(&bp->link_lock);
14438 		bnxt_get_port_module_status(bp);
14439 		mutex_unlock(&bp->link_lock);
14440 	}
14441 
14442 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14443 		bnxt_tc_flow_stats_work(bp);
14444 
14445 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14446 		bnxt_chk_missed_irq(bp);
14447 
14448 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14449 		bnxt_fw_echo_reply(bp);
14450 
14451 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14452 		bnxt_hwmon_notify_event(bp);
14453 
14454 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14455 	 * must be the last functions to be called before exiting.
14456 	 */
14457 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14458 		bnxt_reset(bp, false);
14459 
14460 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14461 		bnxt_reset(bp, true);
14462 
14463 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14464 		bnxt_rx_ring_reset(bp);
14465 
14466 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14467 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14468 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14469 			bnxt_devlink_health_fw_report(bp);
14470 		else
14471 			bnxt_fw_reset(bp);
14472 	}
14473 
14474 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14475 		if (!is_bnxt_fw_ok(bp))
14476 			bnxt_devlink_health_fw_report(bp);
14477 	}
14478 
14479 	smp_mb__before_atomic();
14480 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14481 }
14482 
14483 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14484 				int *max_cp);
14485 
14486 /* Under netdev instance lock */
14487 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14488 		     int tx_xdp)
14489 {
14490 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14491 	struct bnxt_hw_rings hwr = {0};
14492 	int rx_rings = rx;
14493 	int rc;
14494 
14495 	if (tcs)
14496 		tx_sets = tcs;
14497 
14498 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14499 
14500 	if (max_rx < rx_rings)
14501 		return -ENOMEM;
14502 
14503 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14504 		rx_rings <<= 1;
14505 
14506 	hwr.rx = rx_rings;
14507 	hwr.tx = tx * tx_sets + tx_xdp;
14508 	if (max_tx < hwr.tx)
14509 		return -ENOMEM;
14510 
14511 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14512 
14513 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14514 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14515 	if (max_cp < hwr.cp)
14516 		return -ENOMEM;
14517 	hwr.stat = hwr.cp;
14518 	if (BNXT_NEW_RM(bp)) {
14519 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14520 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14521 		hwr.grp = rx;
14522 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14523 	}
14524 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14525 		hwr.cp_p5 = hwr.tx + rx;
14526 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14527 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14528 		if (!bnxt_ulp_registered(bp->edev)) {
14529 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14530 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14531 		}
14532 		if (hwr.cp > bp->total_irqs) {
14533 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14534 
14535 			if (total_msix < hwr.cp) {
14536 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14537 					    hwr.cp, total_msix);
14538 				rc = -ENOSPC;
14539 			}
14540 		}
14541 	}
14542 	return rc;
14543 }
14544 
14545 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14546 {
14547 	if (bp->bar2) {
14548 		pci_iounmap(pdev, bp->bar2);
14549 		bp->bar2 = NULL;
14550 	}
14551 
14552 	if (bp->bar1) {
14553 		pci_iounmap(pdev, bp->bar1);
14554 		bp->bar1 = NULL;
14555 	}
14556 
14557 	if (bp->bar0) {
14558 		pci_iounmap(pdev, bp->bar0);
14559 		bp->bar0 = NULL;
14560 	}
14561 }
14562 
14563 static void bnxt_cleanup_pci(struct bnxt *bp)
14564 {
14565 	bnxt_unmap_bars(bp, bp->pdev);
14566 	pci_release_regions(bp->pdev);
14567 	if (pci_is_enabled(bp->pdev))
14568 		pci_disable_device(bp->pdev);
14569 }
14570 
14571 static void bnxt_init_dflt_coal(struct bnxt *bp)
14572 {
14573 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14574 	struct bnxt_coal *coal;
14575 	u16 flags = 0;
14576 
14577 	if (coal_cap->cmpl_params &
14578 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14579 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14580 
14581 	/* Tick values in micro seconds.
14582 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14583 	 */
14584 	coal = &bp->rx_coal;
14585 	coal->coal_ticks = 10;
14586 	coal->coal_bufs = 30;
14587 	coal->coal_ticks_irq = 1;
14588 	coal->coal_bufs_irq = 2;
14589 	coal->idle_thresh = 50;
14590 	coal->bufs_per_record = 2;
14591 	coal->budget = 64;		/* NAPI budget */
14592 	coal->flags = flags;
14593 
14594 	coal = &bp->tx_coal;
14595 	coal->coal_ticks = 28;
14596 	coal->coal_bufs = 30;
14597 	coal->coal_ticks_irq = 2;
14598 	coal->coal_bufs_irq = 2;
14599 	coal->bufs_per_record = 1;
14600 	coal->flags = flags;
14601 
14602 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14603 }
14604 
14605 /* FW that pre-reserves 1 VNIC per function */
14606 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14607 {
14608 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14609 
14610 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14611 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14612 		return true;
14613 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14614 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14615 		return true;
14616 	return false;
14617 }
14618 
14619 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14620 {
14621 	int rc;
14622 
14623 	bp->fw_cap = 0;
14624 	rc = bnxt_hwrm_ver_get(bp);
14625 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14626 	 * so wait before continuing with recovery.
14627 	 */
14628 	if (rc)
14629 		msleep(100);
14630 	bnxt_try_map_fw_health_reg(bp);
14631 	if (rc) {
14632 		rc = bnxt_try_recover_fw(bp);
14633 		if (rc)
14634 			return rc;
14635 		rc = bnxt_hwrm_ver_get(bp);
14636 		if (rc)
14637 			return rc;
14638 	}
14639 
14640 	bnxt_nvm_cfg_ver_get(bp);
14641 
14642 	rc = bnxt_hwrm_func_reset(bp);
14643 	if (rc)
14644 		return -ENODEV;
14645 
14646 	bnxt_hwrm_fw_set_time(bp);
14647 	return 0;
14648 }
14649 
14650 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14651 {
14652 	int rc;
14653 
14654 	/* Get the MAX capabilities for this function */
14655 	rc = bnxt_hwrm_func_qcaps(bp);
14656 	if (rc) {
14657 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14658 			   rc);
14659 		return -ENODEV;
14660 	}
14661 
14662 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14663 	if (rc)
14664 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14665 			    rc);
14666 
14667 	if (bnxt_alloc_fw_health(bp)) {
14668 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14669 	} else {
14670 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14671 		if (rc)
14672 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14673 				    rc);
14674 	}
14675 
14676 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14677 	if (rc)
14678 		return -ENODEV;
14679 
14680 	rc = bnxt_alloc_crash_dump_mem(bp);
14681 	if (rc)
14682 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14683 			    rc);
14684 	if (!rc) {
14685 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14686 		if (rc) {
14687 			bnxt_free_crash_dump_mem(bp);
14688 			netdev_warn(bp->dev,
14689 				    "hwrm crash dump mem failure rc: %d\n", rc);
14690 		}
14691 	}
14692 
14693 	if (bnxt_fw_pre_resv_vnics(bp))
14694 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14695 
14696 	bnxt_hwrm_func_qcfg(bp);
14697 	bnxt_hwrm_vnic_qcaps(bp);
14698 	bnxt_hwrm_port_led_qcaps(bp);
14699 	bnxt_ethtool_init(bp);
14700 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14701 		__bnxt_hwrm_ptp_qcfg(bp);
14702 	bnxt_dcb_init(bp);
14703 	bnxt_hwmon_init(bp);
14704 	return 0;
14705 }
14706 
14707 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14708 {
14709 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14710 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14711 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14712 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14713 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14714 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14715 		bp->rss_hash_delta = bp->rss_hash_cfg;
14716 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14717 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14718 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14719 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14720 	}
14721 }
14722 
14723 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14724 {
14725 	struct net_device *dev = bp->dev;
14726 
14727 	dev->hw_features &= ~NETIF_F_NTUPLE;
14728 	dev->features &= ~NETIF_F_NTUPLE;
14729 	bp->flags &= ~BNXT_FLAG_RFS;
14730 	if (bnxt_rfs_supported(bp)) {
14731 		dev->hw_features |= NETIF_F_NTUPLE;
14732 		if (bnxt_rfs_capable(bp, false)) {
14733 			bp->flags |= BNXT_FLAG_RFS;
14734 			dev->features |= NETIF_F_NTUPLE;
14735 		}
14736 	}
14737 }
14738 
14739 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14740 {
14741 	struct pci_dev *pdev = bp->pdev;
14742 
14743 	bnxt_set_dflt_rss_hash_type(bp);
14744 	bnxt_set_dflt_rfs(bp);
14745 
14746 	bnxt_get_wol_settings(bp);
14747 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14748 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14749 	else
14750 		device_set_wakeup_capable(&pdev->dev, false);
14751 
14752 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14753 	bnxt_hwrm_coal_params_qcaps(bp);
14754 }
14755 
14756 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14757 
14758 int bnxt_fw_init_one(struct bnxt *bp)
14759 {
14760 	int rc;
14761 
14762 	rc = bnxt_fw_init_one_p1(bp);
14763 	if (rc) {
14764 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14765 		return rc;
14766 	}
14767 	rc = bnxt_fw_init_one_p2(bp);
14768 	if (rc) {
14769 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14770 		return rc;
14771 	}
14772 	rc = bnxt_probe_phy(bp, false);
14773 	if (rc)
14774 		return rc;
14775 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14776 	if (rc)
14777 		return rc;
14778 
14779 	bnxt_fw_init_one_p3(bp);
14780 	return 0;
14781 }
14782 
14783 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14784 {
14785 	struct bnxt_fw_health *fw_health = bp->fw_health;
14786 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14787 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14788 	u32 reg_type, reg_off, delay_msecs;
14789 
14790 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14791 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14792 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14793 	switch (reg_type) {
14794 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14795 		pci_write_config_dword(bp->pdev, reg_off, val);
14796 		break;
14797 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14798 		writel(reg_off & BNXT_GRC_BASE_MASK,
14799 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14800 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14801 		fallthrough;
14802 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14803 		writel(val, bp->bar0 + reg_off);
14804 		break;
14805 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14806 		writel(val, bp->bar1 + reg_off);
14807 		break;
14808 	}
14809 	if (delay_msecs) {
14810 		pci_read_config_dword(bp->pdev, 0, &val);
14811 		msleep(delay_msecs);
14812 	}
14813 }
14814 
14815 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14816 {
14817 	struct hwrm_func_qcfg_output *resp;
14818 	struct hwrm_func_qcfg_input *req;
14819 	bool result = true; /* firmware will enforce if unknown */
14820 
14821 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14822 		return result;
14823 
14824 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14825 		return result;
14826 
14827 	req->fid = cpu_to_le16(0xffff);
14828 	resp = hwrm_req_hold(bp, req);
14829 	if (!hwrm_req_send(bp, req))
14830 		result = !!(le16_to_cpu(resp->flags) &
14831 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14832 	hwrm_req_drop(bp, req);
14833 	return result;
14834 }
14835 
14836 static void bnxt_reset_all(struct bnxt *bp)
14837 {
14838 	struct bnxt_fw_health *fw_health = bp->fw_health;
14839 	int i, rc;
14840 
14841 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14842 		bnxt_fw_reset_via_optee(bp);
14843 		bp->fw_reset_timestamp = jiffies;
14844 		return;
14845 	}
14846 
14847 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14848 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14849 			bnxt_fw_reset_writel(bp, i);
14850 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14851 		struct hwrm_fw_reset_input *req;
14852 
14853 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14854 		if (!rc) {
14855 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14856 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14857 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14858 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14859 			rc = hwrm_req_send(bp, req);
14860 		}
14861 		if (rc != -ENODEV)
14862 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14863 	}
14864 	bp->fw_reset_timestamp = jiffies;
14865 }
14866 
14867 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14868 {
14869 	return time_after(jiffies, bp->fw_reset_timestamp +
14870 			  (bp->fw_reset_max_dsecs * HZ / 10));
14871 }
14872 
14873 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14874 {
14875 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14876 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14877 		bnxt_dl_health_fw_status_update(bp, false);
14878 	bp->fw_reset_state = 0;
14879 	netif_close(bp->dev);
14880 }
14881 
14882 static void bnxt_fw_reset_task(struct work_struct *work)
14883 {
14884 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14885 	int rc = 0;
14886 
14887 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14888 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14889 		return;
14890 	}
14891 
14892 	switch (bp->fw_reset_state) {
14893 	case BNXT_FW_RESET_STATE_POLL_VF: {
14894 		int n = bnxt_get_registered_vfs(bp);
14895 		int tmo;
14896 
14897 		if (n < 0) {
14898 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14899 				   n, jiffies_to_msecs(jiffies -
14900 				   bp->fw_reset_timestamp));
14901 			goto fw_reset_abort;
14902 		} else if (n > 0) {
14903 			if (bnxt_fw_reset_timeout(bp)) {
14904 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14905 				bp->fw_reset_state = 0;
14906 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14907 					   n);
14908 				goto ulp_start;
14909 			}
14910 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14911 			return;
14912 		}
14913 		bp->fw_reset_timestamp = jiffies;
14914 		netdev_lock(bp->dev);
14915 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14916 			bnxt_fw_reset_abort(bp, rc);
14917 			netdev_unlock(bp->dev);
14918 			goto ulp_start;
14919 		}
14920 		bnxt_fw_reset_close(bp);
14921 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14922 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14923 			tmo = HZ / 10;
14924 		} else {
14925 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14926 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14927 		}
14928 		netdev_unlock(bp->dev);
14929 		bnxt_queue_fw_reset_work(bp, tmo);
14930 		return;
14931 	}
14932 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14933 		u32 val;
14934 
14935 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14936 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14937 		    !bnxt_fw_reset_timeout(bp)) {
14938 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14939 			return;
14940 		}
14941 
14942 		if (!bp->fw_health->primary) {
14943 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14944 
14945 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14946 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14947 			return;
14948 		}
14949 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14950 	}
14951 		fallthrough;
14952 	case BNXT_FW_RESET_STATE_RESET_FW:
14953 		bnxt_reset_all(bp);
14954 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14955 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14956 		return;
14957 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14958 		bnxt_inv_fw_health_reg(bp);
14959 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14960 		    !bp->fw_reset_min_dsecs) {
14961 			u16 val;
14962 
14963 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14964 			if (val == 0xffff) {
14965 				if (bnxt_fw_reset_timeout(bp)) {
14966 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14967 					rc = -ETIMEDOUT;
14968 					goto fw_reset_abort;
14969 				}
14970 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14971 				return;
14972 			}
14973 		}
14974 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14975 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14976 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14977 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14978 			bnxt_dl_remote_reload(bp);
14979 		if (pci_enable_device(bp->pdev)) {
14980 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14981 			rc = -ENODEV;
14982 			goto fw_reset_abort;
14983 		}
14984 		pci_set_master(bp->pdev);
14985 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14986 		fallthrough;
14987 	case BNXT_FW_RESET_STATE_POLL_FW:
14988 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14989 		rc = bnxt_hwrm_poll(bp);
14990 		if (rc) {
14991 			if (bnxt_fw_reset_timeout(bp)) {
14992 				netdev_err(bp->dev, "Firmware reset aborted\n");
14993 				goto fw_reset_abort_status;
14994 			}
14995 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14996 			return;
14997 		}
14998 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14999 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
15000 		fallthrough;
15001 	case BNXT_FW_RESET_STATE_OPENING:
15002 		while (!netdev_trylock(bp->dev)) {
15003 			bnxt_queue_fw_reset_work(bp, HZ / 10);
15004 			return;
15005 		}
15006 		rc = bnxt_open(bp->dev);
15007 		if (rc) {
15008 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
15009 			bnxt_fw_reset_abort(bp, rc);
15010 			netdev_unlock(bp->dev);
15011 			goto ulp_start;
15012 		}
15013 
15014 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
15015 		    bp->fw_health->enabled) {
15016 			bp->fw_health->last_fw_reset_cnt =
15017 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
15018 		}
15019 		bp->fw_reset_state = 0;
15020 		/* Make sure fw_reset_state is 0 before clearing the flag */
15021 		smp_mb__before_atomic();
15022 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15023 		bnxt_ptp_reapply_pps(bp);
15024 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
15025 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
15026 			bnxt_dl_health_fw_recovery_done(bp);
15027 			bnxt_dl_health_fw_status_update(bp, true);
15028 		}
15029 		netdev_unlock(bp->dev);
15030 		bnxt_ulp_start(bp, 0);
15031 		bnxt_reenable_sriov(bp);
15032 		netdev_lock(bp->dev);
15033 		bnxt_vf_reps_alloc(bp);
15034 		bnxt_vf_reps_open(bp);
15035 		netdev_unlock(bp->dev);
15036 		break;
15037 	}
15038 	return;
15039 
15040 fw_reset_abort_status:
15041 	if (bp->fw_health->status_reliable ||
15042 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
15043 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
15044 
15045 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
15046 	}
15047 fw_reset_abort:
15048 	netdev_lock(bp->dev);
15049 	bnxt_fw_reset_abort(bp, rc);
15050 	netdev_unlock(bp->dev);
15051 ulp_start:
15052 	bnxt_ulp_start(bp, rc);
15053 }
15054 
15055 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
15056 {
15057 	int rc;
15058 	struct bnxt *bp = netdev_priv(dev);
15059 
15060 	SET_NETDEV_DEV(dev, &pdev->dev);
15061 
15062 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
15063 	rc = pci_enable_device(pdev);
15064 	if (rc) {
15065 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
15066 		goto init_err;
15067 	}
15068 
15069 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
15070 		dev_err(&pdev->dev,
15071 			"Cannot find PCI device base address, aborting\n");
15072 		rc = -ENODEV;
15073 		goto init_err_disable;
15074 	}
15075 
15076 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
15077 	if (rc) {
15078 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
15079 		goto init_err_disable;
15080 	}
15081 
15082 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
15083 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
15084 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
15085 		rc = -EIO;
15086 		goto init_err_release;
15087 	}
15088 
15089 	pci_set_master(pdev);
15090 
15091 	bp->dev = dev;
15092 	bp->pdev = pdev;
15093 
15094 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
15095 	 * determines the BAR size.
15096 	 */
15097 	bp->bar0 = pci_ioremap_bar(pdev, 0);
15098 	if (!bp->bar0) {
15099 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
15100 		rc = -ENOMEM;
15101 		goto init_err_release;
15102 	}
15103 
15104 	bp->bar2 = pci_ioremap_bar(pdev, 4);
15105 	if (!bp->bar2) {
15106 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
15107 		rc = -ENOMEM;
15108 		goto init_err_release;
15109 	}
15110 
15111 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
15112 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
15113 
15114 	spin_lock_init(&bp->ntp_fltr_lock);
15115 #if BITS_PER_LONG == 32
15116 	spin_lock_init(&bp->db_lock);
15117 #endif
15118 
15119 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
15120 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
15121 
15122 	timer_setup(&bp->timer, bnxt_timer, 0);
15123 	bp->current_interval = BNXT_TIMER_INTERVAL;
15124 
15125 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
15126 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
15127 
15128 	clear_bit(BNXT_STATE_OPEN, &bp->state);
15129 	return 0;
15130 
15131 init_err_release:
15132 	bnxt_unmap_bars(bp, pdev);
15133 	pci_release_regions(pdev);
15134 
15135 init_err_disable:
15136 	pci_disable_device(pdev);
15137 
15138 init_err:
15139 	return rc;
15140 }
15141 
15142 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
15143 {
15144 	struct sockaddr *addr = p;
15145 	struct bnxt *bp = netdev_priv(dev);
15146 	int rc = 0;
15147 
15148 	netdev_assert_locked(dev);
15149 
15150 	if (!is_valid_ether_addr(addr->sa_data))
15151 		return -EADDRNOTAVAIL;
15152 
15153 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
15154 		return 0;
15155 
15156 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
15157 	if (rc)
15158 		return rc;
15159 
15160 	eth_hw_addr_set(dev, addr->sa_data);
15161 	bnxt_clear_usr_fltrs(bp, true);
15162 	if (netif_running(dev)) {
15163 		bnxt_close_nic(bp, false, false);
15164 		rc = bnxt_open_nic(bp, false, false);
15165 	}
15166 
15167 	return rc;
15168 }
15169 
15170 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
15171 {
15172 	struct bnxt *bp = netdev_priv(dev);
15173 
15174 	netdev_assert_locked(dev);
15175 
15176 	if (netif_running(dev))
15177 		bnxt_close_nic(bp, true, false);
15178 
15179 	WRITE_ONCE(dev->mtu, new_mtu);
15180 
15181 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
15182 	 * program is attached.  We need to set the AGG rings settings and
15183 	 * rx_skb_func accordingly.
15184 	 */
15185 	if (READ_ONCE(bp->xdp_prog))
15186 		bnxt_set_rx_skb_mode(bp, true);
15187 
15188 	bnxt_set_ring_params(bp);
15189 
15190 	if (netif_running(dev))
15191 		return bnxt_open_nic(bp, true, false);
15192 
15193 	return 0;
15194 }
15195 
15196 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
15197 {
15198 	struct bnxt *bp = netdev_priv(dev);
15199 	bool sh = false;
15200 	int rc, tx_cp;
15201 
15202 	if (tc > bp->max_tc) {
15203 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
15204 			   tc, bp->max_tc);
15205 		return -EINVAL;
15206 	}
15207 
15208 	if (bp->num_tc == tc)
15209 		return 0;
15210 
15211 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
15212 		sh = true;
15213 
15214 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
15215 			      sh, tc, bp->tx_nr_rings_xdp);
15216 	if (rc)
15217 		return rc;
15218 
15219 	/* Needs to close the device and do hw resource re-allocations */
15220 	if (netif_running(bp->dev))
15221 		bnxt_close_nic(bp, true, false);
15222 
15223 	if (tc) {
15224 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
15225 		netdev_set_num_tc(dev, tc);
15226 		bp->num_tc = tc;
15227 	} else {
15228 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15229 		netdev_reset_tc(dev);
15230 		bp->num_tc = 0;
15231 	}
15232 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
15233 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
15234 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
15235 			       tx_cp + bp->rx_nr_rings;
15236 
15237 	if (netif_running(bp->dev))
15238 		return bnxt_open_nic(bp, true, false);
15239 
15240 	return 0;
15241 }
15242 
15243 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
15244 				  void *cb_priv)
15245 {
15246 	struct bnxt *bp = cb_priv;
15247 
15248 	if (!bnxt_tc_flower_enabled(bp) ||
15249 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
15250 		return -EOPNOTSUPP;
15251 
15252 	switch (type) {
15253 	case TC_SETUP_CLSFLOWER:
15254 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
15255 	default:
15256 		return -EOPNOTSUPP;
15257 	}
15258 }
15259 
15260 LIST_HEAD(bnxt_block_cb_list);
15261 
15262 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
15263 			 void *type_data)
15264 {
15265 	struct bnxt *bp = netdev_priv(dev);
15266 
15267 	switch (type) {
15268 	case TC_SETUP_BLOCK:
15269 		return flow_block_cb_setup_simple(type_data,
15270 						  &bnxt_block_cb_list,
15271 						  bnxt_setup_tc_block_cb,
15272 						  bp, bp, true);
15273 	case TC_SETUP_QDISC_MQPRIO: {
15274 		struct tc_mqprio_qopt *mqprio = type_data;
15275 
15276 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
15277 
15278 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
15279 	}
15280 	default:
15281 		return -EOPNOTSUPP;
15282 	}
15283 }
15284 
15285 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
15286 			    const struct sk_buff *skb)
15287 {
15288 	struct bnxt_vnic_info *vnic;
15289 
15290 	if (skb)
15291 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15292 
15293 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15294 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15295 }
15296 
15297 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15298 			   u32 idx)
15299 {
15300 	struct hlist_head *head;
15301 	int bit_id;
15302 
15303 	spin_lock_bh(&bp->ntp_fltr_lock);
15304 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15305 	if (bit_id < 0) {
15306 		spin_unlock_bh(&bp->ntp_fltr_lock);
15307 		return -ENOMEM;
15308 	}
15309 
15310 	fltr->base.sw_id = (u16)bit_id;
15311 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15312 	fltr->base.flags |= BNXT_ACT_RING_DST;
15313 	head = &bp->ntp_fltr_hash_tbl[idx];
15314 	hlist_add_head_rcu(&fltr->base.hash, head);
15315 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15316 	bnxt_insert_usr_fltr(bp, &fltr->base);
15317 	bp->ntp_fltr_count++;
15318 	spin_unlock_bh(&bp->ntp_fltr_lock);
15319 	return 0;
15320 }
15321 
15322 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15323 			    struct bnxt_ntuple_filter *f2)
15324 {
15325 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15326 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15327 	struct flow_keys *keys1 = &f1->fkeys;
15328 	struct flow_keys *keys2 = &f2->fkeys;
15329 
15330 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15331 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15332 		return false;
15333 
15334 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15335 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15336 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15337 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15338 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15339 			return false;
15340 	} else {
15341 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15342 				     &keys2->addrs.v6addrs.src) ||
15343 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15344 				     &masks2->addrs.v6addrs.src) ||
15345 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15346 				     &keys2->addrs.v6addrs.dst) ||
15347 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15348 				     &masks2->addrs.v6addrs.dst))
15349 			return false;
15350 	}
15351 
15352 	return keys1->ports.src == keys2->ports.src &&
15353 	       masks1->ports.src == masks2->ports.src &&
15354 	       keys1->ports.dst == keys2->ports.dst &&
15355 	       masks1->ports.dst == masks2->ports.dst &&
15356 	       keys1->control.flags == keys2->control.flags &&
15357 	       f1->l2_fltr == f2->l2_fltr;
15358 }
15359 
15360 struct bnxt_ntuple_filter *
15361 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15362 				struct bnxt_ntuple_filter *fltr, u32 idx)
15363 {
15364 	struct bnxt_ntuple_filter *f;
15365 	struct hlist_head *head;
15366 
15367 	head = &bp->ntp_fltr_hash_tbl[idx];
15368 	hlist_for_each_entry_rcu(f, head, base.hash) {
15369 		if (bnxt_fltr_match(f, fltr))
15370 			return f;
15371 	}
15372 	return NULL;
15373 }
15374 
15375 #ifdef CONFIG_RFS_ACCEL
15376 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15377 			      u16 rxq_index, u32 flow_id)
15378 {
15379 	struct bnxt *bp = netdev_priv(dev);
15380 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15381 	struct flow_keys *fkeys;
15382 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15383 	struct bnxt_l2_filter *l2_fltr;
15384 	int rc = 0, idx;
15385 	u32 flags;
15386 
15387 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15388 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15389 		atomic_inc(&l2_fltr->refcnt);
15390 	} else {
15391 		struct bnxt_l2_key key;
15392 
15393 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15394 		key.vlan = 0;
15395 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15396 		if (!l2_fltr)
15397 			return -EINVAL;
15398 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15399 			bnxt_del_l2_filter(bp, l2_fltr);
15400 			return -EINVAL;
15401 		}
15402 	}
15403 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15404 	if (!new_fltr) {
15405 		bnxt_del_l2_filter(bp, l2_fltr);
15406 		return -ENOMEM;
15407 	}
15408 
15409 	fkeys = &new_fltr->fkeys;
15410 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15411 		rc = -EPROTONOSUPPORT;
15412 		goto err_free;
15413 	}
15414 
15415 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15416 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15417 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15418 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15419 		rc = -EPROTONOSUPPORT;
15420 		goto err_free;
15421 	}
15422 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15423 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15424 		if (bp->hwrm_spec_code < 0x10601) {
15425 			rc = -EPROTONOSUPPORT;
15426 			goto err_free;
15427 		}
15428 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15429 	}
15430 	flags = fkeys->control.flags;
15431 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15432 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15433 		rc = -EPROTONOSUPPORT;
15434 		goto err_free;
15435 	}
15436 	new_fltr->l2_fltr = l2_fltr;
15437 
15438 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15439 	rcu_read_lock();
15440 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15441 	if (fltr) {
15442 		rc = fltr->base.sw_id;
15443 		rcu_read_unlock();
15444 		goto err_free;
15445 	}
15446 	rcu_read_unlock();
15447 
15448 	new_fltr->flow_id = flow_id;
15449 	new_fltr->base.rxq = rxq_index;
15450 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15451 	if (!rc) {
15452 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15453 		return new_fltr->base.sw_id;
15454 	}
15455 
15456 err_free:
15457 	bnxt_del_l2_filter(bp, l2_fltr);
15458 	kfree(new_fltr);
15459 	return rc;
15460 }
15461 #endif
15462 
15463 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15464 {
15465 	spin_lock_bh(&bp->ntp_fltr_lock);
15466 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15467 		spin_unlock_bh(&bp->ntp_fltr_lock);
15468 		return;
15469 	}
15470 	hlist_del_rcu(&fltr->base.hash);
15471 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15472 	bp->ntp_fltr_count--;
15473 	spin_unlock_bh(&bp->ntp_fltr_lock);
15474 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15475 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15476 	kfree_rcu(fltr, base.rcu);
15477 }
15478 
15479 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15480 {
15481 #ifdef CONFIG_RFS_ACCEL
15482 	int i;
15483 
15484 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15485 		struct hlist_head *head;
15486 		struct hlist_node *tmp;
15487 		struct bnxt_ntuple_filter *fltr;
15488 		int rc;
15489 
15490 		head = &bp->ntp_fltr_hash_tbl[i];
15491 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15492 			bool del = false;
15493 
15494 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15495 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15496 					continue;
15497 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15498 							fltr->flow_id,
15499 							fltr->base.sw_id)) {
15500 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15501 									 fltr);
15502 					del = true;
15503 				}
15504 			} else {
15505 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15506 								       fltr);
15507 				if (rc)
15508 					del = true;
15509 				else
15510 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15511 			}
15512 
15513 			if (del)
15514 				bnxt_del_ntp_filter(bp, fltr);
15515 		}
15516 	}
15517 #endif
15518 }
15519 
15520 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15521 				    unsigned int entry, struct udp_tunnel_info *ti)
15522 {
15523 	struct bnxt *bp = netdev_priv(netdev);
15524 	unsigned int cmd;
15525 
15526 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15527 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15528 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15529 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15530 	else
15531 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15532 
15533 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15534 }
15535 
15536 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15537 				      unsigned int entry, struct udp_tunnel_info *ti)
15538 {
15539 	struct bnxt *bp = netdev_priv(netdev);
15540 	unsigned int cmd;
15541 
15542 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15543 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15544 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15545 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15546 	else
15547 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15548 
15549 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15550 }
15551 
15552 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15553 	.set_port	= bnxt_udp_tunnel_set_port,
15554 	.unset_port	= bnxt_udp_tunnel_unset_port,
15555 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15556 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15557 	.tables		= {
15558 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15559 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15560 	},
15561 }, bnxt_udp_tunnels_p7 = {
15562 	.set_port	= bnxt_udp_tunnel_set_port,
15563 	.unset_port	= bnxt_udp_tunnel_unset_port,
15564 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15565 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15566 	.tables		= {
15567 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15568 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15569 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15570 	},
15571 };
15572 
15573 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15574 			       struct net_device *dev, u32 filter_mask,
15575 			       int nlflags)
15576 {
15577 	struct bnxt *bp = netdev_priv(dev);
15578 
15579 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15580 				       nlflags, filter_mask, NULL);
15581 }
15582 
15583 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15584 			       u16 flags, struct netlink_ext_ack *extack)
15585 {
15586 	struct bnxt *bp = netdev_priv(dev);
15587 	struct nlattr *attr, *br_spec;
15588 	int rem, rc = 0;
15589 
15590 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15591 		return -EOPNOTSUPP;
15592 
15593 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15594 	if (!br_spec)
15595 		return -EINVAL;
15596 
15597 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15598 		u16 mode;
15599 
15600 		mode = nla_get_u16(attr);
15601 		if (mode == bp->br_mode)
15602 			break;
15603 
15604 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15605 		if (!rc)
15606 			bp->br_mode = mode;
15607 		break;
15608 	}
15609 	return rc;
15610 }
15611 
15612 int bnxt_get_port_parent_id(struct net_device *dev,
15613 			    struct netdev_phys_item_id *ppid)
15614 {
15615 	struct bnxt *bp = netdev_priv(dev);
15616 
15617 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15618 		return -EOPNOTSUPP;
15619 
15620 	/* The PF and it's VF-reps only support the switchdev framework */
15621 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15622 		return -EOPNOTSUPP;
15623 
15624 	ppid->id_len = sizeof(bp->dsn);
15625 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15626 
15627 	return 0;
15628 }
15629 
15630 static const struct net_device_ops bnxt_netdev_ops = {
15631 	.ndo_open		= bnxt_open,
15632 	.ndo_start_xmit		= bnxt_start_xmit,
15633 	.ndo_stop		= bnxt_close,
15634 	.ndo_get_stats64	= bnxt_get_stats64,
15635 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15636 	.ndo_eth_ioctl		= bnxt_ioctl,
15637 	.ndo_validate_addr	= eth_validate_addr,
15638 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15639 	.ndo_change_mtu		= bnxt_change_mtu,
15640 	.ndo_fix_features	= bnxt_fix_features,
15641 	.ndo_set_features	= bnxt_set_features,
15642 	.ndo_features_check	= bnxt_features_check,
15643 	.ndo_tx_timeout		= bnxt_tx_timeout,
15644 #ifdef CONFIG_BNXT_SRIOV
15645 	.ndo_get_vf_config	= bnxt_get_vf_config,
15646 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15647 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15648 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15649 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15650 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15651 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15652 #endif
15653 	.ndo_setup_tc           = bnxt_setup_tc,
15654 #ifdef CONFIG_RFS_ACCEL
15655 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15656 #endif
15657 	.ndo_bpf		= bnxt_xdp,
15658 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15659 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15660 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15661 };
15662 
15663 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15664 				    struct netdev_queue_stats_rx *stats)
15665 {
15666 	struct bnxt *bp = netdev_priv(dev);
15667 	struct bnxt_cp_ring_info *cpr;
15668 	u64 *sw;
15669 
15670 	if (!bp->bnapi)
15671 		return;
15672 
15673 	cpr = &bp->bnapi[i]->cp_ring;
15674 	sw = cpr->stats.sw_stats;
15675 
15676 	stats->packets = 0;
15677 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15678 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15679 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15680 
15681 	stats->bytes = 0;
15682 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15683 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15684 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15685 
15686 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15687 }
15688 
15689 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15690 				    struct netdev_queue_stats_tx *stats)
15691 {
15692 	struct bnxt *bp = netdev_priv(dev);
15693 	struct bnxt_napi *bnapi;
15694 	u64 *sw;
15695 
15696 	if (!bp->tx_ring)
15697 		return;
15698 
15699 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15700 	sw = bnapi->cp_ring.stats.sw_stats;
15701 
15702 	stats->packets = 0;
15703 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15704 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15705 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15706 
15707 	stats->bytes = 0;
15708 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15709 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15710 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15711 }
15712 
15713 static void bnxt_get_base_stats(struct net_device *dev,
15714 				struct netdev_queue_stats_rx *rx,
15715 				struct netdev_queue_stats_tx *tx)
15716 {
15717 	struct bnxt *bp = netdev_priv(dev);
15718 
15719 	rx->packets = bp->net_stats_prev.rx_packets;
15720 	rx->bytes = bp->net_stats_prev.rx_bytes;
15721 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15722 
15723 	tx->packets = bp->net_stats_prev.tx_packets;
15724 	tx->bytes = bp->net_stats_prev.tx_bytes;
15725 }
15726 
15727 static const struct netdev_stat_ops bnxt_stat_ops = {
15728 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15729 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15730 	.get_base_stats		= bnxt_get_base_stats,
15731 };
15732 
15733 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15734 {
15735 	struct bnxt_rx_ring_info *rxr, *clone;
15736 	struct bnxt *bp = netdev_priv(dev);
15737 	struct bnxt_ring_struct *ring;
15738 	int rc;
15739 
15740 	if (!bp->rx_ring)
15741 		return -ENETDOWN;
15742 
15743 	rxr = &bp->rx_ring[idx];
15744 	clone = qmem;
15745 	memcpy(clone, rxr, sizeof(*rxr));
15746 	bnxt_init_rx_ring_struct(bp, clone);
15747 	bnxt_reset_rx_ring_struct(bp, clone);
15748 
15749 	clone->rx_prod = 0;
15750 	clone->rx_agg_prod = 0;
15751 	clone->rx_sw_agg_prod = 0;
15752 	clone->rx_next_cons = 0;
15753 	clone->need_head_pool = false;
15754 
15755 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15756 	if (rc)
15757 		return rc;
15758 
15759 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15760 	if (rc < 0)
15761 		goto err_page_pool_destroy;
15762 
15763 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15764 					MEM_TYPE_PAGE_POOL,
15765 					clone->page_pool);
15766 	if (rc)
15767 		goto err_rxq_info_unreg;
15768 
15769 	ring = &clone->rx_ring_struct;
15770 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15771 	if (rc)
15772 		goto err_free_rx_ring;
15773 
15774 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15775 		ring = &clone->rx_agg_ring_struct;
15776 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15777 		if (rc)
15778 			goto err_free_rx_agg_ring;
15779 
15780 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15781 		if (rc)
15782 			goto err_free_rx_agg_ring;
15783 	}
15784 
15785 	if (bp->flags & BNXT_FLAG_TPA) {
15786 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15787 		if (rc)
15788 			goto err_free_tpa_info;
15789 	}
15790 
15791 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15792 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15793 
15794 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15795 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15796 		bnxt_alloc_one_rx_ring_netmem(bp, clone, idx);
15797 	if (bp->flags & BNXT_FLAG_TPA)
15798 		bnxt_alloc_one_tpa_info_data(bp, clone);
15799 
15800 	return 0;
15801 
15802 err_free_tpa_info:
15803 	bnxt_free_one_tpa_info(bp, clone);
15804 err_free_rx_agg_ring:
15805 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15806 err_free_rx_ring:
15807 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15808 err_rxq_info_unreg:
15809 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15810 err_page_pool_destroy:
15811 	page_pool_destroy(clone->page_pool);
15812 	if (bnxt_separate_head_pool(clone))
15813 		page_pool_destroy(clone->head_pool);
15814 	clone->page_pool = NULL;
15815 	clone->head_pool = NULL;
15816 	return rc;
15817 }
15818 
15819 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15820 {
15821 	struct bnxt_rx_ring_info *rxr = qmem;
15822 	struct bnxt *bp = netdev_priv(dev);
15823 	struct bnxt_ring_struct *ring;
15824 
15825 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15826 	bnxt_free_one_tpa_info(bp, rxr);
15827 
15828 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15829 
15830 	page_pool_destroy(rxr->page_pool);
15831 	if (bnxt_separate_head_pool(rxr))
15832 		page_pool_destroy(rxr->head_pool);
15833 	rxr->page_pool = NULL;
15834 	rxr->head_pool = NULL;
15835 
15836 	ring = &rxr->rx_ring_struct;
15837 	bnxt_free_ring(bp, &ring->ring_mem);
15838 
15839 	ring = &rxr->rx_agg_ring_struct;
15840 	bnxt_free_ring(bp, &ring->ring_mem);
15841 
15842 	kfree(rxr->rx_agg_bmap);
15843 	rxr->rx_agg_bmap = NULL;
15844 }
15845 
15846 static void bnxt_copy_rx_ring(struct bnxt *bp,
15847 			      struct bnxt_rx_ring_info *dst,
15848 			      struct bnxt_rx_ring_info *src)
15849 {
15850 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15851 	struct bnxt_ring_struct *dst_ring, *src_ring;
15852 	int i;
15853 
15854 	dst_ring = &dst->rx_ring_struct;
15855 	dst_rmem = &dst_ring->ring_mem;
15856 	src_ring = &src->rx_ring_struct;
15857 	src_rmem = &src_ring->ring_mem;
15858 
15859 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15860 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15861 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15862 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15863 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15864 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15865 
15866 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15867 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15868 	*dst_rmem->vmem = *src_rmem->vmem;
15869 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15870 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15871 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15872 	}
15873 
15874 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15875 		return;
15876 
15877 	dst_ring = &dst->rx_agg_ring_struct;
15878 	dst_rmem = &dst_ring->ring_mem;
15879 	src_ring = &src->rx_agg_ring_struct;
15880 	src_rmem = &src_ring->ring_mem;
15881 
15882 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15883 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15884 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15885 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15886 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15887 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15888 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15889 
15890 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15891 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15892 	*dst_rmem->vmem = *src_rmem->vmem;
15893 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15894 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15895 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15896 	}
15897 
15898 	dst->rx_agg_bmap = src->rx_agg_bmap;
15899 }
15900 
15901 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15902 {
15903 	struct bnxt *bp = netdev_priv(dev);
15904 	struct bnxt_rx_ring_info *rxr, *clone;
15905 	struct bnxt_cp_ring_info *cpr;
15906 	struct bnxt_vnic_info *vnic;
15907 	struct bnxt_napi *bnapi;
15908 	int i, rc;
15909 
15910 	rxr = &bp->rx_ring[idx];
15911 	clone = qmem;
15912 
15913 	rxr->rx_prod = clone->rx_prod;
15914 	rxr->rx_agg_prod = clone->rx_agg_prod;
15915 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15916 	rxr->rx_next_cons = clone->rx_next_cons;
15917 	rxr->rx_tpa = clone->rx_tpa;
15918 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15919 	rxr->page_pool = clone->page_pool;
15920 	rxr->head_pool = clone->head_pool;
15921 	rxr->xdp_rxq = clone->xdp_rxq;
15922 	rxr->need_head_pool = clone->need_head_pool;
15923 
15924 	bnxt_copy_rx_ring(bp, rxr, clone);
15925 
15926 	bnapi = rxr->bnapi;
15927 	cpr = &bnapi->cp_ring;
15928 
15929 	/* All rings have been reserved and previously allocated.
15930 	 * Reallocating with the same parameters should never fail.
15931 	 */
15932 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15933 	if (rc)
15934 		goto err_reset;
15935 
15936 	if (bp->tph_mode) {
15937 		rc = bnxt_hwrm_cp_ring_alloc_p5(bp, rxr->rx_cpr);
15938 		if (rc)
15939 			goto err_reset;
15940 	}
15941 
15942 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15943 	if (rc)
15944 		goto err_reset;
15945 
15946 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15947 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15948 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15949 
15950 	if (bp->flags & BNXT_FLAG_SHARED_RINGS) {
15951 		rc = bnxt_tx_queue_start(bp, idx);
15952 		if (rc)
15953 			goto err_reset;
15954 	}
15955 
15956 	napi_enable_locked(&bnapi->napi);
15957 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
15958 
15959 	for (i = 0; i < bp->nr_vnics; i++) {
15960 		vnic = &bp->vnic_info[i];
15961 
15962 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15963 		if (rc) {
15964 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15965 				   vnic->vnic_id, rc);
15966 			return rc;
15967 		}
15968 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15969 		bnxt_hwrm_vnic_update(bp, vnic,
15970 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15971 	}
15972 
15973 	return 0;
15974 
15975 err_reset:
15976 	netdev_err(bp->dev, "Unexpected HWRM error during queue start rc: %d\n",
15977 		   rc);
15978 	napi_enable_locked(&bnapi->napi);
15979 	bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
15980 	bnxt_reset_task(bp, true);
15981 	return rc;
15982 }
15983 
15984 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15985 {
15986 	struct bnxt *bp = netdev_priv(dev);
15987 	struct bnxt_rx_ring_info *rxr;
15988 	struct bnxt_cp_ring_info *cpr;
15989 	struct bnxt_vnic_info *vnic;
15990 	struct bnxt_napi *bnapi;
15991 	int i;
15992 
15993 	for (i = 0; i < bp->nr_vnics; i++) {
15994 		vnic = &bp->vnic_info[i];
15995 		vnic->mru = 0;
15996 		bnxt_hwrm_vnic_update(bp, vnic,
15997 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15998 	}
15999 	/* Make sure NAPI sees that the VNIC is disabled */
16000 	synchronize_net();
16001 	rxr = &bp->rx_ring[idx];
16002 	bnapi = rxr->bnapi;
16003 	cpr = &bnapi->cp_ring;
16004 	cancel_work_sync(&cpr->dim.work);
16005 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
16006 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
16007 	page_pool_disable_direct_recycling(rxr->page_pool);
16008 	if (bnxt_separate_head_pool(rxr))
16009 		page_pool_disable_direct_recycling(rxr->head_pool);
16010 
16011 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
16012 		bnxt_tx_queue_stop(bp, idx);
16013 
16014 	/* Disable NAPI now after freeing the rings because HWRM_RING_FREE
16015 	 * completion is handled in NAPI to guarantee no more DMA on that ring
16016 	 * after seeing the completion.
16017 	 */
16018 	napi_disable_locked(&bnapi->napi);
16019 
16020 	if (bp->tph_mode) {
16021 		bnxt_hwrm_cp_ring_free(bp, rxr->rx_cpr);
16022 		bnxt_clear_one_cp_ring(bp, rxr->rx_cpr);
16023 	}
16024 	bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
16025 
16026 	memcpy(qmem, rxr, sizeof(*rxr));
16027 	bnxt_init_rx_ring_struct(bp, qmem);
16028 
16029 	return 0;
16030 }
16031 
16032 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
16033 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
16034 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
16035 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
16036 	.ndo_queue_start	= bnxt_queue_start,
16037 	.ndo_queue_stop		= bnxt_queue_stop,
16038 };
16039 
16040 static void bnxt_remove_one(struct pci_dev *pdev)
16041 {
16042 	struct net_device *dev = pci_get_drvdata(pdev);
16043 	struct bnxt *bp = netdev_priv(dev);
16044 
16045 	if (BNXT_PF(bp))
16046 		bnxt_sriov_disable(bp);
16047 
16048 	bnxt_rdma_aux_device_del(bp);
16049 
16050 	bnxt_ptp_clear(bp);
16051 	unregister_netdev(dev);
16052 
16053 	bnxt_rdma_aux_device_uninit(bp);
16054 
16055 	bnxt_free_l2_filters(bp, true);
16056 	bnxt_free_ntp_fltrs(bp, true);
16057 	WARN_ON(bp->num_rss_ctx);
16058 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16059 	/* Flush any pending tasks */
16060 	cancel_work_sync(&bp->sp_task);
16061 	cancel_delayed_work_sync(&bp->fw_reset_task);
16062 	bp->sp_event = 0;
16063 
16064 	bnxt_dl_fw_reporters_destroy(bp);
16065 	bnxt_dl_unregister(bp);
16066 	bnxt_shutdown_tc(bp);
16067 
16068 	bnxt_clear_int_mode(bp);
16069 	bnxt_hwrm_func_drv_unrgtr(bp);
16070 	bnxt_free_hwrm_resources(bp);
16071 	bnxt_hwmon_uninit(bp);
16072 	bnxt_ethtool_free(bp);
16073 	bnxt_dcb_free(bp);
16074 	kfree(bp->ptp_cfg);
16075 	bp->ptp_cfg = NULL;
16076 	kfree(bp->fw_health);
16077 	bp->fw_health = NULL;
16078 	bnxt_cleanup_pci(bp);
16079 	bnxt_free_ctx_mem(bp, true);
16080 	bnxt_free_crash_dump_mem(bp);
16081 	kfree(bp->rss_indir_tbl);
16082 	bp->rss_indir_tbl = NULL;
16083 	bnxt_free_port_stats(bp);
16084 	free_netdev(dev);
16085 }
16086 
16087 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
16088 {
16089 	int rc = 0;
16090 	struct bnxt_link_info *link_info = &bp->link_info;
16091 
16092 	bp->phy_flags = 0;
16093 	rc = bnxt_hwrm_phy_qcaps(bp);
16094 	if (rc) {
16095 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
16096 			   rc);
16097 		return rc;
16098 	}
16099 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
16100 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
16101 	else
16102 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
16103 
16104 	bp->mac_flags = 0;
16105 	bnxt_hwrm_mac_qcaps(bp);
16106 
16107 	if (!fw_dflt)
16108 		return 0;
16109 
16110 	mutex_lock(&bp->link_lock);
16111 	rc = bnxt_update_link(bp, false);
16112 	if (rc) {
16113 		mutex_unlock(&bp->link_lock);
16114 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
16115 			   rc);
16116 		return rc;
16117 	}
16118 
16119 	/* Older firmware does not have supported_auto_speeds, so assume
16120 	 * that all supported speeds can be autonegotiated.
16121 	 */
16122 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
16123 		link_info->support_auto_speeds = link_info->support_speeds;
16124 
16125 	bnxt_init_ethtool_link_settings(bp);
16126 	mutex_unlock(&bp->link_lock);
16127 	return 0;
16128 }
16129 
16130 static int bnxt_get_max_irq(struct pci_dev *pdev)
16131 {
16132 	u16 ctrl;
16133 
16134 	if (!pdev->msix_cap)
16135 		return 1;
16136 
16137 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
16138 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
16139 }
16140 
16141 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16142 				int *max_cp)
16143 {
16144 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
16145 	int max_ring_grps = 0, max_irq;
16146 
16147 	*max_tx = hw_resc->max_tx_rings;
16148 	*max_rx = hw_resc->max_rx_rings;
16149 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
16150 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
16151 			bnxt_get_ulp_msix_num_in_use(bp),
16152 			hw_resc->max_stat_ctxs -
16153 			bnxt_get_ulp_stat_ctxs_in_use(bp));
16154 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
16155 		*max_cp = min_t(int, *max_cp, max_irq);
16156 	max_ring_grps = hw_resc->max_hw_ring_grps;
16157 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
16158 		*max_cp -= 1;
16159 		*max_rx -= 2;
16160 	}
16161 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
16162 		*max_rx >>= 1;
16163 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
16164 		int rc;
16165 
16166 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
16167 		if (rc) {
16168 			*max_rx = 0;
16169 			*max_tx = 0;
16170 		}
16171 		/* On P5 chips, max_cp output param should be available NQs */
16172 		*max_cp = max_irq;
16173 	}
16174 	*max_rx = min_t(int, *max_rx, max_ring_grps);
16175 }
16176 
16177 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
16178 {
16179 	int rx, tx, cp;
16180 
16181 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
16182 	*max_rx = rx;
16183 	*max_tx = tx;
16184 	if (!rx || !tx || !cp)
16185 		return -ENOMEM;
16186 
16187 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
16188 }
16189 
16190 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
16191 			       bool shared)
16192 {
16193 	int rc;
16194 
16195 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16196 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
16197 		/* Not enough rings, try disabling agg rings. */
16198 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
16199 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
16200 		if (rc) {
16201 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
16202 			bp->flags |= BNXT_FLAG_AGG_RINGS;
16203 			return rc;
16204 		}
16205 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
16206 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16207 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
16208 		bnxt_set_ring_params(bp);
16209 	}
16210 
16211 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
16212 		int max_cp, max_stat, max_irq;
16213 
16214 		/* Reserve minimum resources for RoCE */
16215 		max_cp = bnxt_get_max_func_cp_rings(bp);
16216 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
16217 		max_irq = bnxt_get_max_func_irqs(bp);
16218 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
16219 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
16220 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
16221 			return 0;
16222 
16223 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
16224 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
16225 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
16226 		max_cp = min_t(int, max_cp, max_irq);
16227 		max_cp = min_t(int, max_cp, max_stat);
16228 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
16229 		if (rc)
16230 			rc = 0;
16231 	}
16232 	return rc;
16233 }
16234 
16235 /* In initial default shared ring setting, each shared ring must have a
16236  * RX/TX ring pair.
16237  */
16238 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
16239 {
16240 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
16241 	bp->rx_nr_rings = bp->cp_nr_rings;
16242 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
16243 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16244 }
16245 
16246 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
16247 {
16248 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
16249 	int avail_msix;
16250 
16251 	if (!bnxt_can_reserve_rings(bp))
16252 		return 0;
16253 
16254 	if (sh)
16255 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
16256 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
16257 	/* Reduce default rings on multi-port cards so that total default
16258 	 * rings do not exceed CPU count.
16259 	 */
16260 	if (bp->port_count > 1) {
16261 		int max_rings =
16262 			max_t(int, num_online_cpus() / bp->port_count, 1);
16263 
16264 		dflt_rings = min_t(int, dflt_rings, max_rings);
16265 	}
16266 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
16267 	if (rc)
16268 		return rc;
16269 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
16270 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
16271 	if (sh)
16272 		bnxt_trim_dflt_sh_rings(bp);
16273 	else
16274 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
16275 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
16276 
16277 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
16278 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
16279 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
16280 
16281 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
16282 		bnxt_set_dflt_ulp_stat_ctxs(bp);
16283 	}
16284 
16285 	rc = __bnxt_reserve_rings(bp);
16286 	if (rc && rc != -ENODEV)
16287 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
16288 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16289 	if (sh)
16290 		bnxt_trim_dflt_sh_rings(bp);
16291 
16292 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
16293 	if (bnxt_need_reserve_rings(bp)) {
16294 		rc = __bnxt_reserve_rings(bp);
16295 		if (rc && rc != -ENODEV)
16296 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
16297 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16298 	}
16299 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
16300 		bp->rx_nr_rings++;
16301 		bp->cp_nr_rings++;
16302 	}
16303 	if (rc) {
16304 		bp->tx_nr_rings = 0;
16305 		bp->rx_nr_rings = 0;
16306 	}
16307 	return rc;
16308 }
16309 
16310 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
16311 {
16312 	int rc;
16313 
16314 	if (bp->tx_nr_rings)
16315 		return 0;
16316 
16317 	bnxt_ulp_irq_stop(bp);
16318 	bnxt_clear_int_mode(bp);
16319 	rc = bnxt_set_dflt_rings(bp, true);
16320 	if (rc) {
16321 		if (BNXT_VF(bp) && rc == -ENODEV)
16322 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16323 		else
16324 			netdev_err(bp->dev, "Not enough rings available.\n");
16325 		goto init_dflt_ring_err;
16326 	}
16327 	rc = bnxt_init_int_mode(bp);
16328 	if (rc)
16329 		goto init_dflt_ring_err;
16330 
16331 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16332 
16333 	bnxt_set_dflt_rfs(bp);
16334 
16335 init_dflt_ring_err:
16336 	bnxt_ulp_irq_restart(bp, rc);
16337 	return rc;
16338 }
16339 
16340 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
16341 {
16342 	int rc;
16343 
16344 	netdev_ops_assert_locked(bp->dev);
16345 	bnxt_hwrm_func_qcaps(bp);
16346 
16347 	if (netif_running(bp->dev))
16348 		__bnxt_close_nic(bp, true, false);
16349 
16350 	bnxt_ulp_irq_stop(bp);
16351 	bnxt_clear_int_mode(bp);
16352 	rc = bnxt_init_int_mode(bp);
16353 	bnxt_ulp_irq_restart(bp, rc);
16354 
16355 	if (netif_running(bp->dev)) {
16356 		if (rc)
16357 			netif_close(bp->dev);
16358 		else
16359 			rc = bnxt_open_nic(bp, true, false);
16360 	}
16361 
16362 	return rc;
16363 }
16364 
16365 static int bnxt_init_mac_addr(struct bnxt *bp)
16366 {
16367 	int rc = 0;
16368 
16369 	if (BNXT_PF(bp)) {
16370 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16371 	} else {
16372 #ifdef CONFIG_BNXT_SRIOV
16373 		struct bnxt_vf_info *vf = &bp->vf;
16374 		bool strict_approval = true;
16375 
16376 		if (is_valid_ether_addr(vf->mac_addr)) {
16377 			/* overwrite netdev dev_addr with admin VF MAC */
16378 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16379 			/* Older PF driver or firmware may not approve this
16380 			 * correctly.
16381 			 */
16382 			strict_approval = false;
16383 		} else {
16384 			eth_hw_addr_random(bp->dev);
16385 		}
16386 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16387 #endif
16388 	}
16389 	return rc;
16390 }
16391 
16392 static void bnxt_vpd_read_info(struct bnxt *bp)
16393 {
16394 	struct pci_dev *pdev = bp->pdev;
16395 	unsigned int vpd_size, kw_len;
16396 	int pos, size;
16397 	u8 *vpd_data;
16398 
16399 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16400 	if (IS_ERR(vpd_data)) {
16401 		pci_warn(pdev, "Unable to read VPD\n");
16402 		return;
16403 	}
16404 
16405 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16406 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16407 	if (pos < 0)
16408 		goto read_sn;
16409 
16410 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16411 	memcpy(bp->board_partno, &vpd_data[pos], size);
16412 
16413 read_sn:
16414 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16415 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16416 					   &kw_len);
16417 	if (pos < 0)
16418 		goto exit;
16419 
16420 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16421 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16422 exit:
16423 	kfree(vpd_data);
16424 }
16425 
16426 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16427 {
16428 	struct pci_dev *pdev = bp->pdev;
16429 	u64 qword;
16430 
16431 	qword = pci_get_dsn(pdev);
16432 	if (!qword) {
16433 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16434 		return -EOPNOTSUPP;
16435 	}
16436 
16437 	put_unaligned_le64(qword, dsn);
16438 
16439 	bp->flags |= BNXT_FLAG_DSN_VALID;
16440 	return 0;
16441 }
16442 
16443 static int bnxt_map_db_bar(struct bnxt *bp)
16444 {
16445 	if (!bp->db_size)
16446 		return -ENODEV;
16447 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16448 	if (!bp->bar1)
16449 		return -ENOMEM;
16450 	return 0;
16451 }
16452 
16453 void bnxt_print_device_info(struct bnxt *bp)
16454 {
16455 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16456 		    board_info[bp->board_idx].name,
16457 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16458 
16459 	pcie_print_link_status(bp->pdev);
16460 }
16461 
16462 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16463 {
16464 	struct bnxt_hw_resc *hw_resc;
16465 	struct net_device *dev;
16466 	struct bnxt *bp;
16467 	int rc, max_irqs;
16468 
16469 	if (pci_is_bridge(pdev))
16470 		return -ENODEV;
16471 
16472 	if (!pdev->msix_cap) {
16473 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16474 		return -ENODEV;
16475 	}
16476 
16477 	/* Clear any pending DMA transactions from crash kernel
16478 	 * while loading driver in capture kernel.
16479 	 */
16480 	if (is_kdump_kernel()) {
16481 		pci_clear_master(pdev);
16482 		pcie_flr(pdev);
16483 	}
16484 
16485 	max_irqs = bnxt_get_max_irq(pdev);
16486 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16487 				 max_irqs);
16488 	if (!dev)
16489 		return -ENOMEM;
16490 
16491 	bp = netdev_priv(dev);
16492 	bp->board_idx = ent->driver_data;
16493 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16494 	bnxt_set_max_func_irqs(bp, max_irqs);
16495 
16496 	if (bnxt_vf_pciid(bp->board_idx))
16497 		bp->flags |= BNXT_FLAG_VF;
16498 
16499 	/* No devlink port registration in case of a VF */
16500 	if (BNXT_PF(bp))
16501 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16502 
16503 	rc = bnxt_init_board(pdev, dev);
16504 	if (rc < 0)
16505 		goto init_err_free;
16506 
16507 	dev->netdev_ops = &bnxt_netdev_ops;
16508 	dev->stat_ops = &bnxt_stat_ops;
16509 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16510 	dev->ethtool_ops = &bnxt_ethtool_ops;
16511 	pci_set_drvdata(pdev, dev);
16512 
16513 	rc = bnxt_alloc_hwrm_resources(bp);
16514 	if (rc)
16515 		goto init_err_pci_clean;
16516 
16517 	mutex_init(&bp->hwrm_cmd_lock);
16518 	mutex_init(&bp->link_lock);
16519 
16520 	rc = bnxt_fw_init_one_p1(bp);
16521 	if (rc)
16522 		goto init_err_pci_clean;
16523 
16524 	if (BNXT_PF(bp))
16525 		bnxt_vpd_read_info(bp);
16526 
16527 	if (BNXT_CHIP_P5_PLUS(bp)) {
16528 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16529 		if (BNXT_CHIP_P7(bp))
16530 			bp->flags |= BNXT_FLAG_CHIP_P7;
16531 	}
16532 
16533 	rc = bnxt_alloc_rss_indir_tbl(bp);
16534 	if (rc)
16535 		goto init_err_pci_clean;
16536 
16537 	rc = bnxt_fw_init_one_p2(bp);
16538 	if (rc)
16539 		goto init_err_pci_clean;
16540 
16541 	rc = bnxt_map_db_bar(bp);
16542 	if (rc) {
16543 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16544 			rc);
16545 		goto init_err_pci_clean;
16546 	}
16547 
16548 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16549 			   NETIF_F_TSO | NETIF_F_TSO6 |
16550 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16551 			   NETIF_F_GSO_IPXIP4 |
16552 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16553 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16554 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16555 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16556 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16557 
16558 	if (BNXT_SUPPORTS_TPA(bp))
16559 		dev->hw_features |= NETIF_F_LRO;
16560 
16561 	dev->hw_enc_features =
16562 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16563 			NETIF_F_TSO | NETIF_F_TSO6 |
16564 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16565 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16566 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16567 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16568 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16569 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16570 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16571 	else
16572 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16573 
16574 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16575 				    NETIF_F_GSO_GRE_CSUM;
16576 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16577 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16578 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16579 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16580 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16581 	if (BNXT_SUPPORTS_TPA(bp))
16582 		dev->hw_features |= NETIF_F_GRO_HW;
16583 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16584 	if (dev->features & NETIF_F_GRO_HW)
16585 		dev->features &= ~NETIF_F_LRO;
16586 	dev->priv_flags |= IFF_UNICAST_FLT;
16587 
16588 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16589 	if (bp->tso_max_segs)
16590 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16591 
16592 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16593 			    NETDEV_XDP_ACT_RX_SG;
16594 
16595 #ifdef CONFIG_BNXT_SRIOV
16596 	init_waitqueue_head(&bp->sriov_cfg_wait);
16597 #endif
16598 	if (BNXT_SUPPORTS_TPA(bp)) {
16599 		bp->gro_func = bnxt_gro_func_5730x;
16600 		if (BNXT_CHIP_P4(bp))
16601 			bp->gro_func = bnxt_gro_func_5731x;
16602 		else if (BNXT_CHIP_P5_PLUS(bp))
16603 			bp->gro_func = bnxt_gro_func_5750x;
16604 	}
16605 	if (!BNXT_CHIP_P4_PLUS(bp))
16606 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16607 
16608 	rc = bnxt_init_mac_addr(bp);
16609 	if (rc) {
16610 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16611 		rc = -EADDRNOTAVAIL;
16612 		goto init_err_pci_clean;
16613 	}
16614 
16615 	if (BNXT_PF(bp)) {
16616 		/* Read the adapter's DSN to use as the eswitch switch_id */
16617 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16618 	}
16619 
16620 	/* MTU range: 60 - FW defined max */
16621 	dev->min_mtu = ETH_ZLEN;
16622 	dev->max_mtu = bp->max_mtu;
16623 
16624 	rc = bnxt_probe_phy(bp, true);
16625 	if (rc)
16626 		goto init_err_pci_clean;
16627 
16628 	hw_resc = &bp->hw_resc;
16629 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16630 		       BNXT_L2_FLTR_MAX_FLTR;
16631 	/* Older firmware may not report these filters properly */
16632 	if (bp->max_fltr < BNXT_MAX_FLTR)
16633 		bp->max_fltr = BNXT_MAX_FLTR;
16634 	bnxt_init_l2_fltr_tbl(bp);
16635 	__bnxt_set_rx_skb_mode(bp, false);
16636 	bnxt_set_tpa_flags(bp);
16637 	bnxt_init_ring_params(bp);
16638 	bnxt_set_ring_params(bp);
16639 	bnxt_rdma_aux_device_init(bp);
16640 	rc = bnxt_set_dflt_rings(bp, true);
16641 	if (rc) {
16642 		if (BNXT_VF(bp) && rc == -ENODEV) {
16643 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16644 		} else {
16645 			netdev_err(bp->dev, "Not enough rings available.\n");
16646 			rc = -ENOMEM;
16647 		}
16648 		goto init_err_pci_clean;
16649 	}
16650 
16651 	bnxt_fw_init_one_p3(bp);
16652 
16653 	bnxt_init_dflt_coal(bp);
16654 
16655 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16656 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16657 
16658 	rc = bnxt_init_int_mode(bp);
16659 	if (rc)
16660 		goto init_err_pci_clean;
16661 
16662 	/* No TC has been set yet and rings may have been trimmed due to
16663 	 * limited MSIX, so we re-initialize the TX rings per TC.
16664 	 */
16665 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16666 
16667 	if (BNXT_PF(bp)) {
16668 		if (!bnxt_pf_wq) {
16669 			bnxt_pf_wq =
16670 				create_singlethread_workqueue("bnxt_pf_wq");
16671 			if (!bnxt_pf_wq) {
16672 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16673 				rc = -ENOMEM;
16674 				goto init_err_pci_clean;
16675 			}
16676 		}
16677 		rc = bnxt_init_tc(bp);
16678 		if (rc)
16679 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16680 				   rc);
16681 	}
16682 
16683 	bnxt_inv_fw_health_reg(bp);
16684 	rc = bnxt_dl_register(bp);
16685 	if (rc)
16686 		goto init_err_dl;
16687 
16688 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16689 
16690 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16691 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16692 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16693 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16694 	dev->request_ops_lock = true;
16695 
16696 	rc = register_netdev(dev);
16697 	if (rc)
16698 		goto init_err_cleanup;
16699 
16700 	bnxt_dl_fw_reporters_create(bp);
16701 
16702 	bnxt_rdma_aux_device_add(bp);
16703 
16704 	bnxt_print_device_info(bp);
16705 
16706 	pci_save_state(pdev);
16707 
16708 	return 0;
16709 init_err_cleanup:
16710 	bnxt_rdma_aux_device_uninit(bp);
16711 	bnxt_dl_unregister(bp);
16712 init_err_dl:
16713 	bnxt_shutdown_tc(bp);
16714 	bnxt_clear_int_mode(bp);
16715 
16716 init_err_pci_clean:
16717 	bnxt_hwrm_func_drv_unrgtr(bp);
16718 	bnxt_free_hwrm_resources(bp);
16719 	bnxt_hwmon_uninit(bp);
16720 	bnxt_ethtool_free(bp);
16721 	bnxt_ptp_clear(bp);
16722 	kfree(bp->ptp_cfg);
16723 	bp->ptp_cfg = NULL;
16724 	kfree(bp->fw_health);
16725 	bp->fw_health = NULL;
16726 	bnxt_cleanup_pci(bp);
16727 	bnxt_free_ctx_mem(bp, true);
16728 	bnxt_free_crash_dump_mem(bp);
16729 	kfree(bp->rss_indir_tbl);
16730 	bp->rss_indir_tbl = NULL;
16731 
16732 init_err_free:
16733 	free_netdev(dev);
16734 	return rc;
16735 }
16736 
16737 static void bnxt_shutdown(struct pci_dev *pdev)
16738 {
16739 	struct net_device *dev = pci_get_drvdata(pdev);
16740 	struct bnxt *bp;
16741 
16742 	if (!dev)
16743 		return;
16744 
16745 	rtnl_lock();
16746 	netdev_lock(dev);
16747 	bp = netdev_priv(dev);
16748 	if (!bp)
16749 		goto shutdown_exit;
16750 
16751 	if (netif_running(dev))
16752 		netif_close(dev);
16753 
16754 	bnxt_ptp_clear(bp);
16755 	bnxt_clear_int_mode(bp);
16756 	pci_disable_device(pdev);
16757 
16758 	if (system_state == SYSTEM_POWER_OFF) {
16759 		pci_wake_from_d3(pdev, bp->wol);
16760 		pci_set_power_state(pdev, PCI_D3hot);
16761 	}
16762 
16763 shutdown_exit:
16764 	netdev_unlock(dev);
16765 	rtnl_unlock();
16766 }
16767 
16768 #ifdef CONFIG_PM_SLEEP
16769 static int bnxt_suspend(struct device *device)
16770 {
16771 	struct net_device *dev = dev_get_drvdata(device);
16772 	struct bnxt *bp = netdev_priv(dev);
16773 	int rc = 0;
16774 
16775 	bnxt_ulp_stop(bp);
16776 
16777 	netdev_lock(dev);
16778 	if (netif_running(dev)) {
16779 		netif_device_detach(dev);
16780 		rc = bnxt_close(dev);
16781 	}
16782 	bnxt_hwrm_func_drv_unrgtr(bp);
16783 	bnxt_ptp_clear(bp);
16784 	pci_disable_device(bp->pdev);
16785 	bnxt_free_ctx_mem(bp, false);
16786 	netdev_unlock(dev);
16787 	return rc;
16788 }
16789 
16790 static int bnxt_resume(struct device *device)
16791 {
16792 	struct net_device *dev = dev_get_drvdata(device);
16793 	struct bnxt *bp = netdev_priv(dev);
16794 	int rc = 0;
16795 
16796 	netdev_lock(dev);
16797 	rc = pci_enable_device(bp->pdev);
16798 	if (rc) {
16799 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16800 			   rc);
16801 		goto resume_exit;
16802 	}
16803 	pci_set_master(bp->pdev);
16804 	if (bnxt_hwrm_ver_get(bp)) {
16805 		rc = -ENODEV;
16806 		goto resume_exit;
16807 	}
16808 	rc = bnxt_hwrm_func_reset(bp);
16809 	if (rc) {
16810 		rc = -EBUSY;
16811 		goto resume_exit;
16812 	}
16813 
16814 	rc = bnxt_hwrm_func_qcaps(bp);
16815 	if (rc)
16816 		goto resume_exit;
16817 
16818 	bnxt_clear_reservations(bp, true);
16819 
16820 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16821 		rc = -ENODEV;
16822 		goto resume_exit;
16823 	}
16824 	if (bp->fw_crash_mem)
16825 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16826 
16827 	if (bnxt_ptp_init(bp)) {
16828 		kfree(bp->ptp_cfg);
16829 		bp->ptp_cfg = NULL;
16830 	}
16831 	bnxt_get_wol_settings(bp);
16832 	if (netif_running(dev)) {
16833 		rc = bnxt_open(dev);
16834 		if (!rc)
16835 			netif_device_attach(dev);
16836 	}
16837 
16838 resume_exit:
16839 	netdev_unlock(bp->dev);
16840 	bnxt_ulp_start(bp, rc);
16841 	if (!rc)
16842 		bnxt_reenable_sriov(bp);
16843 	return rc;
16844 }
16845 
16846 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16847 #define BNXT_PM_OPS (&bnxt_pm_ops)
16848 
16849 #else
16850 
16851 #define BNXT_PM_OPS NULL
16852 
16853 #endif /* CONFIG_PM_SLEEP */
16854 
16855 /**
16856  * bnxt_io_error_detected - called when PCI error is detected
16857  * @pdev: Pointer to PCI device
16858  * @state: The current pci connection state
16859  *
16860  * This function is called after a PCI bus error affecting
16861  * this device has been detected.
16862  */
16863 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16864 					       pci_channel_state_t state)
16865 {
16866 	struct net_device *netdev = pci_get_drvdata(pdev);
16867 	struct bnxt *bp = netdev_priv(netdev);
16868 	bool abort = false;
16869 
16870 	netdev_info(netdev, "PCI I/O error detected\n");
16871 
16872 	bnxt_ulp_stop(bp);
16873 
16874 	netdev_lock(netdev);
16875 	netif_device_detach(netdev);
16876 
16877 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16878 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16879 		abort = true;
16880 	}
16881 
16882 	if (abort || state == pci_channel_io_perm_failure) {
16883 		netdev_unlock(netdev);
16884 		return PCI_ERS_RESULT_DISCONNECT;
16885 	}
16886 
16887 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16888 	 * so we disable bus master to prevent any potential bad DMAs before
16889 	 * freeing kernel memory.
16890 	 */
16891 	if (state == pci_channel_io_frozen) {
16892 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16893 		bnxt_fw_fatal_close(bp);
16894 	}
16895 
16896 	if (netif_running(netdev))
16897 		__bnxt_close_nic(bp, true, true);
16898 
16899 	if (pci_is_enabled(pdev))
16900 		pci_disable_device(pdev);
16901 	bnxt_free_ctx_mem(bp, false);
16902 	netdev_unlock(netdev);
16903 
16904 	/* Request a slot slot reset. */
16905 	return PCI_ERS_RESULT_NEED_RESET;
16906 }
16907 
16908 /**
16909  * bnxt_io_slot_reset - called after the pci bus has been reset.
16910  * @pdev: Pointer to PCI device
16911  *
16912  * Restart the card from scratch, as if from a cold-boot.
16913  * At this point, the card has experienced a hard reset,
16914  * followed by fixups by BIOS, and has its config space
16915  * set up identically to what it was at cold boot.
16916  */
16917 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16918 {
16919 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16920 	struct net_device *netdev = pci_get_drvdata(pdev);
16921 	struct bnxt *bp = netdev_priv(netdev);
16922 	int retry = 0;
16923 	int err = 0;
16924 	int off;
16925 
16926 	netdev_info(bp->dev, "PCI Slot Reset\n");
16927 
16928 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16929 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16930 		msleep(900);
16931 
16932 	netdev_lock(netdev);
16933 
16934 	if (pci_enable_device(pdev)) {
16935 		dev_err(&pdev->dev,
16936 			"Cannot re-enable PCI device after reset.\n");
16937 	} else {
16938 		pci_set_master(pdev);
16939 		/* Upon fatal error, our device internal logic that latches to
16940 		 * BAR value is getting reset and will restore only upon
16941 		 * rewriting the BARs.
16942 		 *
16943 		 * As pci_restore_state() does not re-write the BARs if the
16944 		 * value is same as saved value earlier, driver needs to
16945 		 * write the BARs to 0 to force restore, in case of fatal error.
16946 		 */
16947 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16948 				       &bp->state)) {
16949 			for (off = PCI_BASE_ADDRESS_0;
16950 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16951 				pci_write_config_dword(bp->pdev, off, 0);
16952 		}
16953 		pci_restore_state(pdev);
16954 		pci_save_state(pdev);
16955 
16956 		bnxt_inv_fw_health_reg(bp);
16957 		bnxt_try_map_fw_health_reg(bp);
16958 
16959 		/* In some PCIe AER scenarios, firmware may take up to
16960 		 * 10 seconds to become ready in the worst case.
16961 		 */
16962 		do {
16963 			err = bnxt_try_recover_fw(bp);
16964 			if (!err)
16965 				break;
16966 			retry++;
16967 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16968 
16969 		if (err) {
16970 			dev_err(&pdev->dev, "Firmware not ready\n");
16971 			goto reset_exit;
16972 		}
16973 
16974 		err = bnxt_hwrm_func_reset(bp);
16975 		if (!err)
16976 			result = PCI_ERS_RESULT_RECOVERED;
16977 
16978 		bnxt_ulp_irq_stop(bp);
16979 		bnxt_clear_int_mode(bp);
16980 		err = bnxt_init_int_mode(bp);
16981 		bnxt_ulp_irq_restart(bp, err);
16982 	}
16983 
16984 reset_exit:
16985 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16986 	bnxt_clear_reservations(bp, true);
16987 	netdev_unlock(netdev);
16988 
16989 	return result;
16990 }
16991 
16992 /**
16993  * bnxt_io_resume - called when traffic can start flowing again.
16994  * @pdev: Pointer to PCI device
16995  *
16996  * This callback is called when the error recovery driver tells
16997  * us that its OK to resume normal operation.
16998  */
16999 static void bnxt_io_resume(struct pci_dev *pdev)
17000 {
17001 	struct net_device *netdev = pci_get_drvdata(pdev);
17002 	struct bnxt *bp = netdev_priv(netdev);
17003 	int err;
17004 
17005 	netdev_info(bp->dev, "PCI Slot Resume\n");
17006 	netdev_lock(netdev);
17007 
17008 	err = bnxt_hwrm_func_qcaps(bp);
17009 	if (!err) {
17010 		if (netif_running(netdev))
17011 			err = bnxt_open(netdev);
17012 		else
17013 			err = bnxt_reserve_rings(bp, true);
17014 	}
17015 
17016 	if (!err)
17017 		netif_device_attach(netdev);
17018 
17019 	netdev_unlock(netdev);
17020 	bnxt_ulp_start(bp, err);
17021 	if (!err)
17022 		bnxt_reenable_sriov(bp);
17023 }
17024 
17025 static const struct pci_error_handlers bnxt_err_handler = {
17026 	.error_detected	= bnxt_io_error_detected,
17027 	.slot_reset	= bnxt_io_slot_reset,
17028 	.resume		= bnxt_io_resume
17029 };
17030 
17031 static struct pci_driver bnxt_pci_driver = {
17032 	.name		= DRV_MODULE_NAME,
17033 	.id_table	= bnxt_pci_tbl,
17034 	.probe		= bnxt_init_one,
17035 	.remove		= bnxt_remove_one,
17036 	.shutdown	= bnxt_shutdown,
17037 	.driver.pm	= BNXT_PM_OPS,
17038 	.err_handler	= &bnxt_err_handler,
17039 #if defined(CONFIG_BNXT_SRIOV)
17040 	.sriov_configure = bnxt_sriov_configure,
17041 #endif
17042 };
17043 
17044 static int __init bnxt_init(void)
17045 {
17046 	int err;
17047 
17048 	bnxt_debug_init();
17049 	err = pci_register_driver(&bnxt_pci_driver);
17050 	if (err) {
17051 		bnxt_debug_exit();
17052 		return err;
17053 	}
17054 
17055 	return 0;
17056 }
17057 
17058 static void __exit bnxt_exit(void)
17059 {
17060 	pci_unregister_driver(&bnxt_pci_driver);
17061 	if (bnxt_pf_wq)
17062 		destroy_workqueue(bnxt_pf_wq);
17063 	bnxt_debug_exit();
17064 }
17065 
17066 module_init(bnxt_init);
17067 module_exit(bnxt_exit);
17068