xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision f7f0adfe64de08803990dc4cbecd2849c04e314a)
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_queues.h>
58 
59 #include "bnxt_hsi.h"
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ulp.h"
63 #include "bnxt_sriov.h"
64 #include "bnxt_ethtool.h"
65 #include "bnxt_dcb.h"
66 #include "bnxt_xdp.h"
67 #include "bnxt_ptp.h"
68 #include "bnxt_vfr.h"
69 #include "bnxt_tc.h"
70 #include "bnxt_devlink.h"
71 #include "bnxt_debugfs.h"
72 #include "bnxt_coredump.h"
73 #include "bnxt_hwmon.h"
74 
75 #define BNXT_TX_TIMEOUT		(5 * HZ)
76 #define BNXT_DEF_MSG_ENABLE	(NETIF_MSG_DRV | NETIF_MSG_HW | \
77 				 NETIF_MSG_TX_ERR)
78 
79 MODULE_LICENSE("GPL");
80 MODULE_DESCRIPTION("Broadcom NetXtreme network driver");
81 
82 #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
83 #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
84 
85 #define BNXT_TX_PUSH_THRESH 164
86 
87 /* indexed by enum board_idx */
88 static const struct {
89 	char *name;
90 } board_info[] = {
91 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
92 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
93 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
94 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
95 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
96 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
97 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
98 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
99 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
100 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
101 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
102 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
103 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
104 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
105 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
106 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
108 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
109 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
110 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
111 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
112 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
113 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
114 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
115 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
116 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
117 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
118 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
119 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
120 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
121 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
123 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
124 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
125 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
126 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
127 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
128 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
129 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
130 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
131 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
132 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
134 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
135 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
136 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
137 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
138 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
139 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
140 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
141 };
142 
143 static const struct pci_device_id bnxt_pci_tbl[] = {
144 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
145 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
147 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
153 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
165 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
166 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
171 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
172 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
179 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
187 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
193 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
194 #ifdef CONFIG_BNXT_SRIOV
195 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
196 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
197 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
199 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
200 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
206 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
211 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
212 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
214 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
216 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
217 #endif
218 	{ 0 }
219 };
220 
221 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
222 
223 static const u16 bnxt_vf_req_snif[] = {
224 	HWRM_FUNC_CFG,
225 	HWRM_FUNC_VF_CFG,
226 	HWRM_PORT_PHY_QCFG,
227 	HWRM_CFA_L2_FILTER_ALLOC,
228 };
229 
230 static const u16 bnxt_async_events_arr[] = {
231 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
235 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
236 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
239 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
242 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
243 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
244 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
245 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
246 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
247 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
248 };
249 
250 const u16 bnxt_bstore_to_trace[] = {
251 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
252 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
253 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
254 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
255 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
256 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
257 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
258 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
259 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
260 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
261 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
262 };
263 
264 static struct workqueue_struct *bnxt_pf_wq;
265 
266 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
267 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
268 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
269 
270 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
271 	.ports = {
272 		.src = 0,
273 		.dst = 0,
274 	},
275 	.addrs = {
276 		.v6addrs = {
277 			.src = BNXT_IPV6_MASK_NONE,
278 			.dst = BNXT_IPV6_MASK_NONE,
279 		},
280 	},
281 };
282 
283 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
284 	.ports = {
285 		.src = cpu_to_be16(0xffff),
286 		.dst = cpu_to_be16(0xffff),
287 	},
288 	.addrs = {
289 		.v6addrs = {
290 			.src = BNXT_IPV6_MASK_ALL,
291 			.dst = BNXT_IPV6_MASK_ALL,
292 		},
293 	},
294 };
295 
296 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
297 	.ports = {
298 		.src = cpu_to_be16(0xffff),
299 		.dst = cpu_to_be16(0xffff),
300 	},
301 	.addrs = {
302 		.v4addrs = {
303 			.src = cpu_to_be32(0xffffffff),
304 			.dst = cpu_to_be32(0xffffffff),
305 		},
306 	},
307 };
308 
309 static bool bnxt_vf_pciid(enum board_idx idx)
310 {
311 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
312 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
313 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
314 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
315 }
316 
317 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
318 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
319 
320 #define BNXT_DB_CQ(db, idx)						\
321 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
322 
323 #define BNXT_DB_NQ_P5(db, idx)						\
324 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
325 		    (db)->doorbell)
326 
327 #define BNXT_DB_NQ_P7(db, idx)						\
328 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
329 		    DB_RING_IDX(db, idx), (db)->doorbell)
330 
331 #define BNXT_DB_CQ_ARM(db, idx)						\
332 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
333 
334 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
335 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
336 		    DB_RING_IDX(db, idx), (db)->doorbell)
337 
338 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
339 {
340 	if (bp->flags & BNXT_FLAG_CHIP_P7)
341 		BNXT_DB_NQ_P7(db, idx);
342 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
343 		BNXT_DB_NQ_P5(db, idx);
344 	else
345 		BNXT_DB_CQ(db, idx);
346 }
347 
348 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
349 {
350 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
351 		BNXT_DB_NQ_ARM_P5(db, idx);
352 	else
353 		BNXT_DB_CQ_ARM(db, idx);
354 }
355 
356 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
357 {
358 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
359 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
360 			    DB_RING_IDX(db, idx), db->doorbell);
361 	else
362 		BNXT_DB_CQ(db, idx);
363 }
364 
365 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
366 {
367 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
368 		return;
369 
370 	if (BNXT_PF(bp))
371 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
372 	else
373 		schedule_delayed_work(&bp->fw_reset_task, delay);
374 }
375 
376 static void __bnxt_queue_sp_work(struct bnxt *bp)
377 {
378 	if (BNXT_PF(bp))
379 		queue_work(bnxt_pf_wq, &bp->sp_task);
380 	else
381 		schedule_work(&bp->sp_task);
382 }
383 
384 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
385 {
386 	set_bit(event, &bp->sp_event);
387 	__bnxt_queue_sp_work(bp);
388 }
389 
390 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
391 {
392 	if (!rxr->bnapi->in_reset) {
393 		rxr->bnapi->in_reset = true;
394 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
395 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
396 		else
397 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
398 		__bnxt_queue_sp_work(bp);
399 	}
400 	rxr->rx_next_cons = 0xffff;
401 }
402 
403 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
404 			  u16 curr)
405 {
406 	struct bnxt_napi *bnapi = txr->bnapi;
407 
408 	if (bnapi->tx_fault)
409 		return;
410 
411 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
412 		   txr->txq_index, txr->tx_hw_cons,
413 		   txr->tx_cons, txr->tx_prod, curr);
414 	WARN_ON_ONCE(1);
415 	bnapi->tx_fault = 1;
416 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
417 }
418 
419 const u16 bnxt_lhint_arr[] = {
420 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
421 	TX_BD_FLAGS_LHINT_512_TO_1023,
422 	TX_BD_FLAGS_LHINT_1024_TO_2047,
423 	TX_BD_FLAGS_LHINT_1024_TO_2047,
424 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
425 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
426 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
427 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
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 };
440 
441 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
442 {
443 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
444 
445 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
446 		return 0;
447 
448 	return md_dst->u.port_info.port_id;
449 }
450 
451 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
452 			     u16 prod)
453 {
454 	/* Sync BD data before updating doorbell */
455 	wmb();
456 	bnxt_db_write(bp, &txr->tx_db, prod);
457 	txr->kick_pending = 0;
458 }
459 
460 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
461 {
462 	struct bnxt *bp = netdev_priv(dev);
463 	struct tx_bd *txbd, *txbd0;
464 	struct tx_bd_ext *txbd1;
465 	struct netdev_queue *txq;
466 	int i;
467 	dma_addr_t mapping;
468 	unsigned int length, pad = 0;
469 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
470 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
471 	struct pci_dev *pdev = bp->pdev;
472 	u16 prod, last_frag, txts_prod;
473 	struct bnxt_tx_ring_info *txr;
474 	struct bnxt_sw_tx_bd *tx_buf;
475 	__le32 lflags = 0;
476 
477 	i = skb_get_queue_mapping(skb);
478 	if (unlikely(i >= bp->tx_nr_rings)) {
479 		dev_kfree_skb_any(skb);
480 		dev_core_stats_tx_dropped_inc(dev);
481 		return NETDEV_TX_OK;
482 	}
483 
484 	txq = netdev_get_tx_queue(dev, i);
485 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
486 	prod = txr->tx_prod;
487 
488 	free_size = bnxt_tx_avail(bp, txr);
489 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
490 		/* We must have raced with NAPI cleanup */
491 		if (net_ratelimit() && txr->kick_pending)
492 			netif_warn(bp, tx_err, dev,
493 				   "bnxt: ring busy w/ flush pending!\n");
494 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
495 					bp->tx_wake_thresh))
496 			return NETDEV_TX_BUSY;
497 	}
498 
499 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
500 		goto tx_free;
501 
502 	length = skb->len;
503 	len = skb_headlen(skb);
504 	last_frag = skb_shinfo(skb)->nr_frags;
505 
506 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
507 
508 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
509 	tx_buf->skb = skb;
510 	tx_buf->nr_frags = last_frag;
511 
512 	vlan_tag_flags = 0;
513 	cfa_action = bnxt_xmit_get_cfa_action(skb);
514 	if (skb_vlan_tag_present(skb)) {
515 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
516 				 skb_vlan_tag_get(skb);
517 		/* Currently supports 8021Q, 8021AD vlan offloads
518 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
519 		 */
520 		if (skb->vlan_proto == htons(ETH_P_8021Q))
521 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
522 	}
523 
524 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
525 	    ptp->tx_tstamp_en) {
526 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
527 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
528 			tx_buf->is_ts_pkt = 1;
529 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
530 		} else if (!skb_is_gso(skb)) {
531 			u16 seq_id, hdr_off;
532 
533 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
534 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
535 				if (vlan_tag_flags)
536 					hdr_off += VLAN_HLEN;
537 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
538 				tx_buf->is_ts_pkt = 1;
539 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
540 
541 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
542 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
543 				tx_buf->txts_prod = txts_prod;
544 			}
545 		}
546 	}
547 	if (unlikely(skb->no_fcs))
548 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
549 
550 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
551 	    !lflags) {
552 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
553 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
554 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
555 		void __iomem *db = txr->tx_db.doorbell;
556 		void *pdata = tx_push_buf->data;
557 		u64 *end;
558 		int j, push_len;
559 
560 		/* Set COAL_NOW to be ready quickly for the next push */
561 		tx_push->tx_bd_len_flags_type =
562 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
563 					TX_BD_TYPE_LONG_TX_BD |
564 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
565 					TX_BD_FLAGS_COAL_NOW |
566 					TX_BD_FLAGS_PACKET_END |
567 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
568 
569 		if (skb->ip_summed == CHECKSUM_PARTIAL)
570 			tx_push1->tx_bd_hsize_lflags =
571 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
572 		else
573 			tx_push1->tx_bd_hsize_lflags = 0;
574 
575 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
576 		tx_push1->tx_bd_cfa_action =
577 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
578 
579 		end = pdata + length;
580 		end = PTR_ALIGN(end, 8) - 1;
581 		*end = 0;
582 
583 		skb_copy_from_linear_data(skb, pdata, len);
584 		pdata += len;
585 		for (j = 0; j < last_frag; j++) {
586 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
587 			void *fptr;
588 
589 			fptr = skb_frag_address_safe(frag);
590 			if (!fptr)
591 				goto normal_tx;
592 
593 			memcpy(pdata, fptr, skb_frag_size(frag));
594 			pdata += skb_frag_size(frag);
595 		}
596 
597 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
598 		txbd->tx_bd_haddr = txr->data_mapping;
599 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
600 		prod = NEXT_TX(prod);
601 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
602 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
603 		memcpy(txbd, tx_push1, sizeof(*txbd));
604 		prod = NEXT_TX(prod);
605 		tx_push->doorbell =
606 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
607 				    DB_RING_IDX(&txr->tx_db, prod));
608 		WRITE_ONCE(txr->tx_prod, prod);
609 
610 		tx_buf->is_push = 1;
611 		netdev_tx_sent_queue(txq, skb->len);
612 		wmb();	/* Sync is_push and byte queue before pushing data */
613 
614 		push_len = (length + sizeof(*tx_push) + 7) / 8;
615 		if (push_len > 16) {
616 			__iowrite64_copy(db, tx_push_buf, 16);
617 			__iowrite32_copy(db + 4, tx_push_buf + 1,
618 					 (push_len - 16) << 1);
619 		} else {
620 			__iowrite64_copy(db, tx_push_buf, push_len);
621 		}
622 
623 		goto tx_done;
624 	}
625 
626 normal_tx:
627 	if (length < BNXT_MIN_PKT_SIZE) {
628 		pad = BNXT_MIN_PKT_SIZE - length;
629 		if (skb_pad(skb, pad))
630 			/* SKB already freed. */
631 			goto tx_kick_pending;
632 		length = BNXT_MIN_PKT_SIZE;
633 	}
634 
635 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
636 
637 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
638 		goto tx_free;
639 
640 	dma_unmap_addr_set(tx_buf, mapping, mapping);
641 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
642 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
643 
644 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
645 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
646 
647 	prod = NEXT_TX(prod);
648 	txbd1 = (struct tx_bd_ext *)
649 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
650 
651 	txbd1->tx_bd_hsize_lflags = lflags;
652 	if (skb_is_gso(skb)) {
653 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
654 		u32 hdr_len;
655 
656 		if (skb->encapsulation) {
657 			if (udp_gso)
658 				hdr_len = skb_inner_transport_offset(skb) +
659 					  sizeof(struct udphdr);
660 			else
661 				hdr_len = skb_inner_tcp_all_headers(skb);
662 		} else if (udp_gso) {
663 			hdr_len = skb_transport_offset(skb) +
664 				  sizeof(struct udphdr);
665 		} else {
666 			hdr_len = skb_tcp_all_headers(skb);
667 		}
668 
669 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
670 					TX_BD_FLAGS_T_IPID |
671 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
672 		length = skb_shinfo(skb)->gso_size;
673 		txbd1->tx_bd_mss = cpu_to_le32(length);
674 		length += hdr_len;
675 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
676 		txbd1->tx_bd_hsize_lflags |=
677 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
678 		txbd1->tx_bd_mss = 0;
679 	}
680 
681 	length >>= 9;
682 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
683 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
684 				     skb->len);
685 		i = 0;
686 		goto tx_dma_error;
687 	}
688 	flags |= bnxt_lhint_arr[length];
689 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
690 
691 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
692 	txbd1->tx_bd_cfa_action =
693 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
694 	txbd0 = txbd;
695 	for (i = 0; i < last_frag; i++) {
696 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
697 
698 		prod = NEXT_TX(prod);
699 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
700 
701 		len = skb_frag_size(frag);
702 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
703 					   DMA_TO_DEVICE);
704 
705 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
706 			goto tx_dma_error;
707 
708 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
709 		dma_unmap_addr_set(tx_buf, mapping, mapping);
710 
711 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
712 
713 		flags = len << TX_BD_LEN_SHIFT;
714 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
715 	}
716 
717 	flags &= ~TX_BD_LEN;
718 	txbd->tx_bd_len_flags_type =
719 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
720 			    TX_BD_FLAGS_PACKET_END);
721 
722 	netdev_tx_sent_queue(txq, skb->len);
723 
724 	skb_tx_timestamp(skb);
725 
726 	prod = NEXT_TX(prod);
727 	WRITE_ONCE(txr->tx_prod, prod);
728 
729 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
730 		bnxt_txr_db_kick(bp, txr, prod);
731 	} else {
732 		if (free_size >= bp->tx_wake_thresh)
733 			txbd0->tx_bd_len_flags_type |=
734 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
735 		txr->kick_pending = 1;
736 	}
737 
738 tx_done:
739 
740 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
741 		if (netdev_xmit_more() && !tx_buf->is_push) {
742 			txbd0->tx_bd_len_flags_type &=
743 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
744 			bnxt_txr_db_kick(bp, txr, prod);
745 		}
746 
747 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
748 				   bp->tx_wake_thresh);
749 	}
750 	return NETDEV_TX_OK;
751 
752 tx_dma_error:
753 	last_frag = i;
754 
755 	/* start back at beginning and unmap skb */
756 	prod = txr->tx_prod;
757 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
758 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
759 			 skb_headlen(skb), DMA_TO_DEVICE);
760 	prod = NEXT_TX(prod);
761 
762 	/* unmap remaining mapped pages */
763 	for (i = 0; i < last_frag; i++) {
764 		prod = NEXT_TX(prod);
765 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
766 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
767 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
768 			       DMA_TO_DEVICE);
769 	}
770 
771 tx_free:
772 	dev_kfree_skb_any(skb);
773 tx_kick_pending:
774 	if (BNXT_TX_PTP_IS_SET(lflags)) {
775 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
776 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
777 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
778 			/* set SKB to err so PTP worker will clean up */
779 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
780 	}
781 	if (txr->kick_pending)
782 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
783 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
784 	dev_core_stats_tx_dropped_inc(dev);
785 	return NETDEV_TX_OK;
786 }
787 
788 /* Returns true if some remaining TX packets not processed. */
789 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
790 			  int budget)
791 {
792 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
793 	struct pci_dev *pdev = bp->pdev;
794 	u16 hw_cons = txr->tx_hw_cons;
795 	unsigned int tx_bytes = 0;
796 	u16 cons = txr->tx_cons;
797 	int tx_pkts = 0;
798 	bool rc = false;
799 
800 	while (RING_TX(bp, cons) != hw_cons) {
801 		struct bnxt_sw_tx_bd *tx_buf;
802 		struct sk_buff *skb;
803 		bool is_ts_pkt;
804 		int j, last;
805 
806 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
807 		skb = tx_buf->skb;
808 
809 		if (unlikely(!skb)) {
810 			bnxt_sched_reset_txr(bp, txr, cons);
811 			return rc;
812 		}
813 
814 		is_ts_pkt = tx_buf->is_ts_pkt;
815 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
816 			rc = true;
817 			break;
818 		}
819 
820 		cons = NEXT_TX(cons);
821 		tx_pkts++;
822 		tx_bytes += skb->len;
823 		tx_buf->skb = NULL;
824 		tx_buf->is_ts_pkt = 0;
825 
826 		if (tx_buf->is_push) {
827 			tx_buf->is_push = 0;
828 			goto next_tx_int;
829 		}
830 
831 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
832 				 skb_headlen(skb), DMA_TO_DEVICE);
833 		last = tx_buf->nr_frags;
834 
835 		for (j = 0; j < last; j++) {
836 			cons = NEXT_TX(cons);
837 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
838 			dma_unmap_page(
839 				&pdev->dev,
840 				dma_unmap_addr(tx_buf, mapping),
841 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
842 				DMA_TO_DEVICE);
843 		}
844 		if (unlikely(is_ts_pkt)) {
845 			if (BNXT_CHIP_P5(bp)) {
846 				/* PTP worker takes ownership of the skb */
847 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
848 				skb = NULL;
849 			}
850 		}
851 
852 next_tx_int:
853 		cons = NEXT_TX(cons);
854 
855 		dev_consume_skb_any(skb);
856 	}
857 
858 	WRITE_ONCE(txr->tx_cons, cons);
859 
860 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
861 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
862 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
863 
864 	return rc;
865 }
866 
867 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
868 {
869 	struct bnxt_tx_ring_info *txr;
870 	bool more = false;
871 	int i;
872 
873 	bnxt_for_each_napi_tx(i, bnapi, txr) {
874 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
875 			more |= __bnxt_tx_int(bp, txr, budget);
876 	}
877 	if (!more)
878 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
879 }
880 
881 static bool bnxt_separate_head_pool(void)
882 {
883 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
884 }
885 
886 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
887 					 struct bnxt_rx_ring_info *rxr,
888 					 unsigned int *offset,
889 					 gfp_t gfp)
890 {
891 	struct page *page;
892 
893 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
894 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
895 						BNXT_RX_PAGE_SIZE);
896 	} else {
897 		page = page_pool_dev_alloc_pages(rxr->page_pool);
898 		*offset = 0;
899 	}
900 	if (!page)
901 		return NULL;
902 
903 	*mapping = page_pool_get_dma_addr(page) + *offset;
904 	return page;
905 }
906 
907 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
908 				       struct bnxt_rx_ring_info *rxr,
909 				       gfp_t gfp)
910 {
911 	unsigned int offset;
912 	struct page *page;
913 
914 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
915 				    bp->rx_buf_size, gfp);
916 	if (!page)
917 		return NULL;
918 
919 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
920 	return page_address(page) + offset;
921 }
922 
923 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
924 		       u16 prod, gfp_t gfp)
925 {
926 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
927 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
928 	dma_addr_t mapping;
929 
930 	if (BNXT_RX_PAGE_MODE(bp)) {
931 		unsigned int offset;
932 		struct page *page =
933 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
934 
935 		if (!page)
936 			return -ENOMEM;
937 
938 		mapping += bp->rx_dma_offset;
939 		rx_buf->data = page;
940 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
941 	} else {
942 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
943 
944 		if (!data)
945 			return -ENOMEM;
946 
947 		rx_buf->data = data;
948 		rx_buf->data_ptr = data + bp->rx_offset;
949 	}
950 	rx_buf->mapping = mapping;
951 
952 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
953 	return 0;
954 }
955 
956 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
957 {
958 	u16 prod = rxr->rx_prod;
959 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
960 	struct bnxt *bp = rxr->bnapi->bp;
961 	struct rx_bd *cons_bd, *prod_bd;
962 
963 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
964 	cons_rx_buf = &rxr->rx_buf_ring[cons];
965 
966 	prod_rx_buf->data = data;
967 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
968 
969 	prod_rx_buf->mapping = cons_rx_buf->mapping;
970 
971 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
972 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
973 
974 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
975 }
976 
977 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
978 {
979 	u16 next, max = rxr->rx_agg_bmap_size;
980 
981 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
982 	if (next >= max)
983 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
984 	return next;
985 }
986 
987 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
988 				     struct bnxt_rx_ring_info *rxr,
989 				     u16 prod, gfp_t gfp)
990 {
991 	struct rx_bd *rxbd =
992 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
993 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
994 	struct page *page;
995 	dma_addr_t mapping;
996 	u16 sw_prod = rxr->rx_sw_agg_prod;
997 	unsigned int offset = 0;
998 
999 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1000 
1001 	if (!page)
1002 		return -ENOMEM;
1003 
1004 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1005 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1006 
1007 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1008 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1009 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1010 
1011 	rx_agg_buf->page = page;
1012 	rx_agg_buf->offset = offset;
1013 	rx_agg_buf->mapping = mapping;
1014 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1015 	rxbd->rx_bd_opaque = sw_prod;
1016 	return 0;
1017 }
1018 
1019 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1020 				       struct bnxt_cp_ring_info *cpr,
1021 				       u16 cp_cons, u16 curr)
1022 {
1023 	struct rx_agg_cmp *agg;
1024 
1025 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1026 	agg = (struct rx_agg_cmp *)
1027 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1028 	return agg;
1029 }
1030 
1031 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1032 					      struct bnxt_rx_ring_info *rxr,
1033 					      u16 agg_id, u16 curr)
1034 {
1035 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1036 
1037 	return &tpa_info->agg_arr[curr];
1038 }
1039 
1040 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1041 				   u16 start, u32 agg_bufs, bool tpa)
1042 {
1043 	struct bnxt_napi *bnapi = cpr->bnapi;
1044 	struct bnxt *bp = bnapi->bp;
1045 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1046 	u16 prod = rxr->rx_agg_prod;
1047 	u16 sw_prod = rxr->rx_sw_agg_prod;
1048 	bool p5_tpa = false;
1049 	u32 i;
1050 
1051 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1052 		p5_tpa = true;
1053 
1054 	for (i = 0; i < agg_bufs; i++) {
1055 		u16 cons;
1056 		struct rx_agg_cmp *agg;
1057 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1058 		struct rx_bd *prod_bd;
1059 		struct page *page;
1060 
1061 		if (p5_tpa)
1062 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1063 		else
1064 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1065 		cons = agg->rx_agg_cmp_opaque;
1066 		__clear_bit(cons, rxr->rx_agg_bmap);
1067 
1068 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1069 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1070 
1071 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1072 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1073 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1074 
1075 		/* It is possible for sw_prod to be equal to cons, so
1076 		 * set cons_rx_buf->page to NULL first.
1077 		 */
1078 		page = cons_rx_buf->page;
1079 		cons_rx_buf->page = NULL;
1080 		prod_rx_buf->page = page;
1081 		prod_rx_buf->offset = cons_rx_buf->offset;
1082 
1083 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1084 
1085 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1086 
1087 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1088 		prod_bd->rx_bd_opaque = sw_prod;
1089 
1090 		prod = NEXT_RX_AGG(prod);
1091 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1092 	}
1093 	rxr->rx_agg_prod = prod;
1094 	rxr->rx_sw_agg_prod = sw_prod;
1095 }
1096 
1097 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1098 					      struct bnxt_rx_ring_info *rxr,
1099 					      u16 cons, void *data, u8 *data_ptr,
1100 					      dma_addr_t dma_addr,
1101 					      unsigned int offset_and_len)
1102 {
1103 	unsigned int len = offset_and_len & 0xffff;
1104 	struct page *page = data;
1105 	u16 prod = rxr->rx_prod;
1106 	struct sk_buff *skb;
1107 	int err;
1108 
1109 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1110 	if (unlikely(err)) {
1111 		bnxt_reuse_rx_data(rxr, cons, data);
1112 		return NULL;
1113 	}
1114 	dma_addr -= bp->rx_dma_offset;
1115 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1116 				bp->rx_dir);
1117 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1118 	if (!skb) {
1119 		page_pool_recycle_direct(rxr->page_pool, page);
1120 		return NULL;
1121 	}
1122 	skb_mark_for_recycle(skb);
1123 	skb_reserve(skb, bp->rx_offset);
1124 	__skb_put(skb, len);
1125 
1126 	return skb;
1127 }
1128 
1129 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1130 					struct bnxt_rx_ring_info *rxr,
1131 					u16 cons, void *data, u8 *data_ptr,
1132 					dma_addr_t dma_addr,
1133 					unsigned int offset_and_len)
1134 {
1135 	unsigned int payload = offset_and_len >> 16;
1136 	unsigned int len = offset_and_len & 0xffff;
1137 	skb_frag_t *frag;
1138 	struct page *page = data;
1139 	u16 prod = rxr->rx_prod;
1140 	struct sk_buff *skb;
1141 	int off, err;
1142 
1143 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1144 	if (unlikely(err)) {
1145 		bnxt_reuse_rx_data(rxr, cons, data);
1146 		return NULL;
1147 	}
1148 	dma_addr -= bp->rx_dma_offset;
1149 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1150 				bp->rx_dir);
1151 
1152 	if (unlikely(!payload))
1153 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1154 
1155 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1156 	if (!skb) {
1157 		page_pool_recycle_direct(rxr->page_pool, page);
1158 		return NULL;
1159 	}
1160 
1161 	skb_mark_for_recycle(skb);
1162 	off = (void *)data_ptr - page_address(page);
1163 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1164 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1165 	       payload + NET_IP_ALIGN);
1166 
1167 	frag = &skb_shinfo(skb)->frags[0];
1168 	skb_frag_size_sub(frag, payload);
1169 	skb_frag_off_add(frag, payload);
1170 	skb->data_len -= payload;
1171 	skb->tail += payload;
1172 
1173 	return skb;
1174 }
1175 
1176 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1177 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1178 				   void *data, u8 *data_ptr,
1179 				   dma_addr_t dma_addr,
1180 				   unsigned int offset_and_len)
1181 {
1182 	u16 prod = rxr->rx_prod;
1183 	struct sk_buff *skb;
1184 	int err;
1185 
1186 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1187 	if (unlikely(err)) {
1188 		bnxt_reuse_rx_data(rxr, cons, data);
1189 		return NULL;
1190 	}
1191 
1192 	skb = napi_build_skb(data, bp->rx_buf_size);
1193 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1194 				bp->rx_dir);
1195 	if (!skb) {
1196 		page_pool_free_va(rxr->head_pool, data, true);
1197 		return NULL;
1198 	}
1199 
1200 	skb_mark_for_recycle(skb);
1201 	skb_reserve(skb, bp->rx_offset);
1202 	skb_put(skb, offset_and_len & 0xffff);
1203 	return skb;
1204 }
1205 
1206 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1207 			       struct bnxt_cp_ring_info *cpr,
1208 			       struct skb_shared_info *shinfo,
1209 			       u16 idx, u32 agg_bufs, bool tpa,
1210 			       struct xdp_buff *xdp)
1211 {
1212 	struct bnxt_napi *bnapi = cpr->bnapi;
1213 	struct pci_dev *pdev = bp->pdev;
1214 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1215 	u16 prod = rxr->rx_agg_prod;
1216 	u32 i, total_frag_len = 0;
1217 	bool p5_tpa = false;
1218 
1219 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1220 		p5_tpa = true;
1221 
1222 	for (i = 0; i < agg_bufs; i++) {
1223 		skb_frag_t *frag = &shinfo->frags[i];
1224 		u16 cons, frag_len;
1225 		struct rx_agg_cmp *agg;
1226 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1227 		struct page *page;
1228 		dma_addr_t mapping;
1229 
1230 		if (p5_tpa)
1231 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1232 		else
1233 			agg = bnxt_get_agg(bp, cpr, idx, i);
1234 		cons = agg->rx_agg_cmp_opaque;
1235 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1236 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1237 
1238 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1239 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1240 					cons_rx_buf->offset, frag_len);
1241 		shinfo->nr_frags = i + 1;
1242 		__clear_bit(cons, rxr->rx_agg_bmap);
1243 
1244 		/* It is possible for bnxt_alloc_rx_page() to allocate
1245 		 * a sw_prod index that equals the cons index, so we
1246 		 * need to clear the cons entry now.
1247 		 */
1248 		mapping = cons_rx_buf->mapping;
1249 		page = cons_rx_buf->page;
1250 		cons_rx_buf->page = NULL;
1251 
1252 		if (xdp && page_is_pfmemalloc(page))
1253 			xdp_buff_set_frag_pfmemalloc(xdp);
1254 
1255 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1256 			--shinfo->nr_frags;
1257 			cons_rx_buf->page = page;
1258 
1259 			/* Update prod since possibly some pages have been
1260 			 * allocated already.
1261 			 */
1262 			rxr->rx_agg_prod = prod;
1263 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1264 			return 0;
1265 		}
1266 
1267 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1268 					bp->rx_dir);
1269 
1270 		total_frag_len += frag_len;
1271 		prod = NEXT_RX_AGG(prod);
1272 	}
1273 	rxr->rx_agg_prod = prod;
1274 	return total_frag_len;
1275 }
1276 
1277 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1278 					     struct bnxt_cp_ring_info *cpr,
1279 					     struct sk_buff *skb, u16 idx,
1280 					     u32 agg_bufs, bool tpa)
1281 {
1282 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1283 	u32 total_frag_len = 0;
1284 
1285 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1286 					     agg_bufs, tpa, NULL);
1287 	if (!total_frag_len) {
1288 		skb_mark_for_recycle(skb);
1289 		dev_kfree_skb(skb);
1290 		return NULL;
1291 	}
1292 
1293 	skb->data_len += total_frag_len;
1294 	skb->len += total_frag_len;
1295 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1296 	return skb;
1297 }
1298 
1299 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1300 				 struct bnxt_cp_ring_info *cpr,
1301 				 struct xdp_buff *xdp, u16 idx,
1302 				 u32 agg_bufs, bool tpa)
1303 {
1304 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1305 	u32 total_frag_len = 0;
1306 
1307 	if (!xdp_buff_has_frags(xdp))
1308 		shinfo->nr_frags = 0;
1309 
1310 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1311 					     idx, agg_bufs, tpa, xdp);
1312 	if (total_frag_len) {
1313 		xdp_buff_set_frags_flag(xdp);
1314 		shinfo->nr_frags = agg_bufs;
1315 		shinfo->xdp_frags_size = total_frag_len;
1316 	}
1317 	return total_frag_len;
1318 }
1319 
1320 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1321 			       u8 agg_bufs, u32 *raw_cons)
1322 {
1323 	u16 last;
1324 	struct rx_agg_cmp *agg;
1325 
1326 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1327 	last = RING_CMP(*raw_cons);
1328 	agg = (struct rx_agg_cmp *)
1329 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1330 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1331 }
1332 
1333 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1334 				      unsigned int len,
1335 				      dma_addr_t mapping)
1336 {
1337 	struct bnxt *bp = bnapi->bp;
1338 	struct pci_dev *pdev = bp->pdev;
1339 	struct sk_buff *skb;
1340 
1341 	skb = napi_alloc_skb(&bnapi->napi, len);
1342 	if (!skb)
1343 		return NULL;
1344 
1345 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copybreak,
1346 				bp->rx_dir);
1347 
1348 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1349 	       len + NET_IP_ALIGN);
1350 
1351 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copybreak,
1352 				   bp->rx_dir);
1353 
1354 	skb_put(skb, len);
1355 
1356 	return skb;
1357 }
1358 
1359 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1360 				     unsigned int len,
1361 				     dma_addr_t mapping)
1362 {
1363 	return bnxt_copy_data(bnapi, data, len, mapping);
1364 }
1365 
1366 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1367 				     struct xdp_buff *xdp,
1368 				     unsigned int len,
1369 				     dma_addr_t mapping)
1370 {
1371 	unsigned int metasize = 0;
1372 	u8 *data = xdp->data;
1373 	struct sk_buff *skb;
1374 
1375 	len = xdp->data_end - xdp->data_meta;
1376 	metasize = xdp->data - xdp->data_meta;
1377 	data = xdp->data_meta;
1378 
1379 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1380 	if (!skb)
1381 		return skb;
1382 
1383 	if (metasize) {
1384 		skb_metadata_set(skb, metasize);
1385 		__skb_pull(skb, metasize);
1386 	}
1387 
1388 	return skb;
1389 }
1390 
1391 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1392 			   u32 *raw_cons, void *cmp)
1393 {
1394 	struct rx_cmp *rxcmp = cmp;
1395 	u32 tmp_raw_cons = *raw_cons;
1396 	u8 cmp_type, agg_bufs = 0;
1397 
1398 	cmp_type = RX_CMP_TYPE(rxcmp);
1399 
1400 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1401 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1402 			    RX_CMP_AGG_BUFS) >>
1403 			   RX_CMP_AGG_BUFS_SHIFT;
1404 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1405 		struct rx_tpa_end_cmp *tpa_end = cmp;
1406 
1407 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1408 			return 0;
1409 
1410 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1411 	}
1412 
1413 	if (agg_bufs) {
1414 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1415 			return -EBUSY;
1416 	}
1417 	*raw_cons = tmp_raw_cons;
1418 	return 0;
1419 }
1420 
1421 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1422 {
1423 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1424 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1425 
1426 	if (test_bit(idx, map->agg_idx_bmap))
1427 		idx = find_first_zero_bit(map->agg_idx_bmap,
1428 					  BNXT_AGG_IDX_BMAP_SIZE);
1429 	__set_bit(idx, map->agg_idx_bmap);
1430 	map->agg_id_tbl[agg_id] = idx;
1431 	return idx;
1432 }
1433 
1434 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1435 {
1436 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1437 
1438 	__clear_bit(idx, map->agg_idx_bmap);
1439 }
1440 
1441 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1442 {
1443 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1444 
1445 	return map->agg_id_tbl[agg_id];
1446 }
1447 
1448 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1449 			      struct rx_tpa_start_cmp *tpa_start,
1450 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1451 {
1452 	tpa_info->cfa_code_valid = 1;
1453 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1454 	tpa_info->vlan_valid = 0;
1455 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1456 		tpa_info->vlan_valid = 1;
1457 		tpa_info->metadata =
1458 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1459 	}
1460 }
1461 
1462 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1463 				 struct rx_tpa_start_cmp *tpa_start,
1464 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1465 {
1466 	tpa_info->vlan_valid = 0;
1467 	if (TPA_START_VLAN_VALID(tpa_start)) {
1468 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1469 		u32 vlan_proto = ETH_P_8021Q;
1470 
1471 		tpa_info->vlan_valid = 1;
1472 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1473 			vlan_proto = ETH_P_8021AD;
1474 		tpa_info->metadata = vlan_proto << 16 |
1475 				     TPA_START_METADATA0_TCI(tpa_start1);
1476 	}
1477 }
1478 
1479 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1480 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1481 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1482 {
1483 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1484 	struct bnxt_tpa_info *tpa_info;
1485 	u16 cons, prod, agg_id;
1486 	struct rx_bd *prod_bd;
1487 	dma_addr_t mapping;
1488 
1489 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1490 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1491 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1492 	} else {
1493 		agg_id = TPA_START_AGG_ID(tpa_start);
1494 	}
1495 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1496 	prod = rxr->rx_prod;
1497 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1498 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1499 	tpa_info = &rxr->rx_tpa[agg_id];
1500 
1501 	if (unlikely(cons != rxr->rx_next_cons ||
1502 		     TPA_START_ERROR(tpa_start))) {
1503 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1504 			    cons, rxr->rx_next_cons,
1505 			    TPA_START_ERROR_CODE(tpa_start1));
1506 		bnxt_sched_reset_rxr(bp, rxr);
1507 		return;
1508 	}
1509 	prod_rx_buf->data = tpa_info->data;
1510 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1511 
1512 	mapping = tpa_info->mapping;
1513 	prod_rx_buf->mapping = mapping;
1514 
1515 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1516 
1517 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1518 
1519 	tpa_info->data = cons_rx_buf->data;
1520 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1521 	cons_rx_buf->data = NULL;
1522 	tpa_info->mapping = cons_rx_buf->mapping;
1523 
1524 	tpa_info->len =
1525 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1526 				RX_TPA_START_CMP_LEN_SHIFT;
1527 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1528 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1529 		tpa_info->gso_type = SKB_GSO_TCPV4;
1530 		if (TPA_START_IS_IPV6(tpa_start1))
1531 			tpa_info->gso_type = SKB_GSO_TCPV6;
1532 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1533 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1534 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1535 			tpa_info->gso_type = SKB_GSO_TCPV6;
1536 		tpa_info->rss_hash =
1537 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1538 	} else {
1539 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1540 		tpa_info->gso_type = 0;
1541 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1542 	}
1543 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1544 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1545 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1546 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1547 	else
1548 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1549 	tpa_info->agg_count = 0;
1550 
1551 	rxr->rx_prod = NEXT_RX(prod);
1552 	cons = RING_RX(bp, NEXT_RX(cons));
1553 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1554 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1555 
1556 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1557 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1558 	cons_rx_buf->data = NULL;
1559 }
1560 
1561 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1562 {
1563 	if (agg_bufs)
1564 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1565 }
1566 
1567 #ifdef CONFIG_INET
1568 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1569 {
1570 	struct udphdr *uh = NULL;
1571 
1572 	if (ip_proto == htons(ETH_P_IP)) {
1573 		struct iphdr *iph = (struct iphdr *)skb->data;
1574 
1575 		if (iph->protocol == IPPROTO_UDP)
1576 			uh = (struct udphdr *)(iph + 1);
1577 	} else {
1578 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1579 
1580 		if (iph->nexthdr == IPPROTO_UDP)
1581 			uh = (struct udphdr *)(iph + 1);
1582 	}
1583 	if (uh) {
1584 		if (uh->check)
1585 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1586 		else
1587 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1588 	}
1589 }
1590 #endif
1591 
1592 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1593 					   int payload_off, int tcp_ts,
1594 					   struct sk_buff *skb)
1595 {
1596 #ifdef CONFIG_INET
1597 	struct tcphdr *th;
1598 	int len, nw_off;
1599 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1600 	u32 hdr_info = tpa_info->hdr_info;
1601 	bool loopback = false;
1602 
1603 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1604 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1605 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1606 
1607 	/* If the packet is an internal loopback packet, the offsets will
1608 	 * have an extra 4 bytes.
1609 	 */
1610 	if (inner_mac_off == 4) {
1611 		loopback = true;
1612 	} else if (inner_mac_off > 4) {
1613 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1614 					    ETH_HLEN - 2));
1615 
1616 		/* We only support inner iPv4/ipv6.  If we don't see the
1617 		 * correct protocol ID, it must be a loopback packet where
1618 		 * the offsets are off by 4.
1619 		 */
1620 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1621 			loopback = true;
1622 	}
1623 	if (loopback) {
1624 		/* internal loopback packet, subtract all offsets by 4 */
1625 		inner_ip_off -= 4;
1626 		inner_mac_off -= 4;
1627 		outer_ip_off -= 4;
1628 	}
1629 
1630 	nw_off = inner_ip_off - ETH_HLEN;
1631 	skb_set_network_header(skb, nw_off);
1632 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1633 		struct ipv6hdr *iph = ipv6_hdr(skb);
1634 
1635 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1636 		len = skb->len - skb_transport_offset(skb);
1637 		th = tcp_hdr(skb);
1638 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1639 	} else {
1640 		struct iphdr *iph = ip_hdr(skb);
1641 
1642 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1643 		len = skb->len - skb_transport_offset(skb);
1644 		th = tcp_hdr(skb);
1645 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1646 	}
1647 
1648 	if (inner_mac_off) { /* tunnel */
1649 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1650 					    ETH_HLEN - 2));
1651 
1652 		bnxt_gro_tunnel(skb, proto);
1653 	}
1654 #endif
1655 	return skb;
1656 }
1657 
1658 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1659 					   int payload_off, int tcp_ts,
1660 					   struct sk_buff *skb)
1661 {
1662 #ifdef CONFIG_INET
1663 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1664 	u32 hdr_info = tpa_info->hdr_info;
1665 	int iphdr_len, nw_off;
1666 
1667 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1668 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1669 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1670 
1671 	nw_off = inner_ip_off - ETH_HLEN;
1672 	skb_set_network_header(skb, nw_off);
1673 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1674 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1675 	skb_set_transport_header(skb, nw_off + iphdr_len);
1676 
1677 	if (inner_mac_off) { /* tunnel */
1678 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1679 					    ETH_HLEN - 2));
1680 
1681 		bnxt_gro_tunnel(skb, proto);
1682 	}
1683 #endif
1684 	return skb;
1685 }
1686 
1687 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1688 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1689 
1690 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1691 					   int payload_off, int tcp_ts,
1692 					   struct sk_buff *skb)
1693 {
1694 #ifdef CONFIG_INET
1695 	struct tcphdr *th;
1696 	int len, nw_off, tcp_opt_len = 0;
1697 
1698 	if (tcp_ts)
1699 		tcp_opt_len = 12;
1700 
1701 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1702 		struct iphdr *iph;
1703 
1704 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1705 			 ETH_HLEN;
1706 		skb_set_network_header(skb, nw_off);
1707 		iph = ip_hdr(skb);
1708 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1709 		len = skb->len - skb_transport_offset(skb);
1710 		th = tcp_hdr(skb);
1711 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1712 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1713 		struct ipv6hdr *iph;
1714 
1715 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1716 			 ETH_HLEN;
1717 		skb_set_network_header(skb, nw_off);
1718 		iph = ipv6_hdr(skb);
1719 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1720 		len = skb->len - skb_transport_offset(skb);
1721 		th = tcp_hdr(skb);
1722 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1723 	} else {
1724 		dev_kfree_skb_any(skb);
1725 		return NULL;
1726 	}
1727 
1728 	if (nw_off) /* tunnel */
1729 		bnxt_gro_tunnel(skb, skb->protocol);
1730 #endif
1731 	return skb;
1732 }
1733 
1734 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1735 					   struct bnxt_tpa_info *tpa_info,
1736 					   struct rx_tpa_end_cmp *tpa_end,
1737 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1738 					   struct sk_buff *skb)
1739 {
1740 #ifdef CONFIG_INET
1741 	int payload_off;
1742 	u16 segs;
1743 
1744 	segs = TPA_END_TPA_SEGS(tpa_end);
1745 	if (segs == 1)
1746 		return skb;
1747 
1748 	NAPI_GRO_CB(skb)->count = segs;
1749 	skb_shinfo(skb)->gso_size =
1750 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1751 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1752 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1753 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1754 	else
1755 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1756 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1757 	if (likely(skb))
1758 		tcp_gro_complete(skb);
1759 #endif
1760 	return skb;
1761 }
1762 
1763 /* Given the cfa_code of a received packet determine which
1764  * netdev (vf-rep or PF) the packet is destined to.
1765  */
1766 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1767 {
1768 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1769 
1770 	/* if vf-rep dev is NULL, the must belongs to the PF */
1771 	return dev ? dev : bp->dev;
1772 }
1773 
1774 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1775 					   struct bnxt_cp_ring_info *cpr,
1776 					   u32 *raw_cons,
1777 					   struct rx_tpa_end_cmp *tpa_end,
1778 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1779 					   u8 *event)
1780 {
1781 	struct bnxt_napi *bnapi = cpr->bnapi;
1782 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1783 	struct net_device *dev = bp->dev;
1784 	u8 *data_ptr, agg_bufs;
1785 	unsigned int len;
1786 	struct bnxt_tpa_info *tpa_info;
1787 	dma_addr_t mapping;
1788 	struct sk_buff *skb;
1789 	u16 idx = 0, agg_id;
1790 	void *data;
1791 	bool gro;
1792 
1793 	if (unlikely(bnapi->in_reset)) {
1794 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1795 
1796 		if (rc < 0)
1797 			return ERR_PTR(-EBUSY);
1798 		return NULL;
1799 	}
1800 
1801 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1802 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1803 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1804 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1805 		tpa_info = &rxr->rx_tpa[agg_id];
1806 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1807 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1808 				    agg_bufs, tpa_info->agg_count);
1809 			agg_bufs = tpa_info->agg_count;
1810 		}
1811 		tpa_info->agg_count = 0;
1812 		*event |= BNXT_AGG_EVENT;
1813 		bnxt_free_agg_idx(rxr, agg_id);
1814 		idx = agg_id;
1815 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1816 	} else {
1817 		agg_id = TPA_END_AGG_ID(tpa_end);
1818 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1819 		tpa_info = &rxr->rx_tpa[agg_id];
1820 		idx = RING_CMP(*raw_cons);
1821 		if (agg_bufs) {
1822 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1823 				return ERR_PTR(-EBUSY);
1824 
1825 			*event |= BNXT_AGG_EVENT;
1826 			idx = NEXT_CMP(idx);
1827 		}
1828 		gro = !!TPA_END_GRO(tpa_end);
1829 	}
1830 	data = tpa_info->data;
1831 	data_ptr = tpa_info->data_ptr;
1832 	prefetch(data_ptr);
1833 	len = tpa_info->len;
1834 	mapping = tpa_info->mapping;
1835 
1836 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1837 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1838 		if (agg_bufs > MAX_SKB_FRAGS)
1839 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1840 				    agg_bufs, (int)MAX_SKB_FRAGS);
1841 		return NULL;
1842 	}
1843 
1844 	if (len <= bp->rx_copybreak) {
1845 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1846 		if (!skb) {
1847 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1848 			cpr->sw_stats->rx.rx_oom_discards += 1;
1849 			return NULL;
1850 		}
1851 	} else {
1852 		u8 *new_data;
1853 		dma_addr_t new_mapping;
1854 
1855 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1856 						GFP_ATOMIC);
1857 		if (!new_data) {
1858 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1859 			cpr->sw_stats->rx.rx_oom_discards += 1;
1860 			return NULL;
1861 		}
1862 
1863 		tpa_info->data = new_data;
1864 		tpa_info->data_ptr = new_data + bp->rx_offset;
1865 		tpa_info->mapping = new_mapping;
1866 
1867 		skb = napi_build_skb(data, bp->rx_buf_size);
1868 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1869 					bp->rx_buf_use_size, bp->rx_dir);
1870 
1871 		if (!skb) {
1872 			page_pool_free_va(rxr->head_pool, data, true);
1873 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1874 			cpr->sw_stats->rx.rx_oom_discards += 1;
1875 			return NULL;
1876 		}
1877 		skb_mark_for_recycle(skb);
1878 		skb_reserve(skb, bp->rx_offset);
1879 		skb_put(skb, len);
1880 	}
1881 
1882 	if (agg_bufs) {
1883 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1884 		if (!skb) {
1885 			/* Page reuse already handled by bnxt_rx_pages(). */
1886 			cpr->sw_stats->rx.rx_oom_discards += 1;
1887 			return NULL;
1888 		}
1889 	}
1890 
1891 	if (tpa_info->cfa_code_valid)
1892 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1893 	skb->protocol = eth_type_trans(skb, dev);
1894 
1895 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1896 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1897 
1898 	if (tpa_info->vlan_valid &&
1899 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1900 		__be16 vlan_proto = htons(tpa_info->metadata >>
1901 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1902 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1903 
1904 		if (eth_type_vlan(vlan_proto)) {
1905 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1906 		} else {
1907 			dev_kfree_skb(skb);
1908 			return NULL;
1909 		}
1910 	}
1911 
1912 	skb_checksum_none_assert(skb);
1913 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1914 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1915 		skb->csum_level =
1916 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1917 	}
1918 
1919 	if (gro)
1920 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1921 
1922 	return skb;
1923 }
1924 
1925 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1926 			 struct rx_agg_cmp *rx_agg)
1927 {
1928 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1929 	struct bnxt_tpa_info *tpa_info;
1930 
1931 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1932 	tpa_info = &rxr->rx_tpa[agg_id];
1933 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1934 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1935 }
1936 
1937 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1938 			     struct sk_buff *skb)
1939 {
1940 	skb_mark_for_recycle(skb);
1941 
1942 	if (skb->dev != bp->dev) {
1943 		/* this packet belongs to a vf-rep */
1944 		bnxt_vf_rep_rx(bp, skb);
1945 		return;
1946 	}
1947 	skb_record_rx_queue(skb, bnapi->index);
1948 	napi_gro_receive(&bnapi->napi, skb);
1949 }
1950 
1951 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1952 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1953 {
1954 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1955 
1956 	if (BNXT_PTP_RX_TS_VALID(flags))
1957 		goto ts_valid;
1958 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1959 		return false;
1960 
1961 ts_valid:
1962 	*cmpl_ts = ts;
1963 	return true;
1964 }
1965 
1966 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1967 				    struct rx_cmp *rxcmp,
1968 				    struct rx_cmp_ext *rxcmp1)
1969 {
1970 	__be16 vlan_proto;
1971 	u16 vtag;
1972 
1973 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1974 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1975 		u32 meta_data;
1976 
1977 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1978 			return skb;
1979 
1980 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1981 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1982 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1983 		if (eth_type_vlan(vlan_proto))
1984 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1985 		else
1986 			goto vlan_err;
1987 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1988 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1989 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1990 
1991 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1992 				vlan_proto = htons(ETH_P_8021Q);
1993 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1994 				vlan_proto = htons(ETH_P_8021AD);
1995 			else
1996 				goto vlan_err;
1997 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1998 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1999 		}
2000 	}
2001 	return skb;
2002 vlan_err:
2003 	dev_kfree_skb(skb);
2004 	return NULL;
2005 }
2006 
2007 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2008 					   struct rx_cmp *rxcmp)
2009 {
2010 	u8 ext_op;
2011 
2012 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2013 	switch (ext_op) {
2014 	case EXT_OP_INNER_4:
2015 	case EXT_OP_OUTER_4:
2016 	case EXT_OP_INNFL_3:
2017 	case EXT_OP_OUTFL_3:
2018 		return PKT_HASH_TYPE_L4;
2019 	default:
2020 		return PKT_HASH_TYPE_L3;
2021 	}
2022 }
2023 
2024 /* returns the following:
2025  * 1       - 1 packet successfully received
2026  * 0       - successful TPA_START, packet not completed yet
2027  * -EBUSY  - completion ring does not have all the agg buffers yet
2028  * -ENOMEM - packet aborted due to out of memory
2029  * -EIO    - packet aborted due to hw error indicated in BD
2030  */
2031 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2032 		       u32 *raw_cons, u8 *event)
2033 {
2034 	struct bnxt_napi *bnapi = cpr->bnapi;
2035 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2036 	struct net_device *dev = bp->dev;
2037 	struct rx_cmp *rxcmp;
2038 	struct rx_cmp_ext *rxcmp1;
2039 	u32 tmp_raw_cons = *raw_cons;
2040 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2041 	struct bnxt_sw_rx_bd *rx_buf;
2042 	unsigned int len;
2043 	u8 *data_ptr, agg_bufs, cmp_type;
2044 	bool xdp_active = false;
2045 	dma_addr_t dma_addr;
2046 	struct sk_buff *skb;
2047 	struct xdp_buff xdp;
2048 	u32 flags, misc;
2049 	u32 cmpl_ts;
2050 	void *data;
2051 	int rc = 0;
2052 
2053 	rxcmp = (struct rx_cmp *)
2054 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2055 
2056 	cmp_type = RX_CMP_TYPE(rxcmp);
2057 
2058 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2059 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2060 		goto next_rx_no_prod_no_len;
2061 	}
2062 
2063 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2064 	cp_cons = RING_CMP(tmp_raw_cons);
2065 	rxcmp1 = (struct rx_cmp_ext *)
2066 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2067 
2068 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2069 		return -EBUSY;
2070 
2071 	/* The valid test of the entry must be done first before
2072 	 * reading any further.
2073 	 */
2074 	dma_rmb();
2075 	prod = rxr->rx_prod;
2076 
2077 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2078 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2079 		bnxt_tpa_start(bp, rxr, cmp_type,
2080 			       (struct rx_tpa_start_cmp *)rxcmp,
2081 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2082 
2083 		*event |= BNXT_RX_EVENT;
2084 		goto next_rx_no_prod_no_len;
2085 
2086 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2087 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2088 				   (struct rx_tpa_end_cmp *)rxcmp,
2089 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2090 
2091 		if (IS_ERR(skb))
2092 			return -EBUSY;
2093 
2094 		rc = -ENOMEM;
2095 		if (likely(skb)) {
2096 			bnxt_deliver_skb(bp, bnapi, skb);
2097 			rc = 1;
2098 		}
2099 		*event |= BNXT_RX_EVENT;
2100 		goto next_rx_no_prod_no_len;
2101 	}
2102 
2103 	cons = rxcmp->rx_cmp_opaque;
2104 	if (unlikely(cons != rxr->rx_next_cons)) {
2105 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2106 
2107 		/* 0xffff is forced error, don't print it */
2108 		if (rxr->rx_next_cons != 0xffff)
2109 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2110 				    cons, rxr->rx_next_cons);
2111 		bnxt_sched_reset_rxr(bp, rxr);
2112 		if (rc1)
2113 			return rc1;
2114 		goto next_rx_no_prod_no_len;
2115 	}
2116 	rx_buf = &rxr->rx_buf_ring[cons];
2117 	data = rx_buf->data;
2118 	data_ptr = rx_buf->data_ptr;
2119 	prefetch(data_ptr);
2120 
2121 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2122 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2123 
2124 	if (agg_bufs) {
2125 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2126 			return -EBUSY;
2127 
2128 		cp_cons = NEXT_CMP(cp_cons);
2129 		*event |= BNXT_AGG_EVENT;
2130 	}
2131 	*event |= BNXT_RX_EVENT;
2132 
2133 	rx_buf->data = NULL;
2134 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2135 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2136 
2137 		bnxt_reuse_rx_data(rxr, cons, data);
2138 		if (agg_bufs)
2139 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2140 					       false);
2141 
2142 		rc = -EIO;
2143 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2144 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2145 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2146 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2147 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2148 						 rx_err);
2149 				bnxt_sched_reset_rxr(bp, rxr);
2150 			}
2151 		}
2152 		goto next_rx_no_len;
2153 	}
2154 
2155 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2156 	len = flags >> RX_CMP_LEN_SHIFT;
2157 	dma_addr = rx_buf->mapping;
2158 
2159 	if (bnxt_xdp_attached(bp, rxr)) {
2160 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2161 		if (agg_bufs) {
2162 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2163 							     cp_cons, agg_bufs,
2164 							     false);
2165 			if (!frag_len)
2166 				goto oom_next_rx;
2167 		}
2168 		xdp_active = true;
2169 	}
2170 
2171 	if (xdp_active) {
2172 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2173 			rc = 1;
2174 			goto next_rx;
2175 		}
2176 	}
2177 
2178 	if (len <= bp->rx_copybreak) {
2179 		if (!xdp_active)
2180 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2181 		else
2182 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2183 		bnxt_reuse_rx_data(rxr, cons, data);
2184 		if (!skb) {
2185 			if (agg_bufs) {
2186 				if (!xdp_active)
2187 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2188 							       agg_bufs, false);
2189 				else
2190 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2191 			}
2192 			goto oom_next_rx;
2193 		}
2194 	} else {
2195 		u32 payload;
2196 
2197 		if (rx_buf->data_ptr == data_ptr)
2198 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2199 		else
2200 			payload = 0;
2201 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2202 				      payload | len);
2203 		if (!skb)
2204 			goto oom_next_rx;
2205 	}
2206 
2207 	if (agg_bufs) {
2208 		if (!xdp_active) {
2209 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2210 			if (!skb)
2211 				goto oom_next_rx;
2212 		} else {
2213 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2214 			if (!skb) {
2215 				/* we should be able to free the old skb here */
2216 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2217 				goto oom_next_rx;
2218 			}
2219 		}
2220 	}
2221 
2222 	if (RX_CMP_HASH_VALID(rxcmp)) {
2223 		enum pkt_hash_types type;
2224 
2225 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2226 			type = bnxt_rss_ext_op(bp, rxcmp);
2227 		} else {
2228 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2229 
2230 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2231 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2232 				type = PKT_HASH_TYPE_L4;
2233 			else
2234 				type = PKT_HASH_TYPE_L3;
2235 		}
2236 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2237 	}
2238 
2239 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2240 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2241 	skb->protocol = eth_type_trans(skb, dev);
2242 
2243 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2244 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2245 		if (!skb)
2246 			goto next_rx;
2247 	}
2248 
2249 	skb_checksum_none_assert(skb);
2250 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2251 		if (dev->features & NETIF_F_RXCSUM) {
2252 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2253 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2254 		}
2255 	} else {
2256 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2257 			if (dev->features & NETIF_F_RXCSUM)
2258 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2259 		}
2260 	}
2261 
2262 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2263 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2264 			u64 ns, ts;
2265 
2266 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2267 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2268 
2269 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2270 				memset(skb_hwtstamps(skb), 0,
2271 				       sizeof(*skb_hwtstamps(skb)));
2272 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2273 			}
2274 		}
2275 	}
2276 	bnxt_deliver_skb(bp, bnapi, skb);
2277 	rc = 1;
2278 
2279 next_rx:
2280 	cpr->rx_packets += 1;
2281 	cpr->rx_bytes += len;
2282 
2283 next_rx_no_len:
2284 	rxr->rx_prod = NEXT_RX(prod);
2285 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2286 
2287 next_rx_no_prod_no_len:
2288 	*raw_cons = tmp_raw_cons;
2289 
2290 	return rc;
2291 
2292 oom_next_rx:
2293 	cpr->sw_stats->rx.rx_oom_discards += 1;
2294 	rc = -ENOMEM;
2295 	goto next_rx;
2296 }
2297 
2298 /* In netpoll mode, if we are using a combined completion ring, we need to
2299  * discard the rx packets and recycle the buffers.
2300  */
2301 static int bnxt_force_rx_discard(struct bnxt *bp,
2302 				 struct bnxt_cp_ring_info *cpr,
2303 				 u32 *raw_cons, u8 *event)
2304 {
2305 	u32 tmp_raw_cons = *raw_cons;
2306 	struct rx_cmp_ext *rxcmp1;
2307 	struct rx_cmp *rxcmp;
2308 	u16 cp_cons;
2309 	u8 cmp_type;
2310 	int rc;
2311 
2312 	cp_cons = RING_CMP(tmp_raw_cons);
2313 	rxcmp = (struct rx_cmp *)
2314 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2315 
2316 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2317 	cp_cons = RING_CMP(tmp_raw_cons);
2318 	rxcmp1 = (struct rx_cmp_ext *)
2319 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2320 
2321 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2322 		return -EBUSY;
2323 
2324 	/* The valid test of the entry must be done first before
2325 	 * reading any further.
2326 	 */
2327 	dma_rmb();
2328 	cmp_type = RX_CMP_TYPE(rxcmp);
2329 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2330 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2331 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2332 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2333 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2334 		struct rx_tpa_end_cmp_ext *tpa_end1;
2335 
2336 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2337 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2338 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2339 	}
2340 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2341 	if (rc && rc != -EBUSY)
2342 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2343 	return rc;
2344 }
2345 
2346 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2347 {
2348 	struct bnxt_fw_health *fw_health = bp->fw_health;
2349 	u32 reg = fw_health->regs[reg_idx];
2350 	u32 reg_type, reg_off, val = 0;
2351 
2352 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2353 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2354 	switch (reg_type) {
2355 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2356 		pci_read_config_dword(bp->pdev, reg_off, &val);
2357 		break;
2358 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2359 		reg_off = fw_health->mapped_regs[reg_idx];
2360 		fallthrough;
2361 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2362 		val = readl(bp->bar0 + reg_off);
2363 		break;
2364 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2365 		val = readl(bp->bar1 + reg_off);
2366 		break;
2367 	}
2368 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2369 		val &= fw_health->fw_reset_inprog_reg_mask;
2370 	return val;
2371 }
2372 
2373 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2374 {
2375 	int i;
2376 
2377 	for (i = 0; i < bp->rx_nr_rings; i++) {
2378 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2379 		struct bnxt_ring_grp_info *grp_info;
2380 
2381 		grp_info = &bp->grp_info[grp_idx];
2382 		if (grp_info->agg_fw_ring_id == ring_id)
2383 			return grp_idx;
2384 	}
2385 	return INVALID_HW_RING_ID;
2386 }
2387 
2388 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2389 {
2390 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2391 
2392 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2393 		return link_info->force_link_speed2;
2394 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2395 		return link_info->force_pam4_link_speed;
2396 	return link_info->force_link_speed;
2397 }
2398 
2399 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2400 {
2401 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2402 
2403 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2404 		link_info->req_link_speed = link_info->force_link_speed2;
2405 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2406 		switch (link_info->req_link_speed) {
2407 		case BNXT_LINK_SPEED_50GB_PAM4:
2408 		case BNXT_LINK_SPEED_100GB_PAM4:
2409 		case BNXT_LINK_SPEED_200GB_PAM4:
2410 		case BNXT_LINK_SPEED_400GB_PAM4:
2411 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2412 			break;
2413 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2414 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2415 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2416 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2417 			break;
2418 		default:
2419 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2420 		}
2421 		return;
2422 	}
2423 	link_info->req_link_speed = link_info->force_link_speed;
2424 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2425 	if (link_info->force_pam4_link_speed) {
2426 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2427 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2428 	}
2429 }
2430 
2431 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2432 {
2433 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2434 
2435 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2436 		link_info->advertising = link_info->auto_link_speeds2;
2437 		return;
2438 	}
2439 	link_info->advertising = link_info->auto_link_speeds;
2440 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2441 }
2442 
2443 static bool bnxt_force_speed_updated(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 		if (link_info->req_link_speed != link_info->force_link_speed2)
2449 			return true;
2450 		return false;
2451 	}
2452 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2453 	    link_info->req_link_speed != link_info->force_link_speed)
2454 		return true;
2455 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2456 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2457 		return true;
2458 	return false;
2459 }
2460 
2461 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2462 {
2463 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2464 
2465 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2466 		if (link_info->advertising != link_info->auto_link_speeds2)
2467 			return true;
2468 		return false;
2469 	}
2470 	if (link_info->advertising != link_info->auto_link_speeds ||
2471 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2472 		return true;
2473 	return false;
2474 }
2475 
2476 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2477 {
2478 	u32 flags = bp->ctx->ctx_arr[type].flags;
2479 
2480 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2481 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2482 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2483 }
2484 
2485 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2486 {
2487 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2488 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2489 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2490 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2491 	struct bnxt_bs_trace_info *bs_trace;
2492 	int last_pg;
2493 
2494 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2495 		return;
2496 
2497 	mem_size = ctxm->max_entries * ctxm->entry_size;
2498 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2499 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2500 
2501 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2502 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2503 
2504 	rmem = &ctx_pg[0].ring_mem;
2505 	bs_trace = &bp->bs_trace[trace_type];
2506 	bs_trace->ctx_type = ctxm->type;
2507 	bs_trace->trace_type = trace_type;
2508 	if (pages > MAX_CTX_PAGES) {
2509 		int last_pg_dir = rmem->nr_pages - 1;
2510 
2511 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2512 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2513 	} else {
2514 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2515 	}
2516 	bs_trace->magic_byte += magic_byte_offset;
2517 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2518 }
2519 
2520 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2521 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2522 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2523 
2524 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2525 	(((data2) &							\
2526 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2527 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2528 
2529 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2530 	((data2) &							\
2531 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2532 
2533 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2534 	(((data2) &							\
2535 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2536 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2537 
2538 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2539 	((data1) &							\
2540 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2541 
2542 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2543 	(((data1) &							\
2544 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2545 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2546 
2547 /* Return true if the workqueue has to be scheduled */
2548 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2549 {
2550 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2551 
2552 	switch (err_type) {
2553 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2554 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2555 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2556 		break;
2557 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2558 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2559 		break;
2560 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2561 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2562 		break;
2563 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2564 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2565 		char *threshold_type;
2566 		bool notify = false;
2567 		char *dir_str;
2568 
2569 		switch (type) {
2570 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2571 			threshold_type = "warning";
2572 			break;
2573 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2574 			threshold_type = "critical";
2575 			break;
2576 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2577 			threshold_type = "fatal";
2578 			break;
2579 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2580 			threshold_type = "shutdown";
2581 			break;
2582 		default:
2583 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2584 			return false;
2585 		}
2586 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2587 			dir_str = "above";
2588 			notify = true;
2589 		} else {
2590 			dir_str = "below";
2591 		}
2592 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2593 			    dir_str, threshold_type);
2594 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2595 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2596 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2597 		if (notify) {
2598 			bp->thermal_threshold_type = type;
2599 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2600 			return true;
2601 		}
2602 		return false;
2603 	}
2604 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2605 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2606 		break;
2607 	default:
2608 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2609 			   err_type);
2610 		break;
2611 	}
2612 	return false;
2613 }
2614 
2615 #define BNXT_GET_EVENT_PORT(data)	\
2616 	((data) &			\
2617 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2618 
2619 #define BNXT_EVENT_RING_TYPE(data2)	\
2620 	((data2) &			\
2621 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2622 
2623 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2624 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2625 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2626 
2627 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2628 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2629 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2630 
2631 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2632 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2633 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2634 
2635 #define BNXT_PHC_BITS	48
2636 
2637 static int bnxt_async_event_process(struct bnxt *bp,
2638 				    struct hwrm_async_event_cmpl *cmpl)
2639 {
2640 	u16 event_id = le16_to_cpu(cmpl->event_id);
2641 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2642 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2643 
2644 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2645 		   event_id, data1, data2);
2646 
2647 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2648 	switch (event_id) {
2649 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2650 		struct bnxt_link_info *link_info = &bp->link_info;
2651 
2652 		if (BNXT_VF(bp))
2653 			goto async_event_process_exit;
2654 
2655 		/* print unsupported speed warning in forced speed mode only */
2656 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2657 		    (data1 & 0x20000)) {
2658 			u16 fw_speed = bnxt_get_force_speed(link_info);
2659 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2660 
2661 			if (speed != SPEED_UNKNOWN)
2662 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2663 					    speed);
2664 		}
2665 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2666 	}
2667 		fallthrough;
2668 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2669 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2670 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2671 		fallthrough;
2672 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2673 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2674 		break;
2675 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2676 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2677 		break;
2678 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2679 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2680 
2681 		if (BNXT_VF(bp))
2682 			break;
2683 
2684 		if (bp->pf.port_id != port_id)
2685 			break;
2686 
2687 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2688 		break;
2689 	}
2690 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2691 		if (BNXT_PF(bp))
2692 			goto async_event_process_exit;
2693 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2694 		break;
2695 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2696 		char *type_str = "Solicited";
2697 
2698 		if (!bp->fw_health)
2699 			goto async_event_process_exit;
2700 
2701 		bp->fw_reset_timestamp = jiffies;
2702 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2703 		if (!bp->fw_reset_min_dsecs)
2704 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2705 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2706 		if (!bp->fw_reset_max_dsecs)
2707 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2708 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2709 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2710 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2711 			type_str = "Fatal";
2712 			bp->fw_health->fatalities++;
2713 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2714 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2715 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2716 			type_str = "Non-fatal";
2717 			bp->fw_health->survivals++;
2718 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2719 		}
2720 		netif_warn(bp, hw, bp->dev,
2721 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2722 			   type_str, data1, data2,
2723 			   bp->fw_reset_min_dsecs * 100,
2724 			   bp->fw_reset_max_dsecs * 100);
2725 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2726 		break;
2727 	}
2728 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2729 		struct bnxt_fw_health *fw_health = bp->fw_health;
2730 		char *status_desc = "healthy";
2731 		u32 status;
2732 
2733 		if (!fw_health)
2734 			goto async_event_process_exit;
2735 
2736 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2737 			fw_health->enabled = false;
2738 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2739 			break;
2740 		}
2741 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2742 		fw_health->tmr_multiplier =
2743 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2744 				     bp->current_interval * 10);
2745 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2746 		if (!fw_health->enabled)
2747 			fw_health->last_fw_heartbeat =
2748 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2749 		fw_health->last_fw_reset_cnt =
2750 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2751 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2752 		if (status != BNXT_FW_STATUS_HEALTHY)
2753 			status_desc = "unhealthy";
2754 		netif_info(bp, drv, bp->dev,
2755 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2756 			   fw_health->primary ? "primary" : "backup", status,
2757 			   status_desc, fw_health->last_fw_reset_cnt);
2758 		if (!fw_health->enabled) {
2759 			/* Make sure tmr_counter is set and visible to
2760 			 * bnxt_health_check() before setting enabled to true.
2761 			 */
2762 			smp_wmb();
2763 			fw_health->enabled = true;
2764 		}
2765 		goto async_event_process_exit;
2766 	}
2767 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2768 		netif_notice(bp, hw, bp->dev,
2769 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2770 			     data1, data2);
2771 		goto async_event_process_exit;
2772 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2773 		struct bnxt_rx_ring_info *rxr;
2774 		u16 grp_idx;
2775 
2776 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2777 			goto async_event_process_exit;
2778 
2779 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2780 			    BNXT_EVENT_RING_TYPE(data2), data1);
2781 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2782 			goto async_event_process_exit;
2783 
2784 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2785 		if (grp_idx == INVALID_HW_RING_ID) {
2786 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2787 				    data1);
2788 			goto async_event_process_exit;
2789 		}
2790 		rxr = bp->bnapi[grp_idx]->rx_ring;
2791 		bnxt_sched_reset_rxr(bp, rxr);
2792 		goto async_event_process_exit;
2793 	}
2794 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2795 		struct bnxt_fw_health *fw_health = bp->fw_health;
2796 
2797 		netif_notice(bp, hw, bp->dev,
2798 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2799 			     data1, data2);
2800 		if (fw_health) {
2801 			fw_health->echo_req_data1 = data1;
2802 			fw_health->echo_req_data2 = data2;
2803 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2804 			break;
2805 		}
2806 		goto async_event_process_exit;
2807 	}
2808 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2809 		bnxt_ptp_pps_event(bp, data1, data2);
2810 		goto async_event_process_exit;
2811 	}
2812 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2813 		if (bnxt_event_error_report(bp, data1, data2))
2814 			break;
2815 		goto async_event_process_exit;
2816 	}
2817 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2818 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2819 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2820 			if (BNXT_PTP_USE_RTC(bp)) {
2821 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2822 				unsigned long flags;
2823 				u64 ns;
2824 
2825 				if (!ptp)
2826 					goto async_event_process_exit;
2827 
2828 				bnxt_ptp_update_current_time(bp);
2829 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2830 				       BNXT_PHC_BITS) | ptp->current_time);
2831 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2832 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2833 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2834 			}
2835 			break;
2836 		}
2837 		goto async_event_process_exit;
2838 	}
2839 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2840 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2841 
2842 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2843 		goto async_event_process_exit;
2844 	}
2845 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2846 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2847 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2848 
2849 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2850 		goto async_event_process_exit;
2851 	}
2852 	default:
2853 		goto async_event_process_exit;
2854 	}
2855 	__bnxt_queue_sp_work(bp);
2856 async_event_process_exit:
2857 	bnxt_ulp_async_events(bp, cmpl);
2858 	return 0;
2859 }
2860 
2861 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2862 {
2863 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2864 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2865 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2866 				(struct hwrm_fwd_req_cmpl *)txcmp;
2867 
2868 	switch (cmpl_type) {
2869 	case CMPL_BASE_TYPE_HWRM_DONE:
2870 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2871 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2872 		break;
2873 
2874 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2875 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2876 
2877 		if ((vf_id < bp->pf.first_vf_id) ||
2878 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2879 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2880 				   vf_id);
2881 			return -EINVAL;
2882 		}
2883 
2884 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2885 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2886 		break;
2887 
2888 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2889 		bnxt_async_event_process(bp,
2890 					 (struct hwrm_async_event_cmpl *)txcmp);
2891 		break;
2892 
2893 	default:
2894 		break;
2895 	}
2896 
2897 	return 0;
2898 }
2899 
2900 static bool bnxt_vnic_is_active(struct bnxt *bp)
2901 {
2902 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2903 
2904 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2905 }
2906 
2907 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2908 {
2909 	struct bnxt_napi *bnapi = dev_instance;
2910 	struct bnxt *bp = bnapi->bp;
2911 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2912 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2913 
2914 	cpr->event_ctr++;
2915 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2916 	napi_schedule(&bnapi->napi);
2917 	return IRQ_HANDLED;
2918 }
2919 
2920 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2921 {
2922 	u32 raw_cons = cpr->cp_raw_cons;
2923 	u16 cons = RING_CMP(raw_cons);
2924 	struct tx_cmp *txcmp;
2925 
2926 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2927 
2928 	return TX_CMP_VALID(txcmp, raw_cons);
2929 }
2930 
2931 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2932 			    int budget)
2933 {
2934 	struct bnxt_napi *bnapi = cpr->bnapi;
2935 	u32 raw_cons = cpr->cp_raw_cons;
2936 	u32 cons;
2937 	int rx_pkts = 0;
2938 	u8 event = 0;
2939 	struct tx_cmp *txcmp;
2940 
2941 	cpr->has_more_work = 0;
2942 	cpr->had_work_done = 1;
2943 	while (1) {
2944 		u8 cmp_type;
2945 		int rc;
2946 
2947 		cons = RING_CMP(raw_cons);
2948 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2949 
2950 		if (!TX_CMP_VALID(txcmp, raw_cons))
2951 			break;
2952 
2953 		/* The valid test of the entry must be done first before
2954 		 * reading any further.
2955 		 */
2956 		dma_rmb();
2957 		cmp_type = TX_CMP_TYPE(txcmp);
2958 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2959 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2960 			u32 opaque = txcmp->tx_cmp_opaque;
2961 			struct bnxt_tx_ring_info *txr;
2962 			u16 tx_freed;
2963 
2964 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2965 			event |= BNXT_TX_CMP_EVENT;
2966 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2967 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2968 			else
2969 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2970 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2971 				   bp->tx_ring_mask;
2972 			/* return full budget so NAPI will complete. */
2973 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2974 				rx_pkts = budget;
2975 				raw_cons = NEXT_RAW_CMP(raw_cons);
2976 				if (budget)
2977 					cpr->has_more_work = 1;
2978 				break;
2979 			}
2980 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2981 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2982 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2983 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2984 			if (likely(budget))
2985 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2986 			else
2987 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2988 							   &event);
2989 			if (likely(rc >= 0))
2990 				rx_pkts += rc;
2991 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2992 			 * the NAPI budget.  Otherwise, we may potentially loop
2993 			 * here forever if we consistently cannot allocate
2994 			 * buffers.
2995 			 */
2996 			else if (rc == -ENOMEM && budget)
2997 				rx_pkts++;
2998 			else if (rc == -EBUSY)	/* partial completion */
2999 				break;
3000 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3001 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3002 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3003 			bnxt_hwrm_handler(bp, txcmp);
3004 		}
3005 		raw_cons = NEXT_RAW_CMP(raw_cons);
3006 
3007 		if (rx_pkts && rx_pkts == budget) {
3008 			cpr->has_more_work = 1;
3009 			break;
3010 		}
3011 	}
3012 
3013 	if (event & BNXT_REDIRECT_EVENT) {
3014 		xdp_do_flush();
3015 		event &= ~BNXT_REDIRECT_EVENT;
3016 	}
3017 
3018 	if (event & BNXT_TX_EVENT) {
3019 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3020 		u16 prod = txr->tx_prod;
3021 
3022 		/* Sync BD data before updating doorbell */
3023 		wmb();
3024 
3025 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3026 		event &= ~BNXT_TX_EVENT;
3027 	}
3028 
3029 	cpr->cp_raw_cons = raw_cons;
3030 	bnapi->events |= event;
3031 	return rx_pkts;
3032 }
3033 
3034 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3035 				  int budget)
3036 {
3037 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3038 		bnapi->tx_int(bp, bnapi, budget);
3039 
3040 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3041 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3042 
3043 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3044 		bnapi->events &= ~BNXT_RX_EVENT;
3045 	}
3046 	if (bnapi->events & BNXT_AGG_EVENT) {
3047 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3048 
3049 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3050 		bnapi->events &= ~BNXT_AGG_EVENT;
3051 	}
3052 }
3053 
3054 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3055 			  int budget)
3056 {
3057 	struct bnxt_napi *bnapi = cpr->bnapi;
3058 	int rx_pkts;
3059 
3060 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3061 
3062 	/* ACK completion ring before freeing tx ring and producing new
3063 	 * buffers in rx/agg rings to prevent overflowing the completion
3064 	 * ring.
3065 	 */
3066 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3067 
3068 	__bnxt_poll_work_done(bp, bnapi, budget);
3069 	return rx_pkts;
3070 }
3071 
3072 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3073 {
3074 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3075 	struct bnxt *bp = bnapi->bp;
3076 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3077 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3078 	struct tx_cmp *txcmp;
3079 	struct rx_cmp_ext *rxcmp1;
3080 	u32 cp_cons, tmp_raw_cons;
3081 	u32 raw_cons = cpr->cp_raw_cons;
3082 	bool flush_xdp = false;
3083 	u32 rx_pkts = 0;
3084 	u8 event = 0;
3085 
3086 	while (1) {
3087 		int rc;
3088 
3089 		cp_cons = RING_CMP(raw_cons);
3090 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3091 
3092 		if (!TX_CMP_VALID(txcmp, raw_cons))
3093 			break;
3094 
3095 		/* The valid test of the entry must be done first before
3096 		 * reading any further.
3097 		 */
3098 		dma_rmb();
3099 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3100 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3101 			cp_cons = RING_CMP(tmp_raw_cons);
3102 			rxcmp1 = (struct rx_cmp_ext *)
3103 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3104 
3105 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3106 				break;
3107 
3108 			/* force an error to recycle the buffer */
3109 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3110 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3111 
3112 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3113 			if (likely(rc == -EIO) && budget)
3114 				rx_pkts++;
3115 			else if (rc == -EBUSY)	/* partial completion */
3116 				break;
3117 			if (event & BNXT_REDIRECT_EVENT)
3118 				flush_xdp = true;
3119 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3120 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3121 			bnxt_hwrm_handler(bp, txcmp);
3122 		} else {
3123 			netdev_err(bp->dev,
3124 				   "Invalid completion received on special ring\n");
3125 		}
3126 		raw_cons = NEXT_RAW_CMP(raw_cons);
3127 
3128 		if (rx_pkts == budget)
3129 			break;
3130 	}
3131 
3132 	cpr->cp_raw_cons = raw_cons;
3133 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3134 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3135 
3136 	if (event & BNXT_AGG_EVENT)
3137 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3138 	if (flush_xdp)
3139 		xdp_do_flush();
3140 
3141 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3142 		napi_complete_done(napi, rx_pkts);
3143 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3144 	}
3145 	return rx_pkts;
3146 }
3147 
3148 static int bnxt_poll(struct napi_struct *napi, int budget)
3149 {
3150 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3151 	struct bnxt *bp = bnapi->bp;
3152 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3153 	int work_done = 0;
3154 
3155 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3156 		napi_complete(napi);
3157 		return 0;
3158 	}
3159 	while (1) {
3160 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3161 
3162 		if (work_done >= budget) {
3163 			if (!budget)
3164 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3165 			break;
3166 		}
3167 
3168 		if (!bnxt_has_work(bp, cpr)) {
3169 			if (napi_complete_done(napi, work_done))
3170 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3171 			break;
3172 		}
3173 	}
3174 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3175 		struct dim_sample dim_sample = {};
3176 
3177 		dim_update_sample(cpr->event_ctr,
3178 				  cpr->rx_packets,
3179 				  cpr->rx_bytes,
3180 				  &dim_sample);
3181 		net_dim(&cpr->dim, &dim_sample);
3182 	}
3183 	return work_done;
3184 }
3185 
3186 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3187 {
3188 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3189 	int i, work_done = 0;
3190 
3191 	for (i = 0; i < cpr->cp_ring_count; i++) {
3192 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3193 
3194 		if (cpr2->had_nqe_notify) {
3195 			work_done += __bnxt_poll_work(bp, cpr2,
3196 						      budget - work_done);
3197 			cpr->has_more_work |= cpr2->has_more_work;
3198 		}
3199 	}
3200 	return work_done;
3201 }
3202 
3203 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3204 				 u64 dbr_type, int budget)
3205 {
3206 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3207 	int i;
3208 
3209 	for (i = 0; i < cpr->cp_ring_count; i++) {
3210 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3211 		struct bnxt_db_info *db;
3212 
3213 		if (cpr2->had_work_done) {
3214 			u32 tgl = 0;
3215 
3216 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3217 				cpr2->had_nqe_notify = 0;
3218 				tgl = cpr2->toggle;
3219 			}
3220 			db = &cpr2->cp_db;
3221 			bnxt_writeq(bp,
3222 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3223 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3224 				    db->doorbell);
3225 			cpr2->had_work_done = 0;
3226 		}
3227 	}
3228 	__bnxt_poll_work_done(bp, bnapi, budget);
3229 }
3230 
3231 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3232 {
3233 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3234 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3235 	struct bnxt_cp_ring_info *cpr_rx;
3236 	u32 raw_cons = cpr->cp_raw_cons;
3237 	struct bnxt *bp = bnapi->bp;
3238 	struct nqe_cn *nqcmp;
3239 	int work_done = 0;
3240 	u32 cons;
3241 
3242 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3243 		napi_complete(napi);
3244 		return 0;
3245 	}
3246 	if (cpr->has_more_work) {
3247 		cpr->has_more_work = 0;
3248 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3249 	}
3250 	while (1) {
3251 		u16 type;
3252 
3253 		cons = RING_CMP(raw_cons);
3254 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3255 
3256 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3257 			if (cpr->has_more_work)
3258 				break;
3259 
3260 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3261 					     budget);
3262 			cpr->cp_raw_cons = raw_cons;
3263 			if (napi_complete_done(napi, work_done))
3264 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3265 						  cpr->cp_raw_cons);
3266 			goto poll_done;
3267 		}
3268 
3269 		/* The valid test of the entry must be done first before
3270 		 * reading any further.
3271 		 */
3272 		dma_rmb();
3273 
3274 		type = le16_to_cpu(nqcmp->type);
3275 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3276 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3277 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3278 			struct bnxt_cp_ring_info *cpr2;
3279 
3280 			/* No more budget for RX work */
3281 			if (budget && work_done >= budget &&
3282 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3283 				break;
3284 
3285 			idx = BNXT_NQ_HDL_IDX(idx);
3286 			cpr2 = &cpr->cp_ring_arr[idx];
3287 			cpr2->had_nqe_notify = 1;
3288 			cpr2->toggle = NQE_CN_TOGGLE(type);
3289 			work_done += __bnxt_poll_work(bp, cpr2,
3290 						      budget - work_done);
3291 			cpr->has_more_work |= cpr2->has_more_work;
3292 		} else {
3293 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3294 		}
3295 		raw_cons = NEXT_RAW_CMP(raw_cons);
3296 	}
3297 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3298 	if (raw_cons != cpr->cp_raw_cons) {
3299 		cpr->cp_raw_cons = raw_cons;
3300 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3301 	}
3302 poll_done:
3303 	cpr_rx = &cpr->cp_ring_arr[0];
3304 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3305 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3306 		struct dim_sample dim_sample = {};
3307 
3308 		dim_update_sample(cpr->event_ctr,
3309 				  cpr_rx->rx_packets,
3310 				  cpr_rx->rx_bytes,
3311 				  &dim_sample);
3312 		net_dim(&cpr->dim, &dim_sample);
3313 	}
3314 	return work_done;
3315 }
3316 
3317 static void bnxt_free_tx_skbs(struct bnxt *bp)
3318 {
3319 	int i, max_idx;
3320 	struct pci_dev *pdev = bp->pdev;
3321 
3322 	if (!bp->tx_ring)
3323 		return;
3324 
3325 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3326 	for (i = 0; i < bp->tx_nr_rings; i++) {
3327 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3328 		int j;
3329 
3330 		if (!txr->tx_buf_ring)
3331 			continue;
3332 
3333 		for (j = 0; j < max_idx;) {
3334 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3335 			struct sk_buff *skb;
3336 			int k, last;
3337 
3338 			if (i < bp->tx_nr_rings_xdp &&
3339 			    tx_buf->action == XDP_REDIRECT) {
3340 				dma_unmap_single(&pdev->dev,
3341 					dma_unmap_addr(tx_buf, mapping),
3342 					dma_unmap_len(tx_buf, len),
3343 					DMA_TO_DEVICE);
3344 				xdp_return_frame(tx_buf->xdpf);
3345 				tx_buf->action = 0;
3346 				tx_buf->xdpf = NULL;
3347 				j++;
3348 				continue;
3349 			}
3350 
3351 			skb = tx_buf->skb;
3352 			if (!skb) {
3353 				j++;
3354 				continue;
3355 			}
3356 
3357 			tx_buf->skb = NULL;
3358 
3359 			if (tx_buf->is_push) {
3360 				dev_kfree_skb(skb);
3361 				j += 2;
3362 				continue;
3363 			}
3364 
3365 			dma_unmap_single(&pdev->dev,
3366 					 dma_unmap_addr(tx_buf, mapping),
3367 					 skb_headlen(skb),
3368 					 DMA_TO_DEVICE);
3369 
3370 			last = tx_buf->nr_frags;
3371 			j += 2;
3372 			for (k = 0; k < last; k++, j++) {
3373 				int ring_idx = j & bp->tx_ring_mask;
3374 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3375 
3376 				tx_buf = &txr->tx_buf_ring[ring_idx];
3377 				dma_unmap_page(
3378 					&pdev->dev,
3379 					dma_unmap_addr(tx_buf, mapping),
3380 					skb_frag_size(frag), DMA_TO_DEVICE);
3381 			}
3382 			dev_kfree_skb(skb);
3383 		}
3384 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3385 	}
3386 }
3387 
3388 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3389 {
3390 	int i, max_idx;
3391 
3392 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3393 
3394 	for (i = 0; i < max_idx; i++) {
3395 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3396 		void *data = rx_buf->data;
3397 
3398 		if (!data)
3399 			continue;
3400 
3401 		rx_buf->data = NULL;
3402 		if (BNXT_RX_PAGE_MODE(bp))
3403 			page_pool_recycle_direct(rxr->page_pool, data);
3404 		else
3405 			page_pool_free_va(rxr->head_pool, data, true);
3406 	}
3407 }
3408 
3409 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3410 {
3411 	int i, max_idx;
3412 
3413 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3414 
3415 	for (i = 0; i < max_idx; i++) {
3416 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3417 		struct page *page = rx_agg_buf->page;
3418 
3419 		if (!page)
3420 			continue;
3421 
3422 		rx_agg_buf->page = NULL;
3423 		__clear_bit(i, rxr->rx_agg_bmap);
3424 
3425 		page_pool_recycle_direct(rxr->page_pool, page);
3426 	}
3427 }
3428 
3429 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3430 					struct bnxt_rx_ring_info *rxr)
3431 {
3432 	int i;
3433 
3434 	for (i = 0; i < bp->max_tpa; i++) {
3435 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3436 		u8 *data = tpa_info->data;
3437 
3438 		if (!data)
3439 			continue;
3440 
3441 		tpa_info->data = NULL;
3442 		page_pool_free_va(rxr->head_pool, data, false);
3443 	}
3444 }
3445 
3446 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3447 				       struct bnxt_rx_ring_info *rxr)
3448 {
3449 	struct bnxt_tpa_idx_map *map;
3450 
3451 	if (!rxr->rx_tpa)
3452 		goto skip_rx_tpa_free;
3453 
3454 	bnxt_free_one_tpa_info_data(bp, rxr);
3455 
3456 skip_rx_tpa_free:
3457 	if (!rxr->rx_buf_ring)
3458 		goto skip_rx_buf_free;
3459 
3460 	bnxt_free_one_rx_ring(bp, rxr);
3461 
3462 skip_rx_buf_free:
3463 	if (!rxr->rx_agg_ring)
3464 		goto skip_rx_agg_free;
3465 
3466 	bnxt_free_one_rx_agg_ring(bp, rxr);
3467 
3468 skip_rx_agg_free:
3469 	map = rxr->rx_tpa_idx_map;
3470 	if (map)
3471 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3472 }
3473 
3474 static void bnxt_free_rx_skbs(struct bnxt *bp)
3475 {
3476 	int i;
3477 
3478 	if (!bp->rx_ring)
3479 		return;
3480 
3481 	for (i = 0; i < bp->rx_nr_rings; i++)
3482 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3483 }
3484 
3485 static void bnxt_free_skbs(struct bnxt *bp)
3486 {
3487 	bnxt_free_tx_skbs(bp);
3488 	bnxt_free_rx_skbs(bp);
3489 }
3490 
3491 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3492 {
3493 	u8 init_val = ctxm->init_value;
3494 	u16 offset = ctxm->init_offset;
3495 	u8 *p2 = p;
3496 	int i;
3497 
3498 	if (!init_val)
3499 		return;
3500 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3501 		memset(p, init_val, len);
3502 		return;
3503 	}
3504 	for (i = 0; i < len; i += ctxm->entry_size)
3505 		*(p2 + i + offset) = init_val;
3506 }
3507 
3508 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3509 			       void *buf, size_t offset, size_t head,
3510 			       size_t tail)
3511 {
3512 	int i, head_page, start_idx, source_offset;
3513 	size_t len, rem_len, total_len, max_bytes;
3514 
3515 	head_page = head / rmem->page_size;
3516 	source_offset = head % rmem->page_size;
3517 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3518 	if (!total_len)
3519 		total_len = MAX_CTX_BYTES;
3520 	start_idx = head_page % MAX_CTX_PAGES;
3521 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3522 		    source_offset;
3523 	total_len = min(total_len, max_bytes);
3524 	rem_len = total_len;
3525 
3526 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3527 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3528 		if (buf)
3529 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3530 			       len);
3531 		offset += len;
3532 		rem_len -= len;
3533 	}
3534 	return total_len;
3535 }
3536 
3537 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3538 {
3539 	struct pci_dev *pdev = bp->pdev;
3540 	int i;
3541 
3542 	if (!rmem->pg_arr)
3543 		goto skip_pages;
3544 
3545 	for (i = 0; i < rmem->nr_pages; i++) {
3546 		if (!rmem->pg_arr[i])
3547 			continue;
3548 
3549 		dma_free_coherent(&pdev->dev, rmem->page_size,
3550 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3551 
3552 		rmem->pg_arr[i] = NULL;
3553 	}
3554 skip_pages:
3555 	if (rmem->pg_tbl) {
3556 		size_t pg_tbl_size = rmem->nr_pages * 8;
3557 
3558 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3559 			pg_tbl_size = rmem->page_size;
3560 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3561 				  rmem->pg_tbl, rmem->pg_tbl_map);
3562 		rmem->pg_tbl = NULL;
3563 	}
3564 	if (rmem->vmem_size && *rmem->vmem) {
3565 		vfree(*rmem->vmem);
3566 		*rmem->vmem = NULL;
3567 	}
3568 }
3569 
3570 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3571 {
3572 	struct pci_dev *pdev = bp->pdev;
3573 	u64 valid_bit = 0;
3574 	int i;
3575 
3576 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3577 		valid_bit = PTU_PTE_VALID;
3578 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3579 		size_t pg_tbl_size = rmem->nr_pages * 8;
3580 
3581 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3582 			pg_tbl_size = rmem->page_size;
3583 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3584 						  &rmem->pg_tbl_map,
3585 						  GFP_KERNEL);
3586 		if (!rmem->pg_tbl)
3587 			return -ENOMEM;
3588 	}
3589 
3590 	for (i = 0; i < rmem->nr_pages; i++) {
3591 		u64 extra_bits = valid_bit;
3592 
3593 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3594 						     rmem->page_size,
3595 						     &rmem->dma_arr[i],
3596 						     GFP_KERNEL);
3597 		if (!rmem->pg_arr[i])
3598 			return -ENOMEM;
3599 
3600 		if (rmem->ctx_mem)
3601 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3602 					  rmem->page_size);
3603 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3604 			if (i == rmem->nr_pages - 2 &&
3605 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3606 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3607 			else if (i == rmem->nr_pages - 1 &&
3608 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3609 				extra_bits |= PTU_PTE_LAST;
3610 			rmem->pg_tbl[i] =
3611 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3612 		}
3613 	}
3614 
3615 	if (rmem->vmem_size) {
3616 		*rmem->vmem = vzalloc(rmem->vmem_size);
3617 		if (!(*rmem->vmem))
3618 			return -ENOMEM;
3619 	}
3620 	return 0;
3621 }
3622 
3623 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3624 				   struct bnxt_rx_ring_info *rxr)
3625 {
3626 	int i;
3627 
3628 	kfree(rxr->rx_tpa_idx_map);
3629 	rxr->rx_tpa_idx_map = NULL;
3630 	if (rxr->rx_tpa) {
3631 		for (i = 0; i < bp->max_tpa; i++) {
3632 			kfree(rxr->rx_tpa[i].agg_arr);
3633 			rxr->rx_tpa[i].agg_arr = NULL;
3634 		}
3635 	}
3636 	kfree(rxr->rx_tpa);
3637 	rxr->rx_tpa = NULL;
3638 }
3639 
3640 static void bnxt_free_tpa_info(struct bnxt *bp)
3641 {
3642 	int i;
3643 
3644 	for (i = 0; i < bp->rx_nr_rings; i++) {
3645 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3646 
3647 		bnxt_free_one_tpa_info(bp, rxr);
3648 	}
3649 }
3650 
3651 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3652 				   struct bnxt_rx_ring_info *rxr)
3653 {
3654 	struct rx_agg_cmp *agg;
3655 	int i;
3656 
3657 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3658 			      GFP_KERNEL);
3659 	if (!rxr->rx_tpa)
3660 		return -ENOMEM;
3661 
3662 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3663 		return 0;
3664 	for (i = 0; i < bp->max_tpa; i++) {
3665 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3666 		if (!agg)
3667 			return -ENOMEM;
3668 		rxr->rx_tpa[i].agg_arr = agg;
3669 	}
3670 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3671 				      GFP_KERNEL);
3672 	if (!rxr->rx_tpa_idx_map)
3673 		return -ENOMEM;
3674 
3675 	return 0;
3676 }
3677 
3678 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3679 {
3680 	int i, rc;
3681 
3682 	bp->max_tpa = MAX_TPA;
3683 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3684 		if (!bp->max_tpa_v2)
3685 			return 0;
3686 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3687 	}
3688 
3689 	for (i = 0; i < bp->rx_nr_rings; i++) {
3690 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3691 
3692 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3693 		if (rc)
3694 			return rc;
3695 	}
3696 	return 0;
3697 }
3698 
3699 static void bnxt_free_rx_rings(struct bnxt *bp)
3700 {
3701 	int i;
3702 
3703 	if (!bp->rx_ring)
3704 		return;
3705 
3706 	bnxt_free_tpa_info(bp);
3707 	for (i = 0; i < bp->rx_nr_rings; i++) {
3708 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3709 		struct bnxt_ring_struct *ring;
3710 
3711 		if (rxr->xdp_prog)
3712 			bpf_prog_put(rxr->xdp_prog);
3713 
3714 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3715 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3716 
3717 		page_pool_destroy(rxr->page_pool);
3718 		if (bnxt_separate_head_pool())
3719 			page_pool_destroy(rxr->head_pool);
3720 		rxr->page_pool = rxr->head_pool = NULL;
3721 
3722 		kfree(rxr->rx_agg_bmap);
3723 		rxr->rx_agg_bmap = NULL;
3724 
3725 		ring = &rxr->rx_ring_struct;
3726 		bnxt_free_ring(bp, &ring->ring_mem);
3727 
3728 		ring = &rxr->rx_agg_ring_struct;
3729 		bnxt_free_ring(bp, &ring->ring_mem);
3730 	}
3731 }
3732 
3733 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3734 				   struct bnxt_rx_ring_info *rxr,
3735 				   int numa_node)
3736 {
3737 	struct page_pool_params pp = { 0 };
3738 	struct page_pool *pool;
3739 
3740 	pp.pool_size = bp->rx_agg_ring_size;
3741 	if (BNXT_RX_PAGE_MODE(bp))
3742 		pp.pool_size += bp->rx_ring_size;
3743 	pp.nid = numa_node;
3744 	pp.napi = &rxr->bnapi->napi;
3745 	pp.netdev = bp->dev;
3746 	pp.dev = &bp->pdev->dev;
3747 	pp.dma_dir = bp->rx_dir;
3748 	pp.max_len = PAGE_SIZE;
3749 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3750 
3751 	pool = page_pool_create(&pp);
3752 	if (IS_ERR(pool))
3753 		return PTR_ERR(pool);
3754 	rxr->page_pool = pool;
3755 
3756 	if (bnxt_separate_head_pool()) {
3757 		pp.pool_size = max(bp->rx_ring_size, 1024);
3758 		pool = page_pool_create(&pp);
3759 		if (IS_ERR(pool))
3760 			goto err_destroy_pp;
3761 	}
3762 	rxr->head_pool = pool;
3763 
3764 	return 0;
3765 
3766 err_destroy_pp:
3767 	page_pool_destroy(rxr->page_pool);
3768 	rxr->page_pool = NULL;
3769 	return PTR_ERR(pool);
3770 }
3771 
3772 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3773 {
3774 	u16 mem_size;
3775 
3776 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3777 	mem_size = rxr->rx_agg_bmap_size / 8;
3778 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3779 	if (!rxr->rx_agg_bmap)
3780 		return -ENOMEM;
3781 
3782 	return 0;
3783 }
3784 
3785 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3786 {
3787 	int numa_node = dev_to_node(&bp->pdev->dev);
3788 	int i, rc = 0, agg_rings = 0, cpu;
3789 
3790 	if (!bp->rx_ring)
3791 		return -ENOMEM;
3792 
3793 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3794 		agg_rings = 1;
3795 
3796 	for (i = 0; i < bp->rx_nr_rings; i++) {
3797 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3798 		struct bnxt_ring_struct *ring;
3799 		int cpu_node;
3800 
3801 		ring = &rxr->rx_ring_struct;
3802 
3803 		cpu = cpumask_local_spread(i, numa_node);
3804 		cpu_node = cpu_to_node(cpu);
3805 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3806 			   i, cpu_node);
3807 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3808 		if (rc)
3809 			return rc;
3810 
3811 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3812 		if (rc < 0)
3813 			return rc;
3814 
3815 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3816 						MEM_TYPE_PAGE_POOL,
3817 						rxr->page_pool);
3818 		if (rc) {
3819 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3820 			return rc;
3821 		}
3822 
3823 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3824 		if (rc)
3825 			return rc;
3826 
3827 		ring->grp_idx = i;
3828 		if (agg_rings) {
3829 			ring = &rxr->rx_agg_ring_struct;
3830 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3831 			if (rc)
3832 				return rc;
3833 
3834 			ring->grp_idx = i;
3835 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3836 			if (rc)
3837 				return rc;
3838 		}
3839 	}
3840 	if (bp->flags & BNXT_FLAG_TPA)
3841 		rc = bnxt_alloc_tpa_info(bp);
3842 	return rc;
3843 }
3844 
3845 static void bnxt_free_tx_rings(struct bnxt *bp)
3846 {
3847 	int i;
3848 	struct pci_dev *pdev = bp->pdev;
3849 
3850 	if (!bp->tx_ring)
3851 		return;
3852 
3853 	for (i = 0; i < bp->tx_nr_rings; i++) {
3854 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3855 		struct bnxt_ring_struct *ring;
3856 
3857 		if (txr->tx_push) {
3858 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3859 					  txr->tx_push, txr->tx_push_mapping);
3860 			txr->tx_push = NULL;
3861 		}
3862 
3863 		ring = &txr->tx_ring_struct;
3864 
3865 		bnxt_free_ring(bp, &ring->ring_mem);
3866 	}
3867 }
3868 
3869 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3870 	((tc) * (bp)->tx_nr_rings_per_tc)
3871 
3872 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3873 	((tx) % (bp)->tx_nr_rings_per_tc)
3874 
3875 #define BNXT_RING_TO_TC(bp, tx)		\
3876 	((tx) / (bp)->tx_nr_rings_per_tc)
3877 
3878 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3879 {
3880 	int i, j, rc;
3881 	struct pci_dev *pdev = bp->pdev;
3882 
3883 	bp->tx_push_size = 0;
3884 	if (bp->tx_push_thresh) {
3885 		int push_size;
3886 
3887 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3888 					bp->tx_push_thresh);
3889 
3890 		if (push_size > 256) {
3891 			push_size = 0;
3892 			bp->tx_push_thresh = 0;
3893 		}
3894 
3895 		bp->tx_push_size = push_size;
3896 	}
3897 
3898 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3899 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3900 		struct bnxt_ring_struct *ring;
3901 		u8 qidx;
3902 
3903 		ring = &txr->tx_ring_struct;
3904 
3905 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3906 		if (rc)
3907 			return rc;
3908 
3909 		ring->grp_idx = txr->bnapi->index;
3910 		if (bp->tx_push_size) {
3911 			dma_addr_t mapping;
3912 
3913 			/* One pre-allocated DMA buffer to backup
3914 			 * TX push operation
3915 			 */
3916 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3917 						bp->tx_push_size,
3918 						&txr->tx_push_mapping,
3919 						GFP_KERNEL);
3920 
3921 			if (!txr->tx_push)
3922 				return -ENOMEM;
3923 
3924 			mapping = txr->tx_push_mapping +
3925 				sizeof(struct tx_push_bd);
3926 			txr->data_mapping = cpu_to_le64(mapping);
3927 		}
3928 		qidx = bp->tc_to_qidx[j];
3929 		ring->queue_id = bp->q_info[qidx].queue_id;
3930 		spin_lock_init(&txr->xdp_tx_lock);
3931 		if (i < bp->tx_nr_rings_xdp)
3932 			continue;
3933 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3934 			j++;
3935 	}
3936 	return 0;
3937 }
3938 
3939 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3940 {
3941 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3942 
3943 	kfree(cpr->cp_desc_ring);
3944 	cpr->cp_desc_ring = NULL;
3945 	ring->ring_mem.pg_arr = NULL;
3946 	kfree(cpr->cp_desc_mapping);
3947 	cpr->cp_desc_mapping = NULL;
3948 	ring->ring_mem.dma_arr = NULL;
3949 }
3950 
3951 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3952 {
3953 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3954 	if (!cpr->cp_desc_ring)
3955 		return -ENOMEM;
3956 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3957 				       GFP_KERNEL);
3958 	if (!cpr->cp_desc_mapping)
3959 		return -ENOMEM;
3960 	return 0;
3961 }
3962 
3963 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3964 {
3965 	int i;
3966 
3967 	if (!bp->bnapi)
3968 		return;
3969 	for (i = 0; i < bp->cp_nr_rings; i++) {
3970 		struct bnxt_napi *bnapi = bp->bnapi[i];
3971 
3972 		if (!bnapi)
3973 			continue;
3974 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3975 	}
3976 }
3977 
3978 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3979 {
3980 	int i, n = bp->cp_nr_pages;
3981 
3982 	for (i = 0; i < bp->cp_nr_rings; i++) {
3983 		struct bnxt_napi *bnapi = bp->bnapi[i];
3984 		int rc;
3985 
3986 		if (!bnapi)
3987 			continue;
3988 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3989 		if (rc)
3990 			return rc;
3991 	}
3992 	return 0;
3993 }
3994 
3995 static void bnxt_free_cp_rings(struct bnxt *bp)
3996 {
3997 	int i;
3998 
3999 	if (!bp->bnapi)
4000 		return;
4001 
4002 	for (i = 0; i < bp->cp_nr_rings; i++) {
4003 		struct bnxt_napi *bnapi = bp->bnapi[i];
4004 		struct bnxt_cp_ring_info *cpr;
4005 		struct bnxt_ring_struct *ring;
4006 		int j;
4007 
4008 		if (!bnapi)
4009 			continue;
4010 
4011 		cpr = &bnapi->cp_ring;
4012 		ring = &cpr->cp_ring_struct;
4013 
4014 		bnxt_free_ring(bp, &ring->ring_mem);
4015 
4016 		if (!cpr->cp_ring_arr)
4017 			continue;
4018 
4019 		for (j = 0; j < cpr->cp_ring_count; j++) {
4020 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4021 
4022 			ring = &cpr2->cp_ring_struct;
4023 			bnxt_free_ring(bp, &ring->ring_mem);
4024 			bnxt_free_cp_arrays(cpr2);
4025 		}
4026 		kfree(cpr->cp_ring_arr);
4027 		cpr->cp_ring_arr = NULL;
4028 		cpr->cp_ring_count = 0;
4029 	}
4030 }
4031 
4032 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4033 				  struct bnxt_cp_ring_info *cpr)
4034 {
4035 	struct bnxt_ring_mem_info *rmem;
4036 	struct bnxt_ring_struct *ring;
4037 	int rc;
4038 
4039 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4040 	if (rc) {
4041 		bnxt_free_cp_arrays(cpr);
4042 		return -ENOMEM;
4043 	}
4044 	ring = &cpr->cp_ring_struct;
4045 	rmem = &ring->ring_mem;
4046 	rmem->nr_pages = bp->cp_nr_pages;
4047 	rmem->page_size = HW_CMPD_RING_SIZE;
4048 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4049 	rmem->dma_arr = cpr->cp_desc_mapping;
4050 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4051 	rc = bnxt_alloc_ring(bp, rmem);
4052 	if (rc) {
4053 		bnxt_free_ring(bp, rmem);
4054 		bnxt_free_cp_arrays(cpr);
4055 	}
4056 	return rc;
4057 }
4058 
4059 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4060 {
4061 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4062 	int i, j, rc, ulp_msix;
4063 	int tcs = bp->num_tc;
4064 
4065 	if (!tcs)
4066 		tcs = 1;
4067 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4068 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4069 		struct bnxt_napi *bnapi = bp->bnapi[i];
4070 		struct bnxt_cp_ring_info *cpr, *cpr2;
4071 		struct bnxt_ring_struct *ring;
4072 		int cp_count = 0, k;
4073 		int rx = 0, tx = 0;
4074 
4075 		if (!bnapi)
4076 			continue;
4077 
4078 		cpr = &bnapi->cp_ring;
4079 		cpr->bnapi = bnapi;
4080 		ring = &cpr->cp_ring_struct;
4081 
4082 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4083 		if (rc)
4084 			return rc;
4085 
4086 		ring->map_idx = ulp_msix + i;
4087 
4088 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4089 			continue;
4090 
4091 		if (i < bp->rx_nr_rings) {
4092 			cp_count++;
4093 			rx = 1;
4094 		}
4095 		if (i < bp->tx_nr_rings_xdp) {
4096 			cp_count++;
4097 			tx = 1;
4098 		} else if ((sh && i < bp->tx_nr_rings) ||
4099 			 (!sh && i >= bp->rx_nr_rings)) {
4100 			cp_count += tcs;
4101 			tx = 1;
4102 		}
4103 
4104 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4105 					   GFP_KERNEL);
4106 		if (!cpr->cp_ring_arr)
4107 			return -ENOMEM;
4108 		cpr->cp_ring_count = cp_count;
4109 
4110 		for (k = 0; k < cp_count; k++) {
4111 			cpr2 = &cpr->cp_ring_arr[k];
4112 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4113 			if (rc)
4114 				return rc;
4115 			cpr2->bnapi = bnapi;
4116 			cpr2->sw_stats = cpr->sw_stats;
4117 			cpr2->cp_idx = k;
4118 			if (!k && rx) {
4119 				bp->rx_ring[i].rx_cpr = cpr2;
4120 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4121 			} else {
4122 				int n, tc = k - rx;
4123 
4124 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4125 				bp->tx_ring[n].tx_cpr = cpr2;
4126 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4127 			}
4128 		}
4129 		if (tx)
4130 			j++;
4131 	}
4132 	return 0;
4133 }
4134 
4135 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4136 				     struct bnxt_rx_ring_info *rxr)
4137 {
4138 	struct bnxt_ring_mem_info *rmem;
4139 	struct bnxt_ring_struct *ring;
4140 
4141 	ring = &rxr->rx_ring_struct;
4142 	rmem = &ring->ring_mem;
4143 	rmem->nr_pages = bp->rx_nr_pages;
4144 	rmem->page_size = HW_RXBD_RING_SIZE;
4145 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4146 	rmem->dma_arr = rxr->rx_desc_mapping;
4147 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4148 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4149 
4150 	ring = &rxr->rx_agg_ring_struct;
4151 	rmem = &ring->ring_mem;
4152 	rmem->nr_pages = bp->rx_agg_nr_pages;
4153 	rmem->page_size = HW_RXBD_RING_SIZE;
4154 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4155 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4156 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4157 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4158 }
4159 
4160 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4161 				      struct bnxt_rx_ring_info *rxr)
4162 {
4163 	struct bnxt_ring_mem_info *rmem;
4164 	struct bnxt_ring_struct *ring;
4165 	int i;
4166 
4167 	rxr->page_pool->p.napi = NULL;
4168 	rxr->page_pool = NULL;
4169 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4170 
4171 	ring = &rxr->rx_ring_struct;
4172 	rmem = &ring->ring_mem;
4173 	rmem->pg_tbl = NULL;
4174 	rmem->pg_tbl_map = 0;
4175 	for (i = 0; i < rmem->nr_pages; i++) {
4176 		rmem->pg_arr[i] = NULL;
4177 		rmem->dma_arr[i] = 0;
4178 	}
4179 	*rmem->vmem = NULL;
4180 
4181 	ring = &rxr->rx_agg_ring_struct;
4182 	rmem = &ring->ring_mem;
4183 	rmem->pg_tbl = NULL;
4184 	rmem->pg_tbl_map = 0;
4185 	for (i = 0; i < rmem->nr_pages; i++) {
4186 		rmem->pg_arr[i] = NULL;
4187 		rmem->dma_arr[i] = 0;
4188 	}
4189 	*rmem->vmem = NULL;
4190 }
4191 
4192 static void bnxt_init_ring_struct(struct bnxt *bp)
4193 {
4194 	int i, j;
4195 
4196 	for (i = 0; i < bp->cp_nr_rings; i++) {
4197 		struct bnxt_napi *bnapi = bp->bnapi[i];
4198 		struct bnxt_ring_mem_info *rmem;
4199 		struct bnxt_cp_ring_info *cpr;
4200 		struct bnxt_rx_ring_info *rxr;
4201 		struct bnxt_tx_ring_info *txr;
4202 		struct bnxt_ring_struct *ring;
4203 
4204 		if (!bnapi)
4205 			continue;
4206 
4207 		cpr = &bnapi->cp_ring;
4208 		ring = &cpr->cp_ring_struct;
4209 		rmem = &ring->ring_mem;
4210 		rmem->nr_pages = bp->cp_nr_pages;
4211 		rmem->page_size = HW_CMPD_RING_SIZE;
4212 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4213 		rmem->dma_arr = cpr->cp_desc_mapping;
4214 		rmem->vmem_size = 0;
4215 
4216 		rxr = bnapi->rx_ring;
4217 		if (!rxr)
4218 			goto skip_rx;
4219 
4220 		ring = &rxr->rx_ring_struct;
4221 		rmem = &ring->ring_mem;
4222 		rmem->nr_pages = bp->rx_nr_pages;
4223 		rmem->page_size = HW_RXBD_RING_SIZE;
4224 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4225 		rmem->dma_arr = rxr->rx_desc_mapping;
4226 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4227 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4228 
4229 		ring = &rxr->rx_agg_ring_struct;
4230 		rmem = &ring->ring_mem;
4231 		rmem->nr_pages = bp->rx_agg_nr_pages;
4232 		rmem->page_size = HW_RXBD_RING_SIZE;
4233 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4234 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4235 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4236 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4237 
4238 skip_rx:
4239 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4240 			ring = &txr->tx_ring_struct;
4241 			rmem = &ring->ring_mem;
4242 			rmem->nr_pages = bp->tx_nr_pages;
4243 			rmem->page_size = HW_TXBD_RING_SIZE;
4244 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4245 			rmem->dma_arr = txr->tx_desc_mapping;
4246 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4247 			rmem->vmem = (void **)&txr->tx_buf_ring;
4248 		}
4249 	}
4250 }
4251 
4252 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4253 {
4254 	int i;
4255 	u32 prod;
4256 	struct rx_bd **rx_buf_ring;
4257 
4258 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4259 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4260 		int j;
4261 		struct rx_bd *rxbd;
4262 
4263 		rxbd = rx_buf_ring[i];
4264 		if (!rxbd)
4265 			continue;
4266 
4267 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4268 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4269 			rxbd->rx_bd_opaque = prod;
4270 		}
4271 	}
4272 }
4273 
4274 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4275 				       struct bnxt_rx_ring_info *rxr,
4276 				       int ring_nr)
4277 {
4278 	u32 prod;
4279 	int i;
4280 
4281 	prod = rxr->rx_prod;
4282 	for (i = 0; i < bp->rx_ring_size; i++) {
4283 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4284 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4285 				    ring_nr, i, bp->rx_ring_size);
4286 			break;
4287 		}
4288 		prod = NEXT_RX(prod);
4289 	}
4290 	rxr->rx_prod = prod;
4291 }
4292 
4293 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4294 					struct bnxt_rx_ring_info *rxr,
4295 					int ring_nr)
4296 {
4297 	u32 prod;
4298 	int i;
4299 
4300 	prod = rxr->rx_agg_prod;
4301 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4302 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4303 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4304 				    ring_nr, i, bp->rx_ring_size);
4305 			break;
4306 		}
4307 		prod = NEXT_RX_AGG(prod);
4308 	}
4309 	rxr->rx_agg_prod = prod;
4310 }
4311 
4312 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4313 					struct bnxt_rx_ring_info *rxr)
4314 {
4315 	dma_addr_t mapping;
4316 	u8 *data;
4317 	int i;
4318 
4319 	for (i = 0; i < bp->max_tpa; i++) {
4320 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4321 					    GFP_KERNEL);
4322 		if (!data)
4323 			return -ENOMEM;
4324 
4325 		rxr->rx_tpa[i].data = data;
4326 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4327 		rxr->rx_tpa[i].mapping = mapping;
4328 	}
4329 
4330 	return 0;
4331 }
4332 
4333 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4334 {
4335 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4336 	int rc;
4337 
4338 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4339 
4340 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4341 		return 0;
4342 
4343 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4344 
4345 	if (rxr->rx_tpa) {
4346 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4347 		if (rc)
4348 			return rc;
4349 	}
4350 	return 0;
4351 }
4352 
4353 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4354 				       struct bnxt_rx_ring_info *rxr)
4355 {
4356 	struct bnxt_ring_struct *ring;
4357 	u32 type;
4358 
4359 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4360 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4361 
4362 	if (NET_IP_ALIGN == 2)
4363 		type |= RX_BD_FLAGS_SOP;
4364 
4365 	ring = &rxr->rx_ring_struct;
4366 	bnxt_init_rxbd_pages(ring, type);
4367 	ring->fw_ring_id = INVALID_HW_RING_ID;
4368 }
4369 
4370 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4371 					   struct bnxt_rx_ring_info *rxr)
4372 {
4373 	struct bnxt_ring_struct *ring;
4374 	u32 type;
4375 
4376 	ring = &rxr->rx_agg_ring_struct;
4377 	ring->fw_ring_id = INVALID_HW_RING_ID;
4378 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4379 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4380 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4381 
4382 		bnxt_init_rxbd_pages(ring, type);
4383 	}
4384 }
4385 
4386 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4387 {
4388 	struct bnxt_rx_ring_info *rxr;
4389 
4390 	rxr = &bp->rx_ring[ring_nr];
4391 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4392 
4393 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4394 			     &rxr->bnapi->napi);
4395 
4396 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4397 		bpf_prog_add(bp->xdp_prog, 1);
4398 		rxr->xdp_prog = bp->xdp_prog;
4399 	}
4400 
4401 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4402 
4403 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4404 }
4405 
4406 static void bnxt_init_cp_rings(struct bnxt *bp)
4407 {
4408 	int i, j;
4409 
4410 	for (i = 0; i < bp->cp_nr_rings; i++) {
4411 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4412 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4413 
4414 		ring->fw_ring_id = INVALID_HW_RING_ID;
4415 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4416 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4417 		if (!cpr->cp_ring_arr)
4418 			continue;
4419 		for (j = 0; j < cpr->cp_ring_count; j++) {
4420 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4421 
4422 			ring = &cpr2->cp_ring_struct;
4423 			ring->fw_ring_id = INVALID_HW_RING_ID;
4424 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4425 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4426 		}
4427 	}
4428 }
4429 
4430 static int bnxt_init_rx_rings(struct bnxt *bp)
4431 {
4432 	int i, rc = 0;
4433 
4434 	if (BNXT_RX_PAGE_MODE(bp)) {
4435 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4436 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4437 	} else {
4438 		bp->rx_offset = BNXT_RX_OFFSET;
4439 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4440 	}
4441 
4442 	for (i = 0; i < bp->rx_nr_rings; i++) {
4443 		rc = bnxt_init_one_rx_ring(bp, i);
4444 		if (rc)
4445 			break;
4446 	}
4447 
4448 	return rc;
4449 }
4450 
4451 static int bnxt_init_tx_rings(struct bnxt *bp)
4452 {
4453 	u16 i;
4454 
4455 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4456 				   BNXT_MIN_TX_DESC_CNT);
4457 
4458 	for (i = 0; i < bp->tx_nr_rings; i++) {
4459 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4460 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4461 
4462 		ring->fw_ring_id = INVALID_HW_RING_ID;
4463 
4464 		if (i >= bp->tx_nr_rings_xdp)
4465 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4466 					     NETDEV_QUEUE_TYPE_TX,
4467 					     &txr->bnapi->napi);
4468 	}
4469 
4470 	return 0;
4471 }
4472 
4473 static void bnxt_free_ring_grps(struct bnxt *bp)
4474 {
4475 	kfree(bp->grp_info);
4476 	bp->grp_info = NULL;
4477 }
4478 
4479 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4480 {
4481 	int i;
4482 
4483 	if (irq_re_init) {
4484 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4485 				       sizeof(struct bnxt_ring_grp_info),
4486 				       GFP_KERNEL);
4487 		if (!bp->grp_info)
4488 			return -ENOMEM;
4489 	}
4490 	for (i = 0; i < bp->cp_nr_rings; i++) {
4491 		if (irq_re_init)
4492 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4493 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4494 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4495 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4496 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4497 	}
4498 	return 0;
4499 }
4500 
4501 static void bnxt_free_vnics(struct bnxt *bp)
4502 {
4503 	kfree(bp->vnic_info);
4504 	bp->vnic_info = NULL;
4505 	bp->nr_vnics = 0;
4506 }
4507 
4508 static int bnxt_alloc_vnics(struct bnxt *bp)
4509 {
4510 	int num_vnics = 1;
4511 
4512 #ifdef CONFIG_RFS_ACCEL
4513 	if (bp->flags & BNXT_FLAG_RFS) {
4514 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4515 			num_vnics++;
4516 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4517 			num_vnics += bp->rx_nr_rings;
4518 	}
4519 #endif
4520 
4521 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4522 		num_vnics++;
4523 
4524 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4525 				GFP_KERNEL);
4526 	if (!bp->vnic_info)
4527 		return -ENOMEM;
4528 
4529 	bp->nr_vnics = num_vnics;
4530 	return 0;
4531 }
4532 
4533 static void bnxt_init_vnics(struct bnxt *bp)
4534 {
4535 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4536 	int i;
4537 
4538 	for (i = 0; i < bp->nr_vnics; i++) {
4539 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4540 		int j;
4541 
4542 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4543 		vnic->vnic_id = i;
4544 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4545 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4546 
4547 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4548 
4549 		if (bp->vnic_info[i].rss_hash_key) {
4550 			if (i == BNXT_VNIC_DEFAULT) {
4551 				u8 *key = (void *)vnic->rss_hash_key;
4552 				int k;
4553 
4554 				if (!bp->rss_hash_key_valid &&
4555 				    !bp->rss_hash_key_updated) {
4556 					get_random_bytes(bp->rss_hash_key,
4557 							 HW_HASH_KEY_SIZE);
4558 					bp->rss_hash_key_updated = true;
4559 				}
4560 
4561 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4562 				       HW_HASH_KEY_SIZE);
4563 
4564 				if (!bp->rss_hash_key_updated)
4565 					continue;
4566 
4567 				bp->rss_hash_key_updated = false;
4568 				bp->rss_hash_key_valid = true;
4569 
4570 				bp->toeplitz_prefix = 0;
4571 				for (k = 0; k < 8; k++) {
4572 					bp->toeplitz_prefix <<= 8;
4573 					bp->toeplitz_prefix |= key[k];
4574 				}
4575 			} else {
4576 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4577 				       HW_HASH_KEY_SIZE);
4578 			}
4579 		}
4580 	}
4581 }
4582 
4583 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4584 {
4585 	int pages;
4586 
4587 	pages = ring_size / desc_per_pg;
4588 
4589 	if (!pages)
4590 		return 1;
4591 
4592 	pages++;
4593 
4594 	while (pages & (pages - 1))
4595 		pages++;
4596 
4597 	return pages;
4598 }
4599 
4600 void bnxt_set_tpa_flags(struct bnxt *bp)
4601 {
4602 	bp->flags &= ~BNXT_FLAG_TPA;
4603 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4604 		return;
4605 	if (bp->dev->features & NETIF_F_LRO)
4606 		bp->flags |= BNXT_FLAG_LRO;
4607 	else if (bp->dev->features & NETIF_F_GRO_HW)
4608 		bp->flags |= BNXT_FLAG_GRO;
4609 }
4610 
4611 static void bnxt_init_ring_params(struct bnxt *bp)
4612 {
4613 	unsigned int rx_size;
4614 
4615 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4616 	/* Try to fit 4 chunks into a 4k page */
4617 	rx_size = SZ_1K -
4618 		NET_SKB_PAD - SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4619 	bp->dev->cfg->hds_thresh = max(BNXT_DEFAULT_RX_COPYBREAK, rx_size);
4620 }
4621 
4622 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4623  * be set on entry.
4624  */
4625 void bnxt_set_ring_params(struct bnxt *bp)
4626 {
4627 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4628 	u32 agg_factor = 0, agg_ring_size = 0;
4629 
4630 	/* 8 for CRC and VLAN */
4631 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4632 
4633 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4634 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4635 
4636 	ring_size = bp->rx_ring_size;
4637 	bp->rx_agg_ring_size = 0;
4638 	bp->rx_agg_nr_pages = 0;
4639 
4640 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4641 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4642 
4643 	bp->flags &= ~BNXT_FLAG_JUMBO;
4644 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4645 		u32 jumbo_factor;
4646 
4647 		bp->flags |= BNXT_FLAG_JUMBO;
4648 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4649 		if (jumbo_factor > agg_factor)
4650 			agg_factor = jumbo_factor;
4651 	}
4652 	if (agg_factor) {
4653 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4654 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4655 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4656 				    bp->rx_ring_size, ring_size);
4657 			bp->rx_ring_size = ring_size;
4658 		}
4659 		agg_ring_size = ring_size * agg_factor;
4660 
4661 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4662 							RX_DESC_CNT);
4663 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4664 			u32 tmp = agg_ring_size;
4665 
4666 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4667 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4668 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4669 				    tmp, agg_ring_size);
4670 		}
4671 		bp->rx_agg_ring_size = agg_ring_size;
4672 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4673 
4674 		if (BNXT_RX_PAGE_MODE(bp)) {
4675 			rx_space = PAGE_SIZE;
4676 			rx_size = PAGE_SIZE -
4677 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4678 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4679 		} else {
4680 			rx_size = max3(BNXT_DEFAULT_RX_COPYBREAK,
4681 				       bp->rx_copybreak,
4682 				       bp->dev->cfg_pending->hds_thresh);
4683 			rx_size = SKB_DATA_ALIGN(rx_size + NET_IP_ALIGN);
4684 			rx_space = rx_size + NET_SKB_PAD +
4685 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4686 		}
4687 	}
4688 
4689 	bp->rx_buf_use_size = rx_size;
4690 	bp->rx_buf_size = rx_space;
4691 
4692 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4693 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4694 
4695 	ring_size = bp->tx_ring_size;
4696 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4697 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4698 
4699 	max_rx_cmpl = bp->rx_ring_size;
4700 	/* MAX TPA needs to be added because TPA_START completions are
4701 	 * immediately recycled, so the TPA completions are not bound by
4702 	 * the RX ring size.
4703 	 */
4704 	if (bp->flags & BNXT_FLAG_TPA)
4705 		max_rx_cmpl += bp->max_tpa;
4706 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4707 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4708 	bp->cp_ring_size = ring_size;
4709 
4710 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4711 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4712 		bp->cp_nr_pages = MAX_CP_PAGES;
4713 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4714 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4715 			    ring_size, bp->cp_ring_size);
4716 	}
4717 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4718 	bp->cp_ring_mask = bp->cp_bit - 1;
4719 }
4720 
4721 /* Changing allocation mode of RX rings.
4722  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4723  */
4724 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4725 {
4726 	struct net_device *dev = bp->dev;
4727 
4728 	if (page_mode) {
4729 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4730 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4731 
4732 		if (bp->xdp_prog->aux->xdp_has_frags)
4733 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4734 		else
4735 			dev->max_mtu =
4736 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4737 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4738 			bp->flags |= BNXT_FLAG_JUMBO;
4739 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4740 		} else {
4741 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4742 			bp->rx_skb_func = bnxt_rx_page_skb;
4743 		}
4744 		bp->rx_dir = DMA_BIDIRECTIONAL;
4745 	} else {
4746 		dev->max_mtu = bp->max_mtu;
4747 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4748 		bp->rx_dir = DMA_FROM_DEVICE;
4749 		bp->rx_skb_func = bnxt_rx_skb;
4750 	}
4751 }
4752 
4753 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4754 {
4755 	__bnxt_set_rx_skb_mode(bp, page_mode);
4756 
4757 	if (!page_mode) {
4758 		int rx, tx;
4759 
4760 		bnxt_get_max_rings(bp, &rx, &tx, true);
4761 		if (rx > 1) {
4762 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4763 			bp->dev->hw_features |= NETIF_F_LRO;
4764 		}
4765 	}
4766 
4767 	/* Update LRO and GRO_HW availability */
4768 	netdev_update_features(bp->dev);
4769 }
4770 
4771 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4772 {
4773 	int i;
4774 	struct bnxt_vnic_info *vnic;
4775 	struct pci_dev *pdev = bp->pdev;
4776 
4777 	if (!bp->vnic_info)
4778 		return;
4779 
4780 	for (i = 0; i < bp->nr_vnics; i++) {
4781 		vnic = &bp->vnic_info[i];
4782 
4783 		kfree(vnic->fw_grp_ids);
4784 		vnic->fw_grp_ids = NULL;
4785 
4786 		kfree(vnic->uc_list);
4787 		vnic->uc_list = NULL;
4788 
4789 		if (vnic->mc_list) {
4790 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4791 					  vnic->mc_list, vnic->mc_list_mapping);
4792 			vnic->mc_list = NULL;
4793 		}
4794 
4795 		if (vnic->rss_table) {
4796 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4797 					  vnic->rss_table,
4798 					  vnic->rss_table_dma_addr);
4799 			vnic->rss_table = NULL;
4800 		}
4801 
4802 		vnic->rss_hash_key = NULL;
4803 		vnic->flags = 0;
4804 	}
4805 }
4806 
4807 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4808 {
4809 	int i, rc = 0, size;
4810 	struct bnxt_vnic_info *vnic;
4811 	struct pci_dev *pdev = bp->pdev;
4812 	int max_rings;
4813 
4814 	for (i = 0; i < bp->nr_vnics; i++) {
4815 		vnic = &bp->vnic_info[i];
4816 
4817 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4818 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4819 
4820 			if (mem_size > 0) {
4821 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4822 				if (!vnic->uc_list) {
4823 					rc = -ENOMEM;
4824 					goto out;
4825 				}
4826 			}
4827 		}
4828 
4829 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4830 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4831 			vnic->mc_list =
4832 				dma_alloc_coherent(&pdev->dev,
4833 						   vnic->mc_list_size,
4834 						   &vnic->mc_list_mapping,
4835 						   GFP_KERNEL);
4836 			if (!vnic->mc_list) {
4837 				rc = -ENOMEM;
4838 				goto out;
4839 			}
4840 		}
4841 
4842 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4843 			goto vnic_skip_grps;
4844 
4845 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4846 			max_rings = bp->rx_nr_rings;
4847 		else
4848 			max_rings = 1;
4849 
4850 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4851 		if (!vnic->fw_grp_ids) {
4852 			rc = -ENOMEM;
4853 			goto out;
4854 		}
4855 vnic_skip_grps:
4856 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4857 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4858 			continue;
4859 
4860 		/* Allocate rss table and hash key */
4861 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4862 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4863 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4864 
4865 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4866 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4867 						     vnic->rss_table_size,
4868 						     &vnic->rss_table_dma_addr,
4869 						     GFP_KERNEL);
4870 		if (!vnic->rss_table) {
4871 			rc = -ENOMEM;
4872 			goto out;
4873 		}
4874 
4875 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4876 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4877 	}
4878 	return 0;
4879 
4880 out:
4881 	return rc;
4882 }
4883 
4884 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4885 {
4886 	struct bnxt_hwrm_wait_token *token;
4887 
4888 	dma_pool_destroy(bp->hwrm_dma_pool);
4889 	bp->hwrm_dma_pool = NULL;
4890 
4891 	rcu_read_lock();
4892 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4893 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4894 	rcu_read_unlock();
4895 }
4896 
4897 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4898 {
4899 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4900 					    BNXT_HWRM_DMA_SIZE,
4901 					    BNXT_HWRM_DMA_ALIGN, 0);
4902 	if (!bp->hwrm_dma_pool)
4903 		return -ENOMEM;
4904 
4905 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4906 
4907 	return 0;
4908 }
4909 
4910 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4911 {
4912 	kfree(stats->hw_masks);
4913 	stats->hw_masks = NULL;
4914 	kfree(stats->sw_stats);
4915 	stats->sw_stats = NULL;
4916 	if (stats->hw_stats) {
4917 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4918 				  stats->hw_stats_map);
4919 		stats->hw_stats = NULL;
4920 	}
4921 }
4922 
4923 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4924 				bool alloc_masks)
4925 {
4926 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4927 					     &stats->hw_stats_map, GFP_KERNEL);
4928 	if (!stats->hw_stats)
4929 		return -ENOMEM;
4930 
4931 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4932 	if (!stats->sw_stats)
4933 		goto stats_mem_err;
4934 
4935 	if (alloc_masks) {
4936 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4937 		if (!stats->hw_masks)
4938 			goto stats_mem_err;
4939 	}
4940 	return 0;
4941 
4942 stats_mem_err:
4943 	bnxt_free_stats_mem(bp, stats);
4944 	return -ENOMEM;
4945 }
4946 
4947 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4948 {
4949 	int i;
4950 
4951 	for (i = 0; i < count; i++)
4952 		mask_arr[i] = mask;
4953 }
4954 
4955 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4956 {
4957 	int i;
4958 
4959 	for (i = 0; i < count; i++)
4960 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4961 }
4962 
4963 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4964 				    struct bnxt_stats_mem *stats)
4965 {
4966 	struct hwrm_func_qstats_ext_output *resp;
4967 	struct hwrm_func_qstats_ext_input *req;
4968 	__le64 *hw_masks;
4969 	int rc;
4970 
4971 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4972 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4973 		return -EOPNOTSUPP;
4974 
4975 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4976 	if (rc)
4977 		return rc;
4978 
4979 	req->fid = cpu_to_le16(0xffff);
4980 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4981 
4982 	resp = hwrm_req_hold(bp, req);
4983 	rc = hwrm_req_send(bp, req);
4984 	if (!rc) {
4985 		hw_masks = &resp->rx_ucast_pkts;
4986 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4987 	}
4988 	hwrm_req_drop(bp, req);
4989 	return rc;
4990 }
4991 
4992 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4993 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4994 
4995 static void bnxt_init_stats(struct bnxt *bp)
4996 {
4997 	struct bnxt_napi *bnapi = bp->bnapi[0];
4998 	struct bnxt_cp_ring_info *cpr;
4999 	struct bnxt_stats_mem *stats;
5000 	__le64 *rx_stats, *tx_stats;
5001 	int rc, rx_count, tx_count;
5002 	u64 *rx_masks, *tx_masks;
5003 	u64 mask;
5004 	u8 flags;
5005 
5006 	cpr = &bnapi->cp_ring;
5007 	stats = &cpr->stats;
5008 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
5009 	if (rc) {
5010 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5011 			mask = (1ULL << 48) - 1;
5012 		else
5013 			mask = -1ULL;
5014 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5015 	}
5016 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5017 		stats = &bp->port_stats;
5018 		rx_stats = stats->hw_stats;
5019 		rx_masks = stats->hw_masks;
5020 		rx_count = sizeof(struct rx_port_stats) / 8;
5021 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5022 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5023 		tx_count = sizeof(struct tx_port_stats) / 8;
5024 
5025 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5026 		rc = bnxt_hwrm_port_qstats(bp, flags);
5027 		if (rc) {
5028 			mask = (1ULL << 40) - 1;
5029 
5030 			bnxt_fill_masks(rx_masks, mask, rx_count);
5031 			bnxt_fill_masks(tx_masks, mask, tx_count);
5032 		} else {
5033 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5034 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5035 			bnxt_hwrm_port_qstats(bp, 0);
5036 		}
5037 	}
5038 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5039 		stats = &bp->rx_port_stats_ext;
5040 		rx_stats = stats->hw_stats;
5041 		rx_masks = stats->hw_masks;
5042 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5043 		stats = &bp->tx_port_stats_ext;
5044 		tx_stats = stats->hw_stats;
5045 		tx_masks = stats->hw_masks;
5046 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5047 
5048 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5049 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5050 		if (rc) {
5051 			mask = (1ULL << 40) - 1;
5052 
5053 			bnxt_fill_masks(rx_masks, mask, rx_count);
5054 			if (tx_stats)
5055 				bnxt_fill_masks(tx_masks, mask, tx_count);
5056 		} else {
5057 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5058 			if (tx_stats)
5059 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5060 						   tx_count);
5061 			bnxt_hwrm_port_qstats_ext(bp, 0);
5062 		}
5063 	}
5064 }
5065 
5066 static void bnxt_free_port_stats(struct bnxt *bp)
5067 {
5068 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5069 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5070 
5071 	bnxt_free_stats_mem(bp, &bp->port_stats);
5072 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5073 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5074 }
5075 
5076 static void bnxt_free_ring_stats(struct bnxt *bp)
5077 {
5078 	int i;
5079 
5080 	if (!bp->bnapi)
5081 		return;
5082 
5083 	for (i = 0; i < bp->cp_nr_rings; i++) {
5084 		struct bnxt_napi *bnapi = bp->bnapi[i];
5085 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5086 
5087 		bnxt_free_stats_mem(bp, &cpr->stats);
5088 
5089 		kfree(cpr->sw_stats);
5090 		cpr->sw_stats = NULL;
5091 	}
5092 }
5093 
5094 static int bnxt_alloc_stats(struct bnxt *bp)
5095 {
5096 	u32 size, i;
5097 	int rc;
5098 
5099 	size = bp->hw_ring_stats_size;
5100 
5101 	for (i = 0; i < bp->cp_nr_rings; i++) {
5102 		struct bnxt_napi *bnapi = bp->bnapi[i];
5103 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5104 
5105 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5106 		if (!cpr->sw_stats)
5107 			return -ENOMEM;
5108 
5109 		cpr->stats.len = size;
5110 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5111 		if (rc)
5112 			return rc;
5113 
5114 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5115 	}
5116 
5117 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5118 		return 0;
5119 
5120 	if (bp->port_stats.hw_stats)
5121 		goto alloc_ext_stats;
5122 
5123 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5124 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5125 	if (rc)
5126 		return rc;
5127 
5128 	bp->flags |= BNXT_FLAG_PORT_STATS;
5129 
5130 alloc_ext_stats:
5131 	/* Display extended statistics only if FW supports it */
5132 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5133 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5134 			return 0;
5135 
5136 	if (bp->rx_port_stats_ext.hw_stats)
5137 		goto alloc_tx_ext_stats;
5138 
5139 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5140 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5141 	/* Extended stats are optional */
5142 	if (rc)
5143 		return 0;
5144 
5145 alloc_tx_ext_stats:
5146 	if (bp->tx_port_stats_ext.hw_stats)
5147 		return 0;
5148 
5149 	if (bp->hwrm_spec_code >= 0x10902 ||
5150 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5151 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5152 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5153 		/* Extended stats are optional */
5154 		if (rc)
5155 			return 0;
5156 	}
5157 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5158 	return 0;
5159 }
5160 
5161 static void bnxt_clear_ring_indices(struct bnxt *bp)
5162 {
5163 	int i, j;
5164 
5165 	if (!bp->bnapi)
5166 		return;
5167 
5168 	for (i = 0; i < bp->cp_nr_rings; i++) {
5169 		struct bnxt_napi *bnapi = bp->bnapi[i];
5170 		struct bnxt_cp_ring_info *cpr;
5171 		struct bnxt_rx_ring_info *rxr;
5172 		struct bnxt_tx_ring_info *txr;
5173 
5174 		if (!bnapi)
5175 			continue;
5176 
5177 		cpr = &bnapi->cp_ring;
5178 		cpr->cp_raw_cons = 0;
5179 
5180 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5181 			txr->tx_prod = 0;
5182 			txr->tx_cons = 0;
5183 			txr->tx_hw_cons = 0;
5184 		}
5185 
5186 		rxr = bnapi->rx_ring;
5187 		if (rxr) {
5188 			rxr->rx_prod = 0;
5189 			rxr->rx_agg_prod = 0;
5190 			rxr->rx_sw_agg_prod = 0;
5191 			rxr->rx_next_cons = 0;
5192 		}
5193 		bnapi->events = 0;
5194 	}
5195 }
5196 
5197 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5198 {
5199 	u8 type = fltr->type, flags = fltr->flags;
5200 
5201 	INIT_LIST_HEAD(&fltr->list);
5202 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5203 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5204 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5205 }
5206 
5207 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5208 {
5209 	if (!list_empty(&fltr->list))
5210 		list_del_init(&fltr->list);
5211 }
5212 
5213 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5214 {
5215 	struct bnxt_filter_base *usr_fltr, *tmp;
5216 
5217 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5218 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5219 			continue;
5220 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5221 	}
5222 }
5223 
5224 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5225 {
5226 	hlist_del(&fltr->hash);
5227 	bnxt_del_one_usr_fltr(bp, fltr);
5228 	if (fltr->flags) {
5229 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5230 		bp->ntp_fltr_count--;
5231 	}
5232 	kfree(fltr);
5233 }
5234 
5235 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5236 {
5237 	int i;
5238 
5239 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5240 	 * safe to delete the hash table.
5241 	 */
5242 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5243 		struct hlist_head *head;
5244 		struct hlist_node *tmp;
5245 		struct bnxt_ntuple_filter *fltr;
5246 
5247 		head = &bp->ntp_fltr_hash_tbl[i];
5248 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5249 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5250 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5251 				     !list_empty(&fltr->base.list)))
5252 				continue;
5253 			bnxt_del_fltr(bp, &fltr->base);
5254 		}
5255 	}
5256 	if (!all)
5257 		return;
5258 
5259 	bitmap_free(bp->ntp_fltr_bmap);
5260 	bp->ntp_fltr_bmap = NULL;
5261 	bp->ntp_fltr_count = 0;
5262 }
5263 
5264 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5265 {
5266 	int i, rc = 0;
5267 
5268 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5269 		return 0;
5270 
5271 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5272 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5273 
5274 	bp->ntp_fltr_count = 0;
5275 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5276 
5277 	if (!bp->ntp_fltr_bmap)
5278 		rc = -ENOMEM;
5279 
5280 	return rc;
5281 }
5282 
5283 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5284 {
5285 	int i;
5286 
5287 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5288 		struct hlist_head *head;
5289 		struct hlist_node *tmp;
5290 		struct bnxt_l2_filter *fltr;
5291 
5292 		head = &bp->l2_fltr_hash_tbl[i];
5293 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5294 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5295 				     !list_empty(&fltr->base.list)))
5296 				continue;
5297 			bnxt_del_fltr(bp, &fltr->base);
5298 		}
5299 	}
5300 }
5301 
5302 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5303 {
5304 	int i;
5305 
5306 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5307 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5308 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5309 }
5310 
5311 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5312 {
5313 	bnxt_free_vnic_attributes(bp);
5314 	bnxt_free_tx_rings(bp);
5315 	bnxt_free_rx_rings(bp);
5316 	bnxt_free_cp_rings(bp);
5317 	bnxt_free_all_cp_arrays(bp);
5318 	bnxt_free_ntp_fltrs(bp, false);
5319 	bnxt_free_l2_filters(bp, false);
5320 	if (irq_re_init) {
5321 		bnxt_free_ring_stats(bp);
5322 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5323 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5324 			bnxt_free_port_stats(bp);
5325 		bnxt_free_ring_grps(bp);
5326 		bnxt_free_vnics(bp);
5327 		kfree(bp->tx_ring_map);
5328 		bp->tx_ring_map = NULL;
5329 		kfree(bp->tx_ring);
5330 		bp->tx_ring = NULL;
5331 		kfree(bp->rx_ring);
5332 		bp->rx_ring = NULL;
5333 		kfree(bp->bnapi);
5334 		bp->bnapi = NULL;
5335 	} else {
5336 		bnxt_clear_ring_indices(bp);
5337 	}
5338 }
5339 
5340 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5341 {
5342 	int i, j, rc, size, arr_size;
5343 	void *bnapi;
5344 
5345 	if (irq_re_init) {
5346 		/* Allocate bnapi mem pointer array and mem block for
5347 		 * all queues
5348 		 */
5349 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5350 				bp->cp_nr_rings);
5351 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5352 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5353 		if (!bnapi)
5354 			return -ENOMEM;
5355 
5356 		bp->bnapi = bnapi;
5357 		bnapi += arr_size;
5358 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5359 			bp->bnapi[i] = bnapi;
5360 			bp->bnapi[i]->index = i;
5361 			bp->bnapi[i]->bp = bp;
5362 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5363 				struct bnxt_cp_ring_info *cpr =
5364 					&bp->bnapi[i]->cp_ring;
5365 
5366 				cpr->cp_ring_struct.ring_mem.flags =
5367 					BNXT_RMEM_RING_PTE_FLAG;
5368 			}
5369 		}
5370 
5371 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5372 				      sizeof(struct bnxt_rx_ring_info),
5373 				      GFP_KERNEL);
5374 		if (!bp->rx_ring)
5375 			return -ENOMEM;
5376 
5377 		for (i = 0; i < bp->rx_nr_rings; i++) {
5378 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5379 
5380 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5381 				rxr->rx_ring_struct.ring_mem.flags =
5382 					BNXT_RMEM_RING_PTE_FLAG;
5383 				rxr->rx_agg_ring_struct.ring_mem.flags =
5384 					BNXT_RMEM_RING_PTE_FLAG;
5385 			} else {
5386 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5387 			}
5388 			rxr->bnapi = bp->bnapi[i];
5389 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5390 		}
5391 
5392 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5393 				      sizeof(struct bnxt_tx_ring_info),
5394 				      GFP_KERNEL);
5395 		if (!bp->tx_ring)
5396 			return -ENOMEM;
5397 
5398 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5399 					  GFP_KERNEL);
5400 
5401 		if (!bp->tx_ring_map)
5402 			return -ENOMEM;
5403 
5404 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5405 			j = 0;
5406 		else
5407 			j = bp->rx_nr_rings;
5408 
5409 		for (i = 0; i < bp->tx_nr_rings; i++) {
5410 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5411 			struct bnxt_napi *bnapi2;
5412 
5413 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5414 				txr->tx_ring_struct.ring_mem.flags =
5415 					BNXT_RMEM_RING_PTE_FLAG;
5416 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5417 			if (i >= bp->tx_nr_rings_xdp) {
5418 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5419 
5420 				bnapi2 = bp->bnapi[k];
5421 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5422 				txr->tx_napi_idx =
5423 					BNXT_RING_TO_TC(bp, txr->txq_index);
5424 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5425 				bnapi2->tx_int = bnxt_tx_int;
5426 			} else {
5427 				bnapi2 = bp->bnapi[j];
5428 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5429 				bnapi2->tx_ring[0] = txr;
5430 				bnapi2->tx_int = bnxt_tx_int_xdp;
5431 				j++;
5432 			}
5433 			txr->bnapi = bnapi2;
5434 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5435 				txr->tx_cpr = &bnapi2->cp_ring;
5436 		}
5437 
5438 		rc = bnxt_alloc_stats(bp);
5439 		if (rc)
5440 			goto alloc_mem_err;
5441 		bnxt_init_stats(bp);
5442 
5443 		rc = bnxt_alloc_ntp_fltrs(bp);
5444 		if (rc)
5445 			goto alloc_mem_err;
5446 
5447 		rc = bnxt_alloc_vnics(bp);
5448 		if (rc)
5449 			goto alloc_mem_err;
5450 	}
5451 
5452 	rc = bnxt_alloc_all_cp_arrays(bp);
5453 	if (rc)
5454 		goto alloc_mem_err;
5455 
5456 	bnxt_init_ring_struct(bp);
5457 
5458 	rc = bnxt_alloc_rx_rings(bp);
5459 	if (rc)
5460 		goto alloc_mem_err;
5461 
5462 	rc = bnxt_alloc_tx_rings(bp);
5463 	if (rc)
5464 		goto alloc_mem_err;
5465 
5466 	rc = bnxt_alloc_cp_rings(bp);
5467 	if (rc)
5468 		goto alloc_mem_err;
5469 
5470 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5471 						  BNXT_VNIC_MCAST_FLAG |
5472 						  BNXT_VNIC_UCAST_FLAG;
5473 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5474 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5475 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5476 
5477 	rc = bnxt_alloc_vnic_attributes(bp);
5478 	if (rc)
5479 		goto alloc_mem_err;
5480 	return 0;
5481 
5482 alloc_mem_err:
5483 	bnxt_free_mem(bp, true);
5484 	return rc;
5485 }
5486 
5487 static void bnxt_disable_int(struct bnxt *bp)
5488 {
5489 	int i;
5490 
5491 	if (!bp->bnapi)
5492 		return;
5493 
5494 	for (i = 0; i < bp->cp_nr_rings; i++) {
5495 		struct bnxt_napi *bnapi = bp->bnapi[i];
5496 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5497 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5498 
5499 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5500 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5501 	}
5502 }
5503 
5504 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5505 {
5506 	struct bnxt_napi *bnapi = bp->bnapi[n];
5507 	struct bnxt_cp_ring_info *cpr;
5508 
5509 	cpr = &bnapi->cp_ring;
5510 	return cpr->cp_ring_struct.map_idx;
5511 }
5512 
5513 static void bnxt_disable_int_sync(struct bnxt *bp)
5514 {
5515 	int i;
5516 
5517 	if (!bp->irq_tbl)
5518 		return;
5519 
5520 	atomic_inc(&bp->intr_sem);
5521 
5522 	bnxt_disable_int(bp);
5523 	for (i = 0; i < bp->cp_nr_rings; i++) {
5524 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5525 
5526 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5527 	}
5528 }
5529 
5530 static void bnxt_enable_int(struct bnxt *bp)
5531 {
5532 	int i;
5533 
5534 	atomic_set(&bp->intr_sem, 0);
5535 	for (i = 0; i < bp->cp_nr_rings; i++) {
5536 		struct bnxt_napi *bnapi = bp->bnapi[i];
5537 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5538 
5539 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5540 	}
5541 }
5542 
5543 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5544 			    bool async_only)
5545 {
5546 	DECLARE_BITMAP(async_events_bmap, 256);
5547 	u32 *events = (u32 *)async_events_bmap;
5548 	struct hwrm_func_drv_rgtr_output *resp;
5549 	struct hwrm_func_drv_rgtr_input *req;
5550 	u32 flags;
5551 	int rc, i;
5552 
5553 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5554 	if (rc)
5555 		return rc;
5556 
5557 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5558 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5559 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5560 
5561 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5562 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5563 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5564 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5565 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5566 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5567 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5568 	req->flags = cpu_to_le32(flags);
5569 	req->ver_maj_8b = DRV_VER_MAJ;
5570 	req->ver_min_8b = DRV_VER_MIN;
5571 	req->ver_upd_8b = DRV_VER_UPD;
5572 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5573 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5574 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5575 
5576 	if (BNXT_PF(bp)) {
5577 		u32 data[8];
5578 		int i;
5579 
5580 		memset(data, 0, sizeof(data));
5581 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5582 			u16 cmd = bnxt_vf_req_snif[i];
5583 			unsigned int bit, idx;
5584 
5585 			idx = cmd / 32;
5586 			bit = cmd % 32;
5587 			data[idx] |= 1 << bit;
5588 		}
5589 
5590 		for (i = 0; i < 8; i++)
5591 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5592 
5593 		req->enables |=
5594 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5595 	}
5596 
5597 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5598 		req->flags |= cpu_to_le32(
5599 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5600 
5601 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5602 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5603 		u16 event_id = bnxt_async_events_arr[i];
5604 
5605 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5606 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5607 			continue;
5608 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5609 		    !bp->ptp_cfg)
5610 			continue;
5611 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5612 	}
5613 	if (bmap && bmap_size) {
5614 		for (i = 0; i < bmap_size; i++) {
5615 			if (test_bit(i, bmap))
5616 				__set_bit(i, async_events_bmap);
5617 		}
5618 	}
5619 	for (i = 0; i < 8; i++)
5620 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5621 
5622 	if (async_only)
5623 		req->enables =
5624 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5625 
5626 	resp = hwrm_req_hold(bp, req);
5627 	rc = hwrm_req_send(bp, req);
5628 	if (!rc) {
5629 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5630 		if (resp->flags &
5631 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5632 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5633 	}
5634 	hwrm_req_drop(bp, req);
5635 	return rc;
5636 }
5637 
5638 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5639 {
5640 	struct hwrm_func_drv_unrgtr_input *req;
5641 	int rc;
5642 
5643 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5644 		return 0;
5645 
5646 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5647 	if (rc)
5648 		return rc;
5649 	return hwrm_req_send(bp, req);
5650 }
5651 
5652 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5653 
5654 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5655 {
5656 	struct hwrm_tunnel_dst_port_free_input *req;
5657 	int rc;
5658 
5659 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5660 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5661 		return 0;
5662 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5663 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5664 		return 0;
5665 
5666 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5667 	if (rc)
5668 		return rc;
5669 
5670 	req->tunnel_type = tunnel_type;
5671 
5672 	switch (tunnel_type) {
5673 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5674 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5675 		bp->vxlan_port = 0;
5676 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5677 		break;
5678 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5679 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5680 		bp->nge_port = 0;
5681 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5682 		break;
5683 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5684 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5685 		bp->vxlan_gpe_port = 0;
5686 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5687 		break;
5688 	default:
5689 		break;
5690 	}
5691 
5692 	rc = hwrm_req_send(bp, req);
5693 	if (rc)
5694 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5695 			   rc);
5696 	if (bp->flags & BNXT_FLAG_TPA)
5697 		bnxt_set_tpa(bp, true);
5698 	return rc;
5699 }
5700 
5701 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5702 					   u8 tunnel_type)
5703 {
5704 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5705 	struct hwrm_tunnel_dst_port_alloc_input *req;
5706 	int rc;
5707 
5708 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5709 	if (rc)
5710 		return rc;
5711 
5712 	req->tunnel_type = tunnel_type;
5713 	req->tunnel_dst_port_val = port;
5714 
5715 	resp = hwrm_req_hold(bp, req);
5716 	rc = hwrm_req_send(bp, req);
5717 	if (rc) {
5718 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5719 			   rc);
5720 		goto err_out;
5721 	}
5722 
5723 	switch (tunnel_type) {
5724 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5725 		bp->vxlan_port = port;
5726 		bp->vxlan_fw_dst_port_id =
5727 			le16_to_cpu(resp->tunnel_dst_port_id);
5728 		break;
5729 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5730 		bp->nge_port = port;
5731 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5732 		break;
5733 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5734 		bp->vxlan_gpe_port = port;
5735 		bp->vxlan_gpe_fw_dst_port_id =
5736 			le16_to_cpu(resp->tunnel_dst_port_id);
5737 		break;
5738 	default:
5739 		break;
5740 	}
5741 	if (bp->flags & BNXT_FLAG_TPA)
5742 		bnxt_set_tpa(bp, true);
5743 
5744 err_out:
5745 	hwrm_req_drop(bp, req);
5746 	return rc;
5747 }
5748 
5749 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5750 {
5751 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5752 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5753 	int rc;
5754 
5755 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5756 	if (rc)
5757 		return rc;
5758 
5759 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5760 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5761 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5762 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5763 	}
5764 	req->mask = cpu_to_le32(vnic->rx_mask);
5765 	return hwrm_req_send_silent(bp, req);
5766 }
5767 
5768 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5769 {
5770 	if (!atomic_dec_and_test(&fltr->refcnt))
5771 		return;
5772 	spin_lock_bh(&bp->ntp_fltr_lock);
5773 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5774 		spin_unlock_bh(&bp->ntp_fltr_lock);
5775 		return;
5776 	}
5777 	hlist_del_rcu(&fltr->base.hash);
5778 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5779 	if (fltr->base.flags) {
5780 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5781 		bp->ntp_fltr_count--;
5782 	}
5783 	spin_unlock_bh(&bp->ntp_fltr_lock);
5784 	kfree_rcu(fltr, base.rcu);
5785 }
5786 
5787 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5788 						      struct bnxt_l2_key *key,
5789 						      u32 idx)
5790 {
5791 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5792 	struct bnxt_l2_filter *fltr;
5793 
5794 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5795 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5796 
5797 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5798 		    l2_key->vlan == key->vlan)
5799 			return fltr;
5800 	}
5801 	return NULL;
5802 }
5803 
5804 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5805 						    struct bnxt_l2_key *key,
5806 						    u32 idx)
5807 {
5808 	struct bnxt_l2_filter *fltr = NULL;
5809 
5810 	rcu_read_lock();
5811 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5812 	if (fltr)
5813 		atomic_inc(&fltr->refcnt);
5814 	rcu_read_unlock();
5815 	return fltr;
5816 }
5817 
5818 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5819 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5820 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5821 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5822 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5823 
5824 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5825 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5826 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5827 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5828 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5829 
5830 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5831 {
5832 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5833 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5834 			return sizeof(fkeys->addrs.v4addrs) +
5835 			       sizeof(fkeys->ports);
5836 
5837 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5838 			return sizeof(fkeys->addrs.v4addrs);
5839 	}
5840 
5841 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5842 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5843 			return sizeof(fkeys->addrs.v6addrs) +
5844 			       sizeof(fkeys->ports);
5845 
5846 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5847 			return sizeof(fkeys->addrs.v6addrs);
5848 	}
5849 
5850 	return 0;
5851 }
5852 
5853 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5854 			 const unsigned char *key)
5855 {
5856 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5857 	struct bnxt_ipv4_tuple tuple4;
5858 	struct bnxt_ipv6_tuple tuple6;
5859 	int i, j, len = 0;
5860 	u8 *four_tuple;
5861 
5862 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5863 	if (!len)
5864 		return 0;
5865 
5866 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5867 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5868 		tuple4.ports = fkeys->ports;
5869 		four_tuple = (unsigned char *)&tuple4;
5870 	} else {
5871 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5872 		tuple6.ports = fkeys->ports;
5873 		four_tuple = (unsigned char *)&tuple6;
5874 	}
5875 
5876 	for (i = 0, j = 8; i < len; i++, j++) {
5877 		u8 byte = four_tuple[i];
5878 		int bit;
5879 
5880 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5881 			if (byte & 0x80)
5882 				hash ^= prefix;
5883 		}
5884 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5885 	}
5886 
5887 	/* The valid part of the hash is in the upper 32 bits. */
5888 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5889 }
5890 
5891 #ifdef CONFIG_RFS_ACCEL
5892 static struct bnxt_l2_filter *
5893 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5894 {
5895 	struct bnxt_l2_filter *fltr;
5896 	u32 idx;
5897 
5898 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5899 	      BNXT_L2_FLTR_HASH_MASK;
5900 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5901 	return fltr;
5902 }
5903 #endif
5904 
5905 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5906 			       struct bnxt_l2_key *key, u32 idx)
5907 {
5908 	struct hlist_head *head;
5909 
5910 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5911 	fltr->l2_key.vlan = key->vlan;
5912 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5913 	if (fltr->base.flags) {
5914 		int bit_id;
5915 
5916 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5917 						 bp->max_fltr, 0);
5918 		if (bit_id < 0)
5919 			return -ENOMEM;
5920 		fltr->base.sw_id = (u16)bit_id;
5921 		bp->ntp_fltr_count++;
5922 	}
5923 	head = &bp->l2_fltr_hash_tbl[idx];
5924 	hlist_add_head_rcu(&fltr->base.hash, head);
5925 	bnxt_insert_usr_fltr(bp, &fltr->base);
5926 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5927 	atomic_set(&fltr->refcnt, 1);
5928 	return 0;
5929 }
5930 
5931 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5932 						   struct bnxt_l2_key *key,
5933 						   gfp_t gfp)
5934 {
5935 	struct bnxt_l2_filter *fltr;
5936 	u32 idx;
5937 	int rc;
5938 
5939 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5940 	      BNXT_L2_FLTR_HASH_MASK;
5941 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5942 	if (fltr)
5943 		return fltr;
5944 
5945 	fltr = kzalloc(sizeof(*fltr), gfp);
5946 	if (!fltr)
5947 		return ERR_PTR(-ENOMEM);
5948 	spin_lock_bh(&bp->ntp_fltr_lock);
5949 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5950 	spin_unlock_bh(&bp->ntp_fltr_lock);
5951 	if (rc) {
5952 		bnxt_del_l2_filter(bp, fltr);
5953 		fltr = ERR_PTR(rc);
5954 	}
5955 	return fltr;
5956 }
5957 
5958 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5959 						struct bnxt_l2_key *key,
5960 						u16 flags)
5961 {
5962 	struct bnxt_l2_filter *fltr;
5963 	u32 idx;
5964 	int rc;
5965 
5966 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5967 	      BNXT_L2_FLTR_HASH_MASK;
5968 	spin_lock_bh(&bp->ntp_fltr_lock);
5969 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5970 	if (fltr) {
5971 		fltr = ERR_PTR(-EEXIST);
5972 		goto l2_filter_exit;
5973 	}
5974 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5975 	if (!fltr) {
5976 		fltr = ERR_PTR(-ENOMEM);
5977 		goto l2_filter_exit;
5978 	}
5979 	fltr->base.flags = flags;
5980 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5981 	if (rc) {
5982 		spin_unlock_bh(&bp->ntp_fltr_lock);
5983 		bnxt_del_l2_filter(bp, fltr);
5984 		return ERR_PTR(rc);
5985 	}
5986 
5987 l2_filter_exit:
5988 	spin_unlock_bh(&bp->ntp_fltr_lock);
5989 	return fltr;
5990 }
5991 
5992 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5993 {
5994 #ifdef CONFIG_BNXT_SRIOV
5995 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5996 
5997 	return vf->fw_fid;
5998 #else
5999 	return INVALID_HW_RING_ID;
6000 #endif
6001 }
6002 
6003 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6004 {
6005 	struct hwrm_cfa_l2_filter_free_input *req;
6006 	u16 target_id = 0xffff;
6007 	int rc;
6008 
6009 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6010 		struct bnxt_pf_info *pf = &bp->pf;
6011 
6012 		if (fltr->base.vf_idx >= pf->active_vfs)
6013 			return -EINVAL;
6014 
6015 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6016 		if (target_id == INVALID_HW_RING_ID)
6017 			return -EINVAL;
6018 	}
6019 
6020 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6021 	if (rc)
6022 		return rc;
6023 
6024 	req->target_id = cpu_to_le16(target_id);
6025 	req->l2_filter_id = fltr->base.filter_id;
6026 	return hwrm_req_send(bp, req);
6027 }
6028 
6029 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6030 {
6031 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6032 	struct hwrm_cfa_l2_filter_alloc_input *req;
6033 	u16 target_id = 0xffff;
6034 	int rc;
6035 
6036 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6037 		struct bnxt_pf_info *pf = &bp->pf;
6038 
6039 		if (fltr->base.vf_idx >= pf->active_vfs)
6040 			return -EINVAL;
6041 
6042 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6043 	}
6044 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6045 	if (rc)
6046 		return rc;
6047 
6048 	req->target_id = cpu_to_le16(target_id);
6049 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6050 
6051 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6052 		req->flags |=
6053 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6054 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6055 	req->enables =
6056 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6057 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6058 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6059 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6060 	eth_broadcast_addr(req->l2_addr_mask);
6061 
6062 	if (fltr->l2_key.vlan) {
6063 		req->enables |=
6064 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6065 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6066 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6067 		req->num_vlans = 1;
6068 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6069 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6070 	}
6071 
6072 	resp = hwrm_req_hold(bp, req);
6073 	rc = hwrm_req_send(bp, req);
6074 	if (!rc) {
6075 		fltr->base.filter_id = resp->l2_filter_id;
6076 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6077 	}
6078 	hwrm_req_drop(bp, req);
6079 	return rc;
6080 }
6081 
6082 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6083 				     struct bnxt_ntuple_filter *fltr)
6084 {
6085 	struct hwrm_cfa_ntuple_filter_free_input *req;
6086 	int rc;
6087 
6088 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6089 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6090 	if (rc)
6091 		return rc;
6092 
6093 	req->ntuple_filter_id = fltr->base.filter_id;
6094 	return hwrm_req_send(bp, req);
6095 }
6096 
6097 #define BNXT_NTP_FLTR_FLAGS					\
6098 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6099 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6100 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6101 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6102 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6103 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6104 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6105 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6106 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6107 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6108 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6109 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6110 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6111 
6112 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6113 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6114 
6115 void bnxt_fill_ipv6_mask(__be32 mask[4])
6116 {
6117 	int i;
6118 
6119 	for (i = 0; i < 4; i++)
6120 		mask[i] = cpu_to_be32(~0);
6121 }
6122 
6123 static void
6124 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6125 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6126 			  struct bnxt_ntuple_filter *fltr)
6127 {
6128 	u16 rxq = fltr->base.rxq;
6129 
6130 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6131 		struct ethtool_rxfh_context *ctx;
6132 		struct bnxt_rss_ctx *rss_ctx;
6133 		struct bnxt_vnic_info *vnic;
6134 
6135 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6136 			      fltr->base.fw_vnic_id);
6137 		if (ctx) {
6138 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6139 			vnic = &rss_ctx->vnic;
6140 
6141 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6142 		}
6143 		return;
6144 	}
6145 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6146 		struct bnxt_vnic_info *vnic;
6147 		u32 enables;
6148 
6149 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6150 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6151 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6152 		req->enables |= cpu_to_le32(enables);
6153 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6154 	} else {
6155 		u32 flags;
6156 
6157 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6158 		req->flags |= cpu_to_le32(flags);
6159 		req->dst_id = cpu_to_le16(rxq);
6160 	}
6161 }
6162 
6163 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6164 				      struct bnxt_ntuple_filter *fltr)
6165 {
6166 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6167 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6168 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6169 	struct flow_keys *keys = &fltr->fkeys;
6170 	struct bnxt_l2_filter *l2_fltr;
6171 	struct bnxt_vnic_info *vnic;
6172 	int rc;
6173 
6174 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6175 	if (rc)
6176 		return rc;
6177 
6178 	l2_fltr = fltr->l2_fltr;
6179 	req->l2_filter_id = l2_fltr->base.filter_id;
6180 
6181 	if (fltr->base.flags & BNXT_ACT_DROP) {
6182 		req->flags =
6183 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6184 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6185 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6186 	} else {
6187 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6188 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6189 	}
6190 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6191 
6192 	req->ethertype = htons(ETH_P_IP);
6193 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6194 	req->ip_protocol = keys->basic.ip_proto;
6195 
6196 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6197 		req->ethertype = htons(ETH_P_IPV6);
6198 		req->ip_addr_type =
6199 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6200 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6201 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6202 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6203 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6204 	} else {
6205 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6206 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6207 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6208 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6209 	}
6210 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6211 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6212 		req->tunnel_type =
6213 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6214 	}
6215 
6216 	req->src_port = keys->ports.src;
6217 	req->src_port_mask = masks->ports.src;
6218 	req->dst_port = keys->ports.dst;
6219 	req->dst_port_mask = masks->ports.dst;
6220 
6221 	resp = hwrm_req_hold(bp, req);
6222 	rc = hwrm_req_send(bp, req);
6223 	if (!rc)
6224 		fltr->base.filter_id = resp->ntuple_filter_id;
6225 	hwrm_req_drop(bp, req);
6226 	return rc;
6227 }
6228 
6229 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6230 				     const u8 *mac_addr)
6231 {
6232 	struct bnxt_l2_filter *fltr;
6233 	struct bnxt_l2_key key;
6234 	int rc;
6235 
6236 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6237 	key.vlan = 0;
6238 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6239 	if (IS_ERR(fltr))
6240 		return PTR_ERR(fltr);
6241 
6242 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6243 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6244 	if (rc)
6245 		bnxt_del_l2_filter(bp, fltr);
6246 	else
6247 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6248 	return rc;
6249 }
6250 
6251 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6252 {
6253 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6254 
6255 	/* Any associated ntuple filters will also be cleared by firmware. */
6256 	for (i = 0; i < num_of_vnics; i++) {
6257 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6258 
6259 		for (j = 0; j < vnic->uc_filter_count; j++) {
6260 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6261 
6262 			bnxt_hwrm_l2_filter_free(bp, fltr);
6263 			bnxt_del_l2_filter(bp, fltr);
6264 		}
6265 		vnic->uc_filter_count = 0;
6266 	}
6267 }
6268 
6269 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6270 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6271 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6272 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6273 
6274 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6275 					   struct hwrm_vnic_tpa_cfg_input *req)
6276 {
6277 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6278 
6279 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6280 		return;
6281 
6282 	if (bp->vxlan_port)
6283 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6284 	if (bp->vxlan_gpe_port)
6285 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6286 	if (bp->nge_port)
6287 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6288 
6289 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6290 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6291 }
6292 
6293 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6294 			   u32 tpa_flags)
6295 {
6296 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6297 	struct hwrm_vnic_tpa_cfg_input *req;
6298 	int rc;
6299 
6300 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6301 		return 0;
6302 
6303 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6304 	if (rc)
6305 		return rc;
6306 
6307 	if (tpa_flags) {
6308 		u16 mss = bp->dev->mtu - 40;
6309 		u32 nsegs, n, segs = 0, flags;
6310 
6311 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6312 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6313 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6314 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6315 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6316 		if (tpa_flags & BNXT_FLAG_GRO)
6317 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6318 
6319 		req->flags = cpu_to_le32(flags);
6320 
6321 		req->enables =
6322 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6323 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6324 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6325 
6326 		/* Number of segs are log2 units, and first packet is not
6327 		 * included as part of this units.
6328 		 */
6329 		if (mss <= BNXT_RX_PAGE_SIZE) {
6330 			n = BNXT_RX_PAGE_SIZE / mss;
6331 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6332 		} else {
6333 			n = mss / BNXT_RX_PAGE_SIZE;
6334 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6335 				n++;
6336 			nsegs = (MAX_SKB_FRAGS - n) / n;
6337 		}
6338 
6339 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6340 			segs = MAX_TPA_SEGS_P5;
6341 			max_aggs = bp->max_tpa;
6342 		} else {
6343 			segs = ilog2(nsegs);
6344 		}
6345 		req->max_agg_segs = cpu_to_le16(segs);
6346 		req->max_aggs = cpu_to_le16(max_aggs);
6347 
6348 		req->min_agg_len = cpu_to_le32(512);
6349 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6350 	}
6351 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6352 
6353 	return hwrm_req_send(bp, req);
6354 }
6355 
6356 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6357 {
6358 	struct bnxt_ring_grp_info *grp_info;
6359 
6360 	grp_info = &bp->grp_info[ring->grp_idx];
6361 	return grp_info->cp_fw_ring_id;
6362 }
6363 
6364 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6365 {
6366 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6367 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6368 	else
6369 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6370 }
6371 
6372 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6373 {
6374 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6375 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6376 	else
6377 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6378 }
6379 
6380 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6381 {
6382 	int entries;
6383 
6384 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6385 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6386 	else
6387 		entries = HW_HASH_INDEX_SIZE;
6388 
6389 	bp->rss_indir_tbl_entries = entries;
6390 	bp->rss_indir_tbl =
6391 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6392 	if (!bp->rss_indir_tbl)
6393 		return -ENOMEM;
6394 
6395 	return 0;
6396 }
6397 
6398 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6399 				 struct ethtool_rxfh_context *rss_ctx)
6400 {
6401 	u16 max_rings, max_entries, pad, i;
6402 	u32 *rss_indir_tbl;
6403 
6404 	if (!bp->rx_nr_rings)
6405 		return;
6406 
6407 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6408 		max_rings = bp->rx_nr_rings - 1;
6409 	else
6410 		max_rings = bp->rx_nr_rings;
6411 
6412 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6413 	if (rss_ctx)
6414 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6415 	else
6416 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6417 
6418 	for (i = 0; i < max_entries; i++)
6419 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6420 
6421 	pad = bp->rss_indir_tbl_entries - max_entries;
6422 	if (pad)
6423 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6424 }
6425 
6426 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6427 {
6428 	u32 i, tbl_size, max_ring = 0;
6429 
6430 	if (!bp->rss_indir_tbl)
6431 		return 0;
6432 
6433 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6434 	for (i = 0; i < tbl_size; i++)
6435 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6436 	return max_ring;
6437 }
6438 
6439 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6440 {
6441 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6442 		if (!rx_rings)
6443 			return 0;
6444 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6445 					       BNXT_RSS_TABLE_ENTRIES_P5);
6446 	}
6447 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6448 		return 2;
6449 	return 1;
6450 }
6451 
6452 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6453 {
6454 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6455 	u16 i, j;
6456 
6457 	/* Fill the RSS indirection table with ring group ids */
6458 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6459 		if (!no_rss)
6460 			j = bp->rss_indir_tbl[i];
6461 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6462 	}
6463 }
6464 
6465 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6466 				    struct bnxt_vnic_info *vnic)
6467 {
6468 	__le16 *ring_tbl = vnic->rss_table;
6469 	struct bnxt_rx_ring_info *rxr;
6470 	u16 tbl_size, i;
6471 
6472 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6473 
6474 	for (i = 0; i < tbl_size; i++) {
6475 		u16 ring_id, j;
6476 
6477 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6478 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6479 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6480 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6481 		else
6482 			j = bp->rss_indir_tbl[i];
6483 		rxr = &bp->rx_ring[j];
6484 
6485 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6486 		*ring_tbl++ = cpu_to_le16(ring_id);
6487 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6488 		*ring_tbl++ = cpu_to_le16(ring_id);
6489 	}
6490 }
6491 
6492 static void
6493 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6494 			 struct bnxt_vnic_info *vnic)
6495 {
6496 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6497 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6498 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6499 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6500 	} else {
6501 		bnxt_fill_hw_rss_tbl(bp, vnic);
6502 	}
6503 
6504 	if (bp->rss_hash_delta) {
6505 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6506 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6507 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6508 		else
6509 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6510 	} else {
6511 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6512 	}
6513 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6514 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6515 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6516 }
6517 
6518 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6519 				  bool set_rss)
6520 {
6521 	struct hwrm_vnic_rss_cfg_input *req;
6522 	int rc;
6523 
6524 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6525 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6526 		return 0;
6527 
6528 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6529 	if (rc)
6530 		return rc;
6531 
6532 	if (set_rss)
6533 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6534 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6535 	return hwrm_req_send(bp, req);
6536 }
6537 
6538 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6539 				     struct bnxt_vnic_info *vnic, bool set_rss)
6540 {
6541 	struct hwrm_vnic_rss_cfg_input *req;
6542 	dma_addr_t ring_tbl_map;
6543 	u32 i, nr_ctxs;
6544 	int rc;
6545 
6546 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6547 	if (rc)
6548 		return rc;
6549 
6550 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6551 	if (!set_rss)
6552 		return hwrm_req_send(bp, req);
6553 
6554 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6555 	ring_tbl_map = vnic->rss_table_dma_addr;
6556 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6557 
6558 	hwrm_req_hold(bp, req);
6559 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6560 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6561 		req->ring_table_pair_index = i;
6562 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6563 		rc = hwrm_req_send(bp, req);
6564 		if (rc)
6565 			goto exit;
6566 	}
6567 
6568 exit:
6569 	hwrm_req_drop(bp, req);
6570 	return rc;
6571 }
6572 
6573 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6574 {
6575 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6576 	struct hwrm_vnic_rss_qcfg_output *resp;
6577 	struct hwrm_vnic_rss_qcfg_input *req;
6578 
6579 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6580 		return;
6581 
6582 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6583 	/* all contexts configured to same hash_type, zero always exists */
6584 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6585 	resp = hwrm_req_hold(bp, req);
6586 	if (!hwrm_req_send(bp, req)) {
6587 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6588 		bp->rss_hash_delta = 0;
6589 	}
6590 	hwrm_req_drop(bp, req);
6591 }
6592 
6593 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6594 {
6595 	u16 hds_thresh = (u16)bp->dev->cfg_pending->hds_thresh;
6596 	struct hwrm_vnic_plcmodes_cfg_input *req;
6597 	int rc;
6598 
6599 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6600 	if (rc)
6601 		return rc;
6602 
6603 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6604 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6605 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6606 
6607 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6608 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6609 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6610 		req->enables |=
6611 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6612 		req->hds_threshold = cpu_to_le16(hds_thresh);
6613 	}
6614 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6615 	return hwrm_req_send(bp, req);
6616 }
6617 
6618 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6619 					struct bnxt_vnic_info *vnic,
6620 					u16 ctx_idx)
6621 {
6622 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6623 
6624 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6625 		return;
6626 
6627 	req->rss_cos_lb_ctx_id =
6628 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6629 
6630 	hwrm_req_send(bp, req);
6631 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6632 }
6633 
6634 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6635 {
6636 	int i, j;
6637 
6638 	for (i = 0; i < bp->nr_vnics; i++) {
6639 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6640 
6641 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6642 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6643 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6644 		}
6645 	}
6646 	bp->rsscos_nr_ctxs = 0;
6647 }
6648 
6649 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6650 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6651 {
6652 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6653 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6654 	int rc;
6655 
6656 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6657 	if (rc)
6658 		return rc;
6659 
6660 	resp = hwrm_req_hold(bp, req);
6661 	rc = hwrm_req_send(bp, req);
6662 	if (!rc)
6663 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6664 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6665 	hwrm_req_drop(bp, req);
6666 
6667 	return rc;
6668 }
6669 
6670 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6671 {
6672 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6673 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6674 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6675 }
6676 
6677 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6678 {
6679 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6680 	struct hwrm_vnic_cfg_input *req;
6681 	unsigned int ring = 0, grp_idx;
6682 	u16 def_vlan = 0;
6683 	int rc;
6684 
6685 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6686 	if (rc)
6687 		return rc;
6688 
6689 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6690 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6691 
6692 		req->default_rx_ring_id =
6693 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6694 		req->default_cmpl_ring_id =
6695 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6696 		req->enables =
6697 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6698 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6699 		goto vnic_mru;
6700 	}
6701 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6702 	/* Only RSS support for now TBD: COS & LB */
6703 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6704 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6705 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6706 					   VNIC_CFG_REQ_ENABLES_MRU);
6707 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6708 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6709 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6710 					   VNIC_CFG_REQ_ENABLES_MRU);
6711 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6712 	} else {
6713 		req->rss_rule = cpu_to_le16(0xffff);
6714 	}
6715 
6716 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6717 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6718 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6719 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6720 	} else {
6721 		req->cos_rule = cpu_to_le16(0xffff);
6722 	}
6723 
6724 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6725 		ring = 0;
6726 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6727 		ring = vnic->vnic_id - 1;
6728 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6729 		ring = bp->rx_nr_rings - 1;
6730 
6731 	grp_idx = bp->rx_ring[ring].bnapi->index;
6732 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6733 	req->lb_rule = cpu_to_le16(0xffff);
6734 vnic_mru:
6735 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6736 	req->mru = cpu_to_le16(vnic->mru);
6737 
6738 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6739 #ifdef CONFIG_BNXT_SRIOV
6740 	if (BNXT_VF(bp))
6741 		def_vlan = bp->vf.vlan;
6742 #endif
6743 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6744 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6745 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6746 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6747 
6748 	return hwrm_req_send(bp, req);
6749 }
6750 
6751 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6752 				    struct bnxt_vnic_info *vnic)
6753 {
6754 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6755 		struct hwrm_vnic_free_input *req;
6756 
6757 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6758 			return;
6759 
6760 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6761 
6762 		hwrm_req_send(bp, req);
6763 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6764 	}
6765 }
6766 
6767 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6768 {
6769 	u16 i;
6770 
6771 	for (i = 0; i < bp->nr_vnics; i++)
6772 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6773 }
6774 
6775 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6776 			 unsigned int start_rx_ring_idx,
6777 			 unsigned int nr_rings)
6778 {
6779 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6780 	struct hwrm_vnic_alloc_output *resp;
6781 	struct hwrm_vnic_alloc_input *req;
6782 	int rc;
6783 
6784 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6785 	if (rc)
6786 		return rc;
6787 
6788 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6789 		goto vnic_no_ring_grps;
6790 
6791 	/* map ring groups to this vnic */
6792 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6793 		grp_idx = bp->rx_ring[i].bnapi->index;
6794 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6795 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6796 				   j, nr_rings);
6797 			break;
6798 		}
6799 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6800 	}
6801 
6802 vnic_no_ring_grps:
6803 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6804 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6805 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6806 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6807 
6808 	resp = hwrm_req_hold(bp, req);
6809 	rc = hwrm_req_send(bp, req);
6810 	if (!rc)
6811 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6812 	hwrm_req_drop(bp, req);
6813 	return rc;
6814 }
6815 
6816 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6817 {
6818 	struct hwrm_vnic_qcaps_output *resp;
6819 	struct hwrm_vnic_qcaps_input *req;
6820 	int rc;
6821 
6822 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6823 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6824 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6825 	if (bp->hwrm_spec_code < 0x10600)
6826 		return 0;
6827 
6828 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6829 	if (rc)
6830 		return rc;
6831 
6832 	resp = hwrm_req_hold(bp, req);
6833 	rc = hwrm_req_send(bp, req);
6834 	if (!rc) {
6835 		u32 flags = le32_to_cpu(resp->flags);
6836 
6837 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6838 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6839 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6840 		if (flags &
6841 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6842 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6843 
6844 		/* Older P5 fw before EXT_HW_STATS support did not set
6845 		 * VLAN_STRIP_CAP properly.
6846 		 */
6847 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6848 		    (BNXT_CHIP_P5(bp) &&
6849 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6850 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6851 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6852 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6853 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6854 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6855 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6856 		if (bp->max_tpa_v2) {
6857 			if (BNXT_CHIP_P5(bp))
6858 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6859 			else
6860 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6861 		}
6862 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6863 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6864 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6865 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6866 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6867 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6868 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6869 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6870 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6871 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6872 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6873 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6874 	}
6875 	hwrm_req_drop(bp, req);
6876 	return rc;
6877 }
6878 
6879 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6880 {
6881 	struct hwrm_ring_grp_alloc_output *resp;
6882 	struct hwrm_ring_grp_alloc_input *req;
6883 	int rc;
6884 	u16 i;
6885 
6886 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6887 		return 0;
6888 
6889 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6890 	if (rc)
6891 		return rc;
6892 
6893 	resp = hwrm_req_hold(bp, req);
6894 	for (i = 0; i < bp->rx_nr_rings; i++) {
6895 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6896 
6897 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6898 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6899 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6900 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6901 
6902 		rc = hwrm_req_send(bp, req);
6903 
6904 		if (rc)
6905 			break;
6906 
6907 		bp->grp_info[grp_idx].fw_grp_id =
6908 			le32_to_cpu(resp->ring_group_id);
6909 	}
6910 	hwrm_req_drop(bp, req);
6911 	return rc;
6912 }
6913 
6914 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6915 {
6916 	struct hwrm_ring_grp_free_input *req;
6917 	u16 i;
6918 
6919 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6920 		return;
6921 
6922 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6923 		return;
6924 
6925 	hwrm_req_hold(bp, req);
6926 	for (i = 0; i < bp->cp_nr_rings; i++) {
6927 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6928 			continue;
6929 		req->ring_group_id =
6930 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6931 
6932 		hwrm_req_send(bp, req);
6933 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6934 	}
6935 	hwrm_req_drop(bp, req);
6936 }
6937 
6938 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6939 				    struct bnxt_ring_struct *ring,
6940 				    u32 ring_type, u32 map_index)
6941 {
6942 	struct hwrm_ring_alloc_output *resp;
6943 	struct hwrm_ring_alloc_input *req;
6944 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6945 	struct bnxt_ring_grp_info *grp_info;
6946 	int rc, err = 0;
6947 	u16 ring_id;
6948 
6949 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6950 	if (rc)
6951 		goto exit;
6952 
6953 	req->enables = 0;
6954 	if (rmem->nr_pages > 1) {
6955 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6956 		/* Page size is in log2 units */
6957 		req->page_size = BNXT_PAGE_SHIFT;
6958 		req->page_tbl_depth = 1;
6959 	} else {
6960 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6961 	}
6962 	req->fbo = 0;
6963 	/* Association of ring index with doorbell index and MSIX number */
6964 	req->logical_id = cpu_to_le16(map_index);
6965 
6966 	switch (ring_type) {
6967 	case HWRM_RING_ALLOC_TX: {
6968 		struct bnxt_tx_ring_info *txr;
6969 		u16 flags = 0;
6970 
6971 		txr = container_of(ring, struct bnxt_tx_ring_info,
6972 				   tx_ring_struct);
6973 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6974 		/* Association of transmit ring with completion ring */
6975 		grp_info = &bp->grp_info[ring->grp_idx];
6976 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6977 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6978 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6979 		req->queue_id = cpu_to_le16(ring->queue_id);
6980 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6981 			req->cmpl_coal_cnt =
6982 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6983 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6984 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6985 		req->flags = cpu_to_le16(flags);
6986 		break;
6987 	}
6988 	case HWRM_RING_ALLOC_RX:
6989 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6990 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6991 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6992 			u16 flags = 0;
6993 
6994 			/* Association of rx ring with stats context */
6995 			grp_info = &bp->grp_info[ring->grp_idx];
6996 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6997 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6998 			req->enables |= cpu_to_le32(
6999 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
7000 			if (NET_IP_ALIGN == 2)
7001 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
7002 			req->flags = cpu_to_le16(flags);
7003 		}
7004 		break;
7005 	case HWRM_RING_ALLOC_AGG:
7006 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7007 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
7008 			/* Association of agg ring with rx ring */
7009 			grp_info = &bp->grp_info[ring->grp_idx];
7010 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
7011 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7012 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7013 			req->enables |= cpu_to_le32(
7014 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
7015 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
7016 		} else {
7017 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7018 		}
7019 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
7020 		break;
7021 	case HWRM_RING_ALLOC_CMPL:
7022 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7023 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7024 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7025 			/* Association of cp ring with nq */
7026 			grp_info = &bp->grp_info[map_index];
7027 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7028 			req->cq_handle = cpu_to_le64(ring->handle);
7029 			req->enables |= cpu_to_le32(
7030 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7031 		} else {
7032 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7033 		}
7034 		break;
7035 	case HWRM_RING_ALLOC_NQ:
7036 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7037 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7038 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7039 		break;
7040 	default:
7041 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7042 			   ring_type);
7043 		return -1;
7044 	}
7045 
7046 	resp = hwrm_req_hold(bp, req);
7047 	rc = hwrm_req_send(bp, req);
7048 	err = le16_to_cpu(resp->error_code);
7049 	ring_id = le16_to_cpu(resp->ring_id);
7050 	hwrm_req_drop(bp, req);
7051 
7052 exit:
7053 	if (rc || err) {
7054 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7055 			   ring_type, rc, err);
7056 		return -EIO;
7057 	}
7058 	ring->fw_ring_id = ring_id;
7059 	return rc;
7060 }
7061 
7062 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7063 {
7064 	int rc;
7065 
7066 	if (BNXT_PF(bp)) {
7067 		struct hwrm_func_cfg_input *req;
7068 
7069 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7070 		if (rc)
7071 			return rc;
7072 
7073 		req->fid = cpu_to_le16(0xffff);
7074 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7075 		req->async_event_cr = cpu_to_le16(idx);
7076 		return hwrm_req_send(bp, req);
7077 	} else {
7078 		struct hwrm_func_vf_cfg_input *req;
7079 
7080 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7081 		if (rc)
7082 			return rc;
7083 
7084 		req->enables =
7085 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7086 		req->async_event_cr = cpu_to_le16(idx);
7087 		return hwrm_req_send(bp, req);
7088 	}
7089 }
7090 
7091 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7092 			     u32 ring_type)
7093 {
7094 	switch (ring_type) {
7095 	case HWRM_RING_ALLOC_TX:
7096 		db->db_ring_mask = bp->tx_ring_mask;
7097 		break;
7098 	case HWRM_RING_ALLOC_RX:
7099 		db->db_ring_mask = bp->rx_ring_mask;
7100 		break;
7101 	case HWRM_RING_ALLOC_AGG:
7102 		db->db_ring_mask = bp->rx_agg_ring_mask;
7103 		break;
7104 	case HWRM_RING_ALLOC_CMPL:
7105 	case HWRM_RING_ALLOC_NQ:
7106 		db->db_ring_mask = bp->cp_ring_mask;
7107 		break;
7108 	}
7109 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7110 		db->db_epoch_mask = db->db_ring_mask + 1;
7111 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7112 	}
7113 }
7114 
7115 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7116 			u32 map_idx, u32 xid)
7117 {
7118 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7119 		switch (ring_type) {
7120 		case HWRM_RING_ALLOC_TX:
7121 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7122 			break;
7123 		case HWRM_RING_ALLOC_RX:
7124 		case HWRM_RING_ALLOC_AGG:
7125 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7126 			break;
7127 		case HWRM_RING_ALLOC_CMPL:
7128 			db->db_key64 = DBR_PATH_L2;
7129 			break;
7130 		case HWRM_RING_ALLOC_NQ:
7131 			db->db_key64 = DBR_PATH_L2;
7132 			break;
7133 		}
7134 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7135 
7136 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7137 			db->db_key64 |= DBR_VALID;
7138 
7139 		db->doorbell = bp->bar1 + bp->db_offset;
7140 	} else {
7141 		db->doorbell = bp->bar1 + map_idx * 0x80;
7142 		switch (ring_type) {
7143 		case HWRM_RING_ALLOC_TX:
7144 			db->db_key32 = DB_KEY_TX;
7145 			break;
7146 		case HWRM_RING_ALLOC_RX:
7147 		case HWRM_RING_ALLOC_AGG:
7148 			db->db_key32 = DB_KEY_RX;
7149 			break;
7150 		case HWRM_RING_ALLOC_CMPL:
7151 			db->db_key32 = DB_KEY_CP;
7152 			break;
7153 		}
7154 	}
7155 	bnxt_set_db_mask(bp, db, ring_type);
7156 }
7157 
7158 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7159 				   struct bnxt_rx_ring_info *rxr)
7160 {
7161 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7162 	struct bnxt_napi *bnapi = rxr->bnapi;
7163 	u32 type = HWRM_RING_ALLOC_RX;
7164 	u32 map_idx = bnapi->index;
7165 	int rc;
7166 
7167 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7168 	if (rc)
7169 		return rc;
7170 
7171 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7172 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7173 
7174 	return 0;
7175 }
7176 
7177 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7178 				       struct bnxt_rx_ring_info *rxr)
7179 {
7180 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7181 	u32 type = HWRM_RING_ALLOC_AGG;
7182 	u32 grp_idx = ring->grp_idx;
7183 	u32 map_idx;
7184 	int rc;
7185 
7186 	map_idx = grp_idx + bp->rx_nr_rings;
7187 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7188 	if (rc)
7189 		return rc;
7190 
7191 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7192 		    ring->fw_ring_id);
7193 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7194 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7195 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7196 
7197 	return 0;
7198 }
7199 
7200 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7201 {
7202 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7203 	int i, rc = 0;
7204 	u32 type;
7205 
7206 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7207 		type = HWRM_RING_ALLOC_NQ;
7208 	else
7209 		type = HWRM_RING_ALLOC_CMPL;
7210 	for (i = 0; i < bp->cp_nr_rings; i++) {
7211 		struct bnxt_napi *bnapi = bp->bnapi[i];
7212 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7213 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7214 		u32 map_idx = ring->map_idx;
7215 		unsigned int vector;
7216 
7217 		vector = bp->irq_tbl[map_idx].vector;
7218 		disable_irq_nosync(vector);
7219 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7220 		if (rc) {
7221 			enable_irq(vector);
7222 			goto err_out;
7223 		}
7224 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7225 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7226 		enable_irq(vector);
7227 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7228 
7229 		if (!i) {
7230 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7231 			if (rc)
7232 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7233 		}
7234 	}
7235 
7236 	type = HWRM_RING_ALLOC_TX;
7237 	for (i = 0; i < bp->tx_nr_rings; i++) {
7238 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7239 		struct bnxt_ring_struct *ring;
7240 		u32 map_idx;
7241 
7242 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7243 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7244 			struct bnxt_napi *bnapi = txr->bnapi;
7245 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7246 
7247 			ring = &cpr2->cp_ring_struct;
7248 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7249 			map_idx = bnapi->index;
7250 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7251 			if (rc)
7252 				goto err_out;
7253 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7254 				    ring->fw_ring_id);
7255 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7256 		}
7257 		ring = &txr->tx_ring_struct;
7258 		map_idx = i;
7259 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7260 		if (rc)
7261 			goto err_out;
7262 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7263 	}
7264 
7265 	for (i = 0; i < bp->rx_nr_rings; i++) {
7266 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7267 
7268 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7269 		if (rc)
7270 			goto err_out;
7271 		/* If we have agg rings, post agg buffers first. */
7272 		if (!agg_rings)
7273 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7274 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7275 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7276 			struct bnxt_napi *bnapi = rxr->bnapi;
7277 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7278 			struct bnxt_ring_struct *ring;
7279 			u32 map_idx = bnapi->index;
7280 
7281 			ring = &cpr2->cp_ring_struct;
7282 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7283 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7284 			if (rc)
7285 				goto err_out;
7286 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7287 				    ring->fw_ring_id);
7288 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7289 		}
7290 	}
7291 
7292 	if (agg_rings) {
7293 		for (i = 0; i < bp->rx_nr_rings; i++) {
7294 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7295 			if (rc)
7296 				goto err_out;
7297 		}
7298 	}
7299 err_out:
7300 	return rc;
7301 }
7302 
7303 static void bnxt_cancel_dim(struct bnxt *bp)
7304 {
7305 	int i;
7306 
7307 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7308 	 * if NAPI is enabled.
7309 	 */
7310 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7311 		return;
7312 
7313 	/* Make sure NAPI sees that the VNIC is disabled */
7314 	synchronize_net();
7315 	for (i = 0; i < bp->rx_nr_rings; i++) {
7316 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7317 		struct bnxt_napi *bnapi = rxr->bnapi;
7318 
7319 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7320 	}
7321 }
7322 
7323 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7324 				   struct bnxt_ring_struct *ring,
7325 				   u32 ring_type, int cmpl_ring_id)
7326 {
7327 	struct hwrm_ring_free_output *resp;
7328 	struct hwrm_ring_free_input *req;
7329 	u16 error_code = 0;
7330 	int rc;
7331 
7332 	if (BNXT_NO_FW_ACCESS(bp))
7333 		return 0;
7334 
7335 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7336 	if (rc)
7337 		goto exit;
7338 
7339 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7340 	req->ring_type = ring_type;
7341 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7342 
7343 	resp = hwrm_req_hold(bp, req);
7344 	rc = hwrm_req_send(bp, req);
7345 	error_code = le16_to_cpu(resp->error_code);
7346 	hwrm_req_drop(bp, req);
7347 exit:
7348 	if (rc || error_code) {
7349 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7350 			   ring_type, rc, error_code);
7351 		return -EIO;
7352 	}
7353 	return 0;
7354 }
7355 
7356 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7357 				   struct bnxt_rx_ring_info *rxr,
7358 				   bool close_path)
7359 {
7360 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7361 	u32 grp_idx = rxr->bnapi->index;
7362 	u32 cmpl_ring_id;
7363 
7364 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7365 		return;
7366 
7367 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7368 	hwrm_ring_free_send_msg(bp, ring,
7369 				RING_FREE_REQ_RING_TYPE_RX,
7370 				close_path ? cmpl_ring_id :
7371 				INVALID_HW_RING_ID);
7372 	ring->fw_ring_id = INVALID_HW_RING_ID;
7373 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7374 }
7375 
7376 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7377 				       struct bnxt_rx_ring_info *rxr,
7378 				       bool close_path)
7379 {
7380 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7381 	u32 grp_idx = rxr->bnapi->index;
7382 	u32 type, cmpl_ring_id;
7383 
7384 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7385 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7386 	else
7387 		type = RING_FREE_REQ_RING_TYPE_RX;
7388 
7389 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7390 		return;
7391 
7392 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7393 	hwrm_ring_free_send_msg(bp, ring, type,
7394 				close_path ? cmpl_ring_id :
7395 				INVALID_HW_RING_ID);
7396 	ring->fw_ring_id = INVALID_HW_RING_ID;
7397 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7398 }
7399 
7400 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7401 {
7402 	u32 type;
7403 	int i;
7404 
7405 	if (!bp->bnapi)
7406 		return;
7407 
7408 	for (i = 0; i < bp->tx_nr_rings; i++) {
7409 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7410 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7411 
7412 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7413 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7414 
7415 			hwrm_ring_free_send_msg(bp, ring,
7416 						RING_FREE_REQ_RING_TYPE_TX,
7417 						close_path ? cmpl_ring_id :
7418 						INVALID_HW_RING_ID);
7419 			ring->fw_ring_id = INVALID_HW_RING_ID;
7420 		}
7421 	}
7422 
7423 	bnxt_cancel_dim(bp);
7424 	for (i = 0; i < bp->rx_nr_rings; i++) {
7425 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7426 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7427 	}
7428 
7429 	/* The completion rings are about to be freed.  After that the
7430 	 * IRQ doorbell will not work anymore.  So we need to disable
7431 	 * IRQ here.
7432 	 */
7433 	bnxt_disable_int_sync(bp);
7434 
7435 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7436 		type = RING_FREE_REQ_RING_TYPE_NQ;
7437 	else
7438 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7439 	for (i = 0; i < bp->cp_nr_rings; i++) {
7440 		struct bnxt_napi *bnapi = bp->bnapi[i];
7441 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7442 		struct bnxt_ring_struct *ring;
7443 		int j;
7444 
7445 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7446 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7447 
7448 			ring = &cpr2->cp_ring_struct;
7449 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7450 				continue;
7451 			hwrm_ring_free_send_msg(bp, ring,
7452 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7453 						INVALID_HW_RING_ID);
7454 			ring->fw_ring_id = INVALID_HW_RING_ID;
7455 		}
7456 		ring = &cpr->cp_ring_struct;
7457 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7458 			hwrm_ring_free_send_msg(bp, ring, type,
7459 						INVALID_HW_RING_ID);
7460 			ring->fw_ring_id = INVALID_HW_RING_ID;
7461 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7462 		}
7463 	}
7464 }
7465 
7466 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7467 			     bool shared);
7468 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7469 			   bool shared);
7470 
7471 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7472 {
7473 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7474 	struct hwrm_func_qcfg_output *resp;
7475 	struct hwrm_func_qcfg_input *req;
7476 	int rc;
7477 
7478 	if (bp->hwrm_spec_code < 0x10601)
7479 		return 0;
7480 
7481 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7482 	if (rc)
7483 		return rc;
7484 
7485 	req->fid = cpu_to_le16(0xffff);
7486 	resp = hwrm_req_hold(bp, req);
7487 	rc = hwrm_req_send(bp, req);
7488 	if (rc) {
7489 		hwrm_req_drop(bp, req);
7490 		return rc;
7491 	}
7492 
7493 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7494 	if (BNXT_NEW_RM(bp)) {
7495 		u16 cp, stats;
7496 
7497 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7498 		hw_resc->resv_hw_ring_grps =
7499 			le32_to_cpu(resp->alloc_hw_ring_grps);
7500 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7501 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7502 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7503 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7504 		hw_resc->resv_irqs = cp;
7505 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7506 			int rx = hw_resc->resv_rx_rings;
7507 			int tx = hw_resc->resv_tx_rings;
7508 
7509 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7510 				rx >>= 1;
7511 			if (cp < (rx + tx)) {
7512 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7513 				if (rc)
7514 					goto get_rings_exit;
7515 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7516 					rx <<= 1;
7517 				hw_resc->resv_rx_rings = rx;
7518 				hw_resc->resv_tx_rings = tx;
7519 			}
7520 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7521 			hw_resc->resv_hw_ring_grps = rx;
7522 		}
7523 		hw_resc->resv_cp_rings = cp;
7524 		hw_resc->resv_stat_ctxs = stats;
7525 	}
7526 get_rings_exit:
7527 	hwrm_req_drop(bp, req);
7528 	return rc;
7529 }
7530 
7531 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7532 {
7533 	struct hwrm_func_qcfg_output *resp;
7534 	struct hwrm_func_qcfg_input *req;
7535 	int rc;
7536 
7537 	if (bp->hwrm_spec_code < 0x10601)
7538 		return 0;
7539 
7540 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7541 	if (rc)
7542 		return rc;
7543 
7544 	req->fid = cpu_to_le16(fid);
7545 	resp = hwrm_req_hold(bp, req);
7546 	rc = hwrm_req_send(bp, req);
7547 	if (!rc)
7548 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7549 
7550 	hwrm_req_drop(bp, req);
7551 	return rc;
7552 }
7553 
7554 static bool bnxt_rfs_supported(struct bnxt *bp);
7555 
7556 static struct hwrm_func_cfg_input *
7557 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7558 {
7559 	struct hwrm_func_cfg_input *req;
7560 	u32 enables = 0;
7561 
7562 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7563 		return NULL;
7564 
7565 	req->fid = cpu_to_le16(0xffff);
7566 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7567 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7568 	if (BNXT_NEW_RM(bp)) {
7569 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7570 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7571 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7572 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7573 			enables |= hwr->cp_p5 ?
7574 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7575 		} else {
7576 			enables |= hwr->cp ?
7577 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7578 			enables |= hwr->grp ?
7579 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7580 		}
7581 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7582 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7583 					  0;
7584 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7585 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7586 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7587 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7588 			req->num_msix = cpu_to_le16(hwr->cp);
7589 		} else {
7590 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7591 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7592 		}
7593 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7594 		req->num_vnics = cpu_to_le16(hwr->vnic);
7595 	}
7596 	req->enables = cpu_to_le32(enables);
7597 	return req;
7598 }
7599 
7600 static struct hwrm_func_vf_cfg_input *
7601 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7602 {
7603 	struct hwrm_func_vf_cfg_input *req;
7604 	u32 enables = 0;
7605 
7606 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7607 		return NULL;
7608 
7609 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7610 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7611 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7612 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7613 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7614 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7615 		enables |= hwr->cp_p5 ?
7616 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7617 	} else {
7618 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7619 		enables |= hwr->grp ?
7620 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7621 	}
7622 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7623 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7624 
7625 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7626 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7627 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7628 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7629 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7630 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7631 	} else {
7632 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7633 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7634 	}
7635 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7636 	req->num_vnics = cpu_to_le16(hwr->vnic);
7637 
7638 	req->enables = cpu_to_le32(enables);
7639 	return req;
7640 }
7641 
7642 static int
7643 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7644 {
7645 	struct hwrm_func_cfg_input *req;
7646 	int rc;
7647 
7648 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7649 	if (!req)
7650 		return -ENOMEM;
7651 
7652 	if (!req->enables) {
7653 		hwrm_req_drop(bp, req);
7654 		return 0;
7655 	}
7656 
7657 	rc = hwrm_req_send(bp, req);
7658 	if (rc)
7659 		return rc;
7660 
7661 	if (bp->hwrm_spec_code < 0x10601)
7662 		bp->hw_resc.resv_tx_rings = hwr->tx;
7663 
7664 	return bnxt_hwrm_get_rings(bp);
7665 }
7666 
7667 static int
7668 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7669 {
7670 	struct hwrm_func_vf_cfg_input *req;
7671 	int rc;
7672 
7673 	if (!BNXT_NEW_RM(bp)) {
7674 		bp->hw_resc.resv_tx_rings = hwr->tx;
7675 		return 0;
7676 	}
7677 
7678 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7679 	if (!req)
7680 		return -ENOMEM;
7681 
7682 	rc = hwrm_req_send(bp, req);
7683 	if (rc)
7684 		return rc;
7685 
7686 	return bnxt_hwrm_get_rings(bp);
7687 }
7688 
7689 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7690 {
7691 	if (BNXT_PF(bp))
7692 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7693 	else
7694 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7695 }
7696 
7697 int bnxt_nq_rings_in_use(struct bnxt *bp)
7698 {
7699 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7700 }
7701 
7702 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7703 {
7704 	int cp;
7705 
7706 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7707 		return bnxt_nq_rings_in_use(bp);
7708 
7709 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7710 	return cp;
7711 }
7712 
7713 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7714 {
7715 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7716 }
7717 
7718 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7719 {
7720 	if (!hwr->grp)
7721 		return 0;
7722 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7723 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7724 
7725 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7726 			rss_ctx *= hwr->vnic;
7727 		return rss_ctx;
7728 	}
7729 	if (BNXT_VF(bp))
7730 		return BNXT_VF_MAX_RSS_CTX;
7731 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7732 		return hwr->grp + 1;
7733 	return 1;
7734 }
7735 
7736 /* Check if a default RSS map needs to be setup.  This function is only
7737  * used on older firmware that does not require reserving RX rings.
7738  */
7739 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7740 {
7741 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7742 
7743 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7744 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7745 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7746 		if (!netif_is_rxfh_configured(bp->dev))
7747 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7748 	}
7749 }
7750 
7751 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7752 {
7753 	if (bp->flags & BNXT_FLAG_RFS) {
7754 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7755 			return 2 + bp->num_rss_ctx;
7756 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7757 			return rx_rings + 1;
7758 	}
7759 	return 1;
7760 }
7761 
7762 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7763 {
7764 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7765 	int cp = bnxt_cp_rings_in_use(bp);
7766 	int nq = bnxt_nq_rings_in_use(bp);
7767 	int rx = bp->rx_nr_rings, stat;
7768 	int vnic, grp = rx;
7769 
7770 	/* Old firmware does not need RX ring reservations but we still
7771 	 * need to setup a default RSS map when needed.  With new firmware
7772 	 * we go through RX ring reservations first and then set up the
7773 	 * RSS map for the successfully reserved RX rings when needed.
7774 	 */
7775 	if (!BNXT_NEW_RM(bp))
7776 		bnxt_check_rss_tbl_no_rmgr(bp);
7777 
7778 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7779 	    bp->hwrm_spec_code >= 0x10601)
7780 		return true;
7781 
7782 	if (!BNXT_NEW_RM(bp))
7783 		return false;
7784 
7785 	vnic = bnxt_get_total_vnics(bp, rx);
7786 
7787 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7788 		rx <<= 1;
7789 	stat = bnxt_get_func_stat_ctxs(bp);
7790 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7791 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7792 	    (hw_resc->resv_hw_ring_grps != grp &&
7793 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7794 		return true;
7795 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7796 	    hw_resc->resv_irqs != nq)
7797 		return true;
7798 	return false;
7799 }
7800 
7801 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7802 {
7803 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7804 
7805 	hwr->tx = hw_resc->resv_tx_rings;
7806 	if (BNXT_NEW_RM(bp)) {
7807 		hwr->rx = hw_resc->resv_rx_rings;
7808 		hwr->cp = hw_resc->resv_irqs;
7809 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7810 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7811 		hwr->grp = hw_resc->resv_hw_ring_grps;
7812 		hwr->vnic = hw_resc->resv_vnics;
7813 		hwr->stat = hw_resc->resv_stat_ctxs;
7814 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7815 	}
7816 }
7817 
7818 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7819 {
7820 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7821 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7822 }
7823 
7824 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7825 
7826 static int __bnxt_reserve_rings(struct bnxt *bp)
7827 {
7828 	struct bnxt_hw_rings hwr = {0};
7829 	int rx_rings, old_rx_rings, rc;
7830 	int cp = bp->cp_nr_rings;
7831 	int ulp_msix = 0;
7832 	bool sh = false;
7833 	int tx_cp;
7834 
7835 	if (!bnxt_need_reserve_rings(bp))
7836 		return 0;
7837 
7838 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7839 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7840 		if (!ulp_msix)
7841 			bnxt_set_ulp_stat_ctxs(bp, 0);
7842 
7843 		if (ulp_msix > bp->ulp_num_msix_want)
7844 			ulp_msix = bp->ulp_num_msix_want;
7845 		hwr.cp = cp + ulp_msix;
7846 	} else {
7847 		hwr.cp = bnxt_nq_rings_in_use(bp);
7848 	}
7849 
7850 	hwr.tx = bp->tx_nr_rings;
7851 	hwr.rx = bp->rx_nr_rings;
7852 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7853 		sh = true;
7854 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7855 		hwr.cp_p5 = hwr.rx + hwr.tx;
7856 
7857 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7858 
7859 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7860 		hwr.rx <<= 1;
7861 	hwr.grp = bp->rx_nr_rings;
7862 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7863 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7864 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7865 
7866 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7867 	if (rc)
7868 		return rc;
7869 
7870 	bnxt_copy_reserved_rings(bp, &hwr);
7871 
7872 	rx_rings = hwr.rx;
7873 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7874 		if (hwr.rx >= 2) {
7875 			rx_rings = hwr.rx >> 1;
7876 		} else {
7877 			if (netif_running(bp->dev))
7878 				return -ENOMEM;
7879 
7880 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7881 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7882 			bp->dev->hw_features &= ~NETIF_F_LRO;
7883 			bp->dev->features &= ~NETIF_F_LRO;
7884 			bnxt_set_ring_params(bp);
7885 		}
7886 	}
7887 	rx_rings = min_t(int, rx_rings, hwr.grp);
7888 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7889 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7890 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7891 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7892 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7893 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7894 		hwr.rx = rx_rings << 1;
7895 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7896 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7897 	bp->tx_nr_rings = hwr.tx;
7898 
7899 	/* If we cannot reserve all the RX rings, reset the RSS map only
7900 	 * if absolutely necessary
7901 	 */
7902 	if (rx_rings != bp->rx_nr_rings) {
7903 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7904 			    rx_rings, bp->rx_nr_rings);
7905 		if (netif_is_rxfh_configured(bp->dev) &&
7906 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7907 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7908 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7909 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7910 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7911 		}
7912 	}
7913 	bp->rx_nr_rings = rx_rings;
7914 	bp->cp_nr_rings = hwr.cp;
7915 
7916 	if (!bnxt_rings_ok(bp, &hwr))
7917 		return -ENOMEM;
7918 
7919 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7920 	    !netif_is_rxfh_configured(bp->dev))
7921 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7922 
7923 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7924 		int resv_msix, resv_ctx, ulp_ctxs;
7925 		struct bnxt_hw_resc *hw_resc;
7926 
7927 		hw_resc = &bp->hw_resc;
7928 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7929 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7930 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7931 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7932 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7933 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7934 	}
7935 
7936 	return rc;
7937 }
7938 
7939 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7940 {
7941 	struct hwrm_func_vf_cfg_input *req;
7942 	u32 flags;
7943 
7944 	if (!BNXT_NEW_RM(bp))
7945 		return 0;
7946 
7947 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7948 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7949 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7950 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7951 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7952 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7953 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7954 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7955 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7956 
7957 	req->flags = cpu_to_le32(flags);
7958 	return hwrm_req_send_silent(bp, req);
7959 }
7960 
7961 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7962 {
7963 	struct hwrm_func_cfg_input *req;
7964 	u32 flags;
7965 
7966 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7967 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7968 	if (BNXT_NEW_RM(bp)) {
7969 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7970 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7971 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7972 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7973 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7974 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7975 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7976 		else
7977 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7978 	}
7979 
7980 	req->flags = cpu_to_le32(flags);
7981 	return hwrm_req_send_silent(bp, req);
7982 }
7983 
7984 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7985 {
7986 	if (bp->hwrm_spec_code < 0x10801)
7987 		return 0;
7988 
7989 	if (BNXT_PF(bp))
7990 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7991 
7992 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7993 }
7994 
7995 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7996 {
7997 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7998 	struct hwrm_ring_aggint_qcaps_output *resp;
7999 	struct hwrm_ring_aggint_qcaps_input *req;
8000 	int rc;
8001 
8002 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
8003 	coal_cap->num_cmpl_dma_aggr_max = 63;
8004 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
8005 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
8006 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
8007 	coal_cap->int_lat_tmr_min_max = 65535;
8008 	coal_cap->int_lat_tmr_max_max = 65535;
8009 	coal_cap->num_cmpl_aggr_int_max = 65535;
8010 	coal_cap->timer_units = 80;
8011 
8012 	if (bp->hwrm_spec_code < 0x10902)
8013 		return;
8014 
8015 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8016 		return;
8017 
8018 	resp = hwrm_req_hold(bp, req);
8019 	rc = hwrm_req_send_silent(bp, req);
8020 	if (!rc) {
8021 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8022 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8023 		coal_cap->num_cmpl_dma_aggr_max =
8024 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8025 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8026 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8027 		coal_cap->cmpl_aggr_dma_tmr_max =
8028 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8029 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8030 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8031 		coal_cap->int_lat_tmr_min_max =
8032 			le16_to_cpu(resp->int_lat_tmr_min_max);
8033 		coal_cap->int_lat_tmr_max_max =
8034 			le16_to_cpu(resp->int_lat_tmr_max_max);
8035 		coal_cap->num_cmpl_aggr_int_max =
8036 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8037 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8038 	}
8039 	hwrm_req_drop(bp, req);
8040 }
8041 
8042 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8043 {
8044 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8045 
8046 	return usec * 1000 / coal_cap->timer_units;
8047 }
8048 
8049 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8050 	struct bnxt_coal *hw_coal,
8051 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8052 {
8053 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8054 	u16 val, tmr, max, flags = hw_coal->flags;
8055 	u32 cmpl_params = coal_cap->cmpl_params;
8056 
8057 	max = hw_coal->bufs_per_record * 128;
8058 	if (hw_coal->budget)
8059 		max = hw_coal->bufs_per_record * hw_coal->budget;
8060 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8061 
8062 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8063 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8064 
8065 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8066 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8067 
8068 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8069 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8070 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8071 
8072 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8073 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8074 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8075 
8076 	/* min timer set to 1/2 of interrupt timer */
8077 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8078 		val = tmr / 2;
8079 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8080 		req->int_lat_tmr_min = cpu_to_le16(val);
8081 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8082 	}
8083 
8084 	/* buf timer set to 1/4 of interrupt timer */
8085 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8086 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8087 
8088 	if (cmpl_params &
8089 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8090 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8091 		val = clamp_t(u16, tmr, 1,
8092 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8093 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8094 		req->enables |=
8095 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8096 	}
8097 
8098 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8099 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8100 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8101 	req->flags = cpu_to_le16(flags);
8102 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8103 }
8104 
8105 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8106 				   struct bnxt_coal *hw_coal)
8107 {
8108 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8109 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8110 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8111 	u32 nq_params = coal_cap->nq_params;
8112 	u16 tmr;
8113 	int rc;
8114 
8115 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8116 		return 0;
8117 
8118 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8119 	if (rc)
8120 		return rc;
8121 
8122 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8123 	req->flags =
8124 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8125 
8126 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8127 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8128 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8129 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8130 	return hwrm_req_send(bp, req);
8131 }
8132 
8133 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8134 {
8135 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8136 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8137 	struct bnxt_coal coal;
8138 	int rc;
8139 
8140 	/* Tick values in micro seconds.
8141 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8142 	 */
8143 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8144 
8145 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8146 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8147 
8148 	if (!bnapi->rx_ring)
8149 		return -ENODEV;
8150 
8151 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8152 	if (rc)
8153 		return rc;
8154 
8155 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8156 
8157 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8158 
8159 	return hwrm_req_send(bp, req_rx);
8160 }
8161 
8162 static int
8163 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8164 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8165 {
8166 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8167 
8168 	req->ring_id = cpu_to_le16(ring_id);
8169 	return hwrm_req_send(bp, req);
8170 }
8171 
8172 static int
8173 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8174 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8175 {
8176 	struct bnxt_tx_ring_info *txr;
8177 	int i, rc;
8178 
8179 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8180 		u16 ring_id;
8181 
8182 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8183 		req->ring_id = cpu_to_le16(ring_id);
8184 		rc = hwrm_req_send(bp, req);
8185 		if (rc)
8186 			return rc;
8187 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8188 			return 0;
8189 	}
8190 	return 0;
8191 }
8192 
8193 int bnxt_hwrm_set_coal(struct bnxt *bp)
8194 {
8195 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8196 	int i, rc;
8197 
8198 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8199 	if (rc)
8200 		return rc;
8201 
8202 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8203 	if (rc) {
8204 		hwrm_req_drop(bp, req_rx);
8205 		return rc;
8206 	}
8207 
8208 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8209 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8210 
8211 	hwrm_req_hold(bp, req_rx);
8212 	hwrm_req_hold(bp, req_tx);
8213 	for (i = 0; i < bp->cp_nr_rings; i++) {
8214 		struct bnxt_napi *bnapi = bp->bnapi[i];
8215 		struct bnxt_coal *hw_coal;
8216 
8217 		if (!bnapi->rx_ring)
8218 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8219 		else
8220 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8221 		if (rc)
8222 			break;
8223 
8224 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8225 			continue;
8226 
8227 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8228 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8229 			if (rc)
8230 				break;
8231 		}
8232 		if (bnapi->rx_ring)
8233 			hw_coal = &bp->rx_coal;
8234 		else
8235 			hw_coal = &bp->tx_coal;
8236 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8237 	}
8238 	hwrm_req_drop(bp, req_rx);
8239 	hwrm_req_drop(bp, req_tx);
8240 	return rc;
8241 }
8242 
8243 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8244 {
8245 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8246 	struct hwrm_stat_ctx_free_input *req;
8247 	int i;
8248 
8249 	if (!bp->bnapi)
8250 		return;
8251 
8252 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8253 		return;
8254 
8255 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8256 		return;
8257 	if (BNXT_FW_MAJ(bp) <= 20) {
8258 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8259 			hwrm_req_drop(bp, req);
8260 			return;
8261 		}
8262 		hwrm_req_hold(bp, req0);
8263 	}
8264 	hwrm_req_hold(bp, req);
8265 	for (i = 0; i < bp->cp_nr_rings; i++) {
8266 		struct bnxt_napi *bnapi = bp->bnapi[i];
8267 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8268 
8269 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8270 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8271 			if (req0) {
8272 				req0->stat_ctx_id = req->stat_ctx_id;
8273 				hwrm_req_send(bp, req0);
8274 			}
8275 			hwrm_req_send(bp, req);
8276 
8277 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8278 		}
8279 	}
8280 	hwrm_req_drop(bp, req);
8281 	if (req0)
8282 		hwrm_req_drop(bp, req0);
8283 }
8284 
8285 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8286 {
8287 	struct hwrm_stat_ctx_alloc_output *resp;
8288 	struct hwrm_stat_ctx_alloc_input *req;
8289 	int rc, i;
8290 
8291 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8292 		return 0;
8293 
8294 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8295 	if (rc)
8296 		return rc;
8297 
8298 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8299 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8300 
8301 	resp = hwrm_req_hold(bp, req);
8302 	for (i = 0; i < bp->cp_nr_rings; i++) {
8303 		struct bnxt_napi *bnapi = bp->bnapi[i];
8304 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8305 
8306 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8307 
8308 		rc = hwrm_req_send(bp, req);
8309 		if (rc)
8310 			break;
8311 
8312 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8313 
8314 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8315 	}
8316 	hwrm_req_drop(bp, req);
8317 	return rc;
8318 }
8319 
8320 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8321 {
8322 	struct hwrm_func_qcfg_output *resp;
8323 	struct hwrm_func_qcfg_input *req;
8324 	u16 flags;
8325 	int rc;
8326 
8327 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8328 	if (rc)
8329 		return rc;
8330 
8331 	req->fid = cpu_to_le16(0xffff);
8332 	resp = hwrm_req_hold(bp, req);
8333 	rc = hwrm_req_send(bp, req);
8334 	if (rc)
8335 		goto func_qcfg_exit;
8336 
8337 	flags = le16_to_cpu(resp->flags);
8338 #ifdef CONFIG_BNXT_SRIOV
8339 	if (BNXT_VF(bp)) {
8340 		struct bnxt_vf_info *vf = &bp->vf;
8341 
8342 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8343 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8344 			vf->flags |= BNXT_VF_TRUST;
8345 		else
8346 			vf->flags &= ~BNXT_VF_TRUST;
8347 	} else {
8348 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8349 	}
8350 #endif
8351 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8352 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8353 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8354 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8355 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8356 	}
8357 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8358 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8359 
8360 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8361 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8362 
8363 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8364 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8365 
8366 	switch (resp->port_partition_type) {
8367 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8368 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8369 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8370 		bp->port_partition_type = resp->port_partition_type;
8371 		break;
8372 	}
8373 	if (bp->hwrm_spec_code < 0x10707 ||
8374 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8375 		bp->br_mode = BRIDGE_MODE_VEB;
8376 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8377 		bp->br_mode = BRIDGE_MODE_VEPA;
8378 	else
8379 		bp->br_mode = BRIDGE_MODE_UNDEF;
8380 
8381 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8382 	if (!bp->max_mtu)
8383 		bp->max_mtu = BNXT_MAX_MTU;
8384 
8385 	if (bp->db_size)
8386 		goto func_qcfg_exit;
8387 
8388 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8389 	if (BNXT_CHIP_P5(bp)) {
8390 		if (BNXT_PF(bp))
8391 			bp->db_offset = DB_PF_OFFSET_P5;
8392 		else
8393 			bp->db_offset = DB_VF_OFFSET_P5;
8394 	}
8395 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8396 				 1024);
8397 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8398 	    bp->db_size <= bp->db_offset)
8399 		bp->db_size = pci_resource_len(bp->pdev, 2);
8400 
8401 func_qcfg_exit:
8402 	hwrm_req_drop(bp, req);
8403 	return rc;
8404 }
8405 
8406 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8407 				      u8 init_val, u8 init_offset,
8408 				      bool init_mask_set)
8409 {
8410 	ctxm->init_value = init_val;
8411 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8412 	if (init_mask_set)
8413 		ctxm->init_offset = init_offset * 4;
8414 	else
8415 		ctxm->init_value = 0;
8416 }
8417 
8418 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8419 {
8420 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8421 	u16 type;
8422 
8423 	for (type = 0; type < ctx_max; type++) {
8424 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8425 		int n = 1;
8426 
8427 		if (!ctxm->max_entries || ctxm->pg_info)
8428 			continue;
8429 
8430 		if (ctxm->instance_bmap)
8431 			n = hweight32(ctxm->instance_bmap);
8432 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8433 		if (!ctxm->pg_info)
8434 			return -ENOMEM;
8435 	}
8436 	return 0;
8437 }
8438 
8439 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8440 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8441 
8442 #define BNXT_CTX_INIT_VALID(flags)	\
8443 	(!!((flags) &			\
8444 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8445 
8446 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8447 {
8448 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8449 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8450 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8451 	u16 type;
8452 	int rc;
8453 
8454 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8455 	if (rc)
8456 		return rc;
8457 
8458 	if (!ctx) {
8459 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8460 		if (!ctx)
8461 			return -ENOMEM;
8462 		bp->ctx = ctx;
8463 	}
8464 
8465 	resp = hwrm_req_hold(bp, req);
8466 
8467 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8468 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8469 		u8 init_val, init_off, i;
8470 		u32 max_entries;
8471 		u16 entry_size;
8472 		__le32 *p;
8473 		u32 flags;
8474 
8475 		req->type = cpu_to_le16(type);
8476 		rc = hwrm_req_send(bp, req);
8477 		if (rc)
8478 			goto ctx_done;
8479 		flags = le32_to_cpu(resp->flags);
8480 		type = le16_to_cpu(resp->next_valid_type);
8481 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8482 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8483 			continue;
8484 		}
8485 		entry_size = le16_to_cpu(resp->entry_size);
8486 		max_entries = le32_to_cpu(resp->max_num_entries);
8487 		if (ctxm->mem_valid) {
8488 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8489 			    ctxm->entry_size != entry_size ||
8490 			    ctxm->max_entries != max_entries)
8491 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8492 			else
8493 				continue;
8494 		}
8495 		ctxm->type = le16_to_cpu(resp->type);
8496 		ctxm->entry_size = entry_size;
8497 		ctxm->flags = flags;
8498 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8499 		ctxm->entry_multiple = resp->entry_multiple;
8500 		ctxm->max_entries = max_entries;
8501 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8502 		init_val = resp->ctx_init_value;
8503 		init_off = resp->ctx_init_offset;
8504 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8505 					  BNXT_CTX_INIT_VALID(flags));
8506 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8507 					      BNXT_MAX_SPLIT_ENTRY);
8508 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8509 		     i++, p++)
8510 			ctxm->split[i] = le32_to_cpu(*p);
8511 	}
8512 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8513 
8514 ctx_done:
8515 	hwrm_req_drop(bp, req);
8516 	return rc;
8517 }
8518 
8519 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8520 {
8521 	struct hwrm_func_backing_store_qcaps_output *resp;
8522 	struct hwrm_func_backing_store_qcaps_input *req;
8523 	int rc;
8524 
8525 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8526 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8527 		return 0;
8528 
8529 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8530 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8531 
8532 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8533 	if (rc)
8534 		return rc;
8535 
8536 	resp = hwrm_req_hold(bp, req);
8537 	rc = hwrm_req_send_silent(bp, req);
8538 	if (!rc) {
8539 		struct bnxt_ctx_mem_type *ctxm;
8540 		struct bnxt_ctx_mem_info *ctx;
8541 		u8 init_val, init_idx = 0;
8542 		u16 init_mask;
8543 
8544 		ctx = bp->ctx;
8545 		if (!ctx) {
8546 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8547 			if (!ctx) {
8548 				rc = -ENOMEM;
8549 				goto ctx_err;
8550 			}
8551 			bp->ctx = ctx;
8552 		}
8553 		init_val = resp->ctx_kind_initializer;
8554 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8555 
8556 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8557 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8558 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8559 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8560 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8561 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8562 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8563 					  (init_mask & (1 << init_idx++)) != 0);
8564 
8565 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8566 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8567 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8568 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8569 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8570 					  (init_mask & (1 << init_idx++)) != 0);
8571 
8572 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8573 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8574 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8575 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8576 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8577 					  (init_mask & (1 << init_idx++)) != 0);
8578 
8579 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8580 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8581 		ctxm->max_entries = ctxm->vnic_entries +
8582 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8583 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8584 		bnxt_init_ctx_initializer(ctxm, init_val,
8585 					  resp->vnic_init_offset,
8586 					  (init_mask & (1 << init_idx++)) != 0);
8587 
8588 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8589 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8590 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8591 		bnxt_init_ctx_initializer(ctxm, init_val,
8592 					  resp->stat_init_offset,
8593 					  (init_mask & (1 << init_idx++)) != 0);
8594 
8595 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8596 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8597 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8598 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8599 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8600 		if (!ctxm->entry_multiple)
8601 			ctxm->entry_multiple = 1;
8602 
8603 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8604 
8605 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8606 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8607 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8608 		ctxm->mrav_num_entries_units =
8609 			le16_to_cpu(resp->mrav_num_entries_units);
8610 		bnxt_init_ctx_initializer(ctxm, init_val,
8611 					  resp->mrav_init_offset,
8612 					  (init_mask & (1 << init_idx++)) != 0);
8613 
8614 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8615 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8616 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8617 
8618 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8619 		if (!ctx->tqm_fp_rings_count)
8620 			ctx->tqm_fp_rings_count = bp->max_q;
8621 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8622 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8623 
8624 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8625 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8626 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8627 
8628 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8629 	} else {
8630 		rc = 0;
8631 	}
8632 ctx_err:
8633 	hwrm_req_drop(bp, req);
8634 	return rc;
8635 }
8636 
8637 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8638 				  __le64 *pg_dir)
8639 {
8640 	if (!rmem->nr_pages)
8641 		return;
8642 
8643 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8644 	if (rmem->depth >= 1) {
8645 		if (rmem->depth == 2)
8646 			*pg_attr |= 2;
8647 		else
8648 			*pg_attr |= 1;
8649 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8650 	} else {
8651 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8652 	}
8653 }
8654 
8655 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8656 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8657 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8658 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8659 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8660 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8661 
8662 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8663 {
8664 	struct hwrm_func_backing_store_cfg_input *req;
8665 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8666 	struct bnxt_ctx_pg_info *ctx_pg;
8667 	struct bnxt_ctx_mem_type *ctxm;
8668 	void **__req = (void **)&req;
8669 	u32 req_len = sizeof(*req);
8670 	__le32 *num_entries;
8671 	__le64 *pg_dir;
8672 	u32 flags = 0;
8673 	u8 *pg_attr;
8674 	u32 ena;
8675 	int rc;
8676 	int i;
8677 
8678 	if (!ctx)
8679 		return 0;
8680 
8681 	if (req_len > bp->hwrm_max_ext_req_len)
8682 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8683 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8684 	if (rc)
8685 		return rc;
8686 
8687 	req->enables = cpu_to_le32(enables);
8688 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8689 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8690 		ctx_pg = ctxm->pg_info;
8691 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8692 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8693 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8694 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8695 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8696 				      &req->qpc_pg_size_qpc_lvl,
8697 				      &req->qpc_page_dir);
8698 
8699 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8700 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8701 	}
8702 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8703 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8704 		ctx_pg = ctxm->pg_info;
8705 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8706 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8707 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8708 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8709 				      &req->srq_pg_size_srq_lvl,
8710 				      &req->srq_page_dir);
8711 	}
8712 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8713 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8714 		ctx_pg = ctxm->pg_info;
8715 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8716 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8717 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8718 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8719 				      &req->cq_pg_size_cq_lvl,
8720 				      &req->cq_page_dir);
8721 	}
8722 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8723 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8724 		ctx_pg = ctxm->pg_info;
8725 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8726 		req->vnic_num_ring_table_entries =
8727 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8728 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8729 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8730 				      &req->vnic_pg_size_vnic_lvl,
8731 				      &req->vnic_page_dir);
8732 	}
8733 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8734 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8735 		ctx_pg = ctxm->pg_info;
8736 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8737 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8738 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8739 				      &req->stat_pg_size_stat_lvl,
8740 				      &req->stat_page_dir);
8741 	}
8742 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8743 		u32 units;
8744 
8745 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8746 		ctx_pg = ctxm->pg_info;
8747 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8748 		units = ctxm->mrav_num_entries_units;
8749 		if (units) {
8750 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8751 			u32 entries;
8752 
8753 			num_mr = ctx_pg->entries - num_ah;
8754 			entries = ((num_mr / units) << 16) | (num_ah / units);
8755 			req->mrav_num_entries = cpu_to_le32(entries);
8756 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8757 		}
8758 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8759 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8760 				      &req->mrav_pg_size_mrav_lvl,
8761 				      &req->mrav_page_dir);
8762 	}
8763 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8764 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8765 		ctx_pg = ctxm->pg_info;
8766 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8767 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8768 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8769 				      &req->tim_pg_size_tim_lvl,
8770 				      &req->tim_page_dir);
8771 	}
8772 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8773 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8774 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8775 	     pg_dir = &req->tqm_sp_page_dir,
8776 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8777 	     ctx_pg = ctxm->pg_info;
8778 	     i < BNXT_MAX_TQM_RINGS;
8779 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8780 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8781 		if (!(enables & ena))
8782 			continue;
8783 
8784 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8785 		*num_entries = cpu_to_le32(ctx_pg->entries);
8786 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8787 	}
8788 	req->flags = cpu_to_le32(flags);
8789 	return hwrm_req_send(bp, req);
8790 }
8791 
8792 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8793 				  struct bnxt_ctx_pg_info *ctx_pg)
8794 {
8795 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8796 
8797 	rmem->page_size = BNXT_PAGE_SIZE;
8798 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8799 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8800 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8801 	if (rmem->depth >= 1)
8802 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8803 	return bnxt_alloc_ring(bp, rmem);
8804 }
8805 
8806 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8807 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8808 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8809 {
8810 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8811 	int rc;
8812 
8813 	if (!mem_size)
8814 		return -EINVAL;
8815 
8816 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8817 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8818 		ctx_pg->nr_pages = 0;
8819 		return -EINVAL;
8820 	}
8821 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8822 		int nr_tbls, i;
8823 
8824 		rmem->depth = 2;
8825 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8826 					     GFP_KERNEL);
8827 		if (!ctx_pg->ctx_pg_tbl)
8828 			return -ENOMEM;
8829 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8830 		rmem->nr_pages = nr_tbls;
8831 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8832 		if (rc)
8833 			return rc;
8834 		for (i = 0; i < nr_tbls; i++) {
8835 			struct bnxt_ctx_pg_info *pg_tbl;
8836 
8837 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8838 			if (!pg_tbl)
8839 				return -ENOMEM;
8840 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8841 			rmem = &pg_tbl->ring_mem;
8842 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8843 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8844 			rmem->depth = 1;
8845 			rmem->nr_pages = MAX_CTX_PAGES;
8846 			rmem->ctx_mem = ctxm;
8847 			if (i == (nr_tbls - 1)) {
8848 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8849 
8850 				if (rem)
8851 					rmem->nr_pages = rem;
8852 			}
8853 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8854 			if (rc)
8855 				break;
8856 		}
8857 	} else {
8858 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8859 		if (rmem->nr_pages > 1 || depth)
8860 			rmem->depth = 1;
8861 		rmem->ctx_mem = ctxm;
8862 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8863 	}
8864 	return rc;
8865 }
8866 
8867 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8868 				    struct bnxt_ctx_pg_info *ctx_pg,
8869 				    void *buf, size_t offset, size_t head,
8870 				    size_t tail)
8871 {
8872 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8873 	size_t nr_pages = ctx_pg->nr_pages;
8874 	int page_size = rmem->page_size;
8875 	size_t len = 0, total_len = 0;
8876 	u16 depth = rmem->depth;
8877 
8878 	tail %= nr_pages * page_size;
8879 	do {
8880 		if (depth > 1) {
8881 			int i = head / (page_size * MAX_CTX_PAGES);
8882 			struct bnxt_ctx_pg_info *pg_tbl;
8883 
8884 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8885 			rmem = &pg_tbl->ring_mem;
8886 		}
8887 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8888 		head += len;
8889 		offset += len;
8890 		total_len += len;
8891 		if (head >= nr_pages * page_size)
8892 			head = 0;
8893 	} while (head != tail);
8894 	return total_len;
8895 }
8896 
8897 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8898 				  struct bnxt_ctx_pg_info *ctx_pg)
8899 {
8900 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8901 
8902 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8903 	    ctx_pg->ctx_pg_tbl) {
8904 		int i, nr_tbls = rmem->nr_pages;
8905 
8906 		for (i = 0; i < nr_tbls; i++) {
8907 			struct bnxt_ctx_pg_info *pg_tbl;
8908 			struct bnxt_ring_mem_info *rmem2;
8909 
8910 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8911 			if (!pg_tbl)
8912 				continue;
8913 			rmem2 = &pg_tbl->ring_mem;
8914 			bnxt_free_ring(bp, rmem2);
8915 			ctx_pg->ctx_pg_arr[i] = NULL;
8916 			kfree(pg_tbl);
8917 			ctx_pg->ctx_pg_tbl[i] = NULL;
8918 		}
8919 		kfree(ctx_pg->ctx_pg_tbl);
8920 		ctx_pg->ctx_pg_tbl = NULL;
8921 	}
8922 	bnxt_free_ring(bp, rmem);
8923 	ctx_pg->nr_pages = 0;
8924 }
8925 
8926 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8927 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8928 				   u8 pg_lvl)
8929 {
8930 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8931 	int i, rc = 0, n = 1;
8932 	u32 mem_size;
8933 
8934 	if (!ctxm->entry_size || !ctx_pg)
8935 		return -EINVAL;
8936 	if (ctxm->instance_bmap)
8937 		n = hweight32(ctxm->instance_bmap);
8938 	if (ctxm->entry_multiple)
8939 		entries = roundup(entries, ctxm->entry_multiple);
8940 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8941 	mem_size = entries * ctxm->entry_size;
8942 	for (i = 0; i < n && !rc; i++) {
8943 		ctx_pg[i].entries = entries;
8944 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8945 					    ctxm->init_value ? ctxm : NULL);
8946 	}
8947 	if (!rc)
8948 		ctxm->mem_valid = 1;
8949 	return rc;
8950 }
8951 
8952 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8953 					       struct bnxt_ctx_mem_type *ctxm,
8954 					       bool last)
8955 {
8956 	struct hwrm_func_backing_store_cfg_v2_input *req;
8957 	u32 instance_bmap = ctxm->instance_bmap;
8958 	int i, j, rc = 0, n = 1;
8959 	__le32 *p;
8960 
8961 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8962 		return 0;
8963 
8964 	if (instance_bmap)
8965 		n = hweight32(ctxm->instance_bmap);
8966 	else
8967 		instance_bmap = 1;
8968 
8969 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8970 	if (rc)
8971 		return rc;
8972 	hwrm_req_hold(bp, req);
8973 	req->type = cpu_to_le16(ctxm->type);
8974 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8975 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
8976 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
8977 		struct bnxt_bs_trace_info *bs_trace;
8978 		u32 enables;
8979 
8980 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
8981 		req->enables = cpu_to_le32(enables);
8982 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
8983 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
8984 	}
8985 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8986 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8987 		p[i] = cpu_to_le32(ctxm->split[i]);
8988 	for (i = 0, j = 0; j < n && !rc; i++) {
8989 		struct bnxt_ctx_pg_info *ctx_pg;
8990 
8991 		if (!(instance_bmap & (1 << i)))
8992 			continue;
8993 		req->instance = cpu_to_le16(i);
8994 		ctx_pg = &ctxm->pg_info[j++];
8995 		if (!ctx_pg->entries)
8996 			continue;
8997 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8998 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8999 				      &req->page_size_pbl_level,
9000 				      &req->page_dir);
9001 		if (last && j == n)
9002 			req->flags =
9003 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
9004 		rc = hwrm_req_send(bp, req);
9005 	}
9006 	hwrm_req_drop(bp, req);
9007 	return rc;
9008 }
9009 
9010 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
9011 {
9012 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9013 	struct bnxt_ctx_mem_type *ctxm;
9014 	u16 last_type = BNXT_CTX_INV;
9015 	int rc = 0;
9016 	u16 type;
9017 
9018 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9019 		ctxm = &ctx->ctx_arr[type];
9020 		if (!bnxt_bs_trace_avail(bp, type))
9021 			continue;
9022 		if (!ctxm->mem_valid) {
9023 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9024 						     ctxm->max_entries, 1);
9025 			if (rc) {
9026 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9027 					    type);
9028 				continue;
9029 			}
9030 			bnxt_bs_trace_init(bp, ctxm);
9031 		}
9032 		last_type = type;
9033 	}
9034 
9035 	if (last_type == BNXT_CTX_INV) {
9036 		if (!ena)
9037 			return 0;
9038 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9039 			last_type = BNXT_CTX_MAX - 1;
9040 		else
9041 			last_type = BNXT_CTX_L2_MAX - 1;
9042 	}
9043 	ctx->ctx_arr[last_type].last = 1;
9044 
9045 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9046 		ctxm = &ctx->ctx_arr[type];
9047 
9048 		if (!ctxm->mem_valid)
9049 			continue;
9050 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9051 		if (rc)
9052 			return rc;
9053 	}
9054 	return 0;
9055 }
9056 
9057 /**
9058  * __bnxt_copy_ctx_mem - copy host context memory
9059  * @bp: The driver context
9060  * @ctxm: The pointer to the context memory type
9061  * @buf: The destination buffer or NULL to just obtain the length
9062  * @offset: The buffer offset to copy the data to
9063  * @head: The head offset of context memory to copy from
9064  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9065  *
9066  * This function is called for debugging purposes to dump the host context
9067  * used by the chip.
9068  *
9069  * Return: Length of memory copied
9070  */
9071 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9072 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9073 				  size_t offset, size_t head, size_t tail)
9074 {
9075 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9076 	size_t len = 0, total_len = 0;
9077 	int i, n = 1;
9078 
9079 	if (!ctx_pg)
9080 		return 0;
9081 
9082 	if (ctxm->instance_bmap)
9083 		n = hweight32(ctxm->instance_bmap);
9084 	for (i = 0; i < n; i++) {
9085 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9086 					    tail);
9087 		offset += len;
9088 		total_len += len;
9089 	}
9090 	return total_len;
9091 }
9092 
9093 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9094 			 void *buf, size_t offset)
9095 {
9096 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9097 
9098 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9099 }
9100 
9101 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9102 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9103 {
9104 	struct bnxt_ctx_pg_info *ctx_pg;
9105 	int i, n = 1;
9106 
9107 	ctxm->last = 0;
9108 
9109 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9110 		return;
9111 
9112 	ctx_pg = ctxm->pg_info;
9113 	if (ctx_pg) {
9114 		if (ctxm->instance_bmap)
9115 			n = hweight32(ctxm->instance_bmap);
9116 		for (i = 0; i < n; i++)
9117 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9118 
9119 		kfree(ctx_pg);
9120 		ctxm->pg_info = NULL;
9121 		ctxm->mem_valid = 0;
9122 	}
9123 	memset(ctxm, 0, sizeof(*ctxm));
9124 }
9125 
9126 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9127 {
9128 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9129 	u16 type;
9130 
9131 	if (!ctx)
9132 		return;
9133 
9134 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9135 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9136 
9137 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9138 	if (force) {
9139 		kfree(ctx);
9140 		bp->ctx = NULL;
9141 	}
9142 }
9143 
9144 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9145 {
9146 	struct bnxt_ctx_mem_type *ctxm;
9147 	struct bnxt_ctx_mem_info *ctx;
9148 	u32 l2_qps, qp1_qps, max_qps;
9149 	u32 ena, entries_sp, entries;
9150 	u32 srqs, max_srqs, min;
9151 	u32 num_mr, num_ah;
9152 	u32 extra_srqs = 0;
9153 	u32 extra_qps = 0;
9154 	u32 fast_qpmd_qps;
9155 	u8 pg_lvl = 1;
9156 	int i, rc;
9157 
9158 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9159 	if (rc) {
9160 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9161 			   rc);
9162 		return rc;
9163 	}
9164 	ctx = bp->ctx;
9165 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9166 		return 0;
9167 
9168 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9169 	l2_qps = ctxm->qp_l2_entries;
9170 	qp1_qps = ctxm->qp_qp1_entries;
9171 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9172 	max_qps = ctxm->max_entries;
9173 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9174 	srqs = ctxm->srq_l2_entries;
9175 	max_srqs = ctxm->max_entries;
9176 	ena = 0;
9177 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9178 		pg_lvl = 2;
9179 		if (BNXT_SW_RES_LMT(bp)) {
9180 			extra_qps = max_qps - l2_qps - qp1_qps;
9181 			extra_srqs = max_srqs - srqs;
9182 		} else {
9183 			extra_qps = min_t(u32, 65536,
9184 					  max_qps - l2_qps - qp1_qps);
9185 			/* allocate extra qps if fw supports RoCE fast qp
9186 			 * destroy feature
9187 			 */
9188 			extra_qps += fast_qpmd_qps;
9189 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9190 		}
9191 		if (fast_qpmd_qps)
9192 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9193 	}
9194 
9195 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9196 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9197 				     pg_lvl);
9198 	if (rc)
9199 		return rc;
9200 
9201 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9202 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9203 	if (rc)
9204 		return rc;
9205 
9206 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9207 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9208 				     extra_qps * 2, pg_lvl);
9209 	if (rc)
9210 		return rc;
9211 
9212 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9213 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9214 	if (rc)
9215 		return rc;
9216 
9217 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9218 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9219 	if (rc)
9220 		return rc;
9221 
9222 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9223 		goto skip_rdma;
9224 
9225 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9226 	if (BNXT_SW_RES_LMT(bp) &&
9227 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9228 		num_ah = ctxm->mrav_av_entries;
9229 		num_mr = ctxm->max_entries - num_ah;
9230 	} else {
9231 		/* 128K extra is needed to accommodate static AH context
9232 		 * allocation by f/w.
9233 		 */
9234 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9235 		num_ah = min_t(u32, num_mr, 1024 * 128);
9236 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9237 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9238 			ctxm->mrav_av_entries = num_ah;
9239 	}
9240 
9241 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9242 	if (rc)
9243 		return rc;
9244 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9245 
9246 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9247 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9248 	if (rc)
9249 		return rc;
9250 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9251 
9252 skip_rdma:
9253 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9254 	min = ctxm->min_entries;
9255 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9256 		     2 * (extra_qps + qp1_qps) + min;
9257 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9258 	if (rc)
9259 		return rc;
9260 
9261 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9262 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9263 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9264 	if (rc)
9265 		return rc;
9266 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9267 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9268 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9269 
9270 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9271 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9272 	else
9273 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9274 	if (rc) {
9275 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9276 			   rc);
9277 		return rc;
9278 	}
9279 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9280 	return 0;
9281 }
9282 
9283 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9284 {
9285 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9286 	u16 page_attr;
9287 	int rc;
9288 
9289 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9290 		return 0;
9291 
9292 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9293 	if (rc)
9294 		return rc;
9295 
9296 	if (BNXT_PAGE_SIZE == 0x2000)
9297 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9298 	else if (BNXT_PAGE_SIZE == 0x10000)
9299 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9300 	else
9301 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9302 	req->pg_size_lvl = cpu_to_le16(page_attr |
9303 				       bp->fw_crash_mem->ring_mem.depth);
9304 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9305 	req->size = cpu_to_le32(bp->fw_crash_len);
9306 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9307 	return hwrm_req_send(bp, req);
9308 }
9309 
9310 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9311 {
9312 	if (bp->fw_crash_mem) {
9313 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9314 		kfree(bp->fw_crash_mem);
9315 		bp->fw_crash_mem = NULL;
9316 	}
9317 }
9318 
9319 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9320 {
9321 	u32 mem_size = 0;
9322 	int rc;
9323 
9324 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9325 		return 0;
9326 
9327 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9328 	if (rc)
9329 		return rc;
9330 
9331 	mem_size = round_up(mem_size, 4);
9332 
9333 	/* keep and use the existing pages */
9334 	if (bp->fw_crash_mem &&
9335 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9336 		goto alloc_done;
9337 
9338 	if (bp->fw_crash_mem)
9339 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9340 	else
9341 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9342 					   GFP_KERNEL);
9343 	if (!bp->fw_crash_mem)
9344 		return -ENOMEM;
9345 
9346 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9347 	if (rc) {
9348 		bnxt_free_crash_dump_mem(bp);
9349 		return rc;
9350 	}
9351 
9352 alloc_done:
9353 	bp->fw_crash_len = mem_size;
9354 	return 0;
9355 }
9356 
9357 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9358 {
9359 	struct hwrm_func_resource_qcaps_output *resp;
9360 	struct hwrm_func_resource_qcaps_input *req;
9361 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9362 	int rc;
9363 
9364 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9365 	if (rc)
9366 		return rc;
9367 
9368 	req->fid = cpu_to_le16(0xffff);
9369 	resp = hwrm_req_hold(bp, req);
9370 	rc = hwrm_req_send_silent(bp, req);
9371 	if (rc)
9372 		goto hwrm_func_resc_qcaps_exit;
9373 
9374 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9375 	if (!all)
9376 		goto hwrm_func_resc_qcaps_exit;
9377 
9378 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9379 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9380 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9381 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9382 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9383 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9384 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9385 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9386 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9387 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9388 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9389 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9390 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9391 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9392 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9393 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9394 
9395 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9396 		u16 max_msix = le16_to_cpu(resp->max_msix);
9397 
9398 		hw_resc->max_nqs = max_msix;
9399 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9400 	}
9401 
9402 	if (BNXT_PF(bp)) {
9403 		struct bnxt_pf_info *pf = &bp->pf;
9404 
9405 		pf->vf_resv_strategy =
9406 			le16_to_cpu(resp->vf_reservation_strategy);
9407 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9408 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9409 	}
9410 hwrm_func_resc_qcaps_exit:
9411 	hwrm_req_drop(bp, req);
9412 	return rc;
9413 }
9414 
9415 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9416 {
9417 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9418 	struct hwrm_port_mac_ptp_qcfg_input *req;
9419 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9420 	u8 flags;
9421 	int rc;
9422 
9423 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9424 		rc = -ENODEV;
9425 		goto no_ptp;
9426 	}
9427 
9428 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9429 	if (rc)
9430 		goto no_ptp;
9431 
9432 	req->port_id = cpu_to_le16(bp->pf.port_id);
9433 	resp = hwrm_req_hold(bp, req);
9434 	rc = hwrm_req_send(bp, req);
9435 	if (rc)
9436 		goto exit;
9437 
9438 	flags = resp->flags;
9439 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9440 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9441 		rc = -ENODEV;
9442 		goto exit;
9443 	}
9444 	if (!ptp) {
9445 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9446 		if (!ptp) {
9447 			rc = -ENOMEM;
9448 			goto exit;
9449 		}
9450 		ptp->bp = bp;
9451 		bp->ptp_cfg = ptp;
9452 	}
9453 
9454 	if (flags &
9455 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9456 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9457 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9458 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9459 	} else if (BNXT_CHIP_P5(bp)) {
9460 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9461 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9462 	} else {
9463 		rc = -ENODEV;
9464 		goto exit;
9465 	}
9466 	ptp->rtc_configured =
9467 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9468 	rc = bnxt_ptp_init(bp);
9469 	if (rc)
9470 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9471 exit:
9472 	hwrm_req_drop(bp, req);
9473 	if (!rc)
9474 		return 0;
9475 
9476 no_ptp:
9477 	bnxt_ptp_clear(bp);
9478 	kfree(ptp);
9479 	bp->ptp_cfg = NULL;
9480 	return rc;
9481 }
9482 
9483 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9484 {
9485 	struct hwrm_func_qcaps_output *resp;
9486 	struct hwrm_func_qcaps_input *req;
9487 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9488 	u32 flags, flags_ext, flags_ext2;
9489 	int rc;
9490 
9491 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9492 	if (rc)
9493 		return rc;
9494 
9495 	req->fid = cpu_to_le16(0xffff);
9496 	resp = hwrm_req_hold(bp, req);
9497 	rc = hwrm_req_send(bp, req);
9498 	if (rc)
9499 		goto hwrm_func_qcaps_exit;
9500 
9501 	flags = le32_to_cpu(resp->flags);
9502 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9503 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9504 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9505 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9506 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9507 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9508 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9509 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9510 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9511 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9512 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9513 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9514 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9515 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9516 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9517 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9518 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9519 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9520 
9521 	flags_ext = le32_to_cpu(resp->flags_ext);
9522 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9523 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9524 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9525 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9526 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9527 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9528 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9529 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9530 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9531 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9532 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9533 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9534 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9535 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9536 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9537 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9538 
9539 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9540 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9541 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9542 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9543 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9544 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9545 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9546 	if (flags_ext2 &
9547 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9548 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9549 	if (BNXT_PF(bp) &&
9550 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9551 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9552 
9553 	bp->tx_push_thresh = 0;
9554 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9555 	    BNXT_FW_MAJ(bp) > 217)
9556 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9557 
9558 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9559 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9560 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9561 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9562 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9563 	if (!hw_resc->max_hw_ring_grps)
9564 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9565 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9566 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9567 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9568 
9569 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9570 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9571 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9572 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9573 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9574 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9575 
9576 	if (BNXT_PF(bp)) {
9577 		struct bnxt_pf_info *pf = &bp->pf;
9578 
9579 		pf->fw_fid = le16_to_cpu(resp->fid);
9580 		pf->port_id = le16_to_cpu(resp->port_id);
9581 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9582 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9583 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9584 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9585 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9586 			bp->flags |= BNXT_FLAG_WOL_CAP;
9587 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9588 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9589 		} else {
9590 			bnxt_ptp_clear(bp);
9591 			kfree(bp->ptp_cfg);
9592 			bp->ptp_cfg = NULL;
9593 		}
9594 	} else {
9595 #ifdef CONFIG_BNXT_SRIOV
9596 		struct bnxt_vf_info *vf = &bp->vf;
9597 
9598 		vf->fw_fid = le16_to_cpu(resp->fid);
9599 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9600 #endif
9601 	}
9602 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9603 
9604 hwrm_func_qcaps_exit:
9605 	hwrm_req_drop(bp, req);
9606 	return rc;
9607 }
9608 
9609 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9610 {
9611 	struct hwrm_dbg_qcaps_output *resp;
9612 	struct hwrm_dbg_qcaps_input *req;
9613 	int rc;
9614 
9615 	bp->fw_dbg_cap = 0;
9616 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9617 		return;
9618 
9619 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9620 	if (rc)
9621 		return;
9622 
9623 	req->fid = cpu_to_le16(0xffff);
9624 	resp = hwrm_req_hold(bp, req);
9625 	rc = hwrm_req_send(bp, req);
9626 	if (rc)
9627 		goto hwrm_dbg_qcaps_exit;
9628 
9629 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9630 
9631 hwrm_dbg_qcaps_exit:
9632 	hwrm_req_drop(bp, req);
9633 }
9634 
9635 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9636 
9637 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9638 {
9639 	int rc;
9640 
9641 	rc = __bnxt_hwrm_func_qcaps(bp);
9642 	if (rc)
9643 		return rc;
9644 
9645 	bnxt_hwrm_dbg_qcaps(bp);
9646 
9647 	rc = bnxt_hwrm_queue_qportcfg(bp);
9648 	if (rc) {
9649 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9650 		return rc;
9651 	}
9652 	if (bp->hwrm_spec_code >= 0x10803) {
9653 		rc = bnxt_alloc_ctx_mem(bp);
9654 		if (rc)
9655 			return rc;
9656 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9657 		if (!rc)
9658 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9659 	}
9660 	return 0;
9661 }
9662 
9663 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9664 {
9665 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9666 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9667 	u32 flags;
9668 	int rc;
9669 
9670 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9671 		return 0;
9672 
9673 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9674 	if (rc)
9675 		return rc;
9676 
9677 	resp = hwrm_req_hold(bp, req);
9678 	rc = hwrm_req_send(bp, req);
9679 	if (rc)
9680 		goto hwrm_cfa_adv_qcaps_exit;
9681 
9682 	flags = le32_to_cpu(resp->flags);
9683 	if (flags &
9684 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9685 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9686 
9687 	if (flags &
9688 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9689 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9690 
9691 	if (flags &
9692 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9693 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9694 
9695 hwrm_cfa_adv_qcaps_exit:
9696 	hwrm_req_drop(bp, req);
9697 	return rc;
9698 }
9699 
9700 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9701 {
9702 	if (bp->fw_health)
9703 		return 0;
9704 
9705 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9706 	if (!bp->fw_health)
9707 		return -ENOMEM;
9708 
9709 	mutex_init(&bp->fw_health->lock);
9710 	return 0;
9711 }
9712 
9713 static int bnxt_alloc_fw_health(struct bnxt *bp)
9714 {
9715 	int rc;
9716 
9717 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9718 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9719 		return 0;
9720 
9721 	rc = __bnxt_alloc_fw_health(bp);
9722 	if (rc) {
9723 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9724 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9725 		return rc;
9726 	}
9727 
9728 	return 0;
9729 }
9730 
9731 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9732 {
9733 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9734 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9735 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9736 }
9737 
9738 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9739 {
9740 	struct bnxt_fw_health *fw_health = bp->fw_health;
9741 	u32 reg_type;
9742 
9743 	if (!fw_health)
9744 		return;
9745 
9746 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9747 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9748 		fw_health->status_reliable = false;
9749 
9750 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9751 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9752 		fw_health->resets_reliable = false;
9753 }
9754 
9755 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9756 {
9757 	void __iomem *hs;
9758 	u32 status_loc;
9759 	u32 reg_type;
9760 	u32 sig;
9761 
9762 	if (bp->fw_health)
9763 		bp->fw_health->status_reliable = false;
9764 
9765 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9766 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9767 
9768 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9769 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9770 		if (!bp->chip_num) {
9771 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9772 			bp->chip_num = readl(bp->bar0 +
9773 					     BNXT_FW_HEALTH_WIN_BASE +
9774 					     BNXT_GRC_REG_CHIP_NUM);
9775 		}
9776 		if (!BNXT_CHIP_P5_PLUS(bp))
9777 			return;
9778 
9779 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9780 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9781 	} else {
9782 		status_loc = readl(hs + offsetof(struct hcomm_status,
9783 						 fw_status_loc));
9784 	}
9785 
9786 	if (__bnxt_alloc_fw_health(bp)) {
9787 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9788 		return;
9789 	}
9790 
9791 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9792 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9793 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9794 		__bnxt_map_fw_health_reg(bp, status_loc);
9795 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9796 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9797 	}
9798 
9799 	bp->fw_health->status_reliable = true;
9800 }
9801 
9802 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9803 {
9804 	struct bnxt_fw_health *fw_health = bp->fw_health;
9805 	u32 reg_base = 0xffffffff;
9806 	int i;
9807 
9808 	bp->fw_health->status_reliable = false;
9809 	bp->fw_health->resets_reliable = false;
9810 	/* Only pre-map the monitoring GRC registers using window 3 */
9811 	for (i = 0; i < 4; i++) {
9812 		u32 reg = fw_health->regs[i];
9813 
9814 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9815 			continue;
9816 		if (reg_base == 0xffffffff)
9817 			reg_base = reg & BNXT_GRC_BASE_MASK;
9818 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9819 			return -ERANGE;
9820 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9821 	}
9822 	bp->fw_health->status_reliable = true;
9823 	bp->fw_health->resets_reliable = true;
9824 	if (reg_base == 0xffffffff)
9825 		return 0;
9826 
9827 	__bnxt_map_fw_health_reg(bp, reg_base);
9828 	return 0;
9829 }
9830 
9831 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9832 {
9833 	if (!bp->fw_health)
9834 		return;
9835 
9836 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9837 		bp->fw_health->status_reliable = true;
9838 		bp->fw_health->resets_reliable = true;
9839 	} else {
9840 		bnxt_try_map_fw_health_reg(bp);
9841 	}
9842 }
9843 
9844 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9845 {
9846 	struct bnxt_fw_health *fw_health = bp->fw_health;
9847 	struct hwrm_error_recovery_qcfg_output *resp;
9848 	struct hwrm_error_recovery_qcfg_input *req;
9849 	int rc, i;
9850 
9851 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9852 		return 0;
9853 
9854 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9855 	if (rc)
9856 		return rc;
9857 
9858 	resp = hwrm_req_hold(bp, req);
9859 	rc = hwrm_req_send(bp, req);
9860 	if (rc)
9861 		goto err_recovery_out;
9862 	fw_health->flags = le32_to_cpu(resp->flags);
9863 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9864 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9865 		rc = -EINVAL;
9866 		goto err_recovery_out;
9867 	}
9868 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9869 	fw_health->master_func_wait_dsecs =
9870 		le32_to_cpu(resp->master_func_wait_period);
9871 	fw_health->normal_func_wait_dsecs =
9872 		le32_to_cpu(resp->normal_func_wait_period);
9873 	fw_health->post_reset_wait_dsecs =
9874 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9875 	fw_health->post_reset_max_wait_dsecs =
9876 		le32_to_cpu(resp->max_bailout_time_after_reset);
9877 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9878 		le32_to_cpu(resp->fw_health_status_reg);
9879 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9880 		le32_to_cpu(resp->fw_heartbeat_reg);
9881 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9882 		le32_to_cpu(resp->fw_reset_cnt_reg);
9883 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9884 		le32_to_cpu(resp->reset_inprogress_reg);
9885 	fw_health->fw_reset_inprog_reg_mask =
9886 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9887 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9888 	if (fw_health->fw_reset_seq_cnt >= 16) {
9889 		rc = -EINVAL;
9890 		goto err_recovery_out;
9891 	}
9892 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9893 		fw_health->fw_reset_seq_regs[i] =
9894 			le32_to_cpu(resp->reset_reg[i]);
9895 		fw_health->fw_reset_seq_vals[i] =
9896 			le32_to_cpu(resp->reset_reg_val[i]);
9897 		fw_health->fw_reset_seq_delay_msec[i] =
9898 			resp->delay_after_reset[i];
9899 	}
9900 err_recovery_out:
9901 	hwrm_req_drop(bp, req);
9902 	if (!rc)
9903 		rc = bnxt_map_fw_health_regs(bp);
9904 	if (rc)
9905 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9906 	return rc;
9907 }
9908 
9909 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9910 {
9911 	struct hwrm_func_reset_input *req;
9912 	int rc;
9913 
9914 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9915 	if (rc)
9916 		return rc;
9917 
9918 	req->enables = 0;
9919 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9920 	return hwrm_req_send(bp, req);
9921 }
9922 
9923 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9924 {
9925 	struct hwrm_nvm_get_dev_info_output nvm_info;
9926 
9927 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9928 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9929 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9930 			 nvm_info.nvm_cfg_ver_upd);
9931 }
9932 
9933 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9934 {
9935 	struct hwrm_queue_qportcfg_output *resp;
9936 	struct hwrm_queue_qportcfg_input *req;
9937 	u8 i, j, *qptr;
9938 	bool no_rdma;
9939 	int rc = 0;
9940 
9941 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9942 	if (rc)
9943 		return rc;
9944 
9945 	resp = hwrm_req_hold(bp, req);
9946 	rc = hwrm_req_send(bp, req);
9947 	if (rc)
9948 		goto qportcfg_exit;
9949 
9950 	if (!resp->max_configurable_queues) {
9951 		rc = -EINVAL;
9952 		goto qportcfg_exit;
9953 	}
9954 	bp->max_tc = resp->max_configurable_queues;
9955 	bp->max_lltc = resp->max_configurable_lossless_queues;
9956 	if (bp->max_tc > BNXT_MAX_QUEUE)
9957 		bp->max_tc = BNXT_MAX_QUEUE;
9958 
9959 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9960 	qptr = &resp->queue_id0;
9961 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9962 		bp->q_info[j].queue_id = *qptr;
9963 		bp->q_ids[i] = *qptr++;
9964 		bp->q_info[j].queue_profile = *qptr++;
9965 		bp->tc_to_qidx[j] = j;
9966 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9967 		    (no_rdma && BNXT_PF(bp)))
9968 			j++;
9969 	}
9970 	bp->max_q = bp->max_tc;
9971 	bp->max_tc = max_t(u8, j, 1);
9972 
9973 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9974 		bp->max_tc = 1;
9975 
9976 	if (bp->max_lltc > bp->max_tc)
9977 		bp->max_lltc = bp->max_tc;
9978 
9979 qportcfg_exit:
9980 	hwrm_req_drop(bp, req);
9981 	return rc;
9982 }
9983 
9984 static int bnxt_hwrm_poll(struct bnxt *bp)
9985 {
9986 	struct hwrm_ver_get_input *req;
9987 	int rc;
9988 
9989 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9990 	if (rc)
9991 		return rc;
9992 
9993 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9994 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9995 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9996 
9997 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9998 	rc = hwrm_req_send(bp, req);
9999 	return rc;
10000 }
10001 
10002 static int bnxt_hwrm_ver_get(struct bnxt *bp)
10003 {
10004 	struct hwrm_ver_get_output *resp;
10005 	struct hwrm_ver_get_input *req;
10006 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
10007 	u32 dev_caps_cfg, hwrm_ver;
10008 	int rc, len;
10009 
10010 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
10011 	if (rc)
10012 		return rc;
10013 
10014 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
10015 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
10016 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
10017 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
10018 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
10019 
10020 	resp = hwrm_req_hold(bp, req);
10021 	rc = hwrm_req_send(bp, req);
10022 	if (rc)
10023 		goto hwrm_ver_get_exit;
10024 
10025 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10026 
10027 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10028 			     resp->hwrm_intf_min_8b << 8 |
10029 			     resp->hwrm_intf_upd_8b;
10030 	if (resp->hwrm_intf_maj_8b < 1) {
10031 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10032 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10033 			    resp->hwrm_intf_upd_8b);
10034 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10035 	}
10036 
10037 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10038 			HWRM_VERSION_UPDATE;
10039 
10040 	if (bp->hwrm_spec_code > hwrm_ver)
10041 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10042 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10043 			 HWRM_VERSION_UPDATE);
10044 	else
10045 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10046 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10047 			 resp->hwrm_intf_upd_8b);
10048 
10049 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10050 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10051 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10052 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10053 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10054 		len = FW_VER_STR_LEN;
10055 	} else {
10056 		fw_maj = resp->hwrm_fw_maj_8b;
10057 		fw_min = resp->hwrm_fw_min_8b;
10058 		fw_bld = resp->hwrm_fw_bld_8b;
10059 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10060 		len = BC_HWRM_STR_LEN;
10061 	}
10062 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10063 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10064 		 fw_rsv);
10065 
10066 	if (strlen(resp->active_pkg_name)) {
10067 		int fw_ver_len = strlen(bp->fw_ver_str);
10068 
10069 		snprintf(bp->fw_ver_str + fw_ver_len,
10070 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10071 			 resp->active_pkg_name);
10072 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10073 	}
10074 
10075 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10076 	if (!bp->hwrm_cmd_timeout)
10077 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10078 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10079 	if (!bp->hwrm_cmd_max_timeout)
10080 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10081 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10082 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10083 			    bp->hwrm_cmd_max_timeout / 1000);
10084 
10085 	if (resp->hwrm_intf_maj_8b >= 1) {
10086 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10087 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10088 	}
10089 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10090 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10091 
10092 	bp->chip_num = le16_to_cpu(resp->chip_num);
10093 	bp->chip_rev = resp->chip_rev;
10094 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10095 	    !resp->chip_metal)
10096 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10097 
10098 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10099 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10100 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10101 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10102 
10103 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10104 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10105 
10106 	if (dev_caps_cfg &
10107 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10108 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10109 
10110 	if (dev_caps_cfg &
10111 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10112 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10113 
10114 	if (dev_caps_cfg &
10115 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10116 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10117 
10118 hwrm_ver_get_exit:
10119 	hwrm_req_drop(bp, req);
10120 	return rc;
10121 }
10122 
10123 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10124 {
10125 	struct hwrm_fw_set_time_input *req;
10126 	struct tm tm;
10127 	time64_t now = ktime_get_real_seconds();
10128 	int rc;
10129 
10130 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10131 	    bp->hwrm_spec_code < 0x10400)
10132 		return -EOPNOTSUPP;
10133 
10134 	time64_to_tm(now, 0, &tm);
10135 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10136 	if (rc)
10137 		return rc;
10138 
10139 	req->year = cpu_to_le16(1900 + tm.tm_year);
10140 	req->month = 1 + tm.tm_mon;
10141 	req->day = tm.tm_mday;
10142 	req->hour = tm.tm_hour;
10143 	req->minute = tm.tm_min;
10144 	req->second = tm.tm_sec;
10145 	return hwrm_req_send(bp, req);
10146 }
10147 
10148 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10149 {
10150 	u64 sw_tmp;
10151 
10152 	hw &= mask;
10153 	sw_tmp = (*sw & ~mask) | hw;
10154 	if (hw < (*sw & mask))
10155 		sw_tmp += mask + 1;
10156 	WRITE_ONCE(*sw, sw_tmp);
10157 }
10158 
10159 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10160 				    int count, bool ignore_zero)
10161 {
10162 	int i;
10163 
10164 	for (i = 0; i < count; i++) {
10165 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10166 
10167 		if (ignore_zero && !hw)
10168 			continue;
10169 
10170 		if (masks[i] == -1ULL)
10171 			sw_stats[i] = hw;
10172 		else
10173 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10174 	}
10175 }
10176 
10177 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10178 {
10179 	if (!stats->hw_stats)
10180 		return;
10181 
10182 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10183 				stats->hw_masks, stats->len / 8, false);
10184 }
10185 
10186 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10187 {
10188 	struct bnxt_stats_mem *ring0_stats;
10189 	bool ignore_zero = false;
10190 	int i;
10191 
10192 	/* Chip bug.  Counter intermittently becomes 0. */
10193 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10194 		ignore_zero = true;
10195 
10196 	for (i = 0; i < bp->cp_nr_rings; i++) {
10197 		struct bnxt_napi *bnapi = bp->bnapi[i];
10198 		struct bnxt_cp_ring_info *cpr;
10199 		struct bnxt_stats_mem *stats;
10200 
10201 		cpr = &bnapi->cp_ring;
10202 		stats = &cpr->stats;
10203 		if (!i)
10204 			ring0_stats = stats;
10205 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10206 					ring0_stats->hw_masks,
10207 					ring0_stats->len / 8, ignore_zero);
10208 	}
10209 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10210 		struct bnxt_stats_mem *stats = &bp->port_stats;
10211 		__le64 *hw_stats = stats->hw_stats;
10212 		u64 *sw_stats = stats->sw_stats;
10213 		u64 *masks = stats->hw_masks;
10214 		int cnt;
10215 
10216 		cnt = sizeof(struct rx_port_stats) / 8;
10217 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10218 
10219 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10220 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10221 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10222 		cnt = sizeof(struct tx_port_stats) / 8;
10223 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10224 	}
10225 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10226 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10227 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10228 	}
10229 }
10230 
10231 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10232 {
10233 	struct hwrm_port_qstats_input *req;
10234 	struct bnxt_pf_info *pf = &bp->pf;
10235 	int rc;
10236 
10237 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10238 		return 0;
10239 
10240 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10241 		return -EOPNOTSUPP;
10242 
10243 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10244 	if (rc)
10245 		return rc;
10246 
10247 	req->flags = flags;
10248 	req->port_id = cpu_to_le16(pf->port_id);
10249 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10250 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10251 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10252 	return hwrm_req_send(bp, req);
10253 }
10254 
10255 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10256 {
10257 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10258 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10259 	struct hwrm_port_qstats_ext_output *resp_qs;
10260 	struct hwrm_port_qstats_ext_input *req_qs;
10261 	struct bnxt_pf_info *pf = &bp->pf;
10262 	u32 tx_stat_size;
10263 	int rc;
10264 
10265 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10266 		return 0;
10267 
10268 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10269 		return -EOPNOTSUPP;
10270 
10271 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10272 	if (rc)
10273 		return rc;
10274 
10275 	req_qs->flags = flags;
10276 	req_qs->port_id = cpu_to_le16(pf->port_id);
10277 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10278 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10279 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10280 		       sizeof(struct tx_port_stats_ext) : 0;
10281 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10282 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10283 	resp_qs = hwrm_req_hold(bp, req_qs);
10284 	rc = hwrm_req_send(bp, req_qs);
10285 	if (!rc) {
10286 		bp->fw_rx_stats_ext_size =
10287 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10288 		if (BNXT_FW_MAJ(bp) < 220 &&
10289 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10290 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10291 
10292 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10293 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10294 	} else {
10295 		bp->fw_rx_stats_ext_size = 0;
10296 		bp->fw_tx_stats_ext_size = 0;
10297 	}
10298 	hwrm_req_drop(bp, req_qs);
10299 
10300 	if (flags)
10301 		return rc;
10302 
10303 	if (bp->fw_tx_stats_ext_size <=
10304 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10305 		bp->pri2cos_valid = 0;
10306 		return rc;
10307 	}
10308 
10309 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10310 	if (rc)
10311 		return rc;
10312 
10313 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10314 
10315 	resp_qc = hwrm_req_hold(bp, req_qc);
10316 	rc = hwrm_req_send(bp, req_qc);
10317 	if (!rc) {
10318 		u8 *pri2cos;
10319 		int i, j;
10320 
10321 		pri2cos = &resp_qc->pri0_cos_queue_id;
10322 		for (i = 0; i < 8; i++) {
10323 			u8 queue_id = pri2cos[i];
10324 			u8 queue_idx;
10325 
10326 			/* Per port queue IDs start from 0, 10, 20, etc */
10327 			queue_idx = queue_id % 10;
10328 			if (queue_idx > BNXT_MAX_QUEUE) {
10329 				bp->pri2cos_valid = false;
10330 				hwrm_req_drop(bp, req_qc);
10331 				return rc;
10332 			}
10333 			for (j = 0; j < bp->max_q; j++) {
10334 				if (bp->q_ids[j] == queue_id)
10335 					bp->pri2cos_idx[i] = queue_idx;
10336 			}
10337 		}
10338 		bp->pri2cos_valid = true;
10339 	}
10340 	hwrm_req_drop(bp, req_qc);
10341 
10342 	return rc;
10343 }
10344 
10345 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10346 {
10347 	bnxt_hwrm_tunnel_dst_port_free(bp,
10348 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10349 	bnxt_hwrm_tunnel_dst_port_free(bp,
10350 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10351 }
10352 
10353 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10354 {
10355 	int rc, i;
10356 	u32 tpa_flags = 0;
10357 
10358 	if (set_tpa)
10359 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10360 	else if (BNXT_NO_FW_ACCESS(bp))
10361 		return 0;
10362 	for (i = 0; i < bp->nr_vnics; i++) {
10363 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10364 		if (rc) {
10365 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10366 				   i, rc);
10367 			return rc;
10368 		}
10369 	}
10370 	return 0;
10371 }
10372 
10373 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10374 {
10375 	int i;
10376 
10377 	for (i = 0; i < bp->nr_vnics; i++)
10378 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10379 }
10380 
10381 static void bnxt_clear_vnic(struct bnxt *bp)
10382 {
10383 	if (!bp->vnic_info)
10384 		return;
10385 
10386 	bnxt_hwrm_clear_vnic_filter(bp);
10387 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10388 		/* clear all RSS setting before free vnic ctx */
10389 		bnxt_hwrm_clear_vnic_rss(bp);
10390 		bnxt_hwrm_vnic_ctx_free(bp);
10391 	}
10392 	/* before free the vnic, undo the vnic tpa settings */
10393 	if (bp->flags & BNXT_FLAG_TPA)
10394 		bnxt_set_tpa(bp, false);
10395 	bnxt_hwrm_vnic_free(bp);
10396 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10397 		bnxt_hwrm_vnic_ctx_free(bp);
10398 }
10399 
10400 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10401 				    bool irq_re_init)
10402 {
10403 	bnxt_clear_vnic(bp);
10404 	bnxt_hwrm_ring_free(bp, close_path);
10405 	bnxt_hwrm_ring_grp_free(bp);
10406 	if (irq_re_init) {
10407 		bnxt_hwrm_stat_ctx_free(bp);
10408 		bnxt_hwrm_free_tunnel_ports(bp);
10409 	}
10410 }
10411 
10412 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10413 {
10414 	struct hwrm_func_cfg_input *req;
10415 	u8 evb_mode;
10416 	int rc;
10417 
10418 	if (br_mode == BRIDGE_MODE_VEB)
10419 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10420 	else if (br_mode == BRIDGE_MODE_VEPA)
10421 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10422 	else
10423 		return -EINVAL;
10424 
10425 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10426 	if (rc)
10427 		return rc;
10428 
10429 	req->fid = cpu_to_le16(0xffff);
10430 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10431 	req->evb_mode = evb_mode;
10432 	return hwrm_req_send(bp, req);
10433 }
10434 
10435 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10436 {
10437 	struct hwrm_func_cfg_input *req;
10438 	int rc;
10439 
10440 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10441 		return 0;
10442 
10443 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10444 	if (rc)
10445 		return rc;
10446 
10447 	req->fid = cpu_to_le16(0xffff);
10448 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10449 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10450 	if (size == 128)
10451 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10452 
10453 	return hwrm_req_send(bp, req);
10454 }
10455 
10456 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10457 {
10458 	int rc;
10459 
10460 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10461 		goto skip_rss_ctx;
10462 
10463 	/* allocate context for vnic */
10464 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10465 	if (rc) {
10466 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10467 			   vnic->vnic_id, rc);
10468 		goto vnic_setup_err;
10469 	}
10470 	bp->rsscos_nr_ctxs++;
10471 
10472 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10473 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10474 		if (rc) {
10475 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10476 				   vnic->vnic_id, rc);
10477 			goto vnic_setup_err;
10478 		}
10479 		bp->rsscos_nr_ctxs++;
10480 	}
10481 
10482 skip_rss_ctx:
10483 	/* configure default vnic, ring grp */
10484 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10485 	if (rc) {
10486 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10487 			   vnic->vnic_id, rc);
10488 		goto vnic_setup_err;
10489 	}
10490 
10491 	/* Enable RSS hashing on vnic */
10492 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10493 	if (rc) {
10494 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10495 			   vnic->vnic_id, rc);
10496 		goto vnic_setup_err;
10497 	}
10498 
10499 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10500 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10501 		if (rc) {
10502 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10503 				   vnic->vnic_id, rc);
10504 		}
10505 	}
10506 
10507 vnic_setup_err:
10508 	return rc;
10509 }
10510 
10511 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10512 			  u8 valid)
10513 {
10514 	struct hwrm_vnic_update_input *req;
10515 	int rc;
10516 
10517 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10518 	if (rc)
10519 		return rc;
10520 
10521 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10522 
10523 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10524 		req->mru = cpu_to_le16(vnic->mru);
10525 
10526 	req->enables = cpu_to_le32(valid);
10527 
10528 	return hwrm_req_send(bp, req);
10529 }
10530 
10531 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10532 {
10533 	int rc;
10534 
10535 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10536 	if (rc) {
10537 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10538 			   vnic->vnic_id, rc);
10539 		return rc;
10540 	}
10541 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10542 	if (rc)
10543 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10544 			   vnic->vnic_id, rc);
10545 	return rc;
10546 }
10547 
10548 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10549 {
10550 	int rc, i, nr_ctxs;
10551 
10552 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10553 	for (i = 0; i < nr_ctxs; i++) {
10554 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10555 		if (rc) {
10556 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10557 				   vnic->vnic_id, i, rc);
10558 			break;
10559 		}
10560 		bp->rsscos_nr_ctxs++;
10561 	}
10562 	if (i < nr_ctxs)
10563 		return -ENOMEM;
10564 
10565 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10566 	if (rc)
10567 		return rc;
10568 
10569 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10570 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10571 		if (rc) {
10572 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10573 				   vnic->vnic_id, rc);
10574 		}
10575 	}
10576 	return rc;
10577 }
10578 
10579 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10580 {
10581 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10582 		return __bnxt_setup_vnic_p5(bp, vnic);
10583 	else
10584 		return __bnxt_setup_vnic(bp, vnic);
10585 }
10586 
10587 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10588 				     struct bnxt_vnic_info *vnic,
10589 				     u16 start_rx_ring_idx, int rx_rings)
10590 {
10591 	int rc;
10592 
10593 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10594 	if (rc) {
10595 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10596 			   vnic->vnic_id, rc);
10597 		return rc;
10598 	}
10599 	return bnxt_setup_vnic(bp, vnic);
10600 }
10601 
10602 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10603 {
10604 	struct bnxt_vnic_info *vnic;
10605 	int i, rc = 0;
10606 
10607 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10608 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10609 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10610 	}
10611 
10612 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10613 		return 0;
10614 
10615 	for (i = 0; i < bp->rx_nr_rings; i++) {
10616 		u16 vnic_id = i + 1;
10617 		u16 ring_id = i;
10618 
10619 		if (vnic_id >= bp->nr_vnics)
10620 			break;
10621 
10622 		vnic = &bp->vnic_info[vnic_id];
10623 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10624 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10625 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10626 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10627 			break;
10628 	}
10629 	return rc;
10630 }
10631 
10632 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10633 			  bool all)
10634 {
10635 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10636 	struct bnxt_filter_base *usr_fltr, *tmp;
10637 	struct bnxt_ntuple_filter *ntp_fltr;
10638 	int i;
10639 
10640 	if (netif_running(bp->dev)) {
10641 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10642 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10643 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10644 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10645 		}
10646 	}
10647 	if (!all)
10648 		return;
10649 
10650 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10651 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10652 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10653 			ntp_fltr = container_of(usr_fltr,
10654 						struct bnxt_ntuple_filter,
10655 						base);
10656 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10657 			bnxt_del_ntp_filter(bp, ntp_fltr);
10658 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10659 		}
10660 	}
10661 
10662 	if (vnic->rss_table)
10663 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10664 				  vnic->rss_table,
10665 				  vnic->rss_table_dma_addr);
10666 	bp->num_rss_ctx--;
10667 }
10668 
10669 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10670 {
10671 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10672 	struct ethtool_rxfh_context *ctx;
10673 	unsigned long context;
10674 
10675 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10676 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10677 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10678 
10679 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10680 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10681 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10682 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10683 				   rss_ctx->index);
10684 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10685 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10686 		}
10687 	}
10688 }
10689 
10690 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10691 {
10692 	struct ethtool_rxfh_context *ctx;
10693 	unsigned long context;
10694 
10695 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10696 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10697 
10698 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10699 	}
10700 }
10701 
10702 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10703 static bool bnxt_promisc_ok(struct bnxt *bp)
10704 {
10705 #ifdef CONFIG_BNXT_SRIOV
10706 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10707 		return false;
10708 #endif
10709 	return true;
10710 }
10711 
10712 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10713 {
10714 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10715 	unsigned int rc = 0;
10716 
10717 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10718 	if (rc) {
10719 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10720 			   rc);
10721 		return rc;
10722 	}
10723 
10724 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10725 	if (rc) {
10726 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10727 			   rc);
10728 		return rc;
10729 	}
10730 	return rc;
10731 }
10732 
10733 static int bnxt_cfg_rx_mode(struct bnxt *);
10734 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10735 
10736 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10737 {
10738 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10739 	int rc = 0;
10740 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10741 
10742 	if (irq_re_init) {
10743 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10744 		if (rc) {
10745 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10746 				   rc);
10747 			goto err_out;
10748 		}
10749 	}
10750 
10751 	rc = bnxt_hwrm_ring_alloc(bp);
10752 	if (rc) {
10753 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10754 		goto err_out;
10755 	}
10756 
10757 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10758 	if (rc) {
10759 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10760 		goto err_out;
10761 	}
10762 
10763 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10764 		rx_nr_rings--;
10765 
10766 	/* default vnic 0 */
10767 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10768 	if (rc) {
10769 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10770 		goto err_out;
10771 	}
10772 
10773 	if (BNXT_VF(bp))
10774 		bnxt_hwrm_func_qcfg(bp);
10775 
10776 	rc = bnxt_setup_vnic(bp, vnic);
10777 	if (rc)
10778 		goto err_out;
10779 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10780 		bnxt_hwrm_update_rss_hash_cfg(bp);
10781 
10782 	if (bp->flags & BNXT_FLAG_RFS) {
10783 		rc = bnxt_alloc_rfs_vnics(bp);
10784 		if (rc)
10785 			goto err_out;
10786 	}
10787 
10788 	if (bp->flags & BNXT_FLAG_TPA) {
10789 		rc = bnxt_set_tpa(bp, true);
10790 		if (rc)
10791 			goto err_out;
10792 	}
10793 
10794 	if (BNXT_VF(bp))
10795 		bnxt_update_vf_mac(bp);
10796 
10797 	/* Filter for default vnic 0 */
10798 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10799 	if (rc) {
10800 		if (BNXT_VF(bp) && rc == -ENODEV)
10801 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10802 		else
10803 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10804 		goto err_out;
10805 	}
10806 	vnic->uc_filter_count = 1;
10807 
10808 	vnic->rx_mask = 0;
10809 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10810 		goto skip_rx_mask;
10811 
10812 	if (bp->dev->flags & IFF_BROADCAST)
10813 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10814 
10815 	if (bp->dev->flags & IFF_PROMISC)
10816 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10817 
10818 	if (bp->dev->flags & IFF_ALLMULTI) {
10819 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10820 		vnic->mc_list_count = 0;
10821 	} else if (bp->dev->flags & IFF_MULTICAST) {
10822 		u32 mask = 0;
10823 
10824 		bnxt_mc_list_updated(bp, &mask);
10825 		vnic->rx_mask |= mask;
10826 	}
10827 
10828 	rc = bnxt_cfg_rx_mode(bp);
10829 	if (rc)
10830 		goto err_out;
10831 
10832 skip_rx_mask:
10833 	rc = bnxt_hwrm_set_coal(bp);
10834 	if (rc)
10835 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10836 				rc);
10837 
10838 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10839 		rc = bnxt_setup_nitroa0_vnic(bp);
10840 		if (rc)
10841 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10842 				   rc);
10843 	}
10844 
10845 	if (BNXT_VF(bp)) {
10846 		bnxt_hwrm_func_qcfg(bp);
10847 		netdev_update_features(bp->dev);
10848 	}
10849 
10850 	return 0;
10851 
10852 err_out:
10853 	bnxt_hwrm_resource_free(bp, 0, true);
10854 
10855 	return rc;
10856 }
10857 
10858 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10859 {
10860 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10861 	return 0;
10862 }
10863 
10864 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10865 {
10866 	bnxt_init_cp_rings(bp);
10867 	bnxt_init_rx_rings(bp);
10868 	bnxt_init_tx_rings(bp);
10869 	bnxt_init_ring_grps(bp, irq_re_init);
10870 	bnxt_init_vnics(bp);
10871 
10872 	return bnxt_init_chip(bp, irq_re_init);
10873 }
10874 
10875 static int bnxt_set_real_num_queues(struct bnxt *bp)
10876 {
10877 	int rc;
10878 	struct net_device *dev = bp->dev;
10879 
10880 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10881 					  bp->tx_nr_rings_xdp);
10882 	if (rc)
10883 		return rc;
10884 
10885 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10886 	if (rc)
10887 		return rc;
10888 
10889 #ifdef CONFIG_RFS_ACCEL
10890 	if (bp->flags & BNXT_FLAG_RFS)
10891 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10892 #endif
10893 
10894 	return rc;
10895 }
10896 
10897 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10898 			     bool shared)
10899 {
10900 	int _rx = *rx, _tx = *tx;
10901 
10902 	if (shared) {
10903 		*rx = min_t(int, _rx, max);
10904 		*tx = min_t(int, _tx, max);
10905 	} else {
10906 		if (max < 2)
10907 			return -ENOMEM;
10908 
10909 		while (_rx + _tx > max) {
10910 			if (_rx > _tx && _rx > 1)
10911 				_rx--;
10912 			else if (_tx > 1)
10913 				_tx--;
10914 		}
10915 		*rx = _rx;
10916 		*tx = _tx;
10917 	}
10918 	return 0;
10919 }
10920 
10921 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10922 {
10923 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10924 }
10925 
10926 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10927 {
10928 	int tcs = bp->num_tc;
10929 
10930 	if (!tcs)
10931 		tcs = 1;
10932 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10933 }
10934 
10935 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10936 {
10937 	int tcs = bp->num_tc;
10938 
10939 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10940 	       bp->tx_nr_rings_xdp;
10941 }
10942 
10943 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10944 			   bool sh)
10945 {
10946 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10947 
10948 	if (tx_cp != *tx) {
10949 		int tx_saved = tx_cp, rc;
10950 
10951 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10952 		if (rc)
10953 			return rc;
10954 		if (tx_cp != tx_saved)
10955 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10956 		return 0;
10957 	}
10958 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10959 }
10960 
10961 static void bnxt_setup_msix(struct bnxt *bp)
10962 {
10963 	const int len = sizeof(bp->irq_tbl[0].name);
10964 	struct net_device *dev = bp->dev;
10965 	int tcs, i;
10966 
10967 	tcs = bp->num_tc;
10968 	if (tcs) {
10969 		int i, off, count;
10970 
10971 		for (i = 0; i < tcs; i++) {
10972 			count = bp->tx_nr_rings_per_tc;
10973 			off = BNXT_TC_TO_RING_BASE(bp, i);
10974 			netdev_set_tc_queue(dev, i, count, off);
10975 		}
10976 	}
10977 
10978 	for (i = 0; i < bp->cp_nr_rings; i++) {
10979 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10980 		char *attr;
10981 
10982 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10983 			attr = "TxRx";
10984 		else if (i < bp->rx_nr_rings)
10985 			attr = "rx";
10986 		else
10987 			attr = "tx";
10988 
10989 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10990 			 attr, i);
10991 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10992 	}
10993 }
10994 
10995 static int bnxt_init_int_mode(struct bnxt *bp);
10996 
10997 static int bnxt_change_msix(struct bnxt *bp, int total)
10998 {
10999 	struct msi_map map;
11000 	int i;
11001 
11002 	/* add MSIX to the end if needed */
11003 	for (i = bp->total_irqs; i < total; i++) {
11004 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
11005 		if (map.index < 0)
11006 			return bp->total_irqs;
11007 		bp->irq_tbl[i].vector = map.virq;
11008 		bp->total_irqs++;
11009 	}
11010 
11011 	/* trim MSIX from the end if needed */
11012 	for (i = bp->total_irqs; i > total; i--) {
11013 		map.index = i - 1;
11014 		map.virq = bp->irq_tbl[i - 1].vector;
11015 		pci_msix_free_irq(bp->pdev, map);
11016 		bp->total_irqs--;
11017 	}
11018 	return bp->total_irqs;
11019 }
11020 
11021 static int bnxt_setup_int_mode(struct bnxt *bp)
11022 {
11023 	int rc;
11024 
11025 	if (!bp->irq_tbl) {
11026 		rc = bnxt_init_int_mode(bp);
11027 		if (rc || !bp->irq_tbl)
11028 			return rc ?: -ENODEV;
11029 	}
11030 
11031 	bnxt_setup_msix(bp);
11032 
11033 	rc = bnxt_set_real_num_queues(bp);
11034 	return rc;
11035 }
11036 
11037 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11038 {
11039 	return bp->hw_resc.max_rsscos_ctxs;
11040 }
11041 
11042 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11043 {
11044 	return bp->hw_resc.max_vnics;
11045 }
11046 
11047 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11048 {
11049 	return bp->hw_resc.max_stat_ctxs;
11050 }
11051 
11052 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11053 {
11054 	return bp->hw_resc.max_cp_rings;
11055 }
11056 
11057 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11058 {
11059 	unsigned int cp = bp->hw_resc.max_cp_rings;
11060 
11061 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11062 		cp -= bnxt_get_ulp_msix_num(bp);
11063 
11064 	return cp;
11065 }
11066 
11067 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11068 {
11069 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11070 
11071 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11072 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11073 
11074 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11075 }
11076 
11077 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11078 {
11079 	bp->hw_resc.max_irqs = max_irqs;
11080 }
11081 
11082 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11083 {
11084 	unsigned int cp;
11085 
11086 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11087 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11088 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11089 	else
11090 		return cp - bp->cp_nr_rings;
11091 }
11092 
11093 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11094 {
11095 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11096 }
11097 
11098 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11099 {
11100 	int max_irq = bnxt_get_max_func_irqs(bp);
11101 	int total_req = bp->cp_nr_rings + num;
11102 
11103 	if (max_irq < total_req) {
11104 		num = max_irq - bp->cp_nr_rings;
11105 		if (num <= 0)
11106 			return 0;
11107 	}
11108 	return num;
11109 }
11110 
11111 static int bnxt_get_num_msix(struct bnxt *bp)
11112 {
11113 	if (!BNXT_NEW_RM(bp))
11114 		return bnxt_get_max_func_irqs(bp);
11115 
11116 	return bnxt_nq_rings_in_use(bp);
11117 }
11118 
11119 static int bnxt_init_int_mode(struct bnxt *bp)
11120 {
11121 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11122 
11123 	total_vecs = bnxt_get_num_msix(bp);
11124 	max = bnxt_get_max_func_irqs(bp);
11125 	if (total_vecs > max)
11126 		total_vecs = max;
11127 
11128 	if (!total_vecs)
11129 		return 0;
11130 
11131 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11132 		min = 2;
11133 
11134 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11135 					   PCI_IRQ_MSIX);
11136 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11137 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11138 		rc = -ENODEV;
11139 		goto msix_setup_exit;
11140 	}
11141 
11142 	tbl_size = total_vecs;
11143 	if (pci_msix_can_alloc_dyn(bp->pdev))
11144 		tbl_size = max;
11145 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11146 	if (bp->irq_tbl) {
11147 		for (i = 0; i < total_vecs; i++)
11148 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11149 
11150 		bp->total_irqs = total_vecs;
11151 		/* Trim rings based upon num of vectors allocated */
11152 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11153 				     total_vecs - ulp_msix, min == 1);
11154 		if (rc)
11155 			goto msix_setup_exit;
11156 
11157 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11158 		bp->cp_nr_rings = (min == 1) ?
11159 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11160 				  tx_cp + bp->rx_nr_rings;
11161 
11162 	} else {
11163 		rc = -ENOMEM;
11164 		goto msix_setup_exit;
11165 	}
11166 	return 0;
11167 
11168 msix_setup_exit:
11169 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11170 	kfree(bp->irq_tbl);
11171 	bp->irq_tbl = NULL;
11172 	pci_free_irq_vectors(bp->pdev);
11173 	return rc;
11174 }
11175 
11176 static void bnxt_clear_int_mode(struct bnxt *bp)
11177 {
11178 	pci_free_irq_vectors(bp->pdev);
11179 
11180 	kfree(bp->irq_tbl);
11181 	bp->irq_tbl = NULL;
11182 }
11183 
11184 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11185 {
11186 	bool irq_cleared = false;
11187 	bool irq_change = false;
11188 	int tcs = bp->num_tc;
11189 	int irqs_required;
11190 	int rc;
11191 
11192 	if (!bnxt_need_reserve_rings(bp))
11193 		return 0;
11194 
11195 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11196 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11197 
11198 		if (ulp_msix > bp->ulp_num_msix_want)
11199 			ulp_msix = bp->ulp_num_msix_want;
11200 		irqs_required = ulp_msix + bp->cp_nr_rings;
11201 	} else {
11202 		irqs_required = bnxt_get_num_msix(bp);
11203 	}
11204 
11205 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11206 		irq_change = true;
11207 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11208 			bnxt_ulp_irq_stop(bp);
11209 			bnxt_clear_int_mode(bp);
11210 			irq_cleared = true;
11211 		}
11212 	}
11213 	rc = __bnxt_reserve_rings(bp);
11214 	if (irq_cleared) {
11215 		if (!rc)
11216 			rc = bnxt_init_int_mode(bp);
11217 		bnxt_ulp_irq_restart(bp, rc);
11218 	} else if (irq_change && !rc) {
11219 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11220 			rc = -ENOSPC;
11221 	}
11222 	if (rc) {
11223 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11224 		return rc;
11225 	}
11226 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11227 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11228 		netdev_err(bp->dev, "tx ring reservation failure\n");
11229 		netdev_reset_tc(bp->dev);
11230 		bp->num_tc = 0;
11231 		if (bp->tx_nr_rings_xdp)
11232 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11233 		else
11234 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11235 		return -ENOMEM;
11236 	}
11237 	return 0;
11238 }
11239 
11240 static void bnxt_free_irq(struct bnxt *bp)
11241 {
11242 	struct bnxt_irq *irq;
11243 	int i;
11244 
11245 #ifdef CONFIG_RFS_ACCEL
11246 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11247 	bp->dev->rx_cpu_rmap = NULL;
11248 #endif
11249 	if (!bp->irq_tbl || !bp->bnapi)
11250 		return;
11251 
11252 	for (i = 0; i < bp->cp_nr_rings; i++) {
11253 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11254 
11255 		irq = &bp->irq_tbl[map_idx];
11256 		if (irq->requested) {
11257 			if (irq->have_cpumask) {
11258 				irq_update_affinity_hint(irq->vector, NULL);
11259 				free_cpumask_var(irq->cpu_mask);
11260 				irq->have_cpumask = 0;
11261 			}
11262 			free_irq(irq->vector, bp->bnapi[i]);
11263 		}
11264 
11265 		irq->requested = 0;
11266 	}
11267 }
11268 
11269 static int bnxt_request_irq(struct bnxt *bp)
11270 {
11271 	int i, j, rc = 0;
11272 	unsigned long flags = 0;
11273 #ifdef CONFIG_RFS_ACCEL
11274 	struct cpu_rmap *rmap;
11275 #endif
11276 
11277 	rc = bnxt_setup_int_mode(bp);
11278 	if (rc) {
11279 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11280 			   rc);
11281 		return rc;
11282 	}
11283 #ifdef CONFIG_RFS_ACCEL
11284 	rmap = bp->dev->rx_cpu_rmap;
11285 #endif
11286 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11287 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11288 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11289 
11290 #ifdef CONFIG_RFS_ACCEL
11291 		if (rmap && bp->bnapi[i]->rx_ring) {
11292 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11293 			if (rc)
11294 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11295 					    j);
11296 			j++;
11297 		}
11298 #endif
11299 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11300 				 bp->bnapi[i]);
11301 		if (rc)
11302 			break;
11303 
11304 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
11305 		irq->requested = 1;
11306 
11307 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11308 			int numa_node = dev_to_node(&bp->pdev->dev);
11309 
11310 			irq->have_cpumask = 1;
11311 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11312 					irq->cpu_mask);
11313 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11314 			if (rc) {
11315 				netdev_warn(bp->dev,
11316 					    "Update affinity hint failed, IRQ = %d\n",
11317 					    irq->vector);
11318 				break;
11319 			}
11320 		}
11321 	}
11322 	return rc;
11323 }
11324 
11325 static void bnxt_del_napi(struct bnxt *bp)
11326 {
11327 	int i;
11328 
11329 	if (!bp->bnapi)
11330 		return;
11331 
11332 	for (i = 0; i < bp->rx_nr_rings; i++)
11333 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11334 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11335 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11336 
11337 	for (i = 0; i < bp->cp_nr_rings; i++) {
11338 		struct bnxt_napi *bnapi = bp->bnapi[i];
11339 
11340 		__netif_napi_del(&bnapi->napi);
11341 	}
11342 	/* We called __netif_napi_del(), we need
11343 	 * to respect an RCU grace period before freeing napi structures.
11344 	 */
11345 	synchronize_net();
11346 }
11347 
11348 static void bnxt_init_napi(struct bnxt *bp)
11349 {
11350 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11351 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11352 	struct bnxt_napi *bnapi;
11353 	int i;
11354 
11355 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11356 		poll_fn = bnxt_poll_p5;
11357 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11358 		cp_nr_rings--;
11359 	for (i = 0; i < cp_nr_rings; i++) {
11360 		bnapi = bp->bnapi[i];
11361 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
11362 				      bnapi->index);
11363 	}
11364 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11365 		bnapi = bp->bnapi[cp_nr_rings];
11366 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11367 	}
11368 }
11369 
11370 static void bnxt_disable_napi(struct bnxt *bp)
11371 {
11372 	int i;
11373 
11374 	if (!bp->bnapi ||
11375 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11376 		return;
11377 
11378 	for (i = 0; i < bp->cp_nr_rings; i++) {
11379 		struct bnxt_napi *bnapi = bp->bnapi[i];
11380 		struct bnxt_cp_ring_info *cpr;
11381 
11382 		cpr = &bnapi->cp_ring;
11383 		if (bnapi->tx_fault)
11384 			cpr->sw_stats->tx.tx_resets++;
11385 		if (bnapi->in_reset)
11386 			cpr->sw_stats->rx.rx_resets++;
11387 		napi_disable(&bnapi->napi);
11388 	}
11389 }
11390 
11391 static void bnxt_enable_napi(struct bnxt *bp)
11392 {
11393 	int i;
11394 
11395 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11396 	for (i = 0; i < bp->cp_nr_rings; i++) {
11397 		struct bnxt_napi *bnapi = bp->bnapi[i];
11398 		struct bnxt_cp_ring_info *cpr;
11399 
11400 		bnapi->tx_fault = 0;
11401 
11402 		cpr = &bnapi->cp_ring;
11403 		bnapi->in_reset = false;
11404 
11405 		if (bnapi->rx_ring) {
11406 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11407 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11408 		}
11409 		napi_enable(&bnapi->napi);
11410 	}
11411 }
11412 
11413 void bnxt_tx_disable(struct bnxt *bp)
11414 {
11415 	int i;
11416 	struct bnxt_tx_ring_info *txr;
11417 
11418 	if (bp->tx_ring) {
11419 		for (i = 0; i < bp->tx_nr_rings; i++) {
11420 			txr = &bp->tx_ring[i];
11421 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11422 		}
11423 	}
11424 	/* Make sure napi polls see @dev_state change */
11425 	synchronize_net();
11426 	/* Drop carrier first to prevent TX timeout */
11427 	netif_carrier_off(bp->dev);
11428 	/* Stop all TX queues */
11429 	netif_tx_disable(bp->dev);
11430 }
11431 
11432 void bnxt_tx_enable(struct bnxt *bp)
11433 {
11434 	int i;
11435 	struct bnxt_tx_ring_info *txr;
11436 
11437 	for (i = 0; i < bp->tx_nr_rings; i++) {
11438 		txr = &bp->tx_ring[i];
11439 		WRITE_ONCE(txr->dev_state, 0);
11440 	}
11441 	/* Make sure napi polls see @dev_state change */
11442 	synchronize_net();
11443 	netif_tx_wake_all_queues(bp->dev);
11444 	if (BNXT_LINK_IS_UP(bp))
11445 		netif_carrier_on(bp->dev);
11446 }
11447 
11448 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11449 {
11450 	u8 active_fec = link_info->active_fec_sig_mode &
11451 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11452 
11453 	switch (active_fec) {
11454 	default:
11455 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11456 		return "None";
11457 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11458 		return "Clause 74 BaseR";
11459 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11460 		return "Clause 91 RS(528,514)";
11461 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11462 		return "Clause 91 RS544_1XN";
11463 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11464 		return "Clause 91 RS(544,514)";
11465 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11466 		return "Clause 91 RS272_1XN";
11467 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11468 		return "Clause 91 RS(272,257)";
11469 	}
11470 }
11471 
11472 void bnxt_report_link(struct bnxt *bp)
11473 {
11474 	if (BNXT_LINK_IS_UP(bp)) {
11475 		const char *signal = "";
11476 		const char *flow_ctrl;
11477 		const char *duplex;
11478 		u32 speed;
11479 		u16 fec;
11480 
11481 		netif_carrier_on(bp->dev);
11482 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11483 		if (speed == SPEED_UNKNOWN) {
11484 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11485 			return;
11486 		}
11487 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11488 			duplex = "full";
11489 		else
11490 			duplex = "half";
11491 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11492 			flow_ctrl = "ON - receive & transmit";
11493 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11494 			flow_ctrl = "ON - transmit";
11495 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11496 			flow_ctrl = "ON - receive";
11497 		else
11498 			flow_ctrl = "none";
11499 		if (bp->link_info.phy_qcfg_resp.option_flags &
11500 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11501 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11502 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11503 			switch (sig_mode) {
11504 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11505 				signal = "(NRZ) ";
11506 				break;
11507 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11508 				signal = "(PAM4 56Gbps) ";
11509 				break;
11510 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11511 				signal = "(PAM4 112Gbps) ";
11512 				break;
11513 			default:
11514 				break;
11515 			}
11516 		}
11517 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11518 			    speed, signal, duplex, flow_ctrl);
11519 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11520 			netdev_info(bp->dev, "EEE is %s\n",
11521 				    bp->eee.eee_active ? "active" :
11522 							 "not active");
11523 		fec = bp->link_info.fec_cfg;
11524 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11525 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11526 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11527 				    bnxt_report_fec(&bp->link_info));
11528 	} else {
11529 		netif_carrier_off(bp->dev);
11530 		netdev_err(bp->dev, "NIC Link is Down\n");
11531 	}
11532 }
11533 
11534 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11535 {
11536 	if (!resp->supported_speeds_auto_mode &&
11537 	    !resp->supported_speeds_force_mode &&
11538 	    !resp->supported_pam4_speeds_auto_mode &&
11539 	    !resp->supported_pam4_speeds_force_mode &&
11540 	    !resp->supported_speeds2_auto_mode &&
11541 	    !resp->supported_speeds2_force_mode)
11542 		return true;
11543 	return false;
11544 }
11545 
11546 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11547 {
11548 	struct bnxt_link_info *link_info = &bp->link_info;
11549 	struct hwrm_port_phy_qcaps_output *resp;
11550 	struct hwrm_port_phy_qcaps_input *req;
11551 	int rc = 0;
11552 
11553 	if (bp->hwrm_spec_code < 0x10201)
11554 		return 0;
11555 
11556 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11557 	if (rc)
11558 		return rc;
11559 
11560 	resp = hwrm_req_hold(bp, req);
11561 	rc = hwrm_req_send(bp, req);
11562 	if (rc)
11563 		goto hwrm_phy_qcaps_exit;
11564 
11565 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11566 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11567 		struct ethtool_keee *eee = &bp->eee;
11568 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11569 
11570 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11571 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11572 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11573 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11574 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11575 	}
11576 
11577 	if (bp->hwrm_spec_code >= 0x10a01) {
11578 		if (bnxt_phy_qcaps_no_speed(resp)) {
11579 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11580 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11581 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11582 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11583 			netdev_info(bp->dev, "Ethernet link enabled\n");
11584 			/* Phy re-enabled, reprobe the speeds */
11585 			link_info->support_auto_speeds = 0;
11586 			link_info->support_pam4_auto_speeds = 0;
11587 			link_info->support_auto_speeds2 = 0;
11588 		}
11589 	}
11590 	if (resp->supported_speeds_auto_mode)
11591 		link_info->support_auto_speeds =
11592 			le16_to_cpu(resp->supported_speeds_auto_mode);
11593 	if (resp->supported_pam4_speeds_auto_mode)
11594 		link_info->support_pam4_auto_speeds =
11595 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11596 	if (resp->supported_speeds2_auto_mode)
11597 		link_info->support_auto_speeds2 =
11598 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11599 
11600 	bp->port_count = resp->port_cnt;
11601 
11602 hwrm_phy_qcaps_exit:
11603 	hwrm_req_drop(bp, req);
11604 	return rc;
11605 }
11606 
11607 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11608 {
11609 	struct hwrm_port_mac_qcaps_output *resp;
11610 	struct hwrm_port_mac_qcaps_input *req;
11611 	int rc;
11612 
11613 	if (bp->hwrm_spec_code < 0x10a03)
11614 		return;
11615 
11616 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11617 	if (rc)
11618 		return;
11619 
11620 	resp = hwrm_req_hold(bp, req);
11621 	rc = hwrm_req_send_silent(bp, req);
11622 	if (!rc)
11623 		bp->mac_flags = resp->flags;
11624 	hwrm_req_drop(bp, req);
11625 }
11626 
11627 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11628 {
11629 	u16 diff = advertising ^ supported;
11630 
11631 	return ((supported | diff) != supported);
11632 }
11633 
11634 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11635 {
11636 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11637 
11638 	/* Check if any advertised speeds are no longer supported. The caller
11639 	 * holds the link_lock mutex, so we can modify link_info settings.
11640 	 */
11641 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11642 		if (bnxt_support_dropped(link_info->advertising,
11643 					 link_info->support_auto_speeds2)) {
11644 			link_info->advertising = link_info->support_auto_speeds2;
11645 			return true;
11646 		}
11647 		return false;
11648 	}
11649 	if (bnxt_support_dropped(link_info->advertising,
11650 				 link_info->support_auto_speeds)) {
11651 		link_info->advertising = link_info->support_auto_speeds;
11652 		return true;
11653 	}
11654 	if (bnxt_support_dropped(link_info->advertising_pam4,
11655 				 link_info->support_pam4_auto_speeds)) {
11656 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11657 		return true;
11658 	}
11659 	return false;
11660 }
11661 
11662 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11663 {
11664 	struct bnxt_link_info *link_info = &bp->link_info;
11665 	struct hwrm_port_phy_qcfg_output *resp;
11666 	struct hwrm_port_phy_qcfg_input *req;
11667 	u8 link_state = link_info->link_state;
11668 	bool support_changed;
11669 	int rc;
11670 
11671 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11672 	if (rc)
11673 		return rc;
11674 
11675 	resp = hwrm_req_hold(bp, req);
11676 	rc = hwrm_req_send(bp, req);
11677 	if (rc) {
11678 		hwrm_req_drop(bp, req);
11679 		if (BNXT_VF(bp) && rc == -ENODEV) {
11680 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11681 			rc = 0;
11682 		}
11683 		return rc;
11684 	}
11685 
11686 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11687 	link_info->phy_link_status = resp->link;
11688 	link_info->duplex = resp->duplex_cfg;
11689 	if (bp->hwrm_spec_code >= 0x10800)
11690 		link_info->duplex = resp->duplex_state;
11691 	link_info->pause = resp->pause;
11692 	link_info->auto_mode = resp->auto_mode;
11693 	link_info->auto_pause_setting = resp->auto_pause;
11694 	link_info->lp_pause = resp->link_partner_adv_pause;
11695 	link_info->force_pause_setting = resp->force_pause;
11696 	link_info->duplex_setting = resp->duplex_cfg;
11697 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11698 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11699 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11700 			link_info->active_lanes = resp->active_lanes;
11701 	} else {
11702 		link_info->link_speed = 0;
11703 		link_info->active_lanes = 0;
11704 	}
11705 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11706 	link_info->force_pam4_link_speed =
11707 		le16_to_cpu(resp->force_pam4_link_speed);
11708 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11709 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11710 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11711 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11712 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11713 	link_info->auto_pam4_link_speeds =
11714 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11715 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11716 	link_info->lp_auto_link_speeds =
11717 		le16_to_cpu(resp->link_partner_adv_speeds);
11718 	link_info->lp_auto_pam4_link_speeds =
11719 		resp->link_partner_pam4_adv_speeds;
11720 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11721 	link_info->phy_ver[0] = resp->phy_maj;
11722 	link_info->phy_ver[1] = resp->phy_min;
11723 	link_info->phy_ver[2] = resp->phy_bld;
11724 	link_info->media_type = resp->media_type;
11725 	link_info->phy_type = resp->phy_type;
11726 	link_info->transceiver = resp->xcvr_pkg_type;
11727 	link_info->phy_addr = resp->eee_config_phy_addr &
11728 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11729 	link_info->module_status = resp->module_status;
11730 
11731 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11732 		struct ethtool_keee *eee = &bp->eee;
11733 		u16 fw_speeds;
11734 
11735 		eee->eee_active = 0;
11736 		if (resp->eee_config_phy_addr &
11737 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11738 			eee->eee_active = 1;
11739 			fw_speeds = le16_to_cpu(
11740 				resp->link_partner_adv_eee_link_speed_mask);
11741 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11742 		}
11743 
11744 		/* Pull initial EEE config */
11745 		if (!chng_link_state) {
11746 			if (resp->eee_config_phy_addr &
11747 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11748 				eee->eee_enabled = 1;
11749 
11750 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11751 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11752 
11753 			if (resp->eee_config_phy_addr &
11754 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11755 				__le32 tmr;
11756 
11757 				eee->tx_lpi_enabled = 1;
11758 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11759 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11760 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11761 			}
11762 		}
11763 	}
11764 
11765 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11766 	if (bp->hwrm_spec_code >= 0x10504) {
11767 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11768 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11769 	}
11770 	/* TODO: need to add more logic to report VF link */
11771 	if (chng_link_state) {
11772 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11773 			link_info->link_state = BNXT_LINK_STATE_UP;
11774 		else
11775 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11776 		if (link_state != link_info->link_state)
11777 			bnxt_report_link(bp);
11778 	} else {
11779 		/* always link down if not require to update link state */
11780 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11781 	}
11782 	hwrm_req_drop(bp, req);
11783 
11784 	if (!BNXT_PHY_CFG_ABLE(bp))
11785 		return 0;
11786 
11787 	support_changed = bnxt_support_speed_dropped(link_info);
11788 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11789 		bnxt_hwrm_set_link_setting(bp, true, false);
11790 	return 0;
11791 }
11792 
11793 static void bnxt_get_port_module_status(struct bnxt *bp)
11794 {
11795 	struct bnxt_link_info *link_info = &bp->link_info;
11796 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11797 	u8 module_status;
11798 
11799 	if (bnxt_update_link(bp, true))
11800 		return;
11801 
11802 	module_status = link_info->module_status;
11803 	switch (module_status) {
11804 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11805 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11806 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11807 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11808 			    bp->pf.port_id);
11809 		if (bp->hwrm_spec_code >= 0x10201) {
11810 			netdev_warn(bp->dev, "Module part number %s\n",
11811 				    resp->phy_vendor_partnumber);
11812 		}
11813 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11814 			netdev_warn(bp->dev, "TX is disabled\n");
11815 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11816 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11817 	}
11818 }
11819 
11820 static void
11821 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11822 {
11823 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11824 		if (bp->hwrm_spec_code >= 0x10201)
11825 			req->auto_pause =
11826 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11827 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11828 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11829 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11830 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11831 		req->enables |=
11832 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11833 	} else {
11834 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11835 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11836 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11837 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11838 		req->enables |=
11839 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11840 		if (bp->hwrm_spec_code >= 0x10201) {
11841 			req->auto_pause = req->force_pause;
11842 			req->enables |= cpu_to_le32(
11843 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11844 		}
11845 	}
11846 }
11847 
11848 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11849 {
11850 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11851 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11852 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11853 			req->enables |=
11854 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11855 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11856 		} else if (bp->link_info.advertising) {
11857 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11858 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11859 		}
11860 		if (bp->link_info.advertising_pam4) {
11861 			req->enables |=
11862 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11863 			req->auto_link_pam4_speed_mask =
11864 				cpu_to_le16(bp->link_info.advertising_pam4);
11865 		}
11866 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11867 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11868 	} else {
11869 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11870 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11871 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11872 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11873 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11874 				   (u32)bp->link_info.req_link_speed);
11875 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11876 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11877 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11878 		} else {
11879 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11880 		}
11881 	}
11882 
11883 	/* tell chimp that the setting takes effect immediately */
11884 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11885 }
11886 
11887 int bnxt_hwrm_set_pause(struct bnxt *bp)
11888 {
11889 	struct hwrm_port_phy_cfg_input *req;
11890 	int rc;
11891 
11892 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11893 	if (rc)
11894 		return rc;
11895 
11896 	bnxt_hwrm_set_pause_common(bp, req);
11897 
11898 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11899 	    bp->link_info.force_link_chng)
11900 		bnxt_hwrm_set_link_common(bp, req);
11901 
11902 	rc = hwrm_req_send(bp, req);
11903 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11904 		/* since changing of pause setting doesn't trigger any link
11905 		 * change event, the driver needs to update the current pause
11906 		 * result upon successfully return of the phy_cfg command
11907 		 */
11908 		bp->link_info.pause =
11909 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11910 		bp->link_info.auto_pause_setting = 0;
11911 		if (!bp->link_info.force_link_chng)
11912 			bnxt_report_link(bp);
11913 	}
11914 	bp->link_info.force_link_chng = false;
11915 	return rc;
11916 }
11917 
11918 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11919 			      struct hwrm_port_phy_cfg_input *req)
11920 {
11921 	struct ethtool_keee *eee = &bp->eee;
11922 
11923 	if (eee->eee_enabled) {
11924 		u16 eee_speeds;
11925 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11926 
11927 		if (eee->tx_lpi_enabled)
11928 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11929 		else
11930 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11931 
11932 		req->flags |= cpu_to_le32(flags);
11933 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11934 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11935 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11936 	} else {
11937 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11938 	}
11939 }
11940 
11941 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11942 {
11943 	struct hwrm_port_phy_cfg_input *req;
11944 	int rc;
11945 
11946 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11947 	if (rc)
11948 		return rc;
11949 
11950 	if (set_pause)
11951 		bnxt_hwrm_set_pause_common(bp, req);
11952 
11953 	bnxt_hwrm_set_link_common(bp, req);
11954 
11955 	if (set_eee)
11956 		bnxt_hwrm_set_eee(bp, req);
11957 	return hwrm_req_send(bp, req);
11958 }
11959 
11960 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11961 {
11962 	struct hwrm_port_phy_cfg_input *req;
11963 	int rc;
11964 
11965 	if (!BNXT_SINGLE_PF(bp))
11966 		return 0;
11967 
11968 	if (pci_num_vf(bp->pdev) &&
11969 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11970 		return 0;
11971 
11972 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11973 	if (rc)
11974 		return rc;
11975 
11976 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11977 	rc = hwrm_req_send(bp, req);
11978 	if (!rc) {
11979 		mutex_lock(&bp->link_lock);
11980 		/* Device is not obliged link down in certain scenarios, even
11981 		 * when forced. Setting the state unknown is consistent with
11982 		 * driver startup and will force link state to be reported
11983 		 * during subsequent open based on PORT_PHY_QCFG.
11984 		 */
11985 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11986 		mutex_unlock(&bp->link_lock);
11987 	}
11988 	return rc;
11989 }
11990 
11991 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11992 {
11993 #ifdef CONFIG_TEE_BNXT_FW
11994 	int rc = tee_bnxt_fw_load();
11995 
11996 	if (rc)
11997 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11998 
11999 	return rc;
12000 #else
12001 	netdev_err(bp->dev, "OP-TEE not supported\n");
12002 	return -ENODEV;
12003 #endif
12004 }
12005 
12006 static int bnxt_try_recover_fw(struct bnxt *bp)
12007 {
12008 	if (bp->fw_health && bp->fw_health->status_reliable) {
12009 		int retry = 0, rc;
12010 		u32 sts;
12011 
12012 		do {
12013 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
12014 			rc = bnxt_hwrm_poll(bp);
12015 			if (!BNXT_FW_IS_BOOTING(sts) &&
12016 			    !BNXT_FW_IS_RECOVERING(sts))
12017 				break;
12018 			retry++;
12019 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
12020 
12021 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12022 			netdev_err(bp->dev,
12023 				   "Firmware not responding, status: 0x%x\n",
12024 				   sts);
12025 			rc = -ENODEV;
12026 		}
12027 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12028 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12029 			return bnxt_fw_reset_via_optee(bp);
12030 		}
12031 		return rc;
12032 	}
12033 
12034 	return -ENODEV;
12035 }
12036 
12037 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12038 {
12039 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12040 
12041 	if (!BNXT_NEW_RM(bp))
12042 		return; /* no resource reservations required */
12043 
12044 	hw_resc->resv_cp_rings = 0;
12045 	hw_resc->resv_stat_ctxs = 0;
12046 	hw_resc->resv_irqs = 0;
12047 	hw_resc->resv_tx_rings = 0;
12048 	hw_resc->resv_rx_rings = 0;
12049 	hw_resc->resv_hw_ring_grps = 0;
12050 	hw_resc->resv_vnics = 0;
12051 	hw_resc->resv_rsscos_ctxs = 0;
12052 	if (!fw_reset) {
12053 		bp->tx_nr_rings = 0;
12054 		bp->rx_nr_rings = 0;
12055 	}
12056 }
12057 
12058 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12059 {
12060 	int rc;
12061 
12062 	if (!BNXT_NEW_RM(bp))
12063 		return 0; /* no resource reservations required */
12064 
12065 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12066 	if (rc)
12067 		netdev_err(bp->dev, "resc_qcaps failed\n");
12068 
12069 	bnxt_clear_reservations(bp, fw_reset);
12070 
12071 	return rc;
12072 }
12073 
12074 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12075 {
12076 	struct hwrm_func_drv_if_change_output *resp;
12077 	struct hwrm_func_drv_if_change_input *req;
12078 	bool fw_reset = !bp->irq_tbl;
12079 	bool resc_reinit = false;
12080 	int rc, retry = 0;
12081 	u32 flags = 0;
12082 
12083 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12084 		return 0;
12085 
12086 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12087 	if (rc)
12088 		return rc;
12089 
12090 	if (up)
12091 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12092 	resp = hwrm_req_hold(bp, req);
12093 
12094 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12095 	while (retry < BNXT_FW_IF_RETRY) {
12096 		rc = hwrm_req_send(bp, req);
12097 		if (rc != -EAGAIN)
12098 			break;
12099 
12100 		msleep(50);
12101 		retry++;
12102 	}
12103 
12104 	if (rc == -EAGAIN) {
12105 		hwrm_req_drop(bp, req);
12106 		return rc;
12107 	} else if (!rc) {
12108 		flags = le32_to_cpu(resp->flags);
12109 	} else if (up) {
12110 		rc = bnxt_try_recover_fw(bp);
12111 		fw_reset = true;
12112 	}
12113 	hwrm_req_drop(bp, req);
12114 	if (rc)
12115 		return rc;
12116 
12117 	if (!up) {
12118 		bnxt_inv_fw_health_reg(bp);
12119 		return 0;
12120 	}
12121 
12122 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12123 		resc_reinit = true;
12124 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12125 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12126 		fw_reset = true;
12127 	else
12128 		bnxt_remap_fw_health_regs(bp);
12129 
12130 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12131 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12132 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12133 		return -ENODEV;
12134 	}
12135 	if (resc_reinit || fw_reset) {
12136 		if (fw_reset) {
12137 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12138 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12139 				bnxt_ulp_irq_stop(bp);
12140 			bnxt_free_ctx_mem(bp, false);
12141 			bnxt_dcb_free(bp);
12142 			rc = bnxt_fw_init_one(bp);
12143 			if (rc) {
12144 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12145 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12146 				return rc;
12147 			}
12148 			bnxt_clear_int_mode(bp);
12149 			rc = bnxt_init_int_mode(bp);
12150 			if (rc) {
12151 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12152 				netdev_err(bp->dev, "init int mode failed\n");
12153 				return rc;
12154 			}
12155 		}
12156 		rc = bnxt_cancel_reservations(bp, fw_reset);
12157 	}
12158 	return rc;
12159 }
12160 
12161 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12162 {
12163 	struct hwrm_port_led_qcaps_output *resp;
12164 	struct hwrm_port_led_qcaps_input *req;
12165 	struct bnxt_pf_info *pf = &bp->pf;
12166 	int rc;
12167 
12168 	bp->num_leds = 0;
12169 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12170 		return 0;
12171 
12172 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12173 	if (rc)
12174 		return rc;
12175 
12176 	req->port_id = cpu_to_le16(pf->port_id);
12177 	resp = hwrm_req_hold(bp, req);
12178 	rc = hwrm_req_send(bp, req);
12179 	if (rc) {
12180 		hwrm_req_drop(bp, req);
12181 		return rc;
12182 	}
12183 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12184 		int i;
12185 
12186 		bp->num_leds = resp->num_leds;
12187 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12188 						 bp->num_leds);
12189 		for (i = 0; i < bp->num_leds; i++) {
12190 			struct bnxt_led_info *led = &bp->leds[i];
12191 			__le16 caps = led->led_state_caps;
12192 
12193 			if (!led->led_group_id ||
12194 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12195 				bp->num_leds = 0;
12196 				break;
12197 			}
12198 		}
12199 	}
12200 	hwrm_req_drop(bp, req);
12201 	return 0;
12202 }
12203 
12204 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12205 {
12206 	struct hwrm_wol_filter_alloc_output *resp;
12207 	struct hwrm_wol_filter_alloc_input *req;
12208 	int rc;
12209 
12210 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12211 	if (rc)
12212 		return rc;
12213 
12214 	req->port_id = cpu_to_le16(bp->pf.port_id);
12215 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12216 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12217 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12218 
12219 	resp = hwrm_req_hold(bp, req);
12220 	rc = hwrm_req_send(bp, req);
12221 	if (!rc)
12222 		bp->wol_filter_id = resp->wol_filter_id;
12223 	hwrm_req_drop(bp, req);
12224 	return rc;
12225 }
12226 
12227 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12228 {
12229 	struct hwrm_wol_filter_free_input *req;
12230 	int rc;
12231 
12232 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12233 	if (rc)
12234 		return rc;
12235 
12236 	req->port_id = cpu_to_le16(bp->pf.port_id);
12237 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12238 	req->wol_filter_id = bp->wol_filter_id;
12239 
12240 	return hwrm_req_send(bp, req);
12241 }
12242 
12243 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12244 {
12245 	struct hwrm_wol_filter_qcfg_output *resp;
12246 	struct hwrm_wol_filter_qcfg_input *req;
12247 	u16 next_handle = 0;
12248 	int rc;
12249 
12250 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12251 	if (rc)
12252 		return rc;
12253 
12254 	req->port_id = cpu_to_le16(bp->pf.port_id);
12255 	req->handle = cpu_to_le16(handle);
12256 	resp = hwrm_req_hold(bp, req);
12257 	rc = hwrm_req_send(bp, req);
12258 	if (!rc) {
12259 		next_handle = le16_to_cpu(resp->next_handle);
12260 		if (next_handle != 0) {
12261 			if (resp->wol_type ==
12262 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12263 				bp->wol = 1;
12264 				bp->wol_filter_id = resp->wol_filter_id;
12265 			}
12266 		}
12267 	}
12268 	hwrm_req_drop(bp, req);
12269 	return next_handle;
12270 }
12271 
12272 static void bnxt_get_wol_settings(struct bnxt *bp)
12273 {
12274 	u16 handle = 0;
12275 
12276 	bp->wol = 0;
12277 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12278 		return;
12279 
12280 	do {
12281 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12282 	} while (handle && handle != 0xffff);
12283 }
12284 
12285 static bool bnxt_eee_config_ok(struct bnxt *bp)
12286 {
12287 	struct ethtool_keee *eee = &bp->eee;
12288 	struct bnxt_link_info *link_info = &bp->link_info;
12289 
12290 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12291 		return true;
12292 
12293 	if (eee->eee_enabled) {
12294 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12295 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12296 
12297 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12298 
12299 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12300 			eee->eee_enabled = 0;
12301 			return false;
12302 		}
12303 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12304 			linkmode_and(eee->advertised, advertising,
12305 				     eee->supported);
12306 			return false;
12307 		}
12308 	}
12309 	return true;
12310 }
12311 
12312 static int bnxt_update_phy_setting(struct bnxt *bp)
12313 {
12314 	int rc;
12315 	bool update_link = false;
12316 	bool update_pause = false;
12317 	bool update_eee = false;
12318 	struct bnxt_link_info *link_info = &bp->link_info;
12319 
12320 	rc = bnxt_update_link(bp, true);
12321 	if (rc) {
12322 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12323 			   rc);
12324 		return rc;
12325 	}
12326 	if (!BNXT_SINGLE_PF(bp))
12327 		return 0;
12328 
12329 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12330 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12331 	    link_info->req_flow_ctrl)
12332 		update_pause = true;
12333 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12334 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12335 		update_pause = true;
12336 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12337 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12338 			update_link = true;
12339 		if (bnxt_force_speed_updated(link_info))
12340 			update_link = true;
12341 		if (link_info->req_duplex != link_info->duplex_setting)
12342 			update_link = true;
12343 	} else {
12344 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12345 			update_link = true;
12346 		if (bnxt_auto_speed_updated(link_info))
12347 			update_link = true;
12348 	}
12349 
12350 	/* The last close may have shutdown the link, so need to call
12351 	 * PHY_CFG to bring it back up.
12352 	 */
12353 	if (!BNXT_LINK_IS_UP(bp))
12354 		update_link = true;
12355 
12356 	if (!bnxt_eee_config_ok(bp))
12357 		update_eee = true;
12358 
12359 	if (update_link)
12360 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12361 	else if (update_pause)
12362 		rc = bnxt_hwrm_set_pause(bp);
12363 	if (rc) {
12364 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12365 			   rc);
12366 		return rc;
12367 	}
12368 
12369 	return rc;
12370 }
12371 
12372 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12373 
12374 static int bnxt_reinit_after_abort(struct bnxt *bp)
12375 {
12376 	int rc;
12377 
12378 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12379 		return -EBUSY;
12380 
12381 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12382 		return -ENODEV;
12383 
12384 	rc = bnxt_fw_init_one(bp);
12385 	if (!rc) {
12386 		bnxt_clear_int_mode(bp);
12387 		rc = bnxt_init_int_mode(bp);
12388 		if (!rc) {
12389 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12390 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12391 		}
12392 	}
12393 	return rc;
12394 }
12395 
12396 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12397 {
12398 	struct bnxt_ntuple_filter *ntp_fltr;
12399 	struct bnxt_l2_filter *l2_fltr;
12400 
12401 	if (list_empty(&fltr->list))
12402 		return;
12403 
12404 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12405 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12406 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12407 		atomic_inc(&l2_fltr->refcnt);
12408 		ntp_fltr->l2_fltr = l2_fltr;
12409 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12410 			bnxt_del_ntp_filter(bp, ntp_fltr);
12411 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12412 				   fltr->sw_id);
12413 		}
12414 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12415 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12416 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12417 			bnxt_del_l2_filter(bp, l2_fltr);
12418 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12419 				   fltr->sw_id);
12420 		}
12421 	}
12422 }
12423 
12424 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12425 {
12426 	struct bnxt_filter_base *usr_fltr, *tmp;
12427 
12428 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12429 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12430 }
12431 
12432 static int bnxt_set_xps_mapping(struct bnxt *bp)
12433 {
12434 	int numa_node = dev_to_node(&bp->pdev->dev);
12435 	unsigned int q_idx, map_idx, cpu, i;
12436 	const struct cpumask *cpu_mask_ptr;
12437 	int nr_cpus = num_online_cpus();
12438 	cpumask_t *q_map;
12439 	int rc = 0;
12440 
12441 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12442 	if (!q_map)
12443 		return -ENOMEM;
12444 
12445 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12446 	 * Each TC has the same number of TX queues. The nth TX queue for each
12447 	 * TC will have the same CPU mask.
12448 	 */
12449 	for (i = 0; i < nr_cpus; i++) {
12450 		map_idx = i % bp->tx_nr_rings_per_tc;
12451 		cpu = cpumask_local_spread(i, numa_node);
12452 		cpu_mask_ptr = get_cpu_mask(cpu);
12453 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12454 	}
12455 
12456 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12457 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12458 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12459 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12460 		if (rc) {
12461 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12462 				    q_idx);
12463 			break;
12464 		}
12465 	}
12466 
12467 	kfree(q_map);
12468 
12469 	return rc;
12470 }
12471 
12472 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12473 {
12474 	int rc = 0;
12475 
12476 	netif_carrier_off(bp->dev);
12477 	if (irq_re_init) {
12478 		/* Reserve rings now if none were reserved at driver probe. */
12479 		rc = bnxt_init_dflt_ring_mode(bp);
12480 		if (rc) {
12481 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12482 			return rc;
12483 		}
12484 	}
12485 	rc = bnxt_reserve_rings(bp, irq_re_init);
12486 	if (rc)
12487 		return rc;
12488 
12489 	rc = bnxt_alloc_mem(bp, irq_re_init);
12490 	if (rc) {
12491 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12492 		goto open_err_free_mem;
12493 	}
12494 
12495 	if (irq_re_init) {
12496 		bnxt_init_napi(bp);
12497 		rc = bnxt_request_irq(bp);
12498 		if (rc) {
12499 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12500 			goto open_err_irq;
12501 		}
12502 	}
12503 
12504 	rc = bnxt_init_nic(bp, irq_re_init);
12505 	if (rc) {
12506 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12507 		goto open_err_irq;
12508 	}
12509 
12510 	bnxt_enable_napi(bp);
12511 	bnxt_debug_dev_init(bp);
12512 
12513 	if (link_re_init) {
12514 		mutex_lock(&bp->link_lock);
12515 		rc = bnxt_update_phy_setting(bp);
12516 		mutex_unlock(&bp->link_lock);
12517 		if (rc) {
12518 			netdev_warn(bp->dev, "failed to update phy settings\n");
12519 			if (BNXT_SINGLE_PF(bp)) {
12520 				bp->link_info.phy_retry = true;
12521 				bp->link_info.phy_retry_expires =
12522 					jiffies + 5 * HZ;
12523 			}
12524 		}
12525 	}
12526 
12527 	if (irq_re_init) {
12528 		udp_tunnel_nic_reset_ntf(bp->dev);
12529 		rc = bnxt_set_xps_mapping(bp);
12530 		if (rc)
12531 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12532 	}
12533 
12534 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12535 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12536 			static_branch_enable(&bnxt_xdp_locking_key);
12537 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12538 		static_branch_disable(&bnxt_xdp_locking_key);
12539 	}
12540 	set_bit(BNXT_STATE_OPEN, &bp->state);
12541 	bnxt_enable_int(bp);
12542 	/* Enable TX queues */
12543 	bnxt_tx_enable(bp);
12544 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12545 	/* Poll link status and check for SFP+ module status */
12546 	mutex_lock(&bp->link_lock);
12547 	bnxt_get_port_module_status(bp);
12548 	mutex_unlock(&bp->link_lock);
12549 
12550 	/* VF-reps may need to be re-opened after the PF is re-opened */
12551 	if (BNXT_PF(bp))
12552 		bnxt_vf_reps_open(bp);
12553 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12554 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12555 	bnxt_ptp_init_rtc(bp, true);
12556 	bnxt_ptp_cfg_tstamp_filters(bp);
12557 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12558 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12559 	bnxt_cfg_usr_fltrs(bp);
12560 	return 0;
12561 
12562 open_err_irq:
12563 	bnxt_del_napi(bp);
12564 
12565 open_err_free_mem:
12566 	bnxt_free_skbs(bp);
12567 	bnxt_free_irq(bp);
12568 	bnxt_free_mem(bp, true);
12569 	return rc;
12570 }
12571 
12572 /* rtnl_lock held */
12573 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12574 {
12575 	int rc = 0;
12576 
12577 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12578 		rc = -EIO;
12579 	if (!rc)
12580 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12581 	if (rc) {
12582 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12583 		dev_close(bp->dev);
12584 	}
12585 	return rc;
12586 }
12587 
12588 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12589  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12590  * self tests.
12591  */
12592 int bnxt_half_open_nic(struct bnxt *bp)
12593 {
12594 	int rc = 0;
12595 
12596 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12597 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12598 		rc = -ENODEV;
12599 		goto half_open_err;
12600 	}
12601 
12602 	rc = bnxt_alloc_mem(bp, true);
12603 	if (rc) {
12604 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12605 		goto half_open_err;
12606 	}
12607 	bnxt_init_napi(bp);
12608 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12609 	rc = bnxt_init_nic(bp, true);
12610 	if (rc) {
12611 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12612 		bnxt_del_napi(bp);
12613 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12614 		goto half_open_err;
12615 	}
12616 	return 0;
12617 
12618 half_open_err:
12619 	bnxt_free_skbs(bp);
12620 	bnxt_free_mem(bp, true);
12621 	dev_close(bp->dev);
12622 	return rc;
12623 }
12624 
12625 /* rtnl_lock held, this call can only be made after a previous successful
12626  * call to bnxt_half_open_nic().
12627  */
12628 void bnxt_half_close_nic(struct bnxt *bp)
12629 {
12630 	bnxt_hwrm_resource_free(bp, false, true);
12631 	bnxt_del_napi(bp);
12632 	bnxt_free_skbs(bp);
12633 	bnxt_free_mem(bp, true);
12634 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12635 }
12636 
12637 void bnxt_reenable_sriov(struct bnxt *bp)
12638 {
12639 	if (BNXT_PF(bp)) {
12640 		struct bnxt_pf_info *pf = &bp->pf;
12641 		int n = pf->active_vfs;
12642 
12643 		if (n)
12644 			bnxt_cfg_hw_sriov(bp, &n, true);
12645 	}
12646 }
12647 
12648 static int bnxt_open(struct net_device *dev)
12649 {
12650 	struct bnxt *bp = netdev_priv(dev);
12651 	int rc;
12652 
12653 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12654 		rc = bnxt_reinit_after_abort(bp);
12655 		if (rc) {
12656 			if (rc == -EBUSY)
12657 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12658 			else
12659 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12660 			return -ENODEV;
12661 		}
12662 	}
12663 
12664 	rc = bnxt_hwrm_if_change(bp, true);
12665 	if (rc)
12666 		return rc;
12667 
12668 	rc = __bnxt_open_nic(bp, true, true);
12669 	if (rc) {
12670 		bnxt_hwrm_if_change(bp, false);
12671 	} else {
12672 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12673 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12674 				bnxt_queue_sp_work(bp,
12675 						   BNXT_RESTART_ULP_SP_EVENT);
12676 		}
12677 	}
12678 
12679 	return rc;
12680 }
12681 
12682 static bool bnxt_drv_busy(struct bnxt *bp)
12683 {
12684 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12685 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12686 }
12687 
12688 static void bnxt_get_ring_stats(struct bnxt *bp,
12689 				struct rtnl_link_stats64 *stats);
12690 
12691 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12692 			     bool link_re_init)
12693 {
12694 	/* Close the VF-reps before closing PF */
12695 	if (BNXT_PF(bp))
12696 		bnxt_vf_reps_close(bp);
12697 
12698 	/* Change device state to avoid TX queue wake up's */
12699 	bnxt_tx_disable(bp);
12700 
12701 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12702 	smp_mb__after_atomic();
12703 	while (bnxt_drv_busy(bp))
12704 		msleep(20);
12705 
12706 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12707 		bnxt_clear_rss_ctxs(bp);
12708 	/* Flush rings and disable interrupts */
12709 	bnxt_shutdown_nic(bp, irq_re_init);
12710 
12711 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12712 
12713 	bnxt_debug_dev_exit(bp);
12714 	bnxt_disable_napi(bp);
12715 	del_timer_sync(&bp->timer);
12716 	bnxt_free_skbs(bp);
12717 
12718 	/* Save ring stats before shutdown */
12719 	if (bp->bnapi && irq_re_init) {
12720 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12721 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12722 	}
12723 	if (irq_re_init) {
12724 		bnxt_free_irq(bp);
12725 		bnxt_del_napi(bp);
12726 	}
12727 	bnxt_free_mem(bp, irq_re_init);
12728 }
12729 
12730 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12731 {
12732 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12733 		/* If we get here, it means firmware reset is in progress
12734 		 * while we are trying to close.  We can safely proceed with
12735 		 * the close because we are holding rtnl_lock().  Some firmware
12736 		 * messages may fail as we proceed to close.  We set the
12737 		 * ABORT_ERR flag here so that the FW reset thread will later
12738 		 * abort when it gets the rtnl_lock() and sees the flag.
12739 		 */
12740 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12741 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12742 	}
12743 
12744 #ifdef CONFIG_BNXT_SRIOV
12745 	if (bp->sriov_cfg) {
12746 		int rc;
12747 
12748 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12749 						      !bp->sriov_cfg,
12750 						      BNXT_SRIOV_CFG_WAIT_TMO);
12751 		if (!rc)
12752 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12753 		else if (rc < 0)
12754 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12755 	}
12756 #endif
12757 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12758 }
12759 
12760 static int bnxt_close(struct net_device *dev)
12761 {
12762 	struct bnxt *bp = netdev_priv(dev);
12763 
12764 	bnxt_close_nic(bp, true, true);
12765 	bnxt_hwrm_shutdown_link(bp);
12766 	bnxt_hwrm_if_change(bp, false);
12767 	return 0;
12768 }
12769 
12770 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12771 				   u16 *val)
12772 {
12773 	struct hwrm_port_phy_mdio_read_output *resp;
12774 	struct hwrm_port_phy_mdio_read_input *req;
12775 	int rc;
12776 
12777 	if (bp->hwrm_spec_code < 0x10a00)
12778 		return -EOPNOTSUPP;
12779 
12780 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12781 	if (rc)
12782 		return rc;
12783 
12784 	req->port_id = cpu_to_le16(bp->pf.port_id);
12785 	req->phy_addr = phy_addr;
12786 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12787 	if (mdio_phy_id_is_c45(phy_addr)) {
12788 		req->cl45_mdio = 1;
12789 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12790 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12791 		req->reg_addr = cpu_to_le16(reg);
12792 	}
12793 
12794 	resp = hwrm_req_hold(bp, req);
12795 	rc = hwrm_req_send(bp, req);
12796 	if (!rc)
12797 		*val = le16_to_cpu(resp->reg_data);
12798 	hwrm_req_drop(bp, req);
12799 	return rc;
12800 }
12801 
12802 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12803 				    u16 val)
12804 {
12805 	struct hwrm_port_phy_mdio_write_input *req;
12806 	int rc;
12807 
12808 	if (bp->hwrm_spec_code < 0x10a00)
12809 		return -EOPNOTSUPP;
12810 
12811 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12812 	if (rc)
12813 		return rc;
12814 
12815 	req->port_id = cpu_to_le16(bp->pf.port_id);
12816 	req->phy_addr = phy_addr;
12817 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12818 	if (mdio_phy_id_is_c45(phy_addr)) {
12819 		req->cl45_mdio = 1;
12820 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12821 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12822 		req->reg_addr = cpu_to_le16(reg);
12823 	}
12824 	req->reg_data = cpu_to_le16(val);
12825 
12826 	return hwrm_req_send(bp, req);
12827 }
12828 
12829 /* rtnl_lock held */
12830 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12831 {
12832 	struct mii_ioctl_data *mdio = if_mii(ifr);
12833 	struct bnxt *bp = netdev_priv(dev);
12834 	int rc;
12835 
12836 	switch (cmd) {
12837 	case SIOCGMIIPHY:
12838 		mdio->phy_id = bp->link_info.phy_addr;
12839 
12840 		fallthrough;
12841 	case SIOCGMIIREG: {
12842 		u16 mii_regval = 0;
12843 
12844 		if (!netif_running(dev))
12845 			return -EAGAIN;
12846 
12847 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12848 					     &mii_regval);
12849 		mdio->val_out = mii_regval;
12850 		return rc;
12851 	}
12852 
12853 	case SIOCSMIIREG:
12854 		if (!netif_running(dev))
12855 			return -EAGAIN;
12856 
12857 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12858 						mdio->val_in);
12859 
12860 	case SIOCSHWTSTAMP:
12861 		return bnxt_hwtstamp_set(dev, ifr);
12862 
12863 	case SIOCGHWTSTAMP:
12864 		return bnxt_hwtstamp_get(dev, ifr);
12865 
12866 	default:
12867 		/* do nothing */
12868 		break;
12869 	}
12870 	return -EOPNOTSUPP;
12871 }
12872 
12873 static void bnxt_get_ring_stats(struct bnxt *bp,
12874 				struct rtnl_link_stats64 *stats)
12875 {
12876 	int i;
12877 
12878 	for (i = 0; i < bp->cp_nr_rings; i++) {
12879 		struct bnxt_napi *bnapi = bp->bnapi[i];
12880 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12881 		u64 *sw = cpr->stats.sw_stats;
12882 
12883 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12884 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12885 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12886 
12887 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12888 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12889 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12890 
12891 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12892 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12893 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12894 
12895 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12896 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12897 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12898 
12899 		stats->rx_missed_errors +=
12900 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12901 
12902 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12903 
12904 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12905 
12906 		stats->rx_dropped +=
12907 			cpr->sw_stats->rx.rx_netpoll_discards +
12908 			cpr->sw_stats->rx.rx_oom_discards;
12909 	}
12910 }
12911 
12912 static void bnxt_add_prev_stats(struct bnxt *bp,
12913 				struct rtnl_link_stats64 *stats)
12914 {
12915 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12916 
12917 	stats->rx_packets += prev_stats->rx_packets;
12918 	stats->tx_packets += prev_stats->tx_packets;
12919 	stats->rx_bytes += prev_stats->rx_bytes;
12920 	stats->tx_bytes += prev_stats->tx_bytes;
12921 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12922 	stats->multicast += prev_stats->multicast;
12923 	stats->rx_dropped += prev_stats->rx_dropped;
12924 	stats->tx_dropped += prev_stats->tx_dropped;
12925 }
12926 
12927 static void
12928 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12929 {
12930 	struct bnxt *bp = netdev_priv(dev);
12931 
12932 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12933 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12934 	 * we check the BNXT_STATE_OPEN flag.
12935 	 */
12936 	smp_mb__after_atomic();
12937 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12938 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12939 		*stats = bp->net_stats_prev;
12940 		return;
12941 	}
12942 
12943 	bnxt_get_ring_stats(bp, stats);
12944 	bnxt_add_prev_stats(bp, stats);
12945 
12946 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12947 		u64 *rx = bp->port_stats.sw_stats;
12948 		u64 *tx = bp->port_stats.sw_stats +
12949 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12950 
12951 		stats->rx_crc_errors =
12952 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12953 		stats->rx_frame_errors =
12954 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12955 		stats->rx_length_errors =
12956 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12957 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12958 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12959 		stats->rx_errors =
12960 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12961 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12962 		stats->collisions =
12963 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12964 		stats->tx_fifo_errors =
12965 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12966 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12967 	}
12968 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12969 }
12970 
12971 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12972 					struct bnxt_total_ring_err_stats *stats,
12973 					struct bnxt_cp_ring_info *cpr)
12974 {
12975 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12976 	u64 *hw_stats = cpr->stats.sw_stats;
12977 
12978 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12979 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12980 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12981 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12982 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12983 	stats->rx_total_ring_discards +=
12984 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12985 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12986 	stats->tx_total_ring_discards +=
12987 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12988 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12989 }
12990 
12991 void bnxt_get_ring_err_stats(struct bnxt *bp,
12992 			     struct bnxt_total_ring_err_stats *stats)
12993 {
12994 	int i;
12995 
12996 	for (i = 0; i < bp->cp_nr_rings; i++)
12997 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12998 }
12999 
13000 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
13001 {
13002 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13003 	struct net_device *dev = bp->dev;
13004 	struct netdev_hw_addr *ha;
13005 	u8 *haddr;
13006 	int mc_count = 0;
13007 	bool update = false;
13008 	int off = 0;
13009 
13010 	netdev_for_each_mc_addr(ha, dev) {
13011 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
13012 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13013 			vnic->mc_list_count = 0;
13014 			return false;
13015 		}
13016 		haddr = ha->addr;
13017 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
13018 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
13019 			update = true;
13020 		}
13021 		off += ETH_ALEN;
13022 		mc_count++;
13023 	}
13024 	if (mc_count)
13025 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13026 
13027 	if (mc_count != vnic->mc_list_count) {
13028 		vnic->mc_list_count = mc_count;
13029 		update = true;
13030 	}
13031 	return update;
13032 }
13033 
13034 static bool bnxt_uc_list_updated(struct bnxt *bp)
13035 {
13036 	struct net_device *dev = bp->dev;
13037 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13038 	struct netdev_hw_addr *ha;
13039 	int off = 0;
13040 
13041 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13042 		return true;
13043 
13044 	netdev_for_each_uc_addr(ha, dev) {
13045 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13046 			return true;
13047 
13048 		off += ETH_ALEN;
13049 	}
13050 	return false;
13051 }
13052 
13053 static void bnxt_set_rx_mode(struct net_device *dev)
13054 {
13055 	struct bnxt *bp = netdev_priv(dev);
13056 	struct bnxt_vnic_info *vnic;
13057 	bool mc_update = false;
13058 	bool uc_update;
13059 	u32 mask;
13060 
13061 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13062 		return;
13063 
13064 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13065 	mask = vnic->rx_mask;
13066 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13067 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13068 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13069 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13070 
13071 	if (dev->flags & IFF_PROMISC)
13072 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13073 
13074 	uc_update = bnxt_uc_list_updated(bp);
13075 
13076 	if (dev->flags & IFF_BROADCAST)
13077 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13078 	if (dev->flags & IFF_ALLMULTI) {
13079 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13080 		vnic->mc_list_count = 0;
13081 	} else if (dev->flags & IFF_MULTICAST) {
13082 		mc_update = bnxt_mc_list_updated(bp, &mask);
13083 	}
13084 
13085 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13086 		vnic->rx_mask = mask;
13087 
13088 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13089 	}
13090 }
13091 
13092 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13093 {
13094 	struct net_device *dev = bp->dev;
13095 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13096 	struct netdev_hw_addr *ha;
13097 	int i, off = 0, rc;
13098 	bool uc_update;
13099 
13100 	netif_addr_lock_bh(dev);
13101 	uc_update = bnxt_uc_list_updated(bp);
13102 	netif_addr_unlock_bh(dev);
13103 
13104 	if (!uc_update)
13105 		goto skip_uc;
13106 
13107 	for (i = 1; i < vnic->uc_filter_count; i++) {
13108 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13109 
13110 		bnxt_hwrm_l2_filter_free(bp, fltr);
13111 		bnxt_del_l2_filter(bp, fltr);
13112 	}
13113 
13114 	vnic->uc_filter_count = 1;
13115 
13116 	netif_addr_lock_bh(dev);
13117 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13118 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13119 	} else {
13120 		netdev_for_each_uc_addr(ha, dev) {
13121 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13122 			off += ETH_ALEN;
13123 			vnic->uc_filter_count++;
13124 		}
13125 	}
13126 	netif_addr_unlock_bh(dev);
13127 
13128 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13129 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13130 		if (rc) {
13131 			if (BNXT_VF(bp) && rc == -ENODEV) {
13132 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13133 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13134 				else
13135 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13136 				rc = 0;
13137 			} else {
13138 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13139 			}
13140 			vnic->uc_filter_count = i;
13141 			return rc;
13142 		}
13143 	}
13144 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13145 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13146 
13147 skip_uc:
13148 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13149 	    !bnxt_promisc_ok(bp))
13150 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13151 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13152 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13153 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13154 			    rc);
13155 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13156 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13157 		vnic->mc_list_count = 0;
13158 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13159 	}
13160 	if (rc)
13161 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13162 			   rc);
13163 
13164 	return rc;
13165 }
13166 
13167 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13168 {
13169 #ifdef CONFIG_BNXT_SRIOV
13170 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13171 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13172 
13173 		/* No minimum rings were provisioned by the PF.  Don't
13174 		 * reserve rings by default when device is down.
13175 		 */
13176 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13177 			return true;
13178 
13179 		if (!netif_running(bp->dev))
13180 			return false;
13181 	}
13182 #endif
13183 	return true;
13184 }
13185 
13186 /* If the chip and firmware supports RFS */
13187 static bool bnxt_rfs_supported(struct bnxt *bp)
13188 {
13189 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13190 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13191 			return true;
13192 		return false;
13193 	}
13194 	/* 212 firmware is broken for aRFS */
13195 	if (BNXT_FW_MAJ(bp) == 212)
13196 		return false;
13197 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13198 		return true;
13199 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13200 		return true;
13201 	return false;
13202 }
13203 
13204 /* If runtime conditions support RFS */
13205 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13206 {
13207 	struct bnxt_hw_rings hwr = {0};
13208 	int max_vnics, max_rss_ctxs;
13209 
13210 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13211 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13212 		return bnxt_rfs_supported(bp);
13213 
13214 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13215 		return false;
13216 
13217 	hwr.grp = bp->rx_nr_rings;
13218 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13219 	if (new_rss_ctx)
13220 		hwr.vnic++;
13221 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13222 	max_vnics = bnxt_get_max_func_vnics(bp);
13223 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13224 
13225 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13226 		if (bp->rx_nr_rings > 1)
13227 			netdev_warn(bp->dev,
13228 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13229 				    min(max_rss_ctxs - 1, max_vnics - 1));
13230 		return false;
13231 	}
13232 
13233 	if (!BNXT_NEW_RM(bp))
13234 		return true;
13235 
13236 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13237 	 * issue that will mess up the default VNIC if we reduce the
13238 	 * reservations.
13239 	 */
13240 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13241 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13242 		return true;
13243 
13244 	bnxt_hwrm_reserve_rings(bp, &hwr);
13245 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13246 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13247 		return true;
13248 
13249 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13250 	hwr.vnic = 1;
13251 	hwr.rss_ctx = 0;
13252 	bnxt_hwrm_reserve_rings(bp, &hwr);
13253 	return false;
13254 }
13255 
13256 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13257 					   netdev_features_t features)
13258 {
13259 	struct bnxt *bp = netdev_priv(dev);
13260 	netdev_features_t vlan_features;
13261 
13262 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13263 		features &= ~NETIF_F_NTUPLE;
13264 
13265 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13266 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13267 
13268 	if (!(features & NETIF_F_GRO))
13269 		features &= ~NETIF_F_GRO_HW;
13270 
13271 	if (features & NETIF_F_GRO_HW)
13272 		features &= ~NETIF_F_LRO;
13273 
13274 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13275 	 * turned on or off together.
13276 	 */
13277 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13278 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13279 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13280 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13281 		else if (vlan_features)
13282 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13283 	}
13284 #ifdef CONFIG_BNXT_SRIOV
13285 	if (BNXT_VF(bp) && bp->vf.vlan)
13286 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13287 #endif
13288 	return features;
13289 }
13290 
13291 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13292 				bool link_re_init, u32 flags, bool update_tpa)
13293 {
13294 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13295 	bp->flags = flags;
13296 	if (update_tpa)
13297 		bnxt_set_ring_params(bp);
13298 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13299 }
13300 
13301 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13302 {
13303 	bool update_tpa = false, update_ntuple = false;
13304 	struct bnxt *bp = netdev_priv(dev);
13305 	u32 flags = bp->flags;
13306 	u32 changes;
13307 	int rc = 0;
13308 	bool re_init = false;
13309 
13310 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13311 	if (features & NETIF_F_GRO_HW)
13312 		flags |= BNXT_FLAG_GRO;
13313 	else if (features & NETIF_F_LRO)
13314 		flags |= BNXT_FLAG_LRO;
13315 
13316 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13317 		flags &= ~BNXT_FLAG_TPA;
13318 
13319 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13320 		flags |= BNXT_FLAG_STRIP_VLAN;
13321 
13322 	if (features & NETIF_F_NTUPLE)
13323 		flags |= BNXT_FLAG_RFS;
13324 	else
13325 		bnxt_clear_usr_fltrs(bp, true);
13326 
13327 	changes = flags ^ bp->flags;
13328 	if (changes & BNXT_FLAG_TPA) {
13329 		update_tpa = true;
13330 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13331 		    (flags & BNXT_FLAG_TPA) == 0 ||
13332 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13333 			re_init = true;
13334 	}
13335 
13336 	if (changes & ~BNXT_FLAG_TPA)
13337 		re_init = true;
13338 
13339 	if (changes & BNXT_FLAG_RFS)
13340 		update_ntuple = true;
13341 
13342 	if (flags != bp->flags) {
13343 		u32 old_flags = bp->flags;
13344 
13345 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13346 			bp->flags = flags;
13347 			if (update_tpa)
13348 				bnxt_set_ring_params(bp);
13349 			return rc;
13350 		}
13351 
13352 		if (update_ntuple)
13353 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13354 
13355 		if (re_init)
13356 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13357 
13358 		if (update_tpa) {
13359 			bp->flags = flags;
13360 			rc = bnxt_set_tpa(bp,
13361 					  (flags & BNXT_FLAG_TPA) ?
13362 					  true : false);
13363 			if (rc)
13364 				bp->flags = old_flags;
13365 		}
13366 	}
13367 	return rc;
13368 }
13369 
13370 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13371 			      u8 **nextp)
13372 {
13373 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13374 	struct hop_jumbo_hdr *jhdr;
13375 	int hdr_count = 0;
13376 	u8 *nexthdr;
13377 	int start;
13378 
13379 	/* Check that there are at most 2 IPv6 extension headers, no
13380 	 * fragment header, and each is <= 64 bytes.
13381 	 */
13382 	start = nw_off + sizeof(*ip6h);
13383 	nexthdr = &ip6h->nexthdr;
13384 	while (ipv6_ext_hdr(*nexthdr)) {
13385 		struct ipv6_opt_hdr *hp;
13386 		int hdrlen;
13387 
13388 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13389 		    *nexthdr == NEXTHDR_FRAGMENT)
13390 			return false;
13391 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13392 					  skb_headlen(skb), NULL);
13393 		if (!hp)
13394 			return false;
13395 		if (*nexthdr == NEXTHDR_AUTH)
13396 			hdrlen = ipv6_authlen(hp);
13397 		else
13398 			hdrlen = ipv6_optlen(hp);
13399 
13400 		if (hdrlen > 64)
13401 			return false;
13402 
13403 		/* The ext header may be a hop-by-hop header inserted for
13404 		 * big TCP purposes. This will be removed before sending
13405 		 * from NIC, so do not count it.
13406 		 */
13407 		if (*nexthdr == NEXTHDR_HOP) {
13408 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13409 				goto increment_hdr;
13410 
13411 			jhdr = (struct hop_jumbo_hdr *)hp;
13412 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13413 			    jhdr->nexthdr != IPPROTO_TCP)
13414 				goto increment_hdr;
13415 
13416 			goto next_hdr;
13417 		}
13418 increment_hdr:
13419 		hdr_count++;
13420 next_hdr:
13421 		nexthdr = &hp->nexthdr;
13422 		start += hdrlen;
13423 	}
13424 	if (nextp) {
13425 		/* Caller will check inner protocol */
13426 		if (skb->encapsulation) {
13427 			*nextp = nexthdr;
13428 			return true;
13429 		}
13430 		*nextp = NULL;
13431 	}
13432 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13433 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13434 }
13435 
13436 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13437 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13438 {
13439 	struct udphdr *uh = udp_hdr(skb);
13440 	__be16 udp_port = uh->dest;
13441 
13442 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13443 	    udp_port != bp->vxlan_gpe_port)
13444 		return false;
13445 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13446 		struct ethhdr *eh = inner_eth_hdr(skb);
13447 
13448 		switch (eh->h_proto) {
13449 		case htons(ETH_P_IP):
13450 			return true;
13451 		case htons(ETH_P_IPV6):
13452 			return bnxt_exthdr_check(bp, skb,
13453 						 skb_inner_network_offset(skb),
13454 						 NULL);
13455 		}
13456 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13457 		return true;
13458 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13459 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13460 					 NULL);
13461 	}
13462 	return false;
13463 }
13464 
13465 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13466 {
13467 	switch (l4_proto) {
13468 	case IPPROTO_UDP:
13469 		return bnxt_udp_tunl_check(bp, skb);
13470 	case IPPROTO_IPIP:
13471 		return true;
13472 	case IPPROTO_GRE: {
13473 		switch (skb->inner_protocol) {
13474 		default:
13475 			return false;
13476 		case htons(ETH_P_IP):
13477 			return true;
13478 		case htons(ETH_P_IPV6):
13479 			fallthrough;
13480 		}
13481 	}
13482 	case IPPROTO_IPV6:
13483 		/* Check ext headers of inner ipv6 */
13484 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13485 					 NULL);
13486 	}
13487 	return false;
13488 }
13489 
13490 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13491 					     struct net_device *dev,
13492 					     netdev_features_t features)
13493 {
13494 	struct bnxt *bp = netdev_priv(dev);
13495 	u8 *l4_proto;
13496 
13497 	features = vlan_features_check(skb, features);
13498 	switch (vlan_get_protocol(skb)) {
13499 	case htons(ETH_P_IP):
13500 		if (!skb->encapsulation)
13501 			return features;
13502 		l4_proto = &ip_hdr(skb)->protocol;
13503 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13504 			return features;
13505 		break;
13506 	case htons(ETH_P_IPV6):
13507 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13508 				       &l4_proto))
13509 			break;
13510 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13511 			return features;
13512 		break;
13513 	}
13514 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13515 }
13516 
13517 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13518 			 u32 *reg_buf)
13519 {
13520 	struct hwrm_dbg_read_direct_output *resp;
13521 	struct hwrm_dbg_read_direct_input *req;
13522 	__le32 *dbg_reg_buf;
13523 	dma_addr_t mapping;
13524 	int rc, i;
13525 
13526 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13527 	if (rc)
13528 		return rc;
13529 
13530 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13531 					 &mapping);
13532 	if (!dbg_reg_buf) {
13533 		rc = -ENOMEM;
13534 		goto dbg_rd_reg_exit;
13535 	}
13536 
13537 	req->host_dest_addr = cpu_to_le64(mapping);
13538 
13539 	resp = hwrm_req_hold(bp, req);
13540 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13541 	req->read_len32 = cpu_to_le32(num_words);
13542 
13543 	rc = hwrm_req_send(bp, req);
13544 	if (rc || resp->error_code) {
13545 		rc = -EIO;
13546 		goto dbg_rd_reg_exit;
13547 	}
13548 	for (i = 0; i < num_words; i++)
13549 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13550 
13551 dbg_rd_reg_exit:
13552 	hwrm_req_drop(bp, req);
13553 	return rc;
13554 }
13555 
13556 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13557 				       u32 ring_id, u32 *prod, u32 *cons)
13558 {
13559 	struct hwrm_dbg_ring_info_get_output *resp;
13560 	struct hwrm_dbg_ring_info_get_input *req;
13561 	int rc;
13562 
13563 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13564 	if (rc)
13565 		return rc;
13566 
13567 	req->ring_type = ring_type;
13568 	req->fw_ring_id = cpu_to_le32(ring_id);
13569 	resp = hwrm_req_hold(bp, req);
13570 	rc = hwrm_req_send(bp, req);
13571 	if (!rc) {
13572 		*prod = le32_to_cpu(resp->producer_index);
13573 		*cons = le32_to_cpu(resp->consumer_index);
13574 	}
13575 	hwrm_req_drop(bp, req);
13576 	return rc;
13577 }
13578 
13579 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13580 {
13581 	struct bnxt_tx_ring_info *txr;
13582 	int i = bnapi->index, j;
13583 
13584 	bnxt_for_each_napi_tx(j, bnapi, txr)
13585 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13586 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13587 			    txr->tx_cons);
13588 }
13589 
13590 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13591 {
13592 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13593 	int i = bnapi->index;
13594 
13595 	if (!rxr)
13596 		return;
13597 
13598 	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",
13599 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13600 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13601 		    rxr->rx_sw_agg_prod);
13602 }
13603 
13604 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13605 {
13606 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13607 	int i = bnapi->index;
13608 
13609 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13610 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13611 }
13612 
13613 static void bnxt_dbg_dump_states(struct bnxt *bp)
13614 {
13615 	int i;
13616 	struct bnxt_napi *bnapi;
13617 
13618 	for (i = 0; i < bp->cp_nr_rings; i++) {
13619 		bnapi = bp->bnapi[i];
13620 		if (netif_msg_drv(bp)) {
13621 			bnxt_dump_tx_sw_state(bnapi);
13622 			bnxt_dump_rx_sw_state(bnapi);
13623 			bnxt_dump_cp_sw_state(bnapi);
13624 		}
13625 	}
13626 }
13627 
13628 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13629 {
13630 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13631 	struct hwrm_ring_reset_input *req;
13632 	struct bnxt_napi *bnapi = rxr->bnapi;
13633 	struct bnxt_cp_ring_info *cpr;
13634 	u16 cp_ring_id;
13635 	int rc;
13636 
13637 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13638 	if (rc)
13639 		return rc;
13640 
13641 	cpr = &bnapi->cp_ring;
13642 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13643 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13644 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13645 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13646 	return hwrm_req_send_silent(bp, req);
13647 }
13648 
13649 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13650 {
13651 	if (!silent)
13652 		bnxt_dbg_dump_states(bp);
13653 	if (netif_running(bp->dev)) {
13654 		bnxt_close_nic(bp, !silent, false);
13655 		bnxt_open_nic(bp, !silent, false);
13656 	}
13657 }
13658 
13659 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13660 {
13661 	struct bnxt *bp = netdev_priv(dev);
13662 
13663 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13664 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13665 }
13666 
13667 static void bnxt_fw_health_check(struct bnxt *bp)
13668 {
13669 	struct bnxt_fw_health *fw_health = bp->fw_health;
13670 	struct pci_dev *pdev = bp->pdev;
13671 	u32 val;
13672 
13673 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13674 		return;
13675 
13676 	/* Make sure it is enabled before checking the tmr_counter. */
13677 	smp_rmb();
13678 	if (fw_health->tmr_counter) {
13679 		fw_health->tmr_counter--;
13680 		return;
13681 	}
13682 
13683 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13684 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13685 		fw_health->arrests++;
13686 		goto fw_reset;
13687 	}
13688 
13689 	fw_health->last_fw_heartbeat = val;
13690 
13691 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13692 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13693 		fw_health->discoveries++;
13694 		goto fw_reset;
13695 	}
13696 
13697 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13698 	return;
13699 
13700 fw_reset:
13701 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13702 }
13703 
13704 static void bnxt_timer(struct timer_list *t)
13705 {
13706 	struct bnxt *bp = from_timer(bp, t, timer);
13707 	struct net_device *dev = bp->dev;
13708 
13709 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13710 		return;
13711 
13712 	if (atomic_read(&bp->intr_sem) != 0)
13713 		goto bnxt_restart_timer;
13714 
13715 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13716 		bnxt_fw_health_check(bp);
13717 
13718 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13719 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13720 
13721 	if (bnxt_tc_flower_enabled(bp))
13722 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13723 
13724 #ifdef CONFIG_RFS_ACCEL
13725 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13726 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13727 #endif /*CONFIG_RFS_ACCEL*/
13728 
13729 	if (bp->link_info.phy_retry) {
13730 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13731 			bp->link_info.phy_retry = false;
13732 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13733 		} else {
13734 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13735 		}
13736 	}
13737 
13738 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13739 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13740 
13741 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13742 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13743 
13744 bnxt_restart_timer:
13745 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13746 }
13747 
13748 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13749 {
13750 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13751 	 * set.  If the device is being closed, bnxt_close() may be holding
13752 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13753 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13754 	 */
13755 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13756 	rtnl_lock();
13757 }
13758 
13759 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13760 {
13761 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13762 	rtnl_unlock();
13763 }
13764 
13765 /* Only called from bnxt_sp_task() */
13766 static void bnxt_reset(struct bnxt *bp, bool silent)
13767 {
13768 	bnxt_rtnl_lock_sp(bp);
13769 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13770 		bnxt_reset_task(bp, silent);
13771 	bnxt_rtnl_unlock_sp(bp);
13772 }
13773 
13774 /* Only called from bnxt_sp_task() */
13775 static void bnxt_rx_ring_reset(struct bnxt *bp)
13776 {
13777 	int i;
13778 
13779 	bnxt_rtnl_lock_sp(bp);
13780 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13781 		bnxt_rtnl_unlock_sp(bp);
13782 		return;
13783 	}
13784 	/* Disable and flush TPA before resetting the RX ring */
13785 	if (bp->flags & BNXT_FLAG_TPA)
13786 		bnxt_set_tpa(bp, false);
13787 	for (i = 0; i < bp->rx_nr_rings; i++) {
13788 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13789 		struct bnxt_cp_ring_info *cpr;
13790 		int rc;
13791 
13792 		if (!rxr->bnapi->in_reset)
13793 			continue;
13794 
13795 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13796 		if (rc) {
13797 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13798 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13799 			else
13800 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13801 					    rc);
13802 			bnxt_reset_task(bp, true);
13803 			break;
13804 		}
13805 		bnxt_free_one_rx_ring_skbs(bp, rxr);
13806 		rxr->rx_prod = 0;
13807 		rxr->rx_agg_prod = 0;
13808 		rxr->rx_sw_agg_prod = 0;
13809 		rxr->rx_next_cons = 0;
13810 		rxr->bnapi->in_reset = false;
13811 		bnxt_alloc_one_rx_ring(bp, i);
13812 		cpr = &rxr->bnapi->cp_ring;
13813 		cpr->sw_stats->rx.rx_resets++;
13814 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13815 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13816 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13817 	}
13818 	if (bp->flags & BNXT_FLAG_TPA)
13819 		bnxt_set_tpa(bp, true);
13820 	bnxt_rtnl_unlock_sp(bp);
13821 }
13822 
13823 static void bnxt_fw_fatal_close(struct bnxt *bp)
13824 {
13825 	bnxt_tx_disable(bp);
13826 	bnxt_disable_napi(bp);
13827 	bnxt_disable_int_sync(bp);
13828 	bnxt_free_irq(bp);
13829 	bnxt_clear_int_mode(bp);
13830 	pci_disable_device(bp->pdev);
13831 }
13832 
13833 static void bnxt_fw_reset_close(struct bnxt *bp)
13834 {
13835 	/* When firmware is in fatal state, quiesce device and disable
13836 	 * bus master to prevent any potential bad DMAs before freeing
13837 	 * kernel memory.
13838 	 */
13839 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13840 		u16 val = 0;
13841 
13842 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13843 		if (val == 0xffff)
13844 			bp->fw_reset_min_dsecs = 0;
13845 		bnxt_fw_fatal_close(bp);
13846 	}
13847 	__bnxt_close_nic(bp, true, false);
13848 	bnxt_vf_reps_free(bp);
13849 	bnxt_clear_int_mode(bp);
13850 	bnxt_hwrm_func_drv_unrgtr(bp);
13851 	if (pci_is_enabled(bp->pdev))
13852 		pci_disable_device(bp->pdev);
13853 	bnxt_free_ctx_mem(bp, false);
13854 }
13855 
13856 static bool is_bnxt_fw_ok(struct bnxt *bp)
13857 {
13858 	struct bnxt_fw_health *fw_health = bp->fw_health;
13859 	bool no_heartbeat = false, has_reset = false;
13860 	u32 val;
13861 
13862 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13863 	if (val == fw_health->last_fw_heartbeat)
13864 		no_heartbeat = true;
13865 
13866 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13867 	if (val != fw_health->last_fw_reset_cnt)
13868 		has_reset = true;
13869 
13870 	if (!no_heartbeat && has_reset)
13871 		return true;
13872 
13873 	return false;
13874 }
13875 
13876 /* rtnl_lock is acquired before calling this function */
13877 static void bnxt_force_fw_reset(struct bnxt *bp)
13878 {
13879 	struct bnxt_fw_health *fw_health = bp->fw_health;
13880 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13881 	u32 wait_dsecs;
13882 
13883 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13884 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13885 		return;
13886 
13887 	/* we have to serialize with bnxt_refclk_read()*/
13888 	if (ptp) {
13889 		unsigned long flags;
13890 
13891 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13892 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13893 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13894 	} else {
13895 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13896 	}
13897 	bnxt_fw_reset_close(bp);
13898 	wait_dsecs = fw_health->master_func_wait_dsecs;
13899 	if (fw_health->primary) {
13900 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13901 			wait_dsecs = 0;
13902 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13903 	} else {
13904 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13905 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13906 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13907 	}
13908 
13909 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13910 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13911 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13912 }
13913 
13914 void bnxt_fw_exception(struct bnxt *bp)
13915 {
13916 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13917 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13918 	bnxt_ulp_stop(bp);
13919 	bnxt_rtnl_lock_sp(bp);
13920 	bnxt_force_fw_reset(bp);
13921 	bnxt_rtnl_unlock_sp(bp);
13922 }
13923 
13924 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13925  * < 0 on error.
13926  */
13927 static int bnxt_get_registered_vfs(struct bnxt *bp)
13928 {
13929 #ifdef CONFIG_BNXT_SRIOV
13930 	int rc;
13931 
13932 	if (!BNXT_PF(bp))
13933 		return 0;
13934 
13935 	rc = bnxt_hwrm_func_qcfg(bp);
13936 	if (rc) {
13937 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13938 		return rc;
13939 	}
13940 	if (bp->pf.registered_vfs)
13941 		return bp->pf.registered_vfs;
13942 	if (bp->sriov_cfg)
13943 		return 1;
13944 #endif
13945 	return 0;
13946 }
13947 
13948 void bnxt_fw_reset(struct bnxt *bp)
13949 {
13950 	bnxt_ulp_stop(bp);
13951 	bnxt_rtnl_lock_sp(bp);
13952 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13953 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13954 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13955 		int n = 0, tmo;
13956 
13957 		/* we have to serialize with bnxt_refclk_read()*/
13958 		if (ptp) {
13959 			unsigned long flags;
13960 
13961 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13962 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13963 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13964 		} else {
13965 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13966 		}
13967 		if (bp->pf.active_vfs &&
13968 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13969 			n = bnxt_get_registered_vfs(bp);
13970 		if (n < 0) {
13971 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13972 				   n);
13973 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13974 			dev_close(bp->dev);
13975 			goto fw_reset_exit;
13976 		} else if (n > 0) {
13977 			u16 vf_tmo_dsecs = n * 10;
13978 
13979 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13980 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13981 			bp->fw_reset_state =
13982 				BNXT_FW_RESET_STATE_POLL_VF;
13983 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13984 			goto fw_reset_exit;
13985 		}
13986 		bnxt_fw_reset_close(bp);
13987 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13988 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13989 			tmo = HZ / 10;
13990 		} else {
13991 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13992 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13993 		}
13994 		bnxt_queue_fw_reset_work(bp, tmo);
13995 	}
13996 fw_reset_exit:
13997 	bnxt_rtnl_unlock_sp(bp);
13998 }
13999 
14000 static void bnxt_chk_missed_irq(struct bnxt *bp)
14001 {
14002 	int i;
14003 
14004 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
14005 		return;
14006 
14007 	for (i = 0; i < bp->cp_nr_rings; i++) {
14008 		struct bnxt_napi *bnapi = bp->bnapi[i];
14009 		struct bnxt_cp_ring_info *cpr;
14010 		u32 fw_ring_id;
14011 		int j;
14012 
14013 		if (!bnapi)
14014 			continue;
14015 
14016 		cpr = &bnapi->cp_ring;
14017 		for (j = 0; j < cpr->cp_ring_count; j++) {
14018 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
14019 			u32 val[2];
14020 
14021 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14022 				continue;
14023 
14024 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14025 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14026 				continue;
14027 			}
14028 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14029 			bnxt_dbg_hwrm_ring_info_get(bp,
14030 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14031 				fw_ring_id, &val[0], &val[1]);
14032 			cpr->sw_stats->cmn.missed_irqs++;
14033 		}
14034 	}
14035 }
14036 
14037 static void bnxt_cfg_ntp_filters(struct bnxt *);
14038 
14039 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14040 {
14041 	struct bnxt_link_info *link_info = &bp->link_info;
14042 
14043 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14044 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14045 		if (bp->hwrm_spec_code >= 0x10201) {
14046 			if (link_info->auto_pause_setting &
14047 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14048 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14049 		} else {
14050 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14051 		}
14052 		bnxt_set_auto_speed(link_info);
14053 	} else {
14054 		bnxt_set_force_speed(link_info);
14055 		link_info->req_duplex = link_info->duplex_setting;
14056 	}
14057 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14058 		link_info->req_flow_ctrl =
14059 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14060 	else
14061 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14062 }
14063 
14064 static void bnxt_fw_echo_reply(struct bnxt *bp)
14065 {
14066 	struct bnxt_fw_health *fw_health = bp->fw_health;
14067 	struct hwrm_func_echo_response_input *req;
14068 	int rc;
14069 
14070 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14071 	if (rc)
14072 		return;
14073 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14074 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14075 	hwrm_req_send(bp, req);
14076 }
14077 
14078 static void bnxt_ulp_restart(struct bnxt *bp)
14079 {
14080 	bnxt_ulp_stop(bp);
14081 	bnxt_ulp_start(bp, 0);
14082 }
14083 
14084 static void bnxt_sp_task(struct work_struct *work)
14085 {
14086 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14087 
14088 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14089 	smp_mb__after_atomic();
14090 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14091 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14092 		return;
14093 	}
14094 
14095 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14096 		bnxt_ulp_restart(bp);
14097 		bnxt_reenable_sriov(bp);
14098 	}
14099 
14100 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14101 		bnxt_cfg_rx_mode(bp);
14102 
14103 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14104 		bnxt_cfg_ntp_filters(bp);
14105 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14106 		bnxt_hwrm_exec_fwd_req(bp);
14107 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14108 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14109 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14110 		bnxt_hwrm_port_qstats(bp, 0);
14111 		bnxt_hwrm_port_qstats_ext(bp, 0);
14112 		bnxt_accumulate_all_stats(bp);
14113 	}
14114 
14115 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14116 		int rc;
14117 
14118 		mutex_lock(&bp->link_lock);
14119 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14120 				       &bp->sp_event))
14121 			bnxt_hwrm_phy_qcaps(bp);
14122 
14123 		rc = bnxt_update_link(bp, true);
14124 		if (rc)
14125 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14126 				   rc);
14127 
14128 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14129 				       &bp->sp_event))
14130 			bnxt_init_ethtool_link_settings(bp);
14131 		mutex_unlock(&bp->link_lock);
14132 	}
14133 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14134 		int rc;
14135 
14136 		mutex_lock(&bp->link_lock);
14137 		rc = bnxt_update_phy_setting(bp);
14138 		mutex_unlock(&bp->link_lock);
14139 		if (rc) {
14140 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14141 		} else {
14142 			bp->link_info.phy_retry = false;
14143 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14144 		}
14145 	}
14146 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14147 		mutex_lock(&bp->link_lock);
14148 		bnxt_get_port_module_status(bp);
14149 		mutex_unlock(&bp->link_lock);
14150 	}
14151 
14152 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14153 		bnxt_tc_flow_stats_work(bp);
14154 
14155 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14156 		bnxt_chk_missed_irq(bp);
14157 
14158 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14159 		bnxt_fw_echo_reply(bp);
14160 
14161 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14162 		bnxt_hwmon_notify_event(bp);
14163 
14164 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14165 	 * must be the last functions to be called before exiting.
14166 	 */
14167 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14168 		bnxt_reset(bp, false);
14169 
14170 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14171 		bnxt_reset(bp, true);
14172 
14173 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14174 		bnxt_rx_ring_reset(bp);
14175 
14176 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14177 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14178 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14179 			bnxt_devlink_health_fw_report(bp);
14180 		else
14181 			bnxt_fw_reset(bp);
14182 	}
14183 
14184 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14185 		if (!is_bnxt_fw_ok(bp))
14186 			bnxt_devlink_health_fw_report(bp);
14187 	}
14188 
14189 	smp_mb__before_atomic();
14190 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14191 }
14192 
14193 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14194 				int *max_cp);
14195 
14196 /* Under rtnl_lock */
14197 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14198 		     int tx_xdp)
14199 {
14200 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14201 	struct bnxt_hw_rings hwr = {0};
14202 	int rx_rings = rx;
14203 	int rc;
14204 
14205 	if (tcs)
14206 		tx_sets = tcs;
14207 
14208 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14209 
14210 	if (max_rx < rx_rings)
14211 		return -ENOMEM;
14212 
14213 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14214 		rx_rings <<= 1;
14215 
14216 	hwr.rx = rx_rings;
14217 	hwr.tx = tx * tx_sets + tx_xdp;
14218 	if (max_tx < hwr.tx)
14219 		return -ENOMEM;
14220 
14221 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14222 
14223 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14224 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14225 	if (max_cp < hwr.cp)
14226 		return -ENOMEM;
14227 	hwr.stat = hwr.cp;
14228 	if (BNXT_NEW_RM(bp)) {
14229 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14230 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14231 		hwr.grp = rx;
14232 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14233 	}
14234 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14235 		hwr.cp_p5 = hwr.tx + rx;
14236 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14237 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14238 		if (!bnxt_ulp_registered(bp->edev)) {
14239 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14240 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14241 		}
14242 		if (hwr.cp > bp->total_irqs) {
14243 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14244 
14245 			if (total_msix < hwr.cp) {
14246 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14247 					    hwr.cp, total_msix);
14248 				rc = -ENOSPC;
14249 			}
14250 		}
14251 	}
14252 	return rc;
14253 }
14254 
14255 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14256 {
14257 	if (bp->bar2) {
14258 		pci_iounmap(pdev, bp->bar2);
14259 		bp->bar2 = NULL;
14260 	}
14261 
14262 	if (bp->bar1) {
14263 		pci_iounmap(pdev, bp->bar1);
14264 		bp->bar1 = NULL;
14265 	}
14266 
14267 	if (bp->bar0) {
14268 		pci_iounmap(pdev, bp->bar0);
14269 		bp->bar0 = NULL;
14270 	}
14271 }
14272 
14273 static void bnxt_cleanup_pci(struct bnxt *bp)
14274 {
14275 	bnxt_unmap_bars(bp, bp->pdev);
14276 	pci_release_regions(bp->pdev);
14277 	if (pci_is_enabled(bp->pdev))
14278 		pci_disable_device(bp->pdev);
14279 }
14280 
14281 static void bnxt_init_dflt_coal(struct bnxt *bp)
14282 {
14283 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14284 	struct bnxt_coal *coal;
14285 	u16 flags = 0;
14286 
14287 	if (coal_cap->cmpl_params &
14288 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14289 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14290 
14291 	/* Tick values in micro seconds.
14292 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14293 	 */
14294 	coal = &bp->rx_coal;
14295 	coal->coal_ticks = 10;
14296 	coal->coal_bufs = 30;
14297 	coal->coal_ticks_irq = 1;
14298 	coal->coal_bufs_irq = 2;
14299 	coal->idle_thresh = 50;
14300 	coal->bufs_per_record = 2;
14301 	coal->budget = 64;		/* NAPI budget */
14302 	coal->flags = flags;
14303 
14304 	coal = &bp->tx_coal;
14305 	coal->coal_ticks = 28;
14306 	coal->coal_bufs = 30;
14307 	coal->coal_ticks_irq = 2;
14308 	coal->coal_bufs_irq = 2;
14309 	coal->bufs_per_record = 1;
14310 	coal->flags = flags;
14311 
14312 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14313 }
14314 
14315 /* FW that pre-reserves 1 VNIC per function */
14316 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14317 {
14318 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14319 
14320 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14321 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14322 		return true;
14323 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14324 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14325 		return true;
14326 	return false;
14327 }
14328 
14329 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14330 {
14331 	int rc;
14332 
14333 	bp->fw_cap = 0;
14334 	rc = bnxt_hwrm_ver_get(bp);
14335 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14336 	 * so wait before continuing with recovery.
14337 	 */
14338 	if (rc)
14339 		msleep(100);
14340 	bnxt_try_map_fw_health_reg(bp);
14341 	if (rc) {
14342 		rc = bnxt_try_recover_fw(bp);
14343 		if (rc)
14344 			return rc;
14345 		rc = bnxt_hwrm_ver_get(bp);
14346 		if (rc)
14347 			return rc;
14348 	}
14349 
14350 	bnxt_nvm_cfg_ver_get(bp);
14351 
14352 	rc = bnxt_hwrm_func_reset(bp);
14353 	if (rc)
14354 		return -ENODEV;
14355 
14356 	bnxt_hwrm_fw_set_time(bp);
14357 	return 0;
14358 }
14359 
14360 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14361 {
14362 	int rc;
14363 
14364 	/* Get the MAX capabilities for this function */
14365 	rc = bnxt_hwrm_func_qcaps(bp);
14366 	if (rc) {
14367 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14368 			   rc);
14369 		return -ENODEV;
14370 	}
14371 
14372 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14373 	if (rc)
14374 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14375 			    rc);
14376 
14377 	if (bnxt_alloc_fw_health(bp)) {
14378 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14379 	} else {
14380 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14381 		if (rc)
14382 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14383 				    rc);
14384 	}
14385 
14386 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14387 	if (rc)
14388 		return -ENODEV;
14389 
14390 	rc = bnxt_alloc_crash_dump_mem(bp);
14391 	if (rc)
14392 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14393 			    rc);
14394 	if (!rc) {
14395 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14396 		if (rc) {
14397 			bnxt_free_crash_dump_mem(bp);
14398 			netdev_warn(bp->dev,
14399 				    "hwrm crash dump mem failure rc: %d\n", rc);
14400 		}
14401 	}
14402 
14403 	if (bnxt_fw_pre_resv_vnics(bp))
14404 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14405 
14406 	bnxt_hwrm_func_qcfg(bp);
14407 	bnxt_hwrm_vnic_qcaps(bp);
14408 	bnxt_hwrm_port_led_qcaps(bp);
14409 	bnxt_ethtool_init(bp);
14410 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14411 		__bnxt_hwrm_ptp_qcfg(bp);
14412 	bnxt_dcb_init(bp);
14413 	bnxt_hwmon_init(bp);
14414 	return 0;
14415 }
14416 
14417 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14418 {
14419 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14420 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14421 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14422 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14423 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14424 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14425 		bp->rss_hash_delta = bp->rss_hash_cfg;
14426 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14427 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14428 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14429 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14430 	}
14431 }
14432 
14433 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14434 {
14435 	struct net_device *dev = bp->dev;
14436 
14437 	dev->hw_features &= ~NETIF_F_NTUPLE;
14438 	dev->features &= ~NETIF_F_NTUPLE;
14439 	bp->flags &= ~BNXT_FLAG_RFS;
14440 	if (bnxt_rfs_supported(bp)) {
14441 		dev->hw_features |= NETIF_F_NTUPLE;
14442 		if (bnxt_rfs_capable(bp, false)) {
14443 			bp->flags |= BNXT_FLAG_RFS;
14444 			dev->features |= NETIF_F_NTUPLE;
14445 		}
14446 	}
14447 }
14448 
14449 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14450 {
14451 	struct pci_dev *pdev = bp->pdev;
14452 
14453 	bnxt_set_dflt_rss_hash_type(bp);
14454 	bnxt_set_dflt_rfs(bp);
14455 
14456 	bnxt_get_wol_settings(bp);
14457 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14458 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14459 	else
14460 		device_set_wakeup_capable(&pdev->dev, false);
14461 
14462 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14463 	bnxt_hwrm_coal_params_qcaps(bp);
14464 }
14465 
14466 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14467 
14468 int bnxt_fw_init_one(struct bnxt *bp)
14469 {
14470 	int rc;
14471 
14472 	rc = bnxt_fw_init_one_p1(bp);
14473 	if (rc) {
14474 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14475 		return rc;
14476 	}
14477 	rc = bnxt_fw_init_one_p2(bp);
14478 	if (rc) {
14479 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14480 		return rc;
14481 	}
14482 	rc = bnxt_probe_phy(bp, false);
14483 	if (rc)
14484 		return rc;
14485 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14486 	if (rc)
14487 		return rc;
14488 
14489 	bnxt_fw_init_one_p3(bp);
14490 	return 0;
14491 }
14492 
14493 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14494 {
14495 	struct bnxt_fw_health *fw_health = bp->fw_health;
14496 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14497 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14498 	u32 reg_type, reg_off, delay_msecs;
14499 
14500 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14501 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14502 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14503 	switch (reg_type) {
14504 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14505 		pci_write_config_dword(bp->pdev, reg_off, val);
14506 		break;
14507 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14508 		writel(reg_off & BNXT_GRC_BASE_MASK,
14509 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14510 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14511 		fallthrough;
14512 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14513 		writel(val, bp->bar0 + reg_off);
14514 		break;
14515 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14516 		writel(val, bp->bar1 + reg_off);
14517 		break;
14518 	}
14519 	if (delay_msecs) {
14520 		pci_read_config_dword(bp->pdev, 0, &val);
14521 		msleep(delay_msecs);
14522 	}
14523 }
14524 
14525 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14526 {
14527 	struct hwrm_func_qcfg_output *resp;
14528 	struct hwrm_func_qcfg_input *req;
14529 	bool result = true; /* firmware will enforce if unknown */
14530 
14531 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14532 		return result;
14533 
14534 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14535 		return result;
14536 
14537 	req->fid = cpu_to_le16(0xffff);
14538 	resp = hwrm_req_hold(bp, req);
14539 	if (!hwrm_req_send(bp, req))
14540 		result = !!(le16_to_cpu(resp->flags) &
14541 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14542 	hwrm_req_drop(bp, req);
14543 	return result;
14544 }
14545 
14546 static void bnxt_reset_all(struct bnxt *bp)
14547 {
14548 	struct bnxt_fw_health *fw_health = bp->fw_health;
14549 	int i, rc;
14550 
14551 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14552 		bnxt_fw_reset_via_optee(bp);
14553 		bp->fw_reset_timestamp = jiffies;
14554 		return;
14555 	}
14556 
14557 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14558 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14559 			bnxt_fw_reset_writel(bp, i);
14560 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14561 		struct hwrm_fw_reset_input *req;
14562 
14563 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14564 		if (!rc) {
14565 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14566 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14567 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14568 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14569 			rc = hwrm_req_send(bp, req);
14570 		}
14571 		if (rc != -ENODEV)
14572 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14573 	}
14574 	bp->fw_reset_timestamp = jiffies;
14575 }
14576 
14577 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14578 {
14579 	return time_after(jiffies, bp->fw_reset_timestamp +
14580 			  (bp->fw_reset_max_dsecs * HZ / 10));
14581 }
14582 
14583 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14584 {
14585 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14586 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14587 		bnxt_dl_health_fw_status_update(bp, false);
14588 	bp->fw_reset_state = 0;
14589 	dev_close(bp->dev);
14590 }
14591 
14592 static void bnxt_fw_reset_task(struct work_struct *work)
14593 {
14594 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14595 	int rc = 0;
14596 
14597 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14598 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14599 		return;
14600 	}
14601 
14602 	switch (bp->fw_reset_state) {
14603 	case BNXT_FW_RESET_STATE_POLL_VF: {
14604 		int n = bnxt_get_registered_vfs(bp);
14605 		int tmo;
14606 
14607 		if (n < 0) {
14608 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14609 				   n, jiffies_to_msecs(jiffies -
14610 				   bp->fw_reset_timestamp));
14611 			goto fw_reset_abort;
14612 		} else if (n > 0) {
14613 			if (bnxt_fw_reset_timeout(bp)) {
14614 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14615 				bp->fw_reset_state = 0;
14616 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14617 					   n);
14618 				goto ulp_start;
14619 			}
14620 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14621 			return;
14622 		}
14623 		bp->fw_reset_timestamp = jiffies;
14624 		rtnl_lock();
14625 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14626 			bnxt_fw_reset_abort(bp, rc);
14627 			rtnl_unlock();
14628 			goto ulp_start;
14629 		}
14630 		bnxt_fw_reset_close(bp);
14631 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14632 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14633 			tmo = HZ / 10;
14634 		} else {
14635 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14636 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14637 		}
14638 		rtnl_unlock();
14639 		bnxt_queue_fw_reset_work(bp, tmo);
14640 		return;
14641 	}
14642 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14643 		u32 val;
14644 
14645 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14646 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14647 		    !bnxt_fw_reset_timeout(bp)) {
14648 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14649 			return;
14650 		}
14651 
14652 		if (!bp->fw_health->primary) {
14653 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14654 
14655 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14656 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14657 			return;
14658 		}
14659 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14660 	}
14661 		fallthrough;
14662 	case BNXT_FW_RESET_STATE_RESET_FW:
14663 		bnxt_reset_all(bp);
14664 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14665 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14666 		return;
14667 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14668 		bnxt_inv_fw_health_reg(bp);
14669 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14670 		    !bp->fw_reset_min_dsecs) {
14671 			u16 val;
14672 
14673 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14674 			if (val == 0xffff) {
14675 				if (bnxt_fw_reset_timeout(bp)) {
14676 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14677 					rc = -ETIMEDOUT;
14678 					goto fw_reset_abort;
14679 				}
14680 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14681 				return;
14682 			}
14683 		}
14684 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14685 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14686 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14687 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14688 			bnxt_dl_remote_reload(bp);
14689 		if (pci_enable_device(bp->pdev)) {
14690 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14691 			rc = -ENODEV;
14692 			goto fw_reset_abort;
14693 		}
14694 		pci_set_master(bp->pdev);
14695 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14696 		fallthrough;
14697 	case BNXT_FW_RESET_STATE_POLL_FW:
14698 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14699 		rc = bnxt_hwrm_poll(bp);
14700 		if (rc) {
14701 			if (bnxt_fw_reset_timeout(bp)) {
14702 				netdev_err(bp->dev, "Firmware reset aborted\n");
14703 				goto fw_reset_abort_status;
14704 			}
14705 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14706 			return;
14707 		}
14708 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14709 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14710 		fallthrough;
14711 	case BNXT_FW_RESET_STATE_OPENING:
14712 		while (!rtnl_trylock()) {
14713 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14714 			return;
14715 		}
14716 		rc = bnxt_open(bp->dev);
14717 		if (rc) {
14718 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14719 			bnxt_fw_reset_abort(bp, rc);
14720 			rtnl_unlock();
14721 			goto ulp_start;
14722 		}
14723 
14724 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14725 		    bp->fw_health->enabled) {
14726 			bp->fw_health->last_fw_reset_cnt =
14727 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14728 		}
14729 		bp->fw_reset_state = 0;
14730 		/* Make sure fw_reset_state is 0 before clearing the flag */
14731 		smp_mb__before_atomic();
14732 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14733 		bnxt_ptp_reapply_pps(bp);
14734 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14735 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14736 			bnxt_dl_health_fw_recovery_done(bp);
14737 			bnxt_dl_health_fw_status_update(bp, true);
14738 		}
14739 		rtnl_unlock();
14740 		bnxt_ulp_start(bp, 0);
14741 		bnxt_reenable_sriov(bp);
14742 		rtnl_lock();
14743 		bnxt_vf_reps_alloc(bp);
14744 		bnxt_vf_reps_open(bp);
14745 		rtnl_unlock();
14746 		break;
14747 	}
14748 	return;
14749 
14750 fw_reset_abort_status:
14751 	if (bp->fw_health->status_reliable ||
14752 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14753 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14754 
14755 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14756 	}
14757 fw_reset_abort:
14758 	rtnl_lock();
14759 	bnxt_fw_reset_abort(bp, rc);
14760 	rtnl_unlock();
14761 ulp_start:
14762 	bnxt_ulp_start(bp, rc);
14763 }
14764 
14765 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14766 {
14767 	int rc;
14768 	struct bnxt *bp = netdev_priv(dev);
14769 
14770 	SET_NETDEV_DEV(dev, &pdev->dev);
14771 
14772 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14773 	rc = pci_enable_device(pdev);
14774 	if (rc) {
14775 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14776 		goto init_err;
14777 	}
14778 
14779 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14780 		dev_err(&pdev->dev,
14781 			"Cannot find PCI device base address, aborting\n");
14782 		rc = -ENODEV;
14783 		goto init_err_disable;
14784 	}
14785 
14786 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14787 	if (rc) {
14788 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14789 		goto init_err_disable;
14790 	}
14791 
14792 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14793 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14794 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14795 		rc = -EIO;
14796 		goto init_err_release;
14797 	}
14798 
14799 	pci_set_master(pdev);
14800 
14801 	bp->dev = dev;
14802 	bp->pdev = pdev;
14803 
14804 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14805 	 * determines the BAR size.
14806 	 */
14807 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14808 	if (!bp->bar0) {
14809 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14810 		rc = -ENOMEM;
14811 		goto init_err_release;
14812 	}
14813 
14814 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14815 	if (!bp->bar2) {
14816 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14817 		rc = -ENOMEM;
14818 		goto init_err_release;
14819 	}
14820 
14821 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14822 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14823 
14824 	spin_lock_init(&bp->ntp_fltr_lock);
14825 #if BITS_PER_LONG == 32
14826 	spin_lock_init(&bp->db_lock);
14827 #endif
14828 
14829 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14830 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14831 
14832 	timer_setup(&bp->timer, bnxt_timer, 0);
14833 	bp->current_interval = BNXT_TIMER_INTERVAL;
14834 
14835 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14836 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14837 
14838 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14839 	return 0;
14840 
14841 init_err_release:
14842 	bnxt_unmap_bars(bp, pdev);
14843 	pci_release_regions(pdev);
14844 
14845 init_err_disable:
14846 	pci_disable_device(pdev);
14847 
14848 init_err:
14849 	return rc;
14850 }
14851 
14852 /* rtnl_lock held */
14853 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14854 {
14855 	struct sockaddr *addr = p;
14856 	struct bnxt *bp = netdev_priv(dev);
14857 	int rc = 0;
14858 
14859 	if (!is_valid_ether_addr(addr->sa_data))
14860 		return -EADDRNOTAVAIL;
14861 
14862 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14863 		return 0;
14864 
14865 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14866 	if (rc)
14867 		return rc;
14868 
14869 	eth_hw_addr_set(dev, addr->sa_data);
14870 	bnxt_clear_usr_fltrs(bp, true);
14871 	if (netif_running(dev)) {
14872 		bnxt_close_nic(bp, false, false);
14873 		rc = bnxt_open_nic(bp, false, false);
14874 	}
14875 
14876 	return rc;
14877 }
14878 
14879 /* rtnl_lock held */
14880 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14881 {
14882 	struct bnxt *bp = netdev_priv(dev);
14883 
14884 	if (netif_running(dev))
14885 		bnxt_close_nic(bp, true, false);
14886 
14887 	WRITE_ONCE(dev->mtu, new_mtu);
14888 
14889 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
14890 	 * program is attached.  We need to set the AGG rings settings and
14891 	 * rx_skb_func accordingly.
14892 	 */
14893 	if (READ_ONCE(bp->xdp_prog))
14894 		bnxt_set_rx_skb_mode(bp, true);
14895 
14896 	bnxt_set_ring_params(bp);
14897 
14898 	if (netif_running(dev))
14899 		return bnxt_open_nic(bp, true, false);
14900 
14901 	return 0;
14902 }
14903 
14904 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14905 {
14906 	struct bnxt *bp = netdev_priv(dev);
14907 	bool sh = false;
14908 	int rc, tx_cp;
14909 
14910 	if (tc > bp->max_tc) {
14911 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14912 			   tc, bp->max_tc);
14913 		return -EINVAL;
14914 	}
14915 
14916 	if (bp->num_tc == tc)
14917 		return 0;
14918 
14919 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14920 		sh = true;
14921 
14922 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14923 			      sh, tc, bp->tx_nr_rings_xdp);
14924 	if (rc)
14925 		return rc;
14926 
14927 	/* Needs to close the device and do hw resource re-allocations */
14928 	if (netif_running(bp->dev))
14929 		bnxt_close_nic(bp, true, false);
14930 
14931 	if (tc) {
14932 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14933 		netdev_set_num_tc(dev, tc);
14934 		bp->num_tc = tc;
14935 	} else {
14936 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14937 		netdev_reset_tc(dev);
14938 		bp->num_tc = 0;
14939 	}
14940 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14941 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14942 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14943 			       tx_cp + bp->rx_nr_rings;
14944 
14945 	if (netif_running(bp->dev))
14946 		return bnxt_open_nic(bp, true, false);
14947 
14948 	return 0;
14949 }
14950 
14951 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14952 				  void *cb_priv)
14953 {
14954 	struct bnxt *bp = cb_priv;
14955 
14956 	if (!bnxt_tc_flower_enabled(bp) ||
14957 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14958 		return -EOPNOTSUPP;
14959 
14960 	switch (type) {
14961 	case TC_SETUP_CLSFLOWER:
14962 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14963 	default:
14964 		return -EOPNOTSUPP;
14965 	}
14966 }
14967 
14968 LIST_HEAD(bnxt_block_cb_list);
14969 
14970 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14971 			 void *type_data)
14972 {
14973 	struct bnxt *bp = netdev_priv(dev);
14974 
14975 	switch (type) {
14976 	case TC_SETUP_BLOCK:
14977 		return flow_block_cb_setup_simple(type_data,
14978 						  &bnxt_block_cb_list,
14979 						  bnxt_setup_tc_block_cb,
14980 						  bp, bp, true);
14981 	case TC_SETUP_QDISC_MQPRIO: {
14982 		struct tc_mqprio_qopt *mqprio = type_data;
14983 
14984 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14985 
14986 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14987 	}
14988 	default:
14989 		return -EOPNOTSUPP;
14990 	}
14991 }
14992 
14993 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14994 			    const struct sk_buff *skb)
14995 {
14996 	struct bnxt_vnic_info *vnic;
14997 
14998 	if (skb)
14999 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
15000 
15001 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
15002 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
15003 }
15004 
15005 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
15006 			   u32 idx)
15007 {
15008 	struct hlist_head *head;
15009 	int bit_id;
15010 
15011 	spin_lock_bh(&bp->ntp_fltr_lock);
15012 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
15013 	if (bit_id < 0) {
15014 		spin_unlock_bh(&bp->ntp_fltr_lock);
15015 		return -ENOMEM;
15016 	}
15017 
15018 	fltr->base.sw_id = (u16)bit_id;
15019 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
15020 	fltr->base.flags |= BNXT_ACT_RING_DST;
15021 	head = &bp->ntp_fltr_hash_tbl[idx];
15022 	hlist_add_head_rcu(&fltr->base.hash, head);
15023 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15024 	bnxt_insert_usr_fltr(bp, &fltr->base);
15025 	bp->ntp_fltr_count++;
15026 	spin_unlock_bh(&bp->ntp_fltr_lock);
15027 	return 0;
15028 }
15029 
15030 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15031 			    struct bnxt_ntuple_filter *f2)
15032 {
15033 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15034 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15035 	struct flow_keys *keys1 = &f1->fkeys;
15036 	struct flow_keys *keys2 = &f2->fkeys;
15037 
15038 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15039 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15040 		return false;
15041 
15042 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15043 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15044 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15045 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15046 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15047 			return false;
15048 	} else {
15049 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15050 				     &keys2->addrs.v6addrs.src) ||
15051 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15052 				     &masks2->addrs.v6addrs.src) ||
15053 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15054 				     &keys2->addrs.v6addrs.dst) ||
15055 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15056 				     &masks2->addrs.v6addrs.dst))
15057 			return false;
15058 	}
15059 
15060 	return keys1->ports.src == keys2->ports.src &&
15061 	       masks1->ports.src == masks2->ports.src &&
15062 	       keys1->ports.dst == keys2->ports.dst &&
15063 	       masks1->ports.dst == masks2->ports.dst &&
15064 	       keys1->control.flags == keys2->control.flags &&
15065 	       f1->l2_fltr == f2->l2_fltr;
15066 }
15067 
15068 struct bnxt_ntuple_filter *
15069 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15070 				struct bnxt_ntuple_filter *fltr, u32 idx)
15071 {
15072 	struct bnxt_ntuple_filter *f;
15073 	struct hlist_head *head;
15074 
15075 	head = &bp->ntp_fltr_hash_tbl[idx];
15076 	hlist_for_each_entry_rcu(f, head, base.hash) {
15077 		if (bnxt_fltr_match(f, fltr))
15078 			return f;
15079 	}
15080 	return NULL;
15081 }
15082 
15083 #ifdef CONFIG_RFS_ACCEL
15084 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15085 			      u16 rxq_index, u32 flow_id)
15086 {
15087 	struct bnxt *bp = netdev_priv(dev);
15088 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15089 	struct flow_keys *fkeys;
15090 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15091 	struct bnxt_l2_filter *l2_fltr;
15092 	int rc = 0, idx;
15093 	u32 flags;
15094 
15095 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15096 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15097 		atomic_inc(&l2_fltr->refcnt);
15098 	} else {
15099 		struct bnxt_l2_key key;
15100 
15101 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15102 		key.vlan = 0;
15103 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15104 		if (!l2_fltr)
15105 			return -EINVAL;
15106 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15107 			bnxt_del_l2_filter(bp, l2_fltr);
15108 			return -EINVAL;
15109 		}
15110 	}
15111 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15112 	if (!new_fltr) {
15113 		bnxt_del_l2_filter(bp, l2_fltr);
15114 		return -ENOMEM;
15115 	}
15116 
15117 	fkeys = &new_fltr->fkeys;
15118 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15119 		rc = -EPROTONOSUPPORT;
15120 		goto err_free;
15121 	}
15122 
15123 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15124 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15125 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15126 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15127 		rc = -EPROTONOSUPPORT;
15128 		goto err_free;
15129 	}
15130 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15131 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15132 		if (bp->hwrm_spec_code < 0x10601) {
15133 			rc = -EPROTONOSUPPORT;
15134 			goto err_free;
15135 		}
15136 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15137 	}
15138 	flags = fkeys->control.flags;
15139 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15140 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15141 		rc = -EPROTONOSUPPORT;
15142 		goto err_free;
15143 	}
15144 	new_fltr->l2_fltr = l2_fltr;
15145 
15146 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15147 	rcu_read_lock();
15148 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15149 	if (fltr) {
15150 		rc = fltr->base.sw_id;
15151 		rcu_read_unlock();
15152 		goto err_free;
15153 	}
15154 	rcu_read_unlock();
15155 
15156 	new_fltr->flow_id = flow_id;
15157 	new_fltr->base.rxq = rxq_index;
15158 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15159 	if (!rc) {
15160 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15161 		return new_fltr->base.sw_id;
15162 	}
15163 
15164 err_free:
15165 	bnxt_del_l2_filter(bp, l2_fltr);
15166 	kfree(new_fltr);
15167 	return rc;
15168 }
15169 #endif
15170 
15171 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15172 {
15173 	spin_lock_bh(&bp->ntp_fltr_lock);
15174 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15175 		spin_unlock_bh(&bp->ntp_fltr_lock);
15176 		return;
15177 	}
15178 	hlist_del_rcu(&fltr->base.hash);
15179 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15180 	bp->ntp_fltr_count--;
15181 	spin_unlock_bh(&bp->ntp_fltr_lock);
15182 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15183 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15184 	kfree_rcu(fltr, base.rcu);
15185 }
15186 
15187 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15188 {
15189 #ifdef CONFIG_RFS_ACCEL
15190 	int i;
15191 
15192 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15193 		struct hlist_head *head;
15194 		struct hlist_node *tmp;
15195 		struct bnxt_ntuple_filter *fltr;
15196 		int rc;
15197 
15198 		head = &bp->ntp_fltr_hash_tbl[i];
15199 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15200 			bool del = false;
15201 
15202 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15203 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15204 					continue;
15205 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15206 							fltr->flow_id,
15207 							fltr->base.sw_id)) {
15208 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15209 									 fltr);
15210 					del = true;
15211 				}
15212 			} else {
15213 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15214 								       fltr);
15215 				if (rc)
15216 					del = true;
15217 				else
15218 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15219 			}
15220 
15221 			if (del)
15222 				bnxt_del_ntp_filter(bp, fltr);
15223 		}
15224 	}
15225 #endif
15226 }
15227 
15228 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15229 				    unsigned int entry, struct udp_tunnel_info *ti)
15230 {
15231 	struct bnxt *bp = netdev_priv(netdev);
15232 	unsigned int cmd;
15233 
15234 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15235 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15236 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15237 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15238 	else
15239 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15240 
15241 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15242 }
15243 
15244 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15245 				      unsigned int entry, struct udp_tunnel_info *ti)
15246 {
15247 	struct bnxt *bp = netdev_priv(netdev);
15248 	unsigned int cmd;
15249 
15250 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15251 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15252 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15253 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15254 	else
15255 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15256 
15257 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15258 }
15259 
15260 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15261 	.set_port	= bnxt_udp_tunnel_set_port,
15262 	.unset_port	= bnxt_udp_tunnel_unset_port,
15263 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15264 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15265 	.tables		= {
15266 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15267 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15268 	},
15269 }, bnxt_udp_tunnels_p7 = {
15270 	.set_port	= bnxt_udp_tunnel_set_port,
15271 	.unset_port	= bnxt_udp_tunnel_unset_port,
15272 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15273 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15274 	.tables		= {
15275 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15276 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15277 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15278 	},
15279 };
15280 
15281 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15282 			       struct net_device *dev, u32 filter_mask,
15283 			       int nlflags)
15284 {
15285 	struct bnxt *bp = netdev_priv(dev);
15286 
15287 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15288 				       nlflags, filter_mask, NULL);
15289 }
15290 
15291 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15292 			       u16 flags, struct netlink_ext_ack *extack)
15293 {
15294 	struct bnxt *bp = netdev_priv(dev);
15295 	struct nlattr *attr, *br_spec;
15296 	int rem, rc = 0;
15297 
15298 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15299 		return -EOPNOTSUPP;
15300 
15301 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15302 	if (!br_spec)
15303 		return -EINVAL;
15304 
15305 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15306 		u16 mode;
15307 
15308 		mode = nla_get_u16(attr);
15309 		if (mode == bp->br_mode)
15310 			break;
15311 
15312 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15313 		if (!rc)
15314 			bp->br_mode = mode;
15315 		break;
15316 	}
15317 	return rc;
15318 }
15319 
15320 int bnxt_get_port_parent_id(struct net_device *dev,
15321 			    struct netdev_phys_item_id *ppid)
15322 {
15323 	struct bnxt *bp = netdev_priv(dev);
15324 
15325 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15326 		return -EOPNOTSUPP;
15327 
15328 	/* The PF and it's VF-reps only support the switchdev framework */
15329 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15330 		return -EOPNOTSUPP;
15331 
15332 	ppid->id_len = sizeof(bp->dsn);
15333 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15334 
15335 	return 0;
15336 }
15337 
15338 static const struct net_device_ops bnxt_netdev_ops = {
15339 	.ndo_open		= bnxt_open,
15340 	.ndo_start_xmit		= bnxt_start_xmit,
15341 	.ndo_stop		= bnxt_close,
15342 	.ndo_get_stats64	= bnxt_get_stats64,
15343 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15344 	.ndo_eth_ioctl		= bnxt_ioctl,
15345 	.ndo_validate_addr	= eth_validate_addr,
15346 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15347 	.ndo_change_mtu		= bnxt_change_mtu,
15348 	.ndo_fix_features	= bnxt_fix_features,
15349 	.ndo_set_features	= bnxt_set_features,
15350 	.ndo_features_check	= bnxt_features_check,
15351 	.ndo_tx_timeout		= bnxt_tx_timeout,
15352 #ifdef CONFIG_BNXT_SRIOV
15353 	.ndo_get_vf_config	= bnxt_get_vf_config,
15354 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15355 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15356 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15357 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15358 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15359 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15360 #endif
15361 	.ndo_setup_tc           = bnxt_setup_tc,
15362 #ifdef CONFIG_RFS_ACCEL
15363 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15364 #endif
15365 	.ndo_bpf		= bnxt_xdp,
15366 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15367 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15368 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15369 };
15370 
15371 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15372 				    struct netdev_queue_stats_rx *stats)
15373 {
15374 	struct bnxt *bp = netdev_priv(dev);
15375 	struct bnxt_cp_ring_info *cpr;
15376 	u64 *sw;
15377 
15378 	cpr = &bp->bnapi[i]->cp_ring;
15379 	sw = cpr->stats.sw_stats;
15380 
15381 	stats->packets = 0;
15382 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15383 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15384 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15385 
15386 	stats->bytes = 0;
15387 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15388 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15389 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15390 
15391 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15392 }
15393 
15394 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15395 				    struct netdev_queue_stats_tx *stats)
15396 {
15397 	struct bnxt *bp = netdev_priv(dev);
15398 	struct bnxt_napi *bnapi;
15399 	u64 *sw;
15400 
15401 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15402 	sw = bnapi->cp_ring.stats.sw_stats;
15403 
15404 	stats->packets = 0;
15405 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15406 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15407 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15408 
15409 	stats->bytes = 0;
15410 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15411 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15412 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15413 }
15414 
15415 static void bnxt_get_base_stats(struct net_device *dev,
15416 				struct netdev_queue_stats_rx *rx,
15417 				struct netdev_queue_stats_tx *tx)
15418 {
15419 	struct bnxt *bp = netdev_priv(dev);
15420 
15421 	rx->packets = bp->net_stats_prev.rx_packets;
15422 	rx->bytes = bp->net_stats_prev.rx_bytes;
15423 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15424 
15425 	tx->packets = bp->net_stats_prev.tx_packets;
15426 	tx->bytes = bp->net_stats_prev.tx_bytes;
15427 }
15428 
15429 static const struct netdev_stat_ops bnxt_stat_ops = {
15430 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15431 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15432 	.get_base_stats		= bnxt_get_base_stats,
15433 };
15434 
15435 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15436 {
15437 	struct bnxt_rx_ring_info *rxr, *clone;
15438 	struct bnxt *bp = netdev_priv(dev);
15439 	struct bnxt_ring_struct *ring;
15440 	int rc;
15441 
15442 	rxr = &bp->rx_ring[idx];
15443 	clone = qmem;
15444 	memcpy(clone, rxr, sizeof(*rxr));
15445 	bnxt_init_rx_ring_struct(bp, clone);
15446 	bnxt_reset_rx_ring_struct(bp, clone);
15447 
15448 	clone->rx_prod = 0;
15449 	clone->rx_agg_prod = 0;
15450 	clone->rx_sw_agg_prod = 0;
15451 	clone->rx_next_cons = 0;
15452 
15453 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15454 	if (rc)
15455 		return rc;
15456 
15457 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15458 	if (rc < 0)
15459 		goto err_page_pool_destroy;
15460 
15461 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15462 					MEM_TYPE_PAGE_POOL,
15463 					clone->page_pool);
15464 	if (rc)
15465 		goto err_rxq_info_unreg;
15466 
15467 	ring = &clone->rx_ring_struct;
15468 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15469 	if (rc)
15470 		goto err_free_rx_ring;
15471 
15472 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15473 		ring = &clone->rx_agg_ring_struct;
15474 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15475 		if (rc)
15476 			goto err_free_rx_agg_ring;
15477 
15478 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15479 		if (rc)
15480 			goto err_free_rx_agg_ring;
15481 	}
15482 
15483 	if (bp->flags & BNXT_FLAG_TPA) {
15484 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15485 		if (rc)
15486 			goto err_free_tpa_info;
15487 	}
15488 
15489 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15490 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15491 
15492 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15493 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15494 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15495 	if (bp->flags & BNXT_FLAG_TPA)
15496 		bnxt_alloc_one_tpa_info_data(bp, clone);
15497 
15498 	return 0;
15499 
15500 err_free_tpa_info:
15501 	bnxt_free_one_tpa_info(bp, clone);
15502 err_free_rx_agg_ring:
15503 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15504 err_free_rx_ring:
15505 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15506 err_rxq_info_unreg:
15507 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15508 err_page_pool_destroy:
15509 	page_pool_destroy(clone->page_pool);
15510 	if (bnxt_separate_head_pool())
15511 		page_pool_destroy(clone->head_pool);
15512 	clone->page_pool = NULL;
15513 	clone->head_pool = NULL;
15514 	return rc;
15515 }
15516 
15517 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15518 {
15519 	struct bnxt_rx_ring_info *rxr = qmem;
15520 	struct bnxt *bp = netdev_priv(dev);
15521 	struct bnxt_ring_struct *ring;
15522 
15523 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15524 
15525 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15526 
15527 	page_pool_destroy(rxr->page_pool);
15528 	if (bnxt_separate_head_pool())
15529 		page_pool_destroy(rxr->head_pool);
15530 	rxr->page_pool = NULL;
15531 	rxr->head_pool = NULL;
15532 
15533 	ring = &rxr->rx_ring_struct;
15534 	bnxt_free_ring(bp, &ring->ring_mem);
15535 
15536 	ring = &rxr->rx_agg_ring_struct;
15537 	bnxt_free_ring(bp, &ring->ring_mem);
15538 
15539 	kfree(rxr->rx_agg_bmap);
15540 	rxr->rx_agg_bmap = NULL;
15541 }
15542 
15543 static void bnxt_copy_rx_ring(struct bnxt *bp,
15544 			      struct bnxt_rx_ring_info *dst,
15545 			      struct bnxt_rx_ring_info *src)
15546 {
15547 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15548 	struct bnxt_ring_struct *dst_ring, *src_ring;
15549 	int i;
15550 
15551 	dst_ring = &dst->rx_ring_struct;
15552 	dst_rmem = &dst_ring->ring_mem;
15553 	src_ring = &src->rx_ring_struct;
15554 	src_rmem = &src_ring->ring_mem;
15555 
15556 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15557 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15558 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15559 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15560 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15561 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15562 
15563 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15564 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15565 	*dst_rmem->vmem = *src_rmem->vmem;
15566 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15567 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15568 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15569 	}
15570 
15571 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15572 		return;
15573 
15574 	dst_ring = &dst->rx_agg_ring_struct;
15575 	dst_rmem = &dst_ring->ring_mem;
15576 	src_ring = &src->rx_agg_ring_struct;
15577 	src_rmem = &src_ring->ring_mem;
15578 
15579 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15580 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15581 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15582 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15583 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15584 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15585 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15586 
15587 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15588 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15589 	*dst_rmem->vmem = *src_rmem->vmem;
15590 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15591 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15592 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15593 	}
15594 
15595 	dst->rx_agg_bmap = src->rx_agg_bmap;
15596 }
15597 
15598 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15599 {
15600 	struct bnxt *bp = netdev_priv(dev);
15601 	struct bnxt_rx_ring_info *rxr, *clone;
15602 	struct bnxt_cp_ring_info *cpr;
15603 	struct bnxt_vnic_info *vnic;
15604 	int i, rc;
15605 
15606 	rxr = &bp->rx_ring[idx];
15607 	clone = qmem;
15608 
15609 	rxr->rx_prod = clone->rx_prod;
15610 	rxr->rx_agg_prod = clone->rx_agg_prod;
15611 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15612 	rxr->rx_next_cons = clone->rx_next_cons;
15613 	rxr->rx_tpa = clone->rx_tpa;
15614 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15615 	rxr->page_pool = clone->page_pool;
15616 	rxr->head_pool = clone->head_pool;
15617 	rxr->xdp_rxq = clone->xdp_rxq;
15618 
15619 	bnxt_copy_rx_ring(bp, rxr, clone);
15620 
15621 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15622 	if (rc)
15623 		return rc;
15624 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15625 	if (rc)
15626 		goto err_free_hwrm_rx_ring;
15627 
15628 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15629 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15630 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15631 
15632 	cpr = &rxr->bnapi->cp_ring;
15633 	cpr->sw_stats->rx.rx_resets++;
15634 
15635 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15636 		vnic = &bp->vnic_info[i];
15637 
15638 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15639 		if (rc) {
15640 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15641 				   vnic->vnic_id, rc);
15642 			return rc;
15643 		}
15644 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15645 		bnxt_hwrm_vnic_update(bp, vnic,
15646 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15647 	}
15648 
15649 	return 0;
15650 
15651 err_free_hwrm_rx_ring:
15652 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15653 	return rc;
15654 }
15655 
15656 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15657 {
15658 	struct bnxt *bp = netdev_priv(dev);
15659 	struct bnxt_rx_ring_info *rxr;
15660 	struct bnxt_vnic_info *vnic;
15661 	int i;
15662 
15663 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15664 		vnic = &bp->vnic_info[i];
15665 		vnic->mru = 0;
15666 		bnxt_hwrm_vnic_update(bp, vnic,
15667 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15668 	}
15669 	/* Make sure NAPI sees that the VNIC is disabled */
15670 	synchronize_net();
15671 	rxr = &bp->rx_ring[idx];
15672 	cancel_work_sync(&rxr->bnapi->cp_ring.dim.work);
15673 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15674 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15675 	rxr->rx_next_cons = 0;
15676 	page_pool_disable_direct_recycling(rxr->page_pool);
15677 	if (bnxt_separate_head_pool())
15678 		page_pool_disable_direct_recycling(rxr->head_pool);
15679 
15680 	memcpy(qmem, rxr, sizeof(*rxr));
15681 	bnxt_init_rx_ring_struct(bp, qmem);
15682 
15683 	return 0;
15684 }
15685 
15686 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15687 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15688 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15689 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15690 	.ndo_queue_start	= bnxt_queue_start,
15691 	.ndo_queue_stop		= bnxt_queue_stop,
15692 };
15693 
15694 static void bnxt_remove_one(struct pci_dev *pdev)
15695 {
15696 	struct net_device *dev = pci_get_drvdata(pdev);
15697 	struct bnxt *bp = netdev_priv(dev);
15698 
15699 	if (BNXT_PF(bp))
15700 		bnxt_sriov_disable(bp);
15701 
15702 	bnxt_rdma_aux_device_del(bp);
15703 
15704 	bnxt_ptp_clear(bp);
15705 	unregister_netdev(dev);
15706 
15707 	bnxt_rdma_aux_device_uninit(bp);
15708 
15709 	bnxt_free_l2_filters(bp, true);
15710 	bnxt_free_ntp_fltrs(bp, true);
15711 	WARN_ON(bp->num_rss_ctx);
15712 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15713 	/* Flush any pending tasks */
15714 	cancel_work_sync(&bp->sp_task);
15715 	cancel_delayed_work_sync(&bp->fw_reset_task);
15716 	bp->sp_event = 0;
15717 
15718 	bnxt_dl_fw_reporters_destroy(bp);
15719 	bnxt_dl_unregister(bp);
15720 	bnxt_shutdown_tc(bp);
15721 
15722 	bnxt_clear_int_mode(bp);
15723 	bnxt_hwrm_func_drv_unrgtr(bp);
15724 	bnxt_free_hwrm_resources(bp);
15725 	bnxt_hwmon_uninit(bp);
15726 	bnxt_ethtool_free(bp);
15727 	bnxt_dcb_free(bp);
15728 	kfree(bp->ptp_cfg);
15729 	bp->ptp_cfg = NULL;
15730 	kfree(bp->fw_health);
15731 	bp->fw_health = NULL;
15732 	bnxt_cleanup_pci(bp);
15733 	bnxt_free_ctx_mem(bp, true);
15734 	bnxt_free_crash_dump_mem(bp);
15735 	kfree(bp->rss_indir_tbl);
15736 	bp->rss_indir_tbl = NULL;
15737 	bnxt_free_port_stats(bp);
15738 	free_netdev(dev);
15739 }
15740 
15741 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15742 {
15743 	int rc = 0;
15744 	struct bnxt_link_info *link_info = &bp->link_info;
15745 
15746 	bp->phy_flags = 0;
15747 	rc = bnxt_hwrm_phy_qcaps(bp);
15748 	if (rc) {
15749 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15750 			   rc);
15751 		return rc;
15752 	}
15753 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15754 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15755 	else
15756 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15757 
15758 	bp->mac_flags = 0;
15759 	bnxt_hwrm_mac_qcaps(bp);
15760 
15761 	if (!fw_dflt)
15762 		return 0;
15763 
15764 	mutex_lock(&bp->link_lock);
15765 	rc = bnxt_update_link(bp, false);
15766 	if (rc) {
15767 		mutex_unlock(&bp->link_lock);
15768 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15769 			   rc);
15770 		return rc;
15771 	}
15772 
15773 	/* Older firmware does not have supported_auto_speeds, so assume
15774 	 * that all supported speeds can be autonegotiated.
15775 	 */
15776 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15777 		link_info->support_auto_speeds = link_info->support_speeds;
15778 
15779 	bnxt_init_ethtool_link_settings(bp);
15780 	mutex_unlock(&bp->link_lock);
15781 	return 0;
15782 }
15783 
15784 static int bnxt_get_max_irq(struct pci_dev *pdev)
15785 {
15786 	u16 ctrl;
15787 
15788 	if (!pdev->msix_cap)
15789 		return 1;
15790 
15791 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15792 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15793 }
15794 
15795 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15796 				int *max_cp)
15797 {
15798 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15799 	int max_ring_grps = 0, max_irq;
15800 
15801 	*max_tx = hw_resc->max_tx_rings;
15802 	*max_rx = hw_resc->max_rx_rings;
15803 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15804 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15805 			bnxt_get_ulp_msix_num_in_use(bp),
15806 			hw_resc->max_stat_ctxs -
15807 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15808 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15809 		*max_cp = min_t(int, *max_cp, max_irq);
15810 	max_ring_grps = hw_resc->max_hw_ring_grps;
15811 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15812 		*max_cp -= 1;
15813 		*max_rx -= 2;
15814 	}
15815 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15816 		*max_rx >>= 1;
15817 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15818 		int rc;
15819 
15820 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15821 		if (rc) {
15822 			*max_rx = 0;
15823 			*max_tx = 0;
15824 		}
15825 		/* On P5 chips, max_cp output param should be available NQs */
15826 		*max_cp = max_irq;
15827 	}
15828 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15829 }
15830 
15831 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15832 {
15833 	int rx, tx, cp;
15834 
15835 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15836 	*max_rx = rx;
15837 	*max_tx = tx;
15838 	if (!rx || !tx || !cp)
15839 		return -ENOMEM;
15840 
15841 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15842 }
15843 
15844 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15845 			       bool shared)
15846 {
15847 	int rc;
15848 
15849 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15850 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15851 		/* Not enough rings, try disabling agg rings. */
15852 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15853 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15854 		if (rc) {
15855 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15856 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15857 			return rc;
15858 		}
15859 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15860 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15861 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15862 		bnxt_set_ring_params(bp);
15863 	}
15864 
15865 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15866 		int max_cp, max_stat, max_irq;
15867 
15868 		/* Reserve minimum resources for RoCE */
15869 		max_cp = bnxt_get_max_func_cp_rings(bp);
15870 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15871 		max_irq = bnxt_get_max_func_irqs(bp);
15872 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15873 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15874 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15875 			return 0;
15876 
15877 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15878 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15879 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15880 		max_cp = min_t(int, max_cp, max_irq);
15881 		max_cp = min_t(int, max_cp, max_stat);
15882 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15883 		if (rc)
15884 			rc = 0;
15885 	}
15886 	return rc;
15887 }
15888 
15889 /* In initial default shared ring setting, each shared ring must have a
15890  * RX/TX ring pair.
15891  */
15892 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15893 {
15894 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15895 	bp->rx_nr_rings = bp->cp_nr_rings;
15896 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15897 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15898 }
15899 
15900 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15901 {
15902 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15903 	int avail_msix;
15904 
15905 	if (!bnxt_can_reserve_rings(bp))
15906 		return 0;
15907 
15908 	if (sh)
15909 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15910 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15911 	/* Reduce default rings on multi-port cards so that total default
15912 	 * rings do not exceed CPU count.
15913 	 */
15914 	if (bp->port_count > 1) {
15915 		int max_rings =
15916 			max_t(int, num_online_cpus() / bp->port_count, 1);
15917 
15918 		dflt_rings = min_t(int, dflt_rings, max_rings);
15919 	}
15920 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15921 	if (rc)
15922 		return rc;
15923 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15924 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15925 	if (sh)
15926 		bnxt_trim_dflt_sh_rings(bp);
15927 	else
15928 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15929 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15930 
15931 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15932 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15933 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15934 
15935 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15936 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15937 	}
15938 
15939 	rc = __bnxt_reserve_rings(bp);
15940 	if (rc && rc != -ENODEV)
15941 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15942 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15943 	if (sh)
15944 		bnxt_trim_dflt_sh_rings(bp);
15945 
15946 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15947 	if (bnxt_need_reserve_rings(bp)) {
15948 		rc = __bnxt_reserve_rings(bp);
15949 		if (rc && rc != -ENODEV)
15950 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15951 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15952 	}
15953 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15954 		bp->rx_nr_rings++;
15955 		bp->cp_nr_rings++;
15956 	}
15957 	if (rc) {
15958 		bp->tx_nr_rings = 0;
15959 		bp->rx_nr_rings = 0;
15960 	}
15961 	return rc;
15962 }
15963 
15964 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15965 {
15966 	int rc;
15967 
15968 	if (bp->tx_nr_rings)
15969 		return 0;
15970 
15971 	bnxt_ulp_irq_stop(bp);
15972 	bnxt_clear_int_mode(bp);
15973 	rc = bnxt_set_dflt_rings(bp, true);
15974 	if (rc) {
15975 		if (BNXT_VF(bp) && rc == -ENODEV)
15976 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15977 		else
15978 			netdev_err(bp->dev, "Not enough rings available.\n");
15979 		goto init_dflt_ring_err;
15980 	}
15981 	rc = bnxt_init_int_mode(bp);
15982 	if (rc)
15983 		goto init_dflt_ring_err;
15984 
15985 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15986 
15987 	bnxt_set_dflt_rfs(bp);
15988 
15989 init_dflt_ring_err:
15990 	bnxt_ulp_irq_restart(bp, rc);
15991 	return rc;
15992 }
15993 
15994 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15995 {
15996 	int rc;
15997 
15998 	ASSERT_RTNL();
15999 	bnxt_hwrm_func_qcaps(bp);
16000 
16001 	if (netif_running(bp->dev))
16002 		__bnxt_close_nic(bp, true, false);
16003 
16004 	bnxt_ulp_irq_stop(bp);
16005 	bnxt_clear_int_mode(bp);
16006 	rc = bnxt_init_int_mode(bp);
16007 	bnxt_ulp_irq_restart(bp, rc);
16008 
16009 	if (netif_running(bp->dev)) {
16010 		if (rc)
16011 			dev_close(bp->dev);
16012 		else
16013 			rc = bnxt_open_nic(bp, true, false);
16014 	}
16015 
16016 	return rc;
16017 }
16018 
16019 static int bnxt_init_mac_addr(struct bnxt *bp)
16020 {
16021 	int rc = 0;
16022 
16023 	if (BNXT_PF(bp)) {
16024 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16025 	} else {
16026 #ifdef CONFIG_BNXT_SRIOV
16027 		struct bnxt_vf_info *vf = &bp->vf;
16028 		bool strict_approval = true;
16029 
16030 		if (is_valid_ether_addr(vf->mac_addr)) {
16031 			/* overwrite netdev dev_addr with admin VF MAC */
16032 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16033 			/* Older PF driver or firmware may not approve this
16034 			 * correctly.
16035 			 */
16036 			strict_approval = false;
16037 		} else {
16038 			eth_hw_addr_random(bp->dev);
16039 		}
16040 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16041 #endif
16042 	}
16043 	return rc;
16044 }
16045 
16046 static void bnxt_vpd_read_info(struct bnxt *bp)
16047 {
16048 	struct pci_dev *pdev = bp->pdev;
16049 	unsigned int vpd_size, kw_len;
16050 	int pos, size;
16051 	u8 *vpd_data;
16052 
16053 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16054 	if (IS_ERR(vpd_data)) {
16055 		pci_warn(pdev, "Unable to read VPD\n");
16056 		return;
16057 	}
16058 
16059 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16060 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16061 	if (pos < 0)
16062 		goto read_sn;
16063 
16064 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16065 	memcpy(bp->board_partno, &vpd_data[pos], size);
16066 
16067 read_sn:
16068 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16069 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16070 					   &kw_len);
16071 	if (pos < 0)
16072 		goto exit;
16073 
16074 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16075 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16076 exit:
16077 	kfree(vpd_data);
16078 }
16079 
16080 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16081 {
16082 	struct pci_dev *pdev = bp->pdev;
16083 	u64 qword;
16084 
16085 	qword = pci_get_dsn(pdev);
16086 	if (!qword) {
16087 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16088 		return -EOPNOTSUPP;
16089 	}
16090 
16091 	put_unaligned_le64(qword, dsn);
16092 
16093 	bp->flags |= BNXT_FLAG_DSN_VALID;
16094 	return 0;
16095 }
16096 
16097 static int bnxt_map_db_bar(struct bnxt *bp)
16098 {
16099 	if (!bp->db_size)
16100 		return -ENODEV;
16101 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16102 	if (!bp->bar1)
16103 		return -ENOMEM;
16104 	return 0;
16105 }
16106 
16107 void bnxt_print_device_info(struct bnxt *bp)
16108 {
16109 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16110 		    board_info[bp->board_idx].name,
16111 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16112 
16113 	pcie_print_link_status(bp->pdev);
16114 }
16115 
16116 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16117 {
16118 	struct bnxt_hw_resc *hw_resc;
16119 	struct net_device *dev;
16120 	struct bnxt *bp;
16121 	int rc, max_irqs;
16122 
16123 	if (pci_is_bridge(pdev))
16124 		return -ENODEV;
16125 
16126 	if (!pdev->msix_cap) {
16127 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16128 		return -ENODEV;
16129 	}
16130 
16131 	/* Clear any pending DMA transactions from crash kernel
16132 	 * while loading driver in capture kernel.
16133 	 */
16134 	if (is_kdump_kernel()) {
16135 		pci_clear_master(pdev);
16136 		pcie_flr(pdev);
16137 	}
16138 
16139 	max_irqs = bnxt_get_max_irq(pdev);
16140 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16141 				 max_irqs);
16142 	if (!dev)
16143 		return -ENOMEM;
16144 
16145 	bp = netdev_priv(dev);
16146 	bp->board_idx = ent->driver_data;
16147 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16148 	bnxt_set_max_func_irqs(bp, max_irqs);
16149 
16150 	if (bnxt_vf_pciid(bp->board_idx))
16151 		bp->flags |= BNXT_FLAG_VF;
16152 
16153 	/* No devlink port registration in case of a VF */
16154 	if (BNXT_PF(bp))
16155 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16156 
16157 	rc = bnxt_init_board(pdev, dev);
16158 	if (rc < 0)
16159 		goto init_err_free;
16160 
16161 	dev->netdev_ops = &bnxt_netdev_ops;
16162 	dev->stat_ops = &bnxt_stat_ops;
16163 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16164 	dev->ethtool_ops = &bnxt_ethtool_ops;
16165 	pci_set_drvdata(pdev, dev);
16166 
16167 	rc = bnxt_alloc_hwrm_resources(bp);
16168 	if (rc)
16169 		goto init_err_pci_clean;
16170 
16171 	mutex_init(&bp->hwrm_cmd_lock);
16172 	mutex_init(&bp->link_lock);
16173 
16174 	rc = bnxt_fw_init_one_p1(bp);
16175 	if (rc)
16176 		goto init_err_pci_clean;
16177 
16178 	if (BNXT_PF(bp))
16179 		bnxt_vpd_read_info(bp);
16180 
16181 	if (BNXT_CHIP_P5_PLUS(bp)) {
16182 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16183 		if (BNXT_CHIP_P7(bp))
16184 			bp->flags |= BNXT_FLAG_CHIP_P7;
16185 	}
16186 
16187 	rc = bnxt_alloc_rss_indir_tbl(bp);
16188 	if (rc)
16189 		goto init_err_pci_clean;
16190 
16191 	rc = bnxt_fw_init_one_p2(bp);
16192 	if (rc)
16193 		goto init_err_pci_clean;
16194 
16195 	rc = bnxt_map_db_bar(bp);
16196 	if (rc) {
16197 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16198 			rc);
16199 		goto init_err_pci_clean;
16200 	}
16201 
16202 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16203 			   NETIF_F_TSO | NETIF_F_TSO6 |
16204 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16205 			   NETIF_F_GSO_IPXIP4 |
16206 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16207 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16208 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16209 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16210 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16211 
16212 	if (BNXT_SUPPORTS_TPA(bp))
16213 		dev->hw_features |= NETIF_F_LRO;
16214 
16215 	dev->hw_enc_features =
16216 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16217 			NETIF_F_TSO | NETIF_F_TSO6 |
16218 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16219 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16220 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16221 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16222 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16223 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16224 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16225 	else
16226 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16227 
16228 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16229 				    NETIF_F_GSO_GRE_CSUM;
16230 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16231 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16232 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16233 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16234 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16235 	if (BNXT_SUPPORTS_TPA(bp))
16236 		dev->hw_features |= NETIF_F_GRO_HW;
16237 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16238 	if (dev->features & NETIF_F_GRO_HW)
16239 		dev->features &= ~NETIF_F_LRO;
16240 	dev->priv_flags |= IFF_UNICAST_FLT;
16241 
16242 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16243 	if (bp->tso_max_segs)
16244 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16245 
16246 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16247 			    NETDEV_XDP_ACT_RX_SG;
16248 
16249 #ifdef CONFIG_BNXT_SRIOV
16250 	init_waitqueue_head(&bp->sriov_cfg_wait);
16251 #endif
16252 	if (BNXT_SUPPORTS_TPA(bp)) {
16253 		bp->gro_func = bnxt_gro_func_5730x;
16254 		if (BNXT_CHIP_P4(bp))
16255 			bp->gro_func = bnxt_gro_func_5731x;
16256 		else if (BNXT_CHIP_P5_PLUS(bp))
16257 			bp->gro_func = bnxt_gro_func_5750x;
16258 	}
16259 	if (!BNXT_CHIP_P4_PLUS(bp))
16260 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16261 
16262 	rc = bnxt_init_mac_addr(bp);
16263 	if (rc) {
16264 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16265 		rc = -EADDRNOTAVAIL;
16266 		goto init_err_pci_clean;
16267 	}
16268 
16269 	if (BNXT_PF(bp)) {
16270 		/* Read the adapter's DSN to use as the eswitch switch_id */
16271 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16272 	}
16273 
16274 	/* MTU range: 60 - FW defined max */
16275 	dev->min_mtu = ETH_ZLEN;
16276 	dev->max_mtu = bp->max_mtu;
16277 
16278 	rc = bnxt_probe_phy(bp, true);
16279 	if (rc)
16280 		goto init_err_pci_clean;
16281 
16282 	hw_resc = &bp->hw_resc;
16283 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16284 		       BNXT_L2_FLTR_MAX_FLTR;
16285 	/* Older firmware may not report these filters properly */
16286 	if (bp->max_fltr < BNXT_MAX_FLTR)
16287 		bp->max_fltr = BNXT_MAX_FLTR;
16288 	bnxt_init_l2_fltr_tbl(bp);
16289 	__bnxt_set_rx_skb_mode(bp, false);
16290 	bnxt_set_tpa_flags(bp);
16291 	bnxt_init_ring_params(bp);
16292 	bnxt_set_ring_params(bp);
16293 	bnxt_rdma_aux_device_init(bp);
16294 	rc = bnxt_set_dflt_rings(bp, true);
16295 	if (rc) {
16296 		if (BNXT_VF(bp) && rc == -ENODEV) {
16297 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16298 		} else {
16299 			netdev_err(bp->dev, "Not enough rings available.\n");
16300 			rc = -ENOMEM;
16301 		}
16302 		goto init_err_pci_clean;
16303 	}
16304 
16305 	bnxt_fw_init_one_p3(bp);
16306 
16307 	bnxt_init_dflt_coal(bp);
16308 
16309 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16310 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16311 
16312 	rc = bnxt_init_int_mode(bp);
16313 	if (rc)
16314 		goto init_err_pci_clean;
16315 
16316 	/* No TC has been set yet and rings may have been trimmed due to
16317 	 * limited MSIX, so we re-initialize the TX rings per TC.
16318 	 */
16319 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16320 
16321 	if (BNXT_PF(bp)) {
16322 		if (!bnxt_pf_wq) {
16323 			bnxt_pf_wq =
16324 				create_singlethread_workqueue("bnxt_pf_wq");
16325 			if (!bnxt_pf_wq) {
16326 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16327 				rc = -ENOMEM;
16328 				goto init_err_pci_clean;
16329 			}
16330 		}
16331 		rc = bnxt_init_tc(bp);
16332 		if (rc)
16333 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16334 				   rc);
16335 	}
16336 
16337 	bnxt_inv_fw_health_reg(bp);
16338 	rc = bnxt_dl_register(bp);
16339 	if (rc)
16340 		goto init_err_dl;
16341 
16342 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16343 
16344 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16345 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16346 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16347 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16348 
16349 	rc = register_netdev(dev);
16350 	if (rc)
16351 		goto init_err_cleanup;
16352 
16353 	bnxt_dl_fw_reporters_create(bp);
16354 
16355 	bnxt_rdma_aux_device_add(bp);
16356 
16357 	bnxt_print_device_info(bp);
16358 
16359 	pci_save_state(pdev);
16360 
16361 	return 0;
16362 init_err_cleanup:
16363 	bnxt_rdma_aux_device_uninit(bp);
16364 	bnxt_dl_unregister(bp);
16365 init_err_dl:
16366 	bnxt_shutdown_tc(bp);
16367 	bnxt_clear_int_mode(bp);
16368 
16369 init_err_pci_clean:
16370 	bnxt_hwrm_func_drv_unrgtr(bp);
16371 	bnxt_free_hwrm_resources(bp);
16372 	bnxt_hwmon_uninit(bp);
16373 	bnxt_ethtool_free(bp);
16374 	bnxt_ptp_clear(bp);
16375 	kfree(bp->ptp_cfg);
16376 	bp->ptp_cfg = NULL;
16377 	kfree(bp->fw_health);
16378 	bp->fw_health = NULL;
16379 	bnxt_cleanup_pci(bp);
16380 	bnxt_free_ctx_mem(bp, true);
16381 	bnxt_free_crash_dump_mem(bp);
16382 	kfree(bp->rss_indir_tbl);
16383 	bp->rss_indir_tbl = NULL;
16384 
16385 init_err_free:
16386 	free_netdev(dev);
16387 	return rc;
16388 }
16389 
16390 static void bnxt_shutdown(struct pci_dev *pdev)
16391 {
16392 	struct net_device *dev = pci_get_drvdata(pdev);
16393 	struct bnxt *bp;
16394 
16395 	if (!dev)
16396 		return;
16397 
16398 	rtnl_lock();
16399 	bp = netdev_priv(dev);
16400 	if (!bp)
16401 		goto shutdown_exit;
16402 
16403 	if (netif_running(dev))
16404 		dev_close(dev);
16405 
16406 	bnxt_ptp_clear(bp);
16407 	bnxt_clear_int_mode(bp);
16408 	pci_disable_device(pdev);
16409 
16410 	if (system_state == SYSTEM_POWER_OFF) {
16411 		pci_wake_from_d3(pdev, bp->wol);
16412 		pci_set_power_state(pdev, PCI_D3hot);
16413 	}
16414 
16415 shutdown_exit:
16416 	rtnl_unlock();
16417 }
16418 
16419 #ifdef CONFIG_PM_SLEEP
16420 static int bnxt_suspend(struct device *device)
16421 {
16422 	struct net_device *dev = dev_get_drvdata(device);
16423 	struct bnxt *bp = netdev_priv(dev);
16424 	int rc = 0;
16425 
16426 	bnxt_ulp_stop(bp);
16427 
16428 	rtnl_lock();
16429 	if (netif_running(dev)) {
16430 		netif_device_detach(dev);
16431 		rc = bnxt_close(dev);
16432 	}
16433 	bnxt_hwrm_func_drv_unrgtr(bp);
16434 	bnxt_ptp_clear(bp);
16435 	pci_disable_device(bp->pdev);
16436 	bnxt_free_ctx_mem(bp, false);
16437 	rtnl_unlock();
16438 	return rc;
16439 }
16440 
16441 static int bnxt_resume(struct device *device)
16442 {
16443 	struct net_device *dev = dev_get_drvdata(device);
16444 	struct bnxt *bp = netdev_priv(dev);
16445 	int rc = 0;
16446 
16447 	rtnl_lock();
16448 	rc = pci_enable_device(bp->pdev);
16449 	if (rc) {
16450 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16451 			   rc);
16452 		goto resume_exit;
16453 	}
16454 	pci_set_master(bp->pdev);
16455 	if (bnxt_hwrm_ver_get(bp)) {
16456 		rc = -ENODEV;
16457 		goto resume_exit;
16458 	}
16459 	rc = bnxt_hwrm_func_reset(bp);
16460 	if (rc) {
16461 		rc = -EBUSY;
16462 		goto resume_exit;
16463 	}
16464 
16465 	rc = bnxt_hwrm_func_qcaps(bp);
16466 	if (rc)
16467 		goto resume_exit;
16468 
16469 	bnxt_clear_reservations(bp, true);
16470 
16471 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16472 		rc = -ENODEV;
16473 		goto resume_exit;
16474 	}
16475 	if (bp->fw_crash_mem)
16476 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16477 
16478 	if (bnxt_ptp_init(bp)) {
16479 		kfree(bp->ptp_cfg);
16480 		bp->ptp_cfg = NULL;
16481 	}
16482 	bnxt_get_wol_settings(bp);
16483 	if (netif_running(dev)) {
16484 		rc = bnxt_open(dev);
16485 		if (!rc)
16486 			netif_device_attach(dev);
16487 	}
16488 
16489 resume_exit:
16490 	rtnl_unlock();
16491 	bnxt_ulp_start(bp, rc);
16492 	if (!rc)
16493 		bnxt_reenable_sriov(bp);
16494 	return rc;
16495 }
16496 
16497 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16498 #define BNXT_PM_OPS (&bnxt_pm_ops)
16499 
16500 #else
16501 
16502 #define BNXT_PM_OPS NULL
16503 
16504 #endif /* CONFIG_PM_SLEEP */
16505 
16506 /**
16507  * bnxt_io_error_detected - called when PCI error is detected
16508  * @pdev: Pointer to PCI device
16509  * @state: The current pci connection state
16510  *
16511  * This function is called after a PCI bus error affecting
16512  * this device has been detected.
16513  */
16514 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16515 					       pci_channel_state_t state)
16516 {
16517 	struct net_device *netdev = pci_get_drvdata(pdev);
16518 	struct bnxt *bp = netdev_priv(netdev);
16519 	bool abort = false;
16520 
16521 	netdev_info(netdev, "PCI I/O error detected\n");
16522 
16523 	bnxt_ulp_stop(bp);
16524 
16525 	rtnl_lock();
16526 	netif_device_detach(netdev);
16527 
16528 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16529 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16530 		abort = true;
16531 	}
16532 
16533 	if (abort || state == pci_channel_io_perm_failure) {
16534 		rtnl_unlock();
16535 		return PCI_ERS_RESULT_DISCONNECT;
16536 	}
16537 
16538 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16539 	 * so we disable bus master to prevent any potential bad DMAs before
16540 	 * freeing kernel memory.
16541 	 */
16542 	if (state == pci_channel_io_frozen) {
16543 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16544 		bnxt_fw_fatal_close(bp);
16545 	}
16546 
16547 	if (netif_running(netdev))
16548 		__bnxt_close_nic(bp, true, true);
16549 
16550 	if (pci_is_enabled(pdev))
16551 		pci_disable_device(pdev);
16552 	bnxt_free_ctx_mem(bp, false);
16553 	rtnl_unlock();
16554 
16555 	/* Request a slot slot reset. */
16556 	return PCI_ERS_RESULT_NEED_RESET;
16557 }
16558 
16559 /**
16560  * bnxt_io_slot_reset - called after the pci bus has been reset.
16561  * @pdev: Pointer to PCI device
16562  *
16563  * Restart the card from scratch, as if from a cold-boot.
16564  * At this point, the card has experienced a hard reset,
16565  * followed by fixups by BIOS, and has its config space
16566  * set up identically to what it was at cold boot.
16567  */
16568 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16569 {
16570 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16571 	struct net_device *netdev = pci_get_drvdata(pdev);
16572 	struct bnxt *bp = netdev_priv(netdev);
16573 	int retry = 0;
16574 	int err = 0;
16575 	int off;
16576 
16577 	netdev_info(bp->dev, "PCI Slot Reset\n");
16578 
16579 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16580 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16581 		msleep(900);
16582 
16583 	rtnl_lock();
16584 
16585 	if (pci_enable_device(pdev)) {
16586 		dev_err(&pdev->dev,
16587 			"Cannot re-enable PCI device after reset.\n");
16588 	} else {
16589 		pci_set_master(pdev);
16590 		/* Upon fatal error, our device internal logic that latches to
16591 		 * BAR value is getting reset and will restore only upon
16592 		 * rewriting the BARs.
16593 		 *
16594 		 * As pci_restore_state() does not re-write the BARs if the
16595 		 * value is same as saved value earlier, driver needs to
16596 		 * write the BARs to 0 to force restore, in case of fatal error.
16597 		 */
16598 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16599 				       &bp->state)) {
16600 			for (off = PCI_BASE_ADDRESS_0;
16601 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16602 				pci_write_config_dword(bp->pdev, off, 0);
16603 		}
16604 		pci_restore_state(pdev);
16605 		pci_save_state(pdev);
16606 
16607 		bnxt_inv_fw_health_reg(bp);
16608 		bnxt_try_map_fw_health_reg(bp);
16609 
16610 		/* In some PCIe AER scenarios, firmware may take up to
16611 		 * 10 seconds to become ready in the worst case.
16612 		 */
16613 		do {
16614 			err = bnxt_try_recover_fw(bp);
16615 			if (!err)
16616 				break;
16617 			retry++;
16618 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16619 
16620 		if (err) {
16621 			dev_err(&pdev->dev, "Firmware not ready\n");
16622 			goto reset_exit;
16623 		}
16624 
16625 		err = bnxt_hwrm_func_reset(bp);
16626 		if (!err)
16627 			result = PCI_ERS_RESULT_RECOVERED;
16628 
16629 		bnxt_ulp_irq_stop(bp);
16630 		bnxt_clear_int_mode(bp);
16631 		err = bnxt_init_int_mode(bp);
16632 		bnxt_ulp_irq_restart(bp, err);
16633 	}
16634 
16635 reset_exit:
16636 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16637 	bnxt_clear_reservations(bp, true);
16638 	rtnl_unlock();
16639 
16640 	return result;
16641 }
16642 
16643 /**
16644  * bnxt_io_resume - called when traffic can start flowing again.
16645  * @pdev: Pointer to PCI device
16646  *
16647  * This callback is called when the error recovery driver tells
16648  * us that its OK to resume normal operation.
16649  */
16650 static void bnxt_io_resume(struct pci_dev *pdev)
16651 {
16652 	struct net_device *netdev = pci_get_drvdata(pdev);
16653 	struct bnxt *bp = netdev_priv(netdev);
16654 	int err;
16655 
16656 	netdev_info(bp->dev, "PCI Slot Resume\n");
16657 	rtnl_lock();
16658 
16659 	err = bnxt_hwrm_func_qcaps(bp);
16660 	if (!err) {
16661 		if (netif_running(netdev))
16662 			err = bnxt_open(netdev);
16663 		else
16664 			err = bnxt_reserve_rings(bp, true);
16665 	}
16666 
16667 	if (!err)
16668 		netif_device_attach(netdev);
16669 
16670 	rtnl_unlock();
16671 	bnxt_ulp_start(bp, err);
16672 	if (!err)
16673 		bnxt_reenable_sriov(bp);
16674 }
16675 
16676 static const struct pci_error_handlers bnxt_err_handler = {
16677 	.error_detected	= bnxt_io_error_detected,
16678 	.slot_reset	= bnxt_io_slot_reset,
16679 	.resume		= bnxt_io_resume
16680 };
16681 
16682 static struct pci_driver bnxt_pci_driver = {
16683 	.name		= DRV_MODULE_NAME,
16684 	.id_table	= bnxt_pci_tbl,
16685 	.probe		= bnxt_init_one,
16686 	.remove		= bnxt_remove_one,
16687 	.shutdown	= bnxt_shutdown,
16688 	.driver.pm	= BNXT_PM_OPS,
16689 	.err_handler	= &bnxt_err_handler,
16690 #if defined(CONFIG_BNXT_SRIOV)
16691 	.sriov_configure = bnxt_sriov_configure,
16692 #endif
16693 };
16694 
16695 static int __init bnxt_init(void)
16696 {
16697 	int err;
16698 
16699 	bnxt_debug_init();
16700 	err = pci_register_driver(&bnxt_pci_driver);
16701 	if (err) {
16702 		bnxt_debug_exit();
16703 		return err;
16704 	}
16705 
16706 	return 0;
16707 }
16708 
16709 static void __exit bnxt_exit(void)
16710 {
16711 	pci_unregister_driver(&bnxt_pci_driver);
16712 	if (bnxt_pf_wq)
16713 		destroy_workqueue(bnxt_pf_wq);
16714 	bnxt_debug_exit();
16715 }
16716 
16717 module_init(bnxt_init);
16718 module_exit(bnxt_exit);
16719