xref: /linux/drivers/net/ethernet/broadcom/bnxt/bnxt.c (revision a634dda26186cf9a51567020fcce52bcba5e1e59)
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 #define BNXT_RX_COPY_THRESH 256
85 
86 #define BNXT_TX_PUSH_THRESH 164
87 
88 /* indexed by enum board_idx */
89 static const struct {
90 	char *name;
91 } board_info[] = {
92 	[BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
93 	[BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
94 	[BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
95 	[BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
96 	[BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
97 	[BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
98 	[BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
99 	[BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
100 	[BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
101 	[BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
102 	[BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
103 	[BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
104 	[BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
105 	[BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
106 	[BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
107 	[BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
108 	[BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
109 	[BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
110 	[BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
111 	[BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
112 	[BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
113 	[BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
114 	[BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
115 	[BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
116 	[BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
117 	[BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
118 	[BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
119 	[BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
120 	[BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
121 	[BCM57508] = { "Broadcom BCM57508 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
122 	[BCM57504] = { "Broadcom BCM57504 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
123 	[BCM57502] = { "Broadcom BCM57502 NetXtreme-E 10Gb/25Gb/50Gb Ethernet" },
124 	[BCM57608] = { "Broadcom BCM57608 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
125 	[BCM57604] = { "Broadcom BCM57604 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb Ethernet" },
126 	[BCM57602] = { "Broadcom BCM57602 NetXtreme-E 10Gb/25Gb/50Gb/100Gb Ethernet" },
127 	[BCM57601] = { "Broadcom BCM57601 NetXtreme-E 10Gb/25Gb/50Gb/100Gb/200Gb/400Gb Ethernet" },
128 	[BCM57508_NPAR] = { "Broadcom BCM57508 NetXtreme-E Ethernet Partition" },
129 	[BCM57504_NPAR] = { "Broadcom BCM57504 NetXtreme-E Ethernet Partition" },
130 	[BCM57502_NPAR] = { "Broadcom BCM57502 NetXtreme-E Ethernet Partition" },
131 	[BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
132 	[BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
133 	[BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
134 	[NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
135 	[NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
136 	[NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
137 	[NETXTREME_C_VF_HV] = { "Broadcom NetXtreme-C Virtual Function for Hyper-V" },
138 	[NETXTREME_E_VF_HV] = { "Broadcom NetXtreme-E Virtual Function for Hyper-V" },
139 	[NETXTREME_E_P5_VF] = { "Broadcom BCM5750X NetXtreme-E Ethernet Virtual Function" },
140 	[NETXTREME_E_P5_VF_HV] = { "Broadcom BCM5750X NetXtreme-E Virtual Function for Hyper-V" },
141 	[NETXTREME_E_P7_VF] = { "Broadcom BCM5760X Virtual Function" },
142 };
143 
144 static const struct pci_device_id bnxt_pci_tbl[] = {
145 	{ PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
146 	{ PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
147 	{ PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
148 	{ PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
149 	{ PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
150 	{ PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
151 	{ PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
152 	{ PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
153 	{ PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
154 	{ PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
155 	{ PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
156 	{ PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
157 	{ PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
158 	{ PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
159 	{ PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
160 	{ PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
161 	{ PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
162 	{ PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
163 	{ PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
164 	{ PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
165 	{ PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
166 	{ PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
167 	{ PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
168 	{ PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
169 	{ PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
170 	{ PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
171 	{ PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
172 	{ PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
173 	{ PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
174 	{ PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
175 	{ PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
176 	{ PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
177 	{ PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
178 	{ PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
179 	{ PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
180 	{ PCI_VDEVICE(BROADCOM, 0x1750), .driver_data = BCM57508 },
181 	{ PCI_VDEVICE(BROADCOM, 0x1751), .driver_data = BCM57504 },
182 	{ PCI_VDEVICE(BROADCOM, 0x1752), .driver_data = BCM57502 },
183 	{ PCI_VDEVICE(BROADCOM, 0x1760), .driver_data = BCM57608 },
184 	{ PCI_VDEVICE(BROADCOM, 0x1761), .driver_data = BCM57604 },
185 	{ PCI_VDEVICE(BROADCOM, 0x1762), .driver_data = BCM57602 },
186 	{ PCI_VDEVICE(BROADCOM, 0x1763), .driver_data = BCM57601 },
187 	{ PCI_VDEVICE(BROADCOM, 0x1800), .driver_data = BCM57502_NPAR },
188 	{ PCI_VDEVICE(BROADCOM, 0x1801), .driver_data = BCM57504_NPAR },
189 	{ PCI_VDEVICE(BROADCOM, 0x1802), .driver_data = BCM57508_NPAR },
190 	{ PCI_VDEVICE(BROADCOM, 0x1803), .driver_data = BCM57502_NPAR },
191 	{ PCI_VDEVICE(BROADCOM, 0x1804), .driver_data = BCM57504_NPAR },
192 	{ PCI_VDEVICE(BROADCOM, 0x1805), .driver_data = BCM57508_NPAR },
193 	{ PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
194 	{ PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
195 #ifdef CONFIG_BNXT_SRIOV
196 	{ PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
197 	{ PCI_VDEVICE(BROADCOM, 0x1607), .driver_data = NETXTREME_E_VF_HV },
198 	{ PCI_VDEVICE(BROADCOM, 0x1608), .driver_data = NETXTREME_E_VF_HV },
199 	{ PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
200 	{ PCI_VDEVICE(BROADCOM, 0x16bd), .driver_data = NETXTREME_E_VF_HV },
201 	{ PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
202 	{ PCI_VDEVICE(BROADCOM, 0x16c2), .driver_data = NETXTREME_C_VF_HV },
203 	{ PCI_VDEVICE(BROADCOM, 0x16c3), .driver_data = NETXTREME_C_VF_HV },
204 	{ PCI_VDEVICE(BROADCOM, 0x16c4), .driver_data = NETXTREME_E_VF_HV },
205 	{ PCI_VDEVICE(BROADCOM, 0x16c5), .driver_data = NETXTREME_E_VF_HV },
206 	{ PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
207 	{ PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
208 	{ PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
209 	{ PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
210 	{ PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
211 	{ PCI_VDEVICE(BROADCOM, 0x16e6), .driver_data = NETXTREME_C_VF_HV },
212 	{ PCI_VDEVICE(BROADCOM, 0x1806), .driver_data = NETXTREME_E_P5_VF },
213 	{ PCI_VDEVICE(BROADCOM, 0x1807), .driver_data = NETXTREME_E_P5_VF },
214 	{ PCI_VDEVICE(BROADCOM, 0x1808), .driver_data = NETXTREME_E_P5_VF_HV },
215 	{ PCI_VDEVICE(BROADCOM, 0x1809), .driver_data = NETXTREME_E_P5_VF_HV },
216 	{ PCI_VDEVICE(BROADCOM, 0x1819), .driver_data = NETXTREME_E_P7_VF },
217 	{ PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
218 #endif
219 	{ 0 }
220 };
221 
222 MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
223 
224 static const u16 bnxt_vf_req_snif[] = {
225 	HWRM_FUNC_CFG,
226 	HWRM_FUNC_VF_CFG,
227 	HWRM_PORT_PHY_QCFG,
228 	HWRM_CFA_L2_FILTER_ALLOC,
229 };
230 
231 static const u16 bnxt_async_events_arr[] = {
232 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
233 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE,
234 	ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
235 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
236 	ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
237 	ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
238 	ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE,
239 	ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY,
240 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY,
241 	ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION,
242 	ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE,
243 	ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG,
244 	ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST,
245 	ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP,
246 	ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT,
247 	ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE,
248 	ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER,
249 };
250 
251 const u16 bnxt_bstore_to_trace[] = {
252 	[BNXT_CTX_SRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT_TRACE,
253 	[BNXT_CTX_SRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_SRT2_TRACE,
254 	[BNXT_CTX_CRT]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT_TRACE,
255 	[BNXT_CTX_CRT2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CRT2_TRACE,
256 	[BNXT_CTX_RIGP0]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP0_TRACE,
257 	[BNXT_CTX_L2HWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_L2_HWRM_TRACE,
258 	[BNXT_CTX_REHWRM]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_ROCE_HWRM_TRACE,
259 	[BNXT_CTX_CA0]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA0_TRACE,
260 	[BNXT_CTX_CA1]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA1_TRACE,
261 	[BNXT_CTX_CA2]		= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_CA2_TRACE,
262 	[BNXT_CTX_RIGP1]	= DBG_LOG_BUFFER_FLUSH_REQ_TYPE_RIGP1_TRACE,
263 };
264 
265 static struct workqueue_struct *bnxt_pf_wq;
266 
267 #define BNXT_IPV6_MASK_ALL {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
268 			       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}}
269 #define BNXT_IPV6_MASK_NONE {{{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}}
270 
271 const struct bnxt_flow_masks BNXT_FLOW_MASK_NONE = {
272 	.ports = {
273 		.src = 0,
274 		.dst = 0,
275 	},
276 	.addrs = {
277 		.v6addrs = {
278 			.src = BNXT_IPV6_MASK_NONE,
279 			.dst = BNXT_IPV6_MASK_NONE,
280 		},
281 	},
282 };
283 
284 const struct bnxt_flow_masks BNXT_FLOW_IPV6_MASK_ALL = {
285 	.ports = {
286 		.src = cpu_to_be16(0xffff),
287 		.dst = cpu_to_be16(0xffff),
288 	},
289 	.addrs = {
290 		.v6addrs = {
291 			.src = BNXT_IPV6_MASK_ALL,
292 			.dst = BNXT_IPV6_MASK_ALL,
293 		},
294 	},
295 };
296 
297 const struct bnxt_flow_masks BNXT_FLOW_IPV4_MASK_ALL = {
298 	.ports = {
299 		.src = cpu_to_be16(0xffff),
300 		.dst = cpu_to_be16(0xffff),
301 	},
302 	.addrs = {
303 		.v4addrs = {
304 			.src = cpu_to_be32(0xffffffff),
305 			.dst = cpu_to_be32(0xffffffff),
306 		},
307 	},
308 };
309 
310 static bool bnxt_vf_pciid(enum board_idx idx)
311 {
312 	return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
313 		idx == NETXTREME_S_VF || idx == NETXTREME_C_VF_HV ||
314 		idx == NETXTREME_E_VF_HV || idx == NETXTREME_E_P5_VF ||
315 		idx == NETXTREME_E_P5_VF_HV || idx == NETXTREME_E_P7_VF);
316 }
317 
318 #define DB_CP_REARM_FLAGS	(DB_KEY_CP | DB_IDX_VALID)
319 #define DB_CP_FLAGS		(DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
320 
321 #define BNXT_DB_CQ(db, idx)						\
322 	writel(DB_CP_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
323 
324 #define BNXT_DB_NQ_P5(db, idx)						\
325 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ | DB_RING_IDX(db, idx),\
326 		    (db)->doorbell)
327 
328 #define BNXT_DB_NQ_P7(db, idx)						\
329 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_MASK |		\
330 		    DB_RING_IDX(db, idx), (db)->doorbell)
331 
332 #define BNXT_DB_CQ_ARM(db, idx)						\
333 	writel(DB_CP_REARM_FLAGS | DB_RING_IDX(db, idx), (db)->doorbell)
334 
335 #define BNXT_DB_NQ_ARM_P5(db, idx)					\
336 	bnxt_writeq(bp, (db)->db_key64 | DBR_TYPE_NQ_ARM |		\
337 		    DB_RING_IDX(db, idx), (db)->doorbell)
338 
339 static void bnxt_db_nq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
340 {
341 	if (bp->flags & BNXT_FLAG_CHIP_P7)
342 		BNXT_DB_NQ_P7(db, idx);
343 	else if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
344 		BNXT_DB_NQ_P5(db, idx);
345 	else
346 		BNXT_DB_CQ(db, idx);
347 }
348 
349 static void bnxt_db_nq_arm(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
350 {
351 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
352 		BNXT_DB_NQ_ARM_P5(db, idx);
353 	else
354 		BNXT_DB_CQ_ARM(db, idx);
355 }
356 
357 static void bnxt_db_cq(struct bnxt *bp, struct bnxt_db_info *db, u32 idx)
358 {
359 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
360 		bnxt_writeq(bp, db->db_key64 | DBR_TYPE_CQ_ARMALL |
361 			    DB_RING_IDX(db, idx), db->doorbell);
362 	else
363 		BNXT_DB_CQ(db, idx);
364 }
365 
366 static void bnxt_queue_fw_reset_work(struct bnxt *bp, unsigned long delay)
367 {
368 	if (!(test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)))
369 		return;
370 
371 	if (BNXT_PF(bp))
372 		queue_delayed_work(bnxt_pf_wq, &bp->fw_reset_task, delay);
373 	else
374 		schedule_delayed_work(&bp->fw_reset_task, delay);
375 }
376 
377 static void __bnxt_queue_sp_work(struct bnxt *bp)
378 {
379 	if (BNXT_PF(bp))
380 		queue_work(bnxt_pf_wq, &bp->sp_task);
381 	else
382 		schedule_work(&bp->sp_task);
383 }
384 
385 static void bnxt_queue_sp_work(struct bnxt *bp, unsigned int event)
386 {
387 	set_bit(event, &bp->sp_event);
388 	__bnxt_queue_sp_work(bp);
389 }
390 
391 static void bnxt_sched_reset_rxr(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
392 {
393 	if (!rxr->bnapi->in_reset) {
394 		rxr->bnapi->in_reset = true;
395 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
396 			set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
397 		else
398 			set_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event);
399 		__bnxt_queue_sp_work(bp);
400 	}
401 	rxr->rx_next_cons = 0xffff;
402 }
403 
404 void bnxt_sched_reset_txr(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
405 			  u16 curr)
406 {
407 	struct bnxt_napi *bnapi = txr->bnapi;
408 
409 	if (bnapi->tx_fault)
410 		return;
411 
412 	netdev_err(bp->dev, "Invalid Tx completion (ring:%d tx_hw_cons:%u cons:%u prod:%u curr:%u)",
413 		   txr->txq_index, txr->tx_hw_cons,
414 		   txr->tx_cons, txr->tx_prod, curr);
415 	WARN_ON_ONCE(1);
416 	bnapi->tx_fault = 1;
417 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
418 }
419 
420 const u16 bnxt_lhint_arr[] = {
421 	TX_BD_FLAGS_LHINT_512_AND_SMALLER,
422 	TX_BD_FLAGS_LHINT_512_TO_1023,
423 	TX_BD_FLAGS_LHINT_1024_TO_2047,
424 	TX_BD_FLAGS_LHINT_1024_TO_2047,
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 	TX_BD_FLAGS_LHINT_2048_AND_LARGER,
440 };
441 
442 static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
443 {
444 	struct metadata_dst *md_dst = skb_metadata_dst(skb);
445 
446 	if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
447 		return 0;
448 
449 	return md_dst->u.port_info.port_id;
450 }
451 
452 static void bnxt_txr_db_kick(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
453 			     u16 prod)
454 {
455 	/* Sync BD data before updating doorbell */
456 	wmb();
457 	bnxt_db_write(bp, &txr->tx_db, prod);
458 	txr->kick_pending = 0;
459 }
460 
461 static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
462 {
463 	struct bnxt *bp = netdev_priv(dev);
464 	struct tx_bd *txbd, *txbd0;
465 	struct tx_bd_ext *txbd1;
466 	struct netdev_queue *txq;
467 	int i;
468 	dma_addr_t mapping;
469 	unsigned int length, pad = 0;
470 	u32 len, free_size, vlan_tag_flags, cfa_action, flags;
471 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
472 	struct pci_dev *pdev = bp->pdev;
473 	u16 prod, last_frag, txts_prod;
474 	struct bnxt_tx_ring_info *txr;
475 	struct bnxt_sw_tx_bd *tx_buf;
476 	__le32 lflags = 0;
477 
478 	i = skb_get_queue_mapping(skb);
479 	if (unlikely(i >= bp->tx_nr_rings)) {
480 		dev_kfree_skb_any(skb);
481 		dev_core_stats_tx_dropped_inc(dev);
482 		return NETDEV_TX_OK;
483 	}
484 
485 	txq = netdev_get_tx_queue(dev, i);
486 	txr = &bp->tx_ring[bp->tx_ring_map[i]];
487 	prod = txr->tx_prod;
488 
489 	free_size = bnxt_tx_avail(bp, txr);
490 	if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
491 		/* We must have raced with NAPI cleanup */
492 		if (net_ratelimit() && txr->kick_pending)
493 			netif_warn(bp, tx_err, dev,
494 				   "bnxt: ring busy w/ flush pending!\n");
495 		if (!netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
496 					bp->tx_wake_thresh))
497 			return NETDEV_TX_BUSY;
498 	}
499 
500 	if (unlikely(ipv6_hopopt_jumbo_remove(skb)))
501 		goto tx_free;
502 
503 	length = skb->len;
504 	len = skb_headlen(skb);
505 	last_frag = skb_shinfo(skb)->nr_frags;
506 
507 	txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
508 
509 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
510 	tx_buf->skb = skb;
511 	tx_buf->nr_frags = last_frag;
512 
513 	vlan_tag_flags = 0;
514 	cfa_action = bnxt_xmit_get_cfa_action(skb);
515 	if (skb_vlan_tag_present(skb)) {
516 		vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
517 				 skb_vlan_tag_get(skb);
518 		/* Currently supports 8021Q, 8021AD vlan offloads
519 		 * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
520 		 */
521 		if (skb->vlan_proto == htons(ETH_P_8021Q))
522 			vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
523 	}
524 
525 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && ptp &&
526 	    ptp->tx_tstamp_en) {
527 		if (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) {
528 			lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
529 			tx_buf->is_ts_pkt = 1;
530 			skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
531 		} else if (!skb_is_gso(skb)) {
532 			u16 seq_id, hdr_off;
533 
534 			if (!bnxt_ptp_parse(skb, &seq_id, &hdr_off) &&
535 			    !bnxt_ptp_get_txts_prod(ptp, &txts_prod)) {
536 				if (vlan_tag_flags)
537 					hdr_off += VLAN_HLEN;
538 				lflags |= cpu_to_le32(TX_BD_FLAGS_STAMP);
539 				tx_buf->is_ts_pkt = 1;
540 				skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
541 
542 				ptp->txts_req[txts_prod].tx_seqid = seq_id;
543 				ptp->txts_req[txts_prod].tx_hdr_off = hdr_off;
544 				tx_buf->txts_prod = txts_prod;
545 			}
546 		}
547 	}
548 	if (unlikely(skb->no_fcs))
549 		lflags |= cpu_to_le32(TX_BD_FLAGS_NO_CRC);
550 
551 	if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh &&
552 	    !lflags) {
553 		struct tx_push_buffer *tx_push_buf = txr->tx_push;
554 		struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
555 		struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
556 		void __iomem *db = txr->tx_db.doorbell;
557 		void *pdata = tx_push_buf->data;
558 		u64 *end;
559 		int j, push_len;
560 
561 		/* Set COAL_NOW to be ready quickly for the next push */
562 		tx_push->tx_bd_len_flags_type =
563 			cpu_to_le32((length << TX_BD_LEN_SHIFT) |
564 					TX_BD_TYPE_LONG_TX_BD |
565 					TX_BD_FLAGS_LHINT_512_AND_SMALLER |
566 					TX_BD_FLAGS_COAL_NOW |
567 					TX_BD_FLAGS_PACKET_END |
568 					(2 << TX_BD_FLAGS_BD_CNT_SHIFT));
569 
570 		if (skb->ip_summed == CHECKSUM_PARTIAL)
571 			tx_push1->tx_bd_hsize_lflags =
572 					cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
573 		else
574 			tx_push1->tx_bd_hsize_lflags = 0;
575 
576 		tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
577 		tx_push1->tx_bd_cfa_action =
578 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
579 
580 		end = pdata + length;
581 		end = PTR_ALIGN(end, 8) - 1;
582 		*end = 0;
583 
584 		skb_copy_from_linear_data(skb, pdata, len);
585 		pdata += len;
586 		for (j = 0; j < last_frag; j++) {
587 			skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
588 			void *fptr;
589 
590 			fptr = skb_frag_address_safe(frag);
591 			if (!fptr)
592 				goto normal_tx;
593 
594 			memcpy(pdata, fptr, skb_frag_size(frag));
595 			pdata += skb_frag_size(frag);
596 		}
597 
598 		txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
599 		txbd->tx_bd_haddr = txr->data_mapping;
600 		txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2);
601 		prod = NEXT_TX(prod);
602 		tx_push->tx_bd_opaque = txbd->tx_bd_opaque;
603 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
604 		memcpy(txbd, tx_push1, sizeof(*txbd));
605 		prod = NEXT_TX(prod);
606 		tx_push->doorbell =
607 			cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH |
608 				    DB_RING_IDX(&txr->tx_db, prod));
609 		WRITE_ONCE(txr->tx_prod, prod);
610 
611 		tx_buf->is_push = 1;
612 		netdev_tx_sent_queue(txq, skb->len);
613 		wmb();	/* Sync is_push and byte queue before pushing data */
614 
615 		push_len = (length + sizeof(*tx_push) + 7) / 8;
616 		if (push_len > 16) {
617 			__iowrite64_copy(db, tx_push_buf, 16);
618 			__iowrite32_copy(db + 4, tx_push_buf + 1,
619 					 (push_len - 16) << 1);
620 		} else {
621 			__iowrite64_copy(db, tx_push_buf, push_len);
622 		}
623 
624 		goto tx_done;
625 	}
626 
627 normal_tx:
628 	if (length < BNXT_MIN_PKT_SIZE) {
629 		pad = BNXT_MIN_PKT_SIZE - length;
630 		if (skb_pad(skb, pad))
631 			/* SKB already freed. */
632 			goto tx_kick_pending;
633 		length = BNXT_MIN_PKT_SIZE;
634 	}
635 
636 	mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
637 
638 	if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
639 		goto tx_free;
640 
641 	dma_unmap_addr_set(tx_buf, mapping, mapping);
642 	flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
643 		((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
644 
645 	txbd->tx_bd_haddr = cpu_to_le64(mapping);
646 	txbd->tx_bd_opaque = SET_TX_OPAQUE(bp, txr, prod, 2 + last_frag);
647 
648 	prod = NEXT_TX(prod);
649 	txbd1 = (struct tx_bd_ext *)
650 		&txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
651 
652 	txbd1->tx_bd_hsize_lflags = lflags;
653 	if (skb_is_gso(skb)) {
654 		bool udp_gso = !!(skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4);
655 		u32 hdr_len;
656 
657 		if (skb->encapsulation) {
658 			if (udp_gso)
659 				hdr_len = skb_inner_transport_offset(skb) +
660 					  sizeof(struct udphdr);
661 			else
662 				hdr_len = skb_inner_tcp_all_headers(skb);
663 		} else if (udp_gso) {
664 			hdr_len = skb_transport_offset(skb) +
665 				  sizeof(struct udphdr);
666 		} else {
667 			hdr_len = skb_tcp_all_headers(skb);
668 		}
669 
670 		txbd1->tx_bd_hsize_lflags |= cpu_to_le32(TX_BD_FLAGS_LSO |
671 					TX_BD_FLAGS_T_IPID |
672 					(hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
673 		length = skb_shinfo(skb)->gso_size;
674 		txbd1->tx_bd_mss = cpu_to_le32(length);
675 		length += hdr_len;
676 	} else if (skb->ip_summed == CHECKSUM_PARTIAL) {
677 		txbd1->tx_bd_hsize_lflags |=
678 			cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
679 		txbd1->tx_bd_mss = 0;
680 	}
681 
682 	length >>= 9;
683 	if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
684 		dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
685 				     skb->len);
686 		i = 0;
687 		goto tx_dma_error;
688 	}
689 	flags |= bnxt_lhint_arr[length];
690 	txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
691 
692 	txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
693 	txbd1->tx_bd_cfa_action =
694 			cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
695 	txbd0 = txbd;
696 	for (i = 0; i < last_frag; i++) {
697 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
698 
699 		prod = NEXT_TX(prod);
700 		txbd = &txr->tx_desc_ring[TX_RING(bp, prod)][TX_IDX(prod)];
701 
702 		len = skb_frag_size(frag);
703 		mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
704 					   DMA_TO_DEVICE);
705 
706 		if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
707 			goto tx_dma_error;
708 
709 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
710 		dma_unmap_addr_set(tx_buf, mapping, mapping);
711 
712 		txbd->tx_bd_haddr = cpu_to_le64(mapping);
713 
714 		flags = len << TX_BD_LEN_SHIFT;
715 		txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
716 	}
717 
718 	flags &= ~TX_BD_LEN;
719 	txbd->tx_bd_len_flags_type =
720 		cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
721 			    TX_BD_FLAGS_PACKET_END);
722 
723 	netdev_tx_sent_queue(txq, skb->len);
724 
725 	skb_tx_timestamp(skb);
726 
727 	prod = NEXT_TX(prod);
728 	WRITE_ONCE(txr->tx_prod, prod);
729 
730 	if (!netdev_xmit_more() || netif_xmit_stopped(txq)) {
731 		bnxt_txr_db_kick(bp, txr, prod);
732 	} else {
733 		if (free_size >= bp->tx_wake_thresh)
734 			txbd0->tx_bd_len_flags_type |=
735 				cpu_to_le32(TX_BD_FLAGS_NO_CMPL);
736 		txr->kick_pending = 1;
737 	}
738 
739 tx_done:
740 
741 	if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
742 		if (netdev_xmit_more() && !tx_buf->is_push) {
743 			txbd0->tx_bd_len_flags_type &=
744 				cpu_to_le32(~TX_BD_FLAGS_NO_CMPL);
745 			bnxt_txr_db_kick(bp, txr, prod);
746 		}
747 
748 		netif_txq_try_stop(txq, bnxt_tx_avail(bp, txr),
749 				   bp->tx_wake_thresh);
750 	}
751 	return NETDEV_TX_OK;
752 
753 tx_dma_error:
754 	last_frag = i;
755 
756 	/* start back at beginning and unmap skb */
757 	prod = txr->tx_prod;
758 	tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
759 	dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
760 			 skb_headlen(skb), DMA_TO_DEVICE);
761 	prod = NEXT_TX(prod);
762 
763 	/* unmap remaining mapped pages */
764 	for (i = 0; i < last_frag; i++) {
765 		prod = NEXT_TX(prod);
766 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, prod)];
767 		dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
768 			       skb_frag_size(&skb_shinfo(skb)->frags[i]),
769 			       DMA_TO_DEVICE);
770 	}
771 
772 tx_free:
773 	dev_kfree_skb_any(skb);
774 tx_kick_pending:
775 	if (BNXT_TX_PTP_IS_SET(lflags)) {
776 		txr->tx_buf_ring[txr->tx_prod].is_ts_pkt = 0;
777 		atomic64_inc(&bp->ptp_cfg->stats.ts_err);
778 		if (!(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
779 			/* set SKB to err so PTP worker will clean up */
780 			ptp->txts_req[txts_prod].tx_skb = ERR_PTR(-EIO);
781 	}
782 	if (txr->kick_pending)
783 		bnxt_txr_db_kick(bp, txr, txr->tx_prod);
784 	txr->tx_buf_ring[txr->tx_prod].skb = NULL;
785 	dev_core_stats_tx_dropped_inc(dev);
786 	return NETDEV_TX_OK;
787 }
788 
789 /* Returns true if some remaining TX packets not processed. */
790 static bool __bnxt_tx_int(struct bnxt *bp, struct bnxt_tx_ring_info *txr,
791 			  int budget)
792 {
793 	struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
794 	struct pci_dev *pdev = bp->pdev;
795 	u16 hw_cons = txr->tx_hw_cons;
796 	unsigned int tx_bytes = 0;
797 	u16 cons = txr->tx_cons;
798 	int tx_pkts = 0;
799 	bool rc = false;
800 
801 	while (RING_TX(bp, cons) != hw_cons) {
802 		struct bnxt_sw_tx_bd *tx_buf;
803 		struct sk_buff *skb;
804 		bool is_ts_pkt;
805 		int j, last;
806 
807 		tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
808 		skb = tx_buf->skb;
809 
810 		if (unlikely(!skb)) {
811 			bnxt_sched_reset_txr(bp, txr, cons);
812 			return rc;
813 		}
814 
815 		is_ts_pkt = tx_buf->is_ts_pkt;
816 		if (is_ts_pkt && (bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP)) {
817 			rc = true;
818 			break;
819 		}
820 
821 		cons = NEXT_TX(cons);
822 		tx_pkts++;
823 		tx_bytes += skb->len;
824 		tx_buf->skb = NULL;
825 		tx_buf->is_ts_pkt = 0;
826 
827 		if (tx_buf->is_push) {
828 			tx_buf->is_push = 0;
829 			goto next_tx_int;
830 		}
831 
832 		dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
833 				 skb_headlen(skb), DMA_TO_DEVICE);
834 		last = tx_buf->nr_frags;
835 
836 		for (j = 0; j < last; j++) {
837 			cons = NEXT_TX(cons);
838 			tx_buf = &txr->tx_buf_ring[RING_TX(bp, cons)];
839 			dma_unmap_page(
840 				&pdev->dev,
841 				dma_unmap_addr(tx_buf, mapping),
842 				skb_frag_size(&skb_shinfo(skb)->frags[j]),
843 				DMA_TO_DEVICE);
844 		}
845 		if (unlikely(is_ts_pkt)) {
846 			if (BNXT_CHIP_P5(bp)) {
847 				/* PTP worker takes ownership of the skb */
848 				bnxt_get_tx_ts_p5(bp, skb, tx_buf->txts_prod);
849 				skb = NULL;
850 			}
851 		}
852 
853 next_tx_int:
854 		cons = NEXT_TX(cons);
855 
856 		dev_consume_skb_any(skb);
857 	}
858 
859 	WRITE_ONCE(txr->tx_cons, cons);
860 
861 	__netif_txq_completed_wake(txq, tx_pkts, tx_bytes,
862 				   bnxt_tx_avail(bp, txr), bp->tx_wake_thresh,
863 				   READ_ONCE(txr->dev_state) == BNXT_DEV_STATE_CLOSING);
864 
865 	return rc;
866 }
867 
868 static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
869 {
870 	struct bnxt_tx_ring_info *txr;
871 	bool more = false;
872 	int i;
873 
874 	bnxt_for_each_napi_tx(i, bnapi, txr) {
875 		if (txr->tx_hw_cons != RING_TX(bp, txr->tx_cons))
876 			more |= __bnxt_tx_int(bp, txr, budget);
877 	}
878 	if (!more)
879 		bnapi->events &= ~BNXT_TX_CMP_EVENT;
880 }
881 
882 static bool bnxt_separate_head_pool(void)
883 {
884 	return PAGE_SIZE > BNXT_RX_PAGE_SIZE;
885 }
886 
887 static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
888 					 struct bnxt_rx_ring_info *rxr,
889 					 unsigned int *offset,
890 					 gfp_t gfp)
891 {
892 	struct page *page;
893 
894 	if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
895 		page = page_pool_dev_alloc_frag(rxr->page_pool, offset,
896 						BNXT_RX_PAGE_SIZE);
897 	} else {
898 		page = page_pool_dev_alloc_pages(rxr->page_pool);
899 		*offset = 0;
900 	}
901 	if (!page)
902 		return NULL;
903 
904 	*mapping = page_pool_get_dma_addr(page) + *offset;
905 	return page;
906 }
907 
908 static inline u8 *__bnxt_alloc_rx_frag(struct bnxt *bp, dma_addr_t *mapping,
909 				       struct bnxt_rx_ring_info *rxr,
910 				       gfp_t gfp)
911 {
912 	unsigned int offset;
913 	struct page *page;
914 
915 	page = page_pool_alloc_frag(rxr->head_pool, &offset,
916 				    bp->rx_buf_size, gfp);
917 	if (!page)
918 		return NULL;
919 
920 	*mapping = page_pool_get_dma_addr(page) + bp->rx_dma_offset + offset;
921 	return page_address(page) + offset;
922 }
923 
924 int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
925 		       u16 prod, gfp_t gfp)
926 {
927 	struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
928 	struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
929 	dma_addr_t mapping;
930 
931 	if (BNXT_RX_PAGE_MODE(bp)) {
932 		unsigned int offset;
933 		struct page *page =
934 			__bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
935 
936 		if (!page)
937 			return -ENOMEM;
938 
939 		mapping += bp->rx_dma_offset;
940 		rx_buf->data = page;
941 		rx_buf->data_ptr = page_address(page) + offset + bp->rx_offset;
942 	} else {
943 		u8 *data = __bnxt_alloc_rx_frag(bp, &mapping, rxr, gfp);
944 
945 		if (!data)
946 			return -ENOMEM;
947 
948 		rx_buf->data = data;
949 		rx_buf->data_ptr = data + bp->rx_offset;
950 	}
951 	rx_buf->mapping = mapping;
952 
953 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
954 	return 0;
955 }
956 
957 void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
958 {
959 	u16 prod = rxr->rx_prod;
960 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
961 	struct bnxt *bp = rxr->bnapi->bp;
962 	struct rx_bd *cons_bd, *prod_bd;
963 
964 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
965 	cons_rx_buf = &rxr->rx_buf_ring[cons];
966 
967 	prod_rx_buf->data = data;
968 	prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
969 
970 	prod_rx_buf->mapping = cons_rx_buf->mapping;
971 
972 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
973 	cons_bd = &rxr->rx_desc_ring[RX_RING(bp, cons)][RX_IDX(cons)];
974 
975 	prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
976 }
977 
978 static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
979 {
980 	u16 next, max = rxr->rx_agg_bmap_size;
981 
982 	next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
983 	if (next >= max)
984 		next = find_first_zero_bit(rxr->rx_agg_bmap, max);
985 	return next;
986 }
987 
988 static inline int bnxt_alloc_rx_page(struct bnxt *bp,
989 				     struct bnxt_rx_ring_info *rxr,
990 				     u16 prod, gfp_t gfp)
991 {
992 	struct rx_bd *rxbd =
993 		&rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
994 	struct bnxt_sw_rx_agg_bd *rx_agg_buf;
995 	struct page *page;
996 	dma_addr_t mapping;
997 	u16 sw_prod = rxr->rx_sw_agg_prod;
998 	unsigned int offset = 0;
999 
1000 	page = __bnxt_alloc_rx_page(bp, &mapping, rxr, &offset, gfp);
1001 
1002 	if (!page)
1003 		return -ENOMEM;
1004 
1005 	if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1006 		sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1007 
1008 	__set_bit(sw_prod, rxr->rx_agg_bmap);
1009 	rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
1010 	rxr->rx_sw_agg_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1011 
1012 	rx_agg_buf->page = page;
1013 	rx_agg_buf->offset = offset;
1014 	rx_agg_buf->mapping = mapping;
1015 	rxbd->rx_bd_haddr = cpu_to_le64(mapping);
1016 	rxbd->rx_bd_opaque = sw_prod;
1017 	return 0;
1018 }
1019 
1020 static struct rx_agg_cmp *bnxt_get_agg(struct bnxt *bp,
1021 				       struct bnxt_cp_ring_info *cpr,
1022 				       u16 cp_cons, u16 curr)
1023 {
1024 	struct rx_agg_cmp *agg;
1025 
1026 	cp_cons = RING_CMP(ADV_RAW_CMP(cp_cons, curr));
1027 	agg = (struct rx_agg_cmp *)
1028 		&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
1029 	return agg;
1030 }
1031 
1032 static struct rx_agg_cmp *bnxt_get_tpa_agg_p5(struct bnxt *bp,
1033 					      struct bnxt_rx_ring_info *rxr,
1034 					      u16 agg_id, u16 curr)
1035 {
1036 	struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[agg_id];
1037 
1038 	return &tpa_info->agg_arr[curr];
1039 }
1040 
1041 static void bnxt_reuse_rx_agg_bufs(struct bnxt_cp_ring_info *cpr, u16 idx,
1042 				   u16 start, u32 agg_bufs, bool tpa)
1043 {
1044 	struct bnxt_napi *bnapi = cpr->bnapi;
1045 	struct bnxt *bp = bnapi->bp;
1046 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1047 	u16 prod = rxr->rx_agg_prod;
1048 	u16 sw_prod = rxr->rx_sw_agg_prod;
1049 	bool p5_tpa = false;
1050 	u32 i;
1051 
1052 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1053 		p5_tpa = true;
1054 
1055 	for (i = 0; i < agg_bufs; i++) {
1056 		u16 cons;
1057 		struct rx_agg_cmp *agg;
1058 		struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
1059 		struct rx_bd *prod_bd;
1060 		struct page *page;
1061 
1062 		if (p5_tpa)
1063 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, start + i);
1064 		else
1065 			agg = bnxt_get_agg(bp, cpr, idx, start + i);
1066 		cons = agg->rx_agg_cmp_opaque;
1067 		__clear_bit(cons, rxr->rx_agg_bmap);
1068 
1069 		if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
1070 			sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
1071 
1072 		__set_bit(sw_prod, rxr->rx_agg_bmap);
1073 		prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
1074 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1075 
1076 		/* It is possible for sw_prod to be equal to cons, so
1077 		 * set cons_rx_buf->page to NULL first.
1078 		 */
1079 		page = cons_rx_buf->page;
1080 		cons_rx_buf->page = NULL;
1081 		prod_rx_buf->page = page;
1082 		prod_rx_buf->offset = cons_rx_buf->offset;
1083 
1084 		prod_rx_buf->mapping = cons_rx_buf->mapping;
1085 
1086 		prod_bd = &rxr->rx_agg_desc_ring[RX_AGG_RING(bp, prod)][RX_IDX(prod)];
1087 
1088 		prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
1089 		prod_bd->rx_bd_opaque = sw_prod;
1090 
1091 		prod = NEXT_RX_AGG(prod);
1092 		sw_prod = RING_RX_AGG(bp, NEXT_RX_AGG(sw_prod));
1093 	}
1094 	rxr->rx_agg_prod = prod;
1095 	rxr->rx_sw_agg_prod = sw_prod;
1096 }
1097 
1098 static struct sk_buff *bnxt_rx_multi_page_skb(struct bnxt *bp,
1099 					      struct bnxt_rx_ring_info *rxr,
1100 					      u16 cons, void *data, u8 *data_ptr,
1101 					      dma_addr_t dma_addr,
1102 					      unsigned int offset_and_len)
1103 {
1104 	unsigned int len = offset_and_len & 0xffff;
1105 	struct page *page = data;
1106 	u16 prod = rxr->rx_prod;
1107 	struct sk_buff *skb;
1108 	int err;
1109 
1110 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1111 	if (unlikely(err)) {
1112 		bnxt_reuse_rx_data(rxr, cons, data);
1113 		return NULL;
1114 	}
1115 	dma_addr -= bp->rx_dma_offset;
1116 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1117 				bp->rx_dir);
1118 	skb = napi_build_skb(data_ptr - bp->rx_offset, BNXT_RX_PAGE_SIZE);
1119 	if (!skb) {
1120 		page_pool_recycle_direct(rxr->page_pool, page);
1121 		return NULL;
1122 	}
1123 	skb_mark_for_recycle(skb);
1124 	skb_reserve(skb, bp->rx_offset);
1125 	__skb_put(skb, len);
1126 
1127 	return skb;
1128 }
1129 
1130 static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
1131 					struct bnxt_rx_ring_info *rxr,
1132 					u16 cons, void *data, u8 *data_ptr,
1133 					dma_addr_t dma_addr,
1134 					unsigned int offset_and_len)
1135 {
1136 	unsigned int payload = offset_and_len >> 16;
1137 	unsigned int len = offset_and_len & 0xffff;
1138 	skb_frag_t *frag;
1139 	struct page *page = data;
1140 	u16 prod = rxr->rx_prod;
1141 	struct sk_buff *skb;
1142 	int off, err;
1143 
1144 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1145 	if (unlikely(err)) {
1146 		bnxt_reuse_rx_data(rxr, cons, data);
1147 		return NULL;
1148 	}
1149 	dma_addr -= bp->rx_dma_offset;
1150 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, BNXT_RX_PAGE_SIZE,
1151 				bp->rx_dir);
1152 
1153 	if (unlikely(!payload))
1154 		payload = eth_get_headlen(bp->dev, data_ptr, len);
1155 
1156 	skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
1157 	if (!skb) {
1158 		page_pool_recycle_direct(rxr->page_pool, page);
1159 		return NULL;
1160 	}
1161 
1162 	skb_mark_for_recycle(skb);
1163 	off = (void *)data_ptr - page_address(page);
1164 	skb_add_rx_frag(skb, 0, page, off, len, BNXT_RX_PAGE_SIZE);
1165 	memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
1166 	       payload + NET_IP_ALIGN);
1167 
1168 	frag = &skb_shinfo(skb)->frags[0];
1169 	skb_frag_size_sub(frag, payload);
1170 	skb_frag_off_add(frag, payload);
1171 	skb->data_len -= payload;
1172 	skb->tail += payload;
1173 
1174 	return skb;
1175 }
1176 
1177 static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
1178 				   struct bnxt_rx_ring_info *rxr, u16 cons,
1179 				   void *data, u8 *data_ptr,
1180 				   dma_addr_t dma_addr,
1181 				   unsigned int offset_and_len)
1182 {
1183 	u16 prod = rxr->rx_prod;
1184 	struct sk_buff *skb;
1185 	int err;
1186 
1187 	err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
1188 	if (unlikely(err)) {
1189 		bnxt_reuse_rx_data(rxr, cons, data);
1190 		return NULL;
1191 	}
1192 
1193 	skb = napi_build_skb(data, bp->rx_buf_size);
1194 	dma_sync_single_for_cpu(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
1195 				bp->rx_dir);
1196 	if (!skb) {
1197 		page_pool_free_va(rxr->head_pool, data, true);
1198 		return NULL;
1199 	}
1200 
1201 	skb_mark_for_recycle(skb);
1202 	skb_reserve(skb, bp->rx_offset);
1203 	skb_put(skb, offset_and_len & 0xffff);
1204 	return skb;
1205 }
1206 
1207 static u32 __bnxt_rx_agg_pages(struct bnxt *bp,
1208 			       struct bnxt_cp_ring_info *cpr,
1209 			       struct skb_shared_info *shinfo,
1210 			       u16 idx, u32 agg_bufs, bool tpa,
1211 			       struct xdp_buff *xdp)
1212 {
1213 	struct bnxt_napi *bnapi = cpr->bnapi;
1214 	struct pci_dev *pdev = bp->pdev;
1215 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1216 	u16 prod = rxr->rx_agg_prod;
1217 	u32 i, total_frag_len = 0;
1218 	bool p5_tpa = false;
1219 
1220 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && tpa)
1221 		p5_tpa = true;
1222 
1223 	for (i = 0; i < agg_bufs; i++) {
1224 		skb_frag_t *frag = &shinfo->frags[i];
1225 		u16 cons, frag_len;
1226 		struct rx_agg_cmp *agg;
1227 		struct bnxt_sw_rx_agg_bd *cons_rx_buf;
1228 		struct page *page;
1229 		dma_addr_t mapping;
1230 
1231 		if (p5_tpa)
1232 			agg = bnxt_get_tpa_agg_p5(bp, rxr, idx, i);
1233 		else
1234 			agg = bnxt_get_agg(bp, cpr, idx, i);
1235 		cons = agg->rx_agg_cmp_opaque;
1236 		frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
1237 			    RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
1238 
1239 		cons_rx_buf = &rxr->rx_agg_ring[cons];
1240 		skb_frag_fill_page_desc(frag, cons_rx_buf->page,
1241 					cons_rx_buf->offset, frag_len);
1242 		shinfo->nr_frags = i + 1;
1243 		__clear_bit(cons, rxr->rx_agg_bmap);
1244 
1245 		/* It is possible for bnxt_alloc_rx_page() to allocate
1246 		 * a sw_prod index that equals the cons index, so we
1247 		 * need to clear the cons entry now.
1248 		 */
1249 		mapping = cons_rx_buf->mapping;
1250 		page = cons_rx_buf->page;
1251 		cons_rx_buf->page = NULL;
1252 
1253 		if (xdp && page_is_pfmemalloc(page))
1254 			xdp_buff_set_frag_pfmemalloc(xdp);
1255 
1256 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
1257 			--shinfo->nr_frags;
1258 			cons_rx_buf->page = page;
1259 
1260 			/* Update prod since possibly some pages have been
1261 			 * allocated already.
1262 			 */
1263 			rxr->rx_agg_prod = prod;
1264 			bnxt_reuse_rx_agg_bufs(cpr, idx, i, agg_bufs - i, tpa);
1265 			return 0;
1266 		}
1267 
1268 		dma_sync_single_for_cpu(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
1269 					bp->rx_dir);
1270 
1271 		total_frag_len += frag_len;
1272 		prod = NEXT_RX_AGG(prod);
1273 	}
1274 	rxr->rx_agg_prod = prod;
1275 	return total_frag_len;
1276 }
1277 
1278 static struct sk_buff *bnxt_rx_agg_pages_skb(struct bnxt *bp,
1279 					     struct bnxt_cp_ring_info *cpr,
1280 					     struct sk_buff *skb, u16 idx,
1281 					     u32 agg_bufs, bool tpa)
1282 {
1283 	struct skb_shared_info *shinfo = skb_shinfo(skb);
1284 	u32 total_frag_len = 0;
1285 
1286 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo, idx,
1287 					     agg_bufs, tpa, NULL);
1288 	if (!total_frag_len) {
1289 		skb_mark_for_recycle(skb);
1290 		dev_kfree_skb(skb);
1291 		return NULL;
1292 	}
1293 
1294 	skb->data_len += total_frag_len;
1295 	skb->len += total_frag_len;
1296 	skb->truesize += BNXT_RX_PAGE_SIZE * agg_bufs;
1297 	return skb;
1298 }
1299 
1300 static u32 bnxt_rx_agg_pages_xdp(struct bnxt *bp,
1301 				 struct bnxt_cp_ring_info *cpr,
1302 				 struct xdp_buff *xdp, u16 idx,
1303 				 u32 agg_bufs, bool tpa)
1304 {
1305 	struct skb_shared_info *shinfo = xdp_get_shared_info_from_buff(xdp);
1306 	u32 total_frag_len = 0;
1307 
1308 	if (!xdp_buff_has_frags(xdp))
1309 		shinfo->nr_frags = 0;
1310 
1311 	total_frag_len = __bnxt_rx_agg_pages(bp, cpr, shinfo,
1312 					     idx, agg_bufs, tpa, xdp);
1313 	if (total_frag_len) {
1314 		xdp_buff_set_frags_flag(xdp);
1315 		shinfo->nr_frags = agg_bufs;
1316 		shinfo->xdp_frags_size = total_frag_len;
1317 	}
1318 	return total_frag_len;
1319 }
1320 
1321 static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1322 			       u8 agg_bufs, u32 *raw_cons)
1323 {
1324 	u16 last;
1325 	struct rx_agg_cmp *agg;
1326 
1327 	*raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
1328 	last = RING_CMP(*raw_cons);
1329 	agg = (struct rx_agg_cmp *)
1330 		&cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
1331 	return RX_AGG_CMP_VALID(agg, *raw_cons);
1332 }
1333 
1334 static struct sk_buff *bnxt_copy_data(struct bnxt_napi *bnapi, u8 *data,
1335 				      unsigned int len,
1336 				      dma_addr_t mapping)
1337 {
1338 	struct bnxt *bp = bnapi->bp;
1339 	struct pci_dev *pdev = bp->pdev;
1340 	struct sk_buff *skb;
1341 
1342 	skb = napi_alloc_skb(&bnapi->napi, len);
1343 	if (!skb)
1344 		return NULL;
1345 
1346 	dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
1347 				bp->rx_dir);
1348 
1349 	memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
1350 	       len + NET_IP_ALIGN);
1351 
1352 	dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
1353 				   bp->rx_dir);
1354 
1355 	skb_put(skb, len);
1356 
1357 	return skb;
1358 }
1359 
1360 static struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
1361 				     unsigned int len,
1362 				     dma_addr_t mapping)
1363 {
1364 	return bnxt_copy_data(bnapi, data, len, mapping);
1365 }
1366 
1367 static struct sk_buff *bnxt_copy_xdp(struct bnxt_napi *bnapi,
1368 				     struct xdp_buff *xdp,
1369 				     unsigned int len,
1370 				     dma_addr_t mapping)
1371 {
1372 	unsigned int metasize = 0;
1373 	u8 *data = xdp->data;
1374 	struct sk_buff *skb;
1375 
1376 	len = xdp->data_end - xdp->data_meta;
1377 	metasize = xdp->data - xdp->data_meta;
1378 	data = xdp->data_meta;
1379 
1380 	skb = bnxt_copy_data(bnapi, data, len, mapping);
1381 	if (!skb)
1382 		return skb;
1383 
1384 	if (metasize) {
1385 		skb_metadata_set(skb, metasize);
1386 		__skb_pull(skb, metasize);
1387 	}
1388 
1389 	return skb;
1390 }
1391 
1392 static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
1393 			   u32 *raw_cons, void *cmp)
1394 {
1395 	struct rx_cmp *rxcmp = cmp;
1396 	u32 tmp_raw_cons = *raw_cons;
1397 	u8 cmp_type, agg_bufs = 0;
1398 
1399 	cmp_type = RX_CMP_TYPE(rxcmp);
1400 
1401 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1402 		agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
1403 			    RX_CMP_AGG_BUFS) >>
1404 			   RX_CMP_AGG_BUFS_SHIFT;
1405 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
1406 		struct rx_tpa_end_cmp *tpa_end = cmp;
1407 
1408 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1409 			return 0;
1410 
1411 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1412 	}
1413 
1414 	if (agg_bufs) {
1415 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
1416 			return -EBUSY;
1417 	}
1418 	*raw_cons = tmp_raw_cons;
1419 	return 0;
1420 }
1421 
1422 static u16 bnxt_alloc_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1423 {
1424 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1425 	u16 idx = agg_id & MAX_TPA_P5_MASK;
1426 
1427 	if (test_bit(idx, map->agg_idx_bmap))
1428 		idx = find_first_zero_bit(map->agg_idx_bmap,
1429 					  BNXT_AGG_IDX_BMAP_SIZE);
1430 	__set_bit(idx, map->agg_idx_bmap);
1431 	map->agg_id_tbl[agg_id] = idx;
1432 	return idx;
1433 }
1434 
1435 static void bnxt_free_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
1436 {
1437 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1438 
1439 	__clear_bit(idx, map->agg_idx_bmap);
1440 }
1441 
1442 static u16 bnxt_lookup_agg_idx(struct bnxt_rx_ring_info *rxr, u16 agg_id)
1443 {
1444 	struct bnxt_tpa_idx_map *map = rxr->rx_tpa_idx_map;
1445 
1446 	return map->agg_id_tbl[agg_id];
1447 }
1448 
1449 static void bnxt_tpa_metadata(struct bnxt_tpa_info *tpa_info,
1450 			      struct rx_tpa_start_cmp *tpa_start,
1451 			      struct rx_tpa_start_cmp_ext *tpa_start1)
1452 {
1453 	tpa_info->cfa_code_valid = 1;
1454 	tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
1455 	tpa_info->vlan_valid = 0;
1456 	if (tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) {
1457 		tpa_info->vlan_valid = 1;
1458 		tpa_info->metadata =
1459 			le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
1460 	}
1461 }
1462 
1463 static void bnxt_tpa_metadata_v2(struct bnxt_tpa_info *tpa_info,
1464 				 struct rx_tpa_start_cmp *tpa_start,
1465 				 struct rx_tpa_start_cmp_ext *tpa_start1)
1466 {
1467 	tpa_info->vlan_valid = 0;
1468 	if (TPA_START_VLAN_VALID(tpa_start)) {
1469 		u32 tpid_sel = TPA_START_VLAN_TPID_SEL(tpa_start);
1470 		u32 vlan_proto = ETH_P_8021Q;
1471 
1472 		tpa_info->vlan_valid = 1;
1473 		if (tpid_sel == RX_TPA_START_METADATA1_TPID_8021AD)
1474 			vlan_proto = ETH_P_8021AD;
1475 		tpa_info->metadata = vlan_proto << 16 |
1476 				     TPA_START_METADATA0_TCI(tpa_start1);
1477 	}
1478 }
1479 
1480 static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1481 			   u8 cmp_type, struct rx_tpa_start_cmp *tpa_start,
1482 			   struct rx_tpa_start_cmp_ext *tpa_start1)
1483 {
1484 	struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
1485 	struct bnxt_tpa_info *tpa_info;
1486 	u16 cons, prod, agg_id;
1487 	struct rx_bd *prod_bd;
1488 	dma_addr_t mapping;
1489 
1490 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1491 		agg_id = TPA_START_AGG_ID_P5(tpa_start);
1492 		agg_id = bnxt_alloc_agg_idx(rxr, agg_id);
1493 	} else {
1494 		agg_id = TPA_START_AGG_ID(tpa_start);
1495 	}
1496 	cons = tpa_start->rx_tpa_start_cmp_opaque;
1497 	prod = rxr->rx_prod;
1498 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1499 	prod_rx_buf = &rxr->rx_buf_ring[RING_RX(bp, prod)];
1500 	tpa_info = &rxr->rx_tpa[agg_id];
1501 
1502 	if (unlikely(cons != rxr->rx_next_cons ||
1503 		     TPA_START_ERROR(tpa_start))) {
1504 		netdev_warn(bp->dev, "TPA cons %x, expected cons %x, error code %x\n",
1505 			    cons, rxr->rx_next_cons,
1506 			    TPA_START_ERROR_CODE(tpa_start1));
1507 		bnxt_sched_reset_rxr(bp, rxr);
1508 		return;
1509 	}
1510 	prod_rx_buf->data = tpa_info->data;
1511 	prod_rx_buf->data_ptr = tpa_info->data_ptr;
1512 
1513 	mapping = tpa_info->mapping;
1514 	prod_rx_buf->mapping = mapping;
1515 
1516 	prod_bd = &rxr->rx_desc_ring[RX_RING(bp, prod)][RX_IDX(prod)];
1517 
1518 	prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
1519 
1520 	tpa_info->data = cons_rx_buf->data;
1521 	tpa_info->data_ptr = cons_rx_buf->data_ptr;
1522 	cons_rx_buf->data = NULL;
1523 	tpa_info->mapping = cons_rx_buf->mapping;
1524 
1525 	tpa_info->len =
1526 		le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
1527 				RX_TPA_START_CMP_LEN_SHIFT;
1528 	if (likely(TPA_START_HASH_VALID(tpa_start))) {
1529 		tpa_info->hash_type = PKT_HASH_TYPE_L4;
1530 		tpa_info->gso_type = SKB_GSO_TCPV4;
1531 		if (TPA_START_IS_IPV6(tpa_start1))
1532 			tpa_info->gso_type = SKB_GSO_TCPV6;
1533 		/* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
1534 		else if (!BNXT_CHIP_P4_PLUS(bp) &&
1535 			 TPA_START_HASH_TYPE(tpa_start) == 3)
1536 			tpa_info->gso_type = SKB_GSO_TCPV6;
1537 		tpa_info->rss_hash =
1538 			le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
1539 	} else {
1540 		tpa_info->hash_type = PKT_HASH_TYPE_NONE;
1541 		tpa_info->gso_type = 0;
1542 		netif_warn(bp, rx_err, bp->dev, "TPA packet without valid hash\n");
1543 	}
1544 	tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
1545 	tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
1546 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP)
1547 		bnxt_tpa_metadata(tpa_info, tpa_start, tpa_start1);
1548 	else
1549 		bnxt_tpa_metadata_v2(tpa_info, tpa_start, tpa_start1);
1550 	tpa_info->agg_count = 0;
1551 
1552 	rxr->rx_prod = NEXT_RX(prod);
1553 	cons = RING_RX(bp, NEXT_RX(cons));
1554 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
1555 	cons_rx_buf = &rxr->rx_buf_ring[cons];
1556 
1557 	bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
1558 	rxr->rx_prod = NEXT_RX(rxr->rx_prod);
1559 	cons_rx_buf->data = NULL;
1560 }
1561 
1562 static void bnxt_abort_tpa(struct bnxt_cp_ring_info *cpr, u16 idx, u32 agg_bufs)
1563 {
1564 	if (agg_bufs)
1565 		bnxt_reuse_rx_agg_bufs(cpr, idx, 0, agg_bufs, true);
1566 }
1567 
1568 #ifdef CONFIG_INET
1569 static void bnxt_gro_tunnel(struct sk_buff *skb, __be16 ip_proto)
1570 {
1571 	struct udphdr *uh = NULL;
1572 
1573 	if (ip_proto == htons(ETH_P_IP)) {
1574 		struct iphdr *iph = (struct iphdr *)skb->data;
1575 
1576 		if (iph->protocol == IPPROTO_UDP)
1577 			uh = (struct udphdr *)(iph + 1);
1578 	} else {
1579 		struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
1580 
1581 		if (iph->nexthdr == IPPROTO_UDP)
1582 			uh = (struct udphdr *)(iph + 1);
1583 	}
1584 	if (uh) {
1585 		if (uh->check)
1586 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1587 		else
1588 			skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
1589 	}
1590 }
1591 #endif
1592 
1593 static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
1594 					   int payload_off, int tcp_ts,
1595 					   struct sk_buff *skb)
1596 {
1597 #ifdef CONFIG_INET
1598 	struct tcphdr *th;
1599 	int len, nw_off;
1600 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1601 	u32 hdr_info = tpa_info->hdr_info;
1602 	bool loopback = false;
1603 
1604 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1605 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1606 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1607 
1608 	/* If the packet is an internal loopback packet, the offsets will
1609 	 * have an extra 4 bytes.
1610 	 */
1611 	if (inner_mac_off == 4) {
1612 		loopback = true;
1613 	} else if (inner_mac_off > 4) {
1614 		__be16 proto = *((__be16 *)(skb->data + inner_ip_off -
1615 					    ETH_HLEN - 2));
1616 
1617 		/* We only support inner iPv4/ipv6.  If we don't see the
1618 		 * correct protocol ID, it must be a loopback packet where
1619 		 * the offsets are off by 4.
1620 		 */
1621 		if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
1622 			loopback = true;
1623 	}
1624 	if (loopback) {
1625 		/* internal loopback packet, subtract all offsets by 4 */
1626 		inner_ip_off -= 4;
1627 		inner_mac_off -= 4;
1628 		outer_ip_off -= 4;
1629 	}
1630 
1631 	nw_off = inner_ip_off - ETH_HLEN;
1632 	skb_set_network_header(skb, nw_off);
1633 	if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
1634 		struct ipv6hdr *iph = ipv6_hdr(skb);
1635 
1636 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1637 		len = skb->len - skb_transport_offset(skb);
1638 		th = tcp_hdr(skb);
1639 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1640 	} else {
1641 		struct iphdr *iph = ip_hdr(skb);
1642 
1643 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1644 		len = skb->len - skb_transport_offset(skb);
1645 		th = tcp_hdr(skb);
1646 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1647 	}
1648 
1649 	if (inner_mac_off) { /* tunnel */
1650 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1651 					    ETH_HLEN - 2));
1652 
1653 		bnxt_gro_tunnel(skb, proto);
1654 	}
1655 #endif
1656 	return skb;
1657 }
1658 
1659 static struct sk_buff *bnxt_gro_func_5750x(struct bnxt_tpa_info *tpa_info,
1660 					   int payload_off, int tcp_ts,
1661 					   struct sk_buff *skb)
1662 {
1663 #ifdef CONFIG_INET
1664 	u16 outer_ip_off, inner_ip_off, inner_mac_off;
1665 	u32 hdr_info = tpa_info->hdr_info;
1666 	int iphdr_len, nw_off;
1667 
1668 	inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
1669 	inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
1670 	outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
1671 
1672 	nw_off = inner_ip_off - ETH_HLEN;
1673 	skb_set_network_header(skb, nw_off);
1674 	iphdr_len = (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) ?
1675 		     sizeof(struct ipv6hdr) : sizeof(struct iphdr);
1676 	skb_set_transport_header(skb, nw_off + iphdr_len);
1677 
1678 	if (inner_mac_off) { /* tunnel */
1679 		__be16 proto = *((__be16 *)(skb->data + outer_ip_off -
1680 					    ETH_HLEN - 2));
1681 
1682 		bnxt_gro_tunnel(skb, proto);
1683 	}
1684 #endif
1685 	return skb;
1686 }
1687 
1688 #define BNXT_IPV4_HDR_SIZE	(sizeof(struct iphdr) + sizeof(struct tcphdr))
1689 #define BNXT_IPV6_HDR_SIZE	(sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
1690 
1691 static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
1692 					   int payload_off, int tcp_ts,
1693 					   struct sk_buff *skb)
1694 {
1695 #ifdef CONFIG_INET
1696 	struct tcphdr *th;
1697 	int len, nw_off, tcp_opt_len = 0;
1698 
1699 	if (tcp_ts)
1700 		tcp_opt_len = 12;
1701 
1702 	if (tpa_info->gso_type == SKB_GSO_TCPV4) {
1703 		struct iphdr *iph;
1704 
1705 		nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
1706 			 ETH_HLEN;
1707 		skb_set_network_header(skb, nw_off);
1708 		iph = ip_hdr(skb);
1709 		skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
1710 		len = skb->len - skb_transport_offset(skb);
1711 		th = tcp_hdr(skb);
1712 		th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
1713 	} else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
1714 		struct ipv6hdr *iph;
1715 
1716 		nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
1717 			 ETH_HLEN;
1718 		skb_set_network_header(skb, nw_off);
1719 		iph = ipv6_hdr(skb);
1720 		skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
1721 		len = skb->len - skb_transport_offset(skb);
1722 		th = tcp_hdr(skb);
1723 		th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
1724 	} else {
1725 		dev_kfree_skb_any(skb);
1726 		return NULL;
1727 	}
1728 
1729 	if (nw_off) /* tunnel */
1730 		bnxt_gro_tunnel(skb, skb->protocol);
1731 #endif
1732 	return skb;
1733 }
1734 
1735 static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
1736 					   struct bnxt_tpa_info *tpa_info,
1737 					   struct rx_tpa_end_cmp *tpa_end,
1738 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1739 					   struct sk_buff *skb)
1740 {
1741 #ifdef CONFIG_INET
1742 	int payload_off;
1743 	u16 segs;
1744 
1745 	segs = TPA_END_TPA_SEGS(tpa_end);
1746 	if (segs == 1)
1747 		return skb;
1748 
1749 	NAPI_GRO_CB(skb)->count = segs;
1750 	skb_shinfo(skb)->gso_size =
1751 		le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
1752 	skb_shinfo(skb)->gso_type = tpa_info->gso_type;
1753 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
1754 		payload_off = TPA_END_PAYLOAD_OFF_P5(tpa_end1);
1755 	else
1756 		payload_off = TPA_END_PAYLOAD_OFF(tpa_end);
1757 	skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
1758 	if (likely(skb))
1759 		tcp_gro_complete(skb);
1760 #endif
1761 	return skb;
1762 }
1763 
1764 /* Given the cfa_code of a received packet determine which
1765  * netdev (vf-rep or PF) the packet is destined to.
1766  */
1767 static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
1768 {
1769 	struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
1770 
1771 	/* if vf-rep dev is NULL, the must belongs to the PF */
1772 	return dev ? dev : bp->dev;
1773 }
1774 
1775 static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
1776 					   struct bnxt_cp_ring_info *cpr,
1777 					   u32 *raw_cons,
1778 					   struct rx_tpa_end_cmp *tpa_end,
1779 					   struct rx_tpa_end_cmp_ext *tpa_end1,
1780 					   u8 *event)
1781 {
1782 	struct bnxt_napi *bnapi = cpr->bnapi;
1783 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
1784 	struct net_device *dev = bp->dev;
1785 	u8 *data_ptr, agg_bufs;
1786 	unsigned int len;
1787 	struct bnxt_tpa_info *tpa_info;
1788 	dma_addr_t mapping;
1789 	struct sk_buff *skb;
1790 	u16 idx = 0, agg_id;
1791 	void *data;
1792 	bool gro;
1793 
1794 	if (unlikely(bnapi->in_reset)) {
1795 		int rc = bnxt_discard_rx(bp, cpr, raw_cons, tpa_end);
1796 
1797 		if (rc < 0)
1798 			return ERR_PTR(-EBUSY);
1799 		return NULL;
1800 	}
1801 
1802 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
1803 		agg_id = TPA_END_AGG_ID_P5(tpa_end);
1804 		agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1805 		agg_bufs = TPA_END_AGG_BUFS_P5(tpa_end1);
1806 		tpa_info = &rxr->rx_tpa[agg_id];
1807 		if (unlikely(agg_bufs != tpa_info->agg_count)) {
1808 			netdev_warn(bp->dev, "TPA end agg_buf %d != expected agg_bufs %d\n",
1809 				    agg_bufs, tpa_info->agg_count);
1810 			agg_bufs = tpa_info->agg_count;
1811 		}
1812 		tpa_info->agg_count = 0;
1813 		*event |= BNXT_AGG_EVENT;
1814 		bnxt_free_agg_idx(rxr, agg_id);
1815 		idx = agg_id;
1816 		gro = !!(bp->flags & BNXT_FLAG_GRO);
1817 	} else {
1818 		agg_id = TPA_END_AGG_ID(tpa_end);
1819 		agg_bufs = TPA_END_AGG_BUFS(tpa_end);
1820 		tpa_info = &rxr->rx_tpa[agg_id];
1821 		idx = RING_CMP(*raw_cons);
1822 		if (agg_bufs) {
1823 			if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
1824 				return ERR_PTR(-EBUSY);
1825 
1826 			*event |= BNXT_AGG_EVENT;
1827 			idx = NEXT_CMP(idx);
1828 		}
1829 		gro = !!TPA_END_GRO(tpa_end);
1830 	}
1831 	data = tpa_info->data;
1832 	data_ptr = tpa_info->data_ptr;
1833 	prefetch(data_ptr);
1834 	len = tpa_info->len;
1835 	mapping = tpa_info->mapping;
1836 
1837 	if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
1838 		bnxt_abort_tpa(cpr, idx, agg_bufs);
1839 		if (agg_bufs > MAX_SKB_FRAGS)
1840 			netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
1841 				    agg_bufs, (int)MAX_SKB_FRAGS);
1842 		return NULL;
1843 	}
1844 
1845 	if (len <= bp->rx_copy_thresh) {
1846 		skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
1847 		if (!skb) {
1848 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1849 			cpr->sw_stats->rx.rx_oom_discards += 1;
1850 			return NULL;
1851 		}
1852 	} else {
1853 		u8 *new_data;
1854 		dma_addr_t new_mapping;
1855 
1856 		new_data = __bnxt_alloc_rx_frag(bp, &new_mapping, rxr,
1857 						GFP_ATOMIC);
1858 		if (!new_data) {
1859 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1860 			cpr->sw_stats->rx.rx_oom_discards += 1;
1861 			return NULL;
1862 		}
1863 
1864 		tpa_info->data = new_data;
1865 		tpa_info->data_ptr = new_data + bp->rx_offset;
1866 		tpa_info->mapping = new_mapping;
1867 
1868 		skb = napi_build_skb(data, bp->rx_buf_size);
1869 		dma_sync_single_for_cpu(&bp->pdev->dev, mapping,
1870 					bp->rx_buf_use_size, bp->rx_dir);
1871 
1872 		if (!skb) {
1873 			page_pool_free_va(rxr->head_pool, data, true);
1874 			bnxt_abort_tpa(cpr, idx, agg_bufs);
1875 			cpr->sw_stats->rx.rx_oom_discards += 1;
1876 			return NULL;
1877 		}
1878 		skb_mark_for_recycle(skb);
1879 		skb_reserve(skb, bp->rx_offset);
1880 		skb_put(skb, len);
1881 	}
1882 
1883 	if (agg_bufs) {
1884 		skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, idx, agg_bufs, true);
1885 		if (!skb) {
1886 			/* Page reuse already handled by bnxt_rx_pages(). */
1887 			cpr->sw_stats->rx.rx_oom_discards += 1;
1888 			return NULL;
1889 		}
1890 	}
1891 
1892 	if (tpa_info->cfa_code_valid)
1893 		dev = bnxt_get_pkt_dev(bp, tpa_info->cfa_code);
1894 	skb->protocol = eth_type_trans(skb, dev);
1895 
1896 	if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
1897 		skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
1898 
1899 	if (tpa_info->vlan_valid &&
1900 	    (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)) {
1901 		__be16 vlan_proto = htons(tpa_info->metadata >>
1902 					  RX_CMP_FLAGS2_METADATA_TPID_SFT);
1903 		u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1904 
1905 		if (eth_type_vlan(vlan_proto)) {
1906 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1907 		} else {
1908 			dev_kfree_skb(skb);
1909 			return NULL;
1910 		}
1911 	}
1912 
1913 	skb_checksum_none_assert(skb);
1914 	if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
1915 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1916 		skb->csum_level =
1917 			(tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
1918 	}
1919 
1920 	if (gro)
1921 		skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
1922 
1923 	return skb;
1924 }
1925 
1926 static void bnxt_tpa_agg(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
1927 			 struct rx_agg_cmp *rx_agg)
1928 {
1929 	u16 agg_id = TPA_AGG_AGG_ID(rx_agg);
1930 	struct bnxt_tpa_info *tpa_info;
1931 
1932 	agg_id = bnxt_lookup_agg_idx(rxr, agg_id);
1933 	tpa_info = &rxr->rx_tpa[agg_id];
1934 	BUG_ON(tpa_info->agg_count >= MAX_SKB_FRAGS);
1935 	tpa_info->agg_arr[tpa_info->agg_count++] = *rx_agg;
1936 }
1937 
1938 static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
1939 			     struct sk_buff *skb)
1940 {
1941 	skb_mark_for_recycle(skb);
1942 
1943 	if (skb->dev != bp->dev) {
1944 		/* this packet belongs to a vf-rep */
1945 		bnxt_vf_rep_rx(bp, skb);
1946 		return;
1947 	}
1948 	skb_record_rx_queue(skb, bnapi->index);
1949 	napi_gro_receive(&bnapi->napi, skb);
1950 }
1951 
1952 static bool bnxt_rx_ts_valid(struct bnxt *bp, u32 flags,
1953 			     struct rx_cmp_ext *rxcmp1, u32 *cmpl_ts)
1954 {
1955 	u32 ts = le32_to_cpu(rxcmp1->rx_cmp_timestamp);
1956 
1957 	if (BNXT_PTP_RX_TS_VALID(flags))
1958 		goto ts_valid;
1959 	if (!bp->ptp_all_rx_tstamp || !ts || !BNXT_ALL_RX_TS_VALID(flags))
1960 		return false;
1961 
1962 ts_valid:
1963 	*cmpl_ts = ts;
1964 	return true;
1965 }
1966 
1967 static struct sk_buff *bnxt_rx_vlan(struct sk_buff *skb, u8 cmp_type,
1968 				    struct rx_cmp *rxcmp,
1969 				    struct rx_cmp_ext *rxcmp1)
1970 {
1971 	__be16 vlan_proto;
1972 	u16 vtag;
1973 
1974 	if (cmp_type == CMP_TYPE_RX_L2_CMP) {
1975 		__le32 flags2 = rxcmp1->rx_cmp_flags2;
1976 		u32 meta_data;
1977 
1978 		if (!(flags2 & cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)))
1979 			return skb;
1980 
1981 		meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
1982 		vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
1983 		vlan_proto = htons(meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT);
1984 		if (eth_type_vlan(vlan_proto))
1985 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
1986 		else
1987 			goto vlan_err;
1988 	} else if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
1989 		if (RX_CMP_VLAN_VALID(rxcmp)) {
1990 			u32 tpid_sel = RX_CMP_VLAN_TPID_SEL(rxcmp);
1991 
1992 			if (tpid_sel == RX_CMP_METADATA1_TPID_8021Q)
1993 				vlan_proto = htons(ETH_P_8021Q);
1994 			else if (tpid_sel == RX_CMP_METADATA1_TPID_8021AD)
1995 				vlan_proto = htons(ETH_P_8021AD);
1996 			else
1997 				goto vlan_err;
1998 			vtag = RX_CMP_METADATA0_TCI(rxcmp1);
1999 			__vlan_hwaccel_put_tag(skb, vlan_proto, vtag);
2000 		}
2001 	}
2002 	return skb;
2003 vlan_err:
2004 	dev_kfree_skb(skb);
2005 	return NULL;
2006 }
2007 
2008 static enum pkt_hash_types bnxt_rss_ext_op(struct bnxt *bp,
2009 					   struct rx_cmp *rxcmp)
2010 {
2011 	u8 ext_op;
2012 
2013 	ext_op = RX_CMP_V3_HASH_TYPE(bp, rxcmp);
2014 	switch (ext_op) {
2015 	case EXT_OP_INNER_4:
2016 	case EXT_OP_OUTER_4:
2017 	case EXT_OP_INNFL_3:
2018 	case EXT_OP_OUTFL_3:
2019 		return PKT_HASH_TYPE_L4;
2020 	default:
2021 		return PKT_HASH_TYPE_L3;
2022 	}
2023 }
2024 
2025 /* returns the following:
2026  * 1       - 1 packet successfully received
2027  * 0       - successful TPA_START, packet not completed yet
2028  * -EBUSY  - completion ring does not have all the agg buffers yet
2029  * -ENOMEM - packet aborted due to out of memory
2030  * -EIO    - packet aborted due to hw error indicated in BD
2031  */
2032 static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2033 		       u32 *raw_cons, u8 *event)
2034 {
2035 	struct bnxt_napi *bnapi = cpr->bnapi;
2036 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
2037 	struct net_device *dev = bp->dev;
2038 	struct rx_cmp *rxcmp;
2039 	struct rx_cmp_ext *rxcmp1;
2040 	u32 tmp_raw_cons = *raw_cons;
2041 	u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
2042 	struct bnxt_sw_rx_bd *rx_buf;
2043 	unsigned int len;
2044 	u8 *data_ptr, agg_bufs, cmp_type;
2045 	bool xdp_active = false;
2046 	dma_addr_t dma_addr;
2047 	struct sk_buff *skb;
2048 	struct xdp_buff xdp;
2049 	u32 flags, misc;
2050 	u32 cmpl_ts;
2051 	void *data;
2052 	int rc = 0;
2053 
2054 	rxcmp = (struct rx_cmp *)
2055 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2056 
2057 	cmp_type = RX_CMP_TYPE(rxcmp);
2058 
2059 	if (cmp_type == CMP_TYPE_RX_TPA_AGG_CMP) {
2060 		bnxt_tpa_agg(bp, rxr, (struct rx_agg_cmp *)rxcmp);
2061 		goto next_rx_no_prod_no_len;
2062 	}
2063 
2064 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2065 	cp_cons = RING_CMP(tmp_raw_cons);
2066 	rxcmp1 = (struct rx_cmp_ext *)
2067 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2068 
2069 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2070 		return -EBUSY;
2071 
2072 	/* The valid test of the entry must be done first before
2073 	 * reading any further.
2074 	 */
2075 	dma_rmb();
2076 	prod = rxr->rx_prod;
2077 
2078 	if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP ||
2079 	    cmp_type == CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2080 		bnxt_tpa_start(bp, rxr, cmp_type,
2081 			       (struct rx_tpa_start_cmp *)rxcmp,
2082 			       (struct rx_tpa_start_cmp_ext *)rxcmp1);
2083 
2084 		*event |= BNXT_RX_EVENT;
2085 		goto next_rx_no_prod_no_len;
2086 
2087 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2088 		skb = bnxt_tpa_end(bp, cpr, &tmp_raw_cons,
2089 				   (struct rx_tpa_end_cmp *)rxcmp,
2090 				   (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
2091 
2092 		if (IS_ERR(skb))
2093 			return -EBUSY;
2094 
2095 		rc = -ENOMEM;
2096 		if (likely(skb)) {
2097 			bnxt_deliver_skb(bp, bnapi, skb);
2098 			rc = 1;
2099 		}
2100 		*event |= BNXT_RX_EVENT;
2101 		goto next_rx_no_prod_no_len;
2102 	}
2103 
2104 	cons = rxcmp->rx_cmp_opaque;
2105 	if (unlikely(cons != rxr->rx_next_cons)) {
2106 		int rc1 = bnxt_discard_rx(bp, cpr, &tmp_raw_cons, rxcmp);
2107 
2108 		/* 0xffff is forced error, don't print it */
2109 		if (rxr->rx_next_cons != 0xffff)
2110 			netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
2111 				    cons, rxr->rx_next_cons);
2112 		bnxt_sched_reset_rxr(bp, rxr);
2113 		if (rc1)
2114 			return rc1;
2115 		goto next_rx_no_prod_no_len;
2116 	}
2117 	rx_buf = &rxr->rx_buf_ring[cons];
2118 	data = rx_buf->data;
2119 	data_ptr = rx_buf->data_ptr;
2120 	prefetch(data_ptr);
2121 
2122 	misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
2123 	agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
2124 
2125 	if (agg_bufs) {
2126 		if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
2127 			return -EBUSY;
2128 
2129 		cp_cons = NEXT_CMP(cp_cons);
2130 		*event |= BNXT_AGG_EVENT;
2131 	}
2132 	*event |= BNXT_RX_EVENT;
2133 
2134 	rx_buf->data = NULL;
2135 	if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
2136 		u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
2137 
2138 		bnxt_reuse_rx_data(rxr, cons, data);
2139 		if (agg_bufs)
2140 			bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0, agg_bufs,
2141 					       false);
2142 
2143 		rc = -EIO;
2144 		if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
2145 			bnapi->cp_ring.sw_stats->rx.rx_buf_errors++;
2146 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
2147 			    !(bp->fw_cap & BNXT_FW_CAP_RING_MONITOR)) {
2148 				netdev_warn_once(bp->dev, "RX buffer error %x\n",
2149 						 rx_err);
2150 				bnxt_sched_reset_rxr(bp, rxr);
2151 			}
2152 		}
2153 		goto next_rx_no_len;
2154 	}
2155 
2156 	flags = le32_to_cpu(rxcmp->rx_cmp_len_flags_type);
2157 	len = flags >> RX_CMP_LEN_SHIFT;
2158 	dma_addr = rx_buf->mapping;
2159 
2160 	if (bnxt_xdp_attached(bp, rxr)) {
2161 		bnxt_xdp_buff_init(bp, rxr, cons, data_ptr, len, &xdp);
2162 		if (agg_bufs) {
2163 			u32 frag_len = bnxt_rx_agg_pages_xdp(bp, cpr, &xdp,
2164 							     cp_cons, agg_bufs,
2165 							     false);
2166 			if (!frag_len)
2167 				goto oom_next_rx;
2168 		}
2169 		xdp_active = true;
2170 	}
2171 
2172 	if (xdp_active) {
2173 		if (bnxt_rx_xdp(bp, rxr, cons, &xdp, data, &data_ptr, &len, event)) {
2174 			rc = 1;
2175 			goto next_rx;
2176 		}
2177 	}
2178 
2179 	if (len <= bp->rx_copy_thresh) {
2180 		if (!xdp_active)
2181 			skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
2182 		else
2183 			skb = bnxt_copy_xdp(bnapi, &xdp, len, dma_addr);
2184 		bnxt_reuse_rx_data(rxr, cons, data);
2185 		if (!skb) {
2186 			if (agg_bufs) {
2187 				if (!xdp_active)
2188 					bnxt_reuse_rx_agg_bufs(cpr, cp_cons, 0,
2189 							       agg_bufs, false);
2190 				else
2191 					bnxt_xdp_buff_frags_free(rxr, &xdp);
2192 			}
2193 			goto oom_next_rx;
2194 		}
2195 	} else {
2196 		u32 payload;
2197 
2198 		if (rx_buf->data_ptr == data_ptr)
2199 			payload = misc & RX_CMP_PAYLOAD_OFFSET;
2200 		else
2201 			payload = 0;
2202 		skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
2203 				      payload | len);
2204 		if (!skb)
2205 			goto oom_next_rx;
2206 	}
2207 
2208 	if (agg_bufs) {
2209 		if (!xdp_active) {
2210 			skb = bnxt_rx_agg_pages_skb(bp, cpr, skb, cp_cons, agg_bufs, false);
2211 			if (!skb)
2212 				goto oom_next_rx;
2213 		} else {
2214 			skb = bnxt_xdp_build_skb(bp, skb, agg_bufs, rxr->page_pool, &xdp, rxcmp1);
2215 			if (!skb) {
2216 				/* we should be able to free the old skb here */
2217 				bnxt_xdp_buff_frags_free(rxr, &xdp);
2218 				goto oom_next_rx;
2219 			}
2220 		}
2221 	}
2222 
2223 	if (RX_CMP_HASH_VALID(rxcmp)) {
2224 		enum pkt_hash_types type;
2225 
2226 		if (cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2227 			type = bnxt_rss_ext_op(bp, rxcmp);
2228 		} else {
2229 			u32 itypes = RX_CMP_ITYPES(rxcmp);
2230 
2231 			if (itypes == RX_CMP_FLAGS_ITYPE_TCP ||
2232 			    itypes == RX_CMP_FLAGS_ITYPE_UDP)
2233 				type = PKT_HASH_TYPE_L4;
2234 			else
2235 				type = PKT_HASH_TYPE_L3;
2236 		}
2237 		skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
2238 	}
2239 
2240 	if (cmp_type == CMP_TYPE_RX_L2_CMP)
2241 		dev = bnxt_get_pkt_dev(bp, RX_CMP_CFA_CODE(rxcmp1));
2242 	skb->protocol = eth_type_trans(skb, dev);
2243 
2244 	if (skb->dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX) {
2245 		skb = bnxt_rx_vlan(skb, cmp_type, rxcmp, rxcmp1);
2246 		if (!skb)
2247 			goto next_rx;
2248 	}
2249 
2250 	skb_checksum_none_assert(skb);
2251 	if (RX_CMP_L4_CS_OK(rxcmp1)) {
2252 		if (dev->features & NETIF_F_RXCSUM) {
2253 			skb->ip_summed = CHECKSUM_UNNECESSARY;
2254 			skb->csum_level = RX_CMP_ENCAP(rxcmp1);
2255 		}
2256 	} else {
2257 		if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
2258 			if (dev->features & NETIF_F_RXCSUM)
2259 				bnapi->cp_ring.sw_stats->rx.rx_l4_csum_errors++;
2260 		}
2261 	}
2262 
2263 	if (bnxt_rx_ts_valid(bp, flags, rxcmp1, &cmpl_ts)) {
2264 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
2265 			u64 ns, ts;
2266 
2267 			if (!bnxt_get_rx_ts_p5(bp, &ts, cmpl_ts)) {
2268 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2269 
2270 				ns = bnxt_timecounter_cyc2time(ptp, ts);
2271 				memset(skb_hwtstamps(skb), 0,
2272 				       sizeof(*skb_hwtstamps(skb)));
2273 				skb_hwtstamps(skb)->hwtstamp = ns_to_ktime(ns);
2274 			}
2275 		}
2276 	}
2277 	bnxt_deliver_skb(bp, bnapi, skb);
2278 	rc = 1;
2279 
2280 next_rx:
2281 	cpr->rx_packets += 1;
2282 	cpr->rx_bytes += len;
2283 
2284 next_rx_no_len:
2285 	rxr->rx_prod = NEXT_RX(prod);
2286 	rxr->rx_next_cons = RING_RX(bp, NEXT_RX(cons));
2287 
2288 next_rx_no_prod_no_len:
2289 	*raw_cons = tmp_raw_cons;
2290 
2291 	return rc;
2292 
2293 oom_next_rx:
2294 	cpr->sw_stats->rx.rx_oom_discards += 1;
2295 	rc = -ENOMEM;
2296 	goto next_rx;
2297 }
2298 
2299 /* In netpoll mode, if we are using a combined completion ring, we need to
2300  * discard the rx packets and recycle the buffers.
2301  */
2302 static int bnxt_force_rx_discard(struct bnxt *bp,
2303 				 struct bnxt_cp_ring_info *cpr,
2304 				 u32 *raw_cons, u8 *event)
2305 {
2306 	u32 tmp_raw_cons = *raw_cons;
2307 	struct rx_cmp_ext *rxcmp1;
2308 	struct rx_cmp *rxcmp;
2309 	u16 cp_cons;
2310 	u8 cmp_type;
2311 	int rc;
2312 
2313 	cp_cons = RING_CMP(tmp_raw_cons);
2314 	rxcmp = (struct rx_cmp *)
2315 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2316 
2317 	tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
2318 	cp_cons = RING_CMP(tmp_raw_cons);
2319 	rxcmp1 = (struct rx_cmp_ext *)
2320 			&cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
2321 
2322 	if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
2323 		return -EBUSY;
2324 
2325 	/* The valid test of the entry must be done first before
2326 	 * reading any further.
2327 	 */
2328 	dma_rmb();
2329 	cmp_type = RX_CMP_TYPE(rxcmp);
2330 	if (cmp_type == CMP_TYPE_RX_L2_CMP ||
2331 	    cmp_type == CMP_TYPE_RX_L2_V3_CMP) {
2332 		rxcmp1->rx_cmp_cfa_code_errors_v2 |=
2333 			cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
2334 	} else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
2335 		struct rx_tpa_end_cmp_ext *tpa_end1;
2336 
2337 		tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
2338 		tpa_end1->rx_tpa_end_cmp_errors_v2 |=
2339 			cpu_to_le32(RX_TPA_END_CMP_ERRORS);
2340 	}
2341 	rc = bnxt_rx_pkt(bp, cpr, raw_cons, event);
2342 	if (rc && rc != -EBUSY)
2343 		cpr->sw_stats->rx.rx_netpoll_discards += 1;
2344 	return rc;
2345 }
2346 
2347 u32 bnxt_fw_health_readl(struct bnxt *bp, int reg_idx)
2348 {
2349 	struct bnxt_fw_health *fw_health = bp->fw_health;
2350 	u32 reg = fw_health->regs[reg_idx];
2351 	u32 reg_type, reg_off, val = 0;
2352 
2353 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
2354 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
2355 	switch (reg_type) {
2356 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
2357 		pci_read_config_dword(bp->pdev, reg_off, &val);
2358 		break;
2359 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
2360 		reg_off = fw_health->mapped_regs[reg_idx];
2361 		fallthrough;
2362 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
2363 		val = readl(bp->bar0 + reg_off);
2364 		break;
2365 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
2366 		val = readl(bp->bar1 + reg_off);
2367 		break;
2368 	}
2369 	if (reg_idx == BNXT_FW_RESET_INPROG_REG)
2370 		val &= fw_health->fw_reset_inprog_reg_mask;
2371 	return val;
2372 }
2373 
2374 static u16 bnxt_agg_ring_id_to_grp_idx(struct bnxt *bp, u16 ring_id)
2375 {
2376 	int i;
2377 
2378 	for (i = 0; i < bp->rx_nr_rings; i++) {
2379 		u16 grp_idx = bp->rx_ring[i].bnapi->index;
2380 		struct bnxt_ring_grp_info *grp_info;
2381 
2382 		grp_info = &bp->grp_info[grp_idx];
2383 		if (grp_info->agg_fw_ring_id == ring_id)
2384 			return grp_idx;
2385 	}
2386 	return INVALID_HW_RING_ID;
2387 }
2388 
2389 static u16 bnxt_get_force_speed(struct bnxt_link_info *link_info)
2390 {
2391 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2392 
2393 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
2394 		return link_info->force_link_speed2;
2395 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4)
2396 		return link_info->force_pam4_link_speed;
2397 	return link_info->force_link_speed;
2398 }
2399 
2400 static void bnxt_set_force_speed(struct bnxt_link_info *link_info)
2401 {
2402 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2403 
2404 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2405 		link_info->req_link_speed = link_info->force_link_speed2;
2406 		link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2407 		switch (link_info->req_link_speed) {
2408 		case BNXT_LINK_SPEED_50GB_PAM4:
2409 		case BNXT_LINK_SPEED_100GB_PAM4:
2410 		case BNXT_LINK_SPEED_200GB_PAM4:
2411 		case BNXT_LINK_SPEED_400GB_PAM4:
2412 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2413 			break;
2414 		case BNXT_LINK_SPEED_100GB_PAM4_112:
2415 		case BNXT_LINK_SPEED_200GB_PAM4_112:
2416 		case BNXT_LINK_SPEED_400GB_PAM4_112:
2417 			link_info->req_signal_mode = BNXT_SIG_MODE_PAM4_112;
2418 			break;
2419 		default:
2420 			link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2421 		}
2422 		return;
2423 	}
2424 	link_info->req_link_speed = link_info->force_link_speed;
2425 	link_info->req_signal_mode = BNXT_SIG_MODE_NRZ;
2426 	if (link_info->force_pam4_link_speed) {
2427 		link_info->req_link_speed = link_info->force_pam4_link_speed;
2428 		link_info->req_signal_mode = BNXT_SIG_MODE_PAM4;
2429 	}
2430 }
2431 
2432 static void bnxt_set_auto_speed(struct bnxt_link_info *link_info)
2433 {
2434 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2435 
2436 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2437 		link_info->advertising = link_info->auto_link_speeds2;
2438 		return;
2439 	}
2440 	link_info->advertising = link_info->auto_link_speeds;
2441 	link_info->advertising_pam4 = link_info->auto_pam4_link_speeds;
2442 }
2443 
2444 static bool bnxt_force_speed_updated(struct bnxt_link_info *link_info)
2445 {
2446 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2447 
2448 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2449 		if (link_info->req_link_speed != link_info->force_link_speed2)
2450 			return true;
2451 		return false;
2452 	}
2453 	if (link_info->req_signal_mode == BNXT_SIG_MODE_NRZ &&
2454 	    link_info->req_link_speed != link_info->force_link_speed)
2455 		return true;
2456 	if (link_info->req_signal_mode == BNXT_SIG_MODE_PAM4 &&
2457 	    link_info->req_link_speed != link_info->force_pam4_link_speed)
2458 		return true;
2459 	return false;
2460 }
2461 
2462 static bool bnxt_auto_speed_updated(struct bnxt_link_info *link_info)
2463 {
2464 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
2465 
2466 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
2467 		if (link_info->advertising != link_info->auto_link_speeds2)
2468 			return true;
2469 		return false;
2470 	}
2471 	if (link_info->advertising != link_info->auto_link_speeds ||
2472 	    link_info->advertising_pam4 != link_info->auto_pam4_link_speeds)
2473 		return true;
2474 	return false;
2475 }
2476 
2477 bool bnxt_bs_trace_avail(struct bnxt *bp, u16 type)
2478 {
2479 	u32 flags = bp->ctx->ctx_arr[type].flags;
2480 
2481 	return (flags & BNXT_CTX_MEM_TYPE_VALID) &&
2482 		((flags & BNXT_CTX_MEM_FW_TRACE) ||
2483 		 (flags & BNXT_CTX_MEM_FW_BIN_TRACE));
2484 }
2485 
2486 static void bnxt_bs_trace_init(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm)
2487 {
2488 	u32 mem_size, pages, rem_bytes, magic_byte_offset;
2489 	u16 trace_type = bnxt_bstore_to_trace[ctxm->type];
2490 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
2491 	struct bnxt_ring_mem_info *rmem, *rmem_pg_tbl;
2492 	struct bnxt_bs_trace_info *bs_trace;
2493 	int last_pg;
2494 
2495 	if (ctxm->instance_bmap && ctxm->instance_bmap > 1)
2496 		return;
2497 
2498 	mem_size = ctxm->max_entries * ctxm->entry_size;
2499 	rem_bytes = mem_size % BNXT_PAGE_SIZE;
2500 	pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
2501 
2502 	last_pg = (pages - 1) & (MAX_CTX_PAGES - 1);
2503 	magic_byte_offset = (rem_bytes ? rem_bytes : BNXT_PAGE_SIZE) - 1;
2504 
2505 	rmem = &ctx_pg[0].ring_mem;
2506 	bs_trace = &bp->bs_trace[trace_type];
2507 	bs_trace->ctx_type = ctxm->type;
2508 	bs_trace->trace_type = trace_type;
2509 	if (pages > MAX_CTX_PAGES) {
2510 		int last_pg_dir = rmem->nr_pages - 1;
2511 
2512 		rmem_pg_tbl = &ctx_pg[0].ctx_pg_tbl[last_pg_dir]->ring_mem;
2513 		bs_trace->magic_byte = rmem_pg_tbl->pg_arr[last_pg];
2514 	} else {
2515 		bs_trace->magic_byte = rmem->pg_arr[last_pg];
2516 	}
2517 	bs_trace->magic_byte += magic_byte_offset;
2518 	*bs_trace->magic_byte = BNXT_TRACE_BUF_MAGIC_BYTE;
2519 }
2520 
2521 #define BNXT_EVENT_BUF_PRODUCER_TYPE(data1)				\
2522 	(((data1) & ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_MASK) >>\
2523 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA1_TYPE_SFT)
2524 
2525 #define BNXT_EVENT_BUF_PRODUCER_OFFSET(data2)				\
2526 	(((data2) &							\
2527 	  ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_MASK) >>\
2528 	 ASYNC_EVENT_CMPL_DBG_BUF_PRODUCER_EVENT_DATA2_CURR_OFF_SFT)
2529 
2530 #define BNXT_EVENT_THERMAL_CURRENT_TEMP(data2)				\
2531 	((data2) &							\
2532 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_CURRENT_TEMP_MASK)
2533 
2534 #define BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2)			\
2535 	(((data2) &							\
2536 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_MASK) >>\
2537 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA2_THRESHOLD_TEMP_SFT)
2538 
2539 #define EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1)			\
2540 	((data1) &							\
2541 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_MASK)
2542 
2543 #define EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)		\
2544 	(((data1) &							\
2545 	  ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR) ==\
2546 	 ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_TRANSITION_DIR_INCREASING)
2547 
2548 /* Return true if the workqueue has to be scheduled */
2549 static bool bnxt_event_error_report(struct bnxt *bp, u32 data1, u32 data2)
2550 {
2551 	u32 err_type = BNXT_EVENT_ERROR_REPORT_TYPE(data1);
2552 
2553 	switch (err_type) {
2554 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_INVALID_SIGNAL:
2555 		netdev_err(bp->dev, "1PPS: Received invalid signal on pin%lu from the external source. Please fix the signal and reconfigure the pin\n",
2556 			   BNXT_EVENT_INVALID_SIGNAL_DATA(data2));
2557 		break;
2558 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_PAUSE_STORM:
2559 		netdev_warn(bp->dev, "Pause Storm detected!\n");
2560 		break;
2561 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DOORBELL_DROP_THRESHOLD:
2562 		netdev_warn(bp->dev, "One or more MMIO doorbells dropped by the device!\n");
2563 		break;
2564 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_THERMAL_THRESHOLD: {
2565 		u32 type = EVENT_DATA1_THERMAL_THRESHOLD_TYPE(data1);
2566 		char *threshold_type;
2567 		bool notify = false;
2568 		char *dir_str;
2569 
2570 		switch (type) {
2571 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_WARN:
2572 			threshold_type = "warning";
2573 			break;
2574 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_CRITICAL:
2575 			threshold_type = "critical";
2576 			break;
2577 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_FATAL:
2578 			threshold_type = "fatal";
2579 			break;
2580 		case ASYNC_EVENT_CMPL_ERROR_REPORT_THERMAL_EVENT_DATA1_THRESHOLD_TYPE_SHUTDOWN:
2581 			threshold_type = "shutdown";
2582 			break;
2583 		default:
2584 			netdev_err(bp->dev, "Unknown Thermal threshold type event\n");
2585 			return false;
2586 		}
2587 		if (EVENT_DATA1_THERMAL_THRESHOLD_DIR_INCREASING(data1)) {
2588 			dir_str = "above";
2589 			notify = true;
2590 		} else {
2591 			dir_str = "below";
2592 		}
2593 		netdev_warn(bp->dev, "Chip temperature has gone %s the %s thermal threshold!\n",
2594 			    dir_str, threshold_type);
2595 		netdev_warn(bp->dev, "Temperature (In Celsius), Current: %lu, threshold: %lu\n",
2596 			    BNXT_EVENT_THERMAL_CURRENT_TEMP(data2),
2597 			    BNXT_EVENT_THERMAL_THRESHOLD_TEMP(data2));
2598 		if (notify) {
2599 			bp->thermal_threshold_type = type;
2600 			set_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event);
2601 			return true;
2602 		}
2603 		return false;
2604 	}
2605 	case ASYNC_EVENT_CMPL_ERROR_REPORT_BASE_EVENT_DATA1_ERROR_TYPE_DUAL_DATA_RATE_NOT_SUPPORTED:
2606 		netdev_warn(bp->dev, "Speed change not supported with dual rate transceivers on this board\n");
2607 		break;
2608 	default:
2609 		netdev_err(bp->dev, "FW reported unknown error type %u\n",
2610 			   err_type);
2611 		break;
2612 	}
2613 	return false;
2614 }
2615 
2616 #define BNXT_GET_EVENT_PORT(data)	\
2617 	((data) &			\
2618 	 ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
2619 
2620 #define BNXT_EVENT_RING_TYPE(data2)	\
2621 	((data2) &			\
2622 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_MASK)
2623 
2624 #define BNXT_EVENT_RING_TYPE_RX(data2)	\
2625 	(BNXT_EVENT_RING_TYPE(data2) ==	\
2626 	 ASYNC_EVENT_CMPL_RING_MONITOR_MSG_EVENT_DATA2_DISABLE_RING_TYPE_RX)
2627 
2628 #define BNXT_EVENT_PHC_EVENT_TYPE(data1)	\
2629 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_MASK) >>\
2630 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_SFT)
2631 
2632 #define BNXT_EVENT_PHC_RTC_UPDATE(data1)	\
2633 	(((data1) & ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_MASK) >>\
2634 	 ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_PHC_TIME_MSB_SFT)
2635 
2636 #define BNXT_PHC_BITS	48
2637 
2638 static int bnxt_async_event_process(struct bnxt *bp,
2639 				    struct hwrm_async_event_cmpl *cmpl)
2640 {
2641 	u16 event_id = le16_to_cpu(cmpl->event_id);
2642 	u32 data1 = le32_to_cpu(cmpl->event_data1);
2643 	u32 data2 = le32_to_cpu(cmpl->event_data2);
2644 
2645 	netdev_dbg(bp->dev, "hwrm event 0x%x {0x%x, 0x%x}\n",
2646 		   event_id, data1, data2);
2647 
2648 	/* TODO CHIMP_FW: Define event id's for link change, error etc */
2649 	switch (event_id) {
2650 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
2651 		struct bnxt_link_info *link_info = &bp->link_info;
2652 
2653 		if (BNXT_VF(bp))
2654 			goto async_event_process_exit;
2655 
2656 		/* print unsupported speed warning in forced speed mode only */
2657 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
2658 		    (data1 & 0x20000)) {
2659 			u16 fw_speed = bnxt_get_force_speed(link_info);
2660 			u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
2661 
2662 			if (speed != SPEED_UNKNOWN)
2663 				netdev_warn(bp->dev, "Link speed %d no longer supported\n",
2664 					    speed);
2665 		}
2666 		set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
2667 	}
2668 		fallthrough;
2669 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2670 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_PHY_CFG_CHANGE:
2671 		set_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT, &bp->sp_event);
2672 		fallthrough;
2673 	case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2674 		set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
2675 		break;
2676 	case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2677 		set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
2678 		break;
2679 	case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
2680 		u16 port_id = BNXT_GET_EVENT_PORT(data1);
2681 
2682 		if (BNXT_VF(bp))
2683 			break;
2684 
2685 		if (bp->pf.port_id != port_id)
2686 			break;
2687 
2688 		set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
2689 		break;
2690 	}
2691 	case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2692 		if (BNXT_PF(bp))
2693 			goto async_event_process_exit;
2694 		set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
2695 		break;
2696 	case ASYNC_EVENT_CMPL_EVENT_ID_RESET_NOTIFY: {
2697 		char *type_str = "Solicited";
2698 
2699 		if (!bp->fw_health)
2700 			goto async_event_process_exit;
2701 
2702 		bp->fw_reset_timestamp = jiffies;
2703 		bp->fw_reset_min_dsecs = cmpl->timestamp_lo;
2704 		if (!bp->fw_reset_min_dsecs)
2705 			bp->fw_reset_min_dsecs = BNXT_DFLT_FW_RST_MIN_DSECS;
2706 		bp->fw_reset_max_dsecs = le16_to_cpu(cmpl->timestamp_hi);
2707 		if (!bp->fw_reset_max_dsecs)
2708 			bp->fw_reset_max_dsecs = BNXT_DFLT_FW_RST_MAX_DSECS;
2709 		if (EVENT_DATA1_RESET_NOTIFY_FW_ACTIVATION(data1)) {
2710 			set_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state);
2711 		} else if (EVENT_DATA1_RESET_NOTIFY_FATAL(data1)) {
2712 			type_str = "Fatal";
2713 			bp->fw_health->fatalities++;
2714 			set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
2715 		} else if (data2 && BNXT_FW_STATUS_HEALTHY !=
2716 			   EVENT_DATA2_RESET_NOTIFY_FW_STATUS_CODE(data2)) {
2717 			type_str = "Non-fatal";
2718 			bp->fw_health->survivals++;
2719 			set_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
2720 		}
2721 		netif_warn(bp, hw, bp->dev,
2722 			   "%s firmware reset event, data1: 0x%x, data2: 0x%x, min wait %u ms, max wait %u ms\n",
2723 			   type_str, data1, data2,
2724 			   bp->fw_reset_min_dsecs * 100,
2725 			   bp->fw_reset_max_dsecs * 100);
2726 		set_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event);
2727 		break;
2728 	}
2729 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY: {
2730 		struct bnxt_fw_health *fw_health = bp->fw_health;
2731 		char *status_desc = "healthy";
2732 		u32 status;
2733 
2734 		if (!fw_health)
2735 			goto async_event_process_exit;
2736 
2737 		if (!EVENT_DATA1_RECOVERY_ENABLED(data1)) {
2738 			fw_health->enabled = false;
2739 			netif_info(bp, drv, bp->dev, "Driver recovery watchdog is disabled\n");
2740 			break;
2741 		}
2742 		fw_health->primary = EVENT_DATA1_RECOVERY_MASTER_FUNC(data1);
2743 		fw_health->tmr_multiplier =
2744 			DIV_ROUND_UP(fw_health->polling_dsecs * HZ,
2745 				     bp->current_interval * 10);
2746 		fw_health->tmr_counter = fw_health->tmr_multiplier;
2747 		if (!fw_health->enabled)
2748 			fw_health->last_fw_heartbeat =
2749 				bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
2750 		fw_health->last_fw_reset_cnt =
2751 			bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
2752 		status = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
2753 		if (status != BNXT_FW_STATUS_HEALTHY)
2754 			status_desc = "unhealthy";
2755 		netif_info(bp, drv, bp->dev,
2756 			   "Driver recovery watchdog, role: %s, firmware status: 0x%x (%s), resets: %u\n",
2757 			   fw_health->primary ? "primary" : "backup", status,
2758 			   status_desc, fw_health->last_fw_reset_cnt);
2759 		if (!fw_health->enabled) {
2760 			/* Make sure tmr_counter is set and visible to
2761 			 * bnxt_health_check() before setting enabled to true.
2762 			 */
2763 			smp_wmb();
2764 			fw_health->enabled = true;
2765 		}
2766 		goto async_event_process_exit;
2767 	}
2768 	case ASYNC_EVENT_CMPL_EVENT_ID_DEBUG_NOTIFICATION:
2769 		netif_notice(bp, hw, bp->dev,
2770 			     "Received firmware debug notification, data1: 0x%x, data2: 0x%x\n",
2771 			     data1, data2);
2772 		goto async_event_process_exit;
2773 	case ASYNC_EVENT_CMPL_EVENT_ID_RING_MONITOR_MSG: {
2774 		struct bnxt_rx_ring_info *rxr;
2775 		u16 grp_idx;
2776 
2777 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
2778 			goto async_event_process_exit;
2779 
2780 		netdev_warn(bp->dev, "Ring monitor event, ring type %lu id 0x%x\n",
2781 			    BNXT_EVENT_RING_TYPE(data2), data1);
2782 		if (!BNXT_EVENT_RING_TYPE_RX(data2))
2783 			goto async_event_process_exit;
2784 
2785 		grp_idx = bnxt_agg_ring_id_to_grp_idx(bp, data1);
2786 		if (grp_idx == INVALID_HW_RING_ID) {
2787 			netdev_warn(bp->dev, "Unknown RX agg ring id 0x%x\n",
2788 				    data1);
2789 			goto async_event_process_exit;
2790 		}
2791 		rxr = bp->bnapi[grp_idx]->rx_ring;
2792 		bnxt_sched_reset_rxr(bp, rxr);
2793 		goto async_event_process_exit;
2794 	}
2795 	case ASYNC_EVENT_CMPL_EVENT_ID_ECHO_REQUEST: {
2796 		struct bnxt_fw_health *fw_health = bp->fw_health;
2797 
2798 		netif_notice(bp, hw, bp->dev,
2799 			     "Received firmware echo request, data1: 0x%x, data2: 0x%x\n",
2800 			     data1, data2);
2801 		if (fw_health) {
2802 			fw_health->echo_req_data1 = data1;
2803 			fw_health->echo_req_data2 = data2;
2804 			set_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event);
2805 			break;
2806 		}
2807 		goto async_event_process_exit;
2808 	}
2809 	case ASYNC_EVENT_CMPL_EVENT_ID_PPS_TIMESTAMP: {
2810 		bnxt_ptp_pps_event(bp, data1, data2);
2811 		goto async_event_process_exit;
2812 	}
2813 	case ASYNC_EVENT_CMPL_EVENT_ID_ERROR_REPORT: {
2814 		if (bnxt_event_error_report(bp, data1, data2))
2815 			break;
2816 		goto async_event_process_exit;
2817 	}
2818 	case ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE: {
2819 		switch (BNXT_EVENT_PHC_EVENT_TYPE(data1)) {
2820 		case ASYNC_EVENT_CMPL_PHC_UPDATE_EVENT_DATA1_FLAGS_PHC_RTC_UPDATE:
2821 			if (BNXT_PTP_USE_RTC(bp)) {
2822 				struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
2823 				unsigned long flags;
2824 				u64 ns;
2825 
2826 				if (!ptp)
2827 					goto async_event_process_exit;
2828 
2829 				bnxt_ptp_update_current_time(bp);
2830 				ns = (((u64)BNXT_EVENT_PHC_RTC_UPDATE(data1) <<
2831 				       BNXT_PHC_BITS) | ptp->current_time);
2832 				write_seqlock_irqsave(&ptp->ptp_lock, flags);
2833 				bnxt_ptp_rtc_timecounter_init(ptp, ns);
2834 				write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
2835 			}
2836 			break;
2837 		}
2838 		goto async_event_process_exit;
2839 	}
2840 	case ASYNC_EVENT_CMPL_EVENT_ID_DEFERRED_RESPONSE: {
2841 		u16 seq_id = le32_to_cpu(cmpl->event_data2) & 0xffff;
2842 
2843 		hwrm_update_token(bp, seq_id, BNXT_HWRM_DEFERRED);
2844 		goto async_event_process_exit;
2845 	}
2846 	case ASYNC_EVENT_CMPL_EVENT_ID_DBG_BUF_PRODUCER: {
2847 		u16 type = (u16)BNXT_EVENT_BUF_PRODUCER_TYPE(data1);
2848 		u32 offset =  BNXT_EVENT_BUF_PRODUCER_OFFSET(data2);
2849 
2850 		bnxt_bs_trace_check_wrap(&bp->bs_trace[type], offset);
2851 		goto async_event_process_exit;
2852 	}
2853 	default:
2854 		goto async_event_process_exit;
2855 	}
2856 	__bnxt_queue_sp_work(bp);
2857 async_event_process_exit:
2858 	return 0;
2859 }
2860 
2861 static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
2862 {
2863 	u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
2864 	struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
2865 	struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
2866 				(struct hwrm_fwd_req_cmpl *)txcmp;
2867 
2868 	switch (cmpl_type) {
2869 	case CMPL_BASE_TYPE_HWRM_DONE:
2870 		seq_id = le16_to_cpu(h_cmpl->sequence_id);
2871 		hwrm_update_token(bp, seq_id, BNXT_HWRM_COMPLETE);
2872 		break;
2873 
2874 	case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2875 		vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
2876 
2877 		if ((vf_id < bp->pf.first_vf_id) ||
2878 		    (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
2879 			netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
2880 				   vf_id);
2881 			return -EINVAL;
2882 		}
2883 
2884 		set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
2885 		bnxt_queue_sp_work(bp, BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT);
2886 		break;
2887 
2888 	case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2889 		bnxt_async_event_process(bp,
2890 					 (struct hwrm_async_event_cmpl *)txcmp);
2891 		break;
2892 
2893 	default:
2894 		break;
2895 	}
2896 
2897 	return 0;
2898 }
2899 
2900 static bool bnxt_vnic_is_active(struct bnxt *bp)
2901 {
2902 	struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
2903 
2904 	return vnic->fw_vnic_id != INVALID_HW_RING_ID && vnic->mru > 0;
2905 }
2906 
2907 static irqreturn_t bnxt_msix(int irq, void *dev_instance)
2908 {
2909 	struct bnxt_napi *bnapi = dev_instance;
2910 	struct bnxt *bp = bnapi->bp;
2911 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
2912 	u32 cons = RING_CMP(cpr->cp_raw_cons);
2913 
2914 	cpr->event_ctr++;
2915 	prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
2916 	napi_schedule(&bnapi->napi);
2917 	return IRQ_HANDLED;
2918 }
2919 
2920 static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
2921 {
2922 	u32 raw_cons = cpr->cp_raw_cons;
2923 	u16 cons = RING_CMP(raw_cons);
2924 	struct tx_cmp *txcmp;
2925 
2926 	txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2927 
2928 	return TX_CMP_VALID(txcmp, raw_cons);
2929 }
2930 
2931 static int __bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
2932 			    int budget)
2933 {
2934 	struct bnxt_napi *bnapi = cpr->bnapi;
2935 	u32 raw_cons = cpr->cp_raw_cons;
2936 	u32 cons;
2937 	int rx_pkts = 0;
2938 	u8 event = 0;
2939 	struct tx_cmp *txcmp;
2940 
2941 	cpr->has_more_work = 0;
2942 	cpr->had_work_done = 1;
2943 	while (1) {
2944 		u8 cmp_type;
2945 		int rc;
2946 
2947 		cons = RING_CMP(raw_cons);
2948 		txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
2949 
2950 		if (!TX_CMP_VALID(txcmp, raw_cons))
2951 			break;
2952 
2953 		/* The valid test of the entry must be done first before
2954 		 * reading any further.
2955 		 */
2956 		dma_rmb();
2957 		cmp_type = TX_CMP_TYPE(txcmp);
2958 		if (cmp_type == CMP_TYPE_TX_L2_CMP ||
2959 		    cmp_type == CMP_TYPE_TX_L2_COAL_CMP) {
2960 			u32 opaque = txcmp->tx_cmp_opaque;
2961 			struct bnxt_tx_ring_info *txr;
2962 			u16 tx_freed;
2963 
2964 			txr = bnapi->tx_ring[TX_OPAQUE_RING(opaque)];
2965 			event |= BNXT_TX_CMP_EVENT;
2966 			if (cmp_type == CMP_TYPE_TX_L2_COAL_CMP)
2967 				txr->tx_hw_cons = TX_CMP_SQ_CONS_IDX(txcmp);
2968 			else
2969 				txr->tx_hw_cons = TX_OPAQUE_PROD(bp, opaque);
2970 			tx_freed = (txr->tx_hw_cons - txr->tx_cons) &
2971 				   bp->tx_ring_mask;
2972 			/* return full budget so NAPI will complete. */
2973 			if (unlikely(tx_freed >= bp->tx_wake_thresh)) {
2974 				rx_pkts = budget;
2975 				raw_cons = NEXT_RAW_CMP(raw_cons);
2976 				if (budget)
2977 					cpr->has_more_work = 1;
2978 				break;
2979 			}
2980 		} else if (cmp_type == CMP_TYPE_TX_L2_PKT_TS_CMP) {
2981 			bnxt_tx_ts_cmp(bp, bnapi, (struct tx_ts_cmp *)txcmp);
2982 		} else if (cmp_type >= CMP_TYPE_RX_L2_CMP &&
2983 			   cmp_type <= CMP_TYPE_RX_L2_TPA_START_V3_CMP) {
2984 			if (likely(budget))
2985 				rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
2986 			else
2987 				rc = bnxt_force_rx_discard(bp, cpr, &raw_cons,
2988 							   &event);
2989 			if (likely(rc >= 0))
2990 				rx_pkts += rc;
2991 			/* Increment rx_pkts when rc is -ENOMEM to count towards
2992 			 * the NAPI budget.  Otherwise, we may potentially loop
2993 			 * here forever if we consistently cannot allocate
2994 			 * buffers.
2995 			 */
2996 			else if (rc == -ENOMEM && budget)
2997 				rx_pkts++;
2998 			else if (rc == -EBUSY)	/* partial completion */
2999 				break;
3000 		} else if (unlikely(cmp_type == CMPL_BASE_TYPE_HWRM_DONE ||
3001 				    cmp_type == CMPL_BASE_TYPE_HWRM_FWD_REQ ||
3002 				    cmp_type == CMPL_BASE_TYPE_HWRM_ASYNC_EVENT)) {
3003 			bnxt_hwrm_handler(bp, txcmp);
3004 		}
3005 		raw_cons = NEXT_RAW_CMP(raw_cons);
3006 
3007 		if (rx_pkts && rx_pkts == budget) {
3008 			cpr->has_more_work = 1;
3009 			break;
3010 		}
3011 	}
3012 
3013 	if (event & BNXT_REDIRECT_EVENT) {
3014 		xdp_do_flush();
3015 		event &= ~BNXT_REDIRECT_EVENT;
3016 	}
3017 
3018 	if (event & BNXT_TX_EVENT) {
3019 		struct bnxt_tx_ring_info *txr = bnapi->tx_ring[0];
3020 		u16 prod = txr->tx_prod;
3021 
3022 		/* Sync BD data before updating doorbell */
3023 		wmb();
3024 
3025 		bnxt_db_write_relaxed(bp, &txr->tx_db, prod);
3026 		event &= ~BNXT_TX_EVENT;
3027 	}
3028 
3029 	cpr->cp_raw_cons = raw_cons;
3030 	bnapi->events |= event;
3031 	return rx_pkts;
3032 }
3033 
3034 static void __bnxt_poll_work_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3035 				  int budget)
3036 {
3037 	if ((bnapi->events & BNXT_TX_CMP_EVENT) && !bnapi->tx_fault)
3038 		bnapi->tx_int(bp, bnapi, budget);
3039 
3040 	if ((bnapi->events & BNXT_RX_EVENT) && !(bnapi->in_reset)) {
3041 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3042 
3043 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3044 		bnapi->events &= ~BNXT_RX_EVENT;
3045 	}
3046 	if (bnapi->events & BNXT_AGG_EVENT) {
3047 		struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3048 
3049 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3050 		bnapi->events &= ~BNXT_AGG_EVENT;
3051 	}
3052 }
3053 
3054 static int bnxt_poll_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
3055 			  int budget)
3056 {
3057 	struct bnxt_napi *bnapi = cpr->bnapi;
3058 	int rx_pkts;
3059 
3060 	rx_pkts = __bnxt_poll_work(bp, cpr, budget);
3061 
3062 	/* ACK completion ring before freeing tx ring and producing new
3063 	 * buffers in rx/agg rings to prevent overflowing the completion
3064 	 * ring.
3065 	 */
3066 	bnxt_db_cq(bp, &cpr->cp_db, cpr->cp_raw_cons);
3067 
3068 	__bnxt_poll_work_done(bp, bnapi, budget);
3069 	return rx_pkts;
3070 }
3071 
3072 static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
3073 {
3074 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3075 	struct bnxt *bp = bnapi->bp;
3076 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3077 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
3078 	struct tx_cmp *txcmp;
3079 	struct rx_cmp_ext *rxcmp1;
3080 	u32 cp_cons, tmp_raw_cons;
3081 	u32 raw_cons = cpr->cp_raw_cons;
3082 	bool flush_xdp = false;
3083 	u32 rx_pkts = 0;
3084 	u8 event = 0;
3085 
3086 	while (1) {
3087 		int rc;
3088 
3089 		cp_cons = RING_CMP(raw_cons);
3090 		txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3091 
3092 		if (!TX_CMP_VALID(txcmp, raw_cons))
3093 			break;
3094 
3095 		/* The valid test of the entry must be done first before
3096 		 * reading any further.
3097 		 */
3098 		dma_rmb();
3099 		if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
3100 			tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
3101 			cp_cons = RING_CMP(tmp_raw_cons);
3102 			rxcmp1 = (struct rx_cmp_ext *)
3103 			  &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
3104 
3105 			if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
3106 				break;
3107 
3108 			/* force an error to recycle the buffer */
3109 			rxcmp1->rx_cmp_cfa_code_errors_v2 |=
3110 				cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
3111 
3112 			rc = bnxt_rx_pkt(bp, cpr, &raw_cons, &event);
3113 			if (likely(rc == -EIO) && budget)
3114 				rx_pkts++;
3115 			else if (rc == -EBUSY)	/* partial completion */
3116 				break;
3117 			if (event & BNXT_REDIRECT_EVENT)
3118 				flush_xdp = true;
3119 		} else if (unlikely(TX_CMP_TYPE(txcmp) ==
3120 				    CMPL_BASE_TYPE_HWRM_DONE)) {
3121 			bnxt_hwrm_handler(bp, txcmp);
3122 		} else {
3123 			netdev_err(bp->dev,
3124 				   "Invalid completion received on special ring\n");
3125 		}
3126 		raw_cons = NEXT_RAW_CMP(raw_cons);
3127 
3128 		if (rx_pkts == budget)
3129 			break;
3130 	}
3131 
3132 	cpr->cp_raw_cons = raw_cons;
3133 	BNXT_DB_CQ(&cpr->cp_db, cpr->cp_raw_cons);
3134 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
3135 
3136 	if (event & BNXT_AGG_EVENT)
3137 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
3138 	if (flush_xdp)
3139 		xdp_do_flush();
3140 
3141 	if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
3142 		napi_complete_done(napi, rx_pkts);
3143 		BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3144 	}
3145 	return rx_pkts;
3146 }
3147 
3148 static int bnxt_poll(struct napi_struct *napi, int budget)
3149 {
3150 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3151 	struct bnxt *bp = bnapi->bp;
3152 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3153 	int work_done = 0;
3154 
3155 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3156 		napi_complete(napi);
3157 		return 0;
3158 	}
3159 	while (1) {
3160 		work_done += bnxt_poll_work(bp, cpr, budget - work_done);
3161 
3162 		if (work_done >= budget) {
3163 			if (!budget)
3164 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3165 			break;
3166 		}
3167 
3168 		if (!bnxt_has_work(bp, cpr)) {
3169 			if (napi_complete_done(napi, work_done))
3170 				BNXT_DB_CQ_ARM(&cpr->cp_db, cpr->cp_raw_cons);
3171 			break;
3172 		}
3173 	}
3174 	if ((bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3175 		struct dim_sample dim_sample = {};
3176 
3177 		dim_update_sample(cpr->event_ctr,
3178 				  cpr->rx_packets,
3179 				  cpr->rx_bytes,
3180 				  &dim_sample);
3181 		net_dim(&cpr->dim, &dim_sample);
3182 	}
3183 	return work_done;
3184 }
3185 
3186 static int __bnxt_poll_cqs(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
3187 {
3188 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3189 	int i, work_done = 0;
3190 
3191 	for (i = 0; i < cpr->cp_ring_count; i++) {
3192 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3193 
3194 		if (cpr2->had_nqe_notify) {
3195 			work_done += __bnxt_poll_work(bp, cpr2,
3196 						      budget - work_done);
3197 			cpr->has_more_work |= cpr2->has_more_work;
3198 		}
3199 	}
3200 	return work_done;
3201 }
3202 
3203 static void __bnxt_poll_cqs_done(struct bnxt *bp, struct bnxt_napi *bnapi,
3204 				 u64 dbr_type, int budget)
3205 {
3206 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3207 	int i;
3208 
3209 	for (i = 0; i < cpr->cp_ring_count; i++) {
3210 		struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[i];
3211 		struct bnxt_db_info *db;
3212 
3213 		if (cpr2->had_work_done) {
3214 			u32 tgl = 0;
3215 
3216 			if (dbr_type == DBR_TYPE_CQ_ARMALL) {
3217 				cpr2->had_nqe_notify = 0;
3218 				tgl = cpr2->toggle;
3219 			}
3220 			db = &cpr2->cp_db;
3221 			bnxt_writeq(bp,
3222 				    db->db_key64 | dbr_type | DB_TOGGLE(tgl) |
3223 				    DB_RING_IDX(db, cpr2->cp_raw_cons),
3224 				    db->doorbell);
3225 			cpr2->had_work_done = 0;
3226 		}
3227 	}
3228 	__bnxt_poll_work_done(bp, bnapi, budget);
3229 }
3230 
3231 static int bnxt_poll_p5(struct napi_struct *napi, int budget)
3232 {
3233 	struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
3234 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
3235 	struct bnxt_cp_ring_info *cpr_rx;
3236 	u32 raw_cons = cpr->cp_raw_cons;
3237 	struct bnxt *bp = bnapi->bp;
3238 	struct nqe_cn *nqcmp;
3239 	int work_done = 0;
3240 	u32 cons;
3241 
3242 	if (unlikely(test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))) {
3243 		napi_complete(napi);
3244 		return 0;
3245 	}
3246 	if (cpr->has_more_work) {
3247 		cpr->has_more_work = 0;
3248 		work_done = __bnxt_poll_cqs(bp, bnapi, budget);
3249 	}
3250 	while (1) {
3251 		u16 type;
3252 
3253 		cons = RING_CMP(raw_cons);
3254 		nqcmp = &cpr->nq_desc_ring[CP_RING(cons)][CP_IDX(cons)];
3255 
3256 		if (!NQ_CMP_VALID(nqcmp, raw_cons)) {
3257 			if (cpr->has_more_work)
3258 				break;
3259 
3260 			__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ_ARMALL,
3261 					     budget);
3262 			cpr->cp_raw_cons = raw_cons;
3263 			if (napi_complete_done(napi, work_done))
3264 				BNXT_DB_NQ_ARM_P5(&cpr->cp_db,
3265 						  cpr->cp_raw_cons);
3266 			goto poll_done;
3267 		}
3268 
3269 		/* The valid test of the entry must be done first before
3270 		 * reading any further.
3271 		 */
3272 		dma_rmb();
3273 
3274 		type = le16_to_cpu(nqcmp->type);
3275 		if (NQE_CN_TYPE(type) == NQ_CN_TYPE_CQ_NOTIFICATION) {
3276 			u32 idx = le32_to_cpu(nqcmp->cq_handle_low);
3277 			u32 cq_type = BNXT_NQ_HDL_TYPE(idx);
3278 			struct bnxt_cp_ring_info *cpr2;
3279 
3280 			/* No more budget for RX work */
3281 			if (budget && work_done >= budget &&
3282 			    cq_type == BNXT_NQ_HDL_TYPE_RX)
3283 				break;
3284 
3285 			idx = BNXT_NQ_HDL_IDX(idx);
3286 			cpr2 = &cpr->cp_ring_arr[idx];
3287 			cpr2->had_nqe_notify = 1;
3288 			cpr2->toggle = NQE_CN_TOGGLE(type);
3289 			work_done += __bnxt_poll_work(bp, cpr2,
3290 						      budget - work_done);
3291 			cpr->has_more_work |= cpr2->has_more_work;
3292 		} else {
3293 			bnxt_hwrm_handler(bp, (struct tx_cmp *)nqcmp);
3294 		}
3295 		raw_cons = NEXT_RAW_CMP(raw_cons);
3296 	}
3297 	__bnxt_poll_cqs_done(bp, bnapi, DBR_TYPE_CQ, budget);
3298 	if (raw_cons != cpr->cp_raw_cons) {
3299 		cpr->cp_raw_cons = raw_cons;
3300 		BNXT_DB_NQ_P5(&cpr->cp_db, raw_cons);
3301 	}
3302 poll_done:
3303 	cpr_rx = &cpr->cp_ring_arr[0];
3304 	if (cpr_rx->cp_ring_type == BNXT_NQ_HDL_TYPE_RX &&
3305 	    (bp->flags & BNXT_FLAG_DIM) && bnxt_vnic_is_active(bp)) {
3306 		struct dim_sample dim_sample = {};
3307 
3308 		dim_update_sample(cpr->event_ctr,
3309 				  cpr_rx->rx_packets,
3310 				  cpr_rx->rx_bytes,
3311 				  &dim_sample);
3312 		net_dim(&cpr->dim, &dim_sample);
3313 	}
3314 	return work_done;
3315 }
3316 
3317 static void bnxt_free_tx_skbs(struct bnxt *bp)
3318 {
3319 	int i, max_idx;
3320 	struct pci_dev *pdev = bp->pdev;
3321 
3322 	if (!bp->tx_ring)
3323 		return;
3324 
3325 	max_idx = bp->tx_nr_pages * TX_DESC_CNT;
3326 	for (i = 0; i < bp->tx_nr_rings; i++) {
3327 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3328 		int j;
3329 
3330 		if (!txr->tx_buf_ring)
3331 			continue;
3332 
3333 		for (j = 0; j < max_idx;) {
3334 			struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
3335 			struct sk_buff *skb;
3336 			int k, last;
3337 
3338 			if (i < bp->tx_nr_rings_xdp &&
3339 			    tx_buf->action == XDP_REDIRECT) {
3340 				dma_unmap_single(&pdev->dev,
3341 					dma_unmap_addr(tx_buf, mapping),
3342 					dma_unmap_len(tx_buf, len),
3343 					DMA_TO_DEVICE);
3344 				xdp_return_frame(tx_buf->xdpf);
3345 				tx_buf->action = 0;
3346 				tx_buf->xdpf = NULL;
3347 				j++;
3348 				continue;
3349 			}
3350 
3351 			skb = tx_buf->skb;
3352 			if (!skb) {
3353 				j++;
3354 				continue;
3355 			}
3356 
3357 			tx_buf->skb = NULL;
3358 
3359 			if (tx_buf->is_push) {
3360 				dev_kfree_skb(skb);
3361 				j += 2;
3362 				continue;
3363 			}
3364 
3365 			dma_unmap_single(&pdev->dev,
3366 					 dma_unmap_addr(tx_buf, mapping),
3367 					 skb_headlen(skb),
3368 					 DMA_TO_DEVICE);
3369 
3370 			last = tx_buf->nr_frags;
3371 			j += 2;
3372 			for (k = 0; k < last; k++, j++) {
3373 				int ring_idx = j & bp->tx_ring_mask;
3374 				skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
3375 
3376 				tx_buf = &txr->tx_buf_ring[ring_idx];
3377 				dma_unmap_page(
3378 					&pdev->dev,
3379 					dma_unmap_addr(tx_buf, mapping),
3380 					skb_frag_size(frag), DMA_TO_DEVICE);
3381 			}
3382 			dev_kfree_skb(skb);
3383 		}
3384 		netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
3385 	}
3386 }
3387 
3388 static void bnxt_free_one_rx_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3389 {
3390 	int i, max_idx;
3391 
3392 	max_idx = bp->rx_nr_pages * RX_DESC_CNT;
3393 
3394 	for (i = 0; i < max_idx; i++) {
3395 		struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[i];
3396 		void *data = rx_buf->data;
3397 
3398 		if (!data)
3399 			continue;
3400 
3401 		rx_buf->data = NULL;
3402 		if (BNXT_RX_PAGE_MODE(bp))
3403 			page_pool_recycle_direct(rxr->page_pool, data);
3404 		else
3405 			page_pool_free_va(rxr->head_pool, data, true);
3406 	}
3407 }
3408 
3409 static void bnxt_free_one_rx_agg_ring(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3410 {
3411 	int i, max_idx;
3412 
3413 	max_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
3414 
3415 	for (i = 0; i < max_idx; i++) {
3416 		struct bnxt_sw_rx_agg_bd *rx_agg_buf = &rxr->rx_agg_ring[i];
3417 		struct page *page = rx_agg_buf->page;
3418 
3419 		if (!page)
3420 			continue;
3421 
3422 		rx_agg_buf->page = NULL;
3423 		__clear_bit(i, rxr->rx_agg_bmap);
3424 
3425 		page_pool_recycle_direct(rxr->page_pool, page);
3426 	}
3427 }
3428 
3429 static void bnxt_free_one_tpa_info_data(struct bnxt *bp,
3430 					struct bnxt_rx_ring_info *rxr)
3431 {
3432 	int i;
3433 
3434 	for (i = 0; i < bp->max_tpa; i++) {
3435 		struct bnxt_tpa_info *tpa_info = &rxr->rx_tpa[i];
3436 		u8 *data = tpa_info->data;
3437 
3438 		if (!data)
3439 			continue;
3440 
3441 		tpa_info->data = NULL;
3442 		page_pool_free_va(rxr->head_pool, data, false);
3443 	}
3444 }
3445 
3446 static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp,
3447 				       struct bnxt_rx_ring_info *rxr)
3448 {
3449 	struct bnxt_tpa_idx_map *map;
3450 
3451 	if (!rxr->rx_tpa)
3452 		goto skip_rx_tpa_free;
3453 
3454 	bnxt_free_one_tpa_info_data(bp, rxr);
3455 
3456 skip_rx_tpa_free:
3457 	if (!rxr->rx_buf_ring)
3458 		goto skip_rx_buf_free;
3459 
3460 	bnxt_free_one_rx_ring(bp, rxr);
3461 
3462 skip_rx_buf_free:
3463 	if (!rxr->rx_agg_ring)
3464 		goto skip_rx_agg_free;
3465 
3466 	bnxt_free_one_rx_agg_ring(bp, rxr);
3467 
3468 skip_rx_agg_free:
3469 	map = rxr->rx_tpa_idx_map;
3470 	if (map)
3471 		memset(map->agg_idx_bmap, 0, sizeof(map->agg_idx_bmap));
3472 }
3473 
3474 static void bnxt_free_rx_skbs(struct bnxt *bp)
3475 {
3476 	int i;
3477 
3478 	if (!bp->rx_ring)
3479 		return;
3480 
3481 	for (i = 0; i < bp->rx_nr_rings; i++)
3482 		bnxt_free_one_rx_ring_skbs(bp, &bp->rx_ring[i]);
3483 }
3484 
3485 static void bnxt_free_skbs(struct bnxt *bp)
3486 {
3487 	bnxt_free_tx_skbs(bp);
3488 	bnxt_free_rx_skbs(bp);
3489 }
3490 
3491 static void bnxt_init_ctx_mem(struct bnxt_ctx_mem_type *ctxm, void *p, int len)
3492 {
3493 	u8 init_val = ctxm->init_value;
3494 	u16 offset = ctxm->init_offset;
3495 	u8 *p2 = p;
3496 	int i;
3497 
3498 	if (!init_val)
3499 		return;
3500 	if (offset == BNXT_CTX_INIT_INVALID_OFFSET) {
3501 		memset(p, init_val, len);
3502 		return;
3503 	}
3504 	for (i = 0; i < len; i += ctxm->entry_size)
3505 		*(p2 + i + offset) = init_val;
3506 }
3507 
3508 static size_t __bnxt_copy_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem,
3509 			       void *buf, size_t offset, size_t head,
3510 			       size_t tail)
3511 {
3512 	int i, head_page, start_idx, source_offset;
3513 	size_t len, rem_len, total_len, max_bytes;
3514 
3515 	head_page = head / rmem->page_size;
3516 	source_offset = head % rmem->page_size;
3517 	total_len = (tail - head) & MAX_CTX_BYTES_MASK;
3518 	if (!total_len)
3519 		total_len = MAX_CTX_BYTES;
3520 	start_idx = head_page % MAX_CTX_PAGES;
3521 	max_bytes = (rmem->nr_pages - start_idx) * rmem->page_size -
3522 		    source_offset;
3523 	total_len = min(total_len, max_bytes);
3524 	rem_len = total_len;
3525 
3526 	for (i = start_idx; rem_len; i++, source_offset = 0) {
3527 		len = min((size_t)(rmem->page_size - source_offset), rem_len);
3528 		if (buf)
3529 			memcpy(buf + offset, rmem->pg_arr[i] + source_offset,
3530 			       len);
3531 		offset += len;
3532 		rem_len -= len;
3533 	}
3534 	return total_len;
3535 }
3536 
3537 static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3538 {
3539 	struct pci_dev *pdev = bp->pdev;
3540 	int i;
3541 
3542 	if (!rmem->pg_arr)
3543 		goto skip_pages;
3544 
3545 	for (i = 0; i < rmem->nr_pages; i++) {
3546 		if (!rmem->pg_arr[i])
3547 			continue;
3548 
3549 		dma_free_coherent(&pdev->dev, rmem->page_size,
3550 				  rmem->pg_arr[i], rmem->dma_arr[i]);
3551 
3552 		rmem->pg_arr[i] = NULL;
3553 	}
3554 skip_pages:
3555 	if (rmem->pg_tbl) {
3556 		size_t pg_tbl_size = rmem->nr_pages * 8;
3557 
3558 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3559 			pg_tbl_size = rmem->page_size;
3560 		dma_free_coherent(&pdev->dev, pg_tbl_size,
3561 				  rmem->pg_tbl, rmem->pg_tbl_map);
3562 		rmem->pg_tbl = NULL;
3563 	}
3564 	if (rmem->vmem_size && *rmem->vmem) {
3565 		vfree(*rmem->vmem);
3566 		*rmem->vmem = NULL;
3567 	}
3568 }
3569 
3570 static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_mem_info *rmem)
3571 {
3572 	struct pci_dev *pdev = bp->pdev;
3573 	u64 valid_bit = 0;
3574 	int i;
3575 
3576 	if (rmem->flags & (BNXT_RMEM_VALID_PTE_FLAG | BNXT_RMEM_RING_PTE_FLAG))
3577 		valid_bit = PTU_PTE_VALID;
3578 	if ((rmem->nr_pages > 1 || rmem->depth > 0) && !rmem->pg_tbl) {
3579 		size_t pg_tbl_size = rmem->nr_pages * 8;
3580 
3581 		if (rmem->flags & BNXT_RMEM_USE_FULL_PAGE_FLAG)
3582 			pg_tbl_size = rmem->page_size;
3583 		rmem->pg_tbl = dma_alloc_coherent(&pdev->dev, pg_tbl_size,
3584 						  &rmem->pg_tbl_map,
3585 						  GFP_KERNEL);
3586 		if (!rmem->pg_tbl)
3587 			return -ENOMEM;
3588 	}
3589 
3590 	for (i = 0; i < rmem->nr_pages; i++) {
3591 		u64 extra_bits = valid_bit;
3592 
3593 		rmem->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
3594 						     rmem->page_size,
3595 						     &rmem->dma_arr[i],
3596 						     GFP_KERNEL);
3597 		if (!rmem->pg_arr[i])
3598 			return -ENOMEM;
3599 
3600 		if (rmem->ctx_mem)
3601 			bnxt_init_ctx_mem(rmem->ctx_mem, rmem->pg_arr[i],
3602 					  rmem->page_size);
3603 		if (rmem->nr_pages > 1 || rmem->depth > 0) {
3604 			if (i == rmem->nr_pages - 2 &&
3605 			    (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3606 				extra_bits |= PTU_PTE_NEXT_TO_LAST;
3607 			else if (i == rmem->nr_pages - 1 &&
3608 				 (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
3609 				extra_bits |= PTU_PTE_LAST;
3610 			rmem->pg_tbl[i] =
3611 				cpu_to_le64(rmem->dma_arr[i] | extra_bits);
3612 		}
3613 	}
3614 
3615 	if (rmem->vmem_size) {
3616 		*rmem->vmem = vzalloc(rmem->vmem_size);
3617 		if (!(*rmem->vmem))
3618 			return -ENOMEM;
3619 	}
3620 	return 0;
3621 }
3622 
3623 static void bnxt_free_one_tpa_info(struct bnxt *bp,
3624 				   struct bnxt_rx_ring_info *rxr)
3625 {
3626 	int i;
3627 
3628 	kfree(rxr->rx_tpa_idx_map);
3629 	rxr->rx_tpa_idx_map = NULL;
3630 	if (rxr->rx_tpa) {
3631 		for (i = 0; i < bp->max_tpa; i++) {
3632 			kfree(rxr->rx_tpa[i].agg_arr);
3633 			rxr->rx_tpa[i].agg_arr = NULL;
3634 		}
3635 	}
3636 	kfree(rxr->rx_tpa);
3637 	rxr->rx_tpa = NULL;
3638 }
3639 
3640 static void bnxt_free_tpa_info(struct bnxt *bp)
3641 {
3642 	int i;
3643 
3644 	for (i = 0; i < bp->rx_nr_rings; i++) {
3645 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3646 
3647 		bnxt_free_one_tpa_info(bp, rxr);
3648 	}
3649 }
3650 
3651 static int bnxt_alloc_one_tpa_info(struct bnxt *bp,
3652 				   struct bnxt_rx_ring_info *rxr)
3653 {
3654 	struct rx_agg_cmp *agg;
3655 	int i;
3656 
3657 	rxr->rx_tpa = kcalloc(bp->max_tpa, sizeof(struct bnxt_tpa_info),
3658 			      GFP_KERNEL);
3659 	if (!rxr->rx_tpa)
3660 		return -ENOMEM;
3661 
3662 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
3663 		return 0;
3664 	for (i = 0; i < bp->max_tpa; i++) {
3665 		agg = kcalloc(MAX_SKB_FRAGS, sizeof(*agg), GFP_KERNEL);
3666 		if (!agg)
3667 			return -ENOMEM;
3668 		rxr->rx_tpa[i].agg_arr = agg;
3669 	}
3670 	rxr->rx_tpa_idx_map = kzalloc(sizeof(*rxr->rx_tpa_idx_map),
3671 				      GFP_KERNEL);
3672 	if (!rxr->rx_tpa_idx_map)
3673 		return -ENOMEM;
3674 
3675 	return 0;
3676 }
3677 
3678 static int bnxt_alloc_tpa_info(struct bnxt *bp)
3679 {
3680 	int i, rc;
3681 
3682 	bp->max_tpa = MAX_TPA;
3683 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
3684 		if (!bp->max_tpa_v2)
3685 			return 0;
3686 		bp->max_tpa = max_t(u16, bp->max_tpa_v2, MAX_TPA_P5);
3687 	}
3688 
3689 	for (i = 0; i < bp->rx_nr_rings; i++) {
3690 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3691 
3692 		rc = bnxt_alloc_one_tpa_info(bp, rxr);
3693 		if (rc)
3694 			return rc;
3695 	}
3696 	return 0;
3697 }
3698 
3699 static void bnxt_free_rx_rings(struct bnxt *bp)
3700 {
3701 	int i;
3702 
3703 	if (!bp->rx_ring)
3704 		return;
3705 
3706 	bnxt_free_tpa_info(bp);
3707 	for (i = 0; i < bp->rx_nr_rings; i++) {
3708 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3709 		struct bnxt_ring_struct *ring;
3710 
3711 		if (rxr->xdp_prog)
3712 			bpf_prog_put(rxr->xdp_prog);
3713 
3714 		if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
3715 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3716 
3717 		page_pool_destroy(rxr->page_pool);
3718 		if (bnxt_separate_head_pool())
3719 			page_pool_destroy(rxr->head_pool);
3720 		rxr->page_pool = rxr->head_pool = NULL;
3721 
3722 		kfree(rxr->rx_agg_bmap);
3723 		rxr->rx_agg_bmap = NULL;
3724 
3725 		ring = &rxr->rx_ring_struct;
3726 		bnxt_free_ring(bp, &ring->ring_mem);
3727 
3728 		ring = &rxr->rx_agg_ring_struct;
3729 		bnxt_free_ring(bp, &ring->ring_mem);
3730 	}
3731 }
3732 
3733 static int bnxt_alloc_rx_page_pool(struct bnxt *bp,
3734 				   struct bnxt_rx_ring_info *rxr,
3735 				   int numa_node)
3736 {
3737 	struct page_pool_params pp = { 0 };
3738 	struct page_pool *pool;
3739 
3740 	pp.pool_size = bp->rx_agg_ring_size;
3741 	if (BNXT_RX_PAGE_MODE(bp))
3742 		pp.pool_size += bp->rx_ring_size;
3743 	pp.nid = numa_node;
3744 	pp.napi = &rxr->bnapi->napi;
3745 	pp.netdev = bp->dev;
3746 	pp.dev = &bp->pdev->dev;
3747 	pp.dma_dir = bp->rx_dir;
3748 	pp.max_len = PAGE_SIZE;
3749 	pp.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
3750 
3751 	pool = page_pool_create(&pp);
3752 	if (IS_ERR(pool))
3753 		return PTR_ERR(pool);
3754 	rxr->page_pool = pool;
3755 
3756 	if (bnxt_separate_head_pool()) {
3757 		pp.pool_size = max(bp->rx_ring_size, 1024);
3758 		pool = page_pool_create(&pp);
3759 		if (IS_ERR(pool))
3760 			goto err_destroy_pp;
3761 	}
3762 	rxr->head_pool = pool;
3763 
3764 	return 0;
3765 
3766 err_destroy_pp:
3767 	page_pool_destroy(rxr->page_pool);
3768 	rxr->page_pool = NULL;
3769 	return PTR_ERR(pool);
3770 }
3771 
3772 static int bnxt_alloc_rx_agg_bmap(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
3773 {
3774 	u16 mem_size;
3775 
3776 	rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
3777 	mem_size = rxr->rx_agg_bmap_size / 8;
3778 	rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
3779 	if (!rxr->rx_agg_bmap)
3780 		return -ENOMEM;
3781 
3782 	return 0;
3783 }
3784 
3785 static int bnxt_alloc_rx_rings(struct bnxt *bp)
3786 {
3787 	int numa_node = dev_to_node(&bp->pdev->dev);
3788 	int i, rc = 0, agg_rings = 0, cpu;
3789 
3790 	if (!bp->rx_ring)
3791 		return -ENOMEM;
3792 
3793 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
3794 		agg_rings = 1;
3795 
3796 	for (i = 0; i < bp->rx_nr_rings; i++) {
3797 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
3798 		struct bnxt_ring_struct *ring;
3799 		int cpu_node;
3800 
3801 		ring = &rxr->rx_ring_struct;
3802 
3803 		cpu = cpumask_local_spread(i, numa_node);
3804 		cpu_node = cpu_to_node(cpu);
3805 		netdev_dbg(bp->dev, "Allocating page pool for rx_ring[%d] on numa_node: %d\n",
3806 			   i, cpu_node);
3807 		rc = bnxt_alloc_rx_page_pool(bp, rxr, cpu_node);
3808 		if (rc)
3809 			return rc;
3810 
3811 		rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i, 0);
3812 		if (rc < 0)
3813 			return rc;
3814 
3815 		rc = xdp_rxq_info_reg_mem_model(&rxr->xdp_rxq,
3816 						MEM_TYPE_PAGE_POOL,
3817 						rxr->page_pool);
3818 		if (rc) {
3819 			xdp_rxq_info_unreg(&rxr->xdp_rxq);
3820 			return rc;
3821 		}
3822 
3823 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3824 		if (rc)
3825 			return rc;
3826 
3827 		ring->grp_idx = i;
3828 		if (agg_rings) {
3829 			ring = &rxr->rx_agg_ring_struct;
3830 			rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3831 			if (rc)
3832 				return rc;
3833 
3834 			ring->grp_idx = i;
3835 			rc = bnxt_alloc_rx_agg_bmap(bp, rxr);
3836 			if (rc)
3837 				return rc;
3838 		}
3839 	}
3840 	if (bp->flags & BNXT_FLAG_TPA)
3841 		rc = bnxt_alloc_tpa_info(bp);
3842 	return rc;
3843 }
3844 
3845 static void bnxt_free_tx_rings(struct bnxt *bp)
3846 {
3847 	int i;
3848 	struct pci_dev *pdev = bp->pdev;
3849 
3850 	if (!bp->tx_ring)
3851 		return;
3852 
3853 	for (i = 0; i < bp->tx_nr_rings; i++) {
3854 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3855 		struct bnxt_ring_struct *ring;
3856 
3857 		if (txr->tx_push) {
3858 			dma_free_coherent(&pdev->dev, bp->tx_push_size,
3859 					  txr->tx_push, txr->tx_push_mapping);
3860 			txr->tx_push = NULL;
3861 		}
3862 
3863 		ring = &txr->tx_ring_struct;
3864 
3865 		bnxt_free_ring(bp, &ring->ring_mem);
3866 	}
3867 }
3868 
3869 #define BNXT_TC_TO_RING_BASE(bp, tc)	\
3870 	((tc) * (bp)->tx_nr_rings_per_tc)
3871 
3872 #define BNXT_RING_TO_TC_OFF(bp, tx)	\
3873 	((tx) % (bp)->tx_nr_rings_per_tc)
3874 
3875 #define BNXT_RING_TO_TC(bp, tx)		\
3876 	((tx) / (bp)->tx_nr_rings_per_tc)
3877 
3878 static int bnxt_alloc_tx_rings(struct bnxt *bp)
3879 {
3880 	int i, j, rc;
3881 	struct pci_dev *pdev = bp->pdev;
3882 
3883 	bp->tx_push_size = 0;
3884 	if (bp->tx_push_thresh) {
3885 		int push_size;
3886 
3887 		push_size  = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
3888 					bp->tx_push_thresh);
3889 
3890 		if (push_size > 256) {
3891 			push_size = 0;
3892 			bp->tx_push_thresh = 0;
3893 		}
3894 
3895 		bp->tx_push_size = push_size;
3896 	}
3897 
3898 	for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
3899 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
3900 		struct bnxt_ring_struct *ring;
3901 		u8 qidx;
3902 
3903 		ring = &txr->tx_ring_struct;
3904 
3905 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
3906 		if (rc)
3907 			return rc;
3908 
3909 		ring->grp_idx = txr->bnapi->index;
3910 		if (bp->tx_push_size) {
3911 			dma_addr_t mapping;
3912 
3913 			/* One pre-allocated DMA buffer to backup
3914 			 * TX push operation
3915 			 */
3916 			txr->tx_push = dma_alloc_coherent(&pdev->dev,
3917 						bp->tx_push_size,
3918 						&txr->tx_push_mapping,
3919 						GFP_KERNEL);
3920 
3921 			if (!txr->tx_push)
3922 				return -ENOMEM;
3923 
3924 			mapping = txr->tx_push_mapping +
3925 				sizeof(struct tx_push_bd);
3926 			txr->data_mapping = cpu_to_le64(mapping);
3927 		}
3928 		qidx = bp->tc_to_qidx[j];
3929 		ring->queue_id = bp->q_info[qidx].queue_id;
3930 		spin_lock_init(&txr->xdp_tx_lock);
3931 		if (i < bp->tx_nr_rings_xdp)
3932 			continue;
3933 		if (BNXT_RING_TO_TC_OFF(bp, i) == (bp->tx_nr_rings_per_tc - 1))
3934 			j++;
3935 	}
3936 	return 0;
3937 }
3938 
3939 static void bnxt_free_cp_arrays(struct bnxt_cp_ring_info *cpr)
3940 {
3941 	struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
3942 
3943 	kfree(cpr->cp_desc_ring);
3944 	cpr->cp_desc_ring = NULL;
3945 	ring->ring_mem.pg_arr = NULL;
3946 	kfree(cpr->cp_desc_mapping);
3947 	cpr->cp_desc_mapping = NULL;
3948 	ring->ring_mem.dma_arr = NULL;
3949 }
3950 
3951 static int bnxt_alloc_cp_arrays(struct bnxt_cp_ring_info *cpr, int n)
3952 {
3953 	cpr->cp_desc_ring = kcalloc(n, sizeof(*cpr->cp_desc_ring), GFP_KERNEL);
3954 	if (!cpr->cp_desc_ring)
3955 		return -ENOMEM;
3956 	cpr->cp_desc_mapping = kcalloc(n, sizeof(*cpr->cp_desc_mapping),
3957 				       GFP_KERNEL);
3958 	if (!cpr->cp_desc_mapping)
3959 		return -ENOMEM;
3960 	return 0;
3961 }
3962 
3963 static void bnxt_free_all_cp_arrays(struct bnxt *bp)
3964 {
3965 	int i;
3966 
3967 	if (!bp->bnapi)
3968 		return;
3969 	for (i = 0; i < bp->cp_nr_rings; i++) {
3970 		struct bnxt_napi *bnapi = bp->bnapi[i];
3971 
3972 		if (!bnapi)
3973 			continue;
3974 		bnxt_free_cp_arrays(&bnapi->cp_ring);
3975 	}
3976 }
3977 
3978 static int bnxt_alloc_all_cp_arrays(struct bnxt *bp)
3979 {
3980 	int i, n = bp->cp_nr_pages;
3981 
3982 	for (i = 0; i < bp->cp_nr_rings; i++) {
3983 		struct bnxt_napi *bnapi = bp->bnapi[i];
3984 		int rc;
3985 
3986 		if (!bnapi)
3987 			continue;
3988 		rc = bnxt_alloc_cp_arrays(&bnapi->cp_ring, n);
3989 		if (rc)
3990 			return rc;
3991 	}
3992 	return 0;
3993 }
3994 
3995 static void bnxt_free_cp_rings(struct bnxt *bp)
3996 {
3997 	int i;
3998 
3999 	if (!bp->bnapi)
4000 		return;
4001 
4002 	for (i = 0; i < bp->cp_nr_rings; i++) {
4003 		struct bnxt_napi *bnapi = bp->bnapi[i];
4004 		struct bnxt_cp_ring_info *cpr;
4005 		struct bnxt_ring_struct *ring;
4006 		int j;
4007 
4008 		if (!bnapi)
4009 			continue;
4010 
4011 		cpr = &bnapi->cp_ring;
4012 		ring = &cpr->cp_ring_struct;
4013 
4014 		bnxt_free_ring(bp, &ring->ring_mem);
4015 
4016 		if (!cpr->cp_ring_arr)
4017 			continue;
4018 
4019 		for (j = 0; j < cpr->cp_ring_count; j++) {
4020 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4021 
4022 			ring = &cpr2->cp_ring_struct;
4023 			bnxt_free_ring(bp, &ring->ring_mem);
4024 			bnxt_free_cp_arrays(cpr2);
4025 		}
4026 		kfree(cpr->cp_ring_arr);
4027 		cpr->cp_ring_arr = NULL;
4028 		cpr->cp_ring_count = 0;
4029 	}
4030 }
4031 
4032 static int bnxt_alloc_cp_sub_ring(struct bnxt *bp,
4033 				  struct bnxt_cp_ring_info *cpr)
4034 {
4035 	struct bnxt_ring_mem_info *rmem;
4036 	struct bnxt_ring_struct *ring;
4037 	int rc;
4038 
4039 	rc = bnxt_alloc_cp_arrays(cpr, bp->cp_nr_pages);
4040 	if (rc) {
4041 		bnxt_free_cp_arrays(cpr);
4042 		return -ENOMEM;
4043 	}
4044 	ring = &cpr->cp_ring_struct;
4045 	rmem = &ring->ring_mem;
4046 	rmem->nr_pages = bp->cp_nr_pages;
4047 	rmem->page_size = HW_CMPD_RING_SIZE;
4048 	rmem->pg_arr = (void **)cpr->cp_desc_ring;
4049 	rmem->dma_arr = cpr->cp_desc_mapping;
4050 	rmem->flags = BNXT_RMEM_RING_PTE_FLAG;
4051 	rc = bnxt_alloc_ring(bp, rmem);
4052 	if (rc) {
4053 		bnxt_free_ring(bp, rmem);
4054 		bnxt_free_cp_arrays(cpr);
4055 	}
4056 	return rc;
4057 }
4058 
4059 static int bnxt_alloc_cp_rings(struct bnxt *bp)
4060 {
4061 	bool sh = !!(bp->flags & BNXT_FLAG_SHARED_RINGS);
4062 	int i, j, rc, ulp_msix;
4063 	int tcs = bp->num_tc;
4064 
4065 	if (!tcs)
4066 		tcs = 1;
4067 	ulp_msix = bnxt_get_ulp_msix_num(bp);
4068 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
4069 		struct bnxt_napi *bnapi = bp->bnapi[i];
4070 		struct bnxt_cp_ring_info *cpr, *cpr2;
4071 		struct bnxt_ring_struct *ring;
4072 		int cp_count = 0, k;
4073 		int rx = 0, tx = 0;
4074 
4075 		if (!bnapi)
4076 			continue;
4077 
4078 		cpr = &bnapi->cp_ring;
4079 		cpr->bnapi = bnapi;
4080 		ring = &cpr->cp_ring_struct;
4081 
4082 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
4083 		if (rc)
4084 			return rc;
4085 
4086 		ring->map_idx = ulp_msix + i;
4087 
4088 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4089 			continue;
4090 
4091 		if (i < bp->rx_nr_rings) {
4092 			cp_count++;
4093 			rx = 1;
4094 		}
4095 		if (i < bp->tx_nr_rings_xdp) {
4096 			cp_count++;
4097 			tx = 1;
4098 		} else if ((sh && i < bp->tx_nr_rings) ||
4099 			 (!sh && i >= bp->rx_nr_rings)) {
4100 			cp_count += tcs;
4101 			tx = 1;
4102 		}
4103 
4104 		cpr->cp_ring_arr = kcalloc(cp_count, sizeof(*cpr),
4105 					   GFP_KERNEL);
4106 		if (!cpr->cp_ring_arr)
4107 			return -ENOMEM;
4108 		cpr->cp_ring_count = cp_count;
4109 
4110 		for (k = 0; k < cp_count; k++) {
4111 			cpr2 = &cpr->cp_ring_arr[k];
4112 			rc = bnxt_alloc_cp_sub_ring(bp, cpr2);
4113 			if (rc)
4114 				return rc;
4115 			cpr2->bnapi = bnapi;
4116 			cpr2->sw_stats = cpr->sw_stats;
4117 			cpr2->cp_idx = k;
4118 			if (!k && rx) {
4119 				bp->rx_ring[i].rx_cpr = cpr2;
4120 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_RX;
4121 			} else {
4122 				int n, tc = k - rx;
4123 
4124 				n = BNXT_TC_TO_RING_BASE(bp, tc) + j;
4125 				bp->tx_ring[n].tx_cpr = cpr2;
4126 				cpr2->cp_ring_type = BNXT_NQ_HDL_TYPE_TX;
4127 			}
4128 		}
4129 		if (tx)
4130 			j++;
4131 	}
4132 	return 0;
4133 }
4134 
4135 static void bnxt_init_rx_ring_struct(struct bnxt *bp,
4136 				     struct bnxt_rx_ring_info *rxr)
4137 {
4138 	struct bnxt_ring_mem_info *rmem;
4139 	struct bnxt_ring_struct *ring;
4140 
4141 	ring = &rxr->rx_ring_struct;
4142 	rmem = &ring->ring_mem;
4143 	rmem->nr_pages = bp->rx_nr_pages;
4144 	rmem->page_size = HW_RXBD_RING_SIZE;
4145 	rmem->pg_arr = (void **)rxr->rx_desc_ring;
4146 	rmem->dma_arr = rxr->rx_desc_mapping;
4147 	rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4148 	rmem->vmem = (void **)&rxr->rx_buf_ring;
4149 
4150 	ring = &rxr->rx_agg_ring_struct;
4151 	rmem = &ring->ring_mem;
4152 	rmem->nr_pages = bp->rx_agg_nr_pages;
4153 	rmem->page_size = HW_RXBD_RING_SIZE;
4154 	rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4155 	rmem->dma_arr = rxr->rx_agg_desc_mapping;
4156 	rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4157 	rmem->vmem = (void **)&rxr->rx_agg_ring;
4158 }
4159 
4160 static void bnxt_reset_rx_ring_struct(struct bnxt *bp,
4161 				      struct bnxt_rx_ring_info *rxr)
4162 {
4163 	struct bnxt_ring_mem_info *rmem;
4164 	struct bnxt_ring_struct *ring;
4165 	int i;
4166 
4167 	rxr->page_pool->p.napi = NULL;
4168 	rxr->page_pool = NULL;
4169 	memset(&rxr->xdp_rxq, 0, sizeof(struct xdp_rxq_info));
4170 
4171 	ring = &rxr->rx_ring_struct;
4172 	rmem = &ring->ring_mem;
4173 	rmem->pg_tbl = NULL;
4174 	rmem->pg_tbl_map = 0;
4175 	for (i = 0; i < rmem->nr_pages; i++) {
4176 		rmem->pg_arr[i] = NULL;
4177 		rmem->dma_arr[i] = 0;
4178 	}
4179 	*rmem->vmem = NULL;
4180 
4181 	ring = &rxr->rx_agg_ring_struct;
4182 	rmem = &ring->ring_mem;
4183 	rmem->pg_tbl = NULL;
4184 	rmem->pg_tbl_map = 0;
4185 	for (i = 0; i < rmem->nr_pages; i++) {
4186 		rmem->pg_arr[i] = NULL;
4187 		rmem->dma_arr[i] = 0;
4188 	}
4189 	*rmem->vmem = NULL;
4190 }
4191 
4192 static void bnxt_init_ring_struct(struct bnxt *bp)
4193 {
4194 	int i, j;
4195 
4196 	for (i = 0; i < bp->cp_nr_rings; i++) {
4197 		struct bnxt_napi *bnapi = bp->bnapi[i];
4198 		struct bnxt_ring_mem_info *rmem;
4199 		struct bnxt_cp_ring_info *cpr;
4200 		struct bnxt_rx_ring_info *rxr;
4201 		struct bnxt_tx_ring_info *txr;
4202 		struct bnxt_ring_struct *ring;
4203 
4204 		if (!bnapi)
4205 			continue;
4206 
4207 		cpr = &bnapi->cp_ring;
4208 		ring = &cpr->cp_ring_struct;
4209 		rmem = &ring->ring_mem;
4210 		rmem->nr_pages = bp->cp_nr_pages;
4211 		rmem->page_size = HW_CMPD_RING_SIZE;
4212 		rmem->pg_arr = (void **)cpr->cp_desc_ring;
4213 		rmem->dma_arr = cpr->cp_desc_mapping;
4214 		rmem->vmem_size = 0;
4215 
4216 		rxr = bnapi->rx_ring;
4217 		if (!rxr)
4218 			goto skip_rx;
4219 
4220 		ring = &rxr->rx_ring_struct;
4221 		rmem = &ring->ring_mem;
4222 		rmem->nr_pages = bp->rx_nr_pages;
4223 		rmem->page_size = HW_RXBD_RING_SIZE;
4224 		rmem->pg_arr = (void **)rxr->rx_desc_ring;
4225 		rmem->dma_arr = rxr->rx_desc_mapping;
4226 		rmem->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
4227 		rmem->vmem = (void **)&rxr->rx_buf_ring;
4228 
4229 		ring = &rxr->rx_agg_ring_struct;
4230 		rmem = &ring->ring_mem;
4231 		rmem->nr_pages = bp->rx_agg_nr_pages;
4232 		rmem->page_size = HW_RXBD_RING_SIZE;
4233 		rmem->pg_arr = (void **)rxr->rx_agg_desc_ring;
4234 		rmem->dma_arr = rxr->rx_agg_desc_mapping;
4235 		rmem->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
4236 		rmem->vmem = (void **)&rxr->rx_agg_ring;
4237 
4238 skip_rx:
4239 		bnxt_for_each_napi_tx(j, bnapi, txr) {
4240 			ring = &txr->tx_ring_struct;
4241 			rmem = &ring->ring_mem;
4242 			rmem->nr_pages = bp->tx_nr_pages;
4243 			rmem->page_size = HW_TXBD_RING_SIZE;
4244 			rmem->pg_arr = (void **)txr->tx_desc_ring;
4245 			rmem->dma_arr = txr->tx_desc_mapping;
4246 			rmem->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
4247 			rmem->vmem = (void **)&txr->tx_buf_ring;
4248 		}
4249 	}
4250 }
4251 
4252 static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
4253 {
4254 	int i;
4255 	u32 prod;
4256 	struct rx_bd **rx_buf_ring;
4257 
4258 	rx_buf_ring = (struct rx_bd **)ring->ring_mem.pg_arr;
4259 	for (i = 0, prod = 0; i < ring->ring_mem.nr_pages; i++) {
4260 		int j;
4261 		struct rx_bd *rxbd;
4262 
4263 		rxbd = rx_buf_ring[i];
4264 		if (!rxbd)
4265 			continue;
4266 
4267 		for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
4268 			rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
4269 			rxbd->rx_bd_opaque = prod;
4270 		}
4271 	}
4272 }
4273 
4274 static void bnxt_alloc_one_rx_ring_skb(struct bnxt *bp,
4275 				       struct bnxt_rx_ring_info *rxr,
4276 				       int ring_nr)
4277 {
4278 	u32 prod;
4279 	int i;
4280 
4281 	prod = rxr->rx_prod;
4282 	for (i = 0; i < bp->rx_ring_size; i++) {
4283 		if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL)) {
4284 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d skbs only\n",
4285 				    ring_nr, i, bp->rx_ring_size);
4286 			break;
4287 		}
4288 		prod = NEXT_RX(prod);
4289 	}
4290 	rxr->rx_prod = prod;
4291 }
4292 
4293 static void bnxt_alloc_one_rx_ring_page(struct bnxt *bp,
4294 					struct bnxt_rx_ring_info *rxr,
4295 					int ring_nr)
4296 {
4297 	u32 prod;
4298 	int i;
4299 
4300 	prod = rxr->rx_agg_prod;
4301 	for (i = 0; i < bp->rx_agg_ring_size; i++) {
4302 		if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL)) {
4303 			netdev_warn(bp->dev, "init'ed rx ring %d with %d/%d pages only\n",
4304 				    ring_nr, i, bp->rx_ring_size);
4305 			break;
4306 		}
4307 		prod = NEXT_RX_AGG(prod);
4308 	}
4309 	rxr->rx_agg_prod = prod;
4310 }
4311 
4312 static int bnxt_alloc_one_tpa_info_data(struct bnxt *bp,
4313 					struct bnxt_rx_ring_info *rxr)
4314 {
4315 	dma_addr_t mapping;
4316 	u8 *data;
4317 	int i;
4318 
4319 	for (i = 0; i < bp->max_tpa; i++) {
4320 		data = __bnxt_alloc_rx_frag(bp, &mapping, rxr,
4321 					    GFP_KERNEL);
4322 		if (!data)
4323 			return -ENOMEM;
4324 
4325 		rxr->rx_tpa[i].data = data;
4326 		rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
4327 		rxr->rx_tpa[i].mapping = mapping;
4328 	}
4329 
4330 	return 0;
4331 }
4332 
4333 static int bnxt_alloc_one_rx_ring(struct bnxt *bp, int ring_nr)
4334 {
4335 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
4336 	int rc;
4337 
4338 	bnxt_alloc_one_rx_ring_skb(bp, rxr, ring_nr);
4339 
4340 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
4341 		return 0;
4342 
4343 	bnxt_alloc_one_rx_ring_page(bp, rxr, ring_nr);
4344 
4345 	if (rxr->rx_tpa) {
4346 		rc = bnxt_alloc_one_tpa_info_data(bp, rxr);
4347 		if (rc)
4348 			return rc;
4349 	}
4350 	return 0;
4351 }
4352 
4353 static void bnxt_init_one_rx_ring_rxbd(struct bnxt *bp,
4354 				       struct bnxt_rx_ring_info *rxr)
4355 {
4356 	struct bnxt_ring_struct *ring;
4357 	u32 type;
4358 
4359 	type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
4360 		RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
4361 
4362 	if (NET_IP_ALIGN == 2)
4363 		type |= RX_BD_FLAGS_SOP;
4364 
4365 	ring = &rxr->rx_ring_struct;
4366 	bnxt_init_rxbd_pages(ring, type);
4367 	ring->fw_ring_id = INVALID_HW_RING_ID;
4368 }
4369 
4370 static void bnxt_init_one_rx_agg_ring_rxbd(struct bnxt *bp,
4371 					   struct bnxt_rx_ring_info *rxr)
4372 {
4373 	struct bnxt_ring_struct *ring;
4374 	u32 type;
4375 
4376 	ring = &rxr->rx_agg_ring_struct;
4377 	ring->fw_ring_id = INVALID_HW_RING_ID;
4378 	if ((bp->flags & BNXT_FLAG_AGG_RINGS)) {
4379 		type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
4380 			RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
4381 
4382 		bnxt_init_rxbd_pages(ring, type);
4383 	}
4384 }
4385 
4386 static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
4387 {
4388 	struct bnxt_rx_ring_info *rxr;
4389 
4390 	rxr = &bp->rx_ring[ring_nr];
4391 	bnxt_init_one_rx_ring_rxbd(bp, rxr);
4392 
4393 	netif_queue_set_napi(bp->dev, ring_nr, NETDEV_QUEUE_TYPE_RX,
4394 			     &rxr->bnapi->napi);
4395 
4396 	if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
4397 		bpf_prog_add(bp->xdp_prog, 1);
4398 		rxr->xdp_prog = bp->xdp_prog;
4399 	}
4400 
4401 	bnxt_init_one_rx_agg_ring_rxbd(bp, rxr);
4402 
4403 	return bnxt_alloc_one_rx_ring(bp, ring_nr);
4404 }
4405 
4406 static void bnxt_init_cp_rings(struct bnxt *bp)
4407 {
4408 	int i, j;
4409 
4410 	for (i = 0; i < bp->cp_nr_rings; i++) {
4411 		struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
4412 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
4413 
4414 		ring->fw_ring_id = INVALID_HW_RING_ID;
4415 		cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4416 		cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4417 		if (!cpr->cp_ring_arr)
4418 			continue;
4419 		for (j = 0; j < cpr->cp_ring_count; j++) {
4420 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
4421 
4422 			ring = &cpr2->cp_ring_struct;
4423 			ring->fw_ring_id = INVALID_HW_RING_ID;
4424 			cpr2->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
4425 			cpr2->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
4426 		}
4427 	}
4428 }
4429 
4430 static int bnxt_init_rx_rings(struct bnxt *bp)
4431 {
4432 	int i, rc = 0;
4433 
4434 	if (BNXT_RX_PAGE_MODE(bp)) {
4435 		bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
4436 		bp->rx_dma_offset = XDP_PACKET_HEADROOM;
4437 	} else {
4438 		bp->rx_offset = BNXT_RX_OFFSET;
4439 		bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
4440 	}
4441 
4442 	for (i = 0; i < bp->rx_nr_rings; i++) {
4443 		rc = bnxt_init_one_rx_ring(bp, i);
4444 		if (rc)
4445 			break;
4446 	}
4447 
4448 	return rc;
4449 }
4450 
4451 static int bnxt_init_tx_rings(struct bnxt *bp)
4452 {
4453 	u16 i;
4454 
4455 	bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
4456 				   BNXT_MIN_TX_DESC_CNT);
4457 
4458 	for (i = 0; i < bp->tx_nr_rings; i++) {
4459 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
4460 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
4461 
4462 		ring->fw_ring_id = INVALID_HW_RING_ID;
4463 
4464 		if (i >= bp->tx_nr_rings_xdp)
4465 			netif_queue_set_napi(bp->dev, i - bp->tx_nr_rings_xdp,
4466 					     NETDEV_QUEUE_TYPE_TX,
4467 					     &txr->bnapi->napi);
4468 	}
4469 
4470 	return 0;
4471 }
4472 
4473 static void bnxt_free_ring_grps(struct bnxt *bp)
4474 {
4475 	kfree(bp->grp_info);
4476 	bp->grp_info = NULL;
4477 }
4478 
4479 static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
4480 {
4481 	int i;
4482 
4483 	if (irq_re_init) {
4484 		bp->grp_info = kcalloc(bp->cp_nr_rings,
4485 				       sizeof(struct bnxt_ring_grp_info),
4486 				       GFP_KERNEL);
4487 		if (!bp->grp_info)
4488 			return -ENOMEM;
4489 	}
4490 	for (i = 0; i < bp->cp_nr_rings; i++) {
4491 		if (irq_re_init)
4492 			bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
4493 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
4494 		bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
4495 		bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
4496 		bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
4497 	}
4498 	return 0;
4499 }
4500 
4501 static void bnxt_free_vnics(struct bnxt *bp)
4502 {
4503 	kfree(bp->vnic_info);
4504 	bp->vnic_info = NULL;
4505 	bp->nr_vnics = 0;
4506 }
4507 
4508 static int bnxt_alloc_vnics(struct bnxt *bp)
4509 {
4510 	int num_vnics = 1;
4511 
4512 #ifdef CONFIG_RFS_ACCEL
4513 	if (bp->flags & BNXT_FLAG_RFS) {
4514 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
4515 			num_vnics++;
4516 		else if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4517 			num_vnics += bp->rx_nr_rings;
4518 	}
4519 #endif
4520 
4521 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
4522 		num_vnics++;
4523 
4524 	bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
4525 				GFP_KERNEL);
4526 	if (!bp->vnic_info)
4527 		return -ENOMEM;
4528 
4529 	bp->nr_vnics = num_vnics;
4530 	return 0;
4531 }
4532 
4533 static void bnxt_init_vnics(struct bnxt *bp)
4534 {
4535 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
4536 	int i;
4537 
4538 	for (i = 0; i < bp->nr_vnics; i++) {
4539 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
4540 		int j;
4541 
4542 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
4543 		vnic->vnic_id = i;
4544 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++)
4545 			vnic->fw_rss_cos_lb_ctx[j] = INVALID_HW_RING_ID;
4546 
4547 		vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
4548 
4549 		if (bp->vnic_info[i].rss_hash_key) {
4550 			if (i == BNXT_VNIC_DEFAULT) {
4551 				u8 *key = (void *)vnic->rss_hash_key;
4552 				int k;
4553 
4554 				if (!bp->rss_hash_key_valid &&
4555 				    !bp->rss_hash_key_updated) {
4556 					get_random_bytes(bp->rss_hash_key,
4557 							 HW_HASH_KEY_SIZE);
4558 					bp->rss_hash_key_updated = true;
4559 				}
4560 
4561 				memcpy(vnic->rss_hash_key, bp->rss_hash_key,
4562 				       HW_HASH_KEY_SIZE);
4563 
4564 				if (!bp->rss_hash_key_updated)
4565 					continue;
4566 
4567 				bp->rss_hash_key_updated = false;
4568 				bp->rss_hash_key_valid = true;
4569 
4570 				bp->toeplitz_prefix = 0;
4571 				for (k = 0; k < 8; k++) {
4572 					bp->toeplitz_prefix <<= 8;
4573 					bp->toeplitz_prefix |= key[k];
4574 				}
4575 			} else {
4576 				memcpy(vnic->rss_hash_key, vnic0->rss_hash_key,
4577 				       HW_HASH_KEY_SIZE);
4578 			}
4579 		}
4580 	}
4581 }
4582 
4583 static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
4584 {
4585 	int pages;
4586 
4587 	pages = ring_size / desc_per_pg;
4588 
4589 	if (!pages)
4590 		return 1;
4591 
4592 	pages++;
4593 
4594 	while (pages & (pages - 1))
4595 		pages++;
4596 
4597 	return pages;
4598 }
4599 
4600 void bnxt_set_tpa_flags(struct bnxt *bp)
4601 {
4602 	bp->flags &= ~BNXT_FLAG_TPA;
4603 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
4604 		return;
4605 	if (bp->dev->features & NETIF_F_LRO)
4606 		bp->flags |= BNXT_FLAG_LRO;
4607 	else if (bp->dev->features & NETIF_F_GRO_HW)
4608 		bp->flags |= BNXT_FLAG_GRO;
4609 }
4610 
4611 /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
4612  * be set on entry.
4613  */
4614 void bnxt_set_ring_params(struct bnxt *bp)
4615 {
4616 	u32 ring_size, rx_size, rx_space, max_rx_cmpl;
4617 	u32 agg_factor = 0, agg_ring_size = 0;
4618 
4619 	/* 8 for CRC and VLAN */
4620 	rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
4621 
4622 	rx_space = rx_size + ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) +
4623 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4624 
4625 	bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
4626 	ring_size = bp->rx_ring_size;
4627 	bp->rx_agg_ring_size = 0;
4628 	bp->rx_agg_nr_pages = 0;
4629 
4630 	if (bp->flags & BNXT_FLAG_TPA)
4631 		agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
4632 
4633 	bp->flags &= ~BNXT_FLAG_JUMBO;
4634 	if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
4635 		u32 jumbo_factor;
4636 
4637 		bp->flags |= BNXT_FLAG_JUMBO;
4638 		jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
4639 		if (jumbo_factor > agg_factor)
4640 			agg_factor = jumbo_factor;
4641 	}
4642 	if (agg_factor) {
4643 		if (ring_size > BNXT_MAX_RX_DESC_CNT_JUM_ENA) {
4644 			ring_size = BNXT_MAX_RX_DESC_CNT_JUM_ENA;
4645 			netdev_warn(bp->dev, "RX ring size reduced from %d to %d because the jumbo ring is now enabled\n",
4646 				    bp->rx_ring_size, ring_size);
4647 			bp->rx_ring_size = ring_size;
4648 		}
4649 		agg_ring_size = ring_size * agg_factor;
4650 
4651 		bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
4652 							RX_DESC_CNT);
4653 		if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
4654 			u32 tmp = agg_ring_size;
4655 
4656 			bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
4657 			agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
4658 			netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
4659 				    tmp, agg_ring_size);
4660 		}
4661 		bp->rx_agg_ring_size = agg_ring_size;
4662 		bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
4663 
4664 		if (BNXT_RX_PAGE_MODE(bp)) {
4665 			rx_space = PAGE_SIZE;
4666 			rx_size = PAGE_SIZE -
4667 				  ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8) -
4668 				  SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4669 		} else {
4670 			rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
4671 			rx_space = rx_size + NET_SKB_PAD +
4672 				SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
4673 		}
4674 	}
4675 
4676 	bp->rx_buf_use_size = rx_size;
4677 	bp->rx_buf_size = rx_space;
4678 
4679 	bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
4680 	bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
4681 
4682 	ring_size = bp->tx_ring_size;
4683 	bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
4684 	bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
4685 
4686 	max_rx_cmpl = bp->rx_ring_size;
4687 	/* MAX TPA needs to be added because TPA_START completions are
4688 	 * immediately recycled, so the TPA completions are not bound by
4689 	 * the RX ring size.
4690 	 */
4691 	if (bp->flags & BNXT_FLAG_TPA)
4692 		max_rx_cmpl += bp->max_tpa;
4693 	/* RX and TPA completions are 32-byte, all others are 16-byte */
4694 	ring_size = max_rx_cmpl * 2 + agg_ring_size + bp->tx_ring_size;
4695 	bp->cp_ring_size = ring_size;
4696 
4697 	bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
4698 	if (bp->cp_nr_pages > MAX_CP_PAGES) {
4699 		bp->cp_nr_pages = MAX_CP_PAGES;
4700 		bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
4701 		netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
4702 			    ring_size, bp->cp_ring_size);
4703 	}
4704 	bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
4705 	bp->cp_ring_mask = bp->cp_bit - 1;
4706 }
4707 
4708 /* Changing allocation mode of RX rings.
4709  * TODO: Update when extending xdp_rxq_info to support allocation modes.
4710  */
4711 static void __bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4712 {
4713 	struct net_device *dev = bp->dev;
4714 
4715 	if (page_mode) {
4716 		bp->flags &= ~(BNXT_FLAG_AGG_RINGS | BNXT_FLAG_NO_AGG_RINGS);
4717 		bp->flags |= BNXT_FLAG_RX_PAGE_MODE;
4718 
4719 		if (bp->xdp_prog->aux->xdp_has_frags)
4720 			dev->max_mtu = min_t(u16, bp->max_mtu, BNXT_MAX_MTU);
4721 		else
4722 			dev->max_mtu =
4723 				min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
4724 		if (dev->mtu > BNXT_MAX_PAGE_MODE_MTU) {
4725 			bp->flags |= BNXT_FLAG_JUMBO;
4726 			bp->rx_skb_func = bnxt_rx_multi_page_skb;
4727 		} else {
4728 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
4729 			bp->rx_skb_func = bnxt_rx_page_skb;
4730 		}
4731 		bp->rx_dir = DMA_BIDIRECTIONAL;
4732 	} else {
4733 		dev->max_mtu = bp->max_mtu;
4734 		bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
4735 		bp->rx_dir = DMA_FROM_DEVICE;
4736 		bp->rx_skb_func = bnxt_rx_skb;
4737 	}
4738 }
4739 
4740 void bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
4741 {
4742 	__bnxt_set_rx_skb_mode(bp, page_mode);
4743 
4744 	if (!page_mode) {
4745 		int rx, tx;
4746 
4747 		bnxt_get_max_rings(bp, &rx, &tx, true);
4748 		if (rx > 1) {
4749 			bp->flags &= ~BNXT_FLAG_NO_AGG_RINGS;
4750 			bp->dev->hw_features |= NETIF_F_LRO;
4751 		}
4752 	}
4753 
4754 	/* Update LRO and GRO_HW availability */
4755 	netdev_update_features(bp->dev);
4756 }
4757 
4758 static void bnxt_free_vnic_attributes(struct bnxt *bp)
4759 {
4760 	int i;
4761 	struct bnxt_vnic_info *vnic;
4762 	struct pci_dev *pdev = bp->pdev;
4763 
4764 	if (!bp->vnic_info)
4765 		return;
4766 
4767 	for (i = 0; i < bp->nr_vnics; i++) {
4768 		vnic = &bp->vnic_info[i];
4769 
4770 		kfree(vnic->fw_grp_ids);
4771 		vnic->fw_grp_ids = NULL;
4772 
4773 		kfree(vnic->uc_list);
4774 		vnic->uc_list = NULL;
4775 
4776 		if (vnic->mc_list) {
4777 			dma_free_coherent(&pdev->dev, vnic->mc_list_size,
4778 					  vnic->mc_list, vnic->mc_list_mapping);
4779 			vnic->mc_list = NULL;
4780 		}
4781 
4782 		if (vnic->rss_table) {
4783 			dma_free_coherent(&pdev->dev, vnic->rss_table_size,
4784 					  vnic->rss_table,
4785 					  vnic->rss_table_dma_addr);
4786 			vnic->rss_table = NULL;
4787 		}
4788 
4789 		vnic->rss_hash_key = NULL;
4790 		vnic->flags = 0;
4791 	}
4792 }
4793 
4794 static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
4795 {
4796 	int i, rc = 0, size;
4797 	struct bnxt_vnic_info *vnic;
4798 	struct pci_dev *pdev = bp->pdev;
4799 	int max_rings;
4800 
4801 	for (i = 0; i < bp->nr_vnics; i++) {
4802 		vnic = &bp->vnic_info[i];
4803 
4804 		if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
4805 			int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
4806 
4807 			if (mem_size > 0) {
4808 				vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
4809 				if (!vnic->uc_list) {
4810 					rc = -ENOMEM;
4811 					goto out;
4812 				}
4813 			}
4814 		}
4815 
4816 		if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
4817 			vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
4818 			vnic->mc_list =
4819 				dma_alloc_coherent(&pdev->dev,
4820 						   vnic->mc_list_size,
4821 						   &vnic->mc_list_mapping,
4822 						   GFP_KERNEL);
4823 			if (!vnic->mc_list) {
4824 				rc = -ENOMEM;
4825 				goto out;
4826 			}
4827 		}
4828 
4829 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4830 			goto vnic_skip_grps;
4831 
4832 		if (vnic->flags & BNXT_VNIC_RSS_FLAG)
4833 			max_rings = bp->rx_nr_rings;
4834 		else
4835 			max_rings = 1;
4836 
4837 		vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
4838 		if (!vnic->fw_grp_ids) {
4839 			rc = -ENOMEM;
4840 			goto out;
4841 		}
4842 vnic_skip_grps:
4843 		if ((bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) &&
4844 		    !(vnic->flags & BNXT_VNIC_RSS_FLAG))
4845 			continue;
4846 
4847 		/* Allocate rss table and hash key */
4848 		size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
4849 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4850 			size = L1_CACHE_ALIGN(BNXT_MAX_RSS_TABLE_SIZE_P5);
4851 
4852 		vnic->rss_table_size = size + HW_HASH_KEY_SIZE;
4853 		vnic->rss_table = dma_alloc_coherent(&pdev->dev,
4854 						     vnic->rss_table_size,
4855 						     &vnic->rss_table_dma_addr,
4856 						     GFP_KERNEL);
4857 		if (!vnic->rss_table) {
4858 			rc = -ENOMEM;
4859 			goto out;
4860 		}
4861 
4862 		vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
4863 		vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
4864 	}
4865 	return 0;
4866 
4867 out:
4868 	return rc;
4869 }
4870 
4871 static void bnxt_free_hwrm_resources(struct bnxt *bp)
4872 {
4873 	struct bnxt_hwrm_wait_token *token;
4874 
4875 	dma_pool_destroy(bp->hwrm_dma_pool);
4876 	bp->hwrm_dma_pool = NULL;
4877 
4878 	rcu_read_lock();
4879 	hlist_for_each_entry_rcu(token, &bp->hwrm_pending_list, node)
4880 		WRITE_ONCE(token->state, BNXT_HWRM_CANCELLED);
4881 	rcu_read_unlock();
4882 }
4883 
4884 static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
4885 {
4886 	bp->hwrm_dma_pool = dma_pool_create("bnxt_hwrm", &bp->pdev->dev,
4887 					    BNXT_HWRM_DMA_SIZE,
4888 					    BNXT_HWRM_DMA_ALIGN, 0);
4889 	if (!bp->hwrm_dma_pool)
4890 		return -ENOMEM;
4891 
4892 	INIT_HLIST_HEAD(&bp->hwrm_pending_list);
4893 
4894 	return 0;
4895 }
4896 
4897 static void bnxt_free_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats)
4898 {
4899 	kfree(stats->hw_masks);
4900 	stats->hw_masks = NULL;
4901 	kfree(stats->sw_stats);
4902 	stats->sw_stats = NULL;
4903 	if (stats->hw_stats) {
4904 		dma_free_coherent(&bp->pdev->dev, stats->len, stats->hw_stats,
4905 				  stats->hw_stats_map);
4906 		stats->hw_stats = NULL;
4907 	}
4908 }
4909 
4910 static int bnxt_alloc_stats_mem(struct bnxt *bp, struct bnxt_stats_mem *stats,
4911 				bool alloc_masks)
4912 {
4913 	stats->hw_stats = dma_alloc_coherent(&bp->pdev->dev, stats->len,
4914 					     &stats->hw_stats_map, GFP_KERNEL);
4915 	if (!stats->hw_stats)
4916 		return -ENOMEM;
4917 
4918 	stats->sw_stats = kzalloc(stats->len, GFP_KERNEL);
4919 	if (!stats->sw_stats)
4920 		goto stats_mem_err;
4921 
4922 	if (alloc_masks) {
4923 		stats->hw_masks = kzalloc(stats->len, GFP_KERNEL);
4924 		if (!stats->hw_masks)
4925 			goto stats_mem_err;
4926 	}
4927 	return 0;
4928 
4929 stats_mem_err:
4930 	bnxt_free_stats_mem(bp, stats);
4931 	return -ENOMEM;
4932 }
4933 
4934 static void bnxt_fill_masks(u64 *mask_arr, u64 mask, int count)
4935 {
4936 	int i;
4937 
4938 	for (i = 0; i < count; i++)
4939 		mask_arr[i] = mask;
4940 }
4941 
4942 static void bnxt_copy_hw_masks(u64 *mask_arr, __le64 *hw_mask_arr, int count)
4943 {
4944 	int i;
4945 
4946 	for (i = 0; i < count; i++)
4947 		mask_arr[i] = le64_to_cpu(hw_mask_arr[i]);
4948 }
4949 
4950 static int bnxt_hwrm_func_qstat_ext(struct bnxt *bp,
4951 				    struct bnxt_stats_mem *stats)
4952 {
4953 	struct hwrm_func_qstats_ext_output *resp;
4954 	struct hwrm_func_qstats_ext_input *req;
4955 	__le64 *hw_masks;
4956 	int rc;
4957 
4958 	if (!(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED) ||
4959 	    !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
4960 		return -EOPNOTSUPP;
4961 
4962 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QSTATS_EXT);
4963 	if (rc)
4964 		return rc;
4965 
4966 	req->fid = cpu_to_le16(0xffff);
4967 	req->flags = FUNC_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
4968 
4969 	resp = hwrm_req_hold(bp, req);
4970 	rc = hwrm_req_send(bp, req);
4971 	if (!rc) {
4972 		hw_masks = &resp->rx_ucast_pkts;
4973 		bnxt_copy_hw_masks(stats->hw_masks, hw_masks, stats->len / 8);
4974 	}
4975 	hwrm_req_drop(bp, req);
4976 	return rc;
4977 }
4978 
4979 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags);
4980 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags);
4981 
4982 static void bnxt_init_stats(struct bnxt *bp)
4983 {
4984 	struct bnxt_napi *bnapi = bp->bnapi[0];
4985 	struct bnxt_cp_ring_info *cpr;
4986 	struct bnxt_stats_mem *stats;
4987 	__le64 *rx_stats, *tx_stats;
4988 	int rc, rx_count, tx_count;
4989 	u64 *rx_masks, *tx_masks;
4990 	u64 mask;
4991 	u8 flags;
4992 
4993 	cpr = &bnapi->cp_ring;
4994 	stats = &cpr->stats;
4995 	rc = bnxt_hwrm_func_qstat_ext(bp, stats);
4996 	if (rc) {
4997 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
4998 			mask = (1ULL << 48) - 1;
4999 		else
5000 			mask = -1ULL;
5001 		bnxt_fill_masks(stats->hw_masks, mask, stats->len / 8);
5002 	}
5003 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
5004 		stats = &bp->port_stats;
5005 		rx_stats = stats->hw_stats;
5006 		rx_masks = stats->hw_masks;
5007 		rx_count = sizeof(struct rx_port_stats) / 8;
5008 		tx_stats = rx_stats + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5009 		tx_masks = rx_masks + BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
5010 		tx_count = sizeof(struct tx_port_stats) / 8;
5011 
5012 		flags = PORT_QSTATS_REQ_FLAGS_COUNTER_MASK;
5013 		rc = bnxt_hwrm_port_qstats(bp, flags);
5014 		if (rc) {
5015 			mask = (1ULL << 40) - 1;
5016 
5017 			bnxt_fill_masks(rx_masks, mask, rx_count);
5018 			bnxt_fill_masks(tx_masks, mask, tx_count);
5019 		} else {
5020 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5021 			bnxt_copy_hw_masks(tx_masks, tx_stats, tx_count);
5022 			bnxt_hwrm_port_qstats(bp, 0);
5023 		}
5024 	}
5025 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
5026 		stats = &bp->rx_port_stats_ext;
5027 		rx_stats = stats->hw_stats;
5028 		rx_masks = stats->hw_masks;
5029 		rx_count = sizeof(struct rx_port_stats_ext) / 8;
5030 		stats = &bp->tx_port_stats_ext;
5031 		tx_stats = stats->hw_stats;
5032 		tx_masks = stats->hw_masks;
5033 		tx_count = sizeof(struct tx_port_stats_ext) / 8;
5034 
5035 		flags = PORT_QSTATS_EXT_REQ_FLAGS_COUNTER_MASK;
5036 		rc = bnxt_hwrm_port_qstats_ext(bp, flags);
5037 		if (rc) {
5038 			mask = (1ULL << 40) - 1;
5039 
5040 			bnxt_fill_masks(rx_masks, mask, rx_count);
5041 			if (tx_stats)
5042 				bnxt_fill_masks(tx_masks, mask, tx_count);
5043 		} else {
5044 			bnxt_copy_hw_masks(rx_masks, rx_stats, rx_count);
5045 			if (tx_stats)
5046 				bnxt_copy_hw_masks(tx_masks, tx_stats,
5047 						   tx_count);
5048 			bnxt_hwrm_port_qstats_ext(bp, 0);
5049 		}
5050 	}
5051 }
5052 
5053 static void bnxt_free_port_stats(struct bnxt *bp)
5054 {
5055 	bp->flags &= ~BNXT_FLAG_PORT_STATS;
5056 	bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
5057 
5058 	bnxt_free_stats_mem(bp, &bp->port_stats);
5059 	bnxt_free_stats_mem(bp, &bp->rx_port_stats_ext);
5060 	bnxt_free_stats_mem(bp, &bp->tx_port_stats_ext);
5061 }
5062 
5063 static void bnxt_free_ring_stats(struct bnxt *bp)
5064 {
5065 	int i;
5066 
5067 	if (!bp->bnapi)
5068 		return;
5069 
5070 	for (i = 0; i < bp->cp_nr_rings; i++) {
5071 		struct bnxt_napi *bnapi = bp->bnapi[i];
5072 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5073 
5074 		bnxt_free_stats_mem(bp, &cpr->stats);
5075 
5076 		kfree(cpr->sw_stats);
5077 		cpr->sw_stats = NULL;
5078 	}
5079 }
5080 
5081 static int bnxt_alloc_stats(struct bnxt *bp)
5082 {
5083 	u32 size, i;
5084 	int rc;
5085 
5086 	size = bp->hw_ring_stats_size;
5087 
5088 	for (i = 0; i < bp->cp_nr_rings; i++) {
5089 		struct bnxt_napi *bnapi = bp->bnapi[i];
5090 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5091 
5092 		cpr->sw_stats = kzalloc(sizeof(*cpr->sw_stats), GFP_KERNEL);
5093 		if (!cpr->sw_stats)
5094 			return -ENOMEM;
5095 
5096 		cpr->stats.len = size;
5097 		rc = bnxt_alloc_stats_mem(bp, &cpr->stats, !i);
5098 		if (rc)
5099 			return rc;
5100 
5101 		cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
5102 	}
5103 
5104 	if (BNXT_VF(bp) || bp->chip_num == CHIP_NUM_58700)
5105 		return 0;
5106 
5107 	if (bp->port_stats.hw_stats)
5108 		goto alloc_ext_stats;
5109 
5110 	bp->port_stats.len = BNXT_PORT_STATS_SIZE;
5111 	rc = bnxt_alloc_stats_mem(bp, &bp->port_stats, true);
5112 	if (rc)
5113 		return rc;
5114 
5115 	bp->flags |= BNXT_FLAG_PORT_STATS;
5116 
5117 alloc_ext_stats:
5118 	/* Display extended statistics only if FW supports it */
5119 	if (bp->hwrm_spec_code < 0x10804 || bp->hwrm_spec_code == 0x10900)
5120 		if (!(bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED))
5121 			return 0;
5122 
5123 	if (bp->rx_port_stats_ext.hw_stats)
5124 		goto alloc_tx_ext_stats;
5125 
5126 	bp->rx_port_stats_ext.len = sizeof(struct rx_port_stats_ext);
5127 	rc = bnxt_alloc_stats_mem(bp, &bp->rx_port_stats_ext, true);
5128 	/* Extended stats are optional */
5129 	if (rc)
5130 		return 0;
5131 
5132 alloc_tx_ext_stats:
5133 	if (bp->tx_port_stats_ext.hw_stats)
5134 		return 0;
5135 
5136 	if (bp->hwrm_spec_code >= 0x10902 ||
5137 	    (bp->fw_cap & BNXT_FW_CAP_EXT_STATS_SUPPORTED)) {
5138 		bp->tx_port_stats_ext.len = sizeof(struct tx_port_stats_ext);
5139 		rc = bnxt_alloc_stats_mem(bp, &bp->tx_port_stats_ext, true);
5140 		/* Extended stats are optional */
5141 		if (rc)
5142 			return 0;
5143 	}
5144 	bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
5145 	return 0;
5146 }
5147 
5148 static void bnxt_clear_ring_indices(struct bnxt *bp)
5149 {
5150 	int i, j;
5151 
5152 	if (!bp->bnapi)
5153 		return;
5154 
5155 	for (i = 0; i < bp->cp_nr_rings; i++) {
5156 		struct bnxt_napi *bnapi = bp->bnapi[i];
5157 		struct bnxt_cp_ring_info *cpr;
5158 		struct bnxt_rx_ring_info *rxr;
5159 		struct bnxt_tx_ring_info *txr;
5160 
5161 		if (!bnapi)
5162 			continue;
5163 
5164 		cpr = &bnapi->cp_ring;
5165 		cpr->cp_raw_cons = 0;
5166 
5167 		bnxt_for_each_napi_tx(j, bnapi, txr) {
5168 			txr->tx_prod = 0;
5169 			txr->tx_cons = 0;
5170 			txr->tx_hw_cons = 0;
5171 		}
5172 
5173 		rxr = bnapi->rx_ring;
5174 		if (rxr) {
5175 			rxr->rx_prod = 0;
5176 			rxr->rx_agg_prod = 0;
5177 			rxr->rx_sw_agg_prod = 0;
5178 			rxr->rx_next_cons = 0;
5179 		}
5180 		bnapi->events = 0;
5181 	}
5182 }
5183 
5184 void bnxt_insert_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5185 {
5186 	u8 type = fltr->type, flags = fltr->flags;
5187 
5188 	INIT_LIST_HEAD(&fltr->list);
5189 	if ((type == BNXT_FLTR_TYPE_L2 && flags & BNXT_ACT_RING_DST) ||
5190 	    (type == BNXT_FLTR_TYPE_NTUPLE && flags & BNXT_ACT_NO_AGING))
5191 		list_add_tail(&fltr->list, &bp->usr_fltr_list);
5192 }
5193 
5194 void bnxt_del_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5195 {
5196 	if (!list_empty(&fltr->list))
5197 		list_del_init(&fltr->list);
5198 }
5199 
5200 static void bnxt_clear_usr_fltrs(struct bnxt *bp, bool all)
5201 {
5202 	struct bnxt_filter_base *usr_fltr, *tmp;
5203 
5204 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
5205 		if (!all && usr_fltr->type == BNXT_FLTR_TYPE_L2)
5206 			continue;
5207 		bnxt_del_one_usr_fltr(bp, usr_fltr);
5208 	}
5209 }
5210 
5211 static void bnxt_del_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
5212 {
5213 	hlist_del(&fltr->hash);
5214 	bnxt_del_one_usr_fltr(bp, fltr);
5215 	if (fltr->flags) {
5216 		clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
5217 		bp->ntp_fltr_count--;
5218 	}
5219 	kfree(fltr);
5220 }
5221 
5222 static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool all)
5223 {
5224 	int i;
5225 
5226 	/* Under rtnl_lock and all our NAPIs have been disabled.  It's
5227 	 * safe to delete the hash table.
5228 	 */
5229 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
5230 		struct hlist_head *head;
5231 		struct hlist_node *tmp;
5232 		struct bnxt_ntuple_filter *fltr;
5233 
5234 		head = &bp->ntp_fltr_hash_tbl[i];
5235 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5236 			bnxt_del_l2_filter(bp, fltr->l2_fltr);
5237 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5238 				     !list_empty(&fltr->base.list)))
5239 				continue;
5240 			bnxt_del_fltr(bp, &fltr->base);
5241 		}
5242 	}
5243 	if (!all)
5244 		return;
5245 
5246 	bitmap_free(bp->ntp_fltr_bmap);
5247 	bp->ntp_fltr_bmap = NULL;
5248 	bp->ntp_fltr_count = 0;
5249 }
5250 
5251 static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
5252 {
5253 	int i, rc = 0;
5254 
5255 	if (!(bp->flags & BNXT_FLAG_RFS) || bp->ntp_fltr_bmap)
5256 		return 0;
5257 
5258 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
5259 		INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
5260 
5261 	bp->ntp_fltr_count = 0;
5262 	bp->ntp_fltr_bmap = bitmap_zalloc(bp->max_fltr, GFP_KERNEL);
5263 
5264 	if (!bp->ntp_fltr_bmap)
5265 		rc = -ENOMEM;
5266 
5267 	return rc;
5268 }
5269 
5270 static void bnxt_free_l2_filters(struct bnxt *bp, bool all)
5271 {
5272 	int i;
5273 
5274 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++) {
5275 		struct hlist_head *head;
5276 		struct hlist_node *tmp;
5277 		struct bnxt_l2_filter *fltr;
5278 
5279 		head = &bp->l2_fltr_hash_tbl[i];
5280 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
5281 			if (!all && ((fltr->base.flags & BNXT_ACT_FUNC_DST) ||
5282 				     !list_empty(&fltr->base.list)))
5283 				continue;
5284 			bnxt_del_fltr(bp, &fltr->base);
5285 		}
5286 	}
5287 }
5288 
5289 static void bnxt_init_l2_fltr_tbl(struct bnxt *bp)
5290 {
5291 	int i;
5292 
5293 	for (i = 0; i < BNXT_L2_FLTR_HASH_SIZE; i++)
5294 		INIT_HLIST_HEAD(&bp->l2_fltr_hash_tbl[i]);
5295 	get_random_bytes(&bp->hash_seed, sizeof(bp->hash_seed));
5296 }
5297 
5298 static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
5299 {
5300 	bnxt_free_vnic_attributes(bp);
5301 	bnxt_free_tx_rings(bp);
5302 	bnxt_free_rx_rings(bp);
5303 	bnxt_free_cp_rings(bp);
5304 	bnxt_free_all_cp_arrays(bp);
5305 	bnxt_free_ntp_fltrs(bp, false);
5306 	bnxt_free_l2_filters(bp, false);
5307 	if (irq_re_init) {
5308 		bnxt_free_ring_stats(bp);
5309 		if (!(bp->phy_flags & BNXT_PHY_FL_PORT_STATS_NO_RESET) ||
5310 		    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
5311 			bnxt_free_port_stats(bp);
5312 		bnxt_free_ring_grps(bp);
5313 		bnxt_free_vnics(bp);
5314 		kfree(bp->tx_ring_map);
5315 		bp->tx_ring_map = NULL;
5316 		kfree(bp->tx_ring);
5317 		bp->tx_ring = NULL;
5318 		kfree(bp->rx_ring);
5319 		bp->rx_ring = NULL;
5320 		kfree(bp->bnapi);
5321 		bp->bnapi = NULL;
5322 	} else {
5323 		bnxt_clear_ring_indices(bp);
5324 	}
5325 }
5326 
5327 static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
5328 {
5329 	int i, j, rc, size, arr_size;
5330 	void *bnapi;
5331 
5332 	if (irq_re_init) {
5333 		/* Allocate bnapi mem pointer array and mem block for
5334 		 * all queues
5335 		 */
5336 		arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
5337 				bp->cp_nr_rings);
5338 		size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
5339 		bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
5340 		if (!bnapi)
5341 			return -ENOMEM;
5342 
5343 		bp->bnapi = bnapi;
5344 		bnapi += arr_size;
5345 		for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
5346 			bp->bnapi[i] = bnapi;
5347 			bp->bnapi[i]->index = i;
5348 			bp->bnapi[i]->bp = bp;
5349 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5350 				struct bnxt_cp_ring_info *cpr =
5351 					&bp->bnapi[i]->cp_ring;
5352 
5353 				cpr->cp_ring_struct.ring_mem.flags =
5354 					BNXT_RMEM_RING_PTE_FLAG;
5355 			}
5356 		}
5357 
5358 		bp->rx_ring = kcalloc(bp->rx_nr_rings,
5359 				      sizeof(struct bnxt_rx_ring_info),
5360 				      GFP_KERNEL);
5361 		if (!bp->rx_ring)
5362 			return -ENOMEM;
5363 
5364 		for (i = 0; i < bp->rx_nr_rings; i++) {
5365 			struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
5366 
5367 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
5368 				rxr->rx_ring_struct.ring_mem.flags =
5369 					BNXT_RMEM_RING_PTE_FLAG;
5370 				rxr->rx_agg_ring_struct.ring_mem.flags =
5371 					BNXT_RMEM_RING_PTE_FLAG;
5372 			} else {
5373 				rxr->rx_cpr =  &bp->bnapi[i]->cp_ring;
5374 			}
5375 			rxr->bnapi = bp->bnapi[i];
5376 			bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
5377 		}
5378 
5379 		bp->tx_ring = kcalloc(bp->tx_nr_rings,
5380 				      sizeof(struct bnxt_tx_ring_info),
5381 				      GFP_KERNEL);
5382 		if (!bp->tx_ring)
5383 			return -ENOMEM;
5384 
5385 		bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
5386 					  GFP_KERNEL);
5387 
5388 		if (!bp->tx_ring_map)
5389 			return -ENOMEM;
5390 
5391 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
5392 			j = 0;
5393 		else
5394 			j = bp->rx_nr_rings;
5395 
5396 		for (i = 0; i < bp->tx_nr_rings; i++) {
5397 			struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
5398 			struct bnxt_napi *bnapi2;
5399 
5400 			if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
5401 				txr->tx_ring_struct.ring_mem.flags =
5402 					BNXT_RMEM_RING_PTE_FLAG;
5403 			bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
5404 			if (i >= bp->tx_nr_rings_xdp) {
5405 				int k = j + BNXT_RING_TO_TC_OFF(bp, i);
5406 
5407 				bnapi2 = bp->bnapi[k];
5408 				txr->txq_index = i - bp->tx_nr_rings_xdp;
5409 				txr->tx_napi_idx =
5410 					BNXT_RING_TO_TC(bp, txr->txq_index);
5411 				bnapi2->tx_ring[txr->tx_napi_idx] = txr;
5412 				bnapi2->tx_int = bnxt_tx_int;
5413 			} else {
5414 				bnapi2 = bp->bnapi[j];
5415 				bnapi2->flags |= BNXT_NAPI_FLAG_XDP;
5416 				bnapi2->tx_ring[0] = txr;
5417 				bnapi2->tx_int = bnxt_tx_int_xdp;
5418 				j++;
5419 			}
5420 			txr->bnapi = bnapi2;
5421 			if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
5422 				txr->tx_cpr = &bnapi2->cp_ring;
5423 		}
5424 
5425 		rc = bnxt_alloc_stats(bp);
5426 		if (rc)
5427 			goto alloc_mem_err;
5428 		bnxt_init_stats(bp);
5429 
5430 		rc = bnxt_alloc_ntp_fltrs(bp);
5431 		if (rc)
5432 			goto alloc_mem_err;
5433 
5434 		rc = bnxt_alloc_vnics(bp);
5435 		if (rc)
5436 			goto alloc_mem_err;
5437 	}
5438 
5439 	rc = bnxt_alloc_all_cp_arrays(bp);
5440 	if (rc)
5441 		goto alloc_mem_err;
5442 
5443 	bnxt_init_ring_struct(bp);
5444 
5445 	rc = bnxt_alloc_rx_rings(bp);
5446 	if (rc)
5447 		goto alloc_mem_err;
5448 
5449 	rc = bnxt_alloc_tx_rings(bp);
5450 	if (rc)
5451 		goto alloc_mem_err;
5452 
5453 	rc = bnxt_alloc_cp_rings(bp);
5454 	if (rc)
5455 		goto alloc_mem_err;
5456 
5457 	bp->vnic_info[BNXT_VNIC_DEFAULT].flags |= BNXT_VNIC_RSS_FLAG |
5458 						  BNXT_VNIC_MCAST_FLAG |
5459 						  BNXT_VNIC_UCAST_FLAG;
5460 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp) && (bp->flags & BNXT_FLAG_RFS))
5461 		bp->vnic_info[BNXT_VNIC_NTUPLE].flags |=
5462 			BNXT_VNIC_RSS_FLAG | BNXT_VNIC_NTUPLE_FLAG;
5463 
5464 	rc = bnxt_alloc_vnic_attributes(bp);
5465 	if (rc)
5466 		goto alloc_mem_err;
5467 	return 0;
5468 
5469 alloc_mem_err:
5470 	bnxt_free_mem(bp, true);
5471 	return rc;
5472 }
5473 
5474 static void bnxt_disable_int(struct bnxt *bp)
5475 {
5476 	int i;
5477 
5478 	if (!bp->bnapi)
5479 		return;
5480 
5481 	for (i = 0; i < bp->cp_nr_rings; i++) {
5482 		struct bnxt_napi *bnapi = bp->bnapi[i];
5483 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5484 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
5485 
5486 		if (ring->fw_ring_id != INVALID_HW_RING_ID)
5487 			bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
5488 	}
5489 }
5490 
5491 static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
5492 {
5493 	struct bnxt_napi *bnapi = bp->bnapi[n];
5494 	struct bnxt_cp_ring_info *cpr;
5495 
5496 	cpr = &bnapi->cp_ring;
5497 	return cpr->cp_ring_struct.map_idx;
5498 }
5499 
5500 static void bnxt_disable_int_sync(struct bnxt *bp)
5501 {
5502 	int i;
5503 
5504 	if (!bp->irq_tbl)
5505 		return;
5506 
5507 	atomic_inc(&bp->intr_sem);
5508 
5509 	bnxt_disable_int(bp);
5510 	for (i = 0; i < bp->cp_nr_rings; i++) {
5511 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
5512 
5513 		synchronize_irq(bp->irq_tbl[map_idx].vector);
5514 	}
5515 }
5516 
5517 static void bnxt_enable_int(struct bnxt *bp)
5518 {
5519 	int i;
5520 
5521 	atomic_set(&bp->intr_sem, 0);
5522 	for (i = 0; i < bp->cp_nr_rings; i++) {
5523 		struct bnxt_napi *bnapi = bp->bnapi[i];
5524 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
5525 
5526 		bnxt_db_nq_arm(bp, &cpr->cp_db, cpr->cp_raw_cons);
5527 	}
5528 }
5529 
5530 int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp, unsigned long *bmap, int bmap_size,
5531 			    bool async_only)
5532 {
5533 	DECLARE_BITMAP(async_events_bmap, 256);
5534 	u32 *events = (u32 *)async_events_bmap;
5535 	struct hwrm_func_drv_rgtr_output *resp;
5536 	struct hwrm_func_drv_rgtr_input *req;
5537 	u32 flags;
5538 	int rc, i;
5539 
5540 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_RGTR);
5541 	if (rc)
5542 		return rc;
5543 
5544 	req->enables = cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
5545 				   FUNC_DRV_RGTR_REQ_ENABLES_VER |
5546 				   FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5547 
5548 	req->os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
5549 	flags = FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE;
5550 	if (bp->fw_cap & BNXT_FW_CAP_HOT_RESET)
5551 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_HOT_RESET_SUPPORT;
5552 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
5553 		flags |= FUNC_DRV_RGTR_REQ_FLAGS_ERROR_RECOVERY_SUPPORT |
5554 			 FUNC_DRV_RGTR_REQ_FLAGS_MASTER_SUPPORT;
5555 	req->flags = cpu_to_le32(flags);
5556 	req->ver_maj_8b = DRV_VER_MAJ;
5557 	req->ver_min_8b = DRV_VER_MIN;
5558 	req->ver_upd_8b = DRV_VER_UPD;
5559 	req->ver_maj = cpu_to_le16(DRV_VER_MAJ);
5560 	req->ver_min = cpu_to_le16(DRV_VER_MIN);
5561 	req->ver_upd = cpu_to_le16(DRV_VER_UPD);
5562 
5563 	if (BNXT_PF(bp)) {
5564 		u32 data[8];
5565 		int i;
5566 
5567 		memset(data, 0, sizeof(data));
5568 		for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
5569 			u16 cmd = bnxt_vf_req_snif[i];
5570 			unsigned int bit, idx;
5571 
5572 			idx = cmd / 32;
5573 			bit = cmd % 32;
5574 			data[idx] |= 1 << bit;
5575 		}
5576 
5577 		for (i = 0; i < 8; i++)
5578 			req->vf_req_fwd[i] = cpu_to_le32(data[i]);
5579 
5580 		req->enables |=
5581 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
5582 	}
5583 
5584 	if (bp->fw_cap & BNXT_FW_CAP_OVS_64BIT_HANDLE)
5585 		req->flags |= cpu_to_le32(
5586 			FUNC_DRV_RGTR_REQ_FLAGS_FLOW_HANDLE_64BIT_MODE);
5587 
5588 	memset(async_events_bmap, 0, sizeof(async_events_bmap));
5589 	for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++) {
5590 		u16 event_id = bnxt_async_events_arr[i];
5591 
5592 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_ERROR_RECOVERY &&
5593 		    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
5594 			continue;
5595 		if (event_id == ASYNC_EVENT_CMPL_EVENT_ID_PHC_UPDATE &&
5596 		    !bp->ptp_cfg)
5597 			continue;
5598 		__set_bit(bnxt_async_events_arr[i], async_events_bmap);
5599 	}
5600 	if (bmap && bmap_size) {
5601 		for (i = 0; i < bmap_size; i++) {
5602 			if (test_bit(i, bmap))
5603 				__set_bit(i, async_events_bmap);
5604 		}
5605 	}
5606 	for (i = 0; i < 8; i++)
5607 		req->async_event_fwd[i] |= cpu_to_le32(events[i]);
5608 
5609 	if (async_only)
5610 		req->enables =
5611 			cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
5612 
5613 	resp = hwrm_req_hold(bp, req);
5614 	rc = hwrm_req_send(bp, req);
5615 	if (!rc) {
5616 		set_bit(BNXT_STATE_DRV_REGISTERED, &bp->state);
5617 		if (resp->flags &
5618 		    cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
5619 			bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
5620 	}
5621 	hwrm_req_drop(bp, req);
5622 	return rc;
5623 }
5624 
5625 int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
5626 {
5627 	struct hwrm_func_drv_unrgtr_input *req;
5628 	int rc;
5629 
5630 	if (!test_and_clear_bit(BNXT_STATE_DRV_REGISTERED, &bp->state))
5631 		return 0;
5632 
5633 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_UNRGTR);
5634 	if (rc)
5635 		return rc;
5636 	return hwrm_req_send(bp, req);
5637 }
5638 
5639 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa);
5640 
5641 static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
5642 {
5643 	struct hwrm_tunnel_dst_port_free_input *req;
5644 	int rc;
5645 
5646 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN &&
5647 	    bp->vxlan_fw_dst_port_id == INVALID_HW_RING_ID)
5648 		return 0;
5649 	if (tunnel_type == TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE &&
5650 	    bp->nge_fw_dst_port_id == INVALID_HW_RING_ID)
5651 		return 0;
5652 
5653 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_FREE);
5654 	if (rc)
5655 		return rc;
5656 
5657 	req->tunnel_type = tunnel_type;
5658 
5659 	switch (tunnel_type) {
5660 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
5661 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_fw_dst_port_id);
5662 		bp->vxlan_port = 0;
5663 		bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
5664 		break;
5665 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
5666 		req->tunnel_dst_port_id = cpu_to_le16(bp->nge_fw_dst_port_id);
5667 		bp->nge_port = 0;
5668 		bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
5669 		break;
5670 	case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE:
5671 		req->tunnel_dst_port_id = cpu_to_le16(bp->vxlan_gpe_fw_dst_port_id);
5672 		bp->vxlan_gpe_port = 0;
5673 		bp->vxlan_gpe_fw_dst_port_id = INVALID_HW_RING_ID;
5674 		break;
5675 	default:
5676 		break;
5677 	}
5678 
5679 	rc = hwrm_req_send(bp, req);
5680 	if (rc)
5681 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
5682 			   rc);
5683 	if (bp->flags & BNXT_FLAG_TPA)
5684 		bnxt_set_tpa(bp, true);
5685 	return rc;
5686 }
5687 
5688 static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
5689 					   u8 tunnel_type)
5690 {
5691 	struct hwrm_tunnel_dst_port_alloc_output *resp;
5692 	struct hwrm_tunnel_dst_port_alloc_input *req;
5693 	int rc;
5694 
5695 	rc = hwrm_req_init(bp, req, HWRM_TUNNEL_DST_PORT_ALLOC);
5696 	if (rc)
5697 		return rc;
5698 
5699 	req->tunnel_type = tunnel_type;
5700 	req->tunnel_dst_port_val = port;
5701 
5702 	resp = hwrm_req_hold(bp, req);
5703 	rc = hwrm_req_send(bp, req);
5704 	if (rc) {
5705 		netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
5706 			   rc);
5707 		goto err_out;
5708 	}
5709 
5710 	switch (tunnel_type) {
5711 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
5712 		bp->vxlan_port = port;
5713 		bp->vxlan_fw_dst_port_id =
5714 			le16_to_cpu(resp->tunnel_dst_port_id);
5715 		break;
5716 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
5717 		bp->nge_port = port;
5718 		bp->nge_fw_dst_port_id = le16_to_cpu(resp->tunnel_dst_port_id);
5719 		break;
5720 	case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE:
5721 		bp->vxlan_gpe_port = port;
5722 		bp->vxlan_gpe_fw_dst_port_id =
5723 			le16_to_cpu(resp->tunnel_dst_port_id);
5724 		break;
5725 	default:
5726 		break;
5727 	}
5728 	if (bp->flags & BNXT_FLAG_TPA)
5729 		bnxt_set_tpa(bp, true);
5730 
5731 err_out:
5732 	hwrm_req_drop(bp, req);
5733 	return rc;
5734 }
5735 
5736 static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
5737 {
5738 	struct hwrm_cfa_l2_set_rx_mask_input *req;
5739 	struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
5740 	int rc;
5741 
5742 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_SET_RX_MASK);
5743 	if (rc)
5744 		return rc;
5745 
5746 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
5747 	if (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST) {
5748 		req->num_mc_entries = cpu_to_le32(vnic->mc_list_count);
5749 		req->mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
5750 	}
5751 	req->mask = cpu_to_le32(vnic->rx_mask);
5752 	return hwrm_req_send_silent(bp, req);
5753 }
5754 
5755 void bnxt_del_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5756 {
5757 	if (!atomic_dec_and_test(&fltr->refcnt))
5758 		return;
5759 	spin_lock_bh(&bp->ntp_fltr_lock);
5760 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
5761 		spin_unlock_bh(&bp->ntp_fltr_lock);
5762 		return;
5763 	}
5764 	hlist_del_rcu(&fltr->base.hash);
5765 	bnxt_del_one_usr_fltr(bp, &fltr->base);
5766 	if (fltr->base.flags) {
5767 		clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
5768 		bp->ntp_fltr_count--;
5769 	}
5770 	spin_unlock_bh(&bp->ntp_fltr_lock);
5771 	kfree_rcu(fltr, base.rcu);
5772 }
5773 
5774 static struct bnxt_l2_filter *__bnxt_lookup_l2_filter(struct bnxt *bp,
5775 						      struct bnxt_l2_key *key,
5776 						      u32 idx)
5777 {
5778 	struct hlist_head *head = &bp->l2_fltr_hash_tbl[idx];
5779 	struct bnxt_l2_filter *fltr;
5780 
5781 	hlist_for_each_entry_rcu(fltr, head, base.hash) {
5782 		struct bnxt_l2_key *l2_key = &fltr->l2_key;
5783 
5784 		if (ether_addr_equal(l2_key->dst_mac_addr, key->dst_mac_addr) &&
5785 		    l2_key->vlan == key->vlan)
5786 			return fltr;
5787 	}
5788 	return NULL;
5789 }
5790 
5791 static struct bnxt_l2_filter *bnxt_lookup_l2_filter(struct bnxt *bp,
5792 						    struct bnxt_l2_key *key,
5793 						    u32 idx)
5794 {
5795 	struct bnxt_l2_filter *fltr = NULL;
5796 
5797 	rcu_read_lock();
5798 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5799 	if (fltr)
5800 		atomic_inc(&fltr->refcnt);
5801 	rcu_read_unlock();
5802 	return fltr;
5803 }
5804 
5805 #define BNXT_IPV4_4TUPLE(bp, fkeys)					\
5806 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5807 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4) ||	\
5808 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5809 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4))
5810 
5811 #define BNXT_IPV6_4TUPLE(bp, fkeys)					\
5812 	(((fkeys)->basic.ip_proto == IPPROTO_TCP &&			\
5813 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6) ||	\
5814 	 ((fkeys)->basic.ip_proto == IPPROTO_UDP &&			\
5815 	  (bp)->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6))
5816 
5817 static u32 bnxt_get_rss_flow_tuple_len(struct bnxt *bp, struct flow_keys *fkeys)
5818 {
5819 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5820 		if (BNXT_IPV4_4TUPLE(bp, fkeys))
5821 			return sizeof(fkeys->addrs.v4addrs) +
5822 			       sizeof(fkeys->ports);
5823 
5824 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4)
5825 			return sizeof(fkeys->addrs.v4addrs);
5826 	}
5827 
5828 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
5829 		if (BNXT_IPV6_4TUPLE(bp, fkeys))
5830 			return sizeof(fkeys->addrs.v6addrs) +
5831 			       sizeof(fkeys->ports);
5832 
5833 		if (bp->rss_hash_cfg & VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6)
5834 			return sizeof(fkeys->addrs.v6addrs);
5835 	}
5836 
5837 	return 0;
5838 }
5839 
5840 static u32 bnxt_toeplitz(struct bnxt *bp, struct flow_keys *fkeys,
5841 			 const unsigned char *key)
5842 {
5843 	u64 prefix = bp->toeplitz_prefix, hash = 0;
5844 	struct bnxt_ipv4_tuple tuple4;
5845 	struct bnxt_ipv6_tuple tuple6;
5846 	int i, j, len = 0;
5847 	u8 *four_tuple;
5848 
5849 	len = bnxt_get_rss_flow_tuple_len(bp, fkeys);
5850 	if (!len)
5851 		return 0;
5852 
5853 	if (fkeys->basic.n_proto == htons(ETH_P_IP)) {
5854 		tuple4.v4addrs = fkeys->addrs.v4addrs;
5855 		tuple4.ports = fkeys->ports;
5856 		four_tuple = (unsigned char *)&tuple4;
5857 	} else {
5858 		tuple6.v6addrs = fkeys->addrs.v6addrs;
5859 		tuple6.ports = fkeys->ports;
5860 		four_tuple = (unsigned char *)&tuple6;
5861 	}
5862 
5863 	for (i = 0, j = 8; i < len; i++, j++) {
5864 		u8 byte = four_tuple[i];
5865 		int bit;
5866 
5867 		for (bit = 0; bit < 8; bit++, prefix <<= 1, byte <<= 1) {
5868 			if (byte & 0x80)
5869 				hash ^= prefix;
5870 		}
5871 		prefix |= (j < HW_HASH_KEY_SIZE) ? key[j] : 0;
5872 	}
5873 
5874 	/* The valid part of the hash is in the upper 32 bits. */
5875 	return (hash >> 32) & BNXT_NTP_FLTR_HASH_MASK;
5876 }
5877 
5878 #ifdef CONFIG_RFS_ACCEL
5879 static struct bnxt_l2_filter *
5880 bnxt_lookup_l2_filter_from_key(struct bnxt *bp, struct bnxt_l2_key *key)
5881 {
5882 	struct bnxt_l2_filter *fltr;
5883 	u32 idx;
5884 
5885 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5886 	      BNXT_L2_FLTR_HASH_MASK;
5887 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5888 	return fltr;
5889 }
5890 #endif
5891 
5892 static int bnxt_init_l2_filter(struct bnxt *bp, struct bnxt_l2_filter *fltr,
5893 			       struct bnxt_l2_key *key, u32 idx)
5894 {
5895 	struct hlist_head *head;
5896 
5897 	ether_addr_copy(fltr->l2_key.dst_mac_addr, key->dst_mac_addr);
5898 	fltr->l2_key.vlan = key->vlan;
5899 	fltr->base.type = BNXT_FLTR_TYPE_L2;
5900 	if (fltr->base.flags) {
5901 		int bit_id;
5902 
5903 		bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
5904 						 bp->max_fltr, 0);
5905 		if (bit_id < 0)
5906 			return -ENOMEM;
5907 		fltr->base.sw_id = (u16)bit_id;
5908 		bp->ntp_fltr_count++;
5909 	}
5910 	head = &bp->l2_fltr_hash_tbl[idx];
5911 	hlist_add_head_rcu(&fltr->base.hash, head);
5912 	bnxt_insert_usr_fltr(bp, &fltr->base);
5913 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
5914 	atomic_set(&fltr->refcnt, 1);
5915 	return 0;
5916 }
5917 
5918 static struct bnxt_l2_filter *bnxt_alloc_l2_filter(struct bnxt *bp,
5919 						   struct bnxt_l2_key *key,
5920 						   gfp_t gfp)
5921 {
5922 	struct bnxt_l2_filter *fltr;
5923 	u32 idx;
5924 	int rc;
5925 
5926 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5927 	      BNXT_L2_FLTR_HASH_MASK;
5928 	fltr = bnxt_lookup_l2_filter(bp, key, idx);
5929 	if (fltr)
5930 		return fltr;
5931 
5932 	fltr = kzalloc(sizeof(*fltr), gfp);
5933 	if (!fltr)
5934 		return ERR_PTR(-ENOMEM);
5935 	spin_lock_bh(&bp->ntp_fltr_lock);
5936 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5937 	spin_unlock_bh(&bp->ntp_fltr_lock);
5938 	if (rc) {
5939 		bnxt_del_l2_filter(bp, fltr);
5940 		fltr = ERR_PTR(rc);
5941 	}
5942 	return fltr;
5943 }
5944 
5945 struct bnxt_l2_filter *bnxt_alloc_new_l2_filter(struct bnxt *bp,
5946 						struct bnxt_l2_key *key,
5947 						u16 flags)
5948 {
5949 	struct bnxt_l2_filter *fltr;
5950 	u32 idx;
5951 	int rc;
5952 
5953 	idx = jhash2(&key->filter_key, BNXT_L2_KEY_SIZE, bp->hash_seed) &
5954 	      BNXT_L2_FLTR_HASH_MASK;
5955 	spin_lock_bh(&bp->ntp_fltr_lock);
5956 	fltr = __bnxt_lookup_l2_filter(bp, key, idx);
5957 	if (fltr) {
5958 		fltr = ERR_PTR(-EEXIST);
5959 		goto l2_filter_exit;
5960 	}
5961 	fltr = kzalloc(sizeof(*fltr), GFP_ATOMIC);
5962 	if (!fltr) {
5963 		fltr = ERR_PTR(-ENOMEM);
5964 		goto l2_filter_exit;
5965 	}
5966 	fltr->base.flags = flags;
5967 	rc = bnxt_init_l2_filter(bp, fltr, key, idx);
5968 	if (rc) {
5969 		spin_unlock_bh(&bp->ntp_fltr_lock);
5970 		bnxt_del_l2_filter(bp, fltr);
5971 		return ERR_PTR(rc);
5972 	}
5973 
5974 l2_filter_exit:
5975 	spin_unlock_bh(&bp->ntp_fltr_lock);
5976 	return fltr;
5977 }
5978 
5979 static u16 bnxt_vf_target_id(struct bnxt_pf_info *pf, u16 vf_idx)
5980 {
5981 #ifdef CONFIG_BNXT_SRIOV
5982 	struct bnxt_vf_info *vf = &pf->vf[vf_idx];
5983 
5984 	return vf->fw_fid;
5985 #else
5986 	return INVALID_HW_RING_ID;
5987 #endif
5988 }
5989 
5990 int bnxt_hwrm_l2_filter_free(struct bnxt *bp, struct bnxt_l2_filter *fltr)
5991 {
5992 	struct hwrm_cfa_l2_filter_free_input *req;
5993 	u16 target_id = 0xffff;
5994 	int rc;
5995 
5996 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
5997 		struct bnxt_pf_info *pf = &bp->pf;
5998 
5999 		if (fltr->base.vf_idx >= pf->active_vfs)
6000 			return -EINVAL;
6001 
6002 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6003 		if (target_id == INVALID_HW_RING_ID)
6004 			return -EINVAL;
6005 	}
6006 
6007 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_FREE);
6008 	if (rc)
6009 		return rc;
6010 
6011 	req->target_id = cpu_to_le16(target_id);
6012 	req->l2_filter_id = fltr->base.filter_id;
6013 	return hwrm_req_send(bp, req);
6014 }
6015 
6016 int bnxt_hwrm_l2_filter_alloc(struct bnxt *bp, struct bnxt_l2_filter *fltr)
6017 {
6018 	struct hwrm_cfa_l2_filter_alloc_output *resp;
6019 	struct hwrm_cfa_l2_filter_alloc_input *req;
6020 	u16 target_id = 0xffff;
6021 	int rc;
6022 
6023 	if (fltr->base.flags & BNXT_ACT_FUNC_DST) {
6024 		struct bnxt_pf_info *pf = &bp->pf;
6025 
6026 		if (fltr->base.vf_idx >= pf->active_vfs)
6027 			return -EINVAL;
6028 
6029 		target_id = bnxt_vf_target_id(pf, fltr->base.vf_idx);
6030 	}
6031 	rc = hwrm_req_init(bp, req, HWRM_CFA_L2_FILTER_ALLOC);
6032 	if (rc)
6033 		return rc;
6034 
6035 	req->target_id = cpu_to_le16(target_id);
6036 	req->flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
6037 
6038 	if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
6039 		req->flags |=
6040 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
6041 	req->dst_id = cpu_to_le16(fltr->base.fw_vnic_id);
6042 	req->enables =
6043 		cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
6044 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
6045 			    CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
6046 	ether_addr_copy(req->l2_addr, fltr->l2_key.dst_mac_addr);
6047 	eth_broadcast_addr(req->l2_addr_mask);
6048 
6049 	if (fltr->l2_key.vlan) {
6050 		req->enables |=
6051 			cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN |
6052 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_IVLAN_MASK |
6053 				CFA_L2_FILTER_ALLOC_REQ_ENABLES_NUM_VLANS);
6054 		req->num_vlans = 1;
6055 		req->l2_ivlan = cpu_to_le16(fltr->l2_key.vlan);
6056 		req->l2_ivlan_mask = cpu_to_le16(0xfff);
6057 	}
6058 
6059 	resp = hwrm_req_hold(bp, req);
6060 	rc = hwrm_req_send(bp, req);
6061 	if (!rc) {
6062 		fltr->base.filter_id = resp->l2_filter_id;
6063 		set_bit(BNXT_FLTR_VALID, &fltr->base.state);
6064 	}
6065 	hwrm_req_drop(bp, req);
6066 	return rc;
6067 }
6068 
6069 int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
6070 				     struct bnxt_ntuple_filter *fltr)
6071 {
6072 	struct hwrm_cfa_ntuple_filter_free_input *req;
6073 	int rc;
6074 
6075 	set_bit(BNXT_FLTR_FW_DELETED, &fltr->base.state);
6076 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_FREE);
6077 	if (rc)
6078 		return rc;
6079 
6080 	req->ntuple_filter_id = fltr->base.filter_id;
6081 	return hwrm_req_send(bp, req);
6082 }
6083 
6084 #define BNXT_NTP_FLTR_FLAGS					\
6085 	(CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID |	\
6086 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE |	\
6087 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE |	\
6088 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR |	\
6089 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK |	\
6090 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR |	\
6091 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK |	\
6092 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL |	\
6093 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT |		\
6094 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK |	\
6095 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT |		\
6096 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK |	\
6097 	 CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
6098 
6099 #define BNXT_NTP_TUNNEL_FLTR_FLAG				\
6100 		CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
6101 
6102 void bnxt_fill_ipv6_mask(__be32 mask[4])
6103 {
6104 	int i;
6105 
6106 	for (i = 0; i < 4; i++)
6107 		mask[i] = cpu_to_be32(~0);
6108 }
6109 
6110 static void
6111 bnxt_cfg_rfs_ring_tbl_idx(struct bnxt *bp,
6112 			  struct hwrm_cfa_ntuple_filter_alloc_input *req,
6113 			  struct bnxt_ntuple_filter *fltr)
6114 {
6115 	u16 rxq = fltr->base.rxq;
6116 
6117 	if (fltr->base.flags & BNXT_ACT_RSS_CTX) {
6118 		struct ethtool_rxfh_context *ctx;
6119 		struct bnxt_rss_ctx *rss_ctx;
6120 		struct bnxt_vnic_info *vnic;
6121 
6122 		ctx = xa_load(&bp->dev->ethtool->rss_ctx,
6123 			      fltr->base.fw_vnic_id);
6124 		if (ctx) {
6125 			rss_ctx = ethtool_rxfh_context_priv(ctx);
6126 			vnic = &rss_ctx->vnic;
6127 
6128 			req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6129 		}
6130 		return;
6131 	}
6132 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
6133 		struct bnxt_vnic_info *vnic;
6134 		u32 enables;
6135 
6136 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
6137 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6138 		enables = CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_RFS_RING_TBL_IDX;
6139 		req->enables |= cpu_to_le32(enables);
6140 		req->rfs_ring_tbl_idx = cpu_to_le16(rxq);
6141 	} else {
6142 		u32 flags;
6143 
6144 		flags = CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DEST_RFS_RING_IDX;
6145 		req->flags |= cpu_to_le32(flags);
6146 		req->dst_id = cpu_to_le16(rxq);
6147 	}
6148 }
6149 
6150 int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
6151 				      struct bnxt_ntuple_filter *fltr)
6152 {
6153 	struct hwrm_cfa_ntuple_filter_alloc_output *resp;
6154 	struct hwrm_cfa_ntuple_filter_alloc_input *req;
6155 	struct bnxt_flow_masks *masks = &fltr->fmasks;
6156 	struct flow_keys *keys = &fltr->fkeys;
6157 	struct bnxt_l2_filter *l2_fltr;
6158 	struct bnxt_vnic_info *vnic;
6159 	int rc;
6160 
6161 	rc = hwrm_req_init(bp, req, HWRM_CFA_NTUPLE_FILTER_ALLOC);
6162 	if (rc)
6163 		return rc;
6164 
6165 	l2_fltr = fltr->l2_fltr;
6166 	req->l2_filter_id = l2_fltr->base.filter_id;
6167 
6168 	if (fltr->base.flags & BNXT_ACT_DROP) {
6169 		req->flags =
6170 			cpu_to_le32(CFA_NTUPLE_FILTER_ALLOC_REQ_FLAGS_DROP);
6171 	} else if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2) {
6172 		bnxt_cfg_rfs_ring_tbl_idx(bp, req, fltr);
6173 	} else {
6174 		vnic = &bp->vnic_info[fltr->base.rxq + 1];
6175 		req->dst_id = cpu_to_le16(vnic->fw_vnic_id);
6176 	}
6177 	req->enables |= cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
6178 
6179 	req->ethertype = htons(ETH_P_IP);
6180 	req->ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
6181 	req->ip_protocol = keys->basic.ip_proto;
6182 
6183 	if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
6184 		req->ethertype = htons(ETH_P_IPV6);
6185 		req->ip_addr_type =
6186 			CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
6187 		*(struct in6_addr *)&req->src_ipaddr[0] = keys->addrs.v6addrs.src;
6188 		*(struct in6_addr *)&req->src_ipaddr_mask[0] = masks->addrs.v6addrs.src;
6189 		*(struct in6_addr *)&req->dst_ipaddr[0] = keys->addrs.v6addrs.dst;
6190 		*(struct in6_addr *)&req->dst_ipaddr_mask[0] = masks->addrs.v6addrs.dst;
6191 	} else {
6192 		req->src_ipaddr[0] = keys->addrs.v4addrs.src;
6193 		req->src_ipaddr_mask[0] = masks->addrs.v4addrs.src;
6194 		req->dst_ipaddr[0] = keys->addrs.v4addrs.dst;
6195 		req->dst_ipaddr_mask[0] = masks->addrs.v4addrs.dst;
6196 	}
6197 	if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
6198 		req->enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
6199 		req->tunnel_type =
6200 			CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
6201 	}
6202 
6203 	req->src_port = keys->ports.src;
6204 	req->src_port_mask = masks->ports.src;
6205 	req->dst_port = keys->ports.dst;
6206 	req->dst_port_mask = masks->ports.dst;
6207 
6208 	resp = hwrm_req_hold(bp, req);
6209 	rc = hwrm_req_send(bp, req);
6210 	if (!rc)
6211 		fltr->base.filter_id = resp->ntuple_filter_id;
6212 	hwrm_req_drop(bp, req);
6213 	return rc;
6214 }
6215 
6216 static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
6217 				     const u8 *mac_addr)
6218 {
6219 	struct bnxt_l2_filter *fltr;
6220 	struct bnxt_l2_key key;
6221 	int rc;
6222 
6223 	ether_addr_copy(key.dst_mac_addr, mac_addr);
6224 	key.vlan = 0;
6225 	fltr = bnxt_alloc_l2_filter(bp, &key, GFP_KERNEL);
6226 	if (IS_ERR(fltr))
6227 		return PTR_ERR(fltr);
6228 
6229 	fltr->base.fw_vnic_id = bp->vnic_info[vnic_id].fw_vnic_id;
6230 	rc = bnxt_hwrm_l2_filter_alloc(bp, fltr);
6231 	if (rc)
6232 		bnxt_del_l2_filter(bp, fltr);
6233 	else
6234 		bp->vnic_info[vnic_id].l2_filters[idx] = fltr;
6235 	return rc;
6236 }
6237 
6238 static void bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
6239 {
6240 	u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
6241 
6242 	/* Any associated ntuple filters will also be cleared by firmware. */
6243 	for (i = 0; i < num_of_vnics; i++) {
6244 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6245 
6246 		for (j = 0; j < vnic->uc_filter_count; j++) {
6247 			struct bnxt_l2_filter *fltr = vnic->l2_filters[j];
6248 
6249 			bnxt_hwrm_l2_filter_free(bp, fltr);
6250 			bnxt_del_l2_filter(bp, fltr);
6251 		}
6252 		vnic->uc_filter_count = 0;
6253 	}
6254 }
6255 
6256 #define BNXT_DFLT_TUNL_TPA_BMAP				\
6257 	(VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GRE |	\
6258 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV4 |	\
6259 	 VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_IPV6)
6260 
6261 static void bnxt_hwrm_vnic_update_tunl_tpa(struct bnxt *bp,
6262 					   struct hwrm_vnic_tpa_cfg_input *req)
6263 {
6264 	u32 tunl_tpa_bmap = BNXT_DFLT_TUNL_TPA_BMAP;
6265 
6266 	if (!(bp->fw_cap & BNXT_FW_CAP_VNIC_TUNNEL_TPA))
6267 		return;
6268 
6269 	if (bp->vxlan_port)
6270 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN;
6271 	if (bp->vxlan_gpe_port)
6272 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_VXLAN_GPE;
6273 	if (bp->nge_port)
6274 		tunl_tpa_bmap |= VNIC_TPA_CFG_REQ_TNL_TPA_EN_BITMAP_GENEVE;
6275 
6276 	req->enables |= cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_TNL_TPA_EN);
6277 	req->tnl_tpa_en_bitmap = cpu_to_le32(tunl_tpa_bmap);
6278 }
6279 
6280 int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6281 			   u32 tpa_flags)
6282 {
6283 	u16 max_aggs = VNIC_TPA_CFG_REQ_MAX_AGGS_MAX;
6284 	struct hwrm_vnic_tpa_cfg_input *req;
6285 	int rc;
6286 
6287 	if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
6288 		return 0;
6289 
6290 	rc = hwrm_req_init(bp, req, HWRM_VNIC_TPA_CFG);
6291 	if (rc)
6292 		return rc;
6293 
6294 	if (tpa_flags) {
6295 		u16 mss = bp->dev->mtu - 40;
6296 		u32 nsegs, n, segs = 0, flags;
6297 
6298 		flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
6299 			VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
6300 			VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
6301 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
6302 			VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
6303 		if (tpa_flags & BNXT_FLAG_GRO)
6304 			flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
6305 
6306 		req->flags = cpu_to_le32(flags);
6307 
6308 		req->enables =
6309 			cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
6310 				    VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
6311 				    VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
6312 
6313 		/* Number of segs are log2 units, and first packet is not
6314 		 * included as part of this units.
6315 		 */
6316 		if (mss <= BNXT_RX_PAGE_SIZE) {
6317 			n = BNXT_RX_PAGE_SIZE / mss;
6318 			nsegs = (MAX_SKB_FRAGS - 1) * n;
6319 		} else {
6320 			n = mss / BNXT_RX_PAGE_SIZE;
6321 			if (mss & (BNXT_RX_PAGE_SIZE - 1))
6322 				n++;
6323 			nsegs = (MAX_SKB_FRAGS - n) / n;
6324 		}
6325 
6326 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6327 			segs = MAX_TPA_SEGS_P5;
6328 			max_aggs = bp->max_tpa;
6329 		} else {
6330 			segs = ilog2(nsegs);
6331 		}
6332 		req->max_agg_segs = cpu_to_le16(segs);
6333 		req->max_aggs = cpu_to_le16(max_aggs);
6334 
6335 		req->min_agg_len = cpu_to_le32(512);
6336 		bnxt_hwrm_vnic_update_tunl_tpa(bp, req);
6337 	}
6338 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6339 
6340 	return hwrm_req_send(bp, req);
6341 }
6342 
6343 static u16 bnxt_cp_ring_from_grp(struct bnxt *bp, struct bnxt_ring_struct *ring)
6344 {
6345 	struct bnxt_ring_grp_info *grp_info;
6346 
6347 	grp_info = &bp->grp_info[ring->grp_idx];
6348 	return grp_info->cp_fw_ring_id;
6349 }
6350 
6351 static u16 bnxt_cp_ring_for_rx(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
6352 {
6353 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6354 		return rxr->rx_cpr->cp_ring_struct.fw_ring_id;
6355 	else
6356 		return bnxt_cp_ring_from_grp(bp, &rxr->rx_ring_struct);
6357 }
6358 
6359 static u16 bnxt_cp_ring_for_tx(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
6360 {
6361 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6362 		return txr->tx_cpr->cp_ring_struct.fw_ring_id;
6363 	else
6364 		return bnxt_cp_ring_from_grp(bp, &txr->tx_ring_struct);
6365 }
6366 
6367 static int bnxt_alloc_rss_indir_tbl(struct bnxt *bp)
6368 {
6369 	int entries;
6370 
6371 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6372 		entries = BNXT_MAX_RSS_TABLE_ENTRIES_P5;
6373 	else
6374 		entries = HW_HASH_INDEX_SIZE;
6375 
6376 	bp->rss_indir_tbl_entries = entries;
6377 	bp->rss_indir_tbl =
6378 		kmalloc_array(entries, sizeof(*bp->rss_indir_tbl), GFP_KERNEL);
6379 	if (!bp->rss_indir_tbl)
6380 		return -ENOMEM;
6381 
6382 	return 0;
6383 }
6384 
6385 void bnxt_set_dflt_rss_indir_tbl(struct bnxt *bp,
6386 				 struct ethtool_rxfh_context *rss_ctx)
6387 {
6388 	u16 max_rings, max_entries, pad, i;
6389 	u32 *rss_indir_tbl;
6390 
6391 	if (!bp->rx_nr_rings)
6392 		return;
6393 
6394 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6395 		max_rings = bp->rx_nr_rings - 1;
6396 	else
6397 		max_rings = bp->rx_nr_rings;
6398 
6399 	max_entries = bnxt_get_rxfh_indir_size(bp->dev);
6400 	if (rss_ctx)
6401 		rss_indir_tbl = ethtool_rxfh_context_indir(rss_ctx);
6402 	else
6403 		rss_indir_tbl = &bp->rss_indir_tbl[0];
6404 
6405 	for (i = 0; i < max_entries; i++)
6406 		rss_indir_tbl[i] = ethtool_rxfh_indir_default(i, max_rings);
6407 
6408 	pad = bp->rss_indir_tbl_entries - max_entries;
6409 	if (pad)
6410 		memset(&rss_indir_tbl[i], 0, pad * sizeof(*rss_indir_tbl));
6411 }
6412 
6413 static u16 bnxt_get_max_rss_ring(struct bnxt *bp)
6414 {
6415 	u32 i, tbl_size, max_ring = 0;
6416 
6417 	if (!bp->rss_indir_tbl)
6418 		return 0;
6419 
6420 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6421 	for (i = 0; i < tbl_size; i++)
6422 		max_ring = max(max_ring, bp->rss_indir_tbl[i]);
6423 	return max_ring;
6424 }
6425 
6426 int bnxt_get_nr_rss_ctxs(struct bnxt *bp, int rx_rings)
6427 {
6428 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6429 		if (!rx_rings)
6430 			return 0;
6431 		return bnxt_calc_nr_ring_pages(rx_rings - 1,
6432 					       BNXT_RSS_TABLE_ENTRIES_P5);
6433 	}
6434 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
6435 		return 2;
6436 	return 1;
6437 }
6438 
6439 static void bnxt_fill_hw_rss_tbl(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6440 {
6441 	bool no_rss = !(vnic->flags & BNXT_VNIC_RSS_FLAG);
6442 	u16 i, j;
6443 
6444 	/* Fill the RSS indirection table with ring group ids */
6445 	for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
6446 		if (!no_rss)
6447 			j = bp->rss_indir_tbl[i];
6448 		vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
6449 	}
6450 }
6451 
6452 static void bnxt_fill_hw_rss_tbl_p5(struct bnxt *bp,
6453 				    struct bnxt_vnic_info *vnic)
6454 {
6455 	__le16 *ring_tbl = vnic->rss_table;
6456 	struct bnxt_rx_ring_info *rxr;
6457 	u16 tbl_size, i;
6458 
6459 	tbl_size = bnxt_get_rxfh_indir_size(bp->dev);
6460 
6461 	for (i = 0; i < tbl_size; i++) {
6462 		u16 ring_id, j;
6463 
6464 		if (vnic->flags & BNXT_VNIC_NTUPLE_FLAG)
6465 			j = ethtool_rxfh_indir_default(i, bp->rx_nr_rings);
6466 		else if (vnic->flags & BNXT_VNIC_RSSCTX_FLAG)
6467 			j = ethtool_rxfh_context_indir(vnic->rss_ctx)[i];
6468 		else
6469 			j = bp->rss_indir_tbl[i];
6470 		rxr = &bp->rx_ring[j];
6471 
6472 		ring_id = rxr->rx_ring_struct.fw_ring_id;
6473 		*ring_tbl++ = cpu_to_le16(ring_id);
6474 		ring_id = bnxt_cp_ring_for_rx(bp, rxr);
6475 		*ring_tbl++ = cpu_to_le16(ring_id);
6476 	}
6477 }
6478 
6479 static void
6480 __bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct hwrm_vnic_rss_cfg_input *req,
6481 			 struct bnxt_vnic_info *vnic)
6482 {
6483 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6484 		bnxt_fill_hw_rss_tbl_p5(bp, vnic);
6485 		if (bp->flags & BNXT_FLAG_CHIP_P7)
6486 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_IPSEC_HASH_TYPE_CFG_SUPPORT;
6487 	} else {
6488 		bnxt_fill_hw_rss_tbl(bp, vnic);
6489 	}
6490 
6491 	if (bp->rss_hash_delta) {
6492 		req->hash_type = cpu_to_le32(bp->rss_hash_delta);
6493 		if (bp->rss_hash_cfg & bp->rss_hash_delta)
6494 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_INCLUDE;
6495 		else
6496 			req->flags |= VNIC_RSS_CFG_REQ_FLAGS_HASH_TYPE_EXCLUDE;
6497 	} else {
6498 		req->hash_type = cpu_to_le32(bp->rss_hash_cfg);
6499 	}
6500 	req->hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
6501 	req->ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
6502 	req->hash_key_tbl_addr = cpu_to_le64(vnic->rss_hash_key_dma_addr);
6503 }
6504 
6505 static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6506 				  bool set_rss)
6507 {
6508 	struct hwrm_vnic_rss_cfg_input *req;
6509 	int rc;
6510 
6511 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) ||
6512 	    vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
6513 		return 0;
6514 
6515 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6516 	if (rc)
6517 		return rc;
6518 
6519 	if (set_rss)
6520 		__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6521 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6522 	return hwrm_req_send(bp, req);
6523 }
6524 
6525 static int bnxt_hwrm_vnic_set_rss_p5(struct bnxt *bp,
6526 				     struct bnxt_vnic_info *vnic, bool set_rss)
6527 {
6528 	struct hwrm_vnic_rss_cfg_input *req;
6529 	dma_addr_t ring_tbl_map;
6530 	u32 i, nr_ctxs;
6531 	int rc;
6532 
6533 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_CFG);
6534 	if (rc)
6535 		return rc;
6536 
6537 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6538 	if (!set_rss)
6539 		return hwrm_req_send(bp, req);
6540 
6541 	__bnxt_hwrm_vnic_set_rss(bp, req, vnic);
6542 	ring_tbl_map = vnic->rss_table_dma_addr;
6543 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
6544 
6545 	hwrm_req_hold(bp, req);
6546 	for (i = 0; i < nr_ctxs; ring_tbl_map += BNXT_RSS_TABLE_SIZE_P5, i++) {
6547 		req->ring_grp_tbl_addr = cpu_to_le64(ring_tbl_map);
6548 		req->ring_table_pair_index = i;
6549 		req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[i]);
6550 		rc = hwrm_req_send(bp, req);
6551 		if (rc)
6552 			goto exit;
6553 	}
6554 
6555 exit:
6556 	hwrm_req_drop(bp, req);
6557 	return rc;
6558 }
6559 
6560 static void bnxt_hwrm_update_rss_hash_cfg(struct bnxt *bp)
6561 {
6562 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6563 	struct hwrm_vnic_rss_qcfg_output *resp;
6564 	struct hwrm_vnic_rss_qcfg_input *req;
6565 
6566 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_QCFG))
6567 		return;
6568 
6569 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6570 	/* all contexts configured to same hash_type, zero always exists */
6571 	req->rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6572 	resp = hwrm_req_hold(bp, req);
6573 	if (!hwrm_req_send(bp, req)) {
6574 		bp->rss_hash_cfg = le32_to_cpu(resp->hash_type) ?: bp->rss_hash_cfg;
6575 		bp->rss_hash_delta = 0;
6576 	}
6577 	hwrm_req_drop(bp, req);
6578 }
6579 
6580 static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6581 {
6582 	struct hwrm_vnic_plcmodes_cfg_input *req;
6583 	int rc;
6584 
6585 	rc = hwrm_req_init(bp, req, HWRM_VNIC_PLCMODES_CFG);
6586 	if (rc)
6587 		return rc;
6588 
6589 	req->flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT);
6590 	req->enables = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID);
6591 
6592 	if (BNXT_RX_PAGE_MODE(bp)) {
6593 		req->jumbo_thresh = cpu_to_le16(bp->rx_buf_use_size);
6594 	} else {
6595 		req->flags |= cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
6596 					  VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
6597 		req->enables |=
6598 			cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
6599 		req->jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
6600 		req->hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
6601 	}
6602 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6603 	return hwrm_req_send(bp, req);
6604 }
6605 
6606 static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp,
6607 					struct bnxt_vnic_info *vnic,
6608 					u16 ctx_idx)
6609 {
6610 	struct hwrm_vnic_rss_cos_lb_ctx_free_input *req;
6611 
6612 	if (hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_FREE))
6613 		return;
6614 
6615 	req->rss_cos_lb_ctx_id =
6616 		cpu_to_le16(vnic->fw_rss_cos_lb_ctx[ctx_idx]);
6617 
6618 	hwrm_req_send(bp, req);
6619 	vnic->fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
6620 }
6621 
6622 static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
6623 {
6624 	int i, j;
6625 
6626 	for (i = 0; i < bp->nr_vnics; i++) {
6627 		struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
6628 
6629 		for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
6630 			if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
6631 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, j);
6632 		}
6633 	}
6634 	bp->rsscos_nr_ctxs = 0;
6635 }
6636 
6637 static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp,
6638 				    struct bnxt_vnic_info *vnic, u16 ctx_idx)
6639 {
6640 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp;
6641 	struct hwrm_vnic_rss_cos_lb_ctx_alloc_input *req;
6642 	int rc;
6643 
6644 	rc = hwrm_req_init(bp, req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC);
6645 	if (rc)
6646 		return rc;
6647 
6648 	resp = hwrm_req_hold(bp, req);
6649 	rc = hwrm_req_send(bp, req);
6650 	if (!rc)
6651 		vnic->fw_rss_cos_lb_ctx[ctx_idx] =
6652 			le16_to_cpu(resp->rss_cos_lb_ctx_id);
6653 	hwrm_req_drop(bp, req);
6654 
6655 	return rc;
6656 }
6657 
6658 static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
6659 {
6660 	if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
6661 		return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
6662 	return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
6663 }
6664 
6665 int bnxt_hwrm_vnic_cfg(struct bnxt *bp, struct bnxt_vnic_info *vnic)
6666 {
6667 	struct bnxt_vnic_info *vnic0 = &bp->vnic_info[BNXT_VNIC_DEFAULT];
6668 	struct hwrm_vnic_cfg_input *req;
6669 	unsigned int ring = 0, grp_idx;
6670 	u16 def_vlan = 0;
6671 	int rc;
6672 
6673 	rc = hwrm_req_init(bp, req, HWRM_VNIC_CFG);
6674 	if (rc)
6675 		return rc;
6676 
6677 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6678 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[0];
6679 
6680 		req->default_rx_ring_id =
6681 			cpu_to_le16(rxr->rx_ring_struct.fw_ring_id);
6682 		req->default_cmpl_ring_id =
6683 			cpu_to_le16(bnxt_cp_ring_for_rx(bp, rxr));
6684 		req->enables =
6685 			cpu_to_le32(VNIC_CFG_REQ_ENABLES_DEFAULT_RX_RING_ID |
6686 				    VNIC_CFG_REQ_ENABLES_DEFAULT_CMPL_RING_ID);
6687 		goto vnic_mru;
6688 	}
6689 	req->enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
6690 	/* Only RSS support for now TBD: COS & LB */
6691 	if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
6692 		req->rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
6693 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6694 					   VNIC_CFG_REQ_ENABLES_MRU);
6695 	} else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
6696 		req->rss_rule = cpu_to_le16(vnic0->fw_rss_cos_lb_ctx[0]);
6697 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
6698 					   VNIC_CFG_REQ_ENABLES_MRU);
6699 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
6700 	} else {
6701 		req->rss_rule = cpu_to_le16(0xffff);
6702 	}
6703 
6704 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
6705 	    (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
6706 		req->cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
6707 		req->enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
6708 	} else {
6709 		req->cos_rule = cpu_to_le16(0xffff);
6710 	}
6711 
6712 	if (vnic->flags & BNXT_VNIC_RSS_FLAG)
6713 		ring = 0;
6714 	else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
6715 		ring = vnic->vnic_id - 1;
6716 	else if ((vnic->vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
6717 		ring = bp->rx_nr_rings - 1;
6718 
6719 	grp_idx = bp->rx_ring[ring].bnapi->index;
6720 	req->dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
6721 	req->lb_rule = cpu_to_le16(0xffff);
6722 vnic_mru:
6723 	vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
6724 	req->mru = cpu_to_le16(vnic->mru);
6725 
6726 	req->vnic_id = cpu_to_le16(vnic->fw_vnic_id);
6727 #ifdef CONFIG_BNXT_SRIOV
6728 	if (BNXT_VF(bp))
6729 		def_vlan = bp->vf.vlan;
6730 #endif
6731 	if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
6732 		req->flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
6733 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT && bnxt_ulp_registered(bp->edev))
6734 		req->flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
6735 
6736 	return hwrm_req_send(bp, req);
6737 }
6738 
6739 static void bnxt_hwrm_vnic_free_one(struct bnxt *bp,
6740 				    struct bnxt_vnic_info *vnic)
6741 {
6742 	if (vnic->fw_vnic_id != INVALID_HW_RING_ID) {
6743 		struct hwrm_vnic_free_input *req;
6744 
6745 		if (hwrm_req_init(bp, req, HWRM_VNIC_FREE))
6746 			return;
6747 
6748 		req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
6749 
6750 		hwrm_req_send(bp, req);
6751 		vnic->fw_vnic_id = INVALID_HW_RING_ID;
6752 	}
6753 }
6754 
6755 static void bnxt_hwrm_vnic_free(struct bnxt *bp)
6756 {
6757 	u16 i;
6758 
6759 	for (i = 0; i < bp->nr_vnics; i++)
6760 		bnxt_hwrm_vnic_free_one(bp, &bp->vnic_info[i]);
6761 }
6762 
6763 int bnxt_hwrm_vnic_alloc(struct bnxt *bp, struct bnxt_vnic_info *vnic,
6764 			 unsigned int start_rx_ring_idx,
6765 			 unsigned int nr_rings)
6766 {
6767 	unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
6768 	struct hwrm_vnic_alloc_output *resp;
6769 	struct hwrm_vnic_alloc_input *req;
6770 	int rc;
6771 
6772 	rc = hwrm_req_init(bp, req, HWRM_VNIC_ALLOC);
6773 	if (rc)
6774 		return rc;
6775 
6776 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6777 		goto vnic_no_ring_grps;
6778 
6779 	/* map ring groups to this vnic */
6780 	for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
6781 		grp_idx = bp->rx_ring[i].bnapi->index;
6782 		if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
6783 			netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
6784 				   j, nr_rings);
6785 			break;
6786 		}
6787 		vnic->fw_grp_ids[j] = bp->grp_info[grp_idx].fw_grp_id;
6788 	}
6789 
6790 vnic_no_ring_grps:
6791 	for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++)
6792 		vnic->fw_rss_cos_lb_ctx[i] = INVALID_HW_RING_ID;
6793 	if (vnic->vnic_id == BNXT_VNIC_DEFAULT)
6794 		req->flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
6795 
6796 	resp = hwrm_req_hold(bp, req);
6797 	rc = hwrm_req_send(bp, req);
6798 	if (!rc)
6799 		vnic->fw_vnic_id = le32_to_cpu(resp->vnic_id);
6800 	hwrm_req_drop(bp, req);
6801 	return rc;
6802 }
6803 
6804 static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
6805 {
6806 	struct hwrm_vnic_qcaps_output *resp;
6807 	struct hwrm_vnic_qcaps_input *req;
6808 	int rc;
6809 
6810 	bp->hw_ring_stats_size = sizeof(struct ctx_hw_stats);
6811 	bp->flags &= ~BNXT_FLAG_ROCE_MIRROR_CAP;
6812 	bp->rss_cap &= ~BNXT_RSS_CAP_NEW_RSS_CAP;
6813 	if (bp->hwrm_spec_code < 0x10600)
6814 		return 0;
6815 
6816 	rc = hwrm_req_init(bp, req, HWRM_VNIC_QCAPS);
6817 	if (rc)
6818 		return rc;
6819 
6820 	resp = hwrm_req_hold(bp, req);
6821 	rc = hwrm_req_send(bp, req);
6822 	if (!rc) {
6823 		u32 flags = le32_to_cpu(resp->flags);
6824 
6825 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
6826 		    (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP))
6827 			bp->rss_cap |= BNXT_RSS_CAP_NEW_RSS_CAP;
6828 		if (flags &
6829 		    VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
6830 			bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
6831 
6832 		/* Older P5 fw before EXT_HW_STATS support did not set
6833 		 * VLAN_STRIP_CAP properly.
6834 		 */
6835 		if ((flags & VNIC_QCAPS_RESP_FLAGS_VLAN_STRIP_CAP) ||
6836 		    (BNXT_CHIP_P5(bp) &&
6837 		     !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED)))
6838 			bp->fw_cap |= BNXT_FW_CAP_VLAN_RX_STRIP;
6839 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_HASH_TYPE_DELTA_CAP)
6840 			bp->rss_cap |= BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA;
6841 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_PROF_TCAM_MODE_ENABLED)
6842 			bp->rss_cap |= BNXT_RSS_CAP_RSS_TCAM;
6843 		bp->max_tpa_v2 = le16_to_cpu(resp->max_aggs_supported);
6844 		if (bp->max_tpa_v2) {
6845 			if (BNXT_CHIP_P5(bp))
6846 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P5;
6847 			else
6848 				bp->hw_ring_stats_size = BNXT_RING_STATS_SIZE_P7;
6849 		}
6850 		if (flags & VNIC_QCAPS_RESP_FLAGS_HW_TUNNEL_TPA_CAP)
6851 			bp->fw_cap |= BNXT_FW_CAP_VNIC_TUNNEL_TPA;
6852 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV4_CAP)
6853 			bp->rss_cap |= BNXT_RSS_CAP_AH_V4_RSS_CAP;
6854 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_AH_SPI_IPV6_CAP)
6855 			bp->rss_cap |= BNXT_RSS_CAP_AH_V6_RSS_CAP;
6856 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV4_CAP)
6857 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V4_RSS_CAP;
6858 		if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_IPSEC_ESP_SPI_IPV6_CAP)
6859 			bp->rss_cap |= BNXT_RSS_CAP_ESP_V6_RSS_CAP;
6860 		if (flags & VNIC_QCAPS_RESP_FLAGS_RE_FLUSH_CAP)
6861 			bp->fw_cap |= BNXT_FW_CAP_VNIC_RE_FLUSH;
6862 	}
6863 	hwrm_req_drop(bp, req);
6864 	return rc;
6865 }
6866 
6867 static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
6868 {
6869 	struct hwrm_ring_grp_alloc_output *resp;
6870 	struct hwrm_ring_grp_alloc_input *req;
6871 	int rc;
6872 	u16 i;
6873 
6874 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
6875 		return 0;
6876 
6877 	rc = hwrm_req_init(bp, req, HWRM_RING_GRP_ALLOC);
6878 	if (rc)
6879 		return rc;
6880 
6881 	resp = hwrm_req_hold(bp, req);
6882 	for (i = 0; i < bp->rx_nr_rings; i++) {
6883 		unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
6884 
6885 		req->cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
6886 		req->rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
6887 		req->ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
6888 		req->sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
6889 
6890 		rc = hwrm_req_send(bp, req);
6891 
6892 		if (rc)
6893 			break;
6894 
6895 		bp->grp_info[grp_idx].fw_grp_id =
6896 			le32_to_cpu(resp->ring_group_id);
6897 	}
6898 	hwrm_req_drop(bp, req);
6899 	return rc;
6900 }
6901 
6902 static void bnxt_hwrm_ring_grp_free(struct bnxt *bp)
6903 {
6904 	struct hwrm_ring_grp_free_input *req;
6905 	u16 i;
6906 
6907 	if (!bp->grp_info || (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
6908 		return;
6909 
6910 	if (hwrm_req_init(bp, req, HWRM_RING_GRP_FREE))
6911 		return;
6912 
6913 	hwrm_req_hold(bp, req);
6914 	for (i = 0; i < bp->cp_nr_rings; i++) {
6915 		if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
6916 			continue;
6917 		req->ring_group_id =
6918 			cpu_to_le32(bp->grp_info[i].fw_grp_id);
6919 
6920 		hwrm_req_send(bp, req);
6921 		bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
6922 	}
6923 	hwrm_req_drop(bp, req);
6924 }
6925 
6926 static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
6927 				    struct bnxt_ring_struct *ring,
6928 				    u32 ring_type, u32 map_index)
6929 {
6930 	struct hwrm_ring_alloc_output *resp;
6931 	struct hwrm_ring_alloc_input *req;
6932 	struct bnxt_ring_mem_info *rmem = &ring->ring_mem;
6933 	struct bnxt_ring_grp_info *grp_info;
6934 	int rc, err = 0;
6935 	u16 ring_id;
6936 
6937 	rc = hwrm_req_init(bp, req, HWRM_RING_ALLOC);
6938 	if (rc)
6939 		goto exit;
6940 
6941 	req->enables = 0;
6942 	if (rmem->nr_pages > 1) {
6943 		req->page_tbl_addr = cpu_to_le64(rmem->pg_tbl_map);
6944 		/* Page size is in log2 units */
6945 		req->page_size = BNXT_PAGE_SHIFT;
6946 		req->page_tbl_depth = 1;
6947 	} else {
6948 		req->page_tbl_addr =  cpu_to_le64(rmem->dma_arr[0]);
6949 	}
6950 	req->fbo = 0;
6951 	/* Association of ring index with doorbell index and MSIX number */
6952 	req->logical_id = cpu_to_le16(map_index);
6953 
6954 	switch (ring_type) {
6955 	case HWRM_RING_ALLOC_TX: {
6956 		struct bnxt_tx_ring_info *txr;
6957 		u16 flags = 0;
6958 
6959 		txr = container_of(ring, struct bnxt_tx_ring_info,
6960 				   tx_ring_struct);
6961 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
6962 		/* Association of transmit ring with completion ring */
6963 		grp_info = &bp->grp_info[ring->grp_idx];
6964 		req->cmpl_ring_id = cpu_to_le16(bnxt_cp_ring_for_tx(bp, txr));
6965 		req->length = cpu_to_le32(bp->tx_ring_mask + 1);
6966 		req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6967 		req->queue_id = cpu_to_le16(ring->queue_id);
6968 		if (bp->flags & BNXT_FLAG_TX_COAL_CMPL)
6969 			req->cmpl_coal_cnt =
6970 				RING_ALLOC_REQ_CMPL_COAL_CNT_COAL_64;
6971 		if ((bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP) && bp->ptp_cfg)
6972 			flags |= RING_ALLOC_REQ_FLAGS_TX_PKT_TS_CMPL_ENABLE;
6973 		req->flags = cpu_to_le16(flags);
6974 		break;
6975 	}
6976 	case HWRM_RING_ALLOC_RX:
6977 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
6978 		req->length = cpu_to_le32(bp->rx_ring_mask + 1);
6979 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6980 			u16 flags = 0;
6981 
6982 			/* Association of rx ring with stats context */
6983 			grp_info = &bp->grp_info[ring->grp_idx];
6984 			req->rx_buf_size = cpu_to_le16(bp->rx_buf_use_size);
6985 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
6986 			req->enables |= cpu_to_le32(
6987 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
6988 			if (NET_IP_ALIGN == 2)
6989 				flags = RING_ALLOC_REQ_FLAGS_RX_SOP_PAD;
6990 			req->flags = cpu_to_le16(flags);
6991 		}
6992 		break;
6993 	case HWRM_RING_ALLOC_AGG:
6994 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
6995 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX_AGG;
6996 			/* Association of agg ring with rx ring */
6997 			grp_info = &bp->grp_info[ring->grp_idx];
6998 			req->rx_ring_id = cpu_to_le16(grp_info->rx_fw_ring_id);
6999 			req->rx_buf_size = cpu_to_le16(BNXT_RX_PAGE_SIZE);
7000 			req->stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
7001 			req->enables |= cpu_to_le32(
7002 				RING_ALLOC_REQ_ENABLES_RX_RING_ID_VALID |
7003 				RING_ALLOC_REQ_ENABLES_RX_BUF_SIZE_VALID);
7004 		} else {
7005 			req->ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
7006 		}
7007 		req->length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
7008 		break;
7009 	case HWRM_RING_ALLOC_CMPL:
7010 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
7011 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7012 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7013 			/* Association of cp ring with nq */
7014 			grp_info = &bp->grp_info[map_index];
7015 			req->nq_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
7016 			req->cq_handle = cpu_to_le64(ring->handle);
7017 			req->enables |= cpu_to_le32(
7018 				RING_ALLOC_REQ_ENABLES_NQ_RING_ID_VALID);
7019 		} else {
7020 			req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7021 		}
7022 		break;
7023 	case HWRM_RING_ALLOC_NQ:
7024 		req->ring_type = RING_ALLOC_REQ_RING_TYPE_NQ;
7025 		req->length = cpu_to_le32(bp->cp_ring_mask + 1);
7026 		req->int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
7027 		break;
7028 	default:
7029 		netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
7030 			   ring_type);
7031 		return -1;
7032 	}
7033 
7034 	resp = hwrm_req_hold(bp, req);
7035 	rc = hwrm_req_send(bp, req);
7036 	err = le16_to_cpu(resp->error_code);
7037 	ring_id = le16_to_cpu(resp->ring_id);
7038 	hwrm_req_drop(bp, req);
7039 
7040 exit:
7041 	if (rc || err) {
7042 		netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
7043 			   ring_type, rc, err);
7044 		return -EIO;
7045 	}
7046 	ring->fw_ring_id = ring_id;
7047 	return rc;
7048 }
7049 
7050 static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
7051 {
7052 	int rc;
7053 
7054 	if (BNXT_PF(bp)) {
7055 		struct hwrm_func_cfg_input *req;
7056 
7057 		rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
7058 		if (rc)
7059 			return rc;
7060 
7061 		req->fid = cpu_to_le16(0xffff);
7062 		req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7063 		req->async_event_cr = cpu_to_le16(idx);
7064 		return hwrm_req_send(bp, req);
7065 	} else {
7066 		struct hwrm_func_vf_cfg_input *req;
7067 
7068 		rc = hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG);
7069 		if (rc)
7070 			return rc;
7071 
7072 		req->enables =
7073 			cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
7074 		req->async_event_cr = cpu_to_le16(idx);
7075 		return hwrm_req_send(bp, req);
7076 	}
7077 }
7078 
7079 static void bnxt_set_db_mask(struct bnxt *bp, struct bnxt_db_info *db,
7080 			     u32 ring_type)
7081 {
7082 	switch (ring_type) {
7083 	case HWRM_RING_ALLOC_TX:
7084 		db->db_ring_mask = bp->tx_ring_mask;
7085 		break;
7086 	case HWRM_RING_ALLOC_RX:
7087 		db->db_ring_mask = bp->rx_ring_mask;
7088 		break;
7089 	case HWRM_RING_ALLOC_AGG:
7090 		db->db_ring_mask = bp->rx_agg_ring_mask;
7091 		break;
7092 	case HWRM_RING_ALLOC_CMPL:
7093 	case HWRM_RING_ALLOC_NQ:
7094 		db->db_ring_mask = bp->cp_ring_mask;
7095 		break;
7096 	}
7097 	if (bp->flags & BNXT_FLAG_CHIP_P7) {
7098 		db->db_epoch_mask = db->db_ring_mask + 1;
7099 		db->db_epoch_shift = DBR_EPOCH_SFT - ilog2(db->db_epoch_mask);
7100 	}
7101 }
7102 
7103 static void bnxt_set_db(struct bnxt *bp, struct bnxt_db_info *db, u32 ring_type,
7104 			u32 map_idx, u32 xid)
7105 {
7106 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7107 		switch (ring_type) {
7108 		case HWRM_RING_ALLOC_TX:
7109 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SQ;
7110 			break;
7111 		case HWRM_RING_ALLOC_RX:
7112 		case HWRM_RING_ALLOC_AGG:
7113 			db->db_key64 = DBR_PATH_L2 | DBR_TYPE_SRQ;
7114 			break;
7115 		case HWRM_RING_ALLOC_CMPL:
7116 			db->db_key64 = DBR_PATH_L2;
7117 			break;
7118 		case HWRM_RING_ALLOC_NQ:
7119 			db->db_key64 = DBR_PATH_L2;
7120 			break;
7121 		}
7122 		db->db_key64 |= (u64)xid << DBR_XID_SFT;
7123 
7124 		if (bp->flags & BNXT_FLAG_CHIP_P7)
7125 			db->db_key64 |= DBR_VALID;
7126 
7127 		db->doorbell = bp->bar1 + bp->db_offset;
7128 	} else {
7129 		db->doorbell = bp->bar1 + map_idx * 0x80;
7130 		switch (ring_type) {
7131 		case HWRM_RING_ALLOC_TX:
7132 			db->db_key32 = DB_KEY_TX;
7133 			break;
7134 		case HWRM_RING_ALLOC_RX:
7135 		case HWRM_RING_ALLOC_AGG:
7136 			db->db_key32 = DB_KEY_RX;
7137 			break;
7138 		case HWRM_RING_ALLOC_CMPL:
7139 			db->db_key32 = DB_KEY_CP;
7140 			break;
7141 		}
7142 	}
7143 	bnxt_set_db_mask(bp, db, ring_type);
7144 }
7145 
7146 static int bnxt_hwrm_rx_ring_alloc(struct bnxt *bp,
7147 				   struct bnxt_rx_ring_info *rxr)
7148 {
7149 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7150 	struct bnxt_napi *bnapi = rxr->bnapi;
7151 	u32 type = HWRM_RING_ALLOC_RX;
7152 	u32 map_idx = bnapi->index;
7153 	int rc;
7154 
7155 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7156 	if (rc)
7157 		return rc;
7158 
7159 	bnxt_set_db(bp, &rxr->rx_db, type, map_idx, ring->fw_ring_id);
7160 	bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
7161 
7162 	return 0;
7163 }
7164 
7165 static int bnxt_hwrm_rx_agg_ring_alloc(struct bnxt *bp,
7166 				       struct bnxt_rx_ring_info *rxr)
7167 {
7168 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7169 	u32 type = HWRM_RING_ALLOC_AGG;
7170 	u32 grp_idx = ring->grp_idx;
7171 	u32 map_idx;
7172 	int rc;
7173 
7174 	map_idx = grp_idx + bp->rx_nr_rings;
7175 	rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7176 	if (rc)
7177 		return rc;
7178 
7179 	bnxt_set_db(bp, &rxr->rx_agg_db, type, map_idx,
7180 		    ring->fw_ring_id);
7181 	bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
7182 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7183 	bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
7184 
7185 	return 0;
7186 }
7187 
7188 static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
7189 {
7190 	bool agg_rings = !!(bp->flags & BNXT_FLAG_AGG_RINGS);
7191 	int i, rc = 0;
7192 	u32 type;
7193 
7194 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7195 		type = HWRM_RING_ALLOC_NQ;
7196 	else
7197 		type = HWRM_RING_ALLOC_CMPL;
7198 	for (i = 0; i < bp->cp_nr_rings; i++) {
7199 		struct bnxt_napi *bnapi = bp->bnapi[i];
7200 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7201 		struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
7202 		u32 map_idx = ring->map_idx;
7203 		unsigned int vector;
7204 
7205 		vector = bp->irq_tbl[map_idx].vector;
7206 		disable_irq_nosync(vector);
7207 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7208 		if (rc) {
7209 			enable_irq(vector);
7210 			goto err_out;
7211 		}
7212 		bnxt_set_db(bp, &cpr->cp_db, type, map_idx, ring->fw_ring_id);
7213 		bnxt_db_nq(bp, &cpr->cp_db, cpr->cp_raw_cons);
7214 		enable_irq(vector);
7215 		bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
7216 
7217 		if (!i) {
7218 			rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
7219 			if (rc)
7220 				netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
7221 		}
7222 	}
7223 
7224 	type = HWRM_RING_ALLOC_TX;
7225 	for (i = 0; i < bp->tx_nr_rings; i++) {
7226 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7227 		struct bnxt_ring_struct *ring;
7228 		u32 map_idx;
7229 
7230 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7231 			struct bnxt_cp_ring_info *cpr2 = txr->tx_cpr;
7232 			struct bnxt_napi *bnapi = txr->bnapi;
7233 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7234 
7235 			ring = &cpr2->cp_ring_struct;
7236 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7237 			map_idx = bnapi->index;
7238 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7239 			if (rc)
7240 				goto err_out;
7241 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7242 				    ring->fw_ring_id);
7243 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7244 		}
7245 		ring = &txr->tx_ring_struct;
7246 		map_idx = i;
7247 		rc = hwrm_ring_alloc_send_msg(bp, ring, type, map_idx);
7248 		if (rc)
7249 			goto err_out;
7250 		bnxt_set_db(bp, &txr->tx_db, type, map_idx, ring->fw_ring_id);
7251 	}
7252 
7253 	for (i = 0; i < bp->rx_nr_rings; i++) {
7254 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7255 
7256 		rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
7257 		if (rc)
7258 			goto err_out;
7259 		/* If we have agg rings, post agg buffers first. */
7260 		if (!agg_rings)
7261 			bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
7262 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7263 			struct bnxt_cp_ring_info *cpr2 = rxr->rx_cpr;
7264 			struct bnxt_napi *bnapi = rxr->bnapi;
7265 			u32 type2 = HWRM_RING_ALLOC_CMPL;
7266 			struct bnxt_ring_struct *ring;
7267 			u32 map_idx = bnapi->index;
7268 
7269 			ring = &cpr2->cp_ring_struct;
7270 			ring->handle = BNXT_SET_NQ_HDL(cpr2);
7271 			rc = hwrm_ring_alloc_send_msg(bp, ring, type2, map_idx);
7272 			if (rc)
7273 				goto err_out;
7274 			bnxt_set_db(bp, &cpr2->cp_db, type2, map_idx,
7275 				    ring->fw_ring_id);
7276 			bnxt_db_cq(bp, &cpr2->cp_db, cpr2->cp_raw_cons);
7277 		}
7278 	}
7279 
7280 	if (agg_rings) {
7281 		for (i = 0; i < bp->rx_nr_rings; i++) {
7282 			rc = bnxt_hwrm_rx_agg_ring_alloc(bp, &bp->rx_ring[i]);
7283 			if (rc)
7284 				goto err_out;
7285 		}
7286 	}
7287 err_out:
7288 	return rc;
7289 }
7290 
7291 static void bnxt_cancel_dim(struct bnxt *bp)
7292 {
7293 	int i;
7294 
7295 	/* DIM work is initialized in bnxt_enable_napi().  Proceed only
7296 	 * if NAPI is enabled.
7297 	 */
7298 	if (!bp->bnapi || test_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
7299 		return;
7300 
7301 	/* Make sure NAPI sees that the VNIC is disabled */
7302 	synchronize_net();
7303 	for (i = 0; i < bp->rx_nr_rings; i++) {
7304 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
7305 		struct bnxt_napi *bnapi = rxr->bnapi;
7306 
7307 		cancel_work_sync(&bnapi->cp_ring.dim.work);
7308 	}
7309 }
7310 
7311 static int hwrm_ring_free_send_msg(struct bnxt *bp,
7312 				   struct bnxt_ring_struct *ring,
7313 				   u32 ring_type, int cmpl_ring_id)
7314 {
7315 	struct hwrm_ring_free_output *resp;
7316 	struct hwrm_ring_free_input *req;
7317 	u16 error_code = 0;
7318 	int rc;
7319 
7320 	if (BNXT_NO_FW_ACCESS(bp))
7321 		return 0;
7322 
7323 	rc = hwrm_req_init(bp, req, HWRM_RING_FREE);
7324 	if (rc)
7325 		goto exit;
7326 
7327 	req->cmpl_ring = cpu_to_le16(cmpl_ring_id);
7328 	req->ring_type = ring_type;
7329 	req->ring_id = cpu_to_le16(ring->fw_ring_id);
7330 
7331 	resp = hwrm_req_hold(bp, req);
7332 	rc = hwrm_req_send(bp, req);
7333 	error_code = le16_to_cpu(resp->error_code);
7334 	hwrm_req_drop(bp, req);
7335 exit:
7336 	if (rc || error_code) {
7337 		netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
7338 			   ring_type, rc, error_code);
7339 		return -EIO;
7340 	}
7341 	return 0;
7342 }
7343 
7344 static void bnxt_hwrm_rx_ring_free(struct bnxt *bp,
7345 				   struct bnxt_rx_ring_info *rxr,
7346 				   bool close_path)
7347 {
7348 	struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
7349 	u32 grp_idx = rxr->bnapi->index;
7350 	u32 cmpl_ring_id;
7351 
7352 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7353 		return;
7354 
7355 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7356 	hwrm_ring_free_send_msg(bp, ring,
7357 				RING_FREE_REQ_RING_TYPE_RX,
7358 				close_path ? cmpl_ring_id :
7359 				INVALID_HW_RING_ID);
7360 	ring->fw_ring_id = INVALID_HW_RING_ID;
7361 	bp->grp_info[grp_idx].rx_fw_ring_id = INVALID_HW_RING_ID;
7362 }
7363 
7364 static void bnxt_hwrm_rx_agg_ring_free(struct bnxt *bp,
7365 				       struct bnxt_rx_ring_info *rxr,
7366 				       bool close_path)
7367 {
7368 	struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
7369 	u32 grp_idx = rxr->bnapi->index;
7370 	u32 type, cmpl_ring_id;
7371 
7372 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7373 		type = RING_FREE_REQ_RING_TYPE_RX_AGG;
7374 	else
7375 		type = RING_FREE_REQ_RING_TYPE_RX;
7376 
7377 	if (ring->fw_ring_id == INVALID_HW_RING_ID)
7378 		return;
7379 
7380 	cmpl_ring_id = bnxt_cp_ring_for_rx(bp, rxr);
7381 	hwrm_ring_free_send_msg(bp, ring, type,
7382 				close_path ? cmpl_ring_id :
7383 				INVALID_HW_RING_ID);
7384 	ring->fw_ring_id = INVALID_HW_RING_ID;
7385 	bp->grp_info[grp_idx].agg_fw_ring_id = INVALID_HW_RING_ID;
7386 }
7387 
7388 static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
7389 {
7390 	u32 type;
7391 	int i;
7392 
7393 	if (!bp->bnapi)
7394 		return;
7395 
7396 	for (i = 0; i < bp->tx_nr_rings; i++) {
7397 		struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
7398 		struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
7399 
7400 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7401 			u32 cmpl_ring_id = bnxt_cp_ring_for_tx(bp, txr);
7402 
7403 			hwrm_ring_free_send_msg(bp, ring,
7404 						RING_FREE_REQ_RING_TYPE_TX,
7405 						close_path ? cmpl_ring_id :
7406 						INVALID_HW_RING_ID);
7407 			ring->fw_ring_id = INVALID_HW_RING_ID;
7408 		}
7409 	}
7410 
7411 	bnxt_cancel_dim(bp);
7412 	for (i = 0; i < bp->rx_nr_rings; i++) {
7413 		bnxt_hwrm_rx_ring_free(bp, &bp->rx_ring[i], close_path);
7414 		bnxt_hwrm_rx_agg_ring_free(bp, &bp->rx_ring[i], close_path);
7415 	}
7416 
7417 	/* The completion rings are about to be freed.  After that the
7418 	 * IRQ doorbell will not work anymore.  So we need to disable
7419 	 * IRQ here.
7420 	 */
7421 	bnxt_disable_int_sync(bp);
7422 
7423 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7424 		type = RING_FREE_REQ_RING_TYPE_NQ;
7425 	else
7426 		type = RING_FREE_REQ_RING_TYPE_L2_CMPL;
7427 	for (i = 0; i < bp->cp_nr_rings; i++) {
7428 		struct bnxt_napi *bnapi = bp->bnapi[i];
7429 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
7430 		struct bnxt_ring_struct *ring;
7431 		int j;
7432 
7433 		for (j = 0; j < cpr->cp_ring_count && cpr->cp_ring_arr; j++) {
7434 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
7435 
7436 			ring = &cpr2->cp_ring_struct;
7437 			if (ring->fw_ring_id == INVALID_HW_RING_ID)
7438 				continue;
7439 			hwrm_ring_free_send_msg(bp, ring,
7440 						RING_FREE_REQ_RING_TYPE_L2_CMPL,
7441 						INVALID_HW_RING_ID);
7442 			ring->fw_ring_id = INVALID_HW_RING_ID;
7443 		}
7444 		ring = &cpr->cp_ring_struct;
7445 		if (ring->fw_ring_id != INVALID_HW_RING_ID) {
7446 			hwrm_ring_free_send_msg(bp, ring, type,
7447 						INVALID_HW_RING_ID);
7448 			ring->fw_ring_id = INVALID_HW_RING_ID;
7449 			bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
7450 		}
7451 	}
7452 }
7453 
7454 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7455 			     bool shared);
7456 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
7457 			   bool shared);
7458 
7459 static int bnxt_hwrm_get_rings(struct bnxt *bp)
7460 {
7461 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7462 	struct hwrm_func_qcfg_output *resp;
7463 	struct hwrm_func_qcfg_input *req;
7464 	int rc;
7465 
7466 	if (bp->hwrm_spec_code < 0x10601)
7467 		return 0;
7468 
7469 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7470 	if (rc)
7471 		return rc;
7472 
7473 	req->fid = cpu_to_le16(0xffff);
7474 	resp = hwrm_req_hold(bp, req);
7475 	rc = hwrm_req_send(bp, req);
7476 	if (rc) {
7477 		hwrm_req_drop(bp, req);
7478 		return rc;
7479 	}
7480 
7481 	hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7482 	if (BNXT_NEW_RM(bp)) {
7483 		u16 cp, stats;
7484 
7485 		hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
7486 		hw_resc->resv_hw_ring_grps =
7487 			le32_to_cpu(resp->alloc_hw_ring_grps);
7488 		hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
7489 		hw_resc->resv_rsscos_ctxs = le16_to_cpu(resp->alloc_rsscos_ctx);
7490 		cp = le16_to_cpu(resp->alloc_cmpl_rings);
7491 		stats = le16_to_cpu(resp->alloc_stat_ctx);
7492 		hw_resc->resv_irqs = cp;
7493 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7494 			int rx = hw_resc->resv_rx_rings;
7495 			int tx = hw_resc->resv_tx_rings;
7496 
7497 			if (bp->flags & BNXT_FLAG_AGG_RINGS)
7498 				rx >>= 1;
7499 			if (cp < (rx + tx)) {
7500 				rc = __bnxt_trim_rings(bp, &rx, &tx, cp, false);
7501 				if (rc)
7502 					goto get_rings_exit;
7503 				if (bp->flags & BNXT_FLAG_AGG_RINGS)
7504 					rx <<= 1;
7505 				hw_resc->resv_rx_rings = rx;
7506 				hw_resc->resv_tx_rings = tx;
7507 			}
7508 			hw_resc->resv_irqs = le16_to_cpu(resp->alloc_msix);
7509 			hw_resc->resv_hw_ring_grps = rx;
7510 		}
7511 		hw_resc->resv_cp_rings = cp;
7512 		hw_resc->resv_stat_ctxs = stats;
7513 	}
7514 get_rings_exit:
7515 	hwrm_req_drop(bp, req);
7516 	return rc;
7517 }
7518 
7519 int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
7520 {
7521 	struct hwrm_func_qcfg_output *resp;
7522 	struct hwrm_func_qcfg_input *req;
7523 	int rc;
7524 
7525 	if (bp->hwrm_spec_code < 0x10601)
7526 		return 0;
7527 
7528 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
7529 	if (rc)
7530 		return rc;
7531 
7532 	req->fid = cpu_to_le16(fid);
7533 	resp = hwrm_req_hold(bp, req);
7534 	rc = hwrm_req_send(bp, req);
7535 	if (!rc)
7536 		*tx_rings = le16_to_cpu(resp->alloc_tx_rings);
7537 
7538 	hwrm_req_drop(bp, req);
7539 	return rc;
7540 }
7541 
7542 static bool bnxt_rfs_supported(struct bnxt *bp);
7543 
7544 static struct hwrm_func_cfg_input *
7545 __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7546 {
7547 	struct hwrm_func_cfg_input *req;
7548 	u32 enables = 0;
7549 
7550 	if (bnxt_hwrm_func_cfg_short_req_init(bp, &req))
7551 		return NULL;
7552 
7553 	req->fid = cpu_to_le16(0xffff);
7554 	enables |= hwr->tx ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7555 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7556 	if (BNXT_NEW_RM(bp)) {
7557 		enables |= hwr->rx ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
7558 		enables |= hwr->stat ? FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7559 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7560 			enables |= hwr->cp ? FUNC_CFG_REQ_ENABLES_NUM_MSIX : 0;
7561 			enables |= hwr->cp_p5 ?
7562 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7563 		} else {
7564 			enables |= hwr->cp ?
7565 				   FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7566 			enables |= hwr->grp ?
7567 				   FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7568 		}
7569 		enables |= hwr->vnic ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
7570 		enables |= hwr->rss_ctx ? FUNC_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS :
7571 					  0;
7572 		req->num_rx_rings = cpu_to_le16(hwr->rx);
7573 		req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7574 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7575 			req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7576 			req->num_msix = cpu_to_le16(hwr->cp);
7577 		} else {
7578 			req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7579 			req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7580 		}
7581 		req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7582 		req->num_vnics = cpu_to_le16(hwr->vnic);
7583 	}
7584 	req->enables = cpu_to_le32(enables);
7585 	return req;
7586 }
7587 
7588 static struct hwrm_func_vf_cfg_input *
7589 __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7590 {
7591 	struct hwrm_func_vf_cfg_input *req;
7592 	u32 enables = 0;
7593 
7594 	if (hwrm_req_init(bp, req, HWRM_FUNC_VF_CFG))
7595 		return NULL;
7596 
7597 	enables |= hwr->tx ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
7598 	enables |= hwr->rx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS |
7599 			     FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7600 	enables |= hwr->stat ? FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
7601 	enables |= hwr->rss_ctx ? FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS : 0;
7602 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7603 		enables |= hwr->cp_p5 ?
7604 			   FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7605 	} else {
7606 		enables |= hwr->cp ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS : 0;
7607 		enables |= hwr->grp ?
7608 			   FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
7609 	}
7610 	enables |= hwr->vnic ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
7611 	enables |= FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS;
7612 
7613 	req->num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
7614 	req->num_tx_rings = cpu_to_le16(hwr->tx);
7615 	req->num_rx_rings = cpu_to_le16(hwr->rx);
7616 	req->num_rsscos_ctxs = cpu_to_le16(hwr->rss_ctx);
7617 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7618 		req->num_cmpl_rings = cpu_to_le16(hwr->cp_p5);
7619 	} else {
7620 		req->num_cmpl_rings = cpu_to_le16(hwr->cp);
7621 		req->num_hw_ring_grps = cpu_to_le16(hwr->grp);
7622 	}
7623 	req->num_stat_ctxs = cpu_to_le16(hwr->stat);
7624 	req->num_vnics = cpu_to_le16(hwr->vnic);
7625 
7626 	req->enables = cpu_to_le32(enables);
7627 	return req;
7628 }
7629 
7630 static int
7631 bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7632 {
7633 	struct hwrm_func_cfg_input *req;
7634 	int rc;
7635 
7636 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7637 	if (!req)
7638 		return -ENOMEM;
7639 
7640 	if (!req->enables) {
7641 		hwrm_req_drop(bp, req);
7642 		return 0;
7643 	}
7644 
7645 	rc = hwrm_req_send(bp, req);
7646 	if (rc)
7647 		return rc;
7648 
7649 	if (bp->hwrm_spec_code < 0x10601)
7650 		bp->hw_resc.resv_tx_rings = hwr->tx;
7651 
7652 	return bnxt_hwrm_get_rings(bp);
7653 }
7654 
7655 static int
7656 bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7657 {
7658 	struct hwrm_func_vf_cfg_input *req;
7659 	int rc;
7660 
7661 	if (!BNXT_NEW_RM(bp)) {
7662 		bp->hw_resc.resv_tx_rings = hwr->tx;
7663 		return 0;
7664 	}
7665 
7666 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7667 	if (!req)
7668 		return -ENOMEM;
7669 
7670 	rc = hwrm_req_send(bp, req);
7671 	if (rc)
7672 		return rc;
7673 
7674 	return bnxt_hwrm_get_rings(bp);
7675 }
7676 
7677 static int bnxt_hwrm_reserve_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7678 {
7679 	if (BNXT_PF(bp))
7680 		return bnxt_hwrm_reserve_pf_rings(bp, hwr);
7681 	else
7682 		return bnxt_hwrm_reserve_vf_rings(bp, hwr);
7683 }
7684 
7685 int bnxt_nq_rings_in_use(struct bnxt *bp)
7686 {
7687 	return bp->cp_nr_rings + bnxt_get_ulp_msix_num(bp);
7688 }
7689 
7690 static int bnxt_cp_rings_in_use(struct bnxt *bp)
7691 {
7692 	int cp;
7693 
7694 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7695 		return bnxt_nq_rings_in_use(bp);
7696 
7697 	cp = bp->tx_nr_rings + bp->rx_nr_rings;
7698 	return cp;
7699 }
7700 
7701 static int bnxt_get_func_stat_ctxs(struct bnxt *bp)
7702 {
7703 	return bp->cp_nr_rings + bnxt_get_ulp_stat_ctxs(bp);
7704 }
7705 
7706 static int bnxt_get_total_rss_ctxs(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7707 {
7708 	if (!hwr->grp)
7709 		return 0;
7710 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
7711 		int rss_ctx = bnxt_get_nr_rss_ctxs(bp, hwr->grp);
7712 
7713 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7714 			rss_ctx *= hwr->vnic;
7715 		return rss_ctx;
7716 	}
7717 	if (BNXT_VF(bp))
7718 		return BNXT_VF_MAX_RSS_CTX;
7719 	if (!(bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP) && bnxt_rfs_supported(bp))
7720 		return hwr->grp + 1;
7721 	return 1;
7722 }
7723 
7724 /* Check if a default RSS map needs to be setup.  This function is only
7725  * used on older firmware that does not require reserving RX rings.
7726  */
7727 static void bnxt_check_rss_tbl_no_rmgr(struct bnxt *bp)
7728 {
7729 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7730 
7731 	/* The RSS map is valid for RX rings set to resv_rx_rings */
7732 	if (hw_resc->resv_rx_rings != bp->rx_nr_rings) {
7733 		hw_resc->resv_rx_rings = bp->rx_nr_rings;
7734 		if (!netif_is_rxfh_configured(bp->dev))
7735 			bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7736 	}
7737 }
7738 
7739 static int bnxt_get_total_vnics(struct bnxt *bp, int rx_rings)
7740 {
7741 	if (bp->flags & BNXT_FLAG_RFS) {
7742 		if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
7743 			return 2 + bp->num_rss_ctx;
7744 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7745 			return rx_rings + 1;
7746 	}
7747 	return 1;
7748 }
7749 
7750 static bool bnxt_need_reserve_rings(struct bnxt *bp)
7751 {
7752 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7753 	int cp = bnxt_cp_rings_in_use(bp);
7754 	int nq = bnxt_nq_rings_in_use(bp);
7755 	int rx = bp->rx_nr_rings, stat;
7756 	int vnic, grp = rx;
7757 
7758 	/* Old firmware does not need RX ring reservations but we still
7759 	 * need to setup a default RSS map when needed.  With new firmware
7760 	 * we go through RX ring reservations first and then set up the
7761 	 * RSS map for the successfully reserved RX rings when needed.
7762 	 */
7763 	if (!BNXT_NEW_RM(bp))
7764 		bnxt_check_rss_tbl_no_rmgr(bp);
7765 
7766 	if (hw_resc->resv_tx_rings != bp->tx_nr_rings &&
7767 	    bp->hwrm_spec_code >= 0x10601)
7768 		return true;
7769 
7770 	if (!BNXT_NEW_RM(bp))
7771 		return false;
7772 
7773 	vnic = bnxt_get_total_vnics(bp, rx);
7774 
7775 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7776 		rx <<= 1;
7777 	stat = bnxt_get_func_stat_ctxs(bp);
7778 	if (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
7779 	    hw_resc->resv_vnics != vnic || hw_resc->resv_stat_ctxs != stat ||
7780 	    (hw_resc->resv_hw_ring_grps != grp &&
7781 	     !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)))
7782 		return true;
7783 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) && BNXT_PF(bp) &&
7784 	    hw_resc->resv_irqs != nq)
7785 		return true;
7786 	return false;
7787 }
7788 
7789 static void bnxt_copy_reserved_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7790 {
7791 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
7792 
7793 	hwr->tx = hw_resc->resv_tx_rings;
7794 	if (BNXT_NEW_RM(bp)) {
7795 		hwr->rx = hw_resc->resv_rx_rings;
7796 		hwr->cp = hw_resc->resv_irqs;
7797 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7798 			hwr->cp_p5 = hw_resc->resv_cp_rings;
7799 		hwr->grp = hw_resc->resv_hw_ring_grps;
7800 		hwr->vnic = hw_resc->resv_vnics;
7801 		hwr->stat = hw_resc->resv_stat_ctxs;
7802 		hwr->rss_ctx = hw_resc->resv_rsscos_ctxs;
7803 	}
7804 }
7805 
7806 static bool bnxt_rings_ok(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7807 {
7808 	return hwr->tx && hwr->rx && hwr->cp && hwr->grp && hwr->vnic &&
7809 	       hwr->stat && (hwr->cp_p5 || !(bp->flags & BNXT_FLAG_CHIP_P5_PLUS));
7810 }
7811 
7812 static int bnxt_get_avail_msix(struct bnxt *bp, int num);
7813 
7814 static int __bnxt_reserve_rings(struct bnxt *bp)
7815 {
7816 	struct bnxt_hw_rings hwr = {0};
7817 	int rx_rings, old_rx_rings, rc;
7818 	int cp = bp->cp_nr_rings;
7819 	int ulp_msix = 0;
7820 	bool sh = false;
7821 	int tx_cp;
7822 
7823 	if (!bnxt_need_reserve_rings(bp))
7824 		return 0;
7825 
7826 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
7827 		ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
7828 		if (!ulp_msix)
7829 			bnxt_set_ulp_stat_ctxs(bp, 0);
7830 
7831 		if (ulp_msix > bp->ulp_num_msix_want)
7832 			ulp_msix = bp->ulp_num_msix_want;
7833 		hwr.cp = cp + ulp_msix;
7834 	} else {
7835 		hwr.cp = bnxt_nq_rings_in_use(bp);
7836 	}
7837 
7838 	hwr.tx = bp->tx_nr_rings;
7839 	hwr.rx = bp->rx_nr_rings;
7840 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
7841 		sh = true;
7842 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7843 		hwr.cp_p5 = hwr.rx + hwr.tx;
7844 
7845 	hwr.vnic = bnxt_get_total_vnics(bp, hwr.rx);
7846 
7847 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7848 		hwr.rx <<= 1;
7849 	hwr.grp = bp->rx_nr_rings;
7850 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
7851 	hwr.stat = bnxt_get_func_stat_ctxs(bp);
7852 	old_rx_rings = bp->hw_resc.resv_rx_rings;
7853 
7854 	rc = bnxt_hwrm_reserve_rings(bp, &hwr);
7855 	if (rc)
7856 		return rc;
7857 
7858 	bnxt_copy_reserved_rings(bp, &hwr);
7859 
7860 	rx_rings = hwr.rx;
7861 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
7862 		if (hwr.rx >= 2) {
7863 			rx_rings = hwr.rx >> 1;
7864 		} else {
7865 			if (netif_running(bp->dev))
7866 				return -ENOMEM;
7867 
7868 			bp->flags &= ~BNXT_FLAG_AGG_RINGS;
7869 			bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
7870 			bp->dev->hw_features &= ~NETIF_F_LRO;
7871 			bp->dev->features &= ~NETIF_F_LRO;
7872 			bnxt_set_ring_params(bp);
7873 		}
7874 	}
7875 	rx_rings = min_t(int, rx_rings, hwr.grp);
7876 	hwr.cp = min_t(int, hwr.cp, bp->cp_nr_rings);
7877 	if (hwr.stat > bnxt_get_ulp_stat_ctxs(bp))
7878 		hwr.stat -= bnxt_get_ulp_stat_ctxs(bp);
7879 	hwr.cp = min_t(int, hwr.cp, hwr.stat);
7880 	rc = bnxt_trim_rings(bp, &rx_rings, &hwr.tx, hwr.cp, sh);
7881 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
7882 		hwr.rx = rx_rings << 1;
7883 	tx_cp = bnxt_num_tx_to_cp(bp, hwr.tx);
7884 	hwr.cp = sh ? max_t(int, tx_cp, rx_rings) : tx_cp + rx_rings;
7885 	bp->tx_nr_rings = hwr.tx;
7886 
7887 	/* If we cannot reserve all the RX rings, reset the RSS map only
7888 	 * if absolutely necessary
7889 	 */
7890 	if (rx_rings != bp->rx_nr_rings) {
7891 		netdev_warn(bp->dev, "Able to reserve only %d out of %d requested RX rings\n",
7892 			    rx_rings, bp->rx_nr_rings);
7893 		if (netif_is_rxfh_configured(bp->dev) &&
7894 		    (bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings) !=
7895 		     bnxt_get_nr_rss_ctxs(bp, rx_rings) ||
7896 		     bnxt_get_max_rss_ring(bp) >= rx_rings)) {
7897 			netdev_warn(bp->dev, "RSS table entries reverting to default\n");
7898 			bp->dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
7899 		}
7900 	}
7901 	bp->rx_nr_rings = rx_rings;
7902 	bp->cp_nr_rings = hwr.cp;
7903 
7904 	if (!bnxt_rings_ok(bp, &hwr))
7905 		return -ENOMEM;
7906 
7907 	if (old_rx_rings != bp->hw_resc.resv_rx_rings &&
7908 	    !netif_is_rxfh_configured(bp->dev))
7909 		bnxt_set_dflt_rss_indir_tbl(bp, NULL);
7910 
7911 	if (!bnxt_ulp_registered(bp->edev) && BNXT_NEW_RM(bp)) {
7912 		int resv_msix, resv_ctx, ulp_ctxs;
7913 		struct bnxt_hw_resc *hw_resc;
7914 
7915 		hw_resc = &bp->hw_resc;
7916 		resv_msix = hw_resc->resv_irqs - bp->cp_nr_rings;
7917 		ulp_msix = min_t(int, resv_msix, ulp_msix);
7918 		bnxt_set_ulp_msix_num(bp, ulp_msix);
7919 		resv_ctx = hw_resc->resv_stat_ctxs  - bp->cp_nr_rings;
7920 		ulp_ctxs = min(resv_ctx, bnxt_get_ulp_stat_ctxs(bp));
7921 		bnxt_set_ulp_stat_ctxs(bp, ulp_ctxs);
7922 	}
7923 
7924 	return rc;
7925 }
7926 
7927 static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7928 {
7929 	struct hwrm_func_vf_cfg_input *req;
7930 	u32 flags;
7931 
7932 	if (!BNXT_NEW_RM(bp))
7933 		return 0;
7934 
7935 	req = __bnxt_hwrm_reserve_vf_rings(bp, hwr);
7936 	flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
7937 		FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7938 		FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7939 		FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7940 		FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST |
7941 		FUNC_VF_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST;
7942 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
7943 		flags |= FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7944 
7945 	req->flags = cpu_to_le32(flags);
7946 	return hwrm_req_send_silent(bp, req);
7947 }
7948 
7949 static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7950 {
7951 	struct hwrm_func_cfg_input *req;
7952 	u32 flags;
7953 
7954 	req = __bnxt_hwrm_reserve_pf_rings(bp, hwr);
7955 	flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
7956 	if (BNXT_NEW_RM(bp)) {
7957 		flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
7958 			 FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
7959 			 FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
7960 			 FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
7961 		if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
7962 			flags |= FUNC_CFG_REQ_FLAGS_RSSCOS_CTX_ASSETS_TEST |
7963 				 FUNC_CFG_REQ_FLAGS_NQ_ASSETS_TEST;
7964 		else
7965 			flags |= FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST;
7966 	}
7967 
7968 	req->flags = cpu_to_le32(flags);
7969 	return hwrm_req_send_silent(bp, req);
7970 }
7971 
7972 static int bnxt_hwrm_check_rings(struct bnxt *bp, struct bnxt_hw_rings *hwr)
7973 {
7974 	if (bp->hwrm_spec_code < 0x10801)
7975 		return 0;
7976 
7977 	if (BNXT_PF(bp))
7978 		return bnxt_hwrm_check_pf_rings(bp, hwr);
7979 
7980 	return bnxt_hwrm_check_vf_rings(bp, hwr);
7981 }
7982 
7983 static void bnxt_hwrm_coal_params_qcaps(struct bnxt *bp)
7984 {
7985 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
7986 	struct hwrm_ring_aggint_qcaps_output *resp;
7987 	struct hwrm_ring_aggint_qcaps_input *req;
7988 	int rc;
7989 
7990 	coal_cap->cmpl_params = BNXT_LEGACY_COAL_CMPL_PARAMS;
7991 	coal_cap->num_cmpl_dma_aggr_max = 63;
7992 	coal_cap->num_cmpl_dma_aggr_during_int_max = 63;
7993 	coal_cap->cmpl_aggr_dma_tmr_max = 65535;
7994 	coal_cap->cmpl_aggr_dma_tmr_during_int_max = 65535;
7995 	coal_cap->int_lat_tmr_min_max = 65535;
7996 	coal_cap->int_lat_tmr_max_max = 65535;
7997 	coal_cap->num_cmpl_aggr_int_max = 65535;
7998 	coal_cap->timer_units = 80;
7999 
8000 	if (bp->hwrm_spec_code < 0x10902)
8001 		return;
8002 
8003 	if (hwrm_req_init(bp, req, HWRM_RING_AGGINT_QCAPS))
8004 		return;
8005 
8006 	resp = hwrm_req_hold(bp, req);
8007 	rc = hwrm_req_send_silent(bp, req);
8008 	if (!rc) {
8009 		coal_cap->cmpl_params = le32_to_cpu(resp->cmpl_params);
8010 		coal_cap->nq_params = le32_to_cpu(resp->nq_params);
8011 		coal_cap->num_cmpl_dma_aggr_max =
8012 			le16_to_cpu(resp->num_cmpl_dma_aggr_max);
8013 		coal_cap->num_cmpl_dma_aggr_during_int_max =
8014 			le16_to_cpu(resp->num_cmpl_dma_aggr_during_int_max);
8015 		coal_cap->cmpl_aggr_dma_tmr_max =
8016 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_max);
8017 		coal_cap->cmpl_aggr_dma_tmr_during_int_max =
8018 			le16_to_cpu(resp->cmpl_aggr_dma_tmr_during_int_max);
8019 		coal_cap->int_lat_tmr_min_max =
8020 			le16_to_cpu(resp->int_lat_tmr_min_max);
8021 		coal_cap->int_lat_tmr_max_max =
8022 			le16_to_cpu(resp->int_lat_tmr_max_max);
8023 		coal_cap->num_cmpl_aggr_int_max =
8024 			le16_to_cpu(resp->num_cmpl_aggr_int_max);
8025 		coal_cap->timer_units = le16_to_cpu(resp->timer_units);
8026 	}
8027 	hwrm_req_drop(bp, req);
8028 }
8029 
8030 static u16 bnxt_usec_to_coal_tmr(struct bnxt *bp, u16 usec)
8031 {
8032 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8033 
8034 	return usec * 1000 / coal_cap->timer_units;
8035 }
8036 
8037 static void bnxt_hwrm_set_coal_params(struct bnxt *bp,
8038 	struct bnxt_coal *hw_coal,
8039 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8040 {
8041 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8042 	u16 val, tmr, max, flags = hw_coal->flags;
8043 	u32 cmpl_params = coal_cap->cmpl_params;
8044 
8045 	max = hw_coal->bufs_per_record * 128;
8046 	if (hw_coal->budget)
8047 		max = hw_coal->bufs_per_record * hw_coal->budget;
8048 	max = min_t(u16, max, coal_cap->num_cmpl_aggr_int_max);
8049 
8050 	val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
8051 	req->num_cmpl_aggr_int = cpu_to_le16(val);
8052 
8053 	val = min_t(u16, val, coal_cap->num_cmpl_dma_aggr_max);
8054 	req->num_cmpl_dma_aggr = cpu_to_le16(val);
8055 
8056 	val = clamp_t(u16, hw_coal->coal_bufs_irq, 1,
8057 		      coal_cap->num_cmpl_dma_aggr_during_int_max);
8058 	req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
8059 
8060 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks);
8061 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_max_max);
8062 	req->int_lat_tmr_max = cpu_to_le16(tmr);
8063 
8064 	/* min timer set to 1/2 of interrupt timer */
8065 	if (cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_INT_LAT_TMR_MIN) {
8066 		val = tmr / 2;
8067 		val = clamp_t(u16, val, 1, coal_cap->int_lat_tmr_min_max);
8068 		req->int_lat_tmr_min = cpu_to_le16(val);
8069 		req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8070 	}
8071 
8072 	/* buf timer set to 1/4 of interrupt timer */
8073 	val = clamp_t(u16, tmr / 4, 1, coal_cap->cmpl_aggr_dma_tmr_max);
8074 	req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
8075 
8076 	if (cmpl_params &
8077 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_NUM_CMPL_DMA_AGGR_DURING_INT) {
8078 		tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks_irq);
8079 		val = clamp_t(u16, tmr, 1,
8080 			      coal_cap->cmpl_aggr_dma_tmr_during_int_max);
8081 		req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(val);
8082 		req->enables |=
8083 			cpu_to_le16(BNXT_COAL_CMPL_AGGR_TMR_DURING_INT_ENABLE);
8084 	}
8085 
8086 	if ((cmpl_params & RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_RING_IDLE) &&
8087 	    hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
8088 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
8089 	req->flags = cpu_to_le16(flags);
8090 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_ENABLES);
8091 }
8092 
8093 static int __bnxt_hwrm_set_coal_nq(struct bnxt *bp, struct bnxt_napi *bnapi,
8094 				   struct bnxt_coal *hw_coal)
8095 {
8096 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req;
8097 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8098 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
8099 	u32 nq_params = coal_cap->nq_params;
8100 	u16 tmr;
8101 	int rc;
8102 
8103 	if (!(nq_params & RING_AGGINT_QCAPS_RESP_NQ_PARAMS_INT_LAT_TMR_MIN))
8104 		return 0;
8105 
8106 	rc = hwrm_req_init(bp, req, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8107 	if (rc)
8108 		return rc;
8109 
8110 	req->ring_id = cpu_to_le16(cpr->cp_ring_struct.fw_ring_id);
8111 	req->flags =
8112 		cpu_to_le16(RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_IS_NQ);
8113 
8114 	tmr = bnxt_usec_to_coal_tmr(bp, hw_coal->coal_ticks) / 2;
8115 	tmr = clamp_t(u16, tmr, 1, coal_cap->int_lat_tmr_min_max);
8116 	req->int_lat_tmr_min = cpu_to_le16(tmr);
8117 	req->enables |= cpu_to_le16(BNXT_COAL_CMPL_MIN_TMR_ENABLE);
8118 	return hwrm_req_send(bp, req);
8119 }
8120 
8121 int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
8122 {
8123 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx;
8124 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8125 	struct bnxt_coal coal;
8126 	int rc;
8127 
8128 	/* Tick values in micro seconds.
8129 	 * 1 coal_buf x bufs_per_record = 1 completion record.
8130 	 */
8131 	memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
8132 
8133 	coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
8134 	coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
8135 
8136 	if (!bnapi->rx_ring)
8137 		return -ENODEV;
8138 
8139 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8140 	if (rc)
8141 		return rc;
8142 
8143 	bnxt_hwrm_set_coal_params(bp, &coal, req_rx);
8144 
8145 	req_rx->ring_id = cpu_to_le16(bnxt_cp_ring_for_rx(bp, bnapi->rx_ring));
8146 
8147 	return hwrm_req_send(bp, req_rx);
8148 }
8149 
8150 static int
8151 bnxt_hwrm_set_rx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8152 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8153 {
8154 	u16 ring_id = bnxt_cp_ring_for_rx(bp, bnapi->rx_ring);
8155 
8156 	req->ring_id = cpu_to_le16(ring_id);
8157 	return hwrm_req_send(bp, req);
8158 }
8159 
8160 static int
8161 bnxt_hwrm_set_tx_coal(struct bnxt *bp, struct bnxt_napi *bnapi,
8162 		      struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
8163 {
8164 	struct bnxt_tx_ring_info *txr;
8165 	int i, rc;
8166 
8167 	bnxt_for_each_napi_tx(i, bnapi, txr) {
8168 		u16 ring_id;
8169 
8170 		ring_id = bnxt_cp_ring_for_tx(bp, txr);
8171 		req->ring_id = cpu_to_le16(ring_id);
8172 		rc = hwrm_req_send(bp, req);
8173 		if (rc)
8174 			return rc;
8175 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8176 			return 0;
8177 	}
8178 	return 0;
8179 }
8180 
8181 int bnxt_hwrm_set_coal(struct bnxt *bp)
8182 {
8183 	struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req_rx, *req_tx;
8184 	int i, rc;
8185 
8186 	rc = hwrm_req_init(bp, req_rx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8187 	if (rc)
8188 		return rc;
8189 
8190 	rc = hwrm_req_init(bp, req_tx, HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS);
8191 	if (rc) {
8192 		hwrm_req_drop(bp, req_rx);
8193 		return rc;
8194 	}
8195 
8196 	bnxt_hwrm_set_coal_params(bp, &bp->rx_coal, req_rx);
8197 	bnxt_hwrm_set_coal_params(bp, &bp->tx_coal, req_tx);
8198 
8199 	hwrm_req_hold(bp, req_rx);
8200 	hwrm_req_hold(bp, req_tx);
8201 	for (i = 0; i < bp->cp_nr_rings; i++) {
8202 		struct bnxt_napi *bnapi = bp->bnapi[i];
8203 		struct bnxt_coal *hw_coal;
8204 
8205 		if (!bnapi->rx_ring)
8206 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8207 		else
8208 			rc = bnxt_hwrm_set_rx_coal(bp, bnapi, req_rx);
8209 		if (rc)
8210 			break;
8211 
8212 		if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
8213 			continue;
8214 
8215 		if (bnapi->rx_ring && bnapi->tx_ring[0]) {
8216 			rc = bnxt_hwrm_set_tx_coal(bp, bnapi, req_tx);
8217 			if (rc)
8218 				break;
8219 		}
8220 		if (bnapi->rx_ring)
8221 			hw_coal = &bp->rx_coal;
8222 		else
8223 			hw_coal = &bp->tx_coal;
8224 		__bnxt_hwrm_set_coal_nq(bp, bnapi, hw_coal);
8225 	}
8226 	hwrm_req_drop(bp, req_rx);
8227 	hwrm_req_drop(bp, req_tx);
8228 	return rc;
8229 }
8230 
8231 static void bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
8232 {
8233 	struct hwrm_stat_ctx_clr_stats_input *req0 = NULL;
8234 	struct hwrm_stat_ctx_free_input *req;
8235 	int i;
8236 
8237 	if (!bp->bnapi)
8238 		return;
8239 
8240 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8241 		return;
8242 
8243 	if (hwrm_req_init(bp, req, HWRM_STAT_CTX_FREE))
8244 		return;
8245 	if (BNXT_FW_MAJ(bp) <= 20) {
8246 		if (hwrm_req_init(bp, req0, HWRM_STAT_CTX_CLR_STATS)) {
8247 			hwrm_req_drop(bp, req);
8248 			return;
8249 		}
8250 		hwrm_req_hold(bp, req0);
8251 	}
8252 	hwrm_req_hold(bp, req);
8253 	for (i = 0; i < bp->cp_nr_rings; i++) {
8254 		struct bnxt_napi *bnapi = bp->bnapi[i];
8255 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8256 
8257 		if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
8258 			req->stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
8259 			if (req0) {
8260 				req0->stat_ctx_id = req->stat_ctx_id;
8261 				hwrm_req_send(bp, req0);
8262 			}
8263 			hwrm_req_send(bp, req);
8264 
8265 			cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
8266 		}
8267 	}
8268 	hwrm_req_drop(bp, req);
8269 	if (req0)
8270 		hwrm_req_drop(bp, req0);
8271 }
8272 
8273 static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
8274 {
8275 	struct hwrm_stat_ctx_alloc_output *resp;
8276 	struct hwrm_stat_ctx_alloc_input *req;
8277 	int rc, i;
8278 
8279 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
8280 		return 0;
8281 
8282 	rc = hwrm_req_init(bp, req, HWRM_STAT_CTX_ALLOC);
8283 	if (rc)
8284 		return rc;
8285 
8286 	req->stats_dma_length = cpu_to_le16(bp->hw_ring_stats_size);
8287 	req->update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
8288 
8289 	resp = hwrm_req_hold(bp, req);
8290 	for (i = 0; i < bp->cp_nr_rings; i++) {
8291 		struct bnxt_napi *bnapi = bp->bnapi[i];
8292 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
8293 
8294 		req->stats_dma_addr = cpu_to_le64(cpr->stats.hw_stats_map);
8295 
8296 		rc = hwrm_req_send(bp, req);
8297 		if (rc)
8298 			break;
8299 
8300 		cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
8301 
8302 		bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
8303 	}
8304 	hwrm_req_drop(bp, req);
8305 	return rc;
8306 }
8307 
8308 static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
8309 {
8310 	struct hwrm_func_qcfg_output *resp;
8311 	struct hwrm_func_qcfg_input *req;
8312 	u16 flags;
8313 	int rc;
8314 
8315 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCFG);
8316 	if (rc)
8317 		return rc;
8318 
8319 	req->fid = cpu_to_le16(0xffff);
8320 	resp = hwrm_req_hold(bp, req);
8321 	rc = hwrm_req_send(bp, req);
8322 	if (rc)
8323 		goto func_qcfg_exit;
8324 
8325 #ifdef CONFIG_BNXT_SRIOV
8326 	if (BNXT_VF(bp)) {
8327 		struct bnxt_vf_info *vf = &bp->vf;
8328 
8329 		vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
8330 	} else {
8331 		bp->pf.registered_vfs = le16_to_cpu(resp->registered_vfs);
8332 	}
8333 #endif
8334 	flags = le16_to_cpu(resp->flags);
8335 	if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
8336 		     FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
8337 		bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
8338 		if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
8339 			bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
8340 	}
8341 	if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
8342 		bp->flags |= BNXT_FLAG_MULTI_HOST;
8343 
8344 	if (flags & FUNC_QCFG_RESP_FLAGS_RING_MONITOR_ENABLED)
8345 		bp->fw_cap |= BNXT_FW_CAP_RING_MONITOR;
8346 
8347 	if (flags & FUNC_QCFG_RESP_FLAGS_ENABLE_RDMA_SRIOV)
8348 		bp->fw_cap |= BNXT_FW_CAP_ENABLE_RDMA_SRIOV;
8349 
8350 	switch (resp->port_partition_type) {
8351 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
8352 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
8353 	case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
8354 		bp->port_partition_type = resp->port_partition_type;
8355 		break;
8356 	}
8357 	if (bp->hwrm_spec_code < 0x10707 ||
8358 	    resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
8359 		bp->br_mode = BRIDGE_MODE_VEB;
8360 	else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
8361 		bp->br_mode = BRIDGE_MODE_VEPA;
8362 	else
8363 		bp->br_mode = BRIDGE_MODE_UNDEF;
8364 
8365 	bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
8366 	if (!bp->max_mtu)
8367 		bp->max_mtu = BNXT_MAX_MTU;
8368 
8369 	if (bp->db_size)
8370 		goto func_qcfg_exit;
8371 
8372 	bp->db_offset = le16_to_cpu(resp->legacy_l2_db_size_kb) * 1024;
8373 	if (BNXT_CHIP_P5(bp)) {
8374 		if (BNXT_PF(bp))
8375 			bp->db_offset = DB_PF_OFFSET_P5;
8376 		else
8377 			bp->db_offset = DB_VF_OFFSET_P5;
8378 	}
8379 	bp->db_size = PAGE_ALIGN(le16_to_cpu(resp->l2_doorbell_bar_size_kb) *
8380 				 1024);
8381 	if (!bp->db_size || bp->db_size > pci_resource_len(bp->pdev, 2) ||
8382 	    bp->db_size <= bp->db_offset)
8383 		bp->db_size = pci_resource_len(bp->pdev, 2);
8384 
8385 func_qcfg_exit:
8386 	hwrm_req_drop(bp, req);
8387 	return rc;
8388 }
8389 
8390 static void bnxt_init_ctx_initializer(struct bnxt_ctx_mem_type *ctxm,
8391 				      u8 init_val, u8 init_offset,
8392 				      bool init_mask_set)
8393 {
8394 	ctxm->init_value = init_val;
8395 	ctxm->init_offset = BNXT_CTX_INIT_INVALID_OFFSET;
8396 	if (init_mask_set)
8397 		ctxm->init_offset = init_offset * 4;
8398 	else
8399 		ctxm->init_value = 0;
8400 }
8401 
8402 static int bnxt_alloc_all_ctx_pg_info(struct bnxt *bp, int ctx_max)
8403 {
8404 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8405 	u16 type;
8406 
8407 	for (type = 0; type < ctx_max; type++) {
8408 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8409 		int n = 1;
8410 
8411 		if (!ctxm->max_entries || ctxm->pg_info)
8412 			continue;
8413 
8414 		if (ctxm->instance_bmap)
8415 			n = hweight32(ctxm->instance_bmap);
8416 		ctxm->pg_info = kcalloc(n, sizeof(*ctxm->pg_info), GFP_KERNEL);
8417 		if (!ctxm->pg_info)
8418 			return -ENOMEM;
8419 	}
8420 	return 0;
8421 }
8422 
8423 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
8424 				  struct bnxt_ctx_mem_type *ctxm, bool force);
8425 
8426 #define BNXT_CTX_INIT_VALID(flags)	\
8427 	(!!((flags) &			\
8428 	    FUNC_BACKING_STORE_QCAPS_V2_RESP_FLAGS_ENABLE_CTX_KIND_INIT))
8429 
8430 static int bnxt_hwrm_func_backing_store_qcaps_v2(struct bnxt *bp)
8431 {
8432 	struct hwrm_func_backing_store_qcaps_v2_output *resp;
8433 	struct hwrm_func_backing_store_qcaps_v2_input *req;
8434 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8435 	u16 type;
8436 	int rc;
8437 
8438 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS_V2);
8439 	if (rc)
8440 		return rc;
8441 
8442 	if (!ctx) {
8443 		ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8444 		if (!ctx)
8445 			return -ENOMEM;
8446 		bp->ctx = ctx;
8447 	}
8448 
8449 	resp = hwrm_req_hold(bp, req);
8450 
8451 	for (type = 0; type < BNXT_CTX_V2_MAX; ) {
8452 		struct bnxt_ctx_mem_type *ctxm = &ctx->ctx_arr[type];
8453 		u8 init_val, init_off, i;
8454 		u32 max_entries;
8455 		u16 entry_size;
8456 		__le32 *p;
8457 		u32 flags;
8458 
8459 		req->type = cpu_to_le16(type);
8460 		rc = hwrm_req_send(bp, req);
8461 		if (rc)
8462 			goto ctx_done;
8463 		flags = le32_to_cpu(resp->flags);
8464 		type = le16_to_cpu(resp->next_valid_type);
8465 		if (!(flags & BNXT_CTX_MEM_TYPE_VALID)) {
8466 			bnxt_free_one_ctx_mem(bp, ctxm, true);
8467 			continue;
8468 		}
8469 		entry_size = le16_to_cpu(resp->entry_size);
8470 		max_entries = le32_to_cpu(resp->max_num_entries);
8471 		if (ctxm->mem_valid) {
8472 			if (!(flags & BNXT_CTX_MEM_PERSIST) ||
8473 			    ctxm->entry_size != entry_size ||
8474 			    ctxm->max_entries != max_entries)
8475 				bnxt_free_one_ctx_mem(bp, ctxm, true);
8476 			else
8477 				continue;
8478 		}
8479 		ctxm->type = le16_to_cpu(resp->type);
8480 		ctxm->entry_size = entry_size;
8481 		ctxm->flags = flags;
8482 		ctxm->instance_bmap = le32_to_cpu(resp->instance_bit_map);
8483 		ctxm->entry_multiple = resp->entry_multiple;
8484 		ctxm->max_entries = max_entries;
8485 		ctxm->min_entries = le32_to_cpu(resp->min_num_entries);
8486 		init_val = resp->ctx_init_value;
8487 		init_off = resp->ctx_init_offset;
8488 		bnxt_init_ctx_initializer(ctxm, init_val, init_off,
8489 					  BNXT_CTX_INIT_VALID(flags));
8490 		ctxm->split_entry_cnt = min_t(u8, resp->subtype_valid_cnt,
8491 					      BNXT_MAX_SPLIT_ENTRY);
8492 		for (i = 0, p = &resp->split_entry_0; i < ctxm->split_entry_cnt;
8493 		     i++, p++)
8494 			ctxm->split[i] = le32_to_cpu(*p);
8495 	}
8496 	rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_V2_MAX);
8497 
8498 ctx_done:
8499 	hwrm_req_drop(bp, req);
8500 	return rc;
8501 }
8502 
8503 static int bnxt_hwrm_func_backing_store_qcaps(struct bnxt *bp)
8504 {
8505 	struct hwrm_func_backing_store_qcaps_output *resp;
8506 	struct hwrm_func_backing_store_qcaps_input *req;
8507 	int rc;
8508 
8509 	if (bp->hwrm_spec_code < 0x10902 || BNXT_VF(bp) ||
8510 	    (bp->ctx && bp->ctx->flags & BNXT_CTX_FLAG_INITED))
8511 		return 0;
8512 
8513 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
8514 		return bnxt_hwrm_func_backing_store_qcaps_v2(bp);
8515 
8516 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_QCAPS);
8517 	if (rc)
8518 		return rc;
8519 
8520 	resp = hwrm_req_hold(bp, req);
8521 	rc = hwrm_req_send_silent(bp, req);
8522 	if (!rc) {
8523 		struct bnxt_ctx_mem_type *ctxm;
8524 		struct bnxt_ctx_mem_info *ctx;
8525 		u8 init_val, init_idx = 0;
8526 		u16 init_mask;
8527 
8528 		ctx = bp->ctx;
8529 		if (!ctx) {
8530 			ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
8531 			if (!ctx) {
8532 				rc = -ENOMEM;
8533 				goto ctx_err;
8534 			}
8535 			bp->ctx = ctx;
8536 		}
8537 		init_val = resp->ctx_kind_initializer;
8538 		init_mask = le16_to_cpu(resp->ctx_init_mask);
8539 
8540 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8541 		ctxm->max_entries = le32_to_cpu(resp->qp_max_entries);
8542 		ctxm->qp_qp1_entries = le16_to_cpu(resp->qp_min_qp1_entries);
8543 		ctxm->qp_l2_entries = le16_to_cpu(resp->qp_max_l2_entries);
8544 		ctxm->qp_fast_qpmd_entries = le16_to_cpu(resp->fast_qpmd_qp_num_entries);
8545 		ctxm->entry_size = le16_to_cpu(resp->qp_entry_size);
8546 		bnxt_init_ctx_initializer(ctxm, init_val, resp->qp_init_offset,
8547 					  (init_mask & (1 << init_idx++)) != 0);
8548 
8549 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8550 		ctxm->srq_l2_entries = le16_to_cpu(resp->srq_max_l2_entries);
8551 		ctxm->max_entries = le32_to_cpu(resp->srq_max_entries);
8552 		ctxm->entry_size = le16_to_cpu(resp->srq_entry_size);
8553 		bnxt_init_ctx_initializer(ctxm, init_val, resp->srq_init_offset,
8554 					  (init_mask & (1 << init_idx++)) != 0);
8555 
8556 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8557 		ctxm->cq_l2_entries = le16_to_cpu(resp->cq_max_l2_entries);
8558 		ctxm->max_entries = le32_to_cpu(resp->cq_max_entries);
8559 		ctxm->entry_size = le16_to_cpu(resp->cq_entry_size);
8560 		bnxt_init_ctx_initializer(ctxm, init_val, resp->cq_init_offset,
8561 					  (init_mask & (1 << init_idx++)) != 0);
8562 
8563 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8564 		ctxm->vnic_entries = le16_to_cpu(resp->vnic_max_vnic_entries);
8565 		ctxm->max_entries = ctxm->vnic_entries +
8566 			le16_to_cpu(resp->vnic_max_ring_table_entries);
8567 		ctxm->entry_size = le16_to_cpu(resp->vnic_entry_size);
8568 		bnxt_init_ctx_initializer(ctxm, init_val,
8569 					  resp->vnic_init_offset,
8570 					  (init_mask & (1 << init_idx++)) != 0);
8571 
8572 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8573 		ctxm->max_entries = le32_to_cpu(resp->stat_max_entries);
8574 		ctxm->entry_size = le16_to_cpu(resp->stat_entry_size);
8575 		bnxt_init_ctx_initializer(ctxm, init_val,
8576 					  resp->stat_init_offset,
8577 					  (init_mask & (1 << init_idx++)) != 0);
8578 
8579 		ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8580 		ctxm->entry_size = le16_to_cpu(resp->tqm_entry_size);
8581 		ctxm->min_entries = le32_to_cpu(resp->tqm_min_entries_per_ring);
8582 		ctxm->max_entries = le32_to_cpu(resp->tqm_max_entries_per_ring);
8583 		ctxm->entry_multiple = resp->tqm_entries_multiple;
8584 		if (!ctxm->entry_multiple)
8585 			ctxm->entry_multiple = 1;
8586 
8587 		memcpy(&ctx->ctx_arr[BNXT_CTX_FTQM], ctxm, sizeof(*ctxm));
8588 
8589 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8590 		ctxm->max_entries = le32_to_cpu(resp->mrav_max_entries);
8591 		ctxm->entry_size = le16_to_cpu(resp->mrav_entry_size);
8592 		ctxm->mrav_num_entries_units =
8593 			le16_to_cpu(resp->mrav_num_entries_units);
8594 		bnxt_init_ctx_initializer(ctxm, init_val,
8595 					  resp->mrav_init_offset,
8596 					  (init_mask & (1 << init_idx++)) != 0);
8597 
8598 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8599 		ctxm->entry_size = le16_to_cpu(resp->tim_entry_size);
8600 		ctxm->max_entries = le32_to_cpu(resp->tim_max_entries);
8601 
8602 		ctx->tqm_fp_rings_count = resp->tqm_fp_rings_count;
8603 		if (!ctx->tqm_fp_rings_count)
8604 			ctx->tqm_fp_rings_count = bp->max_q;
8605 		else if (ctx->tqm_fp_rings_count > BNXT_MAX_TQM_FP_RINGS)
8606 			ctx->tqm_fp_rings_count = BNXT_MAX_TQM_FP_RINGS;
8607 
8608 		ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
8609 		memcpy(ctxm, &ctx->ctx_arr[BNXT_CTX_STQM], sizeof(*ctxm));
8610 		ctxm->instance_bmap = (1 << ctx->tqm_fp_rings_count) - 1;
8611 
8612 		rc = bnxt_alloc_all_ctx_pg_info(bp, BNXT_CTX_MAX);
8613 	} else {
8614 		rc = 0;
8615 	}
8616 ctx_err:
8617 	hwrm_req_drop(bp, req);
8618 	return rc;
8619 }
8620 
8621 static void bnxt_hwrm_set_pg_attr(struct bnxt_ring_mem_info *rmem, u8 *pg_attr,
8622 				  __le64 *pg_dir)
8623 {
8624 	if (!rmem->nr_pages)
8625 		return;
8626 
8627 	BNXT_SET_CTX_PAGE_ATTR(*pg_attr);
8628 	if (rmem->depth >= 1) {
8629 		if (rmem->depth == 2)
8630 			*pg_attr |= 2;
8631 		else
8632 			*pg_attr |= 1;
8633 		*pg_dir = cpu_to_le64(rmem->pg_tbl_map);
8634 	} else {
8635 		*pg_dir = cpu_to_le64(rmem->dma_arr[0]);
8636 	}
8637 }
8638 
8639 #define FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES			\
8640 	(FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP |		\
8641 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ |		\
8642 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ |		\
8643 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC |		\
8644 	 FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT)
8645 
8646 static int bnxt_hwrm_func_backing_store_cfg(struct bnxt *bp, u32 enables)
8647 {
8648 	struct hwrm_func_backing_store_cfg_input *req;
8649 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8650 	struct bnxt_ctx_pg_info *ctx_pg;
8651 	struct bnxt_ctx_mem_type *ctxm;
8652 	void **__req = (void **)&req;
8653 	u32 req_len = sizeof(*req);
8654 	__le32 *num_entries;
8655 	__le64 *pg_dir;
8656 	u32 flags = 0;
8657 	u8 *pg_attr;
8658 	u32 ena;
8659 	int rc;
8660 	int i;
8661 
8662 	if (!ctx)
8663 		return 0;
8664 
8665 	if (req_len > bp->hwrm_max_ext_req_len)
8666 		req_len = BNXT_BACKING_STORE_CFG_LEGACY_LEN;
8667 	rc = __hwrm_req_init(bp, __req, HWRM_FUNC_BACKING_STORE_CFG, req_len);
8668 	if (rc)
8669 		return rc;
8670 
8671 	req->enables = cpu_to_le32(enables);
8672 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP) {
8673 		ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
8674 		ctx_pg = ctxm->pg_info;
8675 		req->qp_num_entries = cpu_to_le32(ctx_pg->entries);
8676 		req->qp_num_qp1_entries = cpu_to_le16(ctxm->qp_qp1_entries);
8677 		req->qp_num_l2_entries = cpu_to_le16(ctxm->qp_l2_entries);
8678 		req->qp_entry_size = cpu_to_le16(ctxm->entry_size);
8679 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8680 				      &req->qpc_pg_size_qpc_lvl,
8681 				      &req->qpc_page_dir);
8682 
8683 		if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD)
8684 			req->qp_num_fast_qpmd_entries = cpu_to_le16(ctxm->qp_fast_qpmd_entries);
8685 	}
8686 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_SRQ) {
8687 		ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
8688 		ctx_pg = ctxm->pg_info;
8689 		req->srq_num_entries = cpu_to_le32(ctx_pg->entries);
8690 		req->srq_num_l2_entries = cpu_to_le16(ctxm->srq_l2_entries);
8691 		req->srq_entry_size = cpu_to_le16(ctxm->entry_size);
8692 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8693 				      &req->srq_pg_size_srq_lvl,
8694 				      &req->srq_page_dir);
8695 	}
8696 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_CQ) {
8697 		ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
8698 		ctx_pg = ctxm->pg_info;
8699 		req->cq_num_entries = cpu_to_le32(ctx_pg->entries);
8700 		req->cq_num_l2_entries = cpu_to_le16(ctxm->cq_l2_entries);
8701 		req->cq_entry_size = cpu_to_le16(ctxm->entry_size);
8702 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8703 				      &req->cq_pg_size_cq_lvl,
8704 				      &req->cq_page_dir);
8705 	}
8706 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_VNIC) {
8707 		ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
8708 		ctx_pg = ctxm->pg_info;
8709 		req->vnic_num_vnic_entries = cpu_to_le16(ctxm->vnic_entries);
8710 		req->vnic_num_ring_table_entries =
8711 			cpu_to_le16(ctxm->max_entries - ctxm->vnic_entries);
8712 		req->vnic_entry_size = cpu_to_le16(ctxm->entry_size);
8713 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8714 				      &req->vnic_pg_size_vnic_lvl,
8715 				      &req->vnic_page_dir);
8716 	}
8717 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_STAT) {
8718 		ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
8719 		ctx_pg = ctxm->pg_info;
8720 		req->stat_num_entries = cpu_to_le32(ctxm->max_entries);
8721 		req->stat_entry_size = cpu_to_le16(ctxm->entry_size);
8722 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8723 				      &req->stat_pg_size_stat_lvl,
8724 				      &req->stat_page_dir);
8725 	}
8726 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV) {
8727 		u32 units;
8728 
8729 		ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
8730 		ctx_pg = ctxm->pg_info;
8731 		req->mrav_num_entries = cpu_to_le32(ctx_pg->entries);
8732 		units = ctxm->mrav_num_entries_units;
8733 		if (units) {
8734 			u32 num_mr, num_ah = ctxm->mrav_av_entries;
8735 			u32 entries;
8736 
8737 			num_mr = ctx_pg->entries - num_ah;
8738 			entries = ((num_mr / units) << 16) | (num_ah / units);
8739 			req->mrav_num_entries = cpu_to_le32(entries);
8740 			flags |= FUNC_BACKING_STORE_CFG_REQ_FLAGS_MRAV_RESERVATION_SPLIT;
8741 		}
8742 		req->mrav_entry_size = cpu_to_le16(ctxm->entry_size);
8743 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8744 				      &req->mrav_pg_size_mrav_lvl,
8745 				      &req->mrav_page_dir);
8746 	}
8747 	if (enables & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM) {
8748 		ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
8749 		ctx_pg = ctxm->pg_info;
8750 		req->tim_num_entries = cpu_to_le32(ctx_pg->entries);
8751 		req->tim_entry_size = cpu_to_le16(ctxm->entry_size);
8752 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8753 				      &req->tim_pg_size_tim_lvl,
8754 				      &req->tim_page_dir);
8755 	}
8756 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
8757 	for (i = 0, num_entries = &req->tqm_sp_num_entries,
8758 	     pg_attr = &req->tqm_sp_pg_size_tqm_sp_lvl,
8759 	     pg_dir = &req->tqm_sp_page_dir,
8760 	     ena = FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP,
8761 	     ctx_pg = ctxm->pg_info;
8762 	     i < BNXT_MAX_TQM_RINGS;
8763 	     ctx_pg = &ctx->ctx_arr[BNXT_CTX_FTQM].pg_info[i],
8764 	     i++, num_entries++, pg_attr++, pg_dir++, ena <<= 1) {
8765 		if (!(enables & ena))
8766 			continue;
8767 
8768 		req->tqm_entry_size = cpu_to_le16(ctxm->entry_size);
8769 		*num_entries = cpu_to_le32(ctx_pg->entries);
8770 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem, pg_attr, pg_dir);
8771 	}
8772 	req->flags = cpu_to_le32(flags);
8773 	return hwrm_req_send(bp, req);
8774 }
8775 
8776 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
8777 				  struct bnxt_ctx_pg_info *ctx_pg)
8778 {
8779 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8780 
8781 	rmem->page_size = BNXT_PAGE_SIZE;
8782 	rmem->pg_arr = ctx_pg->ctx_pg_arr;
8783 	rmem->dma_arr = ctx_pg->ctx_dma_arr;
8784 	rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
8785 	if (rmem->depth >= 1)
8786 		rmem->flags |= BNXT_RMEM_USE_FULL_PAGE_FLAG;
8787 	return bnxt_alloc_ring(bp, rmem);
8788 }
8789 
8790 static int bnxt_alloc_ctx_pg_tbls(struct bnxt *bp,
8791 				  struct bnxt_ctx_pg_info *ctx_pg, u32 mem_size,
8792 				  u8 depth, struct bnxt_ctx_mem_type *ctxm)
8793 {
8794 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8795 	int rc;
8796 
8797 	if (!mem_size)
8798 		return -EINVAL;
8799 
8800 	ctx_pg->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8801 	if (ctx_pg->nr_pages > MAX_CTX_TOTAL_PAGES) {
8802 		ctx_pg->nr_pages = 0;
8803 		return -EINVAL;
8804 	}
8805 	if (ctx_pg->nr_pages > MAX_CTX_PAGES || depth > 1) {
8806 		int nr_tbls, i;
8807 
8808 		rmem->depth = 2;
8809 		ctx_pg->ctx_pg_tbl = kcalloc(MAX_CTX_PAGES, sizeof(ctx_pg),
8810 					     GFP_KERNEL);
8811 		if (!ctx_pg->ctx_pg_tbl)
8812 			return -ENOMEM;
8813 		nr_tbls = DIV_ROUND_UP(ctx_pg->nr_pages, MAX_CTX_PAGES);
8814 		rmem->nr_pages = nr_tbls;
8815 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8816 		if (rc)
8817 			return rc;
8818 		for (i = 0; i < nr_tbls; i++) {
8819 			struct bnxt_ctx_pg_info *pg_tbl;
8820 
8821 			pg_tbl = kzalloc(sizeof(*pg_tbl), GFP_KERNEL);
8822 			if (!pg_tbl)
8823 				return -ENOMEM;
8824 			ctx_pg->ctx_pg_tbl[i] = pg_tbl;
8825 			rmem = &pg_tbl->ring_mem;
8826 			rmem->pg_tbl = ctx_pg->ctx_pg_arr[i];
8827 			rmem->pg_tbl_map = ctx_pg->ctx_dma_arr[i];
8828 			rmem->depth = 1;
8829 			rmem->nr_pages = MAX_CTX_PAGES;
8830 			rmem->ctx_mem = ctxm;
8831 			if (i == (nr_tbls - 1)) {
8832 				int rem = ctx_pg->nr_pages % MAX_CTX_PAGES;
8833 
8834 				if (rem)
8835 					rmem->nr_pages = rem;
8836 			}
8837 			rc = bnxt_alloc_ctx_mem_blk(bp, pg_tbl);
8838 			if (rc)
8839 				break;
8840 		}
8841 	} else {
8842 		rmem->nr_pages = DIV_ROUND_UP(mem_size, BNXT_PAGE_SIZE);
8843 		if (rmem->nr_pages > 1 || depth)
8844 			rmem->depth = 1;
8845 		rmem->ctx_mem = ctxm;
8846 		rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg);
8847 	}
8848 	return rc;
8849 }
8850 
8851 static size_t bnxt_copy_ctx_pg_tbls(struct bnxt *bp,
8852 				    struct bnxt_ctx_pg_info *ctx_pg,
8853 				    void *buf, size_t offset, size_t head,
8854 				    size_t tail)
8855 {
8856 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8857 	size_t nr_pages = ctx_pg->nr_pages;
8858 	int page_size = rmem->page_size;
8859 	size_t len = 0, total_len = 0;
8860 	u16 depth = rmem->depth;
8861 
8862 	tail %= nr_pages * page_size;
8863 	do {
8864 		if (depth > 1) {
8865 			int i = head / (page_size * MAX_CTX_PAGES);
8866 			struct bnxt_ctx_pg_info *pg_tbl;
8867 
8868 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8869 			rmem = &pg_tbl->ring_mem;
8870 		}
8871 		len = __bnxt_copy_ring(bp, rmem, buf, offset, head, tail);
8872 		head += len;
8873 		offset += len;
8874 		total_len += len;
8875 		if (head >= nr_pages * page_size)
8876 			head = 0;
8877 	} while (head != tail);
8878 	return total_len;
8879 }
8880 
8881 static void bnxt_free_ctx_pg_tbls(struct bnxt *bp,
8882 				  struct bnxt_ctx_pg_info *ctx_pg)
8883 {
8884 	struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
8885 
8886 	if (rmem->depth > 1 || ctx_pg->nr_pages > MAX_CTX_PAGES ||
8887 	    ctx_pg->ctx_pg_tbl) {
8888 		int i, nr_tbls = rmem->nr_pages;
8889 
8890 		for (i = 0; i < nr_tbls; i++) {
8891 			struct bnxt_ctx_pg_info *pg_tbl;
8892 			struct bnxt_ring_mem_info *rmem2;
8893 
8894 			pg_tbl = ctx_pg->ctx_pg_tbl[i];
8895 			if (!pg_tbl)
8896 				continue;
8897 			rmem2 = &pg_tbl->ring_mem;
8898 			bnxt_free_ring(bp, rmem2);
8899 			ctx_pg->ctx_pg_arr[i] = NULL;
8900 			kfree(pg_tbl);
8901 			ctx_pg->ctx_pg_tbl[i] = NULL;
8902 		}
8903 		kfree(ctx_pg->ctx_pg_tbl);
8904 		ctx_pg->ctx_pg_tbl = NULL;
8905 	}
8906 	bnxt_free_ring(bp, rmem);
8907 	ctx_pg->nr_pages = 0;
8908 }
8909 
8910 static int bnxt_setup_ctxm_pg_tbls(struct bnxt *bp,
8911 				   struct bnxt_ctx_mem_type *ctxm, u32 entries,
8912 				   u8 pg_lvl)
8913 {
8914 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
8915 	int i, rc = 0, n = 1;
8916 	u32 mem_size;
8917 
8918 	if (!ctxm->entry_size || !ctx_pg)
8919 		return -EINVAL;
8920 	if (ctxm->instance_bmap)
8921 		n = hweight32(ctxm->instance_bmap);
8922 	if (ctxm->entry_multiple)
8923 		entries = roundup(entries, ctxm->entry_multiple);
8924 	entries = clamp_t(u32, entries, ctxm->min_entries, ctxm->max_entries);
8925 	mem_size = entries * ctxm->entry_size;
8926 	for (i = 0; i < n && !rc; i++) {
8927 		ctx_pg[i].entries = entries;
8928 		rc = bnxt_alloc_ctx_pg_tbls(bp, &ctx_pg[i], mem_size, pg_lvl,
8929 					    ctxm->init_value ? ctxm : NULL);
8930 	}
8931 	if (!rc)
8932 		ctxm->mem_valid = 1;
8933 	return rc;
8934 }
8935 
8936 static int bnxt_hwrm_func_backing_store_cfg_v2(struct bnxt *bp,
8937 					       struct bnxt_ctx_mem_type *ctxm,
8938 					       bool last)
8939 {
8940 	struct hwrm_func_backing_store_cfg_v2_input *req;
8941 	u32 instance_bmap = ctxm->instance_bmap;
8942 	int i, j, rc = 0, n = 1;
8943 	__le32 *p;
8944 
8945 	if (!(ctxm->flags & BNXT_CTX_MEM_TYPE_VALID) || !ctxm->pg_info)
8946 		return 0;
8947 
8948 	if (instance_bmap)
8949 		n = hweight32(ctxm->instance_bmap);
8950 	else
8951 		instance_bmap = 1;
8952 
8953 	rc = hwrm_req_init(bp, req, HWRM_FUNC_BACKING_STORE_CFG_V2);
8954 	if (rc)
8955 		return rc;
8956 	hwrm_req_hold(bp, req);
8957 	req->type = cpu_to_le16(ctxm->type);
8958 	req->entry_size = cpu_to_le16(ctxm->entry_size);
8959 	if ((ctxm->flags & BNXT_CTX_MEM_PERSIST) &&
8960 	    bnxt_bs_trace_avail(bp, ctxm->type)) {
8961 		struct bnxt_bs_trace_info *bs_trace;
8962 		u32 enables;
8963 
8964 		enables = FUNC_BACKING_STORE_CFG_V2_REQ_ENABLES_NEXT_BS_OFFSET;
8965 		req->enables = cpu_to_le32(enables);
8966 		bs_trace = &bp->bs_trace[bnxt_bstore_to_trace[ctxm->type]];
8967 		req->next_bs_offset = cpu_to_le32(bs_trace->last_offset);
8968 	}
8969 	req->subtype_valid_cnt = ctxm->split_entry_cnt;
8970 	for (i = 0, p = &req->split_entry_0; i < ctxm->split_entry_cnt; i++)
8971 		p[i] = cpu_to_le32(ctxm->split[i]);
8972 	for (i = 0, j = 0; j < n && !rc; i++) {
8973 		struct bnxt_ctx_pg_info *ctx_pg;
8974 
8975 		if (!(instance_bmap & (1 << i)))
8976 			continue;
8977 		req->instance = cpu_to_le16(i);
8978 		ctx_pg = &ctxm->pg_info[j++];
8979 		if (!ctx_pg->entries)
8980 			continue;
8981 		req->num_entries = cpu_to_le32(ctx_pg->entries);
8982 		bnxt_hwrm_set_pg_attr(&ctx_pg->ring_mem,
8983 				      &req->page_size_pbl_level,
8984 				      &req->page_dir);
8985 		if (last && j == n)
8986 			req->flags =
8987 				cpu_to_le32(FUNC_BACKING_STORE_CFG_V2_REQ_FLAGS_BS_CFG_ALL_DONE);
8988 		rc = hwrm_req_send(bp, req);
8989 	}
8990 	hwrm_req_drop(bp, req);
8991 	return rc;
8992 }
8993 
8994 static int bnxt_backing_store_cfg_v2(struct bnxt *bp, u32 ena)
8995 {
8996 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
8997 	struct bnxt_ctx_mem_type *ctxm;
8998 	u16 last_type = BNXT_CTX_INV;
8999 	int rc = 0;
9000 	u16 type;
9001 
9002 	for (type = BNXT_CTX_SRT; type <= BNXT_CTX_RIGP1; type++) {
9003 		ctxm = &ctx->ctx_arr[type];
9004 		if (!bnxt_bs_trace_avail(bp, type))
9005 			continue;
9006 		if (!ctxm->mem_valid) {
9007 			rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm,
9008 						     ctxm->max_entries, 1);
9009 			if (rc) {
9010 				netdev_warn(bp->dev, "Unable to setup ctx page for type:0x%x.\n",
9011 					    type);
9012 				continue;
9013 			}
9014 			bnxt_bs_trace_init(bp, ctxm);
9015 		}
9016 		last_type = type;
9017 	}
9018 
9019 	if (last_type == BNXT_CTX_INV) {
9020 		if (!ena)
9021 			return 0;
9022 		else if (ena & FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM)
9023 			last_type = BNXT_CTX_MAX - 1;
9024 		else
9025 			last_type = BNXT_CTX_L2_MAX - 1;
9026 	}
9027 	ctx->ctx_arr[last_type].last = 1;
9028 
9029 	for (type = 0 ; type < BNXT_CTX_V2_MAX; type++) {
9030 		ctxm = &ctx->ctx_arr[type];
9031 
9032 		if (!ctxm->mem_valid)
9033 			continue;
9034 		rc = bnxt_hwrm_func_backing_store_cfg_v2(bp, ctxm, ctxm->last);
9035 		if (rc)
9036 			return rc;
9037 	}
9038 	return 0;
9039 }
9040 
9041 /**
9042  * __bnxt_copy_ctx_mem - copy host context memory
9043  * @bp: The driver context
9044  * @ctxm: The pointer to the context memory type
9045  * @buf: The destination buffer or NULL to just obtain the length
9046  * @offset: The buffer offset to copy the data to
9047  * @head: The head offset of context memory to copy from
9048  * @tail: The tail offset (last byte + 1) of context memory to end the copy
9049  *
9050  * This function is called for debugging purposes to dump the host context
9051  * used by the chip.
9052  *
9053  * Return: Length of memory copied
9054  */
9055 static size_t __bnxt_copy_ctx_mem(struct bnxt *bp,
9056 				  struct bnxt_ctx_mem_type *ctxm, void *buf,
9057 				  size_t offset, size_t head, size_t tail)
9058 {
9059 	struct bnxt_ctx_pg_info *ctx_pg = ctxm->pg_info;
9060 	size_t len = 0, total_len = 0;
9061 	int i, n = 1;
9062 
9063 	if (!ctx_pg)
9064 		return 0;
9065 
9066 	if (ctxm->instance_bmap)
9067 		n = hweight32(ctxm->instance_bmap);
9068 	for (i = 0; i < n; i++) {
9069 		len = bnxt_copy_ctx_pg_tbls(bp, &ctx_pg[i], buf, offset, head,
9070 					    tail);
9071 		offset += len;
9072 		total_len += len;
9073 	}
9074 	return total_len;
9075 }
9076 
9077 size_t bnxt_copy_ctx_mem(struct bnxt *bp, struct bnxt_ctx_mem_type *ctxm,
9078 			 void *buf, size_t offset)
9079 {
9080 	size_t tail = ctxm->max_entries * ctxm->entry_size;
9081 
9082 	return __bnxt_copy_ctx_mem(bp, ctxm, buf, offset, 0, tail);
9083 }
9084 
9085 static void bnxt_free_one_ctx_mem(struct bnxt *bp,
9086 				  struct bnxt_ctx_mem_type *ctxm, bool force)
9087 {
9088 	struct bnxt_ctx_pg_info *ctx_pg;
9089 	int i, n = 1;
9090 
9091 	ctxm->last = 0;
9092 
9093 	if (ctxm->mem_valid && !force && (ctxm->flags & BNXT_CTX_MEM_PERSIST))
9094 		return;
9095 
9096 	ctx_pg = ctxm->pg_info;
9097 	if (ctx_pg) {
9098 		if (ctxm->instance_bmap)
9099 			n = hweight32(ctxm->instance_bmap);
9100 		for (i = 0; i < n; i++)
9101 			bnxt_free_ctx_pg_tbls(bp, &ctx_pg[i]);
9102 
9103 		kfree(ctx_pg);
9104 		ctxm->pg_info = NULL;
9105 		ctxm->mem_valid = 0;
9106 	}
9107 	memset(ctxm, 0, sizeof(*ctxm));
9108 }
9109 
9110 void bnxt_free_ctx_mem(struct bnxt *bp, bool force)
9111 {
9112 	struct bnxt_ctx_mem_info *ctx = bp->ctx;
9113 	u16 type;
9114 
9115 	if (!ctx)
9116 		return;
9117 
9118 	for (type = 0; type < BNXT_CTX_V2_MAX; type++)
9119 		bnxt_free_one_ctx_mem(bp, &ctx->ctx_arr[type], force);
9120 
9121 	ctx->flags &= ~BNXT_CTX_FLAG_INITED;
9122 	if (force) {
9123 		kfree(ctx);
9124 		bp->ctx = NULL;
9125 	}
9126 }
9127 
9128 static int bnxt_alloc_ctx_mem(struct bnxt *bp)
9129 {
9130 	struct bnxt_ctx_mem_type *ctxm;
9131 	struct bnxt_ctx_mem_info *ctx;
9132 	u32 l2_qps, qp1_qps, max_qps;
9133 	u32 ena, entries_sp, entries;
9134 	u32 srqs, max_srqs, min;
9135 	u32 num_mr, num_ah;
9136 	u32 extra_srqs = 0;
9137 	u32 extra_qps = 0;
9138 	u32 fast_qpmd_qps;
9139 	u8 pg_lvl = 1;
9140 	int i, rc;
9141 
9142 	rc = bnxt_hwrm_func_backing_store_qcaps(bp);
9143 	if (rc) {
9144 		netdev_err(bp->dev, "Failed querying context mem capability, rc = %d.\n",
9145 			   rc);
9146 		return rc;
9147 	}
9148 	ctx = bp->ctx;
9149 	if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
9150 		return 0;
9151 
9152 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9153 	l2_qps = ctxm->qp_l2_entries;
9154 	qp1_qps = ctxm->qp_qp1_entries;
9155 	fast_qpmd_qps = ctxm->qp_fast_qpmd_entries;
9156 	max_qps = ctxm->max_entries;
9157 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9158 	srqs = ctxm->srq_l2_entries;
9159 	max_srqs = ctxm->max_entries;
9160 	ena = 0;
9161 	if ((bp->flags & BNXT_FLAG_ROCE_CAP) && !is_kdump_kernel()) {
9162 		pg_lvl = 2;
9163 		extra_qps = min_t(u32, 65536, max_qps - l2_qps - qp1_qps);
9164 		/* allocate extra qps if fw supports RoCE fast qp destroy feature */
9165 		extra_qps += fast_qpmd_qps;
9166 		extra_srqs = min_t(u32, 8192, max_srqs - srqs);
9167 		if (fast_qpmd_qps)
9168 			ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_QP_FAST_QPMD;
9169 	}
9170 
9171 	ctxm = &ctx->ctx_arr[BNXT_CTX_QP];
9172 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps,
9173 				     pg_lvl);
9174 	if (rc)
9175 		return rc;
9176 
9177 	ctxm = &ctx->ctx_arr[BNXT_CTX_SRQ];
9178 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, srqs + extra_srqs, pg_lvl);
9179 	if (rc)
9180 		return rc;
9181 
9182 	ctxm = &ctx->ctx_arr[BNXT_CTX_CQ];
9183 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->cq_l2_entries +
9184 				     extra_qps * 2, pg_lvl);
9185 	if (rc)
9186 		return rc;
9187 
9188 	ctxm = &ctx->ctx_arr[BNXT_CTX_VNIC];
9189 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9190 	if (rc)
9191 		return rc;
9192 
9193 	ctxm = &ctx->ctx_arr[BNXT_CTX_STAT];
9194 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, ctxm->max_entries, 1);
9195 	if (rc)
9196 		return rc;
9197 
9198 	if (!(bp->flags & BNXT_FLAG_ROCE_CAP))
9199 		goto skip_rdma;
9200 
9201 	ctxm = &ctx->ctx_arr[BNXT_CTX_MRAV];
9202 	/* 128K extra is needed to accommodate static AH context
9203 	 * allocation by f/w.
9204 	 */
9205 	num_mr = min_t(u32, ctxm->max_entries / 2, 1024 * 256);
9206 	num_ah = min_t(u32, num_mr, 1024 * 128);
9207 	ctxm->split_entry_cnt = BNXT_CTX_MRAV_AV_SPLIT_ENTRY + 1;
9208 	if (!ctxm->mrav_av_entries || ctxm->mrav_av_entries > num_ah)
9209 		ctxm->mrav_av_entries = num_ah;
9210 
9211 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, num_mr + num_ah, 2);
9212 	if (rc)
9213 		return rc;
9214 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_MRAV;
9215 
9216 	ctxm = &ctx->ctx_arr[BNXT_CTX_TIM];
9217 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, l2_qps + qp1_qps + extra_qps, 1);
9218 	if (rc)
9219 		return rc;
9220 	ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TIM;
9221 
9222 skip_rdma:
9223 	ctxm = &ctx->ctx_arr[BNXT_CTX_STQM];
9224 	min = ctxm->min_entries;
9225 	entries_sp = ctx->ctx_arr[BNXT_CTX_VNIC].vnic_entries + l2_qps +
9226 		     2 * (extra_qps + qp1_qps) + min;
9227 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries_sp, 2);
9228 	if (rc)
9229 		return rc;
9230 
9231 	ctxm = &ctx->ctx_arr[BNXT_CTX_FTQM];
9232 	entries = l2_qps + 2 * (extra_qps + qp1_qps);
9233 	rc = bnxt_setup_ctxm_pg_tbls(bp, ctxm, entries, 2);
9234 	if (rc)
9235 		return rc;
9236 	for (i = 0; i < ctx->tqm_fp_rings_count + 1; i++)
9237 		ena |= FUNC_BACKING_STORE_CFG_REQ_ENABLES_TQM_SP << i;
9238 	ena |= FUNC_BACKING_STORE_CFG_REQ_DFLT_ENABLES;
9239 
9240 	if (bp->fw_cap & BNXT_FW_CAP_BACKING_STORE_V2)
9241 		rc = bnxt_backing_store_cfg_v2(bp, ena);
9242 	else
9243 		rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
9244 	if (rc) {
9245 		netdev_err(bp->dev, "Failed configuring context mem, rc = %d.\n",
9246 			   rc);
9247 		return rc;
9248 	}
9249 	ctx->flags |= BNXT_CTX_FLAG_INITED;
9250 	return 0;
9251 }
9252 
9253 static int bnxt_hwrm_crash_dump_mem_cfg(struct bnxt *bp)
9254 {
9255 	struct hwrm_dbg_crashdump_medium_cfg_input *req;
9256 	u16 page_attr;
9257 	int rc;
9258 
9259 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9260 		return 0;
9261 
9262 	rc = hwrm_req_init(bp, req, HWRM_DBG_CRASHDUMP_MEDIUM_CFG);
9263 	if (rc)
9264 		return rc;
9265 
9266 	if (BNXT_PAGE_SIZE == 0x2000)
9267 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_8K;
9268 	else if (BNXT_PAGE_SIZE == 0x10000)
9269 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_64K;
9270 	else
9271 		page_attr = DBG_CRASHDUMP_MEDIUM_CFG_REQ_PG_SIZE_PG_4K;
9272 	req->pg_size_lvl = cpu_to_le16(page_attr |
9273 				       bp->fw_crash_mem->ring_mem.depth);
9274 	req->pbl = cpu_to_le64(bp->fw_crash_mem->ring_mem.pg_tbl_map);
9275 	req->size = cpu_to_le32(bp->fw_crash_len);
9276 	req->output_dest_flags = cpu_to_le16(BNXT_DBG_CR_DUMP_MDM_CFG_DDR);
9277 	return hwrm_req_send(bp, req);
9278 }
9279 
9280 static void bnxt_free_crash_dump_mem(struct bnxt *bp)
9281 {
9282 	if (bp->fw_crash_mem) {
9283 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9284 		kfree(bp->fw_crash_mem);
9285 		bp->fw_crash_mem = NULL;
9286 	}
9287 }
9288 
9289 static int bnxt_alloc_crash_dump_mem(struct bnxt *bp)
9290 {
9291 	u32 mem_size = 0;
9292 	int rc;
9293 
9294 	if (!(bp->fw_dbg_cap & DBG_QCAPS_RESP_FLAGS_CRASHDUMP_HOST_DDR))
9295 		return 0;
9296 
9297 	rc = bnxt_hwrm_get_dump_len(bp, BNXT_DUMP_CRASH, &mem_size);
9298 	if (rc)
9299 		return rc;
9300 
9301 	mem_size = round_up(mem_size, 4);
9302 
9303 	/* keep and use the existing pages */
9304 	if (bp->fw_crash_mem &&
9305 	    mem_size <= bp->fw_crash_mem->nr_pages * BNXT_PAGE_SIZE)
9306 		goto alloc_done;
9307 
9308 	if (bp->fw_crash_mem)
9309 		bnxt_free_ctx_pg_tbls(bp, bp->fw_crash_mem);
9310 	else
9311 		bp->fw_crash_mem = kzalloc(sizeof(*bp->fw_crash_mem),
9312 					   GFP_KERNEL);
9313 	if (!bp->fw_crash_mem)
9314 		return -ENOMEM;
9315 
9316 	rc = bnxt_alloc_ctx_pg_tbls(bp, bp->fw_crash_mem, mem_size, 1, NULL);
9317 	if (rc) {
9318 		bnxt_free_crash_dump_mem(bp);
9319 		return rc;
9320 	}
9321 
9322 alloc_done:
9323 	bp->fw_crash_len = mem_size;
9324 	return 0;
9325 }
9326 
9327 int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
9328 {
9329 	struct hwrm_func_resource_qcaps_output *resp;
9330 	struct hwrm_func_resource_qcaps_input *req;
9331 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9332 	int rc;
9333 
9334 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESOURCE_QCAPS);
9335 	if (rc)
9336 		return rc;
9337 
9338 	req->fid = cpu_to_le16(0xffff);
9339 	resp = hwrm_req_hold(bp, req);
9340 	rc = hwrm_req_send_silent(bp, req);
9341 	if (rc)
9342 		goto hwrm_func_resc_qcaps_exit;
9343 
9344 	hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
9345 	if (!all)
9346 		goto hwrm_func_resc_qcaps_exit;
9347 
9348 	hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
9349 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9350 	hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
9351 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9352 	hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
9353 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9354 	hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
9355 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9356 	hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
9357 	hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
9358 	hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
9359 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9360 	hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
9361 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9362 	hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
9363 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9364 
9365 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
9366 		u16 max_msix = le16_to_cpu(resp->max_msix);
9367 
9368 		hw_resc->max_nqs = max_msix;
9369 		hw_resc->max_hw_ring_grps = hw_resc->max_rx_rings;
9370 	}
9371 
9372 	if (BNXT_PF(bp)) {
9373 		struct bnxt_pf_info *pf = &bp->pf;
9374 
9375 		pf->vf_resv_strategy =
9376 			le16_to_cpu(resp->vf_reservation_strategy);
9377 		if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
9378 			pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
9379 	}
9380 hwrm_func_resc_qcaps_exit:
9381 	hwrm_req_drop(bp, req);
9382 	return rc;
9383 }
9384 
9385 static int __bnxt_hwrm_ptp_qcfg(struct bnxt *bp)
9386 {
9387 	struct hwrm_port_mac_ptp_qcfg_output *resp;
9388 	struct hwrm_port_mac_ptp_qcfg_input *req;
9389 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
9390 	u8 flags;
9391 	int rc;
9392 
9393 	if (bp->hwrm_spec_code < 0x10801 || !BNXT_CHIP_P5_PLUS(bp)) {
9394 		rc = -ENODEV;
9395 		goto no_ptp;
9396 	}
9397 
9398 	rc = hwrm_req_init(bp, req, HWRM_PORT_MAC_PTP_QCFG);
9399 	if (rc)
9400 		goto no_ptp;
9401 
9402 	req->port_id = cpu_to_le16(bp->pf.port_id);
9403 	resp = hwrm_req_hold(bp, req);
9404 	rc = hwrm_req_send(bp, req);
9405 	if (rc)
9406 		goto exit;
9407 
9408 	flags = resp->flags;
9409 	if (BNXT_CHIP_P5_AND_MINUS(bp) &&
9410 	    !(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_HWRM_ACCESS)) {
9411 		rc = -ENODEV;
9412 		goto exit;
9413 	}
9414 	if (!ptp) {
9415 		ptp = kzalloc(sizeof(*ptp), GFP_KERNEL);
9416 		if (!ptp) {
9417 			rc = -ENOMEM;
9418 			goto exit;
9419 		}
9420 		ptp->bp = bp;
9421 		bp->ptp_cfg = ptp;
9422 	}
9423 
9424 	if (flags &
9425 	    (PORT_MAC_PTP_QCFG_RESP_FLAGS_PARTIAL_DIRECT_ACCESS_REF_CLOCK |
9426 	     PORT_MAC_PTP_QCFG_RESP_FLAGS_64B_PHC_TIME)) {
9427 		ptp->refclk_regs[0] = le32_to_cpu(resp->ts_ref_clock_reg_lower);
9428 		ptp->refclk_regs[1] = le32_to_cpu(resp->ts_ref_clock_reg_upper);
9429 	} else if (BNXT_CHIP_P5(bp)) {
9430 		ptp->refclk_regs[0] = BNXT_TS_REG_TIMESYNC_TS0_LOWER;
9431 		ptp->refclk_regs[1] = BNXT_TS_REG_TIMESYNC_TS0_UPPER;
9432 	} else {
9433 		rc = -ENODEV;
9434 		goto exit;
9435 	}
9436 	ptp->rtc_configured =
9437 		(flags & PORT_MAC_PTP_QCFG_RESP_FLAGS_RTC_CONFIGURED) != 0;
9438 	rc = bnxt_ptp_init(bp);
9439 	if (rc)
9440 		netdev_warn(bp->dev, "PTP initialization failed.\n");
9441 exit:
9442 	hwrm_req_drop(bp, req);
9443 	if (!rc)
9444 		return 0;
9445 
9446 no_ptp:
9447 	bnxt_ptp_clear(bp);
9448 	kfree(ptp);
9449 	bp->ptp_cfg = NULL;
9450 	return rc;
9451 }
9452 
9453 static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
9454 {
9455 	struct hwrm_func_qcaps_output *resp;
9456 	struct hwrm_func_qcaps_input *req;
9457 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
9458 	u32 flags, flags_ext, flags_ext2;
9459 	int rc;
9460 
9461 	rc = hwrm_req_init(bp, req, HWRM_FUNC_QCAPS);
9462 	if (rc)
9463 		return rc;
9464 
9465 	req->fid = cpu_to_le16(0xffff);
9466 	resp = hwrm_req_hold(bp, req);
9467 	rc = hwrm_req_send(bp, req);
9468 	if (rc)
9469 		goto hwrm_func_qcaps_exit;
9470 
9471 	flags = le32_to_cpu(resp->flags);
9472 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
9473 		bp->flags |= BNXT_FLAG_ROCEV1_CAP;
9474 	if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
9475 		bp->flags |= BNXT_FLAG_ROCEV2_CAP;
9476 	if (flags & FUNC_QCAPS_RESP_FLAGS_PCIE_STATS_SUPPORTED)
9477 		bp->fw_cap |= BNXT_FW_CAP_PCIE_STATS_SUPPORTED;
9478 	if (flags & FUNC_QCAPS_RESP_FLAGS_HOT_RESET_CAPABLE)
9479 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET;
9480 	if (flags & FUNC_QCAPS_RESP_FLAGS_EXT_STATS_SUPPORTED)
9481 		bp->fw_cap |= BNXT_FW_CAP_EXT_STATS_SUPPORTED;
9482 	if (flags &  FUNC_QCAPS_RESP_FLAGS_ERROR_RECOVERY_CAPABLE)
9483 		bp->fw_cap |= BNXT_FW_CAP_ERROR_RECOVERY;
9484 	if (flags & FUNC_QCAPS_RESP_FLAGS_ERR_RECOVER_RELOAD)
9485 		bp->fw_cap |= BNXT_FW_CAP_ERR_RECOVER_RELOAD;
9486 	if (!(flags & FUNC_QCAPS_RESP_FLAGS_VLAN_ACCELERATION_TX_DISABLED))
9487 		bp->fw_cap |= BNXT_FW_CAP_VLAN_TX_INSERT;
9488 	if (flags & FUNC_QCAPS_RESP_FLAGS_DBG_QCAPS_CMD_SUPPORTED)
9489 		bp->fw_cap |= BNXT_FW_CAP_DBG_QCAPS;
9490 
9491 	flags_ext = le32_to_cpu(resp->flags_ext);
9492 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_EXT_HW_STATS_SUPPORTED)
9493 		bp->fw_cap |= BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED;
9494 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_PPS_SUPPORTED))
9495 		bp->fw_cap |= BNXT_FW_CAP_PTP_PPS;
9496 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_PTP_64BIT_RTC_SUPPORTED)
9497 		bp->fw_cap |= BNXT_FW_CAP_PTP_RTC;
9498 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_HOT_RESET_IF_SUPPORT))
9499 		bp->fw_cap |= BNXT_FW_CAP_HOT_RESET_IF;
9500 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_FW_LIVEPATCH_SUPPORTED))
9501 		bp->fw_cap |= BNXT_FW_CAP_LIVEPATCH;
9502 	if (BNXT_PF(bp) && (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_DFLT_VLAN_TPID_PCP_SUPPORTED))
9503 		bp->fw_cap |= BNXT_FW_CAP_DFLT_VLAN_TPID_PCP;
9504 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_BS_V2_SUPPORTED)
9505 		bp->fw_cap |= BNXT_FW_CAP_BACKING_STORE_V2;
9506 	if (flags_ext & FUNC_QCAPS_RESP_FLAGS_EXT_TX_COAL_CMPL_CAP)
9507 		bp->flags |= BNXT_FLAG_TX_COAL_CMPL;
9508 
9509 	flags_ext2 = le32_to_cpu(resp->flags_ext2);
9510 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_RX_ALL_PKTS_TIMESTAMPS_SUPPORTED)
9511 		bp->fw_cap |= BNXT_FW_CAP_RX_ALL_PKT_TS;
9512 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_UDP_GSO_SUPPORTED)
9513 		bp->flags |= BNXT_FLAG_UDP_GSO_CAP;
9514 	if (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_TX_PKT_TS_CMPL_SUPPORTED)
9515 		bp->fw_cap |= BNXT_FW_CAP_TX_TS_CMP;
9516 	if (BNXT_PF(bp) &&
9517 	    (flags_ext2 & FUNC_QCAPS_RESP_FLAGS_EXT2_ROCE_VF_RESOURCE_MGMT_SUPPORTED))
9518 		bp->fw_cap |= BNXT_FW_CAP_ROCE_VF_RESC_MGMT_SUPPORTED;
9519 
9520 	bp->tx_push_thresh = 0;
9521 	if ((flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED) &&
9522 	    BNXT_FW_MAJ(bp) > 217)
9523 		bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
9524 
9525 	hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
9526 	hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
9527 	hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
9528 	hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
9529 	hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
9530 	if (!hw_resc->max_hw_ring_grps)
9531 		hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
9532 	hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
9533 	hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
9534 	hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
9535 
9536 	hw_resc->max_encap_records = le32_to_cpu(resp->max_encap_records);
9537 	hw_resc->max_decap_records = le32_to_cpu(resp->max_decap_records);
9538 	hw_resc->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
9539 	hw_resc->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
9540 	hw_resc->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
9541 	hw_resc->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
9542 
9543 	if (BNXT_PF(bp)) {
9544 		struct bnxt_pf_info *pf = &bp->pf;
9545 
9546 		pf->fw_fid = le16_to_cpu(resp->fid);
9547 		pf->port_id = le16_to_cpu(resp->port_id);
9548 		memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
9549 		pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
9550 		pf->max_vfs = le16_to_cpu(resp->max_vfs);
9551 		bp->flags &= ~BNXT_FLAG_WOL_CAP;
9552 		if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
9553 			bp->flags |= BNXT_FLAG_WOL_CAP;
9554 		if (flags & FUNC_QCAPS_RESP_FLAGS_PTP_SUPPORTED) {
9555 			bp->fw_cap |= BNXT_FW_CAP_PTP;
9556 		} else {
9557 			bnxt_ptp_clear(bp);
9558 			kfree(bp->ptp_cfg);
9559 			bp->ptp_cfg = NULL;
9560 		}
9561 	} else {
9562 #ifdef CONFIG_BNXT_SRIOV
9563 		struct bnxt_vf_info *vf = &bp->vf;
9564 
9565 		vf->fw_fid = le16_to_cpu(resp->fid);
9566 		memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
9567 #endif
9568 	}
9569 	bp->tso_max_segs = le16_to_cpu(resp->max_tso_segs);
9570 
9571 hwrm_func_qcaps_exit:
9572 	hwrm_req_drop(bp, req);
9573 	return rc;
9574 }
9575 
9576 static void bnxt_hwrm_dbg_qcaps(struct bnxt *bp)
9577 {
9578 	struct hwrm_dbg_qcaps_output *resp;
9579 	struct hwrm_dbg_qcaps_input *req;
9580 	int rc;
9581 
9582 	bp->fw_dbg_cap = 0;
9583 	if (!(bp->fw_cap & BNXT_FW_CAP_DBG_QCAPS))
9584 		return;
9585 
9586 	rc = hwrm_req_init(bp, req, HWRM_DBG_QCAPS);
9587 	if (rc)
9588 		return;
9589 
9590 	req->fid = cpu_to_le16(0xffff);
9591 	resp = hwrm_req_hold(bp, req);
9592 	rc = hwrm_req_send(bp, req);
9593 	if (rc)
9594 		goto hwrm_dbg_qcaps_exit;
9595 
9596 	bp->fw_dbg_cap = le32_to_cpu(resp->flags);
9597 
9598 hwrm_dbg_qcaps_exit:
9599 	hwrm_req_drop(bp, req);
9600 }
9601 
9602 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp);
9603 
9604 int bnxt_hwrm_func_qcaps(struct bnxt *bp)
9605 {
9606 	int rc;
9607 
9608 	rc = __bnxt_hwrm_func_qcaps(bp);
9609 	if (rc)
9610 		return rc;
9611 
9612 	bnxt_hwrm_dbg_qcaps(bp);
9613 
9614 	rc = bnxt_hwrm_queue_qportcfg(bp);
9615 	if (rc) {
9616 		netdev_err(bp->dev, "hwrm query qportcfg failure rc: %d\n", rc);
9617 		return rc;
9618 	}
9619 	if (bp->hwrm_spec_code >= 0x10803) {
9620 		rc = bnxt_alloc_ctx_mem(bp);
9621 		if (rc)
9622 			return rc;
9623 		rc = bnxt_hwrm_func_resc_qcaps(bp, true);
9624 		if (!rc)
9625 			bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
9626 	}
9627 	return 0;
9628 }
9629 
9630 static int bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(struct bnxt *bp)
9631 {
9632 	struct hwrm_cfa_adv_flow_mgnt_qcaps_output *resp;
9633 	struct hwrm_cfa_adv_flow_mgnt_qcaps_input *req;
9634 	u32 flags;
9635 	int rc;
9636 
9637 	if (!(bp->fw_cap & BNXT_FW_CAP_CFA_ADV_FLOW))
9638 		return 0;
9639 
9640 	rc = hwrm_req_init(bp, req, HWRM_CFA_ADV_FLOW_MGNT_QCAPS);
9641 	if (rc)
9642 		return rc;
9643 
9644 	resp = hwrm_req_hold(bp, req);
9645 	rc = hwrm_req_send(bp, req);
9646 	if (rc)
9647 		goto hwrm_cfa_adv_qcaps_exit;
9648 
9649 	flags = le32_to_cpu(resp->flags);
9650 	if (flags &
9651 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V2_SUPPORTED)
9652 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2;
9653 
9654 	if (flags &
9655 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_RFS_RING_TBL_IDX_V3_SUPPORTED)
9656 		bp->fw_cap |= BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V3;
9657 
9658 	if (flags &
9659 	    CFA_ADV_FLOW_MGNT_QCAPS_RESP_FLAGS_NTUPLE_FLOW_RX_EXT_IP_PROTO_SUPPORTED)
9660 		bp->fw_cap |= BNXT_FW_CAP_CFA_NTUPLE_RX_EXT_IP_PROTO;
9661 
9662 hwrm_cfa_adv_qcaps_exit:
9663 	hwrm_req_drop(bp, req);
9664 	return rc;
9665 }
9666 
9667 static int __bnxt_alloc_fw_health(struct bnxt *bp)
9668 {
9669 	if (bp->fw_health)
9670 		return 0;
9671 
9672 	bp->fw_health = kzalloc(sizeof(*bp->fw_health), GFP_KERNEL);
9673 	if (!bp->fw_health)
9674 		return -ENOMEM;
9675 
9676 	mutex_init(&bp->fw_health->lock);
9677 	return 0;
9678 }
9679 
9680 static int bnxt_alloc_fw_health(struct bnxt *bp)
9681 {
9682 	int rc;
9683 
9684 	if (!(bp->fw_cap & BNXT_FW_CAP_HOT_RESET) &&
9685 	    !(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9686 		return 0;
9687 
9688 	rc = __bnxt_alloc_fw_health(bp);
9689 	if (rc) {
9690 		bp->fw_cap &= ~BNXT_FW_CAP_HOT_RESET;
9691 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9692 		return rc;
9693 	}
9694 
9695 	return 0;
9696 }
9697 
9698 static void __bnxt_map_fw_health_reg(struct bnxt *bp, u32 reg)
9699 {
9700 	writel(reg & BNXT_GRC_BASE_MASK, bp->bar0 +
9701 					 BNXT_GRCPF_REG_WINDOW_BASE_OUT +
9702 					 BNXT_FW_HEALTH_WIN_MAP_OFF);
9703 }
9704 
9705 static void bnxt_inv_fw_health_reg(struct bnxt *bp)
9706 {
9707 	struct bnxt_fw_health *fw_health = bp->fw_health;
9708 	u32 reg_type;
9709 
9710 	if (!fw_health)
9711 		return;
9712 
9713 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_HEALTH_REG]);
9714 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9715 		fw_health->status_reliable = false;
9716 
9717 	reg_type = BNXT_FW_HEALTH_REG_TYPE(fw_health->regs[BNXT_FW_RESET_CNT_REG]);
9718 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC)
9719 		fw_health->resets_reliable = false;
9720 }
9721 
9722 static void bnxt_try_map_fw_health_reg(struct bnxt *bp)
9723 {
9724 	void __iomem *hs;
9725 	u32 status_loc;
9726 	u32 reg_type;
9727 	u32 sig;
9728 
9729 	if (bp->fw_health)
9730 		bp->fw_health->status_reliable = false;
9731 
9732 	__bnxt_map_fw_health_reg(bp, HCOMM_STATUS_STRUCT_LOC);
9733 	hs = bp->bar0 + BNXT_FW_HEALTH_WIN_OFF(HCOMM_STATUS_STRUCT_LOC);
9734 
9735 	sig = readl(hs + offsetof(struct hcomm_status, sig_ver));
9736 	if ((sig & HCOMM_STATUS_SIGNATURE_MASK) != HCOMM_STATUS_SIGNATURE_VAL) {
9737 		if (!bp->chip_num) {
9738 			__bnxt_map_fw_health_reg(bp, BNXT_GRC_REG_BASE);
9739 			bp->chip_num = readl(bp->bar0 +
9740 					     BNXT_FW_HEALTH_WIN_BASE +
9741 					     BNXT_GRC_REG_CHIP_NUM);
9742 		}
9743 		if (!BNXT_CHIP_P5_PLUS(bp))
9744 			return;
9745 
9746 		status_loc = BNXT_GRC_REG_STATUS_P5 |
9747 			     BNXT_FW_HEALTH_REG_TYPE_BAR0;
9748 	} else {
9749 		status_loc = readl(hs + offsetof(struct hcomm_status,
9750 						 fw_status_loc));
9751 	}
9752 
9753 	if (__bnxt_alloc_fw_health(bp)) {
9754 		netdev_warn(bp->dev, "no memory for firmware status checks\n");
9755 		return;
9756 	}
9757 
9758 	bp->fw_health->regs[BNXT_FW_HEALTH_REG] = status_loc;
9759 	reg_type = BNXT_FW_HEALTH_REG_TYPE(status_loc);
9760 	if (reg_type == BNXT_FW_HEALTH_REG_TYPE_GRC) {
9761 		__bnxt_map_fw_health_reg(bp, status_loc);
9762 		bp->fw_health->mapped_regs[BNXT_FW_HEALTH_REG] =
9763 			BNXT_FW_HEALTH_WIN_OFF(status_loc);
9764 	}
9765 
9766 	bp->fw_health->status_reliable = true;
9767 }
9768 
9769 static int bnxt_map_fw_health_regs(struct bnxt *bp)
9770 {
9771 	struct bnxt_fw_health *fw_health = bp->fw_health;
9772 	u32 reg_base = 0xffffffff;
9773 	int i;
9774 
9775 	bp->fw_health->status_reliable = false;
9776 	bp->fw_health->resets_reliable = false;
9777 	/* Only pre-map the monitoring GRC registers using window 3 */
9778 	for (i = 0; i < 4; i++) {
9779 		u32 reg = fw_health->regs[i];
9780 
9781 		if (BNXT_FW_HEALTH_REG_TYPE(reg) != BNXT_FW_HEALTH_REG_TYPE_GRC)
9782 			continue;
9783 		if (reg_base == 0xffffffff)
9784 			reg_base = reg & BNXT_GRC_BASE_MASK;
9785 		if ((reg & BNXT_GRC_BASE_MASK) != reg_base)
9786 			return -ERANGE;
9787 		fw_health->mapped_regs[i] = BNXT_FW_HEALTH_WIN_OFF(reg);
9788 	}
9789 	bp->fw_health->status_reliable = true;
9790 	bp->fw_health->resets_reliable = true;
9791 	if (reg_base == 0xffffffff)
9792 		return 0;
9793 
9794 	__bnxt_map_fw_health_reg(bp, reg_base);
9795 	return 0;
9796 }
9797 
9798 static void bnxt_remap_fw_health_regs(struct bnxt *bp)
9799 {
9800 	if (!bp->fw_health)
9801 		return;
9802 
9803 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) {
9804 		bp->fw_health->status_reliable = true;
9805 		bp->fw_health->resets_reliable = true;
9806 	} else {
9807 		bnxt_try_map_fw_health_reg(bp);
9808 	}
9809 }
9810 
9811 static int bnxt_hwrm_error_recovery_qcfg(struct bnxt *bp)
9812 {
9813 	struct bnxt_fw_health *fw_health = bp->fw_health;
9814 	struct hwrm_error_recovery_qcfg_output *resp;
9815 	struct hwrm_error_recovery_qcfg_input *req;
9816 	int rc, i;
9817 
9818 	if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
9819 		return 0;
9820 
9821 	rc = hwrm_req_init(bp, req, HWRM_ERROR_RECOVERY_QCFG);
9822 	if (rc)
9823 		return rc;
9824 
9825 	resp = hwrm_req_hold(bp, req);
9826 	rc = hwrm_req_send(bp, req);
9827 	if (rc)
9828 		goto err_recovery_out;
9829 	fw_health->flags = le32_to_cpu(resp->flags);
9830 	if ((fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) &&
9831 	    !(bp->fw_cap & BNXT_FW_CAP_KONG_MB_CHNL)) {
9832 		rc = -EINVAL;
9833 		goto err_recovery_out;
9834 	}
9835 	fw_health->polling_dsecs = le32_to_cpu(resp->driver_polling_freq);
9836 	fw_health->master_func_wait_dsecs =
9837 		le32_to_cpu(resp->master_func_wait_period);
9838 	fw_health->normal_func_wait_dsecs =
9839 		le32_to_cpu(resp->normal_func_wait_period);
9840 	fw_health->post_reset_wait_dsecs =
9841 		le32_to_cpu(resp->master_func_wait_period_after_reset);
9842 	fw_health->post_reset_max_wait_dsecs =
9843 		le32_to_cpu(resp->max_bailout_time_after_reset);
9844 	fw_health->regs[BNXT_FW_HEALTH_REG] =
9845 		le32_to_cpu(resp->fw_health_status_reg);
9846 	fw_health->regs[BNXT_FW_HEARTBEAT_REG] =
9847 		le32_to_cpu(resp->fw_heartbeat_reg);
9848 	fw_health->regs[BNXT_FW_RESET_CNT_REG] =
9849 		le32_to_cpu(resp->fw_reset_cnt_reg);
9850 	fw_health->regs[BNXT_FW_RESET_INPROG_REG] =
9851 		le32_to_cpu(resp->reset_inprogress_reg);
9852 	fw_health->fw_reset_inprog_reg_mask =
9853 		le32_to_cpu(resp->reset_inprogress_reg_mask);
9854 	fw_health->fw_reset_seq_cnt = resp->reg_array_cnt;
9855 	if (fw_health->fw_reset_seq_cnt >= 16) {
9856 		rc = -EINVAL;
9857 		goto err_recovery_out;
9858 	}
9859 	for (i = 0; i < fw_health->fw_reset_seq_cnt; i++) {
9860 		fw_health->fw_reset_seq_regs[i] =
9861 			le32_to_cpu(resp->reset_reg[i]);
9862 		fw_health->fw_reset_seq_vals[i] =
9863 			le32_to_cpu(resp->reset_reg_val[i]);
9864 		fw_health->fw_reset_seq_delay_msec[i] =
9865 			resp->delay_after_reset[i];
9866 	}
9867 err_recovery_out:
9868 	hwrm_req_drop(bp, req);
9869 	if (!rc)
9870 		rc = bnxt_map_fw_health_regs(bp);
9871 	if (rc)
9872 		bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
9873 	return rc;
9874 }
9875 
9876 static int bnxt_hwrm_func_reset(struct bnxt *bp)
9877 {
9878 	struct hwrm_func_reset_input *req;
9879 	int rc;
9880 
9881 	rc = hwrm_req_init(bp, req, HWRM_FUNC_RESET);
9882 	if (rc)
9883 		return rc;
9884 
9885 	req->enables = 0;
9886 	hwrm_req_timeout(bp, req, HWRM_RESET_TIMEOUT);
9887 	return hwrm_req_send(bp, req);
9888 }
9889 
9890 static void bnxt_nvm_cfg_ver_get(struct bnxt *bp)
9891 {
9892 	struct hwrm_nvm_get_dev_info_output nvm_info;
9893 
9894 	if (!bnxt_hwrm_nvm_get_dev_info(bp, &nvm_info))
9895 		snprintf(bp->nvm_cfg_ver, FW_VER_STR_LEN, "%d.%d.%d",
9896 			 nvm_info.nvm_cfg_ver_maj, nvm_info.nvm_cfg_ver_min,
9897 			 nvm_info.nvm_cfg_ver_upd);
9898 }
9899 
9900 static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
9901 {
9902 	struct hwrm_queue_qportcfg_output *resp;
9903 	struct hwrm_queue_qportcfg_input *req;
9904 	u8 i, j, *qptr;
9905 	bool no_rdma;
9906 	int rc = 0;
9907 
9908 	rc = hwrm_req_init(bp, req, HWRM_QUEUE_QPORTCFG);
9909 	if (rc)
9910 		return rc;
9911 
9912 	resp = hwrm_req_hold(bp, req);
9913 	rc = hwrm_req_send(bp, req);
9914 	if (rc)
9915 		goto qportcfg_exit;
9916 
9917 	if (!resp->max_configurable_queues) {
9918 		rc = -EINVAL;
9919 		goto qportcfg_exit;
9920 	}
9921 	bp->max_tc = resp->max_configurable_queues;
9922 	bp->max_lltc = resp->max_configurable_lossless_queues;
9923 	if (bp->max_tc > BNXT_MAX_QUEUE)
9924 		bp->max_tc = BNXT_MAX_QUEUE;
9925 
9926 	no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
9927 	qptr = &resp->queue_id0;
9928 	for (i = 0, j = 0; i < bp->max_tc; i++) {
9929 		bp->q_info[j].queue_id = *qptr;
9930 		bp->q_ids[i] = *qptr++;
9931 		bp->q_info[j].queue_profile = *qptr++;
9932 		bp->tc_to_qidx[j] = j;
9933 		if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
9934 		    (no_rdma && BNXT_PF(bp)))
9935 			j++;
9936 	}
9937 	bp->max_q = bp->max_tc;
9938 	bp->max_tc = max_t(u8, j, 1);
9939 
9940 	if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
9941 		bp->max_tc = 1;
9942 
9943 	if (bp->max_lltc > bp->max_tc)
9944 		bp->max_lltc = bp->max_tc;
9945 
9946 qportcfg_exit:
9947 	hwrm_req_drop(bp, req);
9948 	return rc;
9949 }
9950 
9951 static int bnxt_hwrm_poll(struct bnxt *bp)
9952 {
9953 	struct hwrm_ver_get_input *req;
9954 	int rc;
9955 
9956 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9957 	if (rc)
9958 		return rc;
9959 
9960 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9961 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9962 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9963 
9964 	hwrm_req_flags(bp, req, BNXT_HWRM_CTX_SILENT | BNXT_HWRM_FULL_WAIT);
9965 	rc = hwrm_req_send(bp, req);
9966 	return rc;
9967 }
9968 
9969 static int bnxt_hwrm_ver_get(struct bnxt *bp)
9970 {
9971 	struct hwrm_ver_get_output *resp;
9972 	struct hwrm_ver_get_input *req;
9973 	u16 fw_maj, fw_min, fw_bld, fw_rsv;
9974 	u32 dev_caps_cfg, hwrm_ver;
9975 	int rc, len;
9976 
9977 	rc = hwrm_req_init(bp, req, HWRM_VER_GET);
9978 	if (rc)
9979 		return rc;
9980 
9981 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
9982 	bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
9983 	req->hwrm_intf_maj = HWRM_VERSION_MAJOR;
9984 	req->hwrm_intf_min = HWRM_VERSION_MINOR;
9985 	req->hwrm_intf_upd = HWRM_VERSION_UPDATE;
9986 
9987 	resp = hwrm_req_hold(bp, req);
9988 	rc = hwrm_req_send(bp, req);
9989 	if (rc)
9990 		goto hwrm_ver_get_exit;
9991 
9992 	memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
9993 
9994 	bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
9995 			     resp->hwrm_intf_min_8b << 8 |
9996 			     resp->hwrm_intf_upd_8b;
9997 	if (resp->hwrm_intf_maj_8b < 1) {
9998 		netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
9999 			    resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10000 			    resp->hwrm_intf_upd_8b);
10001 		netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
10002 	}
10003 
10004 	hwrm_ver = HWRM_VERSION_MAJOR << 16 | HWRM_VERSION_MINOR << 8 |
10005 			HWRM_VERSION_UPDATE;
10006 
10007 	if (bp->hwrm_spec_code > hwrm_ver)
10008 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10009 			 HWRM_VERSION_MAJOR, HWRM_VERSION_MINOR,
10010 			 HWRM_VERSION_UPDATE);
10011 	else
10012 		snprintf(bp->hwrm_ver_supp, FW_VER_STR_LEN, "%d.%d.%d",
10013 			 resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
10014 			 resp->hwrm_intf_upd_8b);
10015 
10016 	fw_maj = le16_to_cpu(resp->hwrm_fw_major);
10017 	if (bp->hwrm_spec_code > 0x10803 && fw_maj) {
10018 		fw_min = le16_to_cpu(resp->hwrm_fw_minor);
10019 		fw_bld = le16_to_cpu(resp->hwrm_fw_build);
10020 		fw_rsv = le16_to_cpu(resp->hwrm_fw_patch);
10021 		len = FW_VER_STR_LEN;
10022 	} else {
10023 		fw_maj = resp->hwrm_fw_maj_8b;
10024 		fw_min = resp->hwrm_fw_min_8b;
10025 		fw_bld = resp->hwrm_fw_bld_8b;
10026 		fw_rsv = resp->hwrm_fw_rsvd_8b;
10027 		len = BC_HWRM_STR_LEN;
10028 	}
10029 	bp->fw_ver_code = BNXT_FW_VER_CODE(fw_maj, fw_min, fw_bld, fw_rsv);
10030 	snprintf(bp->fw_ver_str, len, "%d.%d.%d.%d", fw_maj, fw_min, fw_bld,
10031 		 fw_rsv);
10032 
10033 	if (strlen(resp->active_pkg_name)) {
10034 		int fw_ver_len = strlen(bp->fw_ver_str);
10035 
10036 		snprintf(bp->fw_ver_str + fw_ver_len,
10037 			 FW_VER_STR_LEN - fw_ver_len - 1, "/pkg %s",
10038 			 resp->active_pkg_name);
10039 		bp->fw_cap |= BNXT_FW_CAP_PKG_VER;
10040 	}
10041 
10042 	bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
10043 	if (!bp->hwrm_cmd_timeout)
10044 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
10045 	bp->hwrm_cmd_max_timeout = le16_to_cpu(resp->max_req_timeout) * 1000;
10046 	if (!bp->hwrm_cmd_max_timeout)
10047 		bp->hwrm_cmd_max_timeout = HWRM_CMD_MAX_TIMEOUT;
10048 	else if (bp->hwrm_cmd_max_timeout > HWRM_CMD_MAX_TIMEOUT)
10049 		netdev_warn(bp->dev, "Device requests max timeout of %d seconds, may trigger hung task watchdog\n",
10050 			    bp->hwrm_cmd_max_timeout / 1000);
10051 
10052 	if (resp->hwrm_intf_maj_8b >= 1) {
10053 		bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
10054 		bp->hwrm_max_ext_req_len = le16_to_cpu(resp->max_ext_req_len);
10055 	}
10056 	if (bp->hwrm_max_ext_req_len < HWRM_MAX_REQ_LEN)
10057 		bp->hwrm_max_ext_req_len = HWRM_MAX_REQ_LEN;
10058 
10059 	bp->chip_num = le16_to_cpu(resp->chip_num);
10060 	bp->chip_rev = resp->chip_rev;
10061 	if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
10062 	    !resp->chip_metal)
10063 		bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
10064 
10065 	dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
10066 	if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
10067 	    (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
10068 		bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
10069 
10070 	if (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_KONG_MB_CHNL_SUPPORTED)
10071 		bp->fw_cap |= BNXT_FW_CAP_KONG_MB_CHNL;
10072 
10073 	if (dev_caps_cfg &
10074 	    VER_GET_RESP_DEV_CAPS_CFG_FLOW_HANDLE_64BIT_SUPPORTED)
10075 		bp->fw_cap |= BNXT_FW_CAP_OVS_64BIT_HANDLE;
10076 
10077 	if (dev_caps_cfg &
10078 	    VER_GET_RESP_DEV_CAPS_CFG_TRUSTED_VF_SUPPORTED)
10079 		bp->fw_cap |= BNXT_FW_CAP_TRUSTED_VF;
10080 
10081 	if (dev_caps_cfg &
10082 	    VER_GET_RESP_DEV_CAPS_CFG_CFA_ADV_FLOW_MGNT_SUPPORTED)
10083 		bp->fw_cap |= BNXT_FW_CAP_CFA_ADV_FLOW;
10084 
10085 hwrm_ver_get_exit:
10086 	hwrm_req_drop(bp, req);
10087 	return rc;
10088 }
10089 
10090 int bnxt_hwrm_fw_set_time(struct bnxt *bp)
10091 {
10092 	struct hwrm_fw_set_time_input *req;
10093 	struct tm tm;
10094 	time64_t now = ktime_get_real_seconds();
10095 	int rc;
10096 
10097 	if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
10098 	    bp->hwrm_spec_code < 0x10400)
10099 		return -EOPNOTSUPP;
10100 
10101 	time64_to_tm(now, 0, &tm);
10102 	rc = hwrm_req_init(bp, req, HWRM_FW_SET_TIME);
10103 	if (rc)
10104 		return rc;
10105 
10106 	req->year = cpu_to_le16(1900 + tm.tm_year);
10107 	req->month = 1 + tm.tm_mon;
10108 	req->day = tm.tm_mday;
10109 	req->hour = tm.tm_hour;
10110 	req->minute = tm.tm_min;
10111 	req->second = tm.tm_sec;
10112 	return hwrm_req_send(bp, req);
10113 }
10114 
10115 static void bnxt_add_one_ctr(u64 hw, u64 *sw, u64 mask)
10116 {
10117 	u64 sw_tmp;
10118 
10119 	hw &= mask;
10120 	sw_tmp = (*sw & ~mask) | hw;
10121 	if (hw < (*sw & mask))
10122 		sw_tmp += mask + 1;
10123 	WRITE_ONCE(*sw, sw_tmp);
10124 }
10125 
10126 static void __bnxt_accumulate_stats(__le64 *hw_stats, u64 *sw_stats, u64 *masks,
10127 				    int count, bool ignore_zero)
10128 {
10129 	int i;
10130 
10131 	for (i = 0; i < count; i++) {
10132 		u64 hw = le64_to_cpu(READ_ONCE(hw_stats[i]));
10133 
10134 		if (ignore_zero && !hw)
10135 			continue;
10136 
10137 		if (masks[i] == -1ULL)
10138 			sw_stats[i] = hw;
10139 		else
10140 			bnxt_add_one_ctr(hw, &sw_stats[i], masks[i]);
10141 	}
10142 }
10143 
10144 static void bnxt_accumulate_stats(struct bnxt_stats_mem *stats)
10145 {
10146 	if (!stats->hw_stats)
10147 		return;
10148 
10149 	__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10150 				stats->hw_masks, stats->len / 8, false);
10151 }
10152 
10153 static void bnxt_accumulate_all_stats(struct bnxt *bp)
10154 {
10155 	struct bnxt_stats_mem *ring0_stats;
10156 	bool ignore_zero = false;
10157 	int i;
10158 
10159 	/* Chip bug.  Counter intermittently becomes 0. */
10160 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10161 		ignore_zero = true;
10162 
10163 	for (i = 0; i < bp->cp_nr_rings; i++) {
10164 		struct bnxt_napi *bnapi = bp->bnapi[i];
10165 		struct bnxt_cp_ring_info *cpr;
10166 		struct bnxt_stats_mem *stats;
10167 
10168 		cpr = &bnapi->cp_ring;
10169 		stats = &cpr->stats;
10170 		if (!i)
10171 			ring0_stats = stats;
10172 		__bnxt_accumulate_stats(stats->hw_stats, stats->sw_stats,
10173 					ring0_stats->hw_masks,
10174 					ring0_stats->len / 8, ignore_zero);
10175 	}
10176 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
10177 		struct bnxt_stats_mem *stats = &bp->port_stats;
10178 		__le64 *hw_stats = stats->hw_stats;
10179 		u64 *sw_stats = stats->sw_stats;
10180 		u64 *masks = stats->hw_masks;
10181 		int cnt;
10182 
10183 		cnt = sizeof(struct rx_port_stats) / 8;
10184 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10185 
10186 		hw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10187 		sw_stats += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10188 		masks += BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
10189 		cnt = sizeof(struct tx_port_stats) / 8;
10190 		__bnxt_accumulate_stats(hw_stats, sw_stats, masks, cnt, false);
10191 	}
10192 	if (bp->flags & BNXT_FLAG_PORT_STATS_EXT) {
10193 		bnxt_accumulate_stats(&bp->rx_port_stats_ext);
10194 		bnxt_accumulate_stats(&bp->tx_port_stats_ext);
10195 	}
10196 }
10197 
10198 static int bnxt_hwrm_port_qstats(struct bnxt *bp, u8 flags)
10199 {
10200 	struct hwrm_port_qstats_input *req;
10201 	struct bnxt_pf_info *pf = &bp->pf;
10202 	int rc;
10203 
10204 	if (!(bp->flags & BNXT_FLAG_PORT_STATS))
10205 		return 0;
10206 
10207 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10208 		return -EOPNOTSUPP;
10209 
10210 	rc = hwrm_req_init(bp, req, HWRM_PORT_QSTATS);
10211 	if (rc)
10212 		return rc;
10213 
10214 	req->flags = flags;
10215 	req->port_id = cpu_to_le16(pf->port_id);
10216 	req->tx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map +
10217 					    BNXT_TX_PORT_STATS_BYTE_OFFSET);
10218 	req->rx_stat_host_addr = cpu_to_le64(bp->port_stats.hw_stats_map);
10219 	return hwrm_req_send(bp, req);
10220 }
10221 
10222 static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp, u8 flags)
10223 {
10224 	struct hwrm_queue_pri2cos_qcfg_output *resp_qc;
10225 	struct hwrm_queue_pri2cos_qcfg_input *req_qc;
10226 	struct hwrm_port_qstats_ext_output *resp_qs;
10227 	struct hwrm_port_qstats_ext_input *req_qs;
10228 	struct bnxt_pf_info *pf = &bp->pf;
10229 	u32 tx_stat_size;
10230 	int rc;
10231 
10232 	if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
10233 		return 0;
10234 
10235 	if (flags && !(bp->fw_cap & BNXT_FW_CAP_EXT_HW_STATS_SUPPORTED))
10236 		return -EOPNOTSUPP;
10237 
10238 	rc = hwrm_req_init(bp, req_qs, HWRM_PORT_QSTATS_EXT);
10239 	if (rc)
10240 		return rc;
10241 
10242 	req_qs->flags = flags;
10243 	req_qs->port_id = cpu_to_le16(pf->port_id);
10244 	req_qs->rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
10245 	req_qs->rx_stat_host_addr = cpu_to_le64(bp->rx_port_stats_ext.hw_stats_map);
10246 	tx_stat_size = bp->tx_port_stats_ext.hw_stats ?
10247 		       sizeof(struct tx_port_stats_ext) : 0;
10248 	req_qs->tx_stat_size = cpu_to_le16(tx_stat_size);
10249 	req_qs->tx_stat_host_addr = cpu_to_le64(bp->tx_port_stats_ext.hw_stats_map);
10250 	resp_qs = hwrm_req_hold(bp, req_qs);
10251 	rc = hwrm_req_send(bp, req_qs);
10252 	if (!rc) {
10253 		bp->fw_rx_stats_ext_size =
10254 			le16_to_cpu(resp_qs->rx_stat_size) / 8;
10255 		if (BNXT_FW_MAJ(bp) < 220 &&
10256 		    bp->fw_rx_stats_ext_size > BNXT_RX_STATS_EXT_NUM_LEGACY)
10257 			bp->fw_rx_stats_ext_size = BNXT_RX_STATS_EXT_NUM_LEGACY;
10258 
10259 		bp->fw_tx_stats_ext_size = tx_stat_size ?
10260 			le16_to_cpu(resp_qs->tx_stat_size) / 8 : 0;
10261 	} else {
10262 		bp->fw_rx_stats_ext_size = 0;
10263 		bp->fw_tx_stats_ext_size = 0;
10264 	}
10265 	hwrm_req_drop(bp, req_qs);
10266 
10267 	if (flags)
10268 		return rc;
10269 
10270 	if (bp->fw_tx_stats_ext_size <=
10271 	    offsetof(struct tx_port_stats_ext, pfc_pri0_tx_duration_us) / 8) {
10272 		bp->pri2cos_valid = 0;
10273 		return rc;
10274 	}
10275 
10276 	rc = hwrm_req_init(bp, req_qc, HWRM_QUEUE_PRI2COS_QCFG);
10277 	if (rc)
10278 		return rc;
10279 
10280 	req_qc->flags = cpu_to_le32(QUEUE_PRI2COS_QCFG_REQ_FLAGS_IVLAN);
10281 
10282 	resp_qc = hwrm_req_hold(bp, req_qc);
10283 	rc = hwrm_req_send(bp, req_qc);
10284 	if (!rc) {
10285 		u8 *pri2cos;
10286 		int i, j;
10287 
10288 		pri2cos = &resp_qc->pri0_cos_queue_id;
10289 		for (i = 0; i < 8; i++) {
10290 			u8 queue_id = pri2cos[i];
10291 			u8 queue_idx;
10292 
10293 			/* Per port queue IDs start from 0, 10, 20, etc */
10294 			queue_idx = queue_id % 10;
10295 			if (queue_idx > BNXT_MAX_QUEUE) {
10296 				bp->pri2cos_valid = false;
10297 				hwrm_req_drop(bp, req_qc);
10298 				return rc;
10299 			}
10300 			for (j = 0; j < bp->max_q; j++) {
10301 				if (bp->q_ids[j] == queue_id)
10302 					bp->pri2cos_idx[i] = queue_idx;
10303 			}
10304 		}
10305 		bp->pri2cos_valid = true;
10306 	}
10307 	hwrm_req_drop(bp, req_qc);
10308 
10309 	return rc;
10310 }
10311 
10312 static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
10313 {
10314 	bnxt_hwrm_tunnel_dst_port_free(bp,
10315 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
10316 	bnxt_hwrm_tunnel_dst_port_free(bp,
10317 		TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
10318 }
10319 
10320 static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
10321 {
10322 	int rc, i;
10323 	u32 tpa_flags = 0;
10324 
10325 	if (set_tpa)
10326 		tpa_flags = bp->flags & BNXT_FLAG_TPA;
10327 	else if (BNXT_NO_FW_ACCESS(bp))
10328 		return 0;
10329 	for (i = 0; i < bp->nr_vnics; i++) {
10330 		rc = bnxt_hwrm_vnic_set_tpa(bp, &bp->vnic_info[i], tpa_flags);
10331 		if (rc) {
10332 			netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
10333 				   i, rc);
10334 			return rc;
10335 		}
10336 	}
10337 	return 0;
10338 }
10339 
10340 static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
10341 {
10342 	int i;
10343 
10344 	for (i = 0; i < bp->nr_vnics; i++)
10345 		bnxt_hwrm_vnic_set_rss(bp, &bp->vnic_info[i], false);
10346 }
10347 
10348 static void bnxt_clear_vnic(struct bnxt *bp)
10349 {
10350 	if (!bp->vnic_info)
10351 		return;
10352 
10353 	bnxt_hwrm_clear_vnic_filter(bp);
10354 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS)) {
10355 		/* clear all RSS setting before free vnic ctx */
10356 		bnxt_hwrm_clear_vnic_rss(bp);
10357 		bnxt_hwrm_vnic_ctx_free(bp);
10358 	}
10359 	/* before free the vnic, undo the vnic tpa settings */
10360 	if (bp->flags & BNXT_FLAG_TPA)
10361 		bnxt_set_tpa(bp, false);
10362 	bnxt_hwrm_vnic_free(bp);
10363 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10364 		bnxt_hwrm_vnic_ctx_free(bp);
10365 }
10366 
10367 static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
10368 				    bool irq_re_init)
10369 {
10370 	bnxt_clear_vnic(bp);
10371 	bnxt_hwrm_ring_free(bp, close_path);
10372 	bnxt_hwrm_ring_grp_free(bp);
10373 	if (irq_re_init) {
10374 		bnxt_hwrm_stat_ctx_free(bp);
10375 		bnxt_hwrm_free_tunnel_ports(bp);
10376 	}
10377 }
10378 
10379 static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
10380 {
10381 	struct hwrm_func_cfg_input *req;
10382 	u8 evb_mode;
10383 	int rc;
10384 
10385 	if (br_mode == BRIDGE_MODE_VEB)
10386 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
10387 	else if (br_mode == BRIDGE_MODE_VEPA)
10388 		evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
10389 	else
10390 		return -EINVAL;
10391 
10392 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10393 	if (rc)
10394 		return rc;
10395 
10396 	req->fid = cpu_to_le16(0xffff);
10397 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
10398 	req->evb_mode = evb_mode;
10399 	return hwrm_req_send(bp, req);
10400 }
10401 
10402 static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
10403 {
10404 	struct hwrm_func_cfg_input *req;
10405 	int rc;
10406 
10407 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
10408 		return 0;
10409 
10410 	rc = bnxt_hwrm_func_cfg_short_req_init(bp, &req);
10411 	if (rc)
10412 		return rc;
10413 
10414 	req->fid = cpu_to_le16(0xffff);
10415 	req->enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
10416 	req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
10417 	if (size == 128)
10418 		req->options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
10419 
10420 	return hwrm_req_send(bp, req);
10421 }
10422 
10423 static int __bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10424 {
10425 	int rc;
10426 
10427 	if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
10428 		goto skip_rss_ctx;
10429 
10430 	/* allocate context for vnic */
10431 	rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 0);
10432 	if (rc) {
10433 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10434 			   vnic->vnic_id, rc);
10435 		goto vnic_setup_err;
10436 	}
10437 	bp->rsscos_nr_ctxs++;
10438 
10439 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10440 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, 1);
10441 		if (rc) {
10442 			netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
10443 				   vnic->vnic_id, rc);
10444 			goto vnic_setup_err;
10445 		}
10446 		bp->rsscos_nr_ctxs++;
10447 	}
10448 
10449 skip_rss_ctx:
10450 	/* configure default vnic, ring grp */
10451 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10452 	if (rc) {
10453 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10454 			   vnic->vnic_id, rc);
10455 		goto vnic_setup_err;
10456 	}
10457 
10458 	/* Enable RSS hashing on vnic */
10459 	rc = bnxt_hwrm_vnic_set_rss(bp, vnic, true);
10460 	if (rc) {
10461 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
10462 			   vnic->vnic_id, rc);
10463 		goto vnic_setup_err;
10464 	}
10465 
10466 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10467 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10468 		if (rc) {
10469 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10470 				   vnic->vnic_id, rc);
10471 		}
10472 	}
10473 
10474 vnic_setup_err:
10475 	return rc;
10476 }
10477 
10478 int bnxt_hwrm_vnic_update(struct bnxt *bp, struct bnxt_vnic_info *vnic,
10479 			  u8 valid)
10480 {
10481 	struct hwrm_vnic_update_input *req;
10482 	int rc;
10483 
10484 	rc = hwrm_req_init(bp, req, HWRM_VNIC_UPDATE);
10485 	if (rc)
10486 		return rc;
10487 
10488 	req->vnic_id = cpu_to_le32(vnic->fw_vnic_id);
10489 
10490 	if (valid & VNIC_UPDATE_REQ_ENABLES_MRU_VALID)
10491 		req->mru = cpu_to_le16(vnic->mru);
10492 
10493 	req->enables = cpu_to_le32(valid);
10494 
10495 	return hwrm_req_send(bp, req);
10496 }
10497 
10498 int bnxt_hwrm_vnic_rss_cfg_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10499 {
10500 	int rc;
10501 
10502 	rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
10503 	if (rc) {
10504 		netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
10505 			   vnic->vnic_id, rc);
10506 		return rc;
10507 	}
10508 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10509 	if (rc)
10510 		netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
10511 			   vnic->vnic_id, rc);
10512 	return rc;
10513 }
10514 
10515 int __bnxt_setup_vnic_p5(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10516 {
10517 	int rc, i, nr_ctxs;
10518 
10519 	nr_ctxs = bnxt_get_nr_rss_ctxs(bp, bp->rx_nr_rings);
10520 	for (i = 0; i < nr_ctxs; i++) {
10521 		rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, i);
10522 		if (rc) {
10523 			netdev_err(bp->dev, "hwrm vnic %d ctx %d alloc failure rc: %x\n",
10524 				   vnic->vnic_id, i, rc);
10525 			break;
10526 		}
10527 		bp->rsscos_nr_ctxs++;
10528 	}
10529 	if (i < nr_ctxs)
10530 		return -ENOMEM;
10531 
10532 	rc = bnxt_hwrm_vnic_rss_cfg_p5(bp, vnic);
10533 	if (rc)
10534 		return rc;
10535 
10536 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
10537 		rc = bnxt_hwrm_vnic_set_hds(bp, vnic);
10538 		if (rc) {
10539 			netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
10540 				   vnic->vnic_id, rc);
10541 		}
10542 	}
10543 	return rc;
10544 }
10545 
10546 static int bnxt_setup_vnic(struct bnxt *bp, struct bnxt_vnic_info *vnic)
10547 {
10548 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10549 		return __bnxt_setup_vnic_p5(bp, vnic);
10550 	else
10551 		return __bnxt_setup_vnic(bp, vnic);
10552 }
10553 
10554 static int bnxt_alloc_and_setup_vnic(struct bnxt *bp,
10555 				     struct bnxt_vnic_info *vnic,
10556 				     u16 start_rx_ring_idx, int rx_rings)
10557 {
10558 	int rc;
10559 
10560 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, start_rx_ring_idx, rx_rings);
10561 	if (rc) {
10562 		netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
10563 			   vnic->vnic_id, rc);
10564 		return rc;
10565 	}
10566 	return bnxt_setup_vnic(bp, vnic);
10567 }
10568 
10569 static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
10570 {
10571 	struct bnxt_vnic_info *vnic;
10572 	int i, rc = 0;
10573 
10574 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp)) {
10575 		vnic = &bp->vnic_info[BNXT_VNIC_NTUPLE];
10576 		return bnxt_alloc_and_setup_vnic(bp, vnic, 0, bp->rx_nr_rings);
10577 	}
10578 
10579 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
10580 		return 0;
10581 
10582 	for (i = 0; i < bp->rx_nr_rings; i++) {
10583 		u16 vnic_id = i + 1;
10584 		u16 ring_id = i;
10585 
10586 		if (vnic_id >= bp->nr_vnics)
10587 			break;
10588 
10589 		vnic = &bp->vnic_info[vnic_id];
10590 		vnic->flags |= BNXT_VNIC_RFS_FLAG;
10591 		if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
10592 			vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
10593 		if (bnxt_alloc_and_setup_vnic(bp, &bp->vnic_info[vnic_id], ring_id, 1))
10594 			break;
10595 	}
10596 	return rc;
10597 }
10598 
10599 void bnxt_del_one_rss_ctx(struct bnxt *bp, struct bnxt_rss_ctx *rss_ctx,
10600 			  bool all)
10601 {
10602 	struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10603 	struct bnxt_filter_base *usr_fltr, *tmp;
10604 	struct bnxt_ntuple_filter *ntp_fltr;
10605 	int i;
10606 
10607 	if (netif_running(bp->dev)) {
10608 		bnxt_hwrm_vnic_free_one(bp, &rss_ctx->vnic);
10609 		for (i = 0; i < BNXT_MAX_CTX_PER_VNIC; i++) {
10610 			if (vnic->fw_rss_cos_lb_ctx[i] != INVALID_HW_RING_ID)
10611 				bnxt_hwrm_vnic_ctx_free_one(bp, vnic, i);
10612 		}
10613 	}
10614 	if (!all)
10615 		return;
10616 
10617 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list) {
10618 		if ((usr_fltr->flags & BNXT_ACT_RSS_CTX) &&
10619 		    usr_fltr->fw_vnic_id == rss_ctx->index) {
10620 			ntp_fltr = container_of(usr_fltr,
10621 						struct bnxt_ntuple_filter,
10622 						base);
10623 			bnxt_hwrm_cfa_ntuple_filter_free(bp, ntp_fltr);
10624 			bnxt_del_ntp_filter(bp, ntp_fltr);
10625 			bnxt_del_one_usr_fltr(bp, usr_fltr);
10626 		}
10627 	}
10628 
10629 	if (vnic->rss_table)
10630 		dma_free_coherent(&bp->pdev->dev, vnic->rss_table_size,
10631 				  vnic->rss_table,
10632 				  vnic->rss_table_dma_addr);
10633 	bp->num_rss_ctx--;
10634 }
10635 
10636 static void bnxt_hwrm_realloc_rss_ctx_vnic(struct bnxt *bp)
10637 {
10638 	bool set_tpa = !!(bp->flags & BNXT_FLAG_TPA);
10639 	struct ethtool_rxfh_context *ctx;
10640 	unsigned long context;
10641 
10642 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10643 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10644 		struct bnxt_vnic_info *vnic = &rss_ctx->vnic;
10645 
10646 		if (bnxt_hwrm_vnic_alloc(bp, vnic, 0, bp->rx_nr_rings) ||
10647 		    bnxt_hwrm_vnic_set_tpa(bp, vnic, set_tpa) ||
10648 		    __bnxt_setup_vnic_p5(bp, vnic)) {
10649 			netdev_err(bp->dev, "Failed to restore RSS ctx %d\n",
10650 				   rss_ctx->index);
10651 			bnxt_del_one_rss_ctx(bp, rss_ctx, true);
10652 			ethtool_rxfh_context_lost(bp->dev, rss_ctx->index);
10653 		}
10654 	}
10655 }
10656 
10657 static void bnxt_clear_rss_ctxs(struct bnxt *bp)
10658 {
10659 	struct ethtool_rxfh_context *ctx;
10660 	unsigned long context;
10661 
10662 	xa_for_each(&bp->dev->ethtool->rss_ctx, context, ctx) {
10663 		struct bnxt_rss_ctx *rss_ctx = ethtool_rxfh_context_priv(ctx);
10664 
10665 		bnxt_del_one_rss_ctx(bp, rss_ctx, false);
10666 	}
10667 }
10668 
10669 /* Allow PF, trusted VFs and VFs with default VLAN to be in promiscuous mode */
10670 static bool bnxt_promisc_ok(struct bnxt *bp)
10671 {
10672 #ifdef CONFIG_BNXT_SRIOV
10673 	if (BNXT_VF(bp) && !bp->vf.vlan && !bnxt_is_trusted_vf(bp, &bp->vf))
10674 		return false;
10675 #endif
10676 	return true;
10677 }
10678 
10679 static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
10680 {
10681 	struct bnxt_vnic_info *vnic = &bp->vnic_info[1];
10682 	unsigned int rc = 0;
10683 
10684 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, bp->rx_nr_rings - 1, 1);
10685 	if (rc) {
10686 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10687 			   rc);
10688 		return rc;
10689 	}
10690 
10691 	rc = bnxt_hwrm_vnic_cfg(bp, vnic);
10692 	if (rc) {
10693 		netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
10694 			   rc);
10695 		return rc;
10696 	}
10697 	return rc;
10698 }
10699 
10700 static int bnxt_cfg_rx_mode(struct bnxt *);
10701 static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
10702 
10703 static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
10704 {
10705 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
10706 	int rc = 0;
10707 	unsigned int rx_nr_rings = bp->rx_nr_rings;
10708 
10709 	if (irq_re_init) {
10710 		rc = bnxt_hwrm_stat_ctx_alloc(bp);
10711 		if (rc) {
10712 			netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
10713 				   rc);
10714 			goto err_out;
10715 		}
10716 	}
10717 
10718 	rc = bnxt_hwrm_ring_alloc(bp);
10719 	if (rc) {
10720 		netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
10721 		goto err_out;
10722 	}
10723 
10724 	rc = bnxt_hwrm_ring_grp_alloc(bp);
10725 	if (rc) {
10726 		netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
10727 		goto err_out;
10728 	}
10729 
10730 	if (BNXT_CHIP_TYPE_NITRO_A0(bp))
10731 		rx_nr_rings--;
10732 
10733 	/* default vnic 0 */
10734 	rc = bnxt_hwrm_vnic_alloc(bp, vnic, 0, rx_nr_rings);
10735 	if (rc) {
10736 		netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
10737 		goto err_out;
10738 	}
10739 
10740 	if (BNXT_VF(bp))
10741 		bnxt_hwrm_func_qcfg(bp);
10742 
10743 	rc = bnxt_setup_vnic(bp, vnic);
10744 	if (rc)
10745 		goto err_out;
10746 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
10747 		bnxt_hwrm_update_rss_hash_cfg(bp);
10748 
10749 	if (bp->flags & BNXT_FLAG_RFS) {
10750 		rc = bnxt_alloc_rfs_vnics(bp);
10751 		if (rc)
10752 			goto err_out;
10753 	}
10754 
10755 	if (bp->flags & BNXT_FLAG_TPA) {
10756 		rc = bnxt_set_tpa(bp, true);
10757 		if (rc)
10758 			goto err_out;
10759 	}
10760 
10761 	if (BNXT_VF(bp))
10762 		bnxt_update_vf_mac(bp);
10763 
10764 	/* Filter for default vnic 0 */
10765 	rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
10766 	if (rc) {
10767 		if (BNXT_VF(bp) && rc == -ENODEV)
10768 			netdev_err(bp->dev, "Cannot configure L2 filter while PF is unavailable\n");
10769 		else
10770 			netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
10771 		goto err_out;
10772 	}
10773 	vnic->uc_filter_count = 1;
10774 
10775 	vnic->rx_mask = 0;
10776 	if (test_bit(BNXT_STATE_HALF_OPEN, &bp->state))
10777 		goto skip_rx_mask;
10778 
10779 	if (bp->dev->flags & IFF_BROADCAST)
10780 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
10781 
10782 	if (bp->dev->flags & IFF_PROMISC)
10783 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
10784 
10785 	if (bp->dev->flags & IFF_ALLMULTI) {
10786 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
10787 		vnic->mc_list_count = 0;
10788 	} else if (bp->dev->flags & IFF_MULTICAST) {
10789 		u32 mask = 0;
10790 
10791 		bnxt_mc_list_updated(bp, &mask);
10792 		vnic->rx_mask |= mask;
10793 	}
10794 
10795 	rc = bnxt_cfg_rx_mode(bp);
10796 	if (rc)
10797 		goto err_out;
10798 
10799 skip_rx_mask:
10800 	rc = bnxt_hwrm_set_coal(bp);
10801 	if (rc)
10802 		netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
10803 				rc);
10804 
10805 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
10806 		rc = bnxt_setup_nitroa0_vnic(bp);
10807 		if (rc)
10808 			netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
10809 				   rc);
10810 	}
10811 
10812 	if (BNXT_VF(bp)) {
10813 		bnxt_hwrm_func_qcfg(bp);
10814 		netdev_update_features(bp->dev);
10815 	}
10816 
10817 	return 0;
10818 
10819 err_out:
10820 	bnxt_hwrm_resource_free(bp, 0, true);
10821 
10822 	return rc;
10823 }
10824 
10825 static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
10826 {
10827 	bnxt_hwrm_resource_free(bp, 1, irq_re_init);
10828 	return 0;
10829 }
10830 
10831 static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
10832 {
10833 	bnxt_init_cp_rings(bp);
10834 	bnxt_init_rx_rings(bp);
10835 	bnxt_init_tx_rings(bp);
10836 	bnxt_init_ring_grps(bp, irq_re_init);
10837 	bnxt_init_vnics(bp);
10838 
10839 	return bnxt_init_chip(bp, irq_re_init);
10840 }
10841 
10842 static int bnxt_set_real_num_queues(struct bnxt *bp)
10843 {
10844 	int rc;
10845 	struct net_device *dev = bp->dev;
10846 
10847 	rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
10848 					  bp->tx_nr_rings_xdp);
10849 	if (rc)
10850 		return rc;
10851 
10852 	rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
10853 	if (rc)
10854 		return rc;
10855 
10856 #ifdef CONFIG_RFS_ACCEL
10857 	if (bp->flags & BNXT_FLAG_RFS)
10858 		dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
10859 #endif
10860 
10861 	return rc;
10862 }
10863 
10864 static int __bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10865 			     bool shared)
10866 {
10867 	int _rx = *rx, _tx = *tx;
10868 
10869 	if (shared) {
10870 		*rx = min_t(int, _rx, max);
10871 		*tx = min_t(int, _tx, max);
10872 	} else {
10873 		if (max < 2)
10874 			return -ENOMEM;
10875 
10876 		while (_rx + _tx > max) {
10877 			if (_rx > _tx && _rx > 1)
10878 				_rx--;
10879 			else if (_tx > 1)
10880 				_tx--;
10881 		}
10882 		*rx = _rx;
10883 		*tx = _tx;
10884 	}
10885 	return 0;
10886 }
10887 
10888 static int __bnxt_num_tx_to_cp(struct bnxt *bp, int tx, int tx_sets, int tx_xdp)
10889 {
10890 	return (tx - tx_xdp) / tx_sets + tx_xdp;
10891 }
10892 
10893 int bnxt_num_tx_to_cp(struct bnxt *bp, int tx)
10894 {
10895 	int tcs = bp->num_tc;
10896 
10897 	if (!tcs)
10898 		tcs = 1;
10899 	return __bnxt_num_tx_to_cp(bp, tx, tcs, bp->tx_nr_rings_xdp);
10900 }
10901 
10902 static int bnxt_num_cp_to_tx(struct bnxt *bp, int tx_cp)
10903 {
10904 	int tcs = bp->num_tc;
10905 
10906 	return (tx_cp - bp->tx_nr_rings_xdp) * tcs +
10907 	       bp->tx_nr_rings_xdp;
10908 }
10909 
10910 static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
10911 			   bool sh)
10912 {
10913 	int tx_cp = bnxt_num_tx_to_cp(bp, *tx);
10914 
10915 	if (tx_cp != *tx) {
10916 		int tx_saved = tx_cp, rc;
10917 
10918 		rc = __bnxt_trim_rings(bp, rx, &tx_cp, max, sh);
10919 		if (rc)
10920 			return rc;
10921 		if (tx_cp != tx_saved)
10922 			*tx = bnxt_num_cp_to_tx(bp, tx_cp);
10923 		return 0;
10924 	}
10925 	return __bnxt_trim_rings(bp, rx, tx, max, sh);
10926 }
10927 
10928 static void bnxt_setup_msix(struct bnxt *bp)
10929 {
10930 	const int len = sizeof(bp->irq_tbl[0].name);
10931 	struct net_device *dev = bp->dev;
10932 	int tcs, i;
10933 
10934 	tcs = bp->num_tc;
10935 	if (tcs) {
10936 		int i, off, count;
10937 
10938 		for (i = 0; i < tcs; i++) {
10939 			count = bp->tx_nr_rings_per_tc;
10940 			off = BNXT_TC_TO_RING_BASE(bp, i);
10941 			netdev_set_tc_queue(dev, i, count, off);
10942 		}
10943 	}
10944 
10945 	for (i = 0; i < bp->cp_nr_rings; i++) {
10946 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
10947 		char *attr;
10948 
10949 		if (bp->flags & BNXT_FLAG_SHARED_RINGS)
10950 			attr = "TxRx";
10951 		else if (i < bp->rx_nr_rings)
10952 			attr = "rx";
10953 		else
10954 			attr = "tx";
10955 
10956 		snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
10957 			 attr, i);
10958 		bp->irq_tbl[map_idx].handler = bnxt_msix;
10959 	}
10960 }
10961 
10962 static int bnxt_init_int_mode(struct bnxt *bp);
10963 
10964 static int bnxt_change_msix(struct bnxt *bp, int total)
10965 {
10966 	struct msi_map map;
10967 	int i;
10968 
10969 	/* add MSIX to the end if needed */
10970 	for (i = bp->total_irqs; i < total; i++) {
10971 		map = pci_msix_alloc_irq_at(bp->pdev, i, NULL);
10972 		if (map.index < 0)
10973 			return bp->total_irqs;
10974 		bp->irq_tbl[i].vector = map.virq;
10975 		bp->total_irqs++;
10976 	}
10977 
10978 	/* trim MSIX from the end if needed */
10979 	for (i = bp->total_irqs; i > total; i--) {
10980 		map.index = i - 1;
10981 		map.virq = bp->irq_tbl[i - 1].vector;
10982 		pci_msix_free_irq(bp->pdev, map);
10983 		bp->total_irqs--;
10984 	}
10985 	return bp->total_irqs;
10986 }
10987 
10988 static int bnxt_setup_int_mode(struct bnxt *bp)
10989 {
10990 	int rc;
10991 
10992 	if (!bp->irq_tbl) {
10993 		rc = bnxt_init_int_mode(bp);
10994 		if (rc || !bp->irq_tbl)
10995 			return rc ?: -ENODEV;
10996 	}
10997 
10998 	bnxt_setup_msix(bp);
10999 
11000 	rc = bnxt_set_real_num_queues(bp);
11001 	return rc;
11002 }
11003 
11004 static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
11005 {
11006 	return bp->hw_resc.max_rsscos_ctxs;
11007 }
11008 
11009 static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
11010 {
11011 	return bp->hw_resc.max_vnics;
11012 }
11013 
11014 unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
11015 {
11016 	return bp->hw_resc.max_stat_ctxs;
11017 }
11018 
11019 unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
11020 {
11021 	return bp->hw_resc.max_cp_rings;
11022 }
11023 
11024 static unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
11025 {
11026 	unsigned int cp = bp->hw_resc.max_cp_rings;
11027 
11028 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
11029 		cp -= bnxt_get_ulp_msix_num(bp);
11030 
11031 	return cp;
11032 }
11033 
11034 static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
11035 {
11036 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11037 
11038 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11039 		return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_nqs);
11040 
11041 	return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
11042 }
11043 
11044 static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
11045 {
11046 	bp->hw_resc.max_irqs = max_irqs;
11047 }
11048 
11049 unsigned int bnxt_get_avail_cp_rings_for_en(struct bnxt *bp)
11050 {
11051 	unsigned int cp;
11052 
11053 	cp = bnxt_get_max_func_cp_rings_for_en(bp);
11054 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11055 		return cp - bp->rx_nr_rings - bp->tx_nr_rings;
11056 	else
11057 		return cp - bp->cp_nr_rings;
11058 }
11059 
11060 unsigned int bnxt_get_avail_stat_ctxs_for_en(struct bnxt *bp)
11061 {
11062 	return bnxt_get_max_func_stat_ctxs(bp) - bnxt_get_func_stat_ctxs(bp);
11063 }
11064 
11065 static int bnxt_get_avail_msix(struct bnxt *bp, int num)
11066 {
11067 	int max_irq = bnxt_get_max_func_irqs(bp);
11068 	int total_req = bp->cp_nr_rings + num;
11069 
11070 	if (max_irq < total_req) {
11071 		num = max_irq - bp->cp_nr_rings;
11072 		if (num <= 0)
11073 			return 0;
11074 	}
11075 	return num;
11076 }
11077 
11078 static int bnxt_get_num_msix(struct bnxt *bp)
11079 {
11080 	if (!BNXT_NEW_RM(bp))
11081 		return bnxt_get_max_func_irqs(bp);
11082 
11083 	return bnxt_nq_rings_in_use(bp);
11084 }
11085 
11086 static int bnxt_init_int_mode(struct bnxt *bp)
11087 {
11088 	int i, total_vecs, max, rc = 0, min = 1, ulp_msix, tx_cp, tbl_size;
11089 
11090 	total_vecs = bnxt_get_num_msix(bp);
11091 	max = bnxt_get_max_func_irqs(bp);
11092 	if (total_vecs > max)
11093 		total_vecs = max;
11094 
11095 	if (!total_vecs)
11096 		return 0;
11097 
11098 	if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
11099 		min = 2;
11100 
11101 	total_vecs = pci_alloc_irq_vectors(bp->pdev, min, total_vecs,
11102 					   PCI_IRQ_MSIX);
11103 	ulp_msix = bnxt_get_ulp_msix_num(bp);
11104 	if (total_vecs < 0 || total_vecs < ulp_msix) {
11105 		rc = -ENODEV;
11106 		goto msix_setup_exit;
11107 	}
11108 
11109 	tbl_size = total_vecs;
11110 	if (pci_msix_can_alloc_dyn(bp->pdev))
11111 		tbl_size = max;
11112 	bp->irq_tbl = kcalloc(tbl_size, sizeof(*bp->irq_tbl), GFP_KERNEL);
11113 	if (bp->irq_tbl) {
11114 		for (i = 0; i < total_vecs; i++)
11115 			bp->irq_tbl[i].vector = pci_irq_vector(bp->pdev, i);
11116 
11117 		bp->total_irqs = total_vecs;
11118 		/* Trim rings based upon num of vectors allocated */
11119 		rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
11120 				     total_vecs - ulp_msix, min == 1);
11121 		if (rc)
11122 			goto msix_setup_exit;
11123 
11124 		tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
11125 		bp->cp_nr_rings = (min == 1) ?
11126 				  max_t(int, tx_cp, bp->rx_nr_rings) :
11127 				  tx_cp + bp->rx_nr_rings;
11128 
11129 	} else {
11130 		rc = -ENOMEM;
11131 		goto msix_setup_exit;
11132 	}
11133 	return 0;
11134 
11135 msix_setup_exit:
11136 	netdev_err(bp->dev, "bnxt_init_int_mode err: %x\n", rc);
11137 	kfree(bp->irq_tbl);
11138 	bp->irq_tbl = NULL;
11139 	pci_free_irq_vectors(bp->pdev);
11140 	return rc;
11141 }
11142 
11143 static void bnxt_clear_int_mode(struct bnxt *bp)
11144 {
11145 	pci_free_irq_vectors(bp->pdev);
11146 
11147 	kfree(bp->irq_tbl);
11148 	bp->irq_tbl = NULL;
11149 }
11150 
11151 int bnxt_reserve_rings(struct bnxt *bp, bool irq_re_init)
11152 {
11153 	bool irq_cleared = false;
11154 	bool irq_change = false;
11155 	int tcs = bp->num_tc;
11156 	int irqs_required;
11157 	int rc;
11158 
11159 	if (!bnxt_need_reserve_rings(bp))
11160 		return 0;
11161 
11162 	if (BNXT_NEW_RM(bp) && !bnxt_ulp_registered(bp->edev)) {
11163 		int ulp_msix = bnxt_get_avail_msix(bp, bp->ulp_num_msix_want);
11164 
11165 		if (ulp_msix > bp->ulp_num_msix_want)
11166 			ulp_msix = bp->ulp_num_msix_want;
11167 		irqs_required = ulp_msix + bp->cp_nr_rings;
11168 	} else {
11169 		irqs_required = bnxt_get_num_msix(bp);
11170 	}
11171 
11172 	if (irq_re_init && BNXT_NEW_RM(bp) && irqs_required != bp->total_irqs) {
11173 		irq_change = true;
11174 		if (!pci_msix_can_alloc_dyn(bp->pdev)) {
11175 			bnxt_ulp_irq_stop(bp);
11176 			bnxt_clear_int_mode(bp);
11177 			irq_cleared = true;
11178 		}
11179 	}
11180 	rc = __bnxt_reserve_rings(bp);
11181 	if (irq_cleared) {
11182 		if (!rc)
11183 			rc = bnxt_init_int_mode(bp);
11184 		bnxt_ulp_irq_restart(bp, rc);
11185 	} else if (irq_change && !rc) {
11186 		if (bnxt_change_msix(bp, irqs_required) != irqs_required)
11187 			rc = -ENOSPC;
11188 	}
11189 	if (rc) {
11190 		netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
11191 		return rc;
11192 	}
11193 	if (tcs && (bp->tx_nr_rings_per_tc * tcs !=
11194 		    bp->tx_nr_rings - bp->tx_nr_rings_xdp)) {
11195 		netdev_err(bp->dev, "tx ring reservation failure\n");
11196 		netdev_reset_tc(bp->dev);
11197 		bp->num_tc = 0;
11198 		if (bp->tx_nr_rings_xdp)
11199 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings_xdp;
11200 		else
11201 			bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
11202 		return -ENOMEM;
11203 	}
11204 	return 0;
11205 }
11206 
11207 static void bnxt_free_irq(struct bnxt *bp)
11208 {
11209 	struct bnxt_irq *irq;
11210 	int i;
11211 
11212 #ifdef CONFIG_RFS_ACCEL
11213 	free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
11214 	bp->dev->rx_cpu_rmap = NULL;
11215 #endif
11216 	if (!bp->irq_tbl || !bp->bnapi)
11217 		return;
11218 
11219 	for (i = 0; i < bp->cp_nr_rings; i++) {
11220 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11221 
11222 		irq = &bp->irq_tbl[map_idx];
11223 		if (irq->requested) {
11224 			if (irq->have_cpumask) {
11225 				irq_update_affinity_hint(irq->vector, NULL);
11226 				free_cpumask_var(irq->cpu_mask);
11227 				irq->have_cpumask = 0;
11228 			}
11229 			free_irq(irq->vector, bp->bnapi[i]);
11230 		}
11231 
11232 		irq->requested = 0;
11233 	}
11234 }
11235 
11236 static int bnxt_request_irq(struct bnxt *bp)
11237 {
11238 	int i, j, rc = 0;
11239 	unsigned long flags = 0;
11240 #ifdef CONFIG_RFS_ACCEL
11241 	struct cpu_rmap *rmap;
11242 #endif
11243 
11244 	rc = bnxt_setup_int_mode(bp);
11245 	if (rc) {
11246 		netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
11247 			   rc);
11248 		return rc;
11249 	}
11250 #ifdef CONFIG_RFS_ACCEL
11251 	rmap = bp->dev->rx_cpu_rmap;
11252 #endif
11253 	for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
11254 		int map_idx = bnxt_cp_num_to_irq_num(bp, i);
11255 		struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
11256 
11257 #ifdef CONFIG_RFS_ACCEL
11258 		if (rmap && bp->bnapi[i]->rx_ring) {
11259 			rc = irq_cpu_rmap_add(rmap, irq->vector);
11260 			if (rc)
11261 				netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
11262 					    j);
11263 			j++;
11264 		}
11265 #endif
11266 		rc = request_irq(irq->vector, irq->handler, flags, irq->name,
11267 				 bp->bnapi[i]);
11268 		if (rc)
11269 			break;
11270 
11271 		netif_napi_set_irq(&bp->bnapi[i]->napi, irq->vector);
11272 		irq->requested = 1;
11273 
11274 		if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
11275 			int numa_node = dev_to_node(&bp->pdev->dev);
11276 
11277 			irq->have_cpumask = 1;
11278 			cpumask_set_cpu(cpumask_local_spread(i, numa_node),
11279 					irq->cpu_mask);
11280 			rc = irq_update_affinity_hint(irq->vector, irq->cpu_mask);
11281 			if (rc) {
11282 				netdev_warn(bp->dev,
11283 					    "Update affinity hint failed, IRQ = %d\n",
11284 					    irq->vector);
11285 				break;
11286 			}
11287 		}
11288 	}
11289 	return rc;
11290 }
11291 
11292 static void bnxt_del_napi(struct bnxt *bp)
11293 {
11294 	int i;
11295 
11296 	if (!bp->bnapi)
11297 		return;
11298 
11299 	for (i = 0; i < bp->rx_nr_rings; i++)
11300 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_RX, NULL);
11301 	for (i = 0; i < bp->tx_nr_rings - bp->tx_nr_rings_xdp; i++)
11302 		netif_queue_set_napi(bp->dev, i, NETDEV_QUEUE_TYPE_TX, NULL);
11303 
11304 	for (i = 0; i < bp->cp_nr_rings; i++) {
11305 		struct bnxt_napi *bnapi = bp->bnapi[i];
11306 
11307 		__netif_napi_del(&bnapi->napi);
11308 	}
11309 	/* We called __netif_napi_del(), we need
11310 	 * to respect an RCU grace period before freeing napi structures.
11311 	 */
11312 	synchronize_net();
11313 }
11314 
11315 static void bnxt_init_napi(struct bnxt *bp)
11316 {
11317 	int (*poll_fn)(struct napi_struct *, int) = bnxt_poll;
11318 	unsigned int cp_nr_rings = bp->cp_nr_rings;
11319 	struct bnxt_napi *bnapi;
11320 	int i;
11321 
11322 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
11323 		poll_fn = bnxt_poll_p5;
11324 	else if (BNXT_CHIP_TYPE_NITRO_A0(bp))
11325 		cp_nr_rings--;
11326 	for (i = 0; i < cp_nr_rings; i++) {
11327 		bnapi = bp->bnapi[i];
11328 		netif_napi_add_config(bp->dev, &bnapi->napi, poll_fn,
11329 				      bnapi->index);
11330 	}
11331 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
11332 		bnapi = bp->bnapi[cp_nr_rings];
11333 		netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll_nitroa0);
11334 	}
11335 }
11336 
11337 static void bnxt_disable_napi(struct bnxt *bp)
11338 {
11339 	int i;
11340 
11341 	if (!bp->bnapi ||
11342 	    test_and_set_bit(BNXT_STATE_NAPI_DISABLED, &bp->state))
11343 		return;
11344 
11345 	for (i = 0; i < bp->cp_nr_rings; i++) {
11346 		struct bnxt_napi *bnapi = bp->bnapi[i];
11347 		struct bnxt_cp_ring_info *cpr;
11348 
11349 		cpr = &bnapi->cp_ring;
11350 		if (bnapi->tx_fault)
11351 			cpr->sw_stats->tx.tx_resets++;
11352 		if (bnapi->in_reset)
11353 			cpr->sw_stats->rx.rx_resets++;
11354 		napi_disable(&bnapi->napi);
11355 	}
11356 }
11357 
11358 static void bnxt_enable_napi(struct bnxt *bp)
11359 {
11360 	int i;
11361 
11362 	clear_bit(BNXT_STATE_NAPI_DISABLED, &bp->state);
11363 	for (i = 0; i < bp->cp_nr_rings; i++) {
11364 		struct bnxt_napi *bnapi = bp->bnapi[i];
11365 		struct bnxt_cp_ring_info *cpr;
11366 
11367 		bnapi->tx_fault = 0;
11368 
11369 		cpr = &bnapi->cp_ring;
11370 		bnapi->in_reset = false;
11371 
11372 		if (bnapi->rx_ring) {
11373 			INIT_WORK(&cpr->dim.work, bnxt_dim_work);
11374 			cpr->dim.mode = DIM_CQ_PERIOD_MODE_START_FROM_EQE;
11375 		}
11376 		napi_enable(&bnapi->napi);
11377 	}
11378 }
11379 
11380 void bnxt_tx_disable(struct bnxt *bp)
11381 {
11382 	int i;
11383 	struct bnxt_tx_ring_info *txr;
11384 
11385 	if (bp->tx_ring) {
11386 		for (i = 0; i < bp->tx_nr_rings; i++) {
11387 			txr = &bp->tx_ring[i];
11388 			WRITE_ONCE(txr->dev_state, BNXT_DEV_STATE_CLOSING);
11389 		}
11390 	}
11391 	/* Make sure napi polls see @dev_state change */
11392 	synchronize_net();
11393 	/* Drop carrier first to prevent TX timeout */
11394 	netif_carrier_off(bp->dev);
11395 	/* Stop all TX queues */
11396 	netif_tx_disable(bp->dev);
11397 }
11398 
11399 void bnxt_tx_enable(struct bnxt *bp)
11400 {
11401 	int i;
11402 	struct bnxt_tx_ring_info *txr;
11403 
11404 	for (i = 0; i < bp->tx_nr_rings; i++) {
11405 		txr = &bp->tx_ring[i];
11406 		WRITE_ONCE(txr->dev_state, 0);
11407 	}
11408 	/* Make sure napi polls see @dev_state change */
11409 	synchronize_net();
11410 	netif_tx_wake_all_queues(bp->dev);
11411 	if (BNXT_LINK_IS_UP(bp))
11412 		netif_carrier_on(bp->dev);
11413 }
11414 
11415 static char *bnxt_report_fec(struct bnxt_link_info *link_info)
11416 {
11417 	u8 active_fec = link_info->active_fec_sig_mode &
11418 			PORT_PHY_QCFG_RESP_ACTIVE_FEC_MASK;
11419 
11420 	switch (active_fec) {
11421 	default:
11422 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_NONE_ACTIVE:
11423 		return "None";
11424 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE74_ACTIVE:
11425 		return "Clause 74 BaseR";
11426 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_CLAUSE91_ACTIVE:
11427 		return "Clause 91 RS(528,514)";
11428 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_1XN_ACTIVE:
11429 		return "Clause 91 RS544_1XN";
11430 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS544_IEEE_ACTIVE:
11431 		return "Clause 91 RS(544,514)";
11432 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_1XN_ACTIVE:
11433 		return "Clause 91 RS272_1XN";
11434 	case PORT_PHY_QCFG_RESP_ACTIVE_FEC_FEC_RS272_IEEE_ACTIVE:
11435 		return "Clause 91 RS(272,257)";
11436 	}
11437 }
11438 
11439 void bnxt_report_link(struct bnxt *bp)
11440 {
11441 	if (BNXT_LINK_IS_UP(bp)) {
11442 		const char *signal = "";
11443 		const char *flow_ctrl;
11444 		const char *duplex;
11445 		u32 speed;
11446 		u16 fec;
11447 
11448 		netif_carrier_on(bp->dev);
11449 		speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
11450 		if (speed == SPEED_UNKNOWN) {
11451 			netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
11452 			return;
11453 		}
11454 		if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
11455 			duplex = "full";
11456 		else
11457 			duplex = "half";
11458 		if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
11459 			flow_ctrl = "ON - receive & transmit";
11460 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
11461 			flow_ctrl = "ON - transmit";
11462 		else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
11463 			flow_ctrl = "ON - receive";
11464 		else
11465 			flow_ctrl = "none";
11466 		if (bp->link_info.phy_qcfg_resp.option_flags &
11467 		    PORT_PHY_QCFG_RESP_OPTION_FLAGS_SIGNAL_MODE_KNOWN) {
11468 			u8 sig_mode = bp->link_info.active_fec_sig_mode &
11469 				      PORT_PHY_QCFG_RESP_SIGNAL_MODE_MASK;
11470 			switch (sig_mode) {
11471 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_NRZ:
11472 				signal = "(NRZ) ";
11473 				break;
11474 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4:
11475 				signal = "(PAM4 56Gbps) ";
11476 				break;
11477 			case PORT_PHY_QCFG_RESP_SIGNAL_MODE_PAM4_112:
11478 				signal = "(PAM4 112Gbps) ";
11479 				break;
11480 			default:
11481 				break;
11482 			}
11483 		}
11484 		netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s%s duplex, Flow control: %s\n",
11485 			    speed, signal, duplex, flow_ctrl);
11486 		if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP)
11487 			netdev_info(bp->dev, "EEE is %s\n",
11488 				    bp->eee.eee_active ? "active" :
11489 							 "not active");
11490 		fec = bp->link_info.fec_cfg;
11491 		if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
11492 			netdev_info(bp->dev, "FEC autoneg %s encoding: %s\n",
11493 				    (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
11494 				    bnxt_report_fec(&bp->link_info));
11495 	} else {
11496 		netif_carrier_off(bp->dev);
11497 		netdev_err(bp->dev, "NIC Link is Down\n");
11498 	}
11499 }
11500 
11501 static bool bnxt_phy_qcaps_no_speed(struct hwrm_port_phy_qcaps_output *resp)
11502 {
11503 	if (!resp->supported_speeds_auto_mode &&
11504 	    !resp->supported_speeds_force_mode &&
11505 	    !resp->supported_pam4_speeds_auto_mode &&
11506 	    !resp->supported_pam4_speeds_force_mode &&
11507 	    !resp->supported_speeds2_auto_mode &&
11508 	    !resp->supported_speeds2_force_mode)
11509 		return true;
11510 	return false;
11511 }
11512 
11513 static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
11514 {
11515 	struct bnxt_link_info *link_info = &bp->link_info;
11516 	struct hwrm_port_phy_qcaps_output *resp;
11517 	struct hwrm_port_phy_qcaps_input *req;
11518 	int rc = 0;
11519 
11520 	if (bp->hwrm_spec_code < 0x10201)
11521 		return 0;
11522 
11523 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCAPS);
11524 	if (rc)
11525 		return rc;
11526 
11527 	resp = hwrm_req_hold(bp, req);
11528 	rc = hwrm_req_send(bp, req);
11529 	if (rc)
11530 		goto hwrm_phy_qcaps_exit;
11531 
11532 	bp->phy_flags = resp->flags | (le16_to_cpu(resp->flags2) << 8);
11533 	if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
11534 		struct ethtool_keee *eee = &bp->eee;
11535 		u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
11536 
11537 		_bnxt_fw_to_linkmode(eee->supported, fw_speeds);
11538 		bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
11539 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
11540 		bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
11541 				 PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
11542 	}
11543 
11544 	if (bp->hwrm_spec_code >= 0x10a01) {
11545 		if (bnxt_phy_qcaps_no_speed(resp)) {
11546 			link_info->phy_state = BNXT_PHY_STATE_DISABLED;
11547 			netdev_warn(bp->dev, "Ethernet link disabled\n");
11548 		} else if (link_info->phy_state == BNXT_PHY_STATE_DISABLED) {
11549 			link_info->phy_state = BNXT_PHY_STATE_ENABLED;
11550 			netdev_info(bp->dev, "Ethernet link enabled\n");
11551 			/* Phy re-enabled, reprobe the speeds */
11552 			link_info->support_auto_speeds = 0;
11553 			link_info->support_pam4_auto_speeds = 0;
11554 			link_info->support_auto_speeds2 = 0;
11555 		}
11556 	}
11557 	if (resp->supported_speeds_auto_mode)
11558 		link_info->support_auto_speeds =
11559 			le16_to_cpu(resp->supported_speeds_auto_mode);
11560 	if (resp->supported_pam4_speeds_auto_mode)
11561 		link_info->support_pam4_auto_speeds =
11562 			le16_to_cpu(resp->supported_pam4_speeds_auto_mode);
11563 	if (resp->supported_speeds2_auto_mode)
11564 		link_info->support_auto_speeds2 =
11565 			le16_to_cpu(resp->supported_speeds2_auto_mode);
11566 
11567 	bp->port_count = resp->port_cnt;
11568 
11569 hwrm_phy_qcaps_exit:
11570 	hwrm_req_drop(bp, req);
11571 	return rc;
11572 }
11573 
11574 static bool bnxt_support_dropped(u16 advertising, u16 supported)
11575 {
11576 	u16 diff = advertising ^ supported;
11577 
11578 	return ((supported | diff) != supported);
11579 }
11580 
11581 static bool bnxt_support_speed_dropped(struct bnxt_link_info *link_info)
11582 {
11583 	struct bnxt *bp = container_of(link_info, struct bnxt, link_info);
11584 
11585 	/* Check if any advertised speeds are no longer supported. The caller
11586 	 * holds the link_lock mutex, so we can modify link_info settings.
11587 	 */
11588 	if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11589 		if (bnxt_support_dropped(link_info->advertising,
11590 					 link_info->support_auto_speeds2)) {
11591 			link_info->advertising = link_info->support_auto_speeds2;
11592 			return true;
11593 		}
11594 		return false;
11595 	}
11596 	if (bnxt_support_dropped(link_info->advertising,
11597 				 link_info->support_auto_speeds)) {
11598 		link_info->advertising = link_info->support_auto_speeds;
11599 		return true;
11600 	}
11601 	if (bnxt_support_dropped(link_info->advertising_pam4,
11602 				 link_info->support_pam4_auto_speeds)) {
11603 		link_info->advertising_pam4 = link_info->support_pam4_auto_speeds;
11604 		return true;
11605 	}
11606 	return false;
11607 }
11608 
11609 int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
11610 {
11611 	struct bnxt_link_info *link_info = &bp->link_info;
11612 	struct hwrm_port_phy_qcfg_output *resp;
11613 	struct hwrm_port_phy_qcfg_input *req;
11614 	u8 link_state = link_info->link_state;
11615 	bool support_changed;
11616 	int rc;
11617 
11618 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_QCFG);
11619 	if (rc)
11620 		return rc;
11621 
11622 	resp = hwrm_req_hold(bp, req);
11623 	rc = hwrm_req_send(bp, req);
11624 	if (rc) {
11625 		hwrm_req_drop(bp, req);
11626 		if (BNXT_VF(bp) && rc == -ENODEV) {
11627 			netdev_warn(bp->dev, "Cannot obtain link state while PF unavailable.\n");
11628 			rc = 0;
11629 		}
11630 		return rc;
11631 	}
11632 
11633 	memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
11634 	link_info->phy_link_status = resp->link;
11635 	link_info->duplex = resp->duplex_cfg;
11636 	if (bp->hwrm_spec_code >= 0x10800)
11637 		link_info->duplex = resp->duplex_state;
11638 	link_info->pause = resp->pause;
11639 	link_info->auto_mode = resp->auto_mode;
11640 	link_info->auto_pause_setting = resp->auto_pause;
11641 	link_info->lp_pause = resp->link_partner_adv_pause;
11642 	link_info->force_pause_setting = resp->force_pause;
11643 	link_info->duplex_setting = resp->duplex_cfg;
11644 	if (link_info->phy_link_status == BNXT_LINK_LINK) {
11645 		link_info->link_speed = le16_to_cpu(resp->link_speed);
11646 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2)
11647 			link_info->active_lanes = resp->active_lanes;
11648 	} else {
11649 		link_info->link_speed = 0;
11650 		link_info->active_lanes = 0;
11651 	}
11652 	link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
11653 	link_info->force_pam4_link_speed =
11654 		le16_to_cpu(resp->force_pam4_link_speed);
11655 	link_info->force_link_speed2 = le16_to_cpu(resp->force_link_speeds2);
11656 	link_info->support_speeds = le16_to_cpu(resp->support_speeds);
11657 	link_info->support_pam4_speeds = le16_to_cpu(resp->support_pam4_speeds);
11658 	link_info->support_speeds2 = le16_to_cpu(resp->support_speeds2);
11659 	link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
11660 	link_info->auto_pam4_link_speeds =
11661 		le16_to_cpu(resp->auto_pam4_link_speed_mask);
11662 	link_info->auto_link_speeds2 = le16_to_cpu(resp->auto_link_speeds2);
11663 	link_info->lp_auto_link_speeds =
11664 		le16_to_cpu(resp->link_partner_adv_speeds);
11665 	link_info->lp_auto_pam4_link_speeds =
11666 		resp->link_partner_pam4_adv_speeds;
11667 	link_info->preemphasis = le32_to_cpu(resp->preemphasis);
11668 	link_info->phy_ver[0] = resp->phy_maj;
11669 	link_info->phy_ver[1] = resp->phy_min;
11670 	link_info->phy_ver[2] = resp->phy_bld;
11671 	link_info->media_type = resp->media_type;
11672 	link_info->phy_type = resp->phy_type;
11673 	link_info->transceiver = resp->xcvr_pkg_type;
11674 	link_info->phy_addr = resp->eee_config_phy_addr &
11675 			      PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
11676 	link_info->module_status = resp->module_status;
11677 
11678 	if (bp->phy_flags & BNXT_PHY_FL_EEE_CAP) {
11679 		struct ethtool_keee *eee = &bp->eee;
11680 		u16 fw_speeds;
11681 
11682 		eee->eee_active = 0;
11683 		if (resp->eee_config_phy_addr &
11684 		    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
11685 			eee->eee_active = 1;
11686 			fw_speeds = le16_to_cpu(
11687 				resp->link_partner_adv_eee_link_speed_mask);
11688 			_bnxt_fw_to_linkmode(eee->lp_advertised, fw_speeds);
11689 		}
11690 
11691 		/* Pull initial EEE config */
11692 		if (!chng_link_state) {
11693 			if (resp->eee_config_phy_addr &
11694 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
11695 				eee->eee_enabled = 1;
11696 
11697 			fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
11698 			_bnxt_fw_to_linkmode(eee->advertised, fw_speeds);
11699 
11700 			if (resp->eee_config_phy_addr &
11701 			    PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
11702 				__le32 tmr;
11703 
11704 				eee->tx_lpi_enabled = 1;
11705 				tmr = resp->xcvr_identifier_type_tx_lpi_timer;
11706 				eee->tx_lpi_timer = le32_to_cpu(tmr) &
11707 					PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
11708 			}
11709 		}
11710 	}
11711 
11712 	link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
11713 	if (bp->hwrm_spec_code >= 0x10504) {
11714 		link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
11715 		link_info->active_fec_sig_mode = resp->active_fec_signal_mode;
11716 	}
11717 	/* TODO: need to add more logic to report VF link */
11718 	if (chng_link_state) {
11719 		if (link_info->phy_link_status == BNXT_LINK_LINK)
11720 			link_info->link_state = BNXT_LINK_STATE_UP;
11721 		else
11722 			link_info->link_state = BNXT_LINK_STATE_DOWN;
11723 		if (link_state != link_info->link_state)
11724 			bnxt_report_link(bp);
11725 	} else {
11726 		/* always link down if not require to update link state */
11727 		link_info->link_state = BNXT_LINK_STATE_DOWN;
11728 	}
11729 	hwrm_req_drop(bp, req);
11730 
11731 	if (!BNXT_PHY_CFG_ABLE(bp))
11732 		return 0;
11733 
11734 	support_changed = bnxt_support_speed_dropped(link_info);
11735 	if (support_changed && (link_info->autoneg & BNXT_AUTONEG_SPEED))
11736 		bnxt_hwrm_set_link_setting(bp, true, false);
11737 	return 0;
11738 }
11739 
11740 static void bnxt_get_port_module_status(struct bnxt *bp)
11741 {
11742 	struct bnxt_link_info *link_info = &bp->link_info;
11743 	struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
11744 	u8 module_status;
11745 
11746 	if (bnxt_update_link(bp, true))
11747 		return;
11748 
11749 	module_status = link_info->module_status;
11750 	switch (module_status) {
11751 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
11752 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
11753 	case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
11754 		netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
11755 			    bp->pf.port_id);
11756 		if (bp->hwrm_spec_code >= 0x10201) {
11757 			netdev_warn(bp->dev, "Module part number %s\n",
11758 				    resp->phy_vendor_partnumber);
11759 		}
11760 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
11761 			netdev_warn(bp->dev, "TX is disabled\n");
11762 		if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
11763 			netdev_warn(bp->dev, "SFP+ module is shutdown\n");
11764 	}
11765 }
11766 
11767 static void
11768 bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11769 {
11770 	if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
11771 		if (bp->hwrm_spec_code >= 0x10201)
11772 			req->auto_pause =
11773 				PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
11774 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11775 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
11776 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11777 			req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
11778 		req->enables |=
11779 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11780 	} else {
11781 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
11782 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
11783 		if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
11784 			req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
11785 		req->enables |=
11786 			cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
11787 		if (bp->hwrm_spec_code >= 0x10201) {
11788 			req->auto_pause = req->force_pause;
11789 			req->enables |= cpu_to_le32(
11790 				PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
11791 		}
11792 	}
11793 }
11794 
11795 static void bnxt_hwrm_set_link_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
11796 {
11797 	if (bp->link_info.autoneg & BNXT_AUTONEG_SPEED) {
11798 		req->auto_mode |= PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
11799 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11800 			req->enables |=
11801 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEEDS2_MASK);
11802 			req->auto_link_speeds2_mask = cpu_to_le16(bp->link_info.advertising);
11803 		} else if (bp->link_info.advertising) {
11804 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
11805 			req->auto_link_speed_mask = cpu_to_le16(bp->link_info.advertising);
11806 		}
11807 		if (bp->link_info.advertising_pam4) {
11808 			req->enables |=
11809 				cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAM4_LINK_SPEED_MASK);
11810 			req->auto_link_pam4_speed_mask =
11811 				cpu_to_le16(bp->link_info.advertising_pam4);
11812 		}
11813 		req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
11814 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
11815 	} else {
11816 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
11817 		if (bp->phy_flags & BNXT_PHY_FL_SPEEDS2) {
11818 			req->force_link_speeds2 = cpu_to_le16(bp->link_info.req_link_speed);
11819 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_LINK_SPEEDS2);
11820 			netif_info(bp, link, bp->dev, "Forcing FW speed2: %d\n",
11821 				   (u32)bp->link_info.req_link_speed);
11822 		} else if (bp->link_info.req_signal_mode == BNXT_SIG_MODE_PAM4) {
11823 			req->force_pam4_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11824 			req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAM4_LINK_SPEED);
11825 		} else {
11826 			req->force_link_speed = cpu_to_le16(bp->link_info.req_link_speed);
11827 		}
11828 	}
11829 
11830 	/* tell chimp that the setting takes effect immediately */
11831 	req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
11832 }
11833 
11834 int bnxt_hwrm_set_pause(struct bnxt *bp)
11835 {
11836 	struct hwrm_port_phy_cfg_input *req;
11837 	int rc;
11838 
11839 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11840 	if (rc)
11841 		return rc;
11842 
11843 	bnxt_hwrm_set_pause_common(bp, req);
11844 
11845 	if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
11846 	    bp->link_info.force_link_chng)
11847 		bnxt_hwrm_set_link_common(bp, req);
11848 
11849 	rc = hwrm_req_send(bp, req);
11850 	if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
11851 		/* since changing of pause setting doesn't trigger any link
11852 		 * change event, the driver needs to update the current pause
11853 		 * result upon successfully return of the phy_cfg command
11854 		 */
11855 		bp->link_info.pause =
11856 		bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
11857 		bp->link_info.auto_pause_setting = 0;
11858 		if (!bp->link_info.force_link_chng)
11859 			bnxt_report_link(bp);
11860 	}
11861 	bp->link_info.force_link_chng = false;
11862 	return rc;
11863 }
11864 
11865 static void bnxt_hwrm_set_eee(struct bnxt *bp,
11866 			      struct hwrm_port_phy_cfg_input *req)
11867 {
11868 	struct ethtool_keee *eee = &bp->eee;
11869 
11870 	if (eee->eee_enabled) {
11871 		u16 eee_speeds;
11872 		u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
11873 
11874 		if (eee->tx_lpi_enabled)
11875 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
11876 		else
11877 			flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
11878 
11879 		req->flags |= cpu_to_le32(flags);
11880 		eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
11881 		req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
11882 		req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
11883 	} else {
11884 		req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
11885 	}
11886 }
11887 
11888 int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
11889 {
11890 	struct hwrm_port_phy_cfg_input *req;
11891 	int rc;
11892 
11893 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11894 	if (rc)
11895 		return rc;
11896 
11897 	if (set_pause)
11898 		bnxt_hwrm_set_pause_common(bp, req);
11899 
11900 	bnxt_hwrm_set_link_common(bp, req);
11901 
11902 	if (set_eee)
11903 		bnxt_hwrm_set_eee(bp, req);
11904 	return hwrm_req_send(bp, req);
11905 }
11906 
11907 static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
11908 {
11909 	struct hwrm_port_phy_cfg_input *req;
11910 	int rc;
11911 
11912 	if (!BNXT_SINGLE_PF(bp))
11913 		return 0;
11914 
11915 	if (pci_num_vf(bp->pdev) &&
11916 	    !(bp->phy_flags & BNXT_PHY_FL_FW_MANAGED_LKDN))
11917 		return 0;
11918 
11919 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_CFG);
11920 	if (rc)
11921 		return rc;
11922 
11923 	req->flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
11924 	rc = hwrm_req_send(bp, req);
11925 	if (!rc) {
11926 		mutex_lock(&bp->link_lock);
11927 		/* Device is not obliged link down in certain scenarios, even
11928 		 * when forced. Setting the state unknown is consistent with
11929 		 * driver startup and will force link state to be reported
11930 		 * during subsequent open based on PORT_PHY_QCFG.
11931 		 */
11932 		bp->link_info.link_state = BNXT_LINK_STATE_UNKNOWN;
11933 		mutex_unlock(&bp->link_lock);
11934 	}
11935 	return rc;
11936 }
11937 
11938 static int bnxt_fw_reset_via_optee(struct bnxt *bp)
11939 {
11940 #ifdef CONFIG_TEE_BNXT_FW
11941 	int rc = tee_bnxt_fw_load();
11942 
11943 	if (rc)
11944 		netdev_err(bp->dev, "Failed FW reset via OP-TEE, rc=%d\n", rc);
11945 
11946 	return rc;
11947 #else
11948 	netdev_err(bp->dev, "OP-TEE not supported\n");
11949 	return -ENODEV;
11950 #endif
11951 }
11952 
11953 static int bnxt_try_recover_fw(struct bnxt *bp)
11954 {
11955 	if (bp->fw_health && bp->fw_health->status_reliable) {
11956 		int retry = 0, rc;
11957 		u32 sts;
11958 
11959 		do {
11960 			sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
11961 			rc = bnxt_hwrm_poll(bp);
11962 			if (!BNXT_FW_IS_BOOTING(sts) &&
11963 			    !BNXT_FW_IS_RECOVERING(sts))
11964 				break;
11965 			retry++;
11966 		} while (rc == -EBUSY && retry < BNXT_FW_RETRY);
11967 
11968 		if (!BNXT_FW_IS_HEALTHY(sts)) {
11969 			netdev_err(bp->dev,
11970 				   "Firmware not responding, status: 0x%x\n",
11971 				   sts);
11972 			rc = -ENODEV;
11973 		}
11974 		if (sts & FW_STATUS_REG_CRASHED_NO_MASTER) {
11975 			netdev_warn(bp->dev, "Firmware recover via OP-TEE requested\n");
11976 			return bnxt_fw_reset_via_optee(bp);
11977 		}
11978 		return rc;
11979 	}
11980 
11981 	return -ENODEV;
11982 }
11983 
11984 static void bnxt_clear_reservations(struct bnxt *bp, bool fw_reset)
11985 {
11986 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
11987 
11988 	if (!BNXT_NEW_RM(bp))
11989 		return; /* no resource reservations required */
11990 
11991 	hw_resc->resv_cp_rings = 0;
11992 	hw_resc->resv_stat_ctxs = 0;
11993 	hw_resc->resv_irqs = 0;
11994 	hw_resc->resv_tx_rings = 0;
11995 	hw_resc->resv_rx_rings = 0;
11996 	hw_resc->resv_hw_ring_grps = 0;
11997 	hw_resc->resv_vnics = 0;
11998 	hw_resc->resv_rsscos_ctxs = 0;
11999 	if (!fw_reset) {
12000 		bp->tx_nr_rings = 0;
12001 		bp->rx_nr_rings = 0;
12002 	}
12003 }
12004 
12005 int bnxt_cancel_reservations(struct bnxt *bp, bool fw_reset)
12006 {
12007 	int rc;
12008 
12009 	if (!BNXT_NEW_RM(bp))
12010 		return 0; /* no resource reservations required */
12011 
12012 	rc = bnxt_hwrm_func_resc_qcaps(bp, true);
12013 	if (rc)
12014 		netdev_err(bp->dev, "resc_qcaps failed\n");
12015 
12016 	bnxt_clear_reservations(bp, fw_reset);
12017 
12018 	return rc;
12019 }
12020 
12021 static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
12022 {
12023 	struct hwrm_func_drv_if_change_output *resp;
12024 	struct hwrm_func_drv_if_change_input *req;
12025 	bool fw_reset = !bp->irq_tbl;
12026 	bool resc_reinit = false;
12027 	int rc, retry = 0;
12028 	u32 flags = 0;
12029 
12030 	if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
12031 		return 0;
12032 
12033 	rc = hwrm_req_init(bp, req, HWRM_FUNC_DRV_IF_CHANGE);
12034 	if (rc)
12035 		return rc;
12036 
12037 	if (up)
12038 		req->flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
12039 	resp = hwrm_req_hold(bp, req);
12040 
12041 	hwrm_req_flags(bp, req, BNXT_HWRM_FULL_WAIT);
12042 	while (retry < BNXT_FW_IF_RETRY) {
12043 		rc = hwrm_req_send(bp, req);
12044 		if (rc != -EAGAIN)
12045 			break;
12046 
12047 		msleep(50);
12048 		retry++;
12049 	}
12050 
12051 	if (rc == -EAGAIN) {
12052 		hwrm_req_drop(bp, req);
12053 		return rc;
12054 	} else if (!rc) {
12055 		flags = le32_to_cpu(resp->flags);
12056 	} else if (up) {
12057 		rc = bnxt_try_recover_fw(bp);
12058 		fw_reset = true;
12059 	}
12060 	hwrm_req_drop(bp, req);
12061 	if (rc)
12062 		return rc;
12063 
12064 	if (!up) {
12065 		bnxt_inv_fw_health_reg(bp);
12066 		return 0;
12067 	}
12068 
12069 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)
12070 		resc_reinit = true;
12071 	if (flags & FUNC_DRV_IF_CHANGE_RESP_FLAGS_HOT_FW_RESET_DONE ||
12072 	    test_bit(BNXT_STATE_FW_RESET_DET, &bp->state))
12073 		fw_reset = true;
12074 	else
12075 		bnxt_remap_fw_health_regs(bp);
12076 
12077 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state) && !fw_reset) {
12078 		netdev_err(bp->dev, "RESET_DONE not set during FW reset.\n");
12079 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12080 		return -ENODEV;
12081 	}
12082 	if (resc_reinit || fw_reset) {
12083 		if (fw_reset) {
12084 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12085 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12086 				bnxt_ulp_irq_stop(bp);
12087 			bnxt_free_ctx_mem(bp, false);
12088 			bnxt_dcb_free(bp);
12089 			rc = bnxt_fw_init_one(bp);
12090 			if (rc) {
12091 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12092 				set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12093 				return rc;
12094 			}
12095 			bnxt_clear_int_mode(bp);
12096 			rc = bnxt_init_int_mode(bp);
12097 			if (rc) {
12098 				clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12099 				netdev_err(bp->dev, "init int mode failed\n");
12100 				return rc;
12101 			}
12102 		}
12103 		rc = bnxt_cancel_reservations(bp, fw_reset);
12104 	}
12105 	return rc;
12106 }
12107 
12108 static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
12109 {
12110 	struct hwrm_port_led_qcaps_output *resp;
12111 	struct hwrm_port_led_qcaps_input *req;
12112 	struct bnxt_pf_info *pf = &bp->pf;
12113 	int rc;
12114 
12115 	bp->num_leds = 0;
12116 	if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
12117 		return 0;
12118 
12119 	rc = hwrm_req_init(bp, req, HWRM_PORT_LED_QCAPS);
12120 	if (rc)
12121 		return rc;
12122 
12123 	req->port_id = cpu_to_le16(pf->port_id);
12124 	resp = hwrm_req_hold(bp, req);
12125 	rc = hwrm_req_send(bp, req);
12126 	if (rc) {
12127 		hwrm_req_drop(bp, req);
12128 		return rc;
12129 	}
12130 	if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
12131 		int i;
12132 
12133 		bp->num_leds = resp->num_leds;
12134 		memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
12135 						 bp->num_leds);
12136 		for (i = 0; i < bp->num_leds; i++) {
12137 			struct bnxt_led_info *led = &bp->leds[i];
12138 			__le16 caps = led->led_state_caps;
12139 
12140 			if (!led->led_group_id ||
12141 			    !BNXT_LED_ALT_BLINK_CAP(caps)) {
12142 				bp->num_leds = 0;
12143 				break;
12144 			}
12145 		}
12146 	}
12147 	hwrm_req_drop(bp, req);
12148 	return 0;
12149 }
12150 
12151 int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
12152 {
12153 	struct hwrm_wol_filter_alloc_output *resp;
12154 	struct hwrm_wol_filter_alloc_input *req;
12155 	int rc;
12156 
12157 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_ALLOC);
12158 	if (rc)
12159 		return rc;
12160 
12161 	req->port_id = cpu_to_le16(bp->pf.port_id);
12162 	req->wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
12163 	req->enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
12164 	memcpy(req->mac_address, bp->dev->dev_addr, ETH_ALEN);
12165 
12166 	resp = hwrm_req_hold(bp, req);
12167 	rc = hwrm_req_send(bp, req);
12168 	if (!rc)
12169 		bp->wol_filter_id = resp->wol_filter_id;
12170 	hwrm_req_drop(bp, req);
12171 	return rc;
12172 }
12173 
12174 int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
12175 {
12176 	struct hwrm_wol_filter_free_input *req;
12177 	int rc;
12178 
12179 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_FREE);
12180 	if (rc)
12181 		return rc;
12182 
12183 	req->port_id = cpu_to_le16(bp->pf.port_id);
12184 	req->enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
12185 	req->wol_filter_id = bp->wol_filter_id;
12186 
12187 	return hwrm_req_send(bp, req);
12188 }
12189 
12190 static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
12191 {
12192 	struct hwrm_wol_filter_qcfg_output *resp;
12193 	struct hwrm_wol_filter_qcfg_input *req;
12194 	u16 next_handle = 0;
12195 	int rc;
12196 
12197 	rc = hwrm_req_init(bp, req, HWRM_WOL_FILTER_QCFG);
12198 	if (rc)
12199 		return rc;
12200 
12201 	req->port_id = cpu_to_le16(bp->pf.port_id);
12202 	req->handle = cpu_to_le16(handle);
12203 	resp = hwrm_req_hold(bp, req);
12204 	rc = hwrm_req_send(bp, req);
12205 	if (!rc) {
12206 		next_handle = le16_to_cpu(resp->next_handle);
12207 		if (next_handle != 0) {
12208 			if (resp->wol_type ==
12209 			    WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
12210 				bp->wol = 1;
12211 				bp->wol_filter_id = resp->wol_filter_id;
12212 			}
12213 		}
12214 	}
12215 	hwrm_req_drop(bp, req);
12216 	return next_handle;
12217 }
12218 
12219 static void bnxt_get_wol_settings(struct bnxt *bp)
12220 {
12221 	u16 handle = 0;
12222 
12223 	bp->wol = 0;
12224 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
12225 		return;
12226 
12227 	do {
12228 		handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
12229 	} while (handle && handle != 0xffff);
12230 }
12231 
12232 static bool bnxt_eee_config_ok(struct bnxt *bp)
12233 {
12234 	struct ethtool_keee *eee = &bp->eee;
12235 	struct bnxt_link_info *link_info = &bp->link_info;
12236 
12237 	if (!(bp->phy_flags & BNXT_PHY_FL_EEE_CAP))
12238 		return true;
12239 
12240 	if (eee->eee_enabled) {
12241 		__ETHTOOL_DECLARE_LINK_MODE_MASK(advertising);
12242 		__ETHTOOL_DECLARE_LINK_MODE_MASK(tmp);
12243 
12244 		_bnxt_fw_to_linkmode(advertising, link_info->advertising);
12245 
12246 		if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12247 			eee->eee_enabled = 0;
12248 			return false;
12249 		}
12250 		if (linkmode_andnot(tmp, eee->advertised, advertising)) {
12251 			linkmode_and(eee->advertised, advertising,
12252 				     eee->supported);
12253 			return false;
12254 		}
12255 	}
12256 	return true;
12257 }
12258 
12259 static int bnxt_update_phy_setting(struct bnxt *bp)
12260 {
12261 	int rc;
12262 	bool update_link = false;
12263 	bool update_pause = false;
12264 	bool update_eee = false;
12265 	struct bnxt_link_info *link_info = &bp->link_info;
12266 
12267 	rc = bnxt_update_link(bp, true);
12268 	if (rc) {
12269 		netdev_err(bp->dev, "failed to update link (rc: %x)\n",
12270 			   rc);
12271 		return rc;
12272 	}
12273 	if (!BNXT_SINGLE_PF(bp))
12274 		return 0;
12275 
12276 	if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12277 	    (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
12278 	    link_info->req_flow_ctrl)
12279 		update_pause = true;
12280 	if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
12281 	    link_info->force_pause_setting != link_info->req_flow_ctrl)
12282 		update_pause = true;
12283 	if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
12284 		if (BNXT_AUTO_MODE(link_info->auto_mode))
12285 			update_link = true;
12286 		if (bnxt_force_speed_updated(link_info))
12287 			update_link = true;
12288 		if (link_info->req_duplex != link_info->duplex_setting)
12289 			update_link = true;
12290 	} else {
12291 		if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
12292 			update_link = true;
12293 		if (bnxt_auto_speed_updated(link_info))
12294 			update_link = true;
12295 	}
12296 
12297 	/* The last close may have shutdown the link, so need to call
12298 	 * PHY_CFG to bring it back up.
12299 	 */
12300 	if (!BNXT_LINK_IS_UP(bp))
12301 		update_link = true;
12302 
12303 	if (!bnxt_eee_config_ok(bp))
12304 		update_eee = true;
12305 
12306 	if (update_link)
12307 		rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
12308 	else if (update_pause)
12309 		rc = bnxt_hwrm_set_pause(bp);
12310 	if (rc) {
12311 		netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
12312 			   rc);
12313 		return rc;
12314 	}
12315 
12316 	return rc;
12317 }
12318 
12319 static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
12320 
12321 static int bnxt_reinit_after_abort(struct bnxt *bp)
12322 {
12323 	int rc;
12324 
12325 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12326 		return -EBUSY;
12327 
12328 	if (bp->dev->reg_state == NETREG_UNREGISTERED)
12329 		return -ENODEV;
12330 
12331 	rc = bnxt_fw_init_one(bp);
12332 	if (!rc) {
12333 		bnxt_clear_int_mode(bp);
12334 		rc = bnxt_init_int_mode(bp);
12335 		if (!rc) {
12336 			clear_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12337 			set_bit(BNXT_STATE_FW_RESET_DET, &bp->state);
12338 		}
12339 	}
12340 	return rc;
12341 }
12342 
12343 static void bnxt_cfg_one_usr_fltr(struct bnxt *bp, struct bnxt_filter_base *fltr)
12344 {
12345 	struct bnxt_ntuple_filter *ntp_fltr;
12346 	struct bnxt_l2_filter *l2_fltr;
12347 
12348 	if (list_empty(&fltr->list))
12349 		return;
12350 
12351 	if (fltr->type == BNXT_FLTR_TYPE_NTUPLE) {
12352 		ntp_fltr = container_of(fltr, struct bnxt_ntuple_filter, base);
12353 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
12354 		atomic_inc(&l2_fltr->refcnt);
12355 		ntp_fltr->l2_fltr = l2_fltr;
12356 		if (bnxt_hwrm_cfa_ntuple_filter_alloc(bp, ntp_fltr)) {
12357 			bnxt_del_ntp_filter(bp, ntp_fltr);
12358 			netdev_err(bp->dev, "restoring previously configured ntuple filter id %d failed\n",
12359 				   fltr->sw_id);
12360 		}
12361 	} else if (fltr->type == BNXT_FLTR_TYPE_L2) {
12362 		l2_fltr = container_of(fltr, struct bnxt_l2_filter, base);
12363 		if (bnxt_hwrm_l2_filter_alloc(bp, l2_fltr)) {
12364 			bnxt_del_l2_filter(bp, l2_fltr);
12365 			netdev_err(bp->dev, "restoring previously configured l2 filter id %d failed\n",
12366 				   fltr->sw_id);
12367 		}
12368 	}
12369 }
12370 
12371 static void bnxt_cfg_usr_fltrs(struct bnxt *bp)
12372 {
12373 	struct bnxt_filter_base *usr_fltr, *tmp;
12374 
12375 	list_for_each_entry_safe(usr_fltr, tmp, &bp->usr_fltr_list, list)
12376 		bnxt_cfg_one_usr_fltr(bp, usr_fltr);
12377 }
12378 
12379 static int bnxt_set_xps_mapping(struct bnxt *bp)
12380 {
12381 	int numa_node = dev_to_node(&bp->pdev->dev);
12382 	unsigned int q_idx, map_idx, cpu, i;
12383 	const struct cpumask *cpu_mask_ptr;
12384 	int nr_cpus = num_online_cpus();
12385 	cpumask_t *q_map;
12386 	int rc = 0;
12387 
12388 	q_map = kcalloc(bp->tx_nr_rings_per_tc, sizeof(*q_map), GFP_KERNEL);
12389 	if (!q_map)
12390 		return -ENOMEM;
12391 
12392 	/* Create CPU mask for all TX queues across MQPRIO traffic classes.
12393 	 * Each TC has the same number of TX queues. The nth TX queue for each
12394 	 * TC will have the same CPU mask.
12395 	 */
12396 	for (i = 0; i < nr_cpus; i++) {
12397 		map_idx = i % bp->tx_nr_rings_per_tc;
12398 		cpu = cpumask_local_spread(i, numa_node);
12399 		cpu_mask_ptr = get_cpu_mask(cpu);
12400 		cpumask_or(&q_map[map_idx], &q_map[map_idx], cpu_mask_ptr);
12401 	}
12402 
12403 	/* Register CPU mask for each TX queue except the ones marked for XDP */
12404 	for (q_idx = 0; q_idx < bp->dev->real_num_tx_queues; q_idx++) {
12405 		map_idx = q_idx % bp->tx_nr_rings_per_tc;
12406 		rc = netif_set_xps_queue(bp->dev, &q_map[map_idx], q_idx);
12407 		if (rc) {
12408 			netdev_warn(bp->dev, "Error setting XPS for q:%d\n",
12409 				    q_idx);
12410 			break;
12411 		}
12412 	}
12413 
12414 	kfree(q_map);
12415 
12416 	return rc;
12417 }
12418 
12419 static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12420 {
12421 	int rc = 0;
12422 
12423 	netif_carrier_off(bp->dev);
12424 	if (irq_re_init) {
12425 		/* Reserve rings now if none were reserved at driver probe. */
12426 		rc = bnxt_init_dflt_ring_mode(bp);
12427 		if (rc) {
12428 			netdev_err(bp->dev, "Failed to reserve default rings at open\n");
12429 			return rc;
12430 		}
12431 	}
12432 	rc = bnxt_reserve_rings(bp, irq_re_init);
12433 	if (rc)
12434 		return rc;
12435 
12436 	rc = bnxt_alloc_mem(bp, irq_re_init);
12437 	if (rc) {
12438 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12439 		goto open_err_free_mem;
12440 	}
12441 
12442 	if (irq_re_init) {
12443 		bnxt_init_napi(bp);
12444 		rc = bnxt_request_irq(bp);
12445 		if (rc) {
12446 			netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
12447 			goto open_err_irq;
12448 		}
12449 	}
12450 
12451 	rc = bnxt_init_nic(bp, irq_re_init);
12452 	if (rc) {
12453 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12454 		goto open_err_irq;
12455 	}
12456 
12457 	bnxt_enable_napi(bp);
12458 	bnxt_debug_dev_init(bp);
12459 
12460 	if (link_re_init) {
12461 		mutex_lock(&bp->link_lock);
12462 		rc = bnxt_update_phy_setting(bp);
12463 		mutex_unlock(&bp->link_lock);
12464 		if (rc) {
12465 			netdev_warn(bp->dev, "failed to update phy settings\n");
12466 			if (BNXT_SINGLE_PF(bp)) {
12467 				bp->link_info.phy_retry = true;
12468 				bp->link_info.phy_retry_expires =
12469 					jiffies + 5 * HZ;
12470 			}
12471 		}
12472 	}
12473 
12474 	if (irq_re_init) {
12475 		udp_tunnel_nic_reset_ntf(bp->dev);
12476 		rc = bnxt_set_xps_mapping(bp);
12477 		if (rc)
12478 			netdev_warn(bp->dev, "failed to set xps mapping\n");
12479 	}
12480 
12481 	if (bp->tx_nr_rings_xdp < num_possible_cpus()) {
12482 		if (!static_key_enabled(&bnxt_xdp_locking_key))
12483 			static_branch_enable(&bnxt_xdp_locking_key);
12484 	} else if (static_key_enabled(&bnxt_xdp_locking_key)) {
12485 		static_branch_disable(&bnxt_xdp_locking_key);
12486 	}
12487 	set_bit(BNXT_STATE_OPEN, &bp->state);
12488 	bnxt_enable_int(bp);
12489 	/* Enable TX queues */
12490 	bnxt_tx_enable(bp);
12491 	mod_timer(&bp->timer, jiffies + bp->current_interval);
12492 	/* Poll link status and check for SFP+ module status */
12493 	mutex_lock(&bp->link_lock);
12494 	bnxt_get_port_module_status(bp);
12495 	mutex_unlock(&bp->link_lock);
12496 
12497 	/* VF-reps may need to be re-opened after the PF is re-opened */
12498 	if (BNXT_PF(bp))
12499 		bnxt_vf_reps_open(bp);
12500 	if (bp->ptp_cfg && !(bp->fw_cap & BNXT_FW_CAP_TX_TS_CMP))
12501 		WRITE_ONCE(bp->ptp_cfg->tx_avail, BNXT_MAX_TX_TS);
12502 	bnxt_ptp_init_rtc(bp, true);
12503 	bnxt_ptp_cfg_tstamp_filters(bp);
12504 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12505 		bnxt_hwrm_realloc_rss_ctx_vnic(bp);
12506 	bnxt_cfg_usr_fltrs(bp);
12507 	return 0;
12508 
12509 open_err_irq:
12510 	bnxt_del_napi(bp);
12511 
12512 open_err_free_mem:
12513 	bnxt_free_skbs(bp);
12514 	bnxt_free_irq(bp);
12515 	bnxt_free_mem(bp, true);
12516 	return rc;
12517 }
12518 
12519 /* rtnl_lock held */
12520 int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12521 {
12522 	int rc = 0;
12523 
12524 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state))
12525 		rc = -EIO;
12526 	if (!rc)
12527 		rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
12528 	if (rc) {
12529 		netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
12530 		dev_close(bp->dev);
12531 	}
12532 	return rc;
12533 }
12534 
12535 /* rtnl_lock held, open the NIC half way by allocating all resources, but
12536  * NAPI, IRQ, and TX are not enabled.  This is mainly used for offline
12537  * self tests.
12538  */
12539 int bnxt_half_open_nic(struct bnxt *bp)
12540 {
12541 	int rc = 0;
12542 
12543 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12544 		netdev_err(bp->dev, "A previous firmware reset has not completed, aborting half open\n");
12545 		rc = -ENODEV;
12546 		goto half_open_err;
12547 	}
12548 
12549 	rc = bnxt_alloc_mem(bp, true);
12550 	if (rc) {
12551 		netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
12552 		goto half_open_err;
12553 	}
12554 	bnxt_init_napi(bp);
12555 	set_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12556 	rc = bnxt_init_nic(bp, true);
12557 	if (rc) {
12558 		clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12559 		bnxt_del_napi(bp);
12560 		netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
12561 		goto half_open_err;
12562 	}
12563 	return 0;
12564 
12565 half_open_err:
12566 	bnxt_free_skbs(bp);
12567 	bnxt_free_mem(bp, true);
12568 	dev_close(bp->dev);
12569 	return rc;
12570 }
12571 
12572 /* rtnl_lock held, this call can only be made after a previous successful
12573  * call to bnxt_half_open_nic().
12574  */
12575 void bnxt_half_close_nic(struct bnxt *bp)
12576 {
12577 	bnxt_hwrm_resource_free(bp, false, true);
12578 	bnxt_del_napi(bp);
12579 	bnxt_free_skbs(bp);
12580 	bnxt_free_mem(bp, true);
12581 	clear_bit(BNXT_STATE_HALF_OPEN, &bp->state);
12582 }
12583 
12584 void bnxt_reenable_sriov(struct bnxt *bp)
12585 {
12586 	if (BNXT_PF(bp)) {
12587 		struct bnxt_pf_info *pf = &bp->pf;
12588 		int n = pf->active_vfs;
12589 
12590 		if (n)
12591 			bnxt_cfg_hw_sriov(bp, &n, true);
12592 	}
12593 }
12594 
12595 static int bnxt_open(struct net_device *dev)
12596 {
12597 	struct bnxt *bp = netdev_priv(dev);
12598 	int rc;
12599 
12600 	if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
12601 		rc = bnxt_reinit_after_abort(bp);
12602 		if (rc) {
12603 			if (rc == -EBUSY)
12604 				netdev_err(bp->dev, "A previous firmware reset has not completed, aborting\n");
12605 			else
12606 				netdev_err(bp->dev, "Failed to reinitialize after aborted firmware reset\n");
12607 			return -ENODEV;
12608 		}
12609 	}
12610 
12611 	rc = bnxt_hwrm_if_change(bp, true);
12612 	if (rc)
12613 		return rc;
12614 
12615 	rc = __bnxt_open_nic(bp, true, true);
12616 	if (rc) {
12617 		bnxt_hwrm_if_change(bp, false);
12618 	} else {
12619 		if (test_and_clear_bit(BNXT_STATE_FW_RESET_DET, &bp->state)) {
12620 			if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
12621 				bnxt_queue_sp_work(bp,
12622 						   BNXT_RESTART_ULP_SP_EVENT);
12623 		}
12624 	}
12625 
12626 	return rc;
12627 }
12628 
12629 static bool bnxt_drv_busy(struct bnxt *bp)
12630 {
12631 	return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
12632 		test_bit(BNXT_STATE_READ_STATS, &bp->state));
12633 }
12634 
12635 static void bnxt_get_ring_stats(struct bnxt *bp,
12636 				struct rtnl_link_stats64 *stats);
12637 
12638 static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
12639 			     bool link_re_init)
12640 {
12641 	/* Close the VF-reps before closing PF */
12642 	if (BNXT_PF(bp))
12643 		bnxt_vf_reps_close(bp);
12644 
12645 	/* Change device state to avoid TX queue wake up's */
12646 	bnxt_tx_disable(bp);
12647 
12648 	clear_bit(BNXT_STATE_OPEN, &bp->state);
12649 	smp_mb__after_atomic();
12650 	while (bnxt_drv_busy(bp))
12651 		msleep(20);
12652 
12653 	if (BNXT_SUPPORTS_MULTI_RSS_CTX(bp))
12654 		bnxt_clear_rss_ctxs(bp);
12655 	/* Flush rings and disable interrupts */
12656 	bnxt_shutdown_nic(bp, irq_re_init);
12657 
12658 	/* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
12659 
12660 	bnxt_debug_dev_exit(bp);
12661 	bnxt_disable_napi(bp);
12662 	del_timer_sync(&bp->timer);
12663 	bnxt_free_skbs(bp);
12664 
12665 	/* Save ring stats before shutdown */
12666 	if (bp->bnapi && irq_re_init) {
12667 		bnxt_get_ring_stats(bp, &bp->net_stats_prev);
12668 		bnxt_get_ring_err_stats(bp, &bp->ring_err_stats_prev);
12669 	}
12670 	if (irq_re_init) {
12671 		bnxt_free_irq(bp);
12672 		bnxt_del_napi(bp);
12673 	}
12674 	bnxt_free_mem(bp, irq_re_init);
12675 }
12676 
12677 void bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
12678 {
12679 	if (test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
12680 		/* If we get here, it means firmware reset is in progress
12681 		 * while we are trying to close.  We can safely proceed with
12682 		 * the close because we are holding rtnl_lock().  Some firmware
12683 		 * messages may fail as we proceed to close.  We set the
12684 		 * ABORT_ERR flag here so that the FW reset thread will later
12685 		 * abort when it gets the rtnl_lock() and sees the flag.
12686 		 */
12687 		netdev_warn(bp->dev, "FW reset in progress during close, FW reset will be aborted\n");
12688 		set_bit(BNXT_STATE_ABORT_ERR, &bp->state);
12689 	}
12690 
12691 #ifdef CONFIG_BNXT_SRIOV
12692 	if (bp->sriov_cfg) {
12693 		int rc;
12694 
12695 		rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
12696 						      !bp->sriov_cfg,
12697 						      BNXT_SRIOV_CFG_WAIT_TMO);
12698 		if (!rc)
12699 			netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete, proceeding to close!\n");
12700 		else if (rc < 0)
12701 			netdev_warn(bp->dev, "SRIOV config operation interrupted, proceeding to close!\n");
12702 	}
12703 #endif
12704 	__bnxt_close_nic(bp, irq_re_init, link_re_init);
12705 }
12706 
12707 static int bnxt_close(struct net_device *dev)
12708 {
12709 	struct bnxt *bp = netdev_priv(dev);
12710 
12711 	bnxt_close_nic(bp, true, true);
12712 	bnxt_hwrm_shutdown_link(bp);
12713 	bnxt_hwrm_if_change(bp, false);
12714 	return 0;
12715 }
12716 
12717 static int bnxt_hwrm_port_phy_read(struct bnxt *bp, u16 phy_addr, u16 reg,
12718 				   u16 *val)
12719 {
12720 	struct hwrm_port_phy_mdio_read_output *resp;
12721 	struct hwrm_port_phy_mdio_read_input *req;
12722 	int rc;
12723 
12724 	if (bp->hwrm_spec_code < 0x10a00)
12725 		return -EOPNOTSUPP;
12726 
12727 	rc = hwrm_req_init(bp, req, HWRM_PORT_PHY_MDIO_READ);
12728 	if (rc)
12729 		return rc;
12730 
12731 	req->port_id = cpu_to_le16(bp->pf.port_id);
12732 	req->phy_addr = phy_addr;
12733 	req->reg_addr = cpu_to_le16(reg & 0x1f);
12734 	if (mdio_phy_id_is_c45(phy_addr)) {
12735 		req->cl45_mdio = 1;
12736 		req->phy_addr = mdio_phy_id_prtad(phy_addr);
12737 		req->dev_addr = mdio_phy_id_devad(phy_addr);
12738 		req->reg_addr = cpu_to_le16(reg);
12739 	}
12740 
12741 	resp = hwrm_req_hold(bp, req);
12742 	rc = hwrm_req_send(bp, req);
12743 	if (!rc)
12744 		*val = le16_to_cpu(resp->reg_data);
12745 	hwrm_req_drop(bp, req);
12746 	return rc;
12747 }
12748 
12749 static int bnxt_hwrm_port_phy_write(struct bnxt *bp, u16 phy_addr, u16 reg,
12750 				    u16 val)
12751 {
12752 	struct hwrm_port_phy_mdio_write_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_WRITE);
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 	req->reg_data = cpu_to_le16(val);
12772 
12773 	return hwrm_req_send(bp, req);
12774 }
12775 
12776 /* rtnl_lock held */
12777 static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
12778 {
12779 	struct mii_ioctl_data *mdio = if_mii(ifr);
12780 	struct bnxt *bp = netdev_priv(dev);
12781 	int rc;
12782 
12783 	switch (cmd) {
12784 	case SIOCGMIIPHY:
12785 		mdio->phy_id = bp->link_info.phy_addr;
12786 
12787 		fallthrough;
12788 	case SIOCGMIIREG: {
12789 		u16 mii_regval = 0;
12790 
12791 		if (!netif_running(dev))
12792 			return -EAGAIN;
12793 
12794 		rc = bnxt_hwrm_port_phy_read(bp, mdio->phy_id, mdio->reg_num,
12795 					     &mii_regval);
12796 		mdio->val_out = mii_regval;
12797 		return rc;
12798 	}
12799 
12800 	case SIOCSMIIREG:
12801 		if (!netif_running(dev))
12802 			return -EAGAIN;
12803 
12804 		return bnxt_hwrm_port_phy_write(bp, mdio->phy_id, mdio->reg_num,
12805 						mdio->val_in);
12806 
12807 	case SIOCSHWTSTAMP:
12808 		return bnxt_hwtstamp_set(dev, ifr);
12809 
12810 	case SIOCGHWTSTAMP:
12811 		return bnxt_hwtstamp_get(dev, ifr);
12812 
12813 	default:
12814 		/* do nothing */
12815 		break;
12816 	}
12817 	return -EOPNOTSUPP;
12818 }
12819 
12820 static void bnxt_get_ring_stats(struct bnxt *bp,
12821 				struct rtnl_link_stats64 *stats)
12822 {
12823 	int i;
12824 
12825 	for (i = 0; i < bp->cp_nr_rings; i++) {
12826 		struct bnxt_napi *bnapi = bp->bnapi[i];
12827 		struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
12828 		u64 *sw = cpr->stats.sw_stats;
12829 
12830 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
12831 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12832 		stats->rx_packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
12833 
12834 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
12835 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
12836 		stats->tx_packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
12837 
12838 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
12839 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
12840 		stats->rx_bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
12841 
12842 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
12843 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
12844 		stats->tx_bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
12845 
12846 		stats->rx_missed_errors +=
12847 			BNXT_GET_RING_STATS64(sw, rx_discard_pkts);
12848 
12849 		stats->multicast += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
12850 
12851 		stats->tx_dropped += BNXT_GET_RING_STATS64(sw, tx_error_pkts);
12852 
12853 		stats->rx_dropped +=
12854 			cpr->sw_stats->rx.rx_netpoll_discards +
12855 			cpr->sw_stats->rx.rx_oom_discards;
12856 	}
12857 }
12858 
12859 static void bnxt_add_prev_stats(struct bnxt *bp,
12860 				struct rtnl_link_stats64 *stats)
12861 {
12862 	struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
12863 
12864 	stats->rx_packets += prev_stats->rx_packets;
12865 	stats->tx_packets += prev_stats->tx_packets;
12866 	stats->rx_bytes += prev_stats->rx_bytes;
12867 	stats->tx_bytes += prev_stats->tx_bytes;
12868 	stats->rx_missed_errors += prev_stats->rx_missed_errors;
12869 	stats->multicast += prev_stats->multicast;
12870 	stats->rx_dropped += prev_stats->rx_dropped;
12871 	stats->tx_dropped += prev_stats->tx_dropped;
12872 }
12873 
12874 static void
12875 bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
12876 {
12877 	struct bnxt *bp = netdev_priv(dev);
12878 
12879 	set_bit(BNXT_STATE_READ_STATS, &bp->state);
12880 	/* Make sure bnxt_close_nic() sees that we are reading stats before
12881 	 * we check the BNXT_STATE_OPEN flag.
12882 	 */
12883 	smp_mb__after_atomic();
12884 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
12885 		clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12886 		*stats = bp->net_stats_prev;
12887 		return;
12888 	}
12889 
12890 	bnxt_get_ring_stats(bp, stats);
12891 	bnxt_add_prev_stats(bp, stats);
12892 
12893 	if (bp->flags & BNXT_FLAG_PORT_STATS) {
12894 		u64 *rx = bp->port_stats.sw_stats;
12895 		u64 *tx = bp->port_stats.sw_stats +
12896 			  BNXT_TX_PORT_STATS_BYTE_OFFSET / 8;
12897 
12898 		stats->rx_crc_errors =
12899 			BNXT_GET_RX_PORT_STATS64(rx, rx_fcs_err_frames);
12900 		stats->rx_frame_errors =
12901 			BNXT_GET_RX_PORT_STATS64(rx, rx_align_err_frames);
12902 		stats->rx_length_errors =
12903 			BNXT_GET_RX_PORT_STATS64(rx, rx_undrsz_frames) +
12904 			BNXT_GET_RX_PORT_STATS64(rx, rx_ovrsz_frames) +
12905 			BNXT_GET_RX_PORT_STATS64(rx, rx_runt_frames);
12906 		stats->rx_errors =
12907 			BNXT_GET_RX_PORT_STATS64(rx, rx_false_carrier_frames) +
12908 			BNXT_GET_RX_PORT_STATS64(rx, rx_jbr_frames);
12909 		stats->collisions =
12910 			BNXT_GET_TX_PORT_STATS64(tx, tx_total_collisions);
12911 		stats->tx_fifo_errors =
12912 			BNXT_GET_TX_PORT_STATS64(tx, tx_fifo_underruns);
12913 		stats->tx_errors = BNXT_GET_TX_PORT_STATS64(tx, tx_err);
12914 	}
12915 	clear_bit(BNXT_STATE_READ_STATS, &bp->state);
12916 }
12917 
12918 static void bnxt_get_one_ring_err_stats(struct bnxt *bp,
12919 					struct bnxt_total_ring_err_stats *stats,
12920 					struct bnxt_cp_ring_info *cpr)
12921 {
12922 	struct bnxt_sw_stats *sw_stats = cpr->sw_stats;
12923 	u64 *hw_stats = cpr->stats.sw_stats;
12924 
12925 	stats->rx_total_l4_csum_errors += sw_stats->rx.rx_l4_csum_errors;
12926 	stats->rx_total_resets += sw_stats->rx.rx_resets;
12927 	stats->rx_total_buf_errors += sw_stats->rx.rx_buf_errors;
12928 	stats->rx_total_oom_discards += sw_stats->rx.rx_oom_discards;
12929 	stats->rx_total_netpoll_discards += sw_stats->rx.rx_netpoll_discards;
12930 	stats->rx_total_ring_discards +=
12931 		BNXT_GET_RING_STATS64(hw_stats, rx_discard_pkts);
12932 	stats->tx_total_resets += sw_stats->tx.tx_resets;
12933 	stats->tx_total_ring_discards +=
12934 		BNXT_GET_RING_STATS64(hw_stats, tx_discard_pkts);
12935 	stats->total_missed_irqs += sw_stats->cmn.missed_irqs;
12936 }
12937 
12938 void bnxt_get_ring_err_stats(struct bnxt *bp,
12939 			     struct bnxt_total_ring_err_stats *stats)
12940 {
12941 	int i;
12942 
12943 	for (i = 0; i < bp->cp_nr_rings; i++)
12944 		bnxt_get_one_ring_err_stats(bp, stats, &bp->bnapi[i]->cp_ring);
12945 }
12946 
12947 static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
12948 {
12949 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12950 	struct net_device *dev = bp->dev;
12951 	struct netdev_hw_addr *ha;
12952 	u8 *haddr;
12953 	int mc_count = 0;
12954 	bool update = false;
12955 	int off = 0;
12956 
12957 	netdev_for_each_mc_addr(ha, dev) {
12958 		if (mc_count >= BNXT_MAX_MC_ADDRS) {
12959 			*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
12960 			vnic->mc_list_count = 0;
12961 			return false;
12962 		}
12963 		haddr = ha->addr;
12964 		if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
12965 			memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
12966 			update = true;
12967 		}
12968 		off += ETH_ALEN;
12969 		mc_count++;
12970 	}
12971 	if (mc_count)
12972 		*rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
12973 
12974 	if (mc_count != vnic->mc_list_count) {
12975 		vnic->mc_list_count = mc_count;
12976 		update = true;
12977 	}
12978 	return update;
12979 }
12980 
12981 static bool bnxt_uc_list_updated(struct bnxt *bp)
12982 {
12983 	struct net_device *dev = bp->dev;
12984 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
12985 	struct netdev_hw_addr *ha;
12986 	int off = 0;
12987 
12988 	if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
12989 		return true;
12990 
12991 	netdev_for_each_uc_addr(ha, dev) {
12992 		if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
12993 			return true;
12994 
12995 		off += ETH_ALEN;
12996 	}
12997 	return false;
12998 }
12999 
13000 static void bnxt_set_rx_mode(struct net_device *dev)
13001 {
13002 	struct bnxt *bp = netdev_priv(dev);
13003 	struct bnxt_vnic_info *vnic;
13004 	bool mc_update = false;
13005 	bool uc_update;
13006 	u32 mask;
13007 
13008 	if (!test_bit(BNXT_STATE_OPEN, &bp->state))
13009 		return;
13010 
13011 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13012 	mask = vnic->rx_mask;
13013 	mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
13014 		  CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
13015 		  CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
13016 		  CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
13017 
13018 	if (dev->flags & IFF_PROMISC)
13019 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13020 
13021 	uc_update = bnxt_uc_list_updated(bp);
13022 
13023 	if (dev->flags & IFF_BROADCAST)
13024 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
13025 	if (dev->flags & IFF_ALLMULTI) {
13026 		mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13027 		vnic->mc_list_count = 0;
13028 	} else if (dev->flags & IFF_MULTICAST) {
13029 		mc_update = bnxt_mc_list_updated(bp, &mask);
13030 	}
13031 
13032 	if (mask != vnic->rx_mask || uc_update || mc_update) {
13033 		vnic->rx_mask = mask;
13034 
13035 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13036 	}
13037 }
13038 
13039 static int bnxt_cfg_rx_mode(struct bnxt *bp)
13040 {
13041 	struct net_device *dev = bp->dev;
13042 	struct bnxt_vnic_info *vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
13043 	struct netdev_hw_addr *ha;
13044 	int i, off = 0, rc;
13045 	bool uc_update;
13046 
13047 	netif_addr_lock_bh(dev);
13048 	uc_update = bnxt_uc_list_updated(bp);
13049 	netif_addr_unlock_bh(dev);
13050 
13051 	if (!uc_update)
13052 		goto skip_uc;
13053 
13054 	for (i = 1; i < vnic->uc_filter_count; i++) {
13055 		struct bnxt_l2_filter *fltr = vnic->l2_filters[i];
13056 
13057 		bnxt_hwrm_l2_filter_free(bp, fltr);
13058 		bnxt_del_l2_filter(bp, fltr);
13059 	}
13060 
13061 	vnic->uc_filter_count = 1;
13062 
13063 	netif_addr_lock_bh(dev);
13064 	if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
13065 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13066 	} else {
13067 		netdev_for_each_uc_addr(ha, dev) {
13068 			memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
13069 			off += ETH_ALEN;
13070 			vnic->uc_filter_count++;
13071 		}
13072 	}
13073 	netif_addr_unlock_bh(dev);
13074 
13075 	for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
13076 		rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
13077 		if (rc) {
13078 			if (BNXT_VF(bp) && rc == -ENODEV) {
13079 				if (!test_and_set_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13080 					netdev_warn(bp->dev, "Cannot configure L2 filters while PF is unavailable, will retry\n");
13081 				else
13082 					netdev_dbg(bp->dev, "PF still unavailable while configuring L2 filters.\n");
13083 				rc = 0;
13084 			} else {
13085 				netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
13086 			}
13087 			vnic->uc_filter_count = i;
13088 			return rc;
13089 		}
13090 	}
13091 	if (test_and_clear_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13092 		netdev_notice(bp->dev, "Retry of L2 filter configuration successful.\n");
13093 
13094 skip_uc:
13095 	if ((vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS) &&
13096 	    !bnxt_promisc_ok(bp))
13097 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
13098 	rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13099 	if (rc && (vnic->rx_mask & CFA_L2_SET_RX_MASK_REQ_MASK_MCAST)) {
13100 		netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
13101 			    rc);
13102 		vnic->rx_mask &= ~CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
13103 		vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
13104 		vnic->mc_list_count = 0;
13105 		rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
13106 	}
13107 	if (rc)
13108 		netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
13109 			   rc);
13110 
13111 	return rc;
13112 }
13113 
13114 static bool bnxt_can_reserve_rings(struct bnxt *bp)
13115 {
13116 #ifdef CONFIG_BNXT_SRIOV
13117 	if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
13118 		struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
13119 
13120 		/* No minimum rings were provisioned by the PF.  Don't
13121 		 * reserve rings by default when device is down.
13122 		 */
13123 		if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
13124 			return true;
13125 
13126 		if (!netif_running(bp->dev))
13127 			return false;
13128 	}
13129 #endif
13130 	return true;
13131 }
13132 
13133 /* If the chip and firmware supports RFS */
13134 static bool bnxt_rfs_supported(struct bnxt *bp)
13135 {
13136 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
13137 		if (bp->fw_cap & BNXT_FW_CAP_CFA_RFS_RING_TBL_IDX_V2)
13138 			return true;
13139 		return false;
13140 	}
13141 	/* 212 firmware is broken for aRFS */
13142 	if (BNXT_FW_MAJ(bp) == 212)
13143 		return false;
13144 	if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
13145 		return true;
13146 	if (bp->rss_cap & BNXT_RSS_CAP_NEW_RSS_CAP)
13147 		return true;
13148 	return false;
13149 }
13150 
13151 /* If runtime conditions support RFS */
13152 bool bnxt_rfs_capable(struct bnxt *bp, bool new_rss_ctx)
13153 {
13154 	struct bnxt_hw_rings hwr = {0};
13155 	int max_vnics, max_rss_ctxs;
13156 
13157 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
13158 	    !BNXT_SUPPORTS_NTUPLE_VNIC(bp))
13159 		return bnxt_rfs_supported(bp);
13160 
13161 	if (!bnxt_can_reserve_rings(bp) || !bp->rx_nr_rings)
13162 		return false;
13163 
13164 	hwr.grp = bp->rx_nr_rings;
13165 	hwr.vnic = bnxt_get_total_vnics(bp, bp->rx_nr_rings);
13166 	if (new_rss_ctx)
13167 		hwr.vnic++;
13168 	hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
13169 	max_vnics = bnxt_get_max_func_vnics(bp);
13170 	max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
13171 
13172 	if (hwr.vnic > max_vnics || hwr.rss_ctx > max_rss_ctxs) {
13173 		if (bp->rx_nr_rings > 1)
13174 			netdev_warn(bp->dev,
13175 				    "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
13176 				    min(max_rss_ctxs - 1, max_vnics - 1));
13177 		return false;
13178 	}
13179 
13180 	if (!BNXT_NEW_RM(bp))
13181 		return true;
13182 
13183 	/* Do not reduce VNIC and RSS ctx reservations.  There is a FW
13184 	 * issue that will mess up the default VNIC if we reduce the
13185 	 * reservations.
13186 	 */
13187 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13188 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13189 		return true;
13190 
13191 	bnxt_hwrm_reserve_rings(bp, &hwr);
13192 	if (hwr.vnic <= bp->hw_resc.resv_vnics &&
13193 	    hwr.rss_ctx <= bp->hw_resc.resv_rsscos_ctxs)
13194 		return true;
13195 
13196 	netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
13197 	hwr.vnic = 1;
13198 	hwr.rss_ctx = 0;
13199 	bnxt_hwrm_reserve_rings(bp, &hwr);
13200 	return false;
13201 }
13202 
13203 static netdev_features_t bnxt_fix_features(struct net_device *dev,
13204 					   netdev_features_t features)
13205 {
13206 	struct bnxt *bp = netdev_priv(dev);
13207 	netdev_features_t vlan_features;
13208 
13209 	if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp, false))
13210 		features &= ~NETIF_F_NTUPLE;
13211 
13212 	if ((bp->flags & BNXT_FLAG_NO_AGG_RINGS) || bp->xdp_prog)
13213 		features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
13214 
13215 	if (!(features & NETIF_F_GRO))
13216 		features &= ~NETIF_F_GRO_HW;
13217 
13218 	if (features & NETIF_F_GRO_HW)
13219 		features &= ~NETIF_F_LRO;
13220 
13221 	/* Both CTAG and STAG VLAN acceleration on the RX side have to be
13222 	 * turned on or off together.
13223 	 */
13224 	vlan_features = features & BNXT_HW_FEATURE_VLAN_ALL_RX;
13225 	if (vlan_features != BNXT_HW_FEATURE_VLAN_ALL_RX) {
13226 		if (dev->features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13227 			features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13228 		else if (vlan_features)
13229 			features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
13230 	}
13231 #ifdef CONFIG_BNXT_SRIOV
13232 	if (BNXT_VF(bp) && bp->vf.vlan)
13233 		features &= ~BNXT_HW_FEATURE_VLAN_ALL_RX;
13234 #endif
13235 	return features;
13236 }
13237 
13238 static int bnxt_reinit_features(struct bnxt *bp, bool irq_re_init,
13239 				bool link_re_init, u32 flags, bool update_tpa)
13240 {
13241 	bnxt_close_nic(bp, irq_re_init, link_re_init);
13242 	bp->flags = flags;
13243 	if (update_tpa)
13244 		bnxt_set_ring_params(bp);
13245 	return bnxt_open_nic(bp, irq_re_init, link_re_init);
13246 }
13247 
13248 static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
13249 {
13250 	bool update_tpa = false, update_ntuple = false;
13251 	struct bnxt *bp = netdev_priv(dev);
13252 	u32 flags = bp->flags;
13253 	u32 changes;
13254 	int rc = 0;
13255 	bool re_init = false;
13256 
13257 	flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
13258 	if (features & NETIF_F_GRO_HW)
13259 		flags |= BNXT_FLAG_GRO;
13260 	else if (features & NETIF_F_LRO)
13261 		flags |= BNXT_FLAG_LRO;
13262 
13263 	if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
13264 		flags &= ~BNXT_FLAG_TPA;
13265 
13266 	if (features & BNXT_HW_FEATURE_VLAN_ALL_RX)
13267 		flags |= BNXT_FLAG_STRIP_VLAN;
13268 
13269 	if (features & NETIF_F_NTUPLE)
13270 		flags |= BNXT_FLAG_RFS;
13271 	else
13272 		bnxt_clear_usr_fltrs(bp, true);
13273 
13274 	changes = flags ^ bp->flags;
13275 	if (changes & BNXT_FLAG_TPA) {
13276 		update_tpa = true;
13277 		if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
13278 		    (flags & BNXT_FLAG_TPA) == 0 ||
13279 		    (bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13280 			re_init = true;
13281 	}
13282 
13283 	if (changes & ~BNXT_FLAG_TPA)
13284 		re_init = true;
13285 
13286 	if (changes & BNXT_FLAG_RFS)
13287 		update_ntuple = true;
13288 
13289 	if (flags != bp->flags) {
13290 		u32 old_flags = bp->flags;
13291 
13292 		if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13293 			bp->flags = flags;
13294 			if (update_tpa)
13295 				bnxt_set_ring_params(bp);
13296 			return rc;
13297 		}
13298 
13299 		if (update_ntuple)
13300 			return bnxt_reinit_features(bp, true, false, flags, update_tpa);
13301 
13302 		if (re_init)
13303 			return bnxt_reinit_features(bp, false, false, flags, update_tpa);
13304 
13305 		if (update_tpa) {
13306 			bp->flags = flags;
13307 			rc = bnxt_set_tpa(bp,
13308 					  (flags & BNXT_FLAG_TPA) ?
13309 					  true : false);
13310 			if (rc)
13311 				bp->flags = old_flags;
13312 		}
13313 	}
13314 	return rc;
13315 }
13316 
13317 static bool bnxt_exthdr_check(struct bnxt *bp, struct sk_buff *skb, int nw_off,
13318 			      u8 **nextp)
13319 {
13320 	struct ipv6hdr *ip6h = (struct ipv6hdr *)(skb->data + nw_off);
13321 	struct hop_jumbo_hdr *jhdr;
13322 	int hdr_count = 0;
13323 	u8 *nexthdr;
13324 	int start;
13325 
13326 	/* Check that there are at most 2 IPv6 extension headers, no
13327 	 * fragment header, and each is <= 64 bytes.
13328 	 */
13329 	start = nw_off + sizeof(*ip6h);
13330 	nexthdr = &ip6h->nexthdr;
13331 	while (ipv6_ext_hdr(*nexthdr)) {
13332 		struct ipv6_opt_hdr *hp;
13333 		int hdrlen;
13334 
13335 		if (hdr_count >= 3 || *nexthdr == NEXTHDR_NONE ||
13336 		    *nexthdr == NEXTHDR_FRAGMENT)
13337 			return false;
13338 		hp = __skb_header_pointer(NULL, start, sizeof(*hp), skb->data,
13339 					  skb_headlen(skb), NULL);
13340 		if (!hp)
13341 			return false;
13342 		if (*nexthdr == NEXTHDR_AUTH)
13343 			hdrlen = ipv6_authlen(hp);
13344 		else
13345 			hdrlen = ipv6_optlen(hp);
13346 
13347 		if (hdrlen > 64)
13348 			return false;
13349 
13350 		/* The ext header may be a hop-by-hop header inserted for
13351 		 * big TCP purposes. This will be removed before sending
13352 		 * from NIC, so do not count it.
13353 		 */
13354 		if (*nexthdr == NEXTHDR_HOP) {
13355 			if (likely(skb->len <= GRO_LEGACY_MAX_SIZE))
13356 				goto increment_hdr;
13357 
13358 			jhdr = (struct hop_jumbo_hdr *)hp;
13359 			if (jhdr->tlv_type != IPV6_TLV_JUMBO || jhdr->hdrlen != 0 ||
13360 			    jhdr->nexthdr != IPPROTO_TCP)
13361 				goto increment_hdr;
13362 
13363 			goto next_hdr;
13364 		}
13365 increment_hdr:
13366 		hdr_count++;
13367 next_hdr:
13368 		nexthdr = &hp->nexthdr;
13369 		start += hdrlen;
13370 	}
13371 	if (nextp) {
13372 		/* Caller will check inner protocol */
13373 		if (skb->encapsulation) {
13374 			*nextp = nexthdr;
13375 			return true;
13376 		}
13377 		*nextp = NULL;
13378 	}
13379 	/* Only support TCP/UDP for non-tunneled ipv6 and inner ipv6 */
13380 	return *nexthdr == IPPROTO_TCP || *nexthdr == IPPROTO_UDP;
13381 }
13382 
13383 /* For UDP, we can only handle 1 Vxlan port and 1 Geneve port. */
13384 static bool bnxt_udp_tunl_check(struct bnxt *bp, struct sk_buff *skb)
13385 {
13386 	struct udphdr *uh = udp_hdr(skb);
13387 	__be16 udp_port = uh->dest;
13388 
13389 	if (udp_port != bp->vxlan_port && udp_port != bp->nge_port &&
13390 	    udp_port != bp->vxlan_gpe_port)
13391 		return false;
13392 	if (skb->inner_protocol == htons(ETH_P_TEB)) {
13393 		struct ethhdr *eh = inner_eth_hdr(skb);
13394 
13395 		switch (eh->h_proto) {
13396 		case htons(ETH_P_IP):
13397 			return true;
13398 		case htons(ETH_P_IPV6):
13399 			return bnxt_exthdr_check(bp, skb,
13400 						 skb_inner_network_offset(skb),
13401 						 NULL);
13402 		}
13403 	} else if (skb->inner_protocol == htons(ETH_P_IP)) {
13404 		return true;
13405 	} else if (skb->inner_protocol == htons(ETH_P_IPV6)) {
13406 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13407 					 NULL);
13408 	}
13409 	return false;
13410 }
13411 
13412 static bool bnxt_tunl_check(struct bnxt *bp, struct sk_buff *skb, u8 l4_proto)
13413 {
13414 	switch (l4_proto) {
13415 	case IPPROTO_UDP:
13416 		return bnxt_udp_tunl_check(bp, skb);
13417 	case IPPROTO_IPIP:
13418 		return true;
13419 	case IPPROTO_GRE: {
13420 		switch (skb->inner_protocol) {
13421 		default:
13422 			return false;
13423 		case htons(ETH_P_IP):
13424 			return true;
13425 		case htons(ETH_P_IPV6):
13426 			fallthrough;
13427 		}
13428 	}
13429 	case IPPROTO_IPV6:
13430 		/* Check ext headers of inner ipv6 */
13431 		return bnxt_exthdr_check(bp, skb, skb_inner_network_offset(skb),
13432 					 NULL);
13433 	}
13434 	return false;
13435 }
13436 
13437 static netdev_features_t bnxt_features_check(struct sk_buff *skb,
13438 					     struct net_device *dev,
13439 					     netdev_features_t features)
13440 {
13441 	struct bnxt *bp = netdev_priv(dev);
13442 	u8 *l4_proto;
13443 
13444 	features = vlan_features_check(skb, features);
13445 	switch (vlan_get_protocol(skb)) {
13446 	case htons(ETH_P_IP):
13447 		if (!skb->encapsulation)
13448 			return features;
13449 		l4_proto = &ip_hdr(skb)->protocol;
13450 		if (bnxt_tunl_check(bp, skb, *l4_proto))
13451 			return features;
13452 		break;
13453 	case htons(ETH_P_IPV6):
13454 		if (!bnxt_exthdr_check(bp, skb, skb_network_offset(skb),
13455 				       &l4_proto))
13456 			break;
13457 		if (!l4_proto || bnxt_tunl_check(bp, skb, *l4_proto))
13458 			return features;
13459 		break;
13460 	}
13461 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13462 }
13463 
13464 int bnxt_dbg_hwrm_rd_reg(struct bnxt *bp, u32 reg_off, u16 num_words,
13465 			 u32 *reg_buf)
13466 {
13467 	struct hwrm_dbg_read_direct_output *resp;
13468 	struct hwrm_dbg_read_direct_input *req;
13469 	__le32 *dbg_reg_buf;
13470 	dma_addr_t mapping;
13471 	int rc, i;
13472 
13473 	rc = hwrm_req_init(bp, req, HWRM_DBG_READ_DIRECT);
13474 	if (rc)
13475 		return rc;
13476 
13477 	dbg_reg_buf = hwrm_req_dma_slice(bp, req, num_words * 4,
13478 					 &mapping);
13479 	if (!dbg_reg_buf) {
13480 		rc = -ENOMEM;
13481 		goto dbg_rd_reg_exit;
13482 	}
13483 
13484 	req->host_dest_addr = cpu_to_le64(mapping);
13485 
13486 	resp = hwrm_req_hold(bp, req);
13487 	req->read_addr = cpu_to_le32(reg_off + CHIMP_REG_VIEW_ADDR);
13488 	req->read_len32 = cpu_to_le32(num_words);
13489 
13490 	rc = hwrm_req_send(bp, req);
13491 	if (rc || resp->error_code) {
13492 		rc = -EIO;
13493 		goto dbg_rd_reg_exit;
13494 	}
13495 	for (i = 0; i < num_words; i++)
13496 		reg_buf[i] = le32_to_cpu(dbg_reg_buf[i]);
13497 
13498 dbg_rd_reg_exit:
13499 	hwrm_req_drop(bp, req);
13500 	return rc;
13501 }
13502 
13503 static int bnxt_dbg_hwrm_ring_info_get(struct bnxt *bp, u8 ring_type,
13504 				       u32 ring_id, u32 *prod, u32 *cons)
13505 {
13506 	struct hwrm_dbg_ring_info_get_output *resp;
13507 	struct hwrm_dbg_ring_info_get_input *req;
13508 	int rc;
13509 
13510 	rc = hwrm_req_init(bp, req, HWRM_DBG_RING_INFO_GET);
13511 	if (rc)
13512 		return rc;
13513 
13514 	req->ring_type = ring_type;
13515 	req->fw_ring_id = cpu_to_le32(ring_id);
13516 	resp = hwrm_req_hold(bp, req);
13517 	rc = hwrm_req_send(bp, req);
13518 	if (!rc) {
13519 		*prod = le32_to_cpu(resp->producer_index);
13520 		*cons = le32_to_cpu(resp->consumer_index);
13521 	}
13522 	hwrm_req_drop(bp, req);
13523 	return rc;
13524 }
13525 
13526 static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
13527 {
13528 	struct bnxt_tx_ring_info *txr;
13529 	int i = bnapi->index, j;
13530 
13531 	bnxt_for_each_napi_tx(j, bnapi, txr)
13532 		netdev_info(bnapi->bp->dev, "[%d.%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
13533 			    i, j, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
13534 			    txr->tx_cons);
13535 }
13536 
13537 static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
13538 {
13539 	struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
13540 	int i = bnapi->index;
13541 
13542 	if (!rxr)
13543 		return;
13544 
13545 	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",
13546 		    i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
13547 		    rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
13548 		    rxr->rx_sw_agg_prod);
13549 }
13550 
13551 static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
13552 {
13553 	struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
13554 	int i = bnapi->index;
13555 
13556 	netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
13557 		    i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
13558 }
13559 
13560 static void bnxt_dbg_dump_states(struct bnxt *bp)
13561 {
13562 	int i;
13563 	struct bnxt_napi *bnapi;
13564 
13565 	for (i = 0; i < bp->cp_nr_rings; i++) {
13566 		bnapi = bp->bnapi[i];
13567 		if (netif_msg_drv(bp)) {
13568 			bnxt_dump_tx_sw_state(bnapi);
13569 			bnxt_dump_rx_sw_state(bnapi);
13570 			bnxt_dump_cp_sw_state(bnapi);
13571 		}
13572 	}
13573 }
13574 
13575 static int bnxt_hwrm_rx_ring_reset(struct bnxt *bp, int ring_nr)
13576 {
13577 	struct bnxt_rx_ring_info *rxr = &bp->rx_ring[ring_nr];
13578 	struct hwrm_ring_reset_input *req;
13579 	struct bnxt_napi *bnapi = rxr->bnapi;
13580 	struct bnxt_cp_ring_info *cpr;
13581 	u16 cp_ring_id;
13582 	int rc;
13583 
13584 	rc = hwrm_req_init(bp, req, HWRM_RING_RESET);
13585 	if (rc)
13586 		return rc;
13587 
13588 	cpr = &bnapi->cp_ring;
13589 	cp_ring_id = cpr->cp_ring_struct.fw_ring_id;
13590 	req->cmpl_ring = cpu_to_le16(cp_ring_id);
13591 	req->ring_type = RING_RESET_REQ_RING_TYPE_RX_RING_GRP;
13592 	req->ring_id = cpu_to_le16(bp->grp_info[bnapi->index].fw_grp_id);
13593 	return hwrm_req_send_silent(bp, req);
13594 }
13595 
13596 static void bnxt_reset_task(struct bnxt *bp, bool silent)
13597 {
13598 	if (!silent)
13599 		bnxt_dbg_dump_states(bp);
13600 	if (netif_running(bp->dev)) {
13601 		bnxt_close_nic(bp, !silent, false);
13602 		bnxt_open_nic(bp, !silent, false);
13603 	}
13604 }
13605 
13606 static void bnxt_tx_timeout(struct net_device *dev, unsigned int txqueue)
13607 {
13608 	struct bnxt *bp = netdev_priv(dev);
13609 
13610 	netdev_err(bp->dev,  "TX timeout detected, starting reset task!\n");
13611 	bnxt_queue_sp_work(bp, BNXT_RESET_TASK_SP_EVENT);
13612 }
13613 
13614 static void bnxt_fw_health_check(struct bnxt *bp)
13615 {
13616 	struct bnxt_fw_health *fw_health = bp->fw_health;
13617 	struct pci_dev *pdev = bp->pdev;
13618 	u32 val;
13619 
13620 	if (!fw_health->enabled || test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13621 		return;
13622 
13623 	/* Make sure it is enabled before checking the tmr_counter. */
13624 	smp_rmb();
13625 	if (fw_health->tmr_counter) {
13626 		fw_health->tmr_counter--;
13627 		return;
13628 	}
13629 
13630 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13631 	if (val == fw_health->last_fw_heartbeat && pci_device_is_present(pdev)) {
13632 		fw_health->arrests++;
13633 		goto fw_reset;
13634 	}
13635 
13636 	fw_health->last_fw_heartbeat = val;
13637 
13638 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13639 	if (val != fw_health->last_fw_reset_cnt && pci_device_is_present(pdev)) {
13640 		fw_health->discoveries++;
13641 		goto fw_reset;
13642 	}
13643 
13644 	fw_health->tmr_counter = fw_health->tmr_multiplier;
13645 	return;
13646 
13647 fw_reset:
13648 	bnxt_queue_sp_work(bp, BNXT_FW_EXCEPTION_SP_EVENT);
13649 }
13650 
13651 static void bnxt_timer(struct timer_list *t)
13652 {
13653 	struct bnxt *bp = from_timer(bp, t, timer);
13654 	struct net_device *dev = bp->dev;
13655 
13656 	if (!netif_running(dev) || !test_bit(BNXT_STATE_OPEN, &bp->state))
13657 		return;
13658 
13659 	if (atomic_read(&bp->intr_sem) != 0)
13660 		goto bnxt_restart_timer;
13661 
13662 	if (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)
13663 		bnxt_fw_health_check(bp);
13664 
13665 	if (BNXT_LINK_IS_UP(bp) && bp->stats_coal_ticks)
13666 		bnxt_queue_sp_work(bp, BNXT_PERIODIC_STATS_SP_EVENT);
13667 
13668 	if (bnxt_tc_flower_enabled(bp))
13669 		bnxt_queue_sp_work(bp, BNXT_FLOW_STATS_SP_EVENT);
13670 
13671 #ifdef CONFIG_RFS_ACCEL
13672 	if ((bp->flags & BNXT_FLAG_RFS) && bp->ntp_fltr_count)
13673 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
13674 #endif /*CONFIG_RFS_ACCEL*/
13675 
13676 	if (bp->link_info.phy_retry) {
13677 		if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
13678 			bp->link_info.phy_retry = false;
13679 			netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
13680 		} else {
13681 			bnxt_queue_sp_work(bp, BNXT_UPDATE_PHY_SP_EVENT);
13682 		}
13683 	}
13684 
13685 	if (test_bit(BNXT_STATE_L2_FILTER_RETRY, &bp->state))
13686 		bnxt_queue_sp_work(bp, BNXT_RX_MASK_SP_EVENT);
13687 
13688 	if ((BNXT_CHIP_P5(bp)) && !bp->chip_rev && netif_carrier_ok(dev))
13689 		bnxt_queue_sp_work(bp, BNXT_RING_COAL_NOW_SP_EVENT);
13690 
13691 bnxt_restart_timer:
13692 	mod_timer(&bp->timer, jiffies + bp->current_interval);
13693 }
13694 
13695 static void bnxt_rtnl_lock_sp(struct bnxt *bp)
13696 {
13697 	/* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
13698 	 * set.  If the device is being closed, bnxt_close() may be holding
13699 	 * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear.  So we
13700 	 * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
13701 	 */
13702 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13703 	rtnl_lock();
13704 }
13705 
13706 static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
13707 {
13708 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
13709 	rtnl_unlock();
13710 }
13711 
13712 /* Only called from bnxt_sp_task() */
13713 static void bnxt_reset(struct bnxt *bp, bool silent)
13714 {
13715 	bnxt_rtnl_lock_sp(bp);
13716 	if (test_bit(BNXT_STATE_OPEN, &bp->state))
13717 		bnxt_reset_task(bp, silent);
13718 	bnxt_rtnl_unlock_sp(bp);
13719 }
13720 
13721 /* Only called from bnxt_sp_task() */
13722 static void bnxt_rx_ring_reset(struct bnxt *bp)
13723 {
13724 	int i;
13725 
13726 	bnxt_rtnl_lock_sp(bp);
13727 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
13728 		bnxt_rtnl_unlock_sp(bp);
13729 		return;
13730 	}
13731 	/* Disable and flush TPA before resetting the RX ring */
13732 	if (bp->flags & BNXT_FLAG_TPA)
13733 		bnxt_set_tpa(bp, false);
13734 	for (i = 0; i < bp->rx_nr_rings; i++) {
13735 		struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
13736 		struct bnxt_cp_ring_info *cpr;
13737 		int rc;
13738 
13739 		if (!rxr->bnapi->in_reset)
13740 			continue;
13741 
13742 		rc = bnxt_hwrm_rx_ring_reset(bp, i);
13743 		if (rc) {
13744 			if (rc == -EINVAL || rc == -EOPNOTSUPP)
13745 				netdev_info_once(bp->dev, "RX ring reset not supported by firmware, falling back to global reset\n");
13746 			else
13747 				netdev_warn(bp->dev, "RX ring reset failed, rc = %d, falling back to global reset\n",
13748 					    rc);
13749 			bnxt_reset_task(bp, true);
13750 			break;
13751 		}
13752 		bnxt_free_one_rx_ring_skbs(bp, rxr);
13753 		rxr->rx_prod = 0;
13754 		rxr->rx_agg_prod = 0;
13755 		rxr->rx_sw_agg_prod = 0;
13756 		rxr->rx_next_cons = 0;
13757 		rxr->bnapi->in_reset = false;
13758 		bnxt_alloc_one_rx_ring(bp, i);
13759 		cpr = &rxr->bnapi->cp_ring;
13760 		cpr->sw_stats->rx.rx_resets++;
13761 		if (bp->flags & BNXT_FLAG_AGG_RINGS)
13762 			bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
13763 		bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
13764 	}
13765 	if (bp->flags & BNXT_FLAG_TPA)
13766 		bnxt_set_tpa(bp, true);
13767 	bnxt_rtnl_unlock_sp(bp);
13768 }
13769 
13770 static void bnxt_fw_fatal_close(struct bnxt *bp)
13771 {
13772 	bnxt_tx_disable(bp);
13773 	bnxt_disable_napi(bp);
13774 	bnxt_disable_int_sync(bp);
13775 	bnxt_free_irq(bp);
13776 	bnxt_clear_int_mode(bp);
13777 	pci_disable_device(bp->pdev);
13778 }
13779 
13780 static void bnxt_fw_reset_close(struct bnxt *bp)
13781 {
13782 	/* When firmware is in fatal state, quiesce device and disable
13783 	 * bus master to prevent any potential bad DMAs before freeing
13784 	 * kernel memory.
13785 	 */
13786 	if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state)) {
13787 		u16 val = 0;
13788 
13789 		pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
13790 		if (val == 0xffff)
13791 			bp->fw_reset_min_dsecs = 0;
13792 		bnxt_fw_fatal_close(bp);
13793 	}
13794 	__bnxt_close_nic(bp, true, false);
13795 	bnxt_vf_reps_free(bp);
13796 	bnxt_clear_int_mode(bp);
13797 	bnxt_hwrm_func_drv_unrgtr(bp);
13798 	if (pci_is_enabled(bp->pdev))
13799 		pci_disable_device(bp->pdev);
13800 	bnxt_free_ctx_mem(bp, false);
13801 }
13802 
13803 static bool is_bnxt_fw_ok(struct bnxt *bp)
13804 {
13805 	struct bnxt_fw_health *fw_health = bp->fw_health;
13806 	bool no_heartbeat = false, has_reset = false;
13807 	u32 val;
13808 
13809 	val = bnxt_fw_health_readl(bp, BNXT_FW_HEARTBEAT_REG);
13810 	if (val == fw_health->last_fw_heartbeat)
13811 		no_heartbeat = true;
13812 
13813 	val = bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
13814 	if (val != fw_health->last_fw_reset_cnt)
13815 		has_reset = true;
13816 
13817 	if (!no_heartbeat && has_reset)
13818 		return true;
13819 
13820 	return false;
13821 }
13822 
13823 /* rtnl_lock is acquired before calling this function */
13824 static void bnxt_force_fw_reset(struct bnxt *bp)
13825 {
13826 	struct bnxt_fw_health *fw_health = bp->fw_health;
13827 	struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13828 	u32 wait_dsecs;
13829 
13830 	if (!test_bit(BNXT_STATE_OPEN, &bp->state) ||
13831 	    test_bit(BNXT_STATE_IN_FW_RESET, &bp->state))
13832 		return;
13833 
13834 	/* we have to serialize with bnxt_refclk_read()*/
13835 	if (ptp) {
13836 		unsigned long flags;
13837 
13838 		write_seqlock_irqsave(&ptp->ptp_lock, flags);
13839 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13840 		write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13841 	} else {
13842 		set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13843 	}
13844 	bnxt_fw_reset_close(bp);
13845 	wait_dsecs = fw_health->master_func_wait_dsecs;
13846 	if (fw_health->primary) {
13847 		if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU)
13848 			wait_dsecs = 0;
13849 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
13850 	} else {
13851 		bp->fw_reset_timestamp = jiffies + wait_dsecs * HZ / 10;
13852 		wait_dsecs = fw_health->normal_func_wait_dsecs;
13853 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13854 	}
13855 
13856 	bp->fw_reset_min_dsecs = fw_health->post_reset_wait_dsecs;
13857 	bp->fw_reset_max_dsecs = fw_health->post_reset_max_wait_dsecs;
13858 	bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
13859 }
13860 
13861 void bnxt_fw_exception(struct bnxt *bp)
13862 {
13863 	netdev_warn(bp->dev, "Detected firmware fatal condition, initiating reset\n");
13864 	set_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
13865 	bnxt_ulp_stop(bp);
13866 	bnxt_rtnl_lock_sp(bp);
13867 	bnxt_force_fw_reset(bp);
13868 	bnxt_rtnl_unlock_sp(bp);
13869 }
13870 
13871 /* Returns the number of registered VFs, or 1 if VF configuration is pending, or
13872  * < 0 on error.
13873  */
13874 static int bnxt_get_registered_vfs(struct bnxt *bp)
13875 {
13876 #ifdef CONFIG_BNXT_SRIOV
13877 	int rc;
13878 
13879 	if (!BNXT_PF(bp))
13880 		return 0;
13881 
13882 	rc = bnxt_hwrm_func_qcfg(bp);
13883 	if (rc) {
13884 		netdev_err(bp->dev, "func_qcfg cmd failed, rc = %d\n", rc);
13885 		return rc;
13886 	}
13887 	if (bp->pf.registered_vfs)
13888 		return bp->pf.registered_vfs;
13889 	if (bp->sriov_cfg)
13890 		return 1;
13891 #endif
13892 	return 0;
13893 }
13894 
13895 void bnxt_fw_reset(struct bnxt *bp)
13896 {
13897 	bnxt_ulp_stop(bp);
13898 	bnxt_rtnl_lock_sp(bp);
13899 	if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
13900 	    !test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
13901 		struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
13902 		int n = 0, tmo;
13903 
13904 		/* we have to serialize with bnxt_refclk_read()*/
13905 		if (ptp) {
13906 			unsigned long flags;
13907 
13908 			write_seqlock_irqsave(&ptp->ptp_lock, flags);
13909 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13910 			write_sequnlock_irqrestore(&ptp->ptp_lock, flags);
13911 		} else {
13912 			set_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13913 		}
13914 		if (bp->pf.active_vfs &&
13915 		    !test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state))
13916 			n = bnxt_get_registered_vfs(bp);
13917 		if (n < 0) {
13918 			netdev_err(bp->dev, "Firmware reset aborted, rc = %d\n",
13919 				   n);
13920 			clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
13921 			dev_close(bp->dev);
13922 			goto fw_reset_exit;
13923 		} else if (n > 0) {
13924 			u16 vf_tmo_dsecs = n * 10;
13925 
13926 			if (bp->fw_reset_max_dsecs < vf_tmo_dsecs)
13927 				bp->fw_reset_max_dsecs = vf_tmo_dsecs;
13928 			bp->fw_reset_state =
13929 				BNXT_FW_RESET_STATE_POLL_VF;
13930 			bnxt_queue_fw_reset_work(bp, HZ / 10);
13931 			goto fw_reset_exit;
13932 		}
13933 		bnxt_fw_reset_close(bp);
13934 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
13935 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
13936 			tmo = HZ / 10;
13937 		} else {
13938 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
13939 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
13940 		}
13941 		bnxt_queue_fw_reset_work(bp, tmo);
13942 	}
13943 fw_reset_exit:
13944 	bnxt_rtnl_unlock_sp(bp);
13945 }
13946 
13947 static void bnxt_chk_missed_irq(struct bnxt *bp)
13948 {
13949 	int i;
13950 
13951 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
13952 		return;
13953 
13954 	for (i = 0; i < bp->cp_nr_rings; i++) {
13955 		struct bnxt_napi *bnapi = bp->bnapi[i];
13956 		struct bnxt_cp_ring_info *cpr;
13957 		u32 fw_ring_id;
13958 		int j;
13959 
13960 		if (!bnapi)
13961 			continue;
13962 
13963 		cpr = &bnapi->cp_ring;
13964 		for (j = 0; j < cpr->cp_ring_count; j++) {
13965 			struct bnxt_cp_ring_info *cpr2 = &cpr->cp_ring_arr[j];
13966 			u32 val[2];
13967 
13968 			if (cpr2->has_more_work || !bnxt_has_work(bp, cpr2))
13969 				continue;
13970 
13971 			if (cpr2->cp_raw_cons != cpr2->last_cp_raw_cons) {
13972 				cpr2->last_cp_raw_cons = cpr2->cp_raw_cons;
13973 				continue;
13974 			}
13975 			fw_ring_id = cpr2->cp_ring_struct.fw_ring_id;
13976 			bnxt_dbg_hwrm_ring_info_get(bp,
13977 				DBG_RING_INFO_GET_REQ_RING_TYPE_L2_CMPL,
13978 				fw_ring_id, &val[0], &val[1]);
13979 			cpr->sw_stats->cmn.missed_irqs++;
13980 		}
13981 	}
13982 }
13983 
13984 static void bnxt_cfg_ntp_filters(struct bnxt *);
13985 
13986 static void bnxt_init_ethtool_link_settings(struct bnxt *bp)
13987 {
13988 	struct bnxt_link_info *link_info = &bp->link_info;
13989 
13990 	if (BNXT_AUTO_MODE(link_info->auto_mode)) {
13991 		link_info->autoneg = BNXT_AUTONEG_SPEED;
13992 		if (bp->hwrm_spec_code >= 0x10201) {
13993 			if (link_info->auto_pause_setting &
13994 			    PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
13995 				link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13996 		} else {
13997 			link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
13998 		}
13999 		bnxt_set_auto_speed(link_info);
14000 	} else {
14001 		bnxt_set_force_speed(link_info);
14002 		link_info->req_duplex = link_info->duplex_setting;
14003 	}
14004 	if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
14005 		link_info->req_flow_ctrl =
14006 			link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
14007 	else
14008 		link_info->req_flow_ctrl = link_info->force_pause_setting;
14009 }
14010 
14011 static void bnxt_fw_echo_reply(struct bnxt *bp)
14012 {
14013 	struct bnxt_fw_health *fw_health = bp->fw_health;
14014 	struct hwrm_func_echo_response_input *req;
14015 	int rc;
14016 
14017 	rc = hwrm_req_init(bp, req, HWRM_FUNC_ECHO_RESPONSE);
14018 	if (rc)
14019 		return;
14020 	req->event_data1 = cpu_to_le32(fw_health->echo_req_data1);
14021 	req->event_data2 = cpu_to_le32(fw_health->echo_req_data2);
14022 	hwrm_req_send(bp, req);
14023 }
14024 
14025 static void bnxt_ulp_restart(struct bnxt *bp)
14026 {
14027 	bnxt_ulp_stop(bp);
14028 	bnxt_ulp_start(bp, 0);
14029 }
14030 
14031 static void bnxt_sp_task(struct work_struct *work)
14032 {
14033 	struct bnxt *bp = container_of(work, struct bnxt, sp_task);
14034 
14035 	set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14036 	smp_mb__after_atomic();
14037 	if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
14038 		clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14039 		return;
14040 	}
14041 
14042 	if (test_and_clear_bit(BNXT_RESTART_ULP_SP_EVENT, &bp->sp_event)) {
14043 		bnxt_ulp_restart(bp);
14044 		bnxt_reenable_sriov(bp);
14045 	}
14046 
14047 	if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
14048 		bnxt_cfg_rx_mode(bp);
14049 
14050 	if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
14051 		bnxt_cfg_ntp_filters(bp);
14052 	if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
14053 		bnxt_hwrm_exec_fwd_req(bp);
14054 	if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
14055 		netdev_info(bp->dev, "Receive PF driver unload event!\n");
14056 	if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
14057 		bnxt_hwrm_port_qstats(bp, 0);
14058 		bnxt_hwrm_port_qstats_ext(bp, 0);
14059 		bnxt_accumulate_all_stats(bp);
14060 	}
14061 
14062 	if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
14063 		int rc;
14064 
14065 		mutex_lock(&bp->link_lock);
14066 		if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
14067 				       &bp->sp_event))
14068 			bnxt_hwrm_phy_qcaps(bp);
14069 
14070 		rc = bnxt_update_link(bp, true);
14071 		if (rc)
14072 			netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
14073 				   rc);
14074 
14075 		if (test_and_clear_bit(BNXT_LINK_CFG_CHANGE_SP_EVENT,
14076 				       &bp->sp_event))
14077 			bnxt_init_ethtool_link_settings(bp);
14078 		mutex_unlock(&bp->link_lock);
14079 	}
14080 	if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
14081 		int rc;
14082 
14083 		mutex_lock(&bp->link_lock);
14084 		rc = bnxt_update_phy_setting(bp);
14085 		mutex_unlock(&bp->link_lock);
14086 		if (rc) {
14087 			netdev_warn(bp->dev, "update phy settings retry failed\n");
14088 		} else {
14089 			bp->link_info.phy_retry = false;
14090 			netdev_info(bp->dev, "update phy settings retry succeeded\n");
14091 		}
14092 	}
14093 	if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
14094 		mutex_lock(&bp->link_lock);
14095 		bnxt_get_port_module_status(bp);
14096 		mutex_unlock(&bp->link_lock);
14097 	}
14098 
14099 	if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
14100 		bnxt_tc_flow_stats_work(bp);
14101 
14102 	if (test_and_clear_bit(BNXT_RING_COAL_NOW_SP_EVENT, &bp->sp_event))
14103 		bnxt_chk_missed_irq(bp);
14104 
14105 	if (test_and_clear_bit(BNXT_FW_ECHO_REQUEST_SP_EVENT, &bp->sp_event))
14106 		bnxt_fw_echo_reply(bp);
14107 
14108 	if (test_and_clear_bit(BNXT_THERMAL_THRESHOLD_SP_EVENT, &bp->sp_event))
14109 		bnxt_hwmon_notify_event(bp);
14110 
14111 	/* These functions below will clear BNXT_STATE_IN_SP_TASK.  They
14112 	 * must be the last functions to be called before exiting.
14113 	 */
14114 	if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
14115 		bnxt_reset(bp, false);
14116 
14117 	if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
14118 		bnxt_reset(bp, true);
14119 
14120 	if (test_and_clear_bit(BNXT_RST_RING_SP_EVENT, &bp->sp_event))
14121 		bnxt_rx_ring_reset(bp);
14122 
14123 	if (test_and_clear_bit(BNXT_FW_RESET_NOTIFY_SP_EVENT, &bp->sp_event)) {
14124 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) ||
14125 		    test_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state))
14126 			bnxt_devlink_health_fw_report(bp);
14127 		else
14128 			bnxt_fw_reset(bp);
14129 	}
14130 
14131 	if (test_and_clear_bit(BNXT_FW_EXCEPTION_SP_EVENT, &bp->sp_event)) {
14132 		if (!is_bnxt_fw_ok(bp))
14133 			bnxt_devlink_health_fw_report(bp);
14134 	}
14135 
14136 	smp_mb__before_atomic();
14137 	clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
14138 }
14139 
14140 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
14141 				int *max_cp);
14142 
14143 /* Under rtnl_lock */
14144 int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
14145 		     int tx_xdp)
14146 {
14147 	int max_rx, max_tx, max_cp, tx_sets = 1, tx_cp;
14148 	struct bnxt_hw_rings hwr = {0};
14149 	int rx_rings = rx;
14150 	int rc;
14151 
14152 	if (tcs)
14153 		tx_sets = tcs;
14154 
14155 	_bnxt_get_max_rings(bp, &max_rx, &max_tx, &max_cp);
14156 
14157 	if (max_rx < rx_rings)
14158 		return -ENOMEM;
14159 
14160 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
14161 		rx_rings <<= 1;
14162 
14163 	hwr.rx = rx_rings;
14164 	hwr.tx = tx * tx_sets + tx_xdp;
14165 	if (max_tx < hwr.tx)
14166 		return -ENOMEM;
14167 
14168 	hwr.vnic = bnxt_get_total_vnics(bp, rx);
14169 
14170 	tx_cp = __bnxt_num_tx_to_cp(bp, hwr.tx, tx_sets, tx_xdp);
14171 	hwr.cp = sh ? max_t(int, tx_cp, rx) : tx_cp + rx;
14172 	if (max_cp < hwr.cp)
14173 		return -ENOMEM;
14174 	hwr.stat = hwr.cp;
14175 	if (BNXT_NEW_RM(bp)) {
14176 		hwr.cp += bnxt_get_ulp_msix_num_in_use(bp);
14177 		hwr.stat += bnxt_get_ulp_stat_ctxs_in_use(bp);
14178 		hwr.grp = rx;
14179 		hwr.rss_ctx = bnxt_get_total_rss_ctxs(bp, &hwr);
14180 	}
14181 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS)
14182 		hwr.cp_p5 = hwr.tx + rx;
14183 	rc = bnxt_hwrm_check_rings(bp, &hwr);
14184 	if (!rc && pci_msix_can_alloc_dyn(bp->pdev)) {
14185 		if (!bnxt_ulp_registered(bp->edev)) {
14186 			hwr.cp += bnxt_get_ulp_msix_num(bp);
14187 			hwr.cp = min_t(int, hwr.cp, bnxt_get_max_func_irqs(bp));
14188 		}
14189 		if (hwr.cp > bp->total_irqs) {
14190 			int total_msix = bnxt_change_msix(bp, hwr.cp);
14191 
14192 			if (total_msix < hwr.cp) {
14193 				netdev_warn(bp->dev, "Unable to allocate %d MSIX vectors, maximum available %d\n",
14194 					    hwr.cp, total_msix);
14195 				rc = -ENOSPC;
14196 			}
14197 		}
14198 	}
14199 	return rc;
14200 }
14201 
14202 static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
14203 {
14204 	if (bp->bar2) {
14205 		pci_iounmap(pdev, bp->bar2);
14206 		bp->bar2 = NULL;
14207 	}
14208 
14209 	if (bp->bar1) {
14210 		pci_iounmap(pdev, bp->bar1);
14211 		bp->bar1 = NULL;
14212 	}
14213 
14214 	if (bp->bar0) {
14215 		pci_iounmap(pdev, bp->bar0);
14216 		bp->bar0 = NULL;
14217 	}
14218 }
14219 
14220 static void bnxt_cleanup_pci(struct bnxt *bp)
14221 {
14222 	bnxt_unmap_bars(bp, bp->pdev);
14223 	pci_release_regions(bp->pdev);
14224 	if (pci_is_enabled(bp->pdev))
14225 		pci_disable_device(bp->pdev);
14226 }
14227 
14228 static void bnxt_init_dflt_coal(struct bnxt *bp)
14229 {
14230 	struct bnxt_coal_cap *coal_cap = &bp->coal_cap;
14231 	struct bnxt_coal *coal;
14232 	u16 flags = 0;
14233 
14234 	if (coal_cap->cmpl_params &
14235 	    RING_AGGINT_QCAPS_RESP_CMPL_PARAMS_TIMER_RESET)
14236 		flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
14237 
14238 	/* Tick values in micro seconds.
14239 	 * 1 coal_buf x bufs_per_record = 1 completion record.
14240 	 */
14241 	coal = &bp->rx_coal;
14242 	coal->coal_ticks = 10;
14243 	coal->coal_bufs = 30;
14244 	coal->coal_ticks_irq = 1;
14245 	coal->coal_bufs_irq = 2;
14246 	coal->idle_thresh = 50;
14247 	coal->bufs_per_record = 2;
14248 	coal->budget = 64;		/* NAPI budget */
14249 	coal->flags = flags;
14250 
14251 	coal = &bp->tx_coal;
14252 	coal->coal_ticks = 28;
14253 	coal->coal_bufs = 30;
14254 	coal->coal_ticks_irq = 2;
14255 	coal->coal_bufs_irq = 2;
14256 	coal->bufs_per_record = 1;
14257 	coal->flags = flags;
14258 
14259 	bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
14260 }
14261 
14262 /* FW that pre-reserves 1 VNIC per function */
14263 static bool bnxt_fw_pre_resv_vnics(struct bnxt *bp)
14264 {
14265 	u16 fw_maj = BNXT_FW_MAJ(bp), fw_bld = BNXT_FW_BLD(bp);
14266 
14267 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14268 	    (fw_maj > 218 || (fw_maj == 218 && fw_bld >= 18)))
14269 		return true;
14270 	if ((bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
14271 	    (fw_maj > 216 || (fw_maj == 216 && fw_bld >= 172)))
14272 		return true;
14273 	return false;
14274 }
14275 
14276 static int bnxt_fw_init_one_p1(struct bnxt *bp)
14277 {
14278 	int rc;
14279 
14280 	bp->fw_cap = 0;
14281 	rc = bnxt_hwrm_ver_get(bp);
14282 	/* FW may be unresponsive after FLR. FLR must complete within 100 msec
14283 	 * so wait before continuing with recovery.
14284 	 */
14285 	if (rc)
14286 		msleep(100);
14287 	bnxt_try_map_fw_health_reg(bp);
14288 	if (rc) {
14289 		rc = bnxt_try_recover_fw(bp);
14290 		if (rc)
14291 			return rc;
14292 		rc = bnxt_hwrm_ver_get(bp);
14293 		if (rc)
14294 			return rc;
14295 	}
14296 
14297 	bnxt_nvm_cfg_ver_get(bp);
14298 
14299 	rc = bnxt_hwrm_func_reset(bp);
14300 	if (rc)
14301 		return -ENODEV;
14302 
14303 	bnxt_hwrm_fw_set_time(bp);
14304 	return 0;
14305 }
14306 
14307 static int bnxt_fw_init_one_p2(struct bnxt *bp)
14308 {
14309 	int rc;
14310 
14311 	/* Get the MAX capabilities for this function */
14312 	rc = bnxt_hwrm_func_qcaps(bp);
14313 	if (rc) {
14314 		netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
14315 			   rc);
14316 		return -ENODEV;
14317 	}
14318 
14319 	rc = bnxt_hwrm_cfa_adv_flow_mgnt_qcaps(bp);
14320 	if (rc)
14321 		netdev_warn(bp->dev, "hwrm query adv flow mgnt failure rc: %d\n",
14322 			    rc);
14323 
14324 	if (bnxt_alloc_fw_health(bp)) {
14325 		netdev_warn(bp->dev, "no memory for firmware error recovery\n");
14326 	} else {
14327 		rc = bnxt_hwrm_error_recovery_qcfg(bp);
14328 		if (rc)
14329 			netdev_warn(bp->dev, "hwrm query error recovery failure rc: %d\n",
14330 				    rc);
14331 	}
14332 
14333 	rc = bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false);
14334 	if (rc)
14335 		return -ENODEV;
14336 
14337 	rc = bnxt_alloc_crash_dump_mem(bp);
14338 	if (rc)
14339 		netdev_warn(bp->dev, "crash dump mem alloc failure rc: %d\n",
14340 			    rc);
14341 	if (!rc) {
14342 		rc = bnxt_hwrm_crash_dump_mem_cfg(bp);
14343 		if (rc) {
14344 			bnxt_free_crash_dump_mem(bp);
14345 			netdev_warn(bp->dev,
14346 				    "hwrm crash dump mem failure rc: %d\n", rc);
14347 		}
14348 	}
14349 
14350 	if (bnxt_fw_pre_resv_vnics(bp))
14351 		bp->fw_cap |= BNXT_FW_CAP_PRE_RESV_VNICS;
14352 
14353 	bnxt_hwrm_func_qcfg(bp);
14354 	bnxt_hwrm_vnic_qcaps(bp);
14355 	bnxt_hwrm_port_led_qcaps(bp);
14356 	bnxt_ethtool_init(bp);
14357 	if (bp->fw_cap & BNXT_FW_CAP_PTP)
14358 		__bnxt_hwrm_ptp_qcfg(bp);
14359 	bnxt_dcb_init(bp);
14360 	bnxt_hwmon_init(bp);
14361 	return 0;
14362 }
14363 
14364 static void bnxt_set_dflt_rss_hash_type(struct bnxt *bp)
14365 {
14366 	bp->rss_cap &= ~BNXT_RSS_CAP_UDP_RSS_CAP;
14367 	bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
14368 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
14369 			   VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
14370 			   VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
14371 	if (bp->rss_cap & BNXT_RSS_CAP_RSS_HASH_TYPE_DELTA)
14372 		bp->rss_hash_delta = bp->rss_hash_cfg;
14373 	if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
14374 		bp->rss_cap |= BNXT_RSS_CAP_UDP_RSS_CAP;
14375 		bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
14376 				    VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
14377 	}
14378 }
14379 
14380 static void bnxt_set_dflt_rfs(struct bnxt *bp)
14381 {
14382 	struct net_device *dev = bp->dev;
14383 
14384 	dev->hw_features &= ~NETIF_F_NTUPLE;
14385 	dev->features &= ~NETIF_F_NTUPLE;
14386 	bp->flags &= ~BNXT_FLAG_RFS;
14387 	if (bnxt_rfs_supported(bp)) {
14388 		dev->hw_features |= NETIF_F_NTUPLE;
14389 		if (bnxt_rfs_capable(bp, false)) {
14390 			bp->flags |= BNXT_FLAG_RFS;
14391 			dev->features |= NETIF_F_NTUPLE;
14392 		}
14393 	}
14394 }
14395 
14396 static void bnxt_fw_init_one_p3(struct bnxt *bp)
14397 {
14398 	struct pci_dev *pdev = bp->pdev;
14399 
14400 	bnxt_set_dflt_rss_hash_type(bp);
14401 	bnxt_set_dflt_rfs(bp);
14402 
14403 	bnxt_get_wol_settings(bp);
14404 	if (bp->flags & BNXT_FLAG_WOL_CAP)
14405 		device_set_wakeup_enable(&pdev->dev, bp->wol);
14406 	else
14407 		device_set_wakeup_capable(&pdev->dev, false);
14408 
14409 	bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
14410 	bnxt_hwrm_coal_params_qcaps(bp);
14411 }
14412 
14413 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt);
14414 
14415 int bnxt_fw_init_one(struct bnxt *bp)
14416 {
14417 	int rc;
14418 
14419 	rc = bnxt_fw_init_one_p1(bp);
14420 	if (rc) {
14421 		netdev_err(bp->dev, "Firmware init phase 1 failed\n");
14422 		return rc;
14423 	}
14424 	rc = bnxt_fw_init_one_p2(bp);
14425 	if (rc) {
14426 		netdev_err(bp->dev, "Firmware init phase 2 failed\n");
14427 		return rc;
14428 	}
14429 	rc = bnxt_probe_phy(bp, false);
14430 	if (rc)
14431 		return rc;
14432 	rc = bnxt_approve_mac(bp, bp->dev->dev_addr, false);
14433 	if (rc)
14434 		return rc;
14435 
14436 	bnxt_fw_init_one_p3(bp);
14437 	return 0;
14438 }
14439 
14440 static void bnxt_fw_reset_writel(struct bnxt *bp, int reg_idx)
14441 {
14442 	struct bnxt_fw_health *fw_health = bp->fw_health;
14443 	u32 reg = fw_health->fw_reset_seq_regs[reg_idx];
14444 	u32 val = fw_health->fw_reset_seq_vals[reg_idx];
14445 	u32 reg_type, reg_off, delay_msecs;
14446 
14447 	delay_msecs = fw_health->fw_reset_seq_delay_msec[reg_idx];
14448 	reg_type = BNXT_FW_HEALTH_REG_TYPE(reg);
14449 	reg_off = BNXT_FW_HEALTH_REG_OFF(reg);
14450 	switch (reg_type) {
14451 	case BNXT_FW_HEALTH_REG_TYPE_CFG:
14452 		pci_write_config_dword(bp->pdev, reg_off, val);
14453 		break;
14454 	case BNXT_FW_HEALTH_REG_TYPE_GRC:
14455 		writel(reg_off & BNXT_GRC_BASE_MASK,
14456 		       bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
14457 		reg_off = (reg_off & BNXT_GRC_OFFSET_MASK) + 0x2000;
14458 		fallthrough;
14459 	case BNXT_FW_HEALTH_REG_TYPE_BAR0:
14460 		writel(val, bp->bar0 + reg_off);
14461 		break;
14462 	case BNXT_FW_HEALTH_REG_TYPE_BAR1:
14463 		writel(val, bp->bar1 + reg_off);
14464 		break;
14465 	}
14466 	if (delay_msecs) {
14467 		pci_read_config_dword(bp->pdev, 0, &val);
14468 		msleep(delay_msecs);
14469 	}
14470 }
14471 
14472 bool bnxt_hwrm_reset_permitted(struct bnxt *bp)
14473 {
14474 	struct hwrm_func_qcfg_output *resp;
14475 	struct hwrm_func_qcfg_input *req;
14476 	bool result = true; /* firmware will enforce if unknown */
14477 
14478 	if (~bp->fw_cap & BNXT_FW_CAP_HOT_RESET_IF)
14479 		return result;
14480 
14481 	if (hwrm_req_init(bp, req, HWRM_FUNC_QCFG))
14482 		return result;
14483 
14484 	req->fid = cpu_to_le16(0xffff);
14485 	resp = hwrm_req_hold(bp, req);
14486 	if (!hwrm_req_send(bp, req))
14487 		result = !!(le16_to_cpu(resp->flags) &
14488 			    FUNC_QCFG_RESP_FLAGS_HOT_RESET_ALLOWED);
14489 	hwrm_req_drop(bp, req);
14490 	return result;
14491 }
14492 
14493 static void bnxt_reset_all(struct bnxt *bp)
14494 {
14495 	struct bnxt_fw_health *fw_health = bp->fw_health;
14496 	int i, rc;
14497 
14498 	if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14499 		bnxt_fw_reset_via_optee(bp);
14500 		bp->fw_reset_timestamp = jiffies;
14501 		return;
14502 	}
14503 
14504 	if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_HOST) {
14505 		for (i = 0; i < fw_health->fw_reset_seq_cnt; i++)
14506 			bnxt_fw_reset_writel(bp, i);
14507 	} else if (fw_health->flags & ERROR_RECOVERY_QCFG_RESP_FLAGS_CO_CPU) {
14508 		struct hwrm_fw_reset_input *req;
14509 
14510 		rc = hwrm_req_init(bp, req, HWRM_FW_RESET);
14511 		if (!rc) {
14512 			req->target_id = cpu_to_le16(HWRM_TARGET_ID_KONG);
14513 			req->embedded_proc_type = FW_RESET_REQ_EMBEDDED_PROC_TYPE_CHIP;
14514 			req->selfrst_status = FW_RESET_REQ_SELFRST_STATUS_SELFRSTASAP;
14515 			req->flags = FW_RESET_REQ_FLAGS_RESET_GRACEFUL;
14516 			rc = hwrm_req_send(bp, req);
14517 		}
14518 		if (rc != -ENODEV)
14519 			netdev_warn(bp->dev, "Unable to reset FW rc=%d\n", rc);
14520 	}
14521 	bp->fw_reset_timestamp = jiffies;
14522 }
14523 
14524 static bool bnxt_fw_reset_timeout(struct bnxt *bp)
14525 {
14526 	return time_after(jiffies, bp->fw_reset_timestamp +
14527 			  (bp->fw_reset_max_dsecs * HZ / 10));
14528 }
14529 
14530 static void bnxt_fw_reset_abort(struct bnxt *bp, int rc)
14531 {
14532 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14533 	if (bp->fw_reset_state != BNXT_FW_RESET_STATE_POLL_VF)
14534 		bnxt_dl_health_fw_status_update(bp, false);
14535 	bp->fw_reset_state = 0;
14536 	dev_close(bp->dev);
14537 }
14538 
14539 static void bnxt_fw_reset_task(struct work_struct *work)
14540 {
14541 	struct bnxt *bp = container_of(work, struct bnxt, fw_reset_task.work);
14542 	int rc = 0;
14543 
14544 	if (!test_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
14545 		netdev_err(bp->dev, "bnxt_fw_reset_task() called when not in fw reset mode!\n");
14546 		return;
14547 	}
14548 
14549 	switch (bp->fw_reset_state) {
14550 	case BNXT_FW_RESET_STATE_POLL_VF: {
14551 		int n = bnxt_get_registered_vfs(bp);
14552 		int tmo;
14553 
14554 		if (n < 0) {
14555 			netdev_err(bp->dev, "Firmware reset aborted, subsequent func_qcfg cmd failed, rc = %d, %d msecs since reset timestamp\n",
14556 				   n, jiffies_to_msecs(jiffies -
14557 				   bp->fw_reset_timestamp));
14558 			goto fw_reset_abort;
14559 		} else if (n > 0) {
14560 			if (bnxt_fw_reset_timeout(bp)) {
14561 				clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14562 				bp->fw_reset_state = 0;
14563 				netdev_err(bp->dev, "Firmware reset aborted, bnxt_get_registered_vfs() returns %d\n",
14564 					   n);
14565 				goto ulp_start;
14566 			}
14567 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14568 			return;
14569 		}
14570 		bp->fw_reset_timestamp = jiffies;
14571 		rtnl_lock();
14572 		if (test_bit(BNXT_STATE_ABORT_ERR, &bp->state)) {
14573 			bnxt_fw_reset_abort(bp, rc);
14574 			rtnl_unlock();
14575 			goto ulp_start;
14576 		}
14577 		bnxt_fw_reset_close(bp);
14578 		if (bp->fw_cap & BNXT_FW_CAP_ERR_RECOVER_RELOAD) {
14579 			bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW_DOWN;
14580 			tmo = HZ / 10;
14581 		} else {
14582 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14583 			tmo = bp->fw_reset_min_dsecs * HZ / 10;
14584 		}
14585 		rtnl_unlock();
14586 		bnxt_queue_fw_reset_work(bp, tmo);
14587 		return;
14588 	}
14589 	case BNXT_FW_RESET_STATE_POLL_FW_DOWN: {
14590 		u32 val;
14591 
14592 		val = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14593 		if (!(val & BNXT_FW_STATUS_SHUTDOWN) &&
14594 		    !bnxt_fw_reset_timeout(bp)) {
14595 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14596 			return;
14597 		}
14598 
14599 		if (!bp->fw_health->primary) {
14600 			u32 wait_dsecs = bp->fw_health->normal_func_wait_dsecs;
14601 
14602 			bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14603 			bnxt_queue_fw_reset_work(bp, wait_dsecs * HZ / 10);
14604 			return;
14605 		}
14606 		bp->fw_reset_state = BNXT_FW_RESET_STATE_RESET_FW;
14607 	}
14608 		fallthrough;
14609 	case BNXT_FW_RESET_STATE_RESET_FW:
14610 		bnxt_reset_all(bp);
14611 		bp->fw_reset_state = BNXT_FW_RESET_STATE_ENABLE_DEV;
14612 		bnxt_queue_fw_reset_work(bp, bp->fw_reset_min_dsecs * HZ / 10);
14613 		return;
14614 	case BNXT_FW_RESET_STATE_ENABLE_DEV:
14615 		bnxt_inv_fw_health_reg(bp);
14616 		if (test_bit(BNXT_STATE_FW_FATAL_COND, &bp->state) &&
14617 		    !bp->fw_reset_min_dsecs) {
14618 			u16 val;
14619 
14620 			pci_read_config_word(bp->pdev, PCI_SUBSYSTEM_ID, &val);
14621 			if (val == 0xffff) {
14622 				if (bnxt_fw_reset_timeout(bp)) {
14623 					netdev_err(bp->dev, "Firmware reset aborted, PCI config space invalid\n");
14624 					rc = -ETIMEDOUT;
14625 					goto fw_reset_abort;
14626 				}
14627 				bnxt_queue_fw_reset_work(bp, HZ / 1000);
14628 				return;
14629 			}
14630 		}
14631 		clear_bit(BNXT_STATE_FW_FATAL_COND, &bp->state);
14632 		clear_bit(BNXT_STATE_FW_NON_FATAL_COND, &bp->state);
14633 		if (test_and_clear_bit(BNXT_STATE_FW_ACTIVATE_RESET, &bp->state) &&
14634 		    !test_bit(BNXT_STATE_FW_ACTIVATE, &bp->state))
14635 			bnxt_dl_remote_reload(bp);
14636 		if (pci_enable_device(bp->pdev)) {
14637 			netdev_err(bp->dev, "Cannot re-enable PCI device\n");
14638 			rc = -ENODEV;
14639 			goto fw_reset_abort;
14640 		}
14641 		pci_set_master(bp->pdev);
14642 		bp->fw_reset_state = BNXT_FW_RESET_STATE_POLL_FW;
14643 		fallthrough;
14644 	case BNXT_FW_RESET_STATE_POLL_FW:
14645 		bp->hwrm_cmd_timeout = SHORT_HWRM_CMD_TIMEOUT;
14646 		rc = bnxt_hwrm_poll(bp);
14647 		if (rc) {
14648 			if (bnxt_fw_reset_timeout(bp)) {
14649 				netdev_err(bp->dev, "Firmware reset aborted\n");
14650 				goto fw_reset_abort_status;
14651 			}
14652 			bnxt_queue_fw_reset_work(bp, HZ / 5);
14653 			return;
14654 		}
14655 		bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
14656 		bp->fw_reset_state = BNXT_FW_RESET_STATE_OPENING;
14657 		fallthrough;
14658 	case BNXT_FW_RESET_STATE_OPENING:
14659 		while (!rtnl_trylock()) {
14660 			bnxt_queue_fw_reset_work(bp, HZ / 10);
14661 			return;
14662 		}
14663 		rc = bnxt_open(bp->dev);
14664 		if (rc) {
14665 			netdev_err(bp->dev, "bnxt_open() failed during FW reset\n");
14666 			bnxt_fw_reset_abort(bp, rc);
14667 			rtnl_unlock();
14668 			goto ulp_start;
14669 		}
14670 
14671 		if ((bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY) &&
14672 		    bp->fw_health->enabled) {
14673 			bp->fw_health->last_fw_reset_cnt =
14674 				bnxt_fw_health_readl(bp, BNXT_FW_RESET_CNT_REG);
14675 		}
14676 		bp->fw_reset_state = 0;
14677 		/* Make sure fw_reset_state is 0 before clearing the flag */
14678 		smp_mb__before_atomic();
14679 		clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
14680 		bnxt_ptp_reapply_pps(bp);
14681 		clear_bit(BNXT_STATE_FW_ACTIVATE, &bp->state);
14682 		if (test_and_clear_bit(BNXT_STATE_RECOVER, &bp->state)) {
14683 			bnxt_dl_health_fw_recovery_done(bp);
14684 			bnxt_dl_health_fw_status_update(bp, true);
14685 		}
14686 		rtnl_unlock();
14687 		bnxt_ulp_start(bp, 0);
14688 		bnxt_reenable_sriov(bp);
14689 		rtnl_lock();
14690 		bnxt_vf_reps_alloc(bp);
14691 		bnxt_vf_reps_open(bp);
14692 		rtnl_unlock();
14693 		break;
14694 	}
14695 	return;
14696 
14697 fw_reset_abort_status:
14698 	if (bp->fw_health->status_reliable ||
14699 	    (bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY)) {
14700 		u32 sts = bnxt_fw_health_readl(bp, BNXT_FW_HEALTH_REG);
14701 
14702 		netdev_err(bp->dev, "fw_health_status 0x%x\n", sts);
14703 	}
14704 fw_reset_abort:
14705 	rtnl_lock();
14706 	bnxt_fw_reset_abort(bp, rc);
14707 	rtnl_unlock();
14708 ulp_start:
14709 	bnxt_ulp_start(bp, rc);
14710 }
14711 
14712 static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
14713 {
14714 	int rc;
14715 	struct bnxt *bp = netdev_priv(dev);
14716 
14717 	SET_NETDEV_DEV(dev, &pdev->dev);
14718 
14719 	/* enable device (incl. PCI PM wakeup), and bus-mastering */
14720 	rc = pci_enable_device(pdev);
14721 	if (rc) {
14722 		dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
14723 		goto init_err;
14724 	}
14725 
14726 	if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
14727 		dev_err(&pdev->dev,
14728 			"Cannot find PCI device base address, aborting\n");
14729 		rc = -ENODEV;
14730 		goto init_err_disable;
14731 	}
14732 
14733 	rc = pci_request_regions(pdev, DRV_MODULE_NAME);
14734 	if (rc) {
14735 		dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
14736 		goto init_err_disable;
14737 	}
14738 
14739 	if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
14740 	    dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
14741 		dev_err(&pdev->dev, "System does not support DMA, aborting\n");
14742 		rc = -EIO;
14743 		goto init_err_release;
14744 	}
14745 
14746 	pci_set_master(pdev);
14747 
14748 	bp->dev = dev;
14749 	bp->pdev = pdev;
14750 
14751 	/* Doorbell BAR bp->bar1 is mapped after bnxt_fw_init_one_p2()
14752 	 * determines the BAR size.
14753 	 */
14754 	bp->bar0 = pci_ioremap_bar(pdev, 0);
14755 	if (!bp->bar0) {
14756 		dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
14757 		rc = -ENOMEM;
14758 		goto init_err_release;
14759 	}
14760 
14761 	bp->bar2 = pci_ioremap_bar(pdev, 4);
14762 	if (!bp->bar2) {
14763 		dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
14764 		rc = -ENOMEM;
14765 		goto init_err_release;
14766 	}
14767 
14768 	INIT_WORK(&bp->sp_task, bnxt_sp_task);
14769 	INIT_DELAYED_WORK(&bp->fw_reset_task, bnxt_fw_reset_task);
14770 
14771 	spin_lock_init(&bp->ntp_fltr_lock);
14772 #if BITS_PER_LONG == 32
14773 	spin_lock_init(&bp->db_lock);
14774 #endif
14775 
14776 	bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
14777 	bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
14778 
14779 	timer_setup(&bp->timer, bnxt_timer, 0);
14780 	bp->current_interval = BNXT_TIMER_INTERVAL;
14781 
14782 	bp->vxlan_fw_dst_port_id = INVALID_HW_RING_ID;
14783 	bp->nge_fw_dst_port_id = INVALID_HW_RING_ID;
14784 
14785 	clear_bit(BNXT_STATE_OPEN, &bp->state);
14786 	return 0;
14787 
14788 init_err_release:
14789 	bnxt_unmap_bars(bp, pdev);
14790 	pci_release_regions(pdev);
14791 
14792 init_err_disable:
14793 	pci_disable_device(pdev);
14794 
14795 init_err:
14796 	return rc;
14797 }
14798 
14799 /* rtnl_lock held */
14800 static int bnxt_change_mac_addr(struct net_device *dev, void *p)
14801 {
14802 	struct sockaddr *addr = p;
14803 	struct bnxt *bp = netdev_priv(dev);
14804 	int rc = 0;
14805 
14806 	if (!is_valid_ether_addr(addr->sa_data))
14807 		return -EADDRNOTAVAIL;
14808 
14809 	if (ether_addr_equal(addr->sa_data, dev->dev_addr))
14810 		return 0;
14811 
14812 	rc = bnxt_approve_mac(bp, addr->sa_data, true);
14813 	if (rc)
14814 		return rc;
14815 
14816 	eth_hw_addr_set(dev, addr->sa_data);
14817 	bnxt_clear_usr_fltrs(bp, true);
14818 	if (netif_running(dev)) {
14819 		bnxt_close_nic(bp, false, false);
14820 		rc = bnxt_open_nic(bp, false, false);
14821 	}
14822 
14823 	return rc;
14824 }
14825 
14826 /* rtnl_lock held */
14827 static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
14828 {
14829 	struct bnxt *bp = netdev_priv(dev);
14830 
14831 	if (netif_running(dev))
14832 		bnxt_close_nic(bp, true, false);
14833 
14834 	WRITE_ONCE(dev->mtu, new_mtu);
14835 
14836 	/* MTU change may change the AGG ring settings if an XDP multi-buffer
14837 	 * program is attached.  We need to set the AGG rings settings and
14838 	 * rx_skb_func accordingly.
14839 	 */
14840 	if (READ_ONCE(bp->xdp_prog))
14841 		bnxt_set_rx_skb_mode(bp, true);
14842 
14843 	bnxt_set_ring_params(bp);
14844 
14845 	if (netif_running(dev))
14846 		return bnxt_open_nic(bp, true, false);
14847 
14848 	return 0;
14849 }
14850 
14851 int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
14852 {
14853 	struct bnxt *bp = netdev_priv(dev);
14854 	bool sh = false;
14855 	int rc, tx_cp;
14856 
14857 	if (tc > bp->max_tc) {
14858 		netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
14859 			   tc, bp->max_tc);
14860 		return -EINVAL;
14861 	}
14862 
14863 	if (bp->num_tc == tc)
14864 		return 0;
14865 
14866 	if (bp->flags & BNXT_FLAG_SHARED_RINGS)
14867 		sh = true;
14868 
14869 	rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
14870 			      sh, tc, bp->tx_nr_rings_xdp);
14871 	if (rc)
14872 		return rc;
14873 
14874 	/* Needs to close the device and do hw resource re-allocations */
14875 	if (netif_running(bp->dev))
14876 		bnxt_close_nic(bp, true, false);
14877 
14878 	if (tc) {
14879 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
14880 		netdev_set_num_tc(dev, tc);
14881 		bp->num_tc = tc;
14882 	} else {
14883 		bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
14884 		netdev_reset_tc(dev);
14885 		bp->num_tc = 0;
14886 	}
14887 	bp->tx_nr_rings += bp->tx_nr_rings_xdp;
14888 	tx_cp = bnxt_num_tx_to_cp(bp, bp->tx_nr_rings);
14889 	bp->cp_nr_rings = sh ? max_t(int, tx_cp, bp->rx_nr_rings) :
14890 			       tx_cp + bp->rx_nr_rings;
14891 
14892 	if (netif_running(bp->dev))
14893 		return bnxt_open_nic(bp, true, false);
14894 
14895 	return 0;
14896 }
14897 
14898 static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
14899 				  void *cb_priv)
14900 {
14901 	struct bnxt *bp = cb_priv;
14902 
14903 	if (!bnxt_tc_flower_enabled(bp) ||
14904 	    !tc_cls_can_offload_and_chain0(bp->dev, type_data))
14905 		return -EOPNOTSUPP;
14906 
14907 	switch (type) {
14908 	case TC_SETUP_CLSFLOWER:
14909 		return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
14910 	default:
14911 		return -EOPNOTSUPP;
14912 	}
14913 }
14914 
14915 LIST_HEAD(bnxt_block_cb_list);
14916 
14917 static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
14918 			 void *type_data)
14919 {
14920 	struct bnxt *bp = netdev_priv(dev);
14921 
14922 	switch (type) {
14923 	case TC_SETUP_BLOCK:
14924 		return flow_block_cb_setup_simple(type_data,
14925 						  &bnxt_block_cb_list,
14926 						  bnxt_setup_tc_block_cb,
14927 						  bp, bp, true);
14928 	case TC_SETUP_QDISC_MQPRIO: {
14929 		struct tc_mqprio_qopt *mqprio = type_data;
14930 
14931 		mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
14932 
14933 		return bnxt_setup_mq_tc(dev, mqprio->num_tc);
14934 	}
14935 	default:
14936 		return -EOPNOTSUPP;
14937 	}
14938 }
14939 
14940 u32 bnxt_get_ntp_filter_idx(struct bnxt *bp, struct flow_keys *fkeys,
14941 			    const struct sk_buff *skb)
14942 {
14943 	struct bnxt_vnic_info *vnic;
14944 
14945 	if (skb)
14946 		return skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
14947 
14948 	vnic = &bp->vnic_info[BNXT_VNIC_DEFAULT];
14949 	return bnxt_toeplitz(bp, fkeys, (void *)vnic->rss_hash_key);
14950 }
14951 
14952 int bnxt_insert_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr,
14953 			   u32 idx)
14954 {
14955 	struct hlist_head *head;
14956 	int bit_id;
14957 
14958 	spin_lock_bh(&bp->ntp_fltr_lock);
14959 	bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap, bp->max_fltr, 0);
14960 	if (bit_id < 0) {
14961 		spin_unlock_bh(&bp->ntp_fltr_lock);
14962 		return -ENOMEM;
14963 	}
14964 
14965 	fltr->base.sw_id = (u16)bit_id;
14966 	fltr->base.type = BNXT_FLTR_TYPE_NTUPLE;
14967 	fltr->base.flags |= BNXT_ACT_RING_DST;
14968 	head = &bp->ntp_fltr_hash_tbl[idx];
14969 	hlist_add_head_rcu(&fltr->base.hash, head);
14970 	set_bit(BNXT_FLTR_INSERTED, &fltr->base.state);
14971 	bnxt_insert_usr_fltr(bp, &fltr->base);
14972 	bp->ntp_fltr_count++;
14973 	spin_unlock_bh(&bp->ntp_fltr_lock);
14974 	return 0;
14975 }
14976 
14977 static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
14978 			    struct bnxt_ntuple_filter *f2)
14979 {
14980 	struct bnxt_flow_masks *masks1 = &f1->fmasks;
14981 	struct bnxt_flow_masks *masks2 = &f2->fmasks;
14982 	struct flow_keys *keys1 = &f1->fkeys;
14983 	struct flow_keys *keys2 = &f2->fkeys;
14984 
14985 	if (keys1->basic.n_proto != keys2->basic.n_proto ||
14986 	    keys1->basic.ip_proto != keys2->basic.ip_proto)
14987 		return false;
14988 
14989 	if (keys1->basic.n_proto == htons(ETH_P_IP)) {
14990 		if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
14991 		    masks1->addrs.v4addrs.src != masks2->addrs.v4addrs.src ||
14992 		    keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst ||
14993 		    masks1->addrs.v4addrs.dst != masks2->addrs.v4addrs.dst)
14994 			return false;
14995 	} else {
14996 		if (!ipv6_addr_equal(&keys1->addrs.v6addrs.src,
14997 				     &keys2->addrs.v6addrs.src) ||
14998 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.src,
14999 				     &masks2->addrs.v6addrs.src) ||
15000 		    !ipv6_addr_equal(&keys1->addrs.v6addrs.dst,
15001 				     &keys2->addrs.v6addrs.dst) ||
15002 		    !ipv6_addr_equal(&masks1->addrs.v6addrs.dst,
15003 				     &masks2->addrs.v6addrs.dst))
15004 			return false;
15005 	}
15006 
15007 	return keys1->ports.src == keys2->ports.src &&
15008 	       masks1->ports.src == masks2->ports.src &&
15009 	       keys1->ports.dst == keys2->ports.dst &&
15010 	       masks1->ports.dst == masks2->ports.dst &&
15011 	       keys1->control.flags == keys2->control.flags &&
15012 	       f1->l2_fltr == f2->l2_fltr;
15013 }
15014 
15015 struct bnxt_ntuple_filter *
15016 bnxt_lookup_ntp_filter_from_idx(struct bnxt *bp,
15017 				struct bnxt_ntuple_filter *fltr, u32 idx)
15018 {
15019 	struct bnxt_ntuple_filter *f;
15020 	struct hlist_head *head;
15021 
15022 	head = &bp->ntp_fltr_hash_tbl[idx];
15023 	hlist_for_each_entry_rcu(f, head, base.hash) {
15024 		if (bnxt_fltr_match(f, fltr))
15025 			return f;
15026 	}
15027 	return NULL;
15028 }
15029 
15030 #ifdef CONFIG_RFS_ACCEL
15031 static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
15032 			      u16 rxq_index, u32 flow_id)
15033 {
15034 	struct bnxt *bp = netdev_priv(dev);
15035 	struct bnxt_ntuple_filter *fltr, *new_fltr;
15036 	struct flow_keys *fkeys;
15037 	struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
15038 	struct bnxt_l2_filter *l2_fltr;
15039 	int rc = 0, idx;
15040 	u32 flags;
15041 
15042 	if (ether_addr_equal(dev->dev_addr, eth->h_dest)) {
15043 		l2_fltr = bp->vnic_info[BNXT_VNIC_DEFAULT].l2_filters[0];
15044 		atomic_inc(&l2_fltr->refcnt);
15045 	} else {
15046 		struct bnxt_l2_key key;
15047 
15048 		ether_addr_copy(key.dst_mac_addr, eth->h_dest);
15049 		key.vlan = 0;
15050 		l2_fltr = bnxt_lookup_l2_filter_from_key(bp, &key);
15051 		if (!l2_fltr)
15052 			return -EINVAL;
15053 		if (l2_fltr->base.flags & BNXT_ACT_FUNC_DST) {
15054 			bnxt_del_l2_filter(bp, l2_fltr);
15055 			return -EINVAL;
15056 		}
15057 	}
15058 	new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
15059 	if (!new_fltr) {
15060 		bnxt_del_l2_filter(bp, l2_fltr);
15061 		return -ENOMEM;
15062 	}
15063 
15064 	fkeys = &new_fltr->fkeys;
15065 	if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
15066 		rc = -EPROTONOSUPPORT;
15067 		goto err_free;
15068 	}
15069 
15070 	if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
15071 	     fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
15072 	    ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
15073 	     (fkeys->basic.ip_proto != IPPROTO_UDP))) {
15074 		rc = -EPROTONOSUPPORT;
15075 		goto err_free;
15076 	}
15077 	new_fltr->fmasks = BNXT_FLOW_IPV4_MASK_ALL;
15078 	if (fkeys->basic.n_proto == htons(ETH_P_IPV6)) {
15079 		if (bp->hwrm_spec_code < 0x10601) {
15080 			rc = -EPROTONOSUPPORT;
15081 			goto err_free;
15082 		}
15083 		new_fltr->fmasks = BNXT_FLOW_IPV6_MASK_ALL;
15084 	}
15085 	flags = fkeys->control.flags;
15086 	if (((flags & FLOW_DIS_ENCAPSULATION) &&
15087 	     bp->hwrm_spec_code < 0x10601) || (flags & FLOW_DIS_IS_FRAGMENT)) {
15088 		rc = -EPROTONOSUPPORT;
15089 		goto err_free;
15090 	}
15091 	new_fltr->l2_fltr = l2_fltr;
15092 
15093 	idx = bnxt_get_ntp_filter_idx(bp, fkeys, skb);
15094 	rcu_read_lock();
15095 	fltr = bnxt_lookup_ntp_filter_from_idx(bp, new_fltr, idx);
15096 	if (fltr) {
15097 		rc = fltr->base.sw_id;
15098 		rcu_read_unlock();
15099 		goto err_free;
15100 	}
15101 	rcu_read_unlock();
15102 
15103 	new_fltr->flow_id = flow_id;
15104 	new_fltr->base.rxq = rxq_index;
15105 	rc = bnxt_insert_ntp_filter(bp, new_fltr, idx);
15106 	if (!rc) {
15107 		bnxt_queue_sp_work(bp, BNXT_RX_NTP_FLTR_SP_EVENT);
15108 		return new_fltr->base.sw_id;
15109 	}
15110 
15111 err_free:
15112 	bnxt_del_l2_filter(bp, l2_fltr);
15113 	kfree(new_fltr);
15114 	return rc;
15115 }
15116 #endif
15117 
15118 void bnxt_del_ntp_filter(struct bnxt *bp, struct bnxt_ntuple_filter *fltr)
15119 {
15120 	spin_lock_bh(&bp->ntp_fltr_lock);
15121 	if (!test_and_clear_bit(BNXT_FLTR_INSERTED, &fltr->base.state)) {
15122 		spin_unlock_bh(&bp->ntp_fltr_lock);
15123 		return;
15124 	}
15125 	hlist_del_rcu(&fltr->base.hash);
15126 	bnxt_del_one_usr_fltr(bp, &fltr->base);
15127 	bp->ntp_fltr_count--;
15128 	spin_unlock_bh(&bp->ntp_fltr_lock);
15129 	bnxt_del_l2_filter(bp, fltr->l2_fltr);
15130 	clear_bit(fltr->base.sw_id, bp->ntp_fltr_bmap);
15131 	kfree_rcu(fltr, base.rcu);
15132 }
15133 
15134 static void bnxt_cfg_ntp_filters(struct bnxt *bp)
15135 {
15136 #ifdef CONFIG_RFS_ACCEL
15137 	int i;
15138 
15139 	for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
15140 		struct hlist_head *head;
15141 		struct hlist_node *tmp;
15142 		struct bnxt_ntuple_filter *fltr;
15143 		int rc;
15144 
15145 		head = &bp->ntp_fltr_hash_tbl[i];
15146 		hlist_for_each_entry_safe(fltr, tmp, head, base.hash) {
15147 			bool del = false;
15148 
15149 			if (test_bit(BNXT_FLTR_VALID, &fltr->base.state)) {
15150 				if (fltr->base.flags & BNXT_ACT_NO_AGING)
15151 					continue;
15152 				if (rps_may_expire_flow(bp->dev, fltr->base.rxq,
15153 							fltr->flow_id,
15154 							fltr->base.sw_id)) {
15155 					bnxt_hwrm_cfa_ntuple_filter_free(bp,
15156 									 fltr);
15157 					del = true;
15158 				}
15159 			} else {
15160 				rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
15161 								       fltr);
15162 				if (rc)
15163 					del = true;
15164 				else
15165 					set_bit(BNXT_FLTR_VALID, &fltr->base.state);
15166 			}
15167 
15168 			if (del)
15169 				bnxt_del_ntp_filter(bp, fltr);
15170 		}
15171 	}
15172 #endif
15173 }
15174 
15175 static int bnxt_udp_tunnel_set_port(struct net_device *netdev, unsigned int table,
15176 				    unsigned int entry, struct udp_tunnel_info *ti)
15177 {
15178 	struct bnxt *bp = netdev_priv(netdev);
15179 	unsigned int cmd;
15180 
15181 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15182 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN;
15183 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15184 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE;
15185 	else
15186 		cmd = TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN_GPE;
15187 
15188 	return bnxt_hwrm_tunnel_dst_port_alloc(bp, ti->port, cmd);
15189 }
15190 
15191 static int bnxt_udp_tunnel_unset_port(struct net_device *netdev, unsigned int table,
15192 				      unsigned int entry, struct udp_tunnel_info *ti)
15193 {
15194 	struct bnxt *bp = netdev_priv(netdev);
15195 	unsigned int cmd;
15196 
15197 	if (ti->type == UDP_TUNNEL_TYPE_VXLAN)
15198 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN;
15199 	else if (ti->type == UDP_TUNNEL_TYPE_GENEVE)
15200 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE;
15201 	else
15202 		cmd = TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN_GPE;
15203 
15204 	return bnxt_hwrm_tunnel_dst_port_free(bp, cmd);
15205 }
15206 
15207 static const struct udp_tunnel_nic_info bnxt_udp_tunnels = {
15208 	.set_port	= bnxt_udp_tunnel_set_port,
15209 	.unset_port	= bnxt_udp_tunnel_unset_port,
15210 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15211 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15212 	.tables		= {
15213 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15214 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15215 	},
15216 }, bnxt_udp_tunnels_p7 = {
15217 	.set_port	= bnxt_udp_tunnel_set_port,
15218 	.unset_port	= bnxt_udp_tunnel_unset_port,
15219 	.flags		= UDP_TUNNEL_NIC_INFO_MAY_SLEEP |
15220 			  UDP_TUNNEL_NIC_INFO_OPEN_ONLY,
15221 	.tables		= {
15222 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN,  },
15223 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_GENEVE, },
15224 		{ .n_entries = 1, .tunnel_types = UDP_TUNNEL_TYPE_VXLAN_GPE, },
15225 	},
15226 };
15227 
15228 static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
15229 			       struct net_device *dev, u32 filter_mask,
15230 			       int nlflags)
15231 {
15232 	struct bnxt *bp = netdev_priv(dev);
15233 
15234 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
15235 				       nlflags, filter_mask, NULL);
15236 }
15237 
15238 static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
15239 			       u16 flags, struct netlink_ext_ack *extack)
15240 {
15241 	struct bnxt *bp = netdev_priv(dev);
15242 	struct nlattr *attr, *br_spec;
15243 	int rem, rc = 0;
15244 
15245 	if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
15246 		return -EOPNOTSUPP;
15247 
15248 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
15249 	if (!br_spec)
15250 		return -EINVAL;
15251 
15252 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
15253 		u16 mode;
15254 
15255 		mode = nla_get_u16(attr);
15256 		if (mode == bp->br_mode)
15257 			break;
15258 
15259 		rc = bnxt_hwrm_set_br_mode(bp, mode);
15260 		if (!rc)
15261 			bp->br_mode = mode;
15262 		break;
15263 	}
15264 	return rc;
15265 }
15266 
15267 int bnxt_get_port_parent_id(struct net_device *dev,
15268 			    struct netdev_phys_item_id *ppid)
15269 {
15270 	struct bnxt *bp = netdev_priv(dev);
15271 
15272 	if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
15273 		return -EOPNOTSUPP;
15274 
15275 	/* The PF and it's VF-reps only support the switchdev framework */
15276 	if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_DSN_VALID))
15277 		return -EOPNOTSUPP;
15278 
15279 	ppid->id_len = sizeof(bp->dsn);
15280 	memcpy(ppid->id, bp->dsn, ppid->id_len);
15281 
15282 	return 0;
15283 }
15284 
15285 static const struct net_device_ops bnxt_netdev_ops = {
15286 	.ndo_open		= bnxt_open,
15287 	.ndo_start_xmit		= bnxt_start_xmit,
15288 	.ndo_stop		= bnxt_close,
15289 	.ndo_get_stats64	= bnxt_get_stats64,
15290 	.ndo_set_rx_mode	= bnxt_set_rx_mode,
15291 	.ndo_eth_ioctl		= bnxt_ioctl,
15292 	.ndo_validate_addr	= eth_validate_addr,
15293 	.ndo_set_mac_address	= bnxt_change_mac_addr,
15294 	.ndo_change_mtu		= bnxt_change_mtu,
15295 	.ndo_fix_features	= bnxt_fix_features,
15296 	.ndo_set_features	= bnxt_set_features,
15297 	.ndo_features_check	= bnxt_features_check,
15298 	.ndo_tx_timeout		= bnxt_tx_timeout,
15299 #ifdef CONFIG_BNXT_SRIOV
15300 	.ndo_get_vf_config	= bnxt_get_vf_config,
15301 	.ndo_set_vf_mac		= bnxt_set_vf_mac,
15302 	.ndo_set_vf_vlan	= bnxt_set_vf_vlan,
15303 	.ndo_set_vf_rate	= bnxt_set_vf_bw,
15304 	.ndo_set_vf_link_state	= bnxt_set_vf_link_state,
15305 	.ndo_set_vf_spoofchk	= bnxt_set_vf_spoofchk,
15306 	.ndo_set_vf_trust	= bnxt_set_vf_trust,
15307 #endif
15308 	.ndo_setup_tc           = bnxt_setup_tc,
15309 #ifdef CONFIG_RFS_ACCEL
15310 	.ndo_rx_flow_steer	= bnxt_rx_flow_steer,
15311 #endif
15312 	.ndo_bpf		= bnxt_xdp,
15313 	.ndo_xdp_xmit		= bnxt_xdp_xmit,
15314 	.ndo_bridge_getlink	= bnxt_bridge_getlink,
15315 	.ndo_bridge_setlink	= bnxt_bridge_setlink,
15316 };
15317 
15318 static void bnxt_get_queue_stats_rx(struct net_device *dev, int i,
15319 				    struct netdev_queue_stats_rx *stats)
15320 {
15321 	struct bnxt *bp = netdev_priv(dev);
15322 	struct bnxt_cp_ring_info *cpr;
15323 	u64 *sw;
15324 
15325 	cpr = &bp->bnapi[i]->cp_ring;
15326 	sw = cpr->stats.sw_stats;
15327 
15328 	stats->packets = 0;
15329 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_ucast_pkts);
15330 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_mcast_pkts);
15331 	stats->packets += BNXT_GET_RING_STATS64(sw, rx_bcast_pkts);
15332 
15333 	stats->bytes = 0;
15334 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_ucast_bytes);
15335 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_mcast_bytes);
15336 	stats->bytes += BNXT_GET_RING_STATS64(sw, rx_bcast_bytes);
15337 
15338 	stats->alloc_fail = cpr->sw_stats->rx.rx_oom_discards;
15339 }
15340 
15341 static void bnxt_get_queue_stats_tx(struct net_device *dev, int i,
15342 				    struct netdev_queue_stats_tx *stats)
15343 {
15344 	struct bnxt *bp = netdev_priv(dev);
15345 	struct bnxt_napi *bnapi;
15346 	u64 *sw;
15347 
15348 	bnapi = bp->tx_ring[bp->tx_ring_map[i]].bnapi;
15349 	sw = bnapi->cp_ring.stats.sw_stats;
15350 
15351 	stats->packets = 0;
15352 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_ucast_pkts);
15353 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_mcast_pkts);
15354 	stats->packets += BNXT_GET_RING_STATS64(sw, tx_bcast_pkts);
15355 
15356 	stats->bytes = 0;
15357 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_ucast_bytes);
15358 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_mcast_bytes);
15359 	stats->bytes += BNXT_GET_RING_STATS64(sw, tx_bcast_bytes);
15360 }
15361 
15362 static void bnxt_get_base_stats(struct net_device *dev,
15363 				struct netdev_queue_stats_rx *rx,
15364 				struct netdev_queue_stats_tx *tx)
15365 {
15366 	struct bnxt *bp = netdev_priv(dev);
15367 
15368 	rx->packets = bp->net_stats_prev.rx_packets;
15369 	rx->bytes = bp->net_stats_prev.rx_bytes;
15370 	rx->alloc_fail = bp->ring_err_stats_prev.rx_total_oom_discards;
15371 
15372 	tx->packets = bp->net_stats_prev.tx_packets;
15373 	tx->bytes = bp->net_stats_prev.tx_bytes;
15374 }
15375 
15376 static const struct netdev_stat_ops bnxt_stat_ops = {
15377 	.get_queue_stats_rx	= bnxt_get_queue_stats_rx,
15378 	.get_queue_stats_tx	= bnxt_get_queue_stats_tx,
15379 	.get_base_stats		= bnxt_get_base_stats,
15380 };
15381 
15382 static int bnxt_queue_mem_alloc(struct net_device *dev, void *qmem, int idx)
15383 {
15384 	struct bnxt_rx_ring_info *rxr, *clone;
15385 	struct bnxt *bp = netdev_priv(dev);
15386 	struct bnxt_ring_struct *ring;
15387 	int rc;
15388 
15389 	rxr = &bp->rx_ring[idx];
15390 	clone = qmem;
15391 	memcpy(clone, rxr, sizeof(*rxr));
15392 	bnxt_init_rx_ring_struct(bp, clone);
15393 	bnxt_reset_rx_ring_struct(bp, clone);
15394 
15395 	clone->rx_prod = 0;
15396 	clone->rx_agg_prod = 0;
15397 	clone->rx_sw_agg_prod = 0;
15398 	clone->rx_next_cons = 0;
15399 
15400 	rc = bnxt_alloc_rx_page_pool(bp, clone, rxr->page_pool->p.nid);
15401 	if (rc)
15402 		return rc;
15403 
15404 	rc = xdp_rxq_info_reg(&clone->xdp_rxq, bp->dev, idx, 0);
15405 	if (rc < 0)
15406 		goto err_page_pool_destroy;
15407 
15408 	rc = xdp_rxq_info_reg_mem_model(&clone->xdp_rxq,
15409 					MEM_TYPE_PAGE_POOL,
15410 					clone->page_pool);
15411 	if (rc)
15412 		goto err_rxq_info_unreg;
15413 
15414 	ring = &clone->rx_ring_struct;
15415 	rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15416 	if (rc)
15417 		goto err_free_rx_ring;
15418 
15419 	if (bp->flags & BNXT_FLAG_AGG_RINGS) {
15420 		ring = &clone->rx_agg_ring_struct;
15421 		rc = bnxt_alloc_ring(bp, &ring->ring_mem);
15422 		if (rc)
15423 			goto err_free_rx_agg_ring;
15424 
15425 		rc = bnxt_alloc_rx_agg_bmap(bp, clone);
15426 		if (rc)
15427 			goto err_free_rx_agg_ring;
15428 	}
15429 
15430 	if (bp->flags & BNXT_FLAG_TPA) {
15431 		rc = bnxt_alloc_one_tpa_info(bp, clone);
15432 		if (rc)
15433 			goto err_free_tpa_info;
15434 	}
15435 
15436 	bnxt_init_one_rx_ring_rxbd(bp, clone);
15437 	bnxt_init_one_rx_agg_ring_rxbd(bp, clone);
15438 
15439 	bnxt_alloc_one_rx_ring_skb(bp, clone, idx);
15440 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15441 		bnxt_alloc_one_rx_ring_page(bp, clone, idx);
15442 	if (bp->flags & BNXT_FLAG_TPA)
15443 		bnxt_alloc_one_tpa_info_data(bp, clone);
15444 
15445 	return 0;
15446 
15447 err_free_tpa_info:
15448 	bnxt_free_one_tpa_info(bp, clone);
15449 err_free_rx_agg_ring:
15450 	bnxt_free_ring(bp, &clone->rx_agg_ring_struct.ring_mem);
15451 err_free_rx_ring:
15452 	bnxt_free_ring(bp, &clone->rx_ring_struct.ring_mem);
15453 err_rxq_info_unreg:
15454 	xdp_rxq_info_unreg(&clone->xdp_rxq);
15455 err_page_pool_destroy:
15456 	page_pool_destroy(clone->page_pool);
15457 	if (bnxt_separate_head_pool())
15458 		page_pool_destroy(clone->head_pool);
15459 	clone->page_pool = NULL;
15460 	clone->head_pool = NULL;
15461 	return rc;
15462 }
15463 
15464 static void bnxt_queue_mem_free(struct net_device *dev, void *qmem)
15465 {
15466 	struct bnxt_rx_ring_info *rxr = qmem;
15467 	struct bnxt *bp = netdev_priv(dev);
15468 	struct bnxt_ring_struct *ring;
15469 
15470 	bnxt_free_one_rx_ring_skbs(bp, rxr);
15471 
15472 	xdp_rxq_info_unreg(&rxr->xdp_rxq);
15473 
15474 	page_pool_destroy(rxr->page_pool);
15475 	if (bnxt_separate_head_pool())
15476 		page_pool_destroy(rxr->head_pool);
15477 	rxr->page_pool = NULL;
15478 	rxr->head_pool = NULL;
15479 
15480 	ring = &rxr->rx_ring_struct;
15481 	bnxt_free_ring(bp, &ring->ring_mem);
15482 
15483 	ring = &rxr->rx_agg_ring_struct;
15484 	bnxt_free_ring(bp, &ring->ring_mem);
15485 
15486 	kfree(rxr->rx_agg_bmap);
15487 	rxr->rx_agg_bmap = NULL;
15488 }
15489 
15490 static void bnxt_copy_rx_ring(struct bnxt *bp,
15491 			      struct bnxt_rx_ring_info *dst,
15492 			      struct bnxt_rx_ring_info *src)
15493 {
15494 	struct bnxt_ring_mem_info *dst_rmem, *src_rmem;
15495 	struct bnxt_ring_struct *dst_ring, *src_ring;
15496 	int i;
15497 
15498 	dst_ring = &dst->rx_ring_struct;
15499 	dst_rmem = &dst_ring->ring_mem;
15500 	src_ring = &src->rx_ring_struct;
15501 	src_rmem = &src_ring->ring_mem;
15502 
15503 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15504 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15505 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15506 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15507 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15508 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15509 
15510 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15511 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15512 	*dst_rmem->vmem = *src_rmem->vmem;
15513 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15514 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15515 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15516 	}
15517 
15518 	if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
15519 		return;
15520 
15521 	dst_ring = &dst->rx_agg_ring_struct;
15522 	dst_rmem = &dst_ring->ring_mem;
15523 	src_ring = &src->rx_agg_ring_struct;
15524 	src_rmem = &src_ring->ring_mem;
15525 
15526 	WARN_ON(dst_rmem->nr_pages != src_rmem->nr_pages);
15527 	WARN_ON(dst_rmem->page_size != src_rmem->page_size);
15528 	WARN_ON(dst_rmem->flags != src_rmem->flags);
15529 	WARN_ON(dst_rmem->depth != src_rmem->depth);
15530 	WARN_ON(dst_rmem->vmem_size != src_rmem->vmem_size);
15531 	WARN_ON(dst_rmem->ctx_mem != src_rmem->ctx_mem);
15532 	WARN_ON(dst->rx_agg_bmap_size != src->rx_agg_bmap_size);
15533 
15534 	dst_rmem->pg_tbl = src_rmem->pg_tbl;
15535 	dst_rmem->pg_tbl_map = src_rmem->pg_tbl_map;
15536 	*dst_rmem->vmem = *src_rmem->vmem;
15537 	for (i = 0; i < dst_rmem->nr_pages; i++) {
15538 		dst_rmem->pg_arr[i] = src_rmem->pg_arr[i];
15539 		dst_rmem->dma_arr[i] = src_rmem->dma_arr[i];
15540 	}
15541 
15542 	dst->rx_agg_bmap = src->rx_agg_bmap;
15543 }
15544 
15545 static int bnxt_queue_start(struct net_device *dev, void *qmem, int idx)
15546 {
15547 	struct bnxt *bp = netdev_priv(dev);
15548 	struct bnxt_rx_ring_info *rxr, *clone;
15549 	struct bnxt_cp_ring_info *cpr;
15550 	struct bnxt_vnic_info *vnic;
15551 	int i, rc;
15552 
15553 	rxr = &bp->rx_ring[idx];
15554 	clone = qmem;
15555 
15556 	rxr->rx_prod = clone->rx_prod;
15557 	rxr->rx_agg_prod = clone->rx_agg_prod;
15558 	rxr->rx_sw_agg_prod = clone->rx_sw_agg_prod;
15559 	rxr->rx_next_cons = clone->rx_next_cons;
15560 	rxr->rx_tpa = clone->rx_tpa;
15561 	rxr->rx_tpa_idx_map = clone->rx_tpa_idx_map;
15562 	rxr->page_pool = clone->page_pool;
15563 	rxr->head_pool = clone->head_pool;
15564 	rxr->xdp_rxq = clone->xdp_rxq;
15565 
15566 	bnxt_copy_rx_ring(bp, rxr, clone);
15567 
15568 	rc = bnxt_hwrm_rx_ring_alloc(bp, rxr);
15569 	if (rc)
15570 		return rc;
15571 	rc = bnxt_hwrm_rx_agg_ring_alloc(bp, rxr);
15572 	if (rc)
15573 		goto err_free_hwrm_rx_ring;
15574 
15575 	bnxt_db_write(bp, &rxr->rx_db, rxr->rx_prod);
15576 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15577 		bnxt_db_write(bp, &rxr->rx_agg_db, rxr->rx_agg_prod);
15578 
15579 	cpr = &rxr->bnapi->cp_ring;
15580 	cpr->sw_stats->rx.rx_resets++;
15581 
15582 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15583 		vnic = &bp->vnic_info[i];
15584 
15585 		rc = bnxt_hwrm_vnic_set_rss_p5(bp, vnic, true);
15586 		if (rc) {
15587 			netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %d\n",
15588 				   vnic->vnic_id, rc);
15589 			return rc;
15590 		}
15591 		vnic->mru = bp->dev->mtu + ETH_HLEN + VLAN_HLEN;
15592 		bnxt_hwrm_vnic_update(bp, vnic,
15593 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15594 	}
15595 
15596 	return 0;
15597 
15598 err_free_hwrm_rx_ring:
15599 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15600 	return rc;
15601 }
15602 
15603 static int bnxt_queue_stop(struct net_device *dev, void *qmem, int idx)
15604 {
15605 	struct bnxt *bp = netdev_priv(dev);
15606 	struct bnxt_rx_ring_info *rxr;
15607 	struct bnxt_vnic_info *vnic;
15608 	int i;
15609 
15610 	for (i = 0; i <= BNXT_VNIC_NTUPLE; i++) {
15611 		vnic = &bp->vnic_info[i];
15612 		vnic->mru = 0;
15613 		bnxt_hwrm_vnic_update(bp, vnic,
15614 				      VNIC_UPDATE_REQ_ENABLES_MRU_VALID);
15615 	}
15616 	/* Make sure NAPI sees that the VNIC is disabled */
15617 	synchronize_net();
15618 	rxr = &bp->rx_ring[idx];
15619 	cancel_work_sync(&rxr->bnapi->cp_ring.dim.work);
15620 	bnxt_hwrm_rx_ring_free(bp, rxr, false);
15621 	bnxt_hwrm_rx_agg_ring_free(bp, rxr, false);
15622 	rxr->rx_next_cons = 0;
15623 	page_pool_disable_direct_recycling(rxr->page_pool);
15624 	if (bnxt_separate_head_pool())
15625 		page_pool_disable_direct_recycling(rxr->head_pool);
15626 
15627 	memcpy(qmem, rxr, sizeof(*rxr));
15628 	bnxt_init_rx_ring_struct(bp, qmem);
15629 
15630 	return 0;
15631 }
15632 
15633 static const struct netdev_queue_mgmt_ops bnxt_queue_mgmt_ops = {
15634 	.ndo_queue_mem_size	= sizeof(struct bnxt_rx_ring_info),
15635 	.ndo_queue_mem_alloc	= bnxt_queue_mem_alloc,
15636 	.ndo_queue_mem_free	= bnxt_queue_mem_free,
15637 	.ndo_queue_start	= bnxt_queue_start,
15638 	.ndo_queue_stop		= bnxt_queue_stop,
15639 };
15640 
15641 static void bnxt_remove_one(struct pci_dev *pdev)
15642 {
15643 	struct net_device *dev = pci_get_drvdata(pdev);
15644 	struct bnxt *bp = netdev_priv(dev);
15645 
15646 	if (BNXT_PF(bp))
15647 		bnxt_sriov_disable(bp);
15648 
15649 	bnxt_rdma_aux_device_del(bp);
15650 
15651 	bnxt_ptp_clear(bp);
15652 	unregister_netdev(dev);
15653 
15654 	bnxt_rdma_aux_device_uninit(bp);
15655 
15656 	bnxt_free_l2_filters(bp, true);
15657 	bnxt_free_ntp_fltrs(bp, true);
15658 	WARN_ON(bp->num_rss_ctx);
15659 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
15660 	/* Flush any pending tasks */
15661 	cancel_work_sync(&bp->sp_task);
15662 	cancel_delayed_work_sync(&bp->fw_reset_task);
15663 	bp->sp_event = 0;
15664 
15665 	bnxt_dl_fw_reporters_destroy(bp);
15666 	bnxt_dl_unregister(bp);
15667 	bnxt_shutdown_tc(bp);
15668 
15669 	bnxt_clear_int_mode(bp);
15670 	bnxt_hwrm_func_drv_unrgtr(bp);
15671 	bnxt_free_hwrm_resources(bp);
15672 	bnxt_hwmon_uninit(bp);
15673 	bnxt_ethtool_free(bp);
15674 	bnxt_dcb_free(bp);
15675 	kfree(bp->ptp_cfg);
15676 	bp->ptp_cfg = NULL;
15677 	kfree(bp->fw_health);
15678 	bp->fw_health = NULL;
15679 	bnxt_cleanup_pci(bp);
15680 	bnxt_free_ctx_mem(bp, true);
15681 	bnxt_free_crash_dump_mem(bp);
15682 	kfree(bp->rss_indir_tbl);
15683 	bp->rss_indir_tbl = NULL;
15684 	bnxt_free_port_stats(bp);
15685 	free_netdev(dev);
15686 }
15687 
15688 static int bnxt_probe_phy(struct bnxt *bp, bool fw_dflt)
15689 {
15690 	int rc = 0;
15691 	struct bnxt_link_info *link_info = &bp->link_info;
15692 
15693 	bp->phy_flags = 0;
15694 	rc = bnxt_hwrm_phy_qcaps(bp);
15695 	if (rc) {
15696 		netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
15697 			   rc);
15698 		return rc;
15699 	}
15700 	if (bp->phy_flags & BNXT_PHY_FL_NO_FCS)
15701 		bp->dev->priv_flags |= IFF_SUPP_NOFCS;
15702 	else
15703 		bp->dev->priv_flags &= ~IFF_SUPP_NOFCS;
15704 	if (!fw_dflt)
15705 		return 0;
15706 
15707 	mutex_lock(&bp->link_lock);
15708 	rc = bnxt_update_link(bp, false);
15709 	if (rc) {
15710 		mutex_unlock(&bp->link_lock);
15711 		netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
15712 			   rc);
15713 		return rc;
15714 	}
15715 
15716 	/* Older firmware does not have supported_auto_speeds, so assume
15717 	 * that all supported speeds can be autonegotiated.
15718 	 */
15719 	if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
15720 		link_info->support_auto_speeds = link_info->support_speeds;
15721 
15722 	bnxt_init_ethtool_link_settings(bp);
15723 	mutex_unlock(&bp->link_lock);
15724 	return 0;
15725 }
15726 
15727 static int bnxt_get_max_irq(struct pci_dev *pdev)
15728 {
15729 	u16 ctrl;
15730 
15731 	if (!pdev->msix_cap)
15732 		return 1;
15733 
15734 	pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
15735 	return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
15736 }
15737 
15738 static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15739 				int *max_cp)
15740 {
15741 	struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
15742 	int max_ring_grps = 0, max_irq;
15743 
15744 	*max_tx = hw_resc->max_tx_rings;
15745 	*max_rx = hw_resc->max_rx_rings;
15746 	*max_cp = bnxt_get_max_func_cp_rings_for_en(bp);
15747 	max_irq = min_t(int, bnxt_get_max_func_irqs(bp) -
15748 			bnxt_get_ulp_msix_num_in_use(bp),
15749 			hw_resc->max_stat_ctxs -
15750 			bnxt_get_ulp_stat_ctxs_in_use(bp));
15751 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS))
15752 		*max_cp = min_t(int, *max_cp, max_irq);
15753 	max_ring_grps = hw_resc->max_hw_ring_grps;
15754 	if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
15755 		*max_cp -= 1;
15756 		*max_rx -= 2;
15757 	}
15758 	if (bp->flags & BNXT_FLAG_AGG_RINGS)
15759 		*max_rx >>= 1;
15760 	if (bp->flags & BNXT_FLAG_CHIP_P5_PLUS) {
15761 		int rc;
15762 
15763 		rc = __bnxt_trim_rings(bp, max_rx, max_tx, *max_cp, false);
15764 		if (rc) {
15765 			*max_rx = 0;
15766 			*max_tx = 0;
15767 		}
15768 		/* On P5 chips, max_cp output param should be available NQs */
15769 		*max_cp = max_irq;
15770 	}
15771 	*max_rx = min_t(int, *max_rx, max_ring_grps);
15772 }
15773 
15774 int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
15775 {
15776 	int rx, tx, cp;
15777 
15778 	_bnxt_get_max_rings(bp, &rx, &tx, &cp);
15779 	*max_rx = rx;
15780 	*max_tx = tx;
15781 	if (!rx || !tx || !cp)
15782 		return -ENOMEM;
15783 
15784 	return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
15785 }
15786 
15787 static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
15788 			       bool shared)
15789 {
15790 	int rc;
15791 
15792 	rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15793 	if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
15794 		/* Not enough rings, try disabling agg rings. */
15795 		bp->flags &= ~BNXT_FLAG_AGG_RINGS;
15796 		rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
15797 		if (rc) {
15798 			/* set BNXT_FLAG_AGG_RINGS back for consistency */
15799 			bp->flags |= BNXT_FLAG_AGG_RINGS;
15800 			return rc;
15801 		}
15802 		bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
15803 		bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15804 		bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
15805 		bnxt_set_ring_params(bp);
15806 	}
15807 
15808 	if (bp->flags & BNXT_FLAG_ROCE_CAP) {
15809 		int max_cp, max_stat, max_irq;
15810 
15811 		/* Reserve minimum resources for RoCE */
15812 		max_cp = bnxt_get_max_func_cp_rings(bp);
15813 		max_stat = bnxt_get_max_func_stat_ctxs(bp);
15814 		max_irq = bnxt_get_max_func_irqs(bp);
15815 		if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
15816 		    max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
15817 		    max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
15818 			return 0;
15819 
15820 		max_cp -= BNXT_MIN_ROCE_CP_RINGS;
15821 		max_irq -= BNXT_MIN_ROCE_CP_RINGS;
15822 		max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
15823 		max_cp = min_t(int, max_cp, max_irq);
15824 		max_cp = min_t(int, max_cp, max_stat);
15825 		rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
15826 		if (rc)
15827 			rc = 0;
15828 	}
15829 	return rc;
15830 }
15831 
15832 /* In initial default shared ring setting, each shared ring must have a
15833  * RX/TX ring pair.
15834  */
15835 static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
15836 {
15837 	bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
15838 	bp->rx_nr_rings = bp->cp_nr_rings;
15839 	bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
15840 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15841 }
15842 
15843 static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
15844 {
15845 	int dflt_rings, max_rx_rings, max_tx_rings, rc;
15846 	int avail_msix;
15847 
15848 	if (!bnxt_can_reserve_rings(bp))
15849 		return 0;
15850 
15851 	if (sh)
15852 		bp->flags |= BNXT_FLAG_SHARED_RINGS;
15853 	dflt_rings = is_kdump_kernel() ? 1 : netif_get_num_default_rss_queues();
15854 	/* Reduce default rings on multi-port cards so that total default
15855 	 * rings do not exceed CPU count.
15856 	 */
15857 	if (bp->port_count > 1) {
15858 		int max_rings =
15859 			max_t(int, num_online_cpus() / bp->port_count, 1);
15860 
15861 		dflt_rings = min_t(int, dflt_rings, max_rings);
15862 	}
15863 	rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
15864 	if (rc)
15865 		return rc;
15866 	bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
15867 	bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
15868 	if (sh)
15869 		bnxt_trim_dflt_sh_rings(bp);
15870 	else
15871 		bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
15872 	bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
15873 
15874 	avail_msix = bnxt_get_max_func_irqs(bp) - bp->cp_nr_rings;
15875 	if (avail_msix >= BNXT_MIN_ROCE_CP_RINGS) {
15876 		int ulp_num_msix = min(avail_msix, bp->ulp_num_msix_want);
15877 
15878 		bnxt_set_ulp_msix_num(bp, ulp_num_msix);
15879 		bnxt_set_dflt_ulp_stat_ctxs(bp);
15880 	}
15881 
15882 	rc = __bnxt_reserve_rings(bp);
15883 	if (rc && rc != -ENODEV)
15884 		netdev_warn(bp->dev, "Unable to reserve tx rings\n");
15885 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15886 	if (sh)
15887 		bnxt_trim_dflt_sh_rings(bp);
15888 
15889 	/* Rings may have been trimmed, re-reserve the trimmed rings. */
15890 	if (bnxt_need_reserve_rings(bp)) {
15891 		rc = __bnxt_reserve_rings(bp);
15892 		if (rc && rc != -ENODEV)
15893 			netdev_warn(bp->dev, "2nd rings reservation failed.\n");
15894 		bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15895 	}
15896 	if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
15897 		bp->rx_nr_rings++;
15898 		bp->cp_nr_rings++;
15899 	}
15900 	if (rc) {
15901 		bp->tx_nr_rings = 0;
15902 		bp->rx_nr_rings = 0;
15903 	}
15904 	return rc;
15905 }
15906 
15907 static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
15908 {
15909 	int rc;
15910 
15911 	if (bp->tx_nr_rings)
15912 		return 0;
15913 
15914 	bnxt_ulp_irq_stop(bp);
15915 	bnxt_clear_int_mode(bp);
15916 	rc = bnxt_set_dflt_rings(bp, true);
15917 	if (rc) {
15918 		if (BNXT_VF(bp) && rc == -ENODEV)
15919 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
15920 		else
15921 			netdev_err(bp->dev, "Not enough rings available.\n");
15922 		goto init_dflt_ring_err;
15923 	}
15924 	rc = bnxt_init_int_mode(bp);
15925 	if (rc)
15926 		goto init_dflt_ring_err;
15927 
15928 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
15929 
15930 	bnxt_set_dflt_rfs(bp);
15931 
15932 init_dflt_ring_err:
15933 	bnxt_ulp_irq_restart(bp, rc);
15934 	return rc;
15935 }
15936 
15937 int bnxt_restore_pf_fw_resources(struct bnxt *bp)
15938 {
15939 	int rc;
15940 
15941 	ASSERT_RTNL();
15942 	bnxt_hwrm_func_qcaps(bp);
15943 
15944 	if (netif_running(bp->dev))
15945 		__bnxt_close_nic(bp, true, false);
15946 
15947 	bnxt_ulp_irq_stop(bp);
15948 	bnxt_clear_int_mode(bp);
15949 	rc = bnxt_init_int_mode(bp);
15950 	bnxt_ulp_irq_restart(bp, rc);
15951 
15952 	if (netif_running(bp->dev)) {
15953 		if (rc)
15954 			dev_close(bp->dev);
15955 		else
15956 			rc = bnxt_open_nic(bp, true, false);
15957 	}
15958 
15959 	return rc;
15960 }
15961 
15962 static int bnxt_init_mac_addr(struct bnxt *bp)
15963 {
15964 	int rc = 0;
15965 
15966 	if (BNXT_PF(bp)) {
15967 		eth_hw_addr_set(bp->dev, bp->pf.mac_addr);
15968 	} else {
15969 #ifdef CONFIG_BNXT_SRIOV
15970 		struct bnxt_vf_info *vf = &bp->vf;
15971 		bool strict_approval = true;
15972 
15973 		if (is_valid_ether_addr(vf->mac_addr)) {
15974 			/* overwrite netdev dev_addr with admin VF MAC */
15975 			eth_hw_addr_set(bp->dev, vf->mac_addr);
15976 			/* Older PF driver or firmware may not approve this
15977 			 * correctly.
15978 			 */
15979 			strict_approval = false;
15980 		} else {
15981 			eth_hw_addr_random(bp->dev);
15982 		}
15983 		rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
15984 #endif
15985 	}
15986 	return rc;
15987 }
15988 
15989 static void bnxt_vpd_read_info(struct bnxt *bp)
15990 {
15991 	struct pci_dev *pdev = bp->pdev;
15992 	unsigned int vpd_size, kw_len;
15993 	int pos, size;
15994 	u8 *vpd_data;
15995 
15996 	vpd_data = pci_vpd_alloc(pdev, &vpd_size);
15997 	if (IS_ERR(vpd_data)) {
15998 		pci_warn(pdev, "Unable to read VPD\n");
15999 		return;
16000 	}
16001 
16002 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16003 					   PCI_VPD_RO_KEYWORD_PARTNO, &kw_len);
16004 	if (pos < 0)
16005 		goto read_sn;
16006 
16007 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16008 	memcpy(bp->board_partno, &vpd_data[pos], size);
16009 
16010 read_sn:
16011 	pos = pci_vpd_find_ro_info_keyword(vpd_data, vpd_size,
16012 					   PCI_VPD_RO_KEYWORD_SERIALNO,
16013 					   &kw_len);
16014 	if (pos < 0)
16015 		goto exit;
16016 
16017 	size = min_t(int, kw_len, BNXT_VPD_FLD_LEN - 1);
16018 	memcpy(bp->board_serialno, &vpd_data[pos], size);
16019 exit:
16020 	kfree(vpd_data);
16021 }
16022 
16023 static int bnxt_pcie_dsn_get(struct bnxt *bp, u8 dsn[])
16024 {
16025 	struct pci_dev *pdev = bp->pdev;
16026 	u64 qword;
16027 
16028 	qword = pci_get_dsn(pdev);
16029 	if (!qword) {
16030 		netdev_info(bp->dev, "Unable to read adapter's DSN\n");
16031 		return -EOPNOTSUPP;
16032 	}
16033 
16034 	put_unaligned_le64(qword, dsn);
16035 
16036 	bp->flags |= BNXT_FLAG_DSN_VALID;
16037 	return 0;
16038 }
16039 
16040 static int bnxt_map_db_bar(struct bnxt *bp)
16041 {
16042 	if (!bp->db_size)
16043 		return -ENODEV;
16044 	bp->bar1 = pci_iomap(bp->pdev, 2, bp->db_size);
16045 	if (!bp->bar1)
16046 		return -ENOMEM;
16047 	return 0;
16048 }
16049 
16050 void bnxt_print_device_info(struct bnxt *bp)
16051 {
16052 	netdev_info(bp->dev, "%s found at mem %lx, node addr %pM\n",
16053 		    board_info[bp->board_idx].name,
16054 		    (long)pci_resource_start(bp->pdev, 0), bp->dev->dev_addr);
16055 
16056 	pcie_print_link_status(bp->pdev);
16057 }
16058 
16059 static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
16060 {
16061 	struct bnxt_hw_resc *hw_resc;
16062 	struct net_device *dev;
16063 	struct bnxt *bp;
16064 	int rc, max_irqs;
16065 
16066 	if (pci_is_bridge(pdev))
16067 		return -ENODEV;
16068 
16069 	if (!pdev->msix_cap) {
16070 		dev_err(&pdev->dev, "MSIX capability not found, aborting\n");
16071 		return -ENODEV;
16072 	}
16073 
16074 	/* Clear any pending DMA transactions from crash kernel
16075 	 * while loading driver in capture kernel.
16076 	 */
16077 	if (is_kdump_kernel()) {
16078 		pci_clear_master(pdev);
16079 		pcie_flr(pdev);
16080 	}
16081 
16082 	max_irqs = bnxt_get_max_irq(pdev);
16083 	dev = alloc_etherdev_mqs(sizeof(*bp), max_irqs * BNXT_MAX_QUEUE,
16084 				 max_irqs);
16085 	if (!dev)
16086 		return -ENOMEM;
16087 
16088 	bp = netdev_priv(dev);
16089 	bp->board_idx = ent->driver_data;
16090 	bp->msg_enable = BNXT_DEF_MSG_ENABLE;
16091 	bnxt_set_max_func_irqs(bp, max_irqs);
16092 
16093 	if (bnxt_vf_pciid(bp->board_idx))
16094 		bp->flags |= BNXT_FLAG_VF;
16095 
16096 	/* No devlink port registration in case of a VF */
16097 	if (BNXT_PF(bp))
16098 		SET_NETDEV_DEVLINK_PORT(dev, &bp->dl_port);
16099 
16100 	rc = bnxt_init_board(pdev, dev);
16101 	if (rc < 0)
16102 		goto init_err_free;
16103 
16104 	dev->netdev_ops = &bnxt_netdev_ops;
16105 	dev->stat_ops = &bnxt_stat_ops;
16106 	dev->watchdog_timeo = BNXT_TX_TIMEOUT;
16107 	dev->ethtool_ops = &bnxt_ethtool_ops;
16108 	pci_set_drvdata(pdev, dev);
16109 
16110 	rc = bnxt_alloc_hwrm_resources(bp);
16111 	if (rc)
16112 		goto init_err_pci_clean;
16113 
16114 	mutex_init(&bp->hwrm_cmd_lock);
16115 	mutex_init(&bp->link_lock);
16116 
16117 	rc = bnxt_fw_init_one_p1(bp);
16118 	if (rc)
16119 		goto init_err_pci_clean;
16120 
16121 	if (BNXT_PF(bp))
16122 		bnxt_vpd_read_info(bp);
16123 
16124 	if (BNXT_CHIP_P5_PLUS(bp)) {
16125 		bp->flags |= BNXT_FLAG_CHIP_P5_PLUS;
16126 		if (BNXT_CHIP_P7(bp))
16127 			bp->flags |= BNXT_FLAG_CHIP_P7;
16128 	}
16129 
16130 	rc = bnxt_alloc_rss_indir_tbl(bp);
16131 	if (rc)
16132 		goto init_err_pci_clean;
16133 
16134 	rc = bnxt_fw_init_one_p2(bp);
16135 	if (rc)
16136 		goto init_err_pci_clean;
16137 
16138 	rc = bnxt_map_db_bar(bp);
16139 	if (rc) {
16140 		dev_err(&pdev->dev, "Cannot map doorbell BAR rc = %d, aborting\n",
16141 			rc);
16142 		goto init_err_pci_clean;
16143 	}
16144 
16145 	dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16146 			   NETIF_F_TSO | NETIF_F_TSO6 |
16147 			   NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16148 			   NETIF_F_GSO_IPXIP4 |
16149 			   NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16150 			   NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
16151 			   NETIF_F_RXCSUM | NETIF_F_GRO;
16152 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16153 		dev->hw_features |= NETIF_F_GSO_UDP_L4;
16154 
16155 	if (BNXT_SUPPORTS_TPA(bp))
16156 		dev->hw_features |= NETIF_F_LRO;
16157 
16158 	dev->hw_enc_features =
16159 			NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
16160 			NETIF_F_TSO | NETIF_F_TSO6 |
16161 			NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
16162 			NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
16163 			NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
16164 	if (bp->flags & BNXT_FLAG_UDP_GSO_CAP)
16165 		dev->hw_enc_features |= NETIF_F_GSO_UDP_L4;
16166 	if (bp->flags & BNXT_FLAG_CHIP_P7)
16167 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels_p7;
16168 	else
16169 		dev->udp_tunnel_nic_info = &bnxt_udp_tunnels;
16170 
16171 	dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
16172 				    NETIF_F_GSO_GRE_CSUM;
16173 	dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
16174 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_RX_STRIP)
16175 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_RX;
16176 	if (bp->fw_cap & BNXT_FW_CAP_VLAN_TX_INSERT)
16177 		dev->hw_features |= BNXT_HW_FEATURE_VLAN_ALL_TX;
16178 	if (BNXT_SUPPORTS_TPA(bp))
16179 		dev->hw_features |= NETIF_F_GRO_HW;
16180 	dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
16181 	if (dev->features & NETIF_F_GRO_HW)
16182 		dev->features &= ~NETIF_F_LRO;
16183 	dev->priv_flags |= IFF_UNICAST_FLT;
16184 
16185 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
16186 	if (bp->tso_max_segs)
16187 		netif_set_tso_max_segs(dev, bp->tso_max_segs);
16188 
16189 	dev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
16190 			    NETDEV_XDP_ACT_RX_SG;
16191 
16192 #ifdef CONFIG_BNXT_SRIOV
16193 	init_waitqueue_head(&bp->sriov_cfg_wait);
16194 #endif
16195 	if (BNXT_SUPPORTS_TPA(bp)) {
16196 		bp->gro_func = bnxt_gro_func_5730x;
16197 		if (BNXT_CHIP_P4(bp))
16198 			bp->gro_func = bnxt_gro_func_5731x;
16199 		else if (BNXT_CHIP_P5_PLUS(bp))
16200 			bp->gro_func = bnxt_gro_func_5750x;
16201 	}
16202 	if (!BNXT_CHIP_P4_PLUS(bp))
16203 		bp->flags |= BNXT_FLAG_DOUBLE_DB;
16204 
16205 	rc = bnxt_init_mac_addr(bp);
16206 	if (rc) {
16207 		dev_err(&pdev->dev, "Unable to initialize mac address.\n");
16208 		rc = -EADDRNOTAVAIL;
16209 		goto init_err_pci_clean;
16210 	}
16211 
16212 	if (BNXT_PF(bp)) {
16213 		/* Read the adapter's DSN to use as the eswitch switch_id */
16214 		rc = bnxt_pcie_dsn_get(bp, bp->dsn);
16215 	}
16216 
16217 	/* MTU range: 60 - FW defined max */
16218 	dev->min_mtu = ETH_ZLEN;
16219 	dev->max_mtu = bp->max_mtu;
16220 
16221 	rc = bnxt_probe_phy(bp, true);
16222 	if (rc)
16223 		goto init_err_pci_clean;
16224 
16225 	hw_resc = &bp->hw_resc;
16226 	bp->max_fltr = hw_resc->max_rx_em_flows + hw_resc->max_rx_wm_flows +
16227 		       BNXT_L2_FLTR_MAX_FLTR;
16228 	/* Older firmware may not report these filters properly */
16229 	if (bp->max_fltr < BNXT_MAX_FLTR)
16230 		bp->max_fltr = BNXT_MAX_FLTR;
16231 	bnxt_init_l2_fltr_tbl(bp);
16232 	__bnxt_set_rx_skb_mode(bp, false);
16233 	bnxt_set_tpa_flags(bp);
16234 	bnxt_set_ring_params(bp);
16235 	bnxt_rdma_aux_device_init(bp);
16236 	rc = bnxt_set_dflt_rings(bp, true);
16237 	if (rc) {
16238 		if (BNXT_VF(bp) && rc == -ENODEV) {
16239 			netdev_err(bp->dev, "Cannot configure VF rings while PF is unavailable.\n");
16240 		} else {
16241 			netdev_err(bp->dev, "Not enough rings available.\n");
16242 			rc = -ENOMEM;
16243 		}
16244 		goto init_err_pci_clean;
16245 	}
16246 
16247 	bnxt_fw_init_one_p3(bp);
16248 
16249 	bnxt_init_dflt_coal(bp);
16250 
16251 	if (dev->hw_features & BNXT_HW_FEATURE_VLAN_ALL_RX)
16252 		bp->flags |= BNXT_FLAG_STRIP_VLAN;
16253 
16254 	rc = bnxt_init_int_mode(bp);
16255 	if (rc)
16256 		goto init_err_pci_clean;
16257 
16258 	/* No TC has been set yet and rings may have been trimmed due to
16259 	 * limited MSIX, so we re-initialize the TX rings per TC.
16260 	 */
16261 	bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
16262 
16263 	if (BNXT_PF(bp)) {
16264 		if (!bnxt_pf_wq) {
16265 			bnxt_pf_wq =
16266 				create_singlethread_workqueue("bnxt_pf_wq");
16267 			if (!bnxt_pf_wq) {
16268 				dev_err(&pdev->dev, "Unable to create workqueue.\n");
16269 				rc = -ENOMEM;
16270 				goto init_err_pci_clean;
16271 			}
16272 		}
16273 		rc = bnxt_init_tc(bp);
16274 		if (rc)
16275 			netdev_err(dev, "Failed to initialize TC flower offload, err = %d.\n",
16276 				   rc);
16277 	}
16278 
16279 	bnxt_inv_fw_health_reg(bp);
16280 	rc = bnxt_dl_register(bp);
16281 	if (rc)
16282 		goto init_err_dl;
16283 
16284 	INIT_LIST_HEAD(&bp->usr_fltr_list);
16285 
16286 	if (BNXT_SUPPORTS_NTUPLE_VNIC(bp))
16287 		bp->rss_cap |= BNXT_RSS_CAP_MULTI_RSS_CTX;
16288 	if (BNXT_SUPPORTS_QUEUE_API(bp))
16289 		dev->queue_mgmt_ops = &bnxt_queue_mgmt_ops;
16290 
16291 	rc = register_netdev(dev);
16292 	if (rc)
16293 		goto init_err_cleanup;
16294 
16295 	bnxt_dl_fw_reporters_create(bp);
16296 
16297 	bnxt_rdma_aux_device_add(bp);
16298 
16299 	bnxt_print_device_info(bp);
16300 
16301 	pci_save_state(pdev);
16302 
16303 	return 0;
16304 init_err_cleanup:
16305 	bnxt_rdma_aux_device_uninit(bp);
16306 	bnxt_dl_unregister(bp);
16307 init_err_dl:
16308 	bnxt_shutdown_tc(bp);
16309 	bnxt_clear_int_mode(bp);
16310 
16311 init_err_pci_clean:
16312 	bnxt_hwrm_func_drv_unrgtr(bp);
16313 	bnxt_free_hwrm_resources(bp);
16314 	bnxt_hwmon_uninit(bp);
16315 	bnxt_ethtool_free(bp);
16316 	bnxt_ptp_clear(bp);
16317 	kfree(bp->ptp_cfg);
16318 	bp->ptp_cfg = NULL;
16319 	kfree(bp->fw_health);
16320 	bp->fw_health = NULL;
16321 	bnxt_cleanup_pci(bp);
16322 	bnxt_free_ctx_mem(bp, true);
16323 	bnxt_free_crash_dump_mem(bp);
16324 	kfree(bp->rss_indir_tbl);
16325 	bp->rss_indir_tbl = NULL;
16326 
16327 init_err_free:
16328 	free_netdev(dev);
16329 	return rc;
16330 }
16331 
16332 static void bnxt_shutdown(struct pci_dev *pdev)
16333 {
16334 	struct net_device *dev = pci_get_drvdata(pdev);
16335 	struct bnxt *bp;
16336 
16337 	if (!dev)
16338 		return;
16339 
16340 	rtnl_lock();
16341 	bp = netdev_priv(dev);
16342 	if (!bp)
16343 		goto shutdown_exit;
16344 
16345 	if (netif_running(dev))
16346 		dev_close(dev);
16347 
16348 	bnxt_ptp_clear(bp);
16349 	bnxt_clear_int_mode(bp);
16350 	pci_disable_device(pdev);
16351 
16352 	if (system_state == SYSTEM_POWER_OFF) {
16353 		pci_wake_from_d3(pdev, bp->wol);
16354 		pci_set_power_state(pdev, PCI_D3hot);
16355 	}
16356 
16357 shutdown_exit:
16358 	rtnl_unlock();
16359 }
16360 
16361 #ifdef CONFIG_PM_SLEEP
16362 static int bnxt_suspend(struct device *device)
16363 {
16364 	struct net_device *dev = dev_get_drvdata(device);
16365 	struct bnxt *bp = netdev_priv(dev);
16366 	int rc = 0;
16367 
16368 	bnxt_ulp_stop(bp);
16369 
16370 	rtnl_lock();
16371 	if (netif_running(dev)) {
16372 		netif_device_detach(dev);
16373 		rc = bnxt_close(dev);
16374 	}
16375 	bnxt_hwrm_func_drv_unrgtr(bp);
16376 	bnxt_ptp_clear(bp);
16377 	pci_disable_device(bp->pdev);
16378 	bnxt_free_ctx_mem(bp, false);
16379 	rtnl_unlock();
16380 	return rc;
16381 }
16382 
16383 static int bnxt_resume(struct device *device)
16384 {
16385 	struct net_device *dev = dev_get_drvdata(device);
16386 	struct bnxt *bp = netdev_priv(dev);
16387 	int rc = 0;
16388 
16389 	rtnl_lock();
16390 	rc = pci_enable_device(bp->pdev);
16391 	if (rc) {
16392 		netdev_err(dev, "Cannot re-enable PCI device during resume, err = %d\n",
16393 			   rc);
16394 		goto resume_exit;
16395 	}
16396 	pci_set_master(bp->pdev);
16397 	if (bnxt_hwrm_ver_get(bp)) {
16398 		rc = -ENODEV;
16399 		goto resume_exit;
16400 	}
16401 	rc = bnxt_hwrm_func_reset(bp);
16402 	if (rc) {
16403 		rc = -EBUSY;
16404 		goto resume_exit;
16405 	}
16406 
16407 	rc = bnxt_hwrm_func_qcaps(bp);
16408 	if (rc)
16409 		goto resume_exit;
16410 
16411 	bnxt_clear_reservations(bp, true);
16412 
16413 	if (bnxt_hwrm_func_drv_rgtr(bp, NULL, 0, false)) {
16414 		rc = -ENODEV;
16415 		goto resume_exit;
16416 	}
16417 	if (bp->fw_crash_mem)
16418 		bnxt_hwrm_crash_dump_mem_cfg(bp);
16419 
16420 	if (bnxt_ptp_init(bp)) {
16421 		kfree(bp->ptp_cfg);
16422 		bp->ptp_cfg = NULL;
16423 	}
16424 	bnxt_get_wol_settings(bp);
16425 	if (netif_running(dev)) {
16426 		rc = bnxt_open(dev);
16427 		if (!rc)
16428 			netif_device_attach(dev);
16429 	}
16430 
16431 resume_exit:
16432 	rtnl_unlock();
16433 	bnxt_ulp_start(bp, rc);
16434 	if (!rc)
16435 		bnxt_reenable_sriov(bp);
16436 	return rc;
16437 }
16438 
16439 static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
16440 #define BNXT_PM_OPS (&bnxt_pm_ops)
16441 
16442 #else
16443 
16444 #define BNXT_PM_OPS NULL
16445 
16446 #endif /* CONFIG_PM_SLEEP */
16447 
16448 /**
16449  * bnxt_io_error_detected - called when PCI error is detected
16450  * @pdev: Pointer to PCI device
16451  * @state: The current pci connection state
16452  *
16453  * This function is called after a PCI bus error affecting
16454  * this device has been detected.
16455  */
16456 static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
16457 					       pci_channel_state_t state)
16458 {
16459 	struct net_device *netdev = pci_get_drvdata(pdev);
16460 	struct bnxt *bp = netdev_priv(netdev);
16461 	bool abort = false;
16462 
16463 	netdev_info(netdev, "PCI I/O error detected\n");
16464 
16465 	bnxt_ulp_stop(bp);
16466 
16467 	rtnl_lock();
16468 	netif_device_detach(netdev);
16469 
16470 	if (test_and_set_bit(BNXT_STATE_IN_FW_RESET, &bp->state)) {
16471 		netdev_err(bp->dev, "Firmware reset already in progress\n");
16472 		abort = true;
16473 	}
16474 
16475 	if (abort || state == pci_channel_io_perm_failure) {
16476 		rtnl_unlock();
16477 		return PCI_ERS_RESULT_DISCONNECT;
16478 	}
16479 
16480 	/* Link is not reliable anymore if state is pci_channel_io_frozen
16481 	 * so we disable bus master to prevent any potential bad DMAs before
16482 	 * freeing kernel memory.
16483 	 */
16484 	if (state == pci_channel_io_frozen) {
16485 		set_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state);
16486 		bnxt_fw_fatal_close(bp);
16487 	}
16488 
16489 	if (netif_running(netdev))
16490 		__bnxt_close_nic(bp, true, true);
16491 
16492 	if (pci_is_enabled(pdev))
16493 		pci_disable_device(pdev);
16494 	bnxt_free_ctx_mem(bp, false);
16495 	rtnl_unlock();
16496 
16497 	/* Request a slot slot reset. */
16498 	return PCI_ERS_RESULT_NEED_RESET;
16499 }
16500 
16501 /**
16502  * bnxt_io_slot_reset - called after the pci bus has been reset.
16503  * @pdev: Pointer to PCI device
16504  *
16505  * Restart the card from scratch, as if from a cold-boot.
16506  * At this point, the card has experienced a hard reset,
16507  * followed by fixups by BIOS, and has its config space
16508  * set up identically to what it was at cold boot.
16509  */
16510 static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
16511 {
16512 	pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
16513 	struct net_device *netdev = pci_get_drvdata(pdev);
16514 	struct bnxt *bp = netdev_priv(netdev);
16515 	int retry = 0;
16516 	int err = 0;
16517 	int off;
16518 
16519 	netdev_info(bp->dev, "PCI Slot Reset\n");
16520 
16521 	if (!(bp->flags & BNXT_FLAG_CHIP_P5_PLUS) &&
16522 	    test_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN, &bp->state))
16523 		msleep(900);
16524 
16525 	rtnl_lock();
16526 
16527 	if (pci_enable_device(pdev)) {
16528 		dev_err(&pdev->dev,
16529 			"Cannot re-enable PCI device after reset.\n");
16530 	} else {
16531 		pci_set_master(pdev);
16532 		/* Upon fatal error, our device internal logic that latches to
16533 		 * BAR value is getting reset and will restore only upon
16534 		 * rewriting the BARs.
16535 		 *
16536 		 * As pci_restore_state() does not re-write the BARs if the
16537 		 * value is same as saved value earlier, driver needs to
16538 		 * write the BARs to 0 to force restore, in case of fatal error.
16539 		 */
16540 		if (test_and_clear_bit(BNXT_STATE_PCI_CHANNEL_IO_FROZEN,
16541 				       &bp->state)) {
16542 			for (off = PCI_BASE_ADDRESS_0;
16543 			     off <= PCI_BASE_ADDRESS_5; off += 4)
16544 				pci_write_config_dword(bp->pdev, off, 0);
16545 		}
16546 		pci_restore_state(pdev);
16547 		pci_save_state(pdev);
16548 
16549 		bnxt_inv_fw_health_reg(bp);
16550 		bnxt_try_map_fw_health_reg(bp);
16551 
16552 		/* In some PCIe AER scenarios, firmware may take up to
16553 		 * 10 seconds to become ready in the worst case.
16554 		 */
16555 		do {
16556 			err = bnxt_try_recover_fw(bp);
16557 			if (!err)
16558 				break;
16559 			retry++;
16560 		} while (retry < BNXT_FW_SLOT_RESET_RETRY);
16561 
16562 		if (err) {
16563 			dev_err(&pdev->dev, "Firmware not ready\n");
16564 			goto reset_exit;
16565 		}
16566 
16567 		err = bnxt_hwrm_func_reset(bp);
16568 		if (!err)
16569 			result = PCI_ERS_RESULT_RECOVERED;
16570 
16571 		bnxt_ulp_irq_stop(bp);
16572 		bnxt_clear_int_mode(bp);
16573 		err = bnxt_init_int_mode(bp);
16574 		bnxt_ulp_irq_restart(bp, err);
16575 	}
16576 
16577 reset_exit:
16578 	clear_bit(BNXT_STATE_IN_FW_RESET, &bp->state);
16579 	bnxt_clear_reservations(bp, true);
16580 	rtnl_unlock();
16581 
16582 	return result;
16583 }
16584 
16585 /**
16586  * bnxt_io_resume - called when traffic can start flowing again.
16587  * @pdev: Pointer to PCI device
16588  *
16589  * This callback is called when the error recovery driver tells
16590  * us that its OK to resume normal operation.
16591  */
16592 static void bnxt_io_resume(struct pci_dev *pdev)
16593 {
16594 	struct net_device *netdev = pci_get_drvdata(pdev);
16595 	struct bnxt *bp = netdev_priv(netdev);
16596 	int err;
16597 
16598 	netdev_info(bp->dev, "PCI Slot Resume\n");
16599 	rtnl_lock();
16600 
16601 	err = bnxt_hwrm_func_qcaps(bp);
16602 	if (!err) {
16603 		if (netif_running(netdev))
16604 			err = bnxt_open(netdev);
16605 		else
16606 			err = bnxt_reserve_rings(bp, true);
16607 	}
16608 
16609 	if (!err)
16610 		netif_device_attach(netdev);
16611 
16612 	rtnl_unlock();
16613 	bnxt_ulp_start(bp, err);
16614 	if (!err)
16615 		bnxt_reenable_sriov(bp);
16616 }
16617 
16618 static const struct pci_error_handlers bnxt_err_handler = {
16619 	.error_detected	= bnxt_io_error_detected,
16620 	.slot_reset	= bnxt_io_slot_reset,
16621 	.resume		= bnxt_io_resume
16622 };
16623 
16624 static struct pci_driver bnxt_pci_driver = {
16625 	.name		= DRV_MODULE_NAME,
16626 	.id_table	= bnxt_pci_tbl,
16627 	.probe		= bnxt_init_one,
16628 	.remove		= bnxt_remove_one,
16629 	.shutdown	= bnxt_shutdown,
16630 	.driver.pm	= BNXT_PM_OPS,
16631 	.err_handler	= &bnxt_err_handler,
16632 #if defined(CONFIG_BNXT_SRIOV)
16633 	.sriov_configure = bnxt_sriov_configure,
16634 #endif
16635 };
16636 
16637 static int __init bnxt_init(void)
16638 {
16639 	int err;
16640 
16641 	bnxt_debug_init();
16642 	err = pci_register_driver(&bnxt_pci_driver);
16643 	if (err) {
16644 		bnxt_debug_exit();
16645 		return err;
16646 	}
16647 
16648 	return 0;
16649 }
16650 
16651 static void __exit bnxt_exit(void)
16652 {
16653 	pci_unregister_driver(&bnxt_pci_driver);
16654 	if (bnxt_pf_wq)
16655 		destroy_workqueue(bnxt_pf_wq);
16656 	bnxt_debug_exit();
16657 }
16658 
16659 module_init(bnxt_init);
16660 module_exit(bnxt_exit);
16661