xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision ebda2f0bbde540ff7da168d2837f8cfb14581e2e)
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 	return 0;
2858 }
2859 
2860 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2861 {
2862 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2863 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2864 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2865 				(struct hwrm_fwd_req_cmpl *)txcmp;
2866 
2867 	switch (cmpl_type) {
2868 	case CMPL_BASE_TYPE_HWRM_DONE:
2869 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2870 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2871 		break;
2872 
2873 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2874 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2875 
2876 		if ((vf_id < bp->pf.first_vf_id) ||
2877 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2878 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2879 				   vf_id);
2880 			return -EINVAL;
2881 		}
2882 
2883 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2884 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2885 		break;
2886 
2887 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2888 		bnxt_async_event_process(bp,
2889 					 (struct hwrm_async_event_cmpl *)txcmp);
2890 		break;
2891 
2892 	default:
2893 		break;
2894 	}
2895 
2896 	return 0;
2897 }
2898 
2899 static bool bnxt_vnic_is_active(struct bnxt *bp)
2900 {
2901 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2902 
2903 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2904 }
2905 
2906 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2907 {
2908 	struct bnxt_napi *bnapi = dev_instance;
2909 	struct bnxt *bp = bnapi->bp;
2910 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2911 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2912 
2913 	cpr->event_ctr++;
2914 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2915 	napi_schedule(&bnapi->napi);
2916 	return IRQ_HANDLED;
2917 }
2918 
2919 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2920 {
2921 	u32 raw_cons = cpr->cp_raw_cons;
2922 	u16 cons = RING_CMP(raw_cons);
2923 	struct tx_cmp *txcmp;
2924 
2925 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2926 
2927 	return TX_CMP_VALID(txcmp, raw_cons);
2928 }
2929 
2930 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2931 			    int budget)
2932 {
2933 	struct bnxt_napi *bnapi = cpr->bnapi;
2934 	u32 raw_cons = cpr->cp_raw_cons;
2935 	u32 cons;
2936 	int rx_pkts = 0;
2937 	u8 event = 0;
2938 	struct tx_cmp *txcmp;
2939 
2940 	cpr->has_more_work = 0;
2941 	cpr->had_work_done = 1;
2942 	while (1) {
2943 		u8 cmp_type;
2944 		int rc;
2945 
2946 		cons = RING_CMP(raw_cons);
2947 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2948 
2949 		if (!TX_CMP_VALID(txcmp, raw_cons))
2950 			break;
2951 
2952 		/* The valid test of the entry must be done first before
2953 		 * reading any further.
2954 		 */
2955 		dma_rmb();
2956 		cmp_type = TX_CMP_TYPE(txcmp);
2957 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2958 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2959 			u32 opaque = txcmp->tx_cmp_opaque;
2960 			struct bnxt_tx_ring_info *txr;
2961 			u16 tx_freed;
2962 
2963 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2964 			event |= BNXT_TX_CMP_EVENT;
2965 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2966 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2967 			else
2968 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2969 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2970 				   bp->tx_ring_mask;
2971 			/* return full budget so NAPI will complete. */
2972 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2973 				rx_pkts = budget;
2974 				raw_cons = NEXT_RAW_CMP(raw_cons);
2975 				if (budget)
2976 					cpr->has_more_work = 1;
2977 				break;
2978 			}
2979 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2980 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2981 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2982 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2983 			if (likely(budget))
2984 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2985 			else
2986 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2987 							   &event);
2988 			if (likely(rc >= 0))
2989 				rx_pkts += rc;
2990 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2991 			 * the NAPI budget.  Otherwise, we may potentially loop
2992 			 * here forever if we consistently cannot allocate
2993 			 * buffers.
2994 			 */
2995 			else if (rc == -ENOMEM && budget)
2996 				rx_pkts++;
2997 			else if (rc == -EBUSY)	/* partial completion */
2998 				break;
2999 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3000 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3001 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3002 			bnxt_hwrm_handler(bp, txcmp);
3003 		}
3004 		raw_cons = NEXT_RAW_CMP(raw_cons);
3005 
3006 		if (rx_pkts && rx_pkts == budget) {
3007 			cpr->has_more_work = 1;
3008 			break;
3009 		}
3010 	}
3011 
3012 	if (event & BNXT_REDIRECT_EVENT) {
3013 		xdp_do_flush();
3014 		event &= ~BNXT_REDIRECT_EVENT;
3015 	}
3016 
3017 	if (event & BNXT_TX_EVENT) {
3018 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3019 		u16 prod = txr->tx_prod;
3020 
3021 		/* Sync BD data before updating doorbell */
3022 		wmb();
3023 
3024 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3025 		event &= ~BNXT_TX_EVENT;
3026 	}
3027 
3028 	cpr->cp_raw_cons = raw_cons;
3029 	bnapi->events |= event;
3030 	return rx_pkts;
3031 }
3032 
3033 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3034 				  int budget)
3035 {
3036 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3037 		bnapi->tx_int(bp, bnapi, budget);
3038 
3039 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3040 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3041 
3042 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3043 		bnapi->events &= ~BNXT_RX_EVENT;
3044 	}
3045 	if (bnapi->events & BNXT_AGG_EVENT) {
3046 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3047 
3048 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3049 		bnapi->events &= ~BNXT_AGG_EVENT;
3050 	}
3051 }
3052 
3053 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3054 			  int budget)
3055 {
3056 	struct bnxt_napi *bnapi = cpr->bnapi;
3057 	int rx_pkts;
3058 
3059 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3060 
3061 	/* ACK completion ring before freeing tx ring and producing new
3062 	 * buffers in rx/agg rings to prevent overflowing the completion
3063 	 * ring.
3064 	 */
3065 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3066 
3067 	__bnxt_poll_work_done(bp, bnapi, budget);
3068 	return rx_pkts;
3069 }
3070 
3071 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3072 {
3073 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3074 	struct bnxt *bp = bnapi->bp;
3075 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3076 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3077 	struct tx_cmp *txcmp;
3078 	struct rx_cmp_ext *rxcmp1;
3079 	u32 cp_cons, tmp_raw_cons;
3080 	u32 raw_cons = cpr->cp_raw_cons;
3081 	bool flush_xdp = false;
3082 	u32 rx_pkts = 0;
3083 	u8 event = 0;
3084 
3085 	while (1) {
3086 		int rc;
3087 
3088 		cp_cons = RING_CMP(raw_cons);
3089 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3090 
3091 		if (!TX_CMP_VALID(txcmp, raw_cons))
3092 			break;
3093 
3094 		/* The valid test of the entry must be done first before
3095 		 * reading any further.
3096 		 */
3097 		dma_rmb();
3098 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3099 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3100 			cp_cons = RING_CMP(tmp_raw_cons);
3101 			rxcmp1 = (struct rx_cmp_ext *)
3102 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3103 
3104 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3105 				break;
3106 
3107 			/* force an error to recycle the buffer */
3108 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3109 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3110 
3111 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3112 			if (likely(rc == -EIO) && budget)
3113 				rx_pkts++;
3114 			else if (rc == -EBUSY)	/* partial completion */
3115 				break;
3116 			if (event & BNXT_REDIRECT_EVENT)
3117 				flush_xdp = true;
3118 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3119 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3120 			bnxt_hwrm_handler(bp, txcmp);
3121 		} else {
3122 			netdev_err(bp->dev,
3123 				   "Invalid completion received on special ring\n");
3124 		}
3125 		raw_cons = NEXT_RAW_CMP(raw_cons);
3126 
3127 		if (rx_pkts == budget)
3128 			break;
3129 	}
3130 
3131 	cpr->cp_raw_cons = raw_cons;
3132 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3133 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3134 
3135 	if (event & BNXT_AGG_EVENT)
3136 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3137 	if (flush_xdp)
3138 		xdp_do_flush();
3139 
3140 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3141 		napi_complete_done(napi, rx_pkts);
3142 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3143 	}
3144 	return rx_pkts;
3145 }
3146 
3147 static int bnxt_poll(struct napi_struct *napi, int budget)
3148 {
3149 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3150 	struct bnxt *bp = bnapi->bp;
3151 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3152 	int work_done = 0;
3153 
3154 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3155 		napi_complete(napi);
3156 		return 0;
3157 	}
3158 	while (1) {
3159 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3160 
3161 		if (work_done >= budget) {
3162 			if (!budget)
3163 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3164 			break;
3165 		}
3166 
3167 		if (!bnxt_has_work(bp, cpr)) {
3168 			if (napi_complete_done(napi, work_done))
3169 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3170 			break;
3171 		}
3172 	}
3173 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3174 		struct dim_sample dim_sample = {};
3175 
3176 		dim_update_sample(cpr->event_ctr,
3177 				  cpr->rx_packets,
3178 				  cpr->rx_bytes,
3179 				  &dim_sample);
3180 		net_dim(&cpr->dim, &dim_sample);
3181 	}
3182 	return work_done;
3183 }
3184 
3185 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3186 {
3187 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3188 	int i, work_done = 0;
3189 
3190 	for (i = 0; i < cpr->cp_ring_count; i++) {
3191 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3192 
3193 		if (cpr2->had_nqe_notify) {
3194 			work_done += __bnxt_poll_work(bp, cpr2,
3195 						      budget - work_done);
3196 			cpr->has_more_work |= cpr2->has_more_work;
3197 		}
3198 	}
3199 	return work_done;
3200 }
3201 
3202 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3203 				 u64 dbr_type, int budget)
3204 {
3205 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3206 	int i;
3207 
3208 	for (i = 0; i < cpr->cp_ring_count; i++) {
3209 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3210 		struct bnxt_db_info *db;
3211 
3212 		if (cpr2->had_work_done) {
3213 			u32 tgl = 0;
3214 
3215 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3216 				cpr2->had_nqe_notify = 0;
3217 				tgl = cpr2->toggle;
3218 			}
3219 			db = &cpr2->cp_db;
3220 			bnxt_writeq(bp,
3221 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3222 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3223 				    db->doorbell);
3224 			cpr2->had_work_done = 0;
3225 		}
3226 	}
3227 	__bnxt_poll_work_done(bp, bnapi, budget);
3228 }
3229 
3230 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3231 {
3232 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3233 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3234 	struct bnxt_cp_ring_info *cpr_rx;
3235 	u32 raw_cons = cpr->cp_raw_cons;
3236 	struct bnxt *bp = bnapi->bp;
3237 	struct nqe_cn *nqcmp;
3238 	int work_done = 0;
3239 	u32 cons;
3240 
3241 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3242 		napi_complete(napi);
3243 		return 0;
3244 	}
3245 	if (cpr->has_more_work) {
3246 		cpr->has_more_work = 0;
3247 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3248 	}
3249 	while (1) {
3250 		u16 type;
3251 
3252 		cons = RING_CMP(raw_cons);
3253 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3254 
3255 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3256 			if (cpr->has_more_work)
3257 				break;
3258 
3259 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3260 					     budget);
3261 			cpr->cp_raw_cons = raw_cons;
3262 			if (napi_complete_done(napi, work_done))
3263 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3264 						  cpr->cp_raw_cons);
3265 			goto poll_done;
3266 		}
3267 
3268 		/* The valid test of the entry must be done first before
3269 		 * reading any further.
3270 		 */
3271 		dma_rmb();
3272 
3273 		type = le16_to_cpu(nqcmp->type);
3274 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3275 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3276 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3277 			struct bnxt_cp_ring_info *cpr2;
3278 
3279 			/* No more budget for RX work */
3280 			if (budget && work_done >= budget &&
3281 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3282 				break;
3283 
3284 			idx = BNXT_NQ_HDL_IDX(idx);
3285 			cpr2 = &cpr->cp_ring_arr[idx];
3286 			cpr2->had_nqe_notify = 1;
3287 			cpr2->toggle = NQE_CN_TOGGLE(type);
3288 			work_done += __bnxt_poll_work(bp, cpr2,
3289 						      budget - work_done);
3290 			cpr->has_more_work |= cpr2->has_more_work;
3291 		} else {
3292 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3293 		}
3294 		raw_cons = NEXT_RAW_CMP(raw_cons);
3295 	}
3296 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3297 	if (raw_cons != cpr->cp_raw_cons) {
3298 		cpr->cp_raw_cons = raw_cons;
3299 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3300 	}
3301 poll_done:
3302 	cpr_rx = &cpr->cp_ring_arr[0];
3303 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3304 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3305 		struct dim_sample dim_sample = {};
3306 
3307 		dim_update_sample(cpr->event_ctr,
3308 				  cpr_rx->rx_packets,
3309 				  cpr_rx->rx_bytes,
3310 				  &dim_sample);
3311 		net_dim(&cpr->dim, &dim_sample);
3312 	}
3313 	return work_done;
3314 }
3315 
3316 static void bnxt_free_tx_skbs(struct bnxt *bp)
3317 {
3318 	int i, max_idx;
3319 	struct pci_dev *pdev = bp->pdev;
3320 
3321 	if (!bp->tx_ring)
3322 		return;
3323 
3324 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3325 	for (i = 0; i < bp->tx_nr_rings; i++) {
3326 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3327 		int j;
3328 
3329 		if (!txr->tx_buf_ring)
3330 			continue;
3331 
3332 		for (j = 0; j < max_idx;) {
3333 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3334 			struct sk_buff *skb;
3335 			int k, last;
3336 
3337 			if (i < bp->tx_nr_rings_xdp &&
3338 			    tx_buf->action == XDP_REDIRECT) {
3339 				dma_unmap_single(&pdev->dev,
3340 					dma_unmap_addr(tx_buf, mapping),
3341 					dma_unmap_len(tx_buf, len),
3342 					DMA_TO_DEVICE);
3343 				xdp_return_frame(tx_buf->xdpf);
3344 				tx_buf->action = 0;
3345 				tx_buf->xdpf = NULL;
3346 				j++;
3347 				continue;
3348 			}
3349 
3350 			skb = tx_buf->skb;
3351 			if (!skb) {
3352 				j++;
3353 				continue;
3354 			}
3355 
3356 			tx_buf->skb = NULL;
3357 
3358 			if (tx_buf->is_push) {
3359 				dev_kfree_skb(skb);
3360 				j += 2;
3361 				continue;
3362 			}
3363 
3364 			dma_unmap_single(&pdev->dev,
3365 					 dma_unmap_addr(tx_buf, mapping),
3366 					 skb_headlen(skb),
3367 					 DMA_TO_DEVICE);
3368 
3369 			last = tx_buf->nr_frags;
3370 			j += 2;
3371 			for (k = 0; k < last; k++, j++) {
3372 				int ring_idx = j & bp->tx_ring_mask;
3373 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3374 
3375 				tx_buf = &txr->tx_buf_ring[ring_idx];
3376 				dma_unmap_page(
3377 					&pdev->dev,
3378 					dma_unmap_addr(tx_buf, mapping),
3379 					skb_frag_size(frag), DMA_TO_DEVICE);
3380 			}
3381 			dev_kfree_skb(skb);
3382 		}
3383 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3384 	}
3385 }
3386 
3387 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3388 {
3389 	int i, max_idx;
3390 
3391 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3392 
3393 	for (i = 0; i < max_idx; i++) {
3394 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3395 		void *data = rx_buf->data;
3396 
3397 		if (!data)
3398 			continue;
3399 
3400 		rx_buf->data = NULL;
3401 		if (BNXT_RX_PAGE_MODE(bp))
3402 			page_pool_recycle_direct(rxr->page_pool, data);
3403 		else
3404 			page_pool_free_va(rxr->head_pool, data, true);
3405 	}
3406 }
3407 
3408 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3409 {
3410 	int i, max_idx;
3411 
3412 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3413 
3414 	for (i = 0; i < max_idx; i++) {
3415 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3416 		struct page *page = rx_agg_buf->page;
3417 
3418 		if (!page)
3419 			continue;
3420 
3421 		rx_agg_buf->page = NULL;
3422 		__clear_bit(i, rxr->rx_agg_bmap);
3423 
3424 		page_pool_recycle_direct(rxr->page_pool, page);
3425 	}
3426 }
3427 
3428 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3429 					struct bnxt_rx_ring_info *rxr)
3430 {
3431 	int i;
3432 
3433 	for (i = 0; i < bp->max_tpa; i++) {
3434 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3435 		u8 *data = tpa_info->data;
3436 
3437 		if (!data)
3438 			continue;
3439 
3440 		tpa_info->data = NULL;
3441 		page_pool_free_va(rxr->head_pool, data, false);
3442 	}
3443 }
3444 
3445 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3446 				       struct bnxt_rx_ring_info *rxr)
3447 {
3448 	struct bnxt_tpa_idx_map *map;
3449 
3450 	if (!rxr->rx_tpa)
3451 		goto skip_rx_tpa_free;
3452 
3453 	bnxt_free_one_tpa_info_data(bp, rxr);
3454 
3455 skip_rx_tpa_free:
3456 	if (!rxr->rx_buf_ring)
3457 		goto skip_rx_buf_free;
3458 
3459 	bnxt_free_one_rx_ring(bp, rxr);
3460 
3461 skip_rx_buf_free:
3462 	if (!rxr->rx_agg_ring)
3463 		goto skip_rx_agg_free;
3464 
3465 	bnxt_free_one_rx_agg_ring(bp, rxr);
3466 
3467 skip_rx_agg_free:
3468 	map = rxr->rx_tpa_idx_map;
3469 	if (map)
3470 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3471 }
3472 
3473 static void bnxt_free_rx_skbs(struct bnxt *bp)
3474 {
3475 	int i;
3476 
3477 	if (!bp->rx_ring)
3478 		return;
3479 
3480 	for (i = 0; i < bp->rx_nr_rings; i++)
3481 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3482 }
3483 
3484 static void bnxt_free_skbs(struct bnxt *bp)
3485 {
3486 	bnxt_free_tx_skbs(bp);
3487 	bnxt_free_rx_skbs(bp);
3488 }
3489 
3490 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3491 {
3492 	u8 init_val = ctxm->init_value;
3493 	u16 offset = ctxm->init_offset;
3494 	u8 *p2 = p;
3495 	int i;
3496 
3497 	if (!init_val)
3498 		return;
3499 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3500 		memset(p, init_val, len);
3501 		return;
3502 	}
3503 	for (i = 0; i < len; i += ctxm->entry_size)
3504 		*(p2 + i + offset) = init_val;
3505 }
3506 
3507 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3508 			       void *buf, size_t offset, size_t head,
3509 			       size_t tail)
3510 {
3511 	int i, head_page, start_idx, source_offset;
3512 	size_t len, rem_len, total_len, max_bytes;
3513 
3514 	head_page = head / rmem->page_size;
3515 	source_offset = head % rmem->page_size;
3516 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3517 	if (!total_len)
3518 		total_len = MAX_CTX_BYTES;
3519 	start_idx = head_page % MAX_CTX_PAGES;
3520 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3521 		    source_offset;
3522 	total_len = min(total_len, max_bytes);
3523 	rem_len = total_len;
3524 
3525 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3526 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3527 		if (buf)
3528 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3529 			       len);
3530 		offset += len;
3531 		rem_len -= len;
3532 	}
3533 	return total_len;
3534 }
3535 
3536 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3537 {
3538 	struct pci_dev *pdev = bp->pdev;
3539 	int i;
3540 
3541 	if (!rmem->pg_arr)
3542 		goto skip_pages;
3543 
3544 	for (i = 0; i < rmem->nr_pages; i++) {
3545 		if (!rmem->pg_arr[i])
3546 			continue;
3547 
3548 		dma_free_coherent(&pdev->dev, rmem->page_size,
3549 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3550 
3551 		rmem->pg_arr[i] = NULL;
3552 	}
3553 skip_pages:
3554 	if (rmem->pg_tbl) {
3555 		size_t pg_tbl_size = rmem->nr_pages * 8;
3556 
3557 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3558 			pg_tbl_size = rmem->page_size;
3559 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3560 				  rmem->pg_tbl, rmem->pg_tbl_map);
3561 		rmem->pg_tbl = NULL;
3562 	}
3563 	if (rmem->vmem_size && *rmem->vmem) {
3564 		vfree(*rmem->vmem);
3565 		*rmem->vmem = NULL;
3566 	}
3567 }
3568 
3569 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3570 {
3571 	struct pci_dev *pdev = bp->pdev;
3572 	u64 valid_bit = 0;
3573 	int i;
3574 
3575 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3576 		valid_bit = PTU_PTE_VALID;
3577 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3578 		size_t pg_tbl_size = rmem->nr_pages * 8;
3579 
3580 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3581 			pg_tbl_size = rmem->page_size;
3582 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3583 						  &rmem->pg_tbl_map,
3584 						  GFP_KERNEL);
3585 		if (!rmem->pg_tbl)
3586 			return -ENOMEM;
3587 	}
3588 
3589 	for (i = 0; i < rmem->nr_pages; i++) {
3590 		u64 extra_bits = valid_bit;
3591 
3592 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3593 						     rmem->page_size,
3594 						     &rmem->dma_arr[i],
3595 						     GFP_KERNEL);
3596 		if (!rmem->pg_arr[i])
3597 			return -ENOMEM;
3598 
3599 		if (rmem->ctx_mem)
3600 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3601 					  rmem->page_size);
3602 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3603 			if (i == rmem->nr_pages - 2 &&
3604 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3605 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3606 			else if (i == rmem->nr_pages - 1 &&
3607 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3608 				extra_bits |= PTU_PTE_LAST;
3609 			rmem->pg_tbl[i] =
3610 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3611 		}
3612 	}
3613 
3614 	if (rmem->vmem_size) {
3615 		*rmem->vmem = vzalloc(rmem->vmem_size);
3616 		if (!(*rmem->vmem))
3617 			return -ENOMEM;
3618 	}
3619 	return 0;
3620 }
3621 
3622 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3623 				   struct bnxt_rx_ring_info *rxr)
3624 {
3625 	int i;
3626 
3627 	kfree(rxr->rx_tpa_idx_map);
3628 	rxr->rx_tpa_idx_map = NULL;
3629 	if (rxr->rx_tpa) {
3630 		for (i = 0; i < bp->max_tpa; i++) {
3631 			kfree(rxr->rx_tpa[i].agg_arr);
3632 			rxr->rx_tpa[i].agg_arr = NULL;
3633 		}
3634 	}
3635 	kfree(rxr->rx_tpa);
3636 	rxr->rx_tpa = NULL;
3637 }
3638 
3639 static void bnxt_free_tpa_info(struct bnxt *bp)
3640 {
3641 	int i;
3642 
3643 	for (i = 0; i < bp->rx_nr_rings; i++) {
3644 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3645 
3646 		bnxt_free_one_tpa_info(bp, rxr);
3647 	}
3648 }
3649 
3650 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3651 				   struct bnxt_rx_ring_info *rxr)
3652 {
3653 	struct rx_agg_cmp *agg;
3654 	int i;
3655 
3656 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3657 			      GFP_KERNEL);
3658 	if (!rxr->rx_tpa)
3659 		return -ENOMEM;
3660 
3661 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3662 		return 0;
3663 	for (i = 0; i < bp->max_tpa; i++) {
3664 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3665 		if (!agg)
3666 			return -ENOMEM;
3667 		rxr->rx_tpa[i].agg_arr = agg;
3668 	}
3669 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3670 				      GFP_KERNEL);
3671 	if (!rxr->rx_tpa_idx_map)
3672 		return -ENOMEM;
3673 
3674 	return 0;
3675 }
3676 
3677 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3678 {
3679 	int i, rc;
3680 
3681 	bp->max_tpa = MAX_TPA;
3682 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3683 		if (!bp->max_tpa_v2)
3684 			return 0;
3685 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3686 	}
3687 
3688 	for (i = 0; i < bp->rx_nr_rings; i++) {
3689 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3690 
3691 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3692 		if (rc)
3693 			return rc;
3694 	}
3695 	return 0;
3696 }
3697 
3698 static void bnxt_free_rx_rings(struct bnxt *bp)
3699 {
3700 	int i;
3701 
3702 	if (!bp->rx_ring)
3703 		return;
3704 
3705 	bnxt_free_tpa_info(bp);
3706 	for (i = 0; i < bp->rx_nr_rings; i++) {
3707 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3708 		struct bnxt_ring_struct *ring;
3709 
3710 		if (rxr->xdp_prog)
3711 			bpf_prog_put(rxr->xdp_prog);
3712 
3713 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3714 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3715 
3716 		page_pool_destroy(rxr->page_pool);
3717 		if (bnxt_separate_head_pool())
3718 			page_pool_destroy(rxr->head_pool);
3719 		rxr->page_pool = rxr->head_pool = NULL;
3720 
3721 		kfree(rxr->rx_agg_bmap);
3722 		rxr->rx_agg_bmap = NULL;
3723 
3724 		ring = &rxr->rx_ring_struct;
3725 		bnxt_free_ring(bp, &ring->ring_mem);
3726 
3727 		ring = &rxr->rx_agg_ring_struct;
3728 		bnxt_free_ring(bp, &ring->ring_mem);
3729 	}
3730 }
3731 
3732 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3733 				   struct bnxt_rx_ring_info *rxr,
3734 				   int numa_node)
3735 {
3736 	struct page_pool_params pp = { 0 };
3737 	struct page_pool *pool;
3738 
3739 	pp.pool_size = bp->rx_agg_ring_size;
3740 	if (BNXT_RX_PAGE_MODE(bp))
3741 		pp.pool_size += bp->rx_ring_size;
3742 	pp.nid = numa_node;
3743 	pp.napi = &rxr->bnapi->napi;
3744 	pp.netdev = bp->dev;
3745 	pp.dev = &bp->pdev->dev;
3746 	pp.dma_dir = bp->rx_dir;
3747 	pp.max_len = PAGE_SIZE;
3748 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3749 
3750 	pool = page_pool_create(&pp);
3751 	if (IS_ERR(pool))
3752 		return PTR_ERR(pool);
3753 	rxr->page_pool = pool;
3754 
3755 	if (bnxt_separate_head_pool()) {
3756 		pp.pool_size = max(bp->rx_ring_size, 1024);
3757 		pool = page_pool_create(&pp);
3758 		if (IS_ERR(pool))
3759 			goto err_destroy_pp;
3760 	}
3761 	rxr->head_pool = pool;
3762 
3763 	return 0;
3764 
3765 err_destroy_pp:
3766 	page_pool_destroy(rxr->page_pool);
3767 	rxr->page_pool = NULL;
3768 	return PTR_ERR(pool);
3769 }
3770 
3771 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3772 {
3773 	u16 mem_size;
3774 
3775 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3776 	mem_size = rxr->rx_agg_bmap_size / 8;
3777 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3778 	if (!rxr->rx_agg_bmap)
3779 		return -ENOMEM;
3780 
3781 	return 0;
3782 }
3783 
3784 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3785 {
3786 	int numa_node = dev_to_node(&bp->pdev->dev);
3787 	int i, rc = 0, agg_rings = 0, cpu;
3788 
3789 	if (!bp->rx_ring)
3790 		return -ENOMEM;
3791 
3792 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3793 		agg_rings = 1;
3794 
3795 	for (i = 0; i < bp->rx_nr_rings; i++) {
3796 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3797 		struct bnxt_ring_struct *ring;
3798 		int cpu_node;
3799 
3800 		ring = &rxr->rx_ring_struct;
3801 
3802 		cpu = cpumask_local_spread(i, numa_node);
3803 		cpu_node = cpu_to_node(cpu);
3804 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3805 			   i, cpu_node);
3806 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3807 		if (rc)
3808 			return rc;
3809 
3810 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3811 		if (rc < 0)
3812 			return rc;
3813 
3814 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3815 						MEM_TYPE_PAGE_POOL,
3816 						rxr->page_pool);
3817 		if (rc) {
3818 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3819 			return rc;
3820 		}
3821 
3822 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3823 		if (rc)
3824 			return rc;
3825 
3826 		ring->grp_idx = i;
3827 		if (agg_rings) {
3828 			ring = &rxr->rx_agg_ring_struct;
3829 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3830 			if (rc)
3831 				return rc;
3832 
3833 			ring->grp_idx = i;
3834 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3835 			if (rc)
3836 				return rc;
3837 		}
3838 	}
3839 	if (bp->flags & BNXT_FLAG_TPA)
3840 		rc = bnxt_alloc_tpa_info(bp);
3841 	return rc;
3842 }
3843 
3844 static void bnxt_free_tx_rings(struct bnxt *bp)
3845 {
3846 	int i;
3847 	struct pci_dev *pdev = bp->pdev;
3848 
3849 	if (!bp->tx_ring)
3850 		return;
3851 
3852 	for (i = 0; i < bp->tx_nr_rings; i++) {
3853 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3854 		struct bnxt_ring_struct *ring;
3855 
3856 		if (txr->tx_push) {
3857 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3858 					  txr->tx_push, txr->tx_push_mapping);
3859 			txr->tx_push = NULL;
3860 		}
3861 
3862 		ring = &txr->tx_ring_struct;
3863 
3864 		bnxt_free_ring(bp, &ring->ring_mem);
3865 	}
3866 }
3867 
3868 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3869 	((tc) * (bp)->tx_nr_rings_per_tc)
3870 
3871 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3872 	((tx) % (bp)->tx_nr_rings_per_tc)
3873 
3874 #define BNXT_RING_TO_TC(bp, tx)		\
3875 	((tx) / (bp)->tx_nr_rings_per_tc)
3876 
3877 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3878 {
3879 	int i, j, rc;
3880 	struct pci_dev *pdev = bp->pdev;
3881 
3882 	bp->tx_push_size = 0;
3883 	if (bp->tx_push_thresh) {
3884 		int push_size;
3885 
3886 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3887 					bp->tx_push_thresh);
3888 
3889 		if (push_size > 256) {
3890 			push_size = 0;
3891 			bp->tx_push_thresh = 0;
3892 		}
3893 
3894 		bp->tx_push_size = push_size;
3895 	}
3896 
3897 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3898 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3899 		struct bnxt_ring_struct *ring;
3900 		u8 qidx;
3901 
3902 		ring = &txr->tx_ring_struct;
3903 
3904 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3905 		if (rc)
3906 			return rc;
3907 
3908 		ring->grp_idx = txr->bnapi->index;
3909 		if (bp->tx_push_size) {
3910 			dma_addr_t mapping;
3911 
3912 			/* One pre-allocated DMA buffer to backup
3913 			 * TX push operation
3914 			 */
3915 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3916 						bp->tx_push_size,
3917 						&txr->tx_push_mapping,
3918 						GFP_KERNEL);
3919 
3920 			if (!txr->tx_push)
3921 				return -ENOMEM;
3922 
3923 			mapping = txr->tx_push_mapping +
3924 				sizeof(struct tx_push_bd);
3925 			txr->data_mapping = cpu_to_le64(mapping);
3926 		}
3927 		qidx = bp->tc_to_qidx[j];
3928 		ring->queue_id = bp->q_info[qidx].queue_id;
3929 		spin_lock_init(&txr->xdp_tx_lock);
3930 		if (i < bp->tx_nr_rings_xdp)
3931 			continue;
3932 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3933 			j++;
3934 	}
3935 	return 0;
3936 }
3937 
3938 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3939 {
3940 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3941 
3942 	kfree(cpr->cp_desc_ring);
3943 	cpr->cp_desc_ring = NULL;
3944 	ring->ring_mem.pg_arr = NULL;
3945 	kfree(cpr->cp_desc_mapping);
3946 	cpr->cp_desc_mapping = NULL;
3947 	ring->ring_mem.dma_arr = NULL;
3948 }
3949 
3950 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3951 {
3952 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3953 	if (!cpr->cp_desc_ring)
3954 		return -ENOMEM;
3955 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3956 				       GFP_KERNEL);
3957 	if (!cpr->cp_desc_mapping)
3958 		return -ENOMEM;
3959 	return 0;
3960 }
3961 
3962 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3963 {
3964 	int i;
3965 
3966 	if (!bp->bnapi)
3967 		return;
3968 	for (i = 0; i < bp->cp_nr_rings; i++) {
3969 		struct bnxt_napi *bnapi = bp->bnapi[i];
3970 
3971 		if (!bnapi)
3972 			continue;
3973 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3974 	}
3975 }
3976 
3977 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3978 {
3979 	int i, n = bp->cp_nr_pages;
3980 
3981 	for (i = 0; i < bp->cp_nr_rings; i++) {
3982 		struct bnxt_napi *bnapi = bp->bnapi[i];
3983 		int rc;
3984 
3985 		if (!bnapi)
3986 			continue;
3987 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3988 		if (rc)
3989 			return rc;
3990 	}
3991 	return 0;
3992 }
3993 
3994 static void bnxt_free_cp_rings(struct bnxt *bp)
3995 {
3996 	int i;
3997 
3998 	if (!bp->bnapi)
3999 		return;
4000 
4001 	for (i = 0; i < bp->cp_nr_rings; i++) {
4002 		struct bnxt_napi *bnapi = bp->bnapi[i];
4003 		struct bnxt_cp_ring_info *cpr;
4004 		struct bnxt_ring_struct *ring;
4005 		int j;
4006 
4007 		if (!bnapi)
4008 			continue;
4009 
4010 		cpr = &bnapi->cp_ring;
4011 		ring = &cpr->cp_ring_struct;
4012 
4013 		bnxt_free_ring(bp, &ring->ring_mem);
4014 
4015 		if (!cpr->cp_ring_arr)
4016 			continue;
4017 
4018 		for (j = 0; j < cpr->cp_ring_count; j++) {
4019 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4020 
4021 			ring = &cpr2->cp_ring_struct;
4022 			bnxt_free_ring(bp, &ring->ring_mem);
4023 			bnxt_free_cp_arrays(cpr2);
4024 		}
4025 		kfree(cpr->cp_ring_arr);
4026 		cpr->cp_ring_arr = NULL;
4027 		cpr->cp_ring_count = 0;
4028 	}
4029 }
4030 
4031 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4032 				  struct bnxt_cp_ring_info *cpr)
4033 {
4034 	struct bnxt_ring_mem_info *rmem;
4035 	struct bnxt_ring_struct *ring;
4036 	int rc;
4037 
4038 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4039 	if (rc) {
4040 		bnxt_free_cp_arrays(cpr);
4041 		return -ENOMEM;
4042 	}
4043 	ring = &cpr->cp_ring_struct;
4044 	rmem = &ring->ring_mem;
4045 	rmem->nr_pages = bp->cp_nr_pages;
4046 	rmem->page_size = HW_CMPD_RING_SIZE;
4047 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4048 	rmem->dma_arr = cpr->cp_desc_mapping;
4049 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4050 	rc = bnxt_alloc_ring(bp, rmem);
4051 	if (rc) {
4052 		bnxt_free_ring(bp, rmem);
4053 		bnxt_free_cp_arrays(cpr);
4054 	}
4055 	return rc;
4056 }
4057 
4058 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4059 {
4060 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4061 	int i, j, rc, ulp_msix;
4062 	int tcs = bp->num_tc;
4063 
4064 	if (!tcs)
4065 		tcs = 1;
4066 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4067 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4068 		struct bnxt_napi *bnapi = bp->bnapi[i];
4069 		struct bnxt_cp_ring_info *cpr, *cpr2;
4070 		struct bnxt_ring_struct *ring;
4071 		int cp_count = 0, k;
4072 		int rx = 0, tx = 0;
4073 
4074 		if (!bnapi)
4075 			continue;
4076 
4077 		cpr = &bnapi->cp_ring;
4078 		cpr->bnapi = bnapi;
4079 		ring = &cpr->cp_ring_struct;
4080 
4081 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4082 		if (rc)
4083 			return rc;
4084 
4085 		ring->map_idx = ulp_msix + i;
4086 
4087 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4088 			continue;
4089 
4090 		if (i < bp->rx_nr_rings) {
4091 			cp_count++;
4092 			rx = 1;
4093 		}
4094 		if (i < bp->tx_nr_rings_xdp) {
4095 			cp_count++;
4096 			tx = 1;
4097 		} else if ((sh && i < bp->tx_nr_rings) ||
4098 			 (!sh && i >= bp->rx_nr_rings)) {
4099 			cp_count += tcs;
4100 			tx = 1;
4101 		}
4102 
4103 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4104 					   GFP_KERNEL);
4105 		if (!cpr->cp_ring_arr)
4106 			return -ENOMEM;
4107 		cpr->cp_ring_count = cp_count;
4108 
4109 		for (k = 0; k < cp_count; k++) {
4110 			cpr2 = &cpr->cp_ring_arr[k];
4111 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4112 			if (rc)
4113 				return rc;
4114 			cpr2->bnapi = bnapi;
4115 			cpr2->sw_stats = cpr->sw_stats;
4116 			cpr2->cp_idx = k;
4117 			if (!k && rx) {
4118 				bp->rx_ring[i].rx_cpr = cpr2;
4119 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4120 			} else {
4121 				int n, tc = k - rx;
4122 
4123 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4124 				bp->tx_ring[n].tx_cpr = cpr2;
4125 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4126 			}
4127 		}
4128 		if (tx)
4129 			j++;
4130 	}
4131 	return 0;
4132 }
4133 
4134 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4135 				     struct bnxt_rx_ring_info *rxr)
4136 {
4137 	struct bnxt_ring_mem_info *rmem;
4138 	struct bnxt_ring_struct *ring;
4139 
4140 	ring = &rxr->rx_ring_struct;
4141 	rmem = &ring->ring_mem;
4142 	rmem->nr_pages = bp->rx_nr_pages;
4143 	rmem->page_size = HW_RXBD_RING_SIZE;
4144 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4145 	rmem->dma_arr = rxr->rx_desc_mapping;
4146 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4147 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4148 
4149 	ring = &rxr->rx_agg_ring_struct;
4150 	rmem = &ring->ring_mem;
4151 	rmem->nr_pages = bp->rx_agg_nr_pages;
4152 	rmem->page_size = HW_RXBD_RING_SIZE;
4153 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4154 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4155 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4156 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4157 }
4158 
4159 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4160 				      struct bnxt_rx_ring_info *rxr)
4161 {
4162 	struct bnxt_ring_mem_info *rmem;
4163 	struct bnxt_ring_struct *ring;
4164 	int i;
4165 
4166 	rxr->page_pool->p.napi = NULL;
4167 	rxr->page_pool = NULL;
4168 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4169 
4170 	ring = &rxr->rx_ring_struct;
4171 	rmem = &ring->ring_mem;
4172 	rmem->pg_tbl = NULL;
4173 	rmem->pg_tbl_map = 0;
4174 	for (i = 0; i < rmem->nr_pages; i++) {
4175 		rmem->pg_arr[i] = NULL;
4176 		rmem->dma_arr[i] = 0;
4177 	}
4178 	*rmem->vmem = NULL;
4179 
4180 	ring = &rxr->rx_agg_ring_struct;
4181 	rmem = &ring->ring_mem;
4182 	rmem->pg_tbl = NULL;
4183 	rmem->pg_tbl_map = 0;
4184 	for (i = 0; i < rmem->nr_pages; i++) {
4185 		rmem->pg_arr[i] = NULL;
4186 		rmem->dma_arr[i] = 0;
4187 	}
4188 	*rmem->vmem = NULL;
4189 }
4190 
4191 static void bnxt_init_ring_struct(struct bnxt *bp)
4192 {
4193 	int i, j;
4194 
4195 	for (i = 0; i < bp->cp_nr_rings; i++) {
4196 		struct bnxt_napi *bnapi = bp->bnapi[i];
4197 		struct bnxt_ring_mem_info *rmem;
4198 		struct bnxt_cp_ring_info *cpr;
4199 		struct bnxt_rx_ring_info *rxr;
4200 		struct bnxt_tx_ring_info *txr;
4201 		struct bnxt_ring_struct *ring;
4202 
4203 		if (!bnapi)
4204 			continue;
4205 
4206 		cpr = &bnapi->cp_ring;
4207 		ring = &cpr->cp_ring_struct;
4208 		rmem = &ring->ring_mem;
4209 		rmem->nr_pages = bp->cp_nr_pages;
4210 		rmem->page_size = HW_CMPD_RING_SIZE;
4211 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4212 		rmem->dma_arr = cpr->cp_desc_mapping;
4213 		rmem->vmem_size = 0;
4214 
4215 		rxr = bnapi->rx_ring;
4216 		if (!rxr)
4217 			goto skip_rx;
4218 
4219 		ring = &rxr->rx_ring_struct;
4220 		rmem = &ring->ring_mem;
4221 		rmem->nr_pages = bp->rx_nr_pages;
4222 		rmem->page_size = HW_RXBD_RING_SIZE;
4223 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4224 		rmem->dma_arr = rxr->rx_desc_mapping;
4225 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4226 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4227 
4228 		ring = &rxr->rx_agg_ring_struct;
4229 		rmem = &ring->ring_mem;
4230 		rmem->nr_pages = bp->rx_agg_nr_pages;
4231 		rmem->page_size = HW_RXBD_RING_SIZE;
4232 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4233 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4234 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4235 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4236 
4237 skip_rx:
4238 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4239 			ring = &txr->tx_ring_struct;
4240 			rmem = &ring->ring_mem;
4241 			rmem->nr_pages = bp->tx_nr_pages;
4242 			rmem->page_size = HW_TXBD_RING_SIZE;
4243 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4244 			rmem->dma_arr = txr->tx_desc_mapping;
4245 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4246 			rmem->vmem = (void **)&txr->tx_buf_ring;
4247 		}
4248 	}
4249 }
4250 
4251 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4252 {
4253 	int i;
4254 	u32 prod;
4255 	struct rx_bd **rx_buf_ring;
4256 
4257 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4258 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4259 		int j;
4260 		struct rx_bd *rxbd;
4261 
4262 		rxbd = rx_buf_ring[i];
4263 		if (!rxbd)
4264 			continue;
4265 
4266 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4267 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4268 			rxbd->rx_bd_opaque = prod;
4269 		}
4270 	}
4271 }
4272 
4273 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4274 				       struct bnxt_rx_ring_info *rxr,
4275 				       int ring_nr)
4276 {
4277 	u32 prod;
4278 	int i;
4279 
4280 	prod = rxr->rx_prod;
4281 	for (i = 0; i < bp->rx_ring_size; i++) {
4282 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4283 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4284 				    ring_nr, i, bp->rx_ring_size);
4285 			break;
4286 		}
4287 		prod = NEXT_RX(prod);
4288 	}
4289 	rxr->rx_prod = prod;
4290 }
4291 
4292 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4293 					struct bnxt_rx_ring_info *rxr,
4294 					int ring_nr)
4295 {
4296 	u32 prod;
4297 	int i;
4298 
4299 	prod = rxr->rx_agg_prod;
4300 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4301 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4302 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4303 				    ring_nr, i, bp->rx_ring_size);
4304 			break;
4305 		}
4306 		prod = NEXT_RX_AGG(prod);
4307 	}
4308 	rxr->rx_agg_prod = prod;
4309 }
4310 
4311 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4312 					struct bnxt_rx_ring_info *rxr)
4313 {
4314 	dma_addr_t mapping;
4315 	u8 *data;
4316 	int i;
4317 
4318 	for (i = 0; i < bp->max_tpa; i++) {
4319 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4320 					    GFP_KERNEL);
4321 		if (!data)
4322 			return -ENOMEM;
4323 
4324 		rxr->rx_tpa[i].data = data;
4325 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4326 		rxr->rx_tpa[i].mapping = mapping;
4327 	}
4328 
4329 	return 0;
4330 }
4331 
4332 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4333 {
4334 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4335 	int rc;
4336 
4337 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4338 
4339 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4340 		return 0;
4341 
4342 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4343 
4344 	if (rxr->rx_tpa) {
4345 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4346 		if (rc)
4347 			return rc;
4348 	}
4349 	return 0;
4350 }
4351 
4352 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4353 				       struct bnxt_rx_ring_info *rxr)
4354 {
4355 	struct bnxt_ring_struct *ring;
4356 	u32 type;
4357 
4358 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4359 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4360 
4361 	if (NET_IP_ALIGN == 2)
4362 		type |= RX_BD_FLAGS_SOP;
4363 
4364 	ring = &rxr->rx_ring_struct;
4365 	bnxt_init_rxbd_pages(ring, type);
4366 	ring->fw_ring_id = INVALID_HW_RING_ID;
4367 }
4368 
4369 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4370 					   struct bnxt_rx_ring_info *rxr)
4371 {
4372 	struct bnxt_ring_struct *ring;
4373 	u32 type;
4374 
4375 	ring = &rxr->rx_agg_ring_struct;
4376 	ring->fw_ring_id = INVALID_HW_RING_ID;
4377 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4378 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4379 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4380 
4381 		bnxt_init_rxbd_pages(ring, type);
4382 	}
4383 }
4384 
4385 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4386 {
4387 	struct bnxt_rx_ring_info *rxr;
4388 
4389 	rxr = &bp->rx_ring[ring_nr];
4390 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4391 
4392 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4393 			     &rxr->bnapi->napi);
4394 
4395 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4396 		bpf_prog_add(bp->xdp_prog, 1);
4397 		rxr->xdp_prog = bp->xdp_prog;
4398 	}
4399 
4400 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4401 
4402 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4403 }
4404 
4405 static void bnxt_init_cp_rings(struct bnxt *bp)
4406 {
4407 	int i, j;
4408 
4409 	for (i = 0; i < bp->cp_nr_rings; i++) {
4410 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4411 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4412 
4413 		ring->fw_ring_id = INVALID_HW_RING_ID;
4414 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4415 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4416 		if (!cpr->cp_ring_arr)
4417 			continue;
4418 		for (j = 0; j < cpr->cp_ring_count; j++) {
4419 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4420 
4421 			ring = &cpr2->cp_ring_struct;
4422 			ring->fw_ring_id = INVALID_HW_RING_ID;
4423 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4424 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4425 		}
4426 	}
4427 }
4428 
4429 static int bnxt_init_rx_rings(struct bnxt *bp)
4430 {
4431 	int i, rc = 0;
4432 
4433 	if (BNXT_RX_PAGE_MODE(bp)) {
4434 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4435 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4436 	} else {
4437 		bp->rx_offset = BNXT_RX_OFFSET;
4438 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4439 	}
4440 
4441 	for (i = 0; i < bp->rx_nr_rings; i++) {
4442 		rc = bnxt_init_one_rx_ring(bp, i);
4443 		if (rc)
4444 			break;
4445 	}
4446 
4447 	return rc;
4448 }
4449 
4450 static int bnxt_init_tx_rings(struct bnxt *bp)
4451 {
4452 	u16 i;
4453 
4454 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4455 				   BNXT_MIN_TX_DESC_CNT);
4456 
4457 	for (i = 0; i < bp->tx_nr_rings; i++) {
4458 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4459 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4460 
4461 		ring->fw_ring_id = INVALID_HW_RING_ID;
4462 
4463 		if (i >= bp->tx_nr_rings_xdp)
4464 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4465 					     NETDEV_QUEUE_TYPE_TX,
4466 					     &txr->bnapi->napi);
4467 	}
4468 
4469 	return 0;
4470 }
4471 
4472 static void bnxt_free_ring_grps(struct bnxt *bp)
4473 {
4474 	kfree(bp->grp_info);
4475 	bp->grp_info = NULL;
4476 }
4477 
4478 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4479 {
4480 	int i;
4481 
4482 	if (irq_re_init) {
4483 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4484 				       sizeof(struct bnxt_ring_grp_info),
4485 				       GFP_KERNEL);
4486 		if (!bp->grp_info)
4487 			return -ENOMEM;
4488 	}
4489 	for (i = 0; i < bp->cp_nr_rings; i++) {
4490 		if (irq_re_init)
4491 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4492 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4493 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4494 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4495 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4496 	}
4497 	return 0;
4498 }
4499 
4500 static void bnxt_free_vnics(struct bnxt *bp)
4501 {
4502 	kfree(bp->vnic_info);
4503 	bp->vnic_info = NULL;
4504 	bp->nr_vnics = 0;
4505 }
4506 
4507 static int bnxt_alloc_vnics(struct bnxt *bp)
4508 {
4509 	int num_vnics = 1;
4510 
4511 #ifdef CONFIG_RFS_ACCEL
4512 	if (bp->flags & BNXT_FLAG_RFS) {
4513 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4514 			num_vnics++;
4515 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4516 			num_vnics += bp->rx_nr_rings;
4517 	}
4518 #endif
4519 
4520 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4521 		num_vnics++;
4522 
4523 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4524 				GFP_KERNEL);
4525 	if (!bp->vnic_info)
4526 		return -ENOMEM;
4527 
4528 	bp->nr_vnics = num_vnics;
4529 	return 0;
4530 }
4531 
4532 static void bnxt_init_vnics(struct bnxt *bp)
4533 {
4534 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4535 	int i;
4536 
4537 	for (i = 0; i < bp->nr_vnics; i++) {
4538 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4539 		int j;
4540 
4541 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4542 		vnic->vnic_id = i;
4543 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4544 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4545 
4546 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4547 
4548 		if (bp->vnic_info[i].rss_hash_key) {
4549 			if (i == BNXT_VNIC_DEFAULT) {
4550 				u8 *key = (void *)vnic->rss_hash_key;
4551 				int k;
4552 
4553 				if (!bp->rss_hash_key_valid &&
4554 				    !bp->rss_hash_key_updated) {
4555 					get_random_bytes(bp->rss_hash_key,
4556 							 HW_HASH_KEY_SIZE);
4557 					bp->rss_hash_key_updated = true;
4558 				}
4559 
4560 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4561 				       HW_HASH_KEY_SIZE);
4562 
4563 				if (!bp->rss_hash_key_updated)
4564 					continue;
4565 
4566 				bp->rss_hash_key_updated = false;
4567 				bp->rss_hash_key_valid = true;
4568 
4569 				bp->toeplitz_prefix = 0;
4570 				for (k = 0; k < 8; k++) {
4571 					bp->toeplitz_prefix <<= 8;
4572 					bp->toeplitz_prefix |= key[k];
4573 				}
4574 			} else {
4575 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4576 				       HW_HASH_KEY_SIZE);
4577 			}
4578 		}
4579 	}
4580 }
4581 
4582 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4583 {
4584 	int pages;
4585 
4586 	pages = ring_size / desc_per_pg;
4587 
4588 	if (!pages)
4589 		return 1;
4590 
4591 	pages++;
4592 
4593 	while (pages & (pages - 1))
4594 		pages++;
4595 
4596 	return pages;
4597 }
4598 
4599 void bnxt_set_tpa_flags(struct bnxt *bp)
4600 {
4601 	bp->flags &= ~BNXT_FLAG_TPA;
4602 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4603 		return;
4604 	if (bp->dev->features & NETIF_F_LRO)
4605 		bp->flags |= BNXT_FLAG_LRO;
4606 	else if (bp->dev->features & NETIF_F_GRO_HW)
4607 		bp->flags |= BNXT_FLAG_GRO;
4608 }
4609 
4610 static void bnxt_init_ring_params(struct bnxt *bp)
4611 {
4612 	bp->rx_copybreak = BNXT_DEFAULT_RX_COPYBREAK;
4613 	bp->dev->ethtool->hds_thresh = BNXT_DEFAULT_RX_COPYBREAK;
4614 }
4615 
4616 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4617  * be set on entry.
4618  */
4619 void bnxt_set_ring_params(struct bnxt *bp)
4620 {
4621 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4622 	u32 agg_factor = 0, agg_ring_size = 0;
4623 
4624 	/* 8 for CRC and VLAN */
4625 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4626 
4627 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4628 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4629 
4630 	ring_size = bp->rx_ring_size;
4631 	bp->rx_agg_ring_size = 0;
4632 	bp->rx_agg_nr_pages = 0;
4633 
4634 	if (bp->flags & BNXT_FLAG_TPA || bp->flags & BNXT_FLAG_HDS)
4635 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4636 
4637 	bp->flags &= ~BNXT_FLAG_JUMBO;
4638 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4639 		u32 jumbo_factor;
4640 
4641 		bp->flags |= BNXT_FLAG_JUMBO;
4642 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4643 		if (jumbo_factor > agg_factor)
4644 			agg_factor = jumbo_factor;
4645 	}
4646 	if (agg_factor) {
4647 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4648 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4649 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4650 				    bp->rx_ring_size, ring_size);
4651 			bp->rx_ring_size = ring_size;
4652 		}
4653 		agg_ring_size = ring_size * agg_factor;
4654 
4655 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4656 							RX_DESC_CNT);
4657 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4658 			u32 tmp = agg_ring_size;
4659 
4660 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4661 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4662 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4663 				    tmp, agg_ring_size);
4664 		}
4665 		bp->rx_agg_ring_size = agg_ring_size;
4666 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4667 
4668 		if (BNXT_RX_PAGE_MODE(bp)) {
4669 			rx_space = PAGE_SIZE;
4670 			rx_size = PAGE_SIZE -
4671 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4672 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4673 		} else {
4674 			rx_size = SKB_DATA_ALIGN(max(BNXT_DEFAULT_RX_COPYBREAK,
4675 						     bp->rx_copybreak) +
4676 						 NET_IP_ALIGN);
4677 			rx_space = rx_size + NET_SKB_PAD +
4678 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4679 		}
4680 	}
4681 
4682 	bp->rx_buf_use_size = rx_size;
4683 	bp->rx_buf_size = rx_space;
4684 
4685 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4686 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4687 
4688 	ring_size = bp->tx_ring_size;
4689 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4690 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4691 
4692 	max_rx_cmpl = bp->rx_ring_size;
4693 	/* MAX TPA needs to be added because TPA_START completions are
4694 	 * immediately recycled, so the TPA completions are not bound by
4695 	 * the RX ring size.
4696 	 */
4697 	if (bp->flags & BNXT_FLAG_TPA)
4698 		max_rx_cmpl += bp->max_tpa;
4699 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4700 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4701 	bp->cp_ring_size = ring_size;
4702 
4703 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4704 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4705 		bp->cp_nr_pages = MAX_CP_PAGES;
4706 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4707 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4708 			    ring_size, bp->cp_ring_size);
4709 	}
4710 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4711 	bp->cp_ring_mask = bp->cp_bit - 1;
4712 }
4713 
4714 /* Changing allocation mode of RX rings.
4715  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4716  */
4717 int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4718 {
4719 	struct net_device *dev = bp->dev;
4720 
4721 	if (page_mode) {
4722 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4723 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4724 
4725 		if (bp->xdp_prog->aux->xdp_has_frags)
4726 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4727 		else
4728 			dev->max_mtu =
4729 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4730 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4731 			bp->flags |= BNXT_FLAG_JUMBO;
4732 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4733 		} else {
4734 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4735 			bp->rx_skb_func = bnxt_rx_page_skb;
4736 		}
4737 		bp->rx_dir = DMA_BIDIRECTIONAL;
4738 		/* Disable LRO or GRO_HW */
4739 		netdev_update_features(dev);
4740 	} else {
4741 		dev->max_mtu = bp->max_mtu;
4742 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4743 		bp->rx_dir = DMA_FROM_DEVICE;
4744 		bp->rx_skb_func = bnxt_rx_skb;
4745 	}
4746 	return 0;
4747 }
4748 
4749 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4750 {
4751 	int i;
4752 	struct bnxt_vnic_info *vnic;
4753 	struct pci_dev *pdev = bp->pdev;
4754 
4755 	if (!bp->vnic_info)
4756 		return;
4757 
4758 	for (i = 0; i < bp->nr_vnics; i++) {
4759 		vnic = &bp->vnic_info[i];
4760 
4761 		kfree(vnic->fw_grp_ids);
4762 		vnic->fw_grp_ids = NULL;
4763 
4764 		kfree(vnic->uc_list);
4765 		vnic->uc_list = NULL;
4766 
4767 		if (vnic->mc_list) {
4768 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4769 					  vnic->mc_list, vnic->mc_list_mapping);
4770 			vnic->mc_list = NULL;
4771 		}
4772 
4773 		if (vnic->rss_table) {
4774 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4775 					  vnic->rss_table,
4776 					  vnic->rss_table_dma_addr);
4777 			vnic->rss_table = NULL;
4778 		}
4779 
4780 		vnic->rss_hash_key = NULL;
4781 		vnic->flags = 0;
4782 	}
4783 }
4784 
4785 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4786 {
4787 	int i, rc = 0, size;
4788 	struct bnxt_vnic_info *vnic;
4789 	struct pci_dev *pdev = bp->pdev;
4790 	int max_rings;
4791 
4792 	for (i = 0; i < bp->nr_vnics; i++) {
4793 		vnic = &bp->vnic_info[i];
4794 
4795 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4796 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4797 
4798 			if (mem_size > 0) {
4799 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4800 				if (!vnic->uc_list) {
4801 					rc = -ENOMEM;
4802 					goto out;
4803 				}
4804 			}
4805 		}
4806 
4807 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4808 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4809 			vnic->mc_list =
4810 				dma_alloc_coherent(&pdev->dev,
4811 						   vnic->mc_list_size,
4812 						   &vnic->mc_list_mapping,
4813 						   GFP_KERNEL);
4814 			if (!vnic->mc_list) {
4815 				rc = -ENOMEM;
4816 				goto out;
4817 			}
4818 		}
4819 
4820 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4821 			goto vnic_skip_grps;
4822 
4823 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4824 			max_rings = bp->rx_nr_rings;
4825 		else
4826 			max_rings = 1;
4827 
4828 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4829 		if (!vnic->fw_grp_ids) {
4830 			rc = -ENOMEM;
4831 			goto out;
4832 		}
4833 vnic_skip_grps:
4834 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4835 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4836 			continue;
4837 
4838 		/* Allocate rss table and hash key */
4839 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4840 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4841 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4842 
4843 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4844 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4845 						     vnic->rss_table_size,
4846 						     &vnic->rss_table_dma_addr,
4847 						     GFP_KERNEL);
4848 		if (!vnic->rss_table) {
4849 			rc = -ENOMEM;
4850 			goto out;
4851 		}
4852 
4853 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4854 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4855 	}
4856 	return 0;
4857 
4858 out:
4859 	return rc;
4860 }
4861 
4862 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4863 {
4864 	struct bnxt_hwrm_wait_token *token;
4865 
4866 	dma_pool_destroy(bp->hwrm_dma_pool);
4867 	bp->hwrm_dma_pool = NULL;
4868 
4869 	rcu_read_lock();
4870 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4871 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4872 	rcu_read_unlock();
4873 }
4874 
4875 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4876 {
4877 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4878 					    BNXT_HWRM_DMA_SIZE,
4879 					    BNXT_HWRM_DMA_ALIGN, 0);
4880 	if (!bp->hwrm_dma_pool)
4881 		return -ENOMEM;
4882 
4883 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4884 
4885 	return 0;
4886 }
4887 
4888 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4889 {
4890 	kfree(stats->hw_masks);
4891 	stats->hw_masks = NULL;
4892 	kfree(stats->sw_stats);
4893 	stats->sw_stats = NULL;
4894 	if (stats->hw_stats) {
4895 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4896 				  stats->hw_stats_map);
4897 		stats->hw_stats = NULL;
4898 	}
4899 }
4900 
4901 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4902 				bool alloc_masks)
4903 {
4904 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4905 					     &stats->hw_stats_map, GFP_KERNEL);
4906 	if (!stats->hw_stats)
4907 		return -ENOMEM;
4908 
4909 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4910 	if (!stats->sw_stats)
4911 		goto stats_mem_err;
4912 
4913 	if (alloc_masks) {
4914 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4915 		if (!stats->hw_masks)
4916 			goto stats_mem_err;
4917 	}
4918 	return 0;
4919 
4920 stats_mem_err:
4921 	bnxt_free_stats_mem(bp, stats);
4922 	return -ENOMEM;
4923 }
4924 
4925 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4926 {
4927 	int i;
4928 
4929 	for (i = 0; i < count; i++)
4930 		mask_arr[i] = mask;
4931 }
4932 
4933 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4934 {
4935 	int i;
4936 
4937 	for (i = 0; i < count; i++)
4938 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4939 }
4940 
4941 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4942 				    struct bnxt_stats_mem *stats)
4943 {
4944 	struct hwrm_func_qstats_ext_output *resp;
4945 	struct hwrm_func_qstats_ext_input *req;
4946 	__le64 *hw_masks;
4947 	int rc;
4948 
4949 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4950 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4951 		return -EOPNOTSUPP;
4952 
4953 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4954 	if (rc)
4955 		return rc;
4956 
4957 	req->fid = cpu_to_le16(0xffff);
4958 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4959 
4960 	resp = hwrm_req_hold(bp, req);
4961 	rc = hwrm_req_send(bp, req);
4962 	if (!rc) {
4963 		hw_masks = &resp->rx_ucast_pkts;
4964 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4965 	}
4966 	hwrm_req_drop(bp, req);
4967 	return rc;
4968 }
4969 
4970 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4971 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4972 
4973 static void bnxt_init_stats(struct bnxt *bp)
4974 {
4975 	struct bnxt_napi *bnapi = bp->bnapi[0];
4976 	struct bnxt_cp_ring_info *cpr;
4977 	struct bnxt_stats_mem *stats;
4978 	__le64 *rx_stats, *tx_stats;
4979 	int rc, rx_count, tx_count;
4980 	u64 *rx_masks, *tx_masks;
4981 	u64 mask;
4982 	u8 flags;
4983 
4984 	cpr = &bnapi->cp_ring;
4985 	stats = &cpr->stats;
4986 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4987 	if (rc) {
4988 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4989 			mask = (1ULL << 48) - 1;
4990 		else
4991 			mask = -1ULL;
4992 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
4993 	}
4994 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
4995 		stats = &bp->port_stats;
4996 		rx_stats = stats->hw_stats;
4997 		rx_masks = stats->hw_masks;
4998 		rx_count = sizeof(struct rx_port_stats) / 8;
4999 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5000 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5001 		tx_count = sizeof(struct tx_port_stats) / 8;
5002 
5003 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5004 		rc = bnxt_hwrm_port_qstats(bp, flags);
5005 		if (rc) {
5006 			mask = (1ULL << 40) - 1;
5007 
5008 			bnxt_fill_masks(rx_masks, mask, rx_count);
5009 			bnxt_fill_masks(tx_masks, mask, tx_count);
5010 		} else {
5011 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5012 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5013 			bnxt_hwrm_port_qstats(bp, 0);
5014 		}
5015 	}
5016 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5017 		stats = &bp->rx_port_stats_ext;
5018 		rx_stats = stats->hw_stats;
5019 		rx_masks = stats->hw_masks;
5020 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5021 		stats = &bp->tx_port_stats_ext;
5022 		tx_stats = stats->hw_stats;
5023 		tx_masks = stats->hw_masks;
5024 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5025 
5026 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5027 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5028 		if (rc) {
5029 			mask = (1ULL << 40) - 1;
5030 
5031 			bnxt_fill_masks(rx_masks, mask, rx_count);
5032 			if (tx_stats)
5033 				bnxt_fill_masks(tx_masks, mask, tx_count);
5034 		} else {
5035 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5036 			if (tx_stats)
5037 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5038 						   tx_count);
5039 			bnxt_hwrm_port_qstats_ext(bp, 0);
5040 		}
5041 	}
5042 }
5043 
5044 static void bnxt_free_port_stats(struct bnxt *bp)
5045 {
5046 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5047 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5048 
5049 	bnxt_free_stats_mem(bp, &bp->port_stats);
5050 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5051 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5052 }
5053 
5054 static void bnxt_free_ring_stats(struct bnxt *bp)
5055 {
5056 	int i;
5057 
5058 	if (!bp->bnapi)
5059 		return;
5060 
5061 	for (i = 0; i < bp->cp_nr_rings; i++) {
5062 		struct bnxt_napi *bnapi = bp->bnapi[i];
5063 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5064 
5065 		bnxt_free_stats_mem(bp, &cpr->stats);
5066 
5067 		kfree(cpr->sw_stats);
5068 		cpr->sw_stats = NULL;
5069 	}
5070 }
5071 
5072 static int bnxt_alloc_stats(struct bnxt *bp)
5073 {
5074 	u32 size, i;
5075 	int rc;
5076 
5077 	size = bp->hw_ring_stats_size;
5078 
5079 	for (i = 0; i < bp->cp_nr_rings; i++) {
5080 		struct bnxt_napi *bnapi = bp->bnapi[i];
5081 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5082 
5083 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5084 		if (!cpr->sw_stats)
5085 			return -ENOMEM;
5086 
5087 		cpr->stats.len = size;
5088 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5089 		if (rc)
5090 			return rc;
5091 
5092 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5093 	}
5094 
5095 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5096 		return 0;
5097 
5098 	if (bp->port_stats.hw_stats)
5099 		goto alloc_ext_stats;
5100 
5101 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5102 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5103 	if (rc)
5104 		return rc;
5105 
5106 	bp->flags |= BNXT_FLAG_PORT_STATS;
5107 
5108 alloc_ext_stats:
5109 	/* Display extended statistics only if FW supports it */
5110 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5111 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5112 			return 0;
5113 
5114 	if (bp->rx_port_stats_ext.hw_stats)
5115 		goto alloc_tx_ext_stats;
5116 
5117 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5118 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5119 	/* Extended stats are optional */
5120 	if (rc)
5121 		return 0;
5122 
5123 alloc_tx_ext_stats:
5124 	if (bp->tx_port_stats_ext.hw_stats)
5125 		return 0;
5126 
5127 	if (bp->hwrm_spec_code >= 0x10902 ||
5128 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5129 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5130 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5131 		/* Extended stats are optional */
5132 		if (rc)
5133 			return 0;
5134 	}
5135 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5136 	return 0;
5137 }
5138 
5139 static void bnxt_clear_ring_indices(struct bnxt *bp)
5140 {
5141 	int i, j;
5142 
5143 	if (!bp->bnapi)
5144 		return;
5145 
5146 	for (i = 0; i < bp->cp_nr_rings; i++) {
5147 		struct bnxt_napi *bnapi = bp->bnapi[i];
5148 		struct bnxt_cp_ring_info *cpr;
5149 		struct bnxt_rx_ring_info *rxr;
5150 		struct bnxt_tx_ring_info *txr;
5151 
5152 		if (!bnapi)
5153 			continue;
5154 
5155 		cpr = &bnapi->cp_ring;
5156 		cpr->cp_raw_cons = 0;
5157 
5158 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5159 			txr->tx_prod = 0;
5160 			txr->tx_cons = 0;
5161 			txr->tx_hw_cons = 0;
5162 		}
5163 
5164 		rxr = bnapi->rx_ring;
5165 		if (rxr) {
5166 			rxr->rx_prod = 0;
5167 			rxr->rx_agg_prod = 0;
5168 			rxr->rx_sw_agg_prod = 0;
5169 			rxr->rx_next_cons = 0;
5170 		}
5171 		bnapi->events = 0;
5172 	}
5173 }
5174 
5175 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5176 {
5177 	u8 type = fltr->type, flags = fltr->flags;
5178 
5179 	INIT_LIST_HEAD(&fltr->list);
5180 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5181 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5182 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5183 }
5184 
5185 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5186 {
5187 	if (!list_empty(&fltr->list))
5188 		list_del_init(&fltr->list);
5189 }
5190 
5191 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5192 {
5193 	struct bnxt_filter_base *usr_fltr, *tmp;
5194 
5195 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5196 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5197 			continue;
5198 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5199 	}
5200 }
5201 
5202 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5203 {
5204 	hlist_del(&fltr->hash);
5205 	bnxt_del_one_usr_fltr(bp, fltr);
5206 	if (fltr->flags) {
5207 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5208 		bp->ntp_fltr_count--;
5209 	}
5210 	kfree(fltr);
5211 }
5212 
5213 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5214 {
5215 	int i;
5216 
5217 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5218 	 * safe to delete the hash table.
5219 	 */
5220 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5221 		struct hlist_head *head;
5222 		struct hlist_node *tmp;
5223 		struct bnxt_ntuple_filter *fltr;
5224 
5225 		head = &bp->ntp_fltr_hash_tbl[i];
5226 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5227 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5228 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5229 				     !list_empty(&fltr->base.list)))
5230 				continue;
5231 			bnxt_del_fltr(bp, &fltr->base);
5232 		}
5233 	}
5234 	if (!all)
5235 		return;
5236 
5237 	bitmap_free(bp->ntp_fltr_bmap);
5238 	bp->ntp_fltr_bmap = NULL;
5239 	bp->ntp_fltr_count = 0;
5240 }
5241 
5242 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5243 {
5244 	int i, rc = 0;
5245 
5246 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5247 		return 0;
5248 
5249 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5250 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5251 
5252 	bp->ntp_fltr_count = 0;
5253 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5254 
5255 	if (!bp->ntp_fltr_bmap)
5256 		rc = -ENOMEM;
5257 
5258 	return rc;
5259 }
5260 
5261 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5262 {
5263 	int i;
5264 
5265 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5266 		struct hlist_head *head;
5267 		struct hlist_node *tmp;
5268 		struct bnxt_l2_filter *fltr;
5269 
5270 		head = &bp->l2_fltr_hash_tbl[i];
5271 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5272 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5273 				     !list_empty(&fltr->base.list)))
5274 				continue;
5275 			bnxt_del_fltr(bp, &fltr->base);
5276 		}
5277 	}
5278 }
5279 
5280 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5281 {
5282 	int i;
5283 
5284 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5285 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5286 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5287 }
5288 
5289 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5290 {
5291 	bnxt_free_vnic_attributes(bp);
5292 	bnxt_free_tx_rings(bp);
5293 	bnxt_free_rx_rings(bp);
5294 	bnxt_free_cp_rings(bp);
5295 	bnxt_free_all_cp_arrays(bp);
5296 	bnxt_free_ntp_fltrs(bp, false);
5297 	bnxt_free_l2_filters(bp, false);
5298 	if (irq_re_init) {
5299 		bnxt_free_ring_stats(bp);
5300 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5301 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5302 			bnxt_free_port_stats(bp);
5303 		bnxt_free_ring_grps(bp);
5304 		bnxt_free_vnics(bp);
5305 		kfree(bp->tx_ring_map);
5306 		bp->tx_ring_map = NULL;
5307 		kfree(bp->tx_ring);
5308 		bp->tx_ring = NULL;
5309 		kfree(bp->rx_ring);
5310 		bp->rx_ring = NULL;
5311 		kfree(bp->bnapi);
5312 		bp->bnapi = NULL;
5313 	} else {
5314 		bnxt_clear_ring_indices(bp);
5315 	}
5316 }
5317 
5318 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5319 {
5320 	int i, j, rc, size, arr_size;
5321 	void *bnapi;
5322 
5323 	if (irq_re_init) {
5324 		/* Allocate bnapi mem pointer array and mem block for
5325 		 * all queues
5326 		 */
5327 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5328 				bp->cp_nr_rings);
5329 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5330 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5331 		if (!bnapi)
5332 			return -ENOMEM;
5333 
5334 		bp->bnapi = bnapi;
5335 		bnapi += arr_size;
5336 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5337 			bp->bnapi[i] = bnapi;
5338 			bp->bnapi[i]->index = i;
5339 			bp->bnapi[i]->bp = bp;
5340 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5341 				struct bnxt_cp_ring_info *cpr =
5342 					&bp->bnapi[i]->cp_ring;
5343 
5344 				cpr->cp_ring_struct.ring_mem.flags =
5345 					BNXT_RMEM_RING_PTE_FLAG;
5346 			}
5347 		}
5348 
5349 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5350 				      sizeof(struct bnxt_rx_ring_info),
5351 				      GFP_KERNEL);
5352 		if (!bp->rx_ring)
5353 			return -ENOMEM;
5354 
5355 		for (i = 0; i < bp->rx_nr_rings; i++) {
5356 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5357 
5358 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5359 				rxr->rx_ring_struct.ring_mem.flags =
5360 					BNXT_RMEM_RING_PTE_FLAG;
5361 				rxr->rx_agg_ring_struct.ring_mem.flags =
5362 					BNXT_RMEM_RING_PTE_FLAG;
5363 			} else {
5364 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5365 			}
5366 			rxr->bnapi = bp->bnapi[i];
5367 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5368 		}
5369 
5370 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5371 				      sizeof(struct bnxt_tx_ring_info),
5372 				      GFP_KERNEL);
5373 		if (!bp->tx_ring)
5374 			return -ENOMEM;
5375 
5376 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5377 					  GFP_KERNEL);
5378 
5379 		if (!bp->tx_ring_map)
5380 			return -ENOMEM;
5381 
5382 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5383 			j = 0;
5384 		else
5385 			j = bp->rx_nr_rings;
5386 
5387 		for (i = 0; i < bp->tx_nr_rings; i++) {
5388 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5389 			struct bnxt_napi *bnapi2;
5390 
5391 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5392 				txr->tx_ring_struct.ring_mem.flags =
5393 					BNXT_RMEM_RING_PTE_FLAG;
5394 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5395 			if (i >= bp->tx_nr_rings_xdp) {
5396 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5397 
5398 				bnapi2 = bp->bnapi[k];
5399 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5400 				txr->tx_napi_idx =
5401 					BNXT_RING_TO_TC(bp, txr->txq_index);
5402 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5403 				bnapi2->tx_int = bnxt_tx_int;
5404 			} else {
5405 				bnapi2 = bp->bnapi[j];
5406 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5407 				bnapi2->tx_ring[0] = txr;
5408 				bnapi2->tx_int = bnxt_tx_int_xdp;
5409 				j++;
5410 			}
5411 			txr->bnapi = bnapi2;
5412 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5413 				txr->tx_cpr = &bnapi2->cp_ring;
5414 		}
5415 
5416 		rc = bnxt_alloc_stats(bp);
5417 		if (rc)
5418 			goto alloc_mem_err;
5419 		bnxt_init_stats(bp);
5420 
5421 		rc = bnxt_alloc_ntp_fltrs(bp);
5422 		if (rc)
5423 			goto alloc_mem_err;
5424 
5425 		rc = bnxt_alloc_vnics(bp);
5426 		if (rc)
5427 			goto alloc_mem_err;
5428 	}
5429 
5430 	rc = bnxt_alloc_all_cp_arrays(bp);
5431 	if (rc)
5432 		goto alloc_mem_err;
5433 
5434 	bnxt_init_ring_struct(bp);
5435 
5436 	rc = bnxt_alloc_rx_rings(bp);
5437 	if (rc)
5438 		goto alloc_mem_err;
5439 
5440 	rc = bnxt_alloc_tx_rings(bp);
5441 	if (rc)
5442 		goto alloc_mem_err;
5443 
5444 	rc = bnxt_alloc_cp_rings(bp);
5445 	if (rc)
5446 		goto alloc_mem_err;
5447 
5448 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5449 						  BNXT_VNIC_MCAST_FLAG |
5450 						  BNXT_VNIC_UCAST_FLAG;
5451 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5452 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5453 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5454 
5455 	rc = bnxt_alloc_vnic_attributes(bp);
5456 	if (rc)
5457 		goto alloc_mem_err;
5458 	return 0;
5459 
5460 alloc_mem_err:
5461 	bnxt_free_mem(bp, true);
5462 	return rc;
5463 }
5464 
5465 static void bnxt_disable_int(struct bnxt *bp)
5466 {
5467 	int i;
5468 
5469 	if (!bp->bnapi)
5470 		return;
5471 
5472 	for (i = 0; i < bp->cp_nr_rings; i++) {
5473 		struct bnxt_napi *bnapi = bp->bnapi[i];
5474 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5475 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5476 
5477 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5478 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5479 	}
5480 }
5481 
5482 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5483 {
5484 	struct bnxt_napi *bnapi = bp->bnapi[n];
5485 	struct bnxt_cp_ring_info *cpr;
5486 
5487 	cpr = &bnapi->cp_ring;
5488 	return cpr->cp_ring_struct.map_idx;
5489 }
5490 
5491 static void bnxt_disable_int_sync(struct bnxt *bp)
5492 {
5493 	int i;
5494 
5495 	if (!bp->irq_tbl)
5496 		return;
5497 
5498 	atomic_inc(&bp->intr_sem);
5499 
5500 	bnxt_disable_int(bp);
5501 	for (i = 0; i < bp->cp_nr_rings; i++) {
5502 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5503 
5504 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5505 	}
5506 }
5507 
5508 static void bnxt_enable_int(struct bnxt *bp)
5509 {
5510 	int i;
5511 
5512 	atomic_set(&bp->intr_sem, 0);
5513 	for (i = 0; i < bp->cp_nr_rings; i++) {
5514 		struct bnxt_napi *bnapi = bp->bnapi[i];
5515 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5516 
5517 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5518 	}
5519 }
5520 
5521 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5522 			    bool async_only)
5523 {
5524 	DECLARE_BITMAP(async_events_bmap, 256);
5525 	u32 *events = (u32 *)async_events_bmap;
5526 	struct hwrm_func_drv_rgtr_output *resp;
5527 	struct hwrm_func_drv_rgtr_input *req;
5528 	u32 flags;
5529 	int rc, i;
5530 
5531 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5532 	if (rc)
5533 		return rc;
5534 
5535 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5536 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5537 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5538 
5539 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5540 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5541 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5542 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5543 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5544 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5545 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5546 	req->flags = cpu_to_le32(flags);
5547 	req->ver_maj_8b = DRV_VER_MAJ;
5548 	req->ver_min_8b = DRV_VER_MIN;
5549 	req->ver_upd_8b = DRV_VER_UPD;
5550 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5551 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5552 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5553 
5554 	if (BNXT_PF(bp)) {
5555 		u32 data[8];
5556 		int i;
5557 
5558 		memset(data, 0, sizeof(data));
5559 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5560 			u16 cmd = bnxt_vf_req_snif[i];
5561 			unsigned int bit, idx;
5562 
5563 			idx = cmd / 32;
5564 			bit = cmd % 32;
5565 			data[idx] |= 1 << bit;
5566 		}
5567 
5568 		for (i = 0; i < 8; i++)
5569 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5570 
5571 		req->enables |=
5572 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5573 	}
5574 
5575 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5576 		req->flags |= cpu_to_le32(
5577 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5578 
5579 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5580 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5581 		u16 event_id = bnxt_async_events_arr[i];
5582 
5583 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5584 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5585 			continue;
5586 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5587 		    !bp->ptp_cfg)
5588 			continue;
5589 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5590 	}
5591 	if (bmap && bmap_size) {
5592 		for (i = 0; i < bmap_size; i++) {
5593 			if (test_bit(i, bmap))
5594 				__set_bit(i, async_events_bmap);
5595 		}
5596 	}
5597 	for (i = 0; i < 8; i++)
5598 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5599 
5600 	if (async_only)
5601 		req->enables =
5602 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5603 
5604 	resp = hwrm_req_hold(bp, req);
5605 	rc = hwrm_req_send(bp, req);
5606 	if (!rc) {
5607 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5608 		if (resp->flags &
5609 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5610 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5611 	}
5612 	hwrm_req_drop(bp, req);
5613 	return rc;
5614 }
5615 
5616 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5617 {
5618 	struct hwrm_func_drv_unrgtr_input *req;
5619 	int rc;
5620 
5621 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5622 		return 0;
5623 
5624 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5625 	if (rc)
5626 		return rc;
5627 	return hwrm_req_send(bp, req);
5628 }
5629 
5630 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5631 
5632 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5633 {
5634 	struct hwrm_tunnel_dst_port_free_input *req;
5635 	int rc;
5636 
5637 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5638 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5639 		return 0;
5640 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5641 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5642 		return 0;
5643 
5644 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5645 	if (rc)
5646 		return rc;
5647 
5648 	req->tunnel_type = tunnel_type;
5649 
5650 	switch (tunnel_type) {
5651 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5652 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5653 		bp->vxlan_port = 0;
5654 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5655 		break;
5656 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5657 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5658 		bp->nge_port = 0;
5659 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5660 		break;
5661 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5662 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5663 		bp->vxlan_gpe_port = 0;
5664 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5665 		break;
5666 	default:
5667 		break;
5668 	}
5669 
5670 	rc = hwrm_req_send(bp, req);
5671 	if (rc)
5672 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5673 			   rc);
5674 	if (bp->flags & BNXT_FLAG_TPA)
5675 		bnxt_set_tpa(bp, true);
5676 	return rc;
5677 }
5678 
5679 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5680 					   u8 tunnel_type)
5681 {
5682 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5683 	struct hwrm_tunnel_dst_port_alloc_input *req;
5684 	int rc;
5685 
5686 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5687 	if (rc)
5688 		return rc;
5689 
5690 	req->tunnel_type = tunnel_type;
5691 	req->tunnel_dst_port_val = port;
5692 
5693 	resp = hwrm_req_hold(bp, req);
5694 	rc = hwrm_req_send(bp, req);
5695 	if (rc) {
5696 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5697 			   rc);
5698 		goto err_out;
5699 	}
5700 
5701 	switch (tunnel_type) {
5702 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5703 		bp->vxlan_port = port;
5704 		bp->vxlan_fw_dst_port_id =
5705 			le16_to_cpu(resp->tunnel_dst_port_id);
5706 		break;
5707 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5708 		bp->nge_port = port;
5709 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5710 		break;
5711 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5712 		bp->vxlan_gpe_port = port;
5713 		bp->vxlan_gpe_fw_dst_port_id =
5714 			le16_to_cpu(resp->tunnel_dst_port_id);
5715 		break;
5716 	default:
5717 		break;
5718 	}
5719 	if (bp->flags & BNXT_FLAG_TPA)
5720 		bnxt_set_tpa(bp, true);
5721 
5722 err_out:
5723 	hwrm_req_drop(bp, req);
5724 	return rc;
5725 }
5726 
5727 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5728 {
5729 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5730 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5731 	int rc;
5732 
5733 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5734 	if (rc)
5735 		return rc;
5736 
5737 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5738 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5739 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5740 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5741 	}
5742 	req->mask = cpu_to_le32(vnic->rx_mask);
5743 	return hwrm_req_send_silent(bp, req);
5744 }
5745 
5746 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5747 {
5748 	if (!atomic_dec_and_test(&fltr->refcnt))
5749 		return;
5750 	spin_lock_bh(&bp->ntp_fltr_lock);
5751 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5752 		spin_unlock_bh(&bp->ntp_fltr_lock);
5753 		return;
5754 	}
5755 	hlist_del_rcu(&fltr->base.hash);
5756 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5757 	if (fltr->base.flags) {
5758 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5759 		bp->ntp_fltr_count--;
5760 	}
5761 	spin_unlock_bh(&bp->ntp_fltr_lock);
5762 	kfree_rcu(fltr, base.rcu);
5763 }
5764 
5765 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5766 						      struct bnxt_l2_key *key,
5767 						      u32 idx)
5768 {
5769 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5770 	struct bnxt_l2_filter *fltr;
5771 
5772 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5773 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5774 
5775 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5776 		    l2_key->vlan == key->vlan)
5777 			return fltr;
5778 	}
5779 	return NULL;
5780 }
5781 
5782 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5783 						    struct bnxt_l2_key *key,
5784 						    u32 idx)
5785 {
5786 	struct bnxt_l2_filter *fltr = NULL;
5787 
5788 	rcu_read_lock();
5789 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5790 	if (fltr)
5791 		atomic_inc(&fltr->refcnt);
5792 	rcu_read_unlock();
5793 	return fltr;
5794 }
5795 
5796 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5797 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5798 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5799 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5800 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5801 
5802 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5803 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5804 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5805 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5806 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5807 
5808 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5809 {
5810 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5811 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5812 			return sizeof(fkeys->addrs.v4addrs) +
5813 			       sizeof(fkeys->ports);
5814 
5815 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5816 			return sizeof(fkeys->addrs.v4addrs);
5817 	}
5818 
5819 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5820 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5821 			return sizeof(fkeys->addrs.v6addrs) +
5822 			       sizeof(fkeys->ports);
5823 
5824 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5825 			return sizeof(fkeys->addrs.v6addrs);
5826 	}
5827 
5828 	return 0;
5829 }
5830 
5831 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5832 			 const unsigned char *key)
5833 {
5834 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5835 	struct bnxt_ipv4_tuple tuple4;
5836 	struct bnxt_ipv6_tuple tuple6;
5837 	int i, j, len = 0;
5838 	u8 *four_tuple;
5839 
5840 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5841 	if (!len)
5842 		return 0;
5843 
5844 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5845 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5846 		tuple4.ports = fkeys->ports;
5847 		four_tuple = (unsigned char *)&tuple4;
5848 	} else {
5849 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5850 		tuple6.ports = fkeys->ports;
5851 		four_tuple = (unsigned char *)&tuple6;
5852 	}
5853 
5854 	for (i = 0, j = 8; i < len; i++, j++) {
5855 		u8 byte = four_tuple[i];
5856 		int bit;
5857 
5858 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5859 			if (byte & 0x80)
5860 				hash ^= prefix;
5861 		}
5862 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5863 	}
5864 
5865 	/* The valid part of the hash is in the upper 32 bits. */
5866 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5867 }
5868 
5869 #ifdef CONFIG_RFS_ACCEL
5870 static struct bnxt_l2_filter *
5871 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5872 {
5873 	struct bnxt_l2_filter *fltr;
5874 	u32 idx;
5875 
5876 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5877 	      BNXT_L2_FLTR_HASH_MASK;
5878 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5879 	return fltr;
5880 }
5881 #endif
5882 
5883 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5884 			       struct bnxt_l2_key *key, u32 idx)
5885 {
5886 	struct hlist_head *head;
5887 
5888 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5889 	fltr->l2_key.vlan = key->vlan;
5890 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5891 	if (fltr->base.flags) {
5892 		int bit_id;
5893 
5894 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5895 						 bp->max_fltr, 0);
5896 		if (bit_id < 0)
5897 			return -ENOMEM;
5898 		fltr->base.sw_id = (u16)bit_id;
5899 		bp->ntp_fltr_count++;
5900 	}
5901 	head = &bp->l2_fltr_hash_tbl[idx];
5902 	hlist_add_head_rcu(&fltr->base.hash, head);
5903 	bnxt_insert_usr_fltr(bp, &fltr->base);
5904 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5905 	atomic_set(&fltr->refcnt, 1);
5906 	return 0;
5907 }
5908 
5909 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5910 						   struct bnxt_l2_key *key,
5911 						   gfp_t gfp)
5912 {
5913 	struct bnxt_l2_filter *fltr;
5914 	u32 idx;
5915 	int rc;
5916 
5917 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5918 	      BNXT_L2_FLTR_HASH_MASK;
5919 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5920 	if (fltr)
5921 		return fltr;
5922 
5923 	fltr = kzalloc(sizeof(*fltr), gfp);
5924 	if (!fltr)
5925 		return ERR_PTR(-ENOMEM);
5926 	spin_lock_bh(&bp->ntp_fltr_lock);
5927 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5928 	spin_unlock_bh(&bp->ntp_fltr_lock);
5929 	if (rc) {
5930 		bnxt_del_l2_filter(bp, fltr);
5931 		fltr = ERR_PTR(rc);
5932 	}
5933 	return fltr;
5934 }
5935 
5936 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5937 						struct bnxt_l2_key *key,
5938 						u16 flags)
5939 {
5940 	struct bnxt_l2_filter *fltr;
5941 	u32 idx;
5942 	int rc;
5943 
5944 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5945 	      BNXT_L2_FLTR_HASH_MASK;
5946 	spin_lock_bh(&bp->ntp_fltr_lock);
5947 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5948 	if (fltr) {
5949 		fltr = ERR_PTR(-EEXIST);
5950 		goto l2_filter_exit;
5951 	}
5952 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5953 	if (!fltr) {
5954 		fltr = ERR_PTR(-ENOMEM);
5955 		goto l2_filter_exit;
5956 	}
5957 	fltr->base.flags = flags;
5958 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5959 	if (rc) {
5960 		spin_unlock_bh(&bp->ntp_fltr_lock);
5961 		bnxt_del_l2_filter(bp, fltr);
5962 		return ERR_PTR(rc);
5963 	}
5964 
5965 l2_filter_exit:
5966 	spin_unlock_bh(&bp->ntp_fltr_lock);
5967 	return fltr;
5968 }
5969 
5970 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5971 {
5972 #ifdef CONFIG_BNXT_SRIOV
5973 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5974 
5975 	return vf->fw_fid;
5976 #else
5977 	return INVALID_HW_RING_ID;
5978 #endif
5979 }
5980 
5981 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5982 {
5983 	struct hwrm_cfa_l2_filter_free_input *req;
5984 	u16 target_id = 0xffff;
5985 	int rc;
5986 
5987 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5988 		struct bnxt_pf_info *pf = &bp->pf;
5989 
5990 		if (fltr->base.vf_idx >= pf->active_vfs)
5991 			return -EINVAL;
5992 
5993 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
5994 		if (target_id == INVALID_HW_RING_ID)
5995 			return -EINVAL;
5996 	}
5997 
5998 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
5999 	if (rc)
6000 		return rc;
6001 
6002 	req->target_id = cpu_to_le16(target_id);
6003 	req->l2_filter_id = fltr->base.filter_id;
6004 	return hwrm_req_send(bp, req);
6005 }
6006 
6007 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6008 {
6009 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6010 	struct hwrm_cfa_l2_filter_alloc_input *req;
6011 	u16 target_id = 0xffff;
6012 	int rc;
6013 
6014 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6015 		struct bnxt_pf_info *pf = &bp->pf;
6016 
6017 		if (fltr->base.vf_idx >= pf->active_vfs)
6018 			return -EINVAL;
6019 
6020 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6021 	}
6022 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6023 	if (rc)
6024 		return rc;
6025 
6026 	req->target_id = cpu_to_le16(target_id);
6027 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6028 
6029 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6030 		req->flags |=
6031 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6032 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6033 	req->enables =
6034 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6035 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6036 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6037 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6038 	eth_broadcast_addr(req->l2_addr_mask);
6039 
6040 	if (fltr->l2_key.vlan) {
6041 		req->enables |=
6042 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6043 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6044 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6045 		req->num_vlans = 1;
6046 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6047 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6048 	}
6049 
6050 	resp = hwrm_req_hold(bp, req);
6051 	rc = hwrm_req_send(bp, req);
6052 	if (!rc) {
6053 		fltr->base.filter_id = resp->l2_filter_id;
6054 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6055 	}
6056 	hwrm_req_drop(bp, req);
6057 	return rc;
6058 }
6059 
6060 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6061 				     struct bnxt_ntuple_filter *fltr)
6062 {
6063 	struct hwrm_cfa_ntuple_filter_free_input *req;
6064 	int rc;
6065 
6066 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6067 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6068 	if (rc)
6069 		return rc;
6070 
6071 	req->ntuple_filter_id = fltr->base.filter_id;
6072 	return hwrm_req_send(bp, req);
6073 }
6074 
6075 #define BNXT_NTP_FLTR_FLAGS					\
6076 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6077 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6078 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6079 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6080 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6081 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6082 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6083 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6084 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6085 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6086 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6087 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6088 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6089 
6090 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6091 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6092 
6093 void bnxt_fill_ipv6_mask(__be32 mask[4])
6094 {
6095 	int i;
6096 
6097 	for (i = 0; i < 4; i++)
6098 		mask[i] = cpu_to_be32(~0);
6099 }
6100 
6101 static void
6102 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6103 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6104 			  struct bnxt_ntuple_filter *fltr)
6105 {
6106 	u16 rxq = fltr->base.rxq;
6107 
6108 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6109 		struct ethtool_rxfh_context *ctx;
6110 		struct bnxt_rss_ctx *rss_ctx;
6111 		struct bnxt_vnic_info *vnic;
6112 
6113 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6114 			      fltr->base.fw_vnic_id);
6115 		if (ctx) {
6116 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6117 			vnic = &rss_ctx->vnic;
6118 
6119 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6120 		}
6121 		return;
6122 	}
6123 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6124 		struct bnxt_vnic_info *vnic;
6125 		u32 enables;
6126 
6127 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6128 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6129 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6130 		req->enables |= cpu_to_le32(enables);
6131 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6132 	} else {
6133 		u32 flags;
6134 
6135 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6136 		req->flags |= cpu_to_le32(flags);
6137 		req->dst_id = cpu_to_le16(rxq);
6138 	}
6139 }
6140 
6141 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6142 				      struct bnxt_ntuple_filter *fltr)
6143 {
6144 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6145 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6146 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6147 	struct flow_keys *keys = &fltr->fkeys;
6148 	struct bnxt_l2_filter *l2_fltr;
6149 	struct bnxt_vnic_info *vnic;
6150 	int rc;
6151 
6152 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6153 	if (rc)
6154 		return rc;
6155 
6156 	l2_fltr = fltr->l2_fltr;
6157 	req->l2_filter_id = l2_fltr->base.filter_id;
6158 
6159 	if (fltr->base.flags & BNXT_ACT_DROP) {
6160 		req->flags =
6161 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6162 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6163 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6164 	} else {
6165 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6166 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6167 	}
6168 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6169 
6170 	req->ethertype = htons(ETH_P_IP);
6171 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6172 	req->ip_protocol = keys->basic.ip_proto;
6173 
6174 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6175 		req->ethertype = htons(ETH_P_IPV6);
6176 		req->ip_addr_type =
6177 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6178 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6179 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6180 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6181 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6182 	} else {
6183 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6184 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6185 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6186 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6187 	}
6188 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6189 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6190 		req->tunnel_type =
6191 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6192 	}
6193 
6194 	req->src_port = keys->ports.src;
6195 	req->src_port_mask = masks->ports.src;
6196 	req->dst_port = keys->ports.dst;
6197 	req->dst_port_mask = masks->ports.dst;
6198 
6199 	resp = hwrm_req_hold(bp, req);
6200 	rc = hwrm_req_send(bp, req);
6201 	if (!rc)
6202 		fltr->base.filter_id = resp->ntuple_filter_id;
6203 	hwrm_req_drop(bp, req);
6204 	return rc;
6205 }
6206 
6207 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6208 				     const u8 *mac_addr)
6209 {
6210 	struct bnxt_l2_filter *fltr;
6211 	struct bnxt_l2_key key;
6212 	int rc;
6213 
6214 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6215 	key.vlan = 0;
6216 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6217 	if (IS_ERR(fltr))
6218 		return PTR_ERR(fltr);
6219 
6220 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6221 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6222 	if (rc)
6223 		bnxt_del_l2_filter(bp, fltr);
6224 	else
6225 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6226 	return rc;
6227 }
6228 
6229 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6230 {
6231 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6232 
6233 	/* Any associated ntuple filters will also be cleared by firmware. */
6234 	for (i = 0; i < num_of_vnics; i++) {
6235 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6236 
6237 		for (j = 0; j < vnic->uc_filter_count; j++) {
6238 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6239 
6240 			bnxt_hwrm_l2_filter_free(bp, fltr);
6241 			bnxt_del_l2_filter(bp, fltr);
6242 		}
6243 		vnic->uc_filter_count = 0;
6244 	}
6245 }
6246 
6247 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6248 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6249 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6250 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6251 
6252 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6253 					   struct hwrm_vnic_tpa_cfg_input *req)
6254 {
6255 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6256 
6257 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6258 		return;
6259 
6260 	if (bp->vxlan_port)
6261 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6262 	if (bp->vxlan_gpe_port)
6263 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6264 	if (bp->nge_port)
6265 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6266 
6267 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6268 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6269 }
6270 
6271 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6272 			   u32 tpa_flags)
6273 {
6274 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6275 	struct hwrm_vnic_tpa_cfg_input *req;
6276 	int rc;
6277 
6278 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6279 		return 0;
6280 
6281 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6282 	if (rc)
6283 		return rc;
6284 
6285 	if (tpa_flags) {
6286 		u16 mss = bp->dev->mtu - 40;
6287 		u32 nsegs, n, segs = 0, flags;
6288 
6289 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6290 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6291 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6292 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6293 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6294 		if (tpa_flags & BNXT_FLAG_GRO)
6295 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6296 
6297 		req->flags = cpu_to_le32(flags);
6298 
6299 		req->enables =
6300 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6301 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6302 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6303 
6304 		/* Number of segs are log2 units, and first packet is not
6305 		 * included as part of this units.
6306 		 */
6307 		if (mss <= BNXT_RX_PAGE_SIZE) {
6308 			n = BNXT_RX_PAGE_SIZE / mss;
6309 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6310 		} else {
6311 			n = mss / BNXT_RX_PAGE_SIZE;
6312 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6313 				n++;
6314 			nsegs = (MAX_SKB_FRAGS - n) / n;
6315 		}
6316 
6317 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6318 			segs = MAX_TPA_SEGS_P5;
6319 			max_aggs = bp->max_tpa;
6320 		} else {
6321 			segs = ilog2(nsegs);
6322 		}
6323 		req->max_agg_segs = cpu_to_le16(segs);
6324 		req->max_aggs = cpu_to_le16(max_aggs);
6325 
6326 		req->min_agg_len = cpu_to_le32(512);
6327 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6328 	}
6329 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6330 
6331 	return hwrm_req_send(bp, req);
6332 }
6333 
6334 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6335 {
6336 	struct bnxt_ring_grp_info *grp_info;
6337 
6338 	grp_info = &bp->grp_info[ring->grp_idx];
6339 	return grp_info->cp_fw_ring_id;
6340 }
6341 
6342 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6343 {
6344 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6345 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6346 	else
6347 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6348 }
6349 
6350 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6351 {
6352 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6353 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6354 	else
6355 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6356 }
6357 
6358 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6359 {
6360 	int entries;
6361 
6362 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6363 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6364 	else
6365 		entries = HW_HASH_INDEX_SIZE;
6366 
6367 	bp->rss_indir_tbl_entries = entries;
6368 	bp->rss_indir_tbl =
6369 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6370 	if (!bp->rss_indir_tbl)
6371 		return -ENOMEM;
6372 
6373 	return 0;
6374 }
6375 
6376 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6377 				 struct ethtool_rxfh_context *rss_ctx)
6378 {
6379 	u16 max_rings, max_entries, pad, i;
6380 	u32 *rss_indir_tbl;
6381 
6382 	if (!bp->rx_nr_rings)
6383 		return;
6384 
6385 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6386 		max_rings = bp->rx_nr_rings - 1;
6387 	else
6388 		max_rings = bp->rx_nr_rings;
6389 
6390 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6391 	if (rss_ctx)
6392 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6393 	else
6394 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6395 
6396 	for (i = 0; i < max_entries; i++)
6397 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6398 
6399 	pad = bp->rss_indir_tbl_entries - max_entries;
6400 	if (pad)
6401 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6402 }
6403 
6404 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6405 {
6406 	u32 i, tbl_size, max_ring = 0;
6407 
6408 	if (!bp->rss_indir_tbl)
6409 		return 0;
6410 
6411 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6412 	for (i = 0; i < tbl_size; i++)
6413 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6414 	return max_ring;
6415 }
6416 
6417 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6418 {
6419 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6420 		if (!rx_rings)
6421 			return 0;
6422 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6423 					       BNXT_RSS_TABLE_ENTRIES_P5);
6424 	}
6425 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6426 		return 2;
6427 	return 1;
6428 }
6429 
6430 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6431 {
6432 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6433 	u16 i, j;
6434 
6435 	/* Fill the RSS indirection table with ring group ids */
6436 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6437 		if (!no_rss)
6438 			j = bp->rss_indir_tbl[i];
6439 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6440 	}
6441 }
6442 
6443 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6444 				    struct bnxt_vnic_info *vnic)
6445 {
6446 	__le16 *ring_tbl = vnic->rss_table;
6447 	struct bnxt_rx_ring_info *rxr;
6448 	u16 tbl_size, i;
6449 
6450 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6451 
6452 	for (i = 0; i < tbl_size; i++) {
6453 		u16 ring_id, j;
6454 
6455 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6456 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6457 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6458 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6459 		else
6460 			j = bp->rss_indir_tbl[i];
6461 		rxr = &bp->rx_ring[j];
6462 
6463 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6464 		*ring_tbl++ = cpu_to_le16(ring_id);
6465 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6466 		*ring_tbl++ = cpu_to_le16(ring_id);
6467 	}
6468 }
6469 
6470 static void
6471 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6472 			 struct bnxt_vnic_info *vnic)
6473 {
6474 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6475 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6476 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6477 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6478 	} else {
6479 		bnxt_fill_hw_rss_tbl(bp, vnic);
6480 	}
6481 
6482 	if (bp->rss_hash_delta) {
6483 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6484 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6485 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6486 		else
6487 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6488 	} else {
6489 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6490 	}
6491 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6492 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6493 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6494 }
6495 
6496 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6497 				  bool set_rss)
6498 {
6499 	struct hwrm_vnic_rss_cfg_input *req;
6500 	int rc;
6501 
6502 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6503 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6504 		return 0;
6505 
6506 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6507 	if (rc)
6508 		return rc;
6509 
6510 	if (set_rss)
6511 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6512 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6513 	return hwrm_req_send(bp, req);
6514 }
6515 
6516 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6517 				     struct bnxt_vnic_info *vnic, bool set_rss)
6518 {
6519 	struct hwrm_vnic_rss_cfg_input *req;
6520 	dma_addr_t ring_tbl_map;
6521 	u32 i, nr_ctxs;
6522 	int rc;
6523 
6524 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6525 	if (rc)
6526 		return rc;
6527 
6528 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6529 	if (!set_rss)
6530 		return hwrm_req_send(bp, req);
6531 
6532 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6533 	ring_tbl_map = vnic->rss_table_dma_addr;
6534 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6535 
6536 	hwrm_req_hold(bp, req);
6537 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6538 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6539 		req->ring_table_pair_index = i;
6540 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6541 		rc = hwrm_req_send(bp, req);
6542 		if (rc)
6543 			goto exit;
6544 	}
6545 
6546 exit:
6547 	hwrm_req_drop(bp, req);
6548 	return rc;
6549 }
6550 
6551 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6552 {
6553 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6554 	struct hwrm_vnic_rss_qcfg_output *resp;
6555 	struct hwrm_vnic_rss_qcfg_input *req;
6556 
6557 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6558 		return;
6559 
6560 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6561 	/* all contexts configured to same hash_type, zero always exists */
6562 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6563 	resp = hwrm_req_hold(bp, req);
6564 	if (!hwrm_req_send(bp, req)) {
6565 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6566 		bp->rss_hash_delta = 0;
6567 	}
6568 	hwrm_req_drop(bp, req);
6569 }
6570 
6571 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6572 {
6573 	u16 hds_thresh = (u16)bp->dev->ethtool->hds_thresh;
6574 	struct hwrm_vnic_plcmodes_cfg_input *req;
6575 	int rc;
6576 
6577 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6578 	if (rc)
6579 		return rc;
6580 
6581 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6582 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6583 	req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6584 
6585 	if (!BNXT_RX_PAGE_MODE(bp) && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
6586 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6587 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6588 		req->enables |=
6589 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6590 		req->hds_threshold = cpu_to_le16(hds_thresh);
6591 	}
6592 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6593 	return hwrm_req_send(bp, req);
6594 }
6595 
6596 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6597 					struct bnxt_vnic_info *vnic,
6598 					u16 ctx_idx)
6599 {
6600 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6601 
6602 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6603 		return;
6604 
6605 	req->rss_cos_lb_ctx_id =
6606 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6607 
6608 	hwrm_req_send(bp, req);
6609 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6610 }
6611 
6612 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6613 {
6614 	int i, j;
6615 
6616 	for (i = 0; i < bp->nr_vnics; i++) {
6617 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6618 
6619 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6620 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6621 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6622 		}
6623 	}
6624 	bp->rsscos_nr_ctxs = 0;
6625 }
6626 
6627 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6628 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6629 {
6630 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6631 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6632 	int rc;
6633 
6634 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6635 	if (rc)
6636 		return rc;
6637 
6638 	resp = hwrm_req_hold(bp, req);
6639 	rc = hwrm_req_send(bp, req);
6640 	if (!rc)
6641 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6642 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6643 	hwrm_req_drop(bp, req);
6644 
6645 	return rc;
6646 }
6647 
6648 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6649 {
6650 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6651 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6652 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6653 }
6654 
6655 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6656 {
6657 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6658 	struct hwrm_vnic_cfg_input *req;
6659 	unsigned int ring = 0, grp_idx;
6660 	u16 def_vlan = 0;
6661 	int rc;
6662 
6663 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6664 	if (rc)
6665 		return rc;
6666 
6667 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6668 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6669 
6670 		req->default_rx_ring_id =
6671 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6672 		req->default_cmpl_ring_id =
6673 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6674 		req->enables =
6675 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6676 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6677 		goto vnic_mru;
6678 	}
6679 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6680 	/* Only RSS support for now TBD: COS & LB */
6681 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6682 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6683 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6684 					   VNIC_CFG_REQ_ENABLES_MRU);
6685 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6686 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6687 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6688 					   VNIC_CFG_REQ_ENABLES_MRU);
6689 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6690 	} else {
6691 		req->rss_rule = cpu_to_le16(0xffff);
6692 	}
6693 
6694 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6695 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6696 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6697 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6698 	} else {
6699 		req->cos_rule = cpu_to_le16(0xffff);
6700 	}
6701 
6702 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6703 		ring = 0;
6704 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6705 		ring = vnic->vnic_id - 1;
6706 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6707 		ring = bp->rx_nr_rings - 1;
6708 
6709 	grp_idx = bp->rx_ring[ring].bnapi->index;
6710 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6711 	req->lb_rule = cpu_to_le16(0xffff);
6712 vnic_mru:
6713 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6714 	req->mru = cpu_to_le16(vnic->mru);
6715 
6716 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6717 #ifdef CONFIG_BNXT_SRIOV
6718 	if (BNXT_VF(bp))
6719 		def_vlan = bp->vf.vlan;
6720 #endif
6721 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6722 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6723 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6724 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6725 
6726 	return hwrm_req_send(bp, req);
6727 }
6728 
6729 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6730 				    struct bnxt_vnic_info *vnic)
6731 {
6732 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6733 		struct hwrm_vnic_free_input *req;
6734 
6735 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6736 			return;
6737 
6738 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6739 
6740 		hwrm_req_send(bp, req);
6741 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6742 	}
6743 }
6744 
6745 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6746 {
6747 	u16 i;
6748 
6749 	for (i = 0; i < bp->nr_vnics; i++)
6750 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6751 }
6752 
6753 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6754 			 unsigned int start_rx_ring_idx,
6755 			 unsigned int nr_rings)
6756 {
6757 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6758 	struct hwrm_vnic_alloc_output *resp;
6759 	struct hwrm_vnic_alloc_input *req;
6760 	int rc;
6761 
6762 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6763 	if (rc)
6764 		return rc;
6765 
6766 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6767 		goto vnic_no_ring_grps;
6768 
6769 	/* map ring groups to this vnic */
6770 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6771 		grp_idx = bp->rx_ring[i].bnapi->index;
6772 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6773 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6774 				   j, nr_rings);
6775 			break;
6776 		}
6777 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6778 	}
6779 
6780 vnic_no_ring_grps:
6781 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6782 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6783 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6784 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6785 
6786 	resp = hwrm_req_hold(bp, req);
6787 	rc = hwrm_req_send(bp, req);
6788 	if (!rc)
6789 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6790 	hwrm_req_drop(bp, req);
6791 	return rc;
6792 }
6793 
6794 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6795 {
6796 	struct hwrm_vnic_qcaps_output *resp;
6797 	struct hwrm_vnic_qcaps_input *req;
6798 	int rc;
6799 
6800 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6801 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6802 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6803 	if (bp->hwrm_spec_code < 0x10600)
6804 		return 0;
6805 
6806 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6807 	if (rc)
6808 		return rc;
6809 
6810 	resp = hwrm_req_hold(bp, req);
6811 	rc = hwrm_req_send(bp, req);
6812 	if (!rc) {
6813 		u32 flags = le32_to_cpu(resp->flags);
6814 
6815 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6816 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6817 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6818 		if (flags &
6819 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6820 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6821 
6822 		/* Older P5 fw before EXT_HW_STATS support did not set
6823 		 * VLAN_STRIP_CAP properly.
6824 		 */
6825 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6826 		    (BNXT_CHIP_P5(bp) &&
6827 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6828 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6829 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6830 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6831 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6832 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6833 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6834 		if (bp->max_tpa_v2) {
6835 			if (BNXT_CHIP_P5(bp))
6836 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6837 			else
6838 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6839 		}
6840 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6841 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6842 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6843 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6844 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6845 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6846 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6847 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6848 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6849 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6850 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6851 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6852 	}
6853 	hwrm_req_drop(bp, req);
6854 	return rc;
6855 }
6856 
6857 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6858 {
6859 	struct hwrm_ring_grp_alloc_output *resp;
6860 	struct hwrm_ring_grp_alloc_input *req;
6861 	int rc;
6862 	u16 i;
6863 
6864 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6865 		return 0;
6866 
6867 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6868 	if (rc)
6869 		return rc;
6870 
6871 	resp = hwrm_req_hold(bp, req);
6872 	for (i = 0; i < bp->rx_nr_rings; i++) {
6873 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6874 
6875 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6876 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6877 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6878 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6879 
6880 		rc = hwrm_req_send(bp, req);
6881 
6882 		if (rc)
6883 			break;
6884 
6885 		bp->grp_info[grp_idx].fw_grp_id =
6886 			le32_to_cpu(resp->ring_group_id);
6887 	}
6888 	hwrm_req_drop(bp, req);
6889 	return rc;
6890 }
6891 
6892 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6893 {
6894 	struct hwrm_ring_grp_free_input *req;
6895 	u16 i;
6896 
6897 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6898 		return;
6899 
6900 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6901 		return;
6902 
6903 	hwrm_req_hold(bp, req);
6904 	for (i = 0; i < bp->cp_nr_rings; i++) {
6905 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6906 			continue;
6907 		req->ring_group_id =
6908 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6909 
6910 		hwrm_req_send(bp, req);
6911 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6912 	}
6913 	hwrm_req_drop(bp, req);
6914 }
6915 
6916 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6917 				    struct bnxt_ring_struct *ring,
6918 				    u32 ring_type, u32 map_index)
6919 {
6920 	struct hwrm_ring_alloc_output *resp;
6921 	struct hwrm_ring_alloc_input *req;
6922 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6923 	struct bnxt_ring_grp_info *grp_info;
6924 	int rc, err = 0;
6925 	u16 ring_id;
6926 
6927 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6928 	if (rc)
6929 		goto exit;
6930 
6931 	req->enables = 0;
6932 	if (rmem->nr_pages > 1) {
6933 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6934 		/* Page size is in log2 units */
6935 		req->page_size = BNXT_PAGE_SHIFT;
6936 		req->page_tbl_depth = 1;
6937 	} else {
6938 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6939 	}
6940 	req->fbo = 0;
6941 	/* Association of ring index with doorbell index and MSIX number */
6942 	req->logical_id = cpu_to_le16(map_index);
6943 
6944 	switch (ring_type) {
6945 	case HWRM_RING_ALLOC_TX: {
6946 		struct bnxt_tx_ring_info *txr;
6947 		u16 flags = 0;
6948 
6949 		txr = container_of(ring, struct bnxt_tx_ring_info,
6950 				   tx_ring_struct);
6951 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6952 		/* Association of transmit ring with completion ring */
6953 		grp_info = &bp->grp_info[ring->grp_idx];
6954 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6955 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6956 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6957 		req->queue_id = cpu_to_le16(ring->queue_id);
6958 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6959 			req->cmpl_coal_cnt =
6960 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6961 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6962 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6963 		req->flags = cpu_to_le16(flags);
6964 		break;
6965 	}
6966 	case HWRM_RING_ALLOC_RX:
6967 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6968 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6969 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6970 			u16 flags = 0;
6971 
6972 			/* Association of rx ring with stats context */
6973 			grp_info = &bp->grp_info[ring->grp_idx];
6974 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6975 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6976 			req->enables |= cpu_to_le32(
6977 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6978 			if (NET_IP_ALIGN == 2)
6979 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6980 			req->flags = cpu_to_le16(flags);
6981 		}
6982 		break;
6983 	case HWRM_RING_ALLOC_AGG:
6984 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6985 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6986 			/* Association of agg ring with rx ring */
6987 			grp_info = &bp->grp_info[ring->grp_idx];
6988 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6989 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
6990 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6991 			req->enables |= cpu_to_le32(
6992 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
6993 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6994 		} else {
6995 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6996 		}
6997 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
6998 		break;
6999 	case HWRM_RING_ALLOC_CMPL:
7000 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7001 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7002 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7003 			/* Association of cp ring with nq */
7004 			grp_info = &bp->grp_info[map_index];
7005 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7006 			req->cq_handle = cpu_to_le64(ring->handle);
7007 			req->enables |= cpu_to_le32(
7008 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7009 		} else {
7010 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7011 		}
7012 		break;
7013 	case HWRM_RING_ALLOC_NQ:
7014 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7015 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7016 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7017 		break;
7018 	default:
7019 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7020 			   ring_type);
7021 		return -1;
7022 	}
7023 
7024 	resp = hwrm_req_hold(bp, req);
7025 	rc = hwrm_req_send(bp, req);
7026 	err = le16_to_cpu(resp->error_code);
7027 	ring_id = le16_to_cpu(resp->ring_id);
7028 	hwrm_req_drop(bp, req);
7029 
7030 exit:
7031 	if (rc || err) {
7032 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7033 			   ring_type, rc, err);
7034 		return -EIO;
7035 	}
7036 	ring->fw_ring_id = ring_id;
7037 	return rc;
7038 }
7039 
7040 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7041 {
7042 	int rc;
7043 
7044 	if (BNXT_PF(bp)) {
7045 		struct hwrm_func_cfg_input *req;
7046 
7047 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7048 		if (rc)
7049 			return rc;
7050 
7051 		req->fid = cpu_to_le16(0xffff);
7052 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7053 		req->async_event_cr = cpu_to_le16(idx);
7054 		return hwrm_req_send(bp, req);
7055 	} else {
7056 		struct hwrm_func_vf_cfg_input *req;
7057 
7058 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7059 		if (rc)
7060 			return rc;
7061 
7062 		req->enables =
7063 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7064 		req->async_event_cr = cpu_to_le16(idx);
7065 		return hwrm_req_send(bp, req);
7066 	}
7067 }
7068 
7069 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7070 			     u32 ring_type)
7071 {
7072 	switch (ring_type) {
7073 	case HWRM_RING_ALLOC_TX:
7074 		db->db_ring_mask = bp->tx_ring_mask;
7075 		break;
7076 	case HWRM_RING_ALLOC_RX:
7077 		db->db_ring_mask = bp->rx_ring_mask;
7078 		break;
7079 	case HWRM_RING_ALLOC_AGG:
7080 		db->db_ring_mask = bp->rx_agg_ring_mask;
7081 		break;
7082 	case HWRM_RING_ALLOC_CMPL:
7083 	case HWRM_RING_ALLOC_NQ:
7084 		db->db_ring_mask = bp->cp_ring_mask;
7085 		break;
7086 	}
7087 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7088 		db->db_epoch_mask = db->db_ring_mask + 1;
7089 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7090 	}
7091 }
7092 
7093 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7094 			u32 map_idx, u32 xid)
7095 {
7096 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7097 		switch (ring_type) {
7098 		case HWRM_RING_ALLOC_TX:
7099 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7100 			break;
7101 		case HWRM_RING_ALLOC_RX:
7102 		case HWRM_RING_ALLOC_AGG:
7103 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7104 			break;
7105 		case HWRM_RING_ALLOC_CMPL:
7106 			db->db_key64 = DBR_PATH_L2;
7107 			break;
7108 		case HWRM_RING_ALLOC_NQ:
7109 			db->db_key64 = DBR_PATH_L2;
7110 			break;
7111 		}
7112 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7113 
7114 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7115 			db->db_key64 |= DBR_VALID;
7116 
7117 		db->doorbell = bp->bar1 + bp->db_offset;
7118 	} else {
7119 		db->doorbell = bp->bar1 + map_idx * 0x80;
7120 		switch (ring_type) {
7121 		case HWRM_RING_ALLOC_TX:
7122 			db->db_key32 = DB_KEY_TX;
7123 			break;
7124 		case HWRM_RING_ALLOC_RX:
7125 		case HWRM_RING_ALLOC_AGG:
7126 			db->db_key32 = DB_KEY_RX;
7127 			break;
7128 		case HWRM_RING_ALLOC_CMPL:
7129 			db->db_key32 = DB_KEY_CP;
7130 			break;
7131 		}
7132 	}
7133 	bnxt_set_db_mask(bp, db, ring_type);
7134 }
7135 
7136 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7137 				   struct bnxt_rx_ring_info *rxr)
7138 {
7139 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7140 	struct bnxt_napi *bnapi = rxr->bnapi;
7141 	u32 type = HWRM_RING_ALLOC_RX;
7142 	u32 map_idx = bnapi->index;
7143 	int rc;
7144 
7145 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7146 	if (rc)
7147 		return rc;
7148 
7149 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7150 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7151 
7152 	return 0;
7153 }
7154 
7155 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7156 				       struct bnxt_rx_ring_info *rxr)
7157 {
7158 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7159 	u32 type = HWRM_RING_ALLOC_AGG;
7160 	u32 grp_idx = ring->grp_idx;
7161 	u32 map_idx;
7162 	int rc;
7163 
7164 	map_idx = grp_idx + bp->rx_nr_rings;
7165 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7166 	if (rc)
7167 		return rc;
7168 
7169 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7170 		    ring->fw_ring_id);
7171 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7172 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7173 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7174 
7175 	return 0;
7176 }
7177 
7178 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7179 {
7180 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7181 	int i, rc = 0;
7182 	u32 type;
7183 
7184 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7185 		type = HWRM_RING_ALLOC_NQ;
7186 	else
7187 		type = HWRM_RING_ALLOC_CMPL;
7188 	for (i = 0; i < bp->cp_nr_rings; i++) {
7189 		struct bnxt_napi *bnapi = bp->bnapi[i];
7190 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7191 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7192 		u32 map_idx = ring->map_idx;
7193 		unsigned int vector;
7194 
7195 		vector = bp->irq_tbl[map_idx].vector;
7196 		disable_irq_nosync(vector);
7197 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7198 		if (rc) {
7199 			enable_irq(vector);
7200 			goto err_out;
7201 		}
7202 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7203 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7204 		enable_irq(vector);
7205 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7206 
7207 		if (!i) {
7208 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7209 			if (rc)
7210 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7211 		}
7212 	}
7213 
7214 	type = HWRM_RING_ALLOC_TX;
7215 	for (i = 0; i < bp->tx_nr_rings; i++) {
7216 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7217 		struct bnxt_ring_struct *ring;
7218 		u32 map_idx;
7219 
7220 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7221 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7222 			struct bnxt_napi *bnapi = txr->bnapi;
7223 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7224 
7225 			ring = &cpr2->cp_ring_struct;
7226 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7227 			map_idx = bnapi->index;
7228 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7229 			if (rc)
7230 				goto err_out;
7231 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7232 				    ring->fw_ring_id);
7233 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7234 		}
7235 		ring = &txr->tx_ring_struct;
7236 		map_idx = i;
7237 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7238 		if (rc)
7239 			goto err_out;
7240 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7241 	}
7242 
7243 	for (i = 0; i < bp->rx_nr_rings; i++) {
7244 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7245 
7246 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7247 		if (rc)
7248 			goto err_out;
7249 		/* If we have agg rings, post agg buffers first. */
7250 		if (!agg_rings)
7251 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7252 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7253 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7254 			struct bnxt_napi *bnapi = rxr->bnapi;
7255 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7256 			struct bnxt_ring_struct *ring;
7257 			u32 map_idx = bnapi->index;
7258 
7259 			ring = &cpr2->cp_ring_struct;
7260 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7261 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7262 			if (rc)
7263 				goto err_out;
7264 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7265 				    ring->fw_ring_id);
7266 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7267 		}
7268 	}
7269 
7270 	if (agg_rings) {
7271 		for (i = 0; i < bp->rx_nr_rings; i++) {
7272 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7273 			if (rc)
7274 				goto err_out;
7275 		}
7276 	}
7277 err_out:
7278 	return rc;
7279 }
7280 
7281 static void bnxt_cancel_dim(struct bnxt *bp)
7282 {
7283 	int i;
7284 
7285 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7286 	 * if NAPI is enabled.
7287 	 */
7288 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7289 		return;
7290 
7291 	/* Make sure NAPI sees that the VNIC is disabled */
7292 	synchronize_net();
7293 	for (i = 0; i < bp->rx_nr_rings; i++) {
7294 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7295 		struct bnxt_napi *bnapi = rxr->bnapi;
7296 
7297 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7298 	}
7299 }
7300 
7301 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7302 				   struct bnxt_ring_struct *ring,
7303 				   u32 ring_type, int cmpl_ring_id)
7304 {
7305 	struct hwrm_ring_free_output *resp;
7306 	struct hwrm_ring_free_input *req;
7307 	u16 error_code = 0;
7308 	int rc;
7309 
7310 	if (BNXT_NO_FW_ACCESS(bp))
7311 		return 0;
7312 
7313 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7314 	if (rc)
7315 		goto exit;
7316 
7317 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7318 	req->ring_type = ring_type;
7319 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7320 
7321 	resp = hwrm_req_hold(bp, req);
7322 	rc = hwrm_req_send(bp, req);
7323 	error_code = le16_to_cpu(resp->error_code);
7324 	hwrm_req_drop(bp, req);
7325 exit:
7326 	if (rc || error_code) {
7327 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7328 			   ring_type, rc, error_code);
7329 		return -EIO;
7330 	}
7331 	return 0;
7332 }
7333 
7334 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7335 				   struct bnxt_rx_ring_info *rxr,
7336 				   bool close_path)
7337 {
7338 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7339 	u32 grp_idx = rxr->bnapi->index;
7340 	u32 cmpl_ring_id;
7341 
7342 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7343 		return;
7344 
7345 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7346 	hwrm_ring_free_send_msg(bp, ring,
7347 				RING_FREE_REQ_RING_TYPE_RX,
7348 				close_path ? cmpl_ring_id :
7349 				INVALID_HW_RING_ID);
7350 	ring->fw_ring_id = INVALID_HW_RING_ID;
7351 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7352 }
7353 
7354 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7355 				       struct bnxt_rx_ring_info *rxr,
7356 				       bool close_path)
7357 {
7358 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7359 	u32 grp_idx = rxr->bnapi->index;
7360 	u32 type, cmpl_ring_id;
7361 
7362 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7363 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7364 	else
7365 		type = RING_FREE_REQ_RING_TYPE_RX;
7366 
7367 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7368 		return;
7369 
7370 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7371 	hwrm_ring_free_send_msg(bp, ring, type,
7372 				close_path ? cmpl_ring_id :
7373 				INVALID_HW_RING_ID);
7374 	ring->fw_ring_id = INVALID_HW_RING_ID;
7375 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7376 }
7377 
7378 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7379 {
7380 	u32 type;
7381 	int i;
7382 
7383 	if (!bp->bnapi)
7384 		return;
7385 
7386 	for (i = 0; i < bp->tx_nr_rings; i++) {
7387 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7388 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7389 
7390 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7391 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7392 
7393 			hwrm_ring_free_send_msg(bp, ring,
7394 						RING_FREE_REQ_RING_TYPE_TX,
7395 						close_path ? cmpl_ring_id :
7396 						INVALID_HW_RING_ID);
7397 			ring->fw_ring_id = INVALID_HW_RING_ID;
7398 		}
7399 	}
7400 
7401 	bnxt_cancel_dim(bp);
7402 	for (i = 0; i < bp->rx_nr_rings; i++) {
7403 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7404 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7405 	}
7406 
7407 	/* The completion rings are about to be freed.  After that the
7408 	 * IRQ doorbell will not work anymore.  So we need to disable
7409 	 * IRQ here.
7410 	 */
7411 	bnxt_disable_int_sync(bp);
7412 
7413 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7414 		type = RING_FREE_REQ_RING_TYPE_NQ;
7415 	else
7416 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7417 	for (i = 0; i < bp->cp_nr_rings; i++) {
7418 		struct bnxt_napi *bnapi = bp->bnapi[i];
7419 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7420 		struct bnxt_ring_struct *ring;
7421 		int j;
7422 
7423 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7424 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7425 
7426 			ring = &cpr2->cp_ring_struct;
7427 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7428 				continue;
7429 			hwrm_ring_free_send_msg(bp, ring,
7430 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7431 						INVALID_HW_RING_ID);
7432 			ring->fw_ring_id = INVALID_HW_RING_ID;
7433 		}
7434 		ring = &cpr->cp_ring_struct;
7435 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7436 			hwrm_ring_free_send_msg(bp, ring, type,
7437 						INVALID_HW_RING_ID);
7438 			ring->fw_ring_id = INVALID_HW_RING_ID;
7439 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7440 		}
7441 	}
7442 }
7443 
7444 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7445 			     bool shared);
7446 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7447 			   bool shared);
7448 
7449 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7450 {
7451 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7452 	struct hwrm_func_qcfg_output *resp;
7453 	struct hwrm_func_qcfg_input *req;
7454 	int rc;
7455 
7456 	if (bp->hwrm_spec_code < 0x10601)
7457 		return 0;
7458 
7459 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7460 	if (rc)
7461 		return rc;
7462 
7463 	req->fid = cpu_to_le16(0xffff);
7464 	resp = hwrm_req_hold(bp, req);
7465 	rc = hwrm_req_send(bp, req);
7466 	if (rc) {
7467 		hwrm_req_drop(bp, req);
7468 		return rc;
7469 	}
7470 
7471 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7472 	if (BNXT_NEW_RM(bp)) {
7473 		u16 cp, stats;
7474 
7475 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7476 		hw_resc->resv_hw_ring_grps =
7477 			le32_to_cpu(resp->alloc_hw_ring_grps);
7478 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7479 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7480 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7481 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7482 		hw_resc->resv_irqs = cp;
7483 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7484 			int rx = hw_resc->resv_rx_rings;
7485 			int tx = hw_resc->resv_tx_rings;
7486 
7487 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7488 				rx >>= 1;
7489 			if (cp < (rx + tx)) {
7490 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7491 				if (rc)
7492 					goto get_rings_exit;
7493 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7494 					rx <<= 1;
7495 				hw_resc->resv_rx_rings = rx;
7496 				hw_resc->resv_tx_rings = tx;
7497 			}
7498 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7499 			hw_resc->resv_hw_ring_grps = rx;
7500 		}
7501 		hw_resc->resv_cp_rings = cp;
7502 		hw_resc->resv_stat_ctxs = stats;
7503 	}
7504 get_rings_exit:
7505 	hwrm_req_drop(bp, req);
7506 	return rc;
7507 }
7508 
7509 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7510 {
7511 	struct hwrm_func_qcfg_output *resp;
7512 	struct hwrm_func_qcfg_input *req;
7513 	int rc;
7514 
7515 	if (bp->hwrm_spec_code < 0x10601)
7516 		return 0;
7517 
7518 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7519 	if (rc)
7520 		return rc;
7521 
7522 	req->fid = cpu_to_le16(fid);
7523 	resp = hwrm_req_hold(bp, req);
7524 	rc = hwrm_req_send(bp, req);
7525 	if (!rc)
7526 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7527 
7528 	hwrm_req_drop(bp, req);
7529 	return rc;
7530 }
7531 
7532 static bool bnxt_rfs_supported(struct bnxt *bp);
7533 
7534 static struct hwrm_func_cfg_input *
7535 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7536 {
7537 	struct hwrm_func_cfg_input *req;
7538 	u32 enables = 0;
7539 
7540 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7541 		return NULL;
7542 
7543 	req->fid = cpu_to_le16(0xffff);
7544 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7545 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7546 	if (BNXT_NEW_RM(bp)) {
7547 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7548 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7549 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7550 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7551 			enables |= hwr->cp_p5 ?
7552 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7553 		} else {
7554 			enables |= hwr->cp ?
7555 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7556 			enables |= hwr->grp ?
7557 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7558 		}
7559 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7560 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7561 					  0;
7562 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7563 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7564 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7565 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7566 			req->num_msix = cpu_to_le16(hwr->cp);
7567 		} else {
7568 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7569 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7570 		}
7571 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7572 		req->num_vnics = cpu_to_le16(hwr->vnic);
7573 	}
7574 	req->enables = cpu_to_le32(enables);
7575 	return req;
7576 }
7577 
7578 static struct hwrm_func_vf_cfg_input *
7579 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7580 {
7581 	struct hwrm_func_vf_cfg_input *req;
7582 	u32 enables = 0;
7583 
7584 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7585 		return NULL;
7586 
7587 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7588 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7589 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7590 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7591 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7592 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7593 		enables |= hwr->cp_p5 ?
7594 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7595 	} else {
7596 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7597 		enables |= hwr->grp ?
7598 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7599 	}
7600 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7601 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7602 
7603 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7604 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7605 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7606 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7607 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7608 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7609 	} else {
7610 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7611 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7612 	}
7613 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7614 	req->num_vnics = cpu_to_le16(hwr->vnic);
7615 
7616 	req->enables = cpu_to_le32(enables);
7617 	return req;
7618 }
7619 
7620 static int
7621 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7622 {
7623 	struct hwrm_func_cfg_input *req;
7624 	int rc;
7625 
7626 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7627 	if (!req)
7628 		return -ENOMEM;
7629 
7630 	if (!req->enables) {
7631 		hwrm_req_drop(bp, req);
7632 		return 0;
7633 	}
7634 
7635 	rc = hwrm_req_send(bp, req);
7636 	if (rc)
7637 		return rc;
7638 
7639 	if (bp->hwrm_spec_code < 0x10601)
7640 		bp->hw_resc.resv_tx_rings = hwr->tx;
7641 
7642 	return bnxt_hwrm_get_rings(bp);
7643 }
7644 
7645 static int
7646 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7647 {
7648 	struct hwrm_func_vf_cfg_input *req;
7649 	int rc;
7650 
7651 	if (!BNXT_NEW_RM(bp)) {
7652 		bp->hw_resc.resv_tx_rings = hwr->tx;
7653 		return 0;
7654 	}
7655 
7656 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7657 	if (!req)
7658 		return -ENOMEM;
7659 
7660 	rc = hwrm_req_send(bp, req);
7661 	if (rc)
7662 		return rc;
7663 
7664 	return bnxt_hwrm_get_rings(bp);
7665 }
7666 
7667 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7668 {
7669 	if (BNXT_PF(bp))
7670 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7671 	else
7672 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7673 }
7674 
7675 int bnxt_nq_rings_in_use(struct bnxt *bp)
7676 {
7677 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7678 }
7679 
7680 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7681 {
7682 	int cp;
7683 
7684 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7685 		return bnxt_nq_rings_in_use(bp);
7686 
7687 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7688 	return cp;
7689 }
7690 
7691 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7692 {
7693 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7694 }
7695 
7696 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7697 {
7698 	if (!hwr->grp)
7699 		return 0;
7700 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7701 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7702 
7703 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7704 			rss_ctx *= hwr->vnic;
7705 		return rss_ctx;
7706 	}
7707 	if (BNXT_VF(bp))
7708 		return BNXT_VF_MAX_RSS_CTX;
7709 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7710 		return hwr->grp + 1;
7711 	return 1;
7712 }
7713 
7714 /* Check if a default RSS map needs to be setup.  This function is only
7715  * used on older firmware that does not require reserving RX rings.
7716  */
7717 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7718 {
7719 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7720 
7721 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7722 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7723 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7724 		if (!netif_is_rxfh_configured(bp->dev))
7725 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7726 	}
7727 }
7728 
7729 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7730 {
7731 	if (bp->flags & BNXT_FLAG_RFS) {
7732 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7733 			return 2 + bp->num_rss_ctx;
7734 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7735 			return rx_rings + 1;
7736 	}
7737 	return 1;
7738 }
7739 
7740 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7741 {
7742 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7743 	int cp = bnxt_cp_rings_in_use(bp);
7744 	int nq = bnxt_nq_rings_in_use(bp);
7745 	int rx = bp->rx_nr_rings, stat;
7746 	int vnic, grp = rx;
7747 
7748 	/* Old firmware does not need RX ring reservations but we still
7749 	 * need to setup a default RSS map when needed.  With new firmware
7750 	 * we go through RX ring reservations first and then set up the
7751 	 * RSS map for the successfully reserved RX rings when needed.
7752 	 */
7753 	if (!BNXT_NEW_RM(bp))
7754 		bnxt_check_rss_tbl_no_rmgr(bp);
7755 
7756 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7757 	    bp->hwrm_spec_code >= 0x10601)
7758 		return true;
7759 
7760 	if (!BNXT_NEW_RM(bp))
7761 		return false;
7762 
7763 	vnic = bnxt_get_total_vnics(bp, rx);
7764 
7765 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7766 		rx <<= 1;
7767 	stat = bnxt_get_func_stat_ctxs(bp);
7768 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7769 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7770 	    (hw_resc->resv_hw_ring_grps != grp &&
7771 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7772 		return true;
7773 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7774 	    hw_resc->resv_irqs != nq)
7775 		return true;
7776 	return false;
7777 }
7778 
7779 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7780 {
7781 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7782 
7783 	hwr->tx = hw_resc->resv_tx_rings;
7784 	if (BNXT_NEW_RM(bp)) {
7785 		hwr->rx = hw_resc->resv_rx_rings;
7786 		hwr->cp = hw_resc->resv_irqs;
7787 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7788 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7789 		hwr->grp = hw_resc->resv_hw_ring_grps;
7790 		hwr->vnic = hw_resc->resv_vnics;
7791 		hwr->stat = hw_resc->resv_stat_ctxs;
7792 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7793 	}
7794 }
7795 
7796 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7797 {
7798 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7799 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7800 }
7801 
7802 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7803 
7804 static int __bnxt_reserve_rings(struct bnxt *bp)
7805 {
7806 	struct bnxt_hw_rings hwr = {0};
7807 	int rx_rings, old_rx_rings, rc;
7808 	int cp = bp->cp_nr_rings;
7809 	int ulp_msix = 0;
7810 	bool sh = false;
7811 	int tx_cp;
7812 
7813 	if (!bnxt_need_reserve_rings(bp))
7814 		return 0;
7815 
7816 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7817 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7818 		if (!ulp_msix)
7819 			bnxt_set_ulp_stat_ctxs(bp, 0);
7820 
7821 		if (ulp_msix > bp->ulp_num_msix_want)
7822 			ulp_msix = bp->ulp_num_msix_want;
7823 		hwr.cp = cp + ulp_msix;
7824 	} else {
7825 		hwr.cp = bnxt_nq_rings_in_use(bp);
7826 	}
7827 
7828 	hwr.tx = bp->tx_nr_rings;
7829 	hwr.rx = bp->rx_nr_rings;
7830 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7831 		sh = true;
7832 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7833 		hwr.cp_p5 = hwr.rx + hwr.tx;
7834 
7835 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7836 
7837 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7838 		hwr.rx <<= 1;
7839 	hwr.grp = bp->rx_nr_rings;
7840 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7841 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7842 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7843 
7844 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7845 	if (rc)
7846 		return rc;
7847 
7848 	bnxt_copy_reserved_rings(bp, &hwr);
7849 
7850 	rx_rings = hwr.rx;
7851 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7852 		if (hwr.rx >= 2) {
7853 			rx_rings = hwr.rx >> 1;
7854 		} else {
7855 			if (netif_running(bp->dev))
7856 				return -ENOMEM;
7857 
7858 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7859 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7860 			bp->dev->hw_features &= ~NETIF_F_LRO;
7861 			bp->dev->features &= ~NETIF_F_LRO;
7862 			bnxt_set_ring_params(bp);
7863 		}
7864 	}
7865 	rx_rings = min_t(int, rx_rings, hwr.grp);
7866 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7867 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7868 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7869 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7870 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7871 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7872 		hwr.rx = rx_rings << 1;
7873 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7874 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7875 	bp->tx_nr_rings = hwr.tx;
7876 
7877 	/* If we cannot reserve all the RX rings, reset the RSS map only
7878 	 * if absolutely necessary
7879 	 */
7880 	if (rx_rings != bp->rx_nr_rings) {
7881 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7882 			    rx_rings, bp->rx_nr_rings);
7883 		if (netif_is_rxfh_configured(bp->dev) &&
7884 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7885 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7886 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7887 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7888 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7889 		}
7890 	}
7891 	bp->rx_nr_rings = rx_rings;
7892 	bp->cp_nr_rings = hwr.cp;
7893 
7894 	if (!bnxt_rings_ok(bp, &hwr))
7895 		return -ENOMEM;
7896 
7897 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7898 	    !netif_is_rxfh_configured(bp->dev))
7899 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7900 
7901 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7902 		int resv_msix, resv_ctx, ulp_ctxs;
7903 		struct bnxt_hw_resc *hw_resc;
7904 
7905 		hw_resc = &bp->hw_resc;
7906 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7907 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7908 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7909 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7910 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7911 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7912 	}
7913 
7914 	return rc;
7915 }
7916 
7917 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7918 {
7919 	struct hwrm_func_vf_cfg_input *req;
7920 	u32 flags;
7921 
7922 	if (!BNXT_NEW_RM(bp))
7923 		return 0;
7924 
7925 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7926 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7927 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7928 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7929 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7930 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7931 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7932 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7933 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7934 
7935 	req->flags = cpu_to_le32(flags);
7936 	return hwrm_req_send_silent(bp, req);
7937 }
7938 
7939 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7940 {
7941 	struct hwrm_func_cfg_input *req;
7942 	u32 flags;
7943 
7944 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7945 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7946 	if (BNXT_NEW_RM(bp)) {
7947 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7948 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7949 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7950 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7951 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7952 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7953 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7954 		else
7955 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7956 	}
7957 
7958 	req->flags = cpu_to_le32(flags);
7959 	return hwrm_req_send_silent(bp, req);
7960 }
7961 
7962 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7963 {
7964 	if (bp->hwrm_spec_code < 0x10801)
7965 		return 0;
7966 
7967 	if (BNXT_PF(bp))
7968 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7969 
7970 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7971 }
7972 
7973 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7974 {
7975 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7976 	struct hwrm_ring_aggint_qcaps_output *resp;
7977 	struct hwrm_ring_aggint_qcaps_input *req;
7978 	int rc;
7979 
7980 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7981 	coal_cap->num_cmpl_dma_aggr_max = 63;
7982 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7983 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7984 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7985 	coal_cap->int_lat_tmr_min_max = 65535;
7986 	coal_cap->int_lat_tmr_max_max = 65535;
7987 	coal_cap->num_cmpl_aggr_int_max = 65535;
7988 	coal_cap->timer_units = 80;
7989 
7990 	if (bp->hwrm_spec_code < 0x10902)
7991 		return;
7992 
7993 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
7994 		return;
7995 
7996 	resp = hwrm_req_hold(bp, req);
7997 	rc = hwrm_req_send_silent(bp, req);
7998 	if (!rc) {
7999 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8000 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8001 		coal_cap->num_cmpl_dma_aggr_max =
8002 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8003 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8004 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8005 		coal_cap->cmpl_aggr_dma_tmr_max =
8006 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8007 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8008 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8009 		coal_cap->int_lat_tmr_min_max =
8010 			le16_to_cpu(resp->int_lat_tmr_min_max);
8011 		coal_cap->int_lat_tmr_max_max =
8012 			le16_to_cpu(resp->int_lat_tmr_max_max);
8013 		coal_cap->num_cmpl_aggr_int_max =
8014 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8015 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8016 	}
8017 	hwrm_req_drop(bp, req);
8018 }
8019 
8020 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8021 {
8022 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8023 
8024 	return usec * 1000 / coal_cap->timer_units;
8025 }
8026 
8027 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8028 	struct bnxt_coal *hw_coal,
8029 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8030 {
8031 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8032 	u16 val, tmr, max, flags = hw_coal->flags;
8033 	u32 cmpl_params = coal_cap->cmpl_params;
8034 
8035 	max = hw_coal->bufs_per_record * 128;
8036 	if (hw_coal->budget)
8037 		max = hw_coal->bufs_per_record * hw_coal->budget;
8038 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8039 
8040 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8041 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8042 
8043 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8044 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8045 
8046 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8047 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8048 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8049 
8050 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8051 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8052 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8053 
8054 	/* min timer set to 1/2 of interrupt timer */
8055 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8056 		val = tmr / 2;
8057 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8058 		req->int_lat_tmr_min = cpu_to_le16(val);
8059 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8060 	}
8061 
8062 	/* buf timer set to 1/4 of interrupt timer */
8063 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8064 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8065 
8066 	if (cmpl_params &
8067 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8068 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8069 		val = clamp_t(u16, tmr, 1,
8070 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8071 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8072 		req->enables |=
8073 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8074 	}
8075 
8076 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8077 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8078 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8079 	req->flags = cpu_to_le16(flags);
8080 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8081 }
8082 
8083 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8084 				   struct bnxt_coal *hw_coal)
8085 {
8086 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8087 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8088 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8089 	u32 nq_params = coal_cap->nq_params;
8090 	u16 tmr;
8091 	int rc;
8092 
8093 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8094 		return 0;
8095 
8096 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8097 	if (rc)
8098 		return rc;
8099 
8100 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8101 	req->flags =
8102 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8103 
8104 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8105 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8106 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8107 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8108 	return hwrm_req_send(bp, req);
8109 }
8110 
8111 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8112 {
8113 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8114 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8115 	struct bnxt_coal coal;
8116 	int rc;
8117 
8118 	/* Tick values in micro seconds.
8119 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8120 	 */
8121 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8122 
8123 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8124 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8125 
8126 	if (!bnapi->rx_ring)
8127 		return -ENODEV;
8128 
8129 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8130 	if (rc)
8131 		return rc;
8132 
8133 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8134 
8135 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8136 
8137 	return hwrm_req_send(bp, req_rx);
8138 }
8139 
8140 static int
8141 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8142 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8143 {
8144 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8145 
8146 	req->ring_id = cpu_to_le16(ring_id);
8147 	return hwrm_req_send(bp, req);
8148 }
8149 
8150 static int
8151 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8152 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8153 {
8154 	struct bnxt_tx_ring_info *txr;
8155 	int i, rc;
8156 
8157 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8158 		u16 ring_id;
8159 
8160 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8161 		req->ring_id = cpu_to_le16(ring_id);
8162 		rc = hwrm_req_send(bp, req);
8163 		if (rc)
8164 			return rc;
8165 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8166 			return 0;
8167 	}
8168 	return 0;
8169 }
8170 
8171 int bnxt_hwrm_set_coal(struct bnxt *bp)
8172 {
8173 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8174 	int i, rc;
8175 
8176 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8177 	if (rc)
8178 		return rc;
8179 
8180 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8181 	if (rc) {
8182 		hwrm_req_drop(bp, req_rx);
8183 		return rc;
8184 	}
8185 
8186 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8187 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8188 
8189 	hwrm_req_hold(bp, req_rx);
8190 	hwrm_req_hold(bp, req_tx);
8191 	for (i = 0; i < bp->cp_nr_rings; i++) {
8192 		struct bnxt_napi *bnapi = bp->bnapi[i];
8193 		struct bnxt_coal *hw_coal;
8194 
8195 		if (!bnapi->rx_ring)
8196 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8197 		else
8198 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8199 		if (rc)
8200 			break;
8201 
8202 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8203 			continue;
8204 
8205 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8206 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8207 			if (rc)
8208 				break;
8209 		}
8210 		if (bnapi->rx_ring)
8211 			hw_coal = &bp->rx_coal;
8212 		else
8213 			hw_coal = &bp->tx_coal;
8214 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8215 	}
8216 	hwrm_req_drop(bp, req_rx);
8217 	hwrm_req_drop(bp, req_tx);
8218 	return rc;
8219 }
8220 
8221 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8222 {
8223 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8224 	struct hwrm_stat_ctx_free_input *req;
8225 	int i;
8226 
8227 	if (!bp->bnapi)
8228 		return;
8229 
8230 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8231 		return;
8232 
8233 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8234 		return;
8235 	if (BNXT_FW_MAJ(bp) <= 20) {
8236 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8237 			hwrm_req_drop(bp, req);
8238 			return;
8239 		}
8240 		hwrm_req_hold(bp, req0);
8241 	}
8242 	hwrm_req_hold(bp, req);
8243 	for (i = 0; i < bp->cp_nr_rings; i++) {
8244 		struct bnxt_napi *bnapi = bp->bnapi[i];
8245 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8246 
8247 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8248 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8249 			if (req0) {
8250 				req0->stat_ctx_id = req->stat_ctx_id;
8251 				hwrm_req_send(bp, req0);
8252 			}
8253 			hwrm_req_send(bp, req);
8254 
8255 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8256 		}
8257 	}
8258 	hwrm_req_drop(bp, req);
8259 	if (req0)
8260 		hwrm_req_drop(bp, req0);
8261 }
8262 
8263 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8264 {
8265 	struct hwrm_stat_ctx_alloc_output *resp;
8266 	struct hwrm_stat_ctx_alloc_input *req;
8267 	int rc, i;
8268 
8269 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8270 		return 0;
8271 
8272 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8273 	if (rc)
8274 		return rc;
8275 
8276 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8277 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8278 
8279 	resp = hwrm_req_hold(bp, req);
8280 	for (i = 0; i < bp->cp_nr_rings; i++) {
8281 		struct bnxt_napi *bnapi = bp->bnapi[i];
8282 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8283 
8284 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8285 
8286 		rc = hwrm_req_send(bp, req);
8287 		if (rc)
8288 			break;
8289 
8290 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8291 
8292 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8293 	}
8294 	hwrm_req_drop(bp, req);
8295 	return rc;
8296 }
8297 
8298 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8299 {
8300 	struct hwrm_func_qcfg_output *resp;
8301 	struct hwrm_func_qcfg_input *req;
8302 	u16 flags;
8303 	int rc;
8304 
8305 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8306 	if (rc)
8307 		return rc;
8308 
8309 	req->fid = cpu_to_le16(0xffff);
8310 	resp = hwrm_req_hold(bp, req);
8311 	rc = hwrm_req_send(bp, req);
8312 	if (rc)
8313 		goto func_qcfg_exit;
8314 
8315 	flags = le16_to_cpu(resp->flags);
8316 #ifdef CONFIG_BNXT_SRIOV
8317 	if (BNXT_VF(bp)) {
8318 		struct bnxt_vf_info *vf = &bp->vf;
8319 
8320 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8321 		if (flags & FUNC_QCFG_RESP_FLAGS_TRUSTED_VF)
8322 			vf->flags |= BNXT_VF_TRUST;
8323 		else
8324 			vf->flags &= ~BNXT_VF_TRUST;
8325 	} else {
8326 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8327 	}
8328 #endif
8329 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8330 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8331 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8332 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8333 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8334 	}
8335 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8336 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8337 
8338 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8339 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8340 
8341 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8342 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8343 
8344 	switch (resp->port_partition_type) {
8345 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8346 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8347 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8348 		bp->port_partition_type = resp->port_partition_type;
8349 		break;
8350 	}
8351 	if (bp->hwrm_spec_code < 0x10707 ||
8352 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8353 		bp->br_mode = BRIDGE_MODE_VEB;
8354 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8355 		bp->br_mode = BRIDGE_MODE_VEPA;
8356 	else
8357 		bp->br_mode = BRIDGE_MODE_UNDEF;
8358 
8359 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8360 	if (!bp->max_mtu)
8361 		bp->max_mtu = BNXT_MAX_MTU;
8362 
8363 	if (bp->db_size)
8364 		goto func_qcfg_exit;
8365 
8366 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8367 	if (BNXT_CHIP_P5(bp)) {
8368 		if (BNXT_PF(bp))
8369 			bp->db_offset = DB_PF_OFFSET_P5;
8370 		else
8371 			bp->db_offset = DB_VF_OFFSET_P5;
8372 	}
8373 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8374 				 1024);
8375 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8376 	    bp->db_size <= bp->db_offset)
8377 		bp->db_size = pci_resource_len(bp->pdev, 2);
8378 
8379 func_qcfg_exit:
8380 	hwrm_req_drop(bp, req);
8381 	return rc;
8382 }
8383 
8384 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8385 				      u8 init_val, u8 init_offset,
8386 				      bool init_mask_set)
8387 {
8388 	ctxm->init_value = init_val;
8389 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8390 	if (init_mask_set)
8391 		ctxm->init_offset = init_offset * 4;
8392 	else
8393 		ctxm->init_value = 0;
8394 }
8395 
8396 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8397 {
8398 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8399 	u16 type;
8400 
8401 	for (type = 0; type < ctx_max; type++) {
8402 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8403 		int n = 1;
8404 
8405 		if (!ctxm->max_entries || ctxm->pg_info)
8406 			continue;
8407 
8408 		if (ctxm->instance_bmap)
8409 			n = hweight32(ctxm->instance_bmap);
8410 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8411 		if (!ctxm->pg_info)
8412 			return -ENOMEM;
8413 	}
8414 	return 0;
8415 }
8416 
8417 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8418 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8419 
8420 #define BNXT_CTX_INIT_VALID(flags)	\
8421 	(!!((flags) &			\
8422 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8423 
8424 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8425 {
8426 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8427 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8428 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8429 	u16 type;
8430 	int rc;
8431 
8432 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8433 	if (rc)
8434 		return rc;
8435 
8436 	if (!ctx) {
8437 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8438 		if (!ctx)
8439 			return -ENOMEM;
8440 		bp->ctx = ctx;
8441 	}
8442 
8443 	resp = hwrm_req_hold(bp, req);
8444 
8445 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8446 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8447 		u8 init_val, init_off, i;
8448 		u32 max_entries;
8449 		u16 entry_size;
8450 		__le32 *p;
8451 		u32 flags;
8452 
8453 		req->type = cpu_to_le16(type);
8454 		rc = hwrm_req_send(bp, req);
8455 		if (rc)
8456 			goto ctx_done;
8457 		flags = le32_to_cpu(resp->flags);
8458 		type = le16_to_cpu(resp->next_valid_type);
8459 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8460 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8461 			continue;
8462 		}
8463 		entry_size = le16_to_cpu(resp->entry_size);
8464 		max_entries = le32_to_cpu(resp->max_num_entries);
8465 		if (ctxm->mem_valid) {
8466 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8467 			    ctxm->entry_size != entry_size ||
8468 			    ctxm->max_entries != max_entries)
8469 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8470 			else
8471 				continue;
8472 		}
8473 		ctxm->type = le16_to_cpu(resp->type);
8474 		ctxm->entry_size = entry_size;
8475 		ctxm->flags = flags;
8476 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8477 		ctxm->entry_multiple = resp->entry_multiple;
8478 		ctxm->max_entries = max_entries;
8479 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8480 		init_val = resp->ctx_init_value;
8481 		init_off = resp->ctx_init_offset;
8482 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8483 					  BNXT_CTX_INIT_VALID(flags));
8484 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8485 					      BNXT_MAX_SPLIT_ENTRY);
8486 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8487 		     i++, p++)
8488 			ctxm->split[i] = le32_to_cpu(*p);
8489 	}
8490 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8491 
8492 ctx_done:
8493 	hwrm_req_drop(bp, req);
8494 	return rc;
8495 }
8496 
8497 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8498 {
8499 	struct hwrm_func_backing_store_qcaps_output *resp;
8500 	struct hwrm_func_backing_store_qcaps_input *req;
8501 	int rc;
8502 
8503 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8504 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8505 		return 0;
8506 
8507 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8508 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8509 
8510 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8511 	if (rc)
8512 		return rc;
8513 
8514 	resp = hwrm_req_hold(bp, req);
8515 	rc = hwrm_req_send_silent(bp, req);
8516 	if (!rc) {
8517 		struct bnxt_ctx_mem_type *ctxm;
8518 		struct bnxt_ctx_mem_info *ctx;
8519 		u8 init_val, init_idx = 0;
8520 		u16 init_mask;
8521 
8522 		ctx = bp->ctx;
8523 		if (!ctx) {
8524 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8525 			if (!ctx) {
8526 				rc = -ENOMEM;
8527 				goto ctx_err;
8528 			}
8529 			bp->ctx = ctx;
8530 		}
8531 		init_val = resp->ctx_kind_initializer;
8532 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8533 
8534 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8535 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8536 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8537 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8538 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8539 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8540 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8541 					  (init_mask & (1 << init_idx++)) != 0);
8542 
8543 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8544 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8545 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8546 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8547 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8548 					  (init_mask & (1 << init_idx++)) != 0);
8549 
8550 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8551 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8552 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8553 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8554 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8555 					  (init_mask & (1 << init_idx++)) != 0);
8556 
8557 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8558 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8559 		ctxm->max_entries = ctxm->vnic_entries +
8560 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8561 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8562 		bnxt_init_ctx_initializer(ctxm, init_val,
8563 					  resp->vnic_init_offset,
8564 					  (init_mask & (1 << init_idx++)) != 0);
8565 
8566 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8567 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8568 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8569 		bnxt_init_ctx_initializer(ctxm, init_val,
8570 					  resp->stat_init_offset,
8571 					  (init_mask & (1 << init_idx++)) != 0);
8572 
8573 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8574 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8575 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8576 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8577 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8578 		if (!ctxm->entry_multiple)
8579 			ctxm->entry_multiple = 1;
8580 
8581 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8582 
8583 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8584 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8585 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8586 		ctxm->mrav_num_entries_units =
8587 			le16_to_cpu(resp->mrav_num_entries_units);
8588 		bnxt_init_ctx_initializer(ctxm, init_val,
8589 					  resp->mrav_init_offset,
8590 					  (init_mask & (1 << init_idx++)) != 0);
8591 
8592 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8593 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8594 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8595 
8596 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8597 		if (!ctx->tqm_fp_rings_count)
8598 			ctx->tqm_fp_rings_count = bp->max_q;
8599 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8600 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8601 
8602 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8603 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8604 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8605 
8606 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8607 	} else {
8608 		rc = 0;
8609 	}
8610 ctx_err:
8611 	hwrm_req_drop(bp, req);
8612 	return rc;
8613 }
8614 
8615 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8616 				  __le64 *pg_dir)
8617 {
8618 	if (!rmem->nr_pages)
8619 		return;
8620 
8621 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8622 	if (rmem->depth >= 1) {
8623 		if (rmem->depth == 2)
8624 			*pg_attr |= 2;
8625 		else
8626 			*pg_attr |= 1;
8627 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8628 	} else {
8629 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8630 	}
8631 }
8632 
8633 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8634 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8635 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8636 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8637 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8638 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8639 
8640 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8641 {
8642 	struct hwrm_func_backing_store_cfg_input *req;
8643 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8644 	struct bnxt_ctx_pg_info *ctx_pg;
8645 	struct bnxt_ctx_mem_type *ctxm;
8646 	void **__req = (void **)&req;
8647 	u32 req_len = sizeof(*req);
8648 	__le32 *num_entries;
8649 	__le64 *pg_dir;
8650 	u32 flags = 0;
8651 	u8 *pg_attr;
8652 	u32 ena;
8653 	int rc;
8654 	int i;
8655 
8656 	if (!ctx)
8657 		return 0;
8658 
8659 	if (req_len > bp->hwrm_max_ext_req_len)
8660 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8661 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8662 	if (rc)
8663 		return rc;
8664 
8665 	req->enables = cpu_to_le32(enables);
8666 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8667 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8668 		ctx_pg = ctxm->pg_info;
8669 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8670 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8671 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8672 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8673 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8674 				      &req->qpc_pg_size_qpc_lvl,
8675 				      &req->qpc_page_dir);
8676 
8677 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8678 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8679 	}
8680 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8681 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8682 		ctx_pg = ctxm->pg_info;
8683 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8684 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8685 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8686 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8687 				      &req->srq_pg_size_srq_lvl,
8688 				      &req->srq_page_dir);
8689 	}
8690 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8691 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8692 		ctx_pg = ctxm->pg_info;
8693 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8694 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8695 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8696 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8697 				      &req->cq_pg_size_cq_lvl,
8698 				      &req->cq_page_dir);
8699 	}
8700 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8701 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8702 		ctx_pg = ctxm->pg_info;
8703 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8704 		req->vnic_num_ring_table_entries =
8705 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8706 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8707 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8708 				      &req->vnic_pg_size_vnic_lvl,
8709 				      &req->vnic_page_dir);
8710 	}
8711 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8712 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8713 		ctx_pg = ctxm->pg_info;
8714 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8715 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8716 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8717 				      &req->stat_pg_size_stat_lvl,
8718 				      &req->stat_page_dir);
8719 	}
8720 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8721 		u32 units;
8722 
8723 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8724 		ctx_pg = ctxm->pg_info;
8725 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8726 		units = ctxm->mrav_num_entries_units;
8727 		if (units) {
8728 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8729 			u32 entries;
8730 
8731 			num_mr = ctx_pg->entries - num_ah;
8732 			entries = ((num_mr / units) << 16) | (num_ah / units);
8733 			req->mrav_num_entries = cpu_to_le32(entries);
8734 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8735 		}
8736 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8737 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8738 				      &req->mrav_pg_size_mrav_lvl,
8739 				      &req->mrav_page_dir);
8740 	}
8741 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8742 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8743 		ctx_pg = ctxm->pg_info;
8744 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8745 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8746 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8747 				      &req->tim_pg_size_tim_lvl,
8748 				      &req->tim_page_dir);
8749 	}
8750 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8751 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8752 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8753 	     pg_dir = &req->tqm_sp_page_dir,
8754 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8755 	     ctx_pg = ctxm->pg_info;
8756 	     i < BNXT_MAX_TQM_RINGS;
8757 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8758 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8759 		if (!(enables & ena))
8760 			continue;
8761 
8762 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8763 		*num_entries = cpu_to_le32(ctx_pg->entries);
8764 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8765 	}
8766 	req->flags = cpu_to_le32(flags);
8767 	return hwrm_req_send(bp, req);
8768 }
8769 
8770 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8771 				  struct bnxt_ctx_pg_info *ctx_pg)
8772 {
8773 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8774 
8775 	rmem->page_size = BNXT_PAGE_SIZE;
8776 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8777 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8778 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8779 	if (rmem->depth >= 1)
8780 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8781 	return bnxt_alloc_ring(bp, rmem);
8782 }
8783 
8784 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8785 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8786 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8787 {
8788 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8789 	int rc;
8790 
8791 	if (!mem_size)
8792 		return -EINVAL;
8793 
8794 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8795 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8796 		ctx_pg->nr_pages = 0;
8797 		return -EINVAL;
8798 	}
8799 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8800 		int nr_tbls, i;
8801 
8802 		rmem->depth = 2;
8803 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8804 					     GFP_KERNEL);
8805 		if (!ctx_pg->ctx_pg_tbl)
8806 			return -ENOMEM;
8807 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8808 		rmem->nr_pages = nr_tbls;
8809 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8810 		if (rc)
8811 			return rc;
8812 		for (i = 0; i < nr_tbls; i++) {
8813 			struct bnxt_ctx_pg_info *pg_tbl;
8814 
8815 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8816 			if (!pg_tbl)
8817 				return -ENOMEM;
8818 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8819 			rmem = &pg_tbl->ring_mem;
8820 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8821 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8822 			rmem->depth = 1;
8823 			rmem->nr_pages = MAX_CTX_PAGES;
8824 			rmem->ctx_mem = ctxm;
8825 			if (i == (nr_tbls - 1)) {
8826 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8827 
8828 				if (rem)
8829 					rmem->nr_pages = rem;
8830 			}
8831 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8832 			if (rc)
8833 				break;
8834 		}
8835 	} else {
8836 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8837 		if (rmem->nr_pages > 1 || depth)
8838 			rmem->depth = 1;
8839 		rmem->ctx_mem = ctxm;
8840 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8841 	}
8842 	return rc;
8843 }
8844 
8845 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8846 				    struct bnxt_ctx_pg_info *ctx_pg,
8847 				    void *buf, size_t offset, size_t head,
8848 				    size_t tail)
8849 {
8850 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8851 	size_t nr_pages = ctx_pg->nr_pages;
8852 	int page_size = rmem->page_size;
8853 	size_t len = 0, total_len = 0;
8854 	u16 depth = rmem->depth;
8855 
8856 	tail %= nr_pages * page_size;
8857 	do {
8858 		if (depth > 1) {
8859 			int i = head / (page_size * MAX_CTX_PAGES);
8860 			struct bnxt_ctx_pg_info *pg_tbl;
8861 
8862 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8863 			rmem = &pg_tbl->ring_mem;
8864 		}
8865 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8866 		head += len;
8867 		offset += len;
8868 		total_len += len;
8869 		if (head >= nr_pages * page_size)
8870 			head = 0;
8871 	} while (head != tail);
8872 	return total_len;
8873 }
8874 
8875 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8876 				  struct bnxt_ctx_pg_info *ctx_pg)
8877 {
8878 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8879 
8880 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8881 	    ctx_pg->ctx_pg_tbl) {
8882 		int i, nr_tbls = rmem->nr_pages;
8883 
8884 		for (i = 0; i < nr_tbls; i++) {
8885 			struct bnxt_ctx_pg_info *pg_tbl;
8886 			struct bnxt_ring_mem_info *rmem2;
8887 
8888 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8889 			if (!pg_tbl)
8890 				continue;
8891 			rmem2 = &pg_tbl->ring_mem;
8892 			bnxt_free_ring(bp, rmem2);
8893 			ctx_pg->ctx_pg_arr[i] = NULL;
8894 			kfree(pg_tbl);
8895 			ctx_pg->ctx_pg_tbl[i] = NULL;
8896 		}
8897 		kfree(ctx_pg->ctx_pg_tbl);
8898 		ctx_pg->ctx_pg_tbl = NULL;
8899 	}
8900 	bnxt_free_ring(bp, rmem);
8901 	ctx_pg->nr_pages = 0;
8902 }
8903 
8904 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8905 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8906 				   u8 pg_lvl)
8907 {
8908 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8909 	int i, rc = 0, n = 1;
8910 	u32 mem_size;
8911 
8912 	if (!ctxm->entry_size || !ctx_pg)
8913 		return -EINVAL;
8914 	if (ctxm->instance_bmap)
8915 		n = hweight32(ctxm->instance_bmap);
8916 	if (ctxm->entry_multiple)
8917 		entries = roundup(entries, ctxm->entry_multiple);
8918 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8919 	mem_size = entries * ctxm->entry_size;
8920 	for (i = 0; i < n && !rc; i++) {
8921 		ctx_pg[i].entries = entries;
8922 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8923 					    ctxm->init_value ? ctxm : NULL);
8924 	}
8925 	if (!rc)
8926 		ctxm->mem_valid = 1;
8927 	return rc;
8928 }
8929 
8930 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8931 					       struct bnxt_ctx_mem_type *ctxm,
8932 					       bool last)
8933 {
8934 	struct hwrm_func_backing_store_cfg_v2_input *req;
8935 	u32 instance_bmap = ctxm->instance_bmap;
8936 	int i, j, rc = 0, n = 1;
8937 	__le32 *p;
8938 
8939 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8940 		return 0;
8941 
8942 	if (instance_bmap)
8943 		n = hweight32(ctxm->instance_bmap);
8944 	else
8945 		instance_bmap = 1;
8946 
8947 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8948 	if (rc)
8949 		return rc;
8950 	hwrm_req_hold(bp, req);
8951 	req->type = cpu_to_le16(ctxm->type);
8952 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8953 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
8954 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
8955 		struct bnxt_bs_trace_info *bs_trace;
8956 		u32 enables;
8957 
8958 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
8959 		req->enables = cpu_to_le32(enables);
8960 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
8961 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
8962 	}
8963 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8964 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8965 		p[i] = cpu_to_le32(ctxm->split[i]);
8966 	for (i = 0, j = 0; j < n && !rc; i++) {
8967 		struct bnxt_ctx_pg_info *ctx_pg;
8968 
8969 		if (!(instance_bmap & (1 << i)))
8970 			continue;
8971 		req->instance = cpu_to_le16(i);
8972 		ctx_pg = &ctxm->pg_info[j++];
8973 		if (!ctx_pg->entries)
8974 			continue;
8975 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8976 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8977 				      &req->page_size_pbl_level,
8978 				      &req->page_dir);
8979 		if (last && j == n)
8980 			req->flags =
8981 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8982 		rc = hwrm_req_send(bp, req);
8983 	}
8984 	hwrm_req_drop(bp, req);
8985 	return rc;
8986 }
8987 
8988 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8989 {
8990 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8991 	struct bnxt_ctx_mem_type *ctxm;
8992 	u16 last_type = BNXT_CTX_INV;
8993 	int rc = 0;
8994 	u16 type;
8995 
8996 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
8997 		ctxm = &ctx->ctx_arr[type];
8998 		if (!bnxt_bs_trace_avail(bp, type))
8999 			continue;
9000 		if (!ctxm->mem_valid) {
9001 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9002 						     ctxm->max_entries, 1);
9003 			if (rc) {
9004 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9005 					    type);
9006 				continue;
9007 			}
9008 			bnxt_bs_trace_init(bp, ctxm);
9009 		}
9010 		last_type = type;
9011 	}
9012 
9013 	if (last_type == BNXT_CTX_INV) {
9014 		if (!ena)
9015 			return 0;
9016 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9017 			last_type = BNXT_CTX_MAX - 1;
9018 		else
9019 			last_type = BNXT_CTX_L2_MAX - 1;
9020 	}
9021 	ctx->ctx_arr[last_type].last = 1;
9022 
9023 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9024 		ctxm = &ctx->ctx_arr[type];
9025 
9026 		if (!ctxm->mem_valid)
9027 			continue;
9028 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9029 		if (rc)
9030 			return rc;
9031 	}
9032 	return 0;
9033 }
9034 
9035 /**
9036  * __bnxt_copy_ctx_mem - copy host context memory
9037  * @bp: The driver context
9038  * @ctxm: The pointer to the context memory type
9039  * @buf: The destination buffer or NULL to just obtain the length
9040  * @offset: The buffer offset to copy the data to
9041  * @head: The head offset of context memory to copy from
9042  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9043  *
9044  * This function is called for debugging purposes to dump the host context
9045  * used by the chip.
9046  *
9047  * Return: Length of memory copied
9048  */
9049 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9050 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9051 				  size_t offset, size_t head, size_t tail)
9052 {
9053 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9054 	size_t len = 0, total_len = 0;
9055 	int i, n = 1;
9056 
9057 	if (!ctx_pg)
9058 		return 0;
9059 
9060 	if (ctxm->instance_bmap)
9061 		n = hweight32(ctxm->instance_bmap);
9062 	for (i = 0; i < n; i++) {
9063 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9064 					    tail);
9065 		offset += len;
9066 		total_len += len;
9067 	}
9068 	return total_len;
9069 }
9070 
9071 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9072 			 void *buf, size_t offset)
9073 {
9074 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9075 
9076 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9077 }
9078 
9079 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9080 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9081 {
9082 	struct bnxt_ctx_pg_info *ctx_pg;
9083 	int i, n = 1;
9084 
9085 	ctxm->last = 0;
9086 
9087 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9088 		return;
9089 
9090 	ctx_pg = ctxm->pg_info;
9091 	if (ctx_pg) {
9092 		if (ctxm->instance_bmap)
9093 			n = hweight32(ctxm->instance_bmap);
9094 		for (i = 0; i < n; i++)
9095 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9096 
9097 		kfree(ctx_pg);
9098 		ctxm->pg_info = NULL;
9099 		ctxm->mem_valid = 0;
9100 	}
9101 	memset(ctxm, 0, sizeof(*ctxm));
9102 }
9103 
9104 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9105 {
9106 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9107 	u16 type;
9108 
9109 	if (!ctx)
9110 		return;
9111 
9112 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9113 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9114 
9115 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9116 	if (force) {
9117 		kfree(ctx);
9118 		bp->ctx = NULL;
9119 	}
9120 }
9121 
9122 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9123 {
9124 	struct bnxt_ctx_mem_type *ctxm;
9125 	struct bnxt_ctx_mem_info *ctx;
9126 	u32 l2_qps, qp1_qps, max_qps;
9127 	u32 ena, entries_sp, entries;
9128 	u32 srqs, max_srqs, min;
9129 	u32 num_mr, num_ah;
9130 	u32 extra_srqs = 0;
9131 	u32 extra_qps = 0;
9132 	u32 fast_qpmd_qps;
9133 	u8 pg_lvl = 1;
9134 	int i, rc;
9135 
9136 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9137 	if (rc) {
9138 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9139 			   rc);
9140 		return rc;
9141 	}
9142 	ctx = bp->ctx;
9143 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9144 		return 0;
9145 
9146 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9147 	l2_qps = ctxm->qp_l2_entries;
9148 	qp1_qps = ctxm->qp_qp1_entries;
9149 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9150 	max_qps = ctxm->max_entries;
9151 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9152 	srqs = ctxm->srq_l2_entries;
9153 	max_srqs = ctxm->max_entries;
9154 	ena = 0;
9155 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9156 		pg_lvl = 2;
9157 		if (BNXT_SW_RES_LMT(bp)) {
9158 			extra_qps = max_qps - l2_qps - qp1_qps;
9159 			extra_srqs = max_srqs - srqs;
9160 		} else {
9161 			extra_qps = min_t(u32, 65536,
9162 					  max_qps - l2_qps - qp1_qps);
9163 			/* allocate extra qps if fw supports RoCE fast qp
9164 			 * destroy feature
9165 			 */
9166 			extra_qps += fast_qpmd_qps;
9167 			extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9168 		}
9169 		if (fast_qpmd_qps)
9170 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9171 	}
9172 
9173 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9174 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9175 				     pg_lvl);
9176 	if (rc)
9177 		return rc;
9178 
9179 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9180 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9181 	if (rc)
9182 		return rc;
9183 
9184 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9185 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9186 				     extra_qps * 2, pg_lvl);
9187 	if (rc)
9188 		return rc;
9189 
9190 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9191 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9192 	if (rc)
9193 		return rc;
9194 
9195 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9196 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9197 	if (rc)
9198 		return rc;
9199 
9200 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9201 		goto skip_rdma;
9202 
9203 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9204 	if (BNXT_SW_RES_LMT(bp) &&
9205 	    ctxm->split_entry_cnt == BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1) {
9206 		num_ah = ctxm->mrav_av_entries;
9207 		num_mr = ctxm->max_entries - num_ah;
9208 	} else {
9209 		/* 128K extra is needed to accommodate static AH context
9210 		 * allocation by f/w.
9211 		 */
9212 		num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9213 		num_ah = min_t(u32, num_mr, 1024 * 128);
9214 		ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9215 		if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9216 			ctxm->mrav_av_entries = num_ah;
9217 	}
9218 
9219 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9220 	if (rc)
9221 		return rc;
9222 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9223 
9224 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9225 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9226 	if (rc)
9227 		return rc;
9228 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9229 
9230 skip_rdma:
9231 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9232 	min = ctxm->min_entries;
9233 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9234 		     2 * (extra_qps + qp1_qps) + min;
9235 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9236 	if (rc)
9237 		return rc;
9238 
9239 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9240 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9241 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9242 	if (rc)
9243 		return rc;
9244 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9245 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9246 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9247 
9248 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9249 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9250 	else
9251 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9252 	if (rc) {
9253 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9254 			   rc);
9255 		return rc;
9256 	}
9257 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9258 	return 0;
9259 }
9260 
9261 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9262 {
9263 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9264 	u16 page_attr;
9265 	int rc;
9266 
9267 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9268 		return 0;
9269 
9270 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9271 	if (rc)
9272 		return rc;
9273 
9274 	if (BNXT_PAGE_SIZE == 0x2000)
9275 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9276 	else if (BNXT_PAGE_SIZE == 0x10000)
9277 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9278 	else
9279 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9280 	req->pg_size_lvl = cpu_to_le16(page_attr |
9281 				       bp->fw_crash_mem->ring_mem.depth);
9282 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9283 	req->size = cpu_to_le32(bp->fw_crash_len);
9284 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9285 	return hwrm_req_send(bp, req);
9286 }
9287 
9288 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9289 {
9290 	if (bp->fw_crash_mem) {
9291 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9292 		kfree(bp->fw_crash_mem);
9293 		bp->fw_crash_mem = NULL;
9294 	}
9295 }
9296 
9297 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9298 {
9299 	u32 mem_size = 0;
9300 	int rc;
9301 
9302 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9303 		return 0;
9304 
9305 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9306 	if (rc)
9307 		return rc;
9308 
9309 	mem_size = round_up(mem_size, 4);
9310 
9311 	/* keep and use the existing pages */
9312 	if (bp->fw_crash_mem &&
9313 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9314 		goto alloc_done;
9315 
9316 	if (bp->fw_crash_mem)
9317 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9318 	else
9319 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9320 					   GFP_KERNEL);
9321 	if (!bp->fw_crash_mem)
9322 		return -ENOMEM;
9323 
9324 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9325 	if (rc) {
9326 		bnxt_free_crash_dump_mem(bp);
9327 		return rc;
9328 	}
9329 
9330 alloc_done:
9331 	bp->fw_crash_len = mem_size;
9332 	return 0;
9333 }
9334 
9335 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9336 {
9337 	struct hwrm_func_resource_qcaps_output *resp;
9338 	struct hwrm_func_resource_qcaps_input *req;
9339 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9340 	int rc;
9341 
9342 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9343 	if (rc)
9344 		return rc;
9345 
9346 	req->fid = cpu_to_le16(0xffff);
9347 	resp = hwrm_req_hold(bp, req);
9348 	rc = hwrm_req_send_silent(bp, req);
9349 	if (rc)
9350 		goto hwrm_func_resc_qcaps_exit;
9351 
9352 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9353 	if (!all)
9354 		goto hwrm_func_resc_qcaps_exit;
9355 
9356 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9357 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9358 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9359 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9360 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9361 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9362 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9363 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9364 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9365 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9366 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9367 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9368 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9369 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9370 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9371 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9372 
9373 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9374 		u16 max_msix = le16_to_cpu(resp->max_msix);
9375 
9376 		hw_resc->max_nqs = max_msix;
9377 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9378 	}
9379 
9380 	if (BNXT_PF(bp)) {
9381 		struct bnxt_pf_info *pf = &bp->pf;
9382 
9383 		pf->vf_resv_strategy =
9384 			le16_to_cpu(resp->vf_reservation_strategy);
9385 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9386 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9387 	}
9388 hwrm_func_resc_qcaps_exit:
9389 	hwrm_req_drop(bp, req);
9390 	return rc;
9391 }
9392 
9393 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9394 {
9395 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9396 	struct hwrm_port_mac_ptp_qcfg_input *req;
9397 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9398 	u8 flags;
9399 	int rc;
9400 
9401 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9402 		rc = -ENODEV;
9403 		goto no_ptp;
9404 	}
9405 
9406 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9407 	if (rc)
9408 		goto no_ptp;
9409 
9410 	req->port_id = cpu_to_le16(bp->pf.port_id);
9411 	resp = hwrm_req_hold(bp, req);
9412 	rc = hwrm_req_send(bp, req);
9413 	if (rc)
9414 		goto exit;
9415 
9416 	flags = resp->flags;
9417 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9418 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9419 		rc = -ENODEV;
9420 		goto exit;
9421 	}
9422 	if (!ptp) {
9423 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9424 		if (!ptp) {
9425 			rc = -ENOMEM;
9426 			goto exit;
9427 		}
9428 		ptp->bp = bp;
9429 		bp->ptp_cfg = ptp;
9430 	}
9431 
9432 	if (flags &
9433 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9434 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9435 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9436 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9437 	} else if (BNXT_CHIP_P5(bp)) {
9438 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9439 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9440 	} else {
9441 		rc = -ENODEV;
9442 		goto exit;
9443 	}
9444 	ptp->rtc_configured =
9445 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9446 	rc = bnxt_ptp_init(bp);
9447 	if (rc)
9448 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9449 exit:
9450 	hwrm_req_drop(bp, req);
9451 	if (!rc)
9452 		return 0;
9453 
9454 no_ptp:
9455 	bnxt_ptp_clear(bp);
9456 	kfree(ptp);
9457 	bp->ptp_cfg = NULL;
9458 	return rc;
9459 }
9460 
9461 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9462 {
9463 	struct hwrm_func_qcaps_output *resp;
9464 	struct hwrm_func_qcaps_input *req;
9465 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9466 	u32 flags, flags_ext, flags_ext2;
9467 	int rc;
9468 
9469 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9470 	if (rc)
9471 		return rc;
9472 
9473 	req->fid = cpu_to_le16(0xffff);
9474 	resp = hwrm_req_hold(bp, req);
9475 	rc = hwrm_req_send(bp, req);
9476 	if (rc)
9477 		goto hwrm_func_qcaps_exit;
9478 
9479 	flags = le32_to_cpu(resp->flags);
9480 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9481 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9482 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9483 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9484 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9485 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9486 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9487 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9488 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9489 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9490 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9491 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9492 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9493 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9494 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9495 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9496 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9497 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9498 
9499 	flags_ext = le32_to_cpu(resp->flags_ext);
9500 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9501 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9502 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9503 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9504 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9505 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9506 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9507 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9508 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9509 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9510 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9511 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9512 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9513 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9514 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9515 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9516 
9517 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9518 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9519 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9520 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9521 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9522 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9523 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9524 	if (flags_ext2 &
9525 	    FUNC_QCAPS_RESP_FLAGS_EXT2_SW_MAX_RESOURCE_LIMITS_SUPPORTED)
9526 		bp->fw_cap |= BNXT_FW_CAP_SW_MAX_RESOURCE_LIMITS;
9527 	if (BNXT_PF(bp) &&
9528 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9529 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9530 
9531 	bp->tx_push_thresh = 0;
9532 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9533 	    BNXT_FW_MAJ(bp) > 217)
9534 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9535 
9536 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9537 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9538 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9539 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9540 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9541 	if (!hw_resc->max_hw_ring_grps)
9542 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9543 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9544 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9545 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9546 
9547 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9548 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9549 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9550 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9551 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9552 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9553 
9554 	if (BNXT_PF(bp)) {
9555 		struct bnxt_pf_info *pf = &bp->pf;
9556 
9557 		pf->fw_fid = le16_to_cpu(resp->fid);
9558 		pf->port_id = le16_to_cpu(resp->port_id);
9559 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9560 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9561 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9562 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9563 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9564 			bp->flags |= BNXT_FLAG_WOL_CAP;
9565 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9566 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9567 		} else {
9568 			bnxt_ptp_clear(bp);
9569 			kfree(bp->ptp_cfg);
9570 			bp->ptp_cfg = NULL;
9571 		}
9572 	} else {
9573 #ifdef CONFIG_BNXT_SRIOV
9574 		struct bnxt_vf_info *vf = &bp->vf;
9575 
9576 		vf->fw_fid = le16_to_cpu(resp->fid);
9577 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9578 #endif
9579 	}
9580 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9581 
9582 hwrm_func_qcaps_exit:
9583 	hwrm_req_drop(bp, req);
9584 	return rc;
9585 }
9586 
9587 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9588 {
9589 	struct hwrm_dbg_qcaps_output *resp;
9590 	struct hwrm_dbg_qcaps_input *req;
9591 	int rc;
9592 
9593 	bp->fw_dbg_cap = 0;
9594 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9595 		return;
9596 
9597 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9598 	if (rc)
9599 		return;
9600 
9601 	req->fid = cpu_to_le16(0xffff);
9602 	resp = hwrm_req_hold(bp, req);
9603 	rc = hwrm_req_send(bp, req);
9604 	if (rc)
9605 		goto hwrm_dbg_qcaps_exit;
9606 
9607 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9608 
9609 hwrm_dbg_qcaps_exit:
9610 	hwrm_req_drop(bp, req);
9611 }
9612 
9613 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9614 
9615 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9616 {
9617 	int rc;
9618 
9619 	rc = __bnxt_hwrm_func_qcaps(bp);
9620 	if (rc)
9621 		return rc;
9622 
9623 	bnxt_hwrm_dbg_qcaps(bp);
9624 
9625 	rc = bnxt_hwrm_queue_qportcfg(bp);
9626 	if (rc) {
9627 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9628 		return rc;
9629 	}
9630 	if (bp->hwrm_spec_code >= 0x10803) {
9631 		rc = bnxt_alloc_ctx_mem(bp);
9632 		if (rc)
9633 			return rc;
9634 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9635 		if (!rc)
9636 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9637 	}
9638 	return 0;
9639 }
9640 
9641 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9642 {
9643 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9644 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9645 	u32 flags;
9646 	int rc;
9647 
9648 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9649 		return 0;
9650 
9651 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9652 	if (rc)
9653 		return rc;
9654 
9655 	resp = hwrm_req_hold(bp, req);
9656 	rc = hwrm_req_send(bp, req);
9657 	if (rc)
9658 		goto hwrm_cfa_adv_qcaps_exit;
9659 
9660 	flags = le32_to_cpu(resp->flags);
9661 	if (flags &
9662 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9663 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9664 
9665 	if (flags &
9666 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9667 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9668 
9669 	if (flags &
9670 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9671 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9672 
9673 hwrm_cfa_adv_qcaps_exit:
9674 	hwrm_req_drop(bp, req);
9675 	return rc;
9676 }
9677 
9678 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9679 {
9680 	if (bp->fw_health)
9681 		return 0;
9682 
9683 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9684 	if (!bp->fw_health)
9685 		return -ENOMEM;
9686 
9687 	mutex_init(&bp->fw_health->lock);
9688 	return 0;
9689 }
9690 
9691 static int bnxt_alloc_fw_health(struct bnxt *bp)
9692 {
9693 	int rc;
9694 
9695 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9696 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9697 		return 0;
9698 
9699 	rc = __bnxt_alloc_fw_health(bp);
9700 	if (rc) {
9701 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9702 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9703 		return rc;
9704 	}
9705 
9706 	return 0;
9707 }
9708 
9709 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9710 {
9711 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9712 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9713 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9714 }
9715 
9716 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9717 {
9718 	struct bnxt_fw_health *fw_health = bp->fw_health;
9719 	u32 reg_type;
9720 
9721 	if (!fw_health)
9722 		return;
9723 
9724 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9725 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9726 		fw_health->status_reliable = false;
9727 
9728 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9729 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9730 		fw_health->resets_reliable = false;
9731 }
9732 
9733 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9734 {
9735 	void __iomem *hs;
9736 	u32 status_loc;
9737 	u32 reg_type;
9738 	u32 sig;
9739 
9740 	if (bp->fw_health)
9741 		bp->fw_health->status_reliable = false;
9742 
9743 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9744 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9745 
9746 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9747 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9748 		if (!bp->chip_num) {
9749 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9750 			bp->chip_num = readl(bp->bar0 +
9751 					     BNXT_FW_HEALTH_WIN_BASE +
9752 					     BNXT_GRC_REG_CHIP_NUM);
9753 		}
9754 		if (!BNXT_CHIP_P5_PLUS(bp))
9755 			return;
9756 
9757 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9758 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9759 	} else {
9760 		status_loc = readl(hs + offsetof(struct hcomm_status,
9761 						 fw_status_loc));
9762 	}
9763 
9764 	if (__bnxt_alloc_fw_health(bp)) {
9765 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9766 		return;
9767 	}
9768 
9769 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9770 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9771 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9772 		__bnxt_map_fw_health_reg(bp, status_loc);
9773 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9774 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9775 	}
9776 
9777 	bp->fw_health->status_reliable = true;
9778 }
9779 
9780 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9781 {
9782 	struct bnxt_fw_health *fw_health = bp->fw_health;
9783 	u32 reg_base = 0xffffffff;
9784 	int i;
9785 
9786 	bp->fw_health->status_reliable = false;
9787 	bp->fw_health->resets_reliable = false;
9788 	/* Only pre-map the monitoring GRC registers using window 3 */
9789 	for (i = 0; i < 4; i++) {
9790 		u32 reg = fw_health->regs[i];
9791 
9792 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9793 			continue;
9794 		if (reg_base == 0xffffffff)
9795 			reg_base = reg & BNXT_GRC_BASE_MASK;
9796 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9797 			return -ERANGE;
9798 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9799 	}
9800 	bp->fw_health->status_reliable = true;
9801 	bp->fw_health->resets_reliable = true;
9802 	if (reg_base == 0xffffffff)
9803 		return 0;
9804 
9805 	__bnxt_map_fw_health_reg(bp, reg_base);
9806 	return 0;
9807 }
9808 
9809 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9810 {
9811 	if (!bp->fw_health)
9812 		return;
9813 
9814 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9815 		bp->fw_health->status_reliable = true;
9816 		bp->fw_health->resets_reliable = true;
9817 	} else {
9818 		bnxt_try_map_fw_health_reg(bp);
9819 	}
9820 }
9821 
9822 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9823 {
9824 	struct bnxt_fw_health *fw_health = bp->fw_health;
9825 	struct hwrm_error_recovery_qcfg_output *resp;
9826 	struct hwrm_error_recovery_qcfg_input *req;
9827 	int rc, i;
9828 
9829 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9830 		return 0;
9831 
9832 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9833 	if (rc)
9834 		return rc;
9835 
9836 	resp = hwrm_req_hold(bp, req);
9837 	rc = hwrm_req_send(bp, req);
9838 	if (rc)
9839 		goto err_recovery_out;
9840 	fw_health->flags = le32_to_cpu(resp->flags);
9841 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9842 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9843 		rc = -EINVAL;
9844 		goto err_recovery_out;
9845 	}
9846 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9847 	fw_health->master_func_wait_dsecs =
9848 		le32_to_cpu(resp->master_func_wait_period);
9849 	fw_health->normal_func_wait_dsecs =
9850 		le32_to_cpu(resp->normal_func_wait_period);
9851 	fw_health->post_reset_wait_dsecs =
9852 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9853 	fw_health->post_reset_max_wait_dsecs =
9854 		le32_to_cpu(resp->max_bailout_time_after_reset);
9855 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9856 		le32_to_cpu(resp->fw_health_status_reg);
9857 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9858 		le32_to_cpu(resp->fw_heartbeat_reg);
9859 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9860 		le32_to_cpu(resp->fw_reset_cnt_reg);
9861 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9862 		le32_to_cpu(resp->reset_inprogress_reg);
9863 	fw_health->fw_reset_inprog_reg_mask =
9864 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9865 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9866 	if (fw_health->fw_reset_seq_cnt >= 16) {
9867 		rc = -EINVAL;
9868 		goto err_recovery_out;
9869 	}
9870 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9871 		fw_health->fw_reset_seq_regs[i] =
9872 			le32_to_cpu(resp->reset_reg[i]);
9873 		fw_health->fw_reset_seq_vals[i] =
9874 			le32_to_cpu(resp->reset_reg_val[i]);
9875 		fw_health->fw_reset_seq_delay_msec[i] =
9876 			resp->delay_after_reset[i];
9877 	}
9878 err_recovery_out:
9879 	hwrm_req_drop(bp, req);
9880 	if (!rc)
9881 		rc = bnxt_map_fw_health_regs(bp);
9882 	if (rc)
9883 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9884 	return rc;
9885 }
9886 
9887 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9888 {
9889 	struct hwrm_func_reset_input *req;
9890 	int rc;
9891 
9892 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9893 	if (rc)
9894 		return rc;
9895 
9896 	req->enables = 0;
9897 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9898 	return hwrm_req_send(bp, req);
9899 }
9900 
9901 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9902 {
9903 	struct hwrm_nvm_get_dev_info_output nvm_info;
9904 
9905 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9906 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9907 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9908 			 nvm_info.nvm_cfg_ver_upd);
9909 }
9910 
9911 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9912 {
9913 	struct hwrm_queue_qportcfg_output *resp;
9914 	struct hwrm_queue_qportcfg_input *req;
9915 	u8 i, j, *qptr;
9916 	bool no_rdma;
9917 	int rc = 0;
9918 
9919 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9920 	if (rc)
9921 		return rc;
9922 
9923 	resp = hwrm_req_hold(bp, req);
9924 	rc = hwrm_req_send(bp, req);
9925 	if (rc)
9926 		goto qportcfg_exit;
9927 
9928 	if (!resp->max_configurable_queues) {
9929 		rc = -EINVAL;
9930 		goto qportcfg_exit;
9931 	}
9932 	bp->max_tc = resp->max_configurable_queues;
9933 	bp->max_lltc = resp->max_configurable_lossless_queues;
9934 	if (bp->max_tc > BNXT_MAX_QUEUE)
9935 		bp->max_tc = BNXT_MAX_QUEUE;
9936 
9937 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9938 	qptr = &resp->queue_id0;
9939 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9940 		bp->q_info[j].queue_id = *qptr;
9941 		bp->q_ids[i] = *qptr++;
9942 		bp->q_info[j].queue_profile = *qptr++;
9943 		bp->tc_to_qidx[j] = j;
9944 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9945 		    (no_rdma && BNXT_PF(bp)))
9946 			j++;
9947 	}
9948 	bp->max_q = bp->max_tc;
9949 	bp->max_tc = max_t(u8, j, 1);
9950 
9951 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9952 		bp->max_tc = 1;
9953 
9954 	if (bp->max_lltc > bp->max_tc)
9955 		bp->max_lltc = bp->max_tc;
9956 
9957 qportcfg_exit:
9958 	hwrm_req_drop(bp, req);
9959 	return rc;
9960 }
9961 
9962 static int bnxt_hwrm_poll(struct bnxt *bp)
9963 {
9964 	struct hwrm_ver_get_input *req;
9965 	int rc;
9966 
9967 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9968 	if (rc)
9969 		return rc;
9970 
9971 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9972 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9973 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9974 
9975 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9976 	rc = hwrm_req_send(bp, req);
9977 	return rc;
9978 }
9979 
9980 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9981 {
9982 	struct hwrm_ver_get_output *resp;
9983 	struct hwrm_ver_get_input *req;
9984 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9985 	u32 dev_caps_cfg, hwrm_ver;
9986 	int rc, len;
9987 
9988 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9989 	if (rc)
9990 		return rc;
9991 
9992 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9993 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9994 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9995 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9996 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9997 
9998 	resp = hwrm_req_hold(bp, req);
9999 	rc = hwrm_req_send(bp, req);
10000 	if (rc)
10001 		goto hwrm_ver_get_exit;
10002 
10003 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
10004 
10005 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
10006 			     resp->hwrm_intf_min_8b << 8 |
10007 			     resp->hwrm_intf_upd_8b;
10008 	if (resp->hwrm_intf_maj_8b < 1) {
10009 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
10010 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10011 			    resp->hwrm_intf_upd_8b);
10012 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10013 	}
10014 
10015 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10016 			HWRM_VERSION_UPDATE;
10017 
10018 	if (bp->hwrm_spec_code > hwrm_ver)
10019 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10020 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10021 			 HWRM_VERSION_UPDATE);
10022 	else
10023 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10024 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10025 			 resp->hwrm_intf_upd_8b);
10026 
10027 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10028 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10029 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10030 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10031 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10032 		len = FW_VER_STR_LEN;
10033 	} else {
10034 		fw_maj = resp->hwrm_fw_maj_8b;
10035 		fw_min = resp->hwrm_fw_min_8b;
10036 		fw_bld = resp->hwrm_fw_bld_8b;
10037 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10038 		len = BC_HWRM_STR_LEN;
10039 	}
10040 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10041 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10042 		 fw_rsv);
10043 
10044 	if (strlen(resp->active_pkg_name)) {
10045 		int fw_ver_len = strlen(bp->fw_ver_str);
10046 
10047 		snprintf(bp->fw_ver_str + fw_ver_len,
10048 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10049 			 resp->active_pkg_name);
10050 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10051 	}
10052 
10053 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10054 	if (!bp->hwrm_cmd_timeout)
10055 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10056 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10057 	if (!bp->hwrm_cmd_max_timeout)
10058 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10059 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10060 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10061 			    bp->hwrm_cmd_max_timeout / 1000);
10062 
10063 	if (resp->hwrm_intf_maj_8b >= 1) {
10064 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10065 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10066 	}
10067 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10068 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10069 
10070 	bp->chip_num = le16_to_cpu(resp->chip_num);
10071 	bp->chip_rev = resp->chip_rev;
10072 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10073 	    !resp->chip_metal)
10074 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10075 
10076 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10077 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10078 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10079 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10080 
10081 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10082 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10083 
10084 	if (dev_caps_cfg &
10085 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10086 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10087 
10088 	if (dev_caps_cfg &
10089 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10090 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10091 
10092 	if (dev_caps_cfg &
10093 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10094 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10095 
10096 hwrm_ver_get_exit:
10097 	hwrm_req_drop(bp, req);
10098 	return rc;
10099 }
10100 
10101 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10102 {
10103 	struct hwrm_fw_set_time_input *req;
10104 	struct tm tm;
10105 	time64_t now = ktime_get_real_seconds();
10106 	int rc;
10107 
10108 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10109 	    bp->hwrm_spec_code < 0x10400)
10110 		return -EOPNOTSUPP;
10111 
10112 	time64_to_tm(now, 0, &tm);
10113 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10114 	if (rc)
10115 		return rc;
10116 
10117 	req->year = cpu_to_le16(1900 + tm.tm_year);
10118 	req->month = 1 + tm.tm_mon;
10119 	req->day = tm.tm_mday;
10120 	req->hour = tm.tm_hour;
10121 	req->minute = tm.tm_min;
10122 	req->second = tm.tm_sec;
10123 	return hwrm_req_send(bp, req);
10124 }
10125 
10126 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10127 {
10128 	u64 sw_tmp;
10129 
10130 	hw &= mask;
10131 	sw_tmp = (*sw & ~mask) | hw;
10132 	if (hw < (*sw & mask))
10133 		sw_tmp += mask + 1;
10134 	WRITE_ONCE(*sw, sw_tmp);
10135 }
10136 
10137 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10138 				    int count, bool ignore_zero)
10139 {
10140 	int i;
10141 
10142 	for (i = 0; i < count; i++) {
10143 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10144 
10145 		if (ignore_zero && !hw)
10146 			continue;
10147 
10148 		if (masks[i] == -1ULL)
10149 			sw_stats[i] = hw;
10150 		else
10151 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10152 	}
10153 }
10154 
10155 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10156 {
10157 	if (!stats->hw_stats)
10158 		return;
10159 
10160 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10161 				stats->hw_masks, stats->len / 8, false);
10162 }
10163 
10164 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10165 {
10166 	struct bnxt_stats_mem *ring0_stats;
10167 	bool ignore_zero = false;
10168 	int i;
10169 
10170 	/* Chip bug.  Counter intermittently becomes 0. */
10171 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10172 		ignore_zero = true;
10173 
10174 	for (i = 0; i < bp->cp_nr_rings; i++) {
10175 		struct bnxt_napi *bnapi = bp->bnapi[i];
10176 		struct bnxt_cp_ring_info *cpr;
10177 		struct bnxt_stats_mem *stats;
10178 
10179 		cpr = &bnapi->cp_ring;
10180 		stats = &cpr->stats;
10181 		if (!i)
10182 			ring0_stats = stats;
10183 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10184 					ring0_stats->hw_masks,
10185 					ring0_stats->len / 8, ignore_zero);
10186 	}
10187 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10188 		struct bnxt_stats_mem *stats = &bp->port_stats;
10189 		__le64 *hw_stats = stats->hw_stats;
10190 		u64 *sw_stats = stats->sw_stats;
10191 		u64 *masks = stats->hw_masks;
10192 		int cnt;
10193 
10194 		cnt = sizeof(struct rx_port_stats) / 8;
10195 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10196 
10197 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10198 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10199 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10200 		cnt = sizeof(struct tx_port_stats) / 8;
10201 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10202 	}
10203 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10204 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10205 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10206 	}
10207 }
10208 
10209 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10210 {
10211 	struct hwrm_port_qstats_input *req;
10212 	struct bnxt_pf_info *pf = &bp->pf;
10213 	int rc;
10214 
10215 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10216 		return 0;
10217 
10218 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10219 		return -EOPNOTSUPP;
10220 
10221 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10222 	if (rc)
10223 		return rc;
10224 
10225 	req->flags = flags;
10226 	req->port_id = cpu_to_le16(pf->port_id);
10227 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10228 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10229 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10230 	return hwrm_req_send(bp, req);
10231 }
10232 
10233 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10234 {
10235 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10236 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10237 	struct hwrm_port_qstats_ext_output *resp_qs;
10238 	struct hwrm_port_qstats_ext_input *req_qs;
10239 	struct bnxt_pf_info *pf = &bp->pf;
10240 	u32 tx_stat_size;
10241 	int rc;
10242 
10243 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10244 		return 0;
10245 
10246 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10247 		return -EOPNOTSUPP;
10248 
10249 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10250 	if (rc)
10251 		return rc;
10252 
10253 	req_qs->flags = flags;
10254 	req_qs->port_id = cpu_to_le16(pf->port_id);
10255 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10256 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10257 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10258 		       sizeof(struct tx_port_stats_ext) : 0;
10259 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10260 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10261 	resp_qs = hwrm_req_hold(bp, req_qs);
10262 	rc = hwrm_req_send(bp, req_qs);
10263 	if (!rc) {
10264 		bp->fw_rx_stats_ext_size =
10265 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10266 		if (BNXT_FW_MAJ(bp) < 220 &&
10267 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10268 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10269 
10270 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10271 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10272 	} else {
10273 		bp->fw_rx_stats_ext_size = 0;
10274 		bp->fw_tx_stats_ext_size = 0;
10275 	}
10276 	hwrm_req_drop(bp, req_qs);
10277 
10278 	if (flags)
10279 		return rc;
10280 
10281 	if (bp->fw_tx_stats_ext_size <=
10282 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10283 		bp->pri2cos_valid = 0;
10284 		return rc;
10285 	}
10286 
10287 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10288 	if (rc)
10289 		return rc;
10290 
10291 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10292 
10293 	resp_qc = hwrm_req_hold(bp, req_qc);
10294 	rc = hwrm_req_send(bp, req_qc);
10295 	if (!rc) {
10296 		u8 *pri2cos;
10297 		int i, j;
10298 
10299 		pri2cos = &resp_qc->pri0_cos_queue_id;
10300 		for (i = 0; i < 8; i++) {
10301 			u8 queue_id = pri2cos[i];
10302 			u8 queue_idx;
10303 
10304 			/* Per port queue IDs start from 0, 10, 20, etc */
10305 			queue_idx = queue_id % 10;
10306 			if (queue_idx > BNXT_MAX_QUEUE) {
10307 				bp->pri2cos_valid = false;
10308 				hwrm_req_drop(bp, req_qc);
10309 				return rc;
10310 			}
10311 			for (j = 0; j < bp->max_q; j++) {
10312 				if (bp->q_ids[j] == queue_id)
10313 					bp->pri2cos_idx[i] = queue_idx;
10314 			}
10315 		}
10316 		bp->pri2cos_valid = true;
10317 	}
10318 	hwrm_req_drop(bp, req_qc);
10319 
10320 	return rc;
10321 }
10322 
10323 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10324 {
10325 	bnxt_hwrm_tunnel_dst_port_free(bp,
10326 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10327 	bnxt_hwrm_tunnel_dst_port_free(bp,
10328 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10329 }
10330 
10331 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10332 {
10333 	int rc, i;
10334 	u32 tpa_flags = 0;
10335 
10336 	if (set_tpa)
10337 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10338 	else if (BNXT_NO_FW_ACCESS(bp))
10339 		return 0;
10340 	for (i = 0; i < bp->nr_vnics; i++) {
10341 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10342 		if (rc) {
10343 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10344 				   i, rc);
10345 			return rc;
10346 		}
10347 	}
10348 	return 0;
10349 }
10350 
10351 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10352 {
10353 	int i;
10354 
10355 	for (i = 0; i < bp->nr_vnics; i++)
10356 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10357 }
10358 
10359 static void bnxt_clear_vnic(struct bnxt *bp)
10360 {
10361 	if (!bp->vnic_info)
10362 		return;
10363 
10364 	bnxt_hwrm_clear_vnic_filter(bp);
10365 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10366 		/* clear all RSS setting before free vnic ctx */
10367 		bnxt_hwrm_clear_vnic_rss(bp);
10368 		bnxt_hwrm_vnic_ctx_free(bp);
10369 	}
10370 	/* before free the vnic, undo the vnic tpa settings */
10371 	if (bp->flags & BNXT_FLAG_TPA)
10372 		bnxt_set_tpa(bp, false);
10373 	bnxt_hwrm_vnic_free(bp);
10374 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10375 		bnxt_hwrm_vnic_ctx_free(bp);
10376 }
10377 
10378 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10379 				    bool irq_re_init)
10380 {
10381 	bnxt_clear_vnic(bp);
10382 	bnxt_hwrm_ring_free(bp, close_path);
10383 	bnxt_hwrm_ring_grp_free(bp);
10384 	if (irq_re_init) {
10385 		bnxt_hwrm_stat_ctx_free(bp);
10386 		bnxt_hwrm_free_tunnel_ports(bp);
10387 	}
10388 }
10389 
10390 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10391 {
10392 	struct hwrm_func_cfg_input *req;
10393 	u8 evb_mode;
10394 	int rc;
10395 
10396 	if (br_mode == BRIDGE_MODE_VEB)
10397 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10398 	else if (br_mode == BRIDGE_MODE_VEPA)
10399 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10400 	else
10401 		return -EINVAL;
10402 
10403 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10404 	if (rc)
10405 		return rc;
10406 
10407 	req->fid = cpu_to_le16(0xffff);
10408 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10409 	req->evb_mode = evb_mode;
10410 	return hwrm_req_send(bp, req);
10411 }
10412 
10413 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10414 {
10415 	struct hwrm_func_cfg_input *req;
10416 	int rc;
10417 
10418 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10419 		return 0;
10420 
10421 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10422 	if (rc)
10423 		return rc;
10424 
10425 	req->fid = cpu_to_le16(0xffff);
10426 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10427 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10428 	if (size == 128)
10429 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10430 
10431 	return hwrm_req_send(bp, req);
10432 }
10433 
10434 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10435 {
10436 	int rc;
10437 
10438 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10439 		goto skip_rss_ctx;
10440 
10441 	/* allocate context for vnic */
10442 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10443 	if (rc) {
10444 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10445 			   vnic->vnic_id, rc);
10446 		goto vnic_setup_err;
10447 	}
10448 	bp->rsscos_nr_ctxs++;
10449 
10450 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10451 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10452 		if (rc) {
10453 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10454 				   vnic->vnic_id, rc);
10455 			goto vnic_setup_err;
10456 		}
10457 		bp->rsscos_nr_ctxs++;
10458 	}
10459 
10460 skip_rss_ctx:
10461 	/* configure default vnic, ring grp */
10462 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10463 	if (rc) {
10464 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10465 			   vnic->vnic_id, rc);
10466 		goto vnic_setup_err;
10467 	}
10468 
10469 	/* Enable RSS hashing on vnic */
10470 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10471 	if (rc) {
10472 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10473 			   vnic->vnic_id, rc);
10474 		goto vnic_setup_err;
10475 	}
10476 
10477 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10478 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10479 		if (rc) {
10480 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10481 				   vnic->vnic_id, rc);
10482 		}
10483 	}
10484 
10485 vnic_setup_err:
10486 	return rc;
10487 }
10488 
10489 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10490 			  u8 valid)
10491 {
10492 	struct hwrm_vnic_update_input *req;
10493 	int rc;
10494 
10495 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10496 	if (rc)
10497 		return rc;
10498 
10499 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10500 
10501 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10502 		req->mru = cpu_to_le16(vnic->mru);
10503 
10504 	req->enables = cpu_to_le32(valid);
10505 
10506 	return hwrm_req_send(bp, req);
10507 }
10508 
10509 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10510 {
10511 	int rc;
10512 
10513 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10514 	if (rc) {
10515 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10516 			   vnic->vnic_id, rc);
10517 		return rc;
10518 	}
10519 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10520 	if (rc)
10521 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10522 			   vnic->vnic_id, rc);
10523 	return rc;
10524 }
10525 
10526 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10527 {
10528 	int rc, i, nr_ctxs;
10529 
10530 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10531 	for (i = 0; i < nr_ctxs; i++) {
10532 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10533 		if (rc) {
10534 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10535 				   vnic->vnic_id, i, rc);
10536 			break;
10537 		}
10538 		bp->rsscos_nr_ctxs++;
10539 	}
10540 	if (i < nr_ctxs)
10541 		return -ENOMEM;
10542 
10543 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10544 	if (rc)
10545 		return rc;
10546 
10547 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10548 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10549 		if (rc) {
10550 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10551 				   vnic->vnic_id, rc);
10552 		}
10553 	}
10554 	return rc;
10555 }
10556 
10557 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10558 {
10559 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10560 		return __bnxt_setup_vnic_p5(bp, vnic);
10561 	else
10562 		return __bnxt_setup_vnic(bp, vnic);
10563 }
10564 
10565 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10566 				     struct bnxt_vnic_info *vnic,
10567 				     u16 start_rx_ring_idx, int rx_rings)
10568 {
10569 	int rc;
10570 
10571 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10572 	if (rc) {
10573 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10574 			   vnic->vnic_id, rc);
10575 		return rc;
10576 	}
10577 	return bnxt_setup_vnic(bp, vnic);
10578 }
10579 
10580 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10581 {
10582 	struct bnxt_vnic_info *vnic;
10583 	int i, rc = 0;
10584 
10585 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10586 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10587 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10588 	}
10589 
10590 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10591 		return 0;
10592 
10593 	for (i = 0; i < bp->rx_nr_rings; i++) {
10594 		u16 vnic_id = i + 1;
10595 		u16 ring_id = i;
10596 
10597 		if (vnic_id >= bp->nr_vnics)
10598 			break;
10599 
10600 		vnic = &bp->vnic_info[vnic_id];
10601 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10602 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10603 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10604 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10605 			break;
10606 	}
10607 	return rc;
10608 }
10609 
10610 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10611 			  bool all)
10612 {
10613 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10614 	struct bnxt_filter_base *usr_fltr, *tmp;
10615 	struct bnxt_ntuple_filter *ntp_fltr;
10616 	int i;
10617 
10618 	if (netif_running(bp->dev)) {
10619 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10620 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10621 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10622 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10623 		}
10624 	}
10625 	if (!all)
10626 		return;
10627 
10628 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10629 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10630 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10631 			ntp_fltr = container_of(usr_fltr,
10632 						struct bnxt_ntuple_filter,
10633 						base);
10634 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10635 			bnxt_del_ntp_filter(bp, ntp_fltr);
10636 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10637 		}
10638 	}
10639 
10640 	if (vnic->rss_table)
10641 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10642 				  vnic->rss_table,
10643 				  vnic->rss_table_dma_addr);
10644 	bp->num_rss_ctx--;
10645 }
10646 
10647 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10648 {
10649 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10650 	struct ethtool_rxfh_context *ctx;
10651 	unsigned long context;
10652 
10653 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10654 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10655 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10656 
10657 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10658 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10659 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10660 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10661 				   rss_ctx->index);
10662 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10663 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10664 		}
10665 	}
10666 }
10667 
10668 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10669 {
10670 	struct ethtool_rxfh_context *ctx;
10671 	unsigned long context;
10672 
10673 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10674 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10675 
10676 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10677 	}
10678 }
10679 
10680 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10681 static bool bnxt_promisc_ok(struct bnxt *bp)
10682 {
10683 #ifdef CONFIG_BNXT_SRIOV
10684 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10685 		return false;
10686 #endif
10687 	return true;
10688 }
10689 
10690 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10691 {
10692 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10693 	unsigned int rc = 0;
10694 
10695 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10696 	if (rc) {
10697 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10698 			   rc);
10699 		return rc;
10700 	}
10701 
10702 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10703 	if (rc) {
10704 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10705 			   rc);
10706 		return rc;
10707 	}
10708 	return rc;
10709 }
10710 
10711 static int bnxt_cfg_rx_mode(struct bnxt *);
10712 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10713 
10714 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10715 {
10716 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10717 	int rc = 0;
10718 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10719 
10720 	if (irq_re_init) {
10721 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10722 		if (rc) {
10723 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10724 				   rc);
10725 			goto err_out;
10726 		}
10727 	}
10728 
10729 	rc = bnxt_hwrm_ring_alloc(bp);
10730 	if (rc) {
10731 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10732 		goto err_out;
10733 	}
10734 
10735 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10736 	if (rc) {
10737 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10738 		goto err_out;
10739 	}
10740 
10741 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10742 		rx_nr_rings--;
10743 
10744 	/* default vnic 0 */
10745 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10746 	if (rc) {
10747 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10748 		goto err_out;
10749 	}
10750 
10751 	if (BNXT_VF(bp))
10752 		bnxt_hwrm_func_qcfg(bp);
10753 
10754 	rc = bnxt_setup_vnic(bp, vnic);
10755 	if (rc)
10756 		goto err_out;
10757 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10758 		bnxt_hwrm_update_rss_hash_cfg(bp);
10759 
10760 	if (bp->flags & BNXT_FLAG_RFS) {
10761 		rc = bnxt_alloc_rfs_vnics(bp);
10762 		if (rc)
10763 			goto err_out;
10764 	}
10765 
10766 	if (bp->flags & BNXT_FLAG_TPA) {
10767 		rc = bnxt_set_tpa(bp, true);
10768 		if (rc)
10769 			goto err_out;
10770 	}
10771 
10772 	if (BNXT_VF(bp))
10773 		bnxt_update_vf_mac(bp);
10774 
10775 	/* Filter for default vnic 0 */
10776 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10777 	if (rc) {
10778 		if (BNXT_VF(bp) && rc == -ENODEV)
10779 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10780 		else
10781 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10782 		goto err_out;
10783 	}
10784 	vnic->uc_filter_count = 1;
10785 
10786 	vnic->rx_mask = 0;
10787 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10788 		goto skip_rx_mask;
10789 
10790 	if (bp->dev->flags & IFF_BROADCAST)
10791 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10792 
10793 	if (bp->dev->flags & IFF_PROMISC)
10794 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10795 
10796 	if (bp->dev->flags & IFF_ALLMULTI) {
10797 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10798 		vnic->mc_list_count = 0;
10799 	} else if (bp->dev->flags & IFF_MULTICAST) {
10800 		u32 mask = 0;
10801 
10802 		bnxt_mc_list_updated(bp, &mask);
10803 		vnic->rx_mask |= mask;
10804 	}
10805 
10806 	rc = bnxt_cfg_rx_mode(bp);
10807 	if (rc)
10808 		goto err_out;
10809 
10810 skip_rx_mask:
10811 	rc = bnxt_hwrm_set_coal(bp);
10812 	if (rc)
10813 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10814 				rc);
10815 
10816 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10817 		rc = bnxt_setup_nitroa0_vnic(bp);
10818 		if (rc)
10819 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10820 				   rc);
10821 	}
10822 
10823 	if (BNXT_VF(bp)) {
10824 		bnxt_hwrm_func_qcfg(bp);
10825 		netdev_update_features(bp->dev);
10826 	}
10827 
10828 	return 0;
10829 
10830 err_out:
10831 	bnxt_hwrm_resource_free(bp, 0, true);
10832 
10833 	return rc;
10834 }
10835 
10836 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10837 {
10838 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10839 	return 0;
10840 }
10841 
10842 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10843 {
10844 	bnxt_init_cp_rings(bp);
10845 	bnxt_init_rx_rings(bp);
10846 	bnxt_init_tx_rings(bp);
10847 	bnxt_init_ring_grps(bp, irq_re_init);
10848 	bnxt_init_vnics(bp);
10849 
10850 	return bnxt_init_chip(bp, irq_re_init);
10851 }
10852 
10853 static int bnxt_set_real_num_queues(struct bnxt *bp)
10854 {
10855 	int rc;
10856 	struct net_device *dev = bp->dev;
10857 
10858 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10859 					  bp->tx_nr_rings_xdp);
10860 	if (rc)
10861 		return rc;
10862 
10863 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10864 	if (rc)
10865 		return rc;
10866 
10867 #ifdef CONFIG_RFS_ACCEL
10868 	if (bp->flags & BNXT_FLAG_RFS)
10869 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10870 #endif
10871 
10872 	return rc;
10873 }
10874 
10875 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10876 			     bool shared)
10877 {
10878 	int _rx = *rx, _tx = *tx;
10879 
10880 	if (shared) {
10881 		*rx = min_t(int, _rx, max);
10882 		*tx = min_t(int, _tx, max);
10883 	} else {
10884 		if (max < 2)
10885 			return -ENOMEM;
10886 
10887 		while (_rx + _tx > max) {
10888 			if (_rx > _tx && _rx > 1)
10889 				_rx--;
10890 			else if (_tx > 1)
10891 				_tx--;
10892 		}
10893 		*rx = _rx;
10894 		*tx = _tx;
10895 	}
10896 	return 0;
10897 }
10898 
10899 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10900 {
10901 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10902 }
10903 
10904 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10905 {
10906 	int tcs = bp->num_tc;
10907 
10908 	if (!tcs)
10909 		tcs = 1;
10910 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10911 }
10912 
10913 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10914 {
10915 	int tcs = bp->num_tc;
10916 
10917 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10918 	       bp->tx_nr_rings_xdp;
10919 }
10920 
10921 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10922 			   bool sh)
10923 {
10924 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10925 
10926 	if (tx_cp != *tx) {
10927 		int tx_saved = tx_cp, rc;
10928 
10929 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10930 		if (rc)
10931 			return rc;
10932 		if (tx_cp != tx_saved)
10933 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10934 		return 0;
10935 	}
10936 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10937 }
10938 
10939 static void bnxt_setup_msix(struct bnxt *bp)
10940 {
10941 	const int len = sizeof(bp->irq_tbl[0].name);
10942 	struct net_device *dev = bp->dev;
10943 	int tcs, i;
10944 
10945 	tcs = bp->num_tc;
10946 	if (tcs) {
10947 		int i, off, count;
10948 
10949 		for (i = 0; i < tcs; i++) {
10950 			count = bp->tx_nr_rings_per_tc;
10951 			off = BNXT_TC_TO_RING_BASE(bp, i);
10952 			netdev_set_tc_queue(dev, i, count, off);
10953 		}
10954 	}
10955 
10956 	for (i = 0; i < bp->cp_nr_rings; i++) {
10957 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10958 		char *attr;
10959 
10960 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10961 			attr = "TxRx";
10962 		else if (i < bp->rx_nr_rings)
10963 			attr = "rx";
10964 		else
10965 			attr = "tx";
10966 
10967 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10968 			 attr, i);
10969 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10970 	}
10971 }
10972 
10973 static int bnxt_init_int_mode(struct bnxt *bp);
10974 
10975 static int bnxt_change_msix(struct bnxt *bp, int total)
10976 {
10977 	struct msi_map map;
10978 	int i;
10979 
10980 	/* add MSIX to the end if needed */
10981 	for (i = bp->total_irqs; i < total; i++) {
10982 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
10983 		if (map.index < 0)
10984 			return bp->total_irqs;
10985 		bp->irq_tbl[i].vector = map.virq;
10986 		bp->total_irqs++;
10987 	}
10988 
10989 	/* trim MSIX from the end if needed */
10990 	for (i = bp->total_irqs; i > total; i--) {
10991 		map.index = i - 1;
10992 		map.virq = bp->irq_tbl[i - 1].vector;
10993 		pci_msix_free_irq(bp->pdev, map);
10994 		bp->total_irqs--;
10995 	}
10996 	return bp->total_irqs;
10997 }
10998 
10999 static int bnxt_setup_int_mode(struct bnxt *bp)
11000 {
11001 	int rc;
11002 
11003 	if (!bp->irq_tbl) {
11004 		rc = bnxt_init_int_mode(bp);
11005 		if (rc || !bp->irq_tbl)
11006 			return rc ?: -ENODEV;
11007 	}
11008 
11009 	bnxt_setup_msix(bp);
11010 
11011 	rc = bnxt_set_real_num_queues(bp);
11012 	return rc;
11013 }
11014 
11015 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11016 {
11017 	return bp->hw_resc.max_rsscos_ctxs;
11018 }
11019 
11020 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11021 {
11022 	return bp->hw_resc.max_vnics;
11023 }
11024 
11025 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11026 {
11027 	return bp->hw_resc.max_stat_ctxs;
11028 }
11029 
11030 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11031 {
11032 	return bp->hw_resc.max_cp_rings;
11033 }
11034 
11035 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11036 {
11037 	unsigned int cp = bp->hw_resc.max_cp_rings;
11038 
11039 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11040 		cp -= bnxt_get_ulp_msix_num(bp);
11041 
11042 	return cp;
11043 }
11044 
11045 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11046 {
11047 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11048 
11049 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11050 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11051 
11052 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11053 }
11054 
11055 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11056 {
11057 	bp->hw_resc.max_irqs = max_irqs;
11058 }
11059 
11060 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11061 {
11062 	unsigned int cp;
11063 
11064 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11065 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11066 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11067 	else
11068 		return cp - bp->cp_nr_rings;
11069 }
11070 
11071 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11072 {
11073 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11074 }
11075 
11076 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11077 {
11078 	int max_irq = bnxt_get_max_func_irqs(bp);
11079 	int total_req = bp->cp_nr_rings + num;
11080 
11081 	if (max_irq < total_req) {
11082 		num = max_irq - bp->cp_nr_rings;
11083 		if (num <= 0)
11084 			return 0;
11085 	}
11086 	return num;
11087 }
11088 
11089 static int bnxt_get_num_msix(struct bnxt *bp)
11090 {
11091 	if (!BNXT_NEW_RM(bp))
11092 		return bnxt_get_max_func_irqs(bp);
11093 
11094 	return bnxt_nq_rings_in_use(bp);
11095 }
11096 
11097 static int bnxt_init_int_mode(struct bnxt *bp)
11098 {
11099 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11100 
11101 	total_vecs = bnxt_get_num_msix(bp);
11102 	max = bnxt_get_max_func_irqs(bp);
11103 	if (total_vecs > max)
11104 		total_vecs = max;
11105 
11106 	if (!total_vecs)
11107 		return 0;
11108 
11109 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11110 		min = 2;
11111 
11112 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11113 					   PCI_IRQ_MSIX);
11114 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11115 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11116 		rc = -ENODEV;
11117 		goto msix_setup_exit;
11118 	}
11119 
11120 	tbl_size = total_vecs;
11121 	if (pci_msix_can_alloc_dyn(bp->pdev))
11122 		tbl_size = max;
11123 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11124 	if (bp->irq_tbl) {
11125 		for (i = 0; i < total_vecs; i++)
11126 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11127 
11128 		bp->total_irqs = total_vecs;
11129 		/* Trim rings based upon num of vectors allocated */
11130 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11131 				     total_vecs - ulp_msix, min == 1);
11132 		if (rc)
11133 			goto msix_setup_exit;
11134 
11135 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11136 		bp->cp_nr_rings = (min == 1) ?
11137 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11138 				  tx_cp + bp->rx_nr_rings;
11139 
11140 	} else {
11141 		rc = -ENOMEM;
11142 		goto msix_setup_exit;
11143 	}
11144 	return 0;
11145 
11146 msix_setup_exit:
11147 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11148 	kfree(bp->irq_tbl);
11149 	bp->irq_tbl = NULL;
11150 	pci_free_irq_vectors(bp->pdev);
11151 	return rc;
11152 }
11153 
11154 static void bnxt_clear_int_mode(struct bnxt *bp)
11155 {
11156 	pci_free_irq_vectors(bp->pdev);
11157 
11158 	kfree(bp->irq_tbl);
11159 	bp->irq_tbl = NULL;
11160 }
11161 
11162 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11163 {
11164 	bool irq_cleared = false;
11165 	bool irq_change = false;
11166 	int tcs = bp->num_tc;
11167 	int irqs_required;
11168 	int rc;
11169 
11170 	if (!bnxt_need_reserve_rings(bp))
11171 		return 0;
11172 
11173 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11174 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11175 
11176 		if (ulp_msix > bp->ulp_num_msix_want)
11177 			ulp_msix = bp->ulp_num_msix_want;
11178 		irqs_required = ulp_msix + bp->cp_nr_rings;
11179 	} else {
11180 		irqs_required = bnxt_get_num_msix(bp);
11181 	}
11182 
11183 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11184 		irq_change = true;
11185 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11186 			bnxt_ulp_irq_stop(bp);
11187 			bnxt_clear_int_mode(bp);
11188 			irq_cleared = true;
11189 		}
11190 	}
11191 	rc = __bnxt_reserve_rings(bp);
11192 	if (irq_cleared) {
11193 		if (!rc)
11194 			rc = bnxt_init_int_mode(bp);
11195 		bnxt_ulp_irq_restart(bp, rc);
11196 	} else if (irq_change && !rc) {
11197 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11198 			rc = -ENOSPC;
11199 	}
11200 	if (rc) {
11201 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11202 		return rc;
11203 	}
11204 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11205 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11206 		netdev_err(bp->dev, "tx ring reservation failure\n");
11207 		netdev_reset_tc(bp->dev);
11208 		bp->num_tc = 0;
11209 		if (bp->tx_nr_rings_xdp)
11210 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11211 		else
11212 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11213 		return -ENOMEM;
11214 	}
11215 	return 0;
11216 }
11217 
11218 static void bnxt_free_irq(struct bnxt *bp)
11219 {
11220 	struct bnxt_irq *irq;
11221 	int i;
11222 
11223 #ifdef CONFIG_RFS_ACCEL
11224 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11225 	bp->dev->rx_cpu_rmap = NULL;
11226 #endif
11227 	if (!bp->irq_tbl || !bp->bnapi)
11228 		return;
11229 
11230 	for (i = 0; i < bp->cp_nr_rings; i++) {
11231 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11232 
11233 		irq = &bp->irq_tbl[map_idx];
11234 		if (irq->requested) {
11235 			if (irq->have_cpumask) {
11236 				irq_update_affinity_hint(irq->vector, NULL);
11237 				free_cpumask_var(irq->cpu_mask);
11238 				irq->have_cpumask = 0;
11239 			}
11240 			free_irq(irq->vector, bp->bnapi[i]);
11241 		}
11242 
11243 		irq->requested = 0;
11244 	}
11245 }
11246 
11247 static int bnxt_request_irq(struct bnxt *bp)
11248 {
11249 	int i, j, rc = 0;
11250 	unsigned long flags = 0;
11251 #ifdef CONFIG_RFS_ACCEL
11252 	struct cpu_rmap *rmap;
11253 #endif
11254 
11255 	rc = bnxt_setup_int_mode(bp);
11256 	if (rc) {
11257 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11258 			   rc);
11259 		return rc;
11260 	}
11261 #ifdef CONFIG_RFS_ACCEL
11262 	rmap = bp->dev->rx_cpu_rmap;
11263 #endif
11264 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11265 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11266 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11267 
11268 #ifdef CONFIG_RFS_ACCEL
11269 		if (rmap && bp->bnapi[i]->rx_ring) {
11270 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11271 			if (rc)
11272 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11273 					    j);
11274 			j++;
11275 		}
11276 #endif
11277 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11278 				 bp->bnapi[i]);
11279 		if (rc)
11280 			break;
11281 
11282 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
11283 		irq->requested = 1;
11284 
11285 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11286 			int numa_node = dev_to_node(&bp->pdev->dev);
11287 
11288 			irq->have_cpumask = 1;
11289 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11290 					irq->cpu_mask);
11291 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11292 			if (rc) {
11293 				netdev_warn(bp->dev,
11294 					    "Update affinity hint failed, IRQ = %d\n",
11295 					    irq->vector);
11296 				break;
11297 			}
11298 		}
11299 	}
11300 	return rc;
11301 }
11302 
11303 static void bnxt_del_napi(struct bnxt *bp)
11304 {
11305 	int i;
11306 
11307 	if (!bp->bnapi)
11308 		return;
11309 
11310 	for (i = 0; i < bp->rx_nr_rings; i++)
11311 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11312 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11313 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11314 
11315 	for (i = 0; i < bp->cp_nr_rings; i++) {
11316 		struct bnxt_napi *bnapi = bp->bnapi[i];
11317 
11318 		__netif_napi_del(&bnapi->napi);
11319 	}
11320 	/* We called __netif_napi_del(), we need
11321 	 * to respect an RCU grace period before freeing napi structures.
11322 	 */
11323 	synchronize_net();
11324 }
11325 
11326 static void bnxt_init_napi(struct bnxt *bp)
11327 {
11328 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11329 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11330 	struct bnxt_napi *bnapi;
11331 	int i;
11332 
11333 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11334 		poll_fn = bnxt_poll_p5;
11335 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11336 		cp_nr_rings--;
11337 	for (i = 0; i < cp_nr_rings; i++) {
11338 		bnapi = bp->bnapi[i];
11339 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
11340 				      bnapi->index);
11341 	}
11342 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11343 		bnapi = bp->bnapi[cp_nr_rings];
11344 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11345 	}
11346 }
11347 
11348 static void bnxt_disable_napi(struct bnxt *bp)
11349 {
11350 	int i;
11351 
11352 	if (!bp->bnapi ||
11353 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11354 		return;
11355 
11356 	for (i = 0; i < bp->cp_nr_rings; i++) {
11357 		struct bnxt_napi *bnapi = bp->bnapi[i];
11358 		struct bnxt_cp_ring_info *cpr;
11359 
11360 		cpr = &bnapi->cp_ring;
11361 		if (bnapi->tx_fault)
11362 			cpr->sw_stats->tx.tx_resets++;
11363 		if (bnapi->in_reset)
11364 			cpr->sw_stats->rx.rx_resets++;
11365 		napi_disable(&bnapi->napi);
11366 	}
11367 }
11368 
11369 static void bnxt_enable_napi(struct bnxt *bp)
11370 {
11371 	int i;
11372 
11373 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11374 	for (i = 0; i < bp->cp_nr_rings; i++) {
11375 		struct bnxt_napi *bnapi = bp->bnapi[i];
11376 		struct bnxt_cp_ring_info *cpr;
11377 
11378 		bnapi->tx_fault = 0;
11379 
11380 		cpr = &bnapi->cp_ring;
11381 		bnapi->in_reset = false;
11382 
11383 		if (bnapi->rx_ring) {
11384 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11385 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11386 		}
11387 		napi_enable(&bnapi->napi);
11388 	}
11389 }
11390 
11391 void bnxt_tx_disable(struct bnxt *bp)
11392 {
11393 	int i;
11394 	struct bnxt_tx_ring_info *txr;
11395 
11396 	if (bp->tx_ring) {
11397 		for (i = 0; i < bp->tx_nr_rings; i++) {
11398 			txr = &bp->tx_ring[i];
11399 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11400 		}
11401 	}
11402 	/* Make sure napi polls see @dev_state change */
11403 	synchronize_net();
11404 	/* Drop carrier first to prevent TX timeout */
11405 	netif_carrier_off(bp->dev);
11406 	/* Stop all TX queues */
11407 	netif_tx_disable(bp->dev);
11408 }
11409 
11410 void bnxt_tx_enable(struct bnxt *bp)
11411 {
11412 	int i;
11413 	struct bnxt_tx_ring_info *txr;
11414 
11415 	for (i = 0; i < bp->tx_nr_rings; i++) {
11416 		txr = &bp->tx_ring[i];
11417 		WRITE_ONCE(txr->dev_state, 0);
11418 	}
11419 	/* Make sure napi polls see @dev_state change */
11420 	synchronize_net();
11421 	netif_tx_wake_all_queues(bp->dev);
11422 	if (BNXT_LINK_IS_UP(bp))
11423 		netif_carrier_on(bp->dev);
11424 }
11425 
11426 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11427 {
11428 	u8 active_fec = link_info->active_fec_sig_mode &
11429 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11430 
11431 	switch (active_fec) {
11432 	default:
11433 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11434 		return "None";
11435 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11436 		return "Clause 74 BaseR";
11437 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11438 		return "Clause 91 RS(528,514)";
11439 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11440 		return "Clause 91 RS544_1XN";
11441 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11442 		return "Clause 91 RS(544,514)";
11443 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11444 		return "Clause 91 RS272_1XN";
11445 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11446 		return "Clause 91 RS(272,257)";
11447 	}
11448 }
11449 
11450 void bnxt_report_link(struct bnxt *bp)
11451 {
11452 	if (BNXT_LINK_IS_UP(bp)) {
11453 		const char *signal = "";
11454 		const char *flow_ctrl;
11455 		const char *duplex;
11456 		u32 speed;
11457 		u16 fec;
11458 
11459 		netif_carrier_on(bp->dev);
11460 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11461 		if (speed == SPEED_UNKNOWN) {
11462 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11463 			return;
11464 		}
11465 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11466 			duplex = "full";
11467 		else
11468 			duplex = "half";
11469 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11470 			flow_ctrl = "ON - receive & transmit";
11471 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11472 			flow_ctrl = "ON - transmit";
11473 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11474 			flow_ctrl = "ON - receive";
11475 		else
11476 			flow_ctrl = "none";
11477 		if (bp->link_info.phy_qcfg_resp.option_flags &
11478 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11479 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11480 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11481 			switch (sig_mode) {
11482 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11483 				signal = "(NRZ) ";
11484 				break;
11485 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11486 				signal = "(PAM4 56Gbps) ";
11487 				break;
11488 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11489 				signal = "(PAM4 112Gbps) ";
11490 				break;
11491 			default:
11492 				break;
11493 			}
11494 		}
11495 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11496 			    speed, signal, duplex, flow_ctrl);
11497 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11498 			netdev_info(bp->dev, "EEE is %s\n",
11499 				    bp->eee.eee_active ? "active" :
11500 							 "not active");
11501 		fec = bp->link_info.fec_cfg;
11502 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11503 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11504 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11505 				    bnxt_report_fec(&bp->link_info));
11506 	} else {
11507 		netif_carrier_off(bp->dev);
11508 		netdev_err(bp->dev, "NIC Link is Down\n");
11509 	}
11510 }
11511 
11512 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11513 {
11514 	if (!resp->supported_speeds_auto_mode &&
11515 	    !resp->supported_speeds_force_mode &&
11516 	    !resp->supported_pam4_speeds_auto_mode &&
11517 	    !resp->supported_pam4_speeds_force_mode &&
11518 	    !resp->supported_speeds2_auto_mode &&
11519 	    !resp->supported_speeds2_force_mode)
11520 		return true;
11521 	return false;
11522 }
11523 
11524 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11525 {
11526 	struct bnxt_link_info *link_info = &bp->link_info;
11527 	struct hwrm_port_phy_qcaps_output *resp;
11528 	struct hwrm_port_phy_qcaps_input *req;
11529 	int rc = 0;
11530 
11531 	if (bp->hwrm_spec_code < 0x10201)
11532 		return 0;
11533 
11534 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11535 	if (rc)
11536 		return rc;
11537 
11538 	resp = hwrm_req_hold(bp, req);
11539 	rc = hwrm_req_send(bp, req);
11540 	if (rc)
11541 		goto hwrm_phy_qcaps_exit;
11542 
11543 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11544 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11545 		struct ethtool_keee *eee = &bp->eee;
11546 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11547 
11548 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11549 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11550 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11551 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11552 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11553 	}
11554 
11555 	if (bp->hwrm_spec_code >= 0x10a01) {
11556 		if (bnxt_phy_qcaps_no_speed(resp)) {
11557 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11558 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11559 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11560 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11561 			netdev_info(bp->dev, "Ethernet link enabled\n");
11562 			/* Phy re-enabled, reprobe the speeds */
11563 			link_info->support_auto_speeds = 0;
11564 			link_info->support_pam4_auto_speeds = 0;
11565 			link_info->support_auto_speeds2 = 0;
11566 		}
11567 	}
11568 	if (resp->supported_speeds_auto_mode)
11569 		link_info->support_auto_speeds =
11570 			le16_to_cpu(resp->supported_speeds_auto_mode);
11571 	if (resp->supported_pam4_speeds_auto_mode)
11572 		link_info->support_pam4_auto_speeds =
11573 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11574 	if (resp->supported_speeds2_auto_mode)
11575 		link_info->support_auto_speeds2 =
11576 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11577 
11578 	bp->port_count = resp->port_cnt;
11579 
11580 hwrm_phy_qcaps_exit:
11581 	hwrm_req_drop(bp, req);
11582 	return rc;
11583 }
11584 
11585 static void bnxt_hwrm_mac_qcaps(struct bnxt *bp)
11586 {
11587 	struct hwrm_port_mac_qcaps_output *resp;
11588 	struct hwrm_port_mac_qcaps_input *req;
11589 	int rc;
11590 
11591 	if (bp->hwrm_spec_code < 0x10a03)
11592 		return;
11593 
11594 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_QCAPS);
11595 	if (rc)
11596 		return;
11597 
11598 	resp = hwrm_req_hold(bp, req);
11599 	rc = hwrm_req_send_silent(bp, req);
11600 	if (!rc)
11601 		bp->mac_flags = resp->flags;
11602 	hwrm_req_drop(bp, req);
11603 }
11604 
11605 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11606 {
11607 	u16 diff = advertising ^ supported;
11608 
11609 	return ((supported | diff) != supported);
11610 }
11611 
11612 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11613 {
11614 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11615 
11616 	/* Check if any advertised speeds are no longer supported. The caller
11617 	 * holds the link_lock mutex, so we can modify link_info settings.
11618 	 */
11619 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11620 		if (bnxt_support_dropped(link_info->advertising,
11621 					 link_info->support_auto_speeds2)) {
11622 			link_info->advertising = link_info->support_auto_speeds2;
11623 			return true;
11624 		}
11625 		return false;
11626 	}
11627 	if (bnxt_support_dropped(link_info->advertising,
11628 				 link_info->support_auto_speeds)) {
11629 		link_info->advertising = link_info->support_auto_speeds;
11630 		return true;
11631 	}
11632 	if (bnxt_support_dropped(link_info->advertising_pam4,
11633 				 link_info->support_pam4_auto_speeds)) {
11634 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11635 		return true;
11636 	}
11637 	return false;
11638 }
11639 
11640 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11641 {
11642 	struct bnxt_link_info *link_info = &bp->link_info;
11643 	struct hwrm_port_phy_qcfg_output *resp;
11644 	struct hwrm_port_phy_qcfg_input *req;
11645 	u8 link_state = link_info->link_state;
11646 	bool support_changed;
11647 	int rc;
11648 
11649 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11650 	if (rc)
11651 		return rc;
11652 
11653 	resp = hwrm_req_hold(bp, req);
11654 	rc = hwrm_req_send(bp, req);
11655 	if (rc) {
11656 		hwrm_req_drop(bp, req);
11657 		if (BNXT_VF(bp) && rc == -ENODEV) {
11658 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11659 			rc = 0;
11660 		}
11661 		return rc;
11662 	}
11663 
11664 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11665 	link_info->phy_link_status = resp->link;
11666 	link_info->duplex = resp->duplex_cfg;
11667 	if (bp->hwrm_spec_code >= 0x10800)
11668 		link_info->duplex = resp->duplex_state;
11669 	link_info->pause = resp->pause;
11670 	link_info->auto_mode = resp->auto_mode;
11671 	link_info->auto_pause_setting = resp->auto_pause;
11672 	link_info->lp_pause = resp->link_partner_adv_pause;
11673 	link_info->force_pause_setting = resp->force_pause;
11674 	link_info->duplex_setting = resp->duplex_cfg;
11675 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11676 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11677 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11678 			link_info->active_lanes = resp->active_lanes;
11679 	} else {
11680 		link_info->link_speed = 0;
11681 		link_info->active_lanes = 0;
11682 	}
11683 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11684 	link_info->force_pam4_link_speed =
11685 		le16_to_cpu(resp->force_pam4_link_speed);
11686 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11687 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11688 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11689 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11690 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11691 	link_info->auto_pam4_link_speeds =
11692 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11693 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11694 	link_info->lp_auto_link_speeds =
11695 		le16_to_cpu(resp->link_partner_adv_speeds);
11696 	link_info->lp_auto_pam4_link_speeds =
11697 		resp->link_partner_pam4_adv_speeds;
11698 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11699 	link_info->phy_ver[0] = resp->phy_maj;
11700 	link_info->phy_ver[1] = resp->phy_min;
11701 	link_info->phy_ver[2] = resp->phy_bld;
11702 	link_info->media_type = resp->media_type;
11703 	link_info->phy_type = resp->phy_type;
11704 	link_info->transceiver = resp->xcvr_pkg_type;
11705 	link_info->phy_addr = resp->eee_config_phy_addr &
11706 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11707 	link_info->module_status = resp->module_status;
11708 
11709 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11710 		struct ethtool_keee *eee = &bp->eee;
11711 		u16 fw_speeds;
11712 
11713 		eee->eee_active = 0;
11714 		if (resp->eee_config_phy_addr &
11715 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11716 			eee->eee_active = 1;
11717 			fw_speeds = le16_to_cpu(
11718 				resp->link_partner_adv_eee_link_speed_mask);
11719 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11720 		}
11721 
11722 		/* Pull initial EEE config */
11723 		if (!chng_link_state) {
11724 			if (resp->eee_config_phy_addr &
11725 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11726 				eee->eee_enabled = 1;
11727 
11728 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11729 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11730 
11731 			if (resp->eee_config_phy_addr &
11732 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11733 				__le32 tmr;
11734 
11735 				eee->tx_lpi_enabled = 1;
11736 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11737 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11738 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11739 			}
11740 		}
11741 	}
11742 
11743 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11744 	if (bp->hwrm_spec_code >= 0x10504) {
11745 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11746 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11747 	}
11748 	/* TODO: need to add more logic to report VF link */
11749 	if (chng_link_state) {
11750 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11751 			link_info->link_state = BNXT_LINK_STATE_UP;
11752 		else
11753 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11754 		if (link_state != link_info->link_state)
11755 			bnxt_report_link(bp);
11756 	} else {
11757 		/* always link down if not require to update link state */
11758 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11759 	}
11760 	hwrm_req_drop(bp, req);
11761 
11762 	if (!BNXT_PHY_CFG_ABLE(bp))
11763 		return 0;
11764 
11765 	support_changed = bnxt_support_speed_dropped(link_info);
11766 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11767 		bnxt_hwrm_set_link_setting(bp, true, false);
11768 	return 0;
11769 }
11770 
11771 static void bnxt_get_port_module_status(struct bnxt *bp)
11772 {
11773 	struct bnxt_link_info *link_info = &bp->link_info;
11774 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11775 	u8 module_status;
11776 
11777 	if (bnxt_update_link(bp, true))
11778 		return;
11779 
11780 	module_status = link_info->module_status;
11781 	switch (module_status) {
11782 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11783 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11784 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11785 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11786 			    bp->pf.port_id);
11787 		if (bp->hwrm_spec_code >= 0x10201) {
11788 			netdev_warn(bp->dev, "Module part number %s\n",
11789 				    resp->phy_vendor_partnumber);
11790 		}
11791 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11792 			netdev_warn(bp->dev, "TX is disabled\n");
11793 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11794 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11795 	}
11796 }
11797 
11798 static void
11799 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11800 {
11801 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11802 		if (bp->hwrm_spec_code >= 0x10201)
11803 			req->auto_pause =
11804 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11805 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11806 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11807 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11808 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11809 		req->enables |=
11810 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11811 	} else {
11812 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11813 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11814 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11815 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11816 		req->enables |=
11817 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11818 		if (bp->hwrm_spec_code >= 0x10201) {
11819 			req->auto_pause = req->force_pause;
11820 			req->enables |= cpu_to_le32(
11821 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11822 		}
11823 	}
11824 }
11825 
11826 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11827 {
11828 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11829 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11830 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11831 			req->enables |=
11832 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11833 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11834 		} else if (bp->link_info.advertising) {
11835 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11836 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11837 		}
11838 		if (bp->link_info.advertising_pam4) {
11839 			req->enables |=
11840 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11841 			req->auto_link_pam4_speed_mask =
11842 				cpu_to_le16(bp->link_info.advertising_pam4);
11843 		}
11844 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11845 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11846 	} else {
11847 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11848 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11849 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11850 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11851 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11852 				   (u32)bp->link_info.req_link_speed);
11853 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11854 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11855 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11856 		} else {
11857 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11858 		}
11859 	}
11860 
11861 	/* tell chimp that the setting takes effect immediately */
11862 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11863 }
11864 
11865 int bnxt_hwrm_set_pause(struct bnxt *bp)
11866 {
11867 	struct hwrm_port_phy_cfg_input *req;
11868 	int rc;
11869 
11870 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11871 	if (rc)
11872 		return rc;
11873 
11874 	bnxt_hwrm_set_pause_common(bp, req);
11875 
11876 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11877 	    bp->link_info.force_link_chng)
11878 		bnxt_hwrm_set_link_common(bp, req);
11879 
11880 	rc = hwrm_req_send(bp, req);
11881 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11882 		/* since changing of pause setting doesn't trigger any link
11883 		 * change event, the driver needs to update the current pause
11884 		 * result upon successfully return of the phy_cfg command
11885 		 */
11886 		bp->link_info.pause =
11887 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11888 		bp->link_info.auto_pause_setting = 0;
11889 		if (!bp->link_info.force_link_chng)
11890 			bnxt_report_link(bp);
11891 	}
11892 	bp->link_info.force_link_chng = false;
11893 	return rc;
11894 }
11895 
11896 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11897 			      struct hwrm_port_phy_cfg_input *req)
11898 {
11899 	struct ethtool_keee *eee = &bp->eee;
11900 
11901 	if (eee->eee_enabled) {
11902 		u16 eee_speeds;
11903 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11904 
11905 		if (eee->tx_lpi_enabled)
11906 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11907 		else
11908 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11909 
11910 		req->flags |= cpu_to_le32(flags);
11911 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11912 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11913 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11914 	} else {
11915 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11916 	}
11917 }
11918 
11919 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11920 {
11921 	struct hwrm_port_phy_cfg_input *req;
11922 	int rc;
11923 
11924 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11925 	if (rc)
11926 		return rc;
11927 
11928 	if (set_pause)
11929 		bnxt_hwrm_set_pause_common(bp, req);
11930 
11931 	bnxt_hwrm_set_link_common(bp, req);
11932 
11933 	if (set_eee)
11934 		bnxt_hwrm_set_eee(bp, req);
11935 	return hwrm_req_send(bp, req);
11936 }
11937 
11938 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11939 {
11940 	struct hwrm_port_phy_cfg_input *req;
11941 	int rc;
11942 
11943 	if (!BNXT_SINGLE_PF(bp))
11944 		return 0;
11945 
11946 	if (pci_num_vf(bp->pdev) &&
11947 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11948 		return 0;
11949 
11950 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11951 	if (rc)
11952 		return rc;
11953 
11954 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11955 	rc = hwrm_req_send(bp, req);
11956 	if (!rc) {
11957 		mutex_lock(&bp->link_lock);
11958 		/* Device is not obliged link down in certain scenarios, even
11959 		 * when forced. Setting the state unknown is consistent with
11960 		 * driver startup and will force link state to be reported
11961 		 * during subsequent open based on PORT_PHY_QCFG.
11962 		 */
11963 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11964 		mutex_unlock(&bp->link_lock);
11965 	}
11966 	return rc;
11967 }
11968 
11969 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11970 {
11971 #ifdef CONFIG_TEE_BNXT_FW
11972 	int rc = tee_bnxt_fw_load();
11973 
11974 	if (rc)
11975 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11976 
11977 	return rc;
11978 #else
11979 	netdev_err(bp->dev, "OP-TEE not supported\n");
11980 	return -ENODEV;
11981 #endif
11982 }
11983 
11984 static int bnxt_try_recover_fw(struct bnxt *bp)
11985 {
11986 	if (bp->fw_health && bp->fw_health->status_reliable) {
11987 		int retry = 0, rc;
11988 		u32 sts;
11989 
11990 		do {
11991 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11992 			rc = bnxt_hwrm_poll(bp);
11993 			if (!BNXT_FW_IS_BOOTING(sts) &&
11994 			    !BNXT_FW_IS_RECOVERING(sts))
11995 				break;
11996 			retry++;
11997 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11998 
11999 		if (!BNXT_FW_IS_HEALTHY(sts)) {
12000 			netdev_err(bp->dev,
12001 				   "Firmware not responding, status: 0x%x\n",
12002 				   sts);
12003 			rc = -ENODEV;
12004 		}
12005 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
12006 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
12007 			return bnxt_fw_reset_via_optee(bp);
12008 		}
12009 		return rc;
12010 	}
12011 
12012 	return -ENODEV;
12013 }
12014 
12015 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
12016 {
12017 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
12018 
12019 	if (!BNXT_NEW_RM(bp))
12020 		return; /* no resource reservations required */
12021 
12022 	hw_resc->resv_cp_rings = 0;
12023 	hw_resc->resv_stat_ctxs = 0;
12024 	hw_resc->resv_irqs = 0;
12025 	hw_resc->resv_tx_rings = 0;
12026 	hw_resc->resv_rx_rings = 0;
12027 	hw_resc->resv_hw_ring_grps = 0;
12028 	hw_resc->resv_vnics = 0;
12029 	hw_resc->resv_rsscos_ctxs = 0;
12030 	if (!fw_reset) {
12031 		bp->tx_nr_rings = 0;
12032 		bp->rx_nr_rings = 0;
12033 	}
12034 }
12035 
12036 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12037 {
12038 	int rc;
12039 
12040 	if (!BNXT_NEW_RM(bp))
12041 		return 0; /* no resource reservations required */
12042 
12043 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12044 	if (rc)
12045 		netdev_err(bp->dev, "resc_qcaps failed\n");
12046 
12047 	bnxt_clear_reservations(bp, fw_reset);
12048 
12049 	return rc;
12050 }
12051 
12052 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12053 {
12054 	struct hwrm_func_drv_if_change_output *resp;
12055 	struct hwrm_func_drv_if_change_input *req;
12056 	bool fw_reset = !bp->irq_tbl;
12057 	bool resc_reinit = false;
12058 	int rc, retry = 0;
12059 	u32 flags = 0;
12060 
12061 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12062 		return 0;
12063 
12064 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12065 	if (rc)
12066 		return rc;
12067 
12068 	if (up)
12069 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12070 	resp = hwrm_req_hold(bp, req);
12071 
12072 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12073 	while (retry < BNXT_FW_IF_RETRY) {
12074 		rc = hwrm_req_send(bp, req);
12075 		if (rc != -EAGAIN)
12076 			break;
12077 
12078 		msleep(50);
12079 		retry++;
12080 	}
12081 
12082 	if (rc == -EAGAIN) {
12083 		hwrm_req_drop(bp, req);
12084 		return rc;
12085 	} else if (!rc) {
12086 		flags = le32_to_cpu(resp->flags);
12087 	} else if (up) {
12088 		rc = bnxt_try_recover_fw(bp);
12089 		fw_reset = true;
12090 	}
12091 	hwrm_req_drop(bp, req);
12092 	if (rc)
12093 		return rc;
12094 
12095 	if (!up) {
12096 		bnxt_inv_fw_health_reg(bp);
12097 		return 0;
12098 	}
12099 
12100 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12101 		resc_reinit = true;
12102 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12103 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12104 		fw_reset = true;
12105 	else
12106 		bnxt_remap_fw_health_regs(bp);
12107 
12108 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12109 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12110 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12111 		return -ENODEV;
12112 	}
12113 	if (resc_reinit || fw_reset) {
12114 		if (fw_reset) {
12115 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12116 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12117 				bnxt_ulp_irq_stop(bp);
12118 			bnxt_free_ctx_mem(bp, false);
12119 			bnxt_dcb_free(bp);
12120 			rc = bnxt_fw_init_one(bp);
12121 			if (rc) {
12122 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12123 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12124 				return rc;
12125 			}
12126 			bnxt_clear_int_mode(bp);
12127 			rc = bnxt_init_int_mode(bp);
12128 			if (rc) {
12129 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12130 				netdev_err(bp->dev, "init int mode failed\n");
12131 				return rc;
12132 			}
12133 		}
12134 		rc = bnxt_cancel_reservations(bp, fw_reset);
12135 	}
12136 	return rc;
12137 }
12138 
12139 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12140 {
12141 	struct hwrm_port_led_qcaps_output *resp;
12142 	struct hwrm_port_led_qcaps_input *req;
12143 	struct bnxt_pf_info *pf = &bp->pf;
12144 	int rc;
12145 
12146 	bp->num_leds = 0;
12147 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12148 		return 0;
12149 
12150 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12151 	if (rc)
12152 		return rc;
12153 
12154 	req->port_id = cpu_to_le16(pf->port_id);
12155 	resp = hwrm_req_hold(bp, req);
12156 	rc = hwrm_req_send(bp, req);
12157 	if (rc) {
12158 		hwrm_req_drop(bp, req);
12159 		return rc;
12160 	}
12161 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12162 		int i;
12163 
12164 		bp->num_leds = resp->num_leds;
12165 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12166 						 bp->num_leds);
12167 		for (i = 0; i < bp->num_leds; i++) {
12168 			struct bnxt_led_info *led = &bp->leds[i];
12169 			__le16 caps = led->led_state_caps;
12170 
12171 			if (!led->led_group_id ||
12172 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12173 				bp->num_leds = 0;
12174 				break;
12175 			}
12176 		}
12177 	}
12178 	hwrm_req_drop(bp, req);
12179 	return 0;
12180 }
12181 
12182 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12183 {
12184 	struct hwrm_wol_filter_alloc_output *resp;
12185 	struct hwrm_wol_filter_alloc_input *req;
12186 	int rc;
12187 
12188 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12189 	if (rc)
12190 		return rc;
12191 
12192 	req->port_id = cpu_to_le16(bp->pf.port_id);
12193 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12194 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12195 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12196 
12197 	resp = hwrm_req_hold(bp, req);
12198 	rc = hwrm_req_send(bp, req);
12199 	if (!rc)
12200 		bp->wol_filter_id = resp->wol_filter_id;
12201 	hwrm_req_drop(bp, req);
12202 	return rc;
12203 }
12204 
12205 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12206 {
12207 	struct hwrm_wol_filter_free_input *req;
12208 	int rc;
12209 
12210 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12211 	if (rc)
12212 		return rc;
12213 
12214 	req->port_id = cpu_to_le16(bp->pf.port_id);
12215 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12216 	req->wol_filter_id = bp->wol_filter_id;
12217 
12218 	return hwrm_req_send(bp, req);
12219 }
12220 
12221 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12222 {
12223 	struct hwrm_wol_filter_qcfg_output *resp;
12224 	struct hwrm_wol_filter_qcfg_input *req;
12225 	u16 next_handle = 0;
12226 	int rc;
12227 
12228 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12229 	if (rc)
12230 		return rc;
12231 
12232 	req->port_id = cpu_to_le16(bp->pf.port_id);
12233 	req->handle = cpu_to_le16(handle);
12234 	resp = hwrm_req_hold(bp, req);
12235 	rc = hwrm_req_send(bp, req);
12236 	if (!rc) {
12237 		next_handle = le16_to_cpu(resp->next_handle);
12238 		if (next_handle != 0) {
12239 			if (resp->wol_type ==
12240 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12241 				bp->wol = 1;
12242 				bp->wol_filter_id = resp->wol_filter_id;
12243 			}
12244 		}
12245 	}
12246 	hwrm_req_drop(bp, req);
12247 	return next_handle;
12248 }
12249 
12250 static void bnxt_get_wol_settings(struct bnxt *bp)
12251 {
12252 	u16 handle = 0;
12253 
12254 	bp->wol = 0;
12255 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12256 		return;
12257 
12258 	do {
12259 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12260 	} while (handle && handle != 0xffff);
12261 }
12262 
12263 static bool bnxt_eee_config_ok(struct bnxt *bp)
12264 {
12265 	struct ethtool_keee *eee = &bp->eee;
12266 	struct bnxt_link_info *link_info = &bp->link_info;
12267 
12268 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12269 		return true;
12270 
12271 	if (eee->eee_enabled) {
12272 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12273 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12274 
12275 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12276 
12277 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12278 			eee->eee_enabled = 0;
12279 			return false;
12280 		}
12281 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12282 			linkmode_and(eee->advertised, advertising,
12283 				     eee->supported);
12284 			return false;
12285 		}
12286 	}
12287 	return true;
12288 }
12289 
12290 static int bnxt_update_phy_setting(struct bnxt *bp)
12291 {
12292 	int rc;
12293 	bool update_link = false;
12294 	bool update_pause = false;
12295 	bool update_eee = false;
12296 	struct bnxt_link_info *link_info = &bp->link_info;
12297 
12298 	rc = bnxt_update_link(bp, true);
12299 	if (rc) {
12300 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12301 			   rc);
12302 		return rc;
12303 	}
12304 	if (!BNXT_SINGLE_PF(bp))
12305 		return 0;
12306 
12307 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12308 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12309 	    link_info->req_flow_ctrl)
12310 		update_pause = true;
12311 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12312 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12313 		update_pause = true;
12314 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12315 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12316 			update_link = true;
12317 		if (bnxt_force_speed_updated(link_info))
12318 			update_link = true;
12319 		if (link_info->req_duplex != link_info->duplex_setting)
12320 			update_link = true;
12321 	} else {
12322 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12323 			update_link = true;
12324 		if (bnxt_auto_speed_updated(link_info))
12325 			update_link = true;
12326 	}
12327 
12328 	/* The last close may have shutdown the link, so need to call
12329 	 * PHY_CFG to bring it back up.
12330 	 */
12331 	if (!BNXT_LINK_IS_UP(bp))
12332 		update_link = true;
12333 
12334 	if (!bnxt_eee_config_ok(bp))
12335 		update_eee = true;
12336 
12337 	if (update_link)
12338 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12339 	else if (update_pause)
12340 		rc = bnxt_hwrm_set_pause(bp);
12341 	if (rc) {
12342 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12343 			   rc);
12344 		return rc;
12345 	}
12346 
12347 	return rc;
12348 }
12349 
12350 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12351 
12352 static int bnxt_reinit_after_abort(struct bnxt *bp)
12353 {
12354 	int rc;
12355 
12356 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12357 		return -EBUSY;
12358 
12359 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12360 		return -ENODEV;
12361 
12362 	rc = bnxt_fw_init_one(bp);
12363 	if (!rc) {
12364 		bnxt_clear_int_mode(bp);
12365 		rc = bnxt_init_int_mode(bp);
12366 		if (!rc) {
12367 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12368 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12369 		}
12370 	}
12371 	return rc;
12372 }
12373 
12374 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12375 {
12376 	struct bnxt_ntuple_filter *ntp_fltr;
12377 	struct bnxt_l2_filter *l2_fltr;
12378 
12379 	if (list_empty(&fltr->list))
12380 		return;
12381 
12382 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12383 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12384 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12385 		atomic_inc(&l2_fltr->refcnt);
12386 		ntp_fltr->l2_fltr = l2_fltr;
12387 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12388 			bnxt_del_ntp_filter(bp, ntp_fltr);
12389 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12390 				   fltr->sw_id);
12391 		}
12392 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12393 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12394 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12395 			bnxt_del_l2_filter(bp, l2_fltr);
12396 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12397 				   fltr->sw_id);
12398 		}
12399 	}
12400 }
12401 
12402 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12403 {
12404 	struct bnxt_filter_base *usr_fltr, *tmp;
12405 
12406 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12407 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12408 }
12409 
12410 static int bnxt_set_xps_mapping(struct bnxt *bp)
12411 {
12412 	int numa_node = dev_to_node(&bp->pdev->dev);
12413 	unsigned int q_idx, map_idx, cpu, i;
12414 	const struct cpumask *cpu_mask_ptr;
12415 	int nr_cpus = num_online_cpus();
12416 	cpumask_t *q_map;
12417 	int rc = 0;
12418 
12419 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12420 	if (!q_map)
12421 		return -ENOMEM;
12422 
12423 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12424 	 * Each TC has the same number of TX queues. The nth TX queue for each
12425 	 * TC will have the same CPU mask.
12426 	 */
12427 	for (i = 0; i < nr_cpus; i++) {
12428 		map_idx = i % bp->tx_nr_rings_per_tc;
12429 		cpu = cpumask_local_spread(i, numa_node);
12430 		cpu_mask_ptr = get_cpu_mask(cpu);
12431 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12432 	}
12433 
12434 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12435 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12436 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12437 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12438 		if (rc) {
12439 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12440 				    q_idx);
12441 			break;
12442 		}
12443 	}
12444 
12445 	kfree(q_map);
12446 
12447 	return rc;
12448 }
12449 
12450 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12451 {
12452 	int rc = 0;
12453 
12454 	netif_carrier_off(bp->dev);
12455 	if (irq_re_init) {
12456 		/* Reserve rings now if none were reserved at driver probe. */
12457 		rc = bnxt_init_dflt_ring_mode(bp);
12458 		if (rc) {
12459 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12460 			return rc;
12461 		}
12462 	}
12463 	rc = bnxt_reserve_rings(bp, irq_re_init);
12464 	if (rc)
12465 		return rc;
12466 
12467 	rc = bnxt_alloc_mem(bp, irq_re_init);
12468 	if (rc) {
12469 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12470 		goto open_err_free_mem;
12471 	}
12472 
12473 	if (irq_re_init) {
12474 		bnxt_init_napi(bp);
12475 		rc = bnxt_request_irq(bp);
12476 		if (rc) {
12477 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12478 			goto open_err_irq;
12479 		}
12480 	}
12481 
12482 	rc = bnxt_init_nic(bp, irq_re_init);
12483 	if (rc) {
12484 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12485 		goto open_err_irq;
12486 	}
12487 
12488 	bnxt_enable_napi(bp);
12489 	bnxt_debug_dev_init(bp);
12490 
12491 	if (link_re_init) {
12492 		mutex_lock(&bp->link_lock);
12493 		rc = bnxt_update_phy_setting(bp);
12494 		mutex_unlock(&bp->link_lock);
12495 		if (rc) {
12496 			netdev_warn(bp->dev, "failed to update phy settings\n");
12497 			if (BNXT_SINGLE_PF(bp)) {
12498 				bp->link_info.phy_retry = true;
12499 				bp->link_info.phy_retry_expires =
12500 					jiffies + 5 * HZ;
12501 			}
12502 		}
12503 	}
12504 
12505 	if (irq_re_init) {
12506 		udp_tunnel_nic_reset_ntf(bp->dev);
12507 		rc = bnxt_set_xps_mapping(bp);
12508 		if (rc)
12509 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12510 	}
12511 
12512 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12513 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12514 			static_branch_enable(&bnxt_xdp_locking_key);
12515 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12516 		static_branch_disable(&bnxt_xdp_locking_key);
12517 	}
12518 	set_bit(BNXT_STATE_OPEN, &bp->state);
12519 	bnxt_enable_int(bp);
12520 	/* Enable TX queues */
12521 	bnxt_tx_enable(bp);
12522 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12523 	/* Poll link status and check for SFP+ module status */
12524 	mutex_lock(&bp->link_lock);
12525 	bnxt_get_port_module_status(bp);
12526 	mutex_unlock(&bp->link_lock);
12527 
12528 	/* VF-reps may need to be re-opened after the PF is re-opened */
12529 	if (BNXT_PF(bp))
12530 		bnxt_vf_reps_open(bp);
12531 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12532 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12533 	bnxt_ptp_init_rtc(bp, true);
12534 	bnxt_ptp_cfg_tstamp_filters(bp);
12535 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12536 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12537 	bnxt_cfg_usr_fltrs(bp);
12538 	return 0;
12539 
12540 open_err_irq:
12541 	bnxt_del_napi(bp);
12542 
12543 open_err_free_mem:
12544 	bnxt_free_skbs(bp);
12545 	bnxt_free_irq(bp);
12546 	bnxt_free_mem(bp, true);
12547 	return rc;
12548 }
12549 
12550 /* rtnl_lock held */
12551 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12552 {
12553 	int rc = 0;
12554 
12555 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12556 		rc = -EIO;
12557 	if (!rc)
12558 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12559 	if (rc) {
12560 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12561 		dev_close(bp->dev);
12562 	}
12563 	return rc;
12564 }
12565 
12566 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12567  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12568  * self tests.
12569  */
12570 int bnxt_half_open_nic(struct bnxt *bp)
12571 {
12572 	int rc = 0;
12573 
12574 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12575 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12576 		rc = -ENODEV;
12577 		goto half_open_err;
12578 	}
12579 
12580 	rc = bnxt_alloc_mem(bp, true);
12581 	if (rc) {
12582 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12583 		goto half_open_err;
12584 	}
12585 	bnxt_init_napi(bp);
12586 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12587 	rc = bnxt_init_nic(bp, true);
12588 	if (rc) {
12589 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12590 		bnxt_del_napi(bp);
12591 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12592 		goto half_open_err;
12593 	}
12594 	return 0;
12595 
12596 half_open_err:
12597 	bnxt_free_skbs(bp);
12598 	bnxt_free_mem(bp, true);
12599 	dev_close(bp->dev);
12600 	return rc;
12601 }
12602 
12603 /* rtnl_lock held, this call can only be made after a previous successful
12604  * call to bnxt_half_open_nic().
12605  */
12606 void bnxt_half_close_nic(struct bnxt *bp)
12607 {
12608 	bnxt_hwrm_resource_free(bp, false, true);
12609 	bnxt_del_napi(bp);
12610 	bnxt_free_skbs(bp);
12611 	bnxt_free_mem(bp, true);
12612 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12613 }
12614 
12615 void bnxt_reenable_sriov(struct bnxt *bp)
12616 {
12617 	if (BNXT_PF(bp)) {
12618 		struct bnxt_pf_info *pf = &bp->pf;
12619 		int n = pf->active_vfs;
12620 
12621 		if (n)
12622 			bnxt_cfg_hw_sriov(bp, &n, true);
12623 	}
12624 }
12625 
12626 static int bnxt_open(struct net_device *dev)
12627 {
12628 	struct bnxt *bp = netdev_priv(dev);
12629 	int rc;
12630 
12631 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12632 		rc = bnxt_reinit_after_abort(bp);
12633 		if (rc) {
12634 			if (rc == -EBUSY)
12635 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12636 			else
12637 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12638 			return -ENODEV;
12639 		}
12640 	}
12641 
12642 	rc = bnxt_hwrm_if_change(bp, true);
12643 	if (rc)
12644 		return rc;
12645 
12646 	rc = __bnxt_open_nic(bp, true, true);
12647 	if (rc) {
12648 		bnxt_hwrm_if_change(bp, false);
12649 	} else {
12650 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12651 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12652 				bnxt_queue_sp_work(bp,
12653 						   BNXT_RESTART_ULP_SP_EVENT);
12654 		}
12655 	}
12656 
12657 	return rc;
12658 }
12659 
12660 static bool bnxt_drv_busy(struct bnxt *bp)
12661 {
12662 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12663 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12664 }
12665 
12666 static void bnxt_get_ring_stats(struct bnxt *bp,
12667 				struct rtnl_link_stats64 *stats);
12668 
12669 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12670 			     bool link_re_init)
12671 {
12672 	/* Close the VF-reps before closing PF */
12673 	if (BNXT_PF(bp))
12674 		bnxt_vf_reps_close(bp);
12675 
12676 	/* Change device state to avoid TX queue wake up's */
12677 	bnxt_tx_disable(bp);
12678 
12679 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12680 	smp_mb__after_atomic();
12681 	while (bnxt_drv_busy(bp))
12682 		msleep(20);
12683 
12684 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12685 		bnxt_clear_rss_ctxs(bp);
12686 	/* Flush rings and disable interrupts */
12687 	bnxt_shutdown_nic(bp, irq_re_init);
12688 
12689 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12690 
12691 	bnxt_debug_dev_exit(bp);
12692 	bnxt_disable_napi(bp);
12693 	del_timer_sync(&bp->timer);
12694 	bnxt_free_skbs(bp);
12695 
12696 	/* Save ring stats before shutdown */
12697 	if (bp->bnapi && irq_re_init) {
12698 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12699 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12700 	}
12701 	if (irq_re_init) {
12702 		bnxt_free_irq(bp);
12703 		bnxt_del_napi(bp);
12704 	}
12705 	bnxt_free_mem(bp, irq_re_init);
12706 }
12707 
12708 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12709 {
12710 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12711 		/* If we get here, it means firmware reset is in progress
12712 		 * while we are trying to close.  We can safely proceed with
12713 		 * the close because we are holding rtnl_lock().  Some firmware
12714 		 * messages may fail as we proceed to close.  We set the
12715 		 * ABORT_ERR flag here so that the FW reset thread will later
12716 		 * abort when it gets the rtnl_lock() and sees the flag.
12717 		 */
12718 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12719 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12720 	}
12721 
12722 #ifdef CONFIG_BNXT_SRIOV
12723 	if (bp->sriov_cfg) {
12724 		int rc;
12725 
12726 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12727 						      !bp->sriov_cfg,
12728 						      BNXT_SRIOV_CFG_WAIT_TMO);
12729 		if (!rc)
12730 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12731 		else if (rc < 0)
12732 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12733 	}
12734 #endif
12735 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12736 }
12737 
12738 static int bnxt_close(struct net_device *dev)
12739 {
12740 	struct bnxt *bp = netdev_priv(dev);
12741 
12742 	bnxt_close_nic(bp, true, true);
12743 	bnxt_hwrm_shutdown_link(bp);
12744 	bnxt_hwrm_if_change(bp, false);
12745 	return 0;
12746 }
12747 
12748 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12749 				   u16 *val)
12750 {
12751 	struct hwrm_port_phy_mdio_read_output *resp;
12752 	struct hwrm_port_phy_mdio_read_input *req;
12753 	int rc;
12754 
12755 	if (bp->hwrm_spec_code < 0x10a00)
12756 		return -EOPNOTSUPP;
12757 
12758 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12759 	if (rc)
12760 		return rc;
12761 
12762 	req->port_id = cpu_to_le16(bp->pf.port_id);
12763 	req->phy_addr = phy_addr;
12764 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12765 	if (mdio_phy_id_is_c45(phy_addr)) {
12766 		req->cl45_mdio = 1;
12767 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12768 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12769 		req->reg_addr = cpu_to_le16(reg);
12770 	}
12771 
12772 	resp = hwrm_req_hold(bp, req);
12773 	rc = hwrm_req_send(bp, req);
12774 	if (!rc)
12775 		*val = le16_to_cpu(resp->reg_data);
12776 	hwrm_req_drop(bp, req);
12777 	return rc;
12778 }
12779 
12780 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12781 				    u16 val)
12782 {
12783 	struct hwrm_port_phy_mdio_write_input *req;
12784 	int rc;
12785 
12786 	if (bp->hwrm_spec_code < 0x10a00)
12787 		return -EOPNOTSUPP;
12788 
12789 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_WRITE);
12790 	if (rc)
12791 		return rc;
12792 
12793 	req->port_id = cpu_to_le16(bp->pf.port_id);
12794 	req->phy_addr = phy_addr;
12795 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12796 	if (mdio_phy_id_is_c45(phy_addr)) {
12797 		req->cl45_mdio = 1;
12798 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12799 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12800 		req->reg_addr = cpu_to_le16(reg);
12801 	}
12802 	req->reg_data = cpu_to_le16(val);
12803 
12804 	return hwrm_req_send(bp, req);
12805 }
12806 
12807 /* rtnl_lock held */
12808 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12809 {
12810 	struct mii_ioctl_data *mdio = if_mii(ifr);
12811 	struct bnxt *bp = netdev_priv(dev);
12812 	int rc;
12813 
12814 	switch (cmd) {
12815 	case SIOCGMIIPHY:
12816 		mdio->phy_id = bp->link_info.phy_addr;
12817 
12818 		fallthrough;
12819 	case SIOCGMIIREG: {
12820 		u16 mii_regval = 0;
12821 
12822 		if (!netif_running(dev))
12823 			return -EAGAIN;
12824 
12825 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12826 					     &mii_regval);
12827 		mdio->val_out = mii_regval;
12828 		return rc;
12829 	}
12830 
12831 	case SIOCSMIIREG:
12832 		if (!netif_running(dev))
12833 			return -EAGAIN;
12834 
12835 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12836 						mdio->val_in);
12837 
12838 	case SIOCSHWTSTAMP:
12839 		return bnxt_hwtstamp_set(dev, ifr);
12840 
12841 	case SIOCGHWTSTAMP:
12842 		return bnxt_hwtstamp_get(dev, ifr);
12843 
12844 	default:
12845 		/* do nothing */
12846 		break;
12847 	}
12848 	return -EOPNOTSUPP;
12849 }
12850 
12851 static void bnxt_get_ring_stats(struct bnxt *bp,
12852 				struct rtnl_link_stats64 *stats)
12853 {
12854 	int i;
12855 
12856 	for (i = 0; i < bp->cp_nr_rings; i++) {
12857 		struct bnxt_napi *bnapi = bp->bnapi[i];
12858 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12859 		u64 *sw = cpr->stats.sw_stats;
12860 
12861 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12862 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12863 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12864 
12865 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12866 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12867 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12868 
12869 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12870 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12871 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12872 
12873 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12874 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12875 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12876 
12877 		stats->rx_missed_errors +=
12878 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12879 
12880 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12881 
12882 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12883 
12884 		stats->rx_dropped +=
12885 			cpr->sw_stats->rx.rx_netpoll_discards +
12886 			cpr->sw_stats->rx.rx_oom_discards;
12887 	}
12888 }
12889 
12890 static void bnxt_add_prev_stats(struct bnxt *bp,
12891 				struct rtnl_link_stats64 *stats)
12892 {
12893 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12894 
12895 	stats->rx_packets += prev_stats->rx_packets;
12896 	stats->tx_packets += prev_stats->tx_packets;
12897 	stats->rx_bytes += prev_stats->rx_bytes;
12898 	stats->tx_bytes += prev_stats->tx_bytes;
12899 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12900 	stats->multicast += prev_stats->multicast;
12901 	stats->rx_dropped += prev_stats->rx_dropped;
12902 	stats->tx_dropped += prev_stats->tx_dropped;
12903 }
12904 
12905 static void
12906 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12907 {
12908 	struct bnxt *bp = netdev_priv(dev);
12909 
12910 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12911 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12912 	 * we check the BNXT_STATE_OPEN flag.
12913 	 */
12914 	smp_mb__after_atomic();
12915 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12916 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12917 		*stats = bp->net_stats_prev;
12918 		return;
12919 	}
12920 
12921 	bnxt_get_ring_stats(bp, stats);
12922 	bnxt_add_prev_stats(bp, stats);
12923 
12924 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12925 		u64 *rx = bp->port_stats.sw_stats;
12926 		u64 *tx = bp->port_stats.sw_stats +
12927 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12928 
12929 		stats->rx_crc_errors =
12930 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12931 		stats->rx_frame_errors =
12932 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12933 		stats->rx_length_errors =
12934 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12935 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12936 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12937 		stats->rx_errors =
12938 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12939 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12940 		stats->collisions =
12941 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12942 		stats->tx_fifo_errors =
12943 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12944 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12945 	}
12946 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12947 }
12948 
12949 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12950 					struct bnxt_total_ring_err_stats *stats,
12951 					struct bnxt_cp_ring_info *cpr)
12952 {
12953 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12954 	u64 *hw_stats = cpr->stats.sw_stats;
12955 
12956 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12957 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12958 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12959 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12960 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12961 	stats->rx_total_ring_discards +=
12962 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12963 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12964 	stats->tx_total_ring_discards +=
12965 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12966 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12967 }
12968 
12969 void bnxt_get_ring_err_stats(struct bnxt *bp,
12970 			     struct bnxt_total_ring_err_stats *stats)
12971 {
12972 	int i;
12973 
12974 	for (i = 0; i < bp->cp_nr_rings; i++)
12975 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12976 }
12977 
12978 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12979 {
12980 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12981 	struct net_device *dev = bp->dev;
12982 	struct netdev_hw_addr *ha;
12983 	u8 *haddr;
12984 	int mc_count = 0;
12985 	bool update = false;
12986 	int off = 0;
12987 
12988 	netdev_for_each_mc_addr(ha, dev) {
12989 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12990 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12991 			vnic->mc_list_count = 0;
12992 			return false;
12993 		}
12994 		haddr = ha->addr;
12995 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12996 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12997 			update = true;
12998 		}
12999 		off += ETH_ALEN;
13000 		mc_count++;
13001 	}
13002 	if (mc_count)
13003 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13004 
13005 	if (mc_count != vnic->mc_list_count) {
13006 		vnic->mc_list_count = mc_count;
13007 		update = true;
13008 	}
13009 	return update;
13010 }
13011 
13012 static bool bnxt_uc_list_updated(struct bnxt *bp)
13013 {
13014 	struct net_device *dev = bp->dev;
13015 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13016 	struct netdev_hw_addr *ha;
13017 	int off = 0;
13018 
13019 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
13020 		return true;
13021 
13022 	netdev_for_each_uc_addr(ha, dev) {
13023 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
13024 			return true;
13025 
13026 		off += ETH_ALEN;
13027 	}
13028 	return false;
13029 }
13030 
13031 static void bnxt_set_rx_mode(struct net_device *dev)
13032 {
13033 	struct bnxt *bp = netdev_priv(dev);
13034 	struct bnxt_vnic_info *vnic;
13035 	bool mc_update = false;
13036 	bool uc_update;
13037 	u32 mask;
13038 
13039 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13040 		return;
13041 
13042 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13043 	mask = vnic->rx_mask;
13044 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13045 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13046 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13047 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13048 
13049 	if (dev->flags & IFF_PROMISC)
13050 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13051 
13052 	uc_update = bnxt_uc_list_updated(bp);
13053 
13054 	if (dev->flags & IFF_BROADCAST)
13055 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13056 	if (dev->flags & IFF_ALLMULTI) {
13057 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13058 		vnic->mc_list_count = 0;
13059 	} else if (dev->flags & IFF_MULTICAST) {
13060 		mc_update = bnxt_mc_list_updated(bp, &mask);
13061 	}
13062 
13063 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13064 		vnic->rx_mask = mask;
13065 
13066 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13067 	}
13068 }
13069 
13070 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13071 {
13072 	struct net_device *dev = bp->dev;
13073 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13074 	struct netdev_hw_addr *ha;
13075 	int i, off = 0, rc;
13076 	bool uc_update;
13077 
13078 	netif_addr_lock_bh(dev);
13079 	uc_update = bnxt_uc_list_updated(bp);
13080 	netif_addr_unlock_bh(dev);
13081 
13082 	if (!uc_update)
13083 		goto skip_uc;
13084 
13085 	for (i = 1; i < vnic->uc_filter_count; i++) {
13086 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13087 
13088 		bnxt_hwrm_l2_filter_free(bp, fltr);
13089 		bnxt_del_l2_filter(bp, fltr);
13090 	}
13091 
13092 	vnic->uc_filter_count = 1;
13093 
13094 	netif_addr_lock_bh(dev);
13095 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13096 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13097 	} else {
13098 		netdev_for_each_uc_addr(ha, dev) {
13099 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13100 			off += ETH_ALEN;
13101 			vnic->uc_filter_count++;
13102 		}
13103 	}
13104 	netif_addr_unlock_bh(dev);
13105 
13106 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13107 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13108 		if (rc) {
13109 			if (BNXT_VF(bp) && rc == -ENODEV) {
13110 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13111 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13112 				else
13113 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13114 				rc = 0;
13115 			} else {
13116 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13117 			}
13118 			vnic->uc_filter_count = i;
13119 			return rc;
13120 		}
13121 	}
13122 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13123 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13124 
13125 skip_uc:
13126 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13127 	    !bnxt_promisc_ok(bp))
13128 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13129 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13130 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13131 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13132 			    rc);
13133 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13134 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13135 		vnic->mc_list_count = 0;
13136 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13137 	}
13138 	if (rc)
13139 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13140 			   rc);
13141 
13142 	return rc;
13143 }
13144 
13145 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13146 {
13147 #ifdef CONFIG_BNXT_SRIOV
13148 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13149 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13150 
13151 		/* No minimum rings were provisioned by the PF.  Don't
13152 		 * reserve rings by default when device is down.
13153 		 */
13154 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13155 			return true;
13156 
13157 		if (!netif_running(bp->dev))
13158 			return false;
13159 	}
13160 #endif
13161 	return true;
13162 }
13163 
13164 /* If the chip and firmware supports RFS */
13165 static bool bnxt_rfs_supported(struct bnxt *bp)
13166 {
13167 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13168 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13169 			return true;
13170 		return false;
13171 	}
13172 	/* 212 firmware is broken for aRFS */
13173 	if (BNXT_FW_MAJ(bp) == 212)
13174 		return false;
13175 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13176 		return true;
13177 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13178 		return true;
13179 	return false;
13180 }
13181 
13182 /* If runtime conditions support RFS */
13183 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13184 {
13185 	struct bnxt_hw_rings hwr = {0};
13186 	int max_vnics, max_rss_ctxs;
13187 
13188 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13189 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13190 		return bnxt_rfs_supported(bp);
13191 
13192 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13193 		return false;
13194 
13195 	hwr.grp = bp->rx_nr_rings;
13196 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13197 	if (new_rss_ctx)
13198 		hwr.vnic++;
13199 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13200 	max_vnics = bnxt_get_max_func_vnics(bp);
13201 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13202 
13203 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13204 		if (bp->rx_nr_rings > 1)
13205 			netdev_warn(bp->dev,
13206 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13207 				    min(max_rss_ctxs - 1, max_vnics - 1));
13208 		return false;
13209 	}
13210 
13211 	if (!BNXT_NEW_RM(bp))
13212 		return true;
13213 
13214 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13215 	 * issue that will mess up the default VNIC if we reduce the
13216 	 * reservations.
13217 	 */
13218 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13219 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13220 		return true;
13221 
13222 	bnxt_hwrm_reserve_rings(bp, &hwr);
13223 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13224 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13225 		return true;
13226 
13227 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13228 	hwr.vnic = 1;
13229 	hwr.rss_ctx = 0;
13230 	bnxt_hwrm_reserve_rings(bp, &hwr);
13231 	return false;
13232 }
13233 
13234 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13235 					   netdev_features_t features)
13236 {
13237 	struct bnxt *bp = netdev_priv(dev);
13238 	netdev_features_t vlan_features;
13239 
13240 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13241 		features &= ~NETIF_F_NTUPLE;
13242 
13243 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13244 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13245 
13246 	if (!(features & NETIF_F_GRO))
13247 		features &= ~NETIF_F_GRO_HW;
13248 
13249 	if (features & NETIF_F_GRO_HW)
13250 		features &= ~NETIF_F_LRO;
13251 
13252 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13253 	 * turned on or off together.
13254 	 */
13255 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13256 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13257 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13258 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13259 		else if (vlan_features)
13260 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13261 	}
13262 #ifdef CONFIG_BNXT_SRIOV
13263 	if (BNXT_VF(bp) && bp->vf.vlan)
13264 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13265 #endif
13266 	return features;
13267 }
13268 
13269 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13270 				bool link_re_init, u32 flags, bool update_tpa)
13271 {
13272 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13273 	bp->flags = flags;
13274 	if (update_tpa)
13275 		bnxt_set_ring_params(bp);
13276 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13277 }
13278 
13279 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13280 {
13281 	bool update_tpa = false, update_ntuple = false;
13282 	struct bnxt *bp = netdev_priv(dev);
13283 	u32 flags = bp->flags;
13284 	u32 changes;
13285 	int rc = 0;
13286 	bool re_init = false;
13287 
13288 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13289 	if (features & NETIF_F_GRO_HW)
13290 		flags |= BNXT_FLAG_GRO;
13291 	else if (features & NETIF_F_LRO)
13292 		flags |= BNXT_FLAG_LRO;
13293 
13294 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13295 		flags &= ~BNXT_FLAG_TPA;
13296 
13297 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13298 		flags |= BNXT_FLAG_STRIP_VLAN;
13299 
13300 	if (features & NETIF_F_NTUPLE)
13301 		flags |= BNXT_FLAG_RFS;
13302 	else
13303 		bnxt_clear_usr_fltrs(bp, true);
13304 
13305 	changes = flags ^ bp->flags;
13306 	if (changes & BNXT_FLAG_TPA) {
13307 		update_tpa = true;
13308 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13309 		    (flags & BNXT_FLAG_TPA) == 0 ||
13310 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13311 			re_init = true;
13312 	}
13313 
13314 	if (changes & ~BNXT_FLAG_TPA)
13315 		re_init = true;
13316 
13317 	if (changes & BNXT_FLAG_RFS)
13318 		update_ntuple = true;
13319 
13320 	if (flags != bp->flags) {
13321 		u32 old_flags = bp->flags;
13322 
13323 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13324 			bp->flags = flags;
13325 			if (update_tpa)
13326 				bnxt_set_ring_params(bp);
13327 			return rc;
13328 		}
13329 
13330 		if (update_ntuple)
13331 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13332 
13333 		if (re_init)
13334 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13335 
13336 		if (update_tpa) {
13337 			bp->flags = flags;
13338 			rc = bnxt_set_tpa(bp,
13339 					  (flags & BNXT_FLAG_TPA) ?
13340 					  true : false);
13341 			if (rc)
13342 				bp->flags = old_flags;
13343 		}
13344 	}
13345 	return rc;
13346 }
13347 
13348 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13349 			      u8 **nextp)
13350 {
13351 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13352 	struct hop_jumbo_hdr *jhdr;
13353 	int hdr_count = 0;
13354 	u8 *nexthdr;
13355 	int start;
13356 
13357 	/* Check that there are at most 2 IPv6 extension headers, no
13358 	 * fragment header, and each is <= 64 bytes.
13359 	 */
13360 	start = nw_off + sizeof(*ip6h);
13361 	nexthdr = &ip6h->nexthdr;
13362 	while (ipv6_ext_hdr(*nexthdr)) {
13363 		struct ipv6_opt_hdr *hp;
13364 		int hdrlen;
13365 
13366 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13367 		    *nexthdr == NEXTHDR_FRAGMENT)
13368 			return false;
13369 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13370 					  skb_headlen(skb), NULL);
13371 		if (!hp)
13372 			return false;
13373 		if (*nexthdr == NEXTHDR_AUTH)
13374 			hdrlen = ipv6_authlen(hp);
13375 		else
13376 			hdrlen = ipv6_optlen(hp);
13377 
13378 		if (hdrlen > 64)
13379 			return false;
13380 
13381 		/* The ext header may be a hop-by-hop header inserted for
13382 		 * big TCP purposes. This will be removed before sending
13383 		 * from NIC, so do not count it.
13384 		 */
13385 		if (*nexthdr == NEXTHDR_HOP) {
13386 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13387 				goto increment_hdr;
13388 
13389 			jhdr = (struct hop_jumbo_hdr *)hp;
13390 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13391 			    jhdr->nexthdr != IPPROTO_TCP)
13392 				goto increment_hdr;
13393 
13394 			goto next_hdr;
13395 		}
13396 increment_hdr:
13397 		hdr_count++;
13398 next_hdr:
13399 		nexthdr = &hp->nexthdr;
13400 		start += hdrlen;
13401 	}
13402 	if (nextp) {
13403 		/* Caller will check inner protocol */
13404 		if (skb->encapsulation) {
13405 			*nextp = nexthdr;
13406 			return true;
13407 		}
13408 		*nextp = NULL;
13409 	}
13410 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13411 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13412 }
13413 
13414 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13415 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13416 {
13417 	struct udphdr *uh = udp_hdr(skb);
13418 	__be16 udp_port = uh->dest;
13419 
13420 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13421 	    udp_port != bp->vxlan_gpe_port)
13422 		return false;
13423 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13424 		struct ethhdr *eh = inner_eth_hdr(skb);
13425 
13426 		switch (eh->h_proto) {
13427 		case htons(ETH_P_IP):
13428 			return true;
13429 		case htons(ETH_P_IPV6):
13430 			return bnxt_exthdr_check(bp, skb,
13431 						 skb_inner_network_offset(skb),
13432 						 NULL);
13433 		}
13434 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13435 		return true;
13436 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13437 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13438 					 NULL);
13439 	}
13440 	return false;
13441 }
13442 
13443 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13444 {
13445 	switch (l4_proto) {
13446 	case IPPROTO_UDP:
13447 		return bnxt_udp_tunl_check(bp, skb);
13448 	case IPPROTO_IPIP:
13449 		return true;
13450 	case IPPROTO_GRE: {
13451 		switch (skb->inner_protocol) {
13452 		default:
13453 			return false;
13454 		case htons(ETH_P_IP):
13455 			return true;
13456 		case htons(ETH_P_IPV6):
13457 			fallthrough;
13458 		}
13459 	}
13460 	case IPPROTO_IPV6:
13461 		/* Check ext headers of inner ipv6 */
13462 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13463 					 NULL);
13464 	}
13465 	return false;
13466 }
13467 
13468 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13469 					     struct net_device *dev,
13470 					     netdev_features_t features)
13471 {
13472 	struct bnxt *bp = netdev_priv(dev);
13473 	u8 *l4_proto;
13474 
13475 	features = vlan_features_check(skb, features);
13476 	switch (vlan_get_protocol(skb)) {
13477 	case htons(ETH_P_IP):
13478 		if (!skb->encapsulation)
13479 			return features;
13480 		l4_proto = &ip_hdr(skb)->protocol;
13481 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13482 			return features;
13483 		break;
13484 	case htons(ETH_P_IPV6):
13485 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13486 				       &l4_proto))
13487 			break;
13488 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13489 			return features;
13490 		break;
13491 	}
13492 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13493 }
13494 
13495 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13496 			 u32 *reg_buf)
13497 {
13498 	struct hwrm_dbg_read_direct_output *resp;
13499 	struct hwrm_dbg_read_direct_input *req;
13500 	__le32 *dbg_reg_buf;
13501 	dma_addr_t mapping;
13502 	int rc, i;
13503 
13504 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13505 	if (rc)
13506 		return rc;
13507 
13508 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13509 					 &mapping);
13510 	if (!dbg_reg_buf) {
13511 		rc = -ENOMEM;
13512 		goto dbg_rd_reg_exit;
13513 	}
13514 
13515 	req->host_dest_addr = cpu_to_le64(mapping);
13516 
13517 	resp = hwrm_req_hold(bp, req);
13518 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13519 	req->read_len32 = cpu_to_le32(num_words);
13520 
13521 	rc = hwrm_req_send(bp, req);
13522 	if (rc || resp->error_code) {
13523 		rc = -EIO;
13524 		goto dbg_rd_reg_exit;
13525 	}
13526 	for (i = 0; i < num_words; i++)
13527 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13528 
13529 dbg_rd_reg_exit:
13530 	hwrm_req_drop(bp, req);
13531 	return rc;
13532 }
13533 
13534 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13535 				       u32 ring_id, u32 *prod, u32 *cons)
13536 {
13537 	struct hwrm_dbg_ring_info_get_output *resp;
13538 	struct hwrm_dbg_ring_info_get_input *req;
13539 	int rc;
13540 
13541 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13542 	if (rc)
13543 		return rc;
13544 
13545 	req->ring_type = ring_type;
13546 	req->fw_ring_id = cpu_to_le32(ring_id);
13547 	resp = hwrm_req_hold(bp, req);
13548 	rc = hwrm_req_send(bp, req);
13549 	if (!rc) {
13550 		*prod = le32_to_cpu(resp->producer_index);
13551 		*cons = le32_to_cpu(resp->consumer_index);
13552 	}
13553 	hwrm_req_drop(bp, req);
13554 	return rc;
13555 }
13556 
13557 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13558 {
13559 	struct bnxt_tx_ring_info *txr;
13560 	int i = bnapi->index, j;
13561 
13562 	bnxt_for_each_napi_tx(j, bnapi, txr)
13563 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13564 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13565 			    txr->tx_cons);
13566 }
13567 
13568 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13569 {
13570 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13571 	int i = bnapi->index;
13572 
13573 	if (!rxr)
13574 		return;
13575 
13576 	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",
13577 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13578 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13579 		    rxr->rx_sw_agg_prod);
13580 }
13581 
13582 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13583 {
13584 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13585 	int i = bnapi->index;
13586 
13587 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13588 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13589 }
13590 
13591 static void bnxt_dbg_dump_states(struct bnxt *bp)
13592 {
13593 	int i;
13594 	struct bnxt_napi *bnapi;
13595 
13596 	for (i = 0; i < bp->cp_nr_rings; i++) {
13597 		bnapi = bp->bnapi[i];
13598 		if (netif_msg_drv(bp)) {
13599 			bnxt_dump_tx_sw_state(bnapi);
13600 			bnxt_dump_rx_sw_state(bnapi);
13601 			bnxt_dump_cp_sw_state(bnapi);
13602 		}
13603 	}
13604 }
13605 
13606 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13607 {
13608 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13609 	struct hwrm_ring_reset_input *req;
13610 	struct bnxt_napi *bnapi = rxr->bnapi;
13611 	struct bnxt_cp_ring_info *cpr;
13612 	u16 cp_ring_id;
13613 	int rc;
13614 
13615 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13616 	if (rc)
13617 		return rc;
13618 
13619 	cpr = &bnapi->cp_ring;
13620 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13621 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13622 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13623 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13624 	return hwrm_req_send_silent(bp, req);
13625 }
13626 
13627 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13628 {
13629 	if (!silent)
13630 		bnxt_dbg_dump_states(bp);
13631 	if (netif_running(bp->dev)) {
13632 		bnxt_close_nic(bp, !silent, false);
13633 		bnxt_open_nic(bp, !silent, false);
13634 	}
13635 }
13636 
13637 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13638 {
13639 	struct bnxt *bp = netdev_priv(dev);
13640 
13641 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13642 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13643 }
13644 
13645 static void bnxt_fw_health_check(struct bnxt *bp)
13646 {
13647 	struct bnxt_fw_health *fw_health = bp->fw_health;
13648 	struct pci_dev *pdev = bp->pdev;
13649 	u32 val;
13650 
13651 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13652 		return;
13653 
13654 	/* Make sure it is enabled before checking the tmr_counter. */
13655 	smp_rmb();
13656 	if (fw_health->tmr_counter) {
13657 		fw_health->tmr_counter--;
13658 		return;
13659 	}
13660 
13661 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13662 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13663 		fw_health->arrests++;
13664 		goto fw_reset;
13665 	}
13666 
13667 	fw_health->last_fw_heartbeat = val;
13668 
13669 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13670 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13671 		fw_health->discoveries++;
13672 		goto fw_reset;
13673 	}
13674 
13675 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13676 	return;
13677 
13678 fw_reset:
13679 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13680 }
13681 
13682 static void bnxt_timer(struct timer_list *t)
13683 {
13684 	struct bnxt *bp = from_timer(bp, t, timer);
13685 	struct net_device *dev = bp->dev;
13686 
13687 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13688 		return;
13689 
13690 	if (atomic_read(&bp->intr_sem) != 0)
13691 		goto bnxt_restart_timer;
13692 
13693 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13694 		bnxt_fw_health_check(bp);
13695 
13696 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13697 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13698 
13699 	if (bnxt_tc_flower_enabled(bp))
13700 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13701 
13702 #ifdef CONFIG_RFS_ACCEL
13703 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13704 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13705 #endif /*CONFIG_RFS_ACCEL*/
13706 
13707 	if (bp->link_info.phy_retry) {
13708 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13709 			bp->link_info.phy_retry = false;
13710 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13711 		} else {
13712 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13713 		}
13714 	}
13715 
13716 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13717 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13718 
13719 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13720 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13721 
13722 bnxt_restart_timer:
13723 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13724 }
13725 
13726 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13727 {
13728 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13729 	 * set.  If the device is being closed, bnxt_close() may be holding
13730 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13731 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13732 	 */
13733 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13734 	rtnl_lock();
13735 }
13736 
13737 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13738 {
13739 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13740 	rtnl_unlock();
13741 }
13742 
13743 /* Only called from bnxt_sp_task() */
13744 static void bnxt_reset(struct bnxt *bp, bool silent)
13745 {
13746 	bnxt_rtnl_lock_sp(bp);
13747 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13748 		bnxt_reset_task(bp, silent);
13749 	bnxt_rtnl_unlock_sp(bp);
13750 }
13751 
13752 /* Only called from bnxt_sp_task() */
13753 static void bnxt_rx_ring_reset(struct bnxt *bp)
13754 {
13755 	int i;
13756 
13757 	bnxt_rtnl_lock_sp(bp);
13758 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13759 		bnxt_rtnl_unlock_sp(bp);
13760 		return;
13761 	}
13762 	/* Disable and flush TPA before resetting the RX ring */
13763 	if (bp->flags & BNXT_FLAG_TPA)
13764 		bnxt_set_tpa(bp, false);
13765 	for (i = 0; i < bp->rx_nr_rings; i++) {
13766 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13767 		struct bnxt_cp_ring_info *cpr;
13768 		int rc;
13769 
13770 		if (!rxr->bnapi->in_reset)
13771 			continue;
13772 
13773 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13774 		if (rc) {
13775 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13776 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13777 			else
13778 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13779 					    rc);
13780 			bnxt_reset_task(bp, true);
13781 			break;
13782 		}
13783 		bnxt_free_one_rx_ring_skbs(bp, rxr);
13784 		rxr->rx_prod = 0;
13785 		rxr->rx_agg_prod = 0;
13786 		rxr->rx_sw_agg_prod = 0;
13787 		rxr->rx_next_cons = 0;
13788 		rxr->bnapi->in_reset = false;
13789 		bnxt_alloc_one_rx_ring(bp, i);
13790 		cpr = &rxr->bnapi->cp_ring;
13791 		cpr->sw_stats->rx.rx_resets++;
13792 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13793 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13794 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13795 	}
13796 	if (bp->flags & BNXT_FLAG_TPA)
13797 		bnxt_set_tpa(bp, true);
13798 	bnxt_rtnl_unlock_sp(bp);
13799 }
13800 
13801 static void bnxt_fw_fatal_close(struct bnxt *bp)
13802 {
13803 	bnxt_tx_disable(bp);
13804 	bnxt_disable_napi(bp);
13805 	bnxt_disable_int_sync(bp);
13806 	bnxt_free_irq(bp);
13807 	bnxt_clear_int_mode(bp);
13808 	pci_disable_device(bp->pdev);
13809 }
13810 
13811 static void bnxt_fw_reset_close(struct bnxt *bp)
13812 {
13813 	/* When firmware is in fatal state, quiesce device and disable
13814 	 * bus master to prevent any potential bad DMAs before freeing
13815 	 * kernel memory.
13816 	 */
13817 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13818 		u16 val = 0;
13819 
13820 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13821 		if (val == 0xffff)
13822 			bp->fw_reset_min_dsecs = 0;
13823 		bnxt_fw_fatal_close(bp);
13824 	}
13825 	__bnxt_close_nic(bp, true, false);
13826 	bnxt_vf_reps_free(bp);
13827 	bnxt_clear_int_mode(bp);
13828 	bnxt_hwrm_func_drv_unrgtr(bp);
13829 	if (pci_is_enabled(bp->pdev))
13830 		pci_disable_device(bp->pdev);
13831 	bnxt_free_ctx_mem(bp, false);
13832 }
13833 
13834 static bool is_bnxt_fw_ok(struct bnxt *bp)
13835 {
13836 	struct bnxt_fw_health *fw_health = bp->fw_health;
13837 	bool no_heartbeat = false, has_reset = false;
13838 	u32 val;
13839 
13840 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13841 	if (val == fw_health->last_fw_heartbeat)
13842 		no_heartbeat = true;
13843 
13844 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13845 	if (val != fw_health->last_fw_reset_cnt)
13846 		has_reset = true;
13847 
13848 	if (!no_heartbeat && has_reset)
13849 		return true;
13850 
13851 	return false;
13852 }
13853 
13854 /* rtnl_lock is acquired before calling this function */
13855 static void bnxt_force_fw_reset(struct bnxt *bp)
13856 {
13857 	struct bnxt_fw_health *fw_health = bp->fw_health;
13858 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13859 	u32 wait_dsecs;
13860 
13861 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13862 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13863 		return;
13864 
13865 	/* we have to serialize with bnxt_refclk_read()*/
13866 	if (ptp) {
13867 		unsigned long flags;
13868 
13869 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13870 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13871 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13872 	} else {
13873 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13874 	}
13875 	bnxt_fw_reset_close(bp);
13876 	wait_dsecs = fw_health->master_func_wait_dsecs;
13877 	if (fw_health->primary) {
13878 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13879 			wait_dsecs = 0;
13880 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13881 	} else {
13882 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13883 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13884 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13885 	}
13886 
13887 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13888 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13889 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13890 }
13891 
13892 void bnxt_fw_exception(struct bnxt *bp)
13893 {
13894 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13895 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13896 	bnxt_ulp_stop(bp);
13897 	bnxt_rtnl_lock_sp(bp);
13898 	bnxt_force_fw_reset(bp);
13899 	bnxt_rtnl_unlock_sp(bp);
13900 }
13901 
13902 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13903  * < 0 on error.
13904  */
13905 static int bnxt_get_registered_vfs(struct bnxt *bp)
13906 {
13907 #ifdef CONFIG_BNXT_SRIOV
13908 	int rc;
13909 
13910 	if (!BNXT_PF(bp))
13911 		return 0;
13912 
13913 	rc = bnxt_hwrm_func_qcfg(bp);
13914 	if (rc) {
13915 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13916 		return rc;
13917 	}
13918 	if (bp->pf.registered_vfs)
13919 		return bp->pf.registered_vfs;
13920 	if (bp->sriov_cfg)
13921 		return 1;
13922 #endif
13923 	return 0;
13924 }
13925 
13926 void bnxt_fw_reset(struct bnxt *bp)
13927 {
13928 	bnxt_ulp_stop(bp);
13929 	bnxt_rtnl_lock_sp(bp);
13930 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13931 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13932 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13933 		int n = 0, tmo;
13934 
13935 		/* we have to serialize with bnxt_refclk_read()*/
13936 		if (ptp) {
13937 			unsigned long flags;
13938 
13939 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13940 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13941 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13942 		} else {
13943 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13944 		}
13945 		if (bp->pf.active_vfs &&
13946 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13947 			n = bnxt_get_registered_vfs(bp);
13948 		if (n < 0) {
13949 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13950 				   n);
13951 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13952 			dev_close(bp->dev);
13953 			goto fw_reset_exit;
13954 		} else if (n > 0) {
13955 			u16 vf_tmo_dsecs = n * 10;
13956 
13957 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13958 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13959 			bp->fw_reset_state =
13960 				BNXT_FW_RESET_STATE_POLL_VF;
13961 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13962 			goto fw_reset_exit;
13963 		}
13964 		bnxt_fw_reset_close(bp);
13965 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13966 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13967 			tmo = HZ / 10;
13968 		} else {
13969 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13970 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13971 		}
13972 		bnxt_queue_fw_reset_work(bp, tmo);
13973 	}
13974 fw_reset_exit:
13975 	bnxt_rtnl_unlock_sp(bp);
13976 }
13977 
13978 static void bnxt_chk_missed_irq(struct bnxt *bp)
13979 {
13980 	int i;
13981 
13982 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13983 		return;
13984 
13985 	for (i = 0; i < bp->cp_nr_rings; i++) {
13986 		struct bnxt_napi *bnapi = bp->bnapi[i];
13987 		struct bnxt_cp_ring_info *cpr;
13988 		u32 fw_ring_id;
13989 		int j;
13990 
13991 		if (!bnapi)
13992 			continue;
13993 
13994 		cpr = &bnapi->cp_ring;
13995 		for (j = 0; j < cpr->cp_ring_count; j++) {
13996 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13997 			u32 val[2];
13998 
13999 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
14000 				continue;
14001 
14002 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
14003 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
14004 				continue;
14005 			}
14006 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
14007 			bnxt_dbg_hwrm_ring_info_get(bp,
14008 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
14009 				fw_ring_id, &val[0], &val[1]);
14010 			cpr->sw_stats->cmn.missed_irqs++;
14011 		}
14012 	}
14013 }
14014 
14015 static void bnxt_cfg_ntp_filters(struct bnxt *);
14016 
14017 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
14018 {
14019 	struct bnxt_link_info *link_info = &bp->link_info;
14020 
14021 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
14022 		link_info->autoneg = BNXT_AUTONEG_SPEED;
14023 		if (bp->hwrm_spec_code >= 0x10201) {
14024 			if (link_info->auto_pause_setting &
14025 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
14026 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14027 		} else {
14028 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
14029 		}
14030 		bnxt_set_auto_speed(link_info);
14031 	} else {
14032 		bnxt_set_force_speed(link_info);
14033 		link_info->req_duplex = link_info->duplex_setting;
14034 	}
14035 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14036 		link_info->req_flow_ctrl =
14037 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14038 	else
14039 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14040 }
14041 
14042 static void bnxt_fw_echo_reply(struct bnxt *bp)
14043 {
14044 	struct bnxt_fw_health *fw_health = bp->fw_health;
14045 	struct hwrm_func_echo_response_input *req;
14046 	int rc;
14047 
14048 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14049 	if (rc)
14050 		return;
14051 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14052 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14053 	hwrm_req_send(bp, req);
14054 }
14055 
14056 static void bnxt_ulp_restart(struct bnxt *bp)
14057 {
14058 	bnxt_ulp_stop(bp);
14059 	bnxt_ulp_start(bp, 0);
14060 }
14061 
14062 static void bnxt_sp_task(struct work_struct *work)
14063 {
14064 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14065 
14066 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14067 	smp_mb__after_atomic();
14068 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14069 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14070 		return;
14071 	}
14072 
14073 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14074 		bnxt_ulp_restart(bp);
14075 		bnxt_reenable_sriov(bp);
14076 	}
14077 
14078 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14079 		bnxt_cfg_rx_mode(bp);
14080 
14081 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14082 		bnxt_cfg_ntp_filters(bp);
14083 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14084 		bnxt_hwrm_exec_fwd_req(bp);
14085 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14086 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14087 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14088 		bnxt_hwrm_port_qstats(bp, 0);
14089 		bnxt_hwrm_port_qstats_ext(bp, 0);
14090 		bnxt_accumulate_all_stats(bp);
14091 	}
14092 
14093 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14094 		int rc;
14095 
14096 		mutex_lock(&bp->link_lock);
14097 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14098 				       &bp->sp_event))
14099 			bnxt_hwrm_phy_qcaps(bp);
14100 
14101 		rc = bnxt_update_link(bp, true);
14102 		if (rc)
14103 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14104 				   rc);
14105 
14106 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14107 				       &bp->sp_event))
14108 			bnxt_init_ethtool_link_settings(bp);
14109 		mutex_unlock(&bp->link_lock);
14110 	}
14111 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14112 		int rc;
14113 
14114 		mutex_lock(&bp->link_lock);
14115 		rc = bnxt_update_phy_setting(bp);
14116 		mutex_unlock(&bp->link_lock);
14117 		if (rc) {
14118 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14119 		} else {
14120 			bp->link_info.phy_retry = false;
14121 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14122 		}
14123 	}
14124 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14125 		mutex_lock(&bp->link_lock);
14126 		bnxt_get_port_module_status(bp);
14127 		mutex_unlock(&bp->link_lock);
14128 	}
14129 
14130 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14131 		bnxt_tc_flow_stats_work(bp);
14132 
14133 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14134 		bnxt_chk_missed_irq(bp);
14135 
14136 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14137 		bnxt_fw_echo_reply(bp);
14138 
14139 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14140 		bnxt_hwmon_notify_event(bp);
14141 
14142 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14143 	 * must be the last functions to be called before exiting.
14144 	 */
14145 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14146 		bnxt_reset(bp, false);
14147 
14148 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14149 		bnxt_reset(bp, true);
14150 
14151 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14152 		bnxt_rx_ring_reset(bp);
14153 
14154 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14155 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14156 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14157 			bnxt_devlink_health_fw_report(bp);
14158 		else
14159 			bnxt_fw_reset(bp);
14160 	}
14161 
14162 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14163 		if (!is_bnxt_fw_ok(bp))
14164 			bnxt_devlink_health_fw_report(bp);
14165 	}
14166 
14167 	smp_mb__before_atomic();
14168 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14169 }
14170 
14171 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14172 				int *max_cp);
14173 
14174 /* Under rtnl_lock */
14175 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14176 		     int tx_xdp)
14177 {
14178 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14179 	struct bnxt_hw_rings hwr = {0};
14180 	int rx_rings = rx;
14181 	int rc;
14182 
14183 	if (tcs)
14184 		tx_sets = tcs;
14185 
14186 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14187 
14188 	if (max_rx < rx_rings)
14189 		return -ENOMEM;
14190 
14191 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14192 		rx_rings <<= 1;
14193 
14194 	hwr.rx = rx_rings;
14195 	hwr.tx = tx * tx_sets + tx_xdp;
14196 	if (max_tx < hwr.tx)
14197 		return -ENOMEM;
14198 
14199 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14200 
14201 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14202 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14203 	if (max_cp < hwr.cp)
14204 		return -ENOMEM;
14205 	hwr.stat = hwr.cp;
14206 	if (BNXT_NEW_RM(bp)) {
14207 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14208 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14209 		hwr.grp = rx;
14210 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14211 	}
14212 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14213 		hwr.cp_p5 = hwr.tx + rx;
14214 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14215 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14216 		if (!bnxt_ulp_registered(bp->edev)) {
14217 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14218 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14219 		}
14220 		if (hwr.cp > bp->total_irqs) {
14221 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14222 
14223 			if (total_msix < hwr.cp) {
14224 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14225 					    hwr.cp, total_msix);
14226 				rc = -ENOSPC;
14227 			}
14228 		}
14229 	}
14230 	return rc;
14231 }
14232 
14233 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14234 {
14235 	if (bp->bar2) {
14236 		pci_iounmap(pdev, bp->bar2);
14237 		bp->bar2 = NULL;
14238 	}
14239 
14240 	if (bp->bar1) {
14241 		pci_iounmap(pdev, bp->bar1);
14242 		bp->bar1 = NULL;
14243 	}
14244 
14245 	if (bp->bar0) {
14246 		pci_iounmap(pdev, bp->bar0);
14247 		bp->bar0 = NULL;
14248 	}
14249 }
14250 
14251 static void bnxt_cleanup_pci(struct bnxt *bp)
14252 {
14253 	bnxt_unmap_bars(bp, bp->pdev);
14254 	pci_release_regions(bp->pdev);
14255 	if (pci_is_enabled(bp->pdev))
14256 		pci_disable_device(bp->pdev);
14257 }
14258 
14259 static void bnxt_init_dflt_coal(struct bnxt *bp)
14260 {
14261 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14262 	struct bnxt_coal *coal;
14263 	u16 flags = 0;
14264 
14265 	if (coal_cap->cmpl_params &
14266 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14267 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14268 
14269 	/* Tick values in micro seconds.
14270 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14271 	 */
14272 	coal = &bp->rx_coal;
14273 	coal->coal_ticks = 10;
14274 	coal->coal_bufs = 30;
14275 	coal->coal_ticks_irq = 1;
14276 	coal->coal_bufs_irq = 2;
14277 	coal->idle_thresh = 50;
14278 	coal->bufs_per_record = 2;
14279 	coal->budget = 64;		/* NAPI budget */
14280 	coal->flags = flags;
14281 
14282 	coal = &bp->tx_coal;
14283 	coal->coal_ticks = 28;
14284 	coal->coal_bufs = 30;
14285 	coal->coal_ticks_irq = 2;
14286 	coal->coal_bufs_irq = 2;
14287 	coal->bufs_per_record = 1;
14288 	coal->flags = flags;
14289 
14290 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14291 }
14292 
14293 /* FW that pre-reserves 1 VNIC per function */
14294 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14295 {
14296 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14297 
14298 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14299 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14300 		return true;
14301 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14302 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14303 		return true;
14304 	return false;
14305 }
14306 
14307 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14308 {
14309 	int rc;
14310 
14311 	bp->fw_cap = 0;
14312 	rc = bnxt_hwrm_ver_get(bp);
14313 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14314 	 * so wait before continuing with recovery.
14315 	 */
14316 	if (rc)
14317 		msleep(100);
14318 	bnxt_try_map_fw_health_reg(bp);
14319 	if (rc) {
14320 		rc = bnxt_try_recover_fw(bp);
14321 		if (rc)
14322 			return rc;
14323 		rc = bnxt_hwrm_ver_get(bp);
14324 		if (rc)
14325 			return rc;
14326 	}
14327 
14328 	bnxt_nvm_cfg_ver_get(bp);
14329 
14330 	rc = bnxt_hwrm_func_reset(bp);
14331 	if (rc)
14332 		return -ENODEV;
14333 
14334 	bnxt_hwrm_fw_set_time(bp);
14335 	return 0;
14336 }
14337 
14338 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14339 {
14340 	int rc;
14341 
14342 	/* Get the MAX capabilities for this function */
14343 	rc = bnxt_hwrm_func_qcaps(bp);
14344 	if (rc) {
14345 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14346 			   rc);
14347 		return -ENODEV;
14348 	}
14349 
14350 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14351 	if (rc)
14352 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14353 			    rc);
14354 
14355 	if (bnxt_alloc_fw_health(bp)) {
14356 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14357 	} else {
14358 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14359 		if (rc)
14360 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14361 				    rc);
14362 	}
14363 
14364 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14365 	if (rc)
14366 		return -ENODEV;
14367 
14368 	rc = bnxt_alloc_crash_dump_mem(bp);
14369 	if (rc)
14370 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14371 			    rc);
14372 	if (!rc) {
14373 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14374 		if (rc) {
14375 			bnxt_free_crash_dump_mem(bp);
14376 			netdev_warn(bp->dev,
14377 				    "hwrm crash dump mem failure rc: %d\n", rc);
14378 		}
14379 	}
14380 
14381 	if (bnxt_fw_pre_resv_vnics(bp))
14382 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14383 
14384 	bnxt_hwrm_func_qcfg(bp);
14385 	bnxt_hwrm_vnic_qcaps(bp);
14386 	bnxt_hwrm_port_led_qcaps(bp);
14387 	bnxt_ethtool_init(bp);
14388 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14389 		__bnxt_hwrm_ptp_qcfg(bp);
14390 	bnxt_dcb_init(bp);
14391 	bnxt_hwmon_init(bp);
14392 	return 0;
14393 }
14394 
14395 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14396 {
14397 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14398 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14399 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14400 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14401 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14402 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14403 		bp->rss_hash_delta = bp->rss_hash_cfg;
14404 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14405 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14406 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14407 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14408 	}
14409 }
14410 
14411 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14412 {
14413 	struct net_device *dev = bp->dev;
14414 
14415 	dev->hw_features &= ~NETIF_F_NTUPLE;
14416 	dev->features &= ~NETIF_F_NTUPLE;
14417 	bp->flags &= ~BNXT_FLAG_RFS;
14418 	if (bnxt_rfs_supported(bp)) {
14419 		dev->hw_features |= NETIF_F_NTUPLE;
14420 		if (bnxt_rfs_capable(bp, false)) {
14421 			bp->flags |= BNXT_FLAG_RFS;
14422 			dev->features |= NETIF_F_NTUPLE;
14423 		}
14424 	}
14425 }
14426 
14427 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14428 {
14429 	struct pci_dev *pdev = bp->pdev;
14430 
14431 	bnxt_set_dflt_rss_hash_type(bp);
14432 	bnxt_set_dflt_rfs(bp);
14433 
14434 	bnxt_get_wol_settings(bp);
14435 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14436 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14437 	else
14438 		device_set_wakeup_capable(&pdev->dev, false);
14439 
14440 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14441 	bnxt_hwrm_coal_params_qcaps(bp);
14442 }
14443 
14444 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14445 
14446 int bnxt_fw_init_one(struct bnxt *bp)
14447 {
14448 	int rc;
14449 
14450 	rc = bnxt_fw_init_one_p1(bp);
14451 	if (rc) {
14452 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14453 		return rc;
14454 	}
14455 	rc = bnxt_fw_init_one_p2(bp);
14456 	if (rc) {
14457 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14458 		return rc;
14459 	}
14460 	rc = bnxt_probe_phy(bp, false);
14461 	if (rc)
14462 		return rc;
14463 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14464 	if (rc)
14465 		return rc;
14466 
14467 	bnxt_fw_init_one_p3(bp);
14468 	return 0;
14469 }
14470 
14471 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14472 {
14473 	struct bnxt_fw_health *fw_health = bp->fw_health;
14474 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14475 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14476 	u32 reg_type, reg_off, delay_msecs;
14477 
14478 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14479 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14480 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14481 	switch (reg_type) {
14482 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14483 		pci_write_config_dword(bp->pdev, reg_off, val);
14484 		break;
14485 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14486 		writel(reg_off & BNXT_GRC_BASE_MASK,
14487 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14488 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14489 		fallthrough;
14490 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14491 		writel(val, bp->bar0 + reg_off);
14492 		break;
14493 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14494 		writel(val, bp->bar1 + reg_off);
14495 		break;
14496 	}
14497 	if (delay_msecs) {
14498 		pci_read_config_dword(bp->pdev, 0, &val);
14499 		msleep(delay_msecs);
14500 	}
14501 }
14502 
14503 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14504 {
14505 	struct hwrm_func_qcfg_output *resp;
14506 	struct hwrm_func_qcfg_input *req;
14507 	bool result = true; /* firmware will enforce if unknown */
14508 
14509 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14510 		return result;
14511 
14512 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14513 		return result;
14514 
14515 	req->fid = cpu_to_le16(0xffff);
14516 	resp = hwrm_req_hold(bp, req);
14517 	if (!hwrm_req_send(bp, req))
14518 		result = !!(le16_to_cpu(resp->flags) &
14519 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14520 	hwrm_req_drop(bp, req);
14521 	return result;
14522 }
14523 
14524 static void bnxt_reset_all(struct bnxt *bp)
14525 {
14526 	struct bnxt_fw_health *fw_health = bp->fw_health;
14527 	int i, rc;
14528 
14529 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14530 		bnxt_fw_reset_via_optee(bp);
14531 		bp->fw_reset_timestamp = jiffies;
14532 		return;
14533 	}
14534 
14535 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14536 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14537 			bnxt_fw_reset_writel(bp, i);
14538 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14539 		struct hwrm_fw_reset_input *req;
14540 
14541 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14542 		if (!rc) {
14543 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14544 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14545 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14546 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14547 			rc = hwrm_req_send(bp, req);
14548 		}
14549 		if (rc != -ENODEV)
14550 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14551 	}
14552 	bp->fw_reset_timestamp = jiffies;
14553 }
14554 
14555 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14556 {
14557 	return time_after(jiffies, bp->fw_reset_timestamp +
14558 			  (bp->fw_reset_max_dsecs * HZ / 10));
14559 }
14560 
14561 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14562 {
14563 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14564 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14565 		bnxt_dl_health_fw_status_update(bp, false);
14566 	bp->fw_reset_state = 0;
14567 	dev_close(bp->dev);
14568 }
14569 
14570 static void bnxt_fw_reset_task(struct work_struct *work)
14571 {
14572 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14573 	int rc = 0;
14574 
14575 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14576 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14577 		return;
14578 	}
14579 
14580 	switch (bp->fw_reset_state) {
14581 	case BNXT_FW_RESET_STATE_POLL_VF: {
14582 		int n = bnxt_get_registered_vfs(bp);
14583 		int tmo;
14584 
14585 		if (n < 0) {
14586 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14587 				   n, jiffies_to_msecs(jiffies -
14588 				   bp->fw_reset_timestamp));
14589 			goto fw_reset_abort;
14590 		} else if (n > 0) {
14591 			if (bnxt_fw_reset_timeout(bp)) {
14592 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14593 				bp->fw_reset_state = 0;
14594 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14595 					   n);
14596 				goto ulp_start;
14597 			}
14598 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14599 			return;
14600 		}
14601 		bp->fw_reset_timestamp = jiffies;
14602 		rtnl_lock();
14603 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14604 			bnxt_fw_reset_abort(bp, rc);
14605 			rtnl_unlock();
14606 			goto ulp_start;
14607 		}
14608 		bnxt_fw_reset_close(bp);
14609 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14610 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14611 			tmo = HZ / 10;
14612 		} else {
14613 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14614 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14615 		}
14616 		rtnl_unlock();
14617 		bnxt_queue_fw_reset_work(bp, tmo);
14618 		return;
14619 	}
14620 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14621 		u32 val;
14622 
14623 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14624 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14625 		    !bnxt_fw_reset_timeout(bp)) {
14626 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14627 			return;
14628 		}
14629 
14630 		if (!bp->fw_health->primary) {
14631 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14632 
14633 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14634 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14635 			return;
14636 		}
14637 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14638 	}
14639 		fallthrough;
14640 	case BNXT_FW_RESET_STATE_RESET_FW:
14641 		bnxt_reset_all(bp);
14642 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14643 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14644 		return;
14645 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14646 		bnxt_inv_fw_health_reg(bp);
14647 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14648 		    !bp->fw_reset_min_dsecs) {
14649 			u16 val;
14650 
14651 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14652 			if (val == 0xffff) {
14653 				if (bnxt_fw_reset_timeout(bp)) {
14654 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14655 					rc = -ETIMEDOUT;
14656 					goto fw_reset_abort;
14657 				}
14658 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14659 				return;
14660 			}
14661 		}
14662 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14663 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14664 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14665 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14666 			bnxt_dl_remote_reload(bp);
14667 		if (pci_enable_device(bp->pdev)) {
14668 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14669 			rc = -ENODEV;
14670 			goto fw_reset_abort;
14671 		}
14672 		pci_set_master(bp->pdev);
14673 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14674 		fallthrough;
14675 	case BNXT_FW_RESET_STATE_POLL_FW:
14676 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14677 		rc = bnxt_hwrm_poll(bp);
14678 		if (rc) {
14679 			if (bnxt_fw_reset_timeout(bp)) {
14680 				netdev_err(bp->dev, "Firmware reset aborted\n");
14681 				goto fw_reset_abort_status;
14682 			}
14683 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14684 			return;
14685 		}
14686 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14687 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14688 		fallthrough;
14689 	case BNXT_FW_RESET_STATE_OPENING:
14690 		while (!rtnl_trylock()) {
14691 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14692 			return;
14693 		}
14694 		rc = bnxt_open(bp->dev);
14695 		if (rc) {
14696 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14697 			bnxt_fw_reset_abort(bp, rc);
14698 			rtnl_unlock();
14699 			goto ulp_start;
14700 		}
14701 
14702 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14703 		    bp->fw_health->enabled) {
14704 			bp->fw_health->last_fw_reset_cnt =
14705 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14706 		}
14707 		bp->fw_reset_state = 0;
14708 		/* Make sure fw_reset_state is 0 before clearing the flag */
14709 		smp_mb__before_atomic();
14710 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14711 		bnxt_ptp_reapply_pps(bp);
14712 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14713 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14714 			bnxt_dl_health_fw_recovery_done(bp);
14715 			bnxt_dl_health_fw_status_update(bp, true);
14716 		}
14717 		rtnl_unlock();
14718 		bnxt_ulp_start(bp, 0);
14719 		bnxt_reenable_sriov(bp);
14720 		rtnl_lock();
14721 		bnxt_vf_reps_alloc(bp);
14722 		bnxt_vf_reps_open(bp);
14723 		rtnl_unlock();
14724 		break;
14725 	}
14726 	return;
14727 
14728 fw_reset_abort_status:
14729 	if (bp->fw_health->status_reliable ||
14730 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14731 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14732 
14733 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14734 	}
14735 fw_reset_abort:
14736 	rtnl_lock();
14737 	bnxt_fw_reset_abort(bp, rc);
14738 	rtnl_unlock();
14739 ulp_start:
14740 	bnxt_ulp_start(bp, rc);
14741 }
14742 
14743 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14744 {
14745 	int rc;
14746 	struct bnxt *bp = netdev_priv(dev);
14747 
14748 	SET_NETDEV_DEV(dev, &pdev->dev);
14749 
14750 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14751 	rc = pci_enable_device(pdev);
14752 	if (rc) {
14753 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14754 		goto init_err;
14755 	}
14756 
14757 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14758 		dev_err(&pdev->dev,
14759 			"Cannot find PCI device base address, aborting\n");
14760 		rc = -ENODEV;
14761 		goto init_err_disable;
14762 	}
14763 
14764 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14765 	if (rc) {
14766 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14767 		goto init_err_disable;
14768 	}
14769 
14770 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14771 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14772 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14773 		rc = -EIO;
14774 		goto init_err_release;
14775 	}
14776 
14777 	pci_set_master(pdev);
14778 
14779 	bp->dev = dev;
14780 	bp->pdev = pdev;
14781 
14782 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14783 	 * determines the BAR size.
14784 	 */
14785 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14786 	if (!bp->bar0) {
14787 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14788 		rc = -ENOMEM;
14789 		goto init_err_release;
14790 	}
14791 
14792 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14793 	if (!bp->bar2) {
14794 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14795 		rc = -ENOMEM;
14796 		goto init_err_release;
14797 	}
14798 
14799 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14800 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14801 
14802 	spin_lock_init(&bp->ntp_fltr_lock);
14803 #if BITS_PER_LONG == 32
14804 	spin_lock_init(&bp->db_lock);
14805 #endif
14806 
14807 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14808 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14809 
14810 	timer_setup(&bp->timer, bnxt_timer, 0);
14811 	bp->current_interval = BNXT_TIMER_INTERVAL;
14812 
14813 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14814 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14815 
14816 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14817 	return 0;
14818 
14819 init_err_release:
14820 	bnxt_unmap_bars(bp, pdev);
14821 	pci_release_regions(pdev);
14822 
14823 init_err_disable:
14824 	pci_disable_device(pdev);
14825 
14826 init_err:
14827 	return rc;
14828 }
14829 
14830 /* rtnl_lock held */
14831 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14832 {
14833 	struct sockaddr *addr = p;
14834 	struct bnxt *bp = netdev_priv(dev);
14835 	int rc = 0;
14836 
14837 	if (!is_valid_ether_addr(addr->sa_data))
14838 		return -EADDRNOTAVAIL;
14839 
14840 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14841 		return 0;
14842 
14843 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14844 	if (rc)
14845 		return rc;
14846 
14847 	eth_hw_addr_set(dev, addr->sa_data);
14848 	bnxt_clear_usr_fltrs(bp, true);
14849 	if (netif_running(dev)) {
14850 		bnxt_close_nic(bp, false, false);
14851 		rc = bnxt_open_nic(bp, false, false);
14852 	}
14853 
14854 	return rc;
14855 }
14856 
14857 /* rtnl_lock held */
14858 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14859 {
14860 	struct bnxt *bp = netdev_priv(dev);
14861 
14862 	if (netif_running(dev))
14863 		bnxt_close_nic(bp, true, false);
14864 
14865 	WRITE_ONCE(dev->mtu, new_mtu);
14866 
14867 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
14868 	 * program is attached.  We need to set the AGG rings settings and
14869 	 * rx_skb_func accordingly.
14870 	 */
14871 	if (READ_ONCE(bp->xdp_prog))
14872 		bnxt_set_rx_skb_mode(bp, true);
14873 
14874 	bnxt_set_ring_params(bp);
14875 
14876 	if (netif_running(dev))
14877 		return bnxt_open_nic(bp, true, false);
14878 
14879 	return 0;
14880 }
14881 
14882 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14883 {
14884 	struct bnxt *bp = netdev_priv(dev);
14885 	bool sh = false;
14886 	int rc, tx_cp;
14887 
14888 	if (tc > bp->max_tc) {
14889 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14890 			   tc, bp->max_tc);
14891 		return -EINVAL;
14892 	}
14893 
14894 	if (bp->num_tc == tc)
14895 		return 0;
14896 
14897 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14898 		sh = true;
14899 
14900 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14901 			      sh, tc, bp->tx_nr_rings_xdp);
14902 	if (rc)
14903 		return rc;
14904 
14905 	/* Needs to close the device and do hw resource re-allocations */
14906 	if (netif_running(bp->dev))
14907 		bnxt_close_nic(bp, true, false);
14908 
14909 	if (tc) {
14910 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14911 		netdev_set_num_tc(dev, tc);
14912 		bp->num_tc = tc;
14913 	} else {
14914 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14915 		netdev_reset_tc(dev);
14916 		bp->num_tc = 0;
14917 	}
14918 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14919 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14920 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14921 			       tx_cp + bp->rx_nr_rings;
14922 
14923 	if (netif_running(bp->dev))
14924 		return bnxt_open_nic(bp, true, false);
14925 
14926 	return 0;
14927 }
14928 
14929 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14930 				  void *cb_priv)
14931 {
14932 	struct bnxt *bp = cb_priv;
14933 
14934 	if (!bnxt_tc_flower_enabled(bp) ||
14935 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14936 		return -EOPNOTSUPP;
14937 
14938 	switch (type) {
14939 	case TC_SETUP_CLSFLOWER:
14940 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14941 	default:
14942 		return -EOPNOTSUPP;
14943 	}
14944 }
14945 
14946 LIST_HEAD(bnxt_block_cb_list);
14947 
14948 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14949 			 void *type_data)
14950 {
14951 	struct bnxt *bp = netdev_priv(dev);
14952 
14953 	switch (type) {
14954 	case TC_SETUP_BLOCK:
14955 		return flow_block_cb_setup_simple(type_data,
14956 						  &bnxt_block_cb_list,
14957 						  bnxt_setup_tc_block_cb,
14958 						  bp, bp, true);
14959 	case TC_SETUP_QDISC_MQPRIO: {
14960 		struct tc_mqprio_qopt *mqprio = type_data;
14961 
14962 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14963 
14964 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14965 	}
14966 	default:
14967 		return -EOPNOTSUPP;
14968 	}
14969 }
14970 
14971 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14972 			    const struct sk_buff *skb)
14973 {
14974 	struct bnxt_vnic_info *vnic;
14975 
14976 	if (skb)
14977 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14978 
14979 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14980 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14981 }
14982 
14983 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14984 			   u32 idx)
14985 {
14986 	struct hlist_head *head;
14987 	int bit_id;
14988 
14989 	spin_lock_bh(&bp->ntp_fltr_lock);
14990 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14991 	if (bit_id < 0) {
14992 		spin_unlock_bh(&bp->ntp_fltr_lock);
14993 		return -ENOMEM;
14994 	}
14995 
14996 	fltr->base.sw_id = (u16)bit_id;
14997 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14998 	fltr->base.flags |= BNXT_ACT_RING_DST;
14999 	head = &bp->ntp_fltr_hash_tbl[idx];
15000 	hlist_add_head_rcu(&fltr->base.hash, head);
15001 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
15002 	bnxt_insert_usr_fltr(bp, &fltr->base);
15003 	bp->ntp_fltr_count++;
15004 	spin_unlock_bh(&bp->ntp_fltr_lock);
15005 	return 0;
15006 }
15007 
15008 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
15009 			    struct bnxt_ntuple_filter *f2)
15010 {
15011 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
15012 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
15013 	struct flow_keys *keys1 = &f1->fkeys;
15014 	struct flow_keys *keys2 = &f2->fkeys;
15015 
15016 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
15017 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
15018 		return false;
15019 
15020 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
15021 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
15022 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
15023 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
15024 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
15025 			return false;
15026 	} else {
15027 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
15028 				     &keys2->addrs.v6addrs.src) ||
15029 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
15030 				     &masks2->addrs.v6addrs.src) ||
15031 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15032 				     &keys2->addrs.v6addrs.dst) ||
15033 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15034 				     &masks2->addrs.v6addrs.dst))
15035 			return false;
15036 	}
15037 
15038 	return keys1->ports.src == keys2->ports.src &&
15039 	       masks1->ports.src == masks2->ports.src &&
15040 	       keys1->ports.dst == keys2->ports.dst &&
15041 	       masks1->ports.dst == masks2->ports.dst &&
15042 	       keys1->control.flags == keys2->control.flags &&
15043 	       f1->l2_fltr == f2->l2_fltr;
15044 }
15045 
15046 struct bnxt_ntuple_filter *
15047 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15048 				struct bnxt_ntuple_filter *fltr, u32 idx)
15049 {
15050 	struct bnxt_ntuple_filter *f;
15051 	struct hlist_head *head;
15052 
15053 	head = &bp->ntp_fltr_hash_tbl[idx];
15054 	hlist_for_each_entry_rcu(f, head, base.hash) {
15055 		if (bnxt_fltr_match(f, fltr))
15056 			return f;
15057 	}
15058 	return NULL;
15059 }
15060 
15061 #ifdef CONFIG_RFS_ACCEL
15062 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15063 			      u16 rxq_index, u32 flow_id)
15064 {
15065 	struct bnxt *bp = netdev_priv(dev);
15066 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15067 	struct flow_keys *fkeys;
15068 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15069 	struct bnxt_l2_filter *l2_fltr;
15070 	int rc = 0, idx;
15071 	u32 flags;
15072 
15073 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15074 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15075 		atomic_inc(&l2_fltr->refcnt);
15076 	} else {
15077 		struct bnxt_l2_key key;
15078 
15079 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15080 		key.vlan = 0;
15081 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15082 		if (!l2_fltr)
15083 			return -EINVAL;
15084 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15085 			bnxt_del_l2_filter(bp, l2_fltr);
15086 			return -EINVAL;
15087 		}
15088 	}
15089 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15090 	if (!new_fltr) {
15091 		bnxt_del_l2_filter(bp, l2_fltr);
15092 		return -ENOMEM;
15093 	}
15094 
15095 	fkeys = &new_fltr->fkeys;
15096 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15097 		rc = -EPROTONOSUPPORT;
15098 		goto err_free;
15099 	}
15100 
15101 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15102 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15103 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15104 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15105 		rc = -EPROTONOSUPPORT;
15106 		goto err_free;
15107 	}
15108 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15109 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15110 		if (bp->hwrm_spec_code < 0x10601) {
15111 			rc = -EPROTONOSUPPORT;
15112 			goto err_free;
15113 		}
15114 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15115 	}
15116 	flags = fkeys->control.flags;
15117 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15118 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15119 		rc = -EPROTONOSUPPORT;
15120 		goto err_free;
15121 	}
15122 	new_fltr->l2_fltr = l2_fltr;
15123 
15124 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15125 	rcu_read_lock();
15126 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15127 	if (fltr) {
15128 		rc = fltr->base.sw_id;
15129 		rcu_read_unlock();
15130 		goto err_free;
15131 	}
15132 	rcu_read_unlock();
15133 
15134 	new_fltr->flow_id = flow_id;
15135 	new_fltr->base.rxq = rxq_index;
15136 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15137 	if (!rc) {
15138 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15139 		return new_fltr->base.sw_id;
15140 	}
15141 
15142 err_free:
15143 	bnxt_del_l2_filter(bp, l2_fltr);
15144 	kfree(new_fltr);
15145 	return rc;
15146 }
15147 #endif
15148 
15149 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15150 {
15151 	spin_lock_bh(&bp->ntp_fltr_lock);
15152 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15153 		spin_unlock_bh(&bp->ntp_fltr_lock);
15154 		return;
15155 	}
15156 	hlist_del_rcu(&fltr->base.hash);
15157 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15158 	bp->ntp_fltr_count--;
15159 	spin_unlock_bh(&bp->ntp_fltr_lock);
15160 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15161 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15162 	kfree_rcu(fltr, base.rcu);
15163 }
15164 
15165 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15166 {
15167 #ifdef CONFIG_RFS_ACCEL
15168 	int i;
15169 
15170 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15171 		struct hlist_head *head;
15172 		struct hlist_node *tmp;
15173 		struct bnxt_ntuple_filter *fltr;
15174 		int rc;
15175 
15176 		head = &bp->ntp_fltr_hash_tbl[i];
15177 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15178 			bool del = false;
15179 
15180 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15181 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15182 					continue;
15183 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15184 							fltr->flow_id,
15185 							fltr->base.sw_id)) {
15186 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15187 									 fltr);
15188 					del = true;
15189 				}
15190 			} else {
15191 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15192 								       fltr);
15193 				if (rc)
15194 					del = true;
15195 				else
15196 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15197 			}
15198 
15199 			if (del)
15200 				bnxt_del_ntp_filter(bp, fltr);
15201 		}
15202 	}
15203 #endif
15204 }
15205 
15206 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15207 				    unsigned int entry, struct udp_tunnel_info *ti)
15208 {
15209 	struct bnxt *bp = netdev_priv(netdev);
15210 	unsigned int cmd;
15211 
15212 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15213 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15214 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15215 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15216 	else
15217 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15218 
15219 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15220 }
15221 
15222 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15223 				      unsigned int entry, struct udp_tunnel_info *ti)
15224 {
15225 	struct bnxt *bp = netdev_priv(netdev);
15226 	unsigned int cmd;
15227 
15228 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15229 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15230 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15231 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15232 	else
15233 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15234 
15235 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15236 }
15237 
15238 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15239 	.set_port	= bnxt_udp_tunnel_set_port,
15240 	.unset_port	= bnxt_udp_tunnel_unset_port,
15241 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15242 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15243 	.tables		= {
15244 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15245 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15246 	},
15247 }, bnxt_udp_tunnels_p7 = {
15248 	.set_port	= bnxt_udp_tunnel_set_port,
15249 	.unset_port	= bnxt_udp_tunnel_unset_port,
15250 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15251 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15252 	.tables		= {
15253 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15254 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15255 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15256 	},
15257 };
15258 
15259 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15260 			       struct net_device *dev, u32 filter_mask,
15261 			       int nlflags)
15262 {
15263 	struct bnxt *bp = netdev_priv(dev);
15264 
15265 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15266 				       nlflags, filter_mask, NULL);
15267 }
15268 
15269 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15270 			       u16 flags, struct netlink_ext_ack *extack)
15271 {
15272 	struct bnxt *bp = netdev_priv(dev);
15273 	struct nlattr *attr, *br_spec;
15274 	int rem, rc = 0;
15275 
15276 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15277 		return -EOPNOTSUPP;
15278 
15279 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15280 	if (!br_spec)
15281 		return -EINVAL;
15282 
15283 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15284 		u16 mode;
15285 
15286 		mode = nla_get_u16(attr);
15287 		if (mode == bp->br_mode)
15288 			break;
15289 
15290 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15291 		if (!rc)
15292 			bp->br_mode = mode;
15293 		break;
15294 	}
15295 	return rc;
15296 }
15297 
15298 int bnxt_get_port_parent_id(struct net_device *dev,
15299 			    struct netdev_phys_item_id *ppid)
15300 {
15301 	struct bnxt *bp = netdev_priv(dev);
15302 
15303 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15304 		return -EOPNOTSUPP;
15305 
15306 	/* The PF and it's VF-reps only support the switchdev framework */
15307 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15308 		return -EOPNOTSUPP;
15309 
15310 	ppid->id_len = sizeof(bp->dsn);
15311 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15312 
15313 	return 0;
15314 }
15315 
15316 static const struct net_device_ops bnxt_netdev_ops = {
15317 	.ndo_open		= bnxt_open,
15318 	.ndo_start_xmit		= bnxt_start_xmit,
15319 	.ndo_stop		= bnxt_close,
15320 	.ndo_get_stats64	= bnxt_get_stats64,
15321 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15322 	.ndo_eth_ioctl		= bnxt_ioctl,
15323 	.ndo_validate_addr	= eth_validate_addr,
15324 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15325 	.ndo_change_mtu		= bnxt_change_mtu,
15326 	.ndo_fix_features	= bnxt_fix_features,
15327 	.ndo_set_features	= bnxt_set_features,
15328 	.ndo_features_check	= bnxt_features_check,
15329 	.ndo_tx_timeout		= bnxt_tx_timeout,
15330 #ifdef CONFIG_BNXT_SRIOV
15331 	.ndo_get_vf_config	= bnxt_get_vf_config,
15332 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15333 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15334 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15335 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15336 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15337 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15338 #endif
15339 	.ndo_setup_tc           = bnxt_setup_tc,
15340 #ifdef CONFIG_RFS_ACCEL
15341 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15342 #endif
15343 	.ndo_bpf		= bnxt_xdp,
15344 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15345 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15346 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15347 };
15348 
15349 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15350 				    struct netdev_queue_stats_rx *stats)
15351 {
15352 	struct bnxt *bp = netdev_priv(dev);
15353 	struct bnxt_cp_ring_info *cpr;
15354 	u64 *sw;
15355 
15356 	cpr = &bp->bnapi[i]->cp_ring;
15357 	sw = cpr->stats.sw_stats;
15358 
15359 	stats->packets = 0;
15360 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15361 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15362 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15363 
15364 	stats->bytes = 0;
15365 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15366 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15367 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15368 
15369 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15370 }
15371 
15372 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15373 				    struct netdev_queue_stats_tx *stats)
15374 {
15375 	struct bnxt *bp = netdev_priv(dev);
15376 	struct bnxt_napi *bnapi;
15377 	u64 *sw;
15378 
15379 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15380 	sw = bnapi->cp_ring.stats.sw_stats;
15381 
15382 	stats->packets = 0;
15383 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15384 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15385 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15386 
15387 	stats->bytes = 0;
15388 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15389 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15390 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15391 }
15392 
15393 static void bnxt_get_base_stats(struct net_device *dev,
15394 				struct netdev_queue_stats_rx *rx,
15395 				struct netdev_queue_stats_tx *tx)
15396 {
15397 	struct bnxt *bp = netdev_priv(dev);
15398 
15399 	rx->packets = bp->net_stats_prev.rx_packets;
15400 	rx->bytes = bp->net_stats_prev.rx_bytes;
15401 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15402 
15403 	tx->packets = bp->net_stats_prev.tx_packets;
15404 	tx->bytes = bp->net_stats_prev.tx_bytes;
15405 }
15406 
15407 static const struct netdev_stat_ops bnxt_stat_ops = {
15408 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15409 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15410 	.get_base_stats		= bnxt_get_base_stats,
15411 };
15412 
15413 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15414 {
15415 	struct bnxt_rx_ring_info *rxr, *clone;
15416 	struct bnxt *bp = netdev_priv(dev);
15417 	struct bnxt_ring_struct *ring;
15418 	int rc;
15419 
15420 	rxr = &bp->rx_ring[idx];
15421 	clone = qmem;
15422 	memcpy(clone, rxr, sizeof(*rxr));
15423 	bnxt_init_rx_ring_struct(bp, clone);
15424 	bnxt_reset_rx_ring_struct(bp, clone);
15425 
15426 	clone->rx_prod = 0;
15427 	clone->rx_agg_prod = 0;
15428 	clone->rx_sw_agg_prod = 0;
15429 	clone->rx_next_cons = 0;
15430 
15431 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15432 	if (rc)
15433 		return rc;
15434 
15435 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15436 	if (rc < 0)
15437 		goto err_page_pool_destroy;
15438 
15439 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15440 					MEM_TYPE_PAGE_POOL,
15441 					clone->page_pool);
15442 	if (rc)
15443 		goto err_rxq_info_unreg;
15444 
15445 	ring = &clone->rx_ring_struct;
15446 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15447 	if (rc)
15448 		goto err_free_rx_ring;
15449 
15450 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15451 		ring = &clone->rx_agg_ring_struct;
15452 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15453 		if (rc)
15454 			goto err_free_rx_agg_ring;
15455 
15456 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15457 		if (rc)
15458 			goto err_free_rx_agg_ring;
15459 	}
15460 
15461 	if (bp->flags & BNXT_FLAG_TPA) {
15462 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15463 		if (rc)
15464 			goto err_free_tpa_info;
15465 	}
15466 
15467 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15468 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15469 
15470 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15471 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15472 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15473 	if (bp->flags & BNXT_FLAG_TPA)
15474 		bnxt_alloc_one_tpa_info_data(bp, clone);
15475 
15476 	return 0;
15477 
15478 err_free_tpa_info:
15479 	bnxt_free_one_tpa_info(bp, clone);
15480 err_free_rx_agg_ring:
15481 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15482 err_free_rx_ring:
15483 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15484 err_rxq_info_unreg:
15485 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15486 err_page_pool_destroy:
15487 	page_pool_destroy(clone->page_pool);
15488 	if (bnxt_separate_head_pool())
15489 		page_pool_destroy(clone->head_pool);
15490 	clone->page_pool = NULL;
15491 	clone->head_pool = NULL;
15492 	return rc;
15493 }
15494 
15495 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15496 {
15497 	struct bnxt_rx_ring_info *rxr = qmem;
15498 	struct bnxt *bp = netdev_priv(dev);
15499 	struct bnxt_ring_struct *ring;
15500 
15501 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15502 
15503 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15504 
15505 	page_pool_destroy(rxr->page_pool);
15506 	if (bnxt_separate_head_pool())
15507 		page_pool_destroy(rxr->head_pool);
15508 	rxr->page_pool = NULL;
15509 	rxr->head_pool = NULL;
15510 
15511 	ring = &rxr->rx_ring_struct;
15512 	bnxt_free_ring(bp, &ring->ring_mem);
15513 
15514 	ring = &rxr->rx_agg_ring_struct;
15515 	bnxt_free_ring(bp, &ring->ring_mem);
15516 
15517 	kfree(rxr->rx_agg_bmap);
15518 	rxr->rx_agg_bmap = NULL;
15519 }
15520 
15521 static void bnxt_copy_rx_ring(struct bnxt *bp,
15522 			      struct bnxt_rx_ring_info *dst,
15523 			      struct bnxt_rx_ring_info *src)
15524 {
15525 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15526 	struct bnxt_ring_struct *dst_ring, *src_ring;
15527 	int i;
15528 
15529 	dst_ring = &dst->rx_ring_struct;
15530 	dst_rmem = &dst_ring->ring_mem;
15531 	src_ring = &src->rx_ring_struct;
15532 	src_rmem = &src_ring->ring_mem;
15533 
15534 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15535 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15536 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15537 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15538 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15539 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15540 
15541 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15542 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15543 	*dst_rmem->vmem = *src_rmem->vmem;
15544 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15545 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15546 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15547 	}
15548 
15549 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15550 		return;
15551 
15552 	dst_ring = &dst->rx_agg_ring_struct;
15553 	dst_rmem = &dst_ring->ring_mem;
15554 	src_ring = &src->rx_agg_ring_struct;
15555 	src_rmem = &src_ring->ring_mem;
15556 
15557 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15558 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15559 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15560 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15561 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15562 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15563 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15564 
15565 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15566 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15567 	*dst_rmem->vmem = *src_rmem->vmem;
15568 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15569 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15570 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15571 	}
15572 
15573 	dst->rx_agg_bmap = src->rx_agg_bmap;
15574 }
15575 
15576 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15577 {
15578 	struct bnxt *bp = netdev_priv(dev);
15579 	struct bnxt_rx_ring_info *rxr, *clone;
15580 	struct bnxt_cp_ring_info *cpr;
15581 	struct bnxt_vnic_info *vnic;
15582 	int i, rc;
15583 
15584 	rxr = &bp->rx_ring[idx];
15585 	clone = qmem;
15586 
15587 	rxr->rx_prod = clone->rx_prod;
15588 	rxr->rx_agg_prod = clone->rx_agg_prod;
15589 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15590 	rxr->rx_next_cons = clone->rx_next_cons;
15591 	rxr->rx_tpa = clone->rx_tpa;
15592 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15593 	rxr->page_pool = clone->page_pool;
15594 	rxr->head_pool = clone->head_pool;
15595 	rxr->xdp_rxq = clone->xdp_rxq;
15596 
15597 	bnxt_copy_rx_ring(bp, rxr, clone);
15598 
15599 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15600 	if (rc)
15601 		return rc;
15602 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15603 	if (rc)
15604 		goto err_free_hwrm_rx_ring;
15605 
15606 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15607 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15608 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15609 
15610 	cpr = &rxr->bnapi->cp_ring;
15611 	cpr->sw_stats->rx.rx_resets++;
15612 
15613 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15614 		vnic = &bp->vnic_info[i];
15615 
15616 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15617 		if (rc) {
15618 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15619 				   vnic->vnic_id, rc);
15620 			return rc;
15621 		}
15622 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15623 		bnxt_hwrm_vnic_update(bp, vnic,
15624 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15625 	}
15626 
15627 	return 0;
15628 
15629 err_free_hwrm_rx_ring:
15630 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15631 	return rc;
15632 }
15633 
15634 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15635 {
15636 	struct bnxt *bp = netdev_priv(dev);
15637 	struct bnxt_rx_ring_info *rxr;
15638 	struct bnxt_vnic_info *vnic;
15639 	int i;
15640 
15641 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15642 		vnic = &bp->vnic_info[i];
15643 		vnic->mru = 0;
15644 		bnxt_hwrm_vnic_update(bp, vnic,
15645 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15646 	}
15647 	/* Make sure NAPI sees that the VNIC is disabled */
15648 	synchronize_net();
15649 	rxr = &bp->rx_ring[idx];
15650 	cancel_work_sync(&rxr->bnapi->cp_ring.dim.work);
15651 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15652 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15653 	rxr->rx_next_cons = 0;
15654 	page_pool_disable_direct_recycling(rxr->page_pool);
15655 	if (bnxt_separate_head_pool())
15656 		page_pool_disable_direct_recycling(rxr->head_pool);
15657 
15658 	memcpy(qmem, rxr, sizeof(*rxr));
15659 	bnxt_init_rx_ring_struct(bp, qmem);
15660 
15661 	return 0;
15662 }
15663 
15664 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15665 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15666 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15667 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15668 	.ndo_queue_start	= bnxt_queue_start,
15669 	.ndo_queue_stop		= bnxt_queue_stop,
15670 };
15671 
15672 static void bnxt_remove_one(struct pci_dev *pdev)
15673 {
15674 	struct net_device *dev = pci_get_drvdata(pdev);
15675 	struct bnxt *bp = netdev_priv(dev);
15676 
15677 	if (BNXT_PF(bp))
15678 		bnxt_sriov_disable(bp);
15679 
15680 	bnxt_rdma_aux_device_del(bp);
15681 
15682 	bnxt_ptp_clear(bp);
15683 	unregister_netdev(dev);
15684 
15685 	bnxt_rdma_aux_device_uninit(bp);
15686 
15687 	bnxt_free_l2_filters(bp, true);
15688 	bnxt_free_ntp_fltrs(bp, true);
15689 	WARN_ON(bp->num_rss_ctx);
15690 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15691 	/* Flush any pending tasks */
15692 	cancel_work_sync(&bp->sp_task);
15693 	cancel_delayed_work_sync(&bp->fw_reset_task);
15694 	bp->sp_event = 0;
15695 
15696 	bnxt_dl_fw_reporters_destroy(bp);
15697 	bnxt_dl_unregister(bp);
15698 	bnxt_shutdown_tc(bp);
15699 
15700 	bnxt_clear_int_mode(bp);
15701 	bnxt_hwrm_func_drv_unrgtr(bp);
15702 	bnxt_free_hwrm_resources(bp);
15703 	bnxt_hwmon_uninit(bp);
15704 	bnxt_ethtool_free(bp);
15705 	bnxt_dcb_free(bp);
15706 	kfree(bp->ptp_cfg);
15707 	bp->ptp_cfg = NULL;
15708 	kfree(bp->fw_health);
15709 	bp->fw_health = NULL;
15710 	bnxt_cleanup_pci(bp);
15711 	bnxt_free_ctx_mem(bp, true);
15712 	bnxt_free_crash_dump_mem(bp);
15713 	kfree(bp->rss_indir_tbl);
15714 	bp->rss_indir_tbl = NULL;
15715 	bnxt_free_port_stats(bp);
15716 	free_netdev(dev);
15717 }
15718 
15719 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15720 {
15721 	int rc = 0;
15722 	struct bnxt_link_info *link_info = &bp->link_info;
15723 
15724 	bp->phy_flags = 0;
15725 	rc = bnxt_hwrm_phy_qcaps(bp);
15726 	if (rc) {
15727 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15728 			   rc);
15729 		return rc;
15730 	}
15731 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15732 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15733 	else
15734 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15735 
15736 	bp->mac_flags = 0;
15737 	bnxt_hwrm_mac_qcaps(bp);
15738 
15739 	if (!fw_dflt)
15740 		return 0;
15741 
15742 	mutex_lock(&bp->link_lock);
15743 	rc = bnxt_update_link(bp, false);
15744 	if (rc) {
15745 		mutex_unlock(&bp->link_lock);
15746 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15747 			   rc);
15748 		return rc;
15749 	}
15750 
15751 	/* Older firmware does not have supported_auto_speeds, so assume
15752 	 * that all supported speeds can be autonegotiated.
15753 	 */
15754 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15755 		link_info->support_auto_speeds = link_info->support_speeds;
15756 
15757 	bnxt_init_ethtool_link_settings(bp);
15758 	mutex_unlock(&bp->link_lock);
15759 	return 0;
15760 }
15761 
15762 static int bnxt_get_max_irq(struct pci_dev *pdev)
15763 {
15764 	u16 ctrl;
15765 
15766 	if (!pdev->msix_cap)
15767 		return 1;
15768 
15769 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15770 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15771 }
15772 
15773 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15774 				int *max_cp)
15775 {
15776 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15777 	int max_ring_grps = 0, max_irq;
15778 
15779 	*max_tx = hw_resc->max_tx_rings;
15780 	*max_rx = hw_resc->max_rx_rings;
15781 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15782 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15783 			bnxt_get_ulp_msix_num_in_use(bp),
15784 			hw_resc->max_stat_ctxs -
15785 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15786 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15787 		*max_cp = min_t(int, *max_cp, max_irq);
15788 	max_ring_grps = hw_resc->max_hw_ring_grps;
15789 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15790 		*max_cp -= 1;
15791 		*max_rx -= 2;
15792 	}
15793 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15794 		*max_rx >>= 1;
15795 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15796 		int rc;
15797 
15798 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15799 		if (rc) {
15800 			*max_rx = 0;
15801 			*max_tx = 0;
15802 		}
15803 		/* On P5 chips, max_cp output param should be available NQs */
15804 		*max_cp = max_irq;
15805 	}
15806 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15807 }
15808 
15809 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15810 {
15811 	int rx, tx, cp;
15812 
15813 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15814 	*max_rx = rx;
15815 	*max_tx = tx;
15816 	if (!rx || !tx || !cp)
15817 		return -ENOMEM;
15818 
15819 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15820 }
15821 
15822 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15823 			       bool shared)
15824 {
15825 	int rc;
15826 
15827 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15828 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15829 		/* Not enough rings, try disabling agg rings. */
15830 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15831 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15832 		if (rc) {
15833 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15834 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15835 			return rc;
15836 		}
15837 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15838 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15839 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15840 		bnxt_set_ring_params(bp);
15841 	}
15842 
15843 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15844 		int max_cp, max_stat, max_irq;
15845 
15846 		/* Reserve minimum resources for RoCE */
15847 		max_cp = bnxt_get_max_func_cp_rings(bp);
15848 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15849 		max_irq = bnxt_get_max_func_irqs(bp);
15850 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15851 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15852 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15853 			return 0;
15854 
15855 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15856 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15857 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15858 		max_cp = min_t(int, max_cp, max_irq);
15859 		max_cp = min_t(int, max_cp, max_stat);
15860 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15861 		if (rc)
15862 			rc = 0;
15863 	}
15864 	return rc;
15865 }
15866 
15867 /* In initial default shared ring setting, each shared ring must have a
15868  * RX/TX ring pair.
15869  */
15870 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15871 {
15872 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15873 	bp->rx_nr_rings = bp->cp_nr_rings;
15874 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15875 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15876 }
15877 
15878 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15879 {
15880 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15881 	int avail_msix;
15882 
15883 	if (!bnxt_can_reserve_rings(bp))
15884 		return 0;
15885 
15886 	if (sh)
15887 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15888 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15889 	/* Reduce default rings on multi-port cards so that total default
15890 	 * rings do not exceed CPU count.
15891 	 */
15892 	if (bp->port_count > 1) {
15893 		int max_rings =
15894 			max_t(int, num_online_cpus() / bp->port_count, 1);
15895 
15896 		dflt_rings = min_t(int, dflt_rings, max_rings);
15897 	}
15898 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15899 	if (rc)
15900 		return rc;
15901 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15902 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15903 	if (sh)
15904 		bnxt_trim_dflt_sh_rings(bp);
15905 	else
15906 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15907 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15908 
15909 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15910 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15911 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15912 
15913 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15914 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15915 	}
15916 
15917 	rc = __bnxt_reserve_rings(bp);
15918 	if (rc && rc != -ENODEV)
15919 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15920 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15921 	if (sh)
15922 		bnxt_trim_dflt_sh_rings(bp);
15923 
15924 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15925 	if (bnxt_need_reserve_rings(bp)) {
15926 		rc = __bnxt_reserve_rings(bp);
15927 		if (rc && rc != -ENODEV)
15928 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15929 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15930 	}
15931 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15932 		bp->rx_nr_rings++;
15933 		bp->cp_nr_rings++;
15934 	}
15935 	if (rc) {
15936 		bp->tx_nr_rings = 0;
15937 		bp->rx_nr_rings = 0;
15938 	}
15939 	return rc;
15940 }
15941 
15942 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15943 {
15944 	int rc;
15945 
15946 	if (bp->tx_nr_rings)
15947 		return 0;
15948 
15949 	bnxt_ulp_irq_stop(bp);
15950 	bnxt_clear_int_mode(bp);
15951 	rc = bnxt_set_dflt_rings(bp, true);
15952 	if (rc) {
15953 		if (BNXT_VF(bp) && rc == -ENODEV)
15954 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15955 		else
15956 			netdev_err(bp->dev, "Not enough rings available.\n");
15957 		goto init_dflt_ring_err;
15958 	}
15959 	rc = bnxt_init_int_mode(bp);
15960 	if (rc)
15961 		goto init_dflt_ring_err;
15962 
15963 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15964 
15965 	bnxt_set_dflt_rfs(bp);
15966 
15967 init_dflt_ring_err:
15968 	bnxt_ulp_irq_restart(bp, rc);
15969 	return rc;
15970 }
15971 
15972 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15973 {
15974 	int rc;
15975 
15976 	ASSERT_RTNL();
15977 	bnxt_hwrm_func_qcaps(bp);
15978 
15979 	if (netif_running(bp->dev))
15980 		__bnxt_close_nic(bp, true, false);
15981 
15982 	bnxt_ulp_irq_stop(bp);
15983 	bnxt_clear_int_mode(bp);
15984 	rc = bnxt_init_int_mode(bp);
15985 	bnxt_ulp_irq_restart(bp, rc);
15986 
15987 	if (netif_running(bp->dev)) {
15988 		if (rc)
15989 			dev_close(bp->dev);
15990 		else
15991 			rc = bnxt_open_nic(bp, true, false);
15992 	}
15993 
15994 	return rc;
15995 }
15996 
15997 static int bnxt_init_mac_addr(struct bnxt *bp)
15998 {
15999 	int rc = 0;
16000 
16001 	if (BNXT_PF(bp)) {
16002 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
16003 	} else {
16004 #ifdef CONFIG_BNXT_SRIOV
16005 		struct bnxt_vf_info *vf = &bp->vf;
16006 		bool strict_approval = true;
16007 
16008 		if (is_valid_ether_addr(vf->mac_addr)) {
16009 			/* overwrite netdev dev_addr with admin VF MAC */
16010 			eth_hw_addr_set(bp->dev, vf->mac_addr);
16011 			/* Older PF driver or firmware may not approve this
16012 			 * correctly.
16013 			 */
16014 			strict_approval = false;
16015 		} else {
16016 			eth_hw_addr_random(bp->dev);
16017 		}
16018 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
16019 #endif
16020 	}
16021 	return rc;
16022 }
16023 
16024 static void bnxt_vpd_read_info(struct bnxt *bp)
16025 {
16026 	struct pci_dev *pdev = bp->pdev;
16027 	unsigned int vpd_size, kw_len;
16028 	int pos, size;
16029 	u8 *vpd_data;
16030 
16031 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
16032 	if (IS_ERR(vpd_data)) {
16033 		pci_warn(pdev, "Unable to read VPD\n");
16034 		return;
16035 	}
16036 
16037 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16038 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16039 	if (pos < 0)
16040 		goto read_sn;
16041 
16042 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16043 	memcpy(bp->board_partno, &vpd_data[pos], size);
16044 
16045 read_sn:
16046 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16047 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16048 					   &kw_len);
16049 	if (pos < 0)
16050 		goto exit;
16051 
16052 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16053 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16054 exit:
16055 	kfree(vpd_data);
16056 }
16057 
16058 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16059 {
16060 	struct pci_dev *pdev = bp->pdev;
16061 	u64 qword;
16062 
16063 	qword = pci_get_dsn(pdev);
16064 	if (!qword) {
16065 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16066 		return -EOPNOTSUPP;
16067 	}
16068 
16069 	put_unaligned_le64(qword, dsn);
16070 
16071 	bp->flags |= BNXT_FLAG_DSN_VALID;
16072 	return 0;
16073 }
16074 
16075 static int bnxt_map_db_bar(struct bnxt *bp)
16076 {
16077 	if (!bp->db_size)
16078 		return -ENODEV;
16079 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16080 	if (!bp->bar1)
16081 		return -ENOMEM;
16082 	return 0;
16083 }
16084 
16085 void bnxt_print_device_info(struct bnxt *bp)
16086 {
16087 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16088 		    board_info[bp->board_idx].name,
16089 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16090 
16091 	pcie_print_link_status(bp->pdev);
16092 }
16093 
16094 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16095 {
16096 	struct bnxt_hw_resc *hw_resc;
16097 	struct net_device *dev;
16098 	struct bnxt *bp;
16099 	int rc, max_irqs;
16100 
16101 	if (pci_is_bridge(pdev))
16102 		return -ENODEV;
16103 
16104 	if (!pdev->msix_cap) {
16105 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16106 		return -ENODEV;
16107 	}
16108 
16109 	/* Clear any pending DMA transactions from crash kernel
16110 	 * while loading driver in capture kernel.
16111 	 */
16112 	if (is_kdump_kernel()) {
16113 		pci_clear_master(pdev);
16114 		pcie_flr(pdev);
16115 	}
16116 
16117 	max_irqs = bnxt_get_max_irq(pdev);
16118 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16119 				 max_irqs);
16120 	if (!dev)
16121 		return -ENOMEM;
16122 
16123 	bp = netdev_priv(dev);
16124 	bp->board_idx = ent->driver_data;
16125 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16126 	bnxt_set_max_func_irqs(bp, max_irqs);
16127 
16128 	if (bnxt_vf_pciid(bp->board_idx))
16129 		bp->flags |= BNXT_FLAG_VF;
16130 
16131 	/* No devlink port registration in case of a VF */
16132 	if (BNXT_PF(bp))
16133 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16134 
16135 	rc = bnxt_init_board(pdev, dev);
16136 	if (rc < 0)
16137 		goto init_err_free;
16138 
16139 	dev->netdev_ops = &bnxt_netdev_ops;
16140 	dev->stat_ops = &bnxt_stat_ops;
16141 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16142 	dev->ethtool_ops = &bnxt_ethtool_ops;
16143 	pci_set_drvdata(pdev, dev);
16144 
16145 	rc = bnxt_alloc_hwrm_resources(bp);
16146 	if (rc)
16147 		goto init_err_pci_clean;
16148 
16149 	mutex_init(&bp->hwrm_cmd_lock);
16150 	mutex_init(&bp->link_lock);
16151 
16152 	rc = bnxt_fw_init_one_p1(bp);
16153 	if (rc)
16154 		goto init_err_pci_clean;
16155 
16156 	if (BNXT_PF(bp))
16157 		bnxt_vpd_read_info(bp);
16158 
16159 	if (BNXT_CHIP_P5_PLUS(bp)) {
16160 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16161 		if (BNXT_CHIP_P7(bp))
16162 			bp->flags |= BNXT_FLAG_CHIP_P7;
16163 	}
16164 
16165 	rc = bnxt_alloc_rss_indir_tbl(bp);
16166 	if (rc)
16167 		goto init_err_pci_clean;
16168 
16169 	rc = bnxt_fw_init_one_p2(bp);
16170 	if (rc)
16171 		goto init_err_pci_clean;
16172 
16173 	rc = bnxt_map_db_bar(bp);
16174 	if (rc) {
16175 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16176 			rc);
16177 		goto init_err_pci_clean;
16178 	}
16179 
16180 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16181 			   NETIF_F_TSO | NETIF_F_TSO6 |
16182 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16183 			   NETIF_F_GSO_IPXIP4 |
16184 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16185 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16186 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16187 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16188 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16189 
16190 	if (BNXT_SUPPORTS_TPA(bp))
16191 		dev->hw_features |= NETIF_F_LRO;
16192 
16193 	dev->hw_enc_features =
16194 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16195 			NETIF_F_TSO | NETIF_F_TSO6 |
16196 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16197 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16198 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16199 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16200 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16201 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16202 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16203 	else
16204 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16205 
16206 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16207 				    NETIF_F_GSO_GRE_CSUM;
16208 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16209 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16210 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16211 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16212 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16213 	if (BNXT_SUPPORTS_TPA(bp))
16214 		dev->hw_features |= NETIF_F_GRO_HW;
16215 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16216 	if (dev->features & NETIF_F_GRO_HW)
16217 		dev->features &= ~NETIF_F_LRO;
16218 	dev->priv_flags |= IFF_UNICAST_FLT;
16219 
16220 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16221 	if (bp->tso_max_segs)
16222 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16223 
16224 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16225 			    NETDEV_XDP_ACT_RX_SG;
16226 
16227 #ifdef CONFIG_BNXT_SRIOV
16228 	init_waitqueue_head(&bp->sriov_cfg_wait);
16229 #endif
16230 	if (BNXT_SUPPORTS_TPA(bp)) {
16231 		bp->gro_func = bnxt_gro_func_5730x;
16232 		if (BNXT_CHIP_P4(bp))
16233 			bp->gro_func = bnxt_gro_func_5731x;
16234 		else if (BNXT_CHIP_P5_PLUS(bp))
16235 			bp->gro_func = bnxt_gro_func_5750x;
16236 	}
16237 	if (!BNXT_CHIP_P4_PLUS(bp))
16238 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16239 
16240 	rc = bnxt_init_mac_addr(bp);
16241 	if (rc) {
16242 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16243 		rc = -EADDRNOTAVAIL;
16244 		goto init_err_pci_clean;
16245 	}
16246 
16247 	if (BNXT_PF(bp)) {
16248 		/* Read the adapter's DSN to use as the eswitch switch_id */
16249 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16250 	}
16251 
16252 	/* MTU range: 60 - FW defined max */
16253 	dev->min_mtu = ETH_ZLEN;
16254 	dev->max_mtu = bp->max_mtu;
16255 
16256 	rc = bnxt_probe_phy(bp, true);
16257 	if (rc)
16258 		goto init_err_pci_clean;
16259 
16260 	hw_resc = &bp->hw_resc;
16261 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16262 		       BNXT_L2_FLTR_MAX_FLTR;
16263 	/* Older firmware may not report these filters properly */
16264 	if (bp->max_fltr < BNXT_MAX_FLTR)
16265 		bp->max_fltr = BNXT_MAX_FLTR;
16266 	bnxt_init_l2_fltr_tbl(bp);
16267 	bnxt_set_rx_skb_mode(bp, false);
16268 	bnxt_set_tpa_flags(bp);
16269 	bnxt_init_ring_params(bp);
16270 	bnxt_set_ring_params(bp);
16271 	bnxt_rdma_aux_device_init(bp);
16272 	rc = bnxt_set_dflt_rings(bp, true);
16273 	if (rc) {
16274 		if (BNXT_VF(bp) && rc == -ENODEV) {
16275 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16276 		} else {
16277 			netdev_err(bp->dev, "Not enough rings available.\n");
16278 			rc = -ENOMEM;
16279 		}
16280 		goto init_err_pci_clean;
16281 	}
16282 
16283 	bnxt_fw_init_one_p3(bp);
16284 
16285 	bnxt_init_dflt_coal(bp);
16286 
16287 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16288 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16289 
16290 	rc = bnxt_init_int_mode(bp);
16291 	if (rc)
16292 		goto init_err_pci_clean;
16293 
16294 	/* No TC has been set yet and rings may have been trimmed due to
16295 	 * limited MSIX, so we re-initialize the TX rings per TC.
16296 	 */
16297 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16298 
16299 	if (BNXT_PF(bp)) {
16300 		if (!bnxt_pf_wq) {
16301 			bnxt_pf_wq =
16302 				create_singlethread_workqueue("bnxt_pf_wq");
16303 			if (!bnxt_pf_wq) {
16304 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16305 				rc = -ENOMEM;
16306 				goto init_err_pci_clean;
16307 			}
16308 		}
16309 		rc = bnxt_init_tc(bp);
16310 		if (rc)
16311 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16312 				   rc);
16313 	}
16314 
16315 	bnxt_inv_fw_health_reg(bp);
16316 	rc = bnxt_dl_register(bp);
16317 	if (rc)
16318 		goto init_err_dl;
16319 
16320 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16321 
16322 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16323 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16324 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16325 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16326 
16327 	rc = register_netdev(dev);
16328 	if (rc)
16329 		goto init_err_cleanup;
16330 
16331 	bnxt_dl_fw_reporters_create(bp);
16332 
16333 	bnxt_rdma_aux_device_add(bp);
16334 
16335 	bnxt_print_device_info(bp);
16336 
16337 	pci_save_state(pdev);
16338 
16339 	return 0;
16340 init_err_cleanup:
16341 	bnxt_rdma_aux_device_uninit(bp);
16342 	bnxt_dl_unregister(bp);
16343 init_err_dl:
16344 	bnxt_shutdown_tc(bp);
16345 	bnxt_clear_int_mode(bp);
16346 
16347 init_err_pci_clean:
16348 	bnxt_hwrm_func_drv_unrgtr(bp);
16349 	bnxt_free_hwrm_resources(bp);
16350 	bnxt_hwmon_uninit(bp);
16351 	bnxt_ethtool_free(bp);
16352 	bnxt_ptp_clear(bp);
16353 	kfree(bp->ptp_cfg);
16354 	bp->ptp_cfg = NULL;
16355 	kfree(bp->fw_health);
16356 	bp->fw_health = NULL;
16357 	bnxt_cleanup_pci(bp);
16358 	bnxt_free_ctx_mem(bp, true);
16359 	bnxt_free_crash_dump_mem(bp);
16360 	kfree(bp->rss_indir_tbl);
16361 	bp->rss_indir_tbl = NULL;
16362 
16363 init_err_free:
16364 	free_netdev(dev);
16365 	return rc;
16366 }
16367 
16368 static void bnxt_shutdown(struct pci_dev *pdev)
16369 {
16370 	struct net_device *dev = pci_get_drvdata(pdev);
16371 	struct bnxt *bp;
16372 
16373 	if (!dev)
16374 		return;
16375 
16376 	rtnl_lock();
16377 	bp = netdev_priv(dev);
16378 	if (!bp)
16379 		goto shutdown_exit;
16380 
16381 	if (netif_running(dev))
16382 		dev_close(dev);
16383 
16384 	bnxt_ptp_clear(bp);
16385 	bnxt_clear_int_mode(bp);
16386 	pci_disable_device(pdev);
16387 
16388 	if (system_state == SYSTEM_POWER_OFF) {
16389 		pci_wake_from_d3(pdev, bp->wol);
16390 		pci_set_power_state(pdev, PCI_D3hot);
16391 	}
16392 
16393 shutdown_exit:
16394 	rtnl_unlock();
16395 }
16396 
16397 #ifdef CONFIG_PM_SLEEP
16398 static int bnxt_suspend(struct device *device)
16399 {
16400 	struct net_device *dev = dev_get_drvdata(device);
16401 	struct bnxt *bp = netdev_priv(dev);
16402 	int rc = 0;
16403 
16404 	bnxt_ulp_stop(bp);
16405 
16406 	rtnl_lock();
16407 	if (netif_running(dev)) {
16408 		netif_device_detach(dev);
16409 		rc = bnxt_close(dev);
16410 	}
16411 	bnxt_hwrm_func_drv_unrgtr(bp);
16412 	bnxt_ptp_clear(bp);
16413 	pci_disable_device(bp->pdev);
16414 	bnxt_free_ctx_mem(bp, false);
16415 	rtnl_unlock();
16416 	return rc;
16417 }
16418 
16419 static int bnxt_resume(struct device *device)
16420 {
16421 	struct net_device *dev = dev_get_drvdata(device);
16422 	struct bnxt *bp = netdev_priv(dev);
16423 	int rc = 0;
16424 
16425 	rtnl_lock();
16426 	rc = pci_enable_device(bp->pdev);
16427 	if (rc) {
16428 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16429 			   rc);
16430 		goto resume_exit;
16431 	}
16432 	pci_set_master(bp->pdev);
16433 	if (bnxt_hwrm_ver_get(bp)) {
16434 		rc = -ENODEV;
16435 		goto resume_exit;
16436 	}
16437 	rc = bnxt_hwrm_func_reset(bp);
16438 	if (rc) {
16439 		rc = -EBUSY;
16440 		goto resume_exit;
16441 	}
16442 
16443 	rc = bnxt_hwrm_func_qcaps(bp);
16444 	if (rc)
16445 		goto resume_exit;
16446 
16447 	bnxt_clear_reservations(bp, true);
16448 
16449 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16450 		rc = -ENODEV;
16451 		goto resume_exit;
16452 	}
16453 	if (bp->fw_crash_mem)
16454 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16455 
16456 	if (bnxt_ptp_init(bp)) {
16457 		kfree(bp->ptp_cfg);
16458 		bp->ptp_cfg = NULL;
16459 	}
16460 	bnxt_get_wol_settings(bp);
16461 	if (netif_running(dev)) {
16462 		rc = bnxt_open(dev);
16463 		if (!rc)
16464 			netif_device_attach(dev);
16465 	}
16466 
16467 resume_exit:
16468 	rtnl_unlock();
16469 	bnxt_ulp_start(bp, rc);
16470 	if (!rc)
16471 		bnxt_reenable_sriov(bp);
16472 	return rc;
16473 }
16474 
16475 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16476 #define BNXT_PM_OPS (&bnxt_pm_ops)
16477 
16478 #else
16479 
16480 #define BNXT_PM_OPS NULL
16481 
16482 #endif /* CONFIG_PM_SLEEP */
16483 
16484 /**
16485  * bnxt_io_error_detected - called when PCI error is detected
16486  * @pdev: Pointer to PCI device
16487  * @state: The current pci connection state
16488  *
16489  * This function is called after a PCI bus error affecting
16490  * this device has been detected.
16491  */
16492 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16493 					       pci_channel_state_t state)
16494 {
16495 	struct net_device *netdev = pci_get_drvdata(pdev);
16496 	struct bnxt *bp = netdev_priv(netdev);
16497 	bool abort = false;
16498 
16499 	netdev_info(netdev, "PCI I/O error detected\n");
16500 
16501 	bnxt_ulp_stop(bp);
16502 
16503 	rtnl_lock();
16504 	netif_device_detach(netdev);
16505 
16506 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16507 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16508 		abort = true;
16509 	}
16510 
16511 	if (abort || state == pci_channel_io_perm_failure) {
16512 		rtnl_unlock();
16513 		return PCI_ERS_RESULT_DISCONNECT;
16514 	}
16515 
16516 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16517 	 * so we disable bus master to prevent any potential bad DMAs before
16518 	 * freeing kernel memory.
16519 	 */
16520 	if (state == pci_channel_io_frozen) {
16521 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16522 		bnxt_fw_fatal_close(bp);
16523 	}
16524 
16525 	if (netif_running(netdev))
16526 		__bnxt_close_nic(bp, true, true);
16527 
16528 	if (pci_is_enabled(pdev))
16529 		pci_disable_device(pdev);
16530 	bnxt_free_ctx_mem(bp, false);
16531 	rtnl_unlock();
16532 
16533 	/* Request a slot slot reset. */
16534 	return PCI_ERS_RESULT_NEED_RESET;
16535 }
16536 
16537 /**
16538  * bnxt_io_slot_reset - called after the pci bus has been reset.
16539  * @pdev: Pointer to PCI device
16540  *
16541  * Restart the card from scratch, as if from a cold-boot.
16542  * At this point, the card has experienced a hard reset,
16543  * followed by fixups by BIOS, and has its config space
16544  * set up identically to what it was at cold boot.
16545  */
16546 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16547 {
16548 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16549 	struct net_device *netdev = pci_get_drvdata(pdev);
16550 	struct bnxt *bp = netdev_priv(netdev);
16551 	int retry = 0;
16552 	int err = 0;
16553 	int off;
16554 
16555 	netdev_info(bp->dev, "PCI Slot Reset\n");
16556 
16557 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16558 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16559 		msleep(900);
16560 
16561 	rtnl_lock();
16562 
16563 	if (pci_enable_device(pdev)) {
16564 		dev_err(&pdev->dev,
16565 			"Cannot re-enable PCI device after reset.\n");
16566 	} else {
16567 		pci_set_master(pdev);
16568 		/* Upon fatal error, our device internal logic that latches to
16569 		 * BAR value is getting reset and will restore only upon
16570 		 * rewriting the BARs.
16571 		 *
16572 		 * As pci_restore_state() does not re-write the BARs if the
16573 		 * value is same as saved value earlier, driver needs to
16574 		 * write the BARs to 0 to force restore, in case of fatal error.
16575 		 */
16576 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16577 				       &bp->state)) {
16578 			for (off = PCI_BASE_ADDRESS_0;
16579 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16580 				pci_write_config_dword(bp->pdev, off, 0);
16581 		}
16582 		pci_restore_state(pdev);
16583 		pci_save_state(pdev);
16584 
16585 		bnxt_inv_fw_health_reg(bp);
16586 		bnxt_try_map_fw_health_reg(bp);
16587 
16588 		/* In some PCIe AER scenarios, firmware may take up to
16589 		 * 10 seconds to become ready in the worst case.
16590 		 */
16591 		do {
16592 			err = bnxt_try_recover_fw(bp);
16593 			if (!err)
16594 				break;
16595 			retry++;
16596 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16597 
16598 		if (err) {
16599 			dev_err(&pdev->dev, "Firmware not ready\n");
16600 			goto reset_exit;
16601 		}
16602 
16603 		err = bnxt_hwrm_func_reset(bp);
16604 		if (!err)
16605 			result = PCI_ERS_RESULT_RECOVERED;
16606 
16607 		bnxt_ulp_irq_stop(bp);
16608 		bnxt_clear_int_mode(bp);
16609 		err = bnxt_init_int_mode(bp);
16610 		bnxt_ulp_irq_restart(bp, err);
16611 	}
16612 
16613 reset_exit:
16614 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16615 	bnxt_clear_reservations(bp, true);
16616 	rtnl_unlock();
16617 
16618 	return result;
16619 }
16620 
16621 /**
16622  * bnxt_io_resume - called when traffic can start flowing again.
16623  * @pdev: Pointer to PCI device
16624  *
16625  * This callback is called when the error recovery driver tells
16626  * us that its OK to resume normal operation.
16627  */
16628 static void bnxt_io_resume(struct pci_dev *pdev)
16629 {
16630 	struct net_device *netdev = pci_get_drvdata(pdev);
16631 	struct bnxt *bp = netdev_priv(netdev);
16632 	int err;
16633 
16634 	netdev_info(bp->dev, "PCI Slot Resume\n");
16635 	rtnl_lock();
16636 
16637 	err = bnxt_hwrm_func_qcaps(bp);
16638 	if (!err) {
16639 		if (netif_running(netdev))
16640 			err = bnxt_open(netdev);
16641 		else
16642 			err = bnxt_reserve_rings(bp, true);
16643 	}
16644 
16645 	if (!err)
16646 		netif_device_attach(netdev);
16647 
16648 	rtnl_unlock();
16649 	bnxt_ulp_start(bp, err);
16650 	if (!err)
16651 		bnxt_reenable_sriov(bp);
16652 }
16653 
16654 static const struct pci_error_handlers bnxt_err_handler = {
16655 	.error_detected	= bnxt_io_error_detected,
16656 	.slot_reset	= bnxt_io_slot_reset,
16657 	.resume		= bnxt_io_resume
16658 };
16659 
16660 static struct pci_driver bnxt_pci_driver = {
16661 	.name		= DRV_MODULE_NAME,
16662 	.id_table	= bnxt_pci_tbl,
16663 	.probe		= bnxt_init_one,
16664 	.remove		= bnxt_remove_one,
16665 	.shutdown	= bnxt_shutdown,
16666 	.driver.pm	= BNXT_PM_OPS,
16667 	.err_handler	= &bnxt_err_handler,
16668 #if defined(CONFIG_BNXT_SRIOV)
16669 	.sriov_configure = bnxt_sriov_configure,
16670 #endif
16671 };
16672 
16673 static int __init bnxt_init(void)
16674 {
16675 	int err;
16676 
16677 	bnxt_debug_init();
16678 	err = pci_register_driver(&bnxt_pci_driver);
16679 	if (err) {
16680 		bnxt_debug_exit();
16681 		return err;
16682 	}
16683 
16684 	return 0;
16685 }
16686 
16687 static void __exit bnxt_exit(void)
16688 {
16689 	pci_unregister_driver(&bnxt_pci_driver);
16690 	if (bnxt_pf_wq)
16691 		destroy_workqueue(bnxt_pf_wq);
16692 	bnxt_debug_exit();
16693 }
16694 
16695 module_init(bnxt_init);
16696 module_exit(bnxt_exit);
16697